Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
4 | // | ||||
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
6 | // License. See LICENSE.TXT for details. | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // |
10 | // InstructionCombining - Combine instructions to form fewer, simple | ||||
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG. This pass is where |
12 | // algebraic simplification happens. | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // |
14 | // This pass combines things like: | ||||
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 15 | // %Y = add i32 %X, 1 |
16 | // %Z = add i32 %Y, 1 | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 18 | // %Z = add i32 %X, 2 |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // |
20 | // This is a simple worklist driven algorithm. | ||||
21 | // | ||||
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 23 | // the program: |
24 | // 1. If a binary operator has a constant operand, it is moved to the RHS | ||||
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 25 | // 2. Bitwise operators with constant operands are always grouped so that |
26 | // shifts are performed first, then or's, then and's, then xor's. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible |
28 | // 4. All cmp instructions on boolean values are replaced with logical ops | ||||
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 29 | // 5. add X, X is represented as (X*2) => (X << 1) |
30 | // 6. Multiplies with a power-of-two constant argument are transformed into | ||||
31 | // shifts. | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | // ... etc. |
Chris Lattner | 2cd9196 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
35 | |||||
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||
46 | #include "llvm/Transforms/Utils/Local.h" | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 50 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 51 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 52 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 53 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Compiler.h" |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 60 | #include <algorithm> |
Torok Edwin | 3eaee31 | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 61 | #include <climits> |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 62 | #include <sstream> |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 63 | using namespace llvm; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 64 | using namespace llvm::PatternMatch; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 66 | STATISTIC(NumCombined , "Number of insts combined"); |
67 | STATISTIC(NumConstProp, "Number of constant folds"); | ||||
68 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); | ||||
69 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); | ||||
70 | STATISTIC(NumSunkInst , "Number of instructions sunk"); | ||||
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 72 | namespace { |
Chris Lattner | f4b5461 | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 73 | class VISIBILITY_HIDDEN InstCombiner |
74 | : public FunctionPass, | ||||
75 | public InstVisitor<InstCombiner, Instruction*> { | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 76 | // Worklist of all of the instructions that need to be simplified. |
Chris Lattner | 2806dff | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 77 | SmallVector<Instruction*, 256> Worklist; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 78 | DenseMap<Instruction*, unsigned> WorklistMap; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 79 | TargetData *TD; |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 80 | bool MustPreserveLCSSA; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 81 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 82 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 83 | InstCombiner() : FunctionPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 84 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 85 | /// AddToWorkList - Add the specified instruction to the worklist if it |
86 | /// isn't already in it. | ||||
87 | void AddToWorkList(Instruction *I) { | ||||
Dan Gohman | 6b345ee | 2008-07-07 17:46:23 +0000 | [diff] [blame] | 88 | if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 89 | Worklist.push_back(I); |
90 | } | ||||
91 | |||||
92 | // RemoveFromWorkList - remove I from the worklist if it exists. | ||||
93 | void RemoveFromWorkList(Instruction *I) { | ||||
94 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); | ||||
95 | if (It == WorklistMap.end()) return; // Not in worklist. | ||||
96 | |||||
97 | // Don't bother moving everything down, just null out the slot. | ||||
98 | Worklist[It->second] = 0; | ||||
99 | |||||
100 | WorklistMap.erase(It); | ||||
101 | } | ||||
102 | |||||
103 | Instruction *RemoveOneFromWorkList() { | ||||
104 | Instruction *I = Worklist.back(); | ||||
105 | Worklist.pop_back(); | ||||
106 | WorklistMap.erase(I); | ||||
107 | return I; | ||||
108 | } | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 109 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 111 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
112 | /// the instruction to the work lists because they might get more simplified | ||||
113 | /// now. | ||||
114 | /// | ||||
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 115 | void AddUsersToWorkList(Value &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 116 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 117 | UI != UE; ++UI) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 118 | AddToWorkList(cast<Instruction>(*UI)); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 119 | } |
120 | |||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 121 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
122 | /// the work lists because they might get more simplified now. | ||||
123 | /// | ||||
124 | void AddUsesToWorkList(Instruction &I) { | ||||
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 125 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
126 | if (Instruction *Op = dyn_cast<Instruction>(*i)) | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 127 | AddToWorkList(Op); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 129 | |
130 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become | ||||
131 | /// dead. Add all of its operands to the worklist, turning them into | ||||
132 | /// undef's to reduce the number of uses of those instructions. | ||||
133 | /// | ||||
134 | /// Return the specified operand before it is turned into an undef. | ||||
135 | /// | ||||
136 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { | ||||
137 | Value *R = I.getOperand(op); | ||||
138 | |||||
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 139 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
140 | if (Instruction *Op = dyn_cast<Instruction>(*i)) { | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 141 | AddToWorkList(Op); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 142 | // Set the operand to undef to drop the use. |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 143 | *i = UndefValue::get(Op->getType()); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 144 | } |
145 | |||||
146 | return R; | ||||
147 | } | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 148 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 149 | public: |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 150 | virtual bool runOnFunction(Function &F); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 151 | |
152 | bool DoOneIteration(Function &F, unsigned ItNum); | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 154 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 155 | AU.addRequired<TargetData>(); |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 156 | AU.addPreservedID(LCSSAID); |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 157 | AU.setPreservesCFG(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 158 | } |
159 | |||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 160 | TargetData &getTargetData() const { return *TD; } |
161 | |||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 162 | // Visitation implementation - Implement instruction combining for different |
163 | // instruction types. The semantics are as follows: | ||||
164 | // Return Value: | ||||
165 | // null - No change was made | ||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 166 | // 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] | 167 | // otherwise - Change was made, replace I with returned instruction |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 168 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 169 | Instruction *visitAdd(BinaryOperator &I); |
170 | Instruction *visitSub(BinaryOperator &I); | ||||
171 | Instruction *visitMul(BinaryOperator &I); | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 172 | Instruction *visitURem(BinaryOperator &I); |
173 | Instruction *visitSRem(BinaryOperator &I); | ||||
174 | Instruction *visitFRem(BinaryOperator &I); | ||||
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 175 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 176 | Instruction *commonRemTransforms(BinaryOperator &I); |
177 | Instruction *commonIRemTransforms(BinaryOperator &I); | ||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 178 | Instruction *commonDivTransforms(BinaryOperator &I); |
179 | Instruction *commonIDivTransforms(BinaryOperator &I); | ||||
180 | Instruction *visitUDiv(BinaryOperator &I); | ||||
181 | Instruction *visitSDiv(BinaryOperator &I); | ||||
182 | Instruction *visitFDiv(BinaryOperator &I); | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 183 | Instruction *visitAnd(BinaryOperator &I); |
184 | Instruction *visitOr (BinaryOperator &I); | ||||
185 | Instruction *visitXor(BinaryOperator &I); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 186 | Instruction *visitShl(BinaryOperator &I); |
187 | Instruction *visitAShr(BinaryOperator &I); | ||||
188 | Instruction *visitLShr(BinaryOperator &I); | ||||
189 | Instruction *commonShiftTransforms(BinaryOperator &I); | ||||
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 190 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
191 | Constant *RHSC); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 192 | Instruction *visitFCmpInst(FCmpInst &I); |
193 | Instruction *visitICmpInst(ICmpInst &I); | ||||
194 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 195 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
196 | Instruction *LHS, | ||||
197 | ConstantInt *RHS); | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 198 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
199 | ConstantInt *DivRHS); | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 200 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 201 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
202 | ICmpInst::Predicate Cond, Instruction &I); | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 203 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 204 | BinaryOperator &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 205 | Instruction *commonCastTransforms(CastInst &CI); |
206 | Instruction *commonIntCastTransforms(CastInst &CI); | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 207 | Instruction *commonPointerCastTransforms(CastInst &CI); |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 208 | Instruction *visitTrunc(TruncInst &CI); |
209 | Instruction *visitZExt(ZExtInst &CI); | ||||
210 | Instruction *visitSExt(SExtInst &CI); | ||||
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 211 | Instruction *visitFPTrunc(FPTruncInst &CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 212 | Instruction *visitFPExt(CastInst &CI); |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 213 | Instruction *visitFPToUI(FPToUIInst &FI); |
214 | Instruction *visitFPToSI(FPToSIInst &FI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 215 | Instruction *visitUIToFP(CastInst &CI); |
216 | Instruction *visitSIToFP(CastInst &CI); | ||||
217 | Instruction *visitPtrToInt(CastInst &CI); | ||||
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 218 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 219 | Instruction *visitBitCast(BitCastInst &CI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 220 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
221 | Instruction *FI); | ||||
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 222 | Instruction *visitSelectInst(SelectInst &CI); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 223 | Instruction *visitCallInst(CallInst &CI); |
224 | Instruction *visitInvokeInst(InvokeInst &II); | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 225 | Instruction *visitPHINode(PHINode &PN); |
226 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 227 | Instruction *visitAllocationInst(AllocationInst &AI); |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 228 | Instruction *visitFreeInst(FreeInst &FI); |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 229 | Instruction *visitLoadInst(LoadInst &LI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 230 | Instruction *visitStoreInst(StoreInst &SI); |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 231 | Instruction *visitBranchInst(BranchInst &BI); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 232 | Instruction *visitSwitchInst(SwitchInst &SI); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 233 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 234 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 235 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 236 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 237 | |
238 | // visitInstruction - Specify what to return for unhandled instructions... | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 239 | Instruction *visitInstruction(Instruction &I) { return 0; } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 241 | private: |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 242 | Instruction *visitCallSite(CallSite CS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 243 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 244 | Instruction *transformCallThroughTrampoline(CallSite CS); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 245 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
246 | bool DoXform = true); | ||||
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 247 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 249 | public: |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 250 | // InsertNewInstBefore - insert an instruction New before instruction Old |
251 | // in the program. Add the new instruction to the worklist. | ||||
252 | // | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 253 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Chris Lattner | e6f9a91 | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 254 | assert(New && New->getParent() == 0 && |
255 | "New instruction already inserted into a basic block!"); | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 256 | BasicBlock *BB = Old.getParent(); |
257 | BB->getInstList().insert(&Old, New); // Insert inst | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 258 | AddToWorkList(New); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 259 | return New; |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 260 | } |
261 | |||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 262 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
263 | /// This also adds the cast to the worklist. Finally, this returns the | ||||
264 | /// cast. | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 265 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
266 | Instruction &Pos) { | ||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 267 | if (V->getType() == Ty) return V; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 268 | |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 269 | if (Constant *CV = dyn_cast<Constant>(V)) |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 270 | return ConstantExpr::getCast(opc, CV, Ty); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 271 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 272 | Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 273 | AddToWorkList(C); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 274 | return C; |
275 | } | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 276 | |
277 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { | ||||
278 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); | ||||
279 | } | ||||
280 | |||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 282 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
283 | // found to be dead, replacable with another preexisting expression. Here | ||||
284 | // we add all uses of I to the worklist, replace all uses of I with the new | ||||
285 | // value, then return I, so that the inst combiner will know that I was | ||||
286 | // modified. | ||||
287 | // | ||||
288 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 289 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 290 | if (&I != V) { |
291 | I.replaceAllUsesWith(V); | ||||
292 | return &I; | ||||
293 | } else { | ||||
294 | // If we are replacing the instruction with itself, this must be in a | ||||
295 | // segment of unreachable code, so just clobber the instruction. | ||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 296 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 297 | return &I; |
298 | } | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 299 | } |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 301 | // UpdateValueUsesWith - This method is to be used when an value is |
302 | // found to be replacable with another preexisting expression or was | ||||
303 | // updated. Here we add all uses of I to the worklist, replace all uses of | ||||
304 | // I with the new value (unless the instruction was just updated), then | ||||
305 | // return true, so that the inst combiner will know that I was modified. | ||||
306 | // | ||||
307 | bool UpdateValueUsesWith(Value *Old, Value *New) { | ||||
308 | AddUsersToWorkList(*Old); // Add all modified instrs to worklist | ||||
309 | if (Old != New) | ||||
310 | Old->replaceAllUsesWith(New); | ||||
311 | if (Instruction *I = dyn_cast<Instruction>(Old)) | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 312 | AddToWorkList(I); |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 313 | if (Instruction *I = dyn_cast<Instruction>(New)) |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 314 | AddToWorkList(I); |
Chris Lattner | 6dce1a7 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 315 | return true; |
316 | } | ||||
317 | |||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 318 | // EraseInstFromFunction - When dealing with an instruction that has side |
319 | // effects or produces a void value, we can't rely on DCE to delete the | ||||
320 | // instruction. Instead, visit methods should return the value returned by | ||||
321 | // this function. | ||||
322 | Instruction *EraseInstFromFunction(Instruction &I) { | ||||
323 | assert(I.use_empty() && "Cannot erase instruction that is used!"); | ||||
324 | AddUsesToWorkList(I); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 325 | RemoveFromWorkList(&I); |
Chris Lattner | 954f66a | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 326 | I.eraseFromParent(); |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 327 | return 0; // Don't do anything with FI |
328 | } | ||||
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 329 | |
330 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero, | ||||
331 | APInt &KnownOne, unsigned Depth = 0) const { | ||||
332 | return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); | ||||
333 | } | ||||
334 | |||||
335 | bool MaskedValueIsZero(Value *V, const APInt &Mask, | ||||
336 | unsigned Depth = 0) const { | ||||
337 | return llvm::MaskedValueIsZero(V, Mask, TD, Depth); | ||||
338 | } | ||||
339 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const { | ||||
340 | return llvm::ComputeNumSignBits(Op, TD, Depth); | ||||
341 | } | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 342 | |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 343 | private: |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 344 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the |
345 | /// InsertBefore instruction. This is specialized a bit to avoid inserting | ||||
346 | /// casts that are known to not do anything... | ||||
347 | /// | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 348 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, |
349 | Value *V, const Type *DestTy, | ||||
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 350 | Instruction *InsertBefore); |
351 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 352 | /// SimplifyCommutative - This performs a few simplifications for |
353 | /// commutative operators. | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 354 | bool SimplifyCommutative(BinaryOperator &I); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 355 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 356 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
357 | /// most-complex to least-complex order. | ||||
358 | bool SimplifyCompare(CmpInst &I); | ||||
359 | |||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 360 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based |
361 | /// on the demanded bits. | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 362 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, |
363 | APInt& KnownZero, APInt& KnownOne, | ||||
364 | unsigned Depth = 0); | ||||
365 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 366 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, |
367 | uint64_t &UndefElts, unsigned Depth = 0); | ||||
368 | |||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 369 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
370 | // PHI node as operand #0, see if we can fold the instruction into the PHI | ||||
371 | // (which is only possible if all operands to the PHI are constants). | ||||
372 | Instruction *FoldOpIntoPhi(Instruction &I); | ||||
373 | |||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 374 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
375 | // operator and they all are only used by the PHI, PHI together their | ||||
376 | // inputs, and do the operation once, to the result of the PHI. | ||||
377 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 378 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
379 | |||||
380 | |||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 381 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
382 | ConstantInt *AndRHS, BinaryOperator &TheAnd); | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 383 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 384 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 385 | bool isSub, Instruction &I); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 386 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 387 | bool isSigned, bool Inside, Instruction &IB); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 388 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 389 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 390 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 391 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 392 | Instruction *SimplifyMemSet(MemSetInst *MI); |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 393 | |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 394 | |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 395 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 396 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 397 | bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
398 | unsigned CastOpc, | ||||
399 | int &NumCastsRemoved); | ||||
400 | unsigned GetOrEnforceKnownAlignment(Value *V, | ||||
401 | unsigned PrefAlign = 0); | ||||
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 402 | |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 403 | }; |
404 | } | ||||
405 | |||||
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 406 | char InstCombiner::ID = 0; |
407 | static RegisterPass<InstCombiner> | ||||
408 | X("instcombine", "Combine redundant instructions"); | ||||
409 | |||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 410 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 411 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 412 | static unsigned getComplexity(Value *V) { |
413 | if (isa<Instruction>(V)) { | ||||
414 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 415 | return 3; |
416 | return 4; | ||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 418 | if (isa<Argument>(V)) return 3; |
419 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; | ||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 420 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 421 | |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 422 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
423 | // it. | ||||
424 | static bool isOnlyUse(Value *V) { | ||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 425 | return V->hasOneUse() || isa<Constant>(V); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 426 | } |
427 | |||||
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 428 | // getPromotedType - Return the specified type promoted as it would be to pass |
429 | // though a va_arg area... | ||||
430 | static const Type *getPromotedType(const Type *Ty) { | ||||
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 431 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
432 | if (ITy->getBitWidth() < 32) | ||||
433 | return Type::Int32Ty; | ||||
Chris Lattner | 2b7e0ad | 2007-05-23 01:17:04 +0000 | [diff] [blame] | 434 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 435 | return Ty; |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 436 | } |
437 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 438 | /// getBitCastOperand - If the specified operand is a CastInst or a constant |
439 | /// expression bitcast, return the operand value, otherwise return null. | ||||
440 | static Value *getBitCastOperand(Value *V) { | ||||
441 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 442 | return I->getOperand(0); |
443 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 444 | if (CE->getOpcode() == Instruction::BitCast) |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 445 | return CE->getOperand(0); |
446 | return 0; | ||||
447 | } | ||||
448 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 449 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
450 | /// simply extracts arguments and returns what that function returns. | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 451 | static Instruction::CastOps |
452 | isEliminableCastPair( | ||||
453 | const CastInst *CI, ///< The first cast instruction | ||||
454 | unsigned opcode, ///< The opcode of the second cast instruction | ||||
455 | const Type *DstTy, ///< The target type for the second cast instruction | ||||
456 | TargetData *TD ///< The target data for pointer size | ||||
457 | ) { | ||||
458 | |||||
459 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above | ||||
460 | const Type *MidTy = CI->getType(); // B from above | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 461 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 462 | // Get the opcodes of the two Cast instructions |
463 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); | ||||
464 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 465 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 466 | return Instruction::CastOps( |
467 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, | ||||
468 | DstTy, TD->getIntPtrType())); | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 469 | } |
470 | |||||
471 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results | ||||
472 | /// in any code being generated. It does not require codegen if V is simple | ||||
473 | /// enough or if the cast can be folded into other casts. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 474 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
475 | const Type *Ty, TargetData *TD) { | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 476 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
477 | |||||
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 478 | // 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] | 479 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 480 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 481 | return false; |
482 | return true; | ||||
483 | } | ||||
484 | |||||
485 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the | ||||
486 | /// InsertBefore instruction. This is specialized a bit to avoid inserting | ||||
487 | /// casts that are known to not do anything... | ||||
488 | /// | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 489 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, |
490 | Value *V, const Type *DestTy, | ||||
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 491 | Instruction *InsertBefore) { |
492 | if (V->getType() == DestTy) return V; | ||||
493 | if (Constant *C = dyn_cast<Constant>(V)) | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 494 | return ConstantExpr::getCast(opcode, C, DestTy); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 495 | |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 496 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); |
Chris Lattner | 33a6113 | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 497 | } |
498 | |||||
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 499 | // SimplifyCommutative - This performs a few simplifications for commutative |
500 | // operators: | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 501 | // |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 502 | // 1. Order operands such that they are listed from right (least complex) to |
503 | // left (most complex). This puts constants before unary operators before | ||||
504 | // binary operators. | ||||
505 | // | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 506 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
507 | // 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] | 508 | // |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 509 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 510 | bool Changed = false; |
511 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) | ||||
512 | Changed = !I.swapOperands(); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 514 | if (!I.isAssociative()) return Changed; |
515 | Instruction::BinaryOps Opcode = I.getOpcode(); | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 516 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
517 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { | ||||
518 | if (isa<Constant>(I.getOperand(1))) { | ||||
Chris Lattner | 2a9c847 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 519 | Constant *Folded = ConstantExpr::get(I.getOpcode(), |
520 | cast<Constant>(I.getOperand(1)), | ||||
521 | cast<Constant>(Op->getOperand(1))); | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 522 | I.setOperand(0, Op->getOperand(0)); |
523 | I.setOperand(1, Folded); | ||||
524 | return true; | ||||
525 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) | ||||
526 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && | ||||
527 | isOnlyUse(Op) && isOnlyUse(Op1)) { | ||||
528 | Constant *C1 = cast<Constant>(Op->getOperand(1)); | ||||
529 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); | ||||
530 | |||||
531 | // 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] | 532 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 533 | Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 534 | Op1->getOperand(0), |
535 | Op1->getName(), &I); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 536 | AddToWorkList(New); |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 537 | I.setOperand(0, New); |
538 | I.setOperand(1, Folded); | ||||
539 | return true; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 540 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 541 | } |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 542 | return Changed; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 544 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 545 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
546 | /// so that theyare listed from right (least complex) to left (most complex). | ||||
547 | /// This puts constants before unary operators before binary operators. | ||||
548 | bool InstCombiner::SimplifyCompare(CmpInst &I) { | ||||
549 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) | ||||
550 | return false; | ||||
551 | I.swapOperands(); | ||||
552 | // Compare instructions are not associative so there's nothing else we can do. | ||||
553 | return true; | ||||
554 | } | ||||
555 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 556 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
557 | // if the LHS is a constant zero (which is the 'negate' form). | ||||
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 558 | // |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 559 | static inline Value *dyn_castNegVal(Value *V) { |
560 | if (BinaryOperator::isNeg(V)) | ||||
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 561 | return BinaryOperator::getNegArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 0ce8580 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 563 | // Constants can be considered to be negated values if they can be folded. |
564 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) | ||||
565 | return ConstantExpr::getNeg(C); | ||||
Nick Lewycky | 18b3da6 | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 566 | |
567 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) | ||||
568 | if (C->getType()->getElementType()->isInteger()) | ||||
569 | return ConstantExpr::getNeg(C); | ||||
570 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 571 | return 0; |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 572 | } |
573 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 574 | static inline Value *dyn_castNotVal(Value *V) { |
575 | if (BinaryOperator::isNot(V)) | ||||
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 576 | return BinaryOperator::getNotArgument(V); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 577 | |
578 | // Constants can be considered to be not'ed values... | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 579 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 580 | return ConstantInt::get(~C->getValue()); |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 581 | return 0; |
582 | } | ||||
583 | |||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 584 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
585 | // other computations (because it has a constant operand), return the | ||||
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 586 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
587 | // Otherwise, return null. | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 588 | // |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 589 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 590 | if (V->hasOneUse() && V->getType()->isInteger()) |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 591 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 592 | if (I->getOpcode() == Instruction::Mul) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 593 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 594 | return I->getOperand(0); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 595 | if (I->getOpcode() == Instruction::Shl) |
Chris Lattner | 50e60c7 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 596 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 597 | // The multiplier is really 1 << CST. |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 598 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 599 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 600 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 601 | return I->getOperand(0); |
602 | } | ||||
603 | } | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 604 | return 0; |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | af2930e | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 607 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
608 | /// expression, return it. | ||||
609 | static User *dyn_castGetElementPtr(Value *V) { | ||||
610 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); | ||||
611 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | ||||
612 | if (CE->getOpcode() == Instruction::GetElementPtr) | ||||
613 | return cast<User>(V); | ||||
614 | return false; | ||||
615 | } | ||||
616 | |||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 617 | /// getOpcode - If this is an Instruction or a ConstantExpr, return the |
618 | /// opcode value. Otherwise return UserOp1. | ||||
Dan Gohman | b99e2e2 | 2008-05-29 19:53:46 +0000 | [diff] [blame] | 619 | static unsigned getOpcode(const Value *V) { |
620 | if (const Instruction *I = dyn_cast<Instruction>(V)) | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 621 | return I->getOpcode(); |
Dan Gohman | b99e2e2 | 2008-05-29 19:53:46 +0000 | [diff] [blame] | 622 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 623 | return CE->getOpcode(); |
624 | // Use UserOp1 to mean there's no opcode. | ||||
625 | return Instruction::UserOp1; | ||||
626 | } | ||||
627 | |||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 628 | /// AddOne - Add one to a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 629 | static ConstantInt *AddOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 630 | APInt Val(C->getValue()); |
631 | return ConstantInt::get(++Val); | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 632 | } |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 633 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 634 | static ConstantInt *SubOne(ConstantInt *C) { |
Reid Spencer | 2149a9d | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 635 | APInt Val(C->getValue()); |
636 | return ConstantInt::get(--Val); | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 637 | } |
638 | /// Add - Add two ConstantInts together | ||||
639 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { | ||||
640 | return ConstantInt::get(C1->getValue() + C2->getValue()); | ||||
641 | } | ||||
642 | /// And - Bitwise AND two ConstantInts together | ||||
643 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { | ||||
644 | return ConstantInt::get(C1->getValue() & C2->getValue()); | ||||
645 | } | ||||
646 | /// Subtract - Subtract one ConstantInt from another | ||||
647 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { | ||||
648 | return ConstantInt::get(C1->getValue() - C2->getValue()); | ||||
649 | } | ||||
650 | /// Multiply - Multiply two ConstantInts together | ||||
651 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { | ||||
652 | return ConstantInt::get(C1->getValue() * C2->getValue()); | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 653 | } |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 654 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
655 | /// this size. | ||||
656 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) { | ||||
657 | uint32_t W = C1->getBitWidth(); | ||||
658 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); | ||||
659 | if (sign) { | ||||
660 | LHSExt.sext(W * 2); | ||||
661 | RHSExt.sext(W * 2); | ||||
662 | } else { | ||||
663 | LHSExt.zext(W * 2); | ||||
664 | RHSExt.zext(W * 2); | ||||
665 | } | ||||
666 | |||||
667 | APInt MulExt = LHSExt * RHSExt; | ||||
668 | |||||
669 | if (sign) { | ||||
670 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); | ||||
671 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); | ||||
672 | return MulExt.slt(Min) || MulExt.sgt(Max); | ||||
673 | } else | ||||
674 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); | ||||
675 | } | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 676 | |
Reid Spencer | e7816b5 | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 678 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
679 | /// specified instruction is a constant integer. If so, check to see if there | ||||
680 | /// are any bits set in the constant that are not demanded. If so, shrink the | ||||
681 | /// constant and return true. | ||||
682 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, | ||||
Reid Spencer | 6b79e2d | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 683 | APInt Demanded) { |
684 | assert(I && "No instruction?"); | ||||
685 | assert(OpNo < I->getNumOperands() && "Operand index too large"); | ||||
686 | |||||
687 | // If the operand is not a constant integer, nothing to do. | ||||
688 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); | ||||
689 | if (!OpC) return false; | ||||
690 | |||||
691 | // If there are no bits set that aren't demanded, nothing to do. | ||||
692 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); | ||||
693 | if ((~Demanded & OpC->getValue()) == 0) | ||||
694 | return false; | ||||
695 | |||||
696 | // This instruction is producing bits that are not demanded. Shrink the RHS. | ||||
697 | Demanded &= OpC->getValue(); | ||||
698 | I->setOperand(OpNo, ConstantInt::get(Demanded)); | ||||
699 | return true; | ||||
700 | } | ||||
701 | |||||
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 702 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
703 | // set of known zero and one bits, compute the maximum and minimum values that | ||||
704 | // could have the specified known zero and known one bits, returning them in | ||||
705 | // min/max. | ||||
706 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 707 | const APInt& KnownZero, |
708 | const APInt& KnownOne, | ||||
709 | APInt& Min, APInt& Max) { | ||||
710 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | ||||
711 | assert(KnownZero.getBitWidth() == BitWidth && | ||||
712 | KnownOne.getBitWidth() == BitWidth && | ||||
713 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && | ||||
714 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 715 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 716 | |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 717 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
718 | // bit if it is unknown. | ||||
719 | Min = KnownOne; | ||||
720 | Max = KnownOne|UnknownBits; | ||||
721 | |||||
Zhou Sheng | 4acf155 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 722 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 723 | Min.set(BitWidth-1); |
724 | Max.clear(BitWidth-1); | ||||
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 725 | } |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 726 | } |
727 | |||||
728 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and | ||||
729 | // a set of known zero and one bits, compute the maximum and minimum values that | ||||
730 | // could have the specified known zero and known one bits, returning them in | ||||
731 | // min/max. | ||||
732 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, | ||||
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 733 | const APInt &KnownZero, |
734 | const APInt &KnownOne, | ||||
735 | APInt &Min, APInt &Max) { | ||||
736 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); BitWidth = BitWidth; | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 737 | assert(KnownZero.getBitWidth() == BitWidth && |
738 | KnownOne.getBitWidth() == BitWidth && | ||||
739 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && | ||||
740 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 741 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 742 | |
743 | // The minimum value is when the unknown bits are all zeros. | ||||
744 | Min = KnownOne; | ||||
745 | // The maximum value is when the unknown bits are all ones. | ||||
746 | Max = KnownOne|UnknownBits; | ||||
747 | } | ||||
Chris Lattner | 255d891 | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 748 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 749 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler |
750 | /// value based on the demanded bits. When this function is called, it is known | ||||
751 | /// that only the bits set in DemandedMask of the result of V are ever used | ||||
752 | /// downstream. Consequently, depending on the mask and V, it may be possible | ||||
753 | /// to replace V with a constant or one of its operands. In such cases, this | ||||
754 | /// function does the replacement and returns true. In all other cases, it | ||||
755 | /// returns false after analyzing the expression and setting KnownOne and known | ||||
756 | /// to be one in the expression. KnownZero contains all the bits that are known | ||||
757 | /// to be zero in the expression. These are provided to potentially allow the | ||||
758 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify | ||||
759 | /// the expression. KnownOne and KnownZero always follow the invariant that | ||||
760 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that | ||||
761 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set | ||||
762 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero | ||||
763 | /// and KnownOne must all be the same. | ||||
764 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, | ||||
765 | APInt& KnownZero, APInt& KnownOne, | ||||
766 | unsigned Depth) { | ||||
767 | assert(V != 0 && "Null pointer of Value???"); | ||||
768 | assert(Depth <= 6 && "Limit Search Depth"); | ||||
769 | uint32_t BitWidth = DemandedMask.getBitWidth(); | ||||
770 | const IntegerType *VTy = cast<IntegerType>(V->getType()); | ||||
771 | assert(VTy->getBitWidth() == BitWidth && | ||||
772 | KnownZero.getBitWidth() == BitWidth && | ||||
773 | KnownOne.getBitWidth() == BitWidth && | ||||
774 | "Value *V, DemandedMask, KnownZero and KnownOne \ | ||||
775 | must have same BitWidth"); | ||||
776 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | ||||
777 | // We know all of the bits for a constant! | ||||
778 | KnownOne = CI->getValue() & DemandedMask; | ||||
779 | KnownZero = ~KnownOne & DemandedMask; | ||||
780 | return false; | ||||
781 | } | ||||
782 | |||||
Zhou Sheng | 9670445 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 783 | KnownZero.clear(); |
784 | KnownOne.clear(); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 785 | if (!V->hasOneUse()) { // Other users may use these bits. |
786 | if (Depth != 0) { // Not at the root. | ||||
787 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. | ||||
788 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); | ||||
789 | return false; | ||||
790 | } | ||||
791 | // If this is the root being simplified, allow it to have multiple uses, | ||||
792 | // just set the DemandedMask to all bits. | ||||
793 | DemandedMask = APInt::getAllOnesValue(BitWidth); | ||||
794 | } else if (DemandedMask == 0) { // Not demanding any bits from V. | ||||
795 | if (V != UndefValue::get(VTy)) | ||||
796 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); | ||||
797 | return false; | ||||
798 | } else if (Depth == 6) { // Limit search depth. | ||||
799 | return false; | ||||
800 | } | ||||
801 | |||||
802 | Instruction *I = dyn_cast<Instruction>(V); | ||||
803 | if (!I) return false; // Only analyze instructions. | ||||
804 | |||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 805 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
806 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; | ||||
807 | switch (I->getOpcode()) { | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 808 | default: |
809 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); | ||||
810 | break; | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 811 | case Instruction::And: |
812 | // If either the LHS or the RHS are Zero, the result is zero. | ||||
813 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
814 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
815 | return true; | ||||
816 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
817 | "Bits known to be one AND zero?"); | ||||
818 | |||||
819 | // If something is known zero on the RHS, the bits aren't demanded on the | ||||
820 | // LHS. | ||||
821 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, | ||||
822 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
823 | return true; | ||||
824 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
825 | "Bits known to be one AND zero?"); | ||||
826 | |||||
827 | // If all of the demanded bits are known 1 on one side, return the other. | ||||
828 | // These bits cannot contribute to the result of the 'and'. | ||||
829 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == | ||||
830 | (DemandedMask & ~LHSKnownZero)) | ||||
831 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
832 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == | ||||
833 | (DemandedMask & ~RHSKnownZero)) | ||||
834 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
835 | |||||
836 | // If all of the demanded bits in the inputs are known zeros, return zero. | ||||
837 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) | ||||
838 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); | ||||
839 | |||||
840 | // If the RHS is a constant, see if we can simplify it. | ||||
841 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) | ||||
842 | return UpdateValueUsesWith(I, I); | ||||
843 | |||||
844 | // Output known-1 bits are only known if set in both the LHS & RHS. | ||||
845 | RHSKnownOne &= LHSKnownOne; | ||||
846 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | ||||
847 | RHSKnownZero |= LHSKnownZero; | ||||
848 | break; | ||||
849 | case Instruction::Or: | ||||
850 | // If either the LHS or the RHS are One, the result is One. | ||||
851 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
852 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
853 | return true; | ||||
854 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
855 | "Bits known to be one AND zero?"); | ||||
856 | // If something is known one on the RHS, the bits aren't demanded on the | ||||
857 | // LHS. | ||||
858 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, | ||||
859 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
860 | return true; | ||||
861 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
862 | "Bits known to be one AND zero?"); | ||||
863 | |||||
864 | // If all of the demanded bits are known zero on one side, return the other. | ||||
865 | // These bits cannot contribute to the result of the 'or'. | ||||
866 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == | ||||
867 | (DemandedMask & ~LHSKnownOne)) | ||||
868 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
869 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == | ||||
870 | (DemandedMask & ~RHSKnownOne)) | ||||
871 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
872 | |||||
873 | // If all of the potentially set bits on one side are known to be set on | ||||
874 | // the other side, just use the 'other' side. | ||||
875 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == | ||||
876 | (DemandedMask & (~RHSKnownZero))) | ||||
877 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
878 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == | ||||
879 | (DemandedMask & (~LHSKnownZero))) | ||||
880 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
881 | |||||
882 | // If the RHS is a constant, see if we can simplify it. | ||||
883 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | ||||
884 | return UpdateValueUsesWith(I, I); | ||||
885 | |||||
886 | // Output known-0 bits are only known if clear in both the LHS & RHS. | ||||
887 | RHSKnownZero &= LHSKnownZero; | ||||
888 | // Output known-1 are known to be set if set in either the LHS | RHS. | ||||
889 | RHSKnownOne |= LHSKnownOne; | ||||
890 | break; | ||||
891 | case Instruction::Xor: { | ||||
892 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
893 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
894 | return true; | ||||
895 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
896 | "Bits known to be one AND zero?"); | ||||
897 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | ||||
898 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
899 | return true; | ||||
900 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
901 | "Bits known to be one AND zero?"); | ||||
902 | |||||
903 | // If all of the demanded bits are known zero on one side, return the other. | ||||
904 | // These bits cannot contribute to the result of the 'xor'. | ||||
905 | if ((DemandedMask & RHSKnownZero) == DemandedMask) | ||||
906 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
907 | if ((DemandedMask & LHSKnownZero) == DemandedMask) | ||||
908 | return UpdateValueUsesWith(I, I->getOperand(1)); | ||||
909 | |||||
910 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | ||||
911 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | | ||||
912 | (RHSKnownOne & LHSKnownOne); | ||||
913 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | ||||
914 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | | ||||
915 | (RHSKnownOne & LHSKnownZero); | ||||
916 | |||||
917 | // If all of the demanded bits are known to be zero on one side or the | ||||
918 | // other, turn this into an *inclusive* or. | ||||
919 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 | ||||
920 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { | ||||
921 | Instruction *Or = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 922 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 923 | I->getName()); |
924 | InsertNewInstBefore(Or, *I); | ||||
925 | return UpdateValueUsesWith(I, Or); | ||||
926 | } | ||||
927 | |||||
928 | // If all of the demanded bits on one side are known, and all of the set | ||||
929 | // bits on that side are also known to be set on the other side, turn this | ||||
930 | // into an AND, as we know the bits will be cleared. | ||||
931 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 | ||||
932 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { | ||||
933 | // all known | ||||
934 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { | ||||
935 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); | ||||
936 | Instruction *And = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 937 | BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 938 | InsertNewInstBefore(And, *I); |
939 | return UpdateValueUsesWith(I, And); | ||||
940 | } | ||||
941 | } | ||||
942 | |||||
943 | // If the RHS is a constant, see if we can simplify it. | ||||
944 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. | ||||
945 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | ||||
946 | return UpdateValueUsesWith(I, I); | ||||
947 | |||||
948 | RHSKnownZero = KnownZeroOut; | ||||
949 | RHSKnownOne = KnownOneOut; | ||||
950 | break; | ||||
951 | } | ||||
952 | case Instruction::Select: | ||||
953 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, | ||||
954 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
955 | return true; | ||||
956 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | ||||
957 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
958 | return true; | ||||
959 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
960 | "Bits known to be one AND zero?"); | ||||
961 | assert((LHSKnownZero & LHSKnownOne) == 0 && | ||||
962 | "Bits known to be one AND zero?"); | ||||
963 | |||||
964 | // If the operands are constants, see if we can simplify them. | ||||
965 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | ||||
966 | return UpdateValueUsesWith(I, I); | ||||
967 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) | ||||
968 | return UpdateValueUsesWith(I, I); | ||||
969 | |||||
970 | // Only known if known in both the LHS and RHS. | ||||
971 | RHSKnownOne &= LHSKnownOne; | ||||
972 | RHSKnownZero &= LHSKnownZero; | ||||
973 | break; | ||||
974 | case Instruction::Trunc: { | ||||
975 | uint32_t truncBf = | ||||
976 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 977 | DemandedMask.zext(truncBf); |
978 | RHSKnownZero.zext(truncBf); | ||||
979 | RHSKnownOne.zext(truncBf); | ||||
980 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | ||||
981 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 982 | return true; |
983 | DemandedMask.trunc(BitWidth); | ||||
984 | RHSKnownZero.trunc(BitWidth); | ||||
985 | RHSKnownOne.trunc(BitWidth); | ||||
986 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
987 | "Bits known to be one AND zero?"); | ||||
988 | break; | ||||
989 | } | ||||
990 | case Instruction::BitCast: | ||||
991 | if (!I->getOperand(0)->getType()->isInteger()) | ||||
992 | return false; | ||||
993 | |||||
994 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | ||||
995 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
996 | return true; | ||||
997 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
998 | "Bits known to be one AND zero?"); | ||||
999 | break; | ||||
1000 | case Instruction::ZExt: { | ||||
1001 | // Compute the bits in the result that are not present in the input. | ||||
1002 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1003 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1004 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1005 | DemandedMask.trunc(SrcBitWidth); |
1006 | RHSKnownZero.trunc(SrcBitWidth); | ||||
1007 | RHSKnownOne.trunc(SrcBitWidth); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1008 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, |
1009 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1010 | return true; |
1011 | DemandedMask.zext(BitWidth); | ||||
1012 | RHSKnownZero.zext(BitWidth); | ||||
1013 | RHSKnownOne.zext(BitWidth); | ||||
1014 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1015 | "Bits known to be one AND zero?"); | ||||
1016 | // The top bits are known to be zero. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1017 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1018 | break; |
1019 | } | ||||
1020 | case Instruction::SExt: { | ||||
1021 | // Compute the bits in the result that are not present in the input. | ||||
1022 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | ||||
Reid Spencer | 2f54917 | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1023 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1024 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1025 | APInt InputDemandedBits = DemandedMask & |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1026 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1027 | |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1028 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1029 | // If any of the sign extended bits are demanded, we know that the sign |
1030 | // bit is demanded. | ||||
1031 | if ((NewBits & DemandedMask) != 0) | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1032 | InputDemandedBits.set(SrcBitWidth-1); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1033 | |
Zhou Sheng | d48653a | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1034 | InputDemandedBits.trunc(SrcBitWidth); |
1035 | RHSKnownZero.trunc(SrcBitWidth); | ||||
1036 | RHSKnownOne.trunc(SrcBitWidth); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1037 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, |
1038 | RHSKnownZero, RHSKnownOne, Depth+1)) | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1039 | return true; |
1040 | InputDemandedBits.zext(BitWidth); | ||||
1041 | RHSKnownZero.zext(BitWidth); | ||||
1042 | RHSKnownOne.zext(BitWidth); | ||||
1043 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1044 | "Bits known to be one AND zero?"); | ||||
1045 | |||||
1046 | // If the sign bit of the input is known set or clear, then we know the | ||||
1047 | // top bits of the result. | ||||
1048 | |||||
1049 | // If the input sign bit is known zero, or if the NewBits are not demanded | ||||
1050 | // convert this into a zero extension. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1051 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1052 | { |
1053 | // Convert to ZExt cast | ||||
1054 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); | ||||
1055 | return UpdateValueUsesWith(I, NewCast); | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1056 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1057 | RHSKnownOne |= NewBits; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1058 | } |
1059 | break; | ||||
1060 | } | ||||
1061 | case Instruction::Add: { | ||||
1062 | // Figure out what the input bits are. If the top bits of the and result | ||||
1063 | // are not demanded, then the add doesn't demand them from its input | ||||
1064 | // either. | ||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1065 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1066 | |
1067 | // If there is a constant on the RHS, there are a variety of xformations | ||||
1068 | // we can do. | ||||
1069 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
1070 | // If null, this should be simplified elsewhere. Some of the xforms here | ||||
1071 | // won't work if the RHS is zero. | ||||
1072 | if (RHS->isZero()) | ||||
1073 | break; | ||||
1074 | |||||
1075 | // If the top bit of the output is demanded, demand everything from the | ||||
1076 | // input. Otherwise, we demand all the input bits except NLZ top bits. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1077 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1078 | |
1079 | // Find information about known zero/one bits in the input. | ||||
1080 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, | ||||
1081 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1082 | return true; | ||||
1083 | |||||
1084 | // If the RHS of the add has bits set that can't affect the input, reduce | ||||
1085 | // the constant. | ||||
1086 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) | ||||
1087 | return UpdateValueUsesWith(I, I); | ||||
1088 | |||||
1089 | // Avoid excess work. | ||||
1090 | if (LHSKnownZero == 0 && LHSKnownOne == 0) | ||||
1091 | break; | ||||
1092 | |||||
1093 | // Turn it into OR if input bits are zero. | ||||
1094 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { | ||||
1095 | Instruction *Or = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1096 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1097 | I->getName()); |
1098 | InsertNewInstBefore(Or, *I); | ||||
1099 | return UpdateValueUsesWith(I, Or); | ||||
1100 | } | ||||
1101 | |||||
1102 | // We can say something about the output known-zero and known-one bits, | ||||
1103 | // depending on potential carries from the input constant and the | ||||
1104 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 | ||||
1105 | // bits set and the RHS constant is 0x01001, then we know we have a known | ||||
1106 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. | ||||
1107 | |||||
1108 | // To compute this, we first compute the potential carry bits. These are | ||||
1109 | // the bits which may be modified. I'm not aware of a better way to do | ||||
1110 | // this scan. | ||||
Zhou Sheng | b9cb95f | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1111 | const APInt& RHSVal = RHS->getValue(); |
1112 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1113 | |
1114 | // Now that we know which bits have carries, compute the known-1/0 sets. | ||||
1115 | |||||
1116 | // Bits are known one if they are known zero in one operand and one in the | ||||
1117 | // other, and there is no input carry. | ||||
1118 | RHSKnownOne = ((LHSKnownZero & RHSVal) | | ||||
1119 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; | ||||
1120 | |||||
1121 | // Bits are known zero if they are known zero in both operands and there | ||||
1122 | // is no input carry. | ||||
1123 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; | ||||
1124 | } else { | ||||
1125 | // If the high-bits of this ADD are not demanded, then it does not demand | ||||
1126 | // the high bits of its LHS or RHS. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1127 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1128 | // Right fill the mask of bits for this ADD to demand the most |
1129 | // significant bit and all those below it. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1130 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1131 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
1132 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1133 | return true; | ||||
1134 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, | ||||
1135 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1136 | return true; | ||||
1137 | } | ||||
1138 | } | ||||
1139 | break; | ||||
1140 | } | ||||
1141 | case Instruction::Sub: | ||||
1142 | // If the high-bits of this SUB are not demanded, then it does not demand | ||||
1143 | // the high bits of its LHS or RHS. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1144 | if (DemandedMask[BitWidth-1] == 0) { |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1145 | // Right fill the mask of bits for this SUB to demand the most |
1146 | // significant bit and all those below it. | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1147 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1148 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1149 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, |
1150 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1151 | return true; | ||||
1152 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, | ||||
1153 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1154 | return true; | ||||
1155 | } | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1156 | // Otherwise just hand the sub off to ComputeMaskedBits to fill in |
1157 | // the known zeros and ones. | ||||
1158 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1159 | break; |
1160 | case Instruction::Shl: | ||||
1161 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1162 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1163 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
1164 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1165 | RHSKnownZero, RHSKnownOne, Depth+1)) |
1166 | return true; | ||||
1167 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1168 | "Bits known to be one AND zero?"); | ||||
1169 | RHSKnownZero <<= ShiftAmt; | ||||
1170 | RHSKnownOne <<= ShiftAmt; | ||||
1171 | // low bits known zero. | ||||
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1172 | if (ShiftAmt) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1173 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1174 | } |
1175 | break; | ||||
1176 | case Instruction::LShr: | ||||
1177 | // For a logical shift right | ||||
1178 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1179 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1180 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1181 | // Unsigned shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1182 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
1183 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1184 | RHSKnownZero, RHSKnownOne, Depth+1)) |
1185 | return true; | ||||
1186 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1187 | "Bits known to be one AND zero?"); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1188 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
1189 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); | ||||
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1190 | if (ShiftAmt) { |
1191 | // Compute the new bits that are at the top now. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1192 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Zhou Sheng | adc1495 | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1193 | RHSKnownZero |= HighBits; // high bits known zero. |
1194 | } | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1195 | } |
1196 | break; | ||||
1197 | case Instruction::AShr: | ||||
1198 | // If this is an arithmetic shift right and only the low-bit is set, we can | ||||
1199 | // always convert this into a logical shr, even if the shift amount is | ||||
1200 | // variable. The low bit of the shift cannot be an input sign bit unless | ||||
1201 | // the shift amount is >= the size of the datatype, which is undefined. | ||||
1202 | if (DemandedMask == 1) { | ||||
1203 | // Perform the logical shift right. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1204 | Value *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1205 | I->getOperand(0), I->getOperand(1), I->getName()); |
1206 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); | ||||
1207 | return UpdateValueUsesWith(I, NewVal); | ||||
1208 | } | ||||
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 1209 | |
1210 | // If the sign bit is the only bit demanded by this ashr, then there is no | ||||
1211 | // need to do it, the shift doesn't change the high bit. | ||||
1212 | if (DemandedMask.isSignBit()) | ||||
1213 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1214 | |
1215 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1216 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1217 | |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1218 | // Signed shift right. |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1219 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
Lauro Ramos Venancio | d0499af | 2007-06-06 17:08:48 +0000 | [diff] [blame] | 1220 | // If any of the "high bits" are demanded, we should set the sign bit as |
1221 | // demanded. | ||||
1222 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) | ||||
1223 | DemandedMaskIn.set(BitWidth-1); | ||||
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1224 | if (SimplifyDemandedBits(I->getOperand(0), |
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1225 | DemandedMaskIn, |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1226 | RHSKnownZero, RHSKnownOne, Depth+1)) |
1227 | return true; | ||||
1228 | assert((RHSKnownZero & RHSKnownOne) == 0 && | ||||
1229 | "Bits known to be one AND zero?"); | ||||
1230 | // Compute the new bits that are at the top now. | ||||
Zhou Sheng | 01542f3 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1231 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1232 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
1233 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); | ||||
1234 | |||||
1235 | // Handle the sign bits. | ||||
1236 | APInt SignBit(APInt::getSignBit(BitWidth)); | ||||
1237 | // Adjust to where it is now in the mask. | ||||
1238 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); | ||||
1239 | |||||
1240 | // If the input sign bit is known to be zero, or if none of the top bits | ||||
1241 | // are demanded, turn this into an unsigned shift right. | ||||
Zhou Sheng | cc41940 | 2008-06-06 08:32:05 +0000 | [diff] [blame] | 1242 | if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] || |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1243 | (HighBits & ~DemandedMask) == HighBits) { |
1244 | // Perform the logical shift right. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1245 | Value *NewVal = BinaryOperator::CreateLShr( |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1246 | I->getOperand(0), SA, I->getName()); |
1247 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); | ||||
1248 | return UpdateValueUsesWith(I, NewVal); | ||||
1249 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. | ||||
1250 | RHSKnownOne |= HighBits; | ||||
1251 | } | ||||
1252 | } | ||||
1253 | break; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1254 | case Instruction::SRem: |
1255 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
1256 | APInt RA = Rem->getValue(); | ||||
1257 | if (RA.isPowerOf2() || (-RA).isPowerOf2()) { | ||||
Nick Lewycky | 3ac9e10 | 2008-07-12 05:04:38 +0000 | [diff] [blame] | 1258 | if (DemandedMask.ule(RA)) // srem won't affect demanded bits |
1259 | return UpdateValueUsesWith(I, I->getOperand(0)); | ||||
1260 | |||||
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 1261 | APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA; |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1262 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
1263 | if (SimplifyDemandedBits(I->getOperand(0), Mask2, | ||||
1264 | LHSKnownZero, LHSKnownOne, Depth+1)) | ||||
1265 | return true; | ||||
1266 | |||||
1267 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) | ||||
1268 | LHSKnownZero |= ~LowBits; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1269 | |
1270 | KnownZero |= LHSKnownZero & DemandedMask; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1271 | |
1272 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | ||||
1273 | } | ||||
1274 | } | ||||
1275 | break; | ||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1276 | case Instruction::URem: { |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1277 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
1278 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | ||||
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1279 | if (SimplifyDemandedBits(I->getOperand(0), AllOnes, |
1280 | KnownZero2, KnownOne2, Depth+1)) | ||||
1281 | return true; | ||||
1282 | |||||
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1283 | uint32_t Leaders = KnownZero2.countLeadingOnes(); |
Dan Gohman | e85b758 | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1284 | if (SimplifyDemandedBits(I->getOperand(1), AllOnes, |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1285 | KnownZero2, KnownOne2, Depth+1)) |
1286 | return true; | ||||
1287 | |||||
1288 | Leaders = std::max(Leaders, | ||||
1289 | KnownZero2.countLeadingOnes()); | ||||
1290 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; | ||||
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1291 | break; |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1292 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1293 | case Instruction::Call: |
1294 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | ||||
1295 | switch (II->getIntrinsicID()) { | ||||
1296 | default: break; | ||||
1297 | case Intrinsic::bswap: { | ||||
1298 | // If the only bits demanded come from one byte of the bswap result, | ||||
1299 | // just shift the input byte into position to eliminate the bswap. | ||||
1300 | unsigned NLZ = DemandedMask.countLeadingZeros(); | ||||
1301 | unsigned NTZ = DemandedMask.countTrailingZeros(); | ||||
1302 | |||||
1303 | // Round NTZ down to the next byte. If we have 11 trailing zeros, then | ||||
1304 | // we need all the bits down to bit 8. Likewise, round NLZ. If we | ||||
1305 | // have 14 leading zeros, round to 8. | ||||
1306 | NLZ &= ~7; | ||||
1307 | NTZ &= ~7; | ||||
1308 | // If we need exactly one byte, we can do this transformation. | ||||
1309 | if (BitWidth-NLZ-NTZ == 8) { | ||||
1310 | unsigned ResultBit = NTZ; | ||||
1311 | unsigned InputBit = BitWidth-NTZ-8; | ||||
1312 | |||||
1313 | // Replace this with either a left or right shift to get the byte into | ||||
1314 | // the right place. | ||||
1315 | Instruction *NewVal; | ||||
1316 | if (InputBit > ResultBit) | ||||
1317 | NewVal = BinaryOperator::CreateLShr(I->getOperand(1), | ||||
1318 | ConstantInt::get(I->getType(), InputBit-ResultBit)); | ||||
1319 | else | ||||
1320 | NewVal = BinaryOperator::CreateShl(I->getOperand(1), | ||||
1321 | ConstantInt::get(I->getType(), ResultBit-InputBit)); | ||||
1322 | NewVal->takeName(I); | ||||
1323 | InsertNewInstBefore(NewVal, *I); | ||||
1324 | return UpdateValueUsesWith(I, NewVal); | ||||
1325 | } | ||||
1326 | |||||
1327 | // TODO: Could compute known zero/one bits based on the input. | ||||
1328 | break; | ||||
1329 | } | ||||
1330 | } | ||||
1331 | } | ||||
Chris Lattner | 6c3bfba | 2008-06-18 18:11:55 +0000 | [diff] [blame] | 1332 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1333 | break; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1334 | } |
Reid Spencer | 8cb6834 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1335 | |
1336 | // If the client is only demanding bits that we know, return the known | ||||
1337 | // constant. | ||||
1338 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) | ||||
1339 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); | ||||
1340 | return false; | ||||
1341 | } | ||||
1342 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1343 | |
1344 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with | ||||
1345 | /// 64 or fewer elements. DemandedElts contains the set of elements that are | ||||
1346 | /// actually used by the caller. This method analyzes which elements of the | ||||
1347 | /// operand are undef and returns that information in UndefElts. | ||||
1348 | /// | ||||
1349 | /// If the information about demanded elements can be used to simplify the | ||||
1350 | /// operation, the operation is simplified, then the resultant value is | ||||
1351 | /// returned. This returns null if no change was made. | ||||
1352 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, | ||||
1353 | uint64_t &UndefElts, | ||||
1354 | unsigned Depth) { | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1355 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1356 | assert(VWidth <= 64 && "Vector too wide to analyze!"); |
1357 | uint64_t EltMask = ~0ULL >> (64-VWidth); | ||||
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1358 | assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!"); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1359 | |
1360 | if (isa<UndefValue>(V)) { | ||||
1361 | // If the entire vector is undefined, just return this info. | ||||
1362 | UndefElts = EltMask; | ||||
1363 | return 0; | ||||
1364 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. | ||||
1365 | UndefElts = EltMask; | ||||
1366 | return UndefValue::get(V->getType()); | ||||
1367 | } | ||||
1368 | |||||
1369 | UndefElts = 0; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1370 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
1371 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1372 | Constant *Undef = UndefValue::get(EltTy); |
1373 | |||||
1374 | std::vector<Constant*> Elts; | ||||
1375 | for (unsigned i = 0; i != VWidth; ++i) | ||||
1376 | if (!(DemandedElts & (1ULL << i))) { // If not demanded, set to undef. | ||||
1377 | Elts.push_back(Undef); | ||||
1378 | UndefElts |= (1ULL << i); | ||||
1379 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. | ||||
1380 | Elts.push_back(Undef); | ||||
1381 | UndefElts |= (1ULL << i); | ||||
1382 | } else { // Otherwise, defined. | ||||
1383 | Elts.push_back(CP->getOperand(i)); | ||||
1384 | } | ||||
1385 | |||||
1386 | // If we changed the constant, return it. | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1387 | Constant *NewCP = ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1388 | return NewCP != CP ? NewCP : 0; |
1389 | } else if (isa<ConstantAggregateZero>(V)) { | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1390 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1391 | // set to undef. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1392 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1393 | Constant *Zero = Constant::getNullValue(EltTy); |
1394 | Constant *Undef = UndefValue::get(EltTy); | ||||
1395 | std::vector<Constant*> Elts; | ||||
1396 | for (unsigned i = 0; i != VWidth; ++i) | ||||
1397 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); | ||||
1398 | UndefElts = DemandedElts ^ EltMask; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1399 | return ConstantVector::get(Elts); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1400 | } |
1401 | |||||
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1402 | // Limit search depth. |
1403 | if (Depth == 10) | ||||
1404 | return false; | ||||
1405 | |||||
1406 | // If multiple users are using the root value, procede with | ||||
1407 | // simplification conservatively assuming that all elements | ||||
1408 | // are needed. | ||||
1409 | if (!V->hasOneUse()) { | ||||
1410 | // Quit if we find multiple users of a non-root value though. | ||||
1411 | // They'll be handled when it's their turn to be visited by | ||||
1412 | // the main instcombine process. | ||||
1413 | if (Depth != 0) | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1414 | // TODO: Just compute the UndefElts information recursively. |
1415 | return false; | ||||
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1416 | |
1417 | // Conservatively assume that all elements are needed. | ||||
1418 | DemandedElts = EltMask; | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1419 | } |
1420 | |||||
1421 | Instruction *I = dyn_cast<Instruction>(V); | ||||
1422 | if (!I) return false; // Only analyze instructions. | ||||
1423 | |||||
1424 | bool MadeChange = false; | ||||
1425 | uint64_t UndefElts2; | ||||
1426 | Value *TmpV; | ||||
1427 | switch (I->getOpcode()) { | ||||
1428 | default: break; | ||||
1429 | |||||
1430 | case Instruction::InsertElement: { | ||||
1431 | // If this is a variable index, we don't know which element it overwrites. | ||||
1432 | // demand exactly the same input as we produce. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1433 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1434 | if (Idx == 0) { |
1435 | // Note that we can't propagate undef elt info, because we don't know | ||||
1436 | // which elt is getting updated. | ||||
1437 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, | ||||
1438 | UndefElts2, Depth+1); | ||||
1439 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1440 | break; | ||||
1441 | } | ||||
1442 | |||||
1443 | // If this is inserting an element that isn't demanded, remove this | ||||
1444 | // insertelement. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1445 | unsigned IdxNo = Idx->getZExtValue(); |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1446 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) |
1447 | return AddSoonDeadInstToWorklist(*I, 0); | ||||
1448 | |||||
1449 | // Otherwise, the element inserted overwrites whatever was there, so the | ||||
1450 | // input demanded set is simpler than the output set. | ||||
1451 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), | ||||
1452 | DemandedElts & ~(1ULL << IdxNo), | ||||
1453 | UndefElts, Depth+1); | ||||
1454 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1455 | |||||
1456 | // The inserted element is defined. | ||||
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1457 | UndefElts &= ~(1ULL << IdxNo); |
1458 | break; | ||||
1459 | } | ||||
1460 | case Instruction::ShuffleVector: { | ||||
1461 | ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I); | ||||
1462 | uint64_t LeftDemanded = 0, RightDemanded = 0; | ||||
1463 | for (unsigned i = 0; i < VWidth; i++) { | ||||
1464 | if (DemandedElts & (1ULL << i)) { | ||||
1465 | unsigned MaskVal = Shuffle->getMaskValue(i); | ||||
1466 | if (MaskVal != -1u) { | ||||
1467 | assert(MaskVal < VWidth * 2 && | ||||
1468 | "shufflevector mask index out of range!"); | ||||
1469 | if (MaskVal < VWidth) | ||||
1470 | LeftDemanded |= 1ULL << MaskVal; | ||||
1471 | else | ||||
1472 | RightDemanded |= 1ULL << (MaskVal - VWidth); | ||||
1473 | } | ||||
1474 | } | ||||
1475 | } | ||||
1476 | |||||
1477 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded, | ||||
1478 | UndefElts2, Depth+1); | ||||
1479 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1480 | |||||
1481 | uint64_t UndefElts3; | ||||
1482 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded, | ||||
1483 | UndefElts3, Depth+1); | ||||
1484 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } | ||||
1485 | |||||
1486 | bool NewUndefElts = false; | ||||
1487 | for (unsigned i = 0; i < VWidth; i++) { | ||||
1488 | unsigned MaskVal = Shuffle->getMaskValue(i); | ||||
1489 | if (MaskVal == -1) { | ||||
1490 | uint64_t NewBit = 1ULL << i; | ||||
1491 | UndefElts |= NewBit; | ||||
1492 | } else if (MaskVal < VWidth) { | ||||
1493 | uint64_t NewBit = ((UndefElts2 >> MaskVal) & 1) << i; | ||||
1494 | NewUndefElts |= NewBit; | ||||
1495 | UndefElts |= NewBit; | ||||
1496 | } else { | ||||
1497 | uint64_t NewBit = ((UndefElts3 >> (MaskVal - VWidth)) & 1) << i; | ||||
1498 | NewUndefElts |= NewBit; | ||||
1499 | UndefElts |= NewBit; | ||||
1500 | } | ||||
1501 | } | ||||
1502 | |||||
1503 | if (NewUndefElts) { | ||||
1504 | // Add additional discovered undefs. | ||||
1505 | std::vector<Constant*> Elts; | ||||
1506 | for (unsigned i = 0; i < VWidth; ++i) { | ||||
1507 | if (UndefElts & (1ULL << i)) | ||||
1508 | Elts.push_back(UndefValue::get(Type::Int32Ty)); | ||||
1509 | else | ||||
1510 | Elts.push_back(ConstantInt::get(Type::Int32Ty, | ||||
1511 | Shuffle->getMaskValue(i))); | ||||
1512 | } | ||||
1513 | I->setOperand(2, ConstantVector::get(Elts)); | ||||
1514 | MadeChange = true; | ||||
1515 | } | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1516 | break; |
1517 | } | ||||
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1518 | case Instruction::BitCast: { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1519 | // Vector->vector casts only. |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1520 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
1521 | if (!VTy) break; | ||||
1522 | unsigned InVWidth = VTy->getNumElements(); | ||||
1523 | uint64_t InputDemandedElts = 0; | ||||
1524 | unsigned Ratio; | ||||
1525 | |||||
1526 | if (VWidth == InVWidth) { | ||||
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1527 | // 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] | 1528 | // elements as are demanded of us. |
1529 | Ratio = 1; | ||||
1530 | InputDemandedElts = DemandedElts; | ||||
1531 | } else if (VWidth > InVWidth) { | ||||
1532 | // Untested so far. | ||||
1533 | break; | ||||
1534 | |||||
1535 | // If there are more elements in the result than there are in the source, | ||||
1536 | // then an input element is live if any of the corresponding output | ||||
1537 | // elements are live. | ||||
1538 | Ratio = VWidth/InVWidth; | ||||
1539 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { | ||||
1540 | if (DemandedElts & (1ULL << OutIdx)) | ||||
1541 | InputDemandedElts |= 1ULL << (OutIdx/Ratio); | ||||
1542 | } | ||||
1543 | } else { | ||||
1544 | // Untested so far. | ||||
1545 | break; | ||||
1546 | |||||
1547 | // If there are more elements in the source than there are in the result, | ||||
1548 | // then an input element is live if the corresponding output element is | ||||
1549 | // live. | ||||
1550 | Ratio = InVWidth/VWidth; | ||||
1551 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) | ||||
1552 | if (DemandedElts & (1ULL << InIdx/Ratio)) | ||||
1553 | InputDemandedElts |= 1ULL << InIdx; | ||||
1554 | } | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1555 | |
Chris Lattner | 6987833 | 2007-04-14 22:29:23 +0000 | [diff] [blame] | 1556 | // div/rem demand all inputs, because they don't want divide by zero. |
1557 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, | ||||
1558 | UndefElts2, Depth+1); | ||||
1559 | if (TmpV) { | ||||
1560 | I->setOperand(0, TmpV); | ||||
1561 | MadeChange = true; | ||||
1562 | } | ||||
1563 | |||||
1564 | UndefElts = UndefElts2; | ||||
1565 | if (VWidth > InVWidth) { | ||||
1566 | assert(0 && "Unimp"); | ||||
1567 | // If there are more elements in the result than there are in the source, | ||||
1568 | // then an output element is undef if the corresponding input element is | ||||
1569 | // undef. | ||||
1570 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) | ||||
1571 | if (UndefElts2 & (1ULL << (OutIdx/Ratio))) | ||||
1572 | UndefElts |= 1ULL << OutIdx; | ||||
1573 | } else if (VWidth < InVWidth) { | ||||
1574 | assert(0 && "Unimp"); | ||||
1575 | // If there are more elements in the source than there are in the result, | ||||
1576 | // then a result element is undef if all of the corresponding input | ||||
1577 | // elements are undef. | ||||
1578 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. | ||||
1579 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) | ||||
1580 | if ((UndefElts2 & (1ULL << InIdx)) == 0) // Not undef? | ||||
1581 | UndefElts &= ~(1ULL << (InIdx/Ratio)); // Clear undef bit. | ||||
1582 | } | ||||
1583 | break; | ||||
1584 | } | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1585 | case Instruction::And: |
1586 | case Instruction::Or: | ||||
1587 | case Instruction::Xor: | ||||
1588 | case Instruction::Add: | ||||
1589 | case Instruction::Sub: | ||||
1590 | case Instruction::Mul: | ||||
1591 | // div/rem demand all inputs, because they don't want divide by zero. | ||||
1592 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, | ||||
1593 | UndefElts, Depth+1); | ||||
1594 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | ||||
1595 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, | ||||
1596 | UndefElts2, Depth+1); | ||||
1597 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } | ||||
1598 | |||||
1599 | // Output elements are undefined if both are undefined. Consider things | ||||
1600 | // like undef&0. The result is known zero, not undef. | ||||
1601 | UndefElts &= UndefElts2; | ||||
1602 | break; | ||||
1603 | |||||
1604 | case Instruction::Call: { | ||||
1605 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); | ||||
1606 | if (!II) break; | ||||
1607 | switch (II->getIntrinsicID()) { | ||||
1608 | default: break; | ||||
1609 | |||||
1610 | // Binary vector operations that work column-wise. A dest element is a | ||||
1611 | // function of the corresponding input elements from the two inputs. | ||||
1612 | case Intrinsic::x86_sse_sub_ss: | ||||
1613 | case Intrinsic::x86_sse_mul_ss: | ||||
1614 | case Intrinsic::x86_sse_min_ss: | ||||
1615 | case Intrinsic::x86_sse_max_ss: | ||||
1616 | case Intrinsic::x86_sse2_sub_sd: | ||||
1617 | case Intrinsic::x86_sse2_mul_sd: | ||||
1618 | case Intrinsic::x86_sse2_min_sd: | ||||
1619 | case Intrinsic::x86_sse2_max_sd: | ||||
1620 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, | ||||
1621 | UndefElts, Depth+1); | ||||
1622 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } | ||||
1623 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, | ||||
1624 | UndefElts2, Depth+1); | ||||
1625 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } | ||||
1626 | |||||
1627 | // If only the low elt is demanded and this is a scalarizable intrinsic, | ||||
1628 | // scalarize it now. | ||||
1629 | if (DemandedElts == 1) { | ||||
1630 | switch (II->getIntrinsicID()) { | ||||
1631 | default: break; | ||||
1632 | case Intrinsic::x86_sse_sub_ss: | ||||
1633 | case Intrinsic::x86_sse_mul_ss: | ||||
1634 | case Intrinsic::x86_sse2_sub_sd: | ||||
1635 | case Intrinsic::x86_sse2_mul_sd: | ||||
1636 | // TODO: Lower MIN/MAX/ABS/etc | ||||
1637 | Value *LHS = II->getOperand(1); | ||||
1638 | Value *RHS = II->getOperand(2); | ||||
1639 | // Extract the element as scalars. | ||||
1640 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); | ||||
1641 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); | ||||
1642 | |||||
1643 | switch (II->getIntrinsicID()) { | ||||
1644 | default: assert(0 && "Case stmts out of sync!"); | ||||
1645 | case Intrinsic::x86_sse_sub_ss: | ||||
1646 | case Intrinsic::x86_sse2_sub_sd: | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1647 | TmpV = InsertNewInstBefore(BinaryOperator::CreateSub(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1648 | II->getName()), *II); |
1649 | break; | ||||
1650 | case Intrinsic::x86_sse_mul_ss: | ||||
1651 | case Intrinsic::x86_sse2_mul_sd: | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1652 | TmpV = InsertNewInstBefore(BinaryOperator::CreateMul(LHS, RHS, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1653 | II->getName()), *II); |
1654 | break; | ||||
1655 | } | ||||
1656 | |||||
1657 | Instruction *New = | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1658 | InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, |
1659 | II->getName()); | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1660 | InsertNewInstBefore(New, *II); |
1661 | AddSoonDeadInstToWorklist(*II, 0); | ||||
1662 | return New; | ||||
1663 | } | ||||
1664 | } | ||||
1665 | |||||
1666 | // Output elements are undefined if both are undefined. Consider things | ||||
1667 | // like undef&0. The result is known zero, not undef. | ||||
1668 | UndefElts &= UndefElts2; | ||||
1669 | break; | ||||
1670 | } | ||||
1671 | break; | ||||
1672 | } | ||||
1673 | } | ||||
1674 | return MadeChange ? I : 0; | ||||
1675 | } | ||||
1676 | |||||
Dan Gohman | 45b4e48 | 2008-05-19 22:14:15 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1678 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
1679 | /// function is designed to check a chain of associative operators for a | ||||
1680 | /// potential to apply a certain optimization. Since the optimization may be | ||||
1681 | /// applicable if the expression was reassociated, this checks the chain, then | ||||
1682 | /// reassociates the expression as necessary to expose the optimization | ||||
1683 | /// opportunity. This makes use of a special Functor, which must define | ||||
1684 | /// 'shouldApply' and 'apply' methods. | ||||
1685 | /// | ||||
1686 | template<typename Functor> | ||||
Dan Gohman | 76d402b | 2008-05-20 01:14:05 +0000 | [diff] [blame] | 1687 | static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1688 | unsigned Opcode = Root.getOpcode(); |
1689 | Value *LHS = Root.getOperand(0); | ||||
1690 | |||||
1691 | // Quick check, see if the immediate LHS matches... | ||||
1692 | if (F.shouldApply(LHS)) | ||||
1693 | return F.apply(Root); | ||||
1694 | |||||
1695 | // Otherwise, if the LHS is not of the same opcode as the root, return. | ||||
1696 | Instruction *LHSI = dyn_cast<Instruction>(LHS); | ||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1697 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1698 | // Should we apply this transform to the RHS? |
1699 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); | ||||
1700 | |||||
1701 | // If not to the RHS, check to see if we should apply to the LHS... | ||||
1702 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { | ||||
1703 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS | ||||
1704 | ShouldApply = true; | ||||
1705 | } | ||||
1706 | |||||
1707 | // If the functor wants to apply the optimization to the RHS of LHSI, | ||||
1708 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) | ||||
1709 | if (ShouldApply) { | ||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1710 | // Now all of the instructions are in the current basic block, go ahead |
1711 | // and perform the reassociation. | ||||
1712 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); | ||||
1713 | |||||
1714 | // First move the selected RHS to the LHS of the root... | ||||
1715 | Root.setOperand(0, LHSI->getOperand(1)); | ||||
1716 | |||||
1717 | // Make what used to be the LHS of the root be the user of the root... | ||||
1718 | Value *ExtraOperand = TmpLHSI->getOperand(1); | ||||
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1719 | if (&Root == TmpLHSI) { |
Chris Lattner | 15a76c0 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1720 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); |
1721 | return 0; | ||||
1722 | } | ||||
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1723 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1724 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1725 | BasicBlock::iterator ARI = &Root; ++ARI; |
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1726 | TmpLHSI->moveBefore(ARI); // Move TmpLHSI to after Root |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1727 | ARI = Root; |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1728 | |
1729 | // Now propagate the ExtraOperand down the chain of instructions until we | ||||
1730 | // get to LHSI. | ||||
1731 | while (TmpLHSI != LHSI) { | ||||
1732 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); | ||||
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1733 | // Move the instruction to immediately before the chain we are |
1734 | // constructing to avoid breaking dominance properties. | ||||
Dan Gohman | d02d917 | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1735 | NextLHSI->moveBefore(ARI); |
Chris Lattner | 6572531 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1736 | ARI = NextLHSI; |
1737 | |||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1738 | Value *NextOp = NextLHSI->getOperand(1); |
1739 | NextLHSI->setOperand(1, ExtraOperand); | ||||
1740 | TmpLHSI = NextLHSI; | ||||
1741 | ExtraOperand = NextOp; | ||||
1742 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1743 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1744 | // Now that the instructions are reassociated, have the functor perform |
1745 | // the transformation... | ||||
1746 | return F.apply(Root); | ||||
1747 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1748 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1749 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
1750 | } | ||||
1751 | return 0; | ||||
1752 | } | ||||
1753 | |||||
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1754 | namespace { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1755 | |
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1756 | // AddRHS - Implements: X + X --> X << 1 |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1757 | struct AddRHS { |
1758 | Value *RHS; | ||||
1759 | AddRHS(Value *rhs) : RHS(rhs) {} | ||||
1760 | bool shouldApply(Value *LHS) const { return LHS == RHS; } | ||||
1761 | Instruction *apply(BinaryOperator &Add) const { | ||||
Nick Lewycky | 02d639f | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1762 | return BinaryOperator::CreateShl(Add.getOperand(0), |
1763 | ConstantInt::get(Add.getType(), 1)); | ||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1764 | } |
1765 | }; | ||||
1766 | |||||
1767 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) | ||||
1768 | // iff C1&C2 == 0 | ||||
1769 | struct AddMaskingAnd { | ||||
1770 | Constant *C2; | ||||
1771 | AddMaskingAnd(Constant *c) : C2(c) {} | ||||
1772 | bool shouldApply(Value *LHS) const { | ||||
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1773 | ConstantInt *C1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1774 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1775 | ConstantExpr::getAnd(C1, C2)->isNullValue(); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1776 | } |
1777 | Instruction *apply(BinaryOperator &Add) const { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1778 | return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1779 | } |
1780 | }; | ||||
1781 | |||||
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1782 | } |
1783 | |||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1784 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1785 | InstCombiner *IC) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1786 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1787 | if (Constant *SOC = dyn_cast<Constant>(SO)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1788 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1789 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1790 | return IC->InsertNewInstBefore(CastInst::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1791 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1792 | } |
1793 | |||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1794 | // Figure out if the constant is the left or the right argument. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1795 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
1796 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); | ||||
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1798 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
1799 | if (ConstIsRHS) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1800 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); |
1801 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); | ||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1802 | } |
1803 | |||||
1804 | Value *Op0 = SO, *Op1 = ConstOperand; | ||||
1805 | if (!ConstIsRHS) | ||||
1806 | std::swap(Op0, Op1); | ||||
1807 | Instruction *New; | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1808 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1809 | New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1810 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1811 | New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1812 | SO->getName()+".cmp"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1813 | else { |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1814 | assert(0 && "Unknown binary instruction type!"); |
Chris Lattner | 326c0f3 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1815 | abort(); |
1816 | } | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1817 | return IC->InsertNewInstBefore(New, I); |
1818 | } | ||||
1819 | |||||
1820 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a | ||||
1821 | // constant as the other operand, try to fold the binary operator into the | ||||
1822 | // select arguments. This also works for Cast instructions, which obviously do | ||||
1823 | // not have a second operand. | ||||
1824 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, | ||||
1825 | InstCombiner *IC) { | ||||
1826 | // Don't modify shared select instructions | ||||
1827 | if (!SI->hasOneUse()) return 0; | ||||
1828 | Value *TV = SI->getOperand(1); | ||||
1829 | Value *FV = SI->getOperand(2); | ||||
1830 | |||||
1831 | if (isa<Constant>(TV) || isa<Constant>(FV)) { | ||||
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1832 | // Bool selects with constant operands can be folded to logical ops. |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1833 | if (SI->getType() == Type::Int1Ty) return 0; |
Chris Lattner | 956db27 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1834 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1835 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
1836 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); | ||||
1837 | |||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1838 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
1839 | SelectFalseVal); | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1840 | } |
1841 | return 0; | ||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1842 | } |
1843 | |||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1844 | |
1845 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI | ||||
1846 | /// node as operand #0, see if we can fold the instruction into the PHI (which | ||||
1847 | /// is only possible if all operands to the PHI are constants). | ||||
1848 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { | ||||
1849 | PHINode *PN = cast<PHINode>(I.getOperand(0)); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1850 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1851 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1853 | // Check to see if all of the operands of the PHI are constants. If there is |
1854 | // 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] | 1855 | // or if *it* is a PHI, bail out. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1856 | BasicBlock *NonConstBB = 0; |
1857 | for (unsigned i = 0; i != NumPHIValues; ++i) | ||||
1858 | if (!isa<Constant>(PN->getIncomingValue(i))) { | ||||
1859 | if (NonConstBB) return 0; // More than one non-const value. | ||||
Chris Lattner | b303668 | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1860 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1861 | NonConstBB = PN->getIncomingBlock(i); |
1862 | |||||
1863 | // If the incoming non-constant value is in I's block, we have an infinite | ||||
1864 | // loop. | ||||
1865 | if (NonConstBB == I.getParent()) | ||||
1866 | return 0; | ||||
1867 | } | ||||
1868 | |||||
1869 | // If there is exactly one non-constant value, we can insert a copy of the | ||||
1870 | // operation in that block. However, if this is a critical edge, we would be | ||||
1871 | // inserting the computation one some other paths (e.g. inside a loop). Only | ||||
1872 | // do this if the pred block is unconditionally branching into the phi block. | ||||
1873 | if (NonConstBB) { | ||||
1874 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); | ||||
1875 | if (!BI || !BI->isUnconditional()) return 0; | ||||
1876 | } | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1877 | |
1878 | // Okay, we can do the transformation: create the new PHI node. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1879 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1880 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1881 | InsertNewInstBefore(NewPN, *PN); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1882 | NewPN->takeName(PN); |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1883 | |
1884 | // Next, add all of the operands to the PHI. | ||||
1885 | if (I.getNumOperands() == 2) { | ||||
1886 | Constant *C = cast<Constant>(I.getOperand(1)); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1887 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1888 | Value *InV = 0; |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1889 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1890 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
1891 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); | ||||
1892 | else | ||||
1893 | InV = ConstantExpr::get(I.getOpcode(), InC, C); | ||||
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1894 | } else { |
1895 | assert(PN->getIncomingBlock(i) == NonConstBB); | ||||
1896 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1897 | InV = BinaryOperator::Create(BO->getOpcode(), |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1898 | PN->getIncomingValue(i), C, "phitmp", |
1899 | NonConstBB->getTerminator()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1900 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1901 | InV = CmpInst::Create(CI->getOpcode(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1902 | CI->getPredicate(), |
1903 | PN->getIncomingValue(i), C, "phitmp", | ||||
1904 | NonConstBB->getTerminator()); | ||||
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1905 | else |
1906 | assert(0 && "Unknown binop!"); | ||||
1907 | |||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1908 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1909 | } |
1910 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1911 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1912 | } else { |
1913 | CastInst *CI = cast<CastInst>(&I); | ||||
1914 | const Type *RetTy = CI->getType(); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1915 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1916 | Value *InV; |
1917 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1918 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1919 | } else { |
1920 | assert(PN->getIncomingBlock(i) == NonConstBB); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1921 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1922 | I.getType(), "phitmp", |
1923 | NonConstBB->getTerminator()); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1924 | AddToWorkList(cast<Instruction>(InV)); |
Chris Lattner | 2a86f3b | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1925 | } |
1926 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1927 | } |
1928 | } | ||||
1929 | return ReplaceInstUsesWith(I, NewPN); | ||||
1930 | } | ||||
1931 | |||||
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 1932 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 1933 | /// WillNotOverflowSignedAdd - Return true if we can prove that: |
1934 | /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) | ||||
1935 | /// This basically requires proving that the add in the original type would not | ||||
1936 | /// overflow to change the sign bit or have a carry out. | ||||
1937 | bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { | ||||
1938 | // There are different heuristics we can use for this. Here are some simple | ||||
1939 | // ones. | ||||
1940 | |||||
1941 | // Add has the property that adding any two 2's complement numbers can only | ||||
1942 | // have one carry bit which can change a sign. As such, if LHS and RHS each | ||||
1943 | // have at least two sign bits, we know that the addition of the two values will | ||||
1944 | // sign extend fine. | ||||
1945 | if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) | ||||
1946 | return true; | ||||
1947 | |||||
1948 | |||||
1949 | // If one of the operands only has one non-zero bit, and if the other operand | ||||
1950 | // has a known-zero bit in a more significant place than it (not including the | ||||
1951 | // sign bit) the ripple may go up to and fill the zero, but won't change the | ||||
1952 | // sign. For example, (X & ~4) + 1. | ||||
1953 | |||||
1954 | // TODO: Implement. | ||||
1955 | |||||
1956 | return false; | ||||
1957 | } | ||||
1958 | |||||
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1960 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1961 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1962 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1964 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1965 | // X + undef -> undef |
1966 | if (isa<UndefValue>(RHS)) | ||||
1967 | return ReplaceInstUsesWith(I, RHS); | ||||
1968 | |||||
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1969 | // X + 0 --> X |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1970 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1971 | if (RHSC->isNullValue()) |
1972 | return ReplaceInstUsesWith(I, LHS); | ||||
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1973 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1974 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero |
1975 | (I.getType())->getValueAPF())) | ||||
Chris Lattner | 8532cf6 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1976 | return ReplaceInstUsesWith(I, LHS); |
Chris Lattner | 5e678e0 | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1977 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1979 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1980 | // X + (signbit) --> X ^ signbit |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1981 | const APInt& Val = CI->getValue(); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1982 | uint32_t BitWidth = Val.getBitWidth(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1983 | if (Val == APInt::getSignBit(BitWidth)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1984 | return BinaryOperator::CreateXor(LHS, RHS); |
Chris Lattner | b4a2f05 | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1985 | |
1986 | // See if SimplifyDemandedBits can simplify this. This handles stuff like | ||||
1987 | // (X & 254)+1 -> (X&254)|1 | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1988 | if (!isa<VectorType>(I.getType())) { |
1989 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
1990 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
1991 | KnownZero, KnownOne)) | ||||
1992 | return &I; | ||||
1993 | } | ||||
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1994 | } |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1995 | |
1996 | if (isa<PHINode>(LHS)) | ||||
1997 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
1998 | return NV; | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2000 | ConstantInt *XorRHS = 0; |
2001 | Value *XorLHS = 0; | ||||
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 2002 | if (isa<ConstantInt>(RHSC) && |
2003 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2004 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2005 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2006 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2007 | uint32_t Size = TySizeBits / 2; |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2008 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
2009 | APInt CFF80Val(-C0080Val); | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2010 | do { |
2011 | if (TySizeBits > Size) { | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2012 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
2013 | // 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] | 2014 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
2015 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2016 | // This is a sign extend if the top bits are known zero. |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2017 | if (!MaskedValueIsZero(XorLHS, |
2018 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2019 | 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] | 2020 | break; |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2021 | } |
2022 | } | ||||
2023 | Size >>= 1; | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2024 | C0080Val = APIntOps::lshr(C0080Val, Size); |
2025 | CFF80Val = APIntOps::ashr(CFF80Val, Size); | ||||
2026 | } while (Size >= 1); | ||||
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2027 | |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2028 | // FIXME: This shouldn't be necessary. When the backends can handle types |
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 2029 | // with funny bit widths then this switch statement should be removed. It |
2030 | // is just here to get the size of the "middle" type back up to something | ||||
2031 | // that the back ends can handle. | ||||
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2032 | const Type *MiddleType = 0; |
2033 | switch (Size) { | ||||
2034 | default: break; | ||||
2035 | case 32: MiddleType = Type::Int32Ty; break; | ||||
2036 | case 16: MiddleType = Type::Int16Ty; break; | ||||
2037 | case 8: MiddleType = Type::Int8Ty; break; | ||||
2038 | } | ||||
2039 | if (MiddleType) { | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 2040 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2041 | InsertNewInstBefore(NewTrunc, I); |
Reid Spencer | 35c3885 | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 2042 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
Chris Lattner | 5931c54 | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 2043 | } |
2044 | } | ||||
Chris Lattner | 66331a4 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 2045 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2046 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2047 | if (I.getType() == Type::Int1Ty) |
2048 | return BinaryOperator::CreateXor(LHS, RHS); | ||||
2049 | |||||
Nick Lewycky | 7d26bd8 | 2008-05-23 04:39:38 +0000 | [diff] [blame] | 2050 | // X + X --> X << 1 |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2051 | if (I.getType()->isInteger()) { |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2052 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2053 | |
2054 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { | ||||
2055 | if (RHSI->getOpcode() == Instruction::Sub) | ||||
2056 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B | ||||
2057 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); | ||||
2058 | } | ||||
2059 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { | ||||
2060 | if (LHSI->getOpcode() == Instruction::Sub) | ||||
2061 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B | ||||
2062 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); | ||||
2063 | } | ||||
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2064 | } |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | 5c4afb9 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 2066 | // -A + B --> B - A |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2067 | // -A + -B --> -(A + B) |
2068 | if (Value *LHSV = dyn_castNegVal(LHS)) { | ||||
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2069 | if (LHS->getType()->isIntOrIntVector()) { |
2070 | if (Value *RHSV = dyn_castNegVal(RHS)) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2071 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum"); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2072 | InsertNewInstBefore(NewAdd, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2073 | return BinaryOperator::CreateNeg(NewAdd); |
Chris Lattner | e10c0b9 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2074 | } |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2075 | } |
2076 | |||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2077 | return BinaryOperator::CreateSub(RHS, LHSV); |
Chris Lattner | dd12f96 | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2078 | } |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2079 | |
2080 | // A + -B --> A - B | ||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2081 | if (!isa<Constant>(RHS)) |
2082 | if (Value *V = dyn_castNegVal(RHS)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2083 | return BinaryOperator::CreateSub(LHS, V); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2084 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2085 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2086 | ConstantInt *C2; |
2087 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { | ||||
2088 | if (X == RHS) // X*C + X --> X * (C+1) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2089 | return BinaryOperator::CreateMul(RHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2090 | |
2091 | // X*C1 + X*C2 --> X * (C1+C2) | ||||
2092 | ConstantInt *C1; | ||||
2093 | if (X == dyn_castFoldableMul(RHS, C1)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2094 | return BinaryOperator::CreateMul(X, Add(C1, C2)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2095 | } |
2096 | |||||
2097 | // X + X*C --> X * (C+1) | ||||
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2098 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2099 | return BinaryOperator::CreateMul(LHS, AddOne(C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2100 | |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2101 | // X + ~X --> -1 since ~X = -X-1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 2102 | if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) |
2103 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); | ||||
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | 564a727 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 2106 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2107 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) |
Chris Lattner | e617c9e | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 2108 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) |
2109 | return R; | ||||
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2110 | |
2111 | // A+B --> A|B iff A and B have no bits set in common. | ||||
2112 | if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { | ||||
2113 | APInt Mask = APInt::getAllOnesValue(IT->getBitWidth()); | ||||
2114 | APInt LHSKnownOne(IT->getBitWidth(), 0); | ||||
2115 | APInt LHSKnownZero(IT->getBitWidth(), 0); | ||||
2116 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); | ||||
2117 | if (LHSKnownZero != 0) { | ||||
2118 | APInt RHSKnownOne(IT->getBitWidth(), 0); | ||||
2119 | APInt RHSKnownZero(IT->getBitWidth(), 0); | ||||
2120 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); | ||||
2121 | |||||
2122 | // No bits in common -> bitwise or. | ||||
Chris Lattner | 9d60ba9 | 2008-05-19 20:03:53 +0000 | [diff] [blame] | 2123 | if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2124 | return BinaryOperator::CreateOr(LHS, RHS); |
Chris Lattner | 5e0d718 | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2125 | } |
2126 | } | ||||
Chris Lattner | c8802d2 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 2127 | |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2128 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Nick Lewycky | 0c2c3f6 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2129 | if (I.getType()->isIntOrIntVector()) { |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2130 | Value *W, *X, *Y, *Z; |
2131 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && | ||||
2132 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { | ||||
2133 | if (W != Y) { | ||||
2134 | if (W == Z) { | ||||
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2135 | std::swap(Y, Z); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2136 | } else if (Y == X) { |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2137 | std::swap(W, X); |
2138 | } else if (X == Z) { | ||||
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2139 | std::swap(Y, Z); |
2140 | std::swap(W, X); | ||||
2141 | } | ||||
2142 | } | ||||
2143 | |||||
2144 | if (W == Y) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2145 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z, |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2146 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2147 | return BinaryOperator::CreateMul(W, NewAdd); |
Nick Lewycky | b6eabff | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2148 | } |
2149 | } | ||||
2150 | } | ||||
2151 | |||||
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2152 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 2153 | Value *X = 0; |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2154 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2155 | return BinaryOperator::CreateSub(SubOne(CRHS), X); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2156 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2157 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
2158 | 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] | 2159 | Constant *Anded = And(CRHS, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2160 | if (Anded == CRHS) { |
2161 | // See if all bits from the first bit set in the Add RHS up are included | ||||
2162 | // in the mask. First, get the rightmost bit. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2163 | const APInt& AddRHSV = CRHS->getValue(); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2164 | |
2165 | // 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] | 2166 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2167 | |
2168 | // See if the and mask includes all of these bits. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2169 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2170 | |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2171 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
2172 | // Okay, the xform is safe. Insert the new add pronto. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2173 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS, |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2174 | LHS->getName()), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2175 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | b99d6b1 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2176 | } |
2177 | } | ||||
2178 | } | ||||
2179 | |||||
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2180 | // Try to fold constant add into select arguments. |
2181 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2182 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2183 | return R; |
Chris Lattner | 6b03205 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2184 | } |
2185 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2186 | // add (cast *A to intptrtype) B -> |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2187 | // cast (GEP (cast *A to sbyte*) B) --> intptrtype |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2188 | { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2189 | CastInst *CI = dyn_cast<CastInst>(LHS); |
2190 | Value *Other = RHS; | ||||
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2191 | if (!CI) { |
2192 | CI = dyn_cast<CastInst>(RHS); | ||||
2193 | Other = LHS; | ||||
2194 | } | ||||
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2195 | if (CI && CI->getType()->isSized() && |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2196 | (CI->getType()->getPrimitiveSizeInBits() == |
2197 | TD->getIntPtrType()->getPrimitiveSizeInBits()) | ||||
Andrew Lenharth | 4563326 | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2198 | && isa<PointerType>(CI->getOperand(0)->getType())) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 2199 | unsigned AS = |
2200 | cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 2201 | Value *I2 = InsertBitCastBefore(CI->getOperand(0), |
2202 | PointerType::get(Type::Int8Ty, AS), I); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2203 | I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2204 | return new PtrToIntInst(I2, CI->getType()); |
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2205 | } |
2206 | } | ||||
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2207 | |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2208 | // add (select X 0 (sub n A)) A --> select X A n |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2209 | { |
2210 | SelectInst *SI = dyn_cast<SelectInst>(LHS); | ||||
2211 | Value *Other = RHS; | ||||
2212 | if (!SI) { | ||||
2213 | SI = dyn_cast<SelectInst>(RHS); | ||||
2214 | Other = LHS; | ||||
2215 | } | ||||
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2216 | if (SI && SI->hasOneUse()) { |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2217 | Value *TV = SI->getTrueValue(); |
2218 | Value *FV = SI->getFalseValue(); | ||||
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2219 | Value *A, *N; |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2220 | |
2221 | // Can we fold the add into the argument of the select? | ||||
2222 | // We check both true and false select arguments for a matching subtract. | ||||
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2223 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Value(A))) && |
2224 | A == Other) // Fold the add into the true select value. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2225 | return SelectInst::Create(SI->getCondition(), N, A); |
Chris Lattner | 4279048 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2226 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Value(A))) && |
2227 | A == Other) // Fold the add into the false select value. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2228 | return SelectInst::Create(SI->getCondition(), A, N); |
Christopher Lamb | 30f017a | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2229 | } |
2230 | } | ||||
Chris Lattner | 2454a2e | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2231 | |
2232 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. | ||||
2233 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) | ||||
2234 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) | ||||
2235 | return ReplaceInstUsesWith(I, LHS); | ||||
Andrew Lenharth | 16d7955 | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | 3d28b1b | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2237 | // Check for (add (sext x), y), see if we can merge this into an |
2238 | // integer add followed by a sext. | ||||
2239 | if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) { | ||||
2240 | // (add (sext x), cst) --> (sext (add x, cst')) | ||||
2241 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { | ||||
2242 | Constant *CI = | ||||
2243 | ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); | ||||
2244 | if (LHSConv->hasOneUse() && | ||||
2245 | ConstantExpr::getSExt(CI, I.getType()) == RHSC && | ||||
2246 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { | ||||
2247 | // Insert the new, smaller add. | ||||
2248 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), | ||||
2249 | CI, "addconv"); | ||||
2250 | InsertNewInstBefore(NewAdd, I); | ||||
2251 | return new SExtInst(NewAdd, I.getType()); | ||||
2252 | } | ||||
2253 | } | ||||
2254 | |||||
2255 | // (add (sext x), (sext y)) --> (sext (add int x, y)) | ||||
2256 | if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) { | ||||
2257 | // Only do this if x/y have the same type, if at last one of them has a | ||||
2258 | // single use (so we don't increase the number of sexts), and if the | ||||
2259 | // integer add will not overflow. | ||||
2260 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& | ||||
2261 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && | ||||
2262 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), | ||||
2263 | RHSConv->getOperand(0))) { | ||||
2264 | // Insert the new integer add. | ||||
2265 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), | ||||
2266 | RHSConv->getOperand(0), | ||||
2267 | "addconv"); | ||||
2268 | InsertNewInstBefore(NewAdd, I); | ||||
2269 | return new SExtInst(NewAdd, I.getType()); | ||||
2270 | } | ||||
2271 | } | ||||
2272 | } | ||||
2273 | |||||
2274 | // Check for (add double (sitofp x), y), see if we can merge this into an | ||||
2275 | // integer add followed by a promotion. | ||||
2276 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { | ||||
2277 | // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) | ||||
2278 | // ... if the constant fits in the integer value. This is useful for things | ||||
2279 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer | ||||
2280 | // requires a constant pool load, and generally allows the add to be better | ||||
2281 | // instcombined. | ||||
2282 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { | ||||
2283 | Constant *CI = | ||||
2284 | ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); | ||||
2285 | if (LHSConv->hasOneUse() && | ||||
2286 | ConstantExpr::getSIToFP(CI, I.getType()) == CFP && | ||||
2287 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { | ||||
2288 | // Insert the new integer add. | ||||
2289 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), | ||||
2290 | CI, "addconv"); | ||||
2291 | InsertNewInstBefore(NewAdd, I); | ||||
2292 | return new SIToFPInst(NewAdd, I.getType()); | ||||
2293 | } | ||||
2294 | } | ||||
2295 | |||||
2296 | // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) | ||||
2297 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { | ||||
2298 | // Only do this if x/y have the same type, if at last one of them has a | ||||
2299 | // single use (so we don't increase the number of int->fp conversions), | ||||
2300 | // and if the integer add will not overflow. | ||||
2301 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& | ||||
2302 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && | ||||
2303 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), | ||||
2304 | RHSConv->getOperand(0))) { | ||||
2305 | // Insert the new integer add. | ||||
2306 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), | ||||
2307 | RHSConv->getOperand(0), | ||||
2308 | "addconv"); | ||||
2309 | InsertNewInstBefore(NewAdd, I); | ||||
2310 | return new SIToFPInst(NewAdd, I.getType()); | ||||
2311 | } | ||||
2312 | } | ||||
2313 | } | ||||
2314 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2315 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2316 | } |
2317 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2318 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2319 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2320 | |
Chris Lattner | d137ab4 | 2008-07-17 06:07:20 +0000 | [diff] [blame] | 2321 | if (Op0 == Op1 && // sub X, X -> 0 |
2322 | !I.getType()->isFPOrFPVector()) | ||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2323 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2325 | // If this is a 'B = x-(-A)', change to B = x+A... |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2326 | if (Value *V = dyn_castNegVal(Op1)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2327 | return BinaryOperator::CreateAdd(Op0, V); |
Chris Lattner | b35dde1 | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2328 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2329 | if (isa<UndefValue>(Op0)) |
2330 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef | ||||
2331 | if (isa<UndefValue>(Op1)) | ||||
2332 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef | ||||
2333 | |||||
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2334 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
2335 | // Replace (-1 - A) with (~A)... | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2336 | if (C->isAllOnesValue()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2337 | return BinaryOperator::CreateNot(Op1); |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2339 | // C - ~X == X + (1+C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2340 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2341 | if (match(Op1, m_Not(m_Value(X)))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2342 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2344 | // -(X >>u 31) -> (X >>s 31) |
2345 | // -(X >>s 31) -> (X >>u 31) | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2346 | if (C->isZero()) { |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2347 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2348 | if (SI->getOpcode() == Instruction::LShr) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2349 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2350 | // 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] | 2351 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2352 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2353 | // Ok, the transformation is safe. Insert AShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2354 | return BinaryOperator::Create(Instruction::AShr, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2355 | SI->getOperand(0), CU, SI->getName()); |
Chris Lattner | 9c29067 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2356 | } |
2357 | } | ||||
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2358 | } |
2359 | else if (SI->getOpcode() == Instruction::AShr) { | ||||
2360 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { | ||||
2361 | // 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] | 2362 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2363 | SI->getType()->getPrimitiveSizeInBits()-1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2364 | // Ok, the transformation is safe. Insert LShr. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2365 | return BinaryOperator::CreateLShr( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2366 | SI->getOperand(0), CU, SI->getName()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2367 | } |
2368 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2369 | } |
2370 | } | ||||
Chris Lattner | bfe492b | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2371 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2372 | |
2373 | // Try to fold constant sub into select arguments. | ||||
2374 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2375 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2376 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2377 | |
2378 | if (isa<PHINode>(Op0)) | ||||
2379 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2380 | return NV; | ||||
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2381 | } |
2382 | |||||
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2383 | if (I.getType() == Type::Int1Ty) |
2384 | return BinaryOperator::CreateXor(Op0, Op1); | ||||
2385 | |||||
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2386 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
2387 | if (Op1I->getOpcode() == Instruction::Add && | ||||
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2388 | !Op0->getType()->isFPOrFPVector()) { |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2389 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2390 | return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2391 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2392 | return BinaryOperator::CreateNeg(Op1I->getOperand(0), I.getName()); |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2393 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
2394 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) | ||||
2395 | // C1-(X+C2) --> (C1-C2)-X | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2396 | return BinaryOperator::CreateSub(Subtract(CI1, CI2), |
Chris Lattner | 08954a2 | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2397 | Op1I->getOperand(0)); |
2398 | } | ||||
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2399 | } |
2400 | |||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2401 | if (Op1I->hasOneUse()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2402 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
2403 | // is not used by anyone else... | ||||
2404 | // | ||||
Chris Lattner | 0517e72 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2405 | if (Op1I->getOpcode() == Instruction::Sub && |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2406 | !Op1I->getType()->isFPOrFPVector()) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2407 | // Swap the two operands of the subexpr... |
2408 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); | ||||
2409 | Op1I->setOperand(0, IIOp1); | ||||
2410 | Op1I->setOperand(1, IIOp0); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2411 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2412 | // Create the new top level add instruction... |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2413 | return BinaryOperator::CreateAdd(Op0, Op1); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2414 | } |
2415 | |||||
2416 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... | ||||
2417 | // | ||||
2418 | if (Op1I->getOpcode() == Instruction::And && | ||||
2419 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { | ||||
2420 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); | ||||
2421 | |||||
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2422 | Value *NewNot = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2423 | InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I); |
2424 | return BinaryOperator::CreateAnd(Op0, NewNot); | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2425 | } |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2426 | |
Reid Spencer | ac5209e | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2427 | // 0 - (X sdiv C) -> (X sdiv -C) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2428 | if (Op1I->getOpcode() == Instruction::SDiv) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2429 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2430 | if (CSI->isZero()) |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2431 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2432 | return BinaryOperator::CreateSDiv(Op1I->getOperand(0), |
Chris Lattner | 91ccc15 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2433 | ConstantExpr::getNeg(DivRHS)); |
2434 | |||||
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2435 | // X - X*C --> X * (1-C) |
Reid Spencer | 4b828e6 | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2436 | ConstantInt *C2 = 0; |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2437 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2438 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2439 | return BinaryOperator::CreateMul(Op0, CP1); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2440 | } |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2441 | |
2442 | // X - ((X / Y) * Y) --> X % Y | ||||
2443 | if (Op1I->getOpcode() == Instruction::Mul) | ||||
2444 | if (Instruction *I = dyn_cast<Instruction>(Op1I->getOperand(0))) | ||||
2445 | if (Op0 == I->getOperand(0) && | ||||
2446 | Op1I->getOperand(1) == I->getOperand(1)) { | ||||
2447 | if (I->getOpcode() == Instruction::SDiv) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2448 | return BinaryOperator::CreateSRem(Op0, Op1I->getOperand(1)); |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2449 | if (I->getOpcode() == Instruction::UDiv) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2450 | return BinaryOperator::CreateURem(Op0, Op1I->getOperand(1)); |
Dan Gohman | 5d066ff | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 2451 | } |
Chris Lattner | 4037171 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2452 | } |
Chris Lattner | 43d84d6 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2453 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 9919e3d | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2455 | if (!Op0->getType()->isFPOrFPVector()) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2456 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2457 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2458 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
2459 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); | ||||
2460 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X | ||||
2461 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); | ||||
Chris Lattner | 7edc8c2 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2462 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
2463 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2464 | return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2465 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2466 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2467 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2468 | ConstantInt *C1; |
2469 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2470 | if (X == Op1) // X*C - X --> X * (C-1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2471 | return BinaryOperator::CreateMul(Op1, SubOne(C1)); |
Chris Lattner | ad3448c | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2472 | |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2473 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
2474 | if (X == dyn_castFoldableMul(Op1, C2)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2475 | return BinaryOperator::CreateMul(X, Subtract(C1, C2)); |
Chris Lattner | 50af16a | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2476 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2477 | return 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2478 | } |
2479 | |||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2480 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
2481 | /// comparison only checks the sign bit. If it only checks the sign bit, set | ||||
2482 | /// TrueIfSigned if the result of the comparison is true when the input value is | ||||
2483 | /// signed. | ||||
2484 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, | ||||
2485 | bool &TrueIfSigned) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2486 | switch (pred) { |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2487 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
2488 | TrueIfSigned = true; | ||||
2489 | return RHS->isZero(); | ||||
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2490 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
2491 | TrueIfSigned = true; | ||||
2492 | return RHS->isAllOnesValue(); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2493 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
2494 | TrueIfSigned = false; | ||||
2495 | return RHS->isAllOnesValue(); | ||||
Chris Lattner | cb7122b | 2007-07-16 04:15:34 +0000 | [diff] [blame] | 2496 | case ICmpInst::ICMP_UGT: |
2497 | // True if LHS u> RHS and RHS == high-bit-mask - 1 | ||||
2498 | TrueIfSigned = true; | ||||
2499 | return RHS->getValue() == | ||||
2500 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); | ||||
2501 | case ICmpInst::ICMP_UGE: | ||||
2502 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) | ||||
2503 | TrueIfSigned = true; | ||||
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2504 | return RHS->getValue().isSignBit(); |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2505 | default: |
2506 | return false; | ||||
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2507 | } |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2508 | } |
2509 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2510 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2511 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2512 | Value *Op0 = I.getOperand(0); |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2514 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
2515 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
2516 | |||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2517 | // Simplify mul instructions with a constant RHS... |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2518 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
2519 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { | ||||
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2520 | |
2521 | // ((X << C1)*C2) == (X * (C2 << C1)) | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2522 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
Chris Lattner | e92d2f4 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2523 | if (SI->getOpcode() == Instruction::Shl) |
2524 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2525 | return BinaryOperator::CreateMul(SI->getOperand(0), |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2526 | ConstantExpr::getShl(CI, ShOp)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2527 | |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 2528 | if (CI->isZero()) |
Chris Lattner | 515c97c | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2529 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
2530 | if (CI->equalsInt(1)) // X * 1 == X | ||||
2531 | return ReplaceInstUsesWith(I, Op0); | ||||
2532 | if (CI->isAllOnesValue()) // X * -1 == 0 - X | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2533 | return BinaryOperator::CreateNeg(Op0, I.getName()); |
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2534 | |
Zhou Sheng | 97b52c2 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2535 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2536 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2537 | return BinaryOperator::CreateShl(Op0, |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2538 | ConstantInt::get(Op0->getType(), Val.logBase2())); |
Chris Lattner | bcd7db5 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2539 | } |
Robert Bocchino | 7169828 | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2540 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2541 | if (Op1F->isNullValue()) |
2542 | return ReplaceInstUsesWith(I, Op1); | ||||
Chris Lattner | 6c1ce21 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2543 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2544 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
2545 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) | ||||
Chris Lattner | b8cd4d3 | 2008-08-11 22:06:05 +0000 | [diff] [blame] | 2546 | if (Op1F->isExactlyValue(1.0)) |
2547 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' | ||||
2548 | } else if (isa<VectorType>(Op1->getType())) { | ||||
2549 | if (isa<ConstantAggregateZero>(Op1)) | ||||
2550 | return ReplaceInstUsesWith(I, Op1); | ||||
2551 | |||||
2552 | // As above, vector X*splat(1.0) -> X in all defined cases. | ||||
2553 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) | ||||
2554 | if (ConstantFP *F = dyn_cast_or_null<ConstantFP>(Op1V->getSplatValue())) | ||||
2555 | if (F->isExactlyValue(1.0)) | ||||
2556 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2557 | } |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2558 | |
2559 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) | ||||
2560 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && | ||||
Chris Lattner | 47c9909 | 2008-05-18 04:11:26 +0000 | [diff] [blame] | 2561 | isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1)) { |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2562 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2563 | Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2564 | Op1, "tmp"); |
2565 | InsertNewInstBefore(Add, I); | ||||
2566 | Value *C1C2 = ConstantExpr::getMul(Op1, | ||||
2567 | cast<Constant>(Op0I->getOperand(1))); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2568 | return BinaryOperator::CreateAdd(Add, C1C2); |
Chris Lattner | ab51f3f | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2569 | |
2570 | } | ||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2571 | |
2572 | // Try to fold constant mul into select arguments. | ||||
2573 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2574 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2575 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2576 | |
2577 | if (isa<PHINode>(Op0)) | ||||
2578 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2579 | return NV; | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2580 | } |
2581 | |||||
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2582 | if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y |
2583 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2584 | return BinaryOperator::CreateMul(Op0v, Op1v); |
Chris Lattner | a4f445b | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2585 | |
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2586 | if (I.getType() == Type::Int1Ty) |
2587 | return BinaryOperator::CreateAnd(Op0, I.getOperand(1)); | ||||
2588 | |||||
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2589 | // If one of the operands of the multiply is a cast from a boolean value, then |
2590 | // we know the bool is either zero or one, so this is a 'masking' multiply. | ||||
2591 | // See if we can simplify things based on how the boolean was originally | ||||
2592 | // formed. | ||||
2593 | CastInst *BoolCast = 0; | ||||
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2594 | if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0)) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2595 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2596 | BoolCast = CI; |
2597 | if (!BoolCast) | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2598 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2599 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2600 | BoolCast = CI; |
2601 | if (BoolCast) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2602 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2603 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
2604 | const Type *SCOpTy = SCIOp0->getType(); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2605 | bool TIS = false; |
2606 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2607 | // 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] | 2608 | // multiply into a shift/and combination. |
2609 | if (isa<ConstantInt>(SCIOp1) && | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 2610 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
2611 | TIS) { | ||||
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2612 | // Shift the X value right to turn it into "all signbits". |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2613 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2614 | SCOpTy->getPrimitiveSizeInBits()-1); |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2615 | Value *V = |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2616 | InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2617 | BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt, |
Chris Lattner | 4cb170c | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2618 | BoolCast->getOperand(0)->getName()+ |
2619 | ".mask"), I); | ||||
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2620 | |
2621 | // If the multiply type is not the same as the source type, sign extend | ||||
2622 | // or truncate to the multiply type. | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2623 | if (I.getType() != V->getType()) { |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2624 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
2625 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2626 | Instruction::CastOps opcode = |
2627 | (SrcBits == DstBits ? Instruction::BitCast : | ||||
2628 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); | ||||
2629 | V = InsertCastBefore(opcode, V, I.getType(), I); | ||||
2630 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2632 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2633 | return BinaryOperator::CreateAnd(V, OtherOp); |
Chris Lattner | fb54b2b | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2634 | } |
2635 | } | ||||
2636 | } | ||||
2637 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2638 | return Changed ? &I : 0; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2639 | } |
2640 | |||||
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2641 | /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select |
2642 | /// instruction. | ||||
2643 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { | ||||
2644 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); | ||||
2645 | |||||
2646 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y | ||||
2647 | int NonNullOperand = -1; | ||||
2648 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) | ||||
2649 | if (ST->isNullValue()) | ||||
2650 | NonNullOperand = 2; | ||||
2651 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y | ||||
2652 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) | ||||
2653 | if (ST->isNullValue()) | ||||
2654 | NonNullOperand = 1; | ||||
2655 | |||||
2656 | if (NonNullOperand == -1) | ||||
2657 | return false; | ||||
2658 | |||||
2659 | Value *SelectCond = SI->getOperand(0); | ||||
2660 | |||||
2661 | // Change the div/rem to use 'Y' instead of the select. | ||||
2662 | I.setOperand(1, SI->getOperand(NonNullOperand)); | ||||
2663 | |||||
2664 | // Okay, we know we replace the operand of the div/rem with 'Y' with no | ||||
2665 | // problem. However, the select, or the condition of the select may have | ||||
2666 | // multiple uses. Based on our knowledge that the operand must be non-zero, | ||||
2667 | // propagate the known value for the select into other uses of it, and | ||||
2668 | // propagate a known value of the condition into its other users. | ||||
2669 | |||||
2670 | // If the select and condition only have a single use, don't bother with this, | ||||
2671 | // early exit. | ||||
2672 | if (SI->use_empty() && SelectCond->hasOneUse()) | ||||
2673 | return true; | ||||
2674 | |||||
2675 | // Scan the current block backward, looking for other uses of SI. | ||||
2676 | BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); | ||||
2677 | |||||
2678 | while (BBI != BBFront) { | ||||
2679 | --BBI; | ||||
2680 | // If we found a call to a function, we can't assume it will return, so | ||||
2681 | // information from below it cannot be propagated above it. | ||||
2682 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) | ||||
2683 | break; | ||||
2684 | |||||
2685 | // Replace uses of the select or its condition with the known values. | ||||
2686 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); | ||||
2687 | I != E; ++I) { | ||||
2688 | if (*I == SI) { | ||||
2689 | *I = SI->getOperand(NonNullOperand); | ||||
2690 | AddToWorkList(BBI); | ||||
2691 | } else if (*I == SelectCond) { | ||||
2692 | *I = NonNullOperand == 1 ? ConstantInt::getTrue() : | ||||
2693 | ConstantInt::getFalse(); | ||||
2694 | AddToWorkList(BBI); | ||||
2695 | } | ||||
2696 | } | ||||
2697 | |||||
2698 | // If we past the instruction, quit looking for it. | ||||
2699 | if (&*BBI == SI) | ||||
2700 | SI = 0; | ||||
2701 | if (&*BBI == SelectCond) | ||||
2702 | SelectCond = 0; | ||||
2703 | |||||
2704 | // If we ran out of things to eliminate, break out of the loop. | ||||
2705 | if (SelectCond == 0 && SI == 0) | ||||
2706 | break; | ||||
2707 | |||||
2708 | } | ||||
2709 | return true; | ||||
2710 | } | ||||
2711 | |||||
2712 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2713 | /// This function implements the transforms on div instructions that work |
2714 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is | ||||
2715 | /// used by the visitors to those instructions. | ||||
2716 | /// @brief Transforms common to all three div instructions | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2717 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2718 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2719 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2720 | // undef / X -> 0 for integer. |
2721 | // undef / X -> undef for FP (the undef could be a snan). | ||||
2722 | if (isa<UndefValue>(Op0)) { | ||||
2723 | if (Op0->getType()->isFPOrFPVector()) | ||||
2724 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2725 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2726 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2727 | |
2728 | // X / undef -> undef | ||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2729 | if (isa<UndefValue>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2730 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2731 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2732 | return 0; |
2733 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2734 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2735 | /// This function implements the transforms common to both integer division |
2736 | /// instructions (udiv and sdiv). It is called by the visitors to those integer | ||||
2737 | /// division instructions. | ||||
2738 | /// @brief Common integer divide transforms | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2739 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2740 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
2741 | |||||
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2742 | // (sdiv X, X) --> 1 (udiv X, X) --> 1 |
Nick Lewycky | 39ac3b5 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2743 | if (Op0 == Op1) { |
2744 | if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) { | ||||
2745 | ConstantInt *CI = ConstantInt::get(Ty->getElementType(), 1); | ||||
2746 | std::vector<Constant*> Elts(Ty->getNumElements(), CI); | ||||
2747 | return ReplaceInstUsesWith(I, ConstantVector::get(Elts)); | ||||
2748 | } | ||||
2749 | |||||
2750 | ConstantInt *CI = ConstantInt::get(I.getType(), 1); | ||||
2751 | return ReplaceInstUsesWith(I, CI); | ||||
2752 | } | ||||
Chris Lattner | b2ae9e3 | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2753 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2754 | if (Instruction *Common = commonDivTransforms(I)) |
2755 | return Common; | ||||
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2756 | |
2757 | // Handle cases involving: [su]div X, (select Cond, Y, Z) | ||||
2758 | // This does not apply for fdiv. | ||||
2759 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) | ||||
2760 | return &I; | ||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2761 | |
2762 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | ||||
2763 | // div X, 1 == X | ||||
2764 | if (RHS->equalsInt(1)) | ||||
2765 | return ReplaceInstUsesWith(I, Op0); | ||||
2766 | |||||
2767 | // (X / C1) / C2 -> X / (C1*C2) | ||||
2768 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) | ||||
2769 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) | ||||
2770 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { | ||||
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2771 | if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) |
2772 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
2773 | else | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2774 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
Nick Lewycky | e0cfecf | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2775 | Multiply(RHS, LHSRHS)); |
Chris Lattner | bf70b83 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2776 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2777 | |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2778 | if (!RHS->isZero()) { // avoid X udiv 0 |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2779 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
2780 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | ||||
2781 | return R; | ||||
2782 | if (isa<PHINode>(Op0)) | ||||
2783 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2784 | return NV; | ||||
2785 | } | ||||
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2786 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2787 | |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2788 | // 0 / X == 0, we don't need to preserve faults! |
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2789 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2790 | if (LHS->equalsInt(0)) |
2791 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
2792 | |||||
Nick Lewycky | 9419ddb | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2793 | // It can't be division by zero, hence it must be division by one. |
2794 | if (I.getType() == Type::Int1Ty) | ||||
2795 | return ReplaceInstUsesWith(I, Op0); | ||||
2796 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2797 | return 0; |
2798 | } | ||||
2799 | |||||
2800 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { | ||||
2801 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
2802 | |||||
2803 | // Handle the integer div common cases | ||||
2804 | if (Instruction *Common = commonIDivTransforms(I)) | ||||
2805 | return Common; | ||||
2806 | |||||
2807 | // X udiv C^2 -> X >> C | ||||
2808 | // Check to see if this is an unsigned division with an exact power of 2, | ||||
2809 | // if so, convert to a right shift. | ||||
2810 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { | ||||
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2811 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2812 | return BinaryOperator::CreateLShr(Op0, |
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2813 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2814 | } |
2815 | |||||
2816 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2817 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2818 | if (RHSI->getOpcode() == Instruction::Shl && |
2819 | isa<ConstantInt>(RHSI->getOperand(0))) { | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2820 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2821 | if (C1.isPowerOf2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2822 | Value *N = RHSI->getOperand(1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2823 | const Type *NTy = N->getType(); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2824 | if (uint32_t C2 = C1.logBase2()) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2825 | Constant *C2V = ConstantInt::get(NTy, C2); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2826 | N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2827 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2828 | return BinaryOperator::CreateLShr(Op0, N); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2829 | } |
2830 | } | ||||
Chris Lattner | c812e5d | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2831 | } |
2832 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2833 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
2834 | // where C1&C2 are powers of two. | ||||
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2835 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2836 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2837 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2838 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2839 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2840 | // Compute the shift amounts |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2841 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2842 | // Construct the "on true" case of the select |
2843 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2844 | Instruction *TSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2845 | Op0, TC, SI->getName()+".t"); |
2846 | TSI = InsertNewInstBefore(TSI, I); | ||||
2847 | |||||
2848 | // Construct the "on false" case of the select | ||||
2849 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2850 | Instruction *FSI = BinaryOperator::CreateLShr( |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2851 | Op0, FC, SI->getName()+".f"); |
2852 | FSI = InsertNewInstBefore(FSI, I); | ||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2853 | |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2854 | // construct the select instruction and return it. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2855 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2856 | } |
Reid Spencer | baf1e4b | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2857 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2858 | return 0; |
2859 | } | ||||
2860 | |||||
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2861 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
2862 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
2863 | |||||
2864 | // Handle the integer div common cases | ||||
2865 | if (Instruction *Common = commonIDivTransforms(I)) | ||||
2866 | return Common; | ||||
2867 | |||||
2868 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | ||||
2869 | // sdiv X, -1 == -X | ||||
2870 | if (RHS->isAllOnesValue()) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2871 | return BinaryOperator::CreateNeg(Op0); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2872 | |
2873 | // -X/C -> X/-C | ||||
2874 | if (Value *LHSNeg = dyn_castNegVal(Op0)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2875 | return BinaryOperator::CreateSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2876 | } |
2877 | |||||
2878 | // If the sign bits of both operands are zero (i.e. we can prove they are | ||||
2879 | // unsigned inputs), turn this into a udiv. | ||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2880 | if (I.getType()->isInteger()) { |
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2881 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2882 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 2883 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2884 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2885 | } |
2886 | } | ||||
2887 | |||||
2888 | return 0; | ||||
2889 | } | ||||
2890 | |||||
2891 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { | ||||
2892 | return commonDivTransforms(I); | ||||
2893 | } | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2894 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2895 | /// This function implements the transforms on rem instructions that work |
2896 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It | ||||
2897 | /// is used by the visitors to those instructions. | ||||
2898 | /// @brief Transforms common to all three rem instructions | ||||
2899 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { | ||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2900 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2901 | |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2902 | // 0 % X == 0 for integer, we don't need to preserve faults! |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2903 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
2904 | if (LHS->isNullValue()) | ||||
2905 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
2906 | |||||
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2907 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
2908 | if (I.getType()->isFPOrFPVector()) | ||||
2909 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) | ||||
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2910 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | 50b2ca4 | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2911 | } |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2912 | if (isa<UndefValue>(Op1)) |
2913 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2914 | |
2915 | // Handle cases involving: rem X, (select Cond, Y, Z) | ||||
Chris Lattner | fdb19e5 | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2916 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
2917 | return &I; | ||||
Chris Lattner | 5b73c08 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2918 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2919 | return 0; |
2920 | } | ||||
2921 | |||||
2922 | /// This function implements the transforms common to both integer remainder | ||||
2923 | /// instructions (urem and srem). It is called by the visitors to those integer | ||||
2924 | /// remainder instructions. | ||||
2925 | /// @brief Common integer remainder transforms | ||||
2926 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { | ||||
2927 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
2928 | |||||
2929 | if (Instruction *common = commonRemTransforms(I)) | ||||
2930 | return common; | ||||
2931 | |||||
Chris Lattner | 857e8cd | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2932 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 19ccd5c | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2933 | // X % 0 == undef, we don't need to preserve faults! |
2934 | if (RHS->equalsInt(0)) | ||||
2935 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); | ||||
2936 | |||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2937 | if (RHS->equalsInt(1)) // X % 1 == 0 |
2938 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
2939 | |||||
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2940 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
2941 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { | ||||
2942 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | ||||
2943 | return R; | ||||
2944 | } else if (isa<PHINode>(Op0I)) { | ||||
2945 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
2946 | return NV; | ||||
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2947 | } |
Nick Lewycky | c1a2a61 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 2948 | |
2949 | // See if we can fold away this rem instruction. | ||||
2950 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | ||||
2951 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
2952 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
2953 | KnownZero, KnownOne)) | ||||
2954 | return &I; | ||||
Chris Lattner | 9794392 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2955 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2956 | } |
2957 | |||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2958 | return 0; |
2959 | } | ||||
2960 | |||||
2961 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { | ||||
2962 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
2963 | |||||
2964 | if (Instruction *common = commonIRemTransforms(I)) | ||||
2965 | return common; | ||||
2966 | |||||
2967 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | ||||
2968 | // X urem C^2 -> X and C | ||||
2969 | // Check to see if this is an unsigned remainder with an exact power of 2, | ||||
2970 | // if so, convert to a bitwise and. | ||||
2971 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) | ||||
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2972 | if (C->getValue().isPowerOf2()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2973 | return BinaryOperator::CreateAnd(Op0, SubOne(C)); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2974 | } |
2975 | |||||
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2976 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2977 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
2978 | if (RHSI->getOpcode() == Instruction::Shl && | ||||
2979 | isa<ConstantInt>(RHSI->getOperand(0))) { | ||||
Zhou Sheng | 0fc5095 | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2980 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2981 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2982 | Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1, |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2983 | "tmp"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2984 | return BinaryOperator::CreateAnd(Op0, Add); |
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2985 | } |
2986 | } | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2987 | } |
Chris Lattner | 8e49e08 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2988 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2989 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
2990 | // where C1&C2 are powers of two. | ||||
2991 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { | ||||
2992 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) | ||||
2993 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { | ||||
2994 | // STO == 0 and SFO == 0 handled above. | ||||
Reid Spencer | bca0e38 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2995 | if ((STO->getValue().isPowerOf2()) && |
2996 | (SFO->getValue().isPowerOf2())) { | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2997 | Value *TrueAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2998 | BinaryOperator::CreateAnd(Op0, SubOne(STO), SI->getName()+".t"), I); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2999 | Value *FalseAnd = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3000 | BinaryOperator::CreateAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3001 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3002 | } |
3003 | } | ||||
Chris Lattner | 5f3b0ee | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 3004 | } |
3005 | |||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3006 | return 0; |
3007 | } | ||||
3008 | |||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3009 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
3010 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
3011 | |||||
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3012 | // Handle the integer rem common cases |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3013 | if (Instruction *common = commonIRemTransforms(I)) |
3014 | return common; | ||||
3015 | |||||
3016 | if (Value *RHSNeg = dyn_castNegVal(Op1)) | ||||
Nick Lewycky | 23c0430 | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 3017 | if (!isa<Constant>(RHSNeg) || |
3018 | (isa<ConstantInt>(RHSNeg) && | ||||
3019 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3020 | // X % -Y -> X % Y |
3021 | AddUsesToWorkList(I); | ||||
3022 | I.setOperand(1, RHSNeg); | ||||
3023 | return &I; | ||||
3024 | } | ||||
3025 | |||||
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3026 | // 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] | 3027 | // unsigned inputs), turn this into a urem. |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3028 | if (I.getType()->isInteger()) { |
3029 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); | ||||
3030 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { | ||||
3031 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3032 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Dan Gohman | cff5509 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3033 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3034 | } |
3035 | |||||
3036 | return 0; | ||||
3037 | } | ||||
3038 | |||||
3039 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { | ||||
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 3040 | return commonRemTransforms(I); |
3041 | } | ||||
3042 | |||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3043 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
3044 | // constant. | ||||
3045 | static bool isOneBitSet(const ConstantInt *CI) { | ||||
Reid Spencer | 5f6a895 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 3046 | return CI->getValue().isPowerOf2(); |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3047 | } |
3048 | |||||
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3049 | // isHighOnes - Return true if the constant is of the form 1+0+. |
3050 | // This is the same as lowones(~X). | ||||
3051 | static bool isHighOnes(const ConstantInt *CI) { | ||||
Zhou Sheng | 2cde46c | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 3052 | return (~CI->getValue() + 1).isPowerOf2(); |
Chris Lattner | b20ba0a | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 3053 | } |
3054 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3055 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3056 | /// are carefully arranged to allow folding of expressions such as: |
3057 | /// | ||||
3058 | /// (A < B) | (A > B) --> (A != B) | ||||
3059 | /// | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3060 | /// Note that this is only valid if the first and second predicates have the |
3061 | /// same sign. Is illegal to do: (A u< B) | (A s> B) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3062 | /// |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3063 | /// Three bits are used to represent the condition, as follows: |
3064 | /// 0 A > B | ||||
3065 | /// 1 A == B | ||||
3066 | /// 2 A < B | ||||
3067 | /// | ||||
3068 | /// <=> Value Definition | ||||
3069 | /// 000 0 Always false | ||||
3070 | /// 001 1 A > B | ||||
3071 | /// 010 2 A == B | ||||
3072 | /// 011 3 A >= B | ||||
3073 | /// 100 4 A < B | ||||
3074 | /// 101 5 A != B | ||||
3075 | /// 110 6 A <= B | ||||
3076 | /// 111 7 Always true | ||||
3077 | /// | ||||
3078 | static unsigned getICmpCode(const ICmpInst *ICI) { | ||||
3079 | switch (ICI->getPredicate()) { | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3080 | // False -> 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3081 | case ICmpInst::ICMP_UGT: return 1; // 001 |
3082 | case ICmpInst::ICMP_SGT: return 1; // 001 | ||||
3083 | case ICmpInst::ICMP_EQ: return 2; // 010 | ||||
3084 | case ICmpInst::ICMP_UGE: return 3; // 011 | ||||
3085 | case ICmpInst::ICMP_SGE: return 3; // 011 | ||||
3086 | case ICmpInst::ICMP_ULT: return 4; // 100 | ||||
3087 | case ICmpInst::ICMP_SLT: return 4; // 100 | ||||
3088 | case ICmpInst::ICMP_NE: return 5; // 101 | ||||
3089 | case ICmpInst::ICMP_ULE: return 6; // 110 | ||||
3090 | case ICmpInst::ICMP_SLE: return 6; // 110 | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3091 | // True -> 7 |
3092 | default: | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3093 | assert(0 && "Invalid ICmp predicate!"); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3094 | return 0; |
3095 | } | ||||
3096 | } | ||||
3097 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3098 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
3099 | /// 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] | 3100 | /// new ICmp instruction. The sign is passed in to determine which kind |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3101 | /// of predicate to use in new icmp instructions. |
3102 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { | ||||
3103 | switch (code) { | ||||
3104 | default: assert(0 && "Illegal ICmp code!"); | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3105 | case 0: return ConstantInt::getFalse(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3106 | case 1: |
3107 | if (sign) | ||||
3108 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); | ||||
3109 | else | ||||
3110 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); | ||||
3111 | case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS); | ||||
3112 | case 3: | ||||
3113 | if (sign) | ||||
3114 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); | ||||
3115 | else | ||||
3116 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); | ||||
3117 | case 4: | ||||
3118 | if (sign) | ||||
3119 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); | ||||
3120 | else | ||||
3121 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); | ||||
3122 | case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS); | ||||
3123 | case 6: | ||||
3124 | if (sign) | ||||
3125 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); | ||||
3126 | else | ||||
3127 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3128 | case 7: return ConstantInt::getTrue(); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3129 | } |
3130 | } | ||||
3131 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3132 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
3133 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || | ||||
3134 | (ICmpInst::isSignedPredicate(p1) && | ||||
3135 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || | ||||
3136 | (ICmpInst::isSignedPredicate(p2) && | ||||
3137 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); | ||||
3138 | } | ||||
3139 | |||||
3140 | namespace { | ||||
3141 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) | ||||
3142 | struct FoldICmpLogical { | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3143 | InstCombiner &IC; |
3144 | Value *LHS, *RHS; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3145 | ICmpInst::Predicate pred; |
3146 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) | ||||
3147 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), | ||||
3148 | pred(ICI->getPredicate()) {} | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3149 | bool shouldApply(Value *V) const { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3150 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
3151 | if (PredicatesFoldable(pred, ICI->getPredicate())) | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3152 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
3153 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3154 | return false; |
3155 | } | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3156 | Instruction *apply(Instruction &Log) const { |
3157 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); | ||||
3158 | if (ICI->getOperand(0) != LHS) { | ||||
3159 | assert(ICI->getOperand(1) == LHS); | ||||
3160 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3161 | } |
3162 | |||||
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3163 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3164 | unsigned LHSCode = getICmpCode(ICI); |
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3165 | unsigned RHSCode = getICmpCode(RHSICI); |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3166 | unsigned Code; |
3167 | switch (Log.getOpcode()) { | ||||
3168 | case Instruction::And: Code = LHSCode & RHSCode; break; | ||||
3169 | case Instruction::Or: Code = LHSCode | RHSCode; break; | ||||
3170 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; | ||||
Chris Lattner | 021c190 | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 3171 | default: assert(0 && "Illegal logical opcode!"); return 0; |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3172 | } |
3173 | |||||
Chris Lattner | bc1dbfc | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 3174 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
3175 | ICmpInst::isSignedPredicate(ICI->getPredicate()); | ||||
3176 | |||||
3177 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3178 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
3179 | return I; | ||||
3180 | // Otherwise, it's a constant boolean value... | ||||
3181 | return IC.ReplaceInstUsesWith(Log, RV); | ||||
3182 | } | ||||
3183 | }; | ||||
Chris Lattner | d23b5ba | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 3184 | } // end anonymous namespace |
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3186 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
3187 | // 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] | 3188 | // guaranteed to be a binary operator. |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3189 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3190 | ConstantInt *OpRHS, |
3191 | ConstantInt *AndRHS, | ||||
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3192 | BinaryOperator &TheAnd) { |
3193 | Value *X = Op->getOperand(0); | ||||
Chris Lattner | 76f7fe2 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 3194 | Constant *Together = 0; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3195 | if (!Op->isShift()) |
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3196 | Together = And(AndRHS, OpRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3198 | switch (Op->getOpcode()) { |
3199 | case Instruction::Xor: | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3200 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3201 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3202 | Instruction *And = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3203 | InsertNewInstBefore(And, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3204 | And->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3205 | return BinaryOperator::CreateXor(And, Together); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3206 | } |
3207 | break; | ||||
3208 | case Instruction::Or: | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3209 | if (Together == AndRHS) // (X | C) & C --> C |
3210 | return ReplaceInstUsesWith(TheAnd, AndRHS); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3211 | |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3212 | if (Op->hasOneUse() && Together != OpRHS) { |
3213 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3214 | Instruction *Or = BinaryOperator::CreateOr(X, Together); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3215 | InsertNewInstBefore(Or, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3216 | Or->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3217 | return BinaryOperator::CreateAnd(Or, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3218 | } |
3219 | break; | ||||
3220 | case Instruction::Add: | ||||
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 3221 | if (Op->hasOneUse()) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3222 | // Adding a one to a single bit bit-field should be turned into an XOR |
3223 | // of the bit. First thing to check is to see if this AND is with a | ||||
3224 | // single bit constant. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3225 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3226 | |
3227 | // If there is only one bit set... | ||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 3228 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3229 | // Ok, at this point, we know that we are masking the result of the |
3230 | // ADD down to exactly one bit. If the constant we are adding has | ||||
3231 | // no bits set below this bit, then we can eliminate the ADD. | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3232 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3233 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3234 | // Check to see if any bits below the one bit set in AndRHSV are set. |
3235 | if ((AddRHS & (AndRHSV-1)) == 0) { | ||||
3236 | // If not, the only thing that can effect the output of the AND is | ||||
3237 | // the bit specified by AndRHSV. If that bit is set, the effect of | ||||
3238 | // the XOR is to toggle the bit. If it is clear, then the ADD has | ||||
3239 | // no effect. | ||||
3240 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop | ||||
3241 | TheAnd.setOperand(0, X); | ||||
3242 | return &TheAnd; | ||||
3243 | } else { | ||||
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3244 | // Pull the XOR out of the AND. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3245 | Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3246 | InsertNewInstBefore(NewAnd, TheAnd); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3247 | NewAnd->takeName(Op); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3248 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3249 | } |
3250 | } | ||||
3251 | } | ||||
3252 | } | ||||
3253 | break; | ||||
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3254 | |
3255 | case Instruction::Shl: { | ||||
3256 | // We know that the AND will not produce any of the bits shifted in, so if | ||||
3257 | // the anded constant includes them, clear them now! | ||||
3258 | // | ||||
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3259 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3260 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3261 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
3262 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3263 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3264 | if (CI->getValue() == ShlMask) { |
3265 | // Masking out bits that the shift already masks | ||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3266 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
3267 | } else if (CI != AndRHS) { // Reducing bits set in and. | ||||
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3268 | TheAnd.setOperand(1, CI); |
3269 | return &TheAnd; | ||||
3270 | } | ||||
3271 | break; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3272 | } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3273 | case Instruction::LShr: |
3274 | { | ||||
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3275 | // We know that the AND will not produce any of the bits shifted in, so if |
3276 | // the anded constant includes them, clear them now! This only applies to | ||||
3277 | // unsigned shifts, because a signed shr may bring in set bits! | ||||
3278 | // | ||||
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3279 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3280 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3281 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
3282 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); | ||||
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3283 | |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3284 | if (CI->getValue() == ShrMask) { |
3285 | // Masking out bits that the shift already masks. | ||||
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3286 | return ReplaceInstUsesWith(TheAnd, Op); |
3287 | } else if (CI != AndRHS) { | ||||
3288 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. | ||||
3289 | return &TheAnd; | ||||
3290 | } | ||||
3291 | break; | ||||
3292 | } | ||||
3293 | case Instruction::AShr: | ||||
3294 | // Signed shr. | ||||
3295 | // See if this is shifting in some sign extension, then masking it out | ||||
3296 | // with an and. | ||||
3297 | if (Op->hasOneUse()) { | ||||
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3298 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3299 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3300 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
3301 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); | ||||
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3302 | if (C == AndRHS) { // Masking out bits shifted in. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3303 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3304 | // Make the argument unsigned. |
3305 | Value *ShVal = Op->getOperand(0); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3306 | ShVal = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3307 | BinaryOperator::CreateLShr(ShVal, OpRHS, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3308 | Op->getName()), TheAnd); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3309 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
Chris Lattner | 0c96766 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3310 | } |
Chris Lattner | 62a355c | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3311 | } |
3312 | break; | ||||
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3313 | } |
3314 | return 0; | ||||
3315 | } | ||||
3316 | |||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3318 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
3319 | /// 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] | 3320 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
3321 | /// 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] | 3322 | /// insert new instructions. |
3323 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3324 | bool isSigned, bool Inside, |
3325 | Instruction &IB) { | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3326 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3327 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3328 | "Lo is not <= Hi in range emission code!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3330 | if (Inside) { |
3331 | if (Lo == Hi) // Trivially false. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3332 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3333 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3334 | // V >= Min && V < Hi --> V < Hi |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3335 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3336 | ICmpInst::Predicate pred = (isSigned ? |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3337 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
3338 | return new ICmpInst(pred, V, Hi); | ||||
3339 | } | ||||
3340 | |||||
3341 | // Emit V-Lo <u Hi-Lo | ||||
3342 | Constant *NegLo = ConstantExpr::getNeg(Lo); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3343 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3344 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3345 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); |
3346 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3347 | } |
3348 | |||||
3349 | if (Lo == Hi) // Trivially true. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3350 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3351 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3352 | // V < Min || V >= Hi -> V > Hi-1 |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3353 | Hi = SubOne(cast<ConstantInt>(Hi)); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3354 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3355 | ICmpInst::Predicate pred = (isSigned ? |
3356 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); | ||||
3357 | return new ICmpInst(pred, V, Hi); | ||||
3358 | } | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3359 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3360 | // Emit V-Lo >u Hi-1-Lo |
3361 | // Note that Hi has already had one subtracted from it, above. | ||||
3362 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3363 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3364 | InsertNewInstBefore(Add, IB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3365 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); |
3366 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3367 | } |
3368 | |||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3369 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
3370 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to | ||||
3371 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is | ||||
3372 | // not, since all 1s are not contiguous. | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3373 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3374 | const APInt& V = Val->getValue(); |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3375 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
3376 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3377 | |
3378 | // look for the first zero bit after the run of ones | ||||
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3379 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3380 | // look for the first non-zero bit |
Reid Spencer | f244252 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3381 | ME = V.getActiveBits(); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3382 | return true; |
3383 | } | ||||
3384 | |||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3385 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
3386 | /// where isSub determines whether the operator is a sub. If we can fold one of | ||||
3387 | /// the following xforms: | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3388 | /// |
3389 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask | ||||
3390 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 | ||||
3391 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 | ||||
3392 | /// | ||||
3393 | /// return (A +/- B). | ||||
3394 | /// | ||||
3395 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3396 | ConstantInt *Mask, bool isSub, |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3397 | Instruction &I) { |
3398 | Instruction *LHSI = dyn_cast<Instruction>(LHS); | ||||
3399 | if (!LHSI || LHSI->getNumOperands() != 2 || | ||||
3400 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; | ||||
3401 | |||||
3402 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); | ||||
3403 | |||||
3404 | switch (LHSI->getOpcode()) { | ||||
3405 | default: return 0; | ||||
3406 | case Instruction::And: | ||||
Reid Spencer | 7177c3a | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3407 | if (And(N, Mask) == Mask) { |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3408 | // 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] | 3409 | if ((Mask->getValue().countLeadingZeros() + |
3410 | Mask->getValue().countPopulation()) == | ||||
3411 | Mask->getValue().getBitWidth()) | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3412 | break; |
3413 | |||||
3414 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ | ||||
3415 | // part, we don't need any explicit masks to take them out of A. If that | ||||
3416 | // is all N is, ignore it. | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3417 | uint32_t MB = 0, ME = 0; |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3418 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3419 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
Zhou Sheng | 290bec5 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3420 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3421 | if (MaskedValueIsZero(RHS, Mask)) |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3422 | break; |
3423 | } | ||||
3424 | } | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3425 | return 0; |
3426 | case Instruction::Or: | ||||
3427 | case Instruction::Xor: | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3428 | // 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] | 3429 | if ((Mask->getValue().countLeadingZeros() + |
3430 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() | ||||
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3431 | && And(N, Mask)->isZero()) |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3432 | break; |
3433 | return 0; | ||||
3434 | } | ||||
3435 | |||||
3436 | Instruction *New; | ||||
3437 | if (isSub) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3438 | New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3439 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3440 | New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold"); |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3441 | return InsertNewInstBefore(New, I); |
3442 | } | ||||
3443 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3444 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3445 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3446 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3447 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3448 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
3449 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3450 | |||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3451 | // and X, X = X |
3452 | if (Op0 == Op1) | ||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3453 | return ReplaceInstUsesWith(I, Op1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3454 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3455 | // 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] | 3456 | // purpose is to compute bits we don't care about. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3457 | if (!isa<VectorType>(I.getType())) { |
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3458 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); |
3459 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
3460 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3461 | KnownZero, KnownOne)) |
Reid Spencer | 6eb0d99 | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3462 | return &I; |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3463 | } else { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3464 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3465 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3466 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 3467 | } else if (isa<ConstantAggregateZero>(Op1)) { |
3468 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> | ||||
Chris Lattner | 696ee0a | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3469 | } |
3470 | } | ||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3471 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3472 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3473 | const APInt& AndRHSMask = AndRHS->getValue(); |
3474 | APInt NotAndRHS(~AndRHSMask); | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3475 | |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3476 | // Optimize a variety of ((val OP C1) & C2) combinations... |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3477 | if (isa<BinaryOperator>(Op0)) { |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3478 | Instruction *Op0I = cast<Instruction>(Op0); |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3479 | Value *Op0LHS = Op0I->getOperand(0); |
3480 | Value *Op0RHS = Op0I->getOperand(1); | ||||
3481 | switch (Op0I->getOpcode()) { | ||||
3482 | case Instruction::Xor: | ||||
3483 | case Instruction::Or: | ||||
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3484 | // If the mask is only needed on one incoming arm, push it up. |
3485 | if (Op0I->hasOneUse()) { | ||||
3486 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { | ||||
3487 | // Not masking anything out for the LHS, move to RHS. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3488 | Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3489 | Op0RHS->getName()+".masked"); |
3490 | InsertNewInstBefore(NewRHS, I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3491 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3492 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3493 | } |
Chris Lattner | 3bedbd9 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3494 | if (!isa<Constant>(Op0RHS) && |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3495 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
3496 | // Not masking anything out for the RHS, move to LHS. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3497 | Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS, |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3498 | Op0LHS->getName()+".masked"); |
3499 | InsertNewInstBefore(NewLHS, I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3500 | return BinaryOperator::Create( |
Chris Lattner | ad1e302 | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3501 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
3502 | } | ||||
3503 | } | ||||
3504 | |||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3505 | break; |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3506 | case Instruction::Add: |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3507 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
3508 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 | ||||
3509 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 | ||||
3510 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3511 | return BinaryOperator::CreateAnd(V, AndRHS); |
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3512 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3513 | return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes |
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3514 | break; |
3515 | |||||
3516 | case Instruction::Sub: | ||||
Chris Lattner | 7203e15 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3517 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
3518 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 | ||||
3519 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 | ||||
3520 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3521 | return BinaryOperator::CreateAnd(V, AndRHS); |
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3522 | |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 3523 | // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS |
3524 | // has 1's for all bits that the subtraction with A might affect. | ||||
3525 | if (Op0I->hasOneUse()) { | ||||
3526 | uint32_t BitWidth = AndRHSMask.getBitWidth(); | ||||
3527 | uint32_t Zeros = AndRHSMask.countLeadingZeros(); | ||||
3528 | APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros); | ||||
3529 | |||||
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3530 | ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS); |
Nick Lewycky | 5dcc41f | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 3531 | if (!(A && A->isZero()) && // avoid infinite recursion. |
3532 | MaskedValueIsZero(Op0LHS, Mask)) { | ||||
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3533 | Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS); |
3534 | InsertNewInstBefore(NewNeg, I); | ||||
3535 | return BinaryOperator::CreateAnd(NewNeg, AndRHS); | ||||
3536 | } | ||||
3537 | } | ||||
Chris Lattner | c8e7756 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3538 | break; |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 3539 | |
3540 | case Instruction::Shl: | ||||
3541 | case Instruction::LShr: | ||||
3542 | // (1 << x) & 1 --> zext(x == 0) | ||||
3543 | // (1 >> x) & 1 --> zext(x == 0) | ||||
Nick Lewycky | d8ad492 | 2008-07-09 07:35:26 +0000 | [diff] [blame] | 3544 | if (AndRHSMask == 1 && Op0LHS == AndRHS) { |
Nick Lewycky | d1f77bf | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 3545 | Instruction *NewICmp = new ICmpInst(ICmpInst::ICMP_EQ, Op0RHS, |
3546 | Constant::getNullValue(I.getType())); | ||||
3547 | InsertNewInstBefore(NewICmp, I); | ||||
3548 | return new ZExtInst(NewICmp, I.getType()); | ||||
3549 | } | ||||
3550 | break; | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3551 | } |
3552 | |||||
Chris Lattner | 5840326 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3553 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3554 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
Chris Lattner | bd7b5ff | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3555 | return Res; |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3556 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3557 | // If this is an integer truncation or change from signed-to-unsigned, and |
3558 | // if the source is an and/or with immediate, transform it. This | ||||
3559 | // frequently occurs for bitfield accesses. | ||||
3560 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3561 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3562 | CastOp->getNumOperands() == 2) |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3563 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3564 | if (CastOp->getOpcode() == Instruction::And) { |
3565 | // Change: and (cast (and X, C1) to T), C2 | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3566 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
3567 | // This will fold the two constants together, which may allow | ||||
3568 | // other simplifications. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3569 | Instruction *NewCast = CastInst::CreateTruncOrBitCast( |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3570 | CastOp->getOperand(0), I.getType(), |
3571 | CastOp->getName()+".shrunk"); | ||||
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3572 | NewCast = InsertNewInstBefore(NewCast, I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3573 | // trunc_or_bitcast(C1)&C2 |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3574 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3575 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3576 | return BinaryOperator::CreateAnd(NewCast, C3); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3577 | } else if (CastOp->getOpcode() == Instruction::Or) { |
3578 | // Change: and (cast (or X, C1) to T), C2 | ||||
3579 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 | ||||
Chris Lattner | bb4e7b2 | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3580 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3581 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 |
3582 | return ReplaceInstUsesWith(I, AndRHS); | ||||
3583 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3584 | } |
Chris Lattner | 2b83af2 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3585 | } |
Chris Lattner | 06782f8 | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3586 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3587 | |
3588 | // Try to fold constant and into select arguments. | ||||
3589 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3590 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3591 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3592 | if (isa<PHINode>(Op0)) |
3593 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
3594 | return NV; | ||||
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3595 | } |
3596 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3597 | Value *Op0NotVal = dyn_castNotVal(Op0); |
3598 | Value *Op1NotVal = dyn_castNotVal(Op1); | ||||
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3599 | |
Chris Lattner | 5b62aa7 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3600 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
3601 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
3602 | |||||
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3603 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3604 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3605 | Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal, |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3606 | I.getName()+".demorgan"); |
Chris Lattner | c6a8aff | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3607 | InsertNewInstBefore(Or, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3608 | return BinaryOperator::CreateNot(Or); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3609 | } |
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3610 | |
3611 | { | ||||
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3612 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
3613 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3614 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
3615 | return ReplaceInstUsesWith(I, Op1); | ||||
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3616 | |
3617 | // (A|B) & ~(A&B) -> A^B | ||||
3618 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { | ||||
3619 | if ((A == C && B == D) || (A == D && B == C)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3620 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3621 | } |
3622 | } | ||||
3623 | |||||
3624 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3625 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
3626 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3627 | |
3628 | // ~(A&B) & (A|B) -> A^B | ||||
3629 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { | ||||
3630 | if ((A == C && B == D) || (A == D && B == C)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3631 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 003b620 | 2007-06-15 05:58:24 +0000 | [diff] [blame] | 3632 | } |
3633 | } | ||||
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3634 | |
3635 | if (Op0->hasOneUse() && | ||||
3636 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | ||||
3637 | if (A == Op1) { // (A^B)&A -> A&(A^B) | ||||
3638 | I.swapOperands(); // Simplify below | ||||
3639 | std::swap(Op0, Op1); | ||||
3640 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) | ||||
3641 | cast<BinaryOperator>(Op0)->swapOperands(); | ||||
3642 | I.swapOperands(); // Simplify below | ||||
3643 | std::swap(Op0, Op1); | ||||
3644 | } | ||||
3645 | } | ||||
3646 | if (Op1->hasOneUse() && | ||||
3647 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { | ||||
3648 | if (B == Op0) { // B&(A^B) -> B&(B^A) | ||||
3649 | cast<BinaryOperator>(Op1)->swapOperands(); | ||||
3650 | std::swap(A, B); | ||||
3651 | } | ||||
3652 | if (A == Op0) { // A&(A^B) -> A & ~B | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3653 | Instruction *NotB = BinaryOperator::CreateNot(B, "tmp"); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3654 | InsertNewInstBefore(NotB, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3655 | return BinaryOperator::CreateAnd(A, NotB); |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3656 | } |
3657 | } | ||||
Chris Lattner | 2082ad9 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3658 | } |
3659 | |||||
Nick Lewycky | b30591e | 2008-08-06 04:54:03 +0000 | [diff] [blame] | 3660 | { // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) |
3661 | // where C is a power of 2 | ||||
3662 | Value *A, *B; | ||||
3663 | ConstantInt *C1, *C2; | ||||
Evan Cheng | ab5d5e3 | 2008-08-20 23:36:48 +0000 | [diff] [blame] | 3664 | ICmpInst::Predicate LHSCC = ICmpInst::BAD_ICMP_PREDICATE; |
3665 | ICmpInst::Predicate RHSCC = ICmpInst::BAD_ICMP_PREDICATE; | ||||
Nick Lewycky | b30591e | 2008-08-06 04:54:03 +0000 | [diff] [blame] | 3666 | if (match(&I, m_And(m_ICmp(LHSCC, m_Value(A), m_ConstantInt(C1)), |
3667 | m_ICmp(RHSCC, m_Value(B), m_ConstantInt(C2))))) | ||||
3668 | if (C1 == C2 && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT && | ||||
3669 | C1->getValue().isPowerOf2()) { | ||||
3670 | Instruction *NewOr = BinaryOperator::CreateOr(A, B); | ||||
3671 | InsertNewInstBefore(NewOr, I); | ||||
3672 | return new ICmpInst(LHSCC, NewOr, C1); | ||||
3673 | } | ||||
3674 | } | ||||
3675 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3676 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
3677 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) | ||||
3678 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3679 | return R; |
3680 | |||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3681 | Value *LHSVal, *RHSVal; |
3682 | ConstantInt *LHSCst, *RHSCst; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3683 | ICmpInst::Predicate LHSCC, RHSCC; |
3684 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) | ||||
3685 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) | ||||
3686 | if (LHSVal == RHSVal && // Found (X icmp C1) & (X icmp C2) | ||||
3687 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. | ||||
3688 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && | ||||
3689 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && | ||||
3690 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && | ||||
Chris Lattner | eec8b9a | 2007-11-22 23:47:13 +0000 | [diff] [blame] | 3691 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
3692 | |||||
3693 | // Don't try to fold ICMP_SLT + ICMP_ULT. | ||||
3694 | (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) || | ||||
3695 | ICmpInst::isSignedPredicate(LHSCC) == | ||||
3696 | ICmpInst::isSignedPredicate(RHSCC))) { | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3697 | // Ensure that the larger constant is on the RHS. |
Chris Lattner | ee2b7a4 | 2008-01-13 20:59:02 +0000 | [diff] [blame] | 3698 | ICmpInst::Predicate GT; |
3699 | if (ICmpInst::isSignedPredicate(LHSCC) || | ||||
3700 | (ICmpInst::isEquality(LHSCC) && | ||||
3701 | ICmpInst::isSignedPredicate(RHSCC))) | ||||
3702 | GT = ICmpInst::ICMP_SGT; | ||||
3703 | else | ||||
3704 | GT = ICmpInst::ICMP_UGT; | ||||
3705 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3706 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); |
3707 | ICmpInst *LHS = cast<ICmpInst>(Op0); | ||||
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3708 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3709 | std::swap(LHS, RHS); |
3710 | std::swap(LHSCst, RHSCst); | ||||
3711 | std::swap(LHSCC, RHSCC); | ||||
3712 | } | ||||
3713 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3714 | // At this point, we know we have have two icmp instructions |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3715 | // comparing a value against two constants and and'ing the result |
3716 | // together. Because of the above check, we know that we only have | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3717 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
3718 | // (from the FoldICmpLogical check above), that the two constants | ||||
3719 | // are not equal and that the larger constant is on the RHS | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3720 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
3721 | |||||
3722 | switch (LHSCC) { | ||||
3723 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3724 | case ICmpInst::ICMP_EQ: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3725 | switch (RHSCC) { |
3726 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3727 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
3728 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false | ||||
3729 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3730 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3731 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
3732 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 | ||||
3733 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3734 | return ReplaceInstUsesWith(I, LHS); |
3735 | } | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3736 | case ICmpInst::ICMP_NE: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3737 | switch (RHSCC) { |
3738 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3739 | case ICmpInst::ICMP_ULT: |
3740 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 | ||||
3741 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); | ||||
3742 | break; // (X != 13 & X u< 15) -> no change | ||||
3743 | case ICmpInst::ICMP_SLT: | ||||
3744 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 | ||||
3745 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); | ||||
3746 | break; // (X != 13 & X s< 15) -> no change | ||||
3747 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 | ||||
3748 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 | ||||
3749 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3750 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3751 | case ICmpInst::ICMP_NE: |
3752 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3753 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3754 | Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST, |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3755 | LHSVal->getName()+".off"); |
3756 | InsertNewInstBefore(Add, I); | ||||
Chris Lattner | 424db02 | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3757 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, |
3758 | ConstantInt::get(Add->getType(), 1)); | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3759 | } |
3760 | break; // (X != 13 & X != 15) -> no change | ||||
3761 | } | ||||
3762 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3763 | case ICmpInst::ICMP_ULT: |
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3764 | switch (RHSCC) { |
3765 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3766 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
3767 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3768 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3769 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
3770 | break; | ||||
3771 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 | ||||
3772 | 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] | 3773 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3774 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
3775 | break; | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3776 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3777 | break; |
3778 | case ICmpInst::ICMP_SLT: | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3779 | switch (RHSCC) { |
3780 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3781 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
3782 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3783 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3784 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
3785 | break; | ||||
3786 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 | ||||
3787 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3788 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3789 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
3790 | break; | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3791 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3792 | break; |
3793 | case ICmpInst::ICMP_UGT: | ||||
3794 | switch (RHSCC) { | ||||
3795 | default: assert(0 && "Unknown integer condition code!"); | ||||
Eli Friedman | 5c1f172 | 2008-06-21 23:36:13 +0000 | [diff] [blame] | 3796 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3797 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
3798 | return ReplaceInstUsesWith(I, RHS); | ||||
3799 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change | ||||
3800 | break; | ||||
3801 | case ICmpInst::ICMP_NE: | ||||
3802 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 | ||||
3803 | return new ICmpInst(LHSCC, LHSVal, RHSCst); | ||||
3804 | break; // (X u> 13 & X != 15) -> no change | ||||
3805 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) ->(X-14) <u 1 | ||||
3806 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, | ||||
3807 | true, I); | ||||
3808 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change | ||||
3809 | break; | ||||
3810 | } | ||||
3811 | break; | ||||
3812 | case ICmpInst::ICMP_SGT: | ||||
3813 | switch (RHSCC) { | ||||
3814 | default: assert(0 && "Unknown integer condition code!"); | ||||
Chris Lattner | a7d1ab0 | 2007-11-16 06:04:17 +0000 | [diff] [blame] | 3815 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3816 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
3817 | return ReplaceInstUsesWith(I, RHS); | ||||
3818 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change | ||||
3819 | break; | ||||
3820 | case ICmpInst::ICMP_NE: | ||||
3821 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 | ||||
3822 | return new ICmpInst(LHSCC, LHSVal, RHSCst); | ||||
3823 | break; // (X s> 13 & X != 15) -> no change | ||||
3824 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) ->(X-14) s< 1 | ||||
3825 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, | ||||
3826 | true, I); | ||||
3827 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change | ||||
3828 | break; | ||||
3829 | } | ||||
3830 | break; | ||||
Chris Lattner | 955f331 | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3831 | } |
3832 | } | ||||
3833 | } | ||||
3834 | |||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3835 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3836 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
3837 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) | ||||
3838 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? | ||||
3839 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | ||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3840 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3841 | // 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] | 3842 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
3843 | I.getType(), TD) && | ||||
3844 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | ||||
3845 | I.getType(), TD)) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3846 | Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3847 | Op1C->getOperand(0), |
3848 | I.getName()); | ||||
3849 | InsertNewInstBefore(NewOp, I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3850 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3851 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3852 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3853 | |
3854 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3855 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
3856 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) | ||||
3857 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && | ||||
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3858 | SI0->getOperand(1) == SI1->getOperand(1) && |
3859 | (SI0->hasOneUse() || SI1->hasOneUse())) { | ||||
3860 | Instruction *NewOp = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3861 | InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3862 | SI1->getOperand(0), |
3863 | SI0->getName()), I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3864 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3865 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3866 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3867 | } |
3868 | |||||
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3869 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
3870 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { | ||||
3871 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { | ||||
3872 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && | ||||
3873 | RHS->getPredicate() == FCmpInst::FCMP_ORD) | ||||
3874 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) | ||||
3875 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { | ||||
3876 | // If either of the constants are nans, then the whole thing returns | ||||
3877 | // false. | ||||
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 3878 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 3879 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
3880 | return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0), | ||||
3881 | RHS->getOperand(0)); | ||||
3882 | } | ||||
3883 | } | ||||
3884 | } | ||||
Nick Lewycky | b4d1bc9 | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3886 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3887 | } |
3888 | |||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3889 | /// CollectBSwapParts - Look to see if the specified value defines a single byte |
3890 | /// in the result. If it does, and if the specified byte hasn't been filled in | ||||
3891 | /// yet, fill it in and return false. | ||||
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3892 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3893 | Instruction *I = dyn_cast<Instruction>(V); |
3894 | if (I == 0) return true; | ||||
3895 | |||||
3896 | // If this is an or instruction, it is an inner node of the bswap. | ||||
3897 | if (I->getOpcode() == Instruction::Or) | ||||
3898 | return CollectBSwapParts(I->getOperand(0), ByteValues) || | ||||
3899 | CollectBSwapParts(I->getOperand(1), ByteValues); | ||||
3900 | |||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3901 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3902 | // If this is a shift by a constant int, and it is "24", then its operand |
3903 | // defines a byte. We only handle unsigned types here. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3904 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3905 | // Not shifting the entire input by N-1 bytes? |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3906 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3907 | 8*(ByteValues.size()-1)) |
3908 | return true; | ||||
3909 | |||||
3910 | unsigned DestNo; | ||||
3911 | if (I->getOpcode() == Instruction::Shl) { | ||||
3912 | // X << 24 defines the top byte with the lowest of the input bytes. | ||||
3913 | DestNo = ByteValues.size()-1; | ||||
3914 | } else { | ||||
3915 | // X >>u 24 defines the low byte with the highest of the input bytes. | ||||
3916 | DestNo = 0; | ||||
3917 | } | ||||
3918 | |||||
3919 | // If the destination byte value is already defined, the values are or'd | ||||
3920 | // together, which isn't a bswap (unless it's an or of the same bits). | ||||
3921 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) | ||||
3922 | return true; | ||||
3923 | ByteValues[DestNo] = I->getOperand(0); | ||||
3924 | return false; | ||||
3925 | } | ||||
3926 | |||||
3927 | // Otherwise, we can only handle and(shift X, imm), imm). Bail out of if we | ||||
3928 | // don't have this. | ||||
3929 | Value *Shift = 0, *ShiftLHS = 0; | ||||
3930 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; | ||||
3931 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || | ||||
3932 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) | ||||
3933 | return true; | ||||
3934 | Instruction *SI = cast<Instruction>(Shift); | ||||
3935 | |||||
3936 | // 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] | 3937 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || |
3938 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3939 | return true; |
3940 | |||||
3941 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. | ||||
3942 | unsigned DestByte; | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3943 | if (AndAmt->getValue().getActiveBits() > 64) |
3944 | return true; | ||||
3945 | uint64_t AndAmtVal = AndAmt->getZExtValue(); | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3946 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3947 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3948 | break; |
3949 | // Unknown mask for bswap. | ||||
3950 | if (DestByte == ByteValues.size()) return true; | ||||
3951 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3952 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3953 | unsigned SrcByte; |
3954 | if (SI->getOpcode() == Instruction::Shl) | ||||
3955 | SrcByte = DestByte - ShiftBytes; | ||||
3956 | else | ||||
3957 | SrcByte = DestByte + ShiftBytes; | ||||
3958 | |||||
3959 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. | ||||
3960 | if (SrcByte != ByteValues.size()-DestByte-1) | ||||
3961 | return true; | ||||
3962 | |||||
3963 | // If the destination byte value is already defined, the values are or'd | ||||
3964 | // together, which isn't a bswap (unless it's an or of the same bits). | ||||
3965 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) | ||||
3966 | return true; | ||||
3967 | ByteValues[DestByte] = SI->getOperand(0); | ||||
3968 | return false; | ||||
3969 | } | ||||
3970 | |||||
3971 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. | ||||
3972 | /// If so, insert the new bswap intrinsic and return it. | ||||
3973 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { | ||||
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3974 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
3975 | if (!ITy || ITy->getBitWidth() % 16) | ||||
3976 | return 0; // Can only bswap pairs of bytes. Can't do vectors. | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3977 | |
3978 | /// ByteValues - For each byte of the result, we keep track of which value | ||||
3979 | /// defines each byte. | ||||
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3980 | SmallVector<Value*, 8> ByteValues; |
Chris Lattner | 55fc8c4 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3981 | ByteValues.resize(ITy->getBitWidth()/8); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3982 | |
3983 | // Try to find all the pieces corresponding to the bswap. | ||||
3984 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || | ||||
3985 | CollectBSwapParts(I.getOperand(1), ByteValues)) | ||||
3986 | return 0; | ||||
3987 | |||||
3988 | // Check to see if all of the bytes come from the same value. | ||||
3989 | Value *V = ByteValues[0]; | ||||
3990 | if (V == 0) return 0; // Didn't find a byte? Must be zero. | ||||
3991 | |||||
3992 | // Check to make sure that all of the bytes come from the same value. | ||||
3993 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) | ||||
3994 | if (ByteValues[i] != V) | ||||
3995 | return 0; | ||||
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3996 | const Type *Tys[] = { ITy }; |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3997 | Module *M = I.getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 3998 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3999 | return CallInst::Create(F, V); |
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4000 | } |
4001 | |||||
4002 | |||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4003 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4004 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4005 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4006 | |
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4007 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4008 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4009 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4010 | // or X, X = X |
4011 | if (Op0 == Op1) | ||||
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4012 | return ReplaceInstUsesWith(I, Op0); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4013 | |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4014 | // See if we can simplify any instructions used by the instruction whose sole |
4015 | // purpose is to compute bits we don't care about. | ||||
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4016 | if (!isa<VectorType>(I.getType())) { |
4017 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | ||||
4018 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
4019 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
4020 | KnownZero, KnownOne)) | ||||
4021 | return &I; | ||||
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4022 | } else if (isa<ConstantAggregateZero>(Op1)) { |
4023 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X | ||||
4024 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { | ||||
4025 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> | ||||
4026 | return ReplaceInstUsesWith(I, I.getOperand(1)); | ||||
Chris Lattner | 42593e6 | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 4027 | } |
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4028 | |
4029 | |||||
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4030 | |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4031 | // or X, -1 == -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4032 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4033 | ConstantInt *C1 = 0; Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4034 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
4035 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4036 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4037 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4038 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4039 | return BinaryOperator::CreateAnd(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4040 | ConstantInt::get(RHS->getValue() | C1->getValue())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4041 | } |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4043 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
4044 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4045 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4046 | InsertNewInstBefore(Or, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4047 | Or->takeName(Op0); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4048 | return BinaryOperator::CreateXor(Or, |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4049 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); |
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4050 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4051 | |
4052 | // Try to fold constant and into select arguments. | ||||
4053 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4054 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4055 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4056 | if (isa<PHINode>(Op0)) |
4057 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
4058 | return NV; | ||||
Chris Lattner | ad44ebf | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 4059 | } |
4060 | |||||
Chris Lattner | 4f637d4 | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 4061 | Value *A = 0, *B = 0; |
4062 | ConstantInt *C1 = 0, *C2 = 0; | ||||
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4063 | |
4064 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) | ||||
4065 | if (A == Op1 || B == Op1) // (A & ?) | A --> A | ||||
4066 | return ReplaceInstUsesWith(I, Op1); | ||||
4067 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) | ||||
4068 | if (A == Op0 || B == Op0) // A | (A & ?) --> A | ||||
4069 | return ReplaceInstUsesWith(I, Op0); | ||||
4070 | |||||
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4071 | // (A | B) | C and A | (B | C) -> bswap if possible. |
4072 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4073 | if (match(Op0, m_Or(m_Value(), m_Value())) || |
Chris Lattner | 6423d4c | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 4074 | match(Op1, m_Or(m_Value(), m_Value())) || |
4075 | (match(Op0, m_Shift(m_Value(), m_Value())) && | ||||
4076 | match(Op1, m_Shift(m_Value(), m_Value())))) { | ||||
Chris Lattner | afe91a5 | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 4077 | if (Instruction *BSwap = MatchBSwap(I)) |
4078 | return BSwap; | ||||
4079 | } | ||||
4080 | |||||
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4081 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
4082 | 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] | 4083 | MaskedValueIsZero(Op1, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4084 | Instruction *NOr = BinaryOperator::CreateOr(A, Op1); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4085 | InsertNewInstBefore(NOr, I); |
4086 | NOr->takeName(Op0); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4087 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4088 | } |
4089 | |||||
4090 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 | ||||
4091 | 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] | 4092 | MaskedValueIsZero(Op0, C1->getValue())) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4093 | Instruction *NOr = BinaryOperator::CreateOr(A, Op0); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 4094 | InsertNewInstBefore(NOr, I); |
4095 | NOr->takeName(Op0); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4096 | return BinaryOperator::CreateXor(NOr, C1); |
Chris Lattner | 6e4c649 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 4097 | } |
4098 | |||||
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4099 | // (A & C)|(B & D) |
Chris Lattner | 2384d7b | 2007-06-19 05:43:49 +0000 | [diff] [blame] | 4100 | Value *C = 0, *D = 0; |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4101 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
4102 | match(Op1, m_And(m_Value(B), m_Value(D)))) { | ||||
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4103 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
4104 | C1 = dyn_cast<ConstantInt>(C); | ||||
4105 | C2 = dyn_cast<ConstantInt>(D); | ||||
4106 | if (C1 && C2) { // (A & C1)|(B & C2) | ||||
4107 | // If we have: ((V + N) & C1) | (V & C2) | ||||
4108 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 | ||||
4109 | // replace with V+N. | ||||
4110 | if (C1->getValue() == ~C2->getValue()) { | ||||
4111 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ | ||||
4112 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { | ||||
4113 | // Add commutes, try both ways. | ||||
4114 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) | ||||
4115 | return ReplaceInstUsesWith(I, A); | ||||
4116 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) | ||||
4117 | return ReplaceInstUsesWith(I, A); | ||||
4118 | } | ||||
4119 | // Or commutes, try both ways. | ||||
4120 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && | ||||
4121 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { | ||||
4122 | // Add commutes, try both ways. | ||||
4123 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) | ||||
4124 | return ReplaceInstUsesWith(I, B); | ||||
4125 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) | ||||
4126 | return ReplaceInstUsesWith(I, B); | ||||
4127 | } | ||||
4128 | } | ||||
Chris Lattner | 044e533 | 2007-04-08 08:01:49 +0000 | [diff] [blame] | 4129 | V1 = 0; V2 = 0; V3 = 0; |
Chris Lattner | 6cae0e0 | 2007-04-08 07:55:22 +0000 | [diff] [blame] | 4130 | } |
4131 | |||||
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4132 | // Check to see if we have any common things being and'ed. If so, find the |
4133 | // terms for V1 & (V2|V3). | ||||
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4134 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
4135 | if (A == B) // (A & C)|(A & D) == A & (C|D) | ||||
4136 | V1 = A, V2 = C, V3 = D; | ||||
4137 | else if (A == D) // (A & C)|(B & A) == A & (B|C) | ||||
4138 | V1 = A, V2 = B, V3 = C; | ||||
4139 | else if (C == B) // (A & C)|(C & D) == C & (A|D) | ||||
4140 | V1 = C, V2 = A, V3 = D; | ||||
4141 | else if (C == D) // (A & C)|(B & C) == C & (A|B) | ||||
4142 | V1 = C, V2 = A, V3 = B; | ||||
4143 | |||||
4144 | if (V1) { | ||||
4145 | Value *Or = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4146 | InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I); |
4147 | return BinaryOperator::CreateAnd(V1, Or); | ||||
Chris Lattner | 0b7c0bf | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 4148 | } |
Chris Lattner | c5e7ea4 | 2007-04-08 07:47:01 +0000 | [diff] [blame] | 4149 | } |
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4150 | } |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4151 | |
4152 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4153 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
4154 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) | ||||
4155 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && | ||||
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4156 | SI0->getOperand(1) == SI1->getOperand(1) && |
4157 | (SI0->hasOneUse() || SI1->hasOneUse())) { | ||||
4158 | Instruction *NewOp = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4159 | InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0), |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4160 | SI1->getOperand(0), |
4161 | SI0->getName()), I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4162 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4163 | SI1->getOperand(1)); |
Chris Lattner | e511b74 | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4164 | } |
4165 | } | ||||
Chris Lattner | 67ca768 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 4166 | |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4167 | if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 |
4168 | if (A == Op1) // ~A | A == -1 | ||||
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4169 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4170 | } else { |
4171 | A = 0; | ||||
4172 | } | ||||
Chris Lattner | f4d4c87 | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 4173 | // Note, A is still live here! |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4174 | if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B |
4175 | if (Op0 == B) | ||||
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4176 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4177 | |
Misha Brukman | cb6267b | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 4178 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4179 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4180 | Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B, |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4181 | I.getName()+".demorgan"), I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4182 | return BinaryOperator::CreateNot(And); |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 4183 | } |
Chris Lattner | a27231a | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 4184 | } |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4185 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4186 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
4187 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { | ||||
4188 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4189 | return R; |
4190 | |||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4191 | Value *LHSVal, *RHSVal; |
4192 | ConstantInt *LHSCst, *RHSCst; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4193 | ICmpInst::Predicate LHSCC, RHSCC; |
4194 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) | ||||
4195 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) | ||||
4196 | if (LHSVal == RHSVal && // Found (X icmp C1) | (X icmp C2) | ||||
4197 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. | ||||
4198 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && | ||||
4199 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && | ||||
4200 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && | ||||
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4201 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE && |
4202 | // We can't fold (ugt x, C) | (sgt x, C2). | ||||
4203 | PredicatesFoldable(LHSCC, RHSCC)) { | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4204 | // Ensure that the larger constant is on the RHS. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4205 | ICmpInst *LHS = cast<ICmpInst>(Op0); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4206 | bool NeedsSwap; |
4207 | if (ICmpInst::isSignedPredicate(LHSCC)) | ||||
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4208 | NeedsSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4209 | else |
Chris Lattner | 3aea1bd | 2007-05-11 16:58:45 +0000 | [diff] [blame] | 4210 | NeedsSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
Chris Lattner | 8885887 | 2007-05-11 05:55:56 +0000 | [diff] [blame] | 4211 | |
4212 | if (NeedsSwap) { | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4213 | std::swap(LHS, RHS); |
4214 | std::swap(LHSCst, RHSCst); | ||||
4215 | std::swap(LHSCC, RHSCC); | ||||
4216 | } | ||||
4217 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4218 | // At this point, we know we have have two icmp instructions |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4219 | // comparing a value against two constants and or'ing the result |
4220 | // together. Because of the above check, we know that we only have | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4221 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
4222 | // FoldICmpLogical check above), that the two constants are not | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4223 | // equal. |
4224 | assert(LHSCst != RHSCst && "Compares not folded above?"); | ||||
4225 | |||||
4226 | switch (LHSCC) { | ||||
4227 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4228 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4229 | switch (RHSCC) { |
4230 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4231 | case ICmpInst::ICMP_EQ: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4232 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 |
4233 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4234 | Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST, |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4235 | LHSVal->getName()+".off"); |
4236 | InsertNewInstBefore(Add, I); | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4237 | AddCST = Subtract(AddOne(RHSCst), LHSCst); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4238 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4239 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4240 | break; // (X == 13 | X == 15) -> no change |
4241 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change | ||||
4242 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change | ||||
Chris Lattner | 240d6f4 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 4243 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4244 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
4245 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 | ||||
4246 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4247 | return ReplaceInstUsesWith(I, RHS); |
4248 | } | ||||
4249 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4250 | case ICmpInst::ICMP_NE: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4251 | switch (RHSCC) { |
4252 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4253 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
4254 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 | ||||
4255 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4256 | return ReplaceInstUsesWith(I, LHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4257 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
4258 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true | ||||
4259 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4260 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4261 | } |
4262 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4263 | case ICmpInst::ICMP_ULT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4264 | switch (RHSCC) { |
4265 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4266 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4267 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4268 | 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] | 4269 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
4270 | // this can cause overflow. | ||||
4271 | if (RHSCst->isMaxValue(false)) | ||||
4272 | return ReplaceInstUsesWith(I, LHS); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4273 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, |
4274 | false, I); | ||||
4275 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change | ||||
4276 | break; | ||||
4277 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 | ||||
4278 | 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] | 4279 | return ReplaceInstUsesWith(I, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4280 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
4281 | break; | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4282 | } |
4283 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4284 | case ICmpInst::ICMP_SLT: |
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4285 | switch (RHSCC) { |
4286 | default: assert(0 && "Unknown integer condition code!"); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4287 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
4288 | break; | ||||
4289 | 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] | 4290 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
4291 | // this can cause overflow. | ||||
4292 | if (RHSCst->isMaxValue(true)) | ||||
4293 | return ReplaceInstUsesWith(I, LHS); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4294 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, |
4295 | false, I); | ||||
4296 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change | ||||
4297 | break; | ||||
4298 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 | ||||
4299 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 | ||||
4300 | return ReplaceInstUsesWith(I, RHS); | ||||
4301 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change | ||||
4302 | break; | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4303 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4304 | break; |
4305 | case ICmpInst::ICMP_UGT: | ||||
4306 | switch (RHSCC) { | ||||
4307 | default: assert(0 && "Unknown integer condition code!"); | ||||
4308 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 | ||||
4309 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 | ||||
4310 | return ReplaceInstUsesWith(I, LHS); | ||||
4311 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change | ||||
4312 | break; | ||||
4313 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true | ||||
4314 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4315 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4316 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
4317 | break; | ||||
4318 | } | ||||
4319 | break; | ||||
4320 | case ICmpInst::ICMP_SGT: | ||||
4321 | switch (RHSCC) { | ||||
4322 | default: assert(0 && "Unknown integer condition code!"); | ||||
4323 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 | ||||
4324 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 | ||||
4325 | return ReplaceInstUsesWith(I, LHS); | ||||
4326 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change | ||||
4327 | break; | ||||
4328 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true | ||||
4329 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4330 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4331 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
4332 | break; | ||||
4333 | } | ||||
4334 | break; | ||||
Chris Lattner | b4f40d2 | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 4335 | } |
4336 | } | ||||
4337 | } | ||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4338 | |
4339 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) | ||||
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4340 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4341 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4342 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4343 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
4344 | !isa<ICmpInst>(Op1C->getOperand(0))) { | ||||
4345 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | ||||
4346 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && | ||||
4347 | // Only do this if the casts both really cause code to be | ||||
4348 | // generated. | ||||
4349 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), | ||||
4350 | I.getType(), TD) && | ||||
4351 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | ||||
4352 | I.getType(), TD)) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4353 | Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4354 | Op1C->getOperand(0), |
4355 | I.getName()); | ||||
4356 | InsertNewInstBefore(NewOp, I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4357 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4358 | } |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4359 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4360 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4361 | } |
4362 | |||||
4363 | |||||
4364 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) | ||||
4365 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { | ||||
4366 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) { | ||||
4367 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && | ||||
Chris Lattner | 5ebd936 | 2008-02-29 06:09:11 +0000 | [diff] [blame] | 4368 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
4369 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) | ||||
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4370 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
4371 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { | ||||
4372 | // If either of the constants are nans, then the whole thing returns | ||||
4373 | // true. | ||||
Chris Lattner | be3e348 | 2007-10-24 18:54:45 +0000 | [diff] [blame] | 4374 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4375 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
4376 | |||||
4377 | // Otherwise, no need to compare the two constants, compare the | ||||
4378 | // rest. | ||||
4379 | return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0), | ||||
4380 | RHS->getOperand(0)); | ||||
4381 | } | ||||
4382 | } | ||||
4383 | } | ||||
Chris Lattner | e9bed7d | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 4384 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4385 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4386 | } |
4387 | |||||
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4388 | namespace { |
4389 | |||||
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4390 | // XorSelf - Implements: X ^ X --> 0 |
4391 | struct XorSelf { | ||||
4392 | Value *RHS; | ||||
4393 | XorSelf(Value *rhs) : RHS(rhs) {} | ||||
4394 | bool shouldApply(Value *LHS) const { return LHS == RHS; } | ||||
4395 | Instruction *apply(BinaryOperator &Xor) const { | ||||
4396 | return &Xor; | ||||
4397 | } | ||||
4398 | }; | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4399 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 4400 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4401 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4402 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Chris Lattner | 4f98c56 | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 4403 | bool Changed = SimplifyCommutative(I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4404 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4405 | |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4406 | if (isa<UndefValue>(Op1)) { |
4407 | if (isa<UndefValue>(Op0)) | ||||
4408 | // Handle undef ^ undef -> 0 special case. This is a common | ||||
4409 | // idiom (misuse). | ||||
4410 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4411 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
Evan Cheng | d34af78 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 4412 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4413 | |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4414 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
4415 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { | ||||
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 4416 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 4417 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | c317d39 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 4418 | } |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4419 | |
4420 | // See if we can simplify any instructions used by the instruction whose sole | ||||
4421 | // purpose is to compute bits we don't care about. | ||||
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4422 | if (!isa<VectorType>(I.getType())) { |
4423 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | ||||
4424 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
4425 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | ||||
4426 | KnownZero, KnownOne)) | ||||
4427 | return &I; | ||||
Chris Lattner | 041a6c9 | 2007-06-15 05:26:55 +0000 | [diff] [blame] | 4428 | } else if (isa<ConstantAggregateZero>(Op1)) { |
4429 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X | ||||
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4430 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4431 | |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4432 | // Is this a ~ operation? |
4433 | if (Value *NotOp = dyn_castNotVal(&I)) { | ||||
4434 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law | ||||
4435 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law | ||||
4436 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { | ||||
4437 | if (Op0I->getOpcode() == Instruction::And || | ||||
4438 | Op0I->getOpcode() == Instruction::Or) { | ||||
4439 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); | ||||
4440 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { | ||||
4441 | Instruction *NotY = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4442 | BinaryOperator::CreateNot(Op0I->getOperand(1), |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4443 | Op0I->getOperand(1)->getName()+".not"); |
4444 | InsertNewInstBefore(NotY, I); | ||||
4445 | if (Op0I->getOpcode() == Instruction::And) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4446 | return BinaryOperator::CreateOr(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4447 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4448 | return BinaryOperator::CreateAnd(Op0NotVal, NotY); |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4449 | } |
4450 | } | ||||
4451 | } | ||||
4452 | } | ||||
4453 | |||||
4454 | |||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4455 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4456 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
4457 | if (RHS == ConstantInt::getTrue() && Op0->hasOneUse()) { | ||||
4458 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4459 | return new ICmpInst(ICI->getInversePredicate(), |
4460 | ICI->getOperand(0), ICI->getOperand(1)); | ||||
Chris Lattner | ad5b4fb | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 4461 | |
Nick Lewycky | f947b3e | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 4462 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
4463 | return new FCmpInst(FCI->getInversePredicate(), | ||||
4464 | FCI->getOperand(0), FCI->getOperand(1)); | ||||
4465 | } | ||||
4466 | |||||
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 4467 | // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). |
4468 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { | ||||
4469 | if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { | ||||
4470 | if (CI->hasOneUse() && Op0C->hasOneUse()) { | ||||
4471 | Instruction::CastOps Opcode = Op0C->getOpcode(); | ||||
4472 | if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { | ||||
4473 | if (RHS == ConstantExpr::getCast(Opcode, ConstantInt::getTrue(), | ||||
4474 | Op0C->getDestTy())) { | ||||
4475 | Instruction *NewCI = InsertNewInstBefore(CmpInst::Create( | ||||
4476 | CI->getOpcode(), CI->getInversePredicate(), | ||||
4477 | CI->getOperand(0), CI->getOperand(1)), I); | ||||
4478 | NewCI->takeName(CI); | ||||
4479 | return CastInst::Create(Opcode, NewCI, Op0C->getType()); | ||||
4480 | } | ||||
4481 | } | ||||
4482 | } | ||||
4483 | } | ||||
4484 | } | ||||
4485 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4486 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
Chris Lattner | d65460f | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 4487 | // ~(c-X) == X-c-1 == X+(-c-1) |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4488 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
4489 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { | ||||
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4490 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
4491 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, | ||||
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4492 | ConstantInt::get(I.getType(), 1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4493 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4494 | } |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4495 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4496 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
Chris Lattner | f8c36f5 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4497 | if (Op0I->getOpcode() == Instruction::Add) { |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4498 | // ~(X-c) --> (-c-1)-X |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4499 | if (RHS->isAllOnesValue()) { |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4500 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4501 | return BinaryOperator::CreateSub( |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4502 | ConstantExpr::getSub(NegOp0CI, |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4503 | ConstantInt::get(I.getType(), 1)), |
Chris Lattner | 689d24b | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4504 | Op0I->getOperand(0)); |
Chris Lattner | acf4e07 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4505 | } else if (RHS->getValue().isSignBit()) { |
Chris Lattner | 5c6e2db | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4506 | // (X + C) ^ signbit -> (X + C + signbit) |
4507 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4508 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4509 | |
Chris Lattner | 7c4049c | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4510 | } |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4511 | } else if (Op0I->getOpcode() == Instruction::Or) { |
4512 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 | ||||
Reid Spencer | a03d45f | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4513 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4514 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); |
4515 | // Anything in both C1 and C2 is known to be zero, remove it from | ||||
4516 | // NewRHS. | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4517 | Constant *CommonBits = And(Op0CI, RHS); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4518 | NewRHS = ConstantExpr::getAnd(NewRHS, |
4519 | ConstantExpr::getNot(CommonBits)); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4520 | AddToWorkList(Op0I); |
Chris Lattner | 02bd1b3 | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4521 | I.setOperand(0, Op0I->getOperand(0)); |
4522 | I.setOperand(1, NewRHS); | ||||
4523 | return &I; | ||||
4524 | } | ||||
Chris Lattner | eca0c5c | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4525 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4526 | } |
Chris Lattner | 05bd1b2 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4527 | } |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4528 | |
4529 | // Try to fold constant and into select arguments. | ||||
4530 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4531 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4532 | return R; |
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4533 | if (isa<PHINode>(Op0)) |
4534 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
4535 | return NV; | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4536 | } |
4537 | |||||
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4538 | if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4539 | if (X == Op1) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4540 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4541 | |
Chris Lattner | 8d96964 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4542 | if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1 |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4543 | if (X == Op0) |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 4544 | return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); |
Chris Lattner | a288196 | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4545 | |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4546 | |
4547 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); | ||||
4548 | if (Op1I) { | ||||
4549 | Value *A, *B; | ||||
4550 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { | ||||
4551 | if (A == Op0) { // B^(B|A) == (A|B)^B | ||||
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4552 | Op1I->swapOperands(); |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4553 | I.swapOperands(); |
4554 | std::swap(Op0, Op1); | ||||
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4555 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4556 | I.swapOperands(); // Simplified below. |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4557 | std::swap(Op0, Op1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4558 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4559 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { |
4560 | if (Op0 == A) // A^(A^B) == B | ||||
4561 | return ReplaceInstUsesWith(I, B); | ||||
4562 | else if (Op0 == B) // A^(B^A) == B | ||||
4563 | return ReplaceInstUsesWith(I, A); | ||||
4564 | } 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] | 4565 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4566 | Op1I->swapOperands(); |
Chris Lattner | 6abbdf9 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4567 | std::swap(A, B); |
4568 | } | ||||
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4569 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4570 | I.swapOperands(); // Simplified below. |
4571 | std::swap(Op0, Op1); | ||||
4572 | } | ||||
Chris Lattner | 26ca7e1 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4573 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4574 | } |
4575 | |||||
4576 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); | ||||
4577 | if (Op0I) { | ||||
4578 | Value *A, *B; | ||||
4579 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { | ||||
4580 | if (A == Op1) // (B|A)^B == (A|B)^B | ||||
4581 | std::swap(A, B); | ||||
4582 | if (B == Op1) { // (A|B)^B == A & ~B | ||||
4583 | Instruction *NotB = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4584 | InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I); |
4585 | return BinaryOperator::CreateAnd(A, NotB); | ||||
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4586 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4587 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { |
4588 | if (Op1 == A) // (A^B)^A == B | ||||
4589 | return ReplaceInstUsesWith(I, B); | ||||
4590 | else if (Op1 == B) // (B^A)^A == B | ||||
4591 | return ReplaceInstUsesWith(I, A); | ||||
4592 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ | ||||
4593 | if (A == Op1) // (A&B)^A -> (B&A)^A | ||||
4594 | std::swap(A, B); | ||||
4595 | if (B == Op1 && // (B&A)^A == ~B & A | ||||
Chris Lattner | ae1ab39 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4596 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4597 | Instruction *N = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4598 | InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I); |
4599 | return BinaryOperator::CreateAnd(N, Op1); | ||||
Chris Lattner | 64daab5 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4600 | } |
Chris Lattner | cb40a37 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4601 | } |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4602 | } |
4603 | |||||
4604 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. | ||||
4605 | if (Op0I && Op1I && Op0I->isShift() && | ||||
4606 | Op0I->getOpcode() == Op1I->getOpcode() && | ||||
4607 | Op0I->getOperand(1) == Op1I->getOperand(1) && | ||||
4608 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { | ||||
4609 | Instruction *NewOp = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4610 | InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0), |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4611 | Op1I->getOperand(0), |
4612 | Op0I->getName()), I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4613 | return BinaryOperator::Create(Op1I->getOpcode(), NewOp, |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4614 | Op1I->getOperand(1)); |
4615 | } | ||||
4616 | |||||
4617 | if (Op0I && Op1I) { | ||||
4618 | Value *A, *B, *C, *D; | ||||
4619 | // (A & B)^(A | B) -> A ^ B | ||||
4620 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && | ||||
4621 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { | ||||
4622 | if ((A == C && B == D) || (A == D && B == C)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4623 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4624 | } |
4625 | // (A | B)^(A & B) -> A ^ B | ||||
4626 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && | ||||
4627 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { | ||||
4628 | if ((A == C && B == D) || (A == D && B == C)) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4629 | return BinaryOperator::CreateXor(A, B); |
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4630 | } |
4631 | |||||
4632 | // (A & B)^(C & D) | ||||
4633 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && | ||||
4634 | match(Op0I, m_And(m_Value(A), m_Value(B))) && | ||||
4635 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { | ||||
4636 | // (X & Y)^(X & Y) -> (Y^Z) & X | ||||
4637 | Value *X = 0, *Y = 0, *Z = 0; | ||||
4638 | if (A == C) | ||||
4639 | X = A, Y = B, Z = D; | ||||
4640 | else if (A == D) | ||||
4641 | X = A, Y = B, Z = C; | ||||
4642 | else if (B == C) | ||||
4643 | X = B, Y = A, Z = D; | ||||
4644 | else if (B == D) | ||||
4645 | X = B, Y = A, Z = C; | ||||
4646 | |||||
4647 | if (X) { | ||||
4648 | Instruction *NewOp = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4649 | InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I); |
4650 | return BinaryOperator::CreateAnd(NewOp, X); | ||||
Chris Lattner | 318bf79 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4651 | } |
4652 | } | ||||
4653 | } | ||||
4654 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4655 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
4656 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) | ||||
4657 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | ||||
Chris Lattner | aa9c1f1 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4658 | return R; |
4659 | |||||
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4660 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4661 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4662 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4663 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
4664 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | ||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4665 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4666 | // 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] | 4667 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
4668 | I.getType(), TD) && | ||||
4669 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | ||||
4670 | I.getType(), TD)) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4671 | Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0), |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4672 | Op1C->getOperand(0), |
4673 | I.getName()); | ||||
4674 | InsertNewInstBefore(NewOp, I); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4675 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
Reid Spencer | 5ae9ceb | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4676 | } |
Chris Lattner | 6fc205f | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4677 | } |
Chris Lattner | 99c6574 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4678 | } |
Nick Lewycky | 517e1f5 | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4680 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4681 | } |
4682 | |||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4683 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
4684 | /// overflowed for this type. | ||||
4685 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, | ||||
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4686 | ConstantInt *In2, bool IsSigned = false) { |
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4687 | Result = cast<ConstantInt>(Add(In1, In2)); |
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4688 | |
Reid Spencer | e4e4003 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4689 | if (IsSigned) |
4690 | if (In2->getValue().isNegative()) | ||||
4691 | return Result->getValue().sgt(In1->getValue()); | ||||
4692 | else | ||||
4693 | return Result->getValue().slt(In1->getValue()); | ||||
4694 | else | ||||
4695 | return Result->getValue().ult(In1->getValue()); | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4696 | } |
4697 | |||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4698 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
4699 | /// code necessary to compute the offset from the base pointer (without adding | ||||
4700 | /// in the base pointer). Return the result as a signed integer of intptr size. | ||||
4701 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { | ||||
4702 | TargetData &TD = IC.getTargetData(); | ||||
4703 | gep_type_iterator GTI = gep_type_begin(GEP); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4704 | const Type *IntPtrTy = TD.getIntPtrType(); |
4705 | Value *Result = Constant::getNullValue(IntPtrTy); | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4706 | |
4707 | // Build a mask for high order bits. | ||||
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4708 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4709 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4710 | |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 4711 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
4712 | ++i, ++GTI) { | ||||
4713 | Value *Op = *i; | ||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4714 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask; |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4715 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
4716 | if (OpC->isZero()) continue; | ||||
4717 | |||||
4718 | // Handle a struct index, which adds its field offset to the pointer. | ||||
4719 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
4720 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); | ||||
4721 | |||||
4722 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) | ||||
4723 | Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size)); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4724 | else |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4725 | Result = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4726 | BinaryOperator::CreateAdd(Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4727 | ConstantInt::get(IntPtrTy, Size), |
4728 | GEP->getName()+".offs"), I); | ||||
4729 | continue; | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4730 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4731 | |
4732 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); | ||||
4733 | Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); | ||||
4734 | Scale = ConstantExpr::getMul(OC, Scale); | ||||
4735 | if (Constant *RC = dyn_cast<Constant>(Result)) | ||||
4736 | Result = ConstantExpr::getAdd(RC, Scale); | ||||
4737 | else { | ||||
4738 | // Emit an add instruction. | ||||
4739 | Result = IC.InsertNewInstBefore( | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4740 | BinaryOperator::CreateAdd(Result, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4741 | GEP->getName()+".offs"), I); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 4742 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4743 | continue; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4744 | } |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4745 | // Convert to correct type. |
4746 | if (Op->getType() != IntPtrTy) { | ||||
4747 | if (Constant *OpC = dyn_cast<Constant>(Op)) | ||||
4748 | Op = ConstantExpr::getSExt(OpC, IntPtrTy); | ||||
4749 | else | ||||
4750 | Op = IC.InsertNewInstBefore(new SExtInst(Op, IntPtrTy, | ||||
4751 | Op->getName()+".c"), I); | ||||
4752 | } | ||||
4753 | if (Size != 1) { | ||||
4754 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); | ||||
4755 | if (Constant *OpC = dyn_cast<Constant>(Op)) | ||||
4756 | Op = ConstantExpr::getMul(OpC, Scale); | ||||
4757 | else // We'll let instcombine(mul) convert this to a shl if possible. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4758 | Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4759 | GEP->getName()+".idx"), I); |
4760 | } | ||||
4761 | |||||
4762 | // Emit an add instruction. | ||||
4763 | if (isa<Constant>(Op) && isa<Constant>(Result)) | ||||
4764 | Result = ConstantExpr::getAdd(cast<Constant>(Op), | ||||
4765 | cast<Constant>(Result)); | ||||
4766 | else | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4767 | Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result, |
Chris Lattner | e62f021 | 2007-04-28 04:52:43 +0000 | [diff] [blame] | 4768 | GEP->getName()+".offs"), I); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4769 | } |
4770 | return Result; | ||||
4771 | } | ||||
4772 | |||||
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4773 | |
4774 | /// EvaluateGEPOffsetExpression - Return an value that can be used to compare of | ||||
4775 | /// the *offset* implied by GEP to zero. For example, if we have &A[i], we want | ||||
4776 | /// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be | ||||
4777 | /// complex, and scales are involved. The above expression would also be legal | ||||
4778 | /// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This | ||||
4779 | /// later form is less amenable to optimization though, and we are allowed to | ||||
4780 | /// generate the first by knowing that pointer arithmetic doesn't overflow. | ||||
4781 | /// | ||||
4782 | /// If we can't emit an optimized form for this expression, this returns null. | ||||
4783 | /// | ||||
4784 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, | ||||
4785 | InstCombiner &IC) { | ||||
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4786 | TargetData &TD = IC.getTargetData(); |
4787 | gep_type_iterator GTI = gep_type_begin(GEP); | ||||
4788 | |||||
4789 | // Check to see if this gep only has a single variable index. If so, and if | ||||
4790 | // any constant indices are a multiple of its scale, then we can compute this | ||||
4791 | // in terms of the scale of the variable index. For example, if the GEP | ||||
4792 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", | ||||
4793 | // because the expression will cross zero at the same point. | ||||
4794 | unsigned i, e = GEP->getNumOperands(); | ||||
4795 | int64_t Offset = 0; | ||||
4796 | for (i = 1; i != e; ++i, ++GTI) { | ||||
4797 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { | ||||
4798 | // Compute the aggregate offset of constant indices. | ||||
4799 | if (CI->isZero()) continue; | ||||
4800 | |||||
4801 | // Handle a struct index, which adds its field offset to the pointer. | ||||
4802 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
4803 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); | ||||
4804 | } else { | ||||
4805 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); | ||||
4806 | Offset += Size*CI->getSExtValue(); | ||||
4807 | } | ||||
4808 | } else { | ||||
4809 | // Found our variable index. | ||||
4810 | break; | ||||
4811 | } | ||||
4812 | } | ||||
4813 | |||||
4814 | // If there are no variable indices, we must have a constant offset, just | ||||
4815 | // evaluate it the general way. | ||||
4816 | if (i == e) return 0; | ||||
4817 | |||||
4818 | Value *VariableIdx = GEP->getOperand(i); | ||||
4819 | // Determine the scale factor of the variable element. For example, this is | ||||
4820 | // 4 if the variable index is into an array of i32. | ||||
4821 | uint64_t VariableScale = TD.getABITypeSize(GTI.getIndexedType()); | ||||
4822 | |||||
4823 | // Verify that there are no other variable indices. If so, emit the hard way. | ||||
4824 | for (++i, ++GTI; i != e; ++i, ++GTI) { | ||||
4825 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); | ||||
4826 | if (!CI) return 0; | ||||
4827 | |||||
4828 | // Compute the aggregate offset of constant indices. | ||||
4829 | if (CI->isZero()) continue; | ||||
4830 | |||||
4831 | // Handle a struct index, which adds its field offset to the pointer. | ||||
4832 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { | ||||
4833 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); | ||||
4834 | } else { | ||||
4835 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); | ||||
4836 | Offset += Size*CI->getSExtValue(); | ||||
4837 | } | ||||
4838 | } | ||||
4839 | |||||
4840 | // Okay, we know we have a single variable index, which must be a | ||||
4841 | // pointer/array/vector index. If there is no offset, life is simple, return | ||||
4842 | // the index. | ||||
4843 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); | ||||
4844 | if (Offset == 0) { | ||||
4845 | // Cast to intptrty in case a truncation occurs. If an extension is needed, | ||||
4846 | // we don't need to bother extending: the extension won't affect where the | ||||
4847 | // computation crosses zero. | ||||
4848 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) | ||||
4849 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), | ||||
4850 | VariableIdx->getNameStart(), &I); | ||||
4851 | return VariableIdx; | ||||
4852 | } | ||||
4853 | |||||
4854 | // Otherwise, there is an index. The computation we will do will be modulo | ||||
4855 | // the pointer size, so get it. | ||||
4856 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); | ||||
4857 | |||||
4858 | Offset &= PtrSizeMask; | ||||
4859 | VariableScale &= PtrSizeMask; | ||||
4860 | |||||
4861 | // To do this transformation, any constant index must be a multiple of the | ||||
4862 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", | ||||
4863 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a | ||||
4864 | // multiple of the variable scale. | ||||
4865 | int64_t NewOffs = Offset / (int64_t)VariableScale; | ||||
4866 | if (Offset != NewOffs*(int64_t)VariableScale) | ||||
4867 | return 0; | ||||
4868 | |||||
4869 | // Okay, we can do this evaluation. Start by converting the index to intptr. | ||||
4870 | const Type *IntPtrTy = TD.getIntPtrType(); | ||||
4871 | if (VariableIdx->getType() != IntPtrTy) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4872 | VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy, |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4873 | true /*SExt*/, |
4874 | VariableIdx->getNameStart(), &I); | ||||
4875 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4876 | return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I); |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4877 | } |
4878 | |||||
4879 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4880 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4881 | /// 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] | 4882 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
4883 | ICmpInst::Predicate Cond, | ||||
4884 | Instruction &I) { | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4885 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4886 | |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4887 | // Look through bitcasts. |
4888 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) | ||||
4889 | RHS = BCI->getOperand(0); | ||||
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4890 | |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4891 | Value *PtrBase = GEPLHS->getOperand(0); |
4892 | if (PtrBase == RHS) { | ||||
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4893 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
Chris Lattner | 10c0d91 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 4894 | // This transformation (ignoring the base and scales) is valid because we |
4895 | // know pointers can't overflow. See if we can output an optimized form. | ||||
4896 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); | ||||
4897 | |||||
4898 | // If not, synthesize the offset the hard way. | ||||
4899 | if (Offset == 0) | ||||
4900 | Offset = EmitGEPOffset(GEPLHS, I, *this); | ||||
Chris Lattner | 7c95deb | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 4901 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
4902 | Constant::getNullValue(Offset->getType())); | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4903 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4904 | // If the base pointers are different, but the indices are the same, just |
4905 | // compare the base pointer. | ||||
4906 | if (PtrBase != GEPRHS->getOperand(0)) { | ||||
4907 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); | ||||
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4908 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
Chris Lattner | 93b94a6 | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4909 | GEPRHS->getOperand(0)->getType(); |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4910 | if (IndicesTheSame) |
4911 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) | ||||
4912 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { | ||||
4913 | IndicesTheSame = false; | ||||
4914 | break; | ||||
4915 | } | ||||
4916 | |||||
4917 | // If all indices are the same, just compare the base pointers. | ||||
4918 | if (IndicesTheSame) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4919 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
4920 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); | ||||
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4921 | |
4922 | // Otherwise, the base pointers are different and the indices are | ||||
4923 | // different, bail out. | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4924 | return 0; |
Chris Lattner | a70b66d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4925 | } |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4926 | |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4927 | // If one of the GEPs has all zero indices, recurse. |
4928 | bool AllZeros = true; | ||||
4929 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) | ||||
4930 | if (!isa<Constant>(GEPLHS->getOperand(i)) || | ||||
4931 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { | ||||
4932 | AllZeros = false; | ||||
4933 | break; | ||||
4934 | } | ||||
4935 | if (AllZeros) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4936 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
4937 | ICmpInst::getSwappedPredicate(Cond), I); | ||||
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4938 | |
4939 | // If the other GEP has all zero indices, recurse. | ||||
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4940 | AllZeros = true; |
4941 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) | ||||
4942 | if (!isa<Constant>(GEPRHS->getOperand(i)) || | ||||
4943 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { | ||||
4944 | AllZeros = false; | ||||
4945 | break; | ||||
4946 | } | ||||
4947 | if (AllZeros) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4948 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | e9d782b | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4949 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4950 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
4951 | // If the GEPs only differ by one index, compare it. | ||||
4952 | unsigned NumDifferences = 0; // Keep track of # differences. | ||||
4953 | unsigned DiffOperand = 0; // The operand that differs. | ||||
4954 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) | ||||
4955 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4956 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
4957 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { | ||||
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4958 | // Irreconcilable differences. |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4959 | NumDifferences = 2; |
4960 | break; | ||||
4961 | } else { | ||||
4962 | if (NumDifferences++) break; | ||||
4963 | DiffOperand = i; | ||||
4964 | } | ||||
4965 | } | ||||
4966 | |||||
4967 | if (NumDifferences == 0) // SAME GEP? | ||||
4968 | return ReplaceInstUsesWith(I, // No comparison is needed here. | ||||
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 4969 | ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 4970 | ICmpInst::isTrueWhenEqual(Cond))); |
Nick Lewycky | 455e176 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4972 | else if (NumDifferences == 1) { |
Chris Lattner | 45f57b8 | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4973 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
4974 | Value *RHSV = GEPRHS->getOperand(DiffOperand); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4975 | // Make sure we do a signed comparison here. |
4976 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); | ||||
Chris Lattner | 4401c9c | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4977 | } |
4978 | } | ||||
4979 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4980 | // 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] | 4981 | // the result to fold to a constant! |
4982 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && | ||||
4983 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { | ||||
4984 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) | ||||
4985 | Value *L = EmitGEPOffset(GEPLHS, I, *this); | ||||
4986 | Value *R = EmitGEPOffset(GEPRHS, I, *this); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4987 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4988 | } |
4989 | } | ||||
4990 | return 0; | ||||
4991 | } | ||||
4992 | |||||
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 4993 | /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible. |
4994 | /// | ||||
4995 | Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, | ||||
4996 | Instruction *LHSI, | ||||
4997 | Constant *RHSC) { | ||||
4998 | if (!isa<ConstantFP>(RHSC)) return 0; | ||||
4999 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); | ||||
5000 | |||||
5001 | // Get the width of the mantissa. We don't want to hack on conversions that | ||||
5002 | // might lose information from the integer, e.g. "i64 -> float" | ||||
Chris Lattner | 7be1c45 | 2008-05-19 21:17:23 +0000 | [diff] [blame] | 5003 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5004 | if (MantissaWidth == -1) return 0; // Unknown. |
5005 | |||||
5006 | // Check to see that the input is converted from an integer type that is small | ||||
5007 | // enough that preserves all bits. TODO: check here for "known" sign bits. | ||||
5008 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. | ||||
5009 | unsigned InputSize = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); | ||||
5010 | |||||
5011 | // If this is a uitofp instruction, we need an extra bit to hold the sign. | ||||
5012 | if (isa<UIToFPInst>(LHSI)) | ||||
5013 | ++InputSize; | ||||
5014 | |||||
5015 | // If the conversion would lose info, don't hack on this. | ||||
5016 | if ((int)InputSize > MantissaWidth) | ||||
5017 | return 0; | ||||
5018 | |||||
5019 | // Otherwise, we can potentially simplify the comparison. We know that it | ||||
5020 | // will always come through as an integer value and we know the constant is | ||||
5021 | // not a NAN (it would have been previously simplified). | ||||
5022 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); | ||||
5023 | |||||
5024 | ICmpInst::Predicate Pred; | ||||
5025 | switch (I.getPredicate()) { | ||||
5026 | default: assert(0 && "Unexpected predicate!"); | ||||
5027 | case FCmpInst::FCMP_UEQ: | ||||
5028 | case FCmpInst::FCMP_OEQ: Pred = ICmpInst::ICMP_EQ; break; | ||||
5029 | case FCmpInst::FCMP_UGT: | ||||
5030 | case FCmpInst::FCMP_OGT: Pred = ICmpInst::ICMP_SGT; break; | ||||
5031 | case FCmpInst::FCMP_UGE: | ||||
5032 | case FCmpInst::FCMP_OGE: Pred = ICmpInst::ICMP_SGE; break; | ||||
5033 | case FCmpInst::FCMP_ULT: | ||||
5034 | case FCmpInst::FCMP_OLT: Pred = ICmpInst::ICMP_SLT; break; | ||||
5035 | case FCmpInst::FCMP_ULE: | ||||
5036 | case FCmpInst::FCMP_OLE: Pred = ICmpInst::ICMP_SLE; break; | ||||
5037 | case FCmpInst::FCMP_UNE: | ||||
5038 | case FCmpInst::FCMP_ONE: Pred = ICmpInst::ICMP_NE; break; | ||||
5039 | case FCmpInst::FCMP_ORD: | ||||
5040 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
5041 | case FCmpInst::FCMP_UNO: | ||||
5042 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
5043 | } | ||||
5044 | |||||
5045 | const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); | ||||
5046 | |||||
5047 | // Now we know that the APFloat is a normal number, zero or inf. | ||||
5048 | |||||
Chris Lattner | 8516278 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5049 | // See if the FP constant is too large for the integer. For example, |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5050 | // comparing an i8 to 300.0. |
5051 | unsigned IntWidth = IntTy->getPrimitiveSizeInBits(); | ||||
5052 | |||||
5053 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF | ||||
5054 | // and large values. | ||||
5055 | APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false); | ||||
5056 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, | ||||
5057 | APFloat::rmNearestTiesToEven); | ||||
5058 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 | ||||
Chris Lattner | 393f7eb | 2008-05-24 04:06:28 +0000 | [diff] [blame] | 5059 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
5060 | Pred == ICmpInst::ICMP_SLE) | ||||
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5061 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
5062 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
5063 | } | ||||
5064 | |||||
5065 | // See if the RHS value is < SignedMin. | ||||
5066 | APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false); | ||||
5067 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, | ||||
5068 | APFloat::rmNearestTiesToEven); | ||||
5069 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 | ||||
Chris Lattner | 393f7eb | 2008-05-24 04:06:28 +0000 | [diff] [blame] | 5070 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
5071 | Pred == ICmpInst::ICMP_SGE) | ||||
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5072 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); |
5073 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
5074 | } | ||||
5075 | |||||
5076 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] but | ||||
5077 | // it may still be fractional. See if it is fractional by casting the FP | ||||
5078 | // value to the integer value and back, checking for equality. Don't do this | ||||
5079 | // for zero, because -0.0 is not fractional. | ||||
5080 | Constant *RHSInt = ConstantExpr::getFPToSI(RHSC, IntTy); | ||||
5081 | if (!RHS.isZero() && | ||||
5082 | ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) != RHSC) { | ||||
5083 | // If we had a comparison against a fractional value, we have to adjust | ||||
5084 | // the compare predicate and sometimes the value. RHSC is rounded towards | ||||
5085 | // zero at this point. | ||||
5086 | switch (Pred) { | ||||
5087 | default: assert(0 && "Unexpected integer comparison!"); | ||||
5088 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true | ||||
5089 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
5090 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false | ||||
5091 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
5092 | case ICmpInst::ICMP_SLE: | ||||
5093 | // (float)int <= 4.4 --> int <= 4 | ||||
5094 | // (float)int <= -4.4 --> int < -4 | ||||
5095 | if (RHS.isNegative()) | ||||
5096 | Pred = ICmpInst::ICMP_SLT; | ||||
5097 | break; | ||||
5098 | case ICmpInst::ICMP_SLT: | ||||
5099 | // (float)int < -4.4 --> int < -4 | ||||
5100 | // (float)int < 4.4 --> int <= 4 | ||||
5101 | if (!RHS.isNegative()) | ||||
5102 | Pred = ICmpInst::ICMP_SLE; | ||||
5103 | break; | ||||
5104 | case ICmpInst::ICMP_SGT: | ||||
5105 | // (float)int > 4.4 --> int > 4 | ||||
5106 | // (float)int > -4.4 --> int >= -4 | ||||
5107 | if (RHS.isNegative()) | ||||
5108 | Pred = ICmpInst::ICMP_SGE; | ||||
5109 | break; | ||||
5110 | case ICmpInst::ICMP_SGE: | ||||
5111 | // (float)int >= -4.4 --> int >= -4 | ||||
5112 | // (float)int >= 4.4 --> int > 4 | ||||
5113 | if (!RHS.isNegative()) | ||||
5114 | Pred = ICmpInst::ICMP_SGT; | ||||
5115 | break; | ||||
5116 | } | ||||
5117 | } | ||||
5118 | |||||
5119 | // Lower this FP comparison into an appropriate integer version of the | ||||
5120 | // comparison. | ||||
5121 | return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt); | ||||
5122 | } | ||||
5123 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5124 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
5125 | bool Changed = SimplifyCompare(I); | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5126 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5127 | |
Chris Lattner | 58e9746 | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 5128 | // Fold trivial predicates. |
5129 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) | ||||
5130 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); | ||||
5131 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) | ||||
5132 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
5133 | |||||
5134 | // Simplify 'fcmp pred X, X' | ||||
5135 | if (Op0 == Op1) { | ||||
5136 | switch (I.getPredicate()) { | ||||
5137 | default: assert(0 && "Unknown predicate!"); | ||||
5138 | case FCmpInst::FCMP_UEQ: // True if unordered or equal | ||||
5139 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal | ||||
5140 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal | ||||
5141 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
5142 | case FCmpInst::FCMP_OGT: // True if ordered and greater than | ||||
5143 | case FCmpInst::FCMP_OLT: // True if ordered and less than | ||||
5144 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal | ||||
5145 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
5146 | |||||
5147 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) | ||||
5148 | case FCmpInst::FCMP_ULT: // True if unordered or less than | ||||
5149 | case FCmpInst::FCMP_UGT: // True if unordered or greater than | ||||
5150 | case FCmpInst::FCMP_UNE: // True if unordered or not equal | ||||
5151 | // Canonicalize these to be 'fcmp uno %X, 0.0'. | ||||
5152 | I.setPredicate(FCmpInst::FCMP_UNO); | ||||
5153 | I.setOperand(1, Constant::getNullValue(Op0->getType())); | ||||
5154 | return &I; | ||||
5155 | |||||
5156 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) | ||||
5157 | case FCmpInst::FCMP_OEQ: // True if ordered and equal | ||||
5158 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal | ||||
5159 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal | ||||
5160 | // Canonicalize these to be 'fcmp ord %X, 0.0'. | ||||
5161 | I.setPredicate(FCmpInst::FCMP_ORD); | ||||
5162 | I.setOperand(1, Constant::getNullValue(Op0->getType())); | ||||
5163 | return &I; | ||||
5164 | } | ||||
5165 | } | ||||
5166 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5167 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5168 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5169 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5170 | // Handle fcmp with constant RHS |
5171 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { | ||||
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5172 | // If the constant is a nan, see if we can fold the comparison based on it. |
5173 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { | ||||
5174 | if (CFP->getValueAPF().isNaN()) { | ||||
5175 | if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and... | ||||
5176 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | ||||
Chris Lattner | 8516278 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5177 | assert(FCmpInst::isUnordered(I.getPredicate()) && |
5178 | "Comparison must be either ordered or unordered!"); | ||||
5179 | // True if unordered. | ||||
5180 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | ||||
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5181 | } |
5182 | } | ||||
5183 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5184 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
5185 | switch (LHSI->getOpcode()) { | ||||
5186 | case Instruction::PHI: | ||||
Chris Lattner | 7d8ab4e | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 5187 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
5188 | // block. If in the same block, we're encouraging jump threading. If | ||||
5189 | // not, we are just pessimizing the code by making an i1 phi. | ||||
5190 | if (LHSI->getParent() == I.getParent()) | ||||
5191 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
5192 | return NV; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5193 | break; |
Chris Lattner | a540623 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5194 | case Instruction::SIToFP: |
5195 | case Instruction::UIToFP: | ||||
5196 | if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC)) | ||||
5197 | return NV; | ||||
5198 | break; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5199 | case Instruction::Select: |
5200 | // If either operand of the select is a constant, we can fold the | ||||
5201 | // comparison into the select arms, which will cause one to be | ||||
5202 | // constant folded and the select turned into a bitwise or. | ||||
5203 | Value *Op1 = 0, *Op2 = 0; | ||||
5204 | if (LHSI->hasOneUse()) { | ||||
5205 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { | ||||
5206 | // Fold the known value into the constant operand. | ||||
5207 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); | ||||
5208 | // Insert a new FCmp of the other select operand. | ||||
5209 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), | ||||
5210 | LHSI->getOperand(2), RHSC, | ||||
5211 | I.getName()), I); | ||||
5212 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { | ||||
5213 | // Fold the known value into the constant operand. | ||||
5214 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); | ||||
5215 | // Insert a new FCmp of the other select operand. | ||||
5216 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), | ||||
5217 | LHSI->getOperand(1), RHSC, | ||||
5218 | I.getName()), I); | ||||
5219 | } | ||||
5220 | } | ||||
5221 | |||||
5222 | if (Op1) | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5223 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5224 | break; |
5225 | } | ||||
5226 | } | ||||
5227 | |||||
5228 | return Changed ? &I : 0; | ||||
5229 | } | ||||
5230 | |||||
5231 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { | ||||
5232 | bool Changed = SimplifyCompare(I); | ||||
5233 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
5234 | const Type *Ty = Op0->getType(); | ||||
5235 | |||||
5236 | // icmp X, X | ||||
5237 | if (Op0 == Op1) | ||||
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5238 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5239 | I.isTrueWhenEqual())); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5240 | |
5241 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef | ||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5242 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); |
Christopher Lamb | 7a0678c | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5243 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5244 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5245 | // addresses never equal each other! We already know that Op0 != Op1. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5246 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
5247 | isa<ConstantPointerNull>(Op0)) && | ||||
5248 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || | ||||
Chris Lattner | 711b340 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 5249 | isa<ConstantPointerNull>(Op1))) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5250 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, |
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5251 | !I.isTrueWhenEqual())); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5252 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5253 | // icmp's with boolean values can always be turned into bitwise operations |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5254 | if (Ty == Type::Int1Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5255 | switch (I.getPredicate()) { |
5256 | default: assert(0 && "Invalid icmp instruction!"); | ||||
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5257 | case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5258 | Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp"); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5259 | InsertNewInstBefore(Xor, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5260 | return BinaryOperator::CreateNot(Xor); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5261 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5262 | case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5263 | return BinaryOperator::CreateXor(Op0, Op1); |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5264 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5265 | case ICmpInst::ICMP_UGT: |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5266 | std::swap(Op0, Op1); // Change icmp ugt -> icmp ult |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5267 | // FALL THROUGH |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5268 | case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5269 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5270 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5271 | return BinaryOperator::CreateAnd(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5272 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5273 | case ICmpInst::ICMP_SGT: |
5274 | std::swap(Op0, Op1); // Change icmp sgt -> icmp slt | ||||
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5275 | // FALL THROUGH |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5276 | case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B |
5277 | Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp"); | ||||
5278 | InsertNewInstBefore(Not, I); | ||||
5279 | return BinaryOperator::CreateAnd(Not, Op0); | ||||
5280 | } | ||||
5281 | case ICmpInst::ICMP_UGE: | ||||
5282 | std::swap(Op0, Op1); // Change icmp uge -> icmp ule | ||||
5283 | // FALL THROUGH | ||||
5284 | case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5285 | Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp"); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5286 | InsertNewInstBefore(Not, I); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5287 | return BinaryOperator::CreateOr(Not, Op1); |
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5288 | } |
Chris Lattner | 85b5eb0 | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 5289 | case ICmpInst::ICMP_SGE: |
5290 | std::swap(Op0, Op1); // Change icmp sge -> icmp sle | ||||
5291 | // FALL THROUGH | ||||
5292 | case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B | ||||
5293 | Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp"); | ||||
5294 | InsertNewInstBefore(Not, I); | ||||
5295 | return BinaryOperator::CreateOr(Not, Op0); | ||||
5296 | } | ||||
Chris Lattner | 5dbef22 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 5297 | } |
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5298 | } |
5299 | |||||
Chris Lattner | 2be51ae | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 5300 | // See if we are doing a comparison between a constant and an instruction that |
5301 | // can be folded into the comparison. | ||||
Chris Lattner | 8b17094 | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 5302 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 5303 | Value *A, *B; |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5304 | |
Chris Lattner | b656601 | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 5305 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
5306 | if (I.isEquality() && CI->isNullValue() && | ||||
5307 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { | ||||
5308 | // (icmp cond A B) if cond is equality | ||||
5309 | return new ICmpInst(I.getPredicate(), A, B); | ||||
Owen Anderson | f5783f8 | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 5310 | } |
Christopher Lamb | 103e1a3 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 5311 | |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5312 | // If we have a icmp le or icmp ge instruction, turn it into the appropriate |
5313 | // icmp lt or icmp gt instruction. This allows us to rely on them being | ||||
5314 | // folded in the code below. | ||||
5315 | switch (I.getPredicate()) { | ||||
5316 | default: break; | ||||
5317 | case ICmpInst::ICMP_ULE: | ||||
5318 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE | ||||
5319 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | ||||
5320 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); | ||||
5321 | case ICmpInst::ICMP_SLE: | ||||
5322 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE | ||||
5323 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | ||||
5324 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); | ||||
5325 | case ICmpInst::ICMP_UGE: | ||||
5326 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE | ||||
5327 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | ||||
5328 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); | ||||
5329 | case ICmpInst::ICMP_SGE: | ||||
5330 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE | ||||
5331 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | ||||
5332 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); | ||||
5333 | } | ||||
5334 | |||||
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5335 | // See if we can fold the comparison based on range information we can get |
5336 | // by checking whether bits are known to be zero or one in the input. | ||||
5337 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | ||||
5338 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
5339 | |||||
5340 | // If this comparison is a normal comparison, it demands all | ||||
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5341 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5342 | bool UnusedBit; |
5343 | bool isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); | ||||
5344 | |||||
Chris Lattner | 4241e4d | 2007-07-15 20:54:51 +0000 | [diff] [blame] | 5345 | if (SimplifyDemandedBits(Op0, |
5346 | isSignBit ? APInt::getSignBit(BitWidth) | ||||
5347 | : APInt::getAllOnesValue(BitWidth), | ||||
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5348 | KnownZero, KnownOne, 0)) |
5349 | return &I; | ||||
5350 | |||||
5351 | // Given the known and unknown bits, compute a range that the LHS could be | ||||
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5352 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
5353 | // EQ and NE we use unsigned values. | ||||
5354 | APInt Min(BitWidth, 0), Max(BitWidth, 0); | ||||
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5355 | if (ICmpInst::isSignedPredicate(I.getPredicate())) |
5356 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, Max); | ||||
5357 | else | ||||
5358 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne,Min,Max); | ||||
5359 | |||||
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5360 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
5361 | // figured out that the LHS is a constant. Just constant fold this now so | ||||
5362 | // that code below can assume that Min != Max. | ||||
5363 | if (Min == Max) | ||||
5364 | return ReplaceInstUsesWith(I, ConstantExpr::getICmp(I.getPredicate(), | ||||
5365 | ConstantInt::get(Min), | ||||
5366 | CI)); | ||||
5367 | |||||
5368 | // Based on the range information we know about the LHS, see if we can | ||||
5369 | // simplify this comparison. For example, (x&4) < 8 is always true. | ||||
5370 | const APInt &RHSVal = CI->getValue(); | ||||
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5371 | switch (I.getPredicate()) { // LE/GE have been folded already. |
5372 | default: assert(0 && "Unknown icmp opcode!"); | ||||
5373 | case ICmpInst::ICMP_EQ: | ||||
5374 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) | ||||
5375 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | ||||
5376 | break; | ||||
5377 | case ICmpInst::ICMP_NE: | ||||
5378 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) | ||||
5379 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | ||||
5380 | break; | ||||
5381 | case ICmpInst::ICMP_ULT: | ||||
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5382 | if (Max.ult(RHSVal)) // A <u C -> true iff max(A) < C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5383 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5384 | if (Min.uge(RHSVal)) // A <u C -> false iff min(A) >= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5385 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5386 | if (RHSVal == Max) // A <u MAX -> A != MAX |
5387 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
5388 | if (RHSVal == Min+1) // A <u MIN+1 -> A == MIN | ||||
5389 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); | ||||
5390 | |||||
5391 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear | ||||
5392 | if (CI->isMinValue(true)) | ||||
5393 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, | ||||
5394 | ConstantInt::getAllOnesValue(Op0->getType())); | ||||
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5395 | break; |
5396 | case ICmpInst::ICMP_UGT: | ||||
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5397 | if (Min.ugt(RHSVal)) // A >u C -> true iff min(A) > C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5398 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5399 | if (Max.ule(RHSVal)) // A >u C -> false iff max(A) <= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5400 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5401 | |
5402 | if (RHSVal == Min) // A >u MIN -> A != MIN | ||||
5403 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
5404 | if (RHSVal == Max-1) // A >u MAX-1 -> A == MAX | ||||
5405 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); | ||||
5406 | |||||
5407 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set | ||||
5408 | if (CI->isMaxValue(true)) | ||||
5409 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, | ||||
5410 | ConstantInt::getNullValue(Op0->getType())); | ||||
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5411 | break; |
5412 | case ICmpInst::ICMP_SLT: | ||||
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5413 | if (Max.slt(RHSVal)) // A <s C -> true iff max(A) < C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5414 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | d01bee7 | 2008-07-11 06:40:29 +0000 | [diff] [blame] | 5415 | if (Min.sge(RHSVal)) // A <s C -> false iff min(A) >= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5416 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5417 | if (RHSVal == Max) // A <s MAX -> A != MAX |
5418 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
Chris Lattner | a8ff4a8 | 2008-07-11 06:36:01 +0000 | [diff] [blame] | 5419 | if (RHSVal == Min+1) // A <s MIN+1 -> A == MIN |
Chris Lattner | f9685ac | 2008-07-11 06:38:16 +0000 | [diff] [blame] | 5420 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5421 | break; |
5422 | case ICmpInst::ICMP_SGT: | ||||
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5423 | if (Min.sgt(RHSVal)) // A >s C -> true iff min(A) > C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5424 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5425 | if (Max.sle(RHSVal)) // A >s C -> false iff max(A) <= C |
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5426 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); |
Chris Lattner | 183661e | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 5427 | |
5428 | if (RHSVal == Min) // A >s MIN -> A != MIN | ||||
5429 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | ||||
5430 | if (RHSVal == Max-1) // A >s MAX-1 -> A == MAX | ||||
5431 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); | ||||
Chris Lattner | 84dff67 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 5432 | break; |
Chris Lattner | bf5d8a8 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 5433 | } |
5434 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5435 | // 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] | 5436 | // instruction, see if that instruction also has constants so that the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5437 | // instruction can be folded into the icmp |
Chris Lattner | 3c6a0d4 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 5438 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5439 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
5440 | return Res; | ||||
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5441 | } |
5442 | |||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5443 | // Handle icmp with constant (but not simple integer constant) RHS |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5444 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
5445 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) | ||||
5446 | switch (LHSI->getOpcode()) { | ||||
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5447 | case Instruction::GetElementPtr: |
5448 | if (RHSC->isNullValue()) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5449 | // 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] | 5450 | bool isAllZeros = true; |
5451 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) | ||||
5452 | if (!isa<Constant>(LHSI->getOperand(i)) || | ||||
5453 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { | ||||
5454 | isAllZeros = false; | ||||
5455 | break; | ||||
5456 | } | ||||
5457 | if (isAllZeros) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5458 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
Chris Lattner | 9fb25db | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 5459 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
5460 | } | ||||
5461 | break; | ||||
5462 | |||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5463 | case Instruction::PHI: |
Chris Lattner | 7d8ab4e | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 5464 | // Only fold icmp into the PHI if the phi and fcmp are in the same |
5465 | // block. If in the same block, we're encouraging jump threading. If | ||||
5466 | // not, we are just pessimizing the code by making an i1 phi. | ||||
5467 | if (LHSI->getParent() == I.getParent()) | ||||
5468 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
5469 | return NV; | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5470 | break; |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5471 | case Instruction::Select: { |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5472 | // If either operand of the select is a constant, we can fold the |
5473 | // comparison into the select arms, which will cause one to be | ||||
5474 | // constant folded and the select turned into a bitwise or. | ||||
5475 | Value *Op1 = 0, *Op2 = 0; | ||||
5476 | if (LHSI->hasOneUse()) { | ||||
5477 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { | ||||
5478 | // Fold the known value into the constant operand. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5479 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
5480 | // Insert a new ICmp of the other select operand. | ||||
5481 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), | ||||
5482 | LHSI->getOperand(2), RHSC, | ||||
5483 | I.getName()), I); | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5484 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
5485 | // Fold the known value into the constant operand. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5486 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
5487 | // Insert a new ICmp of the other select operand. | ||||
5488 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), | ||||
5489 | LHSI->getOperand(1), RHSC, | ||||
5490 | I.getName()), I); | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5491 | } |
5492 | } | ||||
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 5493 | |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5494 | if (Op1) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5495 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5496 | break; |
5497 | } | ||||
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5498 | case Instruction::Malloc: |
5499 | // If we have (malloc != null), and if the malloc has a single use, we | ||||
5500 | // can assume it is successful and remove the malloc. | ||||
5501 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { | ||||
5502 | AddToWorkList(LHSI); | ||||
5503 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | ||||
Nick Lewycky | fc1efbb | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5504 | !I.isTrueWhenEqual())); |
Chris Lattner | 4802d90 | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 5505 | } |
5506 | break; | ||||
5507 | } | ||||
Chris Lattner | 6970b66 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 5508 | } |
5509 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5510 | // 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] | 5511 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5512 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5513 | return NI; |
5514 | if (User *GEP = dyn_castGetElementPtr(Op1)) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5515 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
5516 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) | ||||
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 5517 | return NI; |
5518 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5519 | // 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] | 5520 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
5521 | // now. | ||||
5522 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { | ||||
5523 | if (isa<PointerType>(Op0->getType()) && | ||||
5524 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { | ||||
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5525 | // We keep moving the cast from the left operand over to the right |
5526 | // operand, where it can often be eliminated completely. | ||||
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5527 | Op0 = CI->getOperand(0); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5528 | |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5529 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
5530 | // so eliminate it as well. | ||||
5531 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) | ||||
5532 | Op1 = CI2->getOperand(0); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 5533 | |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5534 | // 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] | 5535 | if (Op0->getType() != Op1->getType()) { |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5536 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 5537 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5538 | } else { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5539 | // Otherwise, cast the RHS right before the icmp |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 5540 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5541 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5542 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5543 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
Chris Lattner | de90b76 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 5544 | } |
Chris Lattner | 57d8637 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 5545 | } |
5546 | |||||
5547 | if (isa<CastInst>(Op0)) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5548 | // Handle the special case of: icmp (cast bool to X), <cst> |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5549 | // This comes up when you have code like |
5550 | // int X = A < B; | ||||
5551 | // if (X) ... | ||||
5552 | // For generality, we handle any zero-extension of any operand comparison | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5553 | // with a constant or another cast from the same type. |
5554 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5555 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5556 | return R; |
Chris Lattner | 6870805 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 5557 | } |
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5558 | |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 5559 | // See if it's the same type of instruction on the left and right. |
5560 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { | ||||
5561 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { | ||||
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 5562 | if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() && |
5563 | Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1) && | ||||
5564 | I.isEquality()) { | ||||
Nick Lewycky | 23c0430 | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 5565 | switch (Op0I->getOpcode()) { |
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 5566 | default: break; |
5567 | case Instruction::Add: | ||||
5568 | case Instruction::Sub: | ||||
5569 | case Instruction::Xor: | ||||
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 5570 | // a+x icmp eq/ne b+x --> a icmp b |
5571 | return new ICmpInst(I.getPredicate(), Op0I->getOperand(0), | ||||
5572 | Op1I->getOperand(0)); | ||||
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 5573 | break; |
5574 | case Instruction::Mul: | ||||
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 5575 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
5576 | // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask | ||||
5577 | // Mask = -1 >> count-trailing-zeros(Cst). | ||||
5578 | if (!CI->isZero() && !CI->isOne()) { | ||||
5579 | const APInt &AP = CI->getValue(); | ||||
5580 | ConstantInt *Mask = ConstantInt::get( | ||||
5581 | APInt::getLowBitsSet(AP.getBitWidth(), | ||||
5582 | AP.getBitWidth() - | ||||
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 5583 | AP.countTrailingZeros())); |
Nick Lewycky | 5d52c45 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 5584 | Instruction *And1 = BinaryOperator::CreateAnd(Op0I->getOperand(0), |
5585 | Mask); | ||||
5586 | Instruction *And2 = BinaryOperator::CreateAnd(Op1I->getOperand(0), | ||||
5587 | Mask); | ||||
5588 | InsertNewInstBefore(And1, I); | ||||
5589 | InsertNewInstBefore(And2, I); | ||||
5590 | return new ICmpInst(I.getPredicate(), And1, And2); | ||||
Nick Lewycky | 4bf1e59 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 5591 | } |
5592 | } | ||||
5593 | break; | ||||
5594 | } | ||||
5595 | } | ||||
5596 | } | ||||
5597 | } | ||||
5598 | |||||
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 5599 | // ~x < ~y --> y < x |
5600 | { Value *A, *B; | ||||
5601 | if (match(Op0, m_Not(m_Value(A))) && | ||||
5602 | match(Op1, m_Not(m_Value(B)))) | ||||
5603 | return new ICmpInst(I.getPredicate(), B, A); | ||||
5604 | } | ||||
5605 | |||||
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5606 | if (I.isEquality()) { |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5607 | Value *A, *B, *C, *D; |
Chris Lattner | 7d2cbd2 | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 5608 | |
5609 | // -x == -y --> x == y | ||||
5610 | if (match(Op0, m_Neg(m_Value(A))) && | ||||
5611 | match(Op1, m_Neg(m_Value(B)))) | ||||
5612 | return new ICmpInst(I.getPredicate(), A, B); | ||||
5613 | |||||
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5614 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
5615 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 | ||||
5616 | Value *OtherVal = A == Op1 ? B : A; | ||||
5617 | return new ICmpInst(I.getPredicate(), OtherVal, | ||||
5618 | Constant::getNullValue(A->getType())); | ||||
5619 | } | ||||
5620 | |||||
5621 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { | ||||
5622 | // A^c1 == C^c2 --> A == C^(c1^c2) | ||||
5623 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) | ||||
5624 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) | ||||
5625 | if (Op1->hasOneUse()) { | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 5626 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5627 | Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); |
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5628 | return new ICmpInst(I.getPredicate(), A, |
5629 | InsertNewInstBefore(Xor, I)); | ||||
5630 | } | ||||
5631 | |||||
5632 | // A^B == A^D -> B == D | ||||
5633 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); | ||||
5634 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); | ||||
5635 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); | ||||
5636 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); | ||||
5637 | } | ||||
5638 | } | ||||
5639 | |||||
5640 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && | ||||
5641 | (A == Op0 || B == Op0)) { | ||||
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5642 | // A == (A^B) -> B == 0 |
5643 | Value *OtherVal = A == Op0 ? B : A; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5644 | return new ICmpInst(I.getPredicate(), OtherVal, |
5645 | Constant::getNullValue(A->getType())); | ||||
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5646 | } |
5647 | 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] | 5648 | // (A-B) == A -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5649 | return new ICmpInst(I.getPredicate(), B, |
5650 | Constant::getNullValue(B->getType())); | ||||
Chris Lattner | 4f0e33d | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 5651 | } |
5652 | 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] | 5653 | // A == (A-B) -> B == 0 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5654 | return new ICmpInst(I.getPredicate(), B, |
5655 | Constant::getNullValue(B->getType())); | ||||
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5656 | } |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5657 | |
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5658 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
5659 | if (Op0->hasOneUse() && Op1->hasOneUse() && | ||||
5660 | match(Op0, m_And(m_Value(A), m_Value(B))) && | ||||
5661 | match(Op1, m_And(m_Value(C), m_Value(D)))) { | ||||
5662 | Value *X = 0, *Y = 0, *Z = 0; | ||||
5663 | |||||
5664 | if (A == C) { | ||||
5665 | X = B; Y = D; Z = A; | ||||
5666 | } else if (A == D) { | ||||
5667 | X = B; Y = C; Z = A; | ||||
5668 | } else if (B == C) { | ||||
5669 | X = A; Y = D; Z = B; | ||||
5670 | } else if (B == D) { | ||||
5671 | X = A; Y = C; Z = B; | ||||
5672 | } | ||||
5673 | |||||
5674 | if (X) { // Build (X^Y) & Z | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5675 | Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I); |
5676 | Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I); | ||||
Chris Lattner | 9c2328e | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 5677 | I.setOperand(0, Op1); |
5678 | I.setOperand(1, Constant::getNullValue(Op1->getType())); | ||||
5679 | return &I; | ||||
5680 | } | ||||
5681 | } | ||||
Chris Lattner | 26ab9a9 | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 5682 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5683 | return Changed ? &I : 0; |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5684 | } |
5685 | |||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5686 | |
5687 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS | ||||
5688 | /// and CmpRHS are both known to be integer constants. | ||||
5689 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, | ||||
5690 | ConstantInt *DivRHS) { | ||||
5691 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); | ||||
5692 | const APInt &CmpRHSV = CmpRHS->getValue(); | ||||
5693 | |||||
5694 | // FIXME: If the operand types don't match the type of the divide | ||||
5695 | // then don't attempt this transform. The code below doesn't have the | ||||
5696 | // logic to deal with a signed divide and an unsigned compare (and | ||||
5697 | // vice versa). This is because (x /s C1) <s C2 produces different | ||||
5698 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even | ||||
5699 | // (x /u C1) <u C2. Simply casting the operands and result won't | ||||
5700 | // work. :( The if statement below tests that condition and bails | ||||
5701 | // if it finds it. | ||||
5702 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; | ||||
5703 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) | ||||
5704 | return 0; | ||||
5705 | if (DivRHS->isZero()) | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5706 | return 0; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5707 | |
5708 | // Compute Prod = CI * DivRHS. We are essentially solving an equation | ||||
5709 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and | ||||
5710 | // C2 (CI). By solving for X we can turn this into a range check | ||||
5711 | // instead of computing a divide. | ||||
5712 | ConstantInt *Prod = Multiply(CmpRHS, DivRHS); | ||||
5713 | |||||
5714 | // Determine if the product overflows by seeing if the product is | ||||
5715 | // not equal to the divide. Make sure we do the same kind of divide | ||||
5716 | // as in the LHS instruction that we're folding. | ||||
5717 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : | ||||
5718 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; | ||||
5719 | |||||
5720 | // Get the ICmp opcode | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5721 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5722 | |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5723 | // Figure out the interval that is being checked. For example, a comparison |
5724 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). | ||||
5725 | // Compute this interval based on the constants involved and the signedness of | ||||
5726 | // the compare/divide. This computes a half-open interval, keeping track of | ||||
5727 | // whether either value in the interval overflows. After analysis each | ||||
5728 | // overflow variable is set to 0 if it's corresponding bound variable is valid | ||||
5729 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. | ||||
5730 | int LoOverflow = 0, HiOverflow = 0; | ||||
5731 | ConstantInt *LoBound = 0, *HiBound = 0; | ||||
5732 | |||||
5733 | |||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5734 | if (!DivIsSigned) { // udiv |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5735 | // e.g. X/5 op 3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5736 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5737 | HiOverflow = LoOverflow = ProdOV; |
5738 | if (!HiOverflow) | ||||
5739 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, false); | ||||
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5740 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5741 | if (CmpRHSV == 0) { // (X / pos) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5742 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5743 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); |
5744 | HiBound = DivRHS; | ||||
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5745 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5746 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
5747 | HiOverflow = LoOverflow = ProdOV; | ||||
5748 | if (!HiOverflow) | ||||
5749 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, true); | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5750 | } else { // (X / pos) op neg |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5751 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5752 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); |
5753 | LoOverflow = AddWithOverflow(LoBound, Prod, | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5754 | cast<ConstantInt>(DivRHSH), true) ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5755 | HiBound = AddOne(Prod); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5756 | HiOverflow = ProdOV ? -1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5757 | } |
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5758 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5759 | if (CmpRHSV == 0) { // (X / neg) op 0 |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5760 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5761 | LoBound = AddOne(DivRHS); |
5762 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5763 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
5764 | HiOverflow = 1; // [INTMIN+1, overflow) | ||||
5765 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN | ||||
5766 | } | ||||
Dan Gohman | 7649127 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 5767 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5768 | // e.g. X/-5 op 3 --> [-19, -14) |
5769 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5770 | if (!LoOverflow) |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5771 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), true) ?-1:0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5772 | HiBound = AddOne(Prod); |
5773 | } else { // (X / neg) op neg | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5774 | // e.g. X/-5 op -3 --> [15, 20) |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5775 | LoBound = Prod; |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5776 | LoOverflow = HiOverflow = ProdOV ? 1 : 0; |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5777 | HiBound = Subtract(Prod, DivRHS); |
5778 | } | ||||
5779 | |||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5780 | // Dividing by a negative swaps the condition. LT <-> GT |
5781 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5782 | } |
5783 | |||||
5784 | Value *X = DivI->getOperand(0); | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5785 | switch (Pred) { |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5786 | default: assert(0 && "Unhandled icmp opcode!"); |
5787 | case ICmpInst::ICMP_EQ: | ||||
5788 | if (LoOverflow && HiOverflow) | ||||
5789 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | ||||
5790 | else if (HiOverflow) | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5791 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5792 | ICmpInst::ICMP_UGE, X, LoBound); |
5793 | else if (LoOverflow) | ||||
5794 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : | ||||
5795 | ICmpInst::ICMP_ULT, X, HiBound); | ||||
5796 | else | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5797 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5798 | case ICmpInst::ICMP_NE: |
5799 | if (LoOverflow && HiOverflow) | ||||
5800 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5801 | else if (HiOverflow) | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5802 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5803 | ICmpInst::ICMP_ULT, X, LoBound); |
5804 | else if (LoOverflow) | ||||
5805 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : | ||||
5806 | ICmpInst::ICMP_UGE, X, HiBound); | ||||
5807 | else | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5808 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5809 | case ICmpInst::ICMP_ULT: |
5810 | case ICmpInst::ICMP_SLT: | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5811 | if (LoOverflow == +1) // Low bound is greater than input range. |
5812 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5813 | if (LoOverflow == -1) // Low bound is less than input range. | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5814 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5815 | return new ICmpInst(Pred, X, LoBound); |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5816 | case ICmpInst::ICMP_UGT: |
5817 | case ICmpInst::ICMP_SGT: | ||||
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5818 | if (HiOverflow == +1) // High bound greater than input range. |
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5819 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Chris Lattner | 1dbfd48 | 2007-06-21 18:11:19 +0000 | [diff] [blame] | 5820 | else if (HiOverflow == -1) // High bound less than input range. |
5821 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5822 | if (Pred == ICmpInst::ICMP_UGT) | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 5823 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
5824 | else | ||||
5825 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); | ||||
5826 | } | ||||
5827 | } | ||||
5828 | |||||
5829 | |||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5830 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
5831 | /// | ||||
5832 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, | ||||
5833 | Instruction *LHSI, | ||||
5834 | ConstantInt *RHS) { | ||||
5835 | const APInt &RHSV = RHS->getValue(); | ||||
5836 | |||||
5837 | switch (LHSI->getOpcode()) { | ||||
Duncan Sands | 0091bf2 | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 5838 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5839 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
5840 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), | ||||
5841 | // fold the xor. | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5842 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
5843 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5844 | Value *CompareVal = LHSI->getOperand(0); |
5845 | |||||
5846 | // If the sign bit of the XorCST is not set, there is no change to | ||||
5847 | // the operation, just stop using the Xor. | ||||
5848 | if (!XorCST->getValue().isNegative()) { | ||||
5849 | ICI.setOperand(0, CompareVal); | ||||
5850 | AddToWorkList(LHSI); | ||||
5851 | return &ICI; | ||||
5852 | } | ||||
5853 | |||||
5854 | // Was the old condition true if the operand is positive? | ||||
5855 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; | ||||
5856 | |||||
5857 | // If so, the new one isn't. | ||||
5858 | isTrueIfPositive ^= true; | ||||
5859 | |||||
5860 | if (isTrueIfPositive) | ||||
5861 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); | ||||
5862 | else | ||||
5863 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); | ||||
5864 | } | ||||
5865 | } | ||||
5866 | break; | ||||
5867 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) | ||||
5868 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && | ||||
5869 | LHSI->getOperand(0)->hasOneUse()) { | ||||
5870 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); | ||||
5871 | |||||
5872 | // If the LHS is an AND of a truncating cast, we can widen the | ||||
5873 | // and/compare to be the input width without changing the value | ||||
5874 | // produced, eliminating a cast. | ||||
5875 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { | ||||
5876 | // We can do this transformation if either the AND constant does not | ||||
5877 | // have its sign bit set or if it is an equality comparison. | ||||
5878 | // Extending a relational comparison when we're checking the sign | ||||
5879 | // bit would not work. | ||||
5880 | if (Cast->hasOneUse() && | ||||
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 5881 | (ICI.isEquality() || |
5882 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5883 | uint32_t BitWidth = |
5884 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); | ||||
5885 | APInt NewCST = AndCST->getValue(); | ||||
5886 | NewCST.zext(BitWidth); | ||||
5887 | APInt NewCI = RHSV; | ||||
5888 | NewCI.zext(BitWidth); | ||||
5889 | Instruction *NewAnd = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5890 | BinaryOperator::CreateAnd(Cast->getOperand(0), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5891 | ConstantInt::get(NewCST),LHSI->getName()); |
5892 | InsertNewInstBefore(NewAnd, ICI); | ||||
5893 | return new ICmpInst(ICI.getPredicate(), NewAnd, | ||||
5894 | ConstantInt::get(NewCI)); | ||||
5895 | } | ||||
5896 | } | ||||
5897 | |||||
5898 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare | ||||
5899 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This | ||||
5900 | // happens a LOT in code produced by the C front-end, for bitfield | ||||
5901 | // access. | ||||
5902 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); | ||||
5903 | if (Shift && !Shift->isShift()) | ||||
5904 | Shift = 0; | ||||
5905 | |||||
5906 | ConstantInt *ShAmt; | ||||
5907 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; | ||||
5908 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. | ||||
5909 | const Type *AndTy = AndCST->getType(); // Type of the and. | ||||
5910 | |||||
5911 | // We can fold this as long as we can't shift unknown bits | ||||
5912 | // into the mask. This can only happen with signed shift | ||||
5913 | // rights, as they sign-extend. | ||||
5914 | if (ShAmt) { | ||||
5915 | bool CanFold = Shift->isLogicalShift(); | ||||
5916 | if (!CanFold) { | ||||
5917 | // To test for the bad case of the signed shr, see if any | ||||
5918 | // of the bits shifted in could be tested after the mask. | ||||
5919 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); | ||||
5920 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); | ||||
5921 | |||||
5922 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); | ||||
5923 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & | ||||
5924 | AndCST->getValue()) == 0) | ||||
5925 | CanFold = true; | ||||
5926 | } | ||||
5927 | |||||
5928 | if (CanFold) { | ||||
5929 | Constant *NewCst; | ||||
5930 | if (Shift->getOpcode() == Instruction::Shl) | ||||
5931 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); | ||||
5932 | else | ||||
5933 | NewCst = ConstantExpr::getShl(RHS, ShAmt); | ||||
5934 | |||||
5935 | // Check to see if we are shifting out any of the bits being | ||||
5936 | // compared. | ||||
5937 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { | ||||
5938 | // If we shifted bits out, the fold is not going to work out. | ||||
5939 | // As a special case, check to see if this means that the | ||||
5940 | // result is always true or false now. | ||||
5941 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) | ||||
5942 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | ||||
5943 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) | ||||
5944 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | ||||
5945 | } else { | ||||
5946 | ICI.setOperand(1, NewCst); | ||||
5947 | Constant *NewAndCST; | ||||
5948 | if (Shift->getOpcode() == Instruction::Shl) | ||||
5949 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); | ||||
5950 | else | ||||
5951 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); | ||||
5952 | LHSI->setOperand(1, NewAndCST); | ||||
5953 | LHSI->setOperand(0, Shift->getOperand(0)); | ||||
5954 | AddToWorkList(Shift); // Shift is dead. | ||||
5955 | AddUsesToWorkList(ICI); | ||||
5956 | return &ICI; | ||||
5957 | } | ||||
5958 | } | ||||
5959 | } | ||||
5960 | |||||
5961 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is | ||||
5962 | // preferable because it allows the C<<Y expression to be hoisted out | ||||
5963 | // of a loop if Y is invariant and X is not. | ||||
5964 | if (Shift && Shift->hasOneUse() && RHSV == 0 && | ||||
5965 | ICI.isEquality() && !Shift->isArithmeticShift() && | ||||
5966 | isa<Instruction>(Shift->getOperand(0))) { | ||||
5967 | // Compute C << Y. | ||||
5968 | Value *NS; | ||||
5969 | if (Shift->getOpcode() == Instruction::LShr) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5970 | NS = BinaryOperator::CreateShl(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5971 | Shift->getOperand(1), "tmp"); |
5972 | } else { | ||||
5973 | // Insert a logical shift. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5974 | NS = BinaryOperator::CreateLShr(AndCST, |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5975 | Shift->getOperand(1), "tmp"); |
5976 | } | ||||
5977 | InsertNewInstBefore(cast<Instruction>(NS), ICI); | ||||
5978 | |||||
5979 | // Compute X & (C << Y). | ||||
5980 | Instruction *NewAnd = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5981 | BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5982 | InsertNewInstBefore(NewAnd, ICI); |
5983 | |||||
5984 | ICI.setOperand(0, NewAnd); | ||||
5985 | return &ICI; | ||||
5986 | } | ||||
5987 | } | ||||
5988 | break; | ||||
5989 | |||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 5990 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
5991 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); | ||||
5992 | if (!ShAmt) break; | ||||
5993 | |||||
5994 | uint32_t TypeBits = RHSV.getBitWidth(); | ||||
5995 | |||||
5996 | // Check that the shift amount is in range. If not, don't perform | ||||
5997 | // undefined shifts. When the shift is visited it will be | ||||
5998 | // simplified. | ||||
5999 | if (ShAmt->uge(TypeBits)) | ||||
6000 | break; | ||||
6001 | |||||
6002 | if (ICI.isEquality()) { | ||||
6003 | // If we are comparing against bits always shifted out, the | ||||
6004 | // comparison cannot succeed. | ||||
6005 | Constant *Comp = | ||||
6006 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); | ||||
6007 | if (Comp != RHS) {// Comparing against a bit that we know is zero. | ||||
6008 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | ||||
6009 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); | ||||
6010 | return ReplaceInstUsesWith(ICI, Cst); | ||||
6011 | } | ||||
6012 | |||||
6013 | if (LHSI->hasOneUse()) { | ||||
6014 | // Otherwise strength reduce the shift into an and. | ||||
6015 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); | ||||
6016 | Constant *Mask = | ||||
6017 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6018 | |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6019 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6020 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6021 | Mask, LHSI->getName()+".mask"); |
6022 | Value *And = InsertNewInstBefore(AndI, ICI); | ||||
6023 | return new ICmpInst(ICI.getPredicate(), And, | ||||
6024 | ConstantInt::get(RHSV.lshr(ShAmtVal))); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6025 | } |
6026 | } | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6027 | |
6028 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. | ||||
6029 | bool TrueIfSigned = false; | ||||
6030 | if (LHSI->hasOneUse() && | ||||
6031 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { | ||||
6032 | // (X << 31) <s 0 --> (X&1) != 0 | ||||
6033 | Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) << | ||||
6034 | (TypeBits-ShAmt->getZExtValue()-1)); | ||||
6035 | Instruction *AndI = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6036 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6037 | Mask, LHSI->getName()+".mask"); |
6038 | Value *And = InsertNewInstBefore(AndI, ICI); | ||||
6039 | |||||
6040 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, | ||||
6041 | And, Constant::getNullValue(And->getType())); | ||||
6042 | } | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6043 | break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6044 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6045 | |
6046 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6047 | case Instruction::AShr: { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6048 | // Only handle equality comparisons of shift-by-constant. |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6049 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6050 | if (!ShAmt || !ICI.isEquality()) break; |
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6051 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6052 | // Check that the shift amount is in range. If not, don't perform |
6053 | // undefined shifts. When the shift is visited it will be | ||||
6054 | // simplified. | ||||
6055 | uint32_t TypeBits = RHSV.getBitWidth(); | ||||
6056 | if (ShAmt->uge(TypeBits)) | ||||
6057 | break; | ||||
6058 | |||||
6059 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6060 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6061 | // If we are comparing against bits always shifted out, the |
6062 | // comparison cannot succeed. | ||||
6063 | APInt Comp = RHSV << ShAmtVal; | ||||
6064 | if (LHSI->getOpcode() == Instruction::LShr) | ||||
6065 | Comp = Comp.lshr(ShAmtVal); | ||||
6066 | else | ||||
6067 | Comp = Comp.ashr(ShAmtVal); | ||||
6068 | |||||
6069 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. | ||||
6070 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | ||||
6071 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); | ||||
6072 | return ReplaceInstUsesWith(ICI, Cst); | ||||
6073 | } | ||||
6074 | |||||
6075 | // Otherwise, check to see if the bits shifted out are known to be zero. | ||||
6076 | // If so, we can compare against the unshifted value: | ||||
6077 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. | ||||
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6078 | if (LHSI->hasOneUse() && |
6079 | MaskedValueIsZero(LHSI->getOperand(0), | ||||
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6080 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
6081 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), | ||||
6082 | ConstantExpr::getShl(RHS, ShAmt)); | ||||
6083 | } | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6084 | |
Evan Cheng | f30752c | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 6085 | if (LHSI->hasOneUse()) { |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6086 | // Otherwise strength reduce the shift into an and. |
6087 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); | ||||
6088 | Constant *Mask = ConstantInt::get(Val); | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6089 | |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6090 | Instruction *AndI = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6091 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
Chris Lattner | 41dc0fc | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6092 | Mask, LHSI->getName()+".mask"); |
6093 | Value *And = InsertNewInstBefore(AndI, ICI); | ||||
6094 | return new ICmpInst(ICI.getPredicate(), And, | ||||
6095 | ConstantExpr::getShl(RHS, ShAmt)); | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6096 | } |
6097 | break; | ||||
Chris Lattner | a0141b9 | 2007-07-15 20:42:37 +0000 | [diff] [blame] | 6098 | } |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6099 | |
6100 | case Instruction::SDiv: | ||||
6101 | case Instruction::UDiv: | ||||
6102 | // Fold: icmp pred ([us]div X, C1), C2 -> range test | ||||
6103 | // Fold this div into the comparison, producing a range check. | ||||
6104 | // Determine, based on the divide type, what the range is being | ||||
6105 | // checked. If there is an overflow on the low or high side, remember | ||||
6106 | // it, otherwise compute the range [low, hi) bounding the new value. | ||||
6107 | // See: InsertRangeTest above for the kinds of replacements possible. | ||||
Chris Lattner | 562ef78 | 2007-06-20 23:46:26 +0000 | [diff] [blame] | 6108 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
6109 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), | ||||
6110 | DivRHS)) | ||||
6111 | return R; | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6112 | break; |
Nick Lewycky | 5be2920 | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 6113 | |
6114 | case Instruction::Add: | ||||
6115 | // Fold: icmp pred (add, X, C1), C2 | ||||
6116 | |||||
6117 | if (!ICI.isEquality()) { | ||||
6118 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); | ||||
6119 | if (!LHSC) break; | ||||
6120 | const APInt &LHSV = LHSC->getValue(); | ||||
6121 | |||||
6122 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) | ||||
6123 | .subtract(LHSV); | ||||
6124 | |||||
6125 | if (ICI.isSignedPredicate()) { | ||||
6126 | if (CR.getLower().isSignBit()) { | ||||
6127 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), | ||||
6128 | ConstantInt::get(CR.getUpper())); | ||||
6129 | } else if (CR.getUpper().isSignBit()) { | ||||
6130 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), | ||||
6131 | ConstantInt::get(CR.getLower())); | ||||
6132 | } | ||||
6133 | } else { | ||||
6134 | if (CR.getLower().isMinValue()) { | ||||
6135 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), | ||||
6136 | ConstantInt::get(CR.getUpper())); | ||||
6137 | } else if (CR.getUpper().isMinValue()) { | ||||
6138 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), | ||||
6139 | ConstantInt::get(CR.getLower())); | ||||
6140 | } | ||||
6141 | } | ||||
6142 | } | ||||
6143 | break; | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6144 | } |
6145 | |||||
6146 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. | ||||
6147 | if (ICI.isEquality()) { | ||||
6148 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | ||||
6149 | |||||
6150 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and | ||||
6151 | // the second operand is a constant, simplify a bit. | ||||
6152 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { | ||||
6153 | switch (BO->getOpcode()) { | ||||
6154 | case Instruction::SRem: | ||||
6155 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. | ||||
6156 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ | ||||
6157 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); | ||||
6158 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { | ||||
6159 | Instruction *NewRem = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6160 | BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1), |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6161 | BO->getName()); |
6162 | InsertNewInstBefore(NewRem, ICI); | ||||
6163 | return new ICmpInst(ICI.getPredicate(), NewRem, | ||||
6164 | Constant::getNullValue(BO->getType())); | ||||
6165 | } | ||||
6166 | } | ||||
6167 | break; | ||||
6168 | case Instruction::Add: | ||||
6169 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. | ||||
6170 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { | ||||
6171 | if (BO->hasOneUse()) | ||||
6172 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | ||||
6173 | Subtract(RHS, BOp1C)); | ||||
6174 | } else if (RHSV == 0) { | ||||
6175 | // Replace ((add A, B) != 0) with (A != -B) if A or B is | ||||
6176 | // efficiently invertible, or if the add has just this one use. | ||||
6177 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); | ||||
6178 | |||||
6179 | if (Value *NegVal = dyn_castNegVal(BOp1)) | ||||
6180 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); | ||||
6181 | else if (Value *NegVal = dyn_castNegVal(BOp0)) | ||||
6182 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); | ||||
6183 | else if (BO->hasOneUse()) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6184 | Instruction *Neg = BinaryOperator::CreateNeg(BOp1); |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6185 | InsertNewInstBefore(Neg, ICI); |
6186 | Neg->takeName(BO); | ||||
6187 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); | ||||
6188 | } | ||||
6189 | } | ||||
6190 | break; | ||||
6191 | case Instruction::Xor: | ||||
6192 | // For the xor case, we can xor two constants together, eliminating | ||||
6193 | // the explicit xor. | ||||
6194 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) | ||||
6195 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | ||||
6196 | ConstantExpr::getXor(RHS, BOC)); | ||||
6197 | |||||
6198 | // FALLTHROUGH | ||||
6199 | case Instruction::Sub: | ||||
6200 | // Replace (([sub|xor] A, B) != 0) with (A != B) | ||||
6201 | if (RHSV == 0) | ||||
6202 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | ||||
6203 | BO->getOperand(1)); | ||||
6204 | break; | ||||
6205 | |||||
6206 | case Instruction::Or: | ||||
6207 | // If bits are being or'd in that are not present in the constant we | ||||
6208 | // are comparing against, then the comparison could never succeed! | ||||
6209 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { | ||||
6210 | Constant *NotCI = ConstantExpr::getNot(RHS); | ||||
6211 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) | ||||
6212 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, | ||||
6213 | isICMP_NE)); | ||||
6214 | } | ||||
6215 | break; | ||||
6216 | |||||
6217 | case Instruction::And: | ||||
6218 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { | ||||
6219 | // If bits are being compared against that are and'd out, then the | ||||
6220 | // comparison can never succeed! | ||||
6221 | if ((RHSV & ~BOC->getValue()) != 0) | ||||
6222 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, | ||||
6223 | isICMP_NE)); | ||||
6224 | |||||
6225 | // If we have ((X & C) == C), turn it into ((X & C) != 0). | ||||
6226 | if (RHS == BOC && RHSV.isPowerOf2()) | ||||
6227 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : | ||||
6228 | ICmpInst::ICMP_NE, LHSI, | ||||
6229 | Constant::getNullValue(RHS->getType())); | ||||
6230 | |||||
6231 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 | ||||
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 6232 | if (BOC->getValue().isSignBit()) { |
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6233 | Value *X = BO->getOperand(0); |
6234 | Constant *Zero = Constant::getNullValue(X->getType()); | ||||
6235 | ICmpInst::Predicate pred = isICMP_NE ? | ||||
6236 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; | ||||
6237 | return new ICmpInst(pred, X, Zero); | ||||
6238 | } | ||||
6239 | |||||
6240 | // ((X & ~7) == 0) --> X < 8 | ||||
6241 | if (RHSV == 0 && isHighOnes(BOC)) { | ||||
6242 | Value *X = BO->getOperand(0); | ||||
6243 | Constant *NegX = ConstantExpr::getNeg(BOC); | ||||
6244 | ICmpInst::Predicate pred = isICMP_NE ? | ||||
6245 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; | ||||
6246 | return new ICmpInst(pred, X, NegX); | ||||
6247 | } | ||||
6248 | } | ||||
6249 | default: break; | ||||
6250 | } | ||||
6251 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { | ||||
6252 | // Handle icmp {eq|ne} <intrinsic>, intcst. | ||||
6253 | if (II->getIntrinsicID() == Intrinsic::bswap) { | ||||
6254 | AddToWorkList(II); | ||||
6255 | ICI.setOperand(0, II->getOperand(1)); | ||||
6256 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); | ||||
6257 | return &ICI; | ||||
6258 | } | ||||
6259 | } | ||||
6260 | } else { // Not a ICMP_EQ/ICMP_NE | ||||
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 6261 | // If the LHS is a cast from an integral value of the same size, |
6262 | // then since we know the RHS is a constant, try to simlify. | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6263 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { |
6264 | Value *CastOp = Cast->getOperand(0); | ||||
6265 | const Type *SrcTy = CastOp->getType(); | ||||
6266 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); | ||||
6267 | if (SrcTy->isInteger() && | ||||
6268 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { | ||||
6269 | // If this is an unsigned comparison, try to make the comparison use | ||||
6270 | // smaller constant values. | ||||
6271 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { | ||||
6272 | // X u< 128 => X s> -1 | ||||
6273 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, | ||||
6274 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); | ||||
6275 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && | ||||
6276 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { | ||||
6277 | // X u> 127 => X s< 0 | ||||
6278 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, | ||||
6279 | Constant::getNullValue(SrcTy)); | ||||
6280 | } | ||||
6281 | } | ||||
6282 | } | ||||
6283 | } | ||||
6284 | return 0; | ||||
6285 | } | ||||
6286 | |||||
6287 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). | ||||
6288 | /// We only handle extending casts so far. | ||||
6289 | /// | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6290 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
6291 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6292 | Value *LHSCIOp = LHSCI->getOperand(0); |
6293 | const Type *SrcTy = LHSCIOp->getType(); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6294 | const Type *DestTy = LHSCI->getType(); |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6295 | Value *RHSCIOp; |
6296 | |||||
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6297 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
6298 | // integer type is the same size as the pointer type. | ||||
6299 | if (LHSCI->getOpcode() == Instruction::PtrToInt && | ||||
6300 | getTargetData().getPointerSizeInBits() == | ||||
6301 | cast<IntegerType>(DestTy)->getBitWidth()) { | ||||
6302 | Value *RHSOp = 0; | ||||
6303 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { | ||||
Chris Lattner | 6f6f512 | 2007-05-06 07:24:03 +0000 | [diff] [blame] | 6304 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6305 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
6306 | RHSOp = RHSC->getOperand(0); | ||||
6307 | // If the pointer types don't match, insert a bitcast. | ||||
6308 | if (LHSCIOp->getType() != RHSOp->getType()) | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6309 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
Chris Lattner | 8c756c1 | 2007-05-05 22:41:33 +0000 | [diff] [blame] | 6310 | } |
6311 | |||||
6312 | if (RHSOp) | ||||
6313 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); | ||||
6314 | } | ||||
6315 | |||||
6316 | // The code below only handles extension cast instructions, so far. | ||||
6317 | // Enforce this. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6318 | if (LHSCI->getOpcode() != Instruction::ZExt && |
6319 | LHSCI->getOpcode() != Instruction::SExt) | ||||
Chris Lattner | b352fa5 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 6320 | return 0; |
6321 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6322 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
6323 | bool isSignedCmp = ICI.isSignedPredicate(); | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6324 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6325 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6326 | // Not an extension from the same type? |
6327 | RHSCIOp = CI->getOperand(0); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6328 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
6329 | return 0; | ||||
Chris Lattner | a5c5e77 | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 6330 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6331 | // 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] | 6332 | // and the other is a zext), then we can't handle this. |
6333 | if (CI->getOpcode() != LHSCI->getOpcode()) | ||||
6334 | return 0; | ||||
6335 | |||||
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 6336 | // Deal with equality cases early. |
6337 | if (ICI.isEquality()) | ||||
6338 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); | ||||
6339 | |||||
6340 | // A signed comparison of sign extended values simplifies into a | ||||
6341 | // signed comparison. | ||||
6342 | if (isSignedCmp && isSignedExt) | ||||
6343 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); | ||||
6344 | |||||
6345 | // The other three cases all fold into an unsigned comparison. | ||||
6346 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); | ||||
Reid Spencer | 6731d5c | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 6347 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6348 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6349 | // If we aren't dealing with a constant on the RHS, exit early |
6350 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); | ||||
6351 | if (!CI) | ||||
6352 | return 0; | ||||
6353 | |||||
6354 | // Compute the constant that would happen if we truncated to SrcTy then | ||||
6355 | // reextended to DestTy. | ||||
6356 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); | ||||
6357 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); | ||||
6358 | |||||
6359 | // If the re-extended constant didn't change... | ||||
6360 | if (Res2 == CI) { | ||||
6361 | // Make sure that sign of the Cmp and the sign of the Cast are the same. | ||||
6362 | // For example, we might have: | ||||
6363 | // %A = sext short %X to uint | ||||
6364 | // %B = icmp ugt uint %A, 1330 | ||||
6365 | // It is incorrect to transform this into | ||||
6366 | // %B = icmp ugt short %X, 1330 | ||||
6367 | // because %A may have negative value. | ||||
6368 | // | ||||
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6369 | // However, we allow this when the compare is EQ/NE, because they are |
6370 | // signless. | ||||
6371 | if (isSignedExt == isSignedCmp || ICI.isEquality()) | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6372 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6373 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6374 | } |
6375 | |||||
6376 | // The re-extended constant changed so the constant cannot be represented | ||||
6377 | // in the shorter type. Consequently, we cannot emit a simple comparison. | ||||
6378 | |||||
6379 | // First, handle some easy cases. We know the result cannot be equal at this | ||||
6380 | // point so handle the ICI.isEquality() cases | ||||
6381 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6382 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6383 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6384 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6385 | |
6386 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases | ||||
6387 | // should have been folded away previously and not enter in here. | ||||
6388 | Value *Result; | ||||
6389 | if (isSignedCmp) { | ||||
6390 | // We're performing a signed comparison. | ||||
Reid Spencer | 0460fb3 | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 6391 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6392 | Result = ConstantInt::getFalse(); // X < (small) --> false |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6393 | else |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6394 | Result = ConstantInt::getTrue(); // X < (large) --> true |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6395 | } else { |
6396 | // We're performing an unsigned comparison. | ||||
6397 | if (isSignedExt) { | ||||
6398 | // We're performing an unsigned comp with a sign extended value. | ||||
6399 | // This is true if the input is >= 0. [aka >s -1] | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6400 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6401 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, |
6402 | NegOne, ICI.getName()), ICI); | ||||
6403 | } else { | ||||
6404 | // Unsigned extend & unsigned compare -> always true. | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6405 | Result = ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6406 | } |
6407 | } | ||||
6408 | |||||
6409 | // Finally, return the value computed. | ||||
6410 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || | ||||
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6411 | ICI.getPredicate() == ICmpInst::ICMP_SLT) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6412 | return ReplaceInstUsesWith(ICI, Result); |
Chris Lattner | f299184 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 6413 | |
6414 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || | ||||
6415 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && | ||||
6416 | "ICmp should be folded!"); | ||||
6417 | if (Constant *CI = dyn_cast<Constant>(Result)) | ||||
6418 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); | ||||
6419 | return BinaryOperator::CreateNot(Result); | ||||
Chris Lattner | 484d3cf | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 6420 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6421 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6422 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
6423 | return commonShiftTransforms(I); | ||||
6424 | } | ||||
6425 | |||||
6426 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { | ||||
6427 | return commonShiftTransforms(I); | ||||
6428 | } | ||||
6429 | |||||
6430 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { | ||||
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6431 | if (Instruction *R = commonShiftTransforms(I)) |
6432 | return R; | ||||
6433 | |||||
6434 | Value *Op0 = I.getOperand(0); | ||||
6435 | |||||
6436 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) | ||||
6437 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) | ||||
6438 | if (CSI->isAllOnesValue()) | ||||
6439 | return ReplaceInstUsesWith(I, CSI); | ||||
6440 | |||||
6441 | // See if we can turn a signed shr into an unsigned shr. | ||||
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 6442 | if (!isa<VectorType>(I.getType()) && |
6443 | MaskedValueIsZero(Op0, | ||||
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6444 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6445 | return BinaryOperator::CreateLShr(Op0, I.getOperand(1)); |
Chris Lattner | 348f665 | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 6446 | |
6447 | return 0; | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6448 | } |
6449 | |||||
6450 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { | ||||
6451 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 6452 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6453 | |
6454 | // shl X, 0 == X and shr X, 0 == X | ||||
6455 | // shl 0, X == 0 and shr 0, X == 0 | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6456 | if (Op1 == Constant::getNullValue(Op1->getType()) || |
Chris Lattner | 233f7dc | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 6457 | Op0 == Constant::getNullValue(Op0->getType())) |
6458 | return ReplaceInstUsesWith(I, Op0); | ||||
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6459 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6460 | if (isa<UndefValue>(Op0)) { |
6461 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef | ||||
Chris Lattner | 79a564c | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 6462 | return ReplaceInstUsesWith(I, Op0); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6463 | else // undef << X -> 0, undef >>u X -> 0 |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6464 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
6465 | } | ||||
6466 | if (isa<UndefValue>(Op1)) { | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6467 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
6468 | return ReplaceInstUsesWith(I, Op0); | ||||
6469 | else // X << undef, X >>u undef -> 0 | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6470 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6471 | } |
6472 | |||||
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6473 | // Try to fold constant and into select arguments. |
6474 | if (isa<Constant>(Op0)) | ||||
6475 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | ||||
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6476 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6477 | return R; |
6478 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6479 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6480 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
6481 | return Res; | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6482 | return 0; |
6483 | } | ||||
6484 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6485 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6486 | BinaryOperator &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6487 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6488 | |
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6489 | // See if we can simplify any instructions used by the instruction whose sole |
6490 | // purpose is to compute bits we don't care about. | ||||
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6491 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); |
6492 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); | ||||
6493 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), | ||||
Chris Lattner | 8d6bbdb | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 6494 | KnownZero, KnownOne)) |
6495 | return &I; | ||||
6496 | |||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6497 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr |
6498 | // of a signed value. | ||||
6499 | // | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6500 | if (Op1->uge(TypeBits)) { |
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6501 | if (I.getOpcode() != Instruction::AShr) |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6502 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); |
6503 | else { | ||||
Chris Lattner | 0737c24 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 6504 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6505 | return &I; |
Chris Lattner | 8adac75 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 6506 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6507 | } |
6508 | |||||
6509 | // ((X*C1) << C2) == (X * (C1 << C2)) | ||||
6510 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) | ||||
6511 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) | ||||
6512 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6513 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6514 | ConstantExpr::getShl(BOOp, Op1)); |
6515 | |||||
6516 | // Try to fold constant and into select arguments. | ||||
6517 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | ||||
6518 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | ||||
6519 | return R; | ||||
6520 | if (isa<PHINode>(Op0)) | ||||
6521 | if (Instruction *NV = FoldOpIntoPhi(I)) | ||||
6522 | return NV; | ||||
6523 | |||||
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6524 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
6525 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { | ||||
6526 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); | ||||
6527 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny | ||||
6528 | // place. Don't try to do this transformation in this case. Also, we | ||||
6529 | // require that the input operand is a shift-by-constant so that we have | ||||
6530 | // confidence that the shifts will get folded together. We could do this | ||||
6531 | // xform in more cases, but it is unlikely to be profitable. | ||||
6532 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && | ||||
6533 | isa<ConstantInt>(TrOp->getOperand(1))) { | ||||
6534 | // Okay, we'll do this xform. Make the shift of shift. | ||||
6535 | Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6536 | Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt, |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6537 | I.getName()); |
6538 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) | ||||
6539 | |||||
6540 | // For logical shifts, the truncation has the effect of making the high | ||||
6541 | // part of the register be zeros. Emulate this by inserting an AND to | ||||
6542 | // clear the top bits as needed. This 'and' will usually be zapped by | ||||
6543 | // other xforms later if dead. | ||||
6544 | unsigned SrcSize = TrOp->getType()->getPrimitiveSizeInBits(); | ||||
6545 | unsigned DstSize = TI->getType()->getPrimitiveSizeInBits(); | ||||
6546 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); | ||||
6547 | |||||
6548 | // The mask we constructed says what the trunc would do if occurring | ||||
6549 | // between the shifts. We want to know the effect *after* the second | ||||
6550 | // shift. We know that it is a logical shift by a constant, so adjust the | ||||
6551 | // mask as appropriate. | ||||
6552 | if (I.getOpcode() == Instruction::Shl) | ||||
6553 | MaskV <<= Op1->getZExtValue(); | ||||
6554 | else { | ||||
6555 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); | ||||
6556 | MaskV = MaskV.lshr(Op1->getZExtValue()); | ||||
6557 | } | ||||
6558 | |||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6559 | Instruction *And = BinaryOperator::CreateAnd(NSh, ConstantInt::get(MaskV), |
Chris Lattner | 8999dd3 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 6560 | TI->getName()); |
6561 | InsertNewInstBefore(And, I); // shift1 & 0x00FF | ||||
6562 | |||||
6563 | // Return the value truncated to the interesting size. | ||||
6564 | return new TruncInst(And, I.getType()); | ||||
6565 | } | ||||
6566 | } | ||||
6567 | |||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6568 | if (Op0->hasOneUse()) { |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6569 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
6570 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) | ||||
6571 | Value *V1, *V2; | ||||
6572 | ConstantInt *CC; | ||||
6573 | switch (Op0BO->getOpcode()) { | ||||
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6574 | default: break; |
6575 | case Instruction::Add: | ||||
6576 | case Instruction::And: | ||||
6577 | case Instruction::Or: | ||||
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6578 | case Instruction::Xor: { |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6579 | // These operators commute. |
6580 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6581 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
6582 | match(Op0BO->getOperand(1), | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6583 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6584 | Instruction *YS = BinaryOperator::CreateShl( |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6585 | Op0BO->getOperand(0), Op1, |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6586 | Op0BO->getName()); |
6587 | InsertNewInstBefore(YS, I); // (Y << C) | ||||
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6588 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6589 | BinaryOperator::Create(Op0BO->getOpcode(), YS, V1, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6590 | Op0BO->getOperand(1)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6591 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6592 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6593 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6594 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6595 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6596 | |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6597 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6598 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
Chris Lattner | 3c69849 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 6599 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6600 | match(Op0BOOp1, |
6601 | 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] | 6602 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && |
6603 | V2 == Op1) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6604 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6605 | Op0BO->getOperand(0), Op1, |
6606 | Op0BO->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6607 | InsertNewInstBefore(YS, I); // (Y << C) |
6608 | Instruction *XM = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6609 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6610 | V1->getName()+".mask"); |
6611 | InsertNewInstBefore(XM, I); // X & (CC << C) | ||||
6612 | |||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6613 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6614 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6615 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6616 | |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6617 | // FALL THROUGH. |
6618 | case Instruction::Sub: { | ||||
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6619 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6620 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
6621 | match(Op0BO->getOperand(0), | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6622 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6623 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6624 | Op0BO->getOperand(1), Op1, |
6625 | Op0BO->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6626 | InsertNewInstBefore(YS, I); // (Y << C) |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6627 | Instruction *X = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6628 | BinaryOperator::Create(Op0BO->getOpcode(), V1, YS, |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6629 | Op0BO->getOperand(0)->getName()); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6630 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6631 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6632 | return BinaryOperator::CreateAnd(X, ConstantInt::get( |
Zhou Sheng | 90b9681 | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 6633 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6634 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6635 | |
Chris Lattner | 13d4ab4 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 6636 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6637 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
6638 | match(Op0BO->getOperand(0), | ||||
6639 | m_And(m_Shr(m_Value(V1), m_Value(V2)), | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6640 | m_ConstantInt(CC))) && V2 == Op1 && |
Chris Lattner | 9a4cacb | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 6641 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
6642 | ->getOperand(0)->hasOneUse()) { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6643 | Instruction *YS = BinaryOperator::CreateShl( |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6644 | Op0BO->getOperand(1), Op1, |
6645 | Op0BO->getName()); | ||||
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6646 | InsertNewInstBefore(YS, I); // (Y << C) |
6647 | Instruction *XM = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6648 | BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6649 | V1->getName()+".mask"); |
6650 | InsertNewInstBefore(XM, I); // X & (CC << C) | ||||
6651 | |||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6652 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
Chris Lattner | 150f12a | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 6653 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6654 | |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6655 | break; |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6656 | } |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6657 | } |
6658 | |||||
6659 | |||||
6660 | // If the operand is an bitwise operator with a constant RHS, and the | ||||
6661 | // shift is the only use, we can pull it out of the shift. | ||||
6662 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { | ||||
6663 | bool isValid = true; // Valid only for And, Or, Xor | ||||
6664 | bool highBitSet = false; // Transform if high bit of constant set? | ||||
6665 | |||||
6666 | switch (Op0BO->getOpcode()) { | ||||
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6667 | default: isValid = false; break; // Do not perform transform! |
Chris Lattner | 1f7e160 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 6668 | case Instruction::Add: |
6669 | isValid = isLeftShift; | ||||
6670 | break; | ||||
Chris Lattner | df17af1 | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 6671 | case Instruction::Or: |
6672 | case Instruction::Xor: | ||||
6673 | highBitSet = false; | ||||
6674 | break; | ||||
6675 | case Instruction::And: | ||||
6676 | highBitSet = true; | ||||
6677 | break; | ||||
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6678 | } |
6679 | |||||
6680 | // If this is a signed shift right, and the high bit is modified | ||||
6681 | // by the logical operation, do not perform the transformation. | ||||
6682 | // The highBitSet boolean indicates the value of the high bit of | ||||
6683 | // the constant which would cause it to be modified for this | ||||
6684 | // operation. | ||||
6685 | // | ||||
Chris Lattner | c95ba44 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 6686 | if (isValid && I.getOpcode() == Instruction::AShr) |
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6687 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6688 | |
6689 | if (isValid) { | ||||
6690 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); | ||||
6691 | |||||
6692 | Instruction *NewShift = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6693 | BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6694 | InsertNewInstBefore(NewShift, I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6695 | NewShift->takeName(Op0BO); |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6696 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6697 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
Chris Lattner | 4d5542c | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 6698 | NewRHS); |
6699 | } | ||||
6700 | } | ||||
6701 | } | ||||
6702 | } | ||||
6703 | |||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6704 | // 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] | 6705 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
6706 | if (ShiftOp && !ShiftOp->isShift()) | ||||
6707 | ShiftOp = 0; | ||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6708 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6709 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6710 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 6711 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
6712 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); | ||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6713 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
6714 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. | ||||
6715 | Value *X = ShiftOp->getOperand(0); | ||||
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6716 | |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6717 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
Reid Spencer | b35ae03 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 6718 | if (AmtSum > TypeBits) |
6719 | AmtSum = TypeBits; | ||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6720 | |
6721 | const IntegerType *Ty = cast<IntegerType>(I.getType()); | ||||
6722 | |||||
6723 | // Check for (X << c1) << c2 and (X >> c1) >> c2 | ||||
Chris Lattner | 7f3da2d | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 6724 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6725 | return BinaryOperator::Create(I.getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6726 | ConstantInt::get(Ty, AmtSum)); |
6727 | } else if (ShiftOp->getOpcode() == Instruction::LShr && | ||||
6728 | I.getOpcode() == Instruction::AShr) { | ||||
6729 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6730 | return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6731 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
6732 | I.getOpcode() == Instruction::LShr) { | ||||
6733 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. | ||||
6734 | Instruction *Shift = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6735 | BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6736 | InsertNewInstBefore(Shift, I); |
6737 | |||||
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6738 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6739 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6740 | } |
6741 | |||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6742 | // Okay, if we get here, one shift must be left, and the other shift must be |
6743 | // right. See if the amounts are equal. | ||||
6744 | if (ShiftAmt1 == ShiftAmt2) { | ||||
6745 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). | ||||
6746 | if (I.getOpcode() == Instruction::Shl) { | ||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6747 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6748 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6749 | } |
6750 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). | ||||
6751 | if (I.getOpcode() == Instruction::LShr) { | ||||
Zhou Sheng | 3a507fd | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6752 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6753 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6754 | } |
6755 | // We can simplify ((X << C) >>s C) into a trunc + sext. | ||||
6756 | // NOTE: we could do this for any C, but that would make 'unusual' integer | ||||
6757 | // types. For now, just stick to ones well-supported by the code | ||||
6758 | // generators. | ||||
6759 | const Type *SExtType = 0; | ||||
6760 | switch (Ty->getBitWidth() - ShiftAmt1) { | ||||
Zhou Sheng | e9e03f6 | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 6761 | case 1 : |
6762 | case 8 : | ||||
6763 | case 16 : | ||||
6764 | case 32 : | ||||
6765 | case 64 : | ||||
6766 | case 128: | ||||
6767 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); | ||||
6768 | break; | ||||
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6769 | default: break; |
6770 | } | ||||
6771 | if (SExtType) { | ||||
6772 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); | ||||
6773 | InsertNewInstBefore(NewTrunc, I); | ||||
6774 | return new SExtInst(NewTrunc, Ty); | ||||
6775 | } | ||||
6776 | // Otherwise, we can't handle it yet. | ||||
6777 | } else if (ShiftAmt1 < ShiftAmt2) { | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6778 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6779 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6780 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6781 | if (I.getOpcode() == Instruction::Shl) { |
6782 | assert(ShiftOp->getOpcode() == Instruction::LShr || | ||||
6783 | ShiftOp->getOpcode() == Instruction::AShr); | ||||
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6784 | Instruction *Shift = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6785 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | e8d56c5 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 6786 | InsertNewInstBefore(Shift, I); |
6787 | |||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6788 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6789 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6790 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6791 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6792 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6793 | if (I.getOpcode() == Instruction::LShr) { |
6794 | assert(ShiftOp->getOpcode() == Instruction::Shl); | ||||
6795 | Instruction *Shift = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6796 | BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6797 | InsertNewInstBefore(Shift, I); |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6798 | |
Reid Spencer | d5e30f0 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 6799 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6800 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | 11021cb | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 6801 | } |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6802 | |
6803 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. | ||||
6804 | } else { | ||||
6805 | assert(ShiftAmt2 < ShiftAmt1); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6806 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6807 | |
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6808 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6809 | if (I.getOpcode() == Instruction::Shl) { |
6810 | assert(ShiftOp->getOpcode() == Instruction::LShr || | ||||
6811 | ShiftOp->getOpcode() == Instruction::AShr); | ||||
6812 | Instruction *Shift = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6813 | BinaryOperator::Create(ShiftOp->getOpcode(), X, |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6814 | ConstantInt::get(Ty, ShiftDiff)); |
6815 | InsertNewInstBefore(Shift, I); | ||||
6816 | |||||
Reid Spencer | 55702aa | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 6817 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6818 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6819 | } |
6820 | |||||
Chris Lattner | b0b991a | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 6821 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6822 | if (I.getOpcode() == Instruction::LShr) { |
6823 | assert(ShiftOp->getOpcode() == Instruction::Shl); | ||||
6824 | Instruction *Shift = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6825 | BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6826 | InsertNewInstBefore(Shift, I); |
6827 | |||||
Reid Spencer | 68d27cf | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 6828 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6829 | return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask)); |
Chris Lattner | b87056f | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 6830 | } |
6831 | |||||
6832 | // 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] | 6833 | } |
Chris Lattner | ad0124c | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 6834 | } |
Chris Lattner | 3f5b877 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 6835 | return 0; |
6836 | } | ||||
6837 | |||||
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 6838 | |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6839 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
6840 | /// expression. If so, decompose it, returning some value X, such that Val is | ||||
6841 | /// X*Scale+Offset. | ||||
6842 | /// | ||||
6843 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, | ||||
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6844 | int &Offset) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6845 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6846 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6847 | Offset = CI->getZExtValue(); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6848 | Scale = 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6849 | return ConstantInt::get(Type::Int32Ty, 0); |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 6850 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
6851 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
6852 | if (I->getOpcode() == Instruction::Shl) { | ||||
6853 | // This is a value scaled by '1 << the shift amt'. | ||||
6854 | Scale = 1U << RHS->getZExtValue(); | ||||
6855 | Offset = 0; | ||||
6856 | return I->getOperand(0); | ||||
6857 | } else if (I->getOpcode() == Instruction::Mul) { | ||||
6858 | // This value is scaled by 'RHS'. | ||||
6859 | Scale = RHS->getZExtValue(); | ||||
6860 | Offset = 0; | ||||
6861 | return I->getOperand(0); | ||||
6862 | } else if (I->getOpcode() == Instruction::Add) { | ||||
6863 | // We have X+C. Check to see if we really have (X*C2)+C1, | ||||
6864 | // where C1 is divisible by C2. | ||||
6865 | unsigned SubScale; | ||||
6866 | Value *SubVal = | ||||
6867 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); | ||||
6868 | Offset += RHS->getZExtValue(); | ||||
6869 | Scale = SubScale; | ||||
6870 | return SubVal; | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6871 | } |
6872 | } | ||||
6873 | } | ||||
6874 | |||||
6875 | // Otherwise, we can't look past this. | ||||
6876 | Scale = 1; | ||||
6877 | Offset = 0; | ||||
6878 | return Val; | ||||
6879 | } | ||||
6880 | |||||
6881 | |||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6882 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
6883 | /// 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] | 6884 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6885 | AllocationInst &AI) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 6886 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6887 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6888 | // Remove any uses of AI that are dead. |
6889 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); | ||||
Chris Lattner | 535014f | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 6890 | |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6891 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
6892 | Instruction *User = cast<Instruction>(*UI++); | ||||
6893 | if (isInstructionTriviallyDead(User)) { | ||||
6894 | while (UI != E && *UI == User) | ||||
6895 | ++UI; // If this instruction uses AI more than once, don't break UI. | ||||
6896 | |||||
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6897 | ++NumDeadInst; |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 6898 | DOUT << "IC: DCE: " << *User; |
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 6899 | EraseInstFromFunction(*User); |
Chris Lattner | b53c238 | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 6900 | } |
6901 | } | ||||
6902 | |||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6903 | // Get the type really allocated and the type casted to. |
6904 | const Type *AllocElTy = AI.getAllocatedType(); | ||||
6905 | const Type *CastElTy = PTy->getElementType(); | ||||
6906 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; | ||||
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6907 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 6908 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
6909 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); | ||||
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6910 | if (CastElTyAlign < AllocElTyAlign) return 0; |
6911 | |||||
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6912 | // If the allocation has multiple uses, only promote it if we are strictly |
6913 | // increasing the alignment of the resultant allocation. If we keep it the | ||||
6914 | // same, we open the door to infinite loops of various kinds. | ||||
6915 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; | ||||
6916 | |||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 6917 | uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy); |
6918 | uint64_t CastElTySize = TD->getABITypeSize(CastElTy); | ||||
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6919 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
Chris Lattner | 18e78bb | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 6920 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6921 | // See if we can satisfy the modulus by pulling a scale out of the array |
6922 | // size argument. | ||||
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6923 | unsigned ArraySizeScale; |
6924 | int ArrayOffset; | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6925 | Value *NumElements = // See if the array size is a decomposable linear expr. |
6926 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); | ||||
6927 | |||||
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6928 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
6929 | // do the xform. | ||||
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6930 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
6931 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; | ||||
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6932 | |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6933 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
6934 | Value *Amt = 0; | ||||
6935 | if (Scale == 1) { | ||||
6936 | Amt = NumElements; | ||||
6937 | } else { | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6938 | // If the allocation size is constant, form a constant mul expression |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6939 | Amt = ConstantInt::get(Type::Int32Ty, Scale); |
6940 | if (isa<ConstantInt>(NumElements)) | ||||
Zhou Sheng | 4a1822a | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6941 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6942 | // otherwise multiply the amount and the number of elements |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6943 | else if (Scale != 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6944 | Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp"); |
Chris Lattner | 455fcc8 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6945 | Amt = InsertNewInstBefore(Tmp, AI); |
Chris Lattner | 8142b0a | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6946 | } |
Chris Lattner | 0ddac2a | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6947 | } |
6948 | |||||
Jeff Cohen | 86796be | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6949 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
6950 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6951 | Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp"); |
Chris Lattner | cfd6510 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6952 | Amt = InsertNewInstBefore(Tmp, AI); |
6953 | } | ||||
6954 | |||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6955 | AllocationInst *New; |
6956 | if (isa<MallocInst>(AI)) | ||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6957 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6958 | else |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6959 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6960 | InsertNewInstBefore(New, AI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6961 | New->takeName(&AI); |
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6962 | |
6963 | // If the allocation has multiple uses, insert a cast and change all things | ||||
6964 | // that used it to use the new cast. This will also hack on CI, but it will | ||||
6965 | // die soon. | ||||
6966 | if (!AI.hasOneUse()) { | ||||
6967 | AddUsesToWorkList(AI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6968 | // New is the allocation instruction, pointer typed. AI is the original |
6969 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. | ||||
6970 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); | ||||
Chris Lattner | 39387a5 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6971 | InsertNewInstBefore(NewCast, AI); |
6972 | AI.replaceAllUsesWith(NewCast); | ||||
6973 | } | ||||
Chris Lattner | b3f8397 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6974 | return ReplaceInstUsesWith(CI, New); |
6975 | } | ||||
6976 | |||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6977 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6978 | /// and return it as type Ty without inserting any new casts and without |
6979 | /// changing the computed value. This is used by code that tries to decide | ||||
6980 | /// whether promoting or shrinking integer operations to wider or smaller types | ||||
6981 | /// will allow us to eliminate a truncate or extend. | ||||
6982 | /// | ||||
6983 | /// This is a truncation operation if Ty is smaller than V->getType(), or an | ||||
6984 | /// extension operation if Ty is larger. | ||||
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 6985 | /// |
6986 | /// If CastOpc is a truncation, then Ty will be a type smaller than V. We | ||||
6987 | /// should return true if trunc(V) can be computed by computing V in the smaller | ||||
6988 | /// type. If V is an instruction, then trunc(inst(x,y)) can be computed as | ||||
6989 | /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be | ||||
6990 | /// efficiently truncated. | ||||
6991 | /// | ||||
6992 | /// If CastOpc is a sext or zext, we are asking if the low bits of the value can | ||||
6993 | /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get | ||||
6994 | /// the final result. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 6995 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, |
6996 | unsigned CastOpc, | ||||
6997 | int &NumCastsRemoved) { | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6998 | // We can always evaluate constants in another type. |
6999 | if (isa<ConstantInt>(V)) | ||||
7000 | return true; | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7001 | |
7002 | Instruction *I = dyn_cast<Instruction>(V); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7003 | if (!I) return false; |
7004 | |||||
7005 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7006 | |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7007 | // If this is an extension or truncate, we can often eliminate it. |
7008 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { | ||||
7009 | // If this is a cast from the destination type, we can trivially eliminate | ||||
7010 | // it, and this will remove a cast overall. | ||||
7011 | if (I->getOperand(0)->getType() == Ty) { | ||||
7012 | // If the first operand is itself a cast, and is eliminable, do not count | ||||
7013 | // this as an eliminable cast. We would prefer to eliminate those two | ||||
7014 | // casts first. | ||||
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7015 | if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse()) |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7016 | ++NumCastsRemoved; |
7017 | return true; | ||||
7018 | } | ||||
7019 | } | ||||
7020 | |||||
7021 | // We can't extend or shrink something that has multiple uses: doing so would | ||||
7022 | // require duplicating the instruction in general, which isn't profitable. | ||||
7023 | if (!I->hasOneUse()) return false; | ||||
7024 | |||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7025 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7026 | case Instruction::Add: |
7027 | case Instruction::Sub: | ||||
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7028 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7029 | case Instruction::And: |
7030 | case Instruction::Or: | ||||
7031 | case Instruction::Xor: | ||||
7032 | // These operators can all arbitrarily be extended or truncated. | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7033 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
7034 | NumCastsRemoved) && | ||||
7035 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, | ||||
7036 | NumCastsRemoved); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7037 | |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7038 | case Instruction::Shl: |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7039 | // If we are truncating the result of this SHL, and if it's a shift of a |
7040 | // constant amount, we can always perform a SHL in a smaller type. | ||||
7041 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7042 | uint32_t BitWidth = Ty->getBitWidth(); |
7043 | if (BitWidth < OrigTy->getBitWidth() && | ||||
7044 | CI->getLimitedValue(BitWidth) < BitWidth) | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7045 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
7046 | NumCastsRemoved); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7047 | } |
7048 | break; | ||||
7049 | case Instruction::LShr: | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7050 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
7051 | // lshr iff we know that the bits we would otherwise be shifting in are | ||||
7052 | // already zeros. | ||||
7053 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7054 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); |
7055 | uint32_t BitWidth = Ty->getBitWidth(); | ||||
7056 | if (BitWidth < OrigBitWidth && | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7057 | MaskedValueIsZero(I->getOperand(0), |
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7058 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
7059 | CI->getLimitedValue(BitWidth) < BitWidth) { | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7060 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
7061 | NumCastsRemoved); | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7062 | } |
7063 | } | ||||
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7064 | break; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7065 | case Instruction::ZExt: |
7066 | case Instruction::SExt: | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7067 | case Instruction::Trunc: |
7068 | // 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] | 7069 | // can safely replace it. Note that replacing it does not reduce the number |
7070 | // of casts in the input. | ||||
7071 | if (I->getOpcode() == CastOpc) | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7072 | return true; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7073 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7074 | case Instruction::Select: { |
7075 | SelectInst *SI = cast<SelectInst>(I); | ||||
7076 | return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc, | ||||
7077 | NumCastsRemoved) && | ||||
7078 | CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc, | ||||
7079 | NumCastsRemoved); | ||||
7080 | } | ||||
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7081 | case Instruction::PHI: { |
7082 | // We can change a phi if we can change all operands. | ||||
7083 | PHINode *PN = cast<PHINode>(I); | ||||
7084 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) | ||||
7085 | if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc, | ||||
7086 | NumCastsRemoved)) | ||||
7087 | return false; | ||||
7088 | return true; | ||||
7089 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7090 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7091 | // TODO: Can handle more cases here. |
7092 | break; | ||||
7093 | } | ||||
7094 | |||||
7095 | return false; | ||||
7096 | } | ||||
7097 | |||||
7098 | /// EvaluateInDifferentType - Given an expression that | ||||
7099 | /// CanEvaluateInDifferentType returns true for, actually insert the code to | ||||
7100 | /// evaluate the expression. | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7101 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7102 | bool isSigned) { |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7103 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7104 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7105 | |
7106 | // Otherwise, it must be an instruction. | ||||
7107 | Instruction *I = cast<Instruction>(V); | ||||
Chris Lattner | 01859e8 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 7108 | Instruction *Res = 0; |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7109 | switch (I->getOpcode()) { |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7110 | case Instruction::Add: |
7111 | case Instruction::Sub: | ||||
Nick Lewycky | e6b0c00 | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 7112 | case Instruction::Mul: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7113 | case Instruction::And: |
7114 | case Instruction::Or: | ||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7115 | case Instruction::Xor: |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7116 | case Instruction::AShr: |
7117 | case Instruction::LShr: | ||||
7118 | case Instruction::Shl: { | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7119 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7120 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7121 | Res = BinaryOperator::Create((Instruction::BinaryOps)I->getOpcode(), |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7122 | LHS, RHS); |
Chris Lattner | 46b9605 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 7123 | break; |
7124 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7125 | case Instruction::Trunc: |
7126 | case Instruction::ZExt: | ||||
7127 | case Instruction::SExt: | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7128 | // 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] | 7129 | // just return the source. There's no need to insert it because it is not |
7130 | // new. | ||||
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7131 | if (I->getOperand(0)->getType() == Ty) |
7132 | return I->getOperand(0); | ||||
7133 | |||||
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7134 | // Otherwise, must be the same type of cast, so just reinsert a new one. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7135 | Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7136 | Ty); |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7137 | break; |
Nick Lewycky | b8cd6a4 | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7138 | case Instruction::Select: { |
7139 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); | ||||
7140 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); | ||||
7141 | Res = SelectInst::Create(I->getOperand(0), True, False); | ||||
7142 | break; | ||||
7143 | } | ||||
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7144 | case Instruction::PHI: { |
7145 | PHINode *OPN = cast<PHINode>(I); | ||||
7146 | PHINode *NPN = PHINode::Create(Ty); | ||||
7147 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { | ||||
7148 | Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); | ||||
7149 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); | ||||
7150 | } | ||||
7151 | Res = NPN; | ||||
7152 | break; | ||||
7153 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7154 | default: |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7155 | // TODO: Can handle more cases here. |
7156 | assert(0 && "Unreachable!"); | ||||
7157 | break; | ||||
7158 | } | ||||
7159 | |||||
Chris Lattner | 8114b71 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7160 | Res->takeName(I); |
Chris Lattner | 70074e0 | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 7161 | return InsertNewInstBefore(Res, *I); |
7162 | } | ||||
7163 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7164 | /// @brief Implement the transforms common to all CastInst visitors. |
7165 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { | ||||
Chris Lattner | 79d35b3 | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 7166 | Value *Src = CI.getOperand(0); |
7167 | |||||
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 7168 | // 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] | 7169 | // eliminate it now. |
Chris Lattner | 6e7ba45 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 7170 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7171 | if (Instruction::CastOps opc = |
7172 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { | ||||
7173 | // The first cast (CSrc) is eliminable so we need to fix up or replace | ||||
7174 | // the second cast (CI). CSrc will then have a good chance of being dead. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7175 | return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); |
Chris Lattner | 8fd217c | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 7176 | } |
7177 | } | ||||
Chris Lattner | a710ddc | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 7178 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7179 | // 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] | 7180 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
7181 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) | ||||
7182 | return NV; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7183 | |
7184 | // 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] | 7185 | if (isa<PHINode>(Src)) |
7186 | if (Instruction *NV = FoldOpIntoPhi(CI)) | ||||
7187 | return NV; | ||||
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 7188 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7189 | return 0; |
7190 | } | ||||
7191 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7192 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
7193 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { | ||||
7194 | Value *Src = CI.getOperand(0); | ||||
7195 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7196 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7197 | // If casting the result of a getelementptr instruction with no offset, turn |
7198 | // this into a cast of the original pointer! | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7199 | if (GEP->hasAllZeroIndices()) { |
7200 | // Changing the cast operand is usually not a good idea but it is safe | ||||
7201 | // here because the pointer operand is being replaced with another | ||||
7202 | // pointer operand so the opcode doesn't need to change. | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7203 | AddToWorkList(GEP); |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7204 | CI.setOperand(0, GEP->getOperand(0)); |
7205 | return &CI; | ||||
7206 | } | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7207 | |
7208 | // If the GEP has a single use, and the base pointer is a bitcast, and the | ||||
7209 | // GEP computes a constant offset, see if we can convert these three | ||||
7210 | // instructions into fewer. This typically happens with unions and other | ||||
7211 | // non-type-safe code. | ||||
7212 | if (GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { | ||||
7213 | if (GEP->hasAllConstantIndices()) { | ||||
7214 | // We are guaranteed to get a constant from EmitGEPOffset. | ||||
7215 | ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); | ||||
7216 | int64_t Offset = OffsetV->getSExtValue(); | ||||
7217 | |||||
7218 | // Get the base pointer input of the bitcast, and the type it points to. | ||||
7219 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); | ||||
7220 | const Type *GEPIdxTy = | ||||
7221 | cast<PointerType>(OrigBase->getType())->getElementType(); | ||||
7222 | if (GEPIdxTy->isSized()) { | ||||
7223 | SmallVector<Value*, 8> NewIndices; | ||||
7224 | |||||
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7225 | // Start with the index over the outer type. Note that the type size |
7226 | // might be zero (even if the offset isn't zero) if the indexed type | ||||
7227 | // is something like [0 x {int, int}] | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7228 | const Type *IntPtrTy = TD->getIntPtrType(); |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7229 | int64_t FirstIdx = 0; |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7230 | if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) { |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7231 | FirstIdx = Offset/TySize; |
7232 | Offset %= TySize; | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7233 | |
Chris Lattner | c42e226 | 2007-05-05 01:59:31 +0000 | [diff] [blame] | 7234 | // Handle silly modulus not returning values values [0..TySize). |
7235 | if (Offset < 0) { | ||||
7236 | --FirstIdx; | ||||
7237 | Offset += TySize; | ||||
7238 | assert(Offset >= 0); | ||||
7239 | } | ||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 7240 | assert((uint64_t)Offset < (uint64_t)TySize &&"Out of range offset"); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7241 | } |
7242 | |||||
7243 | NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7244 | |
7245 | // Index into the types. If we fail, set OrigBase to null. | ||||
7246 | while (Offset) { | ||||
7247 | if (const StructType *STy = dyn_cast<StructType>(GEPIdxTy)) { | ||||
7248 | const StructLayout *SL = TD->getStructLayout(STy); | ||||
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7249 | if (Offset < (int64_t)SL->getSizeInBytes()) { |
7250 | unsigned Elt = SL->getElementContainingOffset(Offset); | ||||
7251 | NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7252 | |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7253 | Offset -= SL->getElementOffset(Elt); |
7254 | GEPIdxTy = STy->getElementType(Elt); | ||||
7255 | } else { | ||||
7256 | // Otherwise, we can't index into this, bail out. | ||||
7257 | Offset = 0; | ||||
7258 | OrigBase = 0; | ||||
7259 | } | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7260 | } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) { |
7261 | const SequentialType *STy = cast<SequentialType>(GEPIdxTy); | ||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 7262 | if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){ |
Chris Lattner | 6b6aef8 | 2007-05-15 00:16:00 +0000 | [diff] [blame] | 7263 | NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); |
7264 | Offset %= EltSize; | ||||
7265 | } else { | ||||
7266 | NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); | ||||
7267 | } | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7268 | GEPIdxTy = STy->getElementType(); |
7269 | } else { | ||||
7270 | // Otherwise, we can't index into this, bail out. | ||||
7271 | Offset = 0; | ||||
7272 | OrigBase = 0; | ||||
7273 | } | ||||
7274 | } | ||||
7275 | if (OrigBase) { | ||||
7276 | // If we were able to index down into an element, create the GEP | ||||
7277 | // and bitcast the result. This eliminates one bitcast, potentially | ||||
7278 | // two. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7279 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
7280 | NewIndices.begin(), | ||||
7281 | NewIndices.end(), ""); | ||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7282 | InsertNewInstBefore(NGEP, CI); |
7283 | NGEP->takeName(GEP); | ||||
7284 | |||||
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 7285 | if (isa<BitCastInst>(CI)) |
7286 | return new BitCastInst(NGEP, CI.getType()); | ||||
7287 | assert(isa<PtrToIntInst>(CI)); | ||||
7288 | return new PtrToIntInst(NGEP, CI.getType()); | ||||
7289 | } | ||||
7290 | } | ||||
7291 | } | ||||
7292 | } | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7293 | } |
7294 | |||||
7295 | return commonCastTransforms(CI); | ||||
7296 | } | ||||
7297 | |||||
7298 | |||||
7299 | |||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7300 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as |
7301 | /// integer types. This function implements the common transforms for all those | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7302 | /// cases. |
7303 | /// @brief Implement the transforms common to CastInst with integer operands | ||||
7304 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { | ||||
7305 | if (Instruction *Result = commonCastTransforms(CI)) | ||||
7306 | return Result; | ||||
7307 | |||||
7308 | Value *Src = CI.getOperand(0); | ||||
7309 | const Type *SrcTy = Src->getType(); | ||||
7310 | const Type *DestTy = CI.getType(); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7311 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
7312 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7313 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7314 | // See if we can simplify any instructions used by the LHS whose sole |
7315 | // purpose is to compute bits we don't care about. | ||||
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7316 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); |
7317 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7318 | KnownZero, KnownOne)) |
7319 | return &CI; | ||||
7320 | |||||
7321 | // If the source isn't an instruction or has more than one use then we | ||||
7322 | // can't do anything more. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7323 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
7324 | if (!SrcI || !Src->hasOneUse()) | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7325 | return 0; |
7326 | |||||
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7327 | // Attempt to propagate the cast into the instruction for int->int casts. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7328 | int NumCastsRemoved = 0; |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7329 | if (!isa<BitCastInst>(CI) && |
7330 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), | ||||
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7331 | CI.getOpcode(), NumCastsRemoved)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7332 | // If this cast is a truncate, evaluting in a different type always |
Chris Lattner | 951626b | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7333 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
7334 | // we need to do an AND to maintain the clear top-part of the computation, | ||||
7335 | // so we require that the input have eliminated at least one cast. If this | ||||
7336 | // 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] | 7337 | // require that two casts have been eliminated. |
Chris Lattner | c739cd6 | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 7338 | bool DoXForm; |
7339 | switch (CI.getOpcode()) { | ||||
7340 | default: | ||||
7341 | // All the others use floating point so we shouldn't actually | ||||
7342 | // get here because of the check above. | ||||
7343 | assert(0 && "Unknown cast type"); | ||||
7344 | case Instruction::Trunc: | ||||
7345 | DoXForm = true; | ||||
7346 | break; | ||||
7347 | case Instruction::ZExt: | ||||
7348 | DoXForm = NumCastsRemoved >= 1; | ||||
7349 | break; | ||||
7350 | case Instruction::SExt: | ||||
7351 | DoXForm = NumCastsRemoved >= 2; | ||||
7352 | break; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7353 | } |
7354 | |||||
7355 | if (DoXForm) { | ||||
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 7356 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
7357 | CI.getOpcode() == Instruction::SExt); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7358 | assert(Res->getType() == DestTy); |
7359 | switch (CI.getOpcode()) { | ||||
7360 | default: assert(0 && "Unknown cast type!"); | ||||
7361 | case Instruction::Trunc: | ||||
7362 | case Instruction::BitCast: | ||||
7363 | // Just replace this cast with the result. | ||||
7364 | return ReplaceInstUsesWith(CI, Res); | ||||
7365 | case Instruction::ZExt: { | ||||
7366 | // We need to emit an AND to clear the high bits. | ||||
7367 | assert(SrcBitSize < DestBitSize && "Not a zext?"); | ||||
Chris Lattner | cd1d6d5 | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 7368 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, |
7369 | SrcBitSize)); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7370 | return BinaryOperator::CreateAnd(Res, C); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7371 | } |
7372 | case Instruction::SExt: | ||||
7373 | // We need to emit a cast to truncate, then a cast to sext. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7374 | return CastInst::Create(Instruction::SExt, |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7375 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
7376 | CI), DestTy); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7377 | } |
7378 | } | ||||
7379 | } | ||||
7380 | |||||
7381 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; | ||||
7382 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; | ||||
7383 | |||||
7384 | switch (SrcI->getOpcode()) { | ||||
7385 | case Instruction::Add: | ||||
7386 | case Instruction::Mul: | ||||
7387 | case Instruction::And: | ||||
7388 | case Instruction::Or: | ||||
7389 | case Instruction::Xor: | ||||
Chris Lattner | 01deb9d | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 7390 | // If we are discarding information, rewrite. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7391 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { |
7392 | // Don't insert two casts if they cannot be eliminated. We allow | ||||
7393 | // two casts to be inserted if the sizes are the same. This could | ||||
7394 | // only be converting signedness, which is a noop. | ||||
7395 | if (DestBitSize == SrcBitSize || | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7396 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || |
7397 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { | ||||
Reid Spencer | 7eb7638 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 7398 | Instruction::CastOps opcode = CI.getOpcode(); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7399 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); |
7400 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7401 | return BinaryOperator::Create( |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7402 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7403 | } |
7404 | } | ||||
7405 | |||||
7406 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 | ||||
7407 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && | ||||
7408 | SrcI->getOpcode() == Instruction::Xor && | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7409 | Op1 == ConstantInt::getTrue() && |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7410 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7411 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7412 | return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7413 | } |
7414 | break; | ||||
7415 | case Instruction::SDiv: | ||||
7416 | case Instruction::UDiv: | ||||
7417 | case Instruction::SRem: | ||||
7418 | case Instruction::URem: | ||||
7419 | // If we are just changing the sign, rewrite. | ||||
7420 | if (DestBitSize == SrcBitSize) { | ||||
7421 | // Don't insert two casts if they cannot be eliminated. We allow | ||||
7422 | // two casts to be inserted if the sizes are the same. This could | ||||
7423 | // only be converting signedness, which is a noop. | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7424 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
7425 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7426 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, |
7427 | Op0, DestTy, SrcI); | ||||
7428 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, | ||||
7429 | Op1, DestTy, SrcI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7430 | return BinaryOperator::Create( |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7431 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
7432 | } | ||||
7433 | } | ||||
7434 | break; | ||||
7435 | |||||
7436 | case Instruction::Shl: | ||||
7437 | // Allow changing the sign of the source operand. Do not allow | ||||
7438 | // changing the size of the shift, UNLESS the shift amount is a | ||||
7439 | // constant. We must not change variable sized shifts to a smaller | ||||
7440 | // size, because it is undefined to shift more bits out than exist | ||||
7441 | // in the value. | ||||
7442 | if (DestBitSize == SrcBitSize || | ||||
7443 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7444 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? |
7445 | Instruction::BitCast : Instruction::Trunc); | ||||
7446 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7447 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7448 | return BinaryOperator::CreateShl(Op0c, Op1c); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7449 | } |
7450 | break; | ||||
7451 | case Instruction::AShr: | ||||
7452 | // If this is a signed shr, and if all bits shifted in are about to be | ||||
7453 | // truncated off, turn it into an unsigned shr to allow greater | ||||
7454 | // simplifications. | ||||
7455 | if (DestBitSize < SrcBitSize && | ||||
7456 | isa<ConstantInt>(Op1)) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7457 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7458 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { |
7459 | // Insert the new logical shift right. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7460 | return BinaryOperator::CreateLShr(Op0, Op1); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7461 | } |
7462 | } | ||||
7463 | break; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7464 | } |
7465 | return 0; | ||||
7466 | } | ||||
7467 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7468 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7469 | if (Instruction *Result = commonIntCastTransforms(CI)) |
7470 | return Result; | ||||
7471 | |||||
7472 | Value *Src = CI.getOperand(0); | ||||
7473 | const Type *Ty = CI.getType(); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7474 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); |
7475 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); | ||||
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7476 | |
7477 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { | ||||
7478 | switch (SrcI->getOpcode()) { | ||||
7479 | default: break; | ||||
7480 | case Instruction::LShr: | ||||
7481 | // We can shrink lshr to something smaller if we know the bits shifted in | ||||
7482 | // are already zeros. | ||||
7483 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { | ||||
Zhou Sheng | 302748d | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 7484 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7485 | |
7486 | // Get a mask for the bits shifting in. | ||||
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7487 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7488 | Value* SrcIOp0 = SrcI->getOperand(0); |
7489 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { | ||||
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7490 | if (ShAmt >= DestBitWidth) // All zeros. |
7491 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); | ||||
7492 | |||||
7493 | // Okay, we can shrink this. Truncate the input, then return a new | ||||
7494 | // shift. | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7495 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); |
7496 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), | ||||
7497 | Ty, CI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7498 | return BinaryOperator::CreateLShr(V1, V2); |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7499 | } |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7500 | } else { // This is a variable shr. |
7501 | |||||
7502 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'. This is | ||||
7503 | // more LLVM instructions, but allows '1 << Y' to be hoisted if | ||||
7504 | // loop-invariant and CSE'd. | ||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7505 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7506 | Value *One = ConstantInt::get(SrcI->getType(), 1); |
7507 | |||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7508 | Value *V = InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7509 | BinaryOperator::CreateShl(One, SrcI->getOperand(1), |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7510 | "tmp"), CI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7511 | V = InsertNewInstBefore(BinaryOperator::CreateAnd(V, |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7512 | SrcI->getOperand(0), |
7513 | "tmp"), CI); | ||||
7514 | Value *Zero = Constant::getNullValue(V->getType()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7515 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); |
Chris Lattner | e13ab2a | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 7516 | } |
Chris Lattner | 6aa5eb1 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 7517 | } |
7518 | break; | ||||
7519 | } | ||||
7520 | } | ||||
7521 | |||||
7522 | return 0; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7523 | } |
7524 | |||||
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7525 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
7526 | /// in order to eliminate the icmp. | ||||
7527 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, | ||||
7528 | bool DoXform) { | ||||
7529 | // If we are just checking for a icmp eq of a single bit and zext'ing it | ||||
7530 | // to an integer, then shift the bit to the appropriate place and then | ||||
7531 | // cast to integer to avoid the comparison. | ||||
7532 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { | ||||
7533 | const APInt &Op1CV = Op1C->getValue(); | ||||
7534 | |||||
7535 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. | ||||
7536 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. | ||||
7537 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || | ||||
7538 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { | ||||
7539 | if (!DoXform) return ICI; | ||||
7540 | |||||
7541 | Value *In = ICI->getOperand(0); | ||||
7542 | Value *Sh = ConstantInt::get(In->getType(), | ||||
7543 | In->getType()->getPrimitiveSizeInBits()-1); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7544 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7545 | In->getName()+".lobit"), |
7546 | CI); | ||||
7547 | if (In->getType() != CI.getType()) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7548 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7549 | false/*ZExt*/, "tmp", &CI); |
7550 | |||||
7551 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { | ||||
7552 | Constant *One = ConstantInt::get(In->getType(), 1); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7553 | In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7554 | In->getName()+".not"), |
7555 | CI); | ||||
7556 | } | ||||
7557 | |||||
7558 | return ReplaceInstUsesWith(CI, In); | ||||
7559 | } | ||||
7560 | |||||
7561 | |||||
7562 | |||||
7563 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. | ||||
7564 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. | ||||
7565 | // zext (X == 1) to i32 --> X iff X has only the low bit set. | ||||
7566 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. | ||||
7567 | // zext (X != 0) to i32 --> X iff X has only the low bit set. | ||||
7568 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. | ||||
7569 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. | ||||
7570 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. | ||||
7571 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && | ||||
7572 | // This only works for EQ and NE | ||||
7573 | ICI->isEquality()) { | ||||
7574 | // If Op1C some other power of two, convert: | ||||
7575 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); | ||||
7576 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
7577 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); | ||||
7578 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); | ||||
7579 | |||||
7580 | APInt KnownZeroMask(~KnownZero); | ||||
7581 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? | ||||
7582 | if (!DoXform) return ICI; | ||||
7583 | |||||
7584 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; | ||||
7585 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { | ||||
7586 | // (X&4) == 2 --> false | ||||
7587 | // (X&4) != 2 --> true | ||||
7588 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); | ||||
7589 | Res = ConstantExpr::getZExt(Res, CI.getType()); | ||||
7590 | return ReplaceInstUsesWith(CI, Res); | ||||
7591 | } | ||||
7592 | |||||
7593 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); | ||||
7594 | Value *In = ICI->getOperand(0); | ||||
7595 | if (ShiftAmt) { | ||||
7596 | // Perform a logical shr by shiftamt. | ||||
7597 | // Insert the shift to put the result in the low bit. | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7598 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7599 | ConstantInt::get(In->getType(), ShiftAmt), |
7600 | In->getName()+".lobit"), CI); | ||||
7601 | } | ||||
7602 | |||||
7603 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. | ||||
7604 | Constant *One = ConstantInt::get(In->getType(), 1); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7605 | In = BinaryOperator::CreateXor(In, One, "tmp"); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7606 | InsertNewInstBefore(cast<Instruction>(In), CI); |
7607 | } | ||||
7608 | |||||
7609 | if (CI.getType() == In->getType()) | ||||
7610 | return ReplaceInstUsesWith(CI, In); | ||||
7611 | else | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7612 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7613 | } |
7614 | } | ||||
7615 | } | ||||
7616 | |||||
7617 | return 0; | ||||
7618 | } | ||||
7619 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7620 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7621 | // If one of the common conversion will work .. |
7622 | if (Instruction *Result = commonIntCastTransforms(CI)) | ||||
7623 | return Result; | ||||
7624 | |||||
7625 | Value *Src = CI.getOperand(0); | ||||
7626 | |||||
7627 | // If this is a cast of a cast | ||||
7628 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7629 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
7630 | // types and if the sizes are just right we can convert this into a logical | ||||
7631 | // 'and' which will be much cheaper than the pair of casts. | ||||
7632 | if (isa<TruncInst>(CSrc)) { | ||||
7633 | // Get the sizes of the types involved | ||||
7634 | Value *A = CSrc->getOperand(0); | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 7635 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); |
7636 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); | ||||
7637 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7638 | // If we're actually extending zero bits and the trunc is a no-op |
7639 | if (MidSize < DstSize && SrcSize == DstSize) { | ||||
7640 | // Replace both of the casts with an And of the type mask. | ||||
Zhou Sheng | e82fca0 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 7641 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
Reid Spencer | ad6676e | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 7642 | Constant *AndConst = ConstantInt::get(AndValue); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7643 | Instruction *And = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7644 | BinaryOperator::CreateAnd(CSrc->getOperand(0), AndConst); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7645 | // Unfortunately, if the type changed, we need to cast it back. |
7646 | if (And->getType() != CI.getType()) { | ||||
7647 | And->setName(CSrc->getName()+".mask"); | ||||
7648 | InsertNewInstBefore(And, CI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7649 | And = CastInst::CreateIntegerCast(And, CI.getType(), false/*ZExt*/); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7650 | } |
7651 | return And; | ||||
7652 | } | ||||
7653 | } | ||||
7654 | } | ||||
7655 | |||||
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7656 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
7657 | return transformZExtICmp(ICI, CI); | ||||
Chris Lattner | a2e2c9b | 2007-04-11 06:53:04 +0000 | [diff] [blame] | 7658 | |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7659 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
7660 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { | ||||
7661 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one | ||||
7662 | // of the (zext icmp) will be transformed. | ||||
7663 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); | ||||
7664 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); | ||||
7665 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && | ||||
7666 | (transformZExtICmp(LHS, CI, false) || | ||||
7667 | transformZExtICmp(RHS, CI, false))) { | ||||
7668 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); | ||||
7669 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7670 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
Chris Lattner | 66bc325 | 2007-04-11 05:45:39 +0000 | [diff] [blame] | 7671 | } |
Evan Cheng | b98a10e | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 7672 | } |
7673 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7674 | return 0; |
7675 | } | ||||
7676 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7677 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7678 | if (Instruction *I = commonIntCastTransforms(CI)) |
7679 | return I; | ||||
7680 | |||||
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7681 | Value *Src = CI.getOperand(0); |
7682 | |||||
7683 | // sext (x <s 0) -> ashr x, 31 -> all ones if signed | ||||
7684 | // sext (x >s -1) -> ashr x, 31 -> all ones if not signed | ||||
7685 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) { | ||||
7686 | // If we are just checking for a icmp eq of a single bit and zext'ing it | ||||
7687 | // to an integer, then shift the bit to the appropriate place and then | ||||
7688 | // cast to integer to avoid the comparison. | ||||
7689 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { | ||||
7690 | const APInt &Op1CV = Op1C->getValue(); | ||||
7691 | |||||
7692 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. | ||||
7693 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. | ||||
7694 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || | ||||
7695 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())){ | ||||
7696 | Value *In = ICI->getOperand(0); | ||||
7697 | Value *Sh = ConstantInt::get(In->getType(), | ||||
7698 | In->getType()->getPrimitiveSizeInBits()-1); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7699 | In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 7700 | In->getName()+".lobit"), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7701 | CI); |
7702 | if (In->getType() != CI.getType()) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7703 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7704 | true/*SExt*/, "tmp", &CI); |
7705 | |||||
7706 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7707 | In = InsertNewInstBefore(BinaryOperator::CreateNot(In, |
Chris Lattner | 8a9f571 | 2007-04-11 06:57:46 +0000 | [diff] [blame] | 7708 | In->getName()+".not"), CI); |
7709 | |||||
7710 | return ReplaceInstUsesWith(CI, In); | ||||
7711 | } | ||||
7712 | } | ||||
7713 | } | ||||
Dan Gohman | f35c882 | 2008-05-20 21:01:12 +0000 | [diff] [blame] | 7714 | |
7715 | // See if the value being truncated is already sign extended. If so, just | ||||
7716 | // eliminate the trunc/sext pair. | ||||
7717 | if (getOpcode(Src) == Instruction::Trunc) { | ||||
7718 | Value *Op = cast<User>(Src)->getOperand(0); | ||||
7719 | unsigned OpBits = cast<IntegerType>(Op->getType())->getBitWidth(); | ||||
7720 | unsigned MidBits = cast<IntegerType>(Src->getType())->getBitWidth(); | ||||
7721 | unsigned DestBits = cast<IntegerType>(CI.getType())->getBitWidth(); | ||||
7722 | unsigned NumSignBits = ComputeNumSignBits(Op); | ||||
7723 | |||||
7724 | if (OpBits == DestBits) { | ||||
7725 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign | ||||
7726 | // bits, it is already ready. | ||||
7727 | if (NumSignBits > DestBits-MidBits) | ||||
7728 | return ReplaceInstUsesWith(CI, Op); | ||||
7729 | } else if (OpBits < DestBits) { | ||||
7730 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign | ||||
7731 | // bits, just sext from i32. | ||||
7732 | if (NumSignBits > OpBits-MidBits) | ||||
7733 | return new SExtInst(Op, CI.getType(), "tmp"); | ||||
7734 | } else { | ||||
7735 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign | ||||
7736 | // bits, just truncate to i32. | ||||
7737 | if (NumSignBits > OpBits-MidBits) | ||||
7738 | return new TruncInst(Op, CI.getType(), "tmp"); | ||||
7739 | } | ||||
7740 | } | ||||
Chris Lattner | 46bbad2 | 2008-08-06 07:35:52 +0000 | [diff] [blame] | 7741 | |
7742 | // If the input is a shl/ashr pair of a same constant, then this is a sign | ||||
7743 | // extension from a smaller value. If we could trust arbitrary bitwidth | ||||
7744 | // integers, we could turn this into a truncate to the smaller bit and then | ||||
7745 | // use a sext for the whole extension. Since we don't, look deeper and check | ||||
7746 | // for a truncate. If the source and dest are the same type, eliminate the | ||||
7747 | // trunc and extend and just do shifts. For example, turn: | ||||
7748 | // %a = trunc i32 %i to i8 | ||||
7749 | // %b = shl i8 %a, 6 | ||||
7750 | // %c = ashr i8 %b, 6 | ||||
7751 | // %d = sext i8 %c to i32 | ||||
7752 | // into: | ||||
7753 | // %a = shl i32 %i, 30 | ||||
7754 | // %d = ashr i32 %a, 30 | ||||
7755 | Value *A = 0; | ||||
7756 | ConstantInt *BA = 0, *CA = 0; | ||||
7757 | if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)), | ||||
7758 | m_ConstantInt(CA))) && | ||||
7759 | BA == CA && isa<TruncInst>(A)) { | ||||
7760 | Value *I = cast<TruncInst>(A)->getOperand(0); | ||||
7761 | if (I->getType() == CI.getType()) { | ||||
7762 | unsigned MidSize = Src->getType()->getPrimitiveSizeInBits(); | ||||
7763 | unsigned SrcDstSize = CI.getType()->getPrimitiveSizeInBits(); | ||||
7764 | unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; | ||||
7765 | Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); | ||||
7766 | I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV, | ||||
7767 | CI.getName()), CI); | ||||
7768 | return BinaryOperator::CreateAShr(I, ShAmtV); | ||||
7769 | } | ||||
7770 | } | ||||
7771 | |||||
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 7772 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7773 | } |
7774 | |||||
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7775 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
7776 | /// in the specified FP type without changing its value. | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7777 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7778 | APFloat F = CFP->getValueAPF(); |
7779 | if (F.convert(Sem, APFloat::rmNearestTiesToEven) == APFloat::opOK) | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7780 | return ConstantFP::get(F); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7781 | return 0; |
7782 | } | ||||
7783 | |||||
7784 | /// LookThroughFPExtensions - If this is an fp extension instruction, look | ||||
7785 | /// through it until we get the source value. | ||||
7786 | static Value *LookThroughFPExtensions(Value *V) { | ||||
7787 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||
7788 | if (I->getOpcode() == Instruction::FPExt) | ||||
7789 | return LookThroughFPExtensions(I->getOperand(0)); | ||||
7790 | |||||
7791 | // If this value is a constant, return the constant in the smallest FP type | ||||
7792 | // that can accurately represent it. This allows us to turn | ||||
7793 | // (float)((double)X+2.0) into x+2.0f. | ||||
7794 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { | ||||
7795 | if (CFP->getType() == Type::PPC_FP128Ty) | ||||
7796 | return V; // No constant folding of this. | ||||
7797 | // See if the value can be truncated to float and then reextended. | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7798 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7799 | return V; |
7800 | if (CFP->getType() == Type::DoubleTy) | ||||
7801 | return V; // Won't shrink. | ||||
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 7802 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble)) |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7803 | return V; |
7804 | // Don't try to shrink to various long double types. | ||||
7805 | } | ||||
7806 | |||||
7807 | return V; | ||||
7808 | } | ||||
7809 | |||||
7810 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { | ||||
7811 | if (Instruction *I = commonCastTransforms(CI)) | ||||
7812 | return I; | ||||
7813 | |||||
7814 | // If we have fptrunc(add (fpextend x), (fpextend y)), where x and y are | ||||
7815 | // smaller than the destination type, we can eliminate the truncate by doing | ||||
7816 | // the add as the smaller type. This applies to add/sub/mul/div as well as | ||||
7817 | // many builtins (sqrt, etc). | ||||
7818 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); | ||||
7819 | if (OpI && OpI->hasOneUse()) { | ||||
7820 | switch (OpI->getOpcode()) { | ||||
7821 | default: break; | ||||
7822 | case Instruction::Add: | ||||
7823 | case Instruction::Sub: | ||||
7824 | case Instruction::Mul: | ||||
7825 | case Instruction::FDiv: | ||||
7826 | case Instruction::FRem: | ||||
7827 | const Type *SrcTy = OpI->getType(); | ||||
7828 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0)); | ||||
7829 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1)); | ||||
7830 | if (LHSTrunc->getType() != SrcTy && | ||||
7831 | RHSTrunc->getType() != SrcTy) { | ||||
7832 | unsigned DstSize = CI.getType()->getPrimitiveSizeInBits(); | ||||
7833 | // If the source types were both smaller than the destination type of | ||||
7834 | // the cast, do this xform. | ||||
7835 | if (LHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize && | ||||
7836 | RHSTrunc->getType()->getPrimitiveSizeInBits() <= DstSize) { | ||||
7837 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, | ||||
7838 | CI.getType(), CI); | ||||
7839 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, | ||||
7840 | CI.getType(), CI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7841 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
Chris Lattner | b753065 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 7842 | } |
7843 | } | ||||
7844 | break; | ||||
7845 | } | ||||
7846 | } | ||||
7847 | return 0; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7848 | } |
7849 | |||||
7850 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { | ||||
7851 | return commonCastTransforms(CI); | ||||
7852 | } | ||||
7853 | |||||
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7854 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
Chris Lattner | 5af5f46 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 7855 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
7856 | if (OpI == 0) | ||||
7857 | return commonCastTransforms(FI); | ||||
7858 | |||||
7859 | // fptoui(uitofp(X)) --> X | ||||
7860 | // fptoui(sitofp(X)) --> X | ||||
7861 | // This is safe if the intermediate type has enough bits in its mantissa to | ||||
7862 | // accurately represent all values of X. For example, do not do this with | ||||
7863 | // i64->float->i64. This is also safe for sitofp case, because any negative | ||||
7864 | // 'X' value would cause an undefined result for the fptoui. | ||||
7865 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && | ||||
7866 | OpI->getOperand(0)->getType() == FI.getType() && | ||||
7867 | (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */ | ||||
7868 | OpI->getType()->getFPMantissaWidth()) | ||||
7869 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); | ||||
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7870 | |
7871 | return commonCastTransforms(FI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7872 | } |
7873 | |||||
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7874 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
Chris Lattner | 5af5f46 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 7875 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
7876 | if (OpI == 0) | ||||
7877 | return commonCastTransforms(FI); | ||||
7878 | |||||
7879 | // fptosi(sitofp(X)) --> X | ||||
7880 | // fptosi(uitofp(X)) --> X | ||||
7881 | // This is safe if the intermediate type has enough bits in its mantissa to | ||||
7882 | // accurately represent all values of X. For example, do not do this with | ||||
7883 | // i64->float->i64. This is also safe for sitofp case, because any negative | ||||
7884 | // 'X' value would cause an undefined result for the fptoui. | ||||
7885 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && | ||||
7886 | OpI->getOperand(0)->getType() == FI.getType() && | ||||
7887 | (int)FI.getType()->getPrimitiveSizeInBits() <= | ||||
7888 | OpI->getType()->getFPMantissaWidth()) | ||||
7889 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); | ||||
Chris Lattner | 0c7a9a0 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 7890 | |
7891 | return commonCastTransforms(FI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7892 | } |
7893 | |||||
7894 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { | ||||
7895 | return commonCastTransforms(CI); | ||||
7896 | } | ||||
7897 | |||||
7898 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { | ||||
7899 | return commonCastTransforms(CI); | ||||
7900 | } | ||||
7901 | |||||
7902 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7903 | return commonPointerCastTransforms(CI); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7904 | } |
7905 | |||||
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7906 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
7907 | if (Instruction *I = commonCastTransforms(CI)) | ||||
7908 | return I; | ||||
7909 | |||||
7910 | const Type *DestPointee = cast<PointerType>(CI.getType())->getElementType(); | ||||
7911 | if (!DestPointee->isSized()) return 0; | ||||
7912 | |||||
7913 | // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. | ||||
7914 | ConstantInt *Cst; | ||||
7915 | Value *X; | ||||
7916 | if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(m_Value(X)), | ||||
7917 | m_ConstantInt(Cst)))) { | ||||
7918 | // If the source and destination operands have the same type, see if this | ||||
7919 | // is a single-index GEP. | ||||
7920 | if (X->getType() == CI.getType()) { | ||||
7921 | // Get the size of the pointee type. | ||||
Bill Wendling | b9d4f8d | 2008-03-14 05:12:19 +0000 | [diff] [blame] | 7922 | uint64_t Size = TD->getABITypeSize(DestPointee); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7923 | |
7924 | // Convert the constant to intptr type. | ||||
7925 | APInt Offset = Cst->getValue(); | ||||
7926 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); | ||||
7927 | |||||
7928 | // If Offset is evenly divisible by Size, we can do this xform. | ||||
7929 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ | ||||
7930 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7931 | return GetElementPtrInst::Create(X, ConstantInt::get(Offset)); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7932 | } |
7933 | } | ||||
7934 | // TODO: Could handle other cases, e.g. where add is indexing into field of | ||||
7935 | // struct etc. | ||||
7936 | } else if (CI.getOperand(0)->hasOneUse() && | ||||
7937 | match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { | ||||
7938 | // Otherwise, if this is inttoptr(add x, cst), try to turn this into an | ||||
7939 | // "inttoptr+GEP" instead of "add+intptr". | ||||
7940 | |||||
7941 | // Get the size of the pointee type. | ||||
7942 | uint64_t Size = TD->getABITypeSize(DestPointee); | ||||
7943 | |||||
7944 | // Convert the constant to intptr type. | ||||
7945 | APInt Offset = Cst->getValue(); | ||||
7946 | Offset.sextOrTrunc(TD->getPointerSizeInBits()); | ||||
7947 | |||||
7948 | // If Offset is evenly divisible by Size, we can do this xform. | ||||
7949 | if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ | ||||
7950 | Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); | ||||
7951 | |||||
7952 | Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), | ||||
7953 | "tmp"), CI); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 7954 | return GetElementPtrInst::Create(P, ConstantInt::get(Offset), "tmp"); |
Chris Lattner | f9d9e45 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 7955 | } |
7956 | } | ||||
7957 | return 0; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7958 | } |
7959 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7960 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7961 | // If the operands are integer typed then apply the integer transforms, |
7962 | // otherwise just apply the common ones. | ||||
7963 | Value *Src = CI.getOperand(0); | ||||
7964 | const Type *SrcTy = Src->getType(); | ||||
7965 | const Type *DestTy = CI.getType(); | ||||
7966 | |||||
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7967 | if (SrcTy->isInteger() && DestTy->isInteger()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7968 | if (Instruction *Result = commonIntCastTransforms(CI)) |
7969 | return Result; | ||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7970 | } else if (isa<PointerType>(SrcTy)) { |
7971 | if (Instruction *I = commonPointerCastTransforms(CI)) | ||||
7972 | return I; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7973 | } else { |
7974 | if (Instruction *Result = commonCastTransforms(CI)) | ||||
7975 | return Result; | ||||
7976 | } | ||||
7977 | |||||
7978 | |||||
7979 | // Get rid of casts from one type to the same type. These are useless and can | ||||
7980 | // be replaced by the operand. | ||||
7981 | if (DestTy == Src->getType()) | ||||
7982 | return ReplaceInstUsesWith(CI, Src); | ||||
7983 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7984 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7985 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
7986 | const Type *DstElTy = DstPTy->getElementType(); | ||||
7987 | const Type *SrcElTy = SrcPTy->getElementType(); | ||||
7988 | |||||
Nate Begeman | 83ad90a | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 7989 | // If the address spaces don't match, don't eliminate the bitcast, which is |
7990 | // required for changing types. | ||||
7991 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) | ||||
7992 | return 0; | ||||
7993 | |||||
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 7994 | // If we are casting a malloc or alloca to a pointer to a type of the same |
7995 | // size, rewrite the allocation instruction to allocate the "right" type. | ||||
7996 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) | ||||
7997 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) | ||||
7998 | return V; | ||||
7999 | |||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 8000 | // If the source and destination are pointers, and this cast is equivalent |
8001 | // 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] | 8002 | // This can enhance SROA and other transforms that want type-safe pointers. |
8003 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); | ||||
8004 | unsigned NumZeros = 0; | ||||
8005 | while (SrcElTy != DstElTy && | ||||
8006 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && | ||||
8007 | SrcElTy->getNumContainedTypes() /* not "{}" */) { | ||||
8008 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); | ||||
8009 | ++NumZeros; | ||||
8010 | } | ||||
Chris Lattner | 4e998b2 | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 8011 | |
Chris Lattner | d3e2834 | 2007-04-27 17:44:50 +0000 | [diff] [blame] | 8012 | // If we found a path from the src to dest, create the getelementptr now. |
8013 | if (SrcElTy == DstElTy) { | ||||
8014 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8015 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
8016 | ((Instruction*) NULL)); | ||||
Chris Lattner | 9fb9213 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 8017 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8018 | } |
Chris Lattner | 24c8e38 | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 8019 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8020 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
8021 | if (SVI->hasOneUse()) { | ||||
8022 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is | ||||
8023 | // a bitconvert to a vector with the same # elts. | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8024 | if (isa<VectorType>(DestTy) && |
8025 | cast<VectorType>(DestTy)->getNumElements() == | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8026 | SVI->getType()->getNumElements()) { |
8027 | CastInst *Tmp; | ||||
8028 | // If either of the operands is a cast from CI.getType(), then | ||||
8029 | // evaluating the shuffle in the casted destination's type will allow | ||||
8030 | // us to eliminate at least one cast. | ||||
8031 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && | ||||
8032 | Tmp->getOperand(0)->getType() == DestTy) || | ||||
8033 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && | ||||
8034 | Tmp->getOperand(0)->getType() == DestTy)) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8035 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, |
8036 | SVI->getOperand(0), DestTy, &CI); | ||||
8037 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, | ||||
8038 | SVI->getOperand(1), DestTy, &CI); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8039 | // Return a new shuffle vector. Use the same element ID's, as we |
8040 | // know the vector types match #elts. | ||||
8041 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); | ||||
Chris Lattner | 01575b7 | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 8042 | } |
8043 | } | ||||
8044 | } | ||||
8045 | } | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 8046 | return 0; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8047 | } |
8048 | |||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8049 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
8050 | /// %C = or %A, %B | ||||
8051 | /// %D = select %cond, %C, %A | ||||
8052 | /// into: | ||||
8053 | /// %C = select %cond, %B, 0 | ||||
8054 | /// %D = or %A, %C | ||||
8055 | /// | ||||
8056 | /// Assuming that the specified instruction is an operand to the select, return | ||||
8057 | /// a bitmask indicating which operands of this instruction are foldable if they | ||||
8058 | /// equal the other incoming value of the select. | ||||
8059 | /// | ||||
8060 | static unsigned GetSelectFoldableOperands(Instruction *I) { | ||||
8061 | switch (I->getOpcode()) { | ||||
8062 | case Instruction::Add: | ||||
8063 | case Instruction::Mul: | ||||
8064 | case Instruction::And: | ||||
8065 | case Instruction::Or: | ||||
8066 | case Instruction::Xor: | ||||
8067 | return 3; // Can fold through either operand. | ||||
8068 | case Instruction::Sub: // Can only fold on the amount subtracted. | ||||
8069 | case Instruction::Shl: // Can only fold on the shift amount. | ||||
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8070 | case Instruction::LShr: |
8071 | case Instruction::AShr: | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8072 | return 1; |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8073 | default: |
8074 | return 0; // Cannot fold | ||||
8075 | } | ||||
8076 | } | ||||
8077 | |||||
8078 | /// GetSelectFoldableConstant - For the same transformation as the previous | ||||
8079 | /// function, return the identity constant that goes into the select. | ||||
8080 | static Constant *GetSelectFoldableConstant(Instruction *I) { | ||||
8081 | switch (I->getOpcode()) { | ||||
8082 | default: assert(0 && "This cannot happen!"); abort(); | ||||
8083 | case Instruction::Add: | ||||
8084 | case Instruction::Sub: | ||||
8085 | case Instruction::Or: | ||||
8086 | case Instruction::Xor: | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8087 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 8088 | case Instruction::LShr: |
8089 | case Instruction::AShr: | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8090 | return Constant::getNullValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8091 | case Instruction::And: |
Chris Lattner | 7cbe2eb | 2007-06-15 06:23:19 +0000 | [diff] [blame] | 8092 | return Constant::getAllOnesValue(I->getType()); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8093 | case Instruction::Mul: |
8094 | return ConstantInt::get(I->getType(), 1); | ||||
8095 | } | ||||
8096 | } | ||||
8097 | |||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8098 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
8099 | /// have the same opcode and only one use each. Try to simplify this. | ||||
8100 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, | ||||
8101 | Instruction *FI) { | ||||
8102 | if (TI->getNumOperands() == 1) { | ||||
8103 | // If this is a non-volatile load or a cast from the same type, | ||||
8104 | // merge. | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8105 | if (TI->isCast()) { |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8106 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
8107 | return 0; | ||||
8108 | } else { | ||||
8109 | return 0; // unknown unary op. | ||||
8110 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8111 | |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8112 | // Fold this by inserting a select from the input values. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8113 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
8114 | FI->getOperand(0), SI.getName()+".v"); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8115 | InsertNewInstBefore(NewSI, SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8116 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8117 | TI->getType()); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8118 | } |
8119 | |||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8120 | // Only handle binary operators here. |
8121 | if (!isa<BinaryOperator>(TI)) | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8122 | return 0; |
8123 | |||||
8124 | // Figure out if the operations have any operands in common. | ||||
8125 | Value *MatchOp, *OtherOpT, *OtherOpF; | ||||
8126 | bool MatchIsOpZero; | ||||
8127 | if (TI->getOperand(0) == FI->getOperand(0)) { | ||||
8128 | MatchOp = TI->getOperand(0); | ||||
8129 | OtherOpT = TI->getOperand(1); | ||||
8130 | OtherOpF = FI->getOperand(1); | ||||
8131 | MatchIsOpZero = true; | ||||
8132 | } else if (TI->getOperand(1) == FI->getOperand(1)) { | ||||
8133 | MatchOp = TI->getOperand(1); | ||||
8134 | OtherOpT = TI->getOperand(0); | ||||
8135 | OtherOpF = FI->getOperand(0); | ||||
8136 | MatchIsOpZero = false; | ||||
8137 | } else if (!TI->isCommutative()) { | ||||
8138 | return 0; | ||||
8139 | } else if (TI->getOperand(0) == FI->getOperand(1)) { | ||||
8140 | MatchOp = TI->getOperand(0); | ||||
8141 | OtherOpT = TI->getOperand(1); | ||||
8142 | OtherOpF = FI->getOperand(0); | ||||
8143 | MatchIsOpZero = true; | ||||
8144 | } else if (TI->getOperand(1) == FI->getOperand(0)) { | ||||
8145 | MatchOp = TI->getOperand(1); | ||||
8146 | OtherOpT = TI->getOperand(0); | ||||
8147 | OtherOpF = FI->getOperand(1); | ||||
8148 | MatchIsOpZero = true; | ||||
8149 | } else { | ||||
8150 | return 0; | ||||
8151 | } | ||||
8152 | |||||
8153 | // If we reach here, they do have operations in common. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8154 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
8155 | OtherOpF, SI.getName()+".v"); | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8156 | InsertNewInstBefore(NewSI, SI); |
8157 | |||||
8158 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { | ||||
8159 | if (MatchIsOpZero) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8160 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8161 | else |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8162 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8163 | } |
Reid Spencer | a07cb7d | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 8164 | assert(0 && "Shouldn't get here"); |
8165 | return 0; | ||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8166 | } |
8167 | |||||
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8168 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8169 | Value *CondVal = SI.getCondition(); |
8170 | Value *TrueVal = SI.getTrueValue(); | ||||
8171 | Value *FalseVal = SI.getFalseValue(); | ||||
8172 | |||||
8173 | // select true, X, Y -> X | ||||
8174 | // select false, X, Y -> Y | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8175 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8176 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8177 | |
8178 | // select C, X, X -> X | ||||
8179 | if (TrueVal == FalseVal) | ||||
8180 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8181 | |||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8182 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
8183 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8184 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X | ||||
8185 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8186 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y | ||||
8187 | if (isa<Constant>(TrueVal)) | ||||
8188 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8189 | else | ||||
8190 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8191 | } | ||||
8192 | |||||
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8193 | if (SI.getType() == Type::Int1Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8194 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8195 | if (C->getZExtValue()) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8196 | // Change: A = select B, true, C --> A = or B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8197 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8198 | } else { |
8199 | // Change: A = select B, false, C --> A = and !B, C | ||||
8200 | Value *NotCond = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8201 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8202 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8203 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8204 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8205 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 8206 | if (C->getZExtValue() == false) { |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8207 | // Change: A = select B, C, false --> A = and B, C |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8208 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8209 | } else { |
8210 | // Change: A = select B, C, true --> A = or !B, C | ||||
8211 | Value *NotCond = | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8212 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8213 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8214 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8215 | } |
8216 | } | ||||
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8217 | |
8218 | // select a, b, a -> a&b | ||||
8219 | // select a, a, b -> a|b | ||||
8220 | if (CondVal == TrueVal) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8221 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | cfa5975 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 8222 | else if (CondVal == FalseVal) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8223 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8224 | } |
Chris Lattner | 0c199a7 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 8225 | |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8226 | // Selecting between two integer constants? |
8227 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) | ||||
8228 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { | ||||
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8229 | // select C, 1, 0 -> zext C to int |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8230 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8231 | return CastInst::Create(Instruction::ZExt, CondVal, SI.getType()); |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8232 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8233 | // select C, 0, 1 -> zext !C to int |
Chris Lattner | 2eefe51 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 8234 | Value *NotCond = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8235 | InsertNewInstBefore(BinaryOperator::CreateNot(CondVal, |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8236 | "not."+CondVal->getName()), SI); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8237 | return CastInst::Create(Instruction::ZExt, NotCond, SI.getType()); |
Chris Lattner | 82e14fe | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 8238 | } |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8239 | |
8240 | // FIXME: Turn select 0/-1 and -1/0 into sext from condition! | ||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8241 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8242 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8243 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8244 | // (x <s 0) ? -1 : 0 -> ashr x, 31 |
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8245 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8246 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { |
Chris Lattner | ba41783 | 2007-04-11 06:12:58 +0000 | [diff] [blame] | 8247 | if (IC->getPredicate() == ICmpInst::ICMP_SLT && CmpCst->isZero()) { |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8248 | // The comparison constant and the result are not neccessarily the |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8249 | // same width. Make an all-ones value by inserting a AShr. |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8250 | Value *X = IC->getOperand(0); |
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8251 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8252 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8253 | Instruction *SRA = BinaryOperator::Create(Instruction::AShr, X, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8254 | ShAmt, "ones"); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8255 | InsertNewInstBefore(SRA, SI); |
8256 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8257 | // Finally, convert to the type of the select RHS. We figure out |
8258 | // if this requires a SExt, Trunc or BitCast based on the sizes. | ||||
8259 | Instruction::CastOps opc = Instruction::BitCast; | ||||
Zhou Sheng | 4351c64 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 8260 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); |
8261 | uint32_t SISize = SI.getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8262 | if (SRASize < SISize) |
8263 | opc = Instruction::SExt; | ||||
8264 | else if (SRASize > SISize) | ||||
8265 | opc = Instruction::Trunc; | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8266 | return CastInst::Create(opc, SRA, SI.getType()); |
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8267 | } |
8268 | } | ||||
8269 | |||||
8270 | |||||
8271 | // 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] | 8272 | // 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] | 8273 | // non-constant value, eliminate this whole mess. This corresponds to |
8274 | // cases like this: ((X & 27) ? 27 : 0) | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8275 | if (TrueValC->isZero() || FalseValC->isZero()) |
Chris Lattner | 65b72ba | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 8276 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8277 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
8278 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) | ||||
8279 | if (ICA->getOpcode() == Instruction::And && | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8280 | isa<ConstantInt>(ICA->getOperand(1)) && |
8281 | (ICA->getOperand(1) == TrueValC || | ||||
8282 | ICA->getOperand(1) == FalseValC) && | ||||
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8283 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
8284 | // 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] | 8285 | // know whether we have a icmp_ne or icmp_eq and whether the |
8286 | // true or false val is the zero. | ||||
Reid Spencer | 2ec619a | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 8287 | bool ShouldNotVal = !TrueValC->isZero(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8288 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8289 | Value *V = ICA; |
8290 | if (ShouldNotVal) | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8291 | V = InsertNewInstBefore(BinaryOperator::Create( |
Chris Lattner | 457dd82 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 8292 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
8293 | return ReplaceInstUsesWith(SI, V); | ||||
8294 | } | ||||
Chris Lattner | b845646 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 8295 | } |
Chris Lattner | c32b30a | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 8296 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8297 | |
8298 | // 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] | 8299 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
8300 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { | ||||
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8301 | // Transform (X == Y) ? X : Y -> Y |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8302 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
8303 | // This is not safe in general for floating point: | ||||
8304 | // consider X== -0, Y== +0. | ||||
8305 | // It becomes safe if either operand is a nonzero constant. | ||||
8306 | ConstantFP *CFPt, *CFPf; | ||||
8307 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && | ||||
8308 | !CFPt->getValueAPF().isZero()) || | ||||
8309 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && | ||||
8310 | !CFPf->getValueAPF().isZero())) | ||||
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8311 | return ReplaceInstUsesWith(SI, FalseVal); |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8312 | } |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8313 | // Transform (X != Y) ? X : Y -> X |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8314 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8315 | return ReplaceInstUsesWith(SI, TrueVal); |
8316 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | ||||
8317 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8318 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8319 | // Transform (X == Y) ? Y : X -> X |
Dale Johannesen | 5a2174f | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 8320 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
8321 | // This is not safe in general for floating point: | ||||
8322 | // consider X== -0, Y== +0. | ||||
8323 | // It becomes safe if either operand is a nonzero constant. | ||||
8324 | ConstantFP *CFPt, *CFPf; | ||||
8325 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && | ||||
8326 | !CFPt->getValueAPF().isZero()) || | ||||
8327 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && | ||||
8328 | !CFPf->getValueAPF().isZero())) | ||||
8329 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8330 | } | ||||
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8331 | // Transform (X != Y) ? Y : X -> Y |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8332 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
8333 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8334 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | ||||
8335 | } | ||||
8336 | } | ||||
8337 | |||||
8338 | // See if we are selecting two values based on a comparison of the two values. | ||||
8339 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { | ||||
8340 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { | ||||
8341 | // Transform (X == Y) ? X : Y -> Y | ||||
8342 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) | ||||
8343 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8344 | // Transform (X != Y) ? X : Y -> X | ||||
8345 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | ||||
8346 | return ReplaceInstUsesWith(SI, TrueVal); | ||||
8347 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | ||||
8348 | |||||
8349 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ | ||||
8350 | // Transform (X == Y) ? Y : X -> X | ||||
8351 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) | ||||
8352 | return ReplaceInstUsesWith(SI, FalseVal); | ||||
8353 | // Transform (X != Y) ? Y : X -> Y | ||||
8354 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | ||||
Chris Lattner | fbede52 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 8355 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | d76956d | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 8356 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. |
8357 | } | ||||
8358 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8359 | |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8360 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
8361 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) | ||||
8362 | if (TI->hasOneUse() && FI->hasOneUse()) { | ||||
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8363 | Instruction *AddOp = 0, *SubOp = 0; |
8364 | |||||
Chris Lattner | 6fb5a4a | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 8365 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
8366 | if (TI->getOpcode() == FI->getOpcode()) | ||||
8367 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) | ||||
8368 | return IV; | ||||
8369 | |||||
8370 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is | ||||
8371 | // even legal for FP. | ||||
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8372 | if (TI->getOpcode() == Instruction::Sub && |
8373 | FI->getOpcode() == Instruction::Add) { | ||||
8374 | AddOp = FI; SubOp = TI; | ||||
8375 | } else if (FI->getOpcode() == Instruction::Sub && | ||||
8376 | TI->getOpcode() == Instruction::Add) { | ||||
8377 | AddOp = TI; SubOp = FI; | ||||
8378 | } | ||||
8379 | |||||
8380 | if (AddOp) { | ||||
8381 | Value *OtherAddOp = 0; | ||||
8382 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { | ||||
8383 | OtherAddOp = AddOp->getOperand(1); | ||||
8384 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { | ||||
8385 | OtherAddOp = AddOp->getOperand(0); | ||||
8386 | } | ||||
8387 | |||||
8388 | if (OtherAddOp) { | ||||
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8389 | // So at this point we know we have (Y -> OtherAddOp): |
8390 | // select C, (add X, Y), (sub X, Z) | ||||
8391 | Value *NegVal; // Compute -Z | ||||
8392 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { | ||||
8393 | NegVal = ConstantExpr::getNeg(C); | ||||
8394 | } else { | ||||
8395 | NegVal = InsertNewInstBefore( | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8396 | BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8397 | } |
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8398 | |
8399 | Value *NewTrueOp = OtherAddOp; | ||||
8400 | Value *NewFalseOp = NegVal; | ||||
8401 | if (AddOp != TI) | ||||
8402 | std::swap(NewTrueOp, NewFalseOp); | ||||
8403 | Instruction *NewSel = | ||||
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8404 | SelectInst::Create(CondVal, NewTrueOp, |
8405 | NewFalseOp, SI.getName() + ".p"); | ||||
Chris Lattner | 97f37a4 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 8406 | |
8407 | NewSel = InsertNewInstBefore(NewSel, SI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8408 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | 87875da | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 8409 | } |
8410 | } | ||||
8411 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8412 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8413 | // See if we can fold the select into one of our operands. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 8414 | if (SI.getType()->isInteger()) { |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8415 | // See the comment above GetSelectFoldableOperands for a description of the |
8416 | // transformation we are doing here. | ||||
8417 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) | ||||
8418 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && | ||||
8419 | !isa<Constant>(FalseVal)) | ||||
8420 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { | ||||
8421 | unsigned OpToFold = 0; | ||||
8422 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { | ||||
8423 | OpToFold = 1; | ||||
8424 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { | ||||
8425 | OpToFold = 2; | ||||
8426 | } | ||||
8427 | |||||
8428 | if (OpToFold) { | ||||
8429 | Constant *C = GetSelectFoldableConstant(TVI); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8430 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8431 | SelectInst::Create(SI.getCondition(), |
8432 | TVI->getOperand(2-OpToFold), C); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8433 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8434 | NewSel->takeName(TVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8435 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8436 | return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8437 | else { |
8438 | assert(0 && "Unknown instruction!!"); | ||||
8439 | } | ||||
8440 | } | ||||
8441 | } | ||||
Chris Lattner | a96879a | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 8442 | |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8443 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) |
8444 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && | ||||
8445 | !isa<Constant>(TrueVal)) | ||||
8446 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { | ||||
8447 | unsigned OpToFold = 0; | ||||
8448 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { | ||||
8449 | OpToFold = 1; | ||||
8450 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { | ||||
8451 | OpToFold = 2; | ||||
8452 | } | ||||
8453 | |||||
8454 | if (OpToFold) { | ||||
8455 | Constant *C = GetSelectFoldableConstant(FVI); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8456 | Instruction *NewSel = |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 8457 | SelectInst::Create(SI.getCondition(), C, |
8458 | FVI->getOperand(2-OpToFold)); | ||||
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8459 | InsertNewInstBefore(NewSel, SI); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8460 | NewSel->takeName(FVI); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8461 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8462 | return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 8463 | else |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8464 | assert(0 && "Unknown instruction!!"); |
Chris Lattner | e576b91 | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 8465 | } |
8466 | } | ||||
8467 | } | ||||
Chris Lattner | a1df33c | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 8468 | |
8469 | if (BinaryOperator::isNot(CondVal)) { | ||||
8470 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); | ||||
8471 | SI.setOperand(1, FalseVal); | ||||
8472 | SI.setOperand(2, TrueVal); | ||||
8473 | return &SI; | ||||
8474 | } | ||||
8475 | |||||
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8476 | return 0; |
8477 | } | ||||
8478 | |||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8479 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
8480 | /// we control, modify the object's alignment to PrefAlign. This isn't | ||||
8481 | /// often possible though. If alignment is important, a more reliable approach | ||||
8482 | /// is to simply align all global variables and allocation instructions to | ||||
8483 | /// their preferred alignment from the beginning. | ||||
8484 | /// | ||||
8485 | static unsigned EnforceKnownAlignment(Value *V, | ||||
8486 | unsigned Align, unsigned PrefAlign) { | ||||
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8487 | |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8488 | User *U = dyn_cast<User>(V); |
8489 | if (!U) return Align; | ||||
8490 | |||||
8491 | switch (getOpcode(U)) { | ||||
8492 | default: break; | ||||
8493 | case Instruction::BitCast: | ||||
8494 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); | ||||
8495 | case Instruction::GetElementPtr: { | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8496 | // If all indexes are zero, it is just the alignment of the base pointer. |
8497 | bool AllZeroOperands = true; | ||||
Gabor Greif | 52ed363 | 2008-06-12 21:51:29 +0000 | [diff] [blame] | 8498 | for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 8499 | if (!isa<Constant>(*i) || |
8500 | !cast<Constant>(*i)->isNullValue()) { | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8501 | AllZeroOperands = false; |
8502 | break; | ||||
8503 | } | ||||
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8504 | |
8505 | if (AllZeroOperands) { | ||||
8506 | // Treat this like a bitcast. | ||||
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8507 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
Chris Lattner | f2369f2 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 8508 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8509 | break; |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8510 | } |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8511 | } |
8512 | |||||
8513 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { | ||||
8514 | // If there is a large requested alignment and we can, bump up the alignment | ||||
8515 | // of the global. | ||||
8516 | if (!GV->isDeclaration()) { | ||||
8517 | GV->setAlignment(PrefAlign); | ||||
8518 | Align = PrefAlign; | ||||
8519 | } | ||||
8520 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { | ||||
8521 | // If there is a requested alignment and if this is an alloca, round up. We | ||||
8522 | // don't do this for malloc, because some systems can't respect the request. | ||||
8523 | if (isa<AllocaInst>(AI)) { | ||||
8524 | AI->setAlignment(PrefAlign); | ||||
8525 | Align = PrefAlign; | ||||
8526 | } | ||||
8527 | } | ||||
8528 | |||||
8529 | return Align; | ||||
8530 | } | ||||
8531 | |||||
8532 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that | ||||
8533 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, | ||||
8534 | /// and it is more than the alignment of the ultimate object, see if we can | ||||
8535 | /// increase the alignment of the ultimate object, making this check succeed. | ||||
8536 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, | ||||
8537 | unsigned PrefAlign) { | ||||
8538 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : | ||||
8539 | sizeof(PrefAlign) * CHAR_BIT; | ||||
8540 | APInt Mask = APInt::getAllOnesValue(BitWidth); | ||||
8541 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | ||||
8542 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); | ||||
8543 | unsigned TrailZ = KnownZero.countTrailingOnes(); | ||||
8544 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); | ||||
8545 | |||||
8546 | if (PrefAlign > Align) | ||||
8547 | Align = EnforceKnownAlignment(V, Align, PrefAlign); | ||||
8548 | |||||
8549 | // We don't need to make any adjustment. | ||||
8550 | return Align; | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8551 | } |
8552 | |||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8553 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 8554 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
8555 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8556 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
8557 | unsigned CopyAlign = MI->getAlignment()->getZExtValue(); | ||||
8558 | |||||
8559 | if (CopyAlign < MinAlign) { | ||||
8560 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign)); | ||||
8561 | return MI; | ||||
8562 | } | ||||
8563 | |||||
8564 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with | ||||
8565 | // load/store. | ||||
8566 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); | ||||
8567 | if (MemOpLength == 0) return 0; | ||||
8568 | |||||
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8569 | // Source and destination pointer types are always "i8*" for intrinsic. See |
8570 | // if the size is something we can handle with a single primitive load/store. | ||||
8571 | // A single load+store correctly handles overlapping memory in the memmove | ||||
8572 | // case. | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8573 | unsigned Size = MemOpLength->getZExtValue(); |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8574 | if (Size == 0) return MI; // Delete this mem transfer. |
8575 | |||||
8576 | if (Size > 8 || (Size&(Size-1))) | ||||
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8577 | return 0; // If not 1/2/4/8 bytes, exit. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8578 | |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8579 | // Use an integer load+store unless we can find something better. |
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8580 | Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3)); |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8581 | |
8582 | // Memcpy forces the use of i8* for the source and destination. That means | ||||
8583 | // that if you're using memcpy to move one double around, you'll get a cast | ||||
8584 | // from double* to i8*. We'd much rather use a double load+store rather than | ||||
8585 | // an i64 load+store, here because this improves the odds that the source or | ||||
8586 | // dest address will be promotable. See if we can find a better type than the | ||||
8587 | // integer datatype. | ||||
8588 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { | ||||
8589 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); | ||||
8590 | if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { | ||||
8591 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip | ||||
8592 | // down through these levels if so. | ||||
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 8593 | while (!SrcETy->isSingleValueType()) { |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8594 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
8595 | if (STy->getNumElements() == 1) | ||||
8596 | SrcETy = STy->getElementType(0); | ||||
8597 | else | ||||
8598 | break; | ||||
8599 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { | ||||
8600 | if (ATy->getNumElements() == 1) | ||||
8601 | SrcETy = ATy->getElementType(); | ||||
8602 | else | ||||
8603 | break; | ||||
8604 | } else | ||||
8605 | break; | ||||
8606 | } | ||||
8607 | |||||
Dan Gohman | 8f8e269 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 8608 | if (SrcETy->isSingleValueType()) |
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8609 | NewPtrTy = PointerType::getUnqual(SrcETy); |
8610 | } | ||||
8611 | } | ||||
8612 | |||||
8613 | |||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8614 | // If the memcpy/memmove provides better alignment info than we can |
8615 | // infer, use it. | ||||
8616 | SrcAlign = std::max(SrcAlign, CopyAlign); | ||||
8617 | DstAlign = std::max(DstAlign, CopyAlign); | ||||
8618 | |||||
8619 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); | ||||
8620 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); | ||||
Chris Lattner | 37ac608 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 8621 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
8622 | InsertNewInstBefore(L, *MI); | ||||
8623 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); | ||||
8624 | |||||
8625 | // Set the size of the copy to 0, it will be deleted on the next iteration. | ||||
8626 | MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); | ||||
8627 | return MI; | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8628 | } |
Chris Lattner | 3d69f46 | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 8629 | |
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8630 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
8631 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); | ||||
8632 | if (MI->getAlignment()->getZExtValue() < Alignment) { | ||||
8633 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); | ||||
8634 | return MI; | ||||
8635 | } | ||||
8636 | |||||
8637 | // Extract the length and alignment and fill if they are constant. | ||||
8638 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); | ||||
8639 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); | ||||
8640 | if (!LenC || !FillC || FillC->getType() != Type::Int8Ty) | ||||
8641 | return 0; | ||||
8642 | uint64_t Len = LenC->getZExtValue(); | ||||
8643 | Alignment = MI->getAlignment()->getZExtValue(); | ||||
8644 | |||||
8645 | // If the length is zero, this is a no-op | ||||
8646 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop | ||||
8647 | |||||
8648 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) | ||||
8649 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { | ||||
8650 | const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8. | ||||
8651 | |||||
8652 | Value *Dest = MI->getDest(); | ||||
8653 | Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI); | ||||
8654 | |||||
8655 | // Alignment 0 is identity for alignment 1 for memset, but not store. | ||||
8656 | if (Alignment == 0) Alignment = 1; | ||||
8657 | |||||
8658 | // Extract the fill value and store. | ||||
8659 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; | ||||
8660 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false, | ||||
8661 | Alignment), *MI); | ||||
8662 | |||||
8663 | // Set the size of the copy to 0, it will be deleted on the next iteration. | ||||
8664 | MI->setLength(Constant::getNullValue(LenC->getType())); | ||||
8665 | return MI; | ||||
8666 | } | ||||
8667 | |||||
8668 | return 0; | ||||
8669 | } | ||||
8670 | |||||
8671 | |||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8672 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
8673 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do | ||||
8674 | /// the heavy lifting. | ||||
8675 | /// | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8676 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8677 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
8678 | if (!II) return visitCallSite(&CI); | ||||
8679 | |||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8680 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
8681 | // visitCallSite. | ||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8682 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8683 | bool Changed = false; |
8684 | |||||
8685 | // memmove/cpy/set of zero bytes is a noop. | ||||
8686 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { | ||||
8687 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); | ||||
8688 | |||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8689 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8690 | if (CI->getZExtValue() == 1) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8691 | // Replace the instruction with just byte operations. We would |
8692 | // transform other cases to loads/stores, but we don't know if | ||||
8693 | // alignment is sufficient. | ||||
8694 | } | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8695 | } |
8696 | |||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8697 | // If we have a memmove and the source operation is a constant global, |
8698 | // then the source and dest pointers can't alias, so we can change this | ||||
8699 | // into a call to memcpy. | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8700 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8701 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
8702 | if (GVSrc->isConstant()) { | ||||
8703 | Module *M = CI.getParent()->getParent()->getParent(); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8704 | Intrinsic::ID MemCpyID; |
8705 | if (CI.getOperand(3)->getType() == Type::Int32Ty) | ||||
8706 | MemCpyID = Intrinsic::memcpy_i32; | ||||
Chris Lattner | 2195939 | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 8707 | else |
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 8708 | MemCpyID = Intrinsic::memcpy_i64; |
8709 | CI.setOperand(0, Intrinsic::getDeclaration(M, MemCpyID)); | ||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8710 | Changed = true; |
8711 | } | ||||
Chris Lattner | a935db8 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 8712 | |
8713 | // memmove(x,x,size) -> noop. | ||||
8714 | if (MMI->getSource() == MMI->getDest()) | ||||
8715 | return EraseInstFromFunction(CI); | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8716 | } |
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8717 | |
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8718 | // If we can determine a pointer alignment that is bigger than currently |
8719 | // set, update the alignment. | ||||
8720 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { | ||||
Chris Lattner | f497b02 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 8721 | if (Instruction *I = SimplifyMemTransfer(MI)) |
8722 | return I; | ||||
Chris Lattner | 69ea9d2 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 8723 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
8724 | if (Instruction *I = SimplifyMemSet(MSI)) | ||||
8725 | return I; | ||||
Chris Lattner | 95a959d | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 8726 | } |
8727 | |||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8728 | if (Changed) return II; |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8729 | } |
8730 | |||||
8731 | switch (II->getIntrinsicID()) { | ||||
8732 | default: break; | ||||
8733 | case Intrinsic::bswap: | ||||
8734 | // bswap(bswap(x)) -> x | ||||
8735 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) | ||||
8736 | if (Operand->getIntrinsicID() == Intrinsic::bswap) | ||||
8737 | return ReplaceInstUsesWith(CI, Operand->getOperand(1)); | ||||
8738 | break; | ||||
8739 | case Intrinsic::ppc_altivec_lvx: | ||||
8740 | case Intrinsic::ppc_altivec_lvxl: | ||||
8741 | case Intrinsic::x86_sse_loadu_ps: | ||||
8742 | case Intrinsic::x86_sse2_loadu_pd: | ||||
8743 | case Intrinsic::x86_sse2_loadu_dq: | ||||
8744 | // Turn PPC lvx -> load if the pointer is known aligned. | ||||
8745 | // Turn X86 loadups -> load if the pointer is known aligned. | ||||
8746 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { | ||||
8747 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), | ||||
8748 | PointerType::getUnqual(II->getType()), | ||||
8749 | CI); | ||||
8750 | return new LoadInst(Ptr); | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8751 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8752 | break; |
8753 | case Intrinsic::ppc_altivec_stvx: | ||||
8754 | case Intrinsic::ppc_altivec_stvxl: | ||||
8755 | // Turn stvx -> store if the pointer is known aligned. | ||||
8756 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { | ||||
8757 | const Type *OpPtrTy = | ||||
8758 | PointerType::getUnqual(II->getOperand(1)->getType()); | ||||
8759 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); | ||||
8760 | return new StoreInst(II->getOperand(1), Ptr); | ||||
8761 | } | ||||
8762 | break; | ||||
8763 | case Intrinsic::x86_sse_storeu_ps: | ||||
8764 | case Intrinsic::x86_sse2_storeu_pd: | ||||
8765 | case Intrinsic::x86_sse2_storeu_dq: | ||||
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8766 | // Turn X86 storeu -> store if the pointer is known aligned. |
8767 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { | ||||
8768 | const Type *OpPtrTy = | ||||
8769 | PointerType::getUnqual(II->getOperand(2)->getType()); | ||||
8770 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); | ||||
8771 | return new StoreInst(II->getOperand(2), Ptr); | ||||
8772 | } | ||||
8773 | break; | ||||
8774 | |||||
8775 | case Intrinsic::x86_sse_cvttss2si: { | ||||
8776 | // These intrinsics only demands the 0th element of its input vector. If | ||||
8777 | // we can simplify the input based on that, do so now. | ||||
8778 | uint64_t UndefElts; | ||||
8779 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, | ||||
8780 | UndefElts)) { | ||||
8781 | II->setOperand(1, V); | ||||
8782 | return II; | ||||
8783 | } | ||||
8784 | break; | ||||
8785 | } | ||||
8786 | |||||
8787 | case Intrinsic::ppc_altivec_vperm: | ||||
8788 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. | ||||
8789 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { | ||||
8790 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); | ||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8791 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8792 | // Check that all of the elements are integer constants or undefs. |
8793 | bool AllEltsOk = true; | ||||
8794 | for (unsigned i = 0; i != 16; ++i) { | ||||
8795 | if (!isa<ConstantInt>(Mask->getOperand(i)) && | ||||
8796 | !isa<UndefValue>(Mask->getOperand(i))) { | ||||
8797 | AllEltsOk = false; | ||||
8798 | break; | ||||
8799 | } | ||||
8800 | } | ||||
8801 | |||||
8802 | if (AllEltsOk) { | ||||
8803 | // Cast the input vectors to byte vectors. | ||||
8804 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); | ||||
8805 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); | ||||
8806 | Value *Result = UndefValue::get(Op0->getType()); | ||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8807 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8808 | // Only extract each element once. |
8809 | Value *ExtractedElts[32]; | ||||
8810 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); | ||||
8811 | |||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8812 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8813 | if (isa<UndefValue>(Mask->getOperand(i))) |
8814 | continue; | ||||
8815 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); | ||||
8816 | Idx &= 31; // Match the hardware behavior. | ||||
8817 | |||||
8818 | if (ExtractedElts[Idx] == 0) { | ||||
8819 | Instruction *Elt = | ||||
8820 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); | ||||
8821 | InsertNewInstBefore(Elt, CI); | ||||
8822 | ExtractedElts[Idx] = Elt; | ||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8823 | } |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8824 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8825 | // Insert this value into the result vector. |
8826 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], | ||||
8827 | i, "tmp"); | ||||
8828 | InsertNewInstBefore(cast<Instruction>(Result), CI); | ||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8829 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8830 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8831 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8832 | } |
8833 | break; | ||||
Chris Lattner | e2ed057 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 8834 | |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8835 | case Intrinsic::stackrestore: { |
8836 | // If the save is right next to the restore, remove the restore. This can | ||||
8837 | // happen when variable allocas are DCE'd. | ||||
8838 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { | ||||
8839 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { | ||||
8840 | BasicBlock::iterator BI = SS; | ||||
8841 | if (&*++BI == II) | ||||
8842 | return EraseInstFromFunction(CI); | ||||
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8843 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8844 | } |
8845 | |||||
8846 | // Scan down this block to see if there is another stack restore in the | ||||
8847 | // same block without an intervening call/alloca. | ||||
8848 | BasicBlock::iterator BI = II; | ||||
8849 | TerminatorInst *TI = II->getParent()->getTerminator(); | ||||
8850 | bool CannotRemove = false; | ||||
8851 | for (++BI; &*BI != TI; ++BI) { | ||||
8852 | if (isa<AllocaInst>(BI)) { | ||||
8853 | CannotRemove = true; | ||||
8854 | break; | ||||
8855 | } | ||||
Chris Lattner | aa0bf52 | 2008-06-25 05:59:28 +0000 | [diff] [blame] | 8856 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
8857 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { | ||||
8858 | // If there is a stackrestore below this one, remove this one. | ||||
8859 | if (II->getIntrinsicID() == Intrinsic::stackrestore) | ||||
8860 | return EraseInstFromFunction(CI); | ||||
8861 | // Otherwise, ignore the intrinsic. | ||||
8862 | } else { | ||||
8863 | // If we found a non-intrinsic call, we can't remove the stack | ||||
8864 | // restore. | ||||
Chris Lattner | bf1d8a7 | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 8865 | CannotRemove = true; |
8866 | break; | ||||
8867 | } | ||||
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8868 | } |
Chris Lattner | a728ddc | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 8869 | } |
Chris Lattner | 0521e3c | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 8870 | |
8871 | // If the stack restore is in a return/unwind block and if there are no | ||||
8872 | // allocas or calls between the restore and the return, nuke the restore. | ||||
8873 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) | ||||
8874 | return EraseInstFromFunction(CI); | ||||
8875 | break; | ||||
8876 | } | ||||
Chris Lattner | 35b9e48 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 8877 | } |
8878 | |||||
Chris Lattner | 8b0ea31 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 8879 | return visitCallSite(II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8880 | } |
8881 | |||||
8882 | // InvokeInst simplification | ||||
8883 | // | ||||
8884 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { | ||||
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8885 | return visitCallSite(&II); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8886 | } |
8887 | |||||
Dale Johannesen | da30ccb | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 8888 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
8889 | /// 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] | 8890 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
8891 | const CastInst * const CI, | ||||
8892 | const TargetData * const TD, | ||||
8893 | const int ix) { | ||||
8894 | if (!CI->isLosslessCast()) | ||||
8895 | return false; | ||||
8896 | |||||
8897 | // The size of ByVal arguments is derived from the type, so we | ||||
8898 | // can't change to a type with a different size. If the size were | ||||
8899 | // passed explicitly we could avoid this check. | ||||
8900 | if (!CS.paramHasAttr(ix, ParamAttr::ByVal)) | ||||
8901 | return true; | ||||
8902 | |||||
8903 | const Type* SrcTy = | ||||
8904 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); | ||||
8905 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); | ||||
8906 | if (!SrcTy->isSized() || !DstTy->isSized()) | ||||
8907 | return false; | ||||
8908 | if (TD->getABITypeSize(SrcTy) != TD->getABITypeSize(DstTy)) | ||||
8909 | return false; | ||||
8910 | return true; | ||||
8911 | } | ||||
8912 | |||||
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8913 | // visitCallSite - Improvements for call and invoke instructions. |
8914 | // | ||||
8915 | Instruction *InstCombiner::visitCallSite(CallSite CS) { | ||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8916 | bool Changed = false; |
8917 | |||||
8918 | // If the callee is a constexpr cast of a function, attempt to move the cast | ||||
8919 | // to the arguments of the call/invoke. | ||||
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8920 | if (transformConstExprCastCall(CS)) return 0; |
8921 | |||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8922 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8923 | |
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8924 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
8925 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { | ||||
8926 | Instruction *OldCall = CS.getInstruction(); | ||||
8927 | // If the call and callee calling conventions don't match, this call must | ||||
8928 | // be unreachable, as the call is undefined. | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8929 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8930 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
8931 | OldCall); | ||||
Chris Lattner | 08b22ec | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 8932 | if (!OldCall->use_empty()) |
8933 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); | ||||
8934 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. | ||||
8935 | return EraseInstFromFunction(*OldCall); | ||||
8936 | return 0; | ||||
8937 | } | ||||
8938 | |||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8939 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
8940 | // This instruction is not reachable, just remove it. We insert a store to | ||||
8941 | // undef so that we know that this code is not reachable, despite the fact | ||||
8942 | // that we can't modify the CFG here. | ||||
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8943 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 8944 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8945 | CS.getInstruction()); |
8946 | |||||
8947 | if (!CS.getInstruction()->use_empty()) | ||||
8948 | CS.getInstruction()-> | ||||
8949 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); | ||||
8950 | |||||
8951 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { | ||||
8952 | // Don't break the CFG, insert a dummy cond branch. | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8953 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
8954 | ConstantInt::getTrue(), II); | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8955 | } |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8956 | return EraseInstFromFunction(*CS.getInstruction()); |
8957 | } | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8958 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 8959 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
8960 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) | ||||
8961 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) | ||||
8962 | return transformCallThroughTrampoline(CS); | ||||
8963 | |||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8964 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
8965 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); | ||||
8966 | if (FTy->isVarArg()) { | ||||
Dale Johannesen | 63e7eb4 | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 8967 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8968 | // See if we can optimize any arguments passed through the varargs area of |
8969 | // the call. | ||||
8970 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), | ||||
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8971 | E = CS.arg_end(); I != E; ++I, ++ix) { |
8972 | CastInst *CI = dyn_cast<CastInst>(*I); | ||||
8973 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { | ||||
8974 | *I = CI->getOperand(0); | ||||
8975 | Changed = true; | ||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8976 | } |
Dale Johannesen | 1f530a5 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 8977 | } |
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8978 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8979 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8980 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8981 | // Inline asm calls cannot throw - mark them 'nounwind'. |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 8982 | CS.setDoesNotThrow(); |
Duncan Sands | ece2c04 | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 8983 | Changed = true; |
8984 | } | ||||
8985 | |||||
Chris Lattner | 6c266db | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 8986 | return Changed ? CS.getInstruction() : 0; |
Chris Lattner | a44d8a2 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 8987 | } |
8988 | |||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8989 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
8990 | // attempt to move the cast to the arguments of the call/invoke. | ||||
8991 | // | ||||
8992 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { | ||||
8993 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; | ||||
8994 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8995 | if (CE->getOpcode() != Instruction::BitCast || |
8996 | !isa<Function>(CE->getOperand(0))) | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8997 | return false; |
Reid Spencer | 8863f18 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 8998 | Function *Callee = cast<Function>(CE->getOperand(0)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 8999 | Instruction *Caller = CS.getInstruction(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9000 | const PAListPtr &CallerPAL = CS.getParamAttrs(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9001 | |
9002 | // Okay, this is a cast from a function to a different type. Unless doing so | ||||
9003 | // would cause a type conversion of one of our arguments, change this call to | ||||
9004 | // be a direct call with arguments casted to the appropriate types. | ||||
9005 | // | ||||
9006 | const FunctionType *FT = Callee->getFunctionType(); | ||||
9007 | const Type *OldRetTy = Caller->getType(); | ||||
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9008 | const Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9009 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9010 | if (isa<StructType>(NewRetTy)) |
Devang Patel | 75e6f02 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 9011 | return false; // TODO: Handle multiple return values. |
9012 | |||||
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9013 | // Check to see if we are changing the return type... |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9014 | if (OldRetTy != NewRetTy) { |
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 9015 | if (Callee->isDeclaration() && |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9016 | // Conversion is ok if changing from one pointer type to another or from |
9017 | // a pointer to an integer of the same size. | ||||
9018 | !((isa<PointerType>(OldRetTy) || OldRetTy == TD->getIntPtrType()) && | ||||
Duncan Sands | 34b176a | 2008-06-17 15:55:30 +0000 | [diff] [blame] | 9019 | (isa<PointerType>(NewRetTy) || NewRetTy == TD->getIntPtrType()))) |
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 9020 | return false; // Cannot transform this return value. |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9021 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9022 | if (!Caller->use_empty() && |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9023 | // void -> non-void is handled specially |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9024 | NewRetTy != Type::VoidTy && !CastInst::isCastable(NewRetTy, OldRetTy)) |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9025 | return false; // Cannot transform this return value. |
9026 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9027 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
9028 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); | ||||
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9029 | if (RAttrs & ParamAttr::typeIncompatible(NewRetTy)) |
Duncan Sands | 6c3470e | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 9030 | return false; // Attribute not compatible with transformed value. |
9031 | } | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9032 | |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9033 | // If the callsite is an invoke instruction, and the return value is used by |
9034 | // a PHI node in a successor, we cannot change the return type of the call | ||||
9035 | // because there is no place to put the cast instruction (without breaking | ||||
9036 | // the critical edge). Bail out in this case. | ||||
9037 | if (!Caller->use_empty()) | ||||
9038 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) | ||||
9039 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); | ||||
9040 | UI != E; ++UI) | ||||
9041 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) | ||||
9042 | if (PN->getParent() == II->getNormalDest() || | ||||
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 9043 | PN->getParent() == II->getUnwindDest()) |
Chris Lattner | f78616b | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 9044 | return false; |
9045 | } | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9046 | |
9047 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); | ||||
9048 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9049 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9050 | CallSite::arg_iterator AI = CS.arg_begin(); |
9051 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { | ||||
9052 | const Type *ParamTy = FT->getParamType(i); | ||||
Andrew Lenharth | b8e604c | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 9053 | const Type *ActTy = (*AI)->getType(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9054 | |
9055 | if (!CastInst::isCastable(ActTy, ParamTy)) | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9056 | return false; // Cannot transform this parameter value. |
9057 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9058 | if (CallerPAL.getParamAttrs(i + 1) & ParamAttr::typeIncompatible(ParamTy)) |
9059 | return false; // Attribute not compatible with transformed value. | ||||
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9060 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9061 | // Converting from one pointer type to another or between a pointer and an |
9062 | // integer of the same size is safe even if we do not have a body. | ||||
Chris Lattner | ec47992 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 9063 | bool isConvertible = ActTy == ParamTy || |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9064 | ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) && |
9065 | (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType())); | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9066 | if (Callee->isDeclaration() && !isConvertible) return false; |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9067 | } |
9068 | |||||
9069 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 9070 | Callee->isDeclaration()) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9071 | return false; // Do not delete arguments unless we have a function body. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9072 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9073 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
9074 | !CallerPAL.isEmpty()) | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9075 | // 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] | 9076 | // won't be dropping them. Check that these extra arguments have attributes |
9077 | // that are compatible with being a vararg call argument. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9078 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
9079 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) | ||||
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9080 | break; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9081 | ParameterAttributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9082 | if (PAttrs & ParamAttr::VarArgsIncompatible) |
9083 | return false; | ||||
9084 | } | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9085 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9086 | // Okay, we decided that this is a safe thing to do: go ahead and start |
9087 | // inserting cast instructions as necessary... | ||||
9088 | std::vector<Value*> Args; | ||||
9089 | Args.reserve(NumActualArgs); | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9090 | SmallVector<ParamAttrsWithIndex, 8> attrVec; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9091 | attrVec.reserve(NumCommonArgs); |
9092 | |||||
9093 | // Get any return attributes. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9094 | ParameterAttributes RAttrs = CallerPAL.getParamAttrs(0); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9095 | |
9096 | // If the return value is not being used, the type may not be compatible | ||||
9097 | // with the existing attributes. Wipe out any problematic attributes. | ||||
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9098 | RAttrs &= ~ParamAttr::typeIncompatible(NewRetTy); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9099 | |
9100 | // Add the new return attributes. | ||||
9101 | if (RAttrs) | ||||
9102 | attrVec.push_back(ParamAttrsWithIndex::get(0, RAttrs)); | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9103 | |
9104 | AI = CS.arg_begin(); | ||||
9105 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { | ||||
9106 | const Type *ParamTy = FT->getParamType(i); | ||||
9107 | if ((*AI)->getType() == ParamTy) { | ||||
9108 | Args.push_back(*AI); | ||||
9109 | } else { | ||||
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 9110 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9111 | false, ParamTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9112 | CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9113 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9114 | } |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9115 | |
9116 | // Add any parameter attributes. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9117 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9118 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9119 | } |
9120 | |||||
9121 | // If the function takes more arguments than the call was taking, add them | ||||
9122 | // now... | ||||
9123 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) | ||||
9124 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); | ||||
9125 | |||||
9126 | // If we are removing arguments to the function, emit an obnoxious warning... | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9127 | if (FT->getNumParams() < NumActualArgs) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9128 | if (!FT->isVarArg()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 9129 | cerr << "WARNING: While resolving call to function '" |
9130 | << Callee->getName() << "' arguments were dropped!\n"; | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9131 | } else { |
9132 | // Add all of the arguments in their promoted form to the arg list... | ||||
9133 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { | ||||
9134 | const Type *PTy = getPromotedType((*AI)->getType()); | ||||
9135 | if (PTy != (*AI)->getType()) { | ||||
9136 | // Must promote to pass through va_arg area! | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9137 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
9138 | PTy, false); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9139 | Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp"); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9140 | InsertNewInstBefore(Cast, *Caller); |
9141 | Args.push_back(Cast); | ||||
9142 | } else { | ||||
9143 | Args.push_back(*AI); | ||||
9144 | } | ||||
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9145 | |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9146 | // Add any parameter attributes. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9147 | if (ParameterAttributes PAttrs = CallerPAL.getParamAttrs(i + 1)) |
Duncan Sands | e1e520f | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 9148 | attrVec.push_back(ParamAttrsWithIndex::get(i + 1, PAttrs)); |
9149 | } | ||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9150 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9151 | } |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9152 | |
Duncan Sands | f413cdf | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 9153 | if (NewRetTy == Type::VoidTy) |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9154 | Caller->setName(""); // Void type should not have a name. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9155 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9156 | const PAListPtr &NewCallerPAL = PAListPtr::get(attrVec.begin(),attrVec.end()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9157 | |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9158 | Instruction *NC; |
9159 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9160 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9161 | Args.begin(), Args.end(), |
9162 | Caller->getName(), Caller); | ||||
Reid Spencer | ed3fa85 | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 9163 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9164 | cast<InvokeInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9165 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9166 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
9167 | Caller->getName(), Caller); | ||||
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9168 | CallInst *CI = cast<CallInst>(Caller); |
9169 | if (CI->isTailCall()) | ||||
Chris Lattner | a9e9211 | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 9170 | cast<CallInst>(NC)->setTailCall(); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9171 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 9172 | cast<CallInst>(NC)->setParamAttrs(NewCallerPAL); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9173 | } |
9174 | |||||
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9175 | // Insert a cast of the return type as necessary. |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9176 | Value *NV = NC; |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9177 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9178 | if (NV->getType() != Type::VoidTy) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9179 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 9180 | OldRetTy, false); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9181 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9182 | |
9183 | // If this is an invoke instruction, we should insert it after the first | ||||
9184 | // non-phi, instruction in the normal successor block. | ||||
9185 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | ||||
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 9186 | BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); |
Chris Lattner | bb60904 | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 9187 | InsertNewInstBefore(NC, *I); |
9188 | } else { | ||||
9189 | // Otherwise, it's a call, just insert cast right after the call instr | ||||
9190 | InsertNewInstBefore(NC, *Caller); | ||||
9191 | } | ||||
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9192 | AddUsersToWorkList(*Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9193 | } else { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 9194 | NV = UndefValue::get(Caller->getType()); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9195 | } |
9196 | } | ||||
9197 | |||||
9198 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) | ||||
9199 | Caller->replaceAllUsesWith(NV); | ||||
Chris Lattner | f22a5c6 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 9200 | Caller->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9201 | RemoveFromWorkList(Caller); |
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9202 | return true; |
9203 | } | ||||
9204 | |||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9205 | // transformCallThroughTrampoline - Turn a call to a function created by the |
9206 | // init_trampoline intrinsic into a direct call to the underlying function. | ||||
9207 | // | ||||
9208 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { | ||||
9209 | Value *Callee = CS.getCalledValue(); | ||||
9210 | const PointerType *PTy = cast<PointerType>(Callee->getType()); | ||||
9211 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9212 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9213 | |
9214 | // If the call already has the 'nest' attribute somewhere then give up - | ||||
9215 | // otherwise 'nest' would occur twice after splicing in the chain. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9216 | if (Attrs.hasAttrSomewhere(ParamAttr::Nest)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9217 | return 0; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9218 | |
9219 | IntrinsicInst *Tramp = | ||||
9220 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); | ||||
9221 | |||||
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 9222 | Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9223 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
9224 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); | ||||
9225 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9226 | const PAListPtr &NestAttrs = NestF->getParamAttrs(); |
9227 | if (!NestAttrs.isEmpty()) { | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9228 | unsigned NestIdx = 1; |
9229 | const Type *NestTy = 0; | ||||
Dale Johannesen | 0d51e7e | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 9230 | ParameterAttributes NestAttr = ParamAttr::None; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9231 | |
9232 | // Look for a parameter marked with the 'nest' attribute. | ||||
9233 | for (FunctionType::param_iterator I = NestFTy->param_begin(), | ||||
9234 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9235 | if (NestAttrs.paramHasAttr(NestIdx, ParamAttr::Nest)) { |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9236 | // Record the parameter type and any other attributes. |
9237 | NestTy = *I; | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9238 | NestAttr = NestAttrs.getParamAttrs(NestIdx); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9239 | break; |
9240 | } | ||||
9241 | |||||
9242 | if (NestTy) { | ||||
9243 | Instruction *Caller = CS.getInstruction(); | ||||
9244 | std::vector<Value*> NewArgs; | ||||
9245 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); | ||||
9246 | |||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9247 | SmallVector<ParamAttrsWithIndex, 8> NewAttrs; |
9248 | NewAttrs.reserve(Attrs.getNumSlots() + 1); | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9249 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9250 | // Insert the nest argument into the call argument list, which may |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9251 | // mean appending it. Likewise for attributes. |
9252 | |||||
9253 | // Add any function result attributes. | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9254 | if (ParameterAttributes Attr = Attrs.getParamAttrs(0)) |
9255 | NewAttrs.push_back(ParamAttrsWithIndex::get(0, Attr)); | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9256 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9257 | { |
9258 | unsigned Idx = 1; | ||||
9259 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); | ||||
9260 | do { | ||||
9261 | if (Idx == NestIdx) { | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9262 | // Add the chain argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9263 | Value *NestVal = Tramp->getOperand(3); |
9264 | if (NestVal->getType() != NestTy) | ||||
9265 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); | ||||
9266 | NewArgs.push_back(NestVal); | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9267 | NewAttrs.push_back(ParamAttrsWithIndex::get(NestIdx, NestAttr)); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9268 | } |
9269 | |||||
9270 | if (I == E) | ||||
9271 | break; | ||||
9272 | |||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9273 | // Add the original argument and attributes. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9274 | NewArgs.push_back(*I); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9275 | if (ParameterAttributes Attr = Attrs.getParamAttrs(Idx)) |
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9276 | NewAttrs.push_back |
9277 | (ParamAttrsWithIndex::get(Idx + (Idx >= NestIdx), Attr)); | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9278 | |
9279 | ++Idx, ++I; | ||||
9280 | } while (1); | ||||
9281 | } | ||||
9282 | |||||
9283 | // The trampoline may have been bitcast to a bogus type (FTy). | ||||
9284 | // Handle this by synthesizing a new function type, equal to FTy | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9285 | // with the chain parameter inserted. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9286 | |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9287 | std::vector<const Type*> NewTypes; |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9288 | NewTypes.reserve(FTy->getNumParams()+1); |
9289 | |||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9290 | // 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] | 9291 | // mean appending it. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9292 | { |
9293 | unsigned Idx = 1; | ||||
9294 | FunctionType::param_iterator I = FTy->param_begin(), | ||||
9295 | E = FTy->param_end(); | ||||
9296 | |||||
9297 | do { | ||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9298 | if (Idx == NestIdx) |
9299 | // Add the chain's type. | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9300 | NewTypes.push_back(NestTy); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9301 | |
9302 | if (I == E) | ||||
9303 | break; | ||||
9304 | |||||
Duncan Sands | b0c9b93 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 9305 | // Add the original type. |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9306 | NewTypes.push_back(*I); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9307 | |
9308 | ++Idx, ++I; | ||||
9309 | } while (1); | ||||
9310 | } | ||||
9311 | |||||
9312 | // Replace the trampoline call with a direct call. Let the generic | ||||
9313 | // code sort out any function type mismatches. | ||||
9314 | FunctionType *NewFTy = | ||||
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9315 | FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 9316 | Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ? |
9317 | NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy)); | ||||
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 9318 | const PAListPtr &NewPAL = PAListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9319 | |
9320 | Instruction *NewCaller; | ||||
9321 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9322 | NewCaller = InvokeInst::Create(NewCallee, |
9323 | II->getNormalDest(), II->getUnwindDest(), | ||||
9324 | NewArgs.begin(), NewArgs.end(), | ||||
9325 | Caller->getName(), Caller); | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9326 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9327 | cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9328 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9329 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
9330 | Caller->getName(), Caller); | ||||
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9331 | if (cast<CallInst>(Caller)->isTailCall()) |
9332 | cast<CallInst>(NewCaller)->setTailCall(); | ||||
9333 | cast<CallInst>(NewCaller)-> | ||||
9334 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); | ||||
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 9335 | cast<CallInst>(NewCaller)->setParamAttrs(NewPAL); |
Duncan Sands | cdb6d92 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 9336 | } |
9337 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) | ||||
9338 | Caller->replaceAllUsesWith(NewCaller); | ||||
9339 | Caller->eraseFromParent(); | ||||
9340 | RemoveFromWorkList(Caller); | ||||
9341 | return 0; | ||||
9342 | } | ||||
9343 | } | ||||
9344 | |||||
9345 | // Replace the trampoline call with a direct call. Since there is no 'nest' | ||||
9346 | // parameter, there is no need to adjust the argument list. Let the generic | ||||
9347 | // code sort out any function type mismatches. | ||||
9348 | Constant *NewCallee = | ||||
9349 | NestF->getType() == PTy ? NestF : ConstantExpr::getBitCast(NestF, PTy); | ||||
9350 | CS.setCalledFunction(NewCallee); | ||||
9351 | return CS.getInstruction(); | ||||
9352 | } | ||||
9353 | |||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9354 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
9355 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's | ||||
9356 | /// and a single binop. | ||||
9357 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { | ||||
9358 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9359 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || |
9360 | isa<CmpInst>(FirstInst)); | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9361 | unsigned Opc = FirstInst->getOpcode(); |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9362 | Value *LHSVal = FirstInst->getOperand(0); |
9363 | Value *RHSVal = FirstInst->getOperand(1); | ||||
9364 | |||||
9365 | const Type *LHSType = LHSVal->getType(); | ||||
9366 | const Type *RHSType = RHSVal->getType(); | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9367 | |
9368 | // Scan to see if all operands are the same opcode, all have one use, and all | ||||
9369 | // kill their operands (i.e. the operands have one use). | ||||
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9370 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9371 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
Chris Lattner | a90a24c | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 9372 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9373 | // 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] | 9374 | // types or GEP's with different index types. |
9375 | I->getOperand(0)->getType() != LHSType || | ||||
9376 | I->getOperand(1)->getType() != RHSType) | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9377 | return 0; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9378 | |
9379 | // If they are CmpInst instructions, check their predicates | ||||
9380 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) | ||||
9381 | if (cast<CmpInst>(I)->getPredicate() != | ||||
9382 | cast<CmpInst>(FirstInst)->getPredicate()) | ||||
9383 | return 0; | ||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9384 | |
9385 | // Keep track of which operand needs a phi node. | ||||
9386 | if (I->getOperand(0) != LHSVal) LHSVal = 0; | ||||
9387 | if (I->getOperand(1) != RHSVal) RHSVal = 0; | ||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9388 | } |
9389 | |||||
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9390 | // Otherwise, this is safe to transform, determine if it is profitable. |
9391 | |||||
9392 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. | ||||
9393 | // Indexes are often folded into load/store instructions, so we don't want to | ||||
9394 | // hide them behind a phi. | ||||
9395 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) | ||||
9396 | return 0; | ||||
9397 | |||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9398 | Value *InLHS = FirstInst->getOperand(0); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9399 | Value *InRHS = FirstInst->getOperand(1); |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9400 | PHINode *NewLHS = 0, *NewRHS = 0; |
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9401 | if (LHSVal == 0) { |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9402 | NewLHS = PHINode::Create(LHSType, |
9403 | FirstInst->getOperand(0)->getName() + ".pn"); | ||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9404 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
9405 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); | ||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9406 | InsertNewInstBefore(NewLHS, PN); |
9407 | LHSVal = NewLHS; | ||||
9408 | } | ||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9409 | |
9410 | if (RHSVal == 0) { | ||||
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9411 | NewRHS = PHINode::Create(RHSType, |
9412 | FirstInst->getOperand(1)->getName() + ".pn"); | ||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9413 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
9414 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); | ||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9415 | InsertNewInstBefore(NewRHS, PN); |
9416 | RHSVal = NewRHS; | ||||
9417 | } | ||||
9418 | |||||
Chris Lattner | f6fd94d | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 9419 | // Add all operands to the new PHIs. |
9420 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | ||||
9421 | if (NewLHS) { | ||||
9422 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); | ||||
9423 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); | ||||
9424 | } | ||||
9425 | if (NewRHS) { | ||||
9426 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); | ||||
9427 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); | ||||
9428 | } | ||||
9429 | } | ||||
9430 | |||||
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9431 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9432 | return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9433 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9434 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9435 | RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9436 | else { |
9437 | assert(isa<GetElementPtrInst>(FirstInst)); | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9438 | return GetElementPtrInst::Create(LHSVal, RHSVal); |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9439 | } |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9440 | } |
9441 | |||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9442 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out |
9443 | /// of the block that defines it. This means that it must be obvious the value | ||||
9444 | /// of the load is not changed from the point of the load to the end of the | ||||
9445 | /// block it is in. | ||||
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9446 | /// |
9447 | /// Finally, it is safe, but not profitable, to sink a load targetting a | ||||
9448 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca | ||||
9449 | /// to a register. | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9450 | static bool isSafeToSinkLoad(LoadInst *L) { |
9451 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); | ||||
9452 | |||||
9453 | for (++BBI; BBI != E; ++BBI) | ||||
9454 | if (BBI->mayWriteToMemory()) | ||||
9455 | return false; | ||||
Chris Lattner | fd905ca | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 9456 | |
9457 | // Check for non-address taken alloca. If not address-taken already, it isn't | ||||
9458 | // profitable to do this xform. | ||||
9459 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { | ||||
9460 | bool isAddressTaken = false; | ||||
9461 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); | ||||
9462 | UI != E; ++UI) { | ||||
9463 | if (isa<LoadInst>(UI)) continue; | ||||
9464 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { | ||||
9465 | // If storing TO the alloca, then the address isn't taken. | ||||
9466 | if (SI->getOperand(1) == AI) continue; | ||||
9467 | } | ||||
9468 | isAddressTaken = true; | ||||
9469 | break; | ||||
9470 | } | ||||
9471 | |||||
9472 | if (!isAddressTaken) | ||||
9473 | return false; | ||||
9474 | } | ||||
9475 | |||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9476 | return true; |
9477 | } | ||||
9478 | |||||
Chris Lattner | 9fe3886 | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 9479 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9480 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
9481 | // operator and they all are only used by the PHI, PHI together their | ||||
9482 | // inputs, and do the operation once, to the result of the PHI. | ||||
9483 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { | ||||
9484 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); | ||||
9485 | |||||
9486 | // Scan the instruction, looking for input operations that can be folded away. | ||||
9487 | // If all input operands to the phi are the same instruction (e.g. a cast from | ||||
9488 | // the same type or "+42") we can pull the operation through the PHI, reducing | ||||
9489 | // code size and simplifying code. | ||||
9490 | Constant *ConstantOp = 0; | ||||
9491 | const Type *CastSrcTy = 0; | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9492 | bool isVolatile = false; |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9493 | if (isa<CastInst>(FirstInst)) { |
9494 | CastSrcTy = FirstInst->getOperand(0)->getType(); | ||||
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 9495 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9496 | // Can fold binop, compare or shift here if the RHS is a constant, |
9497 | // otherwise call FoldPHIArgBinOpIntoPHI. | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9498 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
Chris Lattner | 7da52b2 | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 9499 | if (ConstantOp == 0) |
9500 | return FoldPHIArgBinOpIntoPHI(PN); | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9501 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
9502 | isVolatile = LI->isVolatile(); | ||||
9503 | // We can't sink the load if the loaded value could be modified between the | ||||
9504 | // load and the PHI. | ||||
9505 | if (LI->getParent() != PN.getIncomingBlock(0) || | ||||
9506 | !isSafeToSinkLoad(LI)) | ||||
9507 | return 0; | ||||
Chris Lattner | 7104296 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 9508 | |
9509 | // If the PHI is of volatile loads and the load block has multiple | ||||
9510 | // successors, sinking it would remove a load of the volatile value from | ||||
9511 | // the path through the other successor. | ||||
9512 | if (isVolatile && | ||||
9513 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) | ||||
9514 | return 0; | ||||
9515 | |||||
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9516 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
Chris Lattner | 53738a4 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 9517 | if (FirstInst->getNumOperands() == 2) |
Chris Lattner | 9c08050 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 9518 | return FoldPHIArgBinOpIntoPHI(PN); |
9519 | // Can't handle general GEPs yet. | ||||
9520 | return 0; | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9521 | } else { |
9522 | return 0; // Cannot fold this operation. | ||||
9523 | } | ||||
9524 | |||||
9525 | // Check to see if all arguments are the same operation. | ||||
9526 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | ||||
9527 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; | ||||
9528 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9529 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9530 | return 0; |
9531 | if (CastSrcTy) { | ||||
9532 | if (I->getOperand(0)->getType() != CastSrcTy) | ||||
9533 | return 0; // Cast operation must match. | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9534 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9535 | // We can't sink the load if the loaded value could be modified between |
9536 | // the load and the PHI. | ||||
Chris Lattner | 76c7314 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 9537 | if (LI->isVolatile() != isVolatile || |
9538 | LI->getParent() != PN.getIncomingBlock(i) || | ||||
9539 | !isSafeToSinkLoad(LI)) | ||||
9540 | return 0; | ||||
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 9541 | |
Chris Lattner | 7104296 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 9542 | // If the PHI is of volatile loads and the load block has multiple |
9543 | // successors, sinking it would remove a load of the volatile value from | ||||
9544 | // the path through the other successor. | ||||
Chris Lattner | 40700fe | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 9545 | if (isVolatile && |
9546 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) | ||||
9547 | return 0; | ||||
9548 | |||||
9549 | |||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9550 | } else if (I->getOperand(1) != ConstantOp) { |
9551 | return 0; | ||||
9552 | } | ||||
9553 | } | ||||
9554 | |||||
9555 | // Okay, they are all the same operation. Create a new PHI node of the | ||||
9556 | // 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] | 9557 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
9558 | PN.getName()+".in"); | ||||
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 9559 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9560 | |
9561 | Value *InVal = FirstInst->getOperand(0); | ||||
9562 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9563 | |
9564 | // Add all operands to the new PHI. | ||||
Chris Lattner | b589344 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 9565 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
9566 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); | ||||
9567 | if (NewInVal != InVal) | ||||
9568 | InVal = 0; | ||||
9569 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); | ||||
9570 | } | ||||
9571 | |||||
9572 | Value *PhiVal; | ||||
9573 | if (InVal) { | ||||
9574 | // The new PHI unions all of the same values together. This is really | ||||
9575 | // common, so we handle it intelligently here for compile-time speed. | ||||
9576 | PhiVal = InVal; | ||||
9577 | delete NewPN; | ||||
9578 | } else { | ||||
9579 | InsertNewInstBefore(NewPN, PN); | ||||
9580 | PhiVal = NewPN; | ||||
9581 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9582 | |
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9583 | // Insert and return the new operation. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9584 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9585 | return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9586 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9587 | return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9588 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9589 | return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 9590 | PhiVal, ConstantOp); |
Chris Lattner | 54545ac | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 9591 | assert(isa<LoadInst>(FirstInst) && "Unknown operation"); |
9592 | |||||
9593 | // If this was a volatile load that we are merging, make sure to loop through | ||||
9594 | // and mark all the input loads as non-volatile. If we don't do this, we will | ||||
9595 | // insert a new volatile load and the old ones will not be deletable. | ||||
9596 | if (isVolatile) | ||||
9597 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) | ||||
9598 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); | ||||
9599 | |||||
9600 | return new LoadInst(PhiVal, "", isVolatile); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9601 | } |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9602 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9603 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
9604 | /// that is dead. | ||||
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9605 | static bool DeadPHICycle(PHINode *PN, |
9606 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { | ||||
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9607 | if (PN->use_empty()) return true; |
9608 | if (!PN->hasOneUse()) return false; | ||||
9609 | |||||
9610 | // Remember this node, and if we find the cycle, return. | ||||
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9611 | if (!PotentiallyDeadPHIs.insert(PN)) |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9612 | return true; |
Chris Lattner | 92103de | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 9613 | |
9614 | // Don't scan crazily complex things. | ||||
9615 | if (PotentiallyDeadPHIs.size() == 16) | ||||
9616 | return false; | ||||
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9617 | |
9618 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) | ||||
9619 | return DeadPHICycle(PU, PotentiallyDeadPHIs); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9620 | |
Chris Lattner | a3fd1c5 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 9621 | return false; |
9622 | } | ||||
9623 | |||||
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9624 | /// PHIsEqualValue - Return true if this phi node is always equal to |
9625 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: | ||||
9626 | /// z = some value; x = phi (y, z); y = phi (x, z) | ||||
9627 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, | ||||
9628 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { | ||||
9629 | // See if we already saw this PHI node. | ||||
9630 | if (!ValueEqualPHIs.insert(PN)) | ||||
9631 | return true; | ||||
9632 | |||||
9633 | // Don't scan crazily complex things. | ||||
9634 | if (ValueEqualPHIs.size() == 16) | ||||
9635 | return false; | ||||
9636 | |||||
9637 | // Scan the operands to see if they are either phi nodes or are equal to | ||||
9638 | // the value. | ||||
9639 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { | ||||
9640 | Value *Op = PN->getIncomingValue(i); | ||||
9641 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { | ||||
9642 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) | ||||
9643 | return false; | ||||
9644 | } else if (Op != NonPhiInVal) | ||||
9645 | return false; | ||||
9646 | } | ||||
9647 | |||||
9648 | return true; | ||||
9649 | } | ||||
9650 | |||||
9651 | |||||
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9652 | // PHINode simplification |
9653 | // | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9654 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
Owen Anderson | b64ab87 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 9655 | // If LCSSA is around, don't mess with Phi nodes |
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9656 | if (MustPreserveLCSSA) return 0; |
Owen Anderson | d1b78a1 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 9657 | |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9658 | if (Value *V = PN.hasConstantValue()) |
9659 | return ReplaceInstUsesWith(PN, V); | ||||
9660 | |||||
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9661 | // If all PHI operands are the same operation, pull them through the PHI, |
9662 | // reducing code size. | ||||
9663 | if (isa<Instruction>(PN.getIncomingValue(0)) && | ||||
9664 | PN.getIncomingValue(0)->hasOneUse()) | ||||
9665 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) | ||||
9666 | return Result; | ||||
9667 | |||||
9668 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if | ||||
9669 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a | ||||
9670 | // PHI)... break the cycle. | ||||
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9671 | if (PN.hasOneUse()) { |
9672 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); | ||||
9673 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { | ||||
Chris Lattner | 0e5444b | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 9674 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9675 | PotentiallyDeadPHIs.insert(&PN); |
9676 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) | ||||
9677 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); | ||||
9678 | } | ||||
Chris Lattner | ff9f13a | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 9679 | |
9680 | // If this phi has a single use, and if that use just computes a value for | ||||
9681 | // the next iteration of a loop, delete the phi. This occurs with unused | ||||
9682 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this | ||||
9683 | // common case here is good because the only other things that catch this | ||||
9684 | // are induction variable analysis (sometimes) and ADCE, which is only run | ||||
9685 | // late. | ||||
9686 | if (PHIUser->hasOneUse() && | ||||
9687 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && | ||||
9688 | PHIUser->use_back() == &PN) { | ||||
9689 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); | ||||
9690 | } | ||||
9691 | } | ||||
Owen Anderson | 7e05714 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 9692 | |
Chris Lattner | cf5008a | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 9693 | // We sometimes end up with phi cycles that non-obviously end up being the |
9694 | // same value, for example: | ||||
9695 | // z = some value; x = phi (y, z); y = phi (x, z) | ||||
9696 | // where the phi nodes don't necessarily need to be in the same block. Do a | ||||
9697 | // quick check to see if the PHI node only contains a single non-phi value, if | ||||
9698 | // so, scan to see if the phi cycle is actually equal to that value. | ||||
9699 | { | ||||
9700 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); | ||||
9701 | // Scan for the first non-phi operand. | ||||
9702 | while (InValNo != NumOperandVals && | ||||
9703 | isa<PHINode>(PN.getIncomingValue(InValNo))) | ||||
9704 | ++InValNo; | ||||
9705 | |||||
9706 | if (InValNo != NumOperandVals) { | ||||
9707 | Value *NonPhiInVal = PN.getOperand(InValNo); | ||||
9708 | |||||
9709 | // Scan the rest of the operands to see if there are any conflicts, if so | ||||
9710 | // there is no need to recursively scan other phis. | ||||
9711 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { | ||||
9712 | Value *OpVal = PN.getIncomingValue(InValNo); | ||||
9713 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) | ||||
9714 | break; | ||||
9715 | } | ||||
9716 | |||||
9717 | // If we scanned over all operands, then we have one unique value plus | ||||
9718 | // phi values. Scan PHI nodes to see if they all merge in each other or | ||||
9719 | // the value. | ||||
9720 | if (InValNo == NumOperandVals) { | ||||
9721 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; | ||||
9722 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) | ||||
9723 | return ReplaceInstUsesWith(PN, NonPhiInVal); | ||||
9724 | } | ||||
9725 | } | ||||
9726 | } | ||||
Chris Lattner | 60921c9 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 9727 | return 0; |
Chris Lattner | 473945d | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 9728 | } |
9729 | |||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9730 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
9731 | Instruction *InsertPoint, | ||||
9732 | InstCombiner *IC) { | ||||
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 9733 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); |
9734 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9735 | // We must cast correctly to the pointer type. Ensure that we |
9736 | // sign extend the integer value if it is smaller as this is | ||||
9737 | // used for address computation. | ||||
9738 | Instruction::CastOps opcode = | ||||
9739 | (VTySize < PtrSize ? Instruction::SExt : | ||||
9740 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); | ||||
9741 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9742 | } |
9743 | |||||
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9744 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9745 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9746 | Value *PtrOp = GEP.getOperand(0); |
Chris Lattner | 9bc1464 | 2007-04-28 00:57:34 +0000 | [diff] [blame] | 9747 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9748 | // If so, eliminate the noop. |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9749 | if (GEP.getNumOperands() == 1) |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9750 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9751 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 9752 | if (isa<UndefValue>(GEP.getOperand(0))) |
9753 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); | ||||
9754 | |||||
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9755 | bool HasZeroPointerIndex = false; |
9756 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) | ||||
9757 | HasZeroPointerIndex = C->isNullValue(); | ||||
9758 | |||||
9759 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9760 | return ReplaceInstUsesWith(GEP, PtrOp); |
Chris Lattner | a1be566 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 9761 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9762 | // Eliminate unneeded casts for indices. |
9763 | bool MadeChange = false; | ||||
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9764 | |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9765 | gep_type_iterator GTI = gep_type_begin(GEP); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9766 | for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end(); |
9767 | i != e; ++i, ++GTI) { | ||||
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9768 | if (isa<SequentialType>(*GTI)) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9769 | if (CastInst *CI = dyn_cast<CastInst>(*i)) { |
Chris Lattner | 76b7a06 | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 9770 | if (CI->getOpcode() == Instruction::ZExt || |
9771 | CI->getOpcode() == Instruction::SExt) { | ||||
9772 | const Type *SrcTy = CI->getOperand(0)->getType(); | ||||
9773 | // We can eliminate a cast from i32 to i64 iff the target | ||||
9774 | // is a 32-bit pointer target. | ||||
9775 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { | ||||
9776 | MadeChange = true; | ||||
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9777 | *i = CI->getOperand(0); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9778 | } |
9779 | } | ||||
9780 | } | ||||
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9781 | // If we are using a wider index than needed for this platform, shrink it |
9782 | // to what we need. If the incoming value needs a cast instruction, | ||||
9783 | // insert it. This explicit cast can make subsequent optimizations more | ||||
9784 | // obvious. | ||||
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9785 | Value *Op = *i; |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9786 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9787 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9788 | *i = ConstantExpr::getTrunc(C, TD->getIntPtrType()); |
Chris Lattner | 4f1134e | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 9789 | MadeChange = true; |
9790 | } else { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9791 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
9792 | GEP); | ||||
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9793 | *i = Op; |
Chris Lattner | cb69a4e | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 9794 | MadeChange = true; |
9795 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 9796 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9797 | } |
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9798 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9799 | if (MadeChange) return &GEP; |
9800 | |||||
Chris Lattner | db9654e | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 9801 | // If this GEP instruction doesn't move the pointer, and if the input operand |
9802 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the | ||||
9803 | // real input to the dest type. | ||||
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9804 | if (GEP.hasAllZeroIndices()) { |
9805 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(GEP.getOperand(0))) { | ||||
9806 | // If the bitcast is of an allocation, and the allocation will be | ||||
9807 | // converted to match the type of the cast, don't touch this. | ||||
9808 | if (isa<AllocationInst>(BCI->getOperand(0))) { | ||||
9809 | // 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] | 9810 | if (Instruction *I = visitBitCast(*BCI)) { |
9811 | if (I != BCI) { | ||||
9812 | I->takeName(BCI); | ||||
9813 | BCI->getParent()->getInstList().insert(BCI, I); | ||||
9814 | ReplaceInstUsesWith(*BCI, I); | ||||
9815 | } | ||||
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9816 | return &GEP; |
Chris Lattner | a79dd43 | 2007-10-12 18:05:47 +0000 | [diff] [blame] | 9817 | } |
Chris Lattner | 6a94de2 | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 9818 | } |
9819 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); | ||||
9820 | } | ||||
9821 | } | ||||
9822 | |||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9823 | // Combine Indices - If the source pointer to this getelementptr instruction |
9824 | // is a getelementptr instruction, combine the indices of the two | ||||
9825 | // getelementptr instructions into a single instruction. | ||||
9826 | // | ||||
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9827 | SmallVector<Value*, 8> SrcGEPOperands; |
Chris Lattner | 574da9b | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 9828 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9829 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
Chris Lattner | ebd985c | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 9830 | |
9831 | if (!SrcGEPOperands.empty()) { | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9832 | // Note that if our source is a gep chain itself that we wait for that |
9833 | // chain to be resolved before we perform this transformation. This | ||||
9834 | // avoids us creating a TON of code in some cases. | ||||
9835 | // | ||||
9836 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && | ||||
9837 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) | ||||
9838 | return 0; // Wait until our source is folded to completion. | ||||
9839 | |||||
Chris Lattner | 72588fc | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 9840 | SmallVector<Value*, 8> Indices; |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9841 | |
9842 | // Find out whether the last index in the source GEP is a sequential idx. | ||||
9843 | bool EndsWithSequential = false; | ||||
9844 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), | ||||
9845 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) | ||||
Chris Lattner | be97b4e | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 9846 | EndsWithSequential = !isa<StructType>(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9847 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9848 | // Can we combine the two pointer arithmetics offsets? |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9849 | if (EndsWithSequential) { |
Chris Lattner | decd081 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 9850 | // Replace: gep (gep %P, long B), long A, ... |
9851 | // With: T = long A+B; gep %P, T, ... | ||||
9852 | // | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9853 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9854 | if (SO1 == Constant::getNullValue(SO1->getType())) { |
9855 | Sum = GO1; | ||||
9856 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { | ||||
9857 | Sum = SO1; | ||||
9858 | } else { | ||||
9859 | // If they aren't the same type, convert both to an integer of the | ||||
9860 | // target's pointer size. | ||||
9861 | if (SO1->getType() != GO1->getType()) { | ||||
9862 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9863 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9864 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9865 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9866 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9867 | unsigned PS = TD->getPointerSizeInBits(); |
9868 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9869 | // Convert GO1 to SO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9870 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9871 | |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9872 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9873 | // Convert SO1 to GO1's type. |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9874 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9875 | } else { |
9876 | const Type *PT = TD->getIntPtrType(); | ||||
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 9877 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
9878 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); | ||||
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9879 | } |
9880 | } | ||||
9881 | } | ||||
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9882 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
9883 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); | ||||
9884 | else { | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9885 | Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
Chris Lattner | 48595f1 | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 9886 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9887 | } |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9888 | } |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9889 | |
9890 | // Recycle the GEP we already have if possible. | ||||
9891 | if (SrcGEPOperands.size() == 2) { | ||||
9892 | GEP.setOperand(0, SrcGEPOperands[0]); | ||||
9893 | GEP.setOperand(1, Sum); | ||||
9894 | return &GEP; | ||||
9895 | } else { | ||||
9896 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, | ||||
9897 | SrcGEPOperands.end()-1); | ||||
9898 | Indices.push_back(Sum); | ||||
9899 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); | ||||
9900 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9901 | } else if (isa<Constant>(*GEP.idx_begin()) && |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 9902 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9903 | SrcGEPOperands.size() != 1) { |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9904 | // 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] | 9905 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
9906 | SrcGEPOperands.end()); | ||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9907 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
9908 | } | ||||
9909 | |||||
9910 | if (!Indices.empty()) | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9911 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
9912 | Indices.end(), GEP.getName()); | ||||
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9913 | |
Chris Lattner | 620ce14 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 9914 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9915 | // GEP of global variable. If all of the indices for this GEP are |
9916 | // constants, we can promote this to a constexpr instead of an instruction. | ||||
9917 | |||||
9918 | // Scan for nonconstants... | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9919 | SmallVector<Constant*, 8> Indices; |
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9920 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
9921 | for (; I != E && isa<Constant>(*I); ++I) | ||||
9922 | Indices.push_back(cast<Constant>(*I)); | ||||
9923 | |||||
9924 | if (I == E) { // If they are all constants... | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 9925 | Constant *CE = ConstantExpr::getGetElementPtr(GV, |
9926 | &Indices[0],Indices.size()); | ||||
Chris Lattner | 9b76123 | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 9927 | |
9928 | // Replace all uses of the GEP with the new constexpr... | ||||
9929 | return ReplaceInstUsesWith(GEP, CE); | ||||
9930 | } | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9931 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9932 | if (!isa<PointerType>(X->getType())) { |
9933 | // Not interesting. Source pointer must be a cast from pointer. | ||||
9934 | } else if (HasZeroPointerIndex) { | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9935 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
9936 | // into : GEP [10 x i8]* X, i32 0, ... | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9937 | // |
9938 | // This occurs when the program declares an array extern like "int X[];" | ||||
9939 | // | ||||
9940 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); | ||||
9941 | const PointerType *XTy = cast<PointerType>(X->getType()); | ||||
9942 | if (const ArrayType *XATy = | ||||
9943 | dyn_cast<ArrayType>(XTy->getElementType())) | ||||
9944 | if (const ArrayType *CATy = | ||||
9945 | dyn_cast<ArrayType>(CPTy->getElementType())) | ||||
9946 | if (CATy->getElementType() == XATy->getElementType()) { | ||||
9947 | // At this point, we know that the cast source type is a pointer | ||||
9948 | // to an array of the same type as the destination pointer | ||||
9949 | // array. Because the array type is never stepped over (there | ||||
9950 | // is a leading zero) we can fold the cast into this GEP. | ||||
9951 | GEP.setOperand(0, X); | ||||
9952 | return &GEP; | ||||
9953 | } | ||||
9954 | } else if (GEP.getNumOperands() == 2) { | ||||
9955 | // Transform things like: | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9956 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
9957 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9958 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
9959 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); | ||||
9960 | if (isa<ArrayType>(SrcElTy) && | ||||
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9961 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
9962 | TD->getABITypeSize(ResElTy)) { | ||||
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 9963 | Value *Idx[2]; |
9964 | Idx[0] = Constant::getNullValue(Type::Int32Ty); | ||||
9965 | Idx[1] = GEP.getOperand(1); | ||||
Chris Lattner | eed4827 | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 9966 | Value *V = InsertNewInstBefore( |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9967 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 9968 | // V and GEP are both pointer types --> BitCast |
9969 | return new BitCastInst(V, GEP.getType()); | ||||
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 9970 | } |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9971 | |
9972 | // Transform things like: | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9973 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9974 | // (where tmp = 8*tmp2) into: |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9975 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9976 | |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 9977 | if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9978 | uint64_t ArrayEltSize = |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 9979 | TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9980 | |
9981 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We | ||||
9982 | // allow either a mul, shift, or constant here. | ||||
9983 | Value *NewIdx = 0; | ||||
9984 | ConstantInt *Scale = 0; | ||||
9985 | if (ArrayEltSize == 1) { | ||||
9986 | NewIdx = GEP.getOperand(1); | ||||
9987 | Scale = ConstantInt::get(NewIdx->getType(), 1); | ||||
9988 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { | ||||
Chris Lattner | 6e2f843 | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 9989 | NewIdx = ConstantInt::get(CI->getType(), 1); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9990 | Scale = CI; |
9991 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ | ||||
9992 | if (Inst->getOpcode() == Instruction::Shl && | ||||
9993 | isa<ConstantInt>(Inst->getOperand(1))) { | ||||
Zhou Sheng | 0e2d3ac | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 9994 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
9995 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); | ||||
9996 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); | ||||
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 9997 | NewIdx = Inst->getOperand(0); |
9998 | } else if (Inst->getOpcode() == Instruction::Mul && | ||||
9999 | isa<ConstantInt>(Inst->getOperand(1))) { | ||||
10000 | Scale = cast<ConstantInt>(Inst->getOperand(1)); | ||||
10001 | NewIdx = Inst->getOperand(0); | ||||
10002 | } | ||||
10003 | } | ||||
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10004 | |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10005 | // 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] | 10006 | // out, perform the transformation. Note, we don't know whether Scale is |
10007 | // signed or not. We'll use unsigned version of division/modulo | ||||
10008 | // operation after making sure Scale doesn't have the sign bit set. | ||||
10009 | if (Scale && Scale->getSExtValue() >= 0LL && | ||||
10010 | Scale->getZExtValue() % ArrayEltSize == 0) { | ||||
10011 | Scale = ConstantInt::get(Scale->getType(), | ||||
10012 | Scale->getZExtValue() / ArrayEltSize); | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10013 | if (Scale->getZExtValue() != 1) { |
Reid Spencer | 17212df | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 10014 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), |
Wojciech Matyjewicz | ed22325 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 10015 | false /*ZExt*/); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10016 | Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale"); |
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10017 | NewIdx = InsertNewInstBefore(Sc, GEP); |
10018 | } | ||||
10019 | |||||
10020 | // Insert the new GEP instruction. | ||||
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 10021 | Value *Idx[2]; |
10022 | Idx[0] = Constant::getNullValue(Type::Int32Ty); | ||||
10023 | Idx[1] = NewIdx; | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10024 | Instruction *NewGEP = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10025 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10026 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
10027 | // The NewGEP must be pointer typed, so must the old one -> BitCast | ||||
10028 | return new BitCastInst(NewGEP, GEP.getType()); | ||||
Chris Lattner | 7835cdd | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 10029 | } |
10030 | } | ||||
Chris Lattner | c6bd195 | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 10031 | } |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10032 | } |
10033 | |||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 10034 | return 0; |
10035 | } | ||||
10036 | |||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10037 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
10038 | // 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] | 10039 | if (AI.isArrayAllocation()) { // Check C != 1 |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10040 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
10041 | const Type *NewTy = | ||||
10042 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); | ||||
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 10043 | AllocationInst *New = 0; |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10044 | |
10045 | // Create and insert the replacement instruction... | ||||
10046 | if (isa<MallocInst>(AI)) | ||||
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 10047 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 10048 | else { |
10049 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); | ||||
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 10050 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 10051 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 10052 | |
10053 | InsertNewInstBefore(New, AI); | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10054 | |
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10055 | // Scan to the end of the allocation instructions, to skip over a block of |
10056 | // allocas if possible... | ||||
10057 | // | ||||
10058 | BasicBlock::iterator It = New; | ||||
10059 | while (isa<AllocationInst>(*It)) ++It; | ||||
10060 | |||||
10061 | // Now that I is pointing to the first non-allocation-inst in the block, | ||||
10062 | // insert our getelementptr instruction... | ||||
10063 | // | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 10064 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 10065 | Value *Idx[2]; |
10066 | Idx[0] = NullIdx; | ||||
10067 | Idx[1] = NullIdx; | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10068 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
10069 | New->getName()+".sub", It); | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10070 | |
10071 | // Now make everything use the getelementptr instead of the original | ||||
10072 | // allocation. | ||||
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 10073 | return ReplaceInstUsesWith(AI, V); |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10074 | } else if (isa<UndefValue>(AI.getArraySize())) { |
10075 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10076 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10077 | } |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 10078 | |
10079 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. | ||||
10080 | // Note that we only do this for alloca's, because malloc should allocate and | ||||
10081 | // return a unique pointer, even for a zero byte allocation. | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10082 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 10083 | TD->getABITypeSize(AI.getAllocatedType()) == 0) |
Chris Lattner | 7c881df | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 10084 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
10085 | |||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10086 | return 0; |
10087 | } | ||||
10088 | |||||
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 10089 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
10090 | Value *Op = FI.getOperand(0); | ||||
10091 | |||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10092 | // free undef -> unreachable. |
10093 | if (isa<UndefValue>(Op)) { | ||||
10094 | // 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] | 10095 | new StoreInst(ConstantInt::getTrue(), |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 10096 | UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10097 | return EraseInstFromFunction(FI); |
10098 | } | ||||
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 10099 | |
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 10100 | // If we have 'free null' delete the instruction. This can happen in stl code |
10101 | // when lots of inlining happens. | ||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10102 | if (isa<ConstantPointerNull>(Op)) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 10103 | return EraseInstFromFunction(FI); |
Chris Lattner | 6fe5541 | 2007-04-14 00:20:02 +0000 | [diff] [blame] | 10104 | |
10105 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X | ||||
10106 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { | ||||
10107 | FI.setOperand(0, CI->getOperand(0)); | ||||
10108 | return &FI; | ||||
10109 | } | ||||
10110 | |||||
10111 | // Change free (gep X, 0,0,0,0) into free(X) | ||||
10112 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { | ||||
10113 | if (GEPI->hasAllZeroIndices()) { | ||||
10114 | AddToWorkList(GEPI); | ||||
10115 | FI.setOperand(0, GEPI->getOperand(0)); | ||||
10116 | return &FI; | ||||
10117 | } | ||||
10118 | } | ||||
10119 | |||||
10120 | // Change free(malloc) into nothing, if the malloc has a single use. | ||||
10121 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) | ||||
10122 | if (MI->hasOneUse()) { | ||||
10123 | EraseInstFromFunction(FI); | ||||
10124 | return EraseInstFromFunction(*MI); | ||||
10125 | } | ||||
Chris Lattner | 6160e85 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 10126 | |
Chris Lattner | 67b1e1b | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 10127 | return 0; |
10128 | } | ||||
10129 | |||||
10130 | |||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10131 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10132 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 10133 | const TargetData *TD) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10134 | User *CI = cast<User>(LI.getOperand(0)); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10135 | Value *CastOp = CI->getOperand(0); |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10136 | |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10137 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
10138 | // Instead of loading constant c string, use corresponding integer value | ||||
10139 | // directly if string length is small enough. | ||||
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 10140 | std::string Str; |
10141 | if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) { | ||||
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10142 | unsigned len = Str.length(); |
10143 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); | ||||
10144 | unsigned numBits = Ty->getPrimitiveSizeInBits(); | ||||
10145 | // Replace LI with immediate integer store. | ||||
10146 | if ((numBits >> 3) == len + 1) { | ||||
Bill Wendling | 587c01d | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 10147 | APInt StrVal(numBits, 0); |
10148 | APInt SingleChar(numBits, 0); | ||||
10149 | if (TD->isLittleEndian()) { | ||||
10150 | for (signed i = len-1; i >= 0; i--) { | ||||
10151 | SingleChar = (uint64_t) Str[i]; | ||||
10152 | StrVal = (StrVal << 8) | SingleChar; | ||||
10153 | } | ||||
10154 | } else { | ||||
10155 | for (unsigned i = 0; i < len; i++) { | ||||
10156 | SingleChar = (uint64_t) Str[i]; | ||||
10157 | StrVal = (StrVal << 8) | SingleChar; | ||||
10158 | } | ||||
10159 | // Append NULL at the end. | ||||
10160 | SingleChar = 0; | ||||
10161 | StrVal = (StrVal << 8) | SingleChar; | ||||
10162 | } | ||||
10163 | Value *NL = ConstantInt::get(StrVal); | ||||
10164 | return IC.ReplaceInstUsesWith(LI, NL); | ||||
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10165 | } |
10166 | } | ||||
10167 | } | ||||
10168 | |||||
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10169 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10170 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10171 | const Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10172 | |
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10173 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10174 | isa<VectorType>(DestPTy)) { |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10175 | // If the source is an array, the code below will not succeed. Check to |
10176 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for | ||||
10177 | // constants. | ||||
10178 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) | ||||
10179 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) | ||||
10180 | if (ASrcTy->getNumElements() != 0) { | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10181 | Value *Idxs[2]; |
10182 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); | ||||
10183 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); | ||||
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10184 | SrcTy = cast<PointerType>(CastOp->getType()); |
10185 | SrcPTy = SrcTy->getElementType(); | ||||
10186 | } | ||||
10187 | |||||
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10188 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10189 | isa<VectorType>(SrcPTy)) && |
Chris Lattner | b1515fe | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 10190 | // Do not allow turning this into a load of an integer, which is then |
10191 | // casted to a pointer, this pessimizes pointer analysis a lot. | ||||
10192 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && | ||||
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10193 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == |
10194 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10195 | |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10196 | // Okay, we are casting from one integer or pointer type to another of |
10197 | // the same size. Instead of casting the pointer before the load, cast | ||||
10198 | // the result of the loaded value. | ||||
10199 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, | ||||
10200 | CI->getName(), | ||||
10201 | LI.isVolatile()),LI); | ||||
10202 | // Now cast the result of the load. | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10203 | return new BitCastInst(NewLoad, LI.getType()); |
Chris Lattner | f952785 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 10204 | } |
Chris Lattner | b89e071 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 10205 | } |
10206 | } | ||||
10207 | return 0; | ||||
10208 | } | ||||
10209 | |||||
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10210 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10211 | /// from this value cannot trap. If it is not obviously safe to load from the |
10212 | /// specified pointer, we do a quick local scan of the basic block containing | ||||
10213 | /// ScanFrom, to determine if the address is already accessed. | ||||
10214 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { | ||||
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10215 | // If it is an alloca it is always safe to load from. |
10216 | if (isa<AllocaInst>(V)) return true; | ||||
10217 | |||||
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10218 | // 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] | 10219 | if (const GlobalValue *GV = dyn_cast<GlobalVariable>(V)) |
Duncan Sands | 46318cd | 2007-09-19 10:25:38 +0000 | [diff] [blame] | 10220 | // Don't try to evaluate aliases. External weak GV can be null. |
Duncan Sands | 892c7e4 | 2007-09-19 10:10:31 +0000 | [diff] [blame] | 10221 | return !isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage(); |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10222 | |
10223 | // Otherwise, be a little bit agressive by scanning the local block where we | ||||
10224 | // 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] | 10225 | // from/to. If so, the previous load or store would have already trapped, |
10226 | // so there is no harm doing an extra load (also, CSE will later eliminate | ||||
10227 | // the load entirely). | ||||
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10228 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); |
10229 | |||||
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10230 | while (BBI != E) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10231 | --BBI; |
10232 | |||||
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 10233 | // If we see a free or a call (which might do a free) the pointer could be |
10234 | // marked invalid. | ||||
10235 | if (isa<FreeInst>(BBI) || isa<CallInst>(BBI)) | ||||
10236 | return false; | ||||
10237 | |||||
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10238 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
10239 | if (LI->getOperand(0) == V) return true; | ||||
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 10240 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10241 | if (SI->getOperand(1) == V) return true; |
Chris Lattner | 2de3fec | 2008-06-20 05:12:56 +0000 | [diff] [blame] | 10242 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10243 | |
Alkis Evlogimenos | 7b6ec60 | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 10244 | } |
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10245 | return false; |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10246 | } |
10247 | |||||
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10248 | /// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts |
10249 | /// until we find the underlying object a pointer is referring to or something | ||||
10250 | /// we don't understand. Note that the returned pointer may be offset from the | ||||
10251 | /// input, because we ignore GEP indices. | ||||
10252 | static Value *GetUnderlyingObject(Value *Ptr) { | ||||
10253 | while (1) { | ||||
10254 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { | ||||
10255 | if (CE->getOpcode() == Instruction::BitCast || | ||||
10256 | CE->getOpcode() == Instruction::GetElementPtr) | ||||
10257 | Ptr = CE->getOperand(0); | ||||
10258 | else | ||||
10259 | return Ptr; | ||||
10260 | } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) { | ||||
10261 | Ptr = BCI->getOperand(0); | ||||
10262 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { | ||||
10263 | Ptr = GEP->getOperand(0); | ||||
10264 | } else { | ||||
10265 | return Ptr; | ||||
10266 | } | ||||
10267 | } | ||||
10268 | } | ||||
10269 | |||||
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10270 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
10271 | Value *Op = LI.getOperand(0); | ||||
Chris Lattner | 5f16a13 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 10272 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10273 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10274 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Op); |
10275 | if (KnownAlign > | ||||
10276 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : | ||||
10277 | LI.getAlignment())) | ||||
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10278 | LI.setAlignment(KnownAlign); |
10279 | |||||
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10280 | // load (cast X) --> cast (load X) iff safe |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10281 | if (isa<CastInst>(Op)) |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10282 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10283 | return Res; |
10284 | |||||
10285 | // None of the following transforms are legal for volatile loads. | ||||
10286 | if (LI.isVolatile()) return 0; | ||||
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10287 | |
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10288 | if (&LI.getParent()->front() != &LI) { |
10289 | BasicBlock::iterator BBI = &LI; --BBI; | ||||
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10290 | // If the instruction immediately before this is a store to the same |
10291 | // address, do a simple form of store->load forwarding. | ||||
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10292 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) |
10293 | if (SI->getOperand(1) == LI.getOperand(0)) | ||||
10294 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); | ||||
Chris Lattner | 9c1f0fd | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 10295 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) |
10296 | if (LIB->getOperand(0) == LI.getOperand(0)) | ||||
10297 | return ReplaceInstUsesWith(LI, LIB); | ||||
Chris Lattner | 62f254d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 10298 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10299 | |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10300 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
10301 | const Value *GEPI0 = GEPI->getOperand(0); | ||||
10302 | // TODO: Consider a target hook for valid address spaces for this xform. | ||||
10303 | if (isa<ConstantPointerNull>(GEPI0) && | ||||
10304 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { | ||||
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10305 | // Insert a new store to null instruction before the load to indicate |
10306 | // that this code is not reachable. We do this instead of inserting | ||||
10307 | // an unreachable instruction directly because we cannot modify the | ||||
10308 | // CFG. | ||||
10309 | new StoreInst(UndefValue::get(LI.getType()), | ||||
10310 | Constant::getNullValue(Op->getType()), &LI); | ||||
10311 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | ||||
10312 | } | ||||
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10313 | } |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10314 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10315 | if (Constant *C = dyn_cast<Constant>(Op)) { |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10316 | // load null/undef -> undef |
Christopher Lamb | b15147e | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 10317 | // TODO: Consider a target hook for valid address spaces for this xform. |
10318 | if (isa<UndefValue>(C) || (C->isNullValue() && | ||||
10319 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { | ||||
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10320 | // Insert a new store to null instruction before the load to indicate that |
10321 | // this code is not reachable. We do this instead of inserting an | ||||
10322 | // unreachable instruction directly because we cannot modify the CFG. | ||||
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10323 | new StoreInst(UndefValue::get(LI.getType()), |
10324 | Constant::getNullValue(Op->getType()), &LI); | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10325 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | 17be635 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 10326 | } |
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10327 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10328 | // Instcombine load (constant global) into the value loaded. |
10329 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10330 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10331 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10332 | |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10333 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10334 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10335 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
10336 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) | ||||
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 10337 | if (GV->isConstant() && !GV->isDeclaration()) |
Chris Lattner | 363f2a2 | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 10338 | if (Constant *V = |
10339 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10340 | return ReplaceInstUsesWith(LI, V); |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10341 | if (CE->getOperand(0)->isNullValue()) { |
10342 | // Insert a new store to null instruction before the load to indicate | ||||
10343 | // that this code is not reachable. We do this instead of inserting | ||||
10344 | // an unreachable instruction directly because we cannot modify the | ||||
10345 | // CFG. | ||||
10346 | new StoreInst(UndefValue::get(LI.getType()), | ||||
10347 | Constant::getNullValue(Op->getType()), &LI); | ||||
10348 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | ||||
10349 | } | ||||
10350 | |||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10351 | } else if (CE->isCast()) { |
Devang Patel | 99db6ad | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 10352 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10353 | return Res; |
10354 | } | ||||
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10355 | } |
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10356 | } |
Chris Lattner | 8d2e888 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 10357 | |
10358 | // If this load comes from anywhere in a constant global, and if the global | ||||
10359 | // is all undef or zero, we know what it loads. | ||||
10360 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) { | ||||
10361 | if (GV->isConstant() && GV->hasInitializer()) { | ||||
10362 | if (GV->getInitializer()->isNullValue()) | ||||
10363 | return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); | ||||
10364 | else if (isa<UndefValue>(GV->getInitializer())) | ||||
10365 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | ||||
10366 | } | ||||
10367 | } | ||||
Chris Lattner | f499eac | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 10368 | |
Chris Lattner | 37366c1 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 10369 | if (Op->hasOneUse()) { |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10370 | // Change select and PHI nodes to select values instead of addresses: this |
10371 | // helps alias analysis out a lot, allows many others simplifications, and | ||||
10372 | // exposes redundancy in the code. | ||||
10373 | // | ||||
10374 | // Note that we cannot do the transformation unless we know that the | ||||
10375 | // introduced loads cannot trap! Something like this is valid as long as | ||||
10376 | // the condition is always false: load (select bool %C, int* null, int* %G), | ||||
10377 | // but it would not be valid if we transformed it to load from null | ||||
10378 | // unconditionally. | ||||
10379 | // | ||||
10380 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { | ||||
10381 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). | ||||
Chris Lattner | 8a37520 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 10382 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
10383 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { | ||||
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10384 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10385 | SI->getOperand(1)->getName()+".val"), LI); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10386 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
Chris Lattner | 79f0c8e | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 10387 | SI->getOperand(2)->getName()+".val"), LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10388 | return SelectInst::Create(SI->getCondition(), V1, V2); |
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10389 | } |
10390 | |||||
Chris Lattner | 684fe21 | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 10391 | // load (select (cond, null, P)) -> load P |
10392 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) | ||||
10393 | if (C->isNullValue()) { | ||||
10394 | LI.setOperand(0, SI->getOperand(2)); | ||||
10395 | return &LI; | ||||
10396 | } | ||||
10397 | |||||
10398 | // load (select (cond, P, null)) -> load P | ||||
10399 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) | ||||
10400 | if (C->isNullValue()) { | ||||
10401 | LI.setOperand(0, SI->getOperand(1)); | ||||
10402 | return &LI; | ||||
10403 | } | ||||
Chris Lattner | c10aced | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 10404 | } |
10405 | } | ||||
Chris Lattner | 833b8a4 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 10406 | return 0; |
10407 | } | ||||
10408 | |||||
Reid Spencer | 55af2b5 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 10409 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10410 | /// when possible. |
10411 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { | ||||
10412 | User *CI = cast<User>(SI.getOperand(1)); | ||||
10413 | Value *CastOp = CI->getOperand(0); | ||||
10414 | |||||
10415 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); | ||||
10416 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { | ||||
10417 | const Type *SrcPTy = SrcTy->getElementType(); | ||||
10418 | |||||
Reid Spencer | 4223016 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 10419 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10420 | // If the source is an array, the code below will not succeed. Check to |
10421 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for | ||||
10422 | // constants. | ||||
10423 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) | ||||
10424 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) | ||||
10425 | if (ASrcTy->getNumElements() != 0) { | ||||
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 10426 | Value* Idxs[2]; |
10427 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); | ||||
10428 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10429 | SrcTy = cast<PointerType>(CastOp->getType()); |
10430 | SrcPTy = SrcTy->getElementType(); | ||||
10431 | } | ||||
10432 | |||||
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10433 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && |
10434 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == | ||||
10435 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10436 | |
10437 | // 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] | 10438 | // the same size. Instead of casting the pointer before |
10439 | // the store, cast the value to be stored. | ||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10440 | Value *NewCast; |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10441 | Value *SIOp0 = SI.getOperand(0); |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10442 | Instruction::CastOps opcode = Instruction::BitCast; |
10443 | const Type* CastSrcTy = SIOp0->getType(); | ||||
10444 | const Type* CastDstTy = SrcPTy; | ||||
10445 | if (isa<PointerType>(CastDstTy)) { | ||||
10446 | if (CastSrcTy->isInteger()) | ||||
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10447 | opcode = Instruction::IntToPtr; |
Reid Spencer | 67f827c | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 10448 | } else if (isa<IntegerType>(CastDstTy)) { |
Reid Spencer | c55b243 | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 10449 | if (isa<PointerType>(SIOp0->getType())) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 10450 | opcode = Instruction::PtrToInt; |
10451 | } | ||||
10452 | if (Constant *C = dyn_cast<Constant>(SIOp0)) | ||||
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10453 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10454 | else |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10455 | NewCast = IC.InsertNewInstBefore( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10456 | CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
Reid Spencer | 7515396 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 10457 | SI); |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10458 | return new StoreInst(NewCast, CastOp); |
10459 | } | ||||
10460 | } | ||||
10461 | } | ||||
10462 | return 0; | ||||
10463 | } | ||||
10464 | |||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10465 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
10466 | Value *Val = SI.getOperand(0); | ||||
10467 | Value *Ptr = SI.getOperand(1); | ||||
10468 | |||||
10469 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) | ||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10470 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10471 | ++NumCombined; |
10472 | return 0; | ||||
10473 | } | ||||
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10474 | |
10475 | // If the RHS is an alloca with a single use, zapify the store, making the | ||||
10476 | // alloca dead. | ||||
Chris Lattner | cea1fdd | 2008-04-29 04:58:38 +0000 | [diff] [blame] | 10477 | if (Ptr->hasOneUse() && !SI.isVolatile()) { |
Chris Lattner | 836692d | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 10478 | if (isa<AllocaInst>(Ptr)) { |
10479 | EraseInstFromFunction(SI); | ||||
10480 | ++NumCombined; | ||||
10481 | return 0; | ||||
10482 | } | ||||
10483 | |||||
10484 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) | ||||
10485 | if (isa<AllocaInst>(GEP->getOperand(0)) && | ||||
10486 | GEP->getOperand(0)->hasOneUse()) { | ||||
10487 | EraseInstFromFunction(SI); | ||||
10488 | ++NumCombined; | ||||
10489 | return 0; | ||||
10490 | } | ||||
10491 | } | ||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10492 | |
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10493 | // Attempt to improve the alignment. |
Dan Gohman | eee962e | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 10494 | unsigned KnownAlign = GetOrEnforceKnownAlignment(Ptr); |
10495 | if (KnownAlign > | ||||
10496 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : | ||||
10497 | SI.getAlignment())) | ||||
Dan Gohman | 9941f74 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 10498 | SI.setAlignment(KnownAlign); |
10499 | |||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10500 | // Do really simple DSE, to catch cases where there are several consequtive |
10501 | // stores to the same location, separated by a few arithmetic operations. This | ||||
10502 | // situation often occurs with bitfield accesses. | ||||
10503 | BasicBlock::iterator BBI = &SI; | ||||
10504 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; | ||||
10505 | --ScanInsts) { | ||||
10506 | --BBI; | ||||
10507 | |||||
10508 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { | ||||
10509 | // Prev store isn't volatile, and stores to the same location? | ||||
10510 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { | ||||
10511 | ++NumDeadStore; | ||||
10512 | ++BBI; | ||||
10513 | EraseInstFromFunction(*PrevSI); | ||||
10514 | continue; | ||||
10515 | } | ||||
10516 | break; | ||||
10517 | } | ||||
10518 | |||||
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10519 | // If this is a load, we have to stop. However, if the loaded value is from |
10520 | // the pointer we're loading and is producing the pointer we're storing, | ||||
10521 | // then *this* store is dead (X = load P; store X -> P). | ||||
10522 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { | ||||
Chris Lattner | a54c7eb | 2007-09-07 05:33:03 +0000 | [diff] [blame] | 10523 | if (LI == Val && LI->getOperand(0) == Ptr && !SI.isVolatile()) { |
Chris Lattner | b4db97f | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 10524 | EraseInstFromFunction(SI); |
10525 | ++NumCombined; | ||||
10526 | return 0; | ||||
10527 | } | ||||
10528 | // Otherwise, this is a load from some other location. Stores before it | ||||
10529 | // may not be dead. | ||||
10530 | break; | ||||
10531 | } | ||||
10532 | |||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10533 | // Don't skip over loads or things that can modify memory. |
Chris Lattner | 0ef546e | 2008-05-08 17:20:30 +0000 | [diff] [blame] | 10534 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10535 | break; |
10536 | } | ||||
10537 | |||||
10538 | |||||
10539 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. | ||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10540 | |
10541 | // store X, null -> turns into 'unreachable' in SimplifyCFG | ||||
10542 | if (isa<ConstantPointerNull>(Ptr)) { | ||||
10543 | if (!isa<UndefValue>(Val)) { | ||||
10544 | SI.setOperand(0, UndefValue::get(Val->getType())); | ||||
10545 | if (Instruction *U = dyn_cast<Instruction>(Val)) | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10546 | AddToWorkList(U); // Dropped a use. |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10547 | ++NumCombined; |
10548 | } | ||||
10549 | return 0; // Do not modify these! | ||||
10550 | } | ||||
10551 | |||||
10552 | // store undef, Ptr -> noop | ||||
10553 | if (isa<UndefValue>(Val)) { | ||||
Chris Lattner | 9ca9641 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 10554 | EraseInstFromFunction(SI); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10555 | ++NumCombined; |
10556 | return 0; | ||||
10557 | } | ||||
10558 | |||||
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10559 | // If the pointer destination is a cast, see if we can fold the cast into the |
10560 | // source instead. | ||||
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 10561 | if (isa<CastInst>(Ptr)) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10562 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
10563 | return Res; | ||||
10564 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) | ||||
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 10565 | if (CE->isCast()) |
Chris Lattner | fcfe33a | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 10566 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
10567 | return Res; | ||||
10568 | |||||
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10569 | |
10570 | // If this store is the last instruction in the basic block, and if the block | ||||
10571 | // 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] | 10572 | BBI = &SI; ++BBI; |
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10573 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10574 | if (BI->isUnconditional()) |
10575 | if (SimplifyStoreAtEndOfBlock(SI)) | ||||
10576 | return 0; // xform done! | ||||
Chris Lattner | 408902b | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 10577 | |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10578 | return 0; |
10579 | } | ||||
10580 | |||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10581 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
10582 | /// if () { *P = v1; } else { *P = v2 } | ||||
10583 | /// into a phi node with a store in the successor. | ||||
10584 | /// | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10585 | /// Simplify things like: |
10586 | /// *P = v1; if () { *P = v2; } | ||||
10587 | /// into a phi node with a store in the successor. | ||||
10588 | /// | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10589 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
10590 | BasicBlock *StoreBB = SI.getParent(); | ||||
10591 | |||||
10592 | // Check to see if the successor block has exactly two incoming edges. If | ||||
10593 | // so, see if the other predecessor contains a store to the same location. | ||||
10594 | // 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] | 10595 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10596 | |
10597 | // Determine whether Dest has exactly two predecessors and, if so, compute | ||||
10598 | // the other predecessor. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10599 | pred_iterator PI = pred_begin(DestBB); |
10600 | BasicBlock *OtherBB = 0; | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10601 | if (*PI != StoreBB) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10602 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10603 | ++PI; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10604 | if (PI == pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10605 | return false; |
10606 | |||||
10607 | if (*PI != StoreBB) { | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10608 | if (OtherBB) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10609 | return false; |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10610 | OtherBB = *PI; |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10611 | } |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10612 | if (++PI != pred_end(DestBB)) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10613 | return false; |
Eli Friedman | 66fe80a | 2008-06-13 21:17:49 +0000 | [diff] [blame] | 10614 | |
10615 | // Bail out if all the relevant blocks aren't distinct (this can happen, | ||||
10616 | // for example, if SI is in an infinite loop) | ||||
10617 | if (StoreBB == DestBB || OtherBB == DestBB) | ||||
10618 | return false; | ||||
10619 | |||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10620 | // Verify that the other block ends in a branch and is not otherwise empty. |
10621 | BasicBlock::iterator BBI = OtherBB->getTerminator(); | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10622 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10623 | if (!OtherBr || BBI == OtherBB->begin()) |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10624 | return false; |
10625 | |||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10626 | // If the other block ends in an unconditional branch, check for the 'if then |
10627 | // else' case. there is an instruction before the branch. | ||||
10628 | StoreInst *OtherStore = 0; | ||||
10629 | if (OtherBr->isUnconditional()) { | ||||
10630 | // If this isn't a store, or isn't a store to the same location, bail out. | ||||
10631 | --BBI; | ||||
10632 | OtherStore = dyn_cast<StoreInst>(BBI); | ||||
10633 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) | ||||
10634 | return false; | ||||
10635 | } else { | ||||
Chris Lattner | d717c18 | 2007-05-05 22:32:24 +0000 | [diff] [blame] | 10636 | // 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] | 10637 | // destinations is StoreBB, then we have the if/then case. |
10638 | if (OtherBr->getSuccessor(0) != StoreBB && | ||||
10639 | OtherBr->getSuccessor(1) != StoreBB) | ||||
10640 | return false; | ||||
10641 | |||||
10642 | // 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] | 10643 | // if/then triangle. See if there is a store to the same ptr as SI that |
10644 | // lives in OtherBB. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10645 | for (;; --BBI) { |
10646 | // Check to see if we find the matching store. | ||||
10647 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { | ||||
10648 | if (OtherStore->getOperand(1) != SI.getOperand(1)) | ||||
10649 | return false; | ||||
10650 | break; | ||||
10651 | } | ||||
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 10652 | // If we find something that may be using or overwriting the stored |
10653 | // value, or if we run out of instructions, we can't do the xform. | ||||
10654 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10655 | BBI == OtherBB->begin()) |
10656 | return false; | ||||
10657 | } | ||||
10658 | |||||
10659 | // In order to eliminate the store in OtherBr, we have to | ||||
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 10660 | // make sure nothing reads or overwrites the stored value in |
10661 | // StoreBB. | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10662 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
10663 | // FIXME: This should really be AA driven. | ||||
Eli Friedman | 6903a24 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 10664 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10665 | return false; |
10666 | } | ||||
10667 | } | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10668 | |
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10669 | // Insert a PHI node now if we need it. |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10670 | Value *MergedVal = OtherStore->getOperand(0); |
10671 | if (MergedVal != SI.getOperand(0)) { | ||||
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10672 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10673 | PN->reserveOperandSpace(2); |
10674 | PN->addIncoming(SI.getOperand(0), SI.getParent()); | ||||
Chris Lattner | 31755a0 | 2007-04-15 01:02:18 +0000 | [diff] [blame] | 10675 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
10676 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); | ||||
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10677 | } |
10678 | |||||
10679 | // Advance to a place where it is safe to insert the new store and | ||||
10680 | // insert it. | ||||
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 10681 | BBI = DestBB->getFirstNonPHI(); |
Chris Lattner | 3284d1f | 2007-04-15 00:07:55 +0000 | [diff] [blame] | 10682 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
10683 | OtherStore->isVolatile()), *BBI); | ||||
10684 | |||||
10685 | // Nuke the old stores. | ||||
10686 | EraseInstFromFunction(SI); | ||||
10687 | EraseInstFromFunction(*OtherStore); | ||||
10688 | ++NumCombined; | ||||
10689 | return true; | ||||
10690 | } | ||||
10691 | |||||
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 10692 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10693 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
10694 | // 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] | 10695 | Value *X = 0; |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10696 | BasicBlock *TrueDest; |
10697 | BasicBlock *FalseDest; | ||||
10698 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && | ||||
10699 | !isa<Constant>(X)) { | ||||
10700 | // Swap Destinations and condition... | ||||
10701 | BI.setCondition(X); | ||||
10702 | BI.setSuccessor(0, FalseDest); | ||||
10703 | BI.setSuccessor(1, TrueDest); | ||||
10704 | return &BI; | ||||
10705 | } | ||||
10706 | |||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10707 | // Cannonicalize fcmp_one -> fcmp_oeq |
10708 | FCmpInst::Predicate FPred; Value *Y; | ||||
10709 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), | ||||
10710 | TrueDest, FalseDest))) | ||||
10711 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || | ||||
10712 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { | ||||
10713 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10714 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10715 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); |
10716 | NewSCC->takeName(I); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10717 | // Swap Destinations and condition... |
10718 | BI.setCondition(NewSCC); | ||||
10719 | BI.setSuccessor(0, FalseDest); | ||||
10720 | BI.setSuccessor(1, TrueDest); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10721 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10722 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10723 | AddToWorkList(NewSCC); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10724 | return &BI; |
10725 | } | ||||
10726 | |||||
10727 | // Cannonicalize icmp_ne -> icmp_eq | ||||
10728 | ICmpInst::Predicate IPred; | ||||
10729 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), | ||||
10730 | TrueDest, FalseDest))) | ||||
10731 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || | ||||
10732 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || | ||||
10733 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { | ||||
10734 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10735 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10736 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); |
10737 | NewSCC->takeName(I); | ||||
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10738 | // Swap Destinations and condition... |
Chris Lattner | acd1f0f | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 10739 | BI.setCondition(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10740 | BI.setSuccessor(0, FalseDest); |
10741 | BI.setSuccessor(1, TrueDest); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10742 | RemoveFromWorkList(I); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 10743 | I->eraseFromParent();; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10744 | AddToWorkList(NewSCC); |
Chris Lattner | 40f5d70 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 10745 | return &BI; |
10746 | } | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 10747 | |
Chris Lattner | c4d10eb | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 10748 | return 0; |
10749 | } | ||||
Chris Lattner | 0864acf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 10750 | |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10751 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
10752 | Value *Cond = SI.getCondition(); | ||||
10753 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { | ||||
10754 | if (I->getOpcode() == Instruction::Add) | ||||
10755 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||
10756 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' | ||||
10757 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) | ||||
Chris Lattner | e87597f | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 10758 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10759 | AddRHS)); |
10760 | SI.setOperand(0, I->getOperand(0)); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 10761 | AddToWorkList(I); |
Chris Lattner | 46238a6 | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 10762 | return &SI; |
10763 | } | ||||
10764 | } | ||||
10765 | return 0; | ||||
10766 | } | ||||
10767 | |||||
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 10768 | Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 10769 | Value *Agg = EV.getAggregateOperand(); |
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 10770 | |
Matthijs Kooijman | 780ae5e | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 10771 | if (!EV.hasIndices()) |
10772 | return ReplaceInstUsesWith(EV, Agg); | ||||
10773 | |||||
10774 | if (Constant *C = dyn_cast<Constant>(Agg)) { | ||||
10775 | if (isa<UndefValue>(C)) | ||||
10776 | return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType())); | ||||
10777 | |||||
10778 | if (isa<ConstantAggregateZero>(C)) | ||||
10779 | return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType())); | ||||
10780 | |||||
10781 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { | ||||
10782 | // Extract the element indexed by the first index out of the constant | ||||
10783 | Value *V = C->getOperand(*EV.idx_begin()); | ||||
10784 | if (EV.getNumIndices() > 1) | ||||
10785 | // Extract the remaining indices out of the constant indexed by the | ||||
10786 | // first index | ||||
10787 | return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end()); | ||||
10788 | else | ||||
10789 | return ReplaceInstUsesWith(EV, V); | ||||
10790 | } | ||||
10791 | return 0; // Can't handle other constants | ||||
10792 | } | ||||
10793 | if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) { | ||||
10794 | // We're extracting from an insertvalue instruction, compare the indices | ||||
10795 | const unsigned *exti, *exte, *insi, *inse; | ||||
10796 | for (exti = EV.idx_begin(), insi = IV->idx_begin(), | ||||
10797 | exte = EV.idx_end(), inse = IV->idx_end(); | ||||
10798 | exti != exte && insi != inse; | ||||
10799 | ++exti, ++insi) { | ||||
10800 | if (*insi != *exti) | ||||
10801 | // The insert and extract both reference distinctly different elements. | ||||
10802 | // This means the extract is not influenced by the insert, and we can | ||||
10803 | // replace the aggregate operand of the extract with the aggregate | ||||
10804 | // operand of the insert. i.e., replace | ||||
10805 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 | ||||
10806 | // %E = extractvalue { i32, { i32 } } %I, 0 | ||||
10807 | // with | ||||
10808 | // %E = extractvalue { i32, { i32 } } %A, 0 | ||||
10809 | return ExtractValueInst::Create(IV->getAggregateOperand(), | ||||
10810 | EV.idx_begin(), EV.idx_end()); | ||||
10811 | } | ||||
10812 | if (exti == exte && insi == inse) | ||||
10813 | // Both iterators are at the end: Index lists are identical. Replace | ||||
10814 | // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 | ||||
10815 | // %C = extractvalue { i32, { i32 } } %B, 1, 0 | ||||
10816 | // with "i32 42" | ||||
10817 | return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand()); | ||||
10818 | if (exti == exte) { | ||||
10819 | // The extract list is a prefix of the insert list. i.e. replace | ||||
10820 | // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 | ||||
10821 | // %E = extractvalue { i32, { i32 } } %I, 1 | ||||
10822 | // with | ||||
10823 | // %X = extractvalue { i32, { i32 } } %A, 1 | ||||
10824 | // %E = insertvalue { i32 } %X, i32 42, 0 | ||||
10825 | // by switching the order of the insert and extract (though the | ||||
10826 | // insertvalue should be left in, since it may have other uses). | ||||
10827 | Value *NewEV = InsertNewInstBefore( | ||||
10828 | ExtractValueInst::Create(IV->getAggregateOperand(), | ||||
10829 | EV.idx_begin(), EV.idx_end()), | ||||
10830 | EV); | ||||
10831 | return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(), | ||||
10832 | insi, inse); | ||||
10833 | } | ||||
10834 | if (insi == inse) | ||||
10835 | // The insert list is a prefix of the extract list | ||||
10836 | // We can simply remove the common indices from the extract and make it | ||||
10837 | // operate on the inserted value instead of the insertvalue result. | ||||
10838 | // i.e., replace | ||||
10839 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 | ||||
10840 | // %E = extractvalue { i32, { i32 } } %I, 1, 0 | ||||
10841 | // with | ||||
10842 | // %E extractvalue { i32 } { i32 42 }, 0 | ||||
10843 | return ExtractValueInst::Create(IV->getInsertedValueOperand(), | ||||
10844 | exti, exte); | ||||
10845 | } | ||||
10846 | // Can't simplify extracts from other values. Note that nested extracts are | ||||
10847 | // already simplified implicitely by the above (extract ( extract (insert) ) | ||||
10848 | // will be translated into extract ( insert ( extract ) ) first and then just | ||||
10849 | // the value inserted, if appropriate). | ||||
Matthijs Kooijman | a9012ec | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 10850 | return 0; |
10851 | } | ||||
10852 | |||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10853 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
10854 | /// is to leave as a vector operation. | ||||
10855 | static bool CheapToScalarize(Value *V, bool isConstant) { | ||||
10856 | if (isa<ConstantAggregateZero>(V)) | ||||
10857 | return true; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10858 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10859 | if (isConstant) return true; |
10860 | // If all elts are the same, we can extract. | ||||
10861 | Constant *Op0 = C->getOperand(0); | ||||
10862 | for (unsigned i = 1; i < C->getNumOperands(); ++i) | ||||
10863 | if (C->getOperand(i) != Op0) | ||||
10864 | return false; | ||||
10865 | return true; | ||||
10866 | } | ||||
10867 | Instruction *I = dyn_cast<Instruction>(V); | ||||
10868 | if (!I) return false; | ||||
10869 | |||||
10870 | // Insert element gets simplified to the inserted element or is deleted if | ||||
10871 | // this is constant idx extract element and its a constant idx insertelt. | ||||
10872 | if (I->getOpcode() == Instruction::InsertElement && isConstant && | ||||
10873 | isa<ConstantInt>(I->getOperand(2))) | ||||
10874 | return true; | ||||
10875 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) | ||||
10876 | return true; | ||||
10877 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) | ||||
10878 | if (BO->hasOneUse() && | ||||
10879 | (CheapToScalarize(BO->getOperand(0), isConstant) || | ||||
10880 | CheapToScalarize(BO->getOperand(1), isConstant))) | ||||
10881 | return true; | ||||
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 10882 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
10883 | if (CI->hasOneUse() && | ||||
10884 | (CheapToScalarize(CI->getOperand(0), isConstant) || | ||||
10885 | CheapToScalarize(CI->getOperand(1), isConstant))) | ||||
10886 | return true; | ||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10887 | |
10888 | return false; | ||||
10889 | } | ||||
10890 | |||||
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 10891 | /// Read and decode a shufflevector mask. |
10892 | /// | ||||
10893 | /// It turns undef elements into values that are larger than the number of | ||||
10894 | /// elements in the input. | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10895 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
10896 | unsigned NElts = SVI->getType()->getNumElements(); | ||||
10897 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) | ||||
10898 | return std::vector<unsigned>(NElts, 0); | ||||
10899 | if (isa<UndefValue>(SVI->getOperand(2))) | ||||
10900 | return std::vector<unsigned>(NElts, 2*NElts); | ||||
10901 | |||||
10902 | std::vector<unsigned> Result; | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10903 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10904 | for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) |
10905 | if (isa<UndefValue>(*i)) | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10906 | Result.push_back(NElts*2); // undef -> 8 |
10907 | else | ||||
Gabor Greif | 177dd3f | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 10908 | Result.push_back(cast<ConstantInt>(*i)->getZExtValue()); |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10909 | return Result; |
10910 | } | ||||
10911 | |||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10912 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
10913 | /// value is already around as a register, for example if it were inserted then | ||||
10914 | /// extracted from the vector. | ||||
10915 | static Value *FindScalarElement(Value *V, unsigned EltNo) { | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10916 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
10917 | const VectorType *PTy = cast<VectorType>(V->getType()); | ||||
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10918 | unsigned Width = PTy->getNumElements(); |
10919 | if (EltNo >= Width) // Out of range access. | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10920 | return UndefValue::get(PTy->getElementType()); |
10921 | |||||
10922 | if (isa<UndefValue>(V)) | ||||
10923 | return UndefValue::get(PTy->getElementType()); | ||||
10924 | else if (isa<ConstantAggregateZero>(V)) | ||||
10925 | return Constant::getNullValue(PTy->getElementType()); | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10926 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10927 | return CP->getOperand(EltNo); |
10928 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { | ||||
10929 | // 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] | 10930 | if (!isa<ConstantInt>(III->getOperand(2))) |
10931 | return 0; | ||||
10932 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10933 | |
10934 | // If this is an insert to the element we are looking for, return the | ||||
10935 | // inserted value. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10936 | if (EltNo == IIElt) |
10937 | return III->getOperand(1); | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10938 | |
10939 | // Otherwise, the insertelement doesn't modify the value, recurse on its | ||||
10940 | // vector input. | ||||
10941 | return FindScalarElement(III->getOperand(0), EltNo); | ||||
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 10942 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 10943 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
10944 | if (InEl < Width) | ||||
10945 | return FindScalarElement(SVI->getOperand(0), InEl); | ||||
10946 | else if (InEl < Width*2) | ||||
10947 | return FindScalarElement(SVI->getOperand(1), InEl - Width); | ||||
10948 | else | ||||
10949 | return UndefValue::get(PTy->getElementType()); | ||||
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10950 | } |
10951 | |||||
10952 | // Otherwise, we don't know. | ||||
10953 | return 0; | ||||
10954 | } | ||||
10955 | |||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10956 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10957 | // If vector val is undef, replace extract with scalar undef. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10958 | if (isa<UndefValue>(EI.getOperand(0))) |
10959 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | ||||
10960 | |||||
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 10961 | // If vector val is constant 0, replace extract with scalar 0. |
Chris Lattner | 1f13c88 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 10962 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
10963 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); | ||||
10964 | |||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 10965 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
Matthijs Kooijman | b4d6a5a | 2008-06-11 09:00:12 +0000 | [diff] [blame] | 10966 | // If vector val is constant with all elements the same, replace EI with |
10967 | // that element. When the elements are not identical, we cannot replace yet | ||||
10968 | // (we do that below, but only when the index is constant). | ||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10969 | Constant *op0 = C->getOperand(0); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10970 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10971 | if (C->getOperand(i) != op0) { |
10972 | op0 = 0; | ||||
10973 | break; | ||||
10974 | } | ||||
10975 | if (op0) | ||||
10976 | return ReplaceInstUsesWith(EI, op0); | ||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 10977 | } |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 10978 | |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 10979 | // If extracting a specified index from the vector, see if we can recursively |
10980 | // find a previously computed scalar that was inserted into the vector. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10981 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10982 | unsigned IndexVal = IdxC->getZExtValue(); |
10983 | unsigned VectorWidth = | ||||
10984 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); | ||||
10985 | |||||
10986 | // If this is extracting an invalid index, turn this into undef, to avoid | ||||
10987 | // crashing the code below. | ||||
10988 | if (IndexVal >= VectorWidth) | ||||
10989 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | ||||
10990 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10991 | // This instruction only demands the single element from the input vector. |
10992 | // If the input vector has a single use, simplify it based on this use | ||||
10993 | // property. | ||||
Chris Lattner | 8546409 | 2007-04-09 01:37:55 +0000 | [diff] [blame] | 10994 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10995 | uint64_t UndefElts; |
10996 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 10997 | 1 << IndexVal, |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 10998 | UndefElts)) { |
10999 | EI.setOperand(0, V); | ||||
11000 | return &EI; | ||||
11001 | } | ||||
11002 | } | ||||
11003 | |||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11004 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 11005 | return ReplaceInstUsesWith(EI, Elt); |
Chris Lattner | b7300fa | 2007-04-14 23:02:14 +0000 | [diff] [blame] | 11006 | |
11007 | // If the this extractelement is directly using a bitcast from a vector of | ||||
11008 | // the same number of elements, see if we can find the source element from | ||||
11009 | // it. In this case, we will end up needing to bitcast the scalars. | ||||
11010 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { | ||||
11011 | if (const VectorType *VT = | ||||
11012 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) | ||||
11013 | if (VT->getNumElements() == VectorWidth) | ||||
11014 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) | ||||
11015 | return new BitCastInst(Elt, EI.getType()); | ||||
11016 | } | ||||
Chris Lattner | 389a6f5 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 11017 | } |
Chris Lattner | 6e6b0da | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 11018 | |
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 11019 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11020 | if (I->hasOneUse()) { |
11021 | // Push extractelement into predecessor operation if legal and | ||||
11022 | // profitable to do so | ||||
11023 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { | ||||
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 11024 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
11025 | if (CheapToScalarize(BO, isConstantElt)) { | ||||
11026 | ExtractElementInst *newEI0 = | ||||
11027 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), | ||||
11028 | EI.getName()+".lhs"); | ||||
11029 | ExtractElementInst *newEI1 = | ||||
11030 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), | ||||
11031 | EI.getName()+".rhs"); | ||||
11032 | InsertNewInstBefore(newEI0, EI); | ||||
11033 | InsertNewInstBefore(newEI1, EI); | ||||
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 11034 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
Chris Lattner | 220b0cf | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 11035 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 11036 | } else if (isa<LoadInst>(I)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 11037 | unsigned AS = |
11038 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); | ||||
Chris Lattner | 6d0339d | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 11039 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
11040 | PointerType::get(EI.getType(), AS),EI); | ||||
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 11041 | GetElementPtrInst *GEP = |
11042 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep"); | ||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11043 | InsertNewInstBefore(GEP, EI); |
11044 | return new LoadInst(GEP); | ||||
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 11045 | } |
11046 | } | ||||
11047 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { | ||||
11048 | // Extracting the inserted element? | ||||
11049 | if (IE->getOperand(2) == EI.getOperand(1)) | ||||
11050 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); | ||||
11051 | // If the inserted and extracted elements are constants, they must not | ||||
11052 | // be the same value, extract from the pre-inserted value instead. | ||||
11053 | if (isa<Constant>(IE->getOperand(2)) && | ||||
11054 | isa<Constant>(EI.getOperand(1))) { | ||||
11055 | AddUsesToWorkList(EI); | ||||
11056 | EI.setOperand(0, IE->getOperand(0)); | ||||
11057 | return &EI; | ||||
11058 | } | ||||
11059 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { | ||||
11060 | // If this is extracting an element from a shufflevector, figure out where | ||||
11061 | // it came from and extract from the appropriate input element instead. | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11062 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
11063 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11064 | Value *Src; |
11065 | if (SrcIdx < SVI->getType()->getNumElements()) | ||||
11066 | Src = SVI->getOperand(0); | ||||
11067 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { | ||||
11068 | SrcIdx -= SVI->getType()->getNumElements(); | ||||
11069 | Src = SVI->getOperand(1); | ||||
11070 | } else { | ||||
11071 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | ||||
Chris Lattner | df084ff | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 11072 | } |
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 11073 | return new ExtractElementInst(Src, SrcIdx); |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11074 | } |
11075 | } | ||||
Chris Lattner | 73fa49d | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 11076 | } |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11077 | return 0; |
11078 | } | ||||
11079 | |||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11080 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
11081 | /// elements from either LHS or RHS, return the shuffle mask and true. | ||||
11082 | /// Otherwise, return false. | ||||
11083 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, | ||||
11084 | std::vector<Constant*> &Mask) { | ||||
11085 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && | ||||
11086 | "Invalid CollectSingleShuffleElements"); | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11087 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11088 | |
11089 | if (isa<UndefValue>(V)) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11090 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11091 | return true; |
11092 | } else if (V == LHS) { | ||||
11093 | for (unsigned i = 0; i != NumElts; ++i) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11094 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11095 | return true; |
11096 | } else if (V == RHS) { | ||||
11097 | for (unsigned i = 0; i != NumElts; ++i) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11098 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11099 | return true; |
11100 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { | ||||
11101 | // If this is an insert of an extract from some other vector, include it. | ||||
11102 | Value *VecOp = IEI->getOperand(0); | ||||
11103 | Value *ScalarOp = IEI->getOperand(1); | ||||
11104 | Value *IdxOp = IEI->getOperand(2); | ||||
11105 | |||||
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 11106 | if (!isa<ConstantInt>(IdxOp)) |
11107 | return false; | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11108 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 11109 | |
11110 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. | ||||
11111 | // Okay, we can handle this if the vector we are insertinting into is | ||||
11112 | // transitively ok. | ||||
11113 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { | ||||
11114 | // If so, update the mask to reflect the inserted undef. | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11115 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); |
Chris Lattner | d929f06 | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 11116 | return true; |
11117 | } | ||||
11118 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ | ||||
11119 | if (isa<ConstantInt>(EI->getOperand(1)) && | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11120 | EI->getOperand(0)->getType() == V->getType()) { |
11121 | unsigned ExtractedIdx = | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11122 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11123 | |
11124 | // This must be extracting from either LHS or RHS. | ||||
11125 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { | ||||
11126 | // Okay, we can handle this if the vector we are insertinting into is | ||||
11127 | // transitively ok. | ||||
11128 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { | ||||
11129 | // If so, update the mask to reflect the inserted value. | ||||
11130 | if (EI->getOperand(0) == LHS) { | ||||
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 11131 | Mask[InsertedIdx % NumElts] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11132 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11133 | } else { |
11134 | assert(EI->getOperand(0) == RHS); | ||||
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 11135 | Mask[InsertedIdx % NumElts] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11136 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11137 | |
11138 | } | ||||
11139 | return true; | ||||
11140 | } | ||||
11141 | } | ||||
11142 | } | ||||
11143 | } | ||||
11144 | } | ||||
11145 | // TODO: Handle shufflevector here! | ||||
11146 | |||||
11147 | return false; | ||||
11148 | } | ||||
11149 | |||||
11150 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the | ||||
11151 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask | ||||
11152 | /// that computes V and the LHS value of the shuffle. | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11153 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11154 | Value *&RHS) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11155 | assert(isa<VectorType>(V->getType()) && |
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11156 | (RHS == 0 || V->getType() == RHS->getType()) && |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11157 | "Invalid shuffle!"); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11158 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11159 | |
11160 | if (isa<UndefValue>(V)) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11161 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11162 | return V; |
11163 | } else if (isa<ConstantAggregateZero>(V)) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11164 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11165 | return V; |
11166 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { | ||||
11167 | // If this is an insert of an extract from some other vector, include it. | ||||
11168 | Value *VecOp = IEI->getOperand(0); | ||||
11169 | Value *ScalarOp = IEI->getOperand(1); | ||||
11170 | Value *IdxOp = IEI->getOperand(2); | ||||
11171 | |||||
11172 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { | ||||
11173 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && | ||||
11174 | EI->getOperand(0)->getType() == V->getType()) { | ||||
11175 | unsigned ExtractedIdx = | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11176 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
11177 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11178 | |
11179 | // Either the extracted from or inserted into vector must be RHSVec, | ||||
11180 | // otherwise we'd end up with a shuffle of three inputs. | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11181 | if (EI->getOperand(0) == RHS || RHS == 0) { |
11182 | RHS = EI->getOperand(0); | ||||
11183 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); | ||||
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 11184 | Mask[InsertedIdx % NumElts] = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11185 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11186 | return V; |
11187 | } | ||||
11188 | |||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11189 | if (VecOp == RHS) { |
11190 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11191 | // Everything but the extracted element is replaced with the RHS. |
11192 | for (unsigned i = 0; i != NumElts; ++i) { | ||||
11193 | if (i != InsertedIdx) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11194 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11195 | } |
11196 | return V; | ||||
11197 | } | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11198 | |
11199 | // If this insertelement is a chain that comes from exactly these two | ||||
11200 | // vectors, return the vector and the effective shuffle. | ||||
11201 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) | ||||
11202 | return EI->getOperand(0); | ||||
11203 | |||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11204 | } |
11205 | } | ||||
11206 | } | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11207 | // TODO: Handle shufflevector here! |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11208 | |
11209 | // Otherwise, can't do anything fancy. Return an identity vector. | ||||
11210 | for (unsigned i = 0; i != NumElts; ++i) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11211 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11212 | return V; |
11213 | } | ||||
11214 | |||||
11215 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { | ||||
11216 | Value *VecOp = IE.getOperand(0); | ||||
11217 | Value *ScalarOp = IE.getOperand(1); | ||||
11218 | Value *IdxOp = IE.getOperand(2); | ||||
11219 | |||||
Chris Lattner | 599ded1 | 2007-04-09 01:11:16 +0000 | [diff] [blame] | 11220 | // Inserting an undef or into an undefined place, remove this. |
11221 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) | ||||
11222 | ReplaceInstUsesWith(IE, VecOp); | ||||
11223 | |||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11224 | // If the inserted element was extracted from some other vector, and if the |
11225 | // indexes are constant, try to turn this into a shufflevector operation. | ||||
11226 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { | ||||
11227 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && | ||||
11228 | EI->getOperand(0)->getType() == IE.getType()) { | ||||
11229 | unsigned NumVectorElts = IE.getType()->getNumElements(); | ||||
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 11230 | unsigned ExtractedIdx = |
11231 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); | ||||
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 11232 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11233 | |
11234 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. | ||||
11235 | return ReplaceInstUsesWith(IE, VecOp); | ||||
11236 | |||||
11237 | if (InsertedIdx >= NumVectorElts) // Out of range insert. | ||||
11238 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); | ||||
11239 | |||||
11240 | // If we are extracting a value from a vector, then inserting it right | ||||
11241 | // back into the same place, just use the input vector. | ||||
11242 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) | ||||
11243 | return ReplaceInstUsesWith(IE, VecOp); | ||||
11244 | |||||
11245 | // We could theoretically do this for ANY input. However, doing so could | ||||
11246 | // turn chains of insertelement instructions into a chain of shufflevector | ||||
11247 | // instructions, and right now we do not merge shufflevectors. As such, | ||||
11248 | // only do this in a situation where it is clear that there is benefit. | ||||
11249 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { | ||||
11250 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of | ||||
11251 | // the values of VecOp, except then one read from EIOp0. | ||||
11252 | // Build a new shuffle mask. | ||||
11253 | std::vector<Constant*> Mask; | ||||
11254 | if (isa<UndefValue>(VecOp)) | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11255 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11256 | else { |
11257 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11258 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11259 | NumVectorElts)); |
11260 | } | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11261 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11262 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11263 | ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11264 | } |
11265 | |||||
11266 | // If this insertelement isn't used by some other insertelement, turn it | ||||
11267 | // (and any insertelements it points to), into one big shuffle. | ||||
11268 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { | ||||
11269 | std::vector<Constant*> Mask; | ||||
Chris Lattner | 7f6cc0c | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 11270 | Value *RHS = 0; |
11271 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); | ||||
11272 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); | ||||
11273 | // We now have a shuffle of LHS, RHS, Mask. | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11274 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11275 | } |
11276 | } | ||||
11277 | } | ||||
11278 | |||||
11279 | return 0; | ||||
11280 | } | ||||
11281 | |||||
11282 | |||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11283 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
11284 | Value *LHS = SVI.getOperand(0); | ||||
11285 | Value *RHS = SVI.getOperand(1); | ||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11286 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11287 | |
11288 | bool MadeChange = false; | ||||
11289 | |||||
Chris Lattner | 867b99f | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 11290 | // Undefined shuffle mask -> undefined value. |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11291 | if (isa<UndefValue>(SVI.getOperand(2))) |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11292 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
Dan Gohman | 488fbfc | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 11293 | |
11294 | uint64_t UndefElts; | ||||
11295 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); | ||||
11296 | uint64_t AllOnesEltMask = ~0ULL >> (64-VWidth); | ||||
11297 | if (VWidth <= 64 && | ||||
11298 | SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) | ||||
11299 | MadeChange = true; | ||||
Chris Lattner | efb4735 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 11300 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11301 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
11302 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). | ||||
11303 | if (LHS == RHS || isa<UndefValue>(LHS)) { | ||||
11304 | if (isa<UndefValue>(LHS) && LHS == RHS) { | ||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11305 | // shuffle(undef,undef,mask) -> undef. |
11306 | return ReplaceInstUsesWith(SVI, LHS); | ||||
11307 | } | ||||
11308 | |||||
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11309 | // Remap any references to RHS to use LHS. |
11310 | std::vector<Constant*> Elts; | ||||
11311 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | ||||
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11312 | if (Mask[i] >= 2*e) |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11313 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11314 | else { |
11315 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || | ||||
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 11316 | (Mask[i] < e && isa<UndefValue>(LHS))) { |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11317 | Mask[i] = 2*e; // Turn into undef. |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 11318 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
11319 | } else { | ||||
Mon P Wang | 4f5ca2c | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 11320 | Mask[i] = Mask[i] % e; // Force to LHS. |
Dan Gohman | 4ce9627 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 11321 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); |
11322 | } | ||||
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11323 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11324 | } |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11325 | SVI.setOperand(0, SVI.getOperand(1)); |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11326 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11327 | SVI.setOperand(2, ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11328 | LHS = SVI.getOperand(0); |
11329 | RHS = SVI.getOperand(1); | ||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11330 | MadeChange = true; |
11331 | } | ||||
11332 | |||||
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11333 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11334 | bool isLHSID = true, isRHSID = true; |
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11335 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11336 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
11337 | if (Mask[i] >= e*2) continue; // Ignore undef values. | ||||
11338 | // Is this an identity shuffle of the LHS value? | ||||
11339 | isLHSID &= (Mask[i] == i); | ||||
11340 | |||||
11341 | // Is this an identity shuffle of the RHS value? | ||||
11342 | isRHSID &= (Mask[i]-e == i); | ||||
Chris Lattner | 706126d | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 11343 | } |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11344 | |
Chris Lattner | 863bcff | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 11345 | // Eliminate identity shuffles. |
11346 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); | ||||
11347 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); | ||||
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11348 | |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11349 | // If the LHS is a shufflevector itself, see if we can combine it with this |
11350 | // one without producing an unusual shuffle. Here we are really conservative: | ||||
11351 | // we are absolutely afraid of producing a shuffle mask not in the input | ||||
11352 | // program, because the code gen may not be smart enough to turn a merged | ||||
11353 | // shuffle into two specific shuffles: it may produce worse code. As such, | ||||
11354 | // we only merge two shuffles if the result is one of the two input shuffle | ||||
11355 | // masks. In this case, merging the shuffles just removes one instruction, | ||||
11356 | // which we know is safe. This is good for things like turning: | ||||
11357 | // (splat(splat)) -> splat. | ||||
11358 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { | ||||
11359 | if (isa<UndefValue>(RHS)) { | ||||
11360 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); | ||||
11361 | |||||
11362 | std::vector<unsigned> NewMask; | ||||
11363 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) | ||||
11364 | if (Mask[i] >= 2*e) | ||||
11365 | NewMask.push_back(2*e); | ||||
11366 | else | ||||
11367 | NewMask.push_back(LHSMask[Mask[i]]); | ||||
11368 | |||||
11369 | // If the result mask is equal to the src shuffle or this shuffle mask, do | ||||
11370 | // the replacement. | ||||
11371 | if (NewMask == LHSMask || NewMask == Mask) { | ||||
11372 | std::vector<Constant*> Elts; | ||||
11373 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { | ||||
11374 | if (NewMask[i] >= e*2) { | ||||
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11375 | Elts.push_back(UndefValue::get(Type::Int32Ty)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11376 | } else { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 11377 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11378 | } |
11379 | } | ||||
11380 | return new ShuffleVectorInst(LHSSVI->getOperand(0), | ||||
11381 | LHSSVI->getOperand(1), | ||||
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 11382 | ConstantVector::get(Elts)); |
Chris Lattner | 7b2e2792 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 11383 | } |
11384 | } | ||||
11385 | } | ||||
Chris Lattner | c5eff44 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 11386 | |
Chris Lattner | a844fc4c | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 11387 | return MadeChange ? &SVI : 0; |
11388 | } | ||||
11389 | |||||
11390 | |||||
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11391 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11392 | |
11393 | /// TryToSinkInstruction - Try to move the specified instruction from its | ||||
11394 | /// current block into the beginning of DestBlock, which can only happen if it's | ||||
11395 | /// safe to move the instruction past all of the instructions between it and the | ||||
11396 | /// end of its block. | ||||
11397 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { | ||||
11398 | assert(I->hasOneUse() && "Invariants didn't hold!"); | ||||
11399 | |||||
Chris Lattner | 108e902 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 11400 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
Chris Lattner | bfc538c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 11401 | if (isa<PHINode>(I) || I->mayWriteToMemory() || isa<TerminatorInst>(I)) |
11402 | return false; | ||||
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11403 | |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11404 | // Do not sink alloca instructions out of the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 11405 | if (isa<AllocaInst>(I) && I->getParent() == |
11406 | &DestBlock->getParent()->getEntryBlock()) | ||||
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11407 | return false; |
11408 | |||||
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11409 | // We can only sink load instructions if there is nothing between the load and |
11410 | // the end of block that could change the value. | ||||
Chris Lattner | 2539e33 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 11411 | if (I->mayReadFromMemory()) { |
11412 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); | ||||
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11413 | Scan != E; ++Scan) |
11414 | if (Scan->mayWriteToMemory()) | ||||
11415 | return false; | ||||
Chris Lattner | 96a52a6 | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 11416 | } |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11417 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 11418 | BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11419 | |
Chris Lattner | 4bc5f80 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 11420 | I->moveBefore(InsertPos); |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11421 | ++NumSunkInst; |
11422 | return true; | ||||
11423 | } | ||||
11424 | |||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11425 | |
11426 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding | ||||
11427 | /// all reachable code to the worklist. | ||||
11428 | /// | ||||
11429 | /// This has a couple of tricks to make the code faster and more powerful. In | ||||
11430 | /// particular, we constant fold and DCE instructions as we go, to avoid adding | ||||
11431 | /// them to the worklist (this significantly speeds up instcombine on code where | ||||
11432 | /// many instructions are dead or constant). Additionally, if we find a branch | ||||
11433 | /// whose condition is a known constant, we only visit the reachable successors. | ||||
11434 | /// | ||||
11435 | static void AddReachableCodeToWorklist(BasicBlock *BB, | ||||
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11436 | SmallPtrSet<BasicBlock*, 64> &Visited, |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11437 | InstCombiner &IC, |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11438 | const TargetData *TD) { |
Chris Lattner | 2806dff | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 11439 | SmallVector<BasicBlock*, 256> Worklist; |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11440 | Worklist.push_back(BB); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11441 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11442 | while (!Worklist.empty()) { |
11443 | BB = Worklist.back(); | ||||
11444 | Worklist.pop_back(); | ||||
11445 | |||||
11446 | // We have now visited this block! If we've already been here, ignore it. | ||||
11447 | if (!Visited.insert(BB)) continue; | ||||
11448 | |||||
11449 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { | ||||
11450 | Instruction *Inst = BBI++; | ||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11451 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11452 | // DCE instruction if trivially dead. |
11453 | if (isInstructionTriviallyDead(Inst)) { | ||||
11454 | ++NumDeadInst; | ||||
11455 | DOUT << "IC: DCE: " << *Inst; | ||||
11456 | Inst->eraseFromParent(); | ||||
11457 | continue; | ||||
11458 | } | ||||
11459 | |||||
11460 | // ConstantProp instruction if trivially constant. | ||||
11461 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { | ||||
11462 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; | ||||
11463 | Inst->replaceAllUsesWith(C); | ||||
11464 | ++NumConstProp; | ||||
11465 | Inst->eraseFromParent(); | ||||
11466 | continue; | ||||
11467 | } | ||||
Chris Lattner | 3ccc6bc | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 11468 | |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11469 | IC.AddToWorkList(Inst); |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11470 | } |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11471 | |
11472 | // Recursively visit successors. If this is a branch or switch on a | ||||
11473 | // constant, only visit the reachable successor. | ||||
11474 | TerminatorInst *TI = BB->getTerminator(); | ||||
11475 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { | ||||
11476 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { | ||||
11477 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); | ||||
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11478 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11479 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11480 | continue; |
11481 | } | ||||
11482 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { | ||||
11483 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { | ||||
11484 | // See if this is an explicit destination. | ||||
11485 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) | ||||
11486 | if (SI->getCaseValue(i) == Cond) { | ||||
Nick Lewycky | 9143699 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 11487 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 11488 | Worklist.push_back(ReachableBB); |
Chris Lattner | 2c7718a | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 11489 | continue; |
11490 | } | ||||
11491 | |||||
11492 | // Otherwise it is the default destination. | ||||
11493 | Worklist.push_back(SI->getSuccessor(0)); | ||||
11494 | continue; | ||||
11495 | } | ||||
11496 | } | ||||
11497 | |||||
11498 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) | ||||
11499 | Worklist.push_back(TI->getSuccessor(i)); | ||||
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11500 | } |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11501 | } |
11502 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11503 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11504 | bool Changed = false; |
Chris Lattner | bc61e66 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 11505 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11506 | |
11507 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " | ||||
11508 | << F.getNameStr() << "\n"); | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11509 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11510 | { |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11511 | // Do a depth-first traversal of the function, populate the worklist with |
11512 | // the reachable instructions. Ignore blocks that are not reachable. Keep | ||||
11513 | // track of which blocks we visit. | ||||
Chris Lattner | 1f87a58 | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 11514 | SmallPtrSet<BasicBlock*, 64> Visited; |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11515 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 11516 | |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11517 | // Do a quick scan over the function. If we find any blocks that are |
11518 | // unreachable, remove any instructions inside of them. This prevents | ||||
11519 | // the instcombine code from having to deal with some bad special cases. | ||||
11520 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) | ||||
11521 | if (!Visited.count(BB)) { | ||||
11522 | Instruction *Term = BB->getTerminator(); | ||||
11523 | while (Term != BB->begin()) { // Remove instrs bottom-up | ||||
11524 | BasicBlock::iterator I = Term; --I; | ||||
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 11525 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11526 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | b3d5970 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 11527 | ++NumDeadInst; |
11528 | |||||
11529 | if (!I->use_empty()) | ||||
11530 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | ||||
11531 | I->eraseFromParent(); | ||||
11532 | } | ||||
11533 | } | ||||
11534 | } | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11535 | |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11536 | while (!Worklist.empty()) { |
11537 | Instruction *I = RemoveOneFromWorkList(); | ||||
11538 | if (I == 0) continue; // skip null values. | ||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11539 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11540 | // Check to see if we can DCE the instruction. |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11541 | if (isInstructionTriviallyDead(I)) { |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11542 | // Add operands to the worklist. |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11543 | if (I->getNumOperands() < 4) |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11544 | AddUsesToWorkList(*I); |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11545 | ++NumDeadInst; |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11546 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11547 | DOUT << "IC: DCE: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11548 | |
11549 | I->eraseFromParent(); | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11550 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11551 | continue; |
11552 | } | ||||
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11553 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11554 | // Instruction isn't dead, see if we can constant propagate it. |
Chris Lattner | 0a19ffa | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 11555 | if (Constant *C = ConstantFoldInstruction(I, TD)) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11556 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
Chris Lattner | ad5fec1 | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 11557 | |
Chris Lattner | 8c8c66a | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 11558 | // Add operands to the worklist. |
Chris Lattner | 7bcc0e7 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 11559 | AddUsesToWorkList(*I); |
Chris Lattner | c736d56 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 11560 | ReplaceInstUsesWith(*I, C); |
11561 | |||||
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11562 | ++NumConstProp; |
Chris Lattner | f4f5a77 | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 11563 | I->eraseFromParent(); |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11564 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11565 | continue; |
Chris Lattner | 62b14df | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11566 | } |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11567 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 11568 | if (TD && I->getType()->getTypeID() == Type::VoidTyID) { |
11569 | // See if we can constant fold its operands. | ||||
11570 | for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) { | ||||
11571 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i)) { | ||||
11572 | if (Constant *NewC = ConstantFoldConstantExpression(CE, TD)) | ||||
11573 | i->set(NewC); | ||||
11574 | } | ||||
11575 | } | ||||
11576 | } | ||||
11577 | |||||
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11578 | // See if we can trivially sink this instruction to a successor basic block. |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 11579 | if (I->hasOneUse()) { |
Chris Lattner | ea1c454 | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 11580 | BasicBlock *BB = I->getParent(); |
11581 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); | ||||
11582 | if (UserParent != BB) { | ||||
11583 | bool UserIsSuccessor = false; | ||||
11584 | // See if the user is one of our successors. | ||||
11585 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) | ||||
11586 | if (*SI == UserParent) { | ||||
11587 | UserIsSuccessor = true; | ||||
11588 | break; | ||||
11589 | } | ||||
11590 | |||||
11591 | // If the user is one of our immediate successors, and if that successor | ||||
11592 | // only has us as a predecessors (we'd have to split the critical edge | ||||
11593 | // otherwise), we can keep going. | ||||
11594 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && | ||||
11595 | next(pred_begin(UserParent)) == pred_end(UserParent)) | ||||
11596 | // Okay, the CFG is simple enough, try to sink this instruction. | ||||
11597 | Changed |= TryToSinkInstruction(I, UserParent); | ||||
11598 | } | ||||
11599 | } | ||||
11600 | |||||
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11601 | // Now that we have an instruction, try combining it to simplify it... |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11602 | #ifndef NDEBUG |
11603 | std::string OrigI; | ||||
11604 | #endif | ||||
11605 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); | ||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11606 | if (Instruction *Result = visit(*I)) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 11607 | ++NumCombined; |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11608 | // Should we replace the old instruction with a new one? |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11609 | if (Result != I) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 11610 | DOUT << "IC: Old = " << *I |
11611 | << " New = " << *Result; | ||||
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11612 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11613 | // Everything uses the new instruction now. |
11614 | I->replaceAllUsesWith(Result); | ||||
11615 | |||||
11616 | // Push the new instruction and any users onto the worklist. | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11617 | AddToWorkList(Result); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11618 | AddUsersToWorkList(*Result); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11619 | |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 11620 | // Move the name to the new instruction first. |
11621 | Result->takeName(I); | ||||
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11622 | |
11623 | // Insert the new instruction into the basic block... | ||||
11624 | BasicBlock *InstParent = I->getParent(); | ||||
Chris Lattner | bac3286 | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 11625 | BasicBlock::iterator InsertPos = I; |
11626 | |||||
11627 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert | ||||
11628 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. | ||||
11629 | ++InsertPos; | ||||
11630 | |||||
11631 | InstParent->getInstList().insert(InsertPos, Result); | ||||
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11632 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11633 | // Make sure that we reprocess all operands now that we reduced their |
11634 | // use counts. | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11635 | AddUsesToWorkList(*I); |
Chris Lattner | 216d4d8 | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 11636 | |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11637 | // Instructions can end up on the worklist more than once. Make sure |
11638 | // we do not process an instruction that has been deleted. | ||||
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11639 | RemoveFromWorkList(I); |
Chris Lattner | 4bb7c02 | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 11640 | |
11641 | // Erase the old instruction. | ||||
11642 | InstParent->getInstList().erase(I); | ||||
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 11643 | } else { |
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11644 | #ifndef NDEBUG |
Reid Spencer | a9b8101 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 11645 | DOUT << "IC: Mod = " << OrigI |
11646 | << " New = " << *I; | ||||
Evan Cheng | c7baf68 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 11647 | #endif |
Chris Lattner | 0cea42a | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 11648 | |
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11649 | // If the instruction was modified, it's possible that it is now dead. |
11650 | // if so, remove it. | ||||
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11651 | if (isInstructionTriviallyDead(I)) { |
11652 | // Make sure we process all operands now that we are reducing their | ||||
11653 | // use counts. | ||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11654 | AddUsesToWorkList(*I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 11655 | |
Chris Lattner | 00d5131 | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 11656 | // Instructions may end up in the worklist more than once. Erase all |
Robert Bocchino | 1d7456d | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 11657 | // occurrences of this instruction. |
Chris Lattner | dbab386 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 11658 | RemoveFromWorkList(I); |
Chris Lattner | 2f503e6 | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 11659 | I->eraseFromParent(); |
Chris Lattner | f523d06 | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 11660 | } else { |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11661 | AddToWorkList(I); |
11662 | AddUsersToWorkList(*I); | ||||
Chris Lattner | 90ac28c | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 11663 | } |
Chris Lattner | b3bc8fa | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 11664 | } |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11665 | Changed = true; |
Chris Lattner | 8a2a311 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 11666 | } |
11667 | } | ||||
11668 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11669 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
Chris Lattner | a9ff5eb | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 11670 | |
11671 | // Do an explicit clear, this shrinks the map if needed. | ||||
11672 | WorklistMap.clear(); | ||||
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11673 | return Changed; |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11674 | } |
11675 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11676 | |
11677 | bool InstCombiner::runOnFunction(Function &F) { | ||||
Chris Lattner | f964f32 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 11678 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
11679 | |||||
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11680 | bool EverMadeChange = false; |
11681 | |||||
11682 | // Iterate while there is work to do. | ||||
11683 | unsigned Iteration = 0; | ||||
Bill Wendling | a6c3112 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 11684 | while (DoOneIteration(F, Iteration++)) |
Chris Lattner | ec9c358 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 11685 | EverMadeChange = true; |
11686 | return EverMadeChange; | ||||
11687 | } | ||||
11688 | |||||
Brian Gaeke | 96d4bf7 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 11689 | FunctionPass *llvm::createInstructionCombiningPass() { |
Chris Lattner | dd841ae | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 11690 | return new InstCombiner(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 11691 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 11692 | |
Chris Lattner | b8cd4d3 | 2008-08-11 22:06:05 +0000 | [diff] [blame] | 11693 |