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 | ||||
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG This pass is where algebraic |
12 | // simplification happens. | ||||
Chris Lattner | 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 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +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 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 387 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 388 | char InstCombiner::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 389 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 390 | } |
391 | |||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 392 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 393 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 394 | static unsigned getComplexity(Value *V) { |
395 | if (isa<Instruction>(V)) { | ||||
396 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 397 | return 3; |
398 | return 4; | ||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 399 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 400 | if (isa<Argument>(V)) return 3; |
401 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; | ||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 403 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 404 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
405 | // it. | ||||
406 | static bool isOnlyUse(Value *V) { | ||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 407 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 408 | } |
409 | |||||
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 410 | // getPromotedType - Return the specified type promoted as it would be to pass |
411 | // though a va_arg area... | ||||
412 | static const Type *getPromotedType(const Type *Ty) { | ||||
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 413 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
414 | if (ITy->getBitWidth() < 32) | ||||
415 | return Type::Int32Ty; | ||||
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 416 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 417 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 418 | } |
419 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 420 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
421 | /// expression bitcast, return the operand value, otherwise return null. | ||||
422 | static Value *getBitCastOperand(Value *V) { | ||||
423 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 424 | return I->getOperand(0); |
425 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 426 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 427 | return CE->getOperand(0); |
428 | return 0; | ||||
429 | } | ||||
430 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 431 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
432 | /// simply extracts arguments and returns what that function returns. | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 433 | static Instruction::CastOps |
434 | isEliminableCastPair( | ||||
435 | const CastInst *CI, ///< The first cast instruction | ||||
436 | unsigned opcode, ///< The opcode of the second cast instruction | ||||
437 | const Type *DstTy, ///< The target type for the second cast instruction | ||||
438 | TargetData *TD ///< The target data for pointer size | ||||
439 | ) { | ||||
440 | |||||
441 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above | ||||
442 | const Type *MidTy = CI->getType(); // B from above | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 443 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 444 | // Get the opcodes of the two Cast instructions |
445 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); | ||||
446 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 447 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 448 | return Instruction::CastOps( |
449 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, | ||||
450 | DstTy, TD->getIntPtrType())); | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 451 | } |
452 | |||||
453 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results | ||||
454 | /// in any code being generated. It does not require codegen if V is simple | ||||
455 | /// enough or if the cast can be folded into other casts. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 456 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
457 | const Type *Ty, TargetData *TD) { | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 458 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
459 | |||||
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 460 | // 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] | 461 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 462 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 463 | return false; |
464 | return true; | ||||
465 | } | ||||
466 | |||||
467 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the | ||||
468 | /// InsertBefore instruction. This is specialized a bit to avoid inserting | ||||
469 | /// casts that are known to not do anything... | ||||
470 | /// | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 471 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
472 | Value *V, const Type *DestTy, | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 473 | Instruction *InsertBefore) { |
474 | if (V->getType() == DestTy) return V; | ||||
475 | if (Constant *C = dyn_cast<Constant>(V)) | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 476 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 477 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 478 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 479 | } |
480 | |||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 481 | // SimplifyCommutative - This performs a few simplifications for commutative |
482 | // operators: | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 483 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 484 | // 1. Order operands such that they are listed from right (least complex) to |
485 | // left (most complex). This puts constants before unary operators before | ||||
486 | // binary operators. | ||||
487 | // | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 488 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
489 | // 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] | 490 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 491 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 492 | bool Changed = false; |
493 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) | ||||
494 | Changed = !I.swapOperands(); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 496 | if (!I.isAssociative()) return Changed; |
497 | Instruction::BinaryOps Opcode = I.getOpcode(); | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 498 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
499 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { | ||||
500 | if (isa<Constant>(I.getOperand(1))) { | ||||
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 501 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
502 | cast<Constant>(I.getOperand(1)), | ||||
503 | cast<Constant>(Op->getOperand(1))); | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 504 | I.setOperand(0, Op->getOperand(0)); |
505 | I.setOperand(1, Folded); | ||||
506 | return true; | ||||
507 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) | ||||
508 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && | ||||
509 | isOnlyUse(Op) && isOnlyUse(Op1)) { | ||||
510 | Constant *C1 = cast<Constant>(Op->getOperand(1)); | ||||
511 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); | ||||
512 | |||||
513 | // 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] | 514 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 515 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), |
516 | Op1->getOperand(0), | ||||
517 | Op1->getName(), &I); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 518 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 519 | I.setOperand(0, New); |
520 | I.setOperand(1, Folded); | ||||
521 | return true; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 522 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 523 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 524 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 525 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 526 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 527 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
528 | /// so that theyare listed from right (least complex) to left (most complex). | ||||
529 | /// This puts constants before unary operators before binary operators. | ||||
530 | bool InstCombiner::SimplifyCompare(CmpInst &I) { | ||||
531 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) | ||||
532 | return false; | ||||
533 | I.swapOperands(); | ||||
534 | // Compare instructions are not associative so there's nothing else we can do. | ||||
535 | return true; | ||||
536 | } | ||||
537 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 538 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
539 | // if the LHS is a constant zero (which is the 'negate' form). | ||||
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 540 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 541 | static inline Value *dyn_castNegVal(Value *V) { |
542 | if (BinaryOperator::isNeg(V)) | ||||
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 543 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 545 | // Constants can be considered to be negated values if they can be folded. |
546 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) | ||||
547 | return ConstantExpr::getNeg(C); | ||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 548 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 549 | } |
550 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 551 | static inline Value *dyn_castNotVal(Value *V) { |
552 | if (BinaryOperator::isNot(V)) | ||||
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 553 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 554 | |
555 | // Constants can be considered to be not'ed values... | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 556 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 557 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 558 | return 0; |
559 | } | ||||
560 | |||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 561 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
562 | // other computations (because it has a constant operand), return the | ||||
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 563 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
564 | // Otherwise, return null. | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 565 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 566 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 567 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 568 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 569 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 570 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 571 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 572 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 573 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 574 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 575 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 576 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 577 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 578 | return I->getOperand(0); |
579 | } | ||||
580 | } | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 581 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 582 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 584 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
585 | /// expression, return it. | ||||
586 | static User *dyn_castGetElementPtr(Value *V) { | ||||
587 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); | ||||
588 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | ||||
589 | if (CE->getOpcode() == Instruction::GetElementPtr) | ||||
590 | return cast<User>(V); | ||||
591 | return false; | ||||
592 | } | ||||
593 | |||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 594 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
595 | /// opcode value. Otherwise return UserOp1. | ||||
596 | static unsigned getOpcode(User *U) { | ||||
597 | if (Instruction *I = dyn_cast<Instruction>(U)) | ||||
598 | return I->getOpcode(); | ||||
599 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) | ||||
600 | return CE->getOpcode(); | ||||
601 | // Use UserOp1 to mean there's no opcode. | ||||
602 | return Instruction::UserOp1; | ||||
603 | } | ||||
604 | |||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 605 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 606 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 607 | APInt Val(C->getValue()); |
608 | return ConstantInt::get(++Val); | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 609 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 610 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 611 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 612 | APInt Val(C->getValue()); |
613 | return ConstantInt::get(--Val); | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 614 | } |
615 | /// Add - Add two ConstantInts together | ||||
616 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { | ||||
617 | return ConstantInt::get(C1->getValue() + C2->getValue()); | ||||
618 | } | ||||
619 | /// And - Bitwise AND two ConstantInts together | ||||
620 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { | ||||
621 | return ConstantInt::get(C1->getValue() & C2->getValue()); | ||||
622 | } | ||||
623 | /// Subtract - Subtract one ConstantInt from another | ||||
624 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { | ||||
625 | return ConstantInt::get(C1->getValue() - C2->getValue()); | ||||
626 | } | ||||
627 | /// Multiply - Multiply two ConstantInts together | ||||
628 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { | ||||
629 | return ConstantInt::get(C1->getValue() * C2->getValue()); | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 630 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 631 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
632 | /// this size. | ||||
633 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { | ||||
634 | uint32_t W = C1->getBitWidth(); | ||||
635 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); | ||||
636 | if (sign) { | ||||
637 | LHSExt.sext(W * 2); | ||||
638 | RHSExt.sext(W * 2); | ||||
639 | } else { | ||||
640 | LHSExt.zext(W * 2); | ||||
641 | RHSExt.zext(W * 2); | ||||
642 | } | ||||
643 | |||||
644 | APInt MulExt = LHSExt * RHSExt; | ||||
645 | |||||
646 | if (sign) { | ||||
647 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); | ||||
648 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); | ||||
649 | return MulExt.slt(Min) || MulExt.sgt(Max); | ||||
650 | } else | ||||
651 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); | ||||
652 | } | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 68d5ff2 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 654 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
655 | /// 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] | 656 | /// bit sets. This code only analyzes bits in Mask, in order to short-circuit |
657 | /// processing. | ||||
658 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that | ||||
659 | /// we cannot optimize based on the assumption that it is zero without changing | ||||
660 | /// it to be an explicit zero. If we don't change it to zero, other code could | ||||
661 | /// optimized based on the contradictory assumption that it is non-zero. | ||||
662 | /// Because instcombine aggressively folds operations with undef args anyway, | ||||
663 | /// this won't lose us code quality. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 664 | void InstCombiner::ComputeMaskedBits(Value *V, const APInt &Mask, |
665 | APInt& KnownZero, APInt& KnownOne, | ||||
666 | unsigned Depth) { | ||||
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 667 | assert(V && "No Value?"); |
668 | assert(Depth <= 6 && "Limit Search Depth"); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 669 | uint32_t BitWidth = Mask.getBitWidth(); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 670 | assert((V->getType()->isInteger() || isa<PointerType>(V->getType())) && |
671 | "Not integer or pointer type!"); | ||||
672 | assert((!TD || TD->getTypeSizeInBits(V->getType()) == BitWidth) && | ||||
673 | (!isa<IntegerType>(V->getType()) || | ||||
674 | V->getType()->getPrimitiveSizeInBits() == BitWidth) && | ||||
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 675 | KnownZero.getBitWidth() == BitWidth && |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 676 | KnownOne.getBitWidth() == BitWidth && |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 677 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 678 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
679 | // We know all of the bits for a constant! | ||||
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 680 | KnownOne = CI->getValue() & Mask; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 681 | KnownZero = ~KnownOne & Mask; |
682 | return; | ||||
683 | } | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 684 | // Null is all-zeros. |
685 | if (isa<ConstantPointerNull>(V)) { | ||||
686 | KnownOne.clear(); | ||||
687 | KnownZero = Mask; | ||||
688 | return; | ||||
689 | } | ||||
690 | // The address of an aligned GlobalValue has trailing zeros. | ||||
691 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { | ||||
692 | unsigned Align = GV->getAlignment(); | ||||
693 | if (Align == 0 && TD && GV->getType()->getElementType()->isSized()) | ||||
694 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); | ||||
695 | if (Align > 0) | ||||
696 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, | ||||
697 | CountTrailingZeros_32(Align)); | ||||
698 | else | ||||
699 | KnownZero.clear(); | ||||
700 | KnownOne.clear(); | ||||
701 | return; | ||||
702 | } | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 703 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 704 | KnownZero.clear(); KnownOne.clear(); // Start out not knowing anything. |
705 | |||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 706 | if (Depth == 6 || Mask == 0) |
707 | return; // Limit search depth. | ||||
708 | |||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 709 | User *I = dyn_cast<User>(V); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 710 | if (!I) return; |
711 | |||||
712 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 713 | switch (getOpcode(I)) { |
714 | default: break; | ||||
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 715 | case Instruction::And: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 716 | // If either the LHS or the RHS are Zero, the result is zero. |
717 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); | ||||
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 718 | APInt Mask2(Mask & ~KnownZero); |
719 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 720 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
721 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
722 | |||||
723 | // Output known-1 bits are only known if set in both the LHS & RHS. | ||||
724 | KnownOne &= KnownOne2; | ||||
725 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | ||||
726 | KnownZero |= KnownZero2; | ||||
727 | return; | ||||
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 728 | } |
729 | case Instruction::Or: { | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 730 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 731 | APInt Mask2(Mask & ~KnownOne); |
732 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 733 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
734 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
735 | |||||
736 | // Output known-0 bits are only known if clear in both the LHS & RHS. | ||||
737 | KnownZero &= KnownZero2; | ||||
738 | // Output known-1 are known to be set if set in either the LHS | RHS. | ||||
739 | KnownOne |= KnownOne2; | ||||
740 | return; | ||||
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 741 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 742 | case Instruction::Xor: { |
743 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); | ||||
744 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); | ||||
745 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | ||||
746 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
747 | |||||
748 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | ||||
749 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); | ||||
750 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | ||||
751 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); | ||||
752 | KnownZero = KnownZeroOut; | ||||
753 | return; | ||||
754 | } | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 755 | case Instruction::Mul: { |
756 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); | ||||
757 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero, KnownOne, Depth+1); | ||||
758 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
759 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | ||||
760 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
761 | |||||
762 | // 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] | 763 | // Also compute a conserative estimate for high known-0 bits. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 764 | // More trickiness is possible, but this is sufficient for the |
765 | // interesting case of alignment computation. | ||||
766 | KnownOne.clear(); | ||||
767 | unsigned TrailZ = KnownZero.countTrailingOnes() + | ||||
768 | KnownZero2.countTrailingOnes(); | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 769 | unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + |
770 | KnownZero2.countLeadingOnes() + | ||||
771 | 1, BitWidth) - BitWidth; | ||||
772 | |||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 773 | TrailZ = std::min(TrailZ, BitWidth); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 774 | LeadZ = std::min(LeadZ, BitWidth); |
775 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | | ||||
776 | APInt::getHighBitsSet(BitWidth, LeadZ); | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 777 | KnownZero &= Mask; |
778 | return; | ||||
779 | } | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 780 | case Instruction::UDiv: { |
781 | // For the purposes of computing leading zeros we can conservatively | ||||
782 | // treat a udiv as a logical right shift by the power of 2 known to | ||||
783 | // be greater than the denominator. | ||||
784 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | ||||
785 | ComputeMaskedBits(I->getOperand(0), | ||||
786 | AllOnes, KnownZero2, KnownOne2, Depth+1); | ||||
787 | unsigned LeadZ = KnownZero2.countLeadingOnes(); | ||||
788 | |||||
789 | KnownOne2.clear(); | ||||
790 | KnownZero2.clear(); | ||||
791 | ComputeMaskedBits(I->getOperand(1), | ||||
792 | AllOnes, KnownZero2, KnownOne2, Depth+1); | ||||
793 | LeadZ = std::min(BitWidth, | ||||
794 | LeadZ + BitWidth - KnownOne2.countLeadingZeros()); | ||||
795 | |||||
796 | KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask; | ||||
797 | return; | ||||
798 | } | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 799 | case Instruction::Select: |
800 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); | ||||
801 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); | ||||
802 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | ||||
803 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
804 | |||||
805 | // Only known if known in both the LHS and RHS. | ||||
806 | KnownOne &= KnownOne2; | ||||
807 | KnownZero &= KnownZero2; | ||||
808 | return; | ||||
809 | case Instruction::FPTrunc: | ||||
810 | case Instruction::FPExt: | ||||
811 | case Instruction::FPToUI: | ||||
812 | case Instruction::FPToSI: | ||||
813 | case Instruction::SIToFP: | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 814 | case Instruction::UIToFP: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 815 | return; // Can't work with floating point. |
816 | case Instruction::PtrToInt: | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 817 | case Instruction::IntToPtr: |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 818 | // We can't handle these if we don't know the pointer size. |
819 | if (!TD) return; | ||||
820 | // Fall through and handle them the same as zext/trunc. | ||||
821 | case Instruction::ZExt: | ||||
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 822 | case Instruction::Trunc: { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 823 | // All these have integer operands |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 824 | const Type *SrcTy = I->getOperand(0)->getType(); |
825 | uint32_t SrcBitWidth = TD ? | ||||
826 | TD->getTypeSizeInBits(SrcTy) : | ||||
827 | SrcTy->getPrimitiveSizeInBits(); | ||||
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 828 | APInt MaskIn(Mask); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 829 | MaskIn.zextOrTrunc(SrcBitWidth); |
830 | KnownZero.zextOrTrunc(SrcBitWidth); | ||||
831 | KnownOne.zextOrTrunc(SrcBitWidth); | ||||
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 832 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 833 | KnownZero.zextOrTrunc(BitWidth); |
834 | KnownOne.zextOrTrunc(BitWidth); | ||||
835 | // Any top bits are known to be zero. | ||||
836 | if (BitWidth > SrcBitWidth) | ||||
837 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 838 | return; |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 839 | } |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 840 | case Instruction::BitCast: { |
841 | const Type *SrcTy = I->getOperand(0)->getType(); | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 842 | if (SrcTy->isInteger() || isa<PointerType>(SrcTy)) { |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 843 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); |
844 | return; | ||||
845 | } | ||||
846 | break; | ||||
847 | } | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 848 | case Instruction::SExt: { |
849 | // Compute the bits in the result that are not present in the input. | ||||
850 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | ||||
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 851 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 852 | |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 853 | APInt MaskIn(Mask); |
854 | MaskIn.trunc(SrcBitWidth); | ||||
855 | KnownZero.trunc(SrcBitWidth); | ||||
856 | KnownOne.trunc(SrcBitWidth); | ||||
857 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 858 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 771dbf7 | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 859 | KnownZero.zext(BitWidth); |
860 | KnownOne.zext(BitWidth); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 861 | |
862 | // If the sign bit of the input is known set or clear, then we know the | ||||
863 | // top bits of the result. | ||||
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 864 | if (KnownZero[SrcBitWidth-1]) // Input sign bit known zero |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 865 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 866 | else if (KnownOne[SrcBitWidth-1]) // Input sign bit known set |
Zhou Sheng | 34a4b38 | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 867 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 868 | return; |
869 | } | ||||
870 | case Instruction::Shl: | ||||
871 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 | ||||
872 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 873 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 874 | APInt Mask2(Mask.lshr(ShiftAmt)); |
875 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 876 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Zhou Sheng | 430f626 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 877 | KnownZero <<= ShiftAmt; |
878 | KnownOne <<= ShiftAmt; | ||||
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 879 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 880 | return; |
881 | } | ||||
882 | break; | ||||
883 | case Instruction::LShr: | ||||
884 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 | ||||
885 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
886 | // Compute the new bits that are at the top now. | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 887 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 888 | |
889 | // Unsigned shift right. | ||||
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 890 | APInt Mask2(Mask.shl(ShiftAmt)); |
891 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 892 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
893 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); | ||||
894 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); | ||||
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 895 | // high bits known zero. |
896 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 897 | return; |
898 | } | ||||
899 | break; | ||||
900 | case Instruction::AShr: | ||||
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 901 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 902 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
903 | // Compute the new bits that are at the top now. | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 904 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 905 | |
906 | // Signed shift right. | ||||
Reid Spencer | 2b81207 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 907 | APInt Mask2(Mask.shl(ShiftAmt)); |
908 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 909 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
910 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); | ||||
911 | KnownOne = APIntOps::lshr(KnownOne, ShiftAmt); | ||||
912 | |||||
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 913 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
914 | if (KnownZero[BitWidth-ShiftAmt-1]) // New bits are known zero. | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 915 | KnownZero |= HighBits; |
Zhou Sheng | aa305ab | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 916 | else if (KnownOne[BitWidth-ShiftAmt-1]) // New bits are known one. |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 917 | KnownOne |= HighBits; |
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 918 | return; |
919 | } | ||||
920 | break; | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 921 | case Instruction::Sub: { |
922 | if (ConstantInt *CLHS = dyn_cast<ConstantInt>(I->getOperand(0))) { | ||||
923 | // We know that the top bits of C-X are clear if X contains less bits | ||||
924 | // than C (i.e. no wrap-around can happen). For example, 20-X is | ||||
925 | // positive if we can prove that X is >= 0 and < 16. | ||||
926 | if (!CLHS->getValue().isNegative()) { | ||||
927 | unsigned NLZ = (CLHS->getValue()+1).countLeadingZeros(); | ||||
928 | // NLZ can't be BitWidth with no sign bit | ||||
929 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 930 | ComputeMaskedBits(I->getOperand(1), MaskV, KnownZero2, KnownOne2, |
931 | Depth+1); | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 932 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 933 | // If all of the MaskV bits are known to be zero, then we know the |
934 | // output top bits are zero, because we now know that the output is | ||||
935 | // from [0-C]. | ||||
936 | if ((KnownZero2 & MaskV) == MaskV) { | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 937 | unsigned NLZ2 = CLHS->getValue().countLeadingZeros(); |
938 | // Top bits known zero. | ||||
939 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask; | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 940 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 941 | } |
942 | } | ||||
943 | } | ||||
944 | // fall through | ||||
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 945 | case Instruction::Add: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 946 | // Output known-0 bits are known if clear or set in both the low clear bits |
947 | // common to both LHS & RHS. For example, 8+(X<<3) is known to have the | ||||
948 | // low 3 bits clear. | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 949 | APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes()); |
950 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
951 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
952 | unsigned KnownZeroOut = KnownZero2.countTrailingOnes(); | ||||
953 | |||||
954 | ComputeMaskedBits(I->getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
955 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | ||||
956 | KnownZeroOut = std::min(KnownZeroOut, | ||||
957 | KnownZero2.countTrailingOnes()); | ||||
958 | |||||
959 | KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut); | ||||
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 960 | return; |
Duncan Sands | 1d57a75 | 2008-03-21 08:32:17 +0000 | [diff] [blame] | 961 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 962 | case Instruction::SRem: |
963 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
964 | APInt RA = Rem->getValue(); | ||||
965 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { | ||||
966 | APInt LowBits = RA.isStrictlyPositive() ? ((RA - 1) | RA) : ~RA; | ||||
967 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); | ||||
968 | ComputeMaskedBits(I->getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1); | ||||
969 | |||||
970 | // The sign of a remainder is equal to the sign of the first | ||||
971 | // operand (zero being positive). | ||||
972 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) | ||||
973 | KnownZero2 |= ~LowBits; | ||||
974 | else if (KnownOne2[BitWidth-1]) | ||||
975 | KnownOne2 |= ~LowBits; | ||||
976 | |||||
977 | KnownZero |= KnownZero2 & Mask; | ||||
978 | KnownOne |= KnownOne2 & Mask; | ||||
979 | |||||
980 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | ||||
981 | } | ||||
982 | } | ||||
983 | break; | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 984 | case Instruction::URem: { |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 985 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
986 | APInt RA = Rem->getValue(); | ||||
987 | if (RA.isStrictlyPositive() && RA.isPowerOf2()) { | ||||
988 | APInt LowBits = (RA - 1) | RA; | ||||
989 | APInt Mask2 = LowBits & Mask; | ||||
990 | KnownZero |= ~LowBits & Mask; | ||||
991 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne,Depth+1); | ||||
992 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 993 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 994 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 995 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 996 | |
997 | // Since the result is less than or equal to either operand, any leading | ||||
998 | // zero bits in either operand must also exist in the result. | ||||
999 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | ||||
1000 | ComputeMaskedBits(I->getOperand(0), AllOnes, KnownZero, KnownOne, | ||||
1001 | Depth+1); | ||||
1002 | ComputeMaskedBits(I->getOperand(1), AllOnes, KnownZero2, KnownOne2, | ||||
1003 | Depth+1); | ||||
1004 | |||||
1005 | uint32_t Leaders = std::max(KnownZero.countLeadingOnes(), | ||||
1006 | KnownZero2.countLeadingOnes()); | ||||
1007 | KnownOne.clear(); | ||||
1008 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1009 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1010 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 1011 | |
1012 | case Instruction::Alloca: | ||||
1013 | case Instruction::Malloc: { | ||||
1014 | AllocationInst *AI = cast<AllocationInst>(V); | ||||
1015 | unsigned Align = AI->getAlignment(); | ||||
1016 | if (Align == 0 && TD) { | ||||
1017 | if (isa<AllocaInst>(AI)) | ||||
1018 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); | ||||
1019 | else if (isa<MallocInst>(AI)) { | ||||
1020 | // Malloc returns maximally aligned memory. | ||||
1021 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); | ||||
1022 | Align = | ||||
1023 | std::max(Align, | ||||
1024 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); | ||||
1025 | Align = | ||||
1026 | std::max(Align, | ||||
1027 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); | ||||
1028 | } | ||||
1029 | } | ||||
1030 | |||||
1031 | if (Align > 0) | ||||
1032 | KnownZero = Mask & APInt::getLowBitsSet(BitWidth, | ||||
1033 | CountTrailingZeros_32(Align)); | ||||
1034 | break; | ||||
1035 | } | ||||
1036 | case Instruction::GetElementPtr: { | ||||
1037 | // Analyze all of the subscripts of this getelementptr instruction | ||||
1038 | // to determine if we can prove known low zero bits. | ||||
1039 | APInt LocalMask = APInt::getAllOnesValue(BitWidth); | ||||
1040 | APInt LocalKnownZero(BitWidth, 0), LocalKnownOne(BitWidth, 0); | ||||
1041 | ComputeMaskedBits(I->getOperand(0), LocalMask, | ||||
1042 | LocalKnownZero, LocalKnownOne, Depth+1); | ||||
1043 | unsigned TrailZ = LocalKnownZero.countTrailingOnes(); | ||||
1044 | |||||
1045 | gep_type_iterator GTI = gep_type_begin(I); | ||||
1046 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { | ||||
1047 | Value *Index = I->getOperand(i); | ||||
1048 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
1049 | // Handle struct member offset arithmetic. | ||||
1050 | if (!TD) return; | ||||
1051 | const StructLayout *SL = TD->getStructLayout(STy); | ||||
1052 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); | ||||
1053 | uint64_t Offset = SL->getElementOffset(Idx); | ||||
1054 | TrailZ = std::min(TrailZ, | ||||
1055 | CountTrailingZeros_64(Offset)); | ||||
1056 | } else { | ||||
1057 | // Handle array index arithmetic. | ||||
1058 | const Type *IndexedTy = GTI.getIndexedType(); | ||||
1059 | if (!IndexedTy->isSized()) return; | ||||
1060 | unsigned GEPOpiBits = Index->getType()->getPrimitiveSizeInBits(); | ||||
1061 | uint64_t TypeSize = TD ? TD->getABITypeSize(IndexedTy) : 1; | ||||
1062 | LocalMask = APInt::getAllOnesValue(GEPOpiBits); | ||||
1063 | LocalKnownZero = LocalKnownOne = APInt(GEPOpiBits, 0); | ||||
1064 | ComputeMaskedBits(Index, LocalMask, | ||||
1065 | LocalKnownZero, LocalKnownOne, Depth+1); | ||||
1066 | TrailZ = std::min(TrailZ, | ||||
1067 | CountTrailingZeros_64(TypeSize) + | ||||
1068 | LocalKnownZero.countTrailingOnes()); | ||||
1069 | } | ||||
1070 | } | ||||
1071 | |||||
1072 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) & Mask; | ||||
1073 | break; | ||||
1074 | } | ||||
1075 | case Instruction::PHI: { | ||||
1076 | PHINode *P = cast<PHINode>(I); | ||||
1077 | // Handle the case of a simple two-predecessor recurrence PHI. | ||||
1078 | // There's a lot more that could theoretically be done here, but | ||||
1079 | // this is sufficient to catch some interesting cases. | ||||
1080 | if (P->getNumIncomingValues() == 2) { | ||||
1081 | for (unsigned i = 0; i != 2; ++i) { | ||||
1082 | Value *L = P->getIncomingValue(i); | ||||
1083 | Value *R = P->getIncomingValue(!i); | ||||
1084 | User *LU = dyn_cast<User>(L); | ||||
1085 | unsigned Opcode = LU ? getOpcode(LU) : (unsigned)Instruction::UserOp1; | ||||
1086 | // Check for operations that have the property that if | ||||
1087 | // both their operands have low zero bits, the result | ||||
1088 | // will have low zero bits. | ||||
1089 | if (Opcode == Instruction::Add || | ||||
1090 | Opcode == Instruction::Sub || | ||||
1091 | Opcode == Instruction::And || | ||||
1092 | Opcode == Instruction::Or || | ||||
1093 | Opcode == Instruction::Mul) { | ||||
1094 | Value *LL = LU->getOperand(0); | ||||
1095 | Value *LR = LU->getOperand(1); | ||||
1096 | // Find a recurrence. | ||||
1097 | if (LL == I) | ||||
1098 | L = LR; | ||||
1099 | else if (LR == I) | ||||
1100 | L = LL; | ||||
1101 | else | ||||
1102 | break; | ||||
1103 | // Ok, we have a PHI of the form L op= R. Check for low | ||||
1104 | // zero bits. | ||||
1105 | APInt Mask2 = APInt::getAllOnesValue(BitWidth); | ||||
1106 | ComputeMaskedBits(R, Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
1107 | Mask2 = APInt::getLowBitsSet(BitWidth, | ||||
1108 | KnownZero2.countTrailingOnes()); | ||||
1109 | KnownOne2.clear(); | ||||
1110 | KnownZero2.clear(); | ||||
1111 | ComputeMaskedBits(L, Mask2, KnownZero2, KnownOne2, Depth+1); | ||||
1112 | KnownZero = Mask & | ||||
1113 | APInt::getLowBitsSet(BitWidth, | ||||
1114 | KnownZero2.countTrailingOnes()); | ||||
1115 | break; | ||||
1116 | } | ||||
1117 | } | ||||
1118 | } | ||||
1119 | break; | ||||
1120 | } | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1121 | case Instruction::Call: |
1122 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | ||||
1123 | switch (II->getIntrinsicID()) { | ||||
1124 | default: break; | ||||
1125 | case Intrinsic::ctpop: | ||||
1126 | case Intrinsic::ctlz: | ||||
1127 | case Intrinsic::cttz: { | ||||
1128 | unsigned LowBits = Log2_32(BitWidth)+1; | ||||
1129 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); | ||||
1130 | break; | ||||
1131 | } | ||||
1132 | } | ||||
1133 | } | ||||
1134 | break; | ||||
Reid Spencer | 3e7594f | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 1135 | } |
1136 | } | ||||
1137 | |||||
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1138 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
1139 | /// this predicate to simplify operations downstream. Mask is known to be zero | ||||
1140 | /// for bits that V cannot have. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 1141 | bool InstCombiner::MaskedValueIsZero(Value *V, const APInt& Mask, |
1142 | unsigned Depth) { | ||||
Zhou Sheng | edd089c | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 1143 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 1144 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); |
1145 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | ||||
1146 | return (KnownZero & Mask) == Mask; | ||||
1147 | } | ||||
1148 | |||||
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1149 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
1150 | /// specified instruction is a constant integer. If so, check to see if there | ||||
1151 | /// are any bits set in the constant that are not demanded. If so, shrink the | ||||
1152 | /// constant and return true. | ||||
1153 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, | ||||
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 1154 | APInt Demanded) { |
1155 | assert(I && "No instruction?"); | ||||
1156 | assert(OpNo < I->getNumOperands() && "Operand index too large"); | ||||
1157 | |||||
1158 | // If the operand is not a constant integer, nothing to do. | ||||
1159 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); | ||||
1160 | if (!OpC) return false; | ||||
1161 | |||||
1162 | // If there are no bits set that aren't demanded, nothing to do. | ||||
1163 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); | ||||
1164 | if ((~Demanded & OpC->getValue()) == 0) | ||||
1165 | return false; | ||||
1166 | |||||
1167 | // This instruction is producing bits that are not demanded. Shrink the RHS. | ||||
1168 | Demanded &= OpC->getValue(); | ||||
1169 | I->setOperand(OpNo, ConstantInt::get(Demanded)); | ||||
1170 | return true; | ||||
1171 | } | ||||
1172 | |||||
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1173 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
1174 | // set of known zero and one bits, compute the maximum and minimum values that | ||||
1175 | // could have the specified known zero and known one bits, returning them in | ||||
1176 | // min/max. | ||||
1177 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1178 | const APInt& KnownZero, |
1179 | const APInt& KnownOne, | ||||
1180 | APInt& Min, APInt& Max) { | ||||
1181 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | ||||
1182 | assert(KnownZero.getBitWidth() == BitWidth && | ||||
1183 | KnownOne.getBitWidth() == BitWidth && | ||||
1184 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && | ||||
1185 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1186 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1188 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
1189 | // bit if it is unknown. | ||||
1190 | Min = KnownOne; | ||||
1191 | Max = KnownOne|UnknownBits; | ||||
1192 | |||||
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 1193 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1194 | Min.set(BitWidth-1); |
1195 | Max.clear(BitWidth-1); | ||||
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1196 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1197 | } |
1198 | |||||
1199 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and | ||||
1200 | // a set of known zero and one bits, compute the maximum and minimum values that | ||||
1201 | // could have the specified known zero and known one bits, returning them in | ||||
1202 | // min/max. | ||||
1203 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, | ||||
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1204 | const APInt &KnownZero, |
1205 | const APInt &KnownOne, | ||||
1206 | APInt &Min, APInt &Max) { | ||||
1207 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 1208 | assert(KnownZero.getBitWidth() == BitWidth && |
1209 | KnownOne.getBitWidth() == BitWidth && | ||||
1210 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && | ||||
1211 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1212 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 1213 | |
1214 | // The minimum value is when the unknown bits are all zeros. | ||||
1215 | Min = KnownOne; | ||||
1216 | // The maximum value is when the unknown bits are all ones. | ||||
1217 | Max = KnownOne|UnknownBits; | ||||
1218 | } | ||||
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 1219 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1220 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
1221 | /// value based on the demanded bits. When this function is called, it is known | ||||
1222 | /// that only the bits set in DemandedMask of the result of V are ever used | ||||
1223 | /// downstream. Consequently, depending on the mask and V, it may be possible | ||||
1224 | /// to replace V with a constant or one of its operands. In such cases, this | ||||
1225 | /// function does the replacement and returns true. In all other cases, it | ||||
1226 | /// returns false after analyzing the expression and setting KnownOne and known | ||||
1227 | /// to be one in the expression. KnownZero contains all the bits that are known | ||||
1228 | /// to be zero in the expression. These are provided to potentially allow the | ||||
1229 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify | ||||
1230 | /// the expression. KnownOne and KnownZero always follow the invariant that | ||||
1231 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that | ||||
1232 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set | ||||
1233 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero | ||||
1234 | /// and KnownOne must all be the same. | ||||
1235 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, | ||||
1236 | APInt& KnownZero, APInt& KnownOne, | ||||
1237 | unsigned Depth) { | ||||
1238 | assert(V != 0 && "Null pointer of Value???"); | ||||
1239 | assert(Depth <= 6 && "Limit Search Depth"); | ||||
1240 | uint32_t BitWidth = DemandedMask.getBitWidth(); | ||||
1241 | const IntegerType *VTy = cast<IntegerType>(V->getType()); | ||||
1242 | assert(VTy->getBitWidth() == BitWidth && | ||||
1243 | KnownZero.getBitWidth() == BitWidth && | ||||
1244 | KnownOne.getBitWidth() == BitWidth && | ||||
1245 | "Value *V, DemandedMask, KnownZero and KnownOne \ | ||||
1246 | must have same BitWidth"); | ||||
1247 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | ||||
1248 | // We know all of the bits for a constant! | ||||
1249 | KnownOne = CI->getValue() & DemandedMask; | ||||
1250 | KnownZero = ~KnownOne & DemandedMask; | ||||
1251 | return false; | ||||
1252 | } | ||||
1253 | |||||
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 1254 | KnownZero.clear(); |
1255 | KnownOne.clear(); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1256 | if (!V->hasOneUse()) { // Other users may use these bits. |
1257 | if (Depth != 0) { // Not at the root. | ||||
1258 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. | ||||
1259 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); | ||||
1260 | return false; | ||||
1261 | } | ||||
1262 | // If this is the root being simplified, allow it to have multiple uses, | ||||
1263 | // just set the DemandedMask to all bits. | ||||
1264 | DemandedMask = APInt::getAllOnesValue(BitWidth); | ||||
1265 | } else if (DemandedMask == 0) { // Not demanding any bits from V. | ||||
1266 | if (V != UndefValue::get(VTy)) | ||||
1267 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); | ||||
1268 | return false; | ||||
1269 | } else if (Depth == 6) { // Limit search depth. | ||||
1270 | return false; | ||||
1271 | } | ||||
1272 | |||||
1273 | Instruction *I = dyn_cast<Instruction>(V); | ||||
1274 | if (!I) return false; // Only analyze instructions. | ||||
1275 | |||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1276 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
1277 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; | ||||
1278 | switch (I->getOpcode()) { | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1279 | default: |
1280 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); | ||||
1281 | break; | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1282 | case Instruction::And: |
1283 | // If either the LHS or the RHS are Zero, the result is zero. | ||||
1284 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
1285 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
1286 | return true; | ||||
1287 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1288 | "Bits known to be one AND zero?"); | ||||
1289 | |||||
1290 | // If something is known zero on the RHS, the bits aren't demanded on the | ||||
1291 | // LHS. | ||||
1292 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, | ||||
1293 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1294 | return true; | ||||
1295 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
1296 | "Bits known to be one AND zero?"); | ||||
1297 | |||||
1298 | // If all of the demanded bits are known 1 on one side, return the other. | ||||
1299 | // These bits cannot contribute to the result of the 'and'. | ||||
1300 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == | ||||
1301 | (DemandedMask & ~LHSKnownZero)) | ||||
1302 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
1303 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == | ||||
1304 | (DemandedMask & ~RHSKnownZero)) | ||||
1305 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
1306 | |||||
1307 | // If all of the demanded bits in the inputs are known zeros, return zero. | ||||
1308 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) | ||||
1309 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); | ||||
1310 | |||||
1311 | // If the RHS is a constant, see if we can simplify it. | ||||
1312 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) | ||||
1313 | return UpdateValueUsesWith(I, I); | ||||
1314 | |||||
1315 | // Output known-1 bits are only known if set in both the LHS & RHS. | ||||
1316 | RHSKnownOne &= LHSKnownOne; | ||||
1317 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | ||||
1318 | RHSKnownZero |= LHSKnownZero; | ||||
1319 | break; | ||||
1320 | case Instruction::Or: | ||||
1321 | // If either the LHS or the RHS are One, the result is One. | ||||
1322 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
1323 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
1324 | return true; | ||||
1325 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1326 | "Bits known to be one AND zero?"); | ||||
1327 | // If something is known one on the RHS, the bits aren't demanded on the | ||||
1328 | // LHS. | ||||
1329 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, | ||||
1330 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1331 | return true; | ||||
1332 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
1333 | "Bits known to be one AND zero?"); | ||||
1334 | |||||
1335 | // If all of the demanded bits are known zero on one side, return the other. | ||||
1336 | // These bits cannot contribute to the result of the 'or'. | ||||
1337 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == | ||||
1338 | (DemandedMask & ~LHSKnownOne)) | ||||
1339 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
1340 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == | ||||
1341 | (DemandedMask & ~RHSKnownOne)) | ||||
1342 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
1343 | |||||
1344 | // If all of the potentially set bits on one side are known to be set on | ||||
1345 | // the other side, just use the 'other' side. | ||||
1346 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == | ||||
1347 | (DemandedMask & (~RHSKnownZero))) | ||||
1348 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
1349 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == | ||||
1350 | (DemandedMask & (~LHSKnownZero))) | ||||
1351 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
1352 | |||||
1353 | // If the RHS is a constant, see if we can simplify it. | ||||
1354 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | ||||
1355 | return UpdateValueUsesWith(I, I); | ||||
1356 | |||||
1357 | // Output known-0 bits are only known if clear in both the LHS & RHS. | ||||
1358 | RHSKnownZero &= LHSKnownZero; | ||||
1359 | // Output known-1 are known to be set if set in either the LHS | RHS. | ||||
1360 | RHSKnownOne |= LHSKnownOne; | ||||
1361 | break; | ||||
1362 | case Instruction::Xor: { | ||||
1363 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
1364 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
1365 | return true; | ||||
1366 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1367 | "Bits known to be one AND zero?"); | ||||
1368 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | ||||
1369 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1370 | return true; | ||||
1371 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
1372 | "Bits known to be one AND zero?"); | ||||
1373 | |||||
1374 | // If all of the demanded bits are known zero on one side, return the other. | ||||
1375 | // These bits cannot contribute to the result of the 'xor'. | ||||
1376 | if ((DemandedMask & RHSKnownZero) == DemandedMask) | ||||
1377 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
1378 | if ((DemandedMask & LHSKnownZero) == DemandedMask) | ||||
1379 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
1380 | |||||
1381 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | ||||
1382 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | | ||||
1383 | (RHSKnownOne & LHSKnownOne); | ||||
1384 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | ||||
1385 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | | ||||
1386 | (RHSKnownOne & LHSKnownZero); | ||||
1387 | |||||
1388 | // If all of the demanded bits are known to be zero on one side or the | ||||
1389 | // other, turn this into an *inclusive* or. | ||||
1390 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 | ||||
1391 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { | ||||
1392 | Instruction *Or = | ||||
1393 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), | ||||
1394 | I->getName()); | ||||
1395 | InsertNewInstBefore(Or, *I); | ||||
1396 | return UpdateValueUsesWith(I, Or); | ||||
1397 | } | ||||
1398 | |||||
1399 | // If all of the demanded bits on one side are known, and all of the set | ||||
1400 | // bits on that side are also known to be set on the other side, turn this | ||||
1401 | // into an AND, as we know the bits will be cleared. | ||||
1402 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 | ||||
1403 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { | ||||
1404 | // all known | ||||
1405 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { | ||||
1406 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); | ||||
1407 | Instruction *And = | ||||
1408 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); | ||||
1409 | InsertNewInstBefore(And, *I); | ||||
1410 | return UpdateValueUsesWith(I, And); | ||||
1411 | } | ||||
1412 | } | ||||
1413 | |||||
1414 | // If the RHS is a constant, see if we can simplify it. | ||||
1415 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. | ||||
1416 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | ||||
1417 | return UpdateValueUsesWith(I, I); | ||||
1418 | |||||
1419 | RHSKnownZero = KnownZeroOut; | ||||
1420 | RHSKnownOne = KnownOneOut; | ||||
1421 | break; | ||||
1422 | } | ||||
1423 | case Instruction::Select: | ||||
1424 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, | ||||
1425 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
1426 | return true; | ||||
1427 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
1428 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1429 | return true; | ||||
1430 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1431 | "Bits known to be one AND zero?"); | ||||
1432 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
1433 | "Bits known to be one AND zero?"); | ||||
1434 | |||||
1435 | // If the operands are constants, see if we can simplify them. | ||||
1436 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | ||||
1437 | return UpdateValueUsesWith(I, I); | ||||
1438 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) | ||||
1439 | return UpdateValueUsesWith(I, I); | ||||
1440 | |||||
1441 | // Only known if known in both the LHS and RHS. | ||||
1442 | RHSKnownOne &= LHSKnownOne; | ||||
1443 | RHSKnownZero &= LHSKnownZero; | ||||
1444 | break; | ||||
1445 | case Instruction::Trunc: { | ||||
1446 | uint32_t truncBf = | ||||
1447 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1448 | DemandedMask.zext(truncBf); |
1449 | RHSKnownZero.zext(truncBf); | ||||
1450 | RHSKnownOne.zext(truncBf); | ||||
1451 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | ||||
1452 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1453 | return true; |
1454 | DemandedMask.trunc(BitWidth); | ||||
1455 | RHSKnownZero.trunc(BitWidth); | ||||
1456 | RHSKnownOne.trunc(BitWidth); | ||||
1457 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1458 | "Bits known to be one AND zero?"); | ||||
1459 | break; | ||||
1460 | } | ||||
1461 | case Instruction::BitCast: | ||||
1462 | if (!I->getOperand(0)->getType()->isInteger()) | ||||
1463 | return false; | ||||
1464 | |||||
1465 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | ||||
1466 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
1467 | return true; | ||||
1468 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1469 | "Bits known to be one AND zero?"); | ||||
1470 | break; | ||||
1471 | case Instruction::ZExt: { | ||||
1472 | // Compute the bits in the result that are not present in the input. | ||||
1473 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1474 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1475 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1476 | DemandedMask.trunc(SrcBitWidth); |
1477 | RHSKnownZero.trunc(SrcBitWidth); | ||||
1478 | RHSKnownOne.trunc(SrcBitWidth); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1479 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
1480 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1481 | return true; |
1482 | DemandedMask.zext(BitWidth); | ||||
1483 | RHSKnownZero.zext(BitWidth); | ||||
1484 | RHSKnownOne.zext(BitWidth); | ||||
1485 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1486 | "Bits known to be one AND zero?"); | ||||
1487 | // The top bits are known to be zero. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1488 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1489 | break; |
1490 | } | ||||
1491 | case Instruction::SExt: { | ||||
1492 | // Compute the bits in the result that are not present in the input. | ||||
1493 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1494 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1495 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1496 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1497 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1498 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1499 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1500 | // If any of the sign extended bits are demanded, we know that the sign |
1501 | // bit is demanded. | ||||
1502 | if ((NewBits & DemandedMask) != 0) | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1503 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1504 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1505 | InputDemandedBits.trunc(SrcBitWidth); |
1506 | RHSKnownZero.trunc(SrcBitWidth); | ||||
1507 | RHSKnownOne.trunc(SrcBitWidth); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1508 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
1509 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1510 | return true; |
1511 | InputDemandedBits.zext(BitWidth); | ||||
1512 | RHSKnownZero.zext(BitWidth); | ||||
1513 | RHSKnownOne.zext(BitWidth); | ||||
1514 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1515 | "Bits known to be one AND zero?"); | ||||
1516 | |||||
1517 | // If the sign bit of the input is known set or clear, then we know the | ||||
1518 | // top bits of the result. | ||||
1519 | |||||
1520 | // If the input sign bit is known zero, or if the NewBits are not demanded | ||||
1521 | // convert this into a zero extension. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1522 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1523 | { |
1524 | // Convert to ZExt cast | ||||
1525 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); | ||||
1526 | return UpdateValueUsesWith(I, NewCast); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1527 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1528 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1529 | } |
1530 | break; | ||||
1531 | } | ||||
1532 | case Instruction::Add: { | ||||
1533 | // Figure out what the input bits are. If the top bits of the and result | ||||
1534 | // are not demanded, then the add doesn't demand them from its input | ||||
1535 | // either. | ||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1536 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1537 | |
1538 | // If there is a constant on the RHS, there are a variety of xformations | ||||
1539 | // we can do. | ||||
1540 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
1541 | // If null, this should be simplified elsewhere. Some of the xforms here | ||||
1542 | // won't work if the RHS is zero. | ||||
1543 | if (RHS->isZero()) | ||||
1544 | break; | ||||
1545 | |||||
1546 | // If the top bit of the output is demanded, demand everything from the | ||||
1547 | // input. Otherwise, we demand all the input bits except NLZ top bits. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1548 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1549 | |
1550 | // Find information about known zero/one bits in the input. | ||||
1551 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, | ||||
1552 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1553 | return true; | ||||
1554 | |||||
1555 | // If the RHS of the add has bits set that can't affect the input, reduce | ||||
1556 | // the constant. | ||||
1557 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) | ||||
1558 | return UpdateValueUsesWith(I, I); | ||||
1559 | |||||
1560 | // Avoid excess work. | ||||
1561 | if (LHSKnownZero == 0 && LHSKnownOne == 0) | ||||
1562 | break; | ||||
1563 | |||||
1564 | // Turn it into OR if input bits are zero. | ||||
1565 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { | ||||
1566 | Instruction *Or = | ||||
1567 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), | ||||
1568 | I->getName()); | ||||
1569 | InsertNewInstBefore(Or, *I); | ||||
1570 | return UpdateValueUsesWith(I, Or); | ||||
1571 | } | ||||
1572 | |||||
1573 | // We can say something about the output known-zero and known-one bits, | ||||
1574 | // depending on potential carries from the input constant and the | ||||
1575 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 | ||||
1576 | // bits set and the RHS constant is 0x01001, then we know we have a known | ||||
1577 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. | ||||
1578 | |||||
1579 | // To compute this, we first compute the potential carry bits. These are | ||||
1580 | // the bits which may be modified. I'm not aware of a better way to do | ||||
1581 | // this scan. | ||||
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1582 | const APInt& RHSVal = RHS->getValue(); |
1583 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1584 | |
1585 | // Now that we know which bits have carries, compute the known-1/0 sets. | ||||
1586 | |||||
1587 | // Bits are known one if they are known zero in one operand and one in the | ||||
1588 | // other, and there is no input carry. | ||||
1589 | RHSKnownOne = ((LHSKnownZero & RHSVal) | | ||||
1590 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; | ||||
1591 | |||||
1592 | // Bits are known zero if they are known zero in both operands and there | ||||
1593 | // is no input carry. | ||||
1594 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; | ||||
1595 | } else { | ||||
1596 | // If the high-bits of this ADD are not demanded, then it does not demand | ||||
1597 | // the high bits of its LHS or RHS. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1598 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1599 | // Right fill the mask of bits for this ADD to demand the most |
1600 | // significant bit and all those below it. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1601 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1602 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
1603 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1604 | return true; | ||||
1605 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, | ||||
1606 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1607 | return true; | ||||
1608 | } | ||||
1609 | } | ||||
1610 | break; | ||||
1611 | } | ||||
1612 | case Instruction::Sub: | ||||
1613 | // If the high-bits of this SUB are not demanded, then it does not demand | ||||
1614 | // the high bits of its LHS or RHS. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1615 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1616 | // Right fill the mask of bits for this SUB to demand the most |
1617 | // significant bit and all those below it. | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1618 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1619 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1620 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
1621 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1622 | return true; | ||||
1623 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, | ||||
1624 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1625 | return true; | ||||
1626 | } | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1627 | // Otherwise just hand the sub off to ComputeMaskedBits to fill in |
1628 | // the known zeros and ones. | ||||
1629 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1630 | break; |
1631 | case Instruction::Shl: | ||||
1632 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1633 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1634 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
1635 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1636 | RHSKnownZero, RHSKnownOne, Depth+1)) |
1637 | return true; | ||||
1638 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1639 | "Bits known to be one AND zero?"); | ||||
1640 | RHSKnownZero <<= ShiftAmt; | ||||
1641 | RHSKnownOne <<= ShiftAmt; | ||||
1642 | // low bits known zero. | ||||
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1643 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1644 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1645 | } |
1646 | break; | ||||
1647 | case Instruction::LShr: | ||||
1648 | // For a logical shift right | ||||
1649 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1650 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1651 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1652 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1653 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
1654 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1655 | RHSKnownZero, RHSKnownOne, Depth+1)) |
1656 | return true; | ||||
1657 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1658 | "Bits known to be one AND zero?"); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1659 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
1660 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); | ||||
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1661 | if (ShiftAmt) { |
1662 | // Compute the new bits that are at the top now. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1663 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1664 | RHSKnownZero |= HighBits; // high bits known zero. |
1665 | } | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1666 | } |
1667 | break; | ||||
1668 | case Instruction::AShr: | ||||
1669 | // If this is an arithmetic shift right and only the low-bit is set, we can | ||||
1670 | // always convert this into a logical shr, even if the shift amount is | ||||
1671 | // variable. The low bit of the shift cannot be an input sign bit unless | ||||
1672 | // the shift amount is >= the size of the datatype, which is undefined. | ||||
1673 | if (DemandedMask == 1) { | ||||
1674 | // Perform the logical shift right. | ||||
1675 | Value *NewVal = BinaryOperator::createLShr( | ||||
1676 | I->getOperand(0), I->getOperand(1), I->getName()); | ||||
1677 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); | ||||
1678 | return UpdateValueUsesWith(I, NewVal); | ||||
1679 | } | ||||
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1680 | |
1681 | // If the sign bit is the only bit demanded by this ashr, then there is no | ||||
1682 | // need to do it, the shift doesn't change the high bit. | ||||
1683 | if (DemandedMask.isSignBit()) | ||||
1684 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1685 | |
1686 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1687 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1688 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1689 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1690 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1691 | // If any of the "high bits" are demanded, we should set the sign bit as |
1692 | // demanded. | ||||
1693 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) | ||||
1694 | DemandedMaskIn.set(BitWidth-1); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1695 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1696 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1697 | RHSKnownZero, RHSKnownOne, Depth+1)) |
1698 | return true; | ||||
1699 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1700 | "Bits known to be one AND zero?"); | ||||
1701 | // Compute the new bits that are at the top now. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1702 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1703 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
1704 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); | ||||
1705 | |||||
1706 | // Handle the sign bits. | ||||
1707 | APInt SignBit(APInt::getSignBit(BitWidth)); | ||||
1708 | // Adjust to where it is now in the mask. | ||||
1709 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); | ||||
1710 | |||||
1711 | // If the input sign bit is known to be zero, or if none of the top bits | ||||
1712 | // are demanded, turn this into an unsigned shift right. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1713 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1714 | (HighBits & ~DemandedMask) == HighBits) { |
1715 | // Perform the logical shift right. | ||||
1716 | Value *NewVal = BinaryOperator::createLShr( | ||||
1717 | I->getOperand(0), SA, I->getName()); | ||||
1718 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); | ||||
1719 | return UpdateValueUsesWith(I, NewVal); | ||||
1720 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. | ||||
1721 | RHSKnownOne |= HighBits; | ||||
1722 | } | ||||
1723 | } | ||||
1724 | break; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1725 | case Instruction::SRem: |
1726 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
1727 | APInt RA = Rem->getValue(); | ||||
1728 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { | ||||
1729 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) | RA : ~RA; | ||||
1730 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); | ||||
1731 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, | ||||
1732 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1733 | return true; | ||||
1734 | |||||
1735 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) | ||||
1736 | LHSKnownZero |= ~LowBits; | ||||
1737 | else if (LHSKnownOne[BitWidth-1]) | ||||
1738 | LHSKnownOne |= ~LowBits; | ||||
1739 | |||||
1740 | KnownZero |= LHSKnownZero & DemandedMask; | ||||
1741 | KnownOne |= LHSKnownOne & DemandedMask; | ||||
1742 | |||||
1743 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | ||||
1744 | } | ||||
1745 | } | ||||
1746 | break; | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1747 | case Instruction::URem: { |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1748 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
1749 | APInt RA = Rem->getValue(); | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1750 | if (RA.isStrictlyPositive() && RA.isPowerOf2()) { |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1751 | APInt LowBits = (RA - 1) | RA; |
1752 | APInt Mask2 = LowBits & DemandedMask; | ||||
1753 | KnownZero |= ~LowBits & DemandedMask; | ||||
1754 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, | ||||
1755 | KnownZero, KnownOne, Depth+1)) | ||||
1756 | return true; | ||||
1757 | |||||
1758 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1759 | break; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1760 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1761 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1762 | |
1763 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); | ||||
1764 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | ||||
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1765 | if (SimplifyDemandedBits(I->getOperand(0), AllOnes, |
1766 | KnownZero2, KnownOne2, Depth+1)) | ||||
1767 | return true; | ||||
1768 | |||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1769 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1770 | if (SimplifyDemandedBits(I->getOperand(1), AllOnes, |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1771 | KnownZero2, KnownOne2, Depth+1)) |
1772 | return true; | ||||
1773 | |||||
1774 | Leaders = std::max(Leaders, | ||||
1775 | KnownZero2.countLeadingOnes()); | ||||
1776 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1777 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1778 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1779 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1780 | |
1781 | // If the client is only demanding bits that we know, return the known | ||||
1782 | // constant. | ||||
1783 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) | ||||
1784 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); | ||||
1785 | return false; | ||||
1786 | } | ||||
1787 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1788 | |
1789 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with | ||||
1790 | /// 64 or fewer elements. DemandedElts contains the set of elements that are | ||||
1791 | /// actually used by the caller. This method analyzes which elements of the | ||||
1792 | /// operand are undef and returns that information in UndefElts. | ||||
1793 | /// | ||||
1794 | /// If the information about demanded elements can be used to simplify the | ||||
1795 | /// operation, the operation is simplified, then the resultant value is | ||||
1796 | /// returned. This returns null if no change was made. | ||||
1797 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, | ||||
1798 | uint64_t &UndefElts, | ||||
1799 | unsigned Depth) { | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1800 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1801 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
1802 | uint64_t EltMask = ~0ULL >> (64-VWidth); | ||||
1803 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && | ||||
1804 | "Invalid DemandedElts!"); | ||||
1805 | |||||
1806 | if (isa<UndefValue>(V)) { | ||||
1807 | // If the entire vector is undefined, just return this info. | ||||
1808 | UndefElts = EltMask; | ||||
1809 | return 0; | ||||
1810 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. | ||||
1811 | UndefElts = EltMask; | ||||
1812 | return UndefValue::get(V->getType()); | ||||
1813 | } | ||||
1814 | |||||
1815 | UndefElts = 0; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1816 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
1817 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1818 | Constant *Undef = UndefValue::get(EltTy); |
1819 | |||||
1820 | std::vector<Constant*> Elts; | ||||
1821 | for (unsigned i = 0; i != VWidth; ++i) | ||||
1822 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. | ||||
1823 | Elts.push_back(Undef); | ||||
1824 | UndefElts |= (1ULL << i); | ||||
1825 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. | ||||
1826 | Elts.push_back(Undef); | ||||
1827 | UndefElts |= (1ULL << i); | ||||
1828 | } else { // Otherwise, defined. | ||||
1829 | Elts.push_back(CP->getOperand(i)); | ||||
1830 | } | ||||
1831 | |||||
1832 | // If we changed the constant, return it. | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1833 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1834 | return NewCP != CP ? NewCP : 0; |
1835 | } else if (isa<ConstantAggregateZero>(V)) { | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1836 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1837 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1838 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1839 | Constant *Zero = Constant::getNullValue(EltTy); |
1840 | Constant *Undef = UndefValue::get(EltTy); | ||||
1841 | std::vector<Constant*> Elts; | ||||
1842 | for (unsigned i = 0; i != VWidth; ++i) | ||||
1843 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); | ||||
1844 | UndefElts = DemandedElts ^ EltMask; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1845 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1846 | } |
1847 | |||||
1848 | if (!V->hasOneUse()) { // Other users may use these bits. | ||||
1849 | if (Depth != 0) { // Not at the root. | ||||
1850 | // TODO: Just compute the UndefElts information recursively. | ||||
1851 | return false; | ||||
1852 | } | ||||
1853 | return false; | ||||
1854 | } else if (Depth == 10) { // Limit search depth. | ||||
1855 | return false; | ||||
1856 | } | ||||
1857 | |||||
1858 | Instruction *I = dyn_cast<Instruction>(V); | ||||
1859 | if (!I) return false; // Only analyze instructions. | ||||
1860 | |||||
1861 | bool MadeChange = false; | ||||
1862 | uint64_t UndefElts2; | ||||
1863 | Value *TmpV; | ||||
1864 | switch (I->getOpcode()) { | ||||
1865 | default: break; | ||||
1866 | |||||
1867 | case Instruction::InsertElement: { | ||||
1868 | // If this is a variable index, we don't know which element it overwrites. | ||||
1869 | // demand exactly the same input as we produce. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1870 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1871 | if (Idx == 0) { |
1872 | // Note that we can't propagate undef elt info, because we don't know | ||||
1873 | // which elt is getting updated. | ||||
1874 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, | ||||
1875 | UndefElts2, Depth+1); | ||||
1876 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1877 | break; | ||||
1878 | } | ||||
1879 | |||||
1880 | // If this is inserting an element that isn't demanded, remove this | ||||
1881 | // insertelement. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1882 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1883 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
1884 | return AddSoonDeadInstToWorklist(*I, 0); | ||||
1885 | |||||
1886 | // Otherwise, the element inserted overwrites whatever was there, so the | ||||
1887 | // input demanded set is simpler than the output set. | ||||
1888 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), | ||||
1889 | DemandedElts & ~(1ULL << IdxNo), | ||||
1890 | UndefElts, Depth+1); | ||||
1891 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1892 | |||||
1893 | // The inserted element is defined. | ||||
1894 | UndefElts |= 1ULL << IdxNo; | ||||
1895 | break; | ||||
1896 | } | ||||
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1897 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1898 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1899 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
1900 | if (!VTy) break; | ||||
1901 | unsigned InVWidth = VTy->getNumElements(); | ||||
1902 | uint64_t InputDemandedElts = 0; | ||||
1903 | unsigned Ratio; | ||||
1904 | |||||
1905 | if (VWidth == InVWidth) { | ||||
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1906 | // 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] | 1907 | // elements as are demanded of us. |
1908 | Ratio = 1; | ||||
1909 | InputDemandedElts = DemandedElts; | ||||
1910 | } else if (VWidth > InVWidth) { | ||||
1911 | // Untested so far. | ||||
1912 | break; | ||||
1913 | |||||
1914 | // If there are more elements in the result than there are in the source, | ||||
1915 | // then an input element is live if any of the corresponding output | ||||
1916 | // elements are live. | ||||
1917 | Ratio = VWidth/InVWidth; | ||||
1918 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { | ||||
1919 | if (DemandedElts & (1ULL << OutIdx)) | ||||
1920 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); | ||||
1921 | } | ||||
1922 | } else { | ||||
1923 | // Untested so far. | ||||
1924 | break; | ||||
1925 | |||||
1926 | // If there are more elements in the source than there are in the result, | ||||
1927 | // then an input element is live if the corresponding output element is | ||||
1928 | // live. | ||||
1929 | Ratio = InVWidth/VWidth; | ||||
1930 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) | ||||
1931 | if (DemandedElts & (1ULL << InIdx/Ratio)) | ||||
1932 | InputDemandedElts |= 1ULL << InIdx; | ||||
1933 | } | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1935 | // div/rem demand all inputs, because they don't want divide by zero. |
1936 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, | ||||
1937 | UndefElts2, Depth+1); | ||||
1938 | if (TmpV) { | ||||
1939 | I->setOperand(0, TmpV); | ||||
1940 | MadeChange = true; | ||||
1941 | } | ||||
1942 | |||||
1943 | UndefElts = UndefElts2; | ||||
1944 | if (VWidth > InVWidth) { | ||||
1945 | assert(0 && "Unimp"); | ||||
1946 | // If there are more elements in the result than there are in the source, | ||||
1947 | // then an output element is undef if the corresponding input element is | ||||
1948 | // undef. | ||||
1949 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) | ||||
1950 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) | ||||
1951 | UndefElts |= 1ULL << OutIdx; | ||||
1952 | } else if (VWidth < InVWidth) { | ||||
1953 | assert(0 && "Unimp"); | ||||
1954 | // If there are more elements in the source than there are in the result, | ||||
1955 | // then a result element is undef if all of the corresponding input | ||||
1956 | // elements are undef. | ||||
1957 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. | ||||
1958 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) | ||||
1959 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? | ||||
1960 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. | ||||
1961 | } | ||||
1962 | break; | ||||
1963 | } | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1964 | case Instruction::And: |
1965 | case Instruction::Or: | ||||
1966 | case Instruction::Xor: | ||||
1967 | case Instruction::Add: | ||||
1968 | case Instruction::Sub: | ||||
1969 | case Instruction::Mul: | ||||
1970 | // div/rem demand all inputs, because they don't want divide by zero. | ||||
1971 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, | ||||
1972 | UndefElts, Depth+1); | ||||
1973 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1974 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, | ||||
1975 | UndefElts2, Depth+1); | ||||
1976 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } | ||||
1977 | |||||
1978 | // Output elements are undefined if both are undefined. Consider things | ||||
1979 | // like undef&0. The result is known zero, not undef. | ||||
1980 | UndefElts &= UndefElts2; | ||||
1981 | break; | ||||
1982 | |||||
1983 | case Instruction::Call: { | ||||
1984 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); | ||||
1985 | if (!II) break; | ||||
1986 | switch (II->getIntrinsicID()) { | ||||
1987 | default: break; | ||||
1988 | |||||
1989 | // Binary vector operations that work column-wise. A dest element is a | ||||
1990 | // function of the corresponding input elements from the two inputs. | ||||
1991 | case Intrinsic::x86_sse_sub_ss: | ||||
1992 | case Intrinsic::x86_sse_mul_ss: | ||||
1993 | case Intrinsic::x86_sse_min_ss: | ||||
1994 | case Intrinsic::x86_sse_max_ss: | ||||
1995 | case Intrinsic::x86_sse2_sub_sd: | ||||
1996 | case Intrinsic::x86_sse2_mul_sd: | ||||
1997 | case Intrinsic::x86_sse2_min_sd: | ||||
1998 | case Intrinsic::x86_sse2_max_sd: | ||||
1999 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, | ||||
2000 | UndefElts, Depth+1); | ||||
2001 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } | ||||
2002 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, | ||||
2003 | UndefElts2, Depth+1); | ||||
2004 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } | ||||
2005 | |||||
2006 | // If only the low elt is demanded and this is a scalarizable intrinsic, | ||||
2007 | // scalarize it now. | ||||
2008 | if (DemandedElts == 1) { | ||||
2009 | switch (II->getIntrinsicID()) { | ||||
2010 | default: break; | ||||
2011 | case Intrinsic::x86_sse_sub_ss: | ||||
2012 | case Intrinsic::x86_sse_mul_ss: | ||||
2013 | case Intrinsic::x86_sse2_sub_sd: | ||||
2014 | case Intrinsic::x86_sse2_mul_sd: | ||||
2015 | // TODO: Lower MIN/MAX/ABS/etc | ||||
2016 | Value *LHS = II->getOperand(1); | ||||
2017 | Value *RHS = II->getOperand(2); | ||||
2018 | // Extract the element as scalars. | ||||
2019 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); | ||||
2020 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); | ||||
2021 | |||||
2022 | switch (II->getIntrinsicID()) { | ||||
2023 | default: assert(0 && "Case stmts out of sync!"); | ||||
2024 | case Intrinsic::x86_sse_sub_ss: | ||||
2025 | case Intrinsic::x86_sse2_sub_sd: | ||||
2026 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, | ||||
2027 | II->getName()), *II); | ||||
2028 | break; | ||||
2029 | case Intrinsic::x86_sse_mul_ss: | ||||
2030 | case Intrinsic::x86_sse2_mul_sd: | ||||
2031 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, | ||||
2032 | II->getName()), *II); | ||||
2033 | break; | ||||
2034 | } | ||||
2035 | |||||
2036 | Instruction *New = | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2037 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
2038 | II->getName()); | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 2039 | InsertNewInstBefore(New, *II); |
2040 | AddSoonDeadInstToWorklist(*II, 0); | ||||
2041 | return New; | ||||
2042 | } | ||||
2043 | } | ||||
2044 | |||||
2045 | // Output elements are undefined if both are undefined. Consider things | ||||
2046 | // like undef&0. The result is known zero, not undef. | ||||
2047 | UndefElts &= UndefElts2; | ||||
2048 | break; | ||||
2049 | } | ||||
2050 | break; | ||||
2051 | } | ||||
2052 | } | ||||
2053 | return MadeChange ? I : 0; | ||||
2054 | } | ||||
2055 | |||||
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2056 | /// @returns true if the specified compare predicate is |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2057 | /// true when both operands are equal... |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2058 | /// @brief Determine if the icmp Predicate is true when both operands are equal |
2059 | static bool isTrueWhenEqual(ICmpInst::Predicate pred) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2060 | return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE || |
2061 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || | ||||
2062 | pred == ICmpInst::ICMP_SLE; | ||||
2063 | } | ||||
2064 | |||||
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 2065 | /// @returns true if the specified compare instruction is |
2066 | /// true when both operands are equal... | ||||
2067 | /// @brief Determine if the ICmpInst returns true when both operands are equal | ||||
2068 | static bool isTrueWhenEqual(ICmpInst &ICI) { | ||||
2069 | return isTrueWhenEqual(ICI.getPredicate()); | ||||
2070 | } | ||||
2071 | |||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2072 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
2073 | /// function is designed to check a chain of associative operators for a | ||||
2074 | /// potential to apply a certain optimization. Since the optimization may be | ||||
2075 | /// applicable if the expression was reassociated, this checks the chain, then | ||||
2076 | /// reassociates the expression as necessary to expose the optimization | ||||
2077 | /// opportunity. This makes use of a special Functor, which must define | ||||
2078 | /// 'shouldApply' and 'apply' methods. | ||||
2079 | /// | ||||
2080 | template<typename Functor> | ||||
2081 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { | ||||
2082 | unsigned Opcode = Root.getOpcode(); | ||||
2083 | Value *LHS = Root.getOperand(0); | ||||
2084 | |||||
2085 | // Quick check, see if the immediate LHS matches... | ||||
2086 | if (F.shouldApply(LHS)) | ||||
2087 | return F.apply(Root); | ||||
2088 | |||||
2089 | // Otherwise, if the LHS is not of the same opcode as the root, return. | ||||
2090 | Instruction *LHSI = dyn_cast<Instruction>(LHS); | ||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2091 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2092 | // Should we apply this transform to the RHS? |
2093 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); | ||||
2094 | |||||
2095 | // If not to the RHS, check to see if we should apply to the LHS... | ||||
2096 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { | ||||
2097 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS | ||||
2098 | ShouldApply = true; | ||||
2099 | } | ||||
2100 | |||||
2101 | // If the functor wants to apply the optimization to the RHS of LHSI, | ||||
2102 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) | ||||
2103 | if (ShouldApply) { | ||||
2104 | BasicBlock *BB = Root.getParent(); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2106 | // Now all of the instructions are in the current basic block, go ahead |
2107 | // and perform the reassociation. | ||||
2108 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); | ||||
2109 | |||||
2110 | // First move the selected RHS to the LHS of the root... | ||||
2111 | Root.setOperand(0, LHSI->getOperand(1)); | ||||
2112 | |||||
2113 | // Make what used to be the LHS of the root be the user of the root... | ||||
2114 | Value *ExtraOperand = TmpLHSI->getOperand(1); | ||||
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2115 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 2116 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
2117 | return 0; | ||||
2118 | } | ||||
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2119 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2120 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2121 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); |
2122 | BasicBlock::iterator ARI = &Root; ++ARI; | ||||
2123 | BB->getInstList().insert(ARI, TmpLHSI); // Move TmpLHSI to after Root | ||||
2124 | ARI = Root; | ||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2125 | |
2126 | // Now propagate the ExtraOperand down the chain of instructions until we | ||||
2127 | // get to LHSI. | ||||
2128 | while (TmpLHSI != LHSI) { | ||||
2129 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); | ||||
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 2130 | // Move the instruction to immediately before the chain we are |
2131 | // constructing to avoid breaking dominance properties. | ||||
2132 | NextLHSI->getParent()->getInstList().remove(NextLHSI); | ||||
2133 | BB->getInstList().insert(ARI, NextLHSI); | ||||
2134 | ARI = NextLHSI; | ||||
2135 | |||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2136 | Value *NextOp = NextLHSI->getOperand(1); |
2137 | NextLHSI->setOperand(1, ExtraOperand); | ||||
2138 | TmpLHSI = NextLHSI; | ||||
2139 | ExtraOperand = NextOp; | ||||
2140 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2141 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2142 | // Now that the instructions are reassociated, have the functor perform |
2143 | // the transformation... | ||||
2144 | return F.apply(Root); | ||||
2145 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2147 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
2148 | } | ||||
2149 | return 0; | ||||
2150 | } | ||||
2151 | |||||
2152 | |||||
2153 | // AddRHS - Implements: X + X --> X << 1 | ||||
2154 | struct AddRHS { | ||||
2155 | Value *RHS; | ||||
2156 | AddRHS(Value *rhs) : RHS(rhs) {} | ||||
2157 | bool shouldApply(Value *LHS) const { return LHS == RHS; } | ||||
2158 | Instruction *apply(BinaryOperator &Add) const { | ||||
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2159 | return BinaryOperator::createShl(Add.getOperand(0), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2160 | ConstantInt::get(Add.getType(), 1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2161 | } |
2162 | }; | ||||
2163 | |||||
2164 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) | ||||
2165 | // iff C1&C2 == 0 | ||||
2166 | struct AddMaskingAnd { | ||||
2167 | Constant *C2; | ||||
2168 | AddMaskingAnd(Constant *c) : C2(c) {} | ||||
2169 | bool shouldApply(Value *LHS) const { | ||||
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2170 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2171 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2172 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2173 | } |
2174 | Instruction *apply(BinaryOperator &Add) const { | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2175 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2176 | } |
2177 | }; | ||||
2178 | |||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2179 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2180 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2181 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2182 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2183 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2184 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2185 | return IC->InsertNewInstBefore(CastInst::create( |
2186 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2187 | } |
2188 | |||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2189 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2190 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
2191 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); | ||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2192 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2193 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
2194 | if (ConstIsRHS) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2195 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
2196 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); | ||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2197 | } |
2198 | |||||
2199 | Value *Op0 = SO, *Op1 = ConstOperand; | ||||
2200 | if (!ConstIsRHS) | ||||
2201 | std::swap(Op0, Op1); | ||||
2202 | Instruction *New; | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2203 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
2204 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2205 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
2206 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, | ||||
2207 | SO->getName()+".cmp"); | ||||
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2208 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2209 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 2210 | abort(); |
2211 | } | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2212 | return IC->InsertNewInstBefore(New, I); |
2213 | } | ||||
2214 | |||||
2215 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a | ||||
2216 | // constant as the other operand, try to fold the binary operator into the | ||||
2217 | // select arguments. This also works for Cast instructions, which obviously do | ||||
2218 | // not have a second operand. | ||||
2219 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, | ||||
2220 | InstCombiner *IC) { | ||||
2221 | // Don't modify shared select instructions | ||||
2222 | if (!SI->hasOneUse()) return 0; | ||||
2223 | Value *TV = SI->getOperand(1); | ||||
2224 | Value *FV = SI->getOperand(2); | ||||
2225 | |||||
2226 | if (isa<Constant>(TV) || isa<Constant>(FV)) { | ||||
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2227 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2228 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2230 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
2231 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); | ||||
2232 | |||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2233 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
2234 | SelectFalseVal); | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2235 | } |
2236 | return 0; | ||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2237 | } |
2238 | |||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2239 | |
2240 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI | ||||
2241 | /// node as operand #0, see if we can fold the instruction into the PHI (which | ||||
2242 | /// is only possible if all operands to the PHI are constants). | ||||
2243 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { | ||||
2244 | PHINode *PN = cast<PHINode>(I.getOperand(0)); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2245 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2246 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2248 | // Check to see if all of the operands of the PHI are constants. If there is |
2249 | // 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] | 2250 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2251 | BasicBlock *NonConstBB = 0; |
2252 | for (unsigned i = 0; i != NumPHIValues; ++i) | ||||
2253 | if (!isa<Constant>(PN->getIncomingValue(i))) { | ||||
2254 | if (NonConstBB) return 0; // More than one non-const value. | ||||
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 2255 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2256 | NonConstBB = PN->getIncomingBlock(i); |
2257 | |||||
2258 | // If the incoming non-constant value is in I's block, we have an infinite | ||||
2259 | // loop. | ||||
2260 | if (NonConstBB == I.getParent()) | ||||
2261 | return 0; | ||||
2262 | } | ||||
2263 | |||||
2264 | // If there is exactly one non-constant value, we can insert a copy of the | ||||
2265 | // operation in that block. However, if this is a critical edge, we would be | ||||
2266 | // inserting the computation one some other paths (e.g. inside a loop). Only | ||||
2267 | // do this if the pred block is unconditionally branching into the phi block. | ||||
2268 | if (NonConstBB) { | ||||
2269 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); | ||||
2270 | if (!BI || !BI->isUnconditional()) return 0; | ||||
2271 | } | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2272 | |
2273 | // Okay, we can do the transformation: create the new PHI node. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2274 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 2275 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2276 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2277 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2278 | |
2279 | // Next, add all of the operands to the PHI. | ||||
2280 | if (I.getNumOperands() == 2) { | ||||
2281 | Constant *C = cast<Constant>(I.getOperand(1)); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2282 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 2283 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2284 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2285 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
2286 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); | ||||
2287 | else | ||||
2288 | InV = ConstantExpr::get(I.getOpcode(), InC, C); | ||||
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2289 | } else { |
2290 | assert(PN->getIncomingBlock(i) == NonConstBB); | ||||
2291 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) | ||||
2292 | InV = BinaryOperator::create(BO->getOpcode(), | ||||
2293 | PN->getIncomingValue(i), C, "phitmp", | ||||
2294 | NonConstBB->getTerminator()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2295 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
2296 | InV = CmpInst::create(CI->getOpcode(), | ||||
2297 | CI->getPredicate(), | ||||
2298 | PN->getIncomingValue(i), C, "phitmp", | ||||
2299 | NonConstBB->getTerminator()); | ||||
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2300 | else |
2301 | assert(0 && "Unknown binop!"); | ||||
2302 | |||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2303 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2304 | } |
2305 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2306 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2307 | } else { |
2308 | CastInst *CI = cast<CastInst>(&I); | ||||
2309 | const Type *RetTy = CI->getType(); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 2310 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2311 | Value *InV; |
2312 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2313 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2314 | } else { |
2315 | assert(PN->getIncomingBlock(i) == NonConstBB); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2316 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), |
2317 | I.getType(), "phitmp", | ||||
2318 | NonConstBB->getTerminator()); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 2319 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 2320 | } |
2321 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2322 | } |
2323 | } | ||||
2324 | return ReplaceInstUsesWith(I, NewPN); | ||||
2325 | } | ||||
2326 | |||||
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2327 | |
2328 | /// CannotBeNegativeZero - Return true if we can prove that the specified FP | ||||
2329 | /// value is never equal to -0.0. | ||||
2330 | /// | ||||
2331 | /// Note that this function will need to be revisited when we support nondefault | ||||
2332 | /// rounding modes! | ||||
2333 | /// | ||||
2334 | static bool CannotBeNegativeZero(const Value *V) { | ||||
2335 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) | ||||
2336 | return !CFP->getValueAPF().isNegZero(); | ||||
2337 | |||||
2338 | // (add x, 0.0) is guaranteed to return +0.0, not -0.0. | ||||
2339 | if (const Instruction *I = dyn_cast<Instruction>(V)) { | ||||
2340 | if (I->getOpcode() == Instruction::Add && | ||||
2341 | isa<ConstantFP>(I->getOperand(1)) && | ||||
2342 | cast<ConstantFP>(I->getOperand(1))->isNullValue()) | ||||
2343 | return true; | ||||
2344 | |||||
2345 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) | ||||
2346 | if (II->getIntrinsicID() == Intrinsic::sqrt) | ||||
2347 | return CannotBeNegativeZero(II->getOperand(1)); | ||||
2348 | |||||
2349 | if (const CallInst *CI = dyn_cast<CallInst>(I)) | ||||
2350 | if (const Function *F = CI->getCalledFunction()) { | ||||
2351 | if (F->isDeclaration()) { | ||||
2352 | switch (F->getNameLen()) { | ||||
2353 | case 3: // abs(x) != -0.0 | ||||
2354 | if (!strcmp(F->getNameStart(), "abs")) return true; | ||||
2355 | break; | ||||
2356 | case 4: // abs[lf](x) != -0.0 | ||||
2357 | if (!strcmp(F->getNameStart(), "absf")) return true; | ||||
2358 | if (!strcmp(F->getNameStart(), "absl")) return true; | ||||
2359 | break; | ||||
2360 | } | ||||
2361 | } | ||||
2362 | } | ||||
2363 | } | ||||
2364 | |||||
2365 | return false; | ||||
2366 | } | ||||
2367 | |||||
2368 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2369 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2370 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2371 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2372 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2373 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2374 | // X + undef -> undef |
2375 | if (isa<UndefValue>(RHS)) | ||||
2376 | return ReplaceInstUsesWith(I, RHS); | ||||
2377 | |||||
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2378 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2379 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2380 | if (RHSC->isNullValue()) |
2381 | return ReplaceInstUsesWith(I, LHS); | ||||
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2382 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2383 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
2384 | (I.getType())->getValueAPF())) | ||||
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 2385 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 2386 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2387 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2388 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2389 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2390 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2391 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2392 | if (Val == APInt::getSignBit(BitWidth)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2393 | return BinaryOperator::createXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 2394 | |
2395 | // See if SimplifyDemandedBits can simplify this. This handles stuff like | ||||
2396 | // (X & 254)+1 -> (X&254)|1 | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2397 | if (!isa<VectorType>(I.getType())) { |
2398 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
2399 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
2400 | KnownZero, KnownOne)) | ||||
2401 | return &I; | ||||
2402 | } | ||||
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2403 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2404 | |
2405 | if (isa<PHINode>(LHS)) | ||||
2406 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2407 | return NV; | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2408 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2409 | ConstantInt *XorRHS = 0; |
2410 | Value *XorLHS = 0; | ||||
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2411 | if (isa<ConstantInt>(RHSC) && |
2412 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2413 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2414 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2415 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2416 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2417 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
2418 | APInt CFF80Val(-C0080Val); | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2419 | do { |
2420 | if (TySizeBits > Size) { | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2421 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
2422 | // 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] | 2423 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
2424 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2425 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2426 | if (!MaskedValueIsZero(XorLHS, |
2427 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2428 | 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] | 2429 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2430 | } |
2431 | } | ||||
2432 | Size >>= 1; | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2433 | C0080Val = APIntOps::lshr(C0080Val, Size); |
2434 | CFF80Val = APIntOps::ashr(CFF80Val, Size); | ||||
2435 | } while (Size >= 1); | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2436 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2437 | // FIXME: This shouldn't be necessary. When the backends can handle types |
2438 | // with funny bit widths then this whole cascade of if statements should | ||||
2439 | // be removed. It is just here to get the size of the "middle" type back | ||||
2440 | // up to something that the back ends can handle. | ||||
2441 | const Type *MiddleType = 0; | ||||
2442 | switch (Size) { | ||||
2443 | default: break; | ||||
2444 | case 32: MiddleType = Type::Int32Ty; break; | ||||
2445 | case 16: MiddleType = Type::Int16Ty; break; | ||||
2446 | case 8: MiddleType = Type::Int8Ty; break; | ||||
2447 | } | ||||
2448 | if (MiddleType) { | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2449 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2450 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2451 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2452 | } |
2453 | } | ||||
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2454 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2456 | // X + X --> X << 1 |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2457 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2458 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2459 | |
2460 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { | ||||
2461 | if (RHSI->getOpcode() == Instruction::Sub) | ||||
2462 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B | ||||
2463 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); | ||||
2464 | } | ||||
2465 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { | ||||
2466 | if (LHSI->getOpcode() == Instruction::Sub) | ||||
2467 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B | ||||
2468 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); | ||||
2469 | } | ||||
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2470 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2471 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2472 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2473 | // -A + -B --> -(A + B) |
2474 | if (Value *LHSV = dyn_castNegVal(LHS)) { | ||||
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2475 | if (LHS->getType()->isIntOrIntVector()) { |
2476 | if (Value *RHSV = dyn_castNegVal(RHS)) { | ||||
2477 | Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum"); | ||||
2478 | InsertNewInstBefore(NewAdd, I); | ||||
2479 | return BinaryOperator::createNeg(NewAdd); | ||||
2480 | } | ||||
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2481 | } |
2482 | |||||
2483 | return BinaryOperator::createSub(RHS, LHSV); | ||||
2484 | } | ||||
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2485 | |
2486 | // A + -B --> A - B | ||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2487 | if (!isa<Constant>(RHS)) |
2488 | if (Value *V = dyn_castNegVal(RHS)) | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2489 | return BinaryOperator::createSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2490 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2492 | ConstantInt *C2; |
2493 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { | ||||
2494 | if (X == RHS) // X*C + X --> X * (C+1) | ||||
2495 | return BinaryOperator::createMul(RHS, AddOne(C2)); | ||||
2496 | |||||
2497 | // X*C1 + X*C2 --> X * (C1+C2) | ||||
2498 | ConstantInt *C1; | ||||
2499 | if (X == dyn_castFoldableMul(RHS, C1)) | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2500 | return BinaryOperator::createMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2501 | } |
2502 | |||||
2503 | // X + X*C --> X * (C+1) | ||||
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2504 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
2505 | return BinaryOperator::createMul(LHS, AddOne(C2)); | ||||
2506 | |||||
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2507 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2508 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
2509 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); | ||||
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2511 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2512 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2513 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2514 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
2515 | return R; | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2516 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2517 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2518 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2519 | Value *W, *X, *Y, *Z; |
2520 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && | ||||
2521 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { | ||||
2522 | if (W != Y) { | ||||
2523 | if (W == Z) { | ||||
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2524 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2525 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2526 | std::swap(W, X); |
2527 | } else if (X == Z) { | ||||
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2528 | std::swap(Y, Z); |
2529 | std::swap(W, X); | ||||
2530 | } | ||||
2531 | } | ||||
2532 | |||||
2533 | if (W == Y) { | ||||
2534 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z, | ||||
2535 | LHS->getName()), I); | ||||
2536 | return BinaryOperator::createMul(W, NewAdd); | ||||
2537 | } | ||||
2538 | } | ||||
2539 | } | ||||
2540 | |||||
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2541 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2542 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2543 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
2544 | return BinaryOperator::createSub(SubOne(CRHS), X); | ||||
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2545 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2546 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
2547 | 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] | 2548 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2549 | if (Anded == CRHS) { |
2550 | // See if all bits from the first bit set in the Add RHS up are included | ||||
2551 | // in the mask. First, get the rightmost bit. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2552 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2553 | |
2554 | // 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] | 2555 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2556 | |
2557 | // See if the and mask includes all of these bits. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2558 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2559 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2560 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
2561 | // Okay, the xform is safe. Insert the new add pronto. | ||||
2562 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, | ||||
2563 | LHS->getName()), I); | ||||
2564 | return BinaryOperator::createAnd(NewAdd, C2); | ||||
2565 | } | ||||
2566 | } | ||||
2567 | } | ||||
2568 | |||||
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2569 | // Try to fold constant add into select arguments. |
2570 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2571 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2572 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2573 | } |
2574 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2575 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2576 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2577 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2578 | CastInst *CI = dyn_cast<CastInst>(LHS); |
2579 | Value *Other = RHS; | ||||
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2580 | if (!CI) { |
2581 | CI = dyn_cast<CastInst>(RHS); | ||||
2582 | Other = LHS; | ||||
2583 | } | ||||
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2584 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2585 | (CI->getType()->getPrimitiveSizeInBits() == |
2586 | TD->getIntPtrType()->getPrimitiveSizeInBits()) | ||||
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2587 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2588 | unsigned AS = |
2589 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2590 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
2591 | PointerType::get(Type::Int8Ty, AS), I); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2592 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2593 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2594 | } |
2595 | } | ||||
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2596 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2597 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2598 | { |
2599 | SelectInst *SI = dyn_cast<SelectInst>(LHS); | ||||
2600 | Value *Other = RHS; | ||||
2601 | if (!SI) { | ||||
2602 | SI = dyn_cast<SelectInst>(RHS); | ||||
2603 | Other = LHS; | ||||
2604 | } | ||||
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2605 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2606 | Value *TV = SI->getTrueValue(); |
2607 | Value *FV = SI->getFalseValue(); | ||||
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2608 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2609 | |
2610 | // Can we fold the add into the argument of the select? | ||||
2611 | // We check both true and false select arguments for a matching subtract. | ||||
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2612 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
2613 | A == Other) // Fold the add into the true select value. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2614 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2615 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
2616 | A == Other) // Fold the add into the false select value. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2617 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2618 | } |
2619 | } | ||||
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2620 | |
2621 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. | ||||
2622 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) | ||||
2623 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) | ||||
2624 | return ReplaceInstUsesWith(I, LHS); | ||||
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2625 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2626 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2627 | } |
2628 | |||||
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2629 | // isSignBit - Return true if the value represented by the constant only has the |
2630 | // highest order bit set. | ||||
2631 | static bool isSignBit(ConstantInt *CI) { | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2632 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 5a1e3e1 | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2633 | return CI->getValue() == APInt::getSignBit(NumBits); |
Chris Lattner | 1ba5bcd | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2634 | } |
2635 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2636 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2637 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2639 | if (Op0 == Op1) // sub X, X -> 0 |
2640 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2641 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2642 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2643 | if (Value *V = dyn_castNegVal(Op1)) |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2644 | return BinaryOperator::createAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2645 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2646 | if (isa<UndefValue>(Op0)) |
2647 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef | ||||
2648 | if (isa<UndefValue>(Op1)) | ||||
2649 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef | ||||
2650 | |||||
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2651 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
2652 | // Replace (-1 - A) with (~A)... | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2653 | if (C->isAllOnesValue()) |
2654 | return BinaryOperator::createNot(Op1); | ||||
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2655 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2656 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2657 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2658 | if (match(Op1, m_Not(m_Value(X)))) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2659 | return BinaryOperator::createAdd(X, AddOne(C)); |
2660 | |||||
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2661 | // -(X >>u 31) -> (X >>s 31) |
2662 | // -(X >>s 31) -> (X >>u 31) | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2663 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2664 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2665 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2666 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2667 | // 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] | 2668 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2669 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2670 | // Ok, the transformation is safe. Insert AShr. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2671 | return BinaryOperator::create(Instruction::AShr, |
2672 | SI->getOperand(0), CU, SI->getName()); | ||||
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2673 | } |
2674 | } | ||||
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2675 | } |
2676 | else if (SI->getOpcode() == Instruction::AShr) { | ||||
2677 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { | ||||
2678 | // 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] | 2679 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2680 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2681 | // Ok, the transformation is safe. Insert LShr. |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2682 | return BinaryOperator::createLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2683 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2684 | } |
2685 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2686 | } |
2687 | } | ||||
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2688 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2689 | |
2690 | // Try to fold constant sub into select arguments. | ||||
2691 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2692 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2693 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2694 | |
2695 | if (isa<PHINode>(Op0)) | ||||
2696 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2697 | return NV; | ||||
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2698 | } |
2699 | |||||
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2700 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
2701 | if (Op1I->getOpcode() == Instruction::Add && | ||||
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2702 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2703 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2704 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2705 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2706 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2707 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
2708 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) | ||||
2709 | // C1-(X+C2) --> (C1-C2)-X | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2710 | return BinaryOperator::createSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2711 | Op1I->getOperand(0)); |
2712 | } | ||||
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2713 | } |
2714 | |||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2715 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2716 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
2717 | // is not used by anyone else... | ||||
2718 | // | ||||
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2719 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2720 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2721 | // Swap the two operands of the subexpr... |
2722 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); | ||||
2723 | Op1I->setOperand(0, IIOp1); | ||||
2724 | Op1I->setOperand(1, IIOp0); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2725 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2726 | // Create the new top level add instruction... |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2727 | return BinaryOperator::createAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2728 | } |
2729 | |||||
2730 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... | ||||
2731 | // | ||||
2732 | if (Op1I->getOpcode() == Instruction::And && | ||||
2733 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { | ||||
2734 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); | ||||
2735 | |||||
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2736 | Value *NewNot = |
2737 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2738 | return BinaryOperator::createAnd(Op0, NewNot); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2739 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2740 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2741 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2742 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2743 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2744 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2745 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2746 | return BinaryOperator::createSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2747 | ConstantExpr::getNeg(DivRHS)); |
2748 | |||||
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2749 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2750 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2751 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2752 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2753 | return BinaryOperator::createMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2754 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2755 | |
2756 | // X - ((X / Y) * Y) --> X % Y | ||||
2757 | if (Op1I->getOpcode() == Instruction::Mul) | ||||
2758 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) | ||||
2759 | if (Op0 == I->getOperand(0) && | ||||
2760 | Op1I->getOperand(1) == I->getOperand(1)) { | ||||
2761 | if (I->getOpcode() == Instruction::SDiv) | ||||
2762 | return BinaryOperator::createSRem(Op0, Op1I->getOperand(1)); | ||||
2763 | if (I->getOpcode() == Instruction::UDiv) | ||||
2764 | return BinaryOperator::createURem(Op0, Op1I->getOperand(1)); | ||||
2765 | } | ||||
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2766 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2767 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2768 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2769 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2770 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2771 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2772 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
2773 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); | ||||
2774 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X | ||||
2775 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); | ||||
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2776 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
2777 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y | ||||
2778 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2779 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2780 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2782 | ConstantInt *C1; |
2783 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2784 | if (X == Op1) // X*C - X --> X * (C-1) |
2785 | return BinaryOperator::createMul(Op1, SubOne(C1)); | ||||
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2787 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
2788 | if (X == dyn_castFoldableMul(Op1, C2)) | ||||
Zhou Sheng | 58d13af | 2008-02-22 10:00:35 +0000 | [diff] [blame] | 2789 | return BinaryOperator::createMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2790 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2791 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2792 | } |
2793 | |||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2794 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
2795 | /// comparison only checks the sign bit. If it only checks the sign bit, set | ||||
2796 | /// TrueIfSigned if the result of the comparison is true when the input value is | ||||
2797 | /// signed. | ||||
2798 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, | ||||
2799 | bool &TrueIfSigned) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2800 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2801 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
2802 | TrueIfSigned = true; | ||||
2803 | return RHS->isZero(); | ||||
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2804 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
2805 | TrueIfSigned = true; | ||||
2806 | return RHS->isAllOnesValue(); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2807 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
2808 | TrueIfSigned = false; | ||||
2809 | return RHS->isAllOnesValue(); | ||||
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2810 | case ICmpInst::ICMP_UGT: |
2811 | // True if LHS u> RHS and RHS == high-bit-mask - 1 | ||||
2812 | TrueIfSigned = true; | ||||
2813 | return RHS->getValue() == | ||||
2814 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); | ||||
2815 | case ICmpInst::ICMP_UGE: | ||||
2816 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) | ||||
2817 | TrueIfSigned = true; | ||||
2818 | return RHS->getValue() == | ||||
2819 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2820 | default: |
2821 | return false; | ||||
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2822 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2823 | } |
2824 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2825 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2826 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2827 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2828 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2829 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
2830 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
2831 | |||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2832 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2833 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
2834 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { | ||||
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2835 | |
2836 | // ((X << C1)*C2) == (X * (C2 << C1)) | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2837 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2838 | if (SI->getOpcode() == Instruction::Shl) |
2839 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2840 | return BinaryOperator::createMul(SI->getOperand(0), |
2841 | ConstantExpr::getShl(CI, ShOp)); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2842 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2843 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2844 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
2845 | if (CI->equalsInt(1)) // X * 1 == X | ||||
2846 | return ReplaceInstUsesWith(I, Op0); | ||||
2847 | if (CI->isAllOnesValue()) // X * -1 == 0 - X | ||||
Chris Lattner | 0af1fab | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2848 | return BinaryOperator::createNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2849 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2850 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2851 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2852 | return BinaryOperator::createShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2853 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2854 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2855 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2856 | if (Op1F->isNullValue()) |
2857 | return ReplaceInstUsesWith(I, Op1); | ||||
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2858 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2859 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
2860 | // 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] | 2861 | // We need a better interface for long double here. |
2862 | if (Op1->getType() == Type::FloatTy || Op1->getType() == Type::DoubleTy) | ||||
2863 | if (Op1F->isExactlyValue(1.0)) | ||||
2864 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2865 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2866 | |
2867 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) | ||||
2868 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && | ||||
2869 | isa<ConstantInt>(Op0I->getOperand(1))) { | ||||
2870 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. | ||||
2871 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), | ||||
2872 | Op1, "tmp"); | ||||
2873 | InsertNewInstBefore(Add, I); | ||||
2874 | Value *C1C2 = ConstantExpr::getMul(Op1, | ||||
2875 | cast<Constant>(Op0I->getOperand(1))); | ||||
2876 | return BinaryOperator::createAdd(Add, C1C2); | ||||
2877 | |||||
2878 | } | ||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2879 | |
2880 | // Try to fold constant mul into select arguments. | ||||
2881 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2882 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2883 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2884 | |
2885 | if (isa<PHINode>(Op0)) | ||||
2886 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2887 | return NV; | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2888 | } |
2889 | |||||
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2890 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
2891 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2892 | return BinaryOperator::createMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2893 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2894 | // If one of the operands of the multiply is a cast from a boolean value, then |
2895 | // we know the bool is either zero or one, so this is a 'masking' multiply. | ||||
2896 | // See if we can simplify things based on how the boolean was originally | ||||
2897 | // formed. | ||||
2898 | CastInst *BoolCast = 0; | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2899 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2900 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2901 | BoolCast = CI; |
2902 | if (!BoolCast) | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2903 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2904 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2905 | BoolCast = CI; |
2906 | if (BoolCast) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2907 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2908 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
2909 | const Type *SCOpTy = SCIOp0->getType(); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2910 | bool TIS = false; |
2911 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2912 | // 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] | 2913 | // multiply into a shift/and combination. |
2914 | if (isa<ConstantInt>(SCIOp1) && | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2915 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
2916 | TIS) { | ||||
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2917 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2918 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2919 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2920 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2921 | InsertNewInstBefore( |
2922 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, | ||||
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2923 | BoolCast->getOperand(0)->getName()+ |
2924 | ".mask"), I); | ||||
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2925 | |
2926 | // If the multiply type is not the same as the source type, sign extend | ||||
2927 | // or truncate to the multiply type. | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2928 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2929 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
2930 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2931 | Instruction::CastOps opcode = |
2932 | (SrcBits == DstBits ? Instruction::BitCast : | ||||
2933 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); | ||||
2934 | V = InsertCastBefore(opcode, V, I.getType(), I); | ||||
2935 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2936 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2937 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2938 | return BinaryOperator::createAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2939 | } |
2940 | } | ||||
2941 | } | ||||
2942 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2943 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2944 | } |
2945 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2946 | /// This function implements the transforms on div instructions that work |
2947 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is | ||||
2948 | /// used by the visitors to those instructions. | ||||
2949 | /// @brief Transforms common to all three div instructions | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2950 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2951 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2953 | // undef / X -> 0 for integer. |
2954 | // undef / X -> undef for FP (the undef could be a snan). | ||||
2955 | if (isa<UndefValue>(Op0)) { | ||||
2956 | if (Op0->getType()->isFPOrFPVector()) | ||||
2957 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2958 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2959 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2960 | |
2961 | // X / undef -> undef | ||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2962 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2963 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2964 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2965 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
2966 | // This does not apply for fdiv. | ||||
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2967 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2968 | // [su]div X, (Cond ? 0 : Y) -> div X, Y. If the div and the select are in |
2969 | // the same basic block, then we replace the select with Y, and the | ||||
2970 | // condition of the select with false (if the cond value is in the same BB). | ||||
2971 | // If the select has uses other than the div, this allows them to be | ||||
2972 | // simplified also. Note that div X, Y is just as good as div X, 0 (undef) | ||||
2973 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(1))) | ||||
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2974 | if (ST->isNullValue()) { |
2975 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | ||||
2976 | if (CondI && CondI->getParent() == I.getParent()) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2977 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2978 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
2979 | I.setOperand(1, SI->getOperand(2)); | ||||
2980 | else | ||||
2981 | UpdateValueUsesWith(SI, SI->getOperand(2)); | ||||
2982 | return &I; | ||||
2983 | } | ||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2984 | |
Chris Lattner | 25feae5 | 2008-01-28 00:58:18 +0000 | [diff] [blame] | 2985 | // Likewise for: [su]div X, (Cond ? Y : 0) -> div X, Y |
2986 | if (ConstantInt *ST = dyn_cast<ConstantInt>(SI->getOperand(2))) | ||||
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2987 | if (ST->isNullValue()) { |
2988 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | ||||
2989 | if (CondI && CondI->getParent() == I.getParent()) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2990 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2991 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
2992 | I.setOperand(1, SI->getOperand(1)); | ||||
2993 | else | ||||
2994 | UpdateValueUsesWith(SI, SI->getOperand(1)); | ||||
2995 | return &I; | ||||
2996 | } | ||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2997 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2998 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2999 | return 0; |
3000 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3001 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3002 | /// This function implements the transforms common to both integer division |
3003 | /// instructions (udiv and sdiv). It is called by the visitors to those integer | ||||
3004 | /// division instructions. | ||||
3005 | /// @brief Common integer divide transforms | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3006 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3007 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
3008 | |||||
3009 | if (Instruction *Common = commonDivTransforms(I)) | ||||
3010 | return Common; | ||||
3011 | |||||
3012 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | ||||
3013 | // div X, 1 == X | ||||
3014 | if (RHS->equalsInt(1)) | ||||
3015 | return ReplaceInstUsesWith(I, Op0); | ||||
3016 | |||||
3017 | // (X / C1) / C2 -> X / (C1*C2) | ||||
3018 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) | ||||
3019 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) | ||||
3020 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { | ||||
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 3021 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
3022 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3023 | else | ||||
3024 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), | ||||
3025 | Multiply(RHS, LHSRHS)); | ||||
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 3026 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3027 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3028 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3029 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
3030 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | ||||
3031 | return R; | ||||
3032 | if (isa<PHINode>(Op0)) | ||||
3033 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
3034 | return NV; | ||||
3035 | } | ||||
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3036 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3038 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3039 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3040 | if (LHS->equalsInt(0)) |
3041 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3042 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3043 | return 0; |
3044 | } | ||||
3045 | |||||
3046 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { | ||||
3047 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
3048 | |||||
3049 | // Handle the integer div common cases | ||||
3050 | if (Instruction *Common = commonIDivTransforms(I)) | ||||
3051 | return Common; | ||||
3052 | |||||
3053 | // X udiv C^2 -> X >> C | ||||
3054 | // Check to see if this is an unsigned division with an exact power of 2, | ||||
3055 | // if so, convert to a right shift. | ||||
3056 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { | ||||
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3057 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3058 | return BinaryOperator::createLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3059 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3060 | } |
3061 | |||||
3062 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3063 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3064 | if (RHSI->getOpcode() == Instruction::Shl && |
3065 | isa<ConstantInt>(RHSI->getOperand(0))) { | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3066 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3067 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3068 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3069 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 3070 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3071 | Constant *C2V = ConstantInt::get(NTy, C2); |
3072 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); | ||||
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3073 | } |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3074 | return BinaryOperator::createLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3075 | } |
3076 | } | ||||
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 3077 | } |
3078 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3079 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
3080 | // where C1&C2 are powers of two. | ||||
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3081 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3082 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3083 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3084 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3085 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3086 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3087 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3088 | // Construct the "on true" case of the select |
3089 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); | ||||
3090 | Instruction *TSI = BinaryOperator::createLShr( | ||||
3091 | Op0, TC, SI->getName()+".t"); | ||||
3092 | TSI = InsertNewInstBefore(TSI, I); | ||||
3093 | |||||
3094 | // Construct the "on false" case of the select | ||||
3095 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); | ||||
3096 | Instruction *FSI = BinaryOperator::createLShr( | ||||
3097 | Op0, FC, SI->getName()+".f"); | ||||
3098 | FSI = InsertNewInstBefore(FSI, I); | ||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3099 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3100 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3101 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3102 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 3103 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3104 | return 0; |
3105 | } | ||||
3106 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3107 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
3108 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
3109 | |||||
3110 | // Handle the integer div common cases | ||||
3111 | if (Instruction *Common = commonIDivTransforms(I)) | ||||
3112 | return Common; | ||||
3113 | |||||
3114 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | ||||
3115 | // sdiv X, -1 == -X | ||||
3116 | if (RHS->isAllOnesValue()) | ||||
3117 | return BinaryOperator::createNeg(Op0); | ||||
3118 | |||||
3119 | // -X/C -> X/-C | ||||
3120 | if (Value *LHSNeg = dyn_castNegVal(Op0)) | ||||
3121 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); | ||||
3122 | } | ||||
3123 | |||||
3124 | // If the sign bits of both operands are zero (i.e. we can prove they are | ||||
3125 | // unsigned inputs), turn this into a udiv. | ||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3126 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3127 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3128 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3129 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 3130 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); |
3131 | } | ||||
3132 | } | ||||
3133 | |||||
3134 | return 0; | ||||
3135 | } | ||||
3136 | |||||
3137 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { | ||||
3138 | return commonDivTransforms(I); | ||||
3139 | } | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3140 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3141 | /// This function implements the transforms on rem instructions that work |
3142 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It | ||||
3143 | /// is used by the visitors to those instructions. | ||||
3144 | /// @brief Transforms common to all three rem instructions | ||||
3145 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { | ||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3146 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3147 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3148 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3149 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
3150 | if (LHS->isNullValue()) | ||||
3151 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3152 | |||||
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3153 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
3154 | if (I.getType()->isFPOrFPVector()) | ||||
3155 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) | ||||
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3156 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3157 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3158 | if (isa<UndefValue>(Op1)) |
3159 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3160 | |
3161 | // Handle cases involving: rem X, (select Cond, Y, Z) | ||||
3162 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { | ||||
3163 | // rem X, (Cond ? 0 : Y) -> rem X, Y. If the rem and the select are in | ||||
3164 | // the same basic block, then we replace the select with Y, and the | ||||
3165 | // condition of the select with false (if the cond value is in the same | ||||
3166 | // BB). If the select has uses other than the div, this allows them to be | ||||
3167 | // simplified also. | ||||
3168 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) | ||||
3169 | if (ST->isNullValue()) { | ||||
3170 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | ||||
3171 | if (CondI && CondI->getParent() == I.getParent()) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3172 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3173 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
3174 | I.setOperand(1, SI->getOperand(2)); | ||||
3175 | else | ||||
3176 | UpdateValueUsesWith(SI, SI->getOperand(2)); | ||||
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3177 | return &I; |
3178 | } | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3179 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y |
3180 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) | ||||
3181 | if (ST->isNullValue()) { | ||||
3182 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | ||||
3183 | if (CondI && CondI->getParent() == I.getParent()) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3184 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3185 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) |
3186 | I.setOperand(1, SI->getOperand(1)); | ||||
3187 | else | ||||
3188 | UpdateValueUsesWith(SI, SI->getOperand(1)); | ||||
3189 | return &I; | ||||
3190 | } | ||||
Chris Lattner | 11a49f2 | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 3191 | } |
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 3192 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3193 | return 0; |
3194 | } | ||||
3195 | |||||
3196 | /// This function implements the transforms common to both integer remainder | ||||
3197 | /// instructions (urem and srem). It is called by the visitors to those integer | ||||
3198 | /// remainder instructions. | ||||
3199 | /// @brief Common integer remainder transforms | ||||
3200 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { | ||||
3201 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
3202 | |||||
3203 | if (Instruction *common = commonRemTransforms(I)) | ||||
3204 | return common; | ||||
3205 | |||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 3206 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 3207 | // X % 0 == undef, we don't need to preserve faults! |
3208 | if (RHS->equalsInt(0)) | ||||
3209 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); | ||||
3210 | |||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3211 | if (RHS->equalsInt(1)) // X % 1 == 0 |
3212 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3213 | |||||
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3214 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
3215 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { | ||||
3216 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | ||||
3217 | return R; | ||||
3218 | } else if (isa<PHINode>(Op0I)) { | ||||
3219 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
3220 | return NV; | ||||
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3221 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3222 | |
3223 | // See if we can fold away this rem instruction. | ||||
3224 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | ||||
3225 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
3226 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
3227 | KnownZero, KnownOne)) | ||||
3228 | return &I; | ||||
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 3229 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3230 | } |
3231 | |||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3232 | return 0; |
3233 | } | ||||
3234 | |||||
3235 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { | ||||
3236 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
3237 | |||||
3238 | if (Instruction *common = commonIRemTransforms(I)) | ||||
3239 | return common; | ||||
3240 | |||||
3241 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | ||||
3242 | // X urem C^2 -> X and C | ||||
3243 | // Check to see if this is an unsigned remainder with an exact power of 2, | ||||
3244 | // if so, convert to a bitwise and. | ||||
3245 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) | ||||
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3246 | if (C->getValue().isPowerOf2()) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3247 | return BinaryOperator::createAnd(Op0, SubOne(C)); |
3248 | } | ||||
3249 | |||||
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3250 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3251 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
3252 | if (RHSI->getOpcode() == Instruction::Shl && | ||||
3253 | isa<ConstantInt>(RHSI->getOperand(0))) { | ||||
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3254 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3255 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
3256 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, | ||||
3257 | "tmp"), I); | ||||
3258 | return BinaryOperator::createAnd(Op0, Add); | ||||
3259 | } | ||||
3260 | } | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3261 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 3262 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3263 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
3264 | // where C1&C2 are powers of two. | ||||
3265 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { | ||||
3266 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) | ||||
3267 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { | ||||
3268 | // STO == 0 and SFO == 0 handled above. | ||||
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 3269 | if ((STO->getValue().isPowerOf2()) && |
3270 | (SFO->getValue().isPowerOf2())) { | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3271 | Value *TrueAnd = InsertNewInstBefore( |
3272 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); | ||||
3273 | Value *FalseAnd = InsertNewInstBefore( | ||||
3274 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3275 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3276 | } |
3277 | } | ||||
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3278 | } |
3279 | |||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3280 | return 0; |
3281 | } | ||||
3282 | |||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3283 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
3284 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
3285 | |||||
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3286 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3287 | if (Instruction *common = commonIRemTransforms(I)) |
3288 | return common; | ||||
3289 | |||||
3290 | if (Value *RHSNeg = dyn_castNegVal(Op1)) | ||||
3291 | if (!isa<ConstantInt>(RHSNeg) || | ||||
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 3292 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3293 | // X % -Y -> X % Y |
3294 | AddUsesToWorkList(I); | ||||
3295 | I.setOperand(1, RHSNeg); | ||||
3296 | return &I; | ||||
3297 | } | ||||
3298 | |||||
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3299 | // 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] | 3300 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3301 | if (I.getType()->isInteger()) { |
3302 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); | ||||
3303 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { | ||||
3304 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set | ||||
3305 | return BinaryOperator::createURem(Op0, Op1, I.getName()); | ||||
3306 | } | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3307 | } |
3308 | |||||
3309 | return 0; | ||||
3310 | } | ||||
3311 | |||||
3312 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3313 | return commonRemTransforms(I); |
3314 | } | ||||
3315 | |||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3316 | // isMaxValueMinusOne - return true if this is Max-1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3317 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { |
Reid Spencer | 3a2a9fb | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 3318 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3319 | if (!isSigned) |
3320 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; | ||||
3321 | return C->getValue() == APInt::getSignedMaxValue(TypeBits)-1; | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3322 | } |
3323 | |||||
3324 | // isMinValuePlusOne - return true if this is Min+1 | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3325 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 3326 | if (!isSigned) |
3327 | return C->getValue() == 1; // unsigned | ||||
3328 | |||||
3329 | // Calculate 1111111111000000000000 | ||||
3330 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); | ||||
3331 | return C->getValue() == APInt::getSignedMinValue(TypeBits)+1; | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3332 | } |
3333 | |||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3334 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
3335 | // constant. | ||||
3336 | static bool isOneBitSet(const ConstantInt *CI) { | ||||
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3337 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3338 | } |
3339 | |||||
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3340 | // isHighOnes - Return true if the constant is of the form 1+0+. |
3341 | // This is the same as lowones(~X). | ||||
3342 | static bool isHighOnes(const ConstantInt *CI) { | ||||
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3343 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3344 | } |
3345 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3346 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3347 | /// are carefully arranged to allow folding of expressions such as: |
3348 | /// | ||||
3349 | /// (A < B) | (A > B) --> (A != B) | ||||
3350 | /// | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3351 | /// Note that this is only valid if the first and second predicates have the |
3352 | /// same sign. Is illegal to do: (A u< B) | (A s> B) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3353 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3354 | /// Three bits are used to represent the condition, as follows: |
3355 | /// 0 A > B | ||||
3356 | /// 1 A == B | ||||
3357 | /// 2 A < B | ||||
3358 | /// | ||||
3359 | /// <=> Value Definition | ||||
3360 | /// 000 0 Always false | ||||
3361 | /// 001 1 A > B | ||||
3362 | /// 010 2 A == B | ||||
3363 | /// 011 3 A >= B | ||||
3364 | /// 100 4 A < B | ||||
3365 | /// 101 5 A != B | ||||
3366 | /// 110 6 A <= B | ||||
3367 | /// 111 7 Always true | ||||
3368 | /// | ||||
3369 | static unsigned getICmpCode(const ICmpInst *ICI) { | ||||
3370 | switch (ICI->getPredicate()) { | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3371 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3372 | case ICmpInst::ICMP_UGT: return 1; // 001 |
3373 | case ICmpInst::ICMP_SGT: return 1; // 001 | ||||
3374 | case ICmpInst::ICMP_EQ: return 2; // 010 | ||||
3375 | case ICmpInst::ICMP_UGE: return 3; // 011 | ||||
3376 | case ICmpInst::ICMP_SGE: return 3; // 011 | ||||
3377 | case ICmpInst::ICMP_ULT: return 4; // 100 | ||||
3378 | case ICmpInst::ICMP_SLT: return 4; // 100 | ||||
3379 | case ICmpInst::ICMP_NE: return 5; // 101 | ||||
3380 | case ICmpInst::ICMP_ULE: return 6; // 110 | ||||
3381 | case ICmpInst::ICMP_SLE: return 6; // 110 | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3382 | // True -> 7 |
3383 | default: | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3384 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3385 | return 0; |
3386 | } | ||||
3387 | } | ||||
3388 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3389 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
3390 | /// 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] | 3391 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3392 | /// of predicate to use in new icmp instructions. |
3393 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { | ||||
3394 | switch (code) { | ||||
3395 | default: assert(0 && "Illegal ICmp code!"); | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3396 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3397 | case 1: |
3398 | if (sign) | ||||
3399 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); | ||||
3400 | else | ||||
3401 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); | ||||
3402 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); | ||||
3403 | case 3: | ||||
3404 | if (sign) | ||||
3405 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); | ||||
3406 | else | ||||
3407 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); | ||||
3408 | case 4: | ||||
3409 | if (sign) | ||||
3410 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); | ||||
3411 | else | ||||
3412 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); | ||||
3413 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); | ||||
3414 | case 6: | ||||
3415 | if (sign) | ||||
3416 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); | ||||
3417 | else | ||||
3418 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3419 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3420 | } |
3421 | } | ||||
3422 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3423 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
3424 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || | ||||
3425 | (ICmpInst::isSignedPredicate(p1) && | ||||
3426 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || | ||||
3427 | (ICmpInst::isSignedPredicate(p2) && | ||||
3428 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); | ||||
3429 | } | ||||
3430 | |||||
3431 | namespace { | ||||
3432 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) | ||||
3433 | struct FoldICmpLogical { | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3434 | InstCombiner &IC; |
3435 | Value *LHS, *RHS; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3436 | ICmpInst::Predicate pred; |
3437 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) | ||||
3438 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), | ||||
3439 | pred(ICI->getPredicate()) {} | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3440 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3441 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
3442 | if (PredicatesFoldable(pred, ICI->getPredicate())) | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3443 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
3444 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3445 | return false; |
3446 | } | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3447 | Instruction *apply(Instruction &Log) const { |
3448 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); | ||||
3449 | if (ICI->getOperand(0) != LHS) { | ||||
3450 | assert(ICI->getOperand(1) == LHS); | ||||
3451 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3452 | } |
3453 | |||||
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3454 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3455 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3456 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3457 | unsigned Code; |
3458 | switch (Log.getOpcode()) { | ||||
3459 | case Instruction::And: Code = LHSCode & RHSCode; break; | ||||
3460 | case Instruction::Or: Code = LHSCode | RHSCode; break; | ||||
3461 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; | ||||
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3462 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3463 | } |
3464 | |||||
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3465 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
3466 | ICmpInst::isSignedPredicate(ICI->getPredicate()); | ||||
3467 | |||||
3468 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3469 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
3470 | return I; | ||||
3471 | // Otherwise, it's a constant boolean value... | ||||
3472 | return IC.ReplaceInstUsesWith(Log, RV); | ||||
3473 | } | ||||
3474 | }; | ||||
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3475 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3476 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3477 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
3478 | // 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] | 3479 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3480 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3481 | ConstantInt *OpRHS, |
3482 | ConstantInt *AndRHS, | ||||
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3483 | BinaryOperator &TheAnd) { |
3484 | Value *X = Op->getOperand(0); | ||||
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3485 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3486 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3487 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3489 | switch (Op->getOpcode()) { |
3490 | case Instruction::Xor: | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3491 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3492 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3493 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3494 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3495 | And->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3496 | return BinaryOperator::createXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3497 | } |
3498 | break; | ||||
3499 | case Instruction::Or: | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3500 | if (Together == AndRHS) // (X | C) & C --> C |
3501 | return ReplaceInstUsesWith(TheAnd, AndRHS); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3502 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3503 | if (Op->hasOneUse() && Together != OpRHS) { |
3504 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3505 | Instruction *Or = BinaryOperator::createOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3506 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3507 | Or->takeName(Op); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3508 | return BinaryOperator::createAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3509 | } |
3510 | break; | ||||
3511 | case Instruction::Add: | ||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3512 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3513 | // Adding a one to a single bit bit-field should be turned into an XOR |
3514 | // of the bit. First thing to check is to see if this AND is with a | ||||
3515 | // single bit constant. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3516 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3517 | |
3518 | // If there is only one bit set... | ||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3519 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3520 | // Ok, at this point, we know that we are masking the result of the |
3521 | // ADD down to exactly one bit. If the constant we are adding has | ||||
3522 | // no bits set below this bit, then we can eliminate the ADD. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3523 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3524 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3525 | // Check to see if any bits below the one bit set in AndRHSV are set. |
3526 | if ((AddRHS & (AndRHSV-1)) == 0) { | ||||
3527 | // If not, the only thing that can effect the output of the AND is | ||||
3528 | // the bit specified by AndRHSV. If that bit is set, the effect of | ||||
3529 | // the XOR is to toggle the bit. If it is clear, then the ADD has | ||||
3530 | // no effect. | ||||
3531 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop | ||||
3532 | TheAnd.setOperand(0, X); | ||||
3533 | return &TheAnd; | ||||
3534 | } else { | ||||
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3535 | // Pull the XOR out of the AND. |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3536 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3537 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3538 | NewAnd->takeName(Op); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3539 | return BinaryOperator::createXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3540 | } |
3541 | } | ||||
3542 | } | ||||
3543 | } | ||||
3544 | break; | ||||
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3545 | |
3546 | case Instruction::Shl: { | ||||
3547 | // We know that the AND will not produce any of the bits shifted in, so if | ||||
3548 | // the anded constant includes them, clear them now! | ||||
3549 | // | ||||
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3550 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3551 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3552 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
3553 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3554 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3555 | if (CI->getValue() == ShlMask) { |
3556 | // Masking out bits that the shift already masks | ||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3557 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
3558 | } else if (CI != AndRHS) { // Reducing bits set in and. | ||||
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3559 | TheAnd.setOperand(1, CI); |
3560 | return &TheAnd; | ||||
3561 | } | ||||
3562 | break; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3563 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3564 | case Instruction::LShr: |
3565 | { | ||||
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3566 | // We know that the AND will not produce any of the bits shifted in, so if |
3567 | // the anded constant includes them, clear them now! This only applies to | ||||
3568 | // unsigned shifts, because a signed shr may bring in set bits! | ||||
3569 | // | ||||
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3570 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3571 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3572 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
3573 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); | ||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3574 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3575 | if (CI->getValue() == ShrMask) { |
3576 | // Masking out bits that the shift already masks. | ||||
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3577 | return ReplaceInstUsesWith(TheAnd, Op); |
3578 | } else if (CI != AndRHS) { | ||||
3579 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. | ||||
3580 | return &TheAnd; | ||||
3581 | } | ||||
3582 | break; | ||||
3583 | } | ||||
3584 | case Instruction::AShr: | ||||
3585 | // Signed shr. | ||||
3586 | // See if this is shifting in some sign extension, then masking it out | ||||
3587 | // with an and. | ||||
3588 | if (Op->hasOneUse()) { | ||||
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3589 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3590 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3591 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
3592 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); | ||||
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3593 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3594 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3595 | // Make the argument unsigned. |
3596 | Value *ShVal = Op->getOperand(0); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3597 | ShVal = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3598 | BinaryOperator::createLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3599 | Op->getName()), TheAnd); |
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3600 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3601 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3602 | } |
3603 | break; | ||||
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3604 | } |
3605 | return 0; | ||||
3606 | } | ||||
3607 | |||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3608 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3609 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
3610 | /// 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] | 3611 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
3612 | /// 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] | 3613 | /// insert new instructions. |
3614 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3615 | bool isSigned, bool Inside, |
3616 | Instruction &IB) { | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3617 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3618 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3619 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3620 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3621 | if (Inside) { |
3622 | if (Lo == Hi) // Trivially false. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3623 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3624 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3625 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3626 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3627 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3628 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
3629 | return new ICmpInst(pred, V, Hi); | ||||
3630 | } | ||||
3631 | |||||
3632 | // Emit V-Lo <u Hi-Lo | ||||
3633 | Constant *NegLo = ConstantExpr::getNeg(Lo); | ||||
3634 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3635 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3636 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
3637 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3638 | } |
3639 | |||||
3640 | if (Lo == Hi) // Trivially true. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3641 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3642 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3643 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3644 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3645 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3646 | ICmpInst::Predicate pred = (isSigned ? |
3647 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); | ||||
3648 | return new ICmpInst(pred, V, Hi); | ||||
3649 | } | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3650 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3651 | // Emit V-Lo >u Hi-1-Lo |
3652 | // Note that Hi has already had one subtracted from it, above. | ||||
3653 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3654 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3655 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3656 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
3657 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3658 | } |
3659 | |||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3660 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
3661 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to | ||||
3662 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is | ||||
3663 | // not, since all 1s are not contiguous. | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3664 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3665 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3666 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
3667 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3668 | |
3669 | // look for the first zero bit after the run of ones | ||||
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3670 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3671 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3672 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3673 | return true; |
3674 | } | ||||
3675 | |||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3676 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
3677 | /// where isSub determines whether the operator is a sub. If we can fold one of | ||||
3678 | /// the following xforms: | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3679 | /// |
3680 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask | ||||
3681 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 | ||||
3682 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 | ||||
3683 | /// | ||||
3684 | /// return (A +/- B). | ||||
3685 | /// | ||||
3686 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3687 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3688 | Instruction &I) { |
3689 | Instruction *LHSI = dyn_cast<Instruction>(LHS); | ||||
3690 | if (!LHSI || LHSI->getNumOperands() != 2 || | ||||
3691 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; | ||||
3692 | |||||
3693 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); | ||||
3694 | |||||
3695 | switch (LHSI->getOpcode()) { | ||||
3696 | default: return 0; | ||||
3697 | case Instruction::And: | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3698 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3699 | // 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] | 3700 | if ((Mask->getValue().countLeadingZeros() + |
3701 | Mask->getValue().countPopulation()) == | ||||
3702 | Mask->getValue().getBitWidth()) | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3703 | break; |
3704 | |||||
3705 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ | ||||
3706 | // part, we don't need any explicit masks to take them out of A. If that | ||||
3707 | // is all N is, ignore it. | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3708 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3709 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3710 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3711 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3712 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3713 | break; |
3714 | } | ||||
3715 | } | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3716 | return 0; |
3717 | case Instruction::Or: | ||||
3718 | case Instruction::Xor: | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3719 | // 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] | 3720 | if ((Mask->getValue().countLeadingZeros() + |
3721 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() | ||||
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3722 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3723 | break; |
3724 | return 0; | ||||
3725 | } | ||||
3726 | |||||
3727 | Instruction *New; | ||||
3728 | if (isSub) | ||||
3729 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); | ||||
3730 | else | ||||
3731 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); | ||||
3732 | return InsertNewInstBefore(New, I); | ||||
3733 | } | ||||
3734 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3735 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3736 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3737 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3738 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3739 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
3740 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3741 | |||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3742 | // and X, X = X |
3743 | if (Op0 == Op1) | ||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3744 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3745 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3746 | // 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] | 3747 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3748 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3749 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
3750 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
3751 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3752 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3753 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3754 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3755 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3756 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3757 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3758 | } else if (isa<ConstantAggregateZero>(Op1)) { |
3759 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> | ||||
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3760 | } |
3761 | } | ||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3762 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3763 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3764 | const APInt& AndRHSMask = AndRHS->getValue(); |
3765 | APInt NotAndRHS(~AndRHSMask); | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3766 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3767 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3768 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3769 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3770 | Value *Op0LHS = Op0I->getOperand(0); |
3771 | Value *Op0RHS = Op0I->getOperand(1); | ||||
3772 | switch (Op0I->getOpcode()) { | ||||
3773 | case Instruction::Xor: | ||||
3774 | case Instruction::Or: | ||||
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3775 | // If the mask is only needed on one incoming arm, push it up. |
3776 | if (Op0I->hasOneUse()) { | ||||
3777 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { | ||||
3778 | // Not masking anything out for the LHS, move to RHS. | ||||
3779 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, | ||||
3780 | Op0RHS->getName()+".masked"); | ||||
3781 | InsertNewInstBefore(NewRHS, I); | ||||
3782 | return BinaryOperator::create( | ||||
3783 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3784 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3785 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3786 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
3787 | // Not masking anything out for the RHS, move to LHS. | ||||
3788 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, | ||||
3789 | Op0LHS->getName()+".masked"); | ||||
3790 | InsertNewInstBefore(NewLHS, I); | ||||
3791 | return BinaryOperator::create( | ||||
3792 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); | ||||
3793 | } | ||||
3794 | } | ||||
3795 | |||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3796 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3797 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3798 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
3799 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 | ||||
3800 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 | ||||
3801 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) | ||||
3802 | return BinaryOperator::createAnd(V, AndRHS); | ||||
3803 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) | ||||
3804 | return BinaryOperator::createAnd(V, AndRHS); // Add commutes | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3805 | break; |
3806 | |||||
3807 | case Instruction::Sub: | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3808 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
3809 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 | ||||
3810 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 | ||||
3811 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) | ||||
3812 | return BinaryOperator::createAnd(V, AndRHS); | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3813 | break; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3814 | } |
3815 | |||||
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3816 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3817 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3818 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3819 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3820 | // If this is an integer truncation or change from signed-to-unsigned, and |
3821 | // if the source is an and/or with immediate, transform it. This | ||||
3822 | // frequently occurs for bitfield accesses. | ||||
3823 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3824 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3825 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3826 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3827 | if (CastOp->getOpcode() == Instruction::And) { |
3828 | // Change: and (cast (and X, C1) to T), C2 | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3829 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
3830 | // This will fold the two constants together, which may allow | ||||
3831 | // other simplifications. | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3832 | Instruction *NewCast = CastInst::createTruncOrBitCast( |
3833 | CastOp->getOperand(0), I.getType(), | ||||
3834 | CastOp->getName()+".shrunk"); | ||||
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3835 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3836 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3837 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3838 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3839 | return BinaryOperator::createAnd(NewCast, C3); |
3840 | } else if (CastOp->getOpcode() == Instruction::Or) { | ||||
3841 | // Change: and (cast (or X, C1) to T), C2 | ||||
3842 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 | ||||
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3843 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3844 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
3845 | return ReplaceInstUsesWith(I, AndRHS); | ||||
3846 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3847 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3848 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3849 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3850 | |
3851 | // Try to fold constant and into select arguments. | ||||
3852 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3853 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3854 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3855 | if (isa<PHINode>(Op0)) |
3856 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
3857 | return NV; | ||||
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3858 | } |
3859 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3860 | Value *Op0NotVal = dyn_castNotVal(Op0); |
3861 | Value *Op1NotVal = dyn_castNotVal(Op1); | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3862 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3863 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
3864 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3865 | |||||
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3866 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3867 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3868 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, |
3869 | I.getName()+".demorgan"); | ||||
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3870 | InsertNewInstBefore(Or, I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3871 | return BinaryOperator::createNot(Or); |
3872 | } | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3873 | |
3874 | { | ||||
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3875 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
3876 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3877 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
3878 | return ReplaceInstUsesWith(I, Op1); | ||||
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3879 | |
3880 | // (A|B) & ~(A&B) -> A^B | ||||
3881 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { | ||||
3882 | if ((A == C && B == D) || (A == D && B == C)) | ||||
3883 | return BinaryOperator::createXor(A, B); | ||||
3884 | } | ||||
3885 | } | ||||
3886 | |||||
3887 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3888 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
3889 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3890 | |
3891 | // ~(A&B) & (A|B) -> A^B | ||||
3892 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { | ||||
3893 | if ((A == C && B == D) || (A == D && B == C)) | ||||
3894 | return BinaryOperator::createXor(A, B); | ||||
3895 | } | ||||
3896 | } | ||||
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3897 | |
3898 | if (Op0->hasOneUse() && | ||||
3899 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | ||||
3900 | if (A == Op1) { // (A^B)&A -> A&(A^B) | ||||
3901 | I.swapOperands(); // Simplify below | ||||
3902 | std::swap(Op0, Op1); | ||||
3903 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) | ||||
3904 | cast<BinaryOperator>(Op0)->swapOperands(); | ||||
3905 | I.swapOperands(); // Simplify below | ||||
3906 | std::swap(Op0, Op1); | ||||
3907 | } | ||||
3908 | } | ||||
3909 | if (Op1->hasOneUse() && | ||||
3910 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { | ||||
3911 | if (B == Op0) { // B&(A^B) -> B&(B^A) | ||||
3912 | cast<BinaryOperator>(Op1)->swapOperands(); | ||||
3913 | std::swap(A, B); | ||||
3914 | } | ||||
3915 | if (A == Op0) { // A&(A^B) -> A & ~B | ||||
3916 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); | ||||
3917 | InsertNewInstBefore(NotB, I); | ||||
3918 | return BinaryOperator::createAnd(A, NotB); | ||||
3919 | } | ||||
3920 | } | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3921 | } |
3922 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3923 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
3924 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) | ||||
3925 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3926 | return R; |
3927 | |||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3928 | Value *LHSVal, *RHSVal; |
3929 | ConstantInt *LHSCst, *RHSCst; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3930 | ICmpInst::Predicate LHSCC, RHSCC; |
3931 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) | ||||
3932 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) | ||||
3933 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) | ||||
3934 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. | ||||
3935 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && | ||||
3936 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && | ||||
3937 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && | ||||
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3938 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
3939 | |||||
3940 | // Don't try to fold ICMP_SLT + ICMP_ULT. | ||||
3941 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || | ||||
3942 | ICmpInst::isSignedPredicate(LHSCC) == | ||||
3943 | ICmpInst::isSignedPredicate(RHSCC))) { | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3944 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3945 | ICmpInst::Predicate GT; |
3946 | if (ICmpInst::isSignedPredicate(LHSCC) || | ||||
3947 | (ICmpInst::isEquality(LHSCC) && | ||||
3948 | ICmpInst::isSignedPredicate(RHSCC))) | ||||
3949 | GT = ICmpInst::ICMP_SGT; | ||||
3950 | else | ||||
3951 | GT = ICmpInst::ICMP_UGT; | ||||
3952 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3953 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
3954 | ICmpInst *LHS = cast<ICmpInst>(Op0); | ||||
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3955 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3956 | std::swap(LHS, RHS); |
3957 | std::swap(LHSCst, RHSCst); | ||||
3958 | std::swap(LHSCC, RHSCC); | ||||
3959 | } | ||||
3960 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3961 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3962 | // comparing a value against two constants and and'ing the result |
3963 | // together. Because of the above check, we know that we only have | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3964 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
3965 | // (from the FoldICmpLogical check above), that the two constants | ||||
3966 | // are not equal and that the larger constant is on the RHS | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3967 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
3968 | |||||
3969 | switch (LHSCC) { | ||||
3970 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3971 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3972 | switch (RHSCC) { |
3973 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3974 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
3975 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false | ||||
3976 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3977 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3978 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
3979 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 | ||||
3980 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3981 | return ReplaceInstUsesWith(I, LHS); |
3982 | } | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3983 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3984 | switch (RHSCC) { |
3985 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3986 | case ICmpInst::ICMP_ULT: |
3987 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 | ||||
3988 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); | ||||
3989 | break; // (X != 13 & X u< 15) -> no change | ||||
3990 | case ICmpInst::ICMP_SLT: | ||||
3991 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 | ||||
3992 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); | ||||
3993 | break; // (X != 13 & X s< 15) -> no change | ||||
3994 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 | ||||
3995 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 | ||||
3996 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3997 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3998 | case ICmpInst::ICMP_NE: |
3999 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4000 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
4001 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, | ||||
4002 | LHSVal->getName()+".off"); | ||||
4003 | InsertNewInstBefore(Add, I); | ||||
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 4004 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
4005 | ConstantInt::get(Add->getType(), 1)); | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4006 | } |
4007 | break; // (X != 13 & X != 15) -> no change | ||||
4008 | } | ||||
4009 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4010 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4011 | switch (RHSCC) { |
4012 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4013 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
4014 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4015 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4016 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
4017 | break; | ||||
4018 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 | ||||
4019 | 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] | 4020 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4021 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
4022 | break; | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4023 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4024 | break; |
4025 | case ICmpInst::ICMP_SLT: | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4026 | switch (RHSCC) { |
4027 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4028 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
4029 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4030 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4031 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
4032 | break; | ||||
4033 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 | ||||
4034 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4035 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4036 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
4037 | break; | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4038 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4039 | break; |
4040 | case ICmpInst::ICMP_UGT: | ||||
4041 | switch (RHSCC) { | ||||
4042 | default: assert(0 && "Unknown integer condition code!"); | ||||
4043 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X > 13 | ||||
4044 | return ReplaceInstUsesWith(I, LHS); | ||||
4045 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 | ||||
4046 | return ReplaceInstUsesWith(I, RHS); | ||||
4047 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change | ||||
4048 | break; | ||||
4049 | case ICmpInst::ICMP_NE: | ||||
4050 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 | ||||
4051 | return new ICmpInst(LHSCC, LHSVal, RHSCst); | ||||
4052 | break; // (X u> 13 & X != 15) -> no change | ||||
4053 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 | ||||
4054 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, | ||||
4055 | true, I); | ||||
4056 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change | ||||
4057 | break; | ||||
4058 | } | ||||
4059 | break; | ||||
4060 | case ICmpInst::ICMP_SGT: | ||||
4061 | switch (RHSCC) { | ||||
4062 | default: assert(0 && "Unknown integer condition code!"); | ||||
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 4063 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4064 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
4065 | return ReplaceInstUsesWith(I, RHS); | ||||
4066 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change | ||||
4067 | break; | ||||
4068 | case ICmpInst::ICMP_NE: | ||||
4069 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 | ||||
4070 | return new ICmpInst(LHSCC, LHSVal, RHSCst); | ||||
4071 | break; // (X s> 13 & X != 15) -> no change | ||||
4072 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 | ||||
4073 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, | ||||
4074 | true, I); | ||||
4075 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change | ||||
4076 | break; | ||||
4077 | } | ||||
4078 | break; | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 4079 | } |
4080 | } | ||||
4081 | } | ||||
4082 | |||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4083 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4084 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
4085 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) | ||||
4086 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? | ||||
4087 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | ||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4088 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4089 | // 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] | 4090 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
4091 | I.getType(), TD) && | ||||
4092 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | ||||
4093 | I.getType(), TD)) { | ||||
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4094 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), |
4095 | Op1C->getOperand(0), | ||||
4096 | I.getName()); | ||||
4097 | InsertNewInstBefore(NewOp, I); | ||||
4098 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); | ||||
4099 | } | ||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4100 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4101 | |
4102 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4103 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
4104 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) | ||||
4105 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && | ||||
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4106 | SI0->getOperand(1) == SI1->getOperand(1) && |
4107 | (SI0->hasOneUse() || SI1->hasOneUse())) { | ||||
4108 | Instruction *NewOp = | ||||
4109 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), | ||||
4110 | SI1->getOperand(0), | ||||
4111 | SI0->getName()), I); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4112 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
4113 | SI1->getOperand(1)); | ||||
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4114 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4115 | } |
4116 | |||||
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4117 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
4118 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { | ||||
4119 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { | ||||
4120 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && | ||||
4121 | RHS->getPredicate() == FCmpInst::FCMP_ORD) | ||||
4122 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) | ||||
4123 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { | ||||
4124 | // If either of the constants are nans, then the whole thing returns | ||||
4125 | // false. | ||||
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4126 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4127 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
4128 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), | ||||
4129 | RHS->getOperand(0)); | ||||
4130 | } | ||||
4131 | } | ||||
4132 | } | ||||
4133 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4134 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4135 | } |
4136 | |||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4137 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
4138 | /// in the result. If it does, and if the specified byte hasn't been filled in | ||||
4139 | /// yet, fill it in and return false. | ||||
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4140 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4141 | Instruction *I = dyn_cast<Instruction>(V); |
4142 | if (I == 0) return true; | ||||
4143 | |||||
4144 | // If this is an or instruction, it is an inner node of the bswap. | ||||
4145 | if (I->getOpcode() == Instruction::Or) | ||||
4146 | return CollectBSwapParts(I->getOperand(0), ByteValues) || | ||||
4147 | CollectBSwapParts(I->getOperand(1), ByteValues); | ||||
4148 | |||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4149 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4150 | // If this is a shift by a constant int, and it is "24", then its operand |
4151 | // defines a byte. We only handle unsigned types here. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4152 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4153 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4154 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4155 | 8*(ByteValues.size()-1)) |
4156 | return true; | ||||
4157 | |||||
4158 | unsigned DestNo; | ||||
4159 | if (I->getOpcode() == Instruction::Shl) { | ||||
4160 | // X << 24 defines the top byte with the lowest of the input bytes. | ||||
4161 | DestNo = ByteValues.size()-1; | ||||
4162 | } else { | ||||
4163 | // X >>u 24 defines the low byte with the highest of the input bytes. | ||||
4164 | DestNo = 0; | ||||
4165 | } | ||||
4166 | |||||
4167 | // If the destination byte value is already defined, the values are or'd | ||||
4168 | // together, which isn't a bswap (unless it's an or of the same bits). | ||||
4169 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) | ||||
4170 | return true; | ||||
4171 | ByteValues[DestNo] = I->getOperand(0); | ||||
4172 | return false; | ||||
4173 | } | ||||
4174 | |||||
4175 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we | ||||
4176 | // don't have this. | ||||
4177 | Value *Shift = 0, *ShiftLHS = 0; | ||||
4178 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; | ||||
4179 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || | ||||
4180 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) | ||||
4181 | return true; | ||||
4182 | Instruction *SI = cast<Instruction>(Shift); | ||||
4183 | |||||
4184 | // 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] | 4185 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
4186 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4187 | return true; |
4188 | |||||
4189 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. | ||||
4190 | unsigned DestByte; | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4191 | if (AndAmt->getValue().getActiveBits() > 64) |
4192 | return true; | ||||
4193 | uint64_t AndAmtVal = AndAmt->getZExtValue(); | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4194 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 4195 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4196 | break; |
4197 | // Unknown mask for bswap. | ||||
4198 | if (DestByte == ByteValues.size()) return true; | ||||
4199 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4200 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4201 | unsigned SrcByte; |
4202 | if (SI->getOpcode() == Instruction::Shl) | ||||
4203 | SrcByte = DestByte - ShiftBytes; | ||||
4204 | else | ||||
4205 | SrcByte = DestByte + ShiftBytes; | ||||
4206 | |||||
4207 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. | ||||
4208 | if (SrcByte != ByteValues.size()-DestByte-1) | ||||
4209 | return true; | ||||
4210 | |||||
4211 | // If the destination byte value is already defined, the values are or'd | ||||
4212 | // together, which isn't a bswap (unless it's an or of the same bits). | ||||
4213 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) | ||||
4214 | return true; | ||||
4215 | ByteValues[DestByte] = SI->getOperand(0); | ||||
4216 | return false; | ||||
4217 | } | ||||
4218 | |||||
4219 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. | ||||
4220 | /// If so, insert the new bswap intrinsic and return it. | ||||
4221 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { | ||||
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4222 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
4223 | if (!ITy || ITy->getBitWidth() % 16) | ||||
4224 | return 0; // Can only bswap pairs of bytes. Can't do vectors. | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4225 | |
4226 | /// ByteValues - For each byte of the result, we keep track of which value | ||||
4227 | /// defines each byte. | ||||
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 4228 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 4229 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4230 | |
4231 | // Try to find all the pieces corresponding to the bswap. | ||||
4232 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || | ||||
4233 | CollectBSwapParts(I.getOperand(1), ByteValues)) | ||||
4234 | return 0; | ||||
4235 | |||||
4236 | // Check to see if all of the bytes come from the same value. | ||||
4237 | Value *V = ByteValues[0]; | ||||
4238 | if (V == 0) return 0; // Didn't find a byte? Must be zero. | ||||
4239 | |||||
4240 | // Check to make sure that all of the bytes come from the same value. | ||||
4241 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) | ||||
4242 | if (ByteValues[i] != V) | ||||
4243 | return 0; | ||||
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4244 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4245 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4246 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4247 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4248 | } |
4249 | |||||
4250 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4251 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4252 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4253 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4254 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4255 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4256 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4257 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4258 | // or X, X = X |
4259 | if (Op0 == Op1) | ||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4260 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4261 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4262 | // See if we can simplify any instructions used by the instruction whose sole |
4263 | // purpose is to compute bits we don't care about. | ||||
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4264 | if (!isa<VectorType>(I.getType())) { |
4265 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | ||||
4266 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
4267 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
4268 | KnownZero, KnownOne)) | ||||
4269 | return &I; | ||||
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4270 | } else if (isa<ConstantAggregateZero>(Op1)) { |
4271 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X | ||||
4272 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { | ||||
4273 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> | ||||
4274 | return ReplaceInstUsesWith(I, I.getOperand(1)); | ||||
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4275 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4276 | |
4277 | |||||
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4278 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4279 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4280 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4281 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4282 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
4283 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4284 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4285 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4286 | Or->takeName(Op0); |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4287 | return BinaryOperator::createAnd(Or, |
4288 | ConstantInt::get(RHS->getValue() | C1->getValue())); | ||||
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4289 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4290 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4291 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
4292 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4293 | Instruction *Or = BinaryOperator::createOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4294 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4295 | Or->takeName(Op0); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4296 | return BinaryOperator::createXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4297 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4298 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4299 | |
4300 | // Try to fold constant and into select arguments. | ||||
4301 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4302 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4303 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4304 | if (isa<PHINode>(Op0)) |
4305 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
4306 | return NV; | ||||
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4307 | } |
4308 | |||||
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4309 | Value *A = 0, *B = 0; |
4310 | ConstantInt *C1 = 0, *C2 = 0; | ||||
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4311 | |
4312 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) | ||||
4313 | if (A == Op1 || B == Op1) // (A & ?) | A --> A | ||||
4314 | return ReplaceInstUsesWith(I, Op1); | ||||
4315 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) | ||||
4316 | if (A == Op0 || B == Op0) // A | (A & ?) --> A | ||||
4317 | return ReplaceInstUsesWith(I, Op0); | ||||
4318 | |||||
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4319 | // (A | B) | C and A | (B | C) -> bswap if possible. |
4320 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4321 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4322 | match(Op1, m_Or(m_Value(), m_Value())) || |
4323 | (match(Op0, m_Shift(m_Value(), m_Value())) && | ||||
4324 | match(Op1, m_Shift(m_Value(), m_Value())))) { | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4325 | if (Instruction *BSwap = MatchBSwap(I)) |
4326 | return BSwap; | ||||
4327 | } | ||||
4328 | |||||
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4329 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
4330 | 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] | 4331 | MaskedValueIsZero(Op1, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4332 | Instruction *NOr = BinaryOperator::createOr(A, Op1); |
4333 | InsertNewInstBefore(NOr, I); | ||||
4334 | NOr->takeName(Op0); | ||||
4335 | return BinaryOperator::createXor(NOr, C1); | ||||
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4336 | } |
4337 | |||||
4338 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 | ||||
4339 | 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] | 4340 | MaskedValueIsZero(Op0, C1->getValue())) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4341 | Instruction *NOr = BinaryOperator::createOr(A, Op0); |
4342 | InsertNewInstBefore(NOr, I); | ||||
4343 | NOr->takeName(Op0); | ||||
4344 | return BinaryOperator::createXor(NOr, C1); | ||||
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4345 | } |
4346 | |||||
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4347 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4348 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4349 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
4350 | match(Op1, m_And(m_Value(B), m_Value(D)))) { | ||||
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4351 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
4352 | C1 = dyn_cast<ConstantInt>(C); | ||||
4353 | C2 = dyn_cast<ConstantInt>(D); | ||||
4354 | if (C1 && C2) { // (A & C1)|(B & C2) | ||||
4355 | // If we have: ((V + N) & C1) | (V & C2) | ||||
4356 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 | ||||
4357 | // replace with V+N. | ||||
4358 | if (C1->getValue() == ~C2->getValue()) { | ||||
4359 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ | ||||
4360 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { | ||||
4361 | // Add commutes, try both ways. | ||||
4362 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) | ||||
4363 | return ReplaceInstUsesWith(I, A); | ||||
4364 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) | ||||
4365 | return ReplaceInstUsesWith(I, A); | ||||
4366 | } | ||||
4367 | // Or commutes, try both ways. | ||||
4368 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && | ||||
4369 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { | ||||
4370 | // Add commutes, try both ways. | ||||
4371 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) | ||||
4372 | return ReplaceInstUsesWith(I, B); | ||||
4373 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) | ||||
4374 | return ReplaceInstUsesWith(I, B); | ||||
4375 | } | ||||
4376 | } | ||||
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4377 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4378 | } |
4379 | |||||
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4380 | // Check to see if we have any common things being and'ed. If so, find the |
4381 | // terms for V1 & (V2|V3). | ||||
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4382 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
4383 | if (A == B) // (A & C)|(A & D) == A & (C|D) | ||||
4384 | V1 = A, V2 = C, V3 = D; | ||||
4385 | else if (A == D) // (A & C)|(B & A) == A & (B|C) | ||||
4386 | V1 = A, V2 = B, V3 = C; | ||||
4387 | else if (C == B) // (A & C)|(C & D) == C & (A|D) | ||||
4388 | V1 = C, V2 = A, V3 = D; | ||||
4389 | else if (C == D) // (A & C)|(B & C) == C & (A|B) | ||||
4390 | V1 = C, V2 = A, V3 = B; | ||||
4391 | |||||
4392 | if (V1) { | ||||
4393 | Value *Or = | ||||
4394 | InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I); | ||||
4395 | return BinaryOperator::createAnd(V1, Or); | ||||
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4396 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4397 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4398 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4399 | |
4400 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4401 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
4402 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) | ||||
4403 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && | ||||
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4404 | SI0->getOperand(1) == SI1->getOperand(1) && |
4405 | (SI0->hasOneUse() || SI1->hasOneUse())) { | ||||
4406 | Instruction *NewOp = | ||||
4407 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), | ||||
4408 | SI1->getOperand(0), | ||||
4409 | SI0->getName()), I); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4410 | return BinaryOperator::create(SI1->getOpcode(), NewOp, |
4411 | SI1->getOperand(1)); | ||||
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4412 | } |
4413 | } | ||||
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4414 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4415 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
4416 | if (A == Op1) // ~A | A == -1 | ||||
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4417 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4418 | } else { |
4419 | A = 0; | ||||
4420 | } | ||||
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4421 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4422 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
4423 | if (Op0 == B) | ||||
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4424 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4425 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4426 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4427 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
4428 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, | ||||
4429 | I.getName()+".demorgan"), I); | ||||
4430 | return BinaryOperator::createNot(And); | ||||
4431 | } | ||||
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4432 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4433 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4434 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
4435 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { | ||||
4436 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4437 | return R; |
4438 | |||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4439 | Value *LHSVal, *RHSVal; |
4440 | ConstantInt *LHSCst, *RHSCst; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4441 | ICmpInst::Predicate LHSCC, RHSCC; |
4442 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) | ||||
4443 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) | ||||
4444 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) | ||||
4445 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. | ||||
4446 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && | ||||
4447 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && | ||||
4448 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && | ||||
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4449 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
4450 | // We can't fold (ugt x, C) | (sgt x, C2). | ||||
4451 | PredicatesFoldable(LHSCC, RHSCC)) { | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4452 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4453 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4454 | bool NeedsSwap; |
4455 | if (ICmpInst::isSignedPredicate(LHSCC)) | ||||
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4456 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4457 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4458 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4459 | |
4460 | if (NeedsSwap) { | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4461 | std::swap(LHS, RHS); |
4462 | std::swap(LHSCst, RHSCst); | ||||
4463 | std::swap(LHSCC, RHSCC); | ||||
4464 | } | ||||
4465 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4466 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4467 | // comparing a value against two constants and or'ing the result |
4468 | // together. Because of the above check, we know that we only have | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4469 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
4470 | // FoldICmpLogical check above), that the two constants are not | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4471 | // equal. |
4472 | assert(LHSCst != RHSCst && "Compares not folded above?"); | ||||
4473 | |||||
4474 | switch (LHSCC) { | ||||
4475 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4476 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4477 | switch (RHSCC) { |
4478 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4479 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4480 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
4481 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); | ||||
4482 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, | ||||
4483 | LHSVal->getName()+".off"); | ||||
4484 | InsertNewInstBefore(Add, I); | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4485 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4486 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4487 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4488 | break; // (X == 13 | X == 15) -> no change |
4489 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change | ||||
4490 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change | ||||
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4491 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4492 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
4493 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 | ||||
4494 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4495 | return ReplaceInstUsesWith(I, RHS); |
4496 | } | ||||
4497 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4498 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4499 | switch (RHSCC) { |
4500 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4501 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
4502 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 | ||||
4503 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4504 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4505 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
4506 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true | ||||
4507 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4508 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4509 | } |
4510 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4511 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4512 | switch (RHSCC) { |
4513 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4514 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4515 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4516 | 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] | 4517 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
4518 | // this can cause overflow. | ||||
4519 | if (RHSCst->isMaxValue(false)) | ||||
4520 | return ReplaceInstUsesWith(I, LHS); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4521 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
4522 | false, I); | ||||
4523 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change | ||||
4524 | break; | ||||
4525 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 | ||||
4526 | 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] | 4527 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4528 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
4529 | break; | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4530 | } |
4531 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4532 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4533 | switch (RHSCC) { |
4534 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4535 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
4536 | break; | ||||
4537 | 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] | 4538 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
4539 | // this can cause overflow. | ||||
4540 | if (RHSCst->isMaxValue(true)) | ||||
4541 | return ReplaceInstUsesWith(I, LHS); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4542 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
4543 | false, I); | ||||
4544 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change | ||||
4545 | break; | ||||
4546 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 | ||||
4547 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 | ||||
4548 | return ReplaceInstUsesWith(I, RHS); | ||||
4549 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change | ||||
4550 | break; | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4551 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4552 | break; |
4553 | case ICmpInst::ICMP_UGT: | ||||
4554 | switch (RHSCC) { | ||||
4555 | default: assert(0 && "Unknown integer condition code!"); | ||||
4556 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 | ||||
4557 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 | ||||
4558 | return ReplaceInstUsesWith(I, LHS); | ||||
4559 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change | ||||
4560 | break; | ||||
4561 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true | ||||
4562 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4563 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4564 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
4565 | break; | ||||
4566 | } | ||||
4567 | break; | ||||
4568 | case ICmpInst::ICMP_SGT: | ||||
4569 | switch (RHSCC) { | ||||
4570 | default: assert(0 && "Unknown integer condition code!"); | ||||
4571 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 | ||||
4572 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 | ||||
4573 | return ReplaceInstUsesWith(I, LHS); | ||||
4574 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change | ||||
4575 | break; | ||||
4576 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true | ||||
4577 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4578 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4579 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
4580 | break; | ||||
4581 | } | ||||
4582 | break; | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4583 | } |
4584 | } | ||||
4585 | } | ||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4586 | |
4587 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) | ||||
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4588 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4589 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4590 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4591 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
4592 | !isa<ICmpInst>(Op1C->getOperand(0))) { | ||||
4593 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | ||||
4594 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && | ||||
4595 | // Only do this if the casts both really cause code to be | ||||
4596 | // generated. | ||||
4597 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), | ||||
4598 | I.getType(), TD) && | ||||
4599 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | ||||
4600 | I.getType(), TD)) { | ||||
4601 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), | ||||
4602 | Op1C->getOperand(0), | ||||
4603 | I.getName()); | ||||
4604 | InsertNewInstBefore(NewOp, I); | ||||
4605 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); | ||||
4606 | } | ||||
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4607 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4608 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4609 | } |
4610 | |||||
4611 | |||||
4612 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) | ||||
4613 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { | ||||
4614 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { | ||||
4615 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && | ||||
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4616 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
4617 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) | ||||
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4618 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
4619 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { | ||||
4620 | // If either of the constants are nans, then the whole thing returns | ||||
4621 | // true. | ||||
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4622 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4623 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
4624 | |||||
4625 | // Otherwise, no need to compare the two constants, compare the | ||||
4626 | // rest. | ||||
4627 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), | ||||
4628 | RHS->getOperand(0)); | ||||
4629 | } | ||||
4630 | } | ||||
4631 | } | ||||
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4632 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4633 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4634 | } |
4635 | |||||
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4636 | // XorSelf - Implements: X ^ X --> 0 |
4637 | struct XorSelf { | ||||
4638 | Value *RHS; | ||||
4639 | XorSelf(Value *rhs) : RHS(rhs) {} | ||||
4640 | bool shouldApply(Value *LHS) const { return LHS == RHS; } | ||||
4641 | Instruction *apply(BinaryOperator &Xor) const { | ||||
4642 | return &Xor; | ||||
4643 | } | ||||
4644 | }; | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4645 | |
4646 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4647 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4648 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4649 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4650 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4651 | if (isa<UndefValue>(Op1)) { |
4652 | if (isa<UndefValue>(Op0)) | ||||
4653 | // Handle undef ^ undef -> 0 special case. This is a common | ||||
4654 | // idiom (misuse). | ||||
4655 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4656 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4657 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4658 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4659 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
4660 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { | ||||
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4661 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4662 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4663 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4664 | |
4665 | // See if we can simplify any instructions used by the instruction whose sole | ||||
4666 | // purpose is to compute bits we don't care about. | ||||
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4667 | if (!isa<VectorType>(I.getType())) { |
4668 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | ||||
4669 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
4670 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
4671 | KnownZero, KnownOne)) | ||||
4672 | return &I; | ||||
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4673 | } else if (isa<ConstantAggregateZero>(Op1)) { |
4674 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X | ||||
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4675 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4676 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4677 | // Is this a ~ operation? |
4678 | if (Value *NotOp = dyn_castNotVal(&I)) { | ||||
4679 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law | ||||
4680 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law | ||||
4681 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { | ||||
4682 | if (Op0I->getOpcode() == Instruction::And || | ||||
4683 | Op0I->getOpcode() == Instruction::Or) { | ||||
4684 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); | ||||
4685 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { | ||||
4686 | Instruction *NotY = | ||||
4687 | BinaryOperator::createNot(Op0I->getOperand(1), | ||||
4688 | Op0I->getOperand(1)->getName()+".not"); | ||||
4689 | InsertNewInstBefore(NotY, I); | ||||
4690 | if (Op0I->getOpcode() == Instruction::And) | ||||
4691 | return BinaryOperator::createOr(Op0NotVal, NotY); | ||||
4692 | else | ||||
4693 | return BinaryOperator::createAnd(Op0NotVal, NotY); | ||||
4694 | } | ||||
4695 | } | ||||
4696 | } | ||||
4697 | } | ||||
4698 | |||||
4699 | |||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4700 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4701 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
4702 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { | ||||
4703 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4704 | return new ICmpInst(ICI->getInversePredicate(), |
4705 | ICI->getOperand(0), ICI->getOperand(1)); | ||||
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4706 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4707 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
4708 | return new FCmpInst(FCI->getInversePredicate(), | ||||
4709 | FCI->getOperand(0), FCI->getOperand(1)); | ||||
4710 | } | ||||
4711 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4712 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4713 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4714 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
4715 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4716 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
4717 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, | ||||
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4718 | ConstantInt::get(I.getType(), 1)); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4719 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4720 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4721 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4722 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4723 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4724 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4725 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4726 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
4727 | return BinaryOperator::createSub( | ||||
4728 | ConstantExpr::getSub(NegOp0CI, | ||||
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4729 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4730 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4731 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4732 | // (X + C) ^ signbit -> (X + C + signbit) |
4733 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); | ||||
4734 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); | ||||
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4735 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4736 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4737 | } else if (Op0I->getOpcode() == Instruction::Or) { |
4738 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 | ||||
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4739 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4740 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
4741 | // Anything in both C1 and C2 is known to be zero, remove it from | ||||
4742 | // NewRHS. | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4743 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4744 | NewRHS = ConstantExpr::getAnd(NewRHS, |
4745 | ConstantExpr::getNot(CommonBits)); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4746 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4747 | I.setOperand(0, Op0I->getOperand(0)); |
4748 | I.setOperand(1, NewRHS); | ||||
4749 | return &I; | ||||
4750 | } | ||||
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4751 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4752 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4753 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4754 | |
4755 | // Try to fold constant and into select arguments. | ||||
4756 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4757 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4758 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4759 | if (isa<PHINode>(Op0)) |
4760 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
4761 | return NV; | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4762 | } |
4763 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4764 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4765 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4766 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4767 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4768 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4769 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4770 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4771 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4772 | |
4773 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); | ||||
4774 | if (Op1I) { | ||||
4775 | Value *A, *B; | ||||
4776 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { | ||||
4777 | if (A == Op0) { // B^(B|A) == (A|B)^B | ||||
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4778 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4779 | I.swapOperands(); |
4780 | std::swap(Op0, Op1); | ||||
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4781 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4782 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4783 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4784 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4785 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
4786 | if (Op0 == A) // A^(A^B) == B | ||||
4787 | return ReplaceInstUsesWith(I, B); | ||||
4788 | else if (Op0 == B) // A^(B^A) == B | ||||
4789 | return ReplaceInstUsesWith(I, A); | ||||
4790 | } 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] | 4791 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4792 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4793 | std::swap(A, B); |
4794 | } | ||||
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4795 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4796 | I.swapOperands(); // Simplified below. |
4797 | std::swap(Op0, Op1); | ||||
4798 | } | ||||
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4799 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4800 | } |
4801 | |||||
4802 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); | ||||
4803 | if (Op0I) { | ||||
4804 | Value *A, *B; | ||||
4805 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { | ||||
4806 | if (A == Op1) // (B|A)^B == (A|B)^B | ||||
4807 | std::swap(A, B); | ||||
4808 | if (B == Op1) { // (A|B)^B == A & ~B | ||||
4809 | Instruction *NotB = | ||||
4810 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); | ||||
4811 | return BinaryOperator::createAnd(A, NotB); | ||||
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4812 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4813 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
4814 | if (Op1 == A) // (A^B)^A == B | ||||
4815 | return ReplaceInstUsesWith(I, B); | ||||
4816 | else if (Op1 == B) // (B^A)^A == B | ||||
4817 | return ReplaceInstUsesWith(I, A); | ||||
4818 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ | ||||
4819 | if (A == Op1) // (A&B)^A -> (B&A)^A | ||||
4820 | std::swap(A, B); | ||||
4821 | if (B == Op1 && // (B&A)^A == ~B & A | ||||
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4822 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4823 | Instruction *N = |
4824 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); | ||||
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4825 | return BinaryOperator::createAnd(N, Op1); |
4826 | } | ||||
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4827 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4828 | } |
4829 | |||||
4830 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. | ||||
4831 | if (Op0I && Op1I && Op0I->isShift() && | ||||
4832 | Op0I->getOpcode() == Op1I->getOpcode() && | ||||
4833 | Op0I->getOperand(1) == Op1I->getOperand(1) && | ||||
4834 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { | ||||
4835 | Instruction *NewOp = | ||||
4836 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), | ||||
4837 | Op1I->getOperand(0), | ||||
4838 | Op0I->getName()), I); | ||||
4839 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, | ||||
4840 | Op1I->getOperand(1)); | ||||
4841 | } | ||||
4842 | |||||
4843 | if (Op0I && Op1I) { | ||||
4844 | Value *A, *B, *C, *D; | ||||
4845 | // (A & B)^(A | B) -> A ^ B | ||||
4846 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && | ||||
4847 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { | ||||
4848 | if ((A == C && B == D) || (A == D && B == C)) | ||||
4849 | return BinaryOperator::createXor(A, B); | ||||
4850 | } | ||||
4851 | // (A | B)^(A & B) -> A ^ B | ||||
4852 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && | ||||
4853 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { | ||||
4854 | if ((A == C && B == D) || (A == D && B == C)) | ||||
4855 | return BinaryOperator::createXor(A, B); | ||||
4856 | } | ||||
4857 | |||||
4858 | // (A & B)^(C & D) | ||||
4859 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && | ||||
4860 | match(Op0I, m_And(m_Value(A), m_Value(B))) && | ||||
4861 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { | ||||
4862 | // (X & Y)^(X & Y) -> (Y^Z) & X | ||||
4863 | Value *X = 0, *Y = 0, *Z = 0; | ||||
4864 | if (A == C) | ||||
4865 | X = A, Y = B, Z = D; | ||||
4866 | else if (A == D) | ||||
4867 | X = A, Y = B, Z = C; | ||||
4868 | else if (B == C) | ||||
4869 | X = B, Y = A, Z = D; | ||||
4870 | else if (B == D) | ||||
4871 | X = B, Y = A, Z = C; | ||||
4872 | |||||
4873 | if (X) { | ||||
4874 | Instruction *NewOp = | ||||
4875 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); | ||||
4876 | return BinaryOperator::createAnd(NewOp, X); | ||||
4877 | } | ||||
4878 | } | ||||
4879 | } | ||||
4880 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4881 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
4882 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) | ||||
4883 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4884 | return R; |
4885 | |||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4886 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4887 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4888 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4889 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
4890 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | ||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4891 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4892 | // 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] | 4893 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
4894 | I.getType(), TD) && | ||||
4895 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | ||||
4896 | I.getType(), TD)) { | ||||
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4897 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), |
4898 | Op1C->getOperand(0), | ||||
4899 | I.getName()); | ||||
4900 | InsertNewInstBefore(NewOp, I); | ||||
4901 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); | ||||
4902 | } | ||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4903 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4904 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4905 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4906 | } |
4907 | |||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4908 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
4909 | /// overflowed for this type. | ||||
4910 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, | ||||
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4911 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4912 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4913 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4914 | if (IsSigned) |
4915 | if (In2->getValue().isNegative()) | ||||
4916 | return Result->getValue().sgt(In1->getValue()); | ||||
4917 | else | ||||
4918 | return Result->getValue().slt(In1->getValue()); | ||||
4919 | else | ||||
4920 | return Result->getValue().ult(In1->getValue()); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4921 | } |
4922 | |||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4923 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
4924 | /// code necessary to compute the offset from the base pointer (without adding | ||||
4925 | /// in the base pointer). Return the result as a signed integer of intptr size. | ||||
4926 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { | ||||
4927 | TargetData &TD = IC.getTargetData(); | ||||
4928 | gep_type_iterator GTI = gep_type_begin(GEP); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4929 | const Type *IntPtrTy = TD.getIntPtrType(); |
4930 | Value *Result = Constant::getNullValue(IntPtrTy); | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4931 | |
4932 | // Build a mask for high order bits. | ||||
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4933 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4934 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4935 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4936 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
4937 | Value *Op = GEP->getOperand(i); | ||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4938 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4939 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
4940 | if (OpC->isZero()) continue; | ||||
4941 | |||||
4942 | // Handle a struct index, which adds its field offset to the pointer. | ||||
4943 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
4944 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); | ||||
4945 | |||||
4946 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) | ||||
4947 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4948 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4949 | Result = IC.InsertNewInstBefore( |
4950 | BinaryOperator::createAdd(Result, | ||||
4951 | ConstantInt::get(IntPtrTy, Size), | ||||
4952 | GEP->getName()+".offs"), I); | ||||
4953 | continue; | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4954 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4955 | |
4956 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); | ||||
4957 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); | ||||
4958 | Scale = ConstantExpr::getMul(OC, Scale); | ||||
4959 | if (Constant *RC = dyn_cast<Constant>(Result)) | ||||
4960 | Result = ConstantExpr::getAdd(RC, Scale); | ||||
4961 | else { | ||||
4962 | // Emit an add instruction. | ||||
4963 | Result = IC.InsertNewInstBefore( | ||||
4964 | BinaryOperator::createAdd(Result, Scale, | ||||
4965 | GEP->getName()+".offs"), I); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4966 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4967 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4968 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4969 | // Convert to correct type. |
4970 | if (Op->getType() != IntPtrTy) { | ||||
4971 | if (Constant *OpC = dyn_cast<Constant>(Op)) | ||||
4972 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); | ||||
4973 | else | ||||
4974 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, | ||||
4975 | Op->getName()+".c"), I); | ||||
4976 | } | ||||
4977 | if (Size != 1) { | ||||
4978 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); | ||||
4979 | if (Constant *OpC = dyn_cast<Constant>(Op)) | ||||
4980 | Op = ConstantExpr::getMul(OpC, Scale); | ||||
4981 | else // We'll let instcombine(mul) convert this to a shl if possible. | ||||
4982 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, | ||||
4983 | GEP->getName()+".idx"), I); | ||||
4984 | } | ||||
4985 | |||||
4986 | // Emit an add instruction. | ||||
4987 | if (isa<Constant>(Op) && isa<Constant>(Result)) | ||||
4988 | Result = ConstantExpr::getAdd(cast<Constant>(Op), | ||||
4989 | cast<Constant>(Result)); | ||||
4990 | else | ||||
4991 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, | ||||
4992 | GEP->getName()+".offs"), I); | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4993 | } |
4994 | return Result; | ||||
4995 | } | ||||
4996 | |||||
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4997 | |
4998 | /// EvaluateGEPOffsetExpression - Return an value that can be used to compare of | ||||
4999 | /// the *offset* implied by GEP to zero. For example, if we have &A[i], we want | ||||
5000 | /// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be | ||||
5001 | /// complex, and scales are involved. The above expression would also be legal | ||||
5002 | /// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This | ||||
5003 | /// later form is less amenable to optimization though, and we are allowed to | ||||
5004 | /// generate the first by knowing that pointer arithmetic doesn't overflow. | ||||
5005 | /// | ||||
5006 | /// If we can't emit an optimized form for this expression, this returns null. | ||||
5007 | /// | ||||
5008 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, | ||||
5009 | InstCombiner &IC) { | ||||
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5010 | TargetData &TD = IC.getTargetData(); |
5011 | gep_type_iterator GTI = gep_type_begin(GEP); | ||||
5012 | |||||
5013 | // Check to see if this gep only has a single variable index. If so, and if | ||||
5014 | // any constant indices are a multiple of its scale, then we can compute this | ||||
5015 | // in terms of the scale of the variable index. For example, if the GEP | ||||
5016 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", | ||||
5017 | // because the expression will cross zero at the same point. | ||||
5018 | unsigned i, e = GEP->getNumOperands(); | ||||
5019 | int64_t Offset = 0; | ||||
5020 | for (i = 1; i != e; ++i, ++GTI) { | ||||
5021 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { | ||||
5022 | // Compute the aggregate offset of constant indices. | ||||
5023 | if (CI->isZero()) continue; | ||||
5024 | |||||
5025 | // Handle a struct index, which adds its field offset to the pointer. | ||||
5026 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
5027 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); | ||||
5028 | } else { | ||||
5029 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); | ||||
5030 | Offset += Size*CI->getSExtValue(); | ||||
5031 | } | ||||
5032 | } else { | ||||
5033 | // Found our variable index. | ||||
5034 | break; | ||||
5035 | } | ||||
5036 | } | ||||
5037 | |||||
5038 | // If there are no variable indices, we must have a constant offset, just | ||||
5039 | // evaluate it the general way. | ||||
5040 | if (i == e) return 0; | ||||
5041 | |||||
5042 | Value *VariableIdx = GEP->getOperand(i); | ||||
5043 | // Determine the scale factor of the variable element. For example, this is | ||||
5044 | // 4 if the variable index is into an array of i32. | ||||
5045 | uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType()); | ||||
5046 | |||||
5047 | // Verify that there are no other variable indices. If so, emit the hard way. | ||||
5048 | for (++i, ++GTI; i != e; ++i, ++GTI) { | ||||
5049 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); | ||||
5050 | if (!CI) return 0; | ||||
5051 | |||||
5052 | // Compute the aggregate offset of constant indices. | ||||
5053 | if (CI->isZero()) continue; | ||||
5054 | |||||
5055 | // Handle a struct index, which adds its field offset to the pointer. | ||||
5056 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
5057 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); | ||||
5058 | } else { | ||||
5059 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); | ||||
5060 | Offset += Size*CI->getSExtValue(); | ||||
5061 | } | ||||
5062 | } | ||||
5063 | |||||
5064 | // Okay, we know we have a single variable index, which must be a | ||||
5065 | // pointer/array/vector index. If there is no offset, life is simple, return | ||||
5066 | // the index. | ||||
5067 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); | ||||
5068 | if (Offset == 0) { | ||||
5069 | // Cast to intptrty in case a truncation occurs. If an extension is needed, | ||||
5070 | // we don't need to bother extending: the extension won't affect where the | ||||
5071 | // computation crosses zero. | ||||
5072 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) | ||||
5073 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), | ||||
5074 | VariableIdx->getNameStart(), &I); | ||||
5075 | return VariableIdx; | ||||
5076 | } | ||||
5077 | |||||
5078 | // Otherwise, there is an index. The computation we will do will be modulo | ||||
5079 | // the pointer size, so get it. | ||||
5080 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); | ||||
5081 | |||||
5082 | Offset &= PtrSizeMask; | ||||
5083 | VariableScale &= PtrSizeMask; | ||||
5084 | |||||
5085 | // To do this transformation, any constant index must be a multiple of the | ||||
5086 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", | ||||
5087 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a | ||||
5088 | // multiple of the variable scale. | ||||
5089 | int64_t NewOffs = Offset / (int64_t)VariableScale; | ||||
5090 | if (Offset != NewOffs*(int64_t)VariableScale) | ||||
5091 | return 0; | ||||
5092 | |||||
5093 | // Okay, we can do this evaluation. Start by converting the index to intptr. | ||||
5094 | const Type *IntPtrTy = TD.getIntPtrType(); | ||||
5095 | if (VariableIdx->getType() != IntPtrTy) | ||||
5096 | VariableIdx = CastInst::createIntegerCast(VariableIdx, IntPtrTy, | ||||
5097 | true /*SExt*/, | ||||
5098 | VariableIdx->getNameStart(), &I); | ||||
5099 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); | ||||
5100 | return BinaryOperator::createAdd(VariableIdx, OffsetVal, "offset", &I); | ||||
5101 | } | ||||
5102 | |||||
5103 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5104 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5105 | /// 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] | 5106 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
5107 | ICmpInst::Predicate Cond, | ||||
5108 | Instruction &I) { | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5109 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5110 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5111 | // Look through bitcasts. |
5112 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) | ||||
5113 | RHS = BCI->getOperand(0); | ||||
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5114 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5115 | Value *PtrBase = GEPLHS->getOperand(0); |
5116 | if (PtrBase == RHS) { | ||||
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5117 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5118 | // This transformation (ignoring the base and scales) is valid because we |
5119 | // know pointers can't overflow. See if we can output an optimized form. | ||||
5120 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); | ||||
5121 | |||||
5122 | // If not, synthesize the offset the hard way. | ||||
5123 | if (Offset == 0) | ||||
5124 | Offset = EmitGEPOffset(GEPLHS, I, *this); | ||||
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5125 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
5126 | Constant::getNullValue(Offset->getType())); | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5127 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5128 | // If the base pointers are different, but the indices are the same, just |
5129 | // compare the base pointer. | ||||
5130 | if (PtrBase != GEPRHS->getOperand(0)) { | ||||
5131 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); | ||||
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5132 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 5133 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5134 | if (IndicesTheSame) |
5135 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) | ||||
5136 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { | ||||
5137 | IndicesTheSame = false; | ||||
5138 | break; | ||||
5139 | } | ||||
5140 | |||||
5141 | // If all indices are the same, just compare the base pointers. | ||||
5142 | if (IndicesTheSame) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5143 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
5144 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); | ||||
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5145 | |
5146 | // Otherwise, the base pointers are different and the indices are | ||||
5147 | // different, bail out. | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5148 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 5149 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5150 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5151 | // If one of the GEPs has all zero indices, recurse. |
5152 | bool AllZeros = true; | ||||
5153 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) | ||||
5154 | if (!isa<Constant>(GEPLHS->getOperand(i)) || | ||||
5155 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { | ||||
5156 | AllZeros = false; | ||||
5157 | break; | ||||
5158 | } | ||||
5159 | if (AllZeros) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5160 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
5161 | ICmpInst::getSwappedPredicate(Cond), I); | ||||
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5162 | |
5163 | // If the other GEP has all zero indices, recurse. | ||||
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5164 | AllZeros = true; |
5165 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) | ||||
5166 | if (!isa<Constant>(GEPRHS->getOperand(i)) || | ||||
5167 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { | ||||
5168 | AllZeros = false; | ||||
5169 | break; | ||||
5170 | } | ||||
5171 | if (AllZeros) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5172 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 5173 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5174 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
5175 | // If the GEPs only differ by one index, compare it. | ||||
5176 | unsigned NumDifferences = 0; // Keep track of # differences. | ||||
5177 | unsigned DiffOperand = 0; // The operand that differs. | ||||
5178 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) | ||||
5179 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5180 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
5181 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { | ||||
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5182 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5183 | NumDifferences = 2; |
5184 | break; | ||||
5185 | } else { | ||||
5186 | if (NumDifferences++) break; | ||||
5187 | DiffOperand = i; | ||||
5188 | } | ||||
5189 | } | ||||
5190 | |||||
5191 | if (NumDifferences == 0) // SAME GEP? | ||||
5192 | return ReplaceInstUsesWith(I, // No comparison is needed here. | ||||
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5193 | ConstantInt::get(Type::Int1Ty, |
5194 | isTrueWhenEqual(Cond))); | ||||
5195 | |||||
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5196 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 5197 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
5198 | Value *RHSV = GEPRHS->getOperand(DiffOperand); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5199 | // Make sure we do a signed comparison here. |
5200 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); | ||||
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 5201 | } |
5202 | } | ||||
5203 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5204 | // 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] | 5205 | // the result to fold to a constant! |
5206 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && | ||||
5207 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { | ||||
5208 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) | ||||
5209 | Value *L = EmitGEPOffset(GEPLHS, I, *this); | ||||
5210 | Value *R = EmitGEPOffset(GEPRHS, I, *this); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5211 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5212 | } |
5213 | } | ||||
5214 | return 0; | ||||
5215 | } | ||||
5216 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5217 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
5218 | bool Changed = SimplifyCompare(I); | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5219 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5220 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5221 | // Fold trivial predicates. |
5222 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) | ||||
5223 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); | ||||
5224 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) | ||||
5225 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
5226 | |||||
5227 | // Simplify 'fcmp pred X, X' | ||||
5228 | if (Op0 == Op1) { | ||||
5229 | switch (I.getPredicate()) { | ||||
5230 | default: assert(0 && "Unknown predicate!"); | ||||
5231 | case FCmpInst::FCMP_UEQ: // True if unordered or equal | ||||
5232 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal | ||||
5233 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal | ||||
5234 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
5235 | case FCmpInst::FCMP_OGT: // True if ordered and greater than | ||||
5236 | case FCmpInst::FCMP_OLT: // True if ordered and less than | ||||
5237 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal | ||||
5238 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
5239 | |||||
5240 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) | ||||
5241 | case FCmpInst::FCMP_ULT: // True if unordered or less than | ||||
5242 | case FCmpInst::FCMP_UGT: // True if unordered or greater than | ||||
5243 | case FCmpInst::FCMP_UNE: // True if unordered or not equal | ||||
5244 | // Canonicalize these to be 'fcmp uno %X, 0.0'. | ||||
5245 | I.setPredicate(FCmpInst::FCMP_UNO); | ||||
5246 | I.setOperand(1, Constant::getNullValue(Op0->getType())); | ||||
5247 | return &I; | ||||
5248 | |||||
5249 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) | ||||
5250 | case FCmpInst::FCMP_OEQ: // True if ordered and equal | ||||
5251 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal | ||||
5252 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal | ||||
5253 | // Canonicalize these to be 'fcmp ord %X, 0.0'. | ||||
5254 | I.setPredicate(FCmpInst::FCMP_ORD); | ||||
5255 | I.setOperand(1, Constant::getNullValue(Op0->getType())); | ||||
5256 | return &I; | ||||
5257 | } | ||||
5258 | } | ||||
5259 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5260 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5261 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5262 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5263 | // Handle fcmp with constant RHS |
5264 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { | ||||
5265 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) | ||||
5266 | switch (LHSI->getOpcode()) { | ||||
5267 | case Instruction::PHI: | ||||
5268 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
5269 | return NV; | ||||
5270 | break; | ||||
5271 | case Instruction::Select: | ||||
5272 | // If either operand of the select is a constant, we can fold the | ||||
5273 | // comparison into the select arms, which will cause one to be | ||||
5274 | // constant folded and the select turned into a bitwise or. | ||||
5275 | Value *Op1 = 0, *Op2 = 0; | ||||
5276 | if (LHSI->hasOneUse()) { | ||||
5277 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { | ||||
5278 | // Fold the known value into the constant operand. | ||||
5279 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); | ||||
5280 | // Insert a new FCmp of the other select operand. | ||||
5281 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), | ||||
5282 | LHSI->getOperand(2), RHSC, | ||||
5283 | I.getName()), I); | ||||
5284 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { | ||||
5285 | // Fold the known value into the constant operand. | ||||
5286 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); | ||||
5287 | // Insert a new FCmp of the other select operand. | ||||
5288 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), | ||||
5289 | LHSI->getOperand(1), RHSC, | ||||
5290 | I.getName()), I); | ||||
5291 | } | ||||
5292 | } | ||||
5293 | |||||
5294 | if (Op1) | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5295 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5296 | break; |
5297 | } | ||||
5298 | } | ||||
5299 | |||||
5300 | return Changed ? &I : 0; | ||||
5301 | } | ||||
5302 | |||||
5303 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { | ||||
5304 | bool Changed = SimplifyCompare(I); | ||||
5305 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
5306 | const Type *Ty = Op0->getType(); | ||||
5307 | |||||
5308 | // icmp X, X | ||||
5309 | if (Op0 == Op1) | ||||
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5310 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
5311 | isTrueWhenEqual(I))); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5312 | |
5313 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef | ||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5314 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5315 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5316 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5317 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5318 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
5319 | isa<ConstantPointerNull>(Op0)) && | ||||
5320 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || | ||||
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5321 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5322 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
5323 | !isTrueWhenEqual(I))); | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5324 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5325 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5326 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5327 | switch (I.getPredicate()) { |
5328 | default: assert(0 && "Invalid icmp instruction!"); | ||||
5329 | case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B) | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 5330 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5331 | InsertNewInstBefore(Xor, I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5332 | return BinaryOperator::createNot(Xor); |
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_NE: // icmp eq bool %A, %B -> A^B |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5335 | return BinaryOperator::createXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5336 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5337 | case ICmpInst::ICMP_UGT: |
5338 | case ICmpInst::ICMP_SGT: | ||||
5339 | std::swap(Op0, Op1); // Change icmp gt -> icmp lt | ||||
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5340 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5341 | case ICmpInst::ICMP_ULT: |
5342 | case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y | ||||
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5343 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
5344 | InsertNewInstBefore(Not, I); | ||||
5345 | return BinaryOperator::createAnd(Not, Op1); | ||||
5346 | } | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5347 | case ICmpInst::ICMP_UGE: |
5348 | case ICmpInst::ICMP_SGE: | ||||
5349 | std::swap(Op0, Op1); // Change icmp ge -> icmp le | ||||
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5350 | // FALL THROUGH |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5351 | case ICmpInst::ICMP_ULE: |
5352 | case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B | ||||
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5353 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); |
5354 | InsertNewInstBefore(Not, I); | ||||
5355 | return BinaryOperator::createOr(Not, Op1); | ||||
5356 | } | ||||
5357 | } | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5358 | } |
5359 | |||||
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 5360 | // See if we are doing a comparison between a constant and an instruction that |
5361 | // can be folded into the comparison. | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5362 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5363 | Value *A, *B; |
5364 | |||||
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5365 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
5366 | if (I.isEquality() && CI->isNullValue() && | ||||
5367 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { | ||||
5368 | // (icmp cond A B) if cond is equality | ||||
5369 | return new ICmpInst(I.getPredicate(), A, B); | ||||
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5370 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5371 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5372 | switch (I.getPredicate()) { |
5373 | default: break; | ||||
5374 | case ICmpInst::ICMP_ULT: // A <u MIN -> FALSE | ||||
5375 | if (CI->isMinValue(false)) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5376 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5377 | if (CI->isMaxValue(false)) // A <u MAX -> A != MAX |
5378 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); | ||||
5379 | if (isMinValuePlusOne(CI,false)) // A <u MIN+1 -> A == MIN | ||||
5380 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); | ||||
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5381 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
5382 | if (CI->isMinValue(true)) | ||||
5383 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, | ||||
5384 | ConstantInt::getAllOnesValue(Op0->getType())); | ||||
5385 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5386 | break; |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5387 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5388 | case ICmpInst::ICMP_SLT: |
5389 | if (CI->isMinValue(true)) // A <s MIN -> FALSE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5390 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5391 | if (CI->isMaxValue(true)) // A <s MAX -> A != MAX |
5392 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
5393 | if (isMinValuePlusOne(CI,true)) // A <s MIN+1 -> A == MIN | ||||
5394 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); | ||||
5395 | break; | ||||
5396 | |||||
5397 | case ICmpInst::ICMP_UGT: | ||||
5398 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5399 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5400 | if (CI->isMinValue(false)) // A >u MIN -> A != MIN |
5401 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
5402 | if (isMaxValueMinusOne(CI, false)) // A >u MAX-1 -> A == MAX | ||||
5403 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); | ||||
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 5404 | |
5405 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set | ||||
5406 | if (CI->isMaxValue(true)) | ||||
5407 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, | ||||
5408 | ConstantInt::getNullValue(Op0->getType())); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5409 | break; |
5410 | |||||
5411 | case ICmpInst::ICMP_SGT: | ||||
5412 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5413 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5414 | if (CI->isMinValue(true)) // A >s MIN -> A != MIN |
5415 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
5416 | if (isMaxValueMinusOne(CI, true)) // A >s MAX-1 -> A == MAX | ||||
5417 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); | ||||
5418 | break; | ||||
5419 | |||||
5420 | case ICmpInst::ICMP_ULE: | ||||
5421 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5422 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5423 | if (CI->isMinValue(false)) // A <=u MIN -> A == MIN |
5424 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | ||||
5425 | if (isMaxValueMinusOne(CI,false)) // A <=u MAX-1 -> A != MAX | ||||
5426 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); | ||||
5427 | break; | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5428 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5429 | case ICmpInst::ICMP_SLE: |
5430 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5431 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5432 | if (CI->isMinValue(true)) // A <=s MIN -> A == MIN |
5433 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | ||||
5434 | if (isMaxValueMinusOne(CI,true)) // A <=s MAX-1 -> A != MAX | ||||
5435 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); | ||||
5436 | break; | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5437 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5438 | case ICmpInst::ICMP_UGE: |
5439 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5440 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5441 | if (CI->isMaxValue(false)) // A >=u MAX -> A == MAX |
5442 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | ||||
5443 | if (isMinValuePlusOne(CI,false)) // A >=u MIN-1 -> A != MIN | ||||
5444 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); | ||||
5445 | break; | ||||
5446 | |||||
5447 | case ICmpInst::ICMP_SGE: | ||||
5448 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5449 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5450 | if (CI->isMaxValue(true)) // A >=s MAX -> A == MAX |
5451 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | ||||
5452 | if (isMinValuePlusOne(CI,true)) // A >=s MIN-1 -> A != MIN | ||||
5453 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); | ||||
5454 | break; | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5455 | } |
5456 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5457 | // If we still have a icmp le or icmp ge instruction, turn it into the |
5458 | // appropriate icmp lt or icmp gt instruction. Since the border cases have | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 5459 | // already been handled above, this requires little checking. |
5460 | // | ||||
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5461 | switch (I.getPredicate()) { |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5462 | default: break; |
5463 | case ICmpInst::ICMP_ULE: | ||||
5464 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); | ||||
5465 | case ICmpInst::ICMP_SLE: | ||||
5466 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); | ||||
5467 | case ICmpInst::ICMP_UGE: | ||||
5468 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); | ||||
5469 | case ICmpInst::ICMP_SGE: | ||||
5470 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); | ||||
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 5471 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5472 | |
5473 | // 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] | 5474 | // in the input. If this comparison is a normal comparison, it demands all |
5475 | // bits, if it is a sign bit comparison, it only demands the sign bit. | ||||
5476 | |||||
5477 | bool UnusedBit; | ||||
5478 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); | ||||
5479 | |||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5480 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
5481 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5482 | if (SimplifyDemandedBits(Op0, |
5483 | isSignBit ? APInt::getSignBit(BitWidth) | ||||
5484 | : APInt::getAllOnesValue(BitWidth), | ||||
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5485 | KnownZero, KnownOne, 0)) |
5486 | return &I; | ||||
5487 | |||||
5488 | // Given the known and unknown bits, compute a range that the LHS could be | ||||
5489 | // in. | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5490 | if ((KnownOne | KnownZero) != 0) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5491 | // Compute the Min, Max and RHS values based on the known bits. For the |
5492 | // EQ and NE we use unsigned values. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5493 | APInt Min(BitWidth, 0), Max(BitWidth, 0); |
5494 | const APInt& RHSVal = CI->getValue(); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5495 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5496 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
5497 | Max); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5498 | } else { |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5499 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, |
5500 | Max); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5501 | } |
5502 | switch (I.getPredicate()) { // LE/GE have been folded already. | ||||
5503 | default: assert(0 && "Unknown icmp opcode!"); | ||||
5504 | case ICmpInst::ICMP_EQ: | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5505 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5506 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5507 | break; |
5508 | case ICmpInst::ICMP_NE: | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5509 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5510 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5511 | break; |
5512 | case ICmpInst::ICMP_ULT: | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5513 | if (Max.ult(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5514 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5515 | if (Min.uge(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5516 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5517 | break; |
5518 | case ICmpInst::ICMP_UGT: | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5519 | if (Min.ugt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5520 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5521 | if (Max.ule(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5522 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5523 | break; |
5524 | case ICmpInst::ICMP_SLT: | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5525 | if (Max.slt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5526 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5527 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5528 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5529 | break; |
5530 | case ICmpInst::ICMP_SGT: | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5531 | if (Min.sgt(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5532 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 81973ef | 2007-04-09 23:52:13 +0000 | [diff] [blame] | 5533 | if (Max.sle(RHSVal)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5534 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5535 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5536 | } |
5537 | } | ||||
5538 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5539 | // 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] | 5540 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5541 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5542 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5543 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
5544 | return Res; | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5545 | } |
5546 | |||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5547 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5548 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
5549 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) | ||||
5550 | switch (LHSI->getOpcode()) { | ||||
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5551 | case Instruction::GetElementPtr: |
5552 | if (RHSC->isNullValue()) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5553 | // 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] | 5554 | bool isAllZeros = true; |
5555 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) | ||||
5556 | if (!isa<Constant>(LHSI->getOperand(i)) || | ||||
5557 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { | ||||
5558 | isAllZeros = false; | ||||
5559 | break; | ||||
5560 | } | ||||
5561 | if (isAllZeros) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5562 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5563 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
5564 | } | ||||
5565 | break; | ||||
5566 | |||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5567 | case Instruction::PHI: |
5568 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
5569 | return NV; | ||||
5570 | break; | ||||
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5571 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5572 | // If either operand of the select is a constant, we can fold the |
5573 | // comparison into the select arms, which will cause one to be | ||||
5574 | // constant folded and the select turned into a bitwise or. | ||||
5575 | Value *Op1 = 0, *Op2 = 0; | ||||
5576 | if (LHSI->hasOneUse()) { | ||||
5577 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { | ||||
5578 | // Fold the known value into the constant operand. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5579 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
5580 | // Insert a new ICmp of the other select operand. | ||||
5581 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), | ||||
5582 | LHSI->getOperand(2), RHSC, | ||||
5583 | I.getName()), I); | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5584 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
5585 | // Fold the known value into the constant operand. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5586 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
5587 | // Insert a new ICmp of the other select operand. | ||||
5588 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), | ||||
5589 | LHSI->getOperand(1), RHSC, | ||||
5590 | I.getName()), I); | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5591 | } |
5592 | } | ||||
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5593 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5594 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5595 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5596 | break; |
5597 | } | ||||
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5598 | case Instruction::Malloc: |
5599 | // If we have (malloc != null), and if the malloc has a single use, we | ||||
5600 | // can assume it is successful and remove the malloc. | ||||
5601 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { | ||||
5602 | AddToWorkList(LHSI); | ||||
5603 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | ||||
5604 | !isTrueWhenEqual(I))); | ||||
5605 | } | ||||
5606 | break; | ||||
5607 | } | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5608 | } |
5609 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5610 | // 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] | 5611 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5612 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5613 | return NI; |
5614 | if (User *GEP = dyn_castGetElementPtr(Op1)) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5615 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
5616 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5617 | return NI; |
5618 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5619 | // 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] | 5620 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
5621 | // now. | ||||
5622 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { | ||||
5623 | if (isa<PointerType>(Op0->getType()) && | ||||
5624 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { | ||||
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5625 | // We keep moving the cast from the left operand over to the right |
5626 | // operand, where it can often be eliminated completely. | ||||
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5627 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5628 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5629 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
5630 | // so eliminate it as well. | ||||
5631 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) | ||||
5632 | Op1 = CI2->getOperand(0); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5633 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5634 | // 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] | 5635 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5636 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5637 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5638 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5639 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5640 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5641 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5642 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5643 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5644 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5645 | } |
5646 | |||||
5647 | if (isa<CastInst>(Op0)) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5648 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5649 | // This comes up when you have code like |
5650 | // int X = A < B; | ||||
5651 | // if (X) ... | ||||
5652 | // For generality, we handle any zero-extension of any operand comparison | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5653 | // with a constant or another cast from the same type. |
5654 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5655 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5656 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5657 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5658 | |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5659 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5660 | Value *A, *B, *C, *D; |
5661 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | ||||
5662 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 | ||||
5663 | Value *OtherVal = A == Op1 ? B : A; | ||||
5664 | return new ICmpInst(I.getPredicate(), OtherVal, | ||||
5665 | Constant::getNullValue(A->getType())); | ||||
5666 | } | ||||
5667 | |||||
5668 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { | ||||
5669 | // A^c1 == C^c2 --> A == C^(c1^c2) | ||||
5670 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) | ||||
5671 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) | ||||
5672 | if (Op1->hasOneUse()) { | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5673 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5674 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); |
5675 | return new ICmpInst(I.getPredicate(), A, | ||||
5676 | InsertNewInstBefore(Xor, I)); | ||||
5677 | } | ||||
5678 | |||||
5679 | // A^B == A^D -> B == D | ||||
5680 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); | ||||
5681 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); | ||||
5682 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); | ||||
5683 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); | ||||
5684 | } | ||||
5685 | } | ||||
5686 | |||||
5687 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && | ||||
5688 | (A == Op0 || B == Op0)) { | ||||
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5689 | // A == (A^B) -> B == 0 |
5690 | Value *OtherVal = A == Op0 ? B : A; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5691 | return new ICmpInst(I.getPredicate(), OtherVal, |
5692 | Constant::getNullValue(A->getType())); | ||||
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5693 | } |
5694 | 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] | 5695 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5696 | return new ICmpInst(I.getPredicate(), B, |
5697 | Constant::getNullValue(B->getType())); | ||||
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5698 | } |
5699 | 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] | 5700 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5701 | return new ICmpInst(I.getPredicate(), B, |
5702 | Constant::getNullValue(B->getType())); | ||||
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5703 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5704 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5705 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
5706 | if (Op0->hasOneUse() && Op1->hasOneUse() && | ||||
5707 | match(Op0, m_And(m_Value(A), m_Value(B))) && | ||||
5708 | match(Op1, m_And(m_Value(C), m_Value(D)))) { | ||||
5709 | Value *X = 0, *Y = 0, *Z = 0; | ||||
5710 | |||||
5711 | if (A == C) { | ||||
5712 | X = B; Y = D; Z = A; | ||||
5713 | } else if (A == D) { | ||||
5714 | X = B; Y = C; Z = A; | ||||
5715 | } else if (B == C) { | ||||
5716 | X = A; Y = D; Z = B; | ||||
5717 | } else if (B == D) { | ||||
5718 | X = A; Y = C; Z = B; | ||||
5719 | } | ||||
5720 | |||||
5721 | if (X) { // Build (X^Y) & Z | ||||
5722 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); | ||||
5723 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); | ||||
5724 | I.setOperand(0, Op1); | ||||
5725 | I.setOperand(1, Constant::getNullValue(Op1->getType())); | ||||
5726 | return &I; | ||||
5727 | } | ||||
5728 | } | ||||
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5729 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5730 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5731 | } |
5732 | |||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5733 | |
5734 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS | ||||
5735 | /// and CmpRHS are both known to be integer constants. | ||||
5736 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, | ||||
5737 | ConstantInt *DivRHS) { | ||||
5738 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); | ||||
5739 | const APInt &CmpRHSV = CmpRHS->getValue(); | ||||
5740 | |||||
5741 | // FIXME: If the operand types don't match the type of the divide | ||||
5742 | // then don't attempt this transform. The code below doesn't have the | ||||
5743 | // logic to deal with a signed divide and an unsigned compare (and | ||||
5744 | // vice versa). This is because (x /s C1) <s C2 produces different | ||||
5745 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even | ||||
5746 | // (x /u C1) <u C2. Simply casting the operands and result won't | ||||
5747 | // work. :( The if statement below tests that condition and bails | ||||
5748 | // if it finds it. | ||||
5749 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; | ||||
5750 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) | ||||
5751 | return 0; | ||||
5752 | if (DivRHS->isZero()) | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5753 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5754 | |
5755 | // Compute Prod = CI * DivRHS. We are essentially solving an equation | ||||
5756 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and | ||||
5757 | // C2 (CI). By solving for X we can turn this into a range check | ||||
5758 | // instead of computing a divide. | ||||
5759 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); | ||||
5760 | |||||
5761 | // Determine if the product overflows by seeing if the product is | ||||
5762 | // not equal to the divide. Make sure we do the same kind of divide | ||||
5763 | // as in the LHS instruction that we're folding. | ||||
5764 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : | ||||
5765 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; | ||||
5766 | |||||
5767 | // Get the ICmp opcode | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5768 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5769 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5770 | // Figure out the interval that is being checked. For example, a comparison |
5771 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). | ||||
5772 | // Compute this interval based on the constants involved and the signedness of | ||||
5773 | // the compare/divide. This computes a half-open interval, keeping track of | ||||
5774 | // whether either value in the interval overflows. After analysis each | ||||
5775 | // overflow variable is set to 0 if it's corresponding bound variable is valid | ||||
5776 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. | ||||
5777 | int LoOverflow = 0, HiOverflow = 0; | ||||
5778 | ConstantInt *LoBound = 0, *HiBound = 0; | ||||
5779 | |||||
5780 | |||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5781 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5782 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5783 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5784 | HiOverflow = LoOverflow = ProdOV; |
5785 | if (!HiOverflow) | ||||
5786 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); | ||||
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5787 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5788 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5789 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5790 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
5791 | HiBound = DivRHS; | ||||
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5792 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5793 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
5794 | HiOverflow = LoOverflow = ProdOV; | ||||
5795 | if (!HiOverflow) | ||||
5796 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5797 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5798 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5799 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
5800 | LoOverflow = AddWithOverflow(LoBound, Prod, | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5801 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5802 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5803 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5804 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5805 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5806 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5807 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5808 | LoBound = AddOne(DivRHS); |
5809 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5810 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
5811 | HiOverflow = 1; // [INTMIN+1, overflow) | ||||
5812 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN | ||||
5813 | } | ||||
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5814 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5815 | // e.g. X/-5 op 3 --> [-19, -14) |
5816 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5817 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5818 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5819 | HiBound = AddOne(Prod); |
5820 | } else { // (X / neg) op neg | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5821 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5822 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5823 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5824 | HiBound = Subtract(Prod, DivRHS); |
5825 | } | ||||
5826 | |||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5827 | // Dividing by a negative swaps the condition. LT <-> GT |
5828 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5829 | } |
5830 | |||||
5831 | Value *X = DivI->getOperand(0); | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5832 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5833 | default: assert(0 && "Unhandled icmp opcode!"); |
5834 | case ICmpInst::ICMP_EQ: | ||||
5835 | if (LoOverflow && HiOverflow) | ||||
5836 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | ||||
5837 | else if (HiOverflow) | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5838 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5839 | ICmpInst::ICMP_UGE, X, LoBound); |
5840 | else if (LoOverflow) | ||||
5841 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : | ||||
5842 | ICmpInst::ICMP_ULT, X, HiBound); | ||||
5843 | else | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5844 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5845 | case ICmpInst::ICMP_NE: |
5846 | if (LoOverflow && HiOverflow) | ||||
5847 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5848 | else if (HiOverflow) | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5849 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5850 | ICmpInst::ICMP_ULT, X, LoBound); |
5851 | else if (LoOverflow) | ||||
5852 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : | ||||
5853 | ICmpInst::ICMP_UGE, X, HiBound); | ||||
5854 | else | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5855 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5856 | case ICmpInst::ICMP_ULT: |
5857 | case ICmpInst::ICMP_SLT: | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5858 | if (LoOverflow == +1) // Low bound is greater than input range. |
5859 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5860 | if (LoOverflow == -1) // Low bound is less than input range. | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5861 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5862 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5863 | case ICmpInst::ICMP_UGT: |
5864 | case ICmpInst::ICMP_SGT: | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5865 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5866 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5867 | else if (HiOverflow == -1) // High bound less than input range. |
5868 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5869 | if (Pred == ICmpInst::ICMP_UGT) | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5870 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
5871 | else | ||||
5872 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); | ||||
5873 | } | ||||
5874 | } | ||||
5875 | |||||
5876 | |||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5877 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
5878 | /// | ||||
5879 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, | ||||
5880 | Instruction *LHSI, | ||||
5881 | ConstantInt *RHS) { | ||||
5882 | const APInt &RHSV = RHS->getValue(); | ||||
5883 | |||||
5884 | switch (LHSI->getOpcode()) { | ||||
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5885 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5886 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
5887 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), | ||||
5888 | // fold the xor. | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5889 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
5890 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5891 | Value *CompareVal = LHSI->getOperand(0); |
5892 | |||||
5893 | // If the sign bit of the XorCST is not set, there is no change to | ||||
5894 | // the operation, just stop using the Xor. | ||||
5895 | if (!XorCST->getValue().isNegative()) { | ||||
5896 | ICI.setOperand(0, CompareVal); | ||||
5897 | AddToWorkList(LHSI); | ||||
5898 | return &ICI; | ||||
5899 | } | ||||
5900 | |||||
5901 | // Was the old condition true if the operand is positive? | ||||
5902 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; | ||||
5903 | |||||
5904 | // If so, the new one isn't. | ||||
5905 | isTrueIfPositive ^= true; | ||||
5906 | |||||
5907 | if (isTrueIfPositive) | ||||
5908 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); | ||||
5909 | else | ||||
5910 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); | ||||
5911 | } | ||||
5912 | } | ||||
5913 | break; | ||||
5914 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) | ||||
5915 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && | ||||
5916 | LHSI->getOperand(0)->hasOneUse()) { | ||||
5917 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); | ||||
5918 | |||||
5919 | // If the LHS is an AND of a truncating cast, we can widen the | ||||
5920 | // and/compare to be the input width without changing the value | ||||
5921 | // produced, eliminating a cast. | ||||
5922 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { | ||||
5923 | // We can do this transformation if either the AND constant does not | ||||
5924 | // have its sign bit set or if it is an equality comparison. | ||||
5925 | // Extending a relational comparison when we're checking the sign | ||||
5926 | // bit would not work. | ||||
5927 | if (Cast->hasOneUse() && | ||||
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5928 | (ICI.isEquality() || |
5929 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5930 | uint32_t BitWidth = |
5931 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); | ||||
5932 | APInt NewCST = AndCST->getValue(); | ||||
5933 | NewCST.zext(BitWidth); | ||||
5934 | APInt NewCI = RHSV; | ||||
5935 | NewCI.zext(BitWidth); | ||||
5936 | Instruction *NewAnd = | ||||
5937 | BinaryOperator::createAnd(Cast->getOperand(0), | ||||
5938 | ConstantInt::get(NewCST),LHSI->getName()); | ||||
5939 | InsertNewInstBefore(NewAnd, ICI); | ||||
5940 | return new ICmpInst(ICI.getPredicate(), NewAnd, | ||||
5941 | ConstantInt::get(NewCI)); | ||||
5942 | } | ||||
5943 | } | ||||
5944 | |||||
5945 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare | ||||
5946 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This | ||||
5947 | // happens a LOT in code produced by the C front-end, for bitfield | ||||
5948 | // access. | ||||
5949 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); | ||||
5950 | if (Shift && !Shift->isShift()) | ||||
5951 | Shift = 0; | ||||
5952 | |||||
5953 | ConstantInt *ShAmt; | ||||
5954 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; | ||||
5955 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. | ||||
5956 | const Type *AndTy = AndCST->getType(); // Type of the and. | ||||
5957 | |||||
5958 | // We can fold this as long as we can't shift unknown bits | ||||
5959 | // into the mask. This can only happen with signed shift | ||||
5960 | // rights, as they sign-extend. | ||||
5961 | if (ShAmt) { | ||||
5962 | bool CanFold = Shift->isLogicalShift(); | ||||
5963 | if (!CanFold) { | ||||
5964 | // To test for the bad case of the signed shr, see if any | ||||
5965 | // of the bits shifted in could be tested after the mask. | ||||
5966 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); | ||||
5967 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); | ||||
5968 | |||||
5969 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); | ||||
5970 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & | ||||
5971 | AndCST->getValue()) == 0) | ||||
5972 | CanFold = true; | ||||
5973 | } | ||||
5974 | |||||
5975 | if (CanFold) { | ||||
5976 | Constant *NewCst; | ||||
5977 | if (Shift->getOpcode() == Instruction::Shl) | ||||
5978 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); | ||||
5979 | else | ||||
5980 | NewCst = ConstantExpr::getShl(RHS, ShAmt); | ||||
5981 | |||||
5982 | // Check to see if we are shifting out any of the bits being | ||||
5983 | // compared. | ||||
5984 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { | ||||
5985 | // If we shifted bits out, the fold is not going to work out. | ||||
5986 | // As a special case, check to see if this means that the | ||||
5987 | // result is always true or false now. | ||||
5988 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) | ||||
5989 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | ||||
5990 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) | ||||
5991 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5992 | } else { | ||||
5993 | ICI.setOperand(1, NewCst); | ||||
5994 | Constant *NewAndCST; | ||||
5995 | if (Shift->getOpcode() == Instruction::Shl) | ||||
5996 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); | ||||
5997 | else | ||||
5998 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); | ||||
5999 | LHSI->setOperand(1, NewAndCST); | ||||
6000 | LHSI->setOperand(0, Shift->getOperand(0)); | ||||
6001 | AddToWorkList(Shift); // Shift is dead. | ||||
6002 | AddUsesToWorkList(ICI); | ||||
6003 | return &ICI; | ||||
6004 | } | ||||
6005 | } | ||||
6006 | } | ||||
6007 | |||||
6008 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is | ||||
6009 | // preferable because it allows the C<<Y expression to be hoisted out | ||||
6010 | // of a loop if Y is invariant and X is not. | ||||
6011 | if (Shift && Shift->hasOneUse() && RHSV == 0 && | ||||
6012 | ICI.isEquality() && !Shift->isArithmeticShift() && | ||||
6013 | isa<Instruction>(Shift->getOperand(0))) { | ||||
6014 | // Compute C << Y. | ||||
6015 | Value *NS; | ||||
6016 | if (Shift->getOpcode() == Instruction::LShr) { | ||||
6017 | NS = BinaryOperator::createShl(AndCST, | ||||
6018 | Shift->getOperand(1), "tmp"); | ||||
6019 | } else { | ||||
6020 | // Insert a logical shift. | ||||
6021 | NS = BinaryOperator::createLShr(AndCST, | ||||
6022 | Shift->getOperand(1), "tmp"); | ||||
6023 | } | ||||
6024 | InsertNewInstBefore(cast<Instruction>(NS), ICI); | ||||
6025 | |||||
6026 | // Compute X & (C << Y). | ||||
6027 | Instruction *NewAnd = | ||||
6028 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); | ||||
6029 | InsertNewInstBefore(NewAnd, ICI); | ||||
6030 | |||||
6031 | ICI.setOperand(0, NewAnd); | ||||
6032 | return &ICI; | ||||
6033 | } | ||||
6034 | } | ||||
6035 | break; | ||||
6036 | |||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6037 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
6038 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); | ||||
6039 | if (!ShAmt) break; | ||||
6040 | |||||
6041 | uint32_t TypeBits = RHSV.getBitWidth(); | ||||
6042 | |||||
6043 | // Check that the shift amount is in range. If not, don't perform | ||||
6044 | // undefined shifts. When the shift is visited it will be | ||||
6045 | // simplified. | ||||
6046 | if (ShAmt->uge(TypeBits)) | ||||
6047 | break; | ||||
6048 | |||||
6049 | if (ICI.isEquality()) { | ||||
6050 | // If we are comparing against bits always shifted out, the | ||||
6051 | // comparison cannot succeed. | ||||
6052 | Constant *Comp = | ||||
6053 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); | ||||
6054 | if (Comp != RHS) {// Comparing against a bit that we know is zero. | ||||
6055 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | ||||
6056 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); | ||||
6057 | return ReplaceInstUsesWith(ICI, Cst); | ||||
6058 | } | ||||
6059 | |||||
6060 | if (LHSI->hasOneUse()) { | ||||
6061 | // Otherwise strength reduce the shift into an and. | ||||
6062 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); | ||||
6063 | Constant *Mask = | ||||
6064 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6065 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6066 | Instruction *AndI = |
6067 | BinaryOperator::createAnd(LHSI->getOperand(0), | ||||
6068 | Mask, LHSI->getName()+".mask"); | ||||
6069 | Value *And = InsertNewInstBefore(AndI, ICI); | ||||
6070 | return new ICmpInst(ICI.getPredicate(), And, | ||||
6071 | ConstantInt::get(RHSV.lshr(ShAmtVal))); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6072 | } |
6073 | } | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6074 | |
6075 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. | ||||
6076 | bool TrueIfSigned = false; | ||||
6077 | if (LHSI->hasOneUse() && | ||||
6078 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { | ||||
6079 | // (X << 31) <s 0 --> (X&1) != 0 | ||||
6080 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << | ||||
6081 | (TypeBits-ShAmt->getZExtValue()-1)); | ||||
6082 | Instruction *AndI = | ||||
6083 | BinaryOperator::createAnd(LHSI->getOperand(0), | ||||
6084 | Mask, LHSI->getName()+".mask"); | ||||
6085 | Value *And = InsertNewInstBefore(AndI, ICI); | ||||
6086 | |||||
6087 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, | ||||
6088 | And, Constant::getNullValue(And->getType())); | ||||
6089 | } | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6090 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6091 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6092 | |
6093 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6094 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6095 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6096 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6097 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6098 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6099 | // Check that the shift amount is in range. If not, don't perform |
6100 | // undefined shifts. When the shift is visited it will be | ||||
6101 | // simplified. | ||||
6102 | uint32_t TypeBits = RHSV.getBitWidth(); | ||||
6103 | if (ShAmt->uge(TypeBits)) | ||||
6104 | break; | ||||
6105 | |||||
6106 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6107 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6108 | // If we are comparing against bits always shifted out, the |
6109 | // comparison cannot succeed. | ||||
6110 | APInt Comp = RHSV << ShAmtVal; | ||||
6111 | if (LHSI->getOpcode() == Instruction::LShr) | ||||
6112 | Comp = Comp.lshr(ShAmtVal); | ||||
6113 | else | ||||
6114 | Comp = Comp.ashr(ShAmtVal); | ||||
6115 | |||||
6116 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. | ||||
6117 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | ||||
6118 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); | ||||
6119 | return ReplaceInstUsesWith(ICI, Cst); | ||||
6120 | } | ||||
6121 | |||||
6122 | // Otherwise, check to see if the bits shifted out are known to be zero. | ||||
6123 | // If so, we can compare against the unshifted value: | ||||
6124 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. | ||||
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6125 | if (LHSI->hasOneUse() && |
6126 | MaskedValueIsZero(LHSI->getOperand(0), | ||||
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6127 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
6128 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), | ||||
6129 | ConstantExpr::getShl(RHS, ShAmt)); | ||||
6130 | } | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6131 | |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6132 | if (LHSI->hasOneUse()) { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6133 | // Otherwise strength reduce the shift into an and. |
6134 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); | ||||
6135 | Constant *Mask = ConstantInt::get(Val); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6136 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6137 | Instruction *AndI = |
6138 | BinaryOperator::createAnd(LHSI->getOperand(0), | ||||
6139 | Mask, LHSI->getName()+".mask"); | ||||
6140 | Value *And = InsertNewInstBefore(AndI, ICI); | ||||
6141 | return new ICmpInst(ICI.getPredicate(), And, | ||||
6142 | ConstantExpr::getShl(RHS, ShAmt)); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6143 | } |
6144 | break; | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6145 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6146 | |
6147 | case Instruction::SDiv: | ||||
6148 | case Instruction::UDiv: | ||||
6149 | // Fold: icmp pred ([us]div X, C1), C2 -> range test | ||||
6150 | // Fold this div into the comparison, producing a range check. | ||||
6151 | // Determine, based on the divide type, what the range is being | ||||
6152 | // checked. If there is an overflow on the low or high side, remember | ||||
6153 | // it, otherwise compute the range [low, hi) bounding the new value. | ||||
6154 | // See: InsertRangeTest above for the kinds of replacements possible. | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6155 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
6156 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), | ||||
6157 | DivRHS)) | ||||
6158 | return R; | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6159 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 6160 | |
6161 | case Instruction::Add: | ||||
6162 | // Fold: icmp pred (add, X, C1), C2 | ||||
6163 | |||||
6164 | if (!ICI.isEquality()) { | ||||
6165 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); | ||||
6166 | if (!LHSC) break; | ||||
6167 | const APInt &LHSV = LHSC->getValue(); | ||||
6168 | |||||
6169 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) | ||||
6170 | .subtract(LHSV); | ||||
6171 | |||||
6172 | if (ICI.isSignedPredicate()) { | ||||
6173 | if (CR.getLower().isSignBit()) { | ||||
6174 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), | ||||
6175 | ConstantInt::get(CR.getUpper())); | ||||
6176 | } else if (CR.getUpper().isSignBit()) { | ||||
6177 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), | ||||
6178 | ConstantInt::get(CR.getLower())); | ||||
6179 | } | ||||
6180 | } else { | ||||
6181 | if (CR.getLower().isMinValue()) { | ||||
6182 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), | ||||
6183 | ConstantInt::get(CR.getUpper())); | ||||
6184 | } else if (CR.getUpper().isMinValue()) { | ||||
6185 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), | ||||
6186 | ConstantInt::get(CR.getLower())); | ||||
6187 | } | ||||
6188 | } | ||||
6189 | } | ||||
6190 | break; | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6191 | } |
6192 | |||||
6193 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. | ||||
6194 | if (ICI.isEquality()) { | ||||
6195 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | ||||
6196 | |||||
6197 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and | ||||
6198 | // the second operand is a constant, simplify a bit. | ||||
6199 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { | ||||
6200 | switch (BO->getOpcode()) { | ||||
6201 | case Instruction::SRem: | ||||
6202 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. | ||||
6203 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ | ||||
6204 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); | ||||
6205 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { | ||||
6206 | Instruction *NewRem = | ||||
6207 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), | ||||
6208 | BO->getName()); | ||||
6209 | InsertNewInstBefore(NewRem, ICI); | ||||
6210 | return new ICmpInst(ICI.getPredicate(), NewRem, | ||||
6211 | Constant::getNullValue(BO->getType())); | ||||
6212 | } | ||||
6213 | } | ||||
6214 | break; | ||||
6215 | case Instruction::Add: | ||||
6216 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. | ||||
6217 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { | ||||
6218 | if (BO->hasOneUse()) | ||||
6219 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | ||||
6220 | Subtract(RHS, BOp1C)); | ||||
6221 | } else if (RHSV == 0) { | ||||
6222 | // Replace ((add A, B) != 0) with (A != -B) if A or B is | ||||
6223 | // efficiently invertible, or if the add has just this one use. | ||||
6224 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); | ||||
6225 | |||||
6226 | if (Value *NegVal = dyn_castNegVal(BOp1)) | ||||
6227 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); | ||||
6228 | else if (Value *NegVal = dyn_castNegVal(BOp0)) | ||||
6229 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); | ||||
6230 | else if (BO->hasOneUse()) { | ||||
6231 | Instruction *Neg = BinaryOperator::createNeg(BOp1); | ||||
6232 | InsertNewInstBefore(Neg, ICI); | ||||
6233 | Neg->takeName(BO); | ||||
6234 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); | ||||
6235 | } | ||||
6236 | } | ||||
6237 | break; | ||||
6238 | case Instruction::Xor: | ||||
6239 | // For the xor case, we can xor two constants together, eliminating | ||||
6240 | // the explicit xor. | ||||
6241 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) | ||||
6242 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | ||||
6243 | ConstantExpr::getXor(RHS, BOC)); | ||||
6244 | |||||
6245 | // FALLTHROUGH | ||||
6246 | case Instruction::Sub: | ||||
6247 | // Replace (([sub|xor] A, B) != 0) with (A != B) | ||||
6248 | if (RHSV == 0) | ||||
6249 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | ||||
6250 | BO->getOperand(1)); | ||||
6251 | break; | ||||
6252 | |||||
6253 | case Instruction::Or: | ||||
6254 | // If bits are being or'd in that are not present in the constant we | ||||
6255 | // are comparing against, then the comparison could never succeed! | ||||
6256 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { | ||||
6257 | Constant *NotCI = ConstantExpr::getNot(RHS); | ||||
6258 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) | ||||
6259 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, | ||||
6260 | isICMP_NE)); | ||||
6261 | } | ||||
6262 | break; | ||||
6263 | |||||
6264 | case Instruction::And: | ||||
6265 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { | ||||
6266 | // If bits are being compared against that are and'd out, then the | ||||
6267 | // comparison can never succeed! | ||||
6268 | if ((RHSV & ~BOC->getValue()) != 0) | ||||
6269 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, | ||||
6270 | isICMP_NE)); | ||||
6271 | |||||
6272 | // If we have ((X & C) == C), turn it into ((X & C) != 0). | ||||
6273 | if (RHS == BOC && RHSV.isPowerOf2()) | ||||
6274 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : | ||||
6275 | ICmpInst::ICMP_NE, LHSI, | ||||
6276 | Constant::getNullValue(RHS->getType())); | ||||
6277 | |||||
6278 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 | ||||
6279 | if (isSignBit(BOC)) { | ||||
6280 | Value *X = BO->getOperand(0); | ||||
6281 | Constant *Zero = Constant::getNullValue(X->getType()); | ||||
6282 | ICmpInst::Predicate pred = isICMP_NE ? | ||||
6283 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; | ||||
6284 | return new ICmpInst(pred, X, Zero); | ||||
6285 | } | ||||
6286 | |||||
6287 | // ((X & ~7) == 0) --> X < 8 | ||||
6288 | if (RHSV == 0 && isHighOnes(BOC)) { | ||||
6289 | Value *X = BO->getOperand(0); | ||||
6290 | Constant *NegX = ConstantExpr::getNeg(BOC); | ||||
6291 | ICmpInst::Predicate pred = isICMP_NE ? | ||||
6292 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; | ||||
6293 | return new ICmpInst(pred, X, NegX); | ||||
6294 | } | ||||
6295 | } | ||||
6296 | default: break; | ||||
6297 | } | ||||
6298 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { | ||||
6299 | // Handle icmp {eq|ne} <intrinsic>, intcst. | ||||
6300 | if (II->getIntrinsicID() == Intrinsic::bswap) { | ||||
6301 | AddToWorkList(II); | ||||
6302 | ICI.setOperand(0, II->getOperand(1)); | ||||
6303 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); | ||||
6304 | return &ICI; | ||||
6305 | } | ||||
6306 | } | ||||
6307 | } else { // Not a ICMP_EQ/ICMP_NE | ||||
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6308 | // If the LHS is a cast from an integral value of the same size, |
6309 | // then since we know the RHS is a constant, try to simlify. | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6310 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
6311 | Value *CastOp = Cast->getOperand(0); | ||||
6312 | const Type *SrcTy = CastOp->getType(); | ||||
6313 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); | ||||
6314 | if (SrcTy->isInteger() && | ||||
6315 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { | ||||
6316 | // If this is an unsigned comparison, try to make the comparison use | ||||
6317 | // smaller constant values. | ||||
6318 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { | ||||
6319 | // X u< 128 => X s> -1 | ||||
6320 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, | ||||
6321 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); | ||||
6322 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && | ||||
6323 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { | ||||
6324 | // X u> 127 => X s< 0 | ||||
6325 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, | ||||
6326 | Constant::getNullValue(SrcTy)); | ||||
6327 | } | ||||
6328 | } | ||||
6329 | } | ||||
6330 | } | ||||
6331 | return 0; | ||||
6332 | } | ||||
6333 | |||||
6334 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). | ||||
6335 | /// We only handle extending casts so far. | ||||
6336 | /// | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6337 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
6338 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6339 | Value *LHSCIOp = LHSCI->getOperand(0); |
6340 | const Type *SrcTy = LHSCIOp->getType(); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6341 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6342 | Value *RHSCIOp; |
6343 | |||||
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6344 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
6345 | // integer type is the same size as the pointer type. | ||||
6346 | if (LHSCI->getOpcode() == Instruction::PtrToInt && | ||||
6347 | getTargetData().getPointerSizeInBits() == | ||||
6348 | cast<IntegerType>(DestTy)->getBitWidth()) { | ||||
6349 | Value *RHSOp = 0; | ||||
6350 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { | ||||
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6351 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6352 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
6353 | RHSOp = RHSC->getOperand(0); | ||||
6354 | // If the pointer types don't match, insert a bitcast. | ||||
6355 | if (LHSCIOp->getType() != RHSOp->getType()) | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6356 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6357 | } |
6358 | |||||
6359 | if (RHSOp) | ||||
6360 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); | ||||
6361 | } | ||||
6362 | |||||
6363 | // The code below only handles extension cast instructions, so far. | ||||
6364 | // Enforce this. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6365 | if (LHSCI->getOpcode() != Instruction::ZExt && |
6366 | LHSCI->getOpcode() != Instruction::SExt) | ||||
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6367 | return 0; |
6368 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6369 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
6370 | bool isSignedCmp = ICI.isSignedPredicate(); | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6371 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6372 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6373 | // Not an extension from the same type? |
6374 | RHSCIOp = CI->getOperand(0); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6375 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
6376 | return 0; | ||||
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6377 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6378 | // 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] | 6379 | // and the other is a zext), then we can't handle this. |
6380 | if (CI->getOpcode() != LHSCI->getOpcode()) | ||||
6381 | return 0; | ||||
6382 | |||||
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6383 | // Deal with equality cases early. |
6384 | if (ICI.isEquality()) | ||||
6385 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); | ||||
6386 | |||||
6387 | // A signed comparison of sign extended values simplifies into a | ||||
6388 | // signed comparison. | ||||
6389 | if (isSignedCmp && isSignedExt) | ||||
6390 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); | ||||
6391 | |||||
6392 | // The other three cases all fold into an unsigned comparison. | ||||
6393 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); | ||||
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6394 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6395 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6396 | // If we aren't dealing with a constant on the RHS, exit early |
6397 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); | ||||
6398 | if (!CI) | ||||
6399 | return 0; | ||||
6400 | |||||
6401 | // Compute the constant that would happen if we truncated to SrcTy then | ||||
6402 | // reextended to DestTy. | ||||
6403 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); | ||||
6404 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); | ||||
6405 | |||||
6406 | // If the re-extended constant didn't change... | ||||
6407 | if (Res2 == CI) { | ||||
6408 | // Make sure that sign of the Cmp and the sign of the Cast are the same. | ||||
6409 | // For example, we might have: | ||||
6410 | // %A = sext short %X to uint | ||||
6411 | // %B = icmp ugt uint %A, 1330 | ||||
6412 | // It is incorrect to transform this into | ||||
6413 | // %B = icmp ugt short %X, 1330 | ||||
6414 | // because %A may have negative value. | ||||
6415 | // | ||||
6416 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) | ||||
6417 | // OR operation is EQ/NE. | ||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6418 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6419 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
6420 | else | ||||
6421 | return 0; | ||||
6422 | } | ||||
6423 | |||||
6424 | // The re-extended constant changed so the constant cannot be represented | ||||
6425 | // in the shorter type. Consequently, we cannot emit a simple comparison. | ||||
6426 | |||||
6427 | // First, handle some easy cases. We know the result cannot be equal at this | ||||
6428 | // point so handle the ICI.isEquality() cases | ||||
6429 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6430 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6431 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6432 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6433 | |
6434 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases | ||||
6435 | // should have been folded away previously and not enter in here. | ||||
6436 | Value *Result; | ||||
6437 | if (isSignedCmp) { | ||||
6438 | // We're performing a signed comparison. | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6439 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6440 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6441 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6442 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6443 | } else { |
6444 | // We're performing an unsigned comparison. | ||||
6445 | if (isSignedExt) { | ||||
6446 | // We're performing an unsigned comp with a sign extended value. | ||||
6447 | // This is true if the input is >= 0. [aka >s -1] | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6448 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6449 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
6450 | NegOne, ICI.getName()), ICI); | ||||
6451 | } else { | ||||
6452 | // Unsigned extend & unsigned compare -> always true. | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6453 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6454 | } |
6455 | } | ||||
6456 | |||||
6457 | // Finally, return the value computed. | ||||
6458 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || | ||||
6459 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { | ||||
6460 | return ReplaceInstUsesWith(ICI, Result); | ||||
6461 | } else { | ||||
6462 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || | ||||
6463 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && | ||||
6464 | "ICmp should be folded!"); | ||||
6465 | if (Constant *CI = dyn_cast<Constant>(Result)) | ||||
6466 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); | ||||
6467 | else | ||||
6468 | return BinaryOperator::createNot(Result); | ||||
6469 | } | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6470 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6471 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6472 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
6473 | return commonShiftTransforms(I); | ||||
6474 | } | ||||
6475 | |||||
6476 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { | ||||
6477 | return commonShiftTransforms(I); | ||||
6478 | } | ||||
6479 | |||||
6480 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { | ||||
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6481 | if (Instruction *R = commonShiftTransforms(I)) |
6482 | return R; | ||||
6483 | |||||
6484 | Value *Op0 = I.getOperand(0); | ||||
6485 | |||||
6486 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) | ||||
6487 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) | ||||
6488 | if (CSI->isAllOnesValue()) | ||||
6489 | return ReplaceInstUsesWith(I, CSI); | ||||
6490 | |||||
6491 | // See if we can turn a signed shr into an unsigned shr. | ||||
6492 | if (MaskedValueIsZero(Op0, | ||||
6493 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) | ||||
6494 | return BinaryOperator::createLShr(Op0, I.getOperand(1)); | ||||
6495 | |||||
6496 | return 0; | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6497 | } |
6498 | |||||
6499 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { | ||||
6500 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6501 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6502 | |
6503 | // shl X, 0 == X and shr X, 0 == X | ||||
6504 | // shl 0, X == 0 and shr 0, X == 0 | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6505 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6506 | Op0 == Constant::getNullValue(Op0->getType())) |
6507 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6508 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6509 | if (isa<UndefValue>(Op0)) { |
6510 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef | ||||
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6511 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6512 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6513 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
6514 | } | ||||
6515 | if (isa<UndefValue>(Op1)) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6516 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
6517 | return ReplaceInstUsesWith(I, Op0); | ||||
6518 | else // X << undef, X >>u undef -> 0 | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6519 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6520 | } |
6521 | |||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6522 | // Try to fold constant and into select arguments. |
6523 | if (isa<Constant>(Op0)) | ||||
6524 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6525 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6526 | return R; |
6527 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6528 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6529 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
6530 | return Res; | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6531 | return 0; |
6532 | } | ||||
6533 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6534 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6535 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6536 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6537 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6538 | // See if we can simplify any instructions used by the instruction whose sole |
6539 | // purpose is to compute bits we don't care about. | ||||
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6540 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
6541 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); | ||||
6542 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), | ||||
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6543 | KnownZero, KnownOne)) |
6544 | return &I; | ||||
6545 | |||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6546 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
6547 | // of a signed value. | ||||
6548 | // | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6549 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6550 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6551 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
6552 | else { | ||||
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6553 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6554 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6555 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6556 | } |
6557 | |||||
6558 | // ((X*C1) << C2) == (X * (C1 << C2)) | ||||
6559 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) | ||||
6560 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) | ||||
6561 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) | ||||
6562 | return BinaryOperator::createMul(BO->getOperand(0), | ||||
6563 | ConstantExpr::getShl(BOOp, Op1)); | ||||
6564 | |||||
6565 | // Try to fold constant and into select arguments. | ||||
6566 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
6567 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | ||||
6568 | return R; | ||||
6569 | if (isa<PHINode>(Op0)) | ||||
6570 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
6571 | return NV; | ||||
6572 | |||||
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6573 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
6574 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { | ||||
6575 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); | ||||
6576 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny | ||||
6577 | // place. Don't try to do this transformation in this case. Also, we | ||||
6578 | // require that the input operand is a shift-by-constant so that we have | ||||
6579 | // confidence that the shifts will get folded together. We could do this | ||||
6580 | // xform in more cases, but it is unlikely to be profitable. | ||||
6581 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && | ||||
6582 | isa<ConstantInt>(TrOp->getOperand(1))) { | ||||
6583 | // Okay, we'll do this xform. Make the shift of shift. | ||||
6584 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); | ||||
6585 | Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt, | ||||
6586 | I.getName()); | ||||
6587 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) | ||||
6588 | |||||
6589 | // For logical shifts, the truncation has the effect of making the high | ||||
6590 | // part of the register be zeros. Emulate this by inserting an AND to | ||||
6591 | // clear the top bits as needed. This 'and' will usually be zapped by | ||||
6592 | // other xforms later if dead. | ||||
6593 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); | ||||
6594 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); | ||||
6595 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); | ||||
6596 | |||||
6597 | // The mask we constructed says what the trunc would do if occurring | ||||
6598 | // between the shifts. We want to know the effect *after* the second | ||||
6599 | // shift. We know that it is a logical shift by a constant, so adjust the | ||||
6600 | // mask as appropriate. | ||||
6601 | if (I.getOpcode() == Instruction::Shl) | ||||
6602 | MaskV <<= Op1->getZExtValue(); | ||||
6603 | else { | ||||
6604 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); | ||||
6605 | MaskV = MaskV.lshr(Op1->getZExtValue()); | ||||
6606 | } | ||||
6607 | |||||
6608 | Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV), | ||||
6609 | TI->getName()); | ||||
6610 | InsertNewInstBefore(And, I); // shift1 & 0x00FF | ||||
6611 | |||||
6612 | // Return the value truncated to the interesting size. | ||||
6613 | return new TruncInst(And, I.getType()); | ||||
6614 | } | ||||
6615 | } | ||||
6616 | |||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6617 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6618 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
6619 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) | ||||
6620 | Value *V1, *V2; | ||||
6621 | ConstantInt *CC; | ||||
6622 | switch (Op0BO->getOpcode()) { | ||||
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6623 | default: break; |
6624 | case Instruction::Add: | ||||
6625 | case Instruction::And: | ||||
6626 | case Instruction::Or: | ||||
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6627 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6628 | // These operators commute. |
6629 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6630 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
6631 | match(Op0BO->getOperand(1), | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6632 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6633 | Instruction *YS = BinaryOperator::createShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6634 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6635 | Op0BO->getName()); |
6636 | InsertNewInstBefore(YS, I); // (Y << C) | ||||
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6637 | Instruction *X = |
6638 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, | ||||
6639 | Op0BO->getOperand(1)->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6640 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6641 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6642 | return BinaryOperator::createAnd(X, ConstantInt::get( |
6643 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6644 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6645 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6646 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6647 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6648 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6649 | match(Op0BOOp1, |
6650 | 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] | 6651 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
6652 | V2 == Op1) { | ||||
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6653 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6654 | Op0BO->getOperand(0), Op1, |
6655 | Op0BO->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6656 | InsertNewInstBefore(YS, I); // (Y << C) |
6657 | Instruction *XM = | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6658 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6659 | V1->getName()+".mask"); |
6660 | InsertNewInstBefore(XM, I); // X & (CC << C) | ||||
6661 | |||||
6662 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); | ||||
6663 | } | ||||
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6664 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6665 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6666 | // FALL THROUGH. |
6667 | case Instruction::Sub: { | ||||
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6668 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6669 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
6670 | match(Op0BO->getOperand(0), | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6671 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6672 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6673 | Op0BO->getOperand(1), Op1, |
6674 | Op0BO->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6675 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6676 | Instruction *X = |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6677 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6678 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6679 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6680 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6681 | return BinaryOperator::createAnd(X, ConstantInt::get( |
6682 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6683 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6684 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6685 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6686 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
6687 | match(Op0BO->getOperand(0), | ||||
6688 | m_And(m_Shr(m_Value(V1), m_Value(V2)), | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6689 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6690 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
6691 | ->getOperand(0)->hasOneUse()) { | ||||
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6692 | Instruction *YS = BinaryOperator::createShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6693 | Op0BO->getOperand(1), Op1, |
6694 | Op0BO->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6695 | InsertNewInstBefore(YS, I); // (Y << C) |
6696 | Instruction *XM = | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6697 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6698 | V1->getName()+".mask"); |
6699 | InsertNewInstBefore(XM, I); // X & (CC << C) | ||||
6700 | |||||
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6701 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6702 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6703 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6704 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6705 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6706 | } |
6707 | |||||
6708 | |||||
6709 | // If the operand is an bitwise operator with a constant RHS, and the | ||||
6710 | // shift is the only use, we can pull it out of the shift. | ||||
6711 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { | ||||
6712 | bool isValid = true; // Valid only for And, Or, Xor | ||||
6713 | bool highBitSet = false; // Transform if high bit of constant set? | ||||
6714 | |||||
6715 | switch (Op0BO->getOpcode()) { | ||||
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6716 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6717 | case Instruction::Add: |
6718 | isValid = isLeftShift; | ||||
6719 | break; | ||||
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6720 | case Instruction::Or: |
6721 | case Instruction::Xor: | ||||
6722 | highBitSet = false; | ||||
6723 | break; | ||||
6724 | case Instruction::And: | ||||
6725 | highBitSet = true; | ||||
6726 | break; | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6727 | } |
6728 | |||||
6729 | // If this is a signed shift right, and the high bit is modified | ||||
6730 | // by the logical operation, do not perform the transformation. | ||||
6731 | // The highBitSet boolean indicates the value of the high bit of | ||||
6732 | // the constant which would cause it to be modified for this | ||||
6733 | // operation. | ||||
6734 | // | ||||
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6735 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6736 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6737 | |
6738 | if (isValid) { | ||||
6739 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); | ||||
6740 | |||||
6741 | Instruction *NewShift = | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6742 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6743 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6744 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6745 | |
6746 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, | ||||
6747 | NewRHS); | ||||
6748 | } | ||||
6749 | } | ||||
6750 | } | ||||
6751 | } | ||||
6752 | |||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6753 | // 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] | 6754 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
6755 | if (ShiftOp && !ShiftOp->isShift()) | ||||
6756 | ShiftOp = 0; | ||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6757 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6758 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6759 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6760 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
6761 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); | ||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6762 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
6763 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. | ||||
6764 | Value *X = ShiftOp->getOperand(0); | ||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6765 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6766 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6767 | if (AmtSum > TypeBits) |
6768 | AmtSum = TypeBits; | ||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6769 | |
6770 | const IntegerType *Ty = cast<IntegerType>(I.getType()); | ||||
6771 | |||||
6772 | // Check for (X << c1) << c2 and (X >> c1) >> c2 | ||||
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6773 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6774 | return BinaryOperator::create(I.getOpcode(), X, |
6775 | ConstantInt::get(Ty, AmtSum)); | ||||
6776 | } else if (ShiftOp->getOpcode() == Instruction::LShr && | ||||
6777 | I.getOpcode() == Instruction::AShr) { | ||||
6778 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. | ||||
6779 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); | ||||
6780 | } else if (ShiftOp->getOpcode() == Instruction::AShr && | ||||
6781 | I.getOpcode() == Instruction::LShr) { | ||||
6782 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. | ||||
6783 | Instruction *Shift = | ||||
6784 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); | ||||
6785 | InsertNewInstBefore(Shift, I); | ||||
6786 | |||||
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6787 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6788 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6789 | } |
6790 | |||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6791 | // Okay, if we get here, one shift must be left, and the other shift must be |
6792 | // right. See if the amounts are equal. | ||||
6793 | if (ShiftAmt1 == ShiftAmt2) { | ||||
6794 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). | ||||
6795 | if (I.getOpcode() == Instruction::Shl) { | ||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6796 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6797 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6798 | } |
6799 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). | ||||
6800 | if (I.getOpcode() == Instruction::LShr) { | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6801 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6802 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6803 | } |
6804 | // We can simplify ((X << C) >>s C) into a trunc + sext. | ||||
6805 | // NOTE: we could do this for any C, but that would make 'unusual' integer | ||||
6806 | // types. For now, just stick to ones well-supported by the code | ||||
6807 | // generators. | ||||
6808 | const Type *SExtType = 0; | ||||
6809 | switch (Ty->getBitWidth() - ShiftAmt1) { | ||||
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6810 | case 1 : |
6811 | case 8 : | ||||
6812 | case 16 : | ||||
6813 | case 32 : | ||||
6814 | case 64 : | ||||
6815 | case 128: | ||||
6816 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); | ||||
6817 | break; | ||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6818 | default: break; |
6819 | } | ||||
6820 | if (SExtType) { | ||||
6821 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); | ||||
6822 | InsertNewInstBefore(NewTrunc, I); | ||||
6823 | return new SExtInst(NewTrunc, Ty); | ||||
6824 | } | ||||
6825 | // Otherwise, we can't handle it yet. | ||||
6826 | } else if (ShiftAmt1 < ShiftAmt2) { | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6827 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6828 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6829 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6830 | if (I.getOpcode() == Instruction::Shl) { |
6831 | assert(ShiftOp->getOpcode() == Instruction::LShr || | ||||
6832 | ShiftOp->getOpcode() == Instruction::AShr); | ||||
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6833 | Instruction *Shift = |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6834 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6835 | InsertNewInstBefore(Shift, I); |
6836 | |||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6837 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
6838 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); | ||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6839 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6840 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6841 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6842 | if (I.getOpcode() == Instruction::LShr) { |
6843 | assert(ShiftOp->getOpcode() == Instruction::Shl); | ||||
6844 | Instruction *Shift = | ||||
6845 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); | ||||
6846 | InsertNewInstBefore(Shift, I); | ||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6847 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6848 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6849 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6850 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6851 | |
6852 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. | ||||
6853 | } else { | ||||
6854 | assert(ShiftAmt2 < ShiftAmt1); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6855 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6856 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6857 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6858 | if (I.getOpcode() == Instruction::Shl) { |
6859 | assert(ShiftOp->getOpcode() == Instruction::LShr || | ||||
6860 | ShiftOp->getOpcode() == Instruction::AShr); | ||||
6861 | Instruction *Shift = | ||||
6862 | BinaryOperator::create(ShiftOp->getOpcode(), X, | ||||
6863 | ConstantInt::get(Ty, ShiftDiff)); | ||||
6864 | InsertNewInstBefore(Shift, I); | ||||
6865 | |||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6866 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6867 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6868 | } |
6869 | |||||
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6870 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6871 | if (I.getOpcode() == Instruction::LShr) { |
6872 | assert(ShiftOp->getOpcode() == Instruction::Shl); | ||||
6873 | Instruction *Shift = | ||||
6874 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); | ||||
6875 | InsertNewInstBefore(Shift, I); | ||||
6876 | |||||
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6877 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6878 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6879 | } |
6880 | |||||
6881 | // 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] | 6882 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6883 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6884 | return 0; |
6885 | } | ||||
6886 | |||||
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6887 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6888 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
6889 | /// expression. If so, decompose it, returning some value X, such that Val is | ||||
6890 | /// X*Scale+Offset. | ||||
6891 | /// | ||||
6892 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, | ||||
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6893 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6894 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6895 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6896 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6897 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6898 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6899 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
6900 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
6901 | if (I->getOpcode() == Instruction::Shl) { | ||||
6902 | // This is a value scaled by '1 << the shift amt'. | ||||
6903 | Scale = 1U << RHS->getZExtValue(); | ||||
6904 | Offset = 0; | ||||
6905 | return I->getOperand(0); | ||||
6906 | } else if (I->getOpcode() == Instruction::Mul) { | ||||
6907 | // This value is scaled by 'RHS'. | ||||
6908 | Scale = RHS->getZExtValue(); | ||||
6909 | Offset = 0; | ||||
6910 | return I->getOperand(0); | ||||
6911 | } else if (I->getOpcode() == Instruction::Add) { | ||||
6912 | // We have X+C. Check to see if we really have (X*C2)+C1, | ||||
6913 | // where C1 is divisible by C2. | ||||
6914 | unsigned SubScale; | ||||
6915 | Value *SubVal = | ||||
6916 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); | ||||
6917 | Offset += RHS->getZExtValue(); | ||||
6918 | Scale = SubScale; | ||||
6919 | return SubVal; | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6920 | } |
6921 | } | ||||
6922 | } | ||||
6923 | |||||
6924 | // Otherwise, we can't look past this. | ||||
6925 | Scale = 1; | ||||
6926 | Offset = 0; | ||||
6927 | return Val; | ||||
6928 | } | ||||
6929 | |||||
6930 | |||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6931 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
6932 | /// 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] | 6933 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6934 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6935 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6936 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6937 | // Remove any uses of AI that are dead. |
6938 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); | ||||
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6939 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6940 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
6941 | Instruction *User = cast<Instruction>(*UI++); | ||||
6942 | if (isInstructionTriviallyDead(User)) { | ||||
6943 | while (UI != E && *UI == User) | ||||
6944 | ++UI; // If this instruction uses AI more than once, don't break UI. | ||||
6945 | |||||
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6946 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6947 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6948 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6949 | } |
6950 | } | ||||
6951 | |||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6952 | // Get the type really allocated and the type casted to. |
6953 | const Type *AllocElTy = AI.getAllocatedType(); | ||||
6954 | const Type *CastElTy = PTy->getElementType(); | ||||
6955 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; | ||||
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6956 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6957 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
6958 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); | ||||
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6959 | if (CastElTyAlign < AllocElTyAlign) return 0; |
6960 | |||||
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6961 | // If the allocation has multiple uses, only promote it if we are strictly |
6962 | // increasing the alignment of the resultant allocation. If we keep it the | ||||
6963 | // same, we open the door to infinite loops of various kinds. | ||||
6964 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; | ||||
6965 | |||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6966 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
6967 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); | ||||
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6968 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6969 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6970 | // See if we can satisfy the modulus by pulling a scale out of the array |
6971 | // size argument. | ||||
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6972 | unsigned ArraySizeScale; |
6973 | int ArrayOffset; | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6974 | Value *NumElements = // See if the array size is a decomposable linear expr. |
6975 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); | ||||
6976 | |||||
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6977 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
6978 | // do the xform. | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6979 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
6980 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; | ||||
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6981 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6982 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
6983 | Value *Amt = 0; | ||||
6984 | if (Scale == 1) { | ||||
6985 | Amt = NumElements; | ||||
6986 | } else { | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6987 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6988 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
6989 | if (isa<ConstantInt>(NumElements)) | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6990 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6991 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6992 | else if (Scale != 1) { |
6993 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); | ||||
6994 | Amt = InsertNewInstBefore(Tmp, AI); | ||||
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6995 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6996 | } |
6997 | |||||
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6998 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
6999 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 7000 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); |
7001 | Amt = InsertNewInstBefore(Tmp, AI); | ||||
7002 | } | ||||
7003 | |||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7004 | AllocationInst *New; |
7005 | if (isa<MallocInst>(AI)) | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7006 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7007 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7008 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7009 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7010 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7011 | |
7012 | // If the allocation has multiple uses, insert a cast and change all things | ||||
7013 | // that used it to use the new cast. This will also hack on CI, but it will | ||||
7014 | // die soon. | ||||
7015 | if (!AI.hasOneUse()) { | ||||
7016 | AddUsesToWorkList(AI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7017 | // New is the allocation instruction, pointer typed. AI is the original |
7018 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. | ||||
7019 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); | ||||
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 7020 | InsertNewInstBefore(NewCast, AI); |
7021 | AI.replaceAllUsesWith(NewCast); | ||||
7022 | } | ||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 7023 | return ReplaceInstUsesWith(CI, New); |
7024 | } | ||||
7025 | |||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7026 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7027 | /// and return it as type Ty without inserting any new casts and without |
7028 | /// changing the computed value. This is used by code that tries to decide | ||||
7029 | /// whether promoting or shrinking integer operations to wider or smaller types | ||||
7030 | /// will allow us to eliminate a truncate or extend. | ||||
7031 | /// | ||||
7032 | /// This is a truncation operation if Ty is smaller than V->getType(), or an | ||||
7033 | /// extension operation if Ty is larger. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 7034 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
7035 | unsigned CastOpc, | ||||
7036 | int &NumCastsRemoved) { | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7037 | // We can always evaluate constants in another type. |
7038 | if (isa<ConstantInt>(V)) | ||||
7039 | return true; | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7040 | |
7041 | Instruction *I = dyn_cast<Instruction>(V); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7042 | if (!I) return false; |
7043 | |||||
7044 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7045 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7046 | // If this is an extension or truncate, we can often eliminate it. |
7047 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { | ||||
7048 | // If this is a cast from the destination type, we can trivially eliminate | ||||
7049 | // it, and this will remove a cast overall. | ||||
7050 | if (I->getOperand(0)->getType() == Ty) { | ||||
7051 | // If the first operand is itself a cast, and is eliminable, do not count | ||||
7052 | // this as an eliminable cast. We would prefer to eliminate those two | ||||
7053 | // casts first. | ||||
7054 | if (!isa<CastInst>(I->getOperand(0))) | ||||
7055 | ++NumCastsRemoved; | ||||
7056 | return true; | ||||
7057 | } | ||||
7058 | } | ||||
7059 | |||||
7060 | // We can't extend or shrink something that has multiple uses: doing so would | ||||
7061 | // require duplicating the instruction in general, which isn't profitable. | ||||
7062 | if (!I->hasOneUse()) return false; | ||||
7063 | |||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7064 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7065 | case Instruction::Add: |
7066 | case Instruction::Sub: | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7067 | case Instruction::And: |
7068 | case Instruction::Or: | ||||
7069 | case Instruction::Xor: | ||||
7070 | // These operators can all arbitrarily be extended or truncated. | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7071 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
7072 | NumCastsRemoved) && | ||||
7073 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, | ||||
7074 | NumCastsRemoved); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7075 | |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7076 | case Instruction::Mul: |
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7077 | // A multiply can be truncated by truncating its operands. |
7078 | return Ty->getBitWidth() < OrigTy->getBitWidth() && | ||||
7079 | CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, | ||||
7080 | NumCastsRemoved) && | ||||
7081 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, | ||||
7082 | NumCastsRemoved); | ||||
7083 | |||||
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7084 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7085 | // If we are truncating the result of this SHL, and if it's a shift of a |
7086 | // constant amount, we can always perform a SHL in a smaller type. | ||||
7087 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7088 | uint32_t BitWidth = Ty->getBitWidth(); |
7089 | if (BitWidth < OrigTy->getBitWidth() && | ||||
7090 | CI->getLimitedValue(BitWidth) < BitWidth) | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7091 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
7092 | NumCastsRemoved); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7093 | } |
7094 | break; | ||||
7095 | case Instruction::LShr: | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7096 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
7097 | // lshr iff we know that the bits we would otherwise be shifting in are | ||||
7098 | // already zeros. | ||||
7099 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7100 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
7101 | uint32_t BitWidth = Ty->getBitWidth(); | ||||
7102 | if (BitWidth < OrigBitWidth && | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7103 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7104 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
7105 | CI->getLimitedValue(BitWidth) < BitWidth) { | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7106 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
7107 | NumCastsRemoved); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7108 | } |
7109 | } | ||||
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7110 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7111 | case Instruction::ZExt: |
7112 | case Instruction::SExt: | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7113 | case Instruction::Trunc: |
7114 | // 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] | 7115 | // can safely replace it. Note that replacing it does not reduce the number |
7116 | // of casts in the input. | ||||
7117 | if (I->getOpcode() == CastOpc) | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7118 | return true; |
Chris Lattner | 50d9d77 | 2007-09-10 23:46:29 +0000 | [diff] [blame] | 7119 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7120 | break; |
7121 | default: | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7122 | // TODO: Can handle more cases here. |
7123 | break; | ||||
7124 | } | ||||
7125 | |||||
7126 | return false; | ||||
7127 | } | ||||
7128 | |||||
7129 | /// EvaluateInDifferentType - Given an expression that | ||||
7130 | /// CanEvaluateInDifferentType returns true for, actually insert the code to | ||||
7131 | /// evaluate the expression. | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7132 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7133 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7134 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7135 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7136 | |
7137 | // Otherwise, it must be an instruction. | ||||
7138 | Instruction *I = cast<Instruction>(V); | ||||
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 7139 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7140 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7141 | case Instruction::Add: |
7142 | case Instruction::Sub: | ||||
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7143 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7144 | case Instruction::And: |
7145 | case Instruction::Or: | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7146 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7147 | case Instruction::AShr: |
7148 | case Instruction::LShr: | ||||
7149 | case Instruction::Shl: { | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7150 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7151 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
7152 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), | ||||
7153 | LHS, RHS, I->getName()); | ||||
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7154 | break; |
7155 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7156 | case Instruction::Trunc: |
7157 | case Instruction::ZExt: | ||||
7158 | case Instruction::SExt: | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7159 | // 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] | 7160 | // just return the source. There's no need to insert it because it is not |
7161 | // new. | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7162 | if (I->getOperand(0)->getType() == Ty) |
7163 | return I->getOperand(0); | ||||
7164 | |||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7165 | // Otherwise, must be the same type of case, so just reinsert a new one. |
7166 | Res = CastInst::create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), | ||||
7167 | Ty, I->getName()); | ||||
7168 | break; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7169 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7170 | // TODO: Can handle more cases here. |
7171 | assert(0 && "Unreachable!"); | ||||
7172 | break; | ||||
7173 | } | ||||
7174 | |||||
7175 | return InsertNewInstBefore(Res, *I); | ||||
7176 | } | ||||
7177 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7178 | /// @brief Implement the transforms common to all CastInst visitors. |
7179 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { | ||||
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7180 | Value *Src = CI.getOperand(0); |
7181 | |||||
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7182 | // 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] | 7183 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7184 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7185 | if (Instruction::CastOps opc = |
7186 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { | ||||
7187 | // The first cast (CSrc) is eliminable so we need to fix up or replace | ||||
7188 | // the second cast (CI). CSrc will then have a good chance of being dead. | ||||
7189 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); | ||||
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7190 | } |
7191 | } | ||||
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7192 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7193 | // 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] | 7194 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
7195 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) | ||||
7196 | return NV; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7197 | |
7198 | // 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] | 7199 | if (isa<PHINode>(Src)) |
7200 | if (Instruction *NV = FoldOpIntoPhi(CI)) | ||||
7201 | return NV; | ||||
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7202 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7203 | return 0; |
7204 | } | ||||
7205 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7206 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
7207 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { | ||||
7208 | Value *Src = CI.getOperand(0); | ||||
7209 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7210 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7211 | // If casting the result of a getelementptr instruction with no offset, turn |
7212 | // this into a cast of the original pointer! | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7213 | if (GEP->hasAllZeroIndices()) { |
7214 | // Changing the cast operand is usually not a good idea but it is safe | ||||
7215 | // here because the pointer operand is being replaced with another | ||||
7216 | // pointer operand so the opcode doesn't need to change. | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7217 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7218 | CI.setOperand(0, GEP->getOperand(0)); |
7219 | return &CI; | ||||
7220 | } | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7221 | |
7222 | // If the GEP has a single use, and the base pointer is a bitcast, and the | ||||
7223 | // GEP computes a constant offset, see if we can convert these three | ||||
7224 | // instructions into fewer. This typically happens with unions and other | ||||
7225 | // non-type-safe code. | ||||
7226 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { | ||||
7227 | if (GEP->hasAllConstantIndices()) { | ||||
7228 | // We are guaranteed to get a constant from EmitGEPOffset. | ||||
7229 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); | ||||
7230 | int64_t Offset = OffsetV->getSExtValue(); | ||||
7231 | |||||
7232 | // Get the base pointer input of the bitcast, and the type it points to. | ||||
7233 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); | ||||
7234 | const Type *GEPIdxTy = | ||||
7235 | cast<PointerType>(OrigBase->getType())->getElementType(); | ||||
7236 | if (GEPIdxTy->isSized()) { | ||||
7237 | SmallVector<Value*, 8> NewIndices; | ||||
7238 | |||||
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7239 | // Start with the index over the outer type. Note that the type size |
7240 | // might be zero (even if the offset isn't zero) if the indexed type | ||||
7241 | // is something like [0 x {int, int}] | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7242 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7243 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7244 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7245 | FirstIdx = Offset/TySize; |
7246 | Offset %= TySize; | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7247 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7248 | // Handle silly modulus not returning values values [0..TySize). |
7249 | if (Offset < 0) { | ||||
7250 | --FirstIdx; | ||||
7251 | Offset += TySize; | ||||
7252 | assert(Offset >= 0); | ||||
7253 | } | ||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7254 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7255 | } |
7256 | |||||
7257 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7258 | |
7259 | // Index into the types. If we fail, set OrigBase to null. | ||||
7260 | while (Offset) { | ||||
7261 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { | ||||
7262 | const StructLayout *SL = TD->getStructLayout(STy); | ||||
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7263 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
7264 | unsigned Elt = SL->getElementContainingOffset(Offset); | ||||
7265 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7266 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7267 | Offset -= SL->getElementOffset(Elt); |
7268 | GEPIdxTy = STy->getElementType(Elt); | ||||
7269 | } else { | ||||
7270 | // Otherwise, we can't index into this, bail out. | ||||
7271 | Offset = 0; | ||||
7272 | OrigBase = 0; | ||||
7273 | } | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7274 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
7275 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); | ||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7276 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7277 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
7278 | Offset %= EltSize; | ||||
7279 | } else { | ||||
7280 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); | ||||
7281 | } | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7282 | GEPIdxTy = STy->getElementType(); |
7283 | } else { | ||||
7284 | // Otherwise, we can't index into this, bail out. | ||||
7285 | Offset = 0; | ||||
7286 | OrigBase = 0; | ||||
7287 | } | ||||
7288 | } | ||||
7289 | if (OrigBase) { | ||||
7290 | // If we were able to index down into an element, create the GEP | ||||
7291 | // and bitcast the result. This eliminates one bitcast, potentially | ||||
7292 | // two. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7293 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
7294 | NewIndices.begin(), | ||||
7295 | NewIndices.end(), ""); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7296 | InsertNewInstBefore(NGEP, CI); |
7297 | NGEP->takeName(GEP); | ||||
7298 | |||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7299 | if (isa<BitCastInst>(CI)) |
7300 | return new BitCastInst(NGEP, CI.getType()); | ||||
7301 | assert(isa<PtrToIntInst>(CI)); | ||||
7302 | return new PtrToIntInst(NGEP, CI.getType()); | ||||
7303 | } | ||||
7304 | } | ||||
7305 | } | ||||
7306 | } | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7307 | } |
7308 | |||||
7309 | return commonCastTransforms(CI); | ||||
7310 | } | ||||
7311 | |||||
7312 | |||||
7313 | |||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7314 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
7315 | /// integer types. This function implements the common transforms for all those | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7316 | /// cases. |
7317 | /// @brief Implement the transforms common to CastInst with integer operands | ||||
7318 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { | ||||
7319 | if (Instruction *Result = commonCastTransforms(CI)) | ||||
7320 | return Result; | ||||
7321 | |||||
7322 | Value *Src = CI.getOperand(0); | ||||
7323 | const Type *SrcTy = Src->getType(); | ||||
7324 | const Type *DestTy = CI.getType(); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7325 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
7326 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7327 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7328 | // See if we can simplify any instructions used by the LHS whose sole |
7329 | // purpose is to compute bits we don't care about. | ||||
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7330 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
7331 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7332 | KnownZero, KnownOne)) |
7333 | return &CI; | ||||
7334 | |||||
7335 | // If the source isn't an instruction or has more than one use then we | ||||
7336 | // can't do anything more. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7337 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
7338 | if (!SrcI || !Src->hasOneUse()) | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7339 | return 0; |
7340 | |||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7341 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7342 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7343 | if (!isa<BitCastInst>(CI) && |
7344 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7345 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7346 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7347 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
7348 | // we need to do an AND to maintain the clear top-part of the computation, | ||||
7349 | // so we require that the input have eliminated at least one cast. If this | ||||
7350 | // 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] | 7351 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7352 | bool DoXForm; |
7353 | switch (CI.getOpcode()) { | ||||
7354 | default: | ||||
7355 | // All the others use floating point so we shouldn't actually | ||||
7356 | // get here because of the check above. | ||||
7357 | assert(0 && "Unknown cast type"); | ||||
7358 | case Instruction::Trunc: | ||||
7359 | DoXForm = true; | ||||
7360 | break; | ||||
7361 | case Instruction::ZExt: | ||||
7362 | DoXForm = NumCastsRemoved >= 1; | ||||
7363 | break; | ||||
7364 | case Instruction::SExt: | ||||
7365 | DoXForm = NumCastsRemoved >= 2; | ||||
7366 | break; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7367 | } |
7368 | |||||
7369 | if (DoXForm) { | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7370 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
7371 | CI.getOpcode() == Instruction::SExt); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7372 | assert(Res->getType() == DestTy); |
7373 | switch (CI.getOpcode()) { | ||||
7374 | default: assert(0 && "Unknown cast type!"); | ||||
7375 | case Instruction::Trunc: | ||||
7376 | case Instruction::BitCast: | ||||
7377 | // Just replace this cast with the result. | ||||
7378 | return ReplaceInstUsesWith(CI, Res); | ||||
7379 | case Instruction::ZExt: { | ||||
7380 | // We need to emit an AND to clear the high bits. | ||||
7381 | assert(SrcBitSize < DestBitSize && "Not a zext?"); | ||||
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 7382 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
7383 | SrcBitSize)); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7384 | return BinaryOperator::createAnd(Res, C); |
7385 | } | ||||
7386 | case Instruction::SExt: | ||||
7387 | // We need to emit a cast to truncate, then a cast to sext. | ||||
7388 | return CastInst::create(Instruction::SExt, | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7389 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
7390 | CI), DestTy); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7391 | } |
7392 | } | ||||
7393 | } | ||||
7394 | |||||
7395 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; | ||||
7396 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; | ||||
7397 | |||||
7398 | switch (SrcI->getOpcode()) { | ||||
7399 | case Instruction::Add: | ||||
7400 | case Instruction::Mul: | ||||
7401 | case Instruction::And: | ||||
7402 | case Instruction::Or: | ||||
7403 | case Instruction::Xor: | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 7404 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7405 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
7406 | // Don't insert two casts if they cannot be eliminated. We allow | ||||
7407 | // two casts to be inserted if the sizes are the same. This could | ||||
7408 | // only be converting signedness, which is a noop. | ||||
7409 | if (DestBitSize == SrcBitSize || | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7410 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
7411 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { | ||||
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 7412 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7413 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
7414 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); | ||||
7415 | return BinaryOperator::create( | ||||
7416 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7417 | } |
7418 | } | ||||
7419 | |||||
7420 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 | ||||
7421 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && | ||||
7422 | SrcI->getOpcode() == Instruction::Xor && | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7423 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7424 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7425 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7426 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); |
7427 | } | ||||
7428 | break; | ||||
7429 | case Instruction::SDiv: | ||||
7430 | case Instruction::UDiv: | ||||
7431 | case Instruction::SRem: | ||||
7432 | case Instruction::URem: | ||||
7433 | // If we are just changing the sign, rewrite. | ||||
7434 | if (DestBitSize == SrcBitSize) { | ||||
7435 | // Don't insert two casts if they cannot be eliminated. We allow | ||||
7436 | // two casts to be inserted if the sizes are the same. This could | ||||
7437 | // only be converting signedness, which is a noop. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7438 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
7439 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7440 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
7441 | Op0, DestTy, SrcI); | ||||
7442 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, | ||||
7443 | Op1, DestTy, SrcI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7444 | return BinaryOperator::create( |
7445 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); | ||||
7446 | } | ||||
7447 | } | ||||
7448 | break; | ||||
7449 | |||||
7450 | case Instruction::Shl: | ||||
7451 | // Allow changing the sign of the source operand. Do not allow | ||||
7452 | // changing the size of the shift, UNLESS the shift amount is a | ||||
7453 | // constant. We must not change variable sized shifts to a smaller | ||||
7454 | // size, because it is undefined to shift more bits out than exist | ||||
7455 | // in the value. | ||||
7456 | if (DestBitSize == SrcBitSize || | ||||
7457 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7458 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
7459 | Instruction::BitCast : Instruction::Trunc); | ||||
7460 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7461 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7462 | return BinaryOperator::createShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7463 | } |
7464 | break; | ||||
7465 | case Instruction::AShr: | ||||
7466 | // If this is a signed shr, and if all bits shifted in are about to be | ||||
7467 | // truncated off, turn it into an unsigned shr to allow greater | ||||
7468 | // simplifications. | ||||
7469 | if (DestBitSize < SrcBitSize && | ||||
7470 | isa<ConstantInt>(Op1)) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7471 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7472 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
7473 | // Insert the new logical shift right. | ||||
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7474 | return BinaryOperator::createLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7475 | } |
7476 | } | ||||
7477 | break; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7478 | } |
7479 | return 0; | ||||
7480 | } | ||||
7481 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7482 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7483 | if (Instruction *Result = commonIntCastTransforms(CI)) |
7484 | return Result; | ||||
7485 | |||||
7486 | Value *Src = CI.getOperand(0); | ||||
7487 | const Type *Ty = CI.getType(); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7488 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
7489 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); | ||||
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7490 | |
7491 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { | ||||
7492 | switch (SrcI->getOpcode()) { | ||||
7493 | default: break; | ||||
7494 | case Instruction::LShr: | ||||
7495 | // We can shrink lshr to something smaller if we know the bits shifted in | ||||
7496 | // are already zeros. | ||||
7497 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7498 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7499 | |
7500 | // Get a mask for the bits shifting in. | ||||
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7501 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7502 | Value* SrcIOp0 = SrcI->getOperand(0); |
7503 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { | ||||
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7504 | if (ShAmt >= DestBitWidth) // All zeros. |
7505 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); | ||||
7506 | |||||
7507 | // Okay, we can shrink this. Truncate the input, then return a new | ||||
7508 | // shift. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7509 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
7510 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), | ||||
7511 | Ty, CI); | ||||
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7512 | return BinaryOperator::createLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7513 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7514 | } else { // This is a variable shr. |
7515 | |||||
7516 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is | ||||
7517 | // more LLVM instructions, but allows '1 << Y' to be hoisted if | ||||
7518 | // loop-invariant and CSE'd. | ||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7519 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7520 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
7521 | |||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7522 | Value *V = InsertNewInstBefore( |
Reid Spencer | cc46cdb | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 7523 | BinaryOperator::createShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7524 | "tmp"), CI); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7525 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, |
7526 | SrcI->getOperand(0), | ||||
7527 | "tmp"), CI); | ||||
7528 | Value *Zero = Constant::getNullValue(V->getType()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7529 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7530 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7531 | } |
7532 | break; | ||||
7533 | } | ||||
7534 | } | ||||
7535 | |||||
7536 | return 0; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7537 | } |
7538 | |||||
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7539 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
7540 | /// in order to eliminate the icmp. | ||||
7541 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, | ||||
7542 | bool DoXform) { | ||||
7543 | // If we are just checking for a icmp eq of a single bit and zext'ing it | ||||
7544 | // to an integer, then shift the bit to the appropriate place and then | ||||
7545 | // cast to integer to avoid the comparison. | ||||
7546 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { | ||||
7547 | const APInt &Op1CV = Op1C->getValue(); | ||||
7548 | |||||
7549 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. | ||||
7550 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. | ||||
7551 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || | ||||
7552 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { | ||||
7553 | if (!DoXform) return ICI; | ||||
7554 | |||||
7555 | Value *In = ICI->getOperand(0); | ||||
7556 | Value *Sh = ConstantInt::get(In->getType(), | ||||
7557 | In->getType()->getPrimitiveSizeInBits()-1); | ||||
7558 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh, | ||||
7559 | In->getName()+".lobit"), | ||||
7560 | CI); | ||||
7561 | if (In->getType() != CI.getType()) | ||||
7562 | In = CastInst::createIntegerCast(In, CI.getType(), | ||||
7563 | false/*ZExt*/, "tmp", &CI); | ||||
7564 | |||||
7565 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { | ||||
7566 | Constant *One = ConstantInt::get(In->getType(), 1); | ||||
7567 | In = InsertNewInstBefore(BinaryOperator::createXor(In, One, | ||||
7568 | In->getName()+".not"), | ||||
7569 | CI); | ||||
7570 | } | ||||
7571 | |||||
7572 | return ReplaceInstUsesWith(CI, In); | ||||
7573 | } | ||||
7574 | |||||
7575 | |||||
7576 | |||||
7577 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. | ||||
7578 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. | ||||
7579 | // zext (X == 1) to i32 --> X iff X has only the low bit set. | ||||
7580 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. | ||||
7581 | // zext (X != 0) to i32 --> X iff X has only the low bit set. | ||||
7582 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. | ||||
7583 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. | ||||
7584 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. | ||||
7585 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && | ||||
7586 | // This only works for EQ and NE | ||||
7587 | ICI->isEquality()) { | ||||
7588 | // If Op1C some other power of two, convert: | ||||
7589 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); | ||||
7590 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
7591 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); | ||||
7592 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); | ||||
7593 | |||||
7594 | APInt KnownZeroMask(~KnownZero); | ||||
7595 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? | ||||
7596 | if (!DoXform) return ICI; | ||||
7597 | |||||
7598 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; | ||||
7599 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { | ||||
7600 | // (X&4) == 2 --> false | ||||
7601 | // (X&4) != 2 --> true | ||||
7602 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); | ||||
7603 | Res = ConstantExpr::getZExt(Res, CI.getType()); | ||||
7604 | return ReplaceInstUsesWith(CI, Res); | ||||
7605 | } | ||||
7606 | |||||
7607 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); | ||||
7608 | Value *In = ICI->getOperand(0); | ||||
7609 | if (ShiftAmt) { | ||||
7610 | // Perform a logical shr by shiftamt. | ||||
7611 | // Insert the shift to put the result in the low bit. | ||||
7612 | In = InsertNewInstBefore(BinaryOperator::createLShr(In, | ||||
7613 | ConstantInt::get(In->getType(), ShiftAmt), | ||||
7614 | In->getName()+".lobit"), CI); | ||||
7615 | } | ||||
7616 | |||||
7617 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. | ||||
7618 | Constant *One = ConstantInt::get(In->getType(), 1); | ||||
7619 | In = BinaryOperator::createXor(In, One, "tmp"); | ||||
7620 | InsertNewInstBefore(cast<Instruction>(In), CI); | ||||
7621 | } | ||||
7622 | |||||
7623 | if (CI.getType() == In->getType()) | ||||
7624 | return ReplaceInstUsesWith(CI, In); | ||||
7625 | else | ||||
7626 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); | ||||
7627 | } | ||||
7628 | } | ||||
7629 | } | ||||
7630 | |||||
7631 | return 0; | ||||
7632 | } | ||||
7633 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7634 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7635 | // If one of the common conversion will work .. |
7636 | if (Instruction *Result = commonIntCastTransforms(CI)) | ||||
7637 | return Result; | ||||
7638 | |||||
7639 | Value *Src = CI.getOperand(0); | ||||
7640 | |||||
7641 | // If this is a cast of a cast | ||||
7642 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7643 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
7644 | // types and if the sizes are just right we can convert this into a logical | ||||
7645 | // 'and' which will be much cheaper than the pair of casts. | ||||
7646 | if (isa<TruncInst>(CSrc)) { | ||||
7647 | // Get the sizes of the types involved | ||||
7648 | Value *A = CSrc->getOperand(0); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7649 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
7650 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); | ||||
7651 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7652 | // If we're actually extending zero bits and the trunc is a no-op |
7653 | if (MidSize < DstSize && SrcSize == DstSize) { | ||||
7654 | // Replace both of the casts with an And of the type mask. | ||||
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7655 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7656 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7657 | Instruction *And = |
7658 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); | ||||
7659 | // Unfortunately, if the type changed, we need to cast it back. | ||||
7660 | if (And->getType() != CI.getType()) { | ||||
7661 | And->setName(CSrc->getName()+".mask"); | ||||
7662 | InsertNewInstBefore(And, CI); | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 7663 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7664 | } |
7665 | return And; | ||||
7666 | } | ||||
7667 | } | ||||
7668 | } | ||||
7669 | |||||
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7670 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
7671 | return transformZExtICmp(ICI, CI); | ||||
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7672 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7673 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
7674 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { | ||||
7675 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one | ||||
7676 | // of the (zext icmp) will be transformed. | ||||
7677 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); | ||||
7678 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); | ||||
7679 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && | ||||
7680 | (transformZExtICmp(LHS, CI, false) || | ||||
7681 | transformZExtICmp(RHS, CI, false))) { | ||||
7682 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); | ||||
7683 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); | ||||
7684 | return BinaryOperator::create(Instruction::Or, LCast, RCast); | ||||
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7685 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7686 | } |
7687 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7688 | return 0; |
7689 | } | ||||
7690 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7691 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7692 | if (Instruction *I = commonIntCastTransforms(CI)) |
7693 | return I; | ||||
7694 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7695 | Value *Src = CI.getOperand(0); |
7696 | |||||
7697 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed | ||||
7698 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed | ||||
7699 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { | ||||
7700 | // If we are just checking for a icmp eq of a single bit and zext'ing it | ||||
7701 | // to an integer, then shift the bit to the appropriate place and then | ||||
7702 | // cast to integer to avoid the comparison. | ||||
7703 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { | ||||
7704 | const APInt &Op1CV = Op1C->getValue(); | ||||
7705 | |||||
7706 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. | ||||
7707 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. | ||||
7708 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || | ||||
7709 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ | ||||
7710 | Value *In = ICI->getOperand(0); | ||||
7711 | Value *Sh = ConstantInt::get(In->getType(), | ||||
7712 | In->getType()->getPrimitiveSizeInBits()-1); | ||||
7713 | In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh, | ||||
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7714 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7715 | CI); |
7716 | if (In->getType() != CI.getType()) | ||||
7717 | In = CastInst::createIntegerCast(In, CI.getType(), | ||||
7718 | true/*SExt*/, "tmp", &CI); | ||||
7719 | |||||
7720 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) | ||||
7721 | In = InsertNewInstBefore(BinaryOperator::createNot(In, | ||||
7722 | In->getName()+".not"), CI); | ||||
7723 | |||||
7724 | return ReplaceInstUsesWith(CI, In); | ||||
7725 | } | ||||
7726 | } | ||||
7727 | } | ||||
7728 | |||||
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7729 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7730 | } |
7731 | |||||
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7732 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
7733 | /// in the specified FP type without changing its value. | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7734 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7735 | APFloat F = CFP->getValueAPF(); |
7736 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7737 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7738 | return 0; |
7739 | } | ||||
7740 | |||||
7741 | /// LookThroughFPExtensions - If this is an fp extension instruction, look | ||||
7742 | /// through it until we get the source value. | ||||
7743 | static Value *LookThroughFPExtensions(Value *V) { | ||||
7744 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||
7745 | if (I->getOpcode() == Instruction::FPExt) | ||||
7746 | return LookThroughFPExtensions(I->getOperand(0)); | ||||
7747 | |||||
7748 | // If this value is a constant, return the constant in the smallest FP type | ||||
7749 | // that can accurately represent it. This allows us to turn | ||||
7750 | // (float)((double)X+2.0) into x+2.0f. | ||||
7751 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { | ||||
7752 | if (CFP->getType() == Type::PPC_FP128Ty) | ||||
7753 | return V; // No constant folding of this. | ||||
7754 | // See if the value can be truncated to float and then reextended. | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7755 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7756 | return V; |
7757 | if (CFP->getType() == Type::DoubleTy) | ||||
7758 | return V; // Won't shrink. | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7759 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7760 | return V; |
7761 | // Don't try to shrink to various long double types. | ||||
7762 | } | ||||
7763 | |||||
7764 | return V; | ||||
7765 | } | ||||
7766 | |||||
7767 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { | ||||
7768 | if (Instruction *I = commonCastTransforms(CI)) | ||||
7769 | return I; | ||||
7770 | |||||
7771 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are | ||||
7772 | // smaller than the destination type, we can eliminate the truncate by doing | ||||
7773 | // the add as the smaller type. This applies to add/sub/mul/div as well as | ||||
7774 | // many builtins (sqrt, etc). | ||||
7775 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); | ||||
7776 | if (OpI && OpI->hasOneUse()) { | ||||
7777 | switch (OpI->getOpcode()) { | ||||
7778 | default: break; | ||||
7779 | case Instruction::Add: | ||||
7780 | case Instruction::Sub: | ||||
7781 | case Instruction::Mul: | ||||
7782 | case Instruction::FDiv: | ||||
7783 | case Instruction::FRem: | ||||
7784 | const Type *SrcTy = OpI->getType(); | ||||
7785 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); | ||||
7786 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); | ||||
7787 | if (LHSTrunc->getType() != SrcTy && | ||||
7788 | RHSTrunc->getType() != SrcTy) { | ||||
7789 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); | ||||
7790 | // If the source types were both smaller than the destination type of | ||||
7791 | // the cast, do this xform. | ||||
7792 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && | ||||
7793 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { | ||||
7794 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, | ||||
7795 | CI.getType(), CI); | ||||
7796 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, | ||||
7797 | CI.getType(), CI); | ||||
7798 | return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc); | ||||
7799 | } | ||||
7800 | } | ||||
7801 | break; | ||||
7802 | } | ||||
7803 | } | ||||
7804 | return 0; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7805 | } |
7806 | |||||
7807 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { | ||||
7808 | return commonCastTransforms(CI); | ||||
7809 | } | ||||
7810 | |||||
7811 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { | ||||
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7812 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7813 | } |
7814 | |||||
7815 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { | ||||
Reid Spencer | 44c030a | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 7816 | return commonCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7817 | } |
7818 | |||||
7819 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { | ||||
7820 | return commonCastTransforms(CI); | ||||
7821 | } | ||||
7822 | |||||
7823 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { | ||||
7824 | return commonCastTransforms(CI); | ||||
7825 | } | ||||
7826 | |||||
7827 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7828 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7829 | } |
7830 | |||||
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7831 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
7832 | if (Instruction *I = commonCastTransforms(CI)) | ||||
7833 | return I; | ||||
7834 | |||||
7835 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); | ||||
7836 | if (!DestPointee->isSized()) return 0; | ||||
7837 | |||||
7838 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. | ||||
7839 | ConstantInt *Cst; | ||||
7840 | Value *X; | ||||
7841 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), | ||||
7842 | m_ConstantInt(Cst)))) { | ||||
7843 | // If the source and destination operands have the same type, see if this | ||||
7844 | // is a single-index GEP. | ||||
7845 | if (X->getType() == CI.getType()) { | ||||
7846 | // Get the size of the pointee type. | ||||
Bill Wendling | b9d4f8d | 2008-03-14 05:12:19 +0000 | [diff] [blame] | 7847 | uint64_t Size = TD->getABITypeSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7848 | |
7849 | // Convert the constant to intptr type. | ||||
7850 | APInt Offset = Cst->getValue(); | ||||
7851 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); | ||||
7852 | |||||
7853 | // If Offset is evenly divisible by Size, we can do this xform. | ||||
7854 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ | ||||
7855 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7856 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7857 | } |
7858 | } | ||||
7859 | // TODO: Could handle other cases, e.g. where add is indexing into field of | ||||
7860 | // struct etc. | ||||
7861 | } else if (CI.getOperand(0)->hasOneUse() && | ||||
7862 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { | ||||
7863 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an | ||||
7864 | // "inttoptr+GEP" instead of "add+intptr". | ||||
7865 | |||||
7866 | // Get the size of the pointee type. | ||||
7867 | uint64_t Size = TD->getABITypeSize(DestPointee); | ||||
7868 | |||||
7869 | // Convert the constant to intptr type. | ||||
7870 | APInt Offset = Cst->getValue(); | ||||
7871 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); | ||||
7872 | |||||
7873 | // If Offset is evenly divisible by Size, we can do this xform. | ||||
7874 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ | ||||
7875 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); | ||||
7876 | |||||
7877 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), | ||||
7878 | "tmp"), CI); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7879 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7880 | } |
7881 | } | ||||
7882 | return 0; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7883 | } |
7884 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7885 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7886 | // If the operands are integer typed then apply the integer transforms, |
7887 | // otherwise just apply the common ones. | ||||
7888 | Value *Src = CI.getOperand(0); | ||||
7889 | const Type *SrcTy = Src->getType(); | ||||
7890 | const Type *DestTy = CI.getType(); | ||||
7891 | |||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7892 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7893 | if (Instruction *Result = commonIntCastTransforms(CI)) |
7894 | return Result; | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7895 | } else if (isa<PointerType>(SrcTy)) { |
7896 | if (Instruction *I = commonPointerCastTransforms(CI)) | ||||
7897 | return I; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7898 | } else { |
7899 | if (Instruction *Result = commonCastTransforms(CI)) | ||||
7900 | return Result; | ||||
7901 | } | ||||
7902 | |||||
7903 | |||||
7904 | // Get rid of casts from one type to the same type. These are useless and can | ||||
7905 | // be replaced by the operand. | ||||
7906 | if (DestTy == Src->getType()) | ||||
7907 | return ReplaceInstUsesWith(CI, Src); | ||||
7908 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7909 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7910 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
7911 | const Type *DstElTy = DstPTy->getElementType(); | ||||
7912 | const Type *SrcElTy = SrcPTy->getElementType(); | ||||
7913 | |||||
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 7914 | // If the address spaces don't match, don't eliminate the bitcast, which is |
7915 | // required for changing types. | ||||
7916 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) | ||||
7917 | return 0; | ||||
7918 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7919 | // If we are casting a malloc or alloca to a pointer to a type of the same |
7920 | // size, rewrite the allocation instruction to allocate the "right" type. | ||||
7921 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) | ||||
7922 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) | ||||
7923 | return V; | ||||
7924 | |||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7925 | // If the source and destination are pointers, and this cast is equivalent |
7926 | // 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] | 7927 | // This can enhance SROA and other transforms that want type-safe pointers. |
7928 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); | ||||
7929 | unsigned NumZeros = 0; | ||||
7930 | while (SrcElTy != DstElTy && | ||||
7931 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && | ||||
7932 | SrcElTy->getNumContainedTypes() /* not "{}" */) { | ||||
7933 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); | ||||
7934 | ++NumZeros; | ||||
7935 | } | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 7936 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7937 | // If we found a path from the src to dest, create the getelementptr now. |
7938 | if (SrcElTy == DstElTy) { | ||||
7939 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7940 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
7941 | ((Instruction*) NULL)); | ||||
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7942 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7943 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 7944 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7945 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
7946 | if (SVI->hasOneUse()) { | ||||
7947 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is | ||||
7948 | // a bitconvert to a vector with the same # elts. | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7949 | if (isa<VectorType>(DestTy) && |
7950 | cast<VectorType>(DestTy)->getNumElements() == | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7951 | SVI->getType()->getNumElements()) { |
7952 | CastInst *Tmp; | ||||
7953 | // If either of the operands is a cast from CI.getType(), then | ||||
7954 | // evaluating the shuffle in the casted destination's type will allow | ||||
7955 | // us to eliminate at least one cast. | ||||
7956 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && | ||||
7957 | Tmp->getOperand(0)->getType() == DestTy) || | ||||
7958 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && | ||||
7959 | Tmp->getOperand(0)->getType() == DestTy)) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7960 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
7961 | SVI->getOperand(0), DestTy, &CI); | ||||
7962 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, | ||||
7963 | SVI->getOperand(1), DestTy, &CI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7964 | // Return a new shuffle vector. Use the same element ID's, as we |
7965 | // know the vector types match #elts. | ||||
7966 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); | ||||
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 7967 | } |
7968 | } | ||||
7969 | } | ||||
7970 | } | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 7971 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 7972 | } |
7973 | |||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7974 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
7975 | /// %C = or %A, %B | ||||
7976 | /// %D = select %cond, %C, %A | ||||
7977 | /// into: | ||||
7978 | /// %C = select %cond, %B, 0 | ||||
7979 | /// %D = or %A, %C | ||||
7980 | /// | ||||
7981 | /// Assuming that the specified instruction is an operand to the select, return | ||||
7982 | /// a bitmask indicating which operands of this instruction are foldable if they | ||||
7983 | /// equal the other incoming value of the select. | ||||
7984 | /// | ||||
7985 | static unsigned GetSelectFoldableOperands(Instruction *I) { | ||||
7986 | switch (I->getOpcode()) { | ||||
7987 | case Instruction::Add: | ||||
7988 | case Instruction::Mul: | ||||
7989 | case Instruction::And: | ||||
7990 | case Instruction::Or: | ||||
7991 | case Instruction::Xor: | ||||
7992 | return 3; // Can fold through either operand. | ||||
7993 | case Instruction::Sub: // Can only fold on the amount subtracted. | ||||
7994 | case Instruction::Shl: // Can only fold on the shift amount. | ||||
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 7995 | case Instruction::LShr: |
7996 | case Instruction::AShr: | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7997 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7998 | default: |
7999 | return 0; // Cannot fold | ||||
8000 | } | ||||
8001 | } | ||||
8002 | |||||
8003 | /// GetSelectFoldableConstant - For the same transformation as the previous | ||||
8004 | /// function, return the identity constant that goes into the select. | ||||
8005 | static Constant *GetSelectFoldableConstant(Instruction *I) { | ||||
8006 | switch (I->getOpcode()) { | ||||
8007 | default: assert(0 && "This cannot happen!"); abort(); | ||||
8008 | case Instruction::Add: | ||||
8009 | case Instruction::Sub: | ||||
8010 | case Instruction::Or: | ||||
8011 | case Instruction::Xor: | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8012 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8013 | case Instruction::LShr: |
8014 | case Instruction::AShr: | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8015 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8016 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 8017 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8018 | case Instruction::Mul: |
8019 | return ConstantInt::get(I->getType(), 1); | ||||
8020 | } | ||||
8021 | } | ||||
8022 | |||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8023 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
8024 | /// have the same opcode and only one use each. Try to simplify this. | ||||
8025 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, | ||||
8026 | Instruction *FI) { | ||||
8027 | if (TI->getNumOperands() == 1) { | ||||
8028 | // If this is a non-volatile load or a cast from the same type, | ||||
8029 | // merge. | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8030 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8031 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
8032 | return 0; | ||||
8033 | } else { | ||||
8034 | return 0; // unknown unary op. | ||||
8035 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8036 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8037 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8038 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
8039 | FI->getOperand(0), SI.getName()+".v"); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8040 | InsertNewInstBefore(NewSI, SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8041 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, |
8042 | TI->getType()); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8043 | } |
8044 | |||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8045 | // Only handle binary operators here. |
8046 | if (!isa<BinaryOperator>(TI)) | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8047 | return 0; |
8048 | |||||
8049 | // Figure out if the operations have any operands in common. | ||||
8050 | Value *MatchOp, *OtherOpT, *OtherOpF; | ||||
8051 | bool MatchIsOpZero; | ||||
8052 | if (TI->getOperand(0) == FI->getOperand(0)) { | ||||
8053 | MatchOp = TI->getOperand(0); | ||||
8054 | OtherOpT = TI->getOperand(1); | ||||
8055 | OtherOpF = FI->getOperand(1); | ||||
8056 | MatchIsOpZero = true; | ||||
8057 | } else if (TI->getOperand(1) == FI->getOperand(1)) { | ||||
8058 | MatchOp = TI->getOperand(1); | ||||
8059 | OtherOpT = TI->getOperand(0); | ||||
8060 | OtherOpF = FI->getOperand(0); | ||||
8061 | MatchIsOpZero = false; | ||||
8062 | } else if (!TI->isCommutative()) { | ||||
8063 | return 0; | ||||
8064 | } else if (TI->getOperand(0) == FI->getOperand(1)) { | ||||
8065 | MatchOp = TI->getOperand(0); | ||||
8066 | OtherOpT = TI->getOperand(1); | ||||
8067 | OtherOpF = FI->getOperand(0); | ||||
8068 | MatchIsOpZero = true; | ||||
8069 | } else if (TI->getOperand(1) == FI->getOperand(0)) { | ||||
8070 | MatchOp = TI->getOperand(1); | ||||
8071 | OtherOpT = TI->getOperand(0); | ||||
8072 | OtherOpF = FI->getOperand(1); | ||||
8073 | MatchIsOpZero = true; | ||||
8074 | } else { | ||||
8075 | return 0; | ||||
8076 | } | ||||
8077 | |||||
8078 | // If we reach here, they do have operations in common. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8079 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
8080 | OtherOpF, SI.getName()+".v"); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8081 | InsertNewInstBefore(NewSI, SI); |
8082 | |||||
8083 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { | ||||
8084 | if (MatchIsOpZero) | ||||
8085 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); | ||||
8086 | else | ||||
8087 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8088 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 8089 | assert(0 && "Shouldn't get here"); |
8090 | return 0; | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8091 | } |
8092 | |||||
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8093 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8094 | Value *CondVal = SI.getCondition(); |
8095 | Value *TrueVal = SI.getTrueValue(); | ||||
8096 | Value *FalseVal = SI.getFalseValue(); | ||||
8097 | |||||
8098 | // select true, X, Y -> X | ||||
8099 | // select false, X, Y -> Y | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8100 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8101 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8102 | |
8103 | // select C, X, X -> X | ||||
8104 | if (TrueVal == FalseVal) | ||||
8105 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8106 | |||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8107 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
8108 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8109 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X | ||||
8110 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8111 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y | ||||
8112 | if (isa<Constant>(TrueVal)) | ||||
8113 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8114 | else | ||||
8115 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8116 | } | ||||
8117 | |||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8118 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8119 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8120 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8121 | // Change: A = select B, true, C --> A = or B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8122 | return BinaryOperator::createOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8123 | } else { |
8124 | // Change: A = select B, false, C --> A = and !B, C | ||||
8125 | Value *NotCond = | ||||
8126 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, | ||||
8127 | "not."+CondVal->getName()), SI); | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8128 | return BinaryOperator::createAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8129 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8130 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8131 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8132 | // Change: A = select B, C, false --> A = and B, C |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8133 | return BinaryOperator::createAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8134 | } else { |
8135 | // Change: A = select B, C, true --> A = or !B, C | ||||
8136 | Value *NotCond = | ||||
8137 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, | ||||
8138 | "not."+CondVal->getName()), SI); | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8139 | return BinaryOperator::createOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8140 | } |
8141 | } | ||||
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8142 | |
8143 | // select a, b, a -> a&b | ||||
8144 | // select a, a, b -> a|b | ||||
8145 | if (CondVal == TrueVal) | ||||
8146 | return BinaryOperator::createOr(CondVal, FalseVal); | ||||
8147 | else if (CondVal == FalseVal) | ||||
8148 | return BinaryOperator::createAnd(CondVal, TrueVal); | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8149 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8150 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8151 | // Selecting between two integer constants? |
8152 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) | ||||
8153 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { | ||||
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8154 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8155 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8156 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8157 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8158 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8159 | Value *NotCond = |
8160 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, | ||||
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8161 | "not."+CondVal->getName()), SI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8162 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8163 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8164 | |
8165 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! | ||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8166 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8167 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8168 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8169 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8170 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8171 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8172 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8173 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8174 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8175 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8176 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8177 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
8178 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, | ||||
8179 | ShAmt, "ones"); | ||||
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8180 | InsertNewInstBefore(SRA, SI); |
8181 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8182 | // Finally, convert to the type of the select RHS. We figure out |
8183 | // if this requires a SExt, Trunc or BitCast based on the sizes. | ||||
8184 | Instruction::CastOps opc = Instruction::BitCast; | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8185 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
8186 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8187 | if (SRASize < SISize) |
8188 | opc = Instruction::SExt; | ||||
8189 | else if (SRASize > SISize) | ||||
8190 | opc = Instruction::Trunc; | ||||
8191 | return CastInst::create(opc, SRA, SI.getType()); | ||||
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8192 | } |
8193 | } | ||||
8194 | |||||
8195 | |||||
8196 | // 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] | 8197 | // 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] | 8198 | // non-constant value, eliminate this whole mess. This corresponds to |
8199 | // cases like this: ((X & 27) ? 27 : 0) | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8200 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 8201 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8202 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
8203 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) | ||||
8204 | if (ICA->getOpcode() == Instruction::And && | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8205 | isa<ConstantInt>(ICA->getOperand(1)) && |
8206 | (ICA->getOperand(1) == TrueValC || | ||||
8207 | ICA->getOperand(1) == FalseValC) && | ||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8208 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
8209 | // 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] | 8210 | // know whether we have a icmp_ne or icmp_eq and whether the |
8211 | // true or false val is the zero. | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8212 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8213 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8214 | Value *V = ICA; |
8215 | if (ShouldNotVal) | ||||
8216 | V = InsertNewInstBefore(BinaryOperator::create( | ||||
8217 | Instruction::Xor, V, ICA->getOperand(1)), SI); | ||||
8218 | return ReplaceInstUsesWith(SI, V); | ||||
8219 | } | ||||
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8220 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8221 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8222 | |
8223 | // 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] | 8224 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
8225 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { | ||||
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8226 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8227 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
8228 | // This is not safe in general for floating point: | ||||
8229 | // consider X== -0, Y== +0. | ||||
8230 | // It becomes safe if either operand is a nonzero constant. | ||||
8231 | ConstantFP *CFPt, *CFPf; | ||||
8232 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && | ||||
8233 | !CFPt->getValueAPF().isZero()) || | ||||
8234 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && | ||||
8235 | !CFPf->getValueAPF().isZero())) | ||||
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8236 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8237 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8238 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8239 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8240 | return ReplaceInstUsesWith(SI, TrueVal); |
8241 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | ||||
8242 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8243 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8244 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8245 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
8246 | // This is not safe in general for floating point: | ||||
8247 | // consider X== -0, Y== +0. | ||||
8248 | // It becomes safe if either operand is a nonzero constant. | ||||
8249 | ConstantFP *CFPt, *CFPf; | ||||
8250 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && | ||||
8251 | !CFPt->getValueAPF().isZero()) || | ||||
8252 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && | ||||
8253 | !CFPf->getValueAPF().isZero())) | ||||
8254 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8255 | } | ||||
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8256 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8257 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
8258 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8259 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | ||||
8260 | } | ||||
8261 | } | ||||
8262 | |||||
8263 | // See if we are selecting two values based on a comparison of the two values. | ||||
8264 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { | ||||
8265 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { | ||||
8266 | // Transform (X == Y) ? X : Y -> Y | ||||
8267 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) | ||||
8268 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8269 | // Transform (X != Y) ? X : Y -> X | ||||
8270 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | ||||
8271 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8272 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | ||||
8273 | |||||
8274 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ | ||||
8275 | // Transform (X == Y) ? Y : X -> X | ||||
8276 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) | ||||
8277 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8278 | // Transform (X != Y) ? Y : X -> Y | ||||
8279 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | ||||
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 8280 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8281 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
8282 | } | ||||
8283 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8284 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8285 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
8286 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) | ||||
8287 | if (TI->hasOneUse() && FI->hasOneUse()) { | ||||
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8288 | Instruction *AddOp = 0, *SubOp = 0; |
8289 | |||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8290 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
8291 | if (TI->getOpcode() == FI->getOpcode()) | ||||
8292 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) | ||||
8293 | return IV; | ||||
8294 | |||||
8295 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is | ||||
8296 | // even legal for FP. | ||||
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8297 | if (TI->getOpcode() == Instruction::Sub && |
8298 | FI->getOpcode() == Instruction::Add) { | ||||
8299 | AddOp = FI; SubOp = TI; | ||||
8300 | } else if (FI->getOpcode() == Instruction::Sub && | ||||
8301 | TI->getOpcode() == Instruction::Add) { | ||||
8302 | AddOp = TI; SubOp = FI; | ||||
8303 | } | ||||
8304 | |||||
8305 | if (AddOp) { | ||||
8306 | Value *OtherAddOp = 0; | ||||
8307 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { | ||||
8308 | OtherAddOp = AddOp->getOperand(1); | ||||
8309 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { | ||||
8310 | OtherAddOp = AddOp->getOperand(0); | ||||
8311 | } | ||||
8312 | |||||
8313 | if (OtherAddOp) { | ||||
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8314 | // So at this point we know we have (Y -> OtherAddOp): |
8315 | // select C, (add X, Y), (sub X, Z) | ||||
8316 | Value *NegVal; // Compute -Z | ||||
8317 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { | ||||
8318 | NegVal = ConstantExpr::getNeg(C); | ||||
8319 | } else { | ||||
8320 | NegVal = InsertNewInstBefore( | ||||
8321 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); | ||||
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8322 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8323 | |
8324 | Value *NewTrueOp = OtherAddOp; | ||||
8325 | Value *NewFalseOp = NegVal; | ||||
8326 | if (AddOp != TI) | ||||
8327 | std::swap(NewTrueOp, NewFalseOp); | ||||
8328 | Instruction *NewSel = | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8329 | SelectInst::Create(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8330 | |
8331 | NewSel = InsertNewInstBefore(NewSel, SI); | ||||
8332 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); | ||||
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8333 | } |
8334 | } | ||||
8335 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8336 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8337 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8338 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8339 | // See the comment above GetSelectFoldableOperands for a description of the |
8340 | // transformation we are doing here. | ||||
8341 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) | ||||
8342 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && | ||||
8343 | !isa<Constant>(FalseVal)) | ||||
8344 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { | ||||
8345 | unsigned OpToFold = 0; | ||||
8346 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { | ||||
8347 | OpToFold = 1; | ||||
8348 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { | ||||
8349 | OpToFold = 2; | ||||
8350 | } | ||||
8351 | |||||
8352 | if (OpToFold) { | ||||
8353 | Constant *C = GetSelectFoldableConstant(TVI); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8354 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8355 | SelectInst::Create(SI.getCondition(), TVI->getOperand(2-OpToFold), C); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8356 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8357 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8358 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
8359 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8360 | else { |
8361 | assert(0 && "Unknown instruction!!"); | ||||
8362 | } | ||||
8363 | } | ||||
8364 | } | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 8365 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8366 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
8367 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && | ||||
8368 | !isa<Constant>(TrueVal)) | ||||
8369 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { | ||||
8370 | unsigned OpToFold = 0; | ||||
8371 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { | ||||
8372 | OpToFold = 1; | ||||
8373 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { | ||||
8374 | OpToFold = 2; | ||||
8375 | } | ||||
8376 | |||||
8377 | if (OpToFold) { | ||||
8378 | Constant *C = GetSelectFoldableConstant(FVI); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8379 | Instruction *NewSel = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8380 | SelectInst::Create(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8381 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8382 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8383 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
8384 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8385 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8386 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8387 | } |
8388 | } | ||||
8389 | } | ||||
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 8390 | |
8391 | if (BinaryOperator::isNot(CondVal)) { | ||||
8392 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); | ||||
8393 | SI.setOperand(1, FalseVal); | ||||
8394 | SI.setOperand(2, TrueVal); | ||||
8395 | return &SI; | ||||
8396 | } | ||||
8397 | |||||
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8398 | return 0; |
8399 | } | ||||
8400 | |||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8401 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
8402 | /// we control, modify the object's alignment to PrefAlign. This isn't | ||||
8403 | /// often possible though. If alignment is important, a more reliable approach | ||||
8404 | /// is to simply align all global variables and allocation instructions to | ||||
8405 | /// their preferred alignment from the beginning. | ||||
8406 | /// | ||||
8407 | static unsigned EnforceKnownAlignment(Value *V, | ||||
8408 | unsigned Align, unsigned PrefAlign) { | ||||
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8409 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8410 | User *U = dyn_cast<User>(V); |
8411 | if (!U) return Align; | ||||
8412 | |||||
8413 | switch (getOpcode(U)) { | ||||
8414 | default: break; | ||||
8415 | case Instruction::BitCast: | ||||
8416 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); | ||||
8417 | case Instruction::GetElementPtr: { | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8418 | // If all indexes are zero, it is just the alignment of the base pointer. |
8419 | bool AllZeroOperands = true; | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8420 | for (unsigned i = 1, e = U->getNumOperands(); i != e; ++i) |
8421 | if (!isa<Constant>(U->getOperand(i)) || | ||||
8422 | !cast<Constant>(U->getOperand(i))->isNullValue()) { | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8423 | AllZeroOperands = false; |
8424 | break; | ||||
8425 | } | ||||
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8426 | |
8427 | if (AllZeroOperands) { | ||||
8428 | // Treat this like a bitcast. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8429 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8430 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8431 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8432 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8433 | } |
8434 | |||||
8435 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { | ||||
8436 | // If there is a large requested alignment and we can, bump up the alignment | ||||
8437 | // of the global. | ||||
8438 | if (!GV->isDeclaration()) { | ||||
8439 | GV->setAlignment(PrefAlign); | ||||
8440 | Align = PrefAlign; | ||||
8441 | } | ||||
8442 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { | ||||
8443 | // If there is a requested alignment and if this is an alloca, round up. We | ||||
8444 | // don't do this for malloc, because some systems can't respect the request. | ||||
8445 | if (isa<AllocaInst>(AI)) { | ||||
8446 | AI->setAlignment(PrefAlign); | ||||
8447 | Align = PrefAlign; | ||||
8448 | } | ||||
8449 | } | ||||
8450 | |||||
8451 | return Align; | ||||
8452 | } | ||||
8453 | |||||
8454 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that | ||||
8455 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, | ||||
8456 | /// and it is more than the alignment of the ultimate object, see if we can | ||||
8457 | /// increase the alignment of the ultimate object, making this check succeed. | ||||
8458 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, | ||||
8459 | unsigned PrefAlign) { | ||||
8460 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : | ||||
8461 | sizeof(PrefAlign) * CHAR_BIT; | ||||
8462 | APInt Mask = APInt::getAllOnesValue(BitWidth); | ||||
8463 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
8464 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); | ||||
8465 | unsigned TrailZ = KnownZero.countTrailingOnes(); | ||||
8466 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); | ||||
8467 | |||||
8468 | if (PrefAlign > Align) | ||||
8469 | Align = EnforceKnownAlignment(V, Align, PrefAlign); | ||||
8470 | |||||
8471 | // We don't need to make any adjustment. | ||||
8472 | return Align; | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8473 | } |
8474 | |||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8475 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8476 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
8477 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8478 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
8479 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); | ||||
8480 | |||||
8481 | if (CopyAlign < MinAlign) { | ||||
8482 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); | ||||
8483 | return MI; | ||||
8484 | } | ||||
8485 | |||||
8486 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with | ||||
8487 | // load/store. | ||||
8488 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); | ||||
8489 | if (MemOpLength == 0) return 0; | ||||
8490 | |||||
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8491 | // Source and destination pointer types are always "i8*" for intrinsic. See |
8492 | // if the size is something we can handle with a single primitive load/store. | ||||
8493 | // A single load+store correctly handles overlapping memory in the memmove | ||||
8494 | // case. | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8495 | unsigned Size = MemOpLength->getZExtValue(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8496 | if (Size == 0) return MI; // Delete this mem transfer. |
8497 | |||||
8498 | if (Size > 8 || (Size&(Size-1))) | ||||
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8499 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8500 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8501 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8502 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8503 | |
8504 | // Memcpy forces the use of i8* for the source and destination. That means | ||||
8505 | // that if you're using memcpy to move one double around, you'll get a cast | ||||
8506 | // from double* to i8*. We'd much rather use a double load+store rather than | ||||
8507 | // an i64 load+store, here because this improves the odds that the source or | ||||
8508 | // dest address will be promotable. See if we can find a better type than the | ||||
8509 | // integer datatype. | ||||
8510 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { | ||||
8511 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); | ||||
8512 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { | ||||
8513 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip | ||||
8514 | // down through these levels if so. | ||||
8515 | while (!SrcETy->isFirstClassType()) { | ||||
8516 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { | ||||
8517 | if (STy->getNumElements() == 1) | ||||
8518 | SrcETy = STy->getElementType(0); | ||||
8519 | else | ||||
8520 | break; | ||||
8521 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { | ||||
8522 | if (ATy->getNumElements() == 1) | ||||
8523 | SrcETy = ATy->getElementType(); | ||||
8524 | else | ||||
8525 | break; | ||||
8526 | } else | ||||
8527 | break; | ||||
8528 | } | ||||
8529 | |||||
8530 | if (SrcETy->isFirstClassType()) | ||||
8531 | NewPtrTy = PointerType::getUnqual(SrcETy); | ||||
8532 | } | ||||
8533 | } | ||||
8534 | |||||
8535 | |||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8536 | // If the memcpy/memmove provides better alignment info than we can |
8537 | // infer, use it. | ||||
8538 | SrcAlign = std::max(SrcAlign, CopyAlign); | ||||
8539 | DstAlign = std::max(DstAlign, CopyAlign); | ||||
8540 | |||||
8541 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); | ||||
8542 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); | ||||
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8543 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
8544 | InsertNewInstBefore(L, *MI); | ||||
8545 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); | ||||
8546 | |||||
8547 | // Set the size of the copy to 0, it will be deleted on the next iteration. | ||||
8548 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); | ||||
8549 | return MI; | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8550 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8551 | |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8552 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
8553 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); | ||||
8554 | if (MI->getAlignment()->getZExtValue() < Alignment) { | ||||
8555 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); | ||||
8556 | return MI; | ||||
8557 | } | ||||
8558 | |||||
8559 | // Extract the length and alignment and fill if they are constant. | ||||
8560 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); | ||||
8561 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); | ||||
8562 | if (!LenC || !FillC || FillC->getType() != Type::Int8Ty) | ||||
8563 | return 0; | ||||
8564 | uint64_t Len = LenC->getZExtValue(); | ||||
8565 | Alignment = MI->getAlignment()->getZExtValue(); | ||||
8566 | |||||
8567 | // If the length is zero, this is a no-op | ||||
8568 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop | ||||
8569 | |||||
8570 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) | ||||
8571 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { | ||||
8572 | const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8. | ||||
8573 | |||||
8574 | Value *Dest = MI->getDest(); | ||||
8575 | Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI); | ||||
8576 | |||||
8577 | // Alignment 0 is identity for alignment 1 for memset, but not store. | ||||
8578 | if (Alignment == 0) Alignment = 1; | ||||
8579 | |||||
8580 | // Extract the fill value and store. | ||||
8581 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; | ||||
8582 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false, | ||||
8583 | Alignment), *MI); | ||||
8584 | |||||
8585 | // Set the size of the copy to 0, it will be deleted on the next iteration. | ||||
8586 | MI->setLength(Constant::getNullValue(LenC->getType())); | ||||
8587 | return MI; | ||||
8588 | } | ||||
8589 | |||||
8590 | return 0; | ||||
8591 | } | ||||
8592 | |||||
8593 | |||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8594 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
8595 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do | ||||
8596 | /// the heavy lifting. | ||||
8597 | /// | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8598 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8599 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
8600 | if (!II) return visitCallSite(&CI); | ||||
8601 | |||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8602 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
8603 | // visitCallSite. | ||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8604 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8605 | bool Changed = false; |
8606 | |||||
8607 | // memmove/cpy/set of zero bytes is a noop. | ||||
8608 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { | ||||
8609 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); | ||||
8610 | |||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8611 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8612 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8613 | // Replace the instruction with just byte operations. We would |
8614 | // transform other cases to loads/stores, but we don't know if | ||||
8615 | // alignment is sufficient. | ||||
8616 | } | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8617 | } |
8618 | |||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8619 | // If we have a memmove and the source operation is a constant global, |
8620 | // then the source and dest pointers can't alias, so we can change this | ||||
8621 | // into a call to memcpy. | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8622 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8623 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
8624 | if (GVSrc->isConstant()) { | ||||
8625 | Module *M = CI.getParent()->getParent()->getParent(); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8626 | Intrinsic::ID MemCpyID; |
8627 | if (CI.getOperand(3)->getType() == Type::Int32Ty) | ||||
8628 | MemCpyID = Intrinsic::memcpy_i32; | ||||
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8629 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8630 | MemCpyID = Intrinsic::memcpy_i64; |
8631 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); | ||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8632 | Changed = true; |
8633 | } | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8634 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8635 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8636 | // If we can determine a pointer alignment that is bigger than currently |
8637 | // set, update the alignment. | ||||
8638 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8639 | if (Instruction *I = SimplifyMemTransfer(MI)) |
8640 | return I; | ||||
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8641 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
8642 | if (Instruction *I = SimplifyMemSet(MSI)) | ||||
8643 | return I; | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8644 | } |
8645 | |||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8646 | if (Changed) return II; |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8647 | } else { |
8648 | switch (II->getIntrinsicID()) { | ||||
8649 | default: break; | ||||
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8650 | case Intrinsic::ppc_altivec_lvx: |
8651 | case Intrinsic::ppc_altivec_lvxl: | ||||
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8652 | case Intrinsic::x86_sse_loadu_ps: |
8653 | case Intrinsic::x86_sse2_loadu_pd: | ||||
8654 | case Intrinsic::x86_sse2_loadu_dq: | ||||
8655 | // Turn PPC lvx -> load if the pointer is known aligned. | ||||
8656 | // Turn X86 loadups -> load if the pointer is known aligned. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8657 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8658 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
8659 | PointerType::getUnqual(II->getType()), | ||||
8660 | CI); | ||||
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8661 | return new LoadInst(Ptr); |
8662 | } | ||||
8663 | break; | ||||
8664 | case Intrinsic::ppc_altivec_stvx: | ||||
8665 | case Intrinsic::ppc_altivec_stvxl: | ||||
8666 | // Turn stvx -> store if the pointer is known aligned. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8667 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8668 | const Type *OpPtrTy = |
8669 | PointerType::getUnqual(II->getOperand(1)->getType()); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8670 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
Chris Lattner | 82ed58f | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 8671 | return new StoreInst(II->getOperand(1), Ptr); |
8672 | } | ||||
8673 | break; | ||||
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8674 | case Intrinsic::x86_sse_storeu_ps: |
8675 | case Intrinsic::x86_sse2_storeu_pd: | ||||
8676 | case Intrinsic::x86_sse2_storeu_dq: | ||||
8677 | case Intrinsic::x86_sse2_storel_dq: | ||||
8678 | // Turn X86 storeu -> store if the pointer is known aligned. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8679 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8680 | const Type *OpPtrTy = |
8681 | PointerType::getUnqual(II->getOperand(2)->getType()); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8682 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
Chris Lattner | fd6bdf0 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 8683 | return new StoreInst(II->getOperand(2), Ptr); |
8684 | } | ||||
8685 | break; | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8686 | |
8687 | case Intrinsic::x86_sse_cvttss2si: { | ||||
8688 | // These intrinsics only demands the 0th element of its input vector. If | ||||
8689 | // we can simplify the input based on that, do so now. | ||||
8690 | uint64_t UndefElts; | ||||
8691 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, | ||||
8692 | UndefElts)) { | ||||
8693 | II->setOperand(1, V); | ||||
8694 | return II; | ||||
8695 | } | ||||
8696 | break; | ||||
8697 | } | ||||
8698 | |||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8699 | case Intrinsic::ppc_altivec_vperm: |
8700 | // 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] | 8701 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8702 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
8703 | |||||
8704 | // Check that all of the elements are integer constants or undefs. | ||||
8705 | bool AllEltsOk = true; | ||||
8706 | for (unsigned i = 0; i != 16; ++i) { | ||||
8707 | if (!isa<ConstantInt>(Mask->getOperand(i)) && | ||||
8708 | !isa<UndefValue>(Mask->getOperand(i))) { | ||||
8709 | AllEltsOk = false; | ||||
8710 | break; | ||||
8711 | } | ||||
8712 | } | ||||
8713 | |||||
8714 | if (AllEltsOk) { | ||||
8715 | // Cast the input vectors to byte vectors. | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8716 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
8717 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); | ||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8718 | Value *Result = UndefValue::get(Op0->getType()); |
8719 | |||||
8720 | // Only extract each element once. | ||||
8721 | Value *ExtractedElts[32]; | ||||
8722 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); | ||||
8723 | |||||
8724 | for (unsigned i = 0; i != 16; ++i) { | ||||
8725 | if (isa<UndefValue>(Mask->getOperand(i))) | ||||
8726 | continue; | ||||
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 8727 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8728 | Idx &= 31; // Match the hardware behavior. |
8729 | |||||
8730 | if (ExtractedElts[Idx] == 0) { | ||||
8731 | Instruction *Elt = | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8732 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8733 | InsertNewInstBefore(Elt, CI); |
8734 | ExtractedElts[Idx] = Elt; | ||||
8735 | } | ||||
8736 | |||||
8737 | // Insert this value into the result vector. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8738 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], i, "tmp"); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8739 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
8740 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8741 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8742 | } |
8743 | } | ||||
8744 | break; | ||||
8745 | |||||
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8746 | case Intrinsic::stackrestore: { |
8747 | // If the save is right next to the restore, remove the restore. This can | ||||
8748 | // happen when variable allocas are DCE'd. | ||||
8749 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { | ||||
8750 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { | ||||
8751 | BasicBlock::iterator BI = SS; | ||||
8752 | if (&*++BI == II) | ||||
8753 | return EraseInstFromFunction(CI); | ||||
8754 | } | ||||
8755 | } | ||||
8756 | |||||
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8757 | // Scan down this block to see if there is another stack restore in the |
8758 | // same block without an intervening call/alloca. | ||||
8759 | BasicBlock::iterator BI = II; | ||||
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8760 | TerminatorInst *TI = II->getParent()->getTerminator(); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8761 | bool CannotRemove = false; |
8762 | for (++BI; &*BI != TI; ++BI) { | ||||
8763 | if (isa<AllocaInst>(BI)) { | ||||
8764 | CannotRemove = true; | ||||
8765 | break; | ||||
8766 | } | ||||
8767 | if (isa<CallInst>(BI)) { | ||||
8768 | if (!isa<IntrinsicInst>(BI)) { | ||||
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8769 | CannotRemove = true; |
8770 | break; | ||||
8771 | } | ||||
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8772 | // If there is a stackrestore below this one, remove this one. |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8773 | return EraseInstFromFunction(CI); |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8774 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8775 | } |
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8776 | |
8777 | // If the stack restore is in a return/unwind block and if there are no | ||||
8778 | // allocas or calls between the restore and the return, nuke the restore. | ||||
8779 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) | ||||
8780 | return EraseInstFromFunction(CI); | ||||
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8781 | break; |
8782 | } | ||||
8783 | } | ||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8784 | } |
8785 | |||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8786 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8787 | } |
8788 | |||||
8789 | // InvokeInst simplification | ||||
8790 | // | ||||
8791 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { | ||||
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8792 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8793 | } |
8794 | |||||
Dale Johannesen | da30ccb | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 8795 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
8796 | /// 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] | 8797 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
8798 | const CastInst * const CI, | ||||
8799 | const TargetData * const TD, | ||||
8800 | const int ix) { | ||||
8801 | if (!CI->isLosslessCast()) | ||||
8802 | return false; | ||||
8803 | |||||
8804 | // The size of ByVal arguments is derived from the type, so we | ||||
8805 | // can't change to a type with a different size. If the size were | ||||
8806 | // passed explicitly we could avoid this check. | ||||
8807 | if (!CS.paramHasAttr(ix, ParamAttr::ByVal)) | ||||
8808 | return true; | ||||
8809 | |||||
8810 | const Type* SrcTy = | ||||
8811 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); | ||||
8812 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); | ||||
8813 | if (!SrcTy->isSized() || !DstTy->isSized()) | ||||
8814 | return false; | ||||
8815 | if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy)) | ||||
8816 | return false; | ||||
8817 | return true; | ||||
8818 | } | ||||
8819 | |||||
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8820 | // visitCallSite - Improvements for call and invoke instructions. |
8821 | // | ||||
8822 | Instruction *InstCombiner::visitCallSite(CallSite CS) { | ||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8823 | bool Changed = false; |
8824 | |||||
8825 | // If the callee is a constexpr cast of a function, attempt to move the cast | ||||
8826 | // to the arguments of the call/invoke. | ||||
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8827 | if (transformConstExprCastCall(CS)) return 0; |
8828 | |||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8829 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8830 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8831 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
8832 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { | ||||
8833 | Instruction *OldCall = CS.getInstruction(); | ||||
8834 | // If the call and callee calling conventions don't match, this call must | ||||
8835 | // be unreachable, as the call is undefined. | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8836 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8837 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
8838 | OldCall); | ||||
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8839 | if (!OldCall->use_empty()) |
8840 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); | ||||
8841 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. | ||||
8842 | return EraseInstFromFunction(*OldCall); | ||||
8843 | return 0; | ||||
8844 | } | ||||
8845 | |||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8846 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
8847 | // This instruction is not reachable, just remove it. We insert a store to | ||||
8848 | // undef so that we know that this code is not reachable, despite the fact | ||||
8849 | // that we can't modify the CFG here. | ||||
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)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8852 | CS.getInstruction()); |
8853 | |||||
8854 | if (!CS.getInstruction()->use_empty()) | ||||
8855 | CS.getInstruction()-> | ||||
8856 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); | ||||
8857 | |||||
8858 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { | ||||
8859 | // Don't break the CFG, insert a dummy cond branch. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8860 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
8861 | ConstantInt::getTrue(), II); | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8862 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8863 | return EraseInstFromFunction(*CS.getInstruction()); |
8864 | } | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8865 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8866 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
8867 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) | ||||
8868 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) | ||||
8869 | return transformCallThroughTrampoline(CS); | ||||
8870 | |||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8871 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
8872 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); | ||||
8873 | if (FTy->isVarArg()) { | ||||
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 8874 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8875 | // See if we can optimize any arguments passed through the varargs area of |
8876 | // the call. | ||||
8877 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), | ||||
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8878 | E = CS.arg_end(); I != E; ++I, ++ix) { |
8879 | CastInst *CI = dyn_cast<CastInst>(*I); | ||||
8880 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { | ||||
8881 | *I = CI->getOperand(0); | ||||
8882 | Changed = true; | ||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8883 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8884 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8885 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8886 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8887 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8888 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8889 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8890 | Changed = true; |
8891 | } | ||||
8892 | |||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8893 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8894 | } |
8895 | |||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8896 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
8897 | // attempt to move the cast to the arguments of the call/invoke. | ||||
8898 | // | ||||
8899 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { | ||||
8900 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; | ||||
8901 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8902 | if (CE->getOpcode() != Instruction::BitCast || |
8903 | !isa<Function>(CE->getOperand(0))) | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8904 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8905 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8906 | Instruction *Caller = CS.getInstruction(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8907 | const PAListPtr &CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8908 | |
8909 | // Okay, this is a cast from a function to a different type. Unless doing so | ||||
8910 | // would cause a type conversion of one of our arguments, change this call to | ||||
8911 | // be a direct call with arguments casted to the appropriate types. | ||||
8912 | // | ||||
8913 | const FunctionType *FT = Callee->getFunctionType(); | ||||
8914 | const Type *OldRetTy = Caller->getType(); | ||||
8915 | |||||
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 8916 | if (isa<StructType>(FT->getReturnType())) |
8917 | return false; // TODO: Handle multiple return values. | ||||
8918 | |||||
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8919 | // Check to see if we are changing the return type... |
8920 | if (OldRetTy != FT->getReturnType()) { | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8921 | if (Callee->isDeclaration() && !Caller->use_empty() && |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8922 | // Conversion is ok if changing from pointer to int of same size. |
8923 | !(isa<PointerType>(FT->getReturnType()) && | ||||
8924 | TD->getIntPtrType() == OldRetTy)) | ||||
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8925 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8926 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8927 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8928 | // void -> non-void is handled specially |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8929 | FT->getReturnType() != Type::VoidTy && |
8930 | !CastInst::isCastable(FT->getReturnType(), OldRetTy)) | ||||
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8931 | return false; // Cannot transform this return value. |
8932 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8933 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
8934 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); | ||||
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 8935 | if (RAttrs & ParamAttr::typeIncompatible(FT->getReturnType())) |
8936 | return false; // Attribute not compatible with transformed value. | ||||
8937 | } | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8938 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8939 | // If the callsite is an invoke instruction, and the return value is used by |
8940 | // a PHI node in a successor, we cannot change the return type of the call | ||||
8941 | // because there is no place to put the cast instruction (without breaking | ||||
8942 | // the critical edge). Bail out in this case. | ||||
8943 | if (!Caller->use_empty()) | ||||
8944 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) | ||||
8945 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); | ||||
8946 | UI != E; ++UI) | ||||
8947 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) | ||||
8948 | if (PN->getParent() == II->getNormalDest() || | ||||
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 8949 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 8950 | return false; |
8951 | } | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8952 | |
8953 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); | ||||
8954 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8955 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8956 | CallSite::arg_iterator AI = CS.arg_begin(); |
8957 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { | ||||
8958 | const Type *ParamTy = FT->getParamType(i); | ||||
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 8959 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8960 | |
8961 | if (!CastInst::isCastable(ActTy, ParamTy)) | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8962 | return false; // Cannot transform this parameter value. |
8963 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8964 | if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy)) |
8965 | return false; // Attribute not compatible with transformed value. | ||||
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8966 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8967 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 8968 | // Some conversions are safe even if we do not have a body. |
8969 | // Either we can cast directly, or we can upconvert the argument | ||||
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 8970 | bool isConvertible = ActTy == ParamTy || |
Chris Lattner | 46013f4 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 8971 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8972 | (ParamTy->isInteger() && ActTy->isInteger() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 8973 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || |
8974 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() | ||||
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 8975 | && c->getValue().isStrictlyPositive()); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8976 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8977 | } |
8978 | |||||
8979 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8980 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8981 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8982 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8983 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
8984 | !CallerPAL.isEmpty()) | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8985 | // 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] | 8986 | // won't be dropping them. Check that these extra arguments have attributes |
8987 | // that are compatible with being a vararg call argument. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8988 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
8989 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) | ||||
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8990 | break; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 8991 | ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 8992 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
8993 | return false; | ||||
8994 | } | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 8995 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8996 | // Okay, we decided that this is a safe thing to do: go ahead and start |
8997 | // inserting cast instructions as necessary... | ||||
8998 | std::vector<Value*> Args; | ||||
8999 | Args.reserve(NumActualArgs); | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9000 | SmallVector<ParamAttrsWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9001 | attrVec.reserve(NumCommonArgs); |
9002 | |||||
9003 | // Get any return attributes. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9004 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9005 | |
9006 | // If the return value is not being used, the type may not be compatible | ||||
9007 | // with the existing attributes. Wipe out any problematic attributes. | ||||
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 9008 | RAttrs &= ~ParamAttr::typeIncompatible(FT->getReturnType()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9009 | |
9010 | // Add the new return attributes. | ||||
9011 | if (RAttrs) | ||||
9012 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9013 | |
9014 | AI = CS.arg_begin(); | ||||
9015 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { | ||||
9016 | const Type *ParamTy = FT->getParamType(i); | ||||
9017 | if ((*AI)->getType() == ParamTy) { | ||||
9018 | Args.push_back(*AI); | ||||
9019 | } else { | ||||
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 9020 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9021 | false, ParamTy, false); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 9022 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9023 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9024 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9025 | |
9026 | // Add any parameter attributes. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9027 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9028 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9029 | } |
9030 | |||||
9031 | // If the function takes more arguments than the call was taking, add them | ||||
9032 | // now... | ||||
9033 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) | ||||
9034 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); | ||||
9035 | |||||
9036 | // If we are removing arguments to the function, emit an obnoxious warning... | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9037 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9038 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 9039 | cerr << "WARNING: While resolving call to function '" |
9040 | << Callee->getName() << "' arguments were dropped!\n"; | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9041 | } else { |
9042 | // Add all of the arguments in their promoted form to the arg list... | ||||
9043 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { | ||||
9044 | const Type *PTy = getPromotedType((*AI)->getType()); | ||||
9045 | if (PTy != (*AI)->getType()) { | ||||
9046 | // Must promote to pass through va_arg area! | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9047 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
9048 | PTy, false); | ||||
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 9049 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9050 | InsertNewInstBefore(Cast, *Caller); |
9051 | Args.push_back(Cast); | ||||
9052 | } else { | ||||
9053 | Args.push_back(*AI); | ||||
9054 | } | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9055 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9056 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9057 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9058 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
9059 | } | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9060 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9061 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9062 | |
9063 | if (FT->getReturnType() == Type::VoidTy) | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9064 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9065 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9066 | const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9067 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9068 | Instruction *NC; |
9069 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9070 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
9071 | Args.begin(), Args.end(), Caller->getName(), Caller); | ||||
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 9072 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9073 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9074 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9075 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
9076 | Caller->getName(), Caller); | ||||
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9077 | CallInst *CI = cast<CallInst>(Caller); |
9078 | if (CI->isTailCall()) | ||||
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 9079 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9080 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9081 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9082 | } |
9083 | |||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9084 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9085 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9086 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9087 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9088 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9089 | OldRetTy, false); |
9090 | NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp"); | ||||
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9091 | |
9092 | // If this is an invoke instruction, we should insert it after the first | ||||
9093 | // non-phi, instruction in the normal successor block. | ||||
9094 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | ||||
9095 | BasicBlock::iterator I = II->getNormalDest()->begin(); | ||||
9096 | while (isa<PHINode>(I)) ++I; | ||||
9097 | InsertNewInstBefore(NC, *I); | ||||
9098 | } else { | ||||
9099 | // Otherwise, it's a call, just insert cast right after the call instr | ||||
9100 | InsertNewInstBefore(NC, *Caller); | ||||
9101 | } | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9102 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9103 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 9104 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9105 | } |
9106 | } | ||||
9107 | |||||
9108 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) | ||||
9109 | Caller->replaceAllUsesWith(NV); | ||||
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 9110 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9111 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9112 | return true; |
9113 | } | ||||
9114 | |||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9115 | // transformCallThroughTrampoline - Turn a call to a function created by the |
9116 | // init_trampoline intrinsic into a direct call to the underlying function. | ||||
9117 | // | ||||
9118 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { | ||||
9119 | Value *Callee = CS.getCalledValue(); | ||||
9120 | const PointerType *PTy = cast<PointerType>(Callee->getType()); | ||||
9121 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9122 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9123 | |
9124 | // If the call already has the 'nest' attribute somewhere then give up - | ||||
9125 | // otherwise 'nest' would occur twice after splicing in the chain. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9126 | if (Attrs.hasAttrSomewhere(ParamAttr::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9127 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9128 | |
9129 | IntrinsicInst *Tramp = | ||||
9130 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); | ||||
9131 | |||||
9132 | Function *NestF = | ||||
9133 | cast<Function>(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); | ||||
9134 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); | ||||
9135 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); | ||||
9136 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9137 | const PAListPtr &NestAttrs = NestF->getParamAttrs(); |
9138 | if (!NestAttrs.isEmpty()) { | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9139 | unsigned NestIdx = 1; |
9140 | const Type *NestTy = 0; | ||||
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 9141 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9142 | |
9143 | // Look for a parameter marked with the 'nest' attribute. | ||||
9144 | for (FunctionType::param_iterator I = NestFTy->param_begin(), | ||||
9145 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9146 | if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9147 | // Record the parameter type and any other attributes. |
9148 | NestTy = *I; | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9149 | NestAttr = NestAttrs.getParamAttrs(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9150 | break; |
9151 | } | ||||
9152 | |||||
9153 | if (NestTy) { | ||||
9154 | Instruction *Caller = CS.getInstruction(); | ||||
9155 | std::vector<Value*> NewArgs; | ||||
9156 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); | ||||
9157 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9158 | SmallVector<ParamAttrsWithIndex, 8> NewAttrs; |
9159 | NewAttrs.reserve(Attrs.getNumSlots() + 1); | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9160 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9161 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9162 | // mean appending it. Likewise for attributes. |
9163 | |||||
9164 | // Add any function result attributes. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9165 | if (ParameterAttributes Attr = Attrs.getParamAttrs(0)) |
9166 | NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr)); | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9167 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9168 | { |
9169 | unsigned Idx = 1; | ||||
9170 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); | ||||
9171 | do { | ||||
9172 | if (Idx == NestIdx) { | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9173 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9174 | Value *NestVal = Tramp->getOperand(3); |
9175 | if (NestVal->getType() != NestTy) | ||||
9176 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); | ||||
9177 | NewArgs.push_back(NestVal); | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9178 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9179 | } |
9180 | |||||
9181 | if (I == E) | ||||
9182 | break; | ||||
9183 | |||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9184 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9185 | NewArgs.push_back(*I); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9186 | if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9187 | NewAttrs.push_back |
9188 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9189 | |
9190 | ++Idx, ++I; | ||||
9191 | } while (1); | ||||
9192 | } | ||||
9193 | |||||
9194 | // The trampoline may have been bitcast to a bogus type (FTy). | ||||
9195 | // Handle this by synthesizing a new function type, equal to FTy | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9196 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9197 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9198 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9199 | NewTypes.reserve(FTy->getNumParams()+1); |
9200 | |||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9201 | // 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] | 9202 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9203 | { |
9204 | unsigned Idx = 1; | ||||
9205 | FunctionType::param_iterator I = FTy->param_begin(), | ||||
9206 | E = FTy->param_end(); | ||||
9207 | |||||
9208 | do { | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9209 | if (Idx == NestIdx) |
9210 | // Add the chain's type. | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9211 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9212 | |
9213 | if (I == E) | ||||
9214 | break; | ||||
9215 | |||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9216 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9217 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9218 | |
9219 | ++Idx, ++I; | ||||
9220 | } while (1); | ||||
9221 | } | ||||
9222 | |||||
9223 | // Replace the trampoline call with a direct call. Let the generic | ||||
9224 | // code sort out any function type mismatches. | ||||
9225 | FunctionType *NewFTy = | ||||
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9226 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9227 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
9228 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9229 | const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9230 | |
9231 | Instruction *NewCaller; | ||||
9232 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9233 | NewCaller = InvokeInst::Create(NewCallee, |
9234 | II->getNormalDest(), II->getUnwindDest(), | ||||
9235 | NewArgs.begin(), NewArgs.end(), | ||||
9236 | Caller->getName(), Caller); | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9237 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9238 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9239 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9240 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
9241 | Caller->getName(), Caller); | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9242 | if (cast<CallInst>(Caller)->isTailCall()) |
9243 | cast<CallInst>(NewCaller)->setTailCall(); | ||||
9244 | cast<CallInst>(NewCaller)-> | ||||
9245 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); | ||||
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9246 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9247 | } |
9248 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) | ||||
9249 | Caller->replaceAllUsesWith(NewCaller); | ||||
9250 | Caller->eraseFromParent(); | ||||
9251 | RemoveFromWorkList(Caller); | ||||
9252 | return 0; | ||||
9253 | } | ||||
9254 | } | ||||
9255 | |||||
9256 | // Replace the trampoline call with a direct call. Since there is no 'nest' | ||||
9257 | // parameter, there is no need to adjust the argument list. Let the generic | ||||
9258 | // code sort out any function type mismatches. | ||||
9259 | Constant *NewCallee = | ||||
9260 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); | ||||
9261 | CS.setCalledFunction(NewCallee); | ||||
9262 | return CS.getInstruction(); | ||||
9263 | } | ||||
9264 | |||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9265 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
9266 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's | ||||
9267 | /// and a single binop. | ||||
9268 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { | ||||
9269 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9270 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
9271 | isa<CmpInst>(FirstInst)); | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9272 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9273 | Value *LHSVal = FirstInst->getOperand(0); |
9274 | Value *RHSVal = FirstInst->getOperand(1); | ||||
9275 | |||||
9276 | const Type *LHSType = LHSVal->getType(); | ||||
9277 | const Type *RHSType = RHSVal->getType(); | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9278 | |
9279 | // Scan to see if all operands are the same opcode, all have one use, and all | ||||
9280 | // kill their operands (i.e. the operands have one use). | ||||
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9281 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9282 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9283 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9284 | // 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] | 9285 | // types or GEP's with different index types. |
9286 | I->getOperand(0)->getType() != LHSType || | ||||
9287 | I->getOperand(1)->getType() != RHSType) | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9288 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9289 | |
9290 | // If they are CmpInst instructions, check their predicates | ||||
9291 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) | ||||
9292 | if (cast<CmpInst>(I)->getPredicate() != | ||||
9293 | cast<CmpInst>(FirstInst)->getPredicate()) | ||||
9294 | return 0; | ||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9295 | |
9296 | // Keep track of which operand needs a phi node. | ||||
9297 | if (I->getOperand(0) != LHSVal) LHSVal = 0; | ||||
9298 | if (I->getOperand(1) != RHSVal) RHSVal = 0; | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9299 | } |
9300 | |||||
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9301 | // Otherwise, this is safe to transform, determine if it is profitable. |
9302 | |||||
9303 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. | ||||
9304 | // Indexes are often folded into load/store instructions, so we don't want to | ||||
9305 | // hide them behind a phi. | ||||
9306 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) | ||||
9307 | return 0; | ||||
9308 | |||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9309 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9310 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9311 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9312 | if (LHSVal == 0) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9313 | NewLHS = PHINode::Create(LHSType, FirstInst->getOperand(0)->getName()+".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9314 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
9315 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); | ||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9316 | InsertNewInstBefore(NewLHS, PN); |
9317 | LHSVal = NewLHS; | ||||
9318 | } | ||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9319 | |
9320 | if (RHSVal == 0) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9321 | NewRHS = PHINode::Create(RHSType, FirstInst->getOperand(1)->getName()+".pn"); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9322 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
9323 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); | ||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9324 | InsertNewInstBefore(NewRHS, PN); |
9325 | RHSVal = NewRHS; | ||||
9326 | } | ||||
9327 | |||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9328 | // Add all operands to the new PHIs. |
9329 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | ||||
9330 | if (NewLHS) { | ||||
9331 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); | ||||
9332 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); | ||||
9333 | } | ||||
9334 | if (NewRHS) { | ||||
9335 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); | ||||
9336 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); | ||||
9337 | } | ||||
9338 | } | ||||
9339 | |||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9340 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9341 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9342 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
9343 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, | ||||
9344 | RHSVal); | ||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9345 | else { |
9346 | assert(isa<GetElementPtrInst>(FirstInst)); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9347 | return GetElementPtrInst::Create(LHSVal, RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9348 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9349 | } |
9350 | |||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9351 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
9352 | /// of the block that defines it. This means that it must be obvious the value | ||||
9353 | /// of the load is not changed from the point of the load to the end of the | ||||
9354 | /// block it is in. | ||||
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9355 | /// |
9356 | /// Finally, it is safe, but not profitable, to sink a load targetting a | ||||
9357 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca | ||||
9358 | /// to a register. | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9359 | static bool isSafeToSinkLoad(LoadInst *L) { |
9360 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); | ||||
9361 | |||||
9362 | for (++BBI; BBI != E; ++BBI) | ||||
9363 | if (BBI->mayWriteToMemory()) | ||||
9364 | return false; | ||||
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9365 | |
9366 | // Check for non-address taken alloca. If not address-taken already, it isn't | ||||
9367 | // profitable to do this xform. | ||||
9368 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { | ||||
9369 | bool isAddressTaken = false; | ||||
9370 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); | ||||
9371 | UI != E; ++UI) { | ||||
9372 | if (isa<LoadInst>(UI)) continue; | ||||
9373 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { | ||||
9374 | // If storing TO the alloca, then the address isn't taken. | ||||
9375 | if (SI->getOperand(1) == AI) continue; | ||||
9376 | } | ||||
9377 | isAddressTaken = true; | ||||
9378 | break; | ||||
9379 | } | ||||
9380 | |||||
9381 | if (!isAddressTaken) | ||||
9382 | return false; | ||||
9383 | } | ||||
9384 | |||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9385 | return true; |
9386 | } | ||||
9387 | |||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9388 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9389 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
9390 | // operator and they all are only used by the PHI, PHI together their | ||||
9391 | // inputs, and do the operation once, to the result of the PHI. | ||||
9392 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { | ||||
9393 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); | ||||
9394 | |||||
9395 | // Scan the instruction, looking for input operations that can be folded away. | ||||
9396 | // If all input operands to the phi are the same instruction (e.g. a cast from | ||||
9397 | // the same type or "+42") we can pull the operation through the PHI, reducing | ||||
9398 | // code size and simplifying code. | ||||
9399 | Constant *ConstantOp = 0; | ||||
9400 | const Type *CastSrcTy = 0; | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9401 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9402 | if (isa<CastInst>(FirstInst)) { |
9403 | CastSrcTy = FirstInst->getOperand(0)->getType(); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9404 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9405 | // Can fold binop, compare or shift here if the RHS is a constant, |
9406 | // otherwise call FoldPHIArgBinOpIntoPHI. | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9407 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9408 | if (ConstantOp == 0) |
9409 | return FoldPHIArgBinOpIntoPHI(PN); | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9410 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
9411 | isVolatile = LI->isVolatile(); | ||||
9412 | // We can't sink the load if the loaded value could be modified between the | ||||
9413 | // load and the PHI. | ||||
9414 | if (LI->getParent() != PN.getIncomingBlock(0) || | ||||
9415 | !isSafeToSinkLoad(LI)) | ||||
9416 | return 0; | ||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9417 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9418 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9419 | return FoldPHIArgBinOpIntoPHI(PN); |
9420 | // Can't handle general GEPs yet. | ||||
9421 | return 0; | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9422 | } else { |
9423 | return 0; // Cannot fold this operation. | ||||
9424 | } | ||||
9425 | |||||
9426 | // Check to see if all arguments are the same operation. | ||||
9427 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | ||||
9428 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; | ||||
9429 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9430 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9431 | return 0; |
9432 | if (CastSrcTy) { | ||||
9433 | if (I->getOperand(0)->getType() != CastSrcTy) | ||||
9434 | return 0; // Cast operation must match. | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9435 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9436 | // We can't sink the load if the loaded value could be modified between |
9437 | // the load and the PHI. | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9438 | if (LI->isVolatile() != isVolatile || |
9439 | LI->getParent() != PN.getIncomingBlock(i) || | ||||
9440 | !isSafeToSinkLoad(LI)) | ||||
9441 | return 0; | ||||
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 9442 | |
9443 | // If the PHI is volatile and its block has multiple successors, sinking | ||||
9444 | // it would remove a load of the volatile value from the path through the | ||||
9445 | // other successor. | ||||
9446 | if (isVolatile && | ||||
9447 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) | ||||
9448 | return 0; | ||||
9449 | |||||
9450 | |||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9451 | } else if (I->getOperand(1) != ConstantOp) { |
9452 | return 0; | ||||
9453 | } | ||||
9454 | } | ||||
9455 | |||||
9456 | // Okay, they are all the same operation. Create a new PHI node of the | ||||
9457 | // 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] | 9458 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
9459 | PN.getName()+".in"); | ||||
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 9460 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9461 | |
9462 | Value *InVal = FirstInst->getOperand(0); | ||||
9463 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9464 | |
9465 | // Add all operands to the new PHI. | ||||
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9466 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
9467 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); | ||||
9468 | if (NewInVal != InVal) | ||||
9469 | InVal = 0; | ||||
9470 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); | ||||
9471 | } | ||||
9472 | |||||
9473 | Value *PhiVal; | ||||
9474 | if (InVal) { | ||||
9475 | // The new PHI unions all of the same values together. This is really | ||||
9476 | // common, so we handle it intelligently here for compile-time speed. | ||||
9477 | PhiVal = InVal; | ||||
9478 | delete NewPN; | ||||
9479 | } else { | ||||
9480 | InsertNewInstBefore(NewPN, PN); | ||||
9481 | PhiVal = NewPN; | ||||
9482 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9483 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9484 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9485 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
9486 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); | ||||
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9487 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9488 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9489 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9490 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), |
9491 | PhiVal, ConstantOp); | ||||
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9492 | assert(isa<LoadInst>(FirstInst) && "Unknown operation"); |
9493 | |||||
9494 | // If this was a volatile load that we are merging, make sure to loop through | ||||
9495 | // and mark all the input loads as non-volatile. If we don't do this, we will | ||||
9496 | // insert a new volatile load and the old ones will not be deletable. | ||||
9497 | if (isVolatile) | ||||
9498 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) | ||||
9499 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); | ||||
9500 | |||||
9501 | return new LoadInst(PhiVal, "", isVolatile); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9502 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9503 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9504 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
9505 | /// that is dead. | ||||
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9506 | static bool DeadPHICycle(PHINode *PN, |
9507 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { | ||||
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9508 | if (PN->use_empty()) return true; |
9509 | if (!PN->hasOneUse()) return false; | ||||
9510 | |||||
9511 | // Remember this node, and if we find the cycle, return. | ||||
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9512 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9513 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 9514 | |
9515 | // Don't scan crazily complex things. | ||||
9516 | if (PotentiallyDeadPHIs.size() == 16) | ||||
9517 | return false; | ||||
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9518 | |
9519 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) | ||||
9520 | return DeadPHICycle(PU, PotentiallyDeadPHIs); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9521 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9522 | return false; |
9523 | } | ||||
9524 | |||||
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9525 | /// PHIsEqualValue - Return true if this phi node is always equal to |
9526 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: | ||||
9527 | /// z = some value; x = phi (y, z); y = phi (x, z) | ||||
9528 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, | ||||
9529 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { | ||||
9530 | // See if we already saw this PHI node. | ||||
9531 | if (!ValueEqualPHIs.insert(PN)) | ||||
9532 | return true; | ||||
9533 | |||||
9534 | // Don't scan crazily complex things. | ||||
9535 | if (ValueEqualPHIs.size() == 16) | ||||
9536 | return false; | ||||
9537 | |||||
9538 | // Scan the operands to see if they are either phi nodes or are equal to | ||||
9539 | // the value. | ||||
9540 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { | ||||
9541 | Value *Op = PN->getIncomingValue(i); | ||||
9542 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { | ||||
9543 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) | ||||
9544 | return false; | ||||
9545 | } else if (Op != NonPhiInVal) | ||||
9546 | return false; | ||||
9547 | } | ||||
9548 | |||||
9549 | return true; | ||||
9550 | } | ||||
9551 | |||||
9552 | |||||
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9553 | // PHINode simplification |
9554 | // | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9555 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9556 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9557 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9558 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9559 | if (Value *V = PN.hasConstantValue()) |
9560 | return ReplaceInstUsesWith(PN, V); | ||||
9561 | |||||
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9562 | // If all PHI operands are the same operation, pull them through the PHI, |
9563 | // reducing code size. | ||||
9564 | if (isa<Instruction>(PN.getIncomingValue(0)) && | ||||
9565 | PN.getIncomingValue(0)->hasOneUse()) | ||||
9566 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) | ||||
9567 | return Result; | ||||
9568 | |||||
9569 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if | ||||
9570 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a | ||||
9571 | // PHI)... break the cycle. | ||||
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9572 | if (PN.hasOneUse()) { |
9573 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); | ||||
9574 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { | ||||
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9575 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9576 | PotentiallyDeadPHIs.insert(&PN); |
9577 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) | ||||
9578 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); | ||||
9579 | } | ||||
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9580 | |
9581 | // If this phi has a single use, and if that use just computes a value for | ||||
9582 | // the next iteration of a loop, delete the phi. This occurs with unused | ||||
9583 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this | ||||
9584 | // common case here is good because the only other things that catch this | ||||
9585 | // are induction variable analysis (sometimes) and ADCE, which is only run | ||||
9586 | // late. | ||||
9587 | if (PHIUser->hasOneUse() && | ||||
9588 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && | ||||
9589 | PHIUser->use_back() == &PN) { | ||||
9590 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); | ||||
9591 | } | ||||
9592 | } | ||||
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9593 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9594 | // We sometimes end up with phi cycles that non-obviously end up being the |
9595 | // same value, for example: | ||||
9596 | // z = some value; x = phi (y, z); y = phi (x, z) | ||||
9597 | // where the phi nodes don't necessarily need to be in the same block. Do a | ||||
9598 | // quick check to see if the PHI node only contains a single non-phi value, if | ||||
9599 | // so, scan to see if the phi cycle is actually equal to that value. | ||||
9600 | { | ||||
9601 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); | ||||
9602 | // Scan for the first non-phi operand. | ||||
9603 | while (InValNo != NumOperandVals && | ||||
9604 | isa<PHINode>(PN.getIncomingValue(InValNo))) | ||||
9605 | ++InValNo; | ||||
9606 | |||||
9607 | if (InValNo != NumOperandVals) { | ||||
9608 | Value *NonPhiInVal = PN.getOperand(InValNo); | ||||
9609 | |||||
9610 | // Scan the rest of the operands to see if there are any conflicts, if so | ||||
9611 | // there is no need to recursively scan other phis. | ||||
9612 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { | ||||
9613 | Value *OpVal = PN.getIncomingValue(InValNo); | ||||
9614 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) | ||||
9615 | break; | ||||
9616 | } | ||||
9617 | |||||
9618 | // If we scanned over all operands, then we have one unique value plus | ||||
9619 | // phi values. Scan PHI nodes to see if they all merge in each other or | ||||
9620 | // the value. | ||||
9621 | if (InValNo == NumOperandVals) { | ||||
9622 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; | ||||
9623 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) | ||||
9624 | return ReplaceInstUsesWith(PN, NonPhiInVal); | ||||
9625 | } | ||||
9626 | } | ||||
9627 | } | ||||
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9628 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9629 | } |
9630 | |||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9631 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
9632 | Instruction *InsertPoint, | ||||
9633 | InstCombiner *IC) { | ||||
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9634 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
9635 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9636 | // We must cast correctly to the pointer type. Ensure that we |
9637 | // sign extend the integer value if it is smaller as this is | ||||
9638 | // used for address computation. | ||||
9639 | Instruction::CastOps opcode = | ||||
9640 | (VTySize < PtrSize ? Instruction::SExt : | ||||
9641 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); | ||||
9642 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9643 | } |
9644 | |||||
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9645 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9646 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9647 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9648 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9649 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9650 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9651 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9652 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9653 | if (isa<UndefValue>(GEP.getOperand(0))) |
9654 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); | ||||
9655 | |||||
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9656 | bool HasZeroPointerIndex = false; |
9657 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) | ||||
9658 | HasZeroPointerIndex = C->isNullValue(); | ||||
9659 | |||||
9660 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9661 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9662 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9663 | // Eliminate unneeded casts for indices. |
9664 | bool MadeChange = false; | ||||
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9665 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9666 | gep_type_iterator GTI = gep_type_begin(GEP); |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9667 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9668 | if (isa<SequentialType>(*GTI)) { |
9669 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { | ||||
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9670 | if (CI->getOpcode() == Instruction::ZExt || |
9671 | CI->getOpcode() == Instruction::SExt) { | ||||
9672 | const Type *SrcTy = CI->getOperand(0)->getType(); | ||||
9673 | // We can eliminate a cast from i32 to i64 iff the target | ||||
9674 | // is a 32-bit pointer target. | ||||
9675 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { | ||||
9676 | MadeChange = true; | ||||
9677 | GEP.setOperand(i, CI->getOperand(0)); | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9678 | } |
9679 | } | ||||
9680 | } | ||||
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9681 | // If we are using a wider index than needed for this platform, shrink it |
9682 | // to what we need. If the incoming value needs a cast instruction, | ||||
9683 | // insert it. This explicit cast can make subsequent optimizations more | ||||
9684 | // obvious. | ||||
9685 | Value *Op = GEP.getOperand(i); | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9686 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9687 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9688 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9689 | MadeChange = true; |
9690 | } else { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9691 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
9692 | GEP); | ||||
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9693 | GEP.setOperand(i, Op); |
9694 | MadeChange = true; | ||||
9695 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9696 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9697 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9698 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9699 | if (MadeChange) return &GEP; |
9700 | |||||
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9701 | // If this GEP instruction doesn't move the pointer, and if the input operand |
9702 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the | ||||
9703 | // real input to the dest type. | ||||
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9704 | if (GEP.hasAllZeroIndices()) { |
9705 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { | ||||
9706 | // If the bitcast is of an allocation, and the allocation will be | ||||
9707 | // converted to match the type of the cast, don't touch this. | ||||
9708 | if (isa<AllocationInst>(BCI->getOperand(0))) { | ||||
9709 | // 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] | 9710 | if (Instruction *I = visitBitCast(*BCI)) { |
9711 | if (I != BCI) { | ||||
9712 | I->takeName(BCI); | ||||
9713 | BCI->getParent()->getInstList().insert(BCI, I); | ||||
9714 | ReplaceInstUsesWith(*BCI, I); | ||||
9715 | } | ||||
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9716 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9717 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9718 | } |
9719 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); | ||||
9720 | } | ||||
9721 | } | ||||
9722 | |||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9723 | // Combine Indices - If the source pointer to this getelementptr instruction |
9724 | // is a getelementptr instruction, combine the indices of the two | ||||
9725 | // getelementptr instructions into a single instruction. | ||||
9726 | // | ||||
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9727 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9728 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9729 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9730 | |
9731 | if (!SrcGEPOperands.empty()) { | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9732 | // Note that if our source is a gep chain itself that we wait for that |
9733 | // chain to be resolved before we perform this transformation. This | ||||
9734 | // avoids us creating a TON of code in some cases. | ||||
9735 | // | ||||
9736 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && | ||||
9737 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) | ||||
9738 | return 0; // Wait until our source is folded to completion. | ||||
9739 | |||||
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9740 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9741 | |
9742 | // Find out whether the last index in the source GEP is a sequential idx. | ||||
9743 | bool EndsWithSequential = false; | ||||
9744 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), | ||||
9745 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) | ||||
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9746 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9747 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9748 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9749 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9750 | // Replace: gep (gep %P, long B), long A, ... |
9751 | // With: T = long A+B; gep %P, T, ... | ||||
9752 | // | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9753 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9754 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
9755 | Sum = GO1; | ||||
9756 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { | ||||
9757 | Sum = SO1; | ||||
9758 | } else { | ||||
9759 | // If they aren't the same type, convert both to an integer of the | ||||
9760 | // target's pointer size. | ||||
9761 | if (SO1->getType() != GO1->getType()) { | ||||
9762 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9763 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9764 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9765 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9766 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9767 | unsigned PS = TD->getPointerSizeInBits(); |
9768 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9769 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9770 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9771 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9772 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9773 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9774 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9775 | } else { |
9776 | const Type *PT = TD->getIntPtrType(); | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9777 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
9778 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9779 | } |
9780 | } | ||||
9781 | } | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9782 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
9783 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); | ||||
9784 | else { | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9785 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); |
9786 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9787 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9788 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9789 | |
9790 | // Recycle the GEP we already have if possible. | ||||
9791 | if (SrcGEPOperands.size() == 2) { | ||||
9792 | GEP.setOperand(0, SrcGEPOperands[0]); | ||||
9793 | GEP.setOperand(1, Sum); | ||||
9794 | return &GEP; | ||||
9795 | } else { | ||||
9796 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, | ||||
9797 | SrcGEPOperands.end()-1); | ||||
9798 | Indices.push_back(Sum); | ||||
9799 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); | ||||
9800 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9801 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9802 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9803 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9804 | // 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] | 9805 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
9806 | SrcGEPOperands.end()); | ||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9807 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
9808 | } | ||||
9809 | |||||
9810 | if (!Indices.empty()) | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9811 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
9812 | Indices.end(), GEP.getName()); | ||||
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9813 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9814 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9815 | // GEP of global variable. If all of the indices for this GEP are |
9816 | // constants, we can promote this to a constexpr instead of an instruction. | ||||
9817 | |||||
9818 | // Scan for nonconstants... | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9819 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9820 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
9821 | for (; I != E && isa<Constant>(*I); ++I) | ||||
9822 | Indices.push_back(cast<Constant>(*I)); | ||||
9823 | |||||
9824 | if (I == E) { // If they are all constants... | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9825 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
9826 | &Indices[0],Indices.size()); | ||||
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9827 | |
9828 | // Replace all uses of the GEP with the new constexpr... | ||||
9829 | return ReplaceInstUsesWith(GEP, CE); | ||||
9830 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9831 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9832 | if (!isa<PointerType>(X->getType())) { |
9833 | // Not interesting. Source pointer must be a cast from pointer. | ||||
9834 | } else if (HasZeroPointerIndex) { | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9835 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
9836 | // into : GEP [10 x i8]* X, i32 0, ... | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9837 | // |
9838 | // This occurs when the program declares an array extern like "int X[];" | ||||
9839 | // | ||||
9840 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); | ||||
9841 | const PointerType *XTy = cast<PointerType>(X->getType()); | ||||
9842 | if (const ArrayType *XATy = | ||||
9843 | dyn_cast<ArrayType>(XTy->getElementType())) | ||||
9844 | if (const ArrayType *CATy = | ||||
9845 | dyn_cast<ArrayType>(CPTy->getElementType())) | ||||
9846 | if (CATy->getElementType() == XATy->getElementType()) { | ||||
9847 | // At this point, we know that the cast source type is a pointer | ||||
9848 | // to an array of the same type as the destination pointer | ||||
9849 | // array. Because the array type is never stepped over (there | ||||
9850 | // is a leading zero) we can fold the cast into this GEP. | ||||
9851 | GEP.setOperand(0, X); | ||||
9852 | return &GEP; | ||||
9853 | } | ||||
9854 | } else if (GEP.getNumOperands() == 2) { | ||||
9855 | // Transform things like: | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9856 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
9857 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9858 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
9859 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); | ||||
9860 | if (isa<ArrayType>(SrcElTy) && | ||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9861 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
9862 | TD->getABITypeSize(ResElTy)) { | ||||
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9863 | Value *Idx[2]; |
9864 | Idx[0] = Constant::getNullValue(Type::Int32Ty); | ||||
9865 | Idx[1] = GEP.getOperand(1); | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9866 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9867 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9868 | // V and GEP are both pointer types --> BitCast |
9869 | return new BitCastInst(V, GEP.getType()); | ||||
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9870 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9871 | |
9872 | // Transform things like: | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9873 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9874 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9875 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9876 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9877 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9878 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9879 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9880 | |
9881 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We | ||||
9882 | // allow either a mul, shift, or constant here. | ||||
9883 | Value *NewIdx = 0; | ||||
9884 | ConstantInt *Scale = 0; | ||||
9885 | if (ArrayEltSize == 1) { | ||||
9886 | NewIdx = GEP.getOperand(1); | ||||
9887 | Scale = ConstantInt::get(NewIdx->getType(), 1); | ||||
9888 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { | ||||
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9889 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9890 | Scale = CI; |
9891 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ | ||||
9892 | if (Inst->getOpcode() == Instruction::Shl && | ||||
9893 | isa<ConstantInt>(Inst->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9894 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
9895 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); | ||||
9896 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); | ||||
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9897 | NewIdx = Inst->getOperand(0); |
9898 | } else if (Inst->getOpcode() == Instruction::Mul && | ||||
9899 | isa<ConstantInt>(Inst->getOperand(1))) { | ||||
9900 | Scale = cast<ConstantInt>(Inst->getOperand(1)); | ||||
9901 | NewIdx = Inst->getOperand(0); | ||||
9902 | } | ||||
9903 | } | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9904 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9905 | // 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] | 9906 | // out, perform the transformation. Note, we don't know whether Scale is |
9907 | // signed or not. We'll use unsigned version of division/modulo | ||||
9908 | // operation after making sure Scale doesn't have the sign bit set. | ||||
9909 | if (Scale && Scale->getSExtValue() >= 0LL && | ||||
9910 | Scale->getZExtValue() % ArrayEltSize == 0) { | ||||
9911 | Scale = ConstantInt::get(Scale->getType(), | ||||
9912 | Scale->getZExtValue() / ArrayEltSize); | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9913 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9914 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9915 | false /*ZExt*/); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9916 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); |
9917 | NewIdx = InsertNewInstBefore(Sc, GEP); | ||||
9918 | } | ||||
9919 | |||||
9920 | // Insert the new GEP instruction. | ||||
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9921 | Value *Idx[2]; |
9922 | Idx[0] = Constant::getNullValue(Type::Int32Ty); | ||||
9923 | Idx[1] = NewIdx; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9924 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9925 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9926 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
9927 | // The NewGEP must be pointer typed, so must the old one -> BitCast | ||||
9928 | return new BitCastInst(NewGEP, GEP.getType()); | ||||
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9929 | } |
9930 | } | ||||
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9931 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9932 | } |
9933 | |||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9934 | return 0; |
9935 | } | ||||
9936 | |||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9937 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
9938 | // 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] | 9939 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9940 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
9941 | const Type *NewTy = | ||||
9942 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); | ||||
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9943 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9944 | |
9945 | // Create and insert the replacement instruction... | ||||
9946 | if (isa<MallocInst>(AI)) | ||||
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9947 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9948 | else { |
9949 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); | ||||
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 9950 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 9951 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9952 | |
9953 | InsertNewInstBefore(New, AI); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9954 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9955 | // Scan to the end of the allocation instructions, to skip over a block of |
9956 | // allocas if possible... | ||||
9957 | // | ||||
9958 | BasicBlock::iterator It = New; | ||||
9959 | while (isa<AllocationInst>(*It)) ++It; | ||||
9960 | |||||
9961 | // Now that I is pointing to the first non-allocation-inst in the block, | ||||
9962 | // insert our getelementptr instruction... | ||||
9963 | // | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9964 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9965 | Value *Idx[2]; |
9966 | Idx[0] = NullIdx; | ||||
9967 | Idx[1] = NullIdx; | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9968 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
9969 | New->getName()+".sub", It); | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9970 | |
9971 | // Now make everything use the getelementptr instead of the original | ||||
9972 | // allocation. | ||||
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9973 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9974 | } else if (isa<UndefValue>(AI.getArraySize())) { |
9975 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9976 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9977 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9978 | |
9979 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. | ||||
9980 | // Note that we only do this for alloca's, because malloc should allocate and | ||||
9981 | // return a unique pointer, even for a zero byte allocation. | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9982 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9983 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 9984 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
9985 | |||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 9986 | return 0; |
9987 | } | ||||
9988 | |||||
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 9989 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
9990 | Value *Op = FI.getOperand(0); | ||||
9991 | |||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9992 | // free undef -> unreachable. |
9993 | if (isa<UndefValue>(Op)) { | ||||
9994 | // 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] | 9995 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9996 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 9997 | return EraseInstFromFunction(FI); |
9998 | } | ||||
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 9999 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 10000 | // If we have 'free null' delete the instruction. This can happen in stl code |
10001 | // when lots of inlining happens. | ||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10002 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10003 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 10004 | |
10005 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X | ||||
10006 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { | ||||
10007 | FI.setOperand(0, CI->getOperand(0)); | ||||
10008 | return &FI; | ||||
10009 | } | ||||
10010 | |||||
10011 | // Change free (gep X, 0,0,0,0) into free(X) | ||||
10012 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { | ||||
10013 | if (GEPI->hasAllZeroIndices()) { | ||||
10014 | AddToWorkList(GEPI); | ||||
10015 | FI.setOperand(0, GEPI->getOperand(0)); | ||||
10016 | return &FI; | ||||
10017 | } | ||||
10018 | } | ||||
10019 | |||||
10020 | // Change free(malloc) into nothing, if the malloc has a single use. | ||||
10021 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) | ||||
10022 | if (MI->hasOneUse()) { | ||||
10023 | EraseInstFromFunction(FI); | ||||
10024 | return EraseInstFromFunction(*MI); | ||||
10025 | } | ||||
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 10026 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 10027 | return 0; |
10028 | } | ||||
10029 | |||||
10030 | |||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10031 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10032 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 10033 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10034 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10035 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10036 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10037 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
10038 | // Instead of loading constant c string, use corresponding integer value | ||||
10039 | // directly if string length is small enough. | ||||
10040 | const std::string &Str = CE->getOperand(0)->getStringValue(); | ||||
10041 | if (!Str.empty()) { | ||||
10042 | unsigned len = Str.length(); | ||||
10043 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); | ||||
10044 | unsigned numBits = Ty->getPrimitiveSizeInBits(); | ||||
10045 | // Replace LI with immediate integer store. | ||||
10046 | if ((numBits >> 3) == len + 1) { | ||||
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 10047 | APInt StrVal(numBits, 0); |
10048 | APInt SingleChar(numBits, 0); | ||||
10049 | if (TD->isLittleEndian()) { | ||||
10050 | for (signed i = len-1; i >= 0; i--) { | ||||
10051 | SingleChar = (uint64_t) Str[i]; | ||||
10052 | StrVal = (StrVal << 8) | SingleChar; | ||||
10053 | } | ||||
10054 | } else { | ||||
10055 | for (unsigned i = 0; i < len; i++) { | ||||
10056 | SingleChar = (uint64_t) Str[i]; | ||||
10057 | StrVal = (StrVal << 8) | SingleChar; | ||||
10058 | } | ||||
10059 | // Append NULL at the end. | ||||
10060 | SingleChar = 0; | ||||
10061 | StrVal = (StrVal << 8) | SingleChar; | ||||
10062 | } | ||||
10063 | Value *NL = ConstantInt::get(StrVal); | ||||
10064 | return IC.ReplaceInstUsesWith(LI, NL); | ||||
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10065 | } |
10066 | } | ||||
10067 | } | ||||
10068 | |||||
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10069 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10070 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10071 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10072 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10073 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10074 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10075 | // If the source is an array, the code below will not succeed. Check to |
10076 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for | ||||
10077 | // constants. | ||||
10078 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) | ||||
10079 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) | ||||
10080 | if (ASrcTy->getNumElements() != 0) { | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10081 | Value *Idxs[2]; |
10082 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); | ||||
10083 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); | ||||
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10084 | SrcTy = cast<PointerType>(CastOp->getType()); |
10085 | SrcPTy = SrcTy->getElementType(); | ||||
10086 | } | ||||
10087 | |||||
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10088 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10089 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 10090 | // Do not allow turning this into a load of an integer, which is then |
10091 | // casted to a pointer, this pessimizes pointer analysis a lot. | ||||
10092 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && | ||||
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10093 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
10094 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10095 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10096 | // Okay, we are casting from one integer or pointer type to another of |
10097 | // the same size. Instead of casting the pointer before the load, cast | ||||
10098 | // the result of the loaded value. | ||||
10099 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, | ||||
10100 | CI->getName(), | ||||
10101 | LI.isVolatile()),LI); | ||||
10102 | // Now cast the result of the load. | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10103 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10104 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10105 | } |
10106 | } | ||||
10107 | return 0; | ||||
10108 | } | ||||
10109 | |||||
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10110 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10111 | /// from this value cannot trap. If it is not obviously safe to load from the |
10112 | /// specified pointer, we do a quick local scan of the basic block containing | ||||
10113 | /// ScanFrom, to determine if the address is already accessed. | ||||
10114 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { | ||||
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10115 | // If it is an alloca it is always safe to load from. |
10116 | if (isa<AllocaInst>(V)) return true; | ||||
10117 | |||||
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10118 | // 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] | 10119 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10120 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10121 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10122 | |
10123 | // Otherwise, be a little bit agressive by scanning the local block where we | ||||
10124 | // 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] | 10125 | // from/to. If so, the previous load or store would have already trapped, |
10126 | // so there is no harm doing an extra load (also, CSE will later eliminate | ||||
10127 | // the load entirely). | ||||
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10128 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
10129 | |||||
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10130 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10131 | --BBI; |
10132 | |||||
10133 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { | ||||
10134 | if (LI->getOperand(0) == V) return true; | ||||
10135 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) | ||||
10136 | if (SI->getOperand(1) == V) return true; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10137 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10138 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10139 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10140 | } |
10141 | |||||
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10142 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
10143 | /// until we find the underlying object a pointer is referring to or something | ||||
10144 | /// we don't understand. Note that the returned pointer may be offset from the | ||||
10145 | /// input, because we ignore GEP indices. | ||||
10146 | static Value *GetUnderlyingObject(Value *Ptr) { | ||||
10147 | while (1) { | ||||
10148 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { | ||||
10149 | if (CE->getOpcode() == Instruction::BitCast || | ||||
10150 | CE->getOpcode() == Instruction::GetElementPtr) | ||||
10151 | Ptr = CE->getOperand(0); | ||||
10152 | else | ||||
10153 | return Ptr; | ||||
10154 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { | ||||
10155 | Ptr = BCI->getOperand(0); | ||||
10156 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { | ||||
10157 | Ptr = GEP->getOperand(0); | ||||
10158 | } else { | ||||
10159 | return Ptr; | ||||
10160 | } | ||||
10161 | } | ||||
10162 | } | ||||
10163 | |||||
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10164 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
10165 | Value *Op = LI.getOperand(0); | ||||
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 10166 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10167 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10168 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op); |
10169 | if (KnownAlign > | ||||
10170 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : | ||||
10171 | LI.getAlignment())) | ||||
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10172 | LI.setAlignment(KnownAlign); |
10173 | |||||
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10174 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10175 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10176 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10177 | return Res; |
10178 | |||||
10179 | // None of the following transforms are legal for volatile loads. | ||||
10180 | if (LI.isVolatile()) return 0; | ||||
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10181 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10182 | if (&LI.getParent()->front() != &LI) { |
10183 | BasicBlock::iterator BBI = &LI; --BBI; | ||||
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10184 | // If the instruction immediately before this is a store to the same |
10185 | // address, do a simple form of store->load forwarding. | ||||
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10186 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
10187 | if (SI->getOperand(1) == LI.getOperand(0)) | ||||
10188 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); | ||||
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10189 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
10190 | if (LIB->getOperand(0) == LI.getOperand(0)) | ||||
10191 | return ReplaceInstUsesWith(LI, LIB); | ||||
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10192 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10193 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10194 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
10195 | const Value *GEPI0 = GEPI->getOperand(0); | ||||
10196 | // TODO: Consider a target hook for valid address spaces for this xform. | ||||
10197 | if (isa<ConstantPointerNull>(GEPI0) && | ||||
10198 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { | ||||
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10199 | // Insert a new store to null instruction before the load to indicate |
10200 | // that this code is not reachable. We do this instead of inserting | ||||
10201 | // an unreachable instruction directly because we cannot modify the | ||||
10202 | // CFG. | ||||
10203 | new StoreInst(UndefValue::get(LI.getType()), | ||||
10204 | Constant::getNullValue(Op->getType()), &LI); | ||||
10205 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | ||||
10206 | } | ||||
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10207 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10208 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10209 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10210 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10211 | // TODO: Consider a target hook for valid address spaces for this xform. |
10212 | if (isa<UndefValue>(C) || (C->isNullValue() && | ||||
10213 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { | ||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10214 | // Insert a new store to null instruction before the load to indicate that |
10215 | // this code is not reachable. We do this instead of inserting an | ||||
10216 | // unreachable instruction directly because we cannot modify the CFG. | ||||
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10217 | new StoreInst(UndefValue::get(LI.getType()), |
10218 | Constant::getNullValue(Op->getType()), &LI); | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10219 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10220 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10221 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10222 | // Instcombine load (constant global) into the value loaded. |
10223 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10224 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10225 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10226 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10227 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10228 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10229 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
10230 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10231 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 10232 | if (Constant *V = |
10233 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10234 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10235 | if (CE->getOperand(0)->isNullValue()) { |
10236 | // Insert a new store to null instruction before the load to indicate | ||||
10237 | // that this code is not reachable. We do this instead of inserting | ||||
10238 | // an unreachable instruction directly because we cannot modify the | ||||
10239 | // CFG. | ||||
10240 | new StoreInst(UndefValue::get(LI.getType()), | ||||
10241 | Constant::getNullValue(Op->getType()), &LI); | ||||
10242 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | ||||
10243 | } | ||||
10244 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10245 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10246 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10247 | return Res; |
10248 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10249 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10250 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10251 | |
10252 | // If this load comes from anywhere in a constant global, and if the global | ||||
10253 | // is all undef or zero, we know what it loads. | ||||
10254 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { | ||||
10255 | if (GV->isConstant() && GV->hasInitializer()) { | ||||
10256 | if (GV->getInitializer()->isNullValue()) | ||||
10257 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); | ||||
10258 | else if (isa<UndefValue>(GV->getInitializer())) | ||||
10259 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | ||||
10260 | } | ||||
10261 | } | ||||
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 10262 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10263 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10264 | // Change select and PHI nodes to select values instead of addresses: this |
10265 | // helps alias analysis out a lot, allows many others simplifications, and | ||||
10266 | // exposes redundancy in the code. | ||||
10267 | // | ||||
10268 | // Note that we cannot do the transformation unless we know that the | ||||
10269 | // introduced loads cannot trap! Something like this is valid as long as | ||||
10270 | // the condition is always false: load (select bool %C, int* null, int* %G), | ||||
10271 | // but it would not be valid if we transformed it to load from null | ||||
10272 | // unconditionally. | ||||
10273 | // | ||||
10274 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { | ||||
10275 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). | ||||
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10276 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
10277 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { | ||||
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10278 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10279 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10280 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10281 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10282 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10283 | } |
10284 | |||||
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 10285 | // load (select (cond, null, P)) -> load P |
10286 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) | ||||
10287 | if (C->isNullValue()) { | ||||
10288 | LI.setOperand(0, SI->getOperand(2)); | ||||
10289 | return &LI; | ||||
10290 | } | ||||
10291 | |||||
10292 | // load (select (cond, P, null)) -> load P | ||||
10293 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) | ||||
10294 | if (C->isNullValue()) { | ||||
10295 | LI.setOperand(0, SI->getOperand(1)); | ||||
10296 | return &LI; | ||||
10297 | } | ||||
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10298 | } |
10299 | } | ||||
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10300 | return 0; |
10301 | } | ||||
10302 | |||||
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 10303 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10304 | /// when possible. |
10305 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { | ||||
10306 | User *CI = cast<User>(SI.getOperand(1)); | ||||
10307 | Value *CastOp = CI->getOperand(0); | ||||
10308 | |||||
10309 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); | ||||
10310 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { | ||||
10311 | const Type *SrcPTy = SrcTy->getElementType(); | ||||
10312 | |||||
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10313 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10314 | // If the source is an array, the code below will not succeed. Check to |
10315 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for | ||||
10316 | // constants. | ||||
10317 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) | ||||
10318 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) | ||||
10319 | if (ASrcTy->getNumElements() != 0) { | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10320 | Value* Idxs[2]; |
10321 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); | ||||
10322 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10323 | SrcTy = cast<PointerType>(CastOp->getType()); |
10324 | SrcPTy = SrcTy->getElementType(); | ||||
10325 | } | ||||
10326 | |||||
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10327 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
10328 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == | ||||
10329 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10330 | |
10331 | // 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] | 10332 | // the same size. Instead of casting the pointer before |
10333 | // the store, cast the value to be stored. | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10334 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10335 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10336 | Instruction::CastOps opcode = Instruction::BitCast; |
10337 | const Type* CastSrcTy = SIOp0->getType(); | ||||
10338 | const Type* CastDstTy = SrcPTy; | ||||
10339 | if (isa<PointerType>(CastDstTy)) { | ||||
10340 | if (CastSrcTy->isInteger()) | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10341 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10342 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 10343 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10344 | opcode = Instruction::PtrToInt; |
10345 | } | ||||
10346 | if (Constant *C = dyn_cast<Constant>(SIOp0)) | ||||
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10347 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10348 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10349 | NewCast = IC.InsertNewInstBefore( |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10350 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
10351 | SI); | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10352 | return new StoreInst(NewCast, CastOp); |
10353 | } | ||||
10354 | } | ||||
10355 | } | ||||
10356 | return 0; | ||||
10357 | } | ||||
10358 | |||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10359 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
10360 | Value *Val = SI.getOperand(0); | ||||
10361 | Value *Ptr = SI.getOperand(1); | ||||
10362 | |||||
10363 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) | ||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10364 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10365 | ++NumCombined; |
10366 | return 0; | ||||
10367 | } | ||||
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10368 | |
10369 | // If the RHS is an alloca with a single use, zapify the store, making the | ||||
10370 | // alloca dead. | ||||
Chris Lattner | cea1fdd | 2008-04-29 04:58:38 +0000 | [diff] [blame] | 10371 | if (Ptr->hasOneUse() && !SI.isVolatile()) { |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10372 | if (isa<AllocaInst>(Ptr)) { |
10373 | EraseInstFromFunction(SI); | ||||
10374 | ++NumCombined; | ||||
10375 | return 0; | ||||
10376 | } | ||||
10377 | |||||
10378 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) | ||||
10379 | if (isa<AllocaInst>(GEP->getOperand(0)) && | ||||
10380 | GEP->getOperand(0)->hasOneUse()) { | ||||
10381 | EraseInstFromFunction(SI); | ||||
10382 | ++NumCombined; | ||||
10383 | return 0; | ||||
10384 | } | ||||
10385 | } | ||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10386 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10387 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10388 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr); |
10389 | if (KnownAlign > | ||||
10390 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : | ||||
10391 | SI.getAlignment())) | ||||
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10392 | SI.setAlignment(KnownAlign); |
10393 | |||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10394 | // Do really simple DSE, to catch cases where there are several consequtive |
10395 | // stores to the same location, separated by a few arithmetic operations. This | ||||
10396 | // situation often occurs with bitfield accesses. | ||||
10397 | BasicBlock::iterator BBI = &SI; | ||||
10398 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; | ||||
10399 | --ScanInsts) { | ||||
10400 | --BBI; | ||||
10401 | |||||
10402 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { | ||||
10403 | // Prev store isn't volatile, and stores to the same location? | ||||
10404 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { | ||||
10405 | ++NumDeadStore; | ||||
10406 | ++BBI; | ||||
10407 | EraseInstFromFunction(*PrevSI); | ||||
10408 | continue; | ||||
10409 | } | ||||
10410 | break; | ||||
10411 | } | ||||
10412 | |||||
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10413 | // If this is a load, we have to stop. However, if the loaded value is from |
10414 | // the pointer we're loading and is producing the pointer we're storing, | ||||
10415 | // then *this* store is dead (X = load P; store X -> P). | ||||
10416 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { | ||||
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 10417 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10418 | EraseInstFromFunction(SI); |
10419 | ++NumCombined; | ||||
10420 | return 0; | ||||
10421 | } | ||||
10422 | // Otherwise, this is a load from some other location. Stores before it | ||||
10423 | // may not be dead. | ||||
10424 | break; | ||||
10425 | } | ||||
10426 | |||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10427 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10428 | if (BBI->mayWriteToMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10429 | break; |
10430 | } | ||||
10431 | |||||
10432 | |||||
10433 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. | ||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10434 | |
10435 | // store X, null -> turns into 'unreachable' in SimplifyCFG | ||||
10436 | if (isa<ConstantPointerNull>(Ptr)) { | ||||
10437 | if (!isa<UndefValue>(Val)) { | ||||
10438 | SI.setOperand(0, UndefValue::get(Val->getType())); | ||||
10439 | if (Instruction *U = dyn_cast<Instruction>(Val)) | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10440 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10441 | ++NumCombined; |
10442 | } | ||||
10443 | return 0; // Do not modify these! | ||||
10444 | } | ||||
10445 | |||||
10446 | // store undef, Ptr -> noop | ||||
10447 | if (isa<UndefValue>(Val)) { | ||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10448 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10449 | ++NumCombined; |
10450 | return 0; | ||||
10451 | } | ||||
10452 | |||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10453 | // If the pointer destination is a cast, see if we can fold the cast into the |
10454 | // source instead. | ||||
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10455 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10456 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
10457 | return Res; | ||||
10458 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10459 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10460 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
10461 | return Res; | ||||
10462 | |||||
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10463 | |
10464 | // If this store is the last instruction in the basic block, and if the block | ||||
10465 | // 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] | 10466 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10467 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10468 | if (BI->isUnconditional()) |
10469 | if (SimplifyStoreAtEndOfBlock(SI)) | ||||
10470 | return 0; // xform done! | ||||
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10471 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10472 | return 0; |
10473 | } | ||||
10474 | |||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10475 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
10476 | /// if () { *P = v1; } else { *P = v2 } | ||||
10477 | /// into a phi node with a store in the successor. | ||||
10478 | /// | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10479 | /// Simplify things like: |
10480 | /// *P = v1; if () { *P = v2; } | ||||
10481 | /// into a phi node with a store in the successor. | ||||
10482 | /// | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10483 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
10484 | BasicBlock *StoreBB = SI.getParent(); | ||||
10485 | |||||
10486 | // Check to see if the successor block has exactly two incoming edges. If | ||||
10487 | // so, see if the other predecessor contains a store to the same location. | ||||
10488 | // 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] | 10489 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10490 | |
10491 | // Determine whether Dest has exactly two predecessors and, if so, compute | ||||
10492 | // the other predecessor. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10493 | pred_iterator PI = pred_begin(DestBB); |
10494 | BasicBlock *OtherBB = 0; | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10495 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10496 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10497 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10498 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10499 | return false; |
10500 | |||||
10501 | if (*PI != StoreBB) { | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10502 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10503 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10504 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10505 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10506 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10507 | return false; |
10508 | |||||
10509 | |||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10510 | // Verify that the other block ends in a branch and is not otherwise empty. |
10511 | BasicBlock::iterator BBI = OtherBB->getTerminator(); | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10512 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10513 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10514 | return false; |
10515 | |||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10516 | // If the other block ends in an unconditional branch, check for the 'if then |
10517 | // else' case. there is an instruction before the branch. | ||||
10518 | StoreInst *OtherStore = 0; | ||||
10519 | if (OtherBr->isUnconditional()) { | ||||
10520 | // If this isn't a store, or isn't a store to the same location, bail out. | ||||
10521 | --BBI; | ||||
10522 | OtherStore = dyn_cast<StoreInst>(BBI); | ||||
10523 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) | ||||
10524 | return false; | ||||
10525 | } else { | ||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10526 | // 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] | 10527 | // destinations is StoreBB, then we have the if/then case. |
10528 | if (OtherBr->getSuccessor(0) != StoreBB && | ||||
10529 | OtherBr->getSuccessor(1) != StoreBB) | ||||
10530 | return false; | ||||
10531 | |||||
10532 | // 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] | 10533 | // if/then triangle. See if there is a store to the same ptr as SI that |
10534 | // lives in OtherBB. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10535 | for (;; --BBI) { |
10536 | // Check to see if we find the matching store. | ||||
10537 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { | ||||
10538 | if (OtherStore->getOperand(1) != SI.getOperand(1)) | ||||
10539 | return false; | ||||
10540 | break; | ||||
10541 | } | ||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10542 | // If we find something that may be using the stored value, or if we run |
10543 | // out of instructions, we can't do the xform. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10544 | if (isa<LoadInst>(BBI) || BBI->mayWriteToMemory() || |
10545 | BBI == OtherBB->begin()) | ||||
10546 | return false; | ||||
10547 | } | ||||
10548 | |||||
10549 | // In order to eliminate the store in OtherBr, we have to | ||||
10550 | // make sure nothing reads the stored value in StoreBB. | ||||
10551 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { | ||||
10552 | // FIXME: This should really be AA driven. | ||||
10553 | if (isa<LoadInst>(I) || I->mayWriteToMemory()) | ||||
10554 | return false; | ||||
10555 | } | ||||
10556 | } | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10557 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10558 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10559 | Value *MergedVal = OtherStore->getOperand(0); |
10560 | if (MergedVal != SI.getOperand(0)) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10561 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10562 | PN->reserveOperandSpace(2); |
10563 | PN->addIncoming(SI.getOperand(0), SI.getParent()); | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10564 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
10565 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10566 | } |
10567 | |||||
10568 | // Advance to a place where it is safe to insert the new store and | ||||
10569 | // insert it. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10570 | BBI = DestBB->begin(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10571 | while (isa<PHINode>(BBI)) ++BBI; |
10572 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), | ||||
10573 | OtherStore->isVolatile()), *BBI); | ||||
10574 | |||||
10575 | // Nuke the old stores. | ||||
10576 | EraseInstFromFunction(SI); | ||||
10577 | EraseInstFromFunction(*OtherStore); | ||||
10578 | ++NumCombined; | ||||
10579 | return true; | ||||
10580 | } | ||||
10581 | |||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10582 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10583 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
10584 | // 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] | 10585 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10586 | BasicBlock *TrueDest; |
10587 | BasicBlock *FalseDest; | ||||
10588 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && | ||||
10589 | !isa<Constant>(X)) { | ||||
10590 | // Swap Destinations and condition... | ||||
10591 | BI.setCondition(X); | ||||
10592 | BI.setSuccessor(0, FalseDest); | ||||
10593 | BI.setSuccessor(1, TrueDest); | ||||
10594 | return &BI; | ||||
10595 | } | ||||
10596 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10597 | // Cannonicalize fcmp_one -> fcmp_oeq |
10598 | FCmpInst::Predicate FPred; Value *Y; | ||||
10599 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), | ||||
10600 | TrueDest, FalseDest))) | ||||
10601 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || | ||||
10602 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { | ||||
10603 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10604 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10605 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
10606 | NewSCC->takeName(I); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10607 | // Swap Destinations and condition... |
10608 | BI.setCondition(NewSCC); | ||||
10609 | BI.setSuccessor(0, FalseDest); | ||||
10610 | BI.setSuccessor(1, TrueDest); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10611 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10612 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10613 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10614 | return &BI; |
10615 | } | ||||
10616 | |||||
10617 | // Cannonicalize icmp_ne -> icmp_eq | ||||
10618 | ICmpInst::Predicate IPred; | ||||
10619 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), | ||||
10620 | TrueDest, FalseDest))) | ||||
10621 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || | ||||
10622 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || | ||||
10623 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { | ||||
10624 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10625 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10626 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
10627 | NewSCC->takeName(I); | ||||
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10628 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10629 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10630 | BI.setSuccessor(0, FalseDest); |
10631 | BI.setSuccessor(1, TrueDest); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10632 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10633 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10634 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10635 | return &BI; |
10636 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10637 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10638 | return 0; |
10639 | } | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10640 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10641 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
10642 | Value *Cond = SI.getCondition(); | ||||
10643 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { | ||||
10644 | if (I->getOpcode() == Instruction::Add) | ||||
10645 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
10646 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' | ||||
10647 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10648 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10649 | AddRHS)); |
10650 | SI.setOperand(0, I->getOperand(0)); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10651 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10652 | return &SI; |
10653 | } | ||||
10654 | } | ||||
10655 | return 0; | ||||
10656 | } | ||||
10657 | |||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10658 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
10659 | /// is to leave as a vector operation. | ||||
10660 | static bool CheapToScalarize(Value *V, bool isConstant) { | ||||
10661 | if (isa<ConstantAggregateZero>(V)) | ||||
10662 | return true; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10663 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10664 | if (isConstant) return true; |
10665 | // If all elts are the same, we can extract. | ||||
10666 | Constant *Op0 = C->getOperand(0); | ||||
10667 | for (unsigned i = 1; i < C->getNumOperands(); ++i) | ||||
10668 | if (C->getOperand(i) != Op0) | ||||
10669 | return false; | ||||
10670 | return true; | ||||
10671 | } | ||||
10672 | Instruction *I = dyn_cast<Instruction>(V); | ||||
10673 | if (!I) return false; | ||||
10674 | |||||
10675 | // Insert element gets simplified to the inserted element or is deleted if | ||||
10676 | // this is constant idx extract element and its a constant idx insertelt. | ||||
10677 | if (I->getOpcode() == Instruction::InsertElement && isConstant && | ||||
10678 | isa<ConstantInt>(I->getOperand(2))) | ||||
10679 | return true; | ||||
10680 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) | ||||
10681 | return true; | ||||
10682 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) | ||||
10683 | if (BO->hasOneUse() && | ||||
10684 | (CheapToScalarize(BO->getOperand(0), isConstant) || | ||||
10685 | CheapToScalarize(BO->getOperand(1), isConstant))) | ||||
10686 | return true; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10687 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
10688 | if (CI->hasOneUse() && | ||||
10689 | (CheapToScalarize(CI->getOperand(0), isConstant) || | ||||
10690 | CheapToScalarize(CI->getOperand(1), isConstant))) | ||||
10691 | return true; | ||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10692 | |
10693 | return false; | ||||
10694 | } | ||||
10695 | |||||
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10696 | /// Read and decode a shufflevector mask. |
10697 | /// | ||||
10698 | /// It turns undef elements into values that are larger than the number of | ||||
10699 | /// elements in the input. | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10700 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
10701 | unsigned NElts = SVI->getType()->getNumElements(); | ||||
10702 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) | ||||
10703 | return std::vector<unsigned>(NElts, 0); | ||||
10704 | if (isa<UndefValue>(SVI->getOperand(2))) | ||||
10705 | return std::vector<unsigned>(NElts, 2*NElts); | ||||
10706 | |||||
10707 | std::vector<unsigned> Result; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10708 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10709 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
10710 | if (isa<UndefValue>(CP->getOperand(i))) | ||||
10711 | Result.push_back(NElts*2); // undef -> 8 | ||||
10712 | else | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10713 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10714 | return Result; |
10715 | } | ||||
10716 | |||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10717 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
10718 | /// value is already around as a register, for example if it were inserted then | ||||
10719 | /// extracted from the vector. | ||||
10720 | static Value *FindScalarElement(Value *V, unsigned EltNo) { | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10721 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
10722 | const VectorType *PTy = cast<VectorType>(V->getType()); | ||||
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10723 | unsigned Width = PTy->getNumElements(); |
10724 | if (EltNo >= Width) // Out of range access. | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10725 | return UndefValue::get(PTy->getElementType()); |
10726 | |||||
10727 | if (isa<UndefValue>(V)) | ||||
10728 | return UndefValue::get(PTy->getElementType()); | ||||
10729 | else if (isa<ConstantAggregateZero>(V)) | ||||
10730 | return Constant::getNullValue(PTy->getElementType()); | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10731 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10732 | return CP->getOperand(EltNo); |
10733 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { | ||||
10734 | // 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] | 10735 | if (!isa<ConstantInt>(III->getOperand(2))) |
10736 | return 0; | ||||
10737 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10738 | |
10739 | // If this is an insert to the element we are looking for, return the | ||||
10740 | // inserted value. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10741 | if (EltNo == IIElt) |
10742 | return III->getOperand(1); | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10743 | |
10744 | // Otherwise, the insertelement doesn't modify the value, recurse on its | ||||
10745 | // vector input. | ||||
10746 | return FindScalarElement(III->getOperand(0), EltNo); | ||||
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10747 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10748 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
10749 | if (InEl < Width) | ||||
10750 | return FindScalarElement(SVI->getOperand(0), InEl); | ||||
10751 | else if (InEl < Width*2) | ||||
10752 | return FindScalarElement(SVI->getOperand(1), InEl - Width); | ||||
10753 | else | ||||
10754 | return UndefValue::get(PTy->getElementType()); | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10755 | } |
10756 | |||||
10757 | // Otherwise, we don't know. | ||||
10758 | return 0; | ||||
10759 | } | ||||
10760 | |||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10761 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10762 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10763 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10764 | if (isa<UndefValue>(EI.getOperand(0))) |
10765 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | ||||
10766 | |||||
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10767 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10768 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
10769 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); | ||||
10770 | |||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10771 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10772 | // If vector val is constant with uniform operands, replace EI |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10773 | // with that operand |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10774 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10775 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10776 | if (C->getOperand(i) != op0) { |
10777 | op0 = 0; | ||||
10778 | break; | ||||
10779 | } | ||||
10780 | if (op0) | ||||
10781 | return ReplaceInstUsesWith(EI, op0); | ||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10782 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10783 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10784 | // If extracting a specified index from the vector, see if we can recursively |
10785 | // find a previously computed scalar that was inserted into the vector. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10786 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10787 | unsigned IndexVal = IdxC->getZExtValue(); |
10788 | unsigned VectorWidth = | ||||
10789 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); | ||||
10790 | |||||
10791 | // If this is extracting an invalid index, turn this into undef, to avoid | ||||
10792 | // crashing the code below. | ||||
10793 | if (IndexVal >= VectorWidth) | ||||
10794 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | ||||
10795 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10796 | // This instruction only demands the single element from the input vector. |
10797 | // If the input vector has a single use, simplify it based on this use | ||||
10798 | // property. | ||||
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10799 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10800 | uint64_t UndefElts; |
10801 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10802 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10803 | UndefElts)) { |
10804 | EI.setOperand(0, V); | ||||
10805 | return &EI; | ||||
10806 | } | ||||
10807 | } | ||||
10808 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10809 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10810 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 10811 | |
10812 | // If the this extractelement is directly using a bitcast from a vector of | ||||
10813 | // the same number of elements, see if we can find the source element from | ||||
10814 | // it. In this case, we will end up needing to bitcast the scalars. | ||||
10815 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { | ||||
10816 | if (const VectorType *VT = | ||||
10817 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) | ||||
10818 | if (VT->getNumElements() == VectorWidth) | ||||
10819 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) | ||||
10820 | return new BitCastInst(Elt, EI.getType()); | ||||
10821 | } | ||||
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10822 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10823 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10824 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10825 | if (I->hasOneUse()) { |
10826 | // Push extractelement into predecessor operation if legal and | ||||
10827 | // profitable to do so | ||||
10828 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { | ||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10829 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
10830 | if (CheapToScalarize(BO, isConstantElt)) { | ||||
10831 | ExtractElementInst *newEI0 = | ||||
10832 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), | ||||
10833 | EI.getName()+".lhs"); | ||||
10834 | ExtractElementInst *newEI1 = | ||||
10835 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), | ||||
10836 | EI.getName()+".rhs"); | ||||
10837 | InsertNewInstBefore(newEI0, EI); | ||||
10838 | InsertNewInstBefore(newEI1, EI); | ||||
10839 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); | ||||
10840 | } | ||||
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10841 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10842 | unsigned AS = |
10843 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 10844 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
10845 | PointerType::get(EI.getType(), AS),EI); | ||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10846 | GetElementPtrInst *GEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10847 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName() + ".gep"); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10848 | InsertNewInstBefore(GEP, EI); |
10849 | return new LoadInst(GEP); | ||||
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10850 | } |
10851 | } | ||||
10852 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { | ||||
10853 | // Extracting the inserted element? | ||||
10854 | if (IE->getOperand(2) == EI.getOperand(1)) | ||||
10855 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); | ||||
10856 | // If the inserted and extracted elements are constants, they must not | ||||
10857 | // be the same value, extract from the pre-inserted value instead. | ||||
10858 | if (isa<Constant>(IE->getOperand(2)) && | ||||
10859 | isa<Constant>(EI.getOperand(1))) { | ||||
10860 | AddUsesToWorkList(EI); | ||||
10861 | EI.setOperand(0, IE->getOperand(0)); | ||||
10862 | return &EI; | ||||
10863 | } | ||||
10864 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { | ||||
10865 | // If this is extracting an element from a shufflevector, figure out where | ||||
10866 | // it came from and extract from the appropriate input element instead. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10867 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
10868 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10869 | Value *Src; |
10870 | if (SrcIdx < SVI->getType()->getNumElements()) | ||||
10871 | Src = SVI->getOperand(0); | ||||
10872 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { | ||||
10873 | SrcIdx -= SVI->getType()->getNumElements(); | ||||
10874 | Src = SVI->getOperand(1); | ||||
10875 | } else { | ||||
10876 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | ||||
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 10877 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10878 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10879 | } |
10880 | } | ||||
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 10881 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10882 | return 0; |
10883 | } | ||||
10884 | |||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10885 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
10886 | /// elements from either LHS or RHS, return the shuffle mask and true. | ||||
10887 | /// Otherwise, return false. | ||||
10888 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, | ||||
10889 | std::vector<Constant*> &Mask) { | ||||
10890 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && | ||||
10891 | "Invalid CollectSingleShuffleElements"); | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10892 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10893 | |
10894 | if (isa<UndefValue>(V)) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10895 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10896 | return true; |
10897 | } else if (V == LHS) { | ||||
10898 | for (unsigned i = 0; i != NumElts; ++i) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10899 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10900 | return true; |
10901 | } else if (V == RHS) { | ||||
10902 | for (unsigned i = 0; i != NumElts; ++i) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10903 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10904 | return true; |
10905 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { | ||||
10906 | // If this is an insert of an extract from some other vector, include it. | ||||
10907 | Value *VecOp = IEI->getOperand(0); | ||||
10908 | Value *ScalarOp = IEI->getOperand(1); | ||||
10909 | Value *IdxOp = IEI->getOperand(2); | ||||
10910 | |||||
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10911 | if (!isa<ConstantInt>(IdxOp)) |
10912 | return false; | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10913 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10914 | |
10915 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. | ||||
10916 | // Okay, we can handle this if the vector we are insertinting into is | ||||
10917 | // transitively ok. | ||||
10918 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { | ||||
10919 | // If so, update the mask to reflect the inserted undef. | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10920 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 10921 | return true; |
10922 | } | ||||
10923 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ | ||||
10924 | if (isa<ConstantInt>(EI->getOperand(1)) && | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10925 | EI->getOperand(0)->getType() == V->getType()) { |
10926 | unsigned ExtractedIdx = | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10927 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10928 | |
10929 | // This must be extracting from either LHS or RHS. | ||||
10930 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { | ||||
10931 | // Okay, we can handle this if the vector we are insertinting into is | ||||
10932 | // transitively ok. | ||||
10933 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { | ||||
10934 | // If so, update the mask to reflect the inserted value. | ||||
10935 | if (EI->getOperand(0) == LHS) { | ||||
10936 | Mask[InsertedIdx & (NumElts-1)] = | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10937 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10938 | } else { |
10939 | assert(EI->getOperand(0) == RHS); | ||||
10940 | Mask[InsertedIdx & (NumElts-1)] = | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10941 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10942 | |
10943 | } | ||||
10944 | return true; | ||||
10945 | } | ||||
10946 | } | ||||
10947 | } | ||||
10948 | } | ||||
10949 | } | ||||
10950 | // TODO: Handle shufflevector here! | ||||
10951 | |||||
10952 | return false; | ||||
10953 | } | ||||
10954 | |||||
10955 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the | ||||
10956 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask | ||||
10957 | /// that computes V and the LHS value of the shuffle. | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10958 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10959 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10960 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10961 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10962 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10963 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10964 | |
10965 | if (isa<UndefValue>(V)) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10966 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10967 | return V; |
10968 | } else if (isa<ConstantAggregateZero>(V)) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10969 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10970 | return V; |
10971 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { | ||||
10972 | // If this is an insert of an extract from some other vector, include it. | ||||
10973 | Value *VecOp = IEI->getOperand(0); | ||||
10974 | Value *ScalarOp = IEI->getOperand(1); | ||||
10975 | Value *IdxOp = IEI->getOperand(2); | ||||
10976 | |||||
10977 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { | ||||
10978 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && | ||||
10979 | EI->getOperand(0)->getType() == V->getType()) { | ||||
10980 | unsigned ExtractedIdx = | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10981 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
10982 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10983 | |
10984 | // Either the extracted from or inserted into vector must be RHSVec, | ||||
10985 | // otherwise we'd end up with a shuffle of three inputs. | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10986 | if (EI->getOperand(0) == RHS || RHS == 0) { |
10987 | RHS = EI->getOperand(0); | ||||
10988 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10989 | Mask[InsertedIdx & (NumElts-1)] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10990 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10991 | return V; |
10992 | } | ||||
10993 | |||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 10994 | if (VecOp == RHS) { |
10995 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 10996 | // Everything but the extracted element is replaced with the RHS. |
10997 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
10998 | if (i != InsertedIdx) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10999 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11000 | } |
11001 | return V; | ||||
11002 | } | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11003 | |
11004 | // If this insertelement is a chain that comes from exactly these two | ||||
11005 | // vectors, return the vector and the effective shuffle. | ||||
11006 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) | ||||
11007 | return EI->getOperand(0); | ||||
11008 | |||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11009 | } |
11010 | } | ||||
11011 | } | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11012 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11013 | |
11014 | // Otherwise, can't do anything fancy. Return an identity vector. | ||||
11015 | for (unsigned i = 0; i != NumElts; ++i) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11016 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11017 | return V; |
11018 | } | ||||
11019 | |||||
11020 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { | ||||
11021 | Value *VecOp = IE.getOperand(0); | ||||
11022 | Value *ScalarOp = IE.getOperand(1); | ||||
11023 | Value *IdxOp = IE.getOperand(2); | ||||
11024 | |||||
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 11025 | // Inserting an undef or into an undefined place, remove this. |
11026 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) | ||||
11027 | ReplaceInstUsesWith(IE, VecOp); | ||||
11028 | |||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11029 | // If the inserted element was extracted from some other vector, and if the |
11030 | // indexes are constant, try to turn this into a shufflevector operation. | ||||
11031 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { | ||||
11032 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && | ||||
11033 | EI->getOperand(0)->getType() == IE.getType()) { | ||||
11034 | unsigned NumVectorElts = IE.getType()->getNumElements(); | ||||
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 11035 | unsigned ExtractedIdx = |
11036 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11037 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11038 | |
11039 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. | ||||
11040 | return ReplaceInstUsesWith(IE, VecOp); | ||||
11041 | |||||
11042 | if (InsertedIdx >= NumVectorElts) // Out of range insert. | ||||
11043 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); | ||||
11044 | |||||
11045 | // If we are extracting a value from a vector, then inserting it right | ||||
11046 | // back into the same place, just use the input vector. | ||||
11047 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) | ||||
11048 | return ReplaceInstUsesWith(IE, VecOp); | ||||
11049 | |||||
11050 | // We could theoretically do this for ANY input. However, doing so could | ||||
11051 | // turn chains of insertelement instructions into a chain of shufflevector | ||||
11052 | // instructions, and right now we do not merge shufflevectors. As such, | ||||
11053 | // only do this in a situation where it is clear that there is benefit. | ||||
11054 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { | ||||
11055 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of | ||||
11056 | // the values of VecOp, except then one read from EIOp0. | ||||
11057 | // Build a new shuffle mask. | ||||
11058 | std::vector<Constant*> Mask; | ||||
11059 | if (isa<UndefValue>(VecOp)) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11060 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11061 | else { |
11062 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11063 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11064 | NumVectorElts)); |
11065 | } | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11066 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11067 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11068 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11069 | } |
11070 | |||||
11071 | // If this insertelement isn't used by some other insertelement, turn it | ||||
11072 | // (and any insertelements it points to), into one big shuffle. | ||||
11073 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { | ||||
11074 | std::vector<Constant*> Mask; | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11075 | Value *RHS = 0; |
11076 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); | ||||
11077 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); | ||||
11078 | // We now have a shuffle of LHS, RHS, Mask. | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11079 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11080 | } |
11081 | } | ||||
11082 | } | ||||
11083 | |||||
11084 | return 0; | ||||
11085 | } | ||||
11086 | |||||
11087 | |||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11088 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
11089 | Value *LHS = SVI.getOperand(0); | ||||
11090 | Value *RHS = SVI.getOperand(1); | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11091 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11092 | |
11093 | bool MadeChange = false; | ||||
11094 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 11095 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11096 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11097 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
11098 | |||||
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11099 | // 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] | 11100 | // the undef, change them to undefs. |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11101 | if (isa<UndefValue>(SVI.getOperand(1))) { |
11102 | // Scan to see if there are any references to the RHS. If so, replace them | ||||
11103 | // with undef element refs and set MadeChange to true. | ||||
11104 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | ||||
11105 | if (Mask[i] >= e && Mask[i] != 2*e) { | ||||
11106 | Mask[i] = 2*e; | ||||
11107 | MadeChange = true; | ||||
11108 | } | ||||
11109 | } | ||||
11110 | |||||
11111 | if (MadeChange) { | ||||
11112 | // Remap any references to RHS to use LHS. | ||||
11113 | std::vector<Constant*> Elts; | ||||
11114 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | ||||
11115 | if (Mask[i] == 2*e) | ||||
11116 | Elts.push_back(UndefValue::get(Type::Int32Ty)); | ||||
11117 | else | ||||
11118 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); | ||||
11119 | } | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11120 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | e4929dd | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 11121 | } |
11122 | } | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11123 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11124 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
11125 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). | ||||
11126 | if (LHS == RHS || isa<UndefValue>(LHS)) { | ||||
11127 | if (isa<UndefValue>(LHS) && LHS == RHS) { | ||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11128 | // shuffle(undef,undef,mask) -> undef. |
11129 | return ReplaceInstUsesWith(SVI, LHS); | ||||
11130 | } | ||||
11131 | |||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11132 | // Remap any references to RHS to use LHS. |
11133 | std::vector<Constant*> Elts; | ||||
11134 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | ||||
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11135 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11136 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11137 | else { |
11138 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || | ||||
11139 | (Mask[i] < e && isa<UndefValue>(LHS))) | ||||
11140 | Mask[i] = 2*e; // Turn into undef. | ||||
11141 | else | ||||
11142 | Mask[i] &= (e-1); // Force to LHS. | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11143 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11144 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11145 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11146 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11147 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11148 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11149 | LHS = SVI.getOperand(0); |
11150 | RHS = SVI.getOperand(1); | ||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11151 | MadeChange = true; |
11152 | } | ||||
11153 | |||||
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11154 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11155 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11156 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11157 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
11158 | if (Mask[i] >= e*2) continue; // Ignore undef values. | ||||
11159 | // Is this an identity shuffle of the LHS value? | ||||
11160 | isLHSID &= (Mask[i] == i); | ||||
11161 | |||||
11162 | // Is this an identity shuffle of the RHS value? | ||||
11163 | isRHSID &= (Mask[i]-e == i); | ||||
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11164 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11165 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11166 | // Eliminate identity shuffles. |
11167 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); | ||||
11168 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); | ||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11169 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11170 | // If the LHS is a shufflevector itself, see if we can combine it with this |
11171 | // one without producing an unusual shuffle. Here we are really conservative: | ||||
11172 | // we are absolutely afraid of producing a shuffle mask not in the input | ||||
11173 | // program, because the code gen may not be smart enough to turn a merged | ||||
11174 | // shuffle into two specific shuffles: it may produce worse code. As such, | ||||
11175 | // we only merge two shuffles if the result is one of the two input shuffle | ||||
11176 | // masks. In this case, merging the shuffles just removes one instruction, | ||||
11177 | // which we know is safe. This is good for things like turning: | ||||
11178 | // (splat(splat)) -> splat. | ||||
11179 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { | ||||
11180 | if (isa<UndefValue>(RHS)) { | ||||
11181 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); | ||||
11182 | |||||
11183 | std::vector<unsigned> NewMask; | ||||
11184 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) | ||||
11185 | if (Mask[i] >= 2*e) | ||||
11186 | NewMask.push_back(2*e); | ||||
11187 | else | ||||
11188 | NewMask.push_back(LHSMask[Mask[i]]); | ||||
11189 | |||||
11190 | // If the result mask is equal to the src shuffle or this shuffle mask, do | ||||
11191 | // the replacement. | ||||
11192 | if (NewMask == LHSMask || NewMask == Mask) { | ||||
11193 | std::vector<Constant*> Elts; | ||||
11194 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { | ||||
11195 | if (NewMask[i] >= e*2) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11196 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11197 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11198 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11199 | } |
11200 | } | ||||
11201 | return new ShuffleVectorInst(LHSSVI->getOperand(0), | ||||
11202 | LHSSVI->getOperand(1), | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11203 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11204 | } |
11205 | } | ||||
11206 | } | ||||
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 11207 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11208 | return MadeChange ? &SVI : 0; |
11209 | } | ||||
11210 | |||||
11211 | |||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11212 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11213 | |
11214 | /// TryToSinkInstruction - Try to move the specified instruction from its | ||||
11215 | /// current block into the beginning of DestBlock, which can only happen if it's | ||||
11216 | /// safe to move the instruction past all of the instructions between it and the | ||||
11217 | /// end of its block. | ||||
11218 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { | ||||
11219 | assert(I->hasOneUse() && "Invariants didn't hold!"); | ||||
11220 | |||||
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 11221 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
11222 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11223 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11224 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 11225 | if (isa<AllocaInst>(I) && I->getParent() == |
11226 | &DestBlock->getParent()->getEntryBlock()) | ||||
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11227 | return false; |
11228 | |||||
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11229 | // We can only sink load instructions if there is nothing between the load and |
11230 | // the end of block that could change the value. | ||||
11231 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { | ||||
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11232 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); |
11233 | Scan != E; ++Scan) | ||||
11234 | if (Scan->mayWriteToMemory()) | ||||
11235 | return false; | ||||
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11236 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11237 | |
11238 | BasicBlock::iterator InsertPos = DestBlock->begin(); | ||||
11239 | while (isa<PHINode>(InsertPos)) ++InsertPos; | ||||
11240 | |||||
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 11241 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11242 | ++NumSunkInst; |
11243 | return true; | ||||
11244 | } | ||||
11245 | |||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11246 | |
11247 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding | ||||
11248 | /// all reachable code to the worklist. | ||||
11249 | /// | ||||
11250 | /// This has a couple of tricks to make the code faster and more powerful. In | ||||
11251 | /// particular, we constant fold and DCE instructions as we go, to avoid adding | ||||
11252 | /// them to the worklist (this significantly speeds up instcombine on code where | ||||
11253 | /// many instructions are dead or constant). Additionally, if we find a branch | ||||
11254 | /// whose condition is a known constant, we only visit the reachable successors. | ||||
11255 | /// | ||||
11256 | static void AddReachableCodeToWorklist(BasicBlock *BB, | ||||
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11257 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11258 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11259 | const TargetData *TD) { |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11260 | std::vector<BasicBlock*> Worklist; |
11261 | Worklist.push_back(BB); | ||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11262 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11263 | while (!Worklist.empty()) { |
11264 | BB = Worklist.back(); | ||||
11265 | Worklist.pop_back(); | ||||
11266 | |||||
11267 | // We have now visited this block! If we've already been here, ignore it. | ||||
11268 | if (!Visited.insert(BB)) continue; | ||||
11269 | |||||
11270 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { | ||||
11271 | Instruction *Inst = BBI++; | ||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11272 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11273 | // DCE instruction if trivially dead. |
11274 | if (isInstructionTriviallyDead(Inst)) { | ||||
11275 | ++NumDeadInst; | ||||
11276 | DOUT << "IC: DCE: " << *Inst; | ||||
11277 | Inst->eraseFromParent(); | ||||
11278 | continue; | ||||
11279 | } | ||||
11280 | |||||
11281 | // ConstantProp instruction if trivially constant. | ||||
11282 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { | ||||
11283 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; | ||||
11284 | Inst->replaceAllUsesWith(C); | ||||
11285 | ++NumConstProp; | ||||
11286 | Inst->eraseFromParent(); | ||||
11287 | continue; | ||||
11288 | } | ||||
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 11289 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11290 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11291 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11292 | |
11293 | // Recursively visit successors. If this is a branch or switch on a | ||||
11294 | // constant, only visit the reachable successor. | ||||
11295 | TerminatorInst *TI = BB->getTerminator(); | ||||
11296 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { | ||||
11297 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { | ||||
11298 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); | ||||
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11299 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11300 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11301 | continue; |
11302 | } | ||||
11303 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { | ||||
11304 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { | ||||
11305 | // See if this is an explicit destination. | ||||
11306 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) | ||||
11307 | if (SI->getCaseValue(i) == Cond) { | ||||
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11308 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11309 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11310 | continue; |
11311 | } | ||||
11312 | |||||
11313 | // Otherwise it is the default destination. | ||||
11314 | Worklist.push_back(SI->getSuccessor(0)); | ||||
11315 | continue; | ||||
11316 | } | ||||
11317 | } | ||||
11318 | |||||
11319 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) | ||||
11320 | Worklist.push_back(TI->getSuccessor(i)); | ||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11321 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11322 | } |
11323 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11324 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11325 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 11326 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11327 | |
11328 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " | ||||
11329 | << F.getNameStr() << "\n"); | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11330 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11331 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11332 | // Do a depth-first traversal of the function, populate the worklist with |
11333 | // the reachable instructions. Ignore blocks that are not reachable. Keep | ||||
11334 | // track of which blocks we visit. | ||||
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11335 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11336 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 11337 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11338 | // Do a quick scan over the function. If we find any blocks that are |
11339 | // unreachable, remove any instructions inside of them. This prevents | ||||
11340 | // the instcombine code from having to deal with some bad special cases. | ||||
11341 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) | ||||
11342 | if (!Visited.count(BB)) { | ||||
11343 | Instruction *Term = BB->getTerminator(); | ||||
11344 | while (Term != BB->begin()) { // Remove instrs bottom-up | ||||
11345 | BasicBlock::iterator I = Term; --I; | ||||
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 11346 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11347 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11348 | ++NumDeadInst; |
11349 | |||||
11350 | if (!I->use_empty()) | ||||
11351 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | ||||
11352 | I->eraseFromParent(); | ||||
11353 | } | ||||
11354 | } | ||||
11355 | } | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11356 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11357 | while (!Worklist.empty()) { |
11358 | Instruction *I = RemoveOneFromWorkList(); | ||||
11359 | if (I == 0) continue; // skip null values. | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11360 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11361 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11362 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11363 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11364 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11365 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11366 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11367 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11368 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11369 | |
11370 | I->eraseFromParent(); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11371 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11372 | continue; |
11373 | } | ||||
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11374 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11375 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 11376 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11377 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11378 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11379 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11380 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 11381 | ReplaceInstUsesWith(*I, C); |
11382 | |||||
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11383 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11384 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11385 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11386 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11387 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11388 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11389 | // See if we can trivially sink this instruction to a successor basic block. |
11390 | if (I->hasOneUse()) { | ||||
11391 | BasicBlock *BB = I->getParent(); | ||||
11392 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); | ||||
11393 | if (UserParent != BB) { | ||||
11394 | bool UserIsSuccessor = false; | ||||
11395 | // See if the user is one of our successors. | ||||
11396 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) | ||||
11397 | if (*SI == UserParent) { | ||||
11398 | UserIsSuccessor = true; | ||||
11399 | break; | ||||
11400 | } | ||||
11401 | |||||
11402 | // If the user is one of our immediate successors, and if that successor | ||||
11403 | // only has us as a predecessors (we'd have to split the critical edge | ||||
11404 | // otherwise), we can keep going. | ||||
11405 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && | ||||
11406 | next(pred_begin(UserParent)) == pred_end(UserParent)) | ||||
11407 | // Okay, the CFG is simple enough, try to sink this instruction. | ||||
11408 | Changed |= TryToSinkInstruction(I, UserParent); | ||||
11409 | } | ||||
11410 | } | ||||
11411 | |||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11412 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11413 | #ifndef NDEBUG |
11414 | std::string OrigI; | ||||
11415 | #endif | ||||
11416 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); | ||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11417 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 11418 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11419 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11420 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11421 | DOUT << "IC: Old = " << *I |
11422 | << " New = " << *Result; | ||||
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11423 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11424 | // Everything uses the new instruction now. |
11425 | I->replaceAllUsesWith(Result); | ||||
11426 | |||||
11427 | // Push the new instruction and any users onto the worklist. | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11428 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11429 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11430 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11431 | // Move the name to the new instruction first. |
11432 | Result->takeName(I); | ||||
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11433 | |
11434 | // Insert the new instruction into the basic block... | ||||
11435 | BasicBlock *InstParent = I->getParent(); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 11436 | BasicBlock::iterator InsertPos = I; |
11437 | |||||
11438 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert | ||||
11439 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. | ||||
11440 | ++InsertPos; | ||||
11441 | |||||
11442 | InstParent->getInstList().insert(InsertPos, Result); | ||||
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11443 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11444 | // Make sure that we reprocess all operands now that we reduced their |
11445 | // use counts. | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11446 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 11447 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11448 | // Instructions can end up on the worklist more than once. Make sure |
11449 | // we do not process an instruction that has been deleted. | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11450 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11451 | |
11452 | // Erase the old instruction. | ||||
11453 | InstParent->getInstList().erase(I); | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 11454 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11455 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11456 | DOUT << "IC: Mod = " << OrigI |
11457 | << " New = " << *I; | ||||
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11458 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11459 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11460 | // If the instruction was modified, it's possible that it is now dead. |
11461 | // if so, remove it. | ||||
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11462 | if (isInstructionTriviallyDead(I)) { |
11463 | // Make sure we process all operands now that we are reducing their | ||||
11464 | // use counts. | ||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11465 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11466 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11467 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11468 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11469 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11470 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11471 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11472 | AddToWorkList(I); |
11473 | AddUsersToWorkList(*I); | ||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11474 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11475 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11476 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11477 | } |
11478 | } | ||||
11479 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11480 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 11481 | |
11482 | // Do an explicit clear, this shrinks the map if needed. | ||||
11483 | WorklistMap.clear(); | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11484 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11485 | } |
11486 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11487 | |
11488 | bool InstCombiner::runOnFunction(Function &F) { | ||||
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 11489 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
11490 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11491 | bool EverMadeChange = false; |
11492 | |||||
11493 | // Iterate while there is work to do. | ||||
11494 | unsigned Iteration = 0; | ||||
11495 | while (DoOneIteration(F, Iteration++)) | ||||
11496 | EverMadeChange = true; | ||||
11497 | return EverMadeChange; | ||||
11498 | } | ||||
11499 | |||||
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 11500 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11501 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11502 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 11503 |