blob: d387864c26ab782184e7e711e54eda38d0208538 [file] [log] [blame]
Chris Lattner0a8191e2010-01-05 07:50:36 +00001//===- InstCombineAndOrXor.cpp --------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the visitAnd, visitOr, and visitXor functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "InstCombine.h"
Chris Lattner0a8191e2010-01-05 07:50:36 +000015#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Intrinsics.h"
Anders Carlssonda80afe2011-03-01 15:05:01 +000017#include "llvm/Support/ConstantRange.h"
Chris Lattner0a8191e2010-01-05 07:50:36 +000018#include "llvm/Support/PatternMatch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Transforms/Utils/CmpInstAnalysis.h"
Chris Lattner0a8191e2010-01-05 07:50:36 +000020using namespace llvm;
21using namespace PatternMatch;
22
23
24/// AddOne - Add one to a ConstantInt.
Jakub Staszak2ef36b62013-03-09 11:18:59 +000025static Constant *AddOne(ConstantInt *C) {
26 return ConstantInt::get(C->getContext(), C->getValue() + 1);
Chris Lattner0a8191e2010-01-05 07:50:36 +000027}
28/// SubOne - Subtract one from a ConstantInt.
29static Constant *SubOne(ConstantInt *C) {
30 return ConstantInt::get(C->getContext(), C->getValue()-1);
31}
32
33/// isFreeToInvert - Return true if the specified value is free to invert (apply
34/// ~ to). This happens in cases where the ~ can be eliminated.
35static inline bool isFreeToInvert(Value *V) {
36 // ~(~(X)) -> X.
37 if (BinaryOperator::isNot(V))
38 return true;
Craig Topper9d4171a2012-12-20 07:09:41 +000039
Chris Lattner0a8191e2010-01-05 07:50:36 +000040 // Constants can be considered to be not'ed values.
41 if (isa<ConstantInt>(V))
42 return true;
Craig Topper9d4171a2012-12-20 07:09:41 +000043
Chris Lattner0a8191e2010-01-05 07:50:36 +000044 // Compares can be inverted if they have a single use.
45 if (CmpInst *CI = dyn_cast<CmpInst>(V))
46 return CI->hasOneUse();
Craig Topper9d4171a2012-12-20 07:09:41 +000047
Chris Lattner0a8191e2010-01-05 07:50:36 +000048 return false;
49}
50
51static inline Value *dyn_castNotVal(Value *V) {
52 // If this is not(not(x)) don't return that this is a not: we want the two
53 // not's to be folded first.
54 if (BinaryOperator::isNot(V)) {
55 Value *Operand = BinaryOperator::getNotArgument(V);
56 if (!isFreeToInvert(Operand))
57 return Operand;
58 }
Craig Topper9d4171a2012-12-20 07:09:41 +000059
Chris Lattner0a8191e2010-01-05 07:50:36 +000060 // Constants can be considered to be not'ed values...
61 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
62 return ConstantInt::get(C->getType(), ~C->getValue());
63 return 0;
64}
65
Chris Lattner0a8191e2010-01-05 07:50:36 +000066/// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
67/// predicate into a three bit mask. It also returns whether it is an ordered
68/// predicate by reference.
69static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
70 isOrdered = false;
71 switch (CC) {
72 case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000
73 case FCmpInst::FCMP_UNO: return 0; // 000
74 case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001
75 case FCmpInst::FCMP_UGT: return 1; // 001
76 case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010
77 case FCmpInst::FCMP_UEQ: return 2; // 010
78 case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011
79 case FCmpInst::FCMP_UGE: return 3; // 011
80 case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100
81 case FCmpInst::FCMP_ULT: return 4; // 100
82 case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101
83 case FCmpInst::FCMP_UNE: return 5; // 101
84 case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110
85 case FCmpInst::FCMP_ULE: return 6; // 110
86 // True -> 7
87 default:
88 // Not expecting FCMP_FALSE and FCMP_TRUE;
89 llvm_unreachable("Unexpected FCmp predicate!");
Chris Lattner0a8191e2010-01-05 07:50:36 +000090 }
91}
92
Benjamin Kramerbaba1aa2012-02-06 11:28:19 +000093/// getNewICmpValue - This is the complement of getICmpCode, which turns an
Craig Topper9d4171a2012-12-20 07:09:41 +000094/// opcode and two operands into either a constant true or false, or a brand
Chris Lattner0a8191e2010-01-05 07:50:36 +000095/// new ICmp instruction. The sign is passed in to determine which kind
96/// of predicate to use in the new icmp instruction.
Benjamin Kramerbaba1aa2012-02-06 11:28:19 +000097static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
98 InstCombiner::BuilderTy *Builder) {
Pete Cooperebf98c12011-12-17 01:20:32 +000099 ICmpInst::Predicate NewPred;
100 if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred))
101 return NewConstant;
102 return Builder->CreateICmp(NewPred, LHS, RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000103}
104
105/// getFCmpValue - This is the complement of getFCmpCode, which turns an
106/// opcode and two operands into either a FCmp instruction. isordered is passed
107/// in to determine which kind of predicate to use in the new fcmp instruction.
108static Value *getFCmpValue(bool isordered, unsigned code,
Chris Lattner067459c2010-03-05 08:46:26 +0000109 Value *LHS, Value *RHS,
110 InstCombiner::BuilderTy *Builder) {
Chris Lattner343d2e42010-03-05 07:47:57 +0000111 CmpInst::Predicate Pred;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000112 switch (code) {
Craig Toppera2886c22012-02-07 05:05:23 +0000113 default: llvm_unreachable("Illegal FCmp code!");
Chris Lattner343d2e42010-03-05 07:47:57 +0000114 case 0: Pred = isordered ? FCmpInst::FCMP_ORD : FCmpInst::FCMP_UNO; break;
115 case 1: Pred = isordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; break;
116 case 2: Pred = isordered ? FCmpInst::FCMP_OEQ : FCmpInst::FCMP_UEQ; break;
117 case 3: Pred = isordered ? FCmpInst::FCMP_OGE : FCmpInst::FCMP_UGE; break;
118 case 4: Pred = isordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; break;
119 case 5: Pred = isordered ? FCmpInst::FCMP_ONE : FCmpInst::FCMP_UNE; break;
120 case 6: Pred = isordered ? FCmpInst::FCMP_OLE : FCmpInst::FCMP_ULE; break;
Craig Topper9d4171a2012-12-20 07:09:41 +0000121 case 7:
Owen Andersona8342002011-01-21 19:39:42 +0000122 if (!isordered) return ConstantInt::getTrue(LHS->getContext());
123 Pred = FCmpInst::FCMP_ORD; break;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000124 }
Chris Lattner067459c2010-03-05 08:46:26 +0000125 return Builder->CreateFCmp(Pred, LHS, RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000126}
127
Chris Lattner0a8191e2010-01-05 07:50:36 +0000128// OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where
129// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is
130// guaranteed to be a binary operator.
131Instruction *InstCombiner::OptAndOp(Instruction *Op,
132 ConstantInt *OpRHS,
133 ConstantInt *AndRHS,
134 BinaryOperator &TheAnd) {
135 Value *X = Op->getOperand(0);
136 Constant *Together = 0;
137 if (!Op->isShift())
138 Together = ConstantExpr::getAnd(AndRHS, OpRHS);
139
140 switch (Op->getOpcode()) {
141 case Instruction::Xor:
142 if (Op->hasOneUse()) {
143 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
144 Value *And = Builder->CreateAnd(X, AndRHS);
145 And->takeName(Op);
146 return BinaryOperator::CreateXor(And, Together);
147 }
148 break;
149 case Instruction::Or:
Owen Andersonc237a842010-09-13 17:59:27 +0000150 if (Op->hasOneUse()){
151 if (Together != OpRHS) {
152 // (X | C1) & C2 --> (X | (C1&C2)) & C2
153 Value *Or = Builder->CreateOr(X, Together);
154 Or->takeName(Op);
155 return BinaryOperator::CreateAnd(Or, AndRHS);
156 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000157
Owen Andersonc237a842010-09-13 17:59:27 +0000158 ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
159 if (TogetherCI && !TogetherCI->isZero()){
160 // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
161 // NOTE: This reduces the number of bits set in the & mask, which
162 // can expose opportunities for store narrowing.
163 Together = ConstantExpr::getXor(AndRHS, Together);
164 Value *And = Builder->CreateAnd(X, Together);
165 And->takeName(Op);
166 return BinaryOperator::CreateOr(And, OpRHS);
167 }
Chris Lattner0a8191e2010-01-05 07:50:36 +0000168 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000169
Chris Lattner0a8191e2010-01-05 07:50:36 +0000170 break;
171 case Instruction::Add:
172 if (Op->hasOneUse()) {
173 // Adding a one to a single bit bit-field should be turned into an XOR
174 // of the bit. First thing to check is to see if this AND is with a
175 // single bit constant.
176 const APInt &AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
177
178 // If there is only one bit set.
179 if (AndRHSV.isPowerOf2()) {
180 // Ok, at this point, we know that we are masking the result of the
181 // ADD down to exactly one bit. If the constant we are adding has
182 // no bits set below this bit, then we can eliminate the ADD.
183 const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
184
185 // Check to see if any bits below the one bit set in AndRHSV are set.
186 if ((AddRHS & (AndRHSV-1)) == 0) {
187 // If not, the only thing that can effect the output of the AND is
188 // the bit specified by AndRHSV. If that bit is set, the effect of
189 // the XOR is to toggle the bit. If it is clear, then the ADD has
190 // no effect.
191 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
192 TheAnd.setOperand(0, X);
193 return &TheAnd;
194 } else {
195 // Pull the XOR out of the AND.
196 Value *NewAnd = Builder->CreateAnd(X, AndRHS);
197 NewAnd->takeName(Op);
198 return BinaryOperator::CreateXor(NewAnd, AndRHS);
199 }
200 }
201 }
202 }
203 break;
204
205 case Instruction::Shl: {
206 // We know that the AND will not produce any of the bits shifted in, so if
207 // the anded constant includes them, clear them now!
208 //
209 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
210 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
211 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
212 ConstantInt *CI = ConstantInt::get(AndRHS->getContext(),
213 AndRHS->getValue() & ShlMask);
214
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000215 if (CI->getValue() == ShlMask)
216 // Masking out bits that the shift already masks.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000217 return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
Craig Topper9d4171a2012-12-20 07:09:41 +0000218
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000219 if (CI != AndRHS) { // Reducing bits set in and.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000220 TheAnd.setOperand(1, CI);
221 return &TheAnd;
222 }
223 break;
224 }
225 case Instruction::LShr: {
226 // We know that the AND will not produce any of the bits shifted in, so if
227 // the anded constant includes them, clear them now! This only applies to
228 // unsigned shifts, because a signed shr may bring in set bits!
229 //
230 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
231 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
232 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
233 ConstantInt *CI = ConstantInt::get(Op->getContext(),
234 AndRHS->getValue() & ShrMask);
235
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000236 if (CI->getValue() == ShrMask)
237 // Masking out bits that the shift already masks.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000238 return ReplaceInstUsesWith(TheAnd, Op);
Craig Topper9d4171a2012-12-20 07:09:41 +0000239
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000240 if (CI != AndRHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000241 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
242 return &TheAnd;
243 }
244 break;
245 }
246 case Instruction::AShr:
247 // Signed shr.
248 // See if this is shifting in some sign extension, then masking it out
249 // with an and.
250 if (Op->hasOneUse()) {
251 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
252 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
253 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
254 Constant *C = ConstantInt::get(Op->getContext(),
255 AndRHS->getValue() & ShrMask);
256 if (C == AndRHS) { // Masking out bits shifted in.
257 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
258 // Make the argument unsigned.
259 Value *ShVal = Op->getOperand(0);
260 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
261 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
262 }
263 }
264 break;
265 }
266 return 0;
267}
268
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000269/// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
270/// (V < Lo || V >= Hi). In practice, we emit the more efficient
NAKAMURA Takumi00d2a102012-11-15 00:35:50 +0000271/// (V-Lo) \<u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates
Chris Lattner0a8191e2010-01-05 07:50:36 +0000272/// whether to treat the V, Lo and HI as signed or not. IB is the location to
273/// insert new instructions.
Chris Lattner067459c2010-03-05 08:46:26 +0000274Value *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
275 bool isSigned, bool Inside) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000276 assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
Chris Lattner0a8191e2010-01-05 07:50:36 +0000277 ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
278 "Lo is not <= Hi in range emission code!");
Craig Topper9d4171a2012-12-20 07:09:41 +0000279
Chris Lattner0a8191e2010-01-05 07:50:36 +0000280 if (Inside) {
281 if (Lo == Hi) // Trivially false.
Chris Lattner067459c2010-03-05 08:46:26 +0000282 return ConstantInt::getFalse(V->getContext());
Chris Lattner0a8191e2010-01-05 07:50:36 +0000283
284 // V >= Min && V < Hi --> V < Hi
285 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000286 ICmpInst::Predicate pred = (isSigned ?
Chris Lattner0a8191e2010-01-05 07:50:36 +0000287 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
Chris Lattner067459c2010-03-05 08:46:26 +0000288 return Builder->CreateICmp(pred, V, Hi);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000289 }
290
291 // Emit V-Lo <u Hi-Lo
292 Constant *NegLo = ConstantExpr::getNeg(Lo);
293 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
294 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
Chris Lattner067459c2010-03-05 08:46:26 +0000295 return Builder->CreateICmpULT(Add, UpperBound);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000296 }
297
298 if (Lo == Hi) // Trivially true.
Chris Lattner067459c2010-03-05 08:46:26 +0000299 return ConstantInt::getTrue(V->getContext());
Chris Lattner0a8191e2010-01-05 07:50:36 +0000300
301 // V < Min || V >= Hi -> V > Hi-1
302 Hi = SubOne(cast<ConstantInt>(Hi));
303 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000304 ICmpInst::Predicate pred = (isSigned ?
Chris Lattner0a8191e2010-01-05 07:50:36 +0000305 ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
Chris Lattner067459c2010-03-05 08:46:26 +0000306 return Builder->CreateICmp(pred, V, Hi);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000307 }
308
309 // Emit V-Lo >u Hi-1-Lo
310 // Note that Hi has already had one subtracted from it, above.
311 ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
312 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
313 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
Chris Lattner067459c2010-03-05 08:46:26 +0000314 return Builder->CreateICmpUGT(Add, LowerBound);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000315}
316
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000317// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
Chris Lattner0a8191e2010-01-05 07:50:36 +0000318// any number of 0s on either side. The 1s are allowed to wrap from LSB to
319// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
320// not, since all 1s are not contiguous.
321static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
322 const APInt& V = Val->getValue();
323 uint32_t BitWidth = Val->getType()->getBitWidth();
324 if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
325
326 // look for the first zero bit after the run of ones
327 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
328 // look for the first non-zero bit
Craig Topper9d4171a2012-12-20 07:09:41 +0000329 ME = V.getActiveBits();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000330 return true;
331}
332
333/// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
334/// where isSub determines whether the operator is a sub. If we can fold one of
335/// the following xforms:
Craig Topper9d4171a2012-12-20 07:09:41 +0000336///
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000337/// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
338/// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
339/// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +0000340///
341/// return (A +/- B).
342///
343Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
344 ConstantInt *Mask, bool isSub,
345 Instruction &I) {
346 Instruction *LHSI = dyn_cast<Instruction>(LHS);
347 if (!LHSI || LHSI->getNumOperands() != 2 ||
348 !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
349
350 ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
351
352 switch (LHSI->getOpcode()) {
353 default: return 0;
354 case Instruction::And:
355 if (ConstantExpr::getAnd(N, Mask) == Mask) {
356 // If the AndRHS is a power of two minus one (0+1+), this is simple.
Craig Topper9d4171a2012-12-20 07:09:41 +0000357 if ((Mask->getValue().countLeadingZeros() +
358 Mask->getValue().countPopulation()) ==
Chris Lattner0a8191e2010-01-05 07:50:36 +0000359 Mask->getValue().getBitWidth())
360 break;
361
362 // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
363 // part, we don't need any explicit masks to take them out of A. If that
364 // is all N is, ignore it.
365 uint32_t MB = 0, ME = 0;
366 if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
367 uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
368 APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
369 if (MaskedValueIsZero(RHS, Mask))
370 break;
371 }
372 }
373 return 0;
374 case Instruction::Or:
375 case Instruction::Xor:
376 // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
Craig Topper9d4171a2012-12-20 07:09:41 +0000377 if ((Mask->getValue().countLeadingZeros() +
Chris Lattner0a8191e2010-01-05 07:50:36 +0000378 Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
379 && ConstantExpr::getAnd(N, Mask)->isNullValue())
380 break;
381 return 0;
382 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000383
Chris Lattner0a8191e2010-01-05 07:50:36 +0000384 if (isSub)
385 return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
386 return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
387}
388
Owen Anderson3fe002d2010-09-08 22:16:17 +0000389/// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C)
Craig Topper9d4171a2012-12-20 07:09:41 +0000390/// One of A and B is considered the mask, the other the value. This is
391/// described as the "AMask" or "BMask" part of the enum. If the enum
Owen Anderson3fe002d2010-09-08 22:16:17 +0000392/// contains only "Mask", then both A and B can be considered masks.
393/// If A is the mask, then it was proven, that (A & C) == C. This
394/// is trivial if C == A, or C == 0. If both A and C are constants, this
395/// proof is also easy.
396/// For the following explanations we assume that A is the mask.
Craig Topper9d4171a2012-12-20 07:09:41 +0000397/// The part "AllOnes" declares, that the comparison is true only
Owen Anderson3fe002d2010-09-08 22:16:17 +0000398/// if (A & B) == A, or all bits of A are set in B.
399/// Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes
Craig Topper9d4171a2012-12-20 07:09:41 +0000400/// The part "AllZeroes" declares, that the comparison is true only
Owen Anderson3fe002d2010-09-08 22:16:17 +0000401/// if (A & B) == 0, or all bits of A are cleared in B.
402/// Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes
Craig Topper9d4171a2012-12-20 07:09:41 +0000403/// The part "Mixed" declares, that (A & B) == C and C might or might not
Owen Anderson3fe002d2010-09-08 22:16:17 +0000404/// contain any number of one bits and zero bits.
405/// Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed
406/// The Part "Not" means, that in above descriptions "==" should be replaced
407/// by "!=".
408/// Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes
409/// If the mask A contains a single bit, then the following is equivalent:
410/// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
411/// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
412enum MaskedICmpType {
413 FoldMskICmp_AMask_AllOnes = 1,
414 FoldMskICmp_AMask_NotAllOnes = 2,
415 FoldMskICmp_BMask_AllOnes = 4,
416 FoldMskICmp_BMask_NotAllOnes = 8,
417 FoldMskICmp_Mask_AllZeroes = 16,
418 FoldMskICmp_Mask_NotAllZeroes = 32,
419 FoldMskICmp_AMask_Mixed = 64,
420 FoldMskICmp_AMask_NotMixed = 128,
421 FoldMskICmp_BMask_Mixed = 256,
422 FoldMskICmp_BMask_NotMixed = 512
423};
424
425/// return the set of pattern classes (from MaskedICmpType)
426/// that (icmp SCC (A & B), C) satisfies
Craig Topper9d4171a2012-12-20 07:09:41 +0000427static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000428 ICmpInst::Predicate SCC)
429{
430 ConstantInt *ACst = dyn_cast<ConstantInt>(A);
431 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
432 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
433 bool icmp_eq = (SCC == ICmpInst::ICMP_EQ);
Craig Topper9d4171a2012-12-20 07:09:41 +0000434 bool icmp_abit = (ACst != 0 && !ACst->isZero() &&
Owen Anderson3fe002d2010-09-08 22:16:17 +0000435 ACst->getValue().isPowerOf2());
Craig Topper9d4171a2012-12-20 07:09:41 +0000436 bool icmp_bbit = (BCst != 0 && !BCst->isZero() &&
Owen Anderson3fe002d2010-09-08 22:16:17 +0000437 BCst->getValue().isPowerOf2());
438 unsigned result = 0;
439 if (CCst != 0 && CCst->isZero()) {
440 // if C is zero, then both A and B qualify as mask
441 result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes |
442 FoldMskICmp_Mask_AllZeroes |
443 FoldMskICmp_AMask_Mixed |
444 FoldMskICmp_BMask_Mixed)
445 : (FoldMskICmp_Mask_NotAllZeroes |
446 FoldMskICmp_Mask_NotAllZeroes |
447 FoldMskICmp_AMask_NotMixed |
448 FoldMskICmp_BMask_NotMixed));
449 if (icmp_abit)
450 result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000451 FoldMskICmp_AMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000452 : (FoldMskICmp_AMask_AllOnes |
453 FoldMskICmp_AMask_Mixed));
454 if (icmp_bbit)
455 result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000456 FoldMskICmp_BMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000457 : (FoldMskICmp_BMask_AllOnes |
458 FoldMskICmp_BMask_Mixed));
459 return result;
460 }
461 if (A == C) {
462 result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes |
463 FoldMskICmp_AMask_Mixed)
464 : (FoldMskICmp_AMask_NotAllOnes |
465 FoldMskICmp_AMask_NotMixed));
466 if (icmp_abit)
467 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
468 FoldMskICmp_AMask_NotMixed)
469 : (FoldMskICmp_Mask_AllZeroes |
470 FoldMskICmp_AMask_Mixed));
Craig Topperae48cb22012-12-20 07:15:54 +0000471 } else if (ACst != 0 && CCst != 0 &&
472 ConstantExpr::getAnd(ACst, CCst) == CCst) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000473 result |= (icmp_eq ? FoldMskICmp_AMask_Mixed
474 : FoldMskICmp_AMask_NotMixed);
475 }
Craig Topperae48cb22012-12-20 07:15:54 +0000476 if (B == C) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000477 result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes |
478 FoldMskICmp_BMask_Mixed)
479 : (FoldMskICmp_BMask_NotAllOnes |
480 FoldMskICmp_BMask_NotMixed));
481 if (icmp_bbit)
482 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000483 FoldMskICmp_BMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000484 : (FoldMskICmp_Mask_AllZeroes |
485 FoldMskICmp_BMask_Mixed));
Craig Topperae48cb22012-12-20 07:15:54 +0000486 } else if (BCst != 0 && CCst != 0 &&
487 ConstantExpr::getAnd(BCst, CCst) == CCst) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000488 result |= (icmp_eq ? FoldMskICmp_BMask_Mixed
489 : FoldMskICmp_BMask_NotMixed);
490 }
491 return result;
492}
493
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000494/// decomposeBitTestICmp - Decompose an icmp into the form ((X & Y) pred Z)
495/// if possible. The returned predicate is either == or !=. Returns false if
496/// decomposition fails.
497static bool decomposeBitTestICmp(const ICmpInst *I, ICmpInst::Predicate &Pred,
498 Value *&X, Value *&Y, Value *&Z) {
499 // X < 0 is equivalent to (X & SignBit) != 0.
500 if (I->getPredicate() == ICmpInst::ICMP_SLT)
501 if (ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
502 if (C->isZero()) {
503 X = I->getOperand(0);
504 Y = ConstantInt::get(I->getContext(),
505 APInt::getSignBit(C->getBitWidth()));
506 Pred = ICmpInst::ICMP_NE;
507 Z = C;
508 return true;
509 }
510
511 // X > -1 is equivalent to (X & SignBit) == 0.
512 if (I->getPredicate() == ICmpInst::ICMP_SGT)
513 if (ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
514 if (C->isAllOnesValue()) {
515 X = I->getOperand(0);
516 Y = ConstantInt::get(I->getContext(),
517 APInt::getSignBit(C->getBitWidth()));
518 Pred = ICmpInst::ICMP_EQ;
519 Z = ConstantInt::getNullValue(C->getType());
520 return true;
521 }
522
523 return false;
524}
525
Owen Anderson3fe002d2010-09-08 22:16:17 +0000526/// foldLogOpOfMaskedICmpsHelper:
527/// handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
528/// return the set of pattern classes (from MaskedICmpType)
529/// that both LHS and RHS satisfy
Craig Topper9d4171a2012-12-20 07:09:41 +0000530static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000531 Value*& B, Value*& C,
532 Value*& D, Value*& E,
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000533 ICmpInst *LHS, ICmpInst *RHS,
534 ICmpInst::Predicate &LHSCC,
535 ICmpInst::Predicate &RHSCC) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000536 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0;
537 // vectors are not (yet?) supported
538 if (LHS->getOperand(0)->getType()->isVectorTy()) return 0;
539
540 // Here comes the tricky part:
Craig Topper9d4171a2012-12-20 07:09:41 +0000541 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000542 // and L11 & L12 == L21 & L22. The same goes for RHS.
543 // Now we must find those components L** and R**, that are equal, so
Craig Topper9d4171a2012-12-20 07:09:41 +0000544 // that we can extract the parameters A, B, C, D, and E for the canonical
Owen Anderson3fe002d2010-09-08 22:16:17 +0000545 // above.
546 Value *L1 = LHS->getOperand(0);
547 Value *L2 = LHS->getOperand(1);
548 Value *L11,*L12,*L21,*L22;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000549 // Check whether the icmp can be decomposed into a bit test.
550 if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) {
551 L21 = L22 = L1 = 0;
552 } else {
553 // Look for ANDs in the LHS icmp.
554 if (match(L1, m_And(m_Value(L11), m_Value(L12)))) {
555 if (!match(L2, m_And(m_Value(L21), m_Value(L22))))
556 L21 = L22 = 0;
557 } else {
558 if (!match(L2, m_And(m_Value(L11), m_Value(L12))))
559 return 0;
560 std::swap(L1, L2);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000561 L21 = L22 = 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000562 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000563 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000564
565 // Bail if LHS was a icmp that can't be decomposed into an equality.
566 if (!ICmpInst::isEquality(LHSCC))
567 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000568
569 Value *R1 = RHS->getOperand(0);
570 Value *R2 = RHS->getOperand(1);
571 Value *R11,*R12;
572 bool ok = false;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000573 if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) {
574 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
575 A = R11; D = R12;
576 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
577 A = R12; D = R11;
578 } else {
579 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000580 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000581 E = R2; R1 = 0; ok = true;
582 } else if (match(R1, m_And(m_Value(R11), m_Value(R12)))) {
583 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
584 A = R11; D = R12; E = R2; ok = true;
585 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000586 A = R12; D = R11; E = R2; ok = true;
587 }
588 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000589
590 // Bail if RHS was a icmp that can't be decomposed into an equality.
591 if (!ICmpInst::isEquality(RHSCC))
592 return 0;
593
594 // Look for ANDs in on the right side of the RHS icmp.
Owen Anderson3fe002d2010-09-08 22:16:17 +0000595 if (!ok && match(R2, m_And(m_Value(R11), m_Value(R12)))) {
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000596 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
597 A = R11; D = R12; E = R1; ok = true;
598 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000599 A = R12; D = R11; E = R1; ok = true;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000600 } else {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000601 return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000602 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000603 }
604 if (!ok)
605 return 0;
606
607 if (L11 == A) {
608 B = L12; C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000609 } else if (L12 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000610 B = L11; C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000611 } else if (L21 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000612 B = L22; C = L1;
Craig Topperae48cb22012-12-20 07:15:54 +0000613 } else if (L22 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000614 B = L21; C = L1;
615 }
616
617 unsigned left_type = getTypeOfMaskedICmp(A, B, C, LHSCC);
618 unsigned right_type = getTypeOfMaskedICmp(A, D, E, RHSCC);
619 return left_type & right_type;
620}
621/// foldLogOpOfMaskedICmps:
622/// try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
623/// into a single (icmp(A & X) ==/!= Y)
624static Value* foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS,
625 ICmpInst::Predicate NEWCC,
626 llvm::InstCombiner::BuilderTy* Builder) {
627 Value *A = 0, *B = 0, *C = 0, *D = 0, *E = 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000628 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
629 unsigned mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS,
630 LHSCC, RHSCC);
Benjamin Kramerf7fe24f2012-01-09 17:36:29 +0000631 if (mask == 0) return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000632 assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) &&
633 "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
Owen Anderson3fe002d2010-09-08 22:16:17 +0000634
635 if (NEWCC == ICmpInst::ICMP_NE)
636 mask >>= 1; // treat "Not"-states as normal states
637
638 if (mask & FoldMskICmp_Mask_AllZeroes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000639 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000640 // -> (icmp eq (A & (B|D)), 0)
641 Value* newOr = Builder->CreateOr(B, D);
642 Value* newAnd = Builder->CreateAnd(A, newOr);
643 // we can't use C as zero, because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000644 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000645 // with B and D, having a single bit set
646 Value* zero = Constant::getNullValue(A->getType());
647 return Builder->CreateICmp(NEWCC, newAnd, zero);
648 }
Craig Topperae48cb22012-12-20 07:15:54 +0000649 if (mask & FoldMskICmp_BMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000650 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000651 // -> (icmp eq (A & (B|D)), (B|D))
652 Value* newOr = Builder->CreateOr(B, D);
653 Value* newAnd = Builder->CreateAnd(A, newOr);
654 return Builder->CreateICmp(NEWCC, newAnd, newOr);
Craig Topper9d4171a2012-12-20 07:09:41 +0000655 }
Craig Topperae48cb22012-12-20 07:15:54 +0000656 if (mask & FoldMskICmp_AMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000657 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000658 // -> (icmp eq (A & (B&D)), A)
659 Value* newAnd1 = Builder->CreateAnd(B, D);
660 Value* newAnd = Builder->CreateAnd(A, newAnd1);
661 return Builder->CreateICmp(NEWCC, newAnd, A);
662 }
Craig Topperae48cb22012-12-20 07:15:54 +0000663 if (mask & FoldMskICmp_BMask_Mixed) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000664 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000665 // We already know that B & C == C && D & E == E.
666 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
667 // C and E, which are shared by both the mask B and the mask D, don't
668 // contradict, then we can transform to
669 // -> (icmp eq (A & (B|D)), (C|E))
670 // Currently, we only handle the case of B, C, D, and E being constant.
671 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
672 if (BCst == 0) return 0;
673 ConstantInt *DCst = dyn_cast<ConstantInt>(D);
674 if (DCst == 0) return 0;
675 // we can't simply use C and E, because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000676 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000677 // with B and D, having a single bit set
678
679 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
680 if (CCst == 0) return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000681 if (LHSCC != NEWCC)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000682 CCst = dyn_cast<ConstantInt>( ConstantExpr::getXor(BCst, CCst) );
683 ConstantInt *ECst = dyn_cast<ConstantInt>(E);
684 if (ECst == 0) return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000685 if (RHSCC != NEWCC)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000686 ECst = dyn_cast<ConstantInt>( ConstantExpr::getXor(DCst, ECst) );
687 ConstantInt* MCst = dyn_cast<ConstantInt>(
688 ConstantExpr::getAnd(ConstantExpr::getAnd(BCst, DCst),
689 ConstantExpr::getXor(CCst, ECst)) );
690 // if there is a conflict we should actually return a false for the
691 // whole construct
692 if (!MCst->isZero())
693 return 0;
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000694 Value *newOr1 = Builder->CreateOr(B, D);
695 Value *newOr2 = ConstantExpr::getOr(CCst, ECst);
696 Value *newAnd = Builder->CreateAnd(A, newOr1);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000697 return Builder->CreateICmp(NEWCC, newAnd, newOr2);
698 }
699 return 0;
700}
701
Chris Lattner0a8191e2010-01-05 07:50:36 +0000702/// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
Chris Lattner067459c2010-03-05 08:46:26 +0000703Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000704 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
705
706 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
707 if (PredicatesFoldable(LHSCC, RHSCC)) {
708 if (LHS->getOperand(0) == RHS->getOperand(1) &&
709 LHS->getOperand(1) == RHS->getOperand(0))
710 LHS->swapOperands();
711 if (LHS->getOperand(0) == RHS->getOperand(0) &&
712 LHS->getOperand(1) == RHS->getOperand(1)) {
713 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
714 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
715 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +0000716 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000717 }
718 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000719
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000720 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
721 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, ICmpInst::ICMP_EQ, Builder))
722 return V;
Craig Topper9d4171a2012-12-20 07:09:41 +0000723
Chris Lattner0a8191e2010-01-05 07:50:36 +0000724 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
725 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
726 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
727 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
728 if (LHSCst == 0 || RHSCst == 0) return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +0000729
Chris Lattner0a8191e2010-01-05 07:50:36 +0000730 if (LHSCst == RHSCst && LHSCC == RHSCC) {
731 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
732 // where C is a power of 2
733 if (LHSCC == ICmpInst::ICMP_ULT &&
734 LHSCst->getValue().isPowerOf2()) {
735 Value *NewOr = Builder->CreateOr(Val, Val2);
Chris Lattner067459c2010-03-05 08:46:26 +0000736 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000737 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000738
Chris Lattner0a8191e2010-01-05 07:50:36 +0000739 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
740 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
741 Value *NewOr = Builder->CreateOr(Val, Val2);
Chris Lattner067459c2010-03-05 08:46:26 +0000742 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000743 }
744 }
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000745
Benjamin Kramer101720f2011-04-28 20:09:57 +0000746 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000747 // where CMAX is the all ones value for the truncated type,
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000748 // iff the lower bits of C2 and CA are zero.
Bill Wendlingf2c78f32012-02-29 01:46:50 +0000749 if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000750 LHS->hasOneUse() && RHS->hasOneUse()) {
751 Value *V;
752 ConstantInt *AndCst, *SmallCst = 0, *BigCst = 0;
753
754 // (trunc x) == C1 & (and x, CA) == C2
Craig Topperae48cb22012-12-20 07:15:54 +0000755 // (and x, CA) == C2 & (trunc x) == C1
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000756 if (match(Val2, m_Trunc(m_Value(V))) &&
757 match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
758 SmallCst = RHSCst;
759 BigCst = LHSCst;
Craig Topperae48cb22012-12-20 07:15:54 +0000760 } else if (match(Val, m_Trunc(m_Value(V))) &&
761 match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000762 SmallCst = LHSCst;
763 BigCst = RHSCst;
764 }
765
766 if (SmallCst && BigCst) {
767 unsigned BigBitSize = BigCst->getType()->getBitWidth();
768 unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
769
770 // Check that the low bits are zero.
771 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
Benjamin Kramercf9d1ad2011-04-28 21:38:51 +0000772 if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000773 Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
774 APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
775 Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
776 return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
777 }
778 }
779 }
Benjamin Kramerda37e152012-01-08 18:32:24 +0000780
Chris Lattner0a8191e2010-01-05 07:50:36 +0000781 // From here on, we only handle:
782 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
783 if (Val != Val2) return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +0000784
Chris Lattner0a8191e2010-01-05 07:50:36 +0000785 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
786 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
787 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
788 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
789 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
790 return 0;
Anders Carlssonda80afe2011-03-01 15:05:01 +0000791
792 // Make a constant range that's the intersection of the two icmp ranges.
793 // If the intersection is empty, we know that the result is false.
Craig Topper9d4171a2012-12-20 07:09:41 +0000794 ConstantRange LHSRange =
Anders Carlssonda80afe2011-03-01 15:05:01 +0000795 ConstantRange::makeICmpRegion(LHSCC, LHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +0000796 ConstantRange RHSRange =
Anders Carlssonda80afe2011-03-01 15:05:01 +0000797 ConstantRange::makeICmpRegion(RHSCC, RHSCst->getValue());
798
799 if (LHSRange.intersectWith(RHSRange).isEmptySet())
800 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
801
Chris Lattner0a8191e2010-01-05 07:50:36 +0000802 // We can't fold (ugt x, C) & (sgt x, C2).
803 if (!PredicatesFoldable(LHSCC, RHSCC))
804 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +0000805
Chris Lattner0a8191e2010-01-05 07:50:36 +0000806 // Ensure that the larger constant is on the RHS.
807 bool ShouldSwap;
808 if (CmpInst::isSigned(LHSCC) ||
Craig Topper9d4171a2012-12-20 07:09:41 +0000809 (ICmpInst::isEquality(LHSCC) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +0000810 CmpInst::isSigned(RHSCC)))
811 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
812 else
813 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +0000814
Chris Lattner0a8191e2010-01-05 07:50:36 +0000815 if (ShouldSwap) {
816 std::swap(LHS, RHS);
817 std::swap(LHSCst, RHSCst);
818 std::swap(LHSCC, RHSCC);
819 }
820
Dan Gohman4a618822010-02-10 16:03:48 +0000821 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +0000822 // comparing a value against two constants and and'ing the result
823 // together. Because of the above check, we know that we only have
Craig Topper9d4171a2012-12-20 07:09:41 +0000824 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
825 // (from the icmp folding check above), that the two constants
Chris Lattner0a8191e2010-01-05 07:50:36 +0000826 // are not equal and that the larger constant is on the RHS
827 assert(LHSCst != RHSCst && "Compares not folded above?");
828
829 switch (LHSCC) {
830 default: llvm_unreachable("Unknown integer condition code!");
831 case ICmpInst::ICMP_EQ:
832 switch (RHSCC) {
833 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000834 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
835 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
836 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
Chris Lattner067459c2010-03-05 08:46:26 +0000837 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000838 }
839 case ICmpInst::ICMP_NE:
840 switch (RHSCC) {
841 default: llvm_unreachable("Unknown integer condition code!");
842 case ICmpInst::ICMP_ULT:
843 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000844 return Builder->CreateICmpULT(Val, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000845 break; // (X != 13 & X u< 15) -> no change
846 case ICmpInst::ICMP_SLT:
847 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000848 return Builder->CreateICmpSLT(Val, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000849 break; // (X != 13 & X s< 15) -> no change
850 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
851 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
852 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000853 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000854 case ICmpInst::ICMP_NE:
855 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
856 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
857 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
Chris Lattner067459c2010-03-05 08:46:26 +0000858 return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000859 }
860 break; // (X != 13 & X != 15) -> no change
861 }
862 break;
863 case ICmpInst::ICMP_ULT:
864 switch (RHSCC) {
865 default: llvm_unreachable("Unknown integer condition code!");
866 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
867 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
Chris Lattner067459c2010-03-05 08:46:26 +0000868 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000869 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
870 break;
871 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
872 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
Chris Lattner067459c2010-03-05 08:46:26 +0000873 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000874 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
875 break;
876 }
877 break;
878 case ICmpInst::ICMP_SLT:
879 switch (RHSCC) {
880 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000881 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
882 break;
883 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
884 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000885 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000886 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
887 break;
888 }
889 break;
890 case ICmpInst::ICMP_UGT:
891 switch (RHSCC) {
892 default: llvm_unreachable("Unknown integer condition code!");
893 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15
894 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000895 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000896 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
897 break;
898 case ICmpInst::ICMP_NE:
899 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
Chris Lattner067459c2010-03-05 08:46:26 +0000900 return Builder->CreateICmp(LHSCC, Val, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000901 break; // (X u> 13 & X != 15) -> no change
902 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
Chris Lattner067459c2010-03-05 08:46:26 +0000903 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000904 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
905 break;
906 }
907 break;
908 case ICmpInst::ICMP_SGT:
909 switch (RHSCC) {
910 default: llvm_unreachable("Unknown integer condition code!");
911 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
912 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000913 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000914 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
915 break;
916 case ICmpInst::ICMP_NE:
917 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
Chris Lattner067459c2010-03-05 08:46:26 +0000918 return Builder->CreateICmp(LHSCC, Val, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000919 break; // (X s> 13 & X != 15) -> no change
920 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
Chris Lattner067459c2010-03-05 08:46:26 +0000921 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000922 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
923 break;
924 }
925 break;
926 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000927
Chris Lattner0a8191e2010-01-05 07:50:36 +0000928 return 0;
929}
930
Chris Lattner067459c2010-03-05 08:46:26 +0000931/// FoldAndOfFCmps - Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of
932/// instcombine, this returns a Value which should already be inserted into the
933/// function.
934Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000935 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
936 RHS->getPredicate() == FCmpInst::FCMP_ORD) {
Benjamin Kramere89c7052013-04-12 21:56:23 +0000937 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
938 return 0;
939
Chris Lattner0a8191e2010-01-05 07:50:36 +0000940 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
941 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
942 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
943 // If either of the constants are nans, then the whole thing returns
944 // false.
945 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Chris Lattner067459c2010-03-05 08:46:26 +0000946 return ConstantInt::getFalse(LHS->getContext());
947 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000948 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000949
Chris Lattner0a8191e2010-01-05 07:50:36 +0000950 // Handle vector zeros. This occurs because the canonical form of
951 // "fcmp ord x,x" is "fcmp ord x, 0".
952 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
953 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +0000954 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000955 return 0;
956 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000957
Chris Lattner0a8191e2010-01-05 07:50:36 +0000958 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
959 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
960 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
Craig Topper9d4171a2012-12-20 07:09:41 +0000961
962
Chris Lattner0a8191e2010-01-05 07:50:36 +0000963 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
964 // Swap RHS operands to match LHS.
965 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
966 std::swap(Op1LHS, Op1RHS);
967 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000968
Chris Lattner0a8191e2010-01-05 07:50:36 +0000969 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
970 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
971 if (Op0CC == Op1CC)
Chris Lattner067459c2010-03-05 08:46:26 +0000972 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000973 if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
Chris Lattner067459c2010-03-05 08:46:26 +0000974 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000975 if (Op0CC == FCmpInst::FCMP_TRUE)
Chris Lattner067459c2010-03-05 08:46:26 +0000976 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000977 if (Op1CC == FCmpInst::FCMP_TRUE)
Chris Lattner067459c2010-03-05 08:46:26 +0000978 return LHS;
Craig Topper9d4171a2012-12-20 07:09:41 +0000979
Chris Lattner0a8191e2010-01-05 07:50:36 +0000980 bool Op0Ordered;
981 bool Op1Ordered;
982 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
983 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
Chad Rosierfaa38942012-06-06 17:22:40 +0000984 // uno && ord -> false
985 if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered)
986 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000987 if (Op1Pred == 0) {
988 std::swap(LHS, RHS);
989 std::swap(Op0Pred, Op1Pred);
990 std::swap(Op0Ordered, Op1Ordered);
991 }
992 if (Op0Pred == 0) {
Manman Renc2bc2d12012-06-14 05:57:42 +0000993 // uno && ueq -> uno && (uno || eq) -> uno
Chris Lattner0a8191e2010-01-05 07:50:36 +0000994 // ord && olt -> ord && (ord && lt) -> olt
Manman Renc2bc2d12012-06-14 05:57:42 +0000995 if (!Op0Ordered && (Op0Ordered == Op1Ordered))
996 return LHS;
997 if (Op0Ordered && (Op0Ordered == Op1Ordered))
Chris Lattner067459c2010-03-05 08:46:26 +0000998 return RHS;
Craig Topper9d4171a2012-12-20 07:09:41 +0000999
Chris Lattner0a8191e2010-01-05 07:50:36 +00001000 // uno && oeq -> uno && (ord && eq) -> false
Chris Lattner0a8191e2010-01-05 07:50:36 +00001001 if (!Op0Ordered)
Chris Lattner067459c2010-03-05 08:46:26 +00001002 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001003 // ord && ueq -> ord && (uno || eq) -> oeq
Chris Lattner067459c2010-03-05 08:46:26 +00001004 return getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001005 }
1006 }
1007
1008 return 0;
1009}
1010
1011
1012Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001013 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001014 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1015
1016 if (Value *V = SimplifyAndInst(Op0, Op1, TD))
1017 return ReplaceInstUsesWith(I, V);
1018
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00001019 // (A|B)&(A|C) -> A|(B&C) etc
1020 if (Value *V = SimplifyUsingDistributiveLaws(I))
1021 return ReplaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001022
Craig Topper9d4171a2012-12-20 07:09:41 +00001023 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001024 // purpose is to compute bits we don't care about.
1025 if (SimplifyDemandedInstructionBits(I))
Craig Topper9d4171a2012-12-20 07:09:41 +00001026 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001027
1028 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1029 const APInt &AndRHSMask = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001030
1031 // Optimize a variety of ((val OP C1) & C2) combinations...
1032 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1033 Value *Op0LHS = Op0I->getOperand(0);
1034 Value *Op0RHS = Op0I->getOperand(1);
1035 switch (Op0I->getOpcode()) {
1036 default: break;
1037 case Instruction::Xor:
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001038 case Instruction::Or: {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001039 // If the mask is only needed on one incoming arm, push it up.
1040 if (!Op0I->hasOneUse()) break;
Craig Topper9d4171a2012-12-20 07:09:41 +00001041
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001042 APInt NotAndRHS(~AndRHSMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001043 if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
1044 // Not masking anything out for the LHS, move to RHS.
1045 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1046 Op0RHS->getName()+".masked");
1047 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1048 }
1049 if (!isa<Constant>(Op0RHS) &&
1050 MaskedValueIsZero(Op0RHS, NotAndRHS)) {
1051 // Not masking anything out for the RHS, move to LHS.
1052 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1053 Op0LHS->getName()+".masked");
1054 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1055 }
1056
1057 break;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001058 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001059 case Instruction::Add:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001060 // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
1061 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1062 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001063 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
1064 return BinaryOperator::CreateAnd(V, AndRHS);
1065 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
1066 return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes
1067 break;
1068
1069 case Instruction::Sub:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001070 // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
1071 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1072 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001073 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
1074 return BinaryOperator::CreateAnd(V, AndRHS);
1075
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001076 // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
Chris Lattner0a8191e2010-01-05 07:50:36 +00001077 // has 1's for all bits that the subtraction with A might affect.
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001078 if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001079 uint32_t BitWidth = AndRHSMask.getBitWidth();
1080 uint32_t Zeros = AndRHSMask.countLeadingZeros();
1081 APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
1082
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001083 if (MaskedValueIsZero(Op0LHS, Mask)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001084 Value *NewNeg = Builder->CreateNeg(Op0RHS);
1085 return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1086 }
1087 }
1088 break;
1089
1090 case Instruction::Shl:
1091 case Instruction::LShr:
1092 // (1 << x) & 1 --> zext(x == 0)
1093 // (1 >> x) & 1 --> zext(x == 0)
1094 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1095 Value *NewICmp =
1096 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1097 return new ZExtInst(NewICmp, I.getType());
1098 }
1099 break;
1100 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001101
Chris Lattner0a8191e2010-01-05 07:50:36 +00001102 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1103 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1104 return Res;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001105 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001106
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001107 // If this is an integer truncation, and if the source is an 'and' with
1108 // immediate, transform it. This frequently occurs for bitfield accesses.
1109 {
1110 Value *X = 0; ConstantInt *YC = 0;
1111 if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1112 // Change: and (trunc (and X, YC) to T), C2
1113 // into : and (trunc X to T), trunc(YC) & C2
Craig Topper9d4171a2012-12-20 07:09:41 +00001114 // This will fold the two constants together, which may allow
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001115 // other simplifications.
1116 Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1117 Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1118 C3 = ConstantExpr::getAnd(C3, AndRHS);
1119 return BinaryOperator::CreateAnd(NewCast, C3);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001120 }
1121 }
1122
1123 // Try to fold constant and into select arguments.
1124 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1125 if (Instruction *R = FoldOpIntoSelect(I, SI))
1126 return R;
1127 if (isa<PHINode>(Op0))
1128 if (Instruction *NV = FoldOpIntoPhi(I))
1129 return NV;
1130 }
1131
1132
1133 // (~A & ~B) == (~(A | B)) - De Morgan's Law
1134 if (Value *Op0NotVal = dyn_castNotVal(Op0))
1135 if (Value *Op1NotVal = dyn_castNotVal(Op1))
1136 if (Op0->hasOneUse() && Op1->hasOneUse()) {
1137 Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
1138 I.getName()+".demorgan");
1139 return BinaryOperator::CreateNot(Or);
1140 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001141
Chris Lattner0a8191e2010-01-05 07:50:36 +00001142 {
1143 Value *A = 0, *B = 0, *C = 0, *D = 0;
1144 // (A|B) & ~(A&B) -> A^B
1145 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1146 match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1147 ((A == C && B == D) || (A == D && B == C)))
1148 return BinaryOperator::CreateXor(A, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001149
Chris Lattner0a8191e2010-01-05 07:50:36 +00001150 // ~(A&B) & (A|B) -> A^B
1151 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1152 match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1153 ((A == C && B == D) || (A == D && B == C)))
1154 return BinaryOperator::CreateXor(A, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001155
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001156 // A&(A^B) => A & ~B
1157 {
1158 Value *tmpOp0 = Op0;
1159 Value *tmpOp1 = Op1;
1160 if (Op0->hasOneUse() &&
1161 match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
1162 if (A == Op1 || B == Op1 ) {
1163 tmpOp1 = Op0;
1164 tmpOp0 = Op1;
1165 // Simplify below
1166 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001167 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001168
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001169 if (tmpOp1->hasOneUse() &&
1170 match(tmpOp1, m_Xor(m_Value(A), m_Value(B)))) {
1171 if (B == tmpOp0) {
1172 std::swap(A, B);
1173 }
1174 // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if
1175 // A is originally -1 (or a vector of -1 and undefs), then we enter
1176 // an endless loop. By checking that A is non-constant we ensure that
1177 // we will never get to the loop.
1178 if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00001179 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001180 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001181 }
1182
1183 // (A&((~A)|B)) -> A&B
1184 if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1185 match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1186 return BinaryOperator::CreateAnd(A, Op1);
1187 if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1188 match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1189 return BinaryOperator::CreateAnd(A, Op0);
1190 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001191
Chris Lattner0a8191e2010-01-05 07:50:36 +00001192 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1))
1193 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
Chris Lattner067459c2010-03-05 08:46:26 +00001194 if (Value *Res = FoldAndOfICmps(LHS, RHS))
1195 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001196
Chris Lattner4e8137d2010-02-11 06:26:33 +00001197 // If and'ing two fcmp, try combine them into one.
1198 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1199 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001200 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1201 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001202
1203
Chris Lattner0a8191e2010-01-05 07:50:36 +00001204 // fold (and (cast A), (cast B)) -> (cast (and A, B))
1205 if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
Chris Lattner4e8137d2010-02-11 06:26:33 +00001206 if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001207 Type *SrcTy = Op0C->getOperand(0)->getType();
Chris Lattner4e8137d2010-02-11 06:26:33 +00001208 if (Op0C->getOpcode() == Op1C->getOpcode() && // same cast kind ?
1209 SrcTy == Op1C->getOperand(0)->getType() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001210 SrcTy->isIntOrIntVectorTy()) {
Chris Lattner4e8137d2010-02-11 06:26:33 +00001211 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
Craig Topper9d4171a2012-12-20 07:09:41 +00001212
Chris Lattner4e8137d2010-02-11 06:26:33 +00001213 // Only do this if the casts both really cause code to be generated.
1214 if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
1215 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
1216 Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.getName());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001217 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
1218 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001219
Chris Lattner4e8137d2010-02-11 06:26:33 +00001220 // If this is and(cast(icmp), cast(icmp)), try to fold this even if the
1221 // cast is otherwise not optimizable. This happens for vector sexts.
1222 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
1223 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
Chris Lattner067459c2010-03-05 08:46:26 +00001224 if (Value *Res = FoldAndOfICmps(LHS, RHS))
Chris Lattner4e8137d2010-02-11 06:26:33 +00001225 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Craig Topper9d4171a2012-12-20 07:09:41 +00001226
Chris Lattner4e8137d2010-02-11 06:26:33 +00001227 // If this is and(cast(fcmp), cast(fcmp)), try to fold this even if the
1228 // cast is otherwise not optimizable. This happens for vector sexts.
1229 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
1230 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
Chris Lattner067459c2010-03-05 08:46:26 +00001231 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
Chris Lattner4e8137d2010-02-11 06:26:33 +00001232 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001233 }
Chris Lattner4e8137d2010-02-11 06:26:33 +00001234 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001235
Chris Lattner0a8191e2010-01-05 07:50:36 +00001236 // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
1237 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
1238 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
Craig Topper9d4171a2012-12-20 07:09:41 +00001239 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001240 SI0->getOperand(1) == SI1->getOperand(1) &&
1241 (SI0->hasOneUse() || SI1->hasOneUse())) {
1242 Value *NewOp =
1243 Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
1244 SI0->getName());
Craig Topper9d4171a2012-12-20 07:09:41 +00001245 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001246 SI1->getOperand(1));
1247 }
1248 }
1249
Nadav Rotem513bd8a2013-01-30 06:35:22 +00001250 {
1251 Value *X = 0;
1252 bool OpsSwapped = false;
1253 // Canonicalize SExt or Not to the LHS
1254 if (match(Op1, m_SExt(m_Value())) ||
1255 match(Op1, m_Not(m_Value()))) {
1256 std::swap(Op0, Op1);
1257 OpsSwapped = true;
1258 }
1259
1260 // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1261 if (match(Op0, m_SExt(m_Value(X))) &&
1262 X->getType()->getScalarType()->isIntegerTy(1)) {
1263 Value *Zero = Constant::getNullValue(Op1->getType());
1264 return SelectInst::Create(X, Op1, Zero);
1265 }
1266
1267 // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1268 if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1269 X->getType()->getScalarType()->isIntegerTy(1)) {
1270 Value *Zero = Constant::getNullValue(Op0->getType());
1271 return SelectInst::Create(X, Zero, Op1);
1272 }
1273
1274 if (OpsSwapped)
1275 std::swap(Op0, Op1);
1276 }
1277
Chris Lattner0a8191e2010-01-05 07:50:36 +00001278 return Changed ? &I : 0;
1279}
1280
1281/// CollectBSwapParts - Analyze the specified subexpression and see if it is
1282/// capable of providing pieces of a bswap. The subexpression provides pieces
1283/// of a bswap if it is proven that each of the non-zero bytes in the output of
1284/// the expression came from the corresponding "byte swapped" byte in some other
1285/// value. For example, if the current subexpression is "(shl i32 %X, 24)" then
1286/// we know that the expression deposits the low byte of %X into the high byte
1287/// of the bswap result and that all other bytes are zero. This expression is
1288/// accepted, the high byte of ByteValues is set to X to indicate a correct
1289/// match.
1290///
1291/// This function returns true if the match was unsuccessful and false if so.
1292/// On entry to the function the "OverallLeftShift" is a signed integer value
1293/// indicating the number of bytes that the subexpression is later shifted. For
1294/// example, if the expression is later right shifted by 16 bits, the
1295/// OverallLeftShift value would be -2 on entry. This is used to specify which
1296/// byte of ByteValues is actually being set.
1297///
1298/// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
1299/// byte is masked to zero by a user. For example, in (X & 255), X will be
1300/// processed with a bytemask of 1. Because bytemask is 32-bits, this limits
1301/// this function to working on up to 32-byte (256 bit) values. ByteMask is
1302/// always in the local (OverallLeftShift) coordinate space.
1303///
1304static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
1305 SmallVector<Value*, 8> &ByteValues) {
1306 if (Instruction *I = dyn_cast<Instruction>(V)) {
1307 // If this is an or instruction, it may be an inner node of the bswap.
1308 if (I->getOpcode() == Instruction::Or) {
1309 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1310 ByteValues) ||
1311 CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
1312 ByteValues);
1313 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001314
Chris Lattner0a8191e2010-01-05 07:50:36 +00001315 // If this is a logical shift by a constant multiple of 8, recurse with
1316 // OverallLeftShift and ByteMask adjusted.
1317 if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00001318 unsigned ShAmt =
Chris Lattner0a8191e2010-01-05 07:50:36 +00001319 cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
1320 // Ensure the shift amount is defined and of a byte value.
1321 if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
1322 return true;
1323
1324 unsigned ByteShift = ShAmt >> 3;
1325 if (I->getOpcode() == Instruction::Shl) {
1326 // X << 2 -> collect(X, +2)
1327 OverallLeftShift += ByteShift;
1328 ByteMask >>= ByteShift;
1329 } else {
1330 // X >>u 2 -> collect(X, -2)
1331 OverallLeftShift -= ByteShift;
1332 ByteMask <<= ByteShift;
1333 ByteMask &= (~0U >> (32-ByteValues.size()));
1334 }
1335
1336 if (OverallLeftShift >= (int)ByteValues.size()) return true;
1337 if (OverallLeftShift <= -(int)ByteValues.size()) return true;
1338
Craig Topper9d4171a2012-12-20 07:09:41 +00001339 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001340 ByteValues);
1341 }
1342
1343 // If this is a logical 'and' with a mask that clears bytes, clear the
1344 // corresponding bytes in ByteMask.
1345 if (I->getOpcode() == Instruction::And &&
1346 isa<ConstantInt>(I->getOperand(1))) {
1347 // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
1348 unsigned NumBytes = ByteValues.size();
1349 APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
1350 const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
Craig Topper9d4171a2012-12-20 07:09:41 +00001351
Chris Lattner0a8191e2010-01-05 07:50:36 +00001352 for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
1353 // If this byte is masked out by a later operation, we don't care what
1354 // the and mask is.
1355 if ((ByteMask & (1 << i)) == 0)
1356 continue;
Craig Topper9d4171a2012-12-20 07:09:41 +00001357
Chris Lattner0a8191e2010-01-05 07:50:36 +00001358 // If the AndMask is all zeros for this byte, clear the bit.
1359 APInt MaskB = AndMask & Byte;
1360 if (MaskB == 0) {
1361 ByteMask &= ~(1U << i);
1362 continue;
1363 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001364
Chris Lattner0a8191e2010-01-05 07:50:36 +00001365 // If the AndMask is not all ones for this byte, it's not a bytezap.
1366 if (MaskB != Byte)
1367 return true;
1368
1369 // Otherwise, this byte is kept.
1370 }
1371
Craig Topper9d4171a2012-12-20 07:09:41 +00001372 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001373 ByteValues);
1374 }
1375 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001376
Chris Lattner0a8191e2010-01-05 07:50:36 +00001377 // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be
1378 // the input value to the bswap. Some observations: 1) if more than one byte
1379 // is demanded from this input, then it could not be successfully assembled
1380 // into a byteswap. At least one of the two bytes would not be aligned with
1381 // their ultimate destination.
1382 if (!isPowerOf2_32(ByteMask)) return true;
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001383 unsigned InputByteNo = countTrailingZeros(ByteMask);
Craig Topper9d4171a2012-12-20 07:09:41 +00001384
Chris Lattner0a8191e2010-01-05 07:50:36 +00001385 // 2) The input and ultimate destinations must line up: if byte 3 of an i32
1386 // is demanded, it needs to go into byte 0 of the result. This means that the
1387 // byte needs to be shifted until it lands in the right byte bucket. The
1388 // shift amount depends on the position: if the byte is coming from the high
1389 // part of the value (e.g. byte 3) then it must be shifted right. If from the
1390 // low part, it must be shifted left.
1391 unsigned DestByteNo = InputByteNo + OverallLeftShift;
Chris Lattnerb1e2e1e2012-03-26 19:13:57 +00001392 if (ByteValues.size()-1-DestByteNo != InputByteNo)
1393 return true;
Craig Topper9d4171a2012-12-20 07:09:41 +00001394
Chris Lattner0a8191e2010-01-05 07:50:36 +00001395 // If the destination byte value is already defined, the values are or'd
1396 // together, which isn't a bswap (unless it's an or of the same bits).
1397 if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
1398 return true;
1399 ByteValues[DestByteNo] = V;
1400 return false;
1401}
1402
1403/// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
1404/// If so, insert the new bswap intrinsic and return it.
1405Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
Jay Foadb804a2b2011-07-12 14:06:48 +00001406 IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
Craig Topper9d4171a2012-12-20 07:09:41 +00001407 if (!ITy || ITy->getBitWidth() % 16 ||
Chris Lattner0a8191e2010-01-05 07:50:36 +00001408 // ByteMask only allows up to 32-byte values.
Craig Topper9d4171a2012-12-20 07:09:41 +00001409 ITy->getBitWidth() > 32*8)
Chris Lattner0a8191e2010-01-05 07:50:36 +00001410 return 0; // Can only bswap pairs of bytes. Can't do vectors.
Craig Topper9d4171a2012-12-20 07:09:41 +00001411
Chris Lattner0a8191e2010-01-05 07:50:36 +00001412 /// ByteValues - For each byte of the result, we keep track of which value
1413 /// defines each byte.
1414 SmallVector<Value*, 8> ByteValues;
1415 ByteValues.resize(ITy->getBitWidth()/8);
Craig Topper9d4171a2012-12-20 07:09:41 +00001416
Chris Lattner0a8191e2010-01-05 07:50:36 +00001417 // Try to find all the pieces corresponding to the bswap.
1418 uint32_t ByteMask = ~0U >> (32-ByteValues.size());
1419 if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
1420 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001421
Chris Lattner0a8191e2010-01-05 07:50:36 +00001422 // Check to see if all of the bytes come from the same value.
1423 Value *V = ByteValues[0];
1424 if (V == 0) return 0; // Didn't find a byte? Must be zero.
Craig Topper9d4171a2012-12-20 07:09:41 +00001425
Chris Lattner0a8191e2010-01-05 07:50:36 +00001426 // Check to make sure that all of the bytes come from the same value.
1427 for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
1428 if (ByteValues[i] != V)
1429 return 0;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001430 Module *M = I.getParent()->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +00001431 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, ITy);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001432 return CallInst::Create(F, V);
1433}
1434
1435/// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check
1436/// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
1437/// we can simplify this expression to "cond ? C : D or B".
1438static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
1439 Value *C, Value *D) {
1440 // If A is not a select of -1/0, this cannot match.
1441 Value *Cond = 0;
Chris Lattner9b6a1782010-02-09 01:12:41 +00001442 if (!match(A, m_SExt(m_Value(Cond))) ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001443 !Cond->getType()->isIntegerTy(1))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001444 return 0;
1445
1446 // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001447 if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001448 return SelectInst::Create(Cond, C, B);
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001449 if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
Chris Lattner64ffd112010-02-05 19:53:02 +00001450 return SelectInst::Create(Cond, C, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001451
Chris Lattner0a8191e2010-01-05 07:50:36 +00001452 // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001453 if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
Chris Lattner64ffd112010-02-05 19:53:02 +00001454 return SelectInst::Create(Cond, C, D);
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001455 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001456 return SelectInst::Create(Cond, C, D);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001457 return 0;
1458}
1459
Chris Lattner067459c2010-03-05 08:46:26 +00001460/// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
1461Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001462 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1463
1464 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1465 if (PredicatesFoldable(LHSCC, RHSCC)) {
1466 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1467 LHS->getOperand(1) == RHS->getOperand(0))
1468 LHS->swapOperands();
1469 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1470 LHS->getOperand(1) == RHS->getOperand(1)) {
1471 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1472 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1473 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +00001474 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001475 }
1476 }
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001477
1478 // handle (roughly):
1479 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
1480 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, ICmpInst::ICMP_NE, Builder))
1481 return V;
Owen Anderson3fe002d2010-09-08 22:16:17 +00001482
Chris Lattner0a8191e2010-01-05 07:50:36 +00001483 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
1484 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1485 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1486 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1487 if (LHSCst == 0 || RHSCst == 0) return 0;
1488
Owen Anderson8f306a72010-08-02 09:32:13 +00001489 if (LHSCst == RHSCst && LHSCC == RHSCC) {
1490 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1491 if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1492 Value *NewOr = Builder->CreateOr(Val, Val2);
1493 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1494 }
Benjamin Kramerda37e152012-01-08 18:32:24 +00001495 }
1496
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001497 // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001498 // iff C2 + CA == C1.
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001499 if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001500 ConstantInt *AddCst;
1501 if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1502 if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001503 return Builder->CreateICmpULE(Val, LHSCst);
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001504 }
1505
Chris Lattner0a8191e2010-01-05 07:50:36 +00001506 // From here on, we only handle:
1507 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1508 if (Val != Val2) return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001509
Chris Lattner0a8191e2010-01-05 07:50:36 +00001510 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1511 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1512 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1513 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1514 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1515 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001516
Chris Lattner0a8191e2010-01-05 07:50:36 +00001517 // We can't fold (ugt x, C) | (sgt x, C2).
1518 if (!PredicatesFoldable(LHSCC, RHSCC))
1519 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001520
Chris Lattner0a8191e2010-01-05 07:50:36 +00001521 // Ensure that the larger constant is on the RHS.
1522 bool ShouldSwap;
1523 if (CmpInst::isSigned(LHSCC) ||
Craig Topper9d4171a2012-12-20 07:09:41 +00001524 (ICmpInst::isEquality(LHSCC) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001525 CmpInst::isSigned(RHSCC)))
1526 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1527 else
1528 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +00001529
Chris Lattner0a8191e2010-01-05 07:50:36 +00001530 if (ShouldSwap) {
1531 std::swap(LHS, RHS);
1532 std::swap(LHSCst, RHSCst);
1533 std::swap(LHSCC, RHSCC);
1534 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001535
Dan Gohman4a618822010-02-10 16:03:48 +00001536 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +00001537 // comparing a value against two constants and or'ing the result
1538 // together. Because of the above check, we know that we only have
1539 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1540 // icmp folding check above), that the two constants are not
1541 // equal.
1542 assert(LHSCst != RHSCst && "Compares not folded above?");
1543
1544 switch (LHSCC) {
1545 default: llvm_unreachable("Unknown integer condition code!");
1546 case ICmpInst::ICMP_EQ:
1547 switch (RHSCC) {
1548 default: llvm_unreachable("Unknown integer condition code!");
1549 case ICmpInst::ICMP_EQ:
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001550 if (LHS->getOperand(0) == RHS->getOperand(0)) {
Jakub Staszakf5849772012-12-31 01:40:44 +00001551 // if LHSCst and RHSCst differ only by one bit:
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001552 // (A == C1 || A == C2) -> (A & ~(C1 ^ C2)) == C1
Jakub Staszakc48bbe72012-12-31 18:26:42 +00001553 assert(LHSCst->getValue().ule(LHSCst->getValue()));
1554
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001555 APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1556 if (Xor.isPowerOf2()) {
1557 Value *NegCst = Builder->getInt(~Xor);
1558 Value *And = Builder->CreateAnd(LHS->getOperand(0), NegCst);
1559 return Builder->CreateICmp(ICmpInst::ICMP_EQ, And, LHSCst);
1560 }
1561 }
1562
David Majnemer1fae1952013-04-14 21:15:43 +00001563 if (LHSCst == SubOne(RHSCst)) {
1564 // (X == 13 | X == 14) -> X-13 <u 2
1565 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1566 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1567 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1568 return Builder->CreateICmpULT(Add, AddCST);
1569 }
1570
Chris Lattner0a8191e2010-01-05 07:50:36 +00001571 break; // (X == 13 | X == 15) -> no change
1572 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1573 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
1574 break;
1575 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
1576 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
1577 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001578 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001579 }
1580 break;
1581 case ICmpInst::ICMP_NE:
1582 switch (RHSCC) {
1583 default: llvm_unreachable("Unknown integer condition code!");
1584 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
1585 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
1586 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
Chris Lattner067459c2010-03-05 08:46:26 +00001587 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001588 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
1589 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
1590 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
Chris Lattner067459c2010-03-05 08:46:26 +00001591 return ConstantInt::getTrue(LHS->getContext());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001592 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001593 case ICmpInst::ICMP_ULT:
1594 switch (RHSCC) {
1595 default: llvm_unreachable("Unknown integer condition code!");
1596 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
1597 break;
1598 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
1599 // If RHSCst is [us]MAXINT, it is always false. Not handling
1600 // this can cause overflow.
1601 if (RHSCst->isMaxValue(false))
Chris Lattner067459c2010-03-05 08:46:26 +00001602 return LHS;
1603 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001604 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
1605 break;
1606 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
1607 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001608 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001609 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
1610 break;
1611 }
1612 break;
1613 case ICmpInst::ICMP_SLT:
1614 switch (RHSCC) {
1615 default: llvm_unreachable("Unknown integer condition code!");
1616 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
1617 break;
1618 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
1619 // If RHSCst is [us]MAXINT, it is always false. Not handling
1620 // this can cause overflow.
1621 if (RHSCst->isMaxValue(true))
Chris Lattner067459c2010-03-05 08:46:26 +00001622 return LHS;
1623 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001624 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
1625 break;
1626 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
1627 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001628 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001629 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
1630 break;
1631 }
1632 break;
1633 case ICmpInst::ICMP_UGT:
1634 switch (RHSCC) {
1635 default: llvm_unreachable("Unknown integer condition code!");
1636 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
1637 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
Chris Lattner067459c2010-03-05 08:46:26 +00001638 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001639 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
1640 break;
1641 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
1642 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
Chris Lattner067459c2010-03-05 08:46:26 +00001643 return ConstantInt::getTrue(LHS->getContext());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001644 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
1645 break;
1646 }
1647 break;
1648 case ICmpInst::ICMP_SGT:
1649 switch (RHSCC) {
1650 default: llvm_unreachable("Unknown integer condition code!");
1651 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
1652 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
Chris Lattner067459c2010-03-05 08:46:26 +00001653 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001654 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
1655 break;
1656 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
1657 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
Chris Lattner067459c2010-03-05 08:46:26 +00001658 return ConstantInt::getTrue(LHS->getContext());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001659 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
1660 break;
1661 }
1662 break;
1663 }
1664 return 0;
1665}
1666
Chris Lattner067459c2010-03-05 08:46:26 +00001667/// FoldOrOfFCmps - Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of
1668/// instcombine, this returns a Value which should already be inserted into the
1669/// function.
1670Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001671 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
Craig Topper9d4171a2012-12-20 07:09:41 +00001672 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001673 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1674 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1675 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1676 // If either of the constants are nans, then the whole thing returns
1677 // true.
1678 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Chris Lattner067459c2010-03-05 08:46:26 +00001679 return ConstantInt::getTrue(LHS->getContext());
Craig Topper9d4171a2012-12-20 07:09:41 +00001680
Chris Lattner0a8191e2010-01-05 07:50:36 +00001681 // Otherwise, no need to compare the two constants, compare the
1682 // rest.
Chris Lattner067459c2010-03-05 08:46:26 +00001683 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001684 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001685
Chris Lattner0a8191e2010-01-05 07:50:36 +00001686 // Handle vector zeros. This occurs because the canonical form of
1687 // "fcmp uno x,x" is "fcmp uno x, 0".
1688 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1689 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001690 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Craig Topper9d4171a2012-12-20 07:09:41 +00001691
Chris Lattner0a8191e2010-01-05 07:50:36 +00001692 return 0;
1693 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001694
Chris Lattner0a8191e2010-01-05 07:50:36 +00001695 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1696 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1697 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
Craig Topper9d4171a2012-12-20 07:09:41 +00001698
Chris Lattner0a8191e2010-01-05 07:50:36 +00001699 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1700 // Swap RHS operands to match LHS.
1701 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1702 std::swap(Op1LHS, Op1RHS);
1703 }
1704 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1705 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1706 if (Op0CC == Op1CC)
Chris Lattner067459c2010-03-05 08:46:26 +00001707 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001708 if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
Chris Lattner067459c2010-03-05 08:46:26 +00001709 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001710 if (Op0CC == FCmpInst::FCMP_FALSE)
Chris Lattner067459c2010-03-05 08:46:26 +00001711 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001712 if (Op1CC == FCmpInst::FCMP_FALSE)
Chris Lattner067459c2010-03-05 08:46:26 +00001713 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001714 bool Op0Ordered;
1715 bool Op1Ordered;
1716 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1717 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
1718 if (Op0Ordered == Op1Ordered) {
1719 // If both are ordered or unordered, return a new fcmp with
1720 // or'ed predicates.
Chris Lattner067459c2010-03-05 08:46:26 +00001721 return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001722 }
1723 }
1724 return 0;
1725}
1726
1727/// FoldOrWithConstants - This helper function folds:
1728///
1729/// ((A | B) & C1) | (B & C2)
1730///
1731/// into:
Craig Topper9d4171a2012-12-20 07:09:41 +00001732///
Chris Lattner0a8191e2010-01-05 07:50:36 +00001733/// (A & C1) | B
1734///
1735/// when the XOR of the two constants is "all ones" (-1).
1736Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
1737 Value *A, Value *B, Value *C) {
1738 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
1739 if (!CI1) return 0;
1740
1741 Value *V1 = 0;
1742 ConstantInt *CI2 = 0;
1743 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
1744
1745 APInt Xor = CI1->getValue() ^ CI2->getValue();
1746 if (!Xor.isAllOnesValue()) return 0;
1747
1748 if (V1 == A || V1 == B) {
1749 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1750 return BinaryOperator::CreateOr(NewOp, V1);
1751 }
1752
1753 return 0;
1754}
1755
1756Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001757 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001758 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1759
1760 if (Value *V = SimplifyOrInst(Op0, Op1, TD))
1761 return ReplaceInstUsesWith(I, V);
Bill Wendlingaf13d822010-03-03 00:35:56 +00001762
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00001763 // (A&B)|(A&C) -> A&(B|C) etc
1764 if (Value *V = SimplifyUsingDistributiveLaws(I))
1765 return ReplaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001766
Craig Topper9d4171a2012-12-20 07:09:41 +00001767 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001768 // purpose is to compute bits we don't care about.
1769 if (SimplifyDemandedInstructionBits(I))
1770 return &I;
1771
1772 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1773 ConstantInt *C1 = 0; Value *X = 0;
1774 // (X & C1) | C2 --> (X | C2) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001775 // iff (C1 & C2) == 0.
Chris Lattner0a8191e2010-01-05 07:50:36 +00001776 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
Bill Wendlingaf13d822010-03-03 00:35:56 +00001777 (RHS->getValue() & C1->getValue()) != 0 &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001778 Op0->hasOneUse()) {
1779 Value *Or = Builder->CreateOr(X, RHS);
1780 Or->takeName(Op0);
Craig Topper9d4171a2012-12-20 07:09:41 +00001781 return BinaryOperator::CreateAnd(Or,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001782 ConstantInt::get(I.getContext(),
1783 RHS->getValue() | C1->getValue()));
1784 }
1785
1786 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1787 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
1788 Op0->hasOneUse()) {
1789 Value *Or = Builder->CreateOr(X, RHS);
1790 Or->takeName(Op0);
1791 return BinaryOperator::CreateXor(Or,
1792 ConstantInt::get(I.getContext(),
1793 C1->getValue() & ~RHS->getValue()));
1794 }
1795
1796 // Try to fold constant and into select arguments.
1797 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1798 if (Instruction *R = FoldOpIntoSelect(I, SI))
1799 return R;
Bill Wendlingaf13d822010-03-03 00:35:56 +00001800
Chris Lattner0a8191e2010-01-05 07:50:36 +00001801 if (isa<PHINode>(Op0))
1802 if (Instruction *NV = FoldOpIntoPhi(I))
1803 return NV;
1804 }
1805
1806 Value *A = 0, *B = 0;
1807 ConstantInt *C1 = 0, *C2 = 0;
1808
1809 // (A | B) | C and A | (B | C) -> bswap if possible.
1810 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
1811 if (match(Op0, m_Or(m_Value(), m_Value())) ||
1812 match(Op1, m_Or(m_Value(), m_Value())) ||
Chris Lattnerb9400912011-02-09 17:00:45 +00001813 (match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1814 match(Op1, m_LogicalShift(m_Value(), m_Value())))) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001815 if (Instruction *BSwap = MatchBSwap(I))
1816 return BSwap;
1817 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001818
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001819 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001820 if (Op0->hasOneUse() &&
1821 match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1822 MaskedValueIsZero(Op1, C1->getValue())) {
1823 Value *NOr = Builder->CreateOr(A, Op1);
1824 NOr->takeName(Op0);
1825 return BinaryOperator::CreateXor(NOr, C1);
1826 }
1827
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001828 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001829 if (Op1->hasOneUse() &&
1830 match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1831 MaskedValueIsZero(Op0, C1->getValue())) {
1832 Value *NOr = Builder->CreateOr(A, Op0);
1833 NOr->takeName(Op0);
1834 return BinaryOperator::CreateXor(NOr, C1);
1835 }
1836
1837 // (A & C)|(B & D)
1838 Value *C = 0, *D = 0;
1839 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
1840 match(Op1, m_And(m_Value(B), m_Value(D)))) {
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001841 Value *V1 = 0, *V2 = 0;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001842 C1 = dyn_cast<ConstantInt>(C);
1843 C2 = dyn_cast<ConstantInt>(D);
1844 if (C1 && C2) { // (A & C1)|(B & C2)
1845 // If we have: ((V + N) & C1) | (V & C2)
1846 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
1847 // replace with V+N.
1848 if (C1->getValue() == ~C2->getValue()) {
1849 if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
1850 match(A, m_Add(m_Value(V1), m_Value(V2)))) {
1851 // Add commutes, try both ways.
1852 if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
1853 return ReplaceInstUsesWith(I, A);
1854 if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
1855 return ReplaceInstUsesWith(I, A);
1856 }
1857 // Or commutes, try both ways.
1858 if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
1859 match(B, m_Add(m_Value(V1), m_Value(V2)))) {
1860 // Add commutes, try both ways.
1861 if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
1862 return ReplaceInstUsesWith(I, B);
1863 if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
1864 return ReplaceInstUsesWith(I, B);
1865 }
1866 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001867
Chris Lattner0a8191e2010-01-05 07:50:36 +00001868 if ((C1->getValue() & C2->getValue()) == 0) {
Chris Lattner95188692010-01-11 06:55:24 +00001869 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001870 // iff (C1&C2) == 0 and (N&~C1) == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001871 if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
1872 ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) || // (V|N)
1873 (V2 == B && MaskedValueIsZero(V1, ~C1->getValue())))) // (N|V)
1874 return BinaryOperator::CreateAnd(A,
1875 ConstantInt::get(A->getContext(),
1876 C1->getValue()|C2->getValue()));
1877 // Or commutes, try both ways.
1878 if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
1879 ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) || // (V|N)
1880 (V2 == A && MaskedValueIsZero(V1, ~C2->getValue())))) // (N|V)
1881 return BinaryOperator::CreateAnd(B,
1882 ConstantInt::get(B->getContext(),
1883 C1->getValue()|C2->getValue()));
Craig Topper9d4171a2012-12-20 07:09:41 +00001884
Chris Lattner95188692010-01-11 06:55:24 +00001885 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001886 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
Chris Lattner95188692010-01-11 06:55:24 +00001887 ConstantInt *C3 = 0, *C4 = 0;
1888 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
1889 (C3->getValue() & ~C1->getValue()) == 0 &&
1890 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
1891 (C4->getValue() & ~C2->getValue()) == 0) {
1892 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
1893 return BinaryOperator::CreateAnd(V2,
1894 ConstantInt::get(B->getContext(),
1895 C1->getValue()|C2->getValue()));
1896 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001897 }
1898 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001899
Chris Lattner8e2c4712010-02-02 02:43:51 +00001900 // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants.
1901 // Don't do this for vector select idioms, the code generator doesn't handle
1902 // them well yet.
Duncan Sands19d0b472010-02-16 11:11:14 +00001903 if (!I.getType()->isVectorTy()) {
Chris Lattner8e2c4712010-02-02 02:43:51 +00001904 if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
1905 return Match;
1906 if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
1907 return Match;
1908 if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
1909 return Match;
1910 if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
1911 return Match;
1912 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001913
1914 // ((A&~B)|(~A&B)) -> A^B
1915 if ((match(C, m_Not(m_Specific(D))) &&
1916 match(B, m_Not(m_Specific(A)))))
1917 return BinaryOperator::CreateXor(A, D);
1918 // ((~B&A)|(~A&B)) -> A^B
1919 if ((match(A, m_Not(m_Specific(D))) &&
1920 match(B, m_Not(m_Specific(C)))))
1921 return BinaryOperator::CreateXor(C, D);
1922 // ((A&~B)|(B&~A)) -> A^B
1923 if ((match(C, m_Not(m_Specific(B))) &&
1924 match(D, m_Not(m_Specific(A)))))
1925 return BinaryOperator::CreateXor(A, B);
1926 // ((~B&A)|(B&~A)) -> A^B
1927 if ((match(A, m_Not(m_Specific(B))) &&
1928 match(D, m_Not(m_Specific(C)))))
1929 return BinaryOperator::CreateXor(C, B);
Benjamin Kramer11743242010-07-12 13:34:22 +00001930
1931 // ((A|B)&1)|(B&-2) -> (A&1) | B
1932 if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
1933 match(A, m_Or(m_Specific(B), m_Value(V1)))) {
1934 Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
1935 if (Ret) return Ret;
1936 }
1937 // (B&-2)|((A|B)&1) -> (A&1) | B
1938 if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
1939 match(B, m_Or(m_Value(V1), m_Specific(A)))) {
1940 Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
1941 if (Ret) return Ret;
1942 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001943 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001944
Chris Lattner0a8191e2010-01-05 07:50:36 +00001945 // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
1946 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
1947 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
Craig Topper9d4171a2012-12-20 07:09:41 +00001948 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001949 SI0->getOperand(1) == SI1->getOperand(1) &&
1950 (SI0->hasOneUse() || SI1->hasOneUse())) {
1951 Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
1952 SI0->getName());
Craig Topper9d4171a2012-12-20 07:09:41 +00001953 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001954 SI1->getOperand(1));
1955 }
1956 }
1957
Chris Lattner0a8191e2010-01-05 07:50:36 +00001958 // (~A | ~B) == (~(A & B)) - De Morgan's Law
1959 if (Value *Op0NotVal = dyn_castNotVal(Op0))
1960 if (Value *Op1NotVal = dyn_castNotVal(Op1))
1961 if (Op0->hasOneUse() && Op1->hasOneUse()) {
1962 Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
1963 I.getName()+".demorgan");
1964 return BinaryOperator::CreateNot(And);
1965 }
1966
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00001967 // Canonicalize xor to the RHS.
Eli Friedmane06535b2012-03-16 00:52:42 +00001968 bool SwappedForXor = false;
1969 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00001970 std::swap(Op0, Op1);
Eli Friedmane06535b2012-03-16 00:52:42 +00001971 SwappedForXor = true;
1972 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00001973
1974 // A | ( A ^ B) -> A | B
1975 // A | (~A ^ B) -> A | ~B
Chad Rosier7813dce2012-04-26 23:29:14 +00001976 // (A & B) | (A ^ B)
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00001977 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
1978 if (Op0 == A || Op0 == B)
1979 return BinaryOperator::CreateOr(A, B);
1980
Chad Rosier7813dce2012-04-26 23:29:14 +00001981 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
1982 match(Op0, m_And(m_Specific(B), m_Specific(A))))
1983 return BinaryOperator::CreateOr(A, B);
1984
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00001985 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
1986 Value *Not = Builder->CreateNot(B, B->getName()+".not");
1987 return BinaryOperator::CreateOr(Not, Op0);
1988 }
1989 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
1990 Value *Not = Builder->CreateNot(A, A->getName()+".not");
1991 return BinaryOperator::CreateOr(Not, Op0);
1992 }
1993 }
1994
1995 // A | ~(A | B) -> A | ~B
1996 // A | ~(A ^ B) -> A | ~B
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00001997 if (match(Op1, m_Not(m_Value(A))))
1998 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
Benjamin Kramer5b7a4e02011-02-20 15:20:01 +00001999 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2000 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2001 B->getOpcode() == Instruction::Xor)) {
2002 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2003 B->getOperand(0);
2004 Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2005 return BinaryOperator::CreateOr(Not, Op0);
2006 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002007
Eli Friedmane06535b2012-03-16 00:52:42 +00002008 if (SwappedForXor)
2009 std::swap(Op0, Op1);
2010
Chris Lattner0a8191e2010-01-05 07:50:36 +00002011 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2012 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
Chris Lattner067459c2010-03-05 08:46:26 +00002013 if (Value *Res = FoldOrOfICmps(LHS, RHS))
2014 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002015
Chris Lattner4e8137d2010-02-11 06:26:33 +00002016 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
2017 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2018 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00002019 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2020 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002021
Chris Lattner0a8191e2010-01-05 07:50:36 +00002022 // fold (or (cast A), (cast B)) -> (cast (or A, B))
2023 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
Chris Lattner311aa632011-01-15 05:40:29 +00002024 CastInst *Op1C = dyn_cast<CastInst>(Op1);
2025 if (Op1C && Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
Chris Lattner229907c2011-07-18 04:54:35 +00002026 Type *SrcTy = Op0C->getOperand(0)->getType();
Chris Lattner311aa632011-01-15 05:40:29 +00002027 if (SrcTy == Op1C->getOperand(0)->getType() &&
2028 SrcTy->isIntOrIntVectorTy()) {
2029 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
Chris Lattner4e8137d2010-02-11 06:26:33 +00002030
Chris Lattner311aa632011-01-15 05:40:29 +00002031 if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
2032 // Only do this if the casts both really cause code to be
2033 // generated.
2034 ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
2035 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
2036 Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.getName());
2037 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002038 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002039
Chris Lattner311aa632011-01-15 05:40:29 +00002040 // If this is or(cast(icmp), cast(icmp)), try to fold this even if the
2041 // cast is otherwise not optimizable. This happens for vector sexts.
2042 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
2043 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
2044 if (Value *Res = FoldOrOfICmps(LHS, RHS))
2045 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Craig Topper9d4171a2012-12-20 07:09:41 +00002046
Chris Lattner311aa632011-01-15 05:40:29 +00002047 // If this is or(cast(fcmp), cast(fcmp)), try to fold this even if the
2048 // cast is otherwise not optimizable. This happens for vector sexts.
2049 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
2050 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
2051 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2052 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002053 }
Chris Lattner311aa632011-01-15 05:40:29 +00002054 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002055 }
Eli Friedman23956262011-04-14 22:41:27 +00002056
2057 // or(sext(A), B) -> A ? -1 : B where A is an i1
2058 // or(A, sext(B)) -> B ? -1 : A where B is an i1
2059 if (match(Op0, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2060 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
2061 if (match(Op1, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2062 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2063
Owen Andersonc237a842010-09-13 17:59:27 +00002064 // Note: If we've gotten to the point of visiting the outer OR, then the
2065 // inner one couldn't be simplified. If it was a constant, then it won't
2066 // be simplified by a later pass either, so we try swapping the inner/outer
2067 // ORs in the hopes that we'll be able to simplify it this way.
2068 // (X|C) | V --> (X|V) | C
2069 if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2070 match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2071 Value *Inner = Builder->CreateOr(A, Op1);
2072 Inner->takeName(Op0);
2073 return BinaryOperator::CreateOr(Inner, C1);
2074 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002075
Bill Wendling23242092013-02-16 23:41:36 +00002076 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2077 // Since this OR statement hasn't been optimized further yet, we hope
2078 // that this transformation will allow the new ORs to be optimized.
2079 {
2080 Value *X = 0, *Y = 0;
2081 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2082 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2083 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2084 Value *orTrue = Builder->CreateOr(A, C);
2085 Value *orFalse = Builder->CreateOr(B, D);
2086 return SelectInst::Create(X, orTrue, orFalse);
2087 }
2088 }
2089
Chris Lattner0a8191e2010-01-05 07:50:36 +00002090 return Changed ? &I : 0;
2091}
2092
2093Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00002094 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002095 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2096
Duncan Sandsc89ac072010-11-17 18:52:15 +00002097 if (Value *V = SimplifyXorInst(Op0, Op1, TD))
2098 return ReplaceInstUsesWith(I, V);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002099
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00002100 // (A&B)^(A&C) -> A&(B^C) etc
2101 if (Value *V = SimplifyUsingDistributiveLaws(I))
2102 return ReplaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002103
Craig Topper9d4171a2012-12-20 07:09:41 +00002104 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00002105 // purpose is to compute bits we don't care about.
2106 if (SimplifyDemandedInstructionBits(I))
2107 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002108
2109 // Is this a ~ operation?
2110 if (Value *NotOp = dyn_castNotVal(&I)) {
2111 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002112 if (Op0I->getOpcode() == Instruction::And ||
Chris Lattner0a8191e2010-01-05 07:50:36 +00002113 Op0I->getOpcode() == Instruction::Or) {
2114 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2115 // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2116 if (dyn_castNotVal(Op0I->getOperand(1)))
2117 Op0I->swapOperands();
2118 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2119 Value *NotY =
2120 Builder->CreateNot(Op0I->getOperand(1),
2121 Op0I->getOperand(1)->getName()+".not");
2122 if (Op0I->getOpcode() == Instruction::And)
2123 return BinaryOperator::CreateOr(Op0NotVal, NotY);
2124 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2125 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002126
Chris Lattner0a8191e2010-01-05 07:50:36 +00002127 // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2128 // ~(X | Y) === (~X & ~Y) - De Morgan's Law
Craig Topper9d4171a2012-12-20 07:09:41 +00002129 if (isFreeToInvert(Op0I->getOperand(0)) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002130 isFreeToInvert(Op0I->getOperand(1))) {
2131 Value *NotX =
2132 Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2133 Value *NotY =
2134 Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2135 if (Op0I->getOpcode() == Instruction::And)
2136 return BinaryOperator::CreateOr(NotX, NotY);
2137 return BinaryOperator::CreateAnd(NotX, NotY);
2138 }
Chris Lattner18f49ce2010-01-19 18:16:19 +00002139
2140 } else if (Op0I->getOpcode() == Instruction::AShr) {
2141 // ~(~X >>s Y) --> (X >>s Y)
2142 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2143 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002144 }
2145 }
2146 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002147
2148
Chris Lattner0a8191e2010-01-05 07:50:36 +00002149 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Dan Gohman0a8175d2010-04-09 14:53:59 +00002150 if (RHS->isOne() && Op0->hasOneUse())
Chris Lattner0a8191e2010-01-05 07:50:36 +00002151 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
Dan Gohman0a8175d2010-04-09 14:53:59 +00002152 if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2153 return CmpInst::Create(CI->getOpcode(),
2154 CI->getInversePredicate(),
2155 CI->getOperand(0), CI->getOperand(1));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002156
2157 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2158 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2159 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2160 if (CI->hasOneUse() && Op0C->hasOneUse()) {
2161 Instruction::CastOps Opcode = Op0C->getOpcode();
2162 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
Craig Topper9d4171a2012-12-20 07:09:41 +00002163 (RHS == ConstantExpr::getCast(Opcode,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002164 ConstantInt::getTrue(I.getContext()),
2165 Op0C->getDestTy()))) {
2166 CI->setPredicate(CI->getInversePredicate());
2167 return CastInst::Create(Opcode, CI, Op0C->getType());
2168 }
2169 }
2170 }
2171 }
2172
2173 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2174 // ~(c-X) == X-c-1 == X+(-c-1)
2175 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2176 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2177 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2178 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2179 ConstantInt::get(I.getType(), 1));
2180 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2181 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002182
Chris Lattner0a8191e2010-01-05 07:50:36 +00002183 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2184 if (Op0I->getOpcode() == Instruction::Add) {
2185 // ~(X-c) --> (-c-1)-X
2186 if (RHS->isAllOnesValue()) {
2187 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2188 return BinaryOperator::CreateSub(
2189 ConstantExpr::getSub(NegOp0CI,
2190 ConstantInt::get(I.getType(), 1)),
2191 Op0I->getOperand(0));
2192 } else if (RHS->getValue().isSignBit()) {
2193 // (X + C) ^ signbit -> (X + C + signbit)
2194 Constant *C = ConstantInt::get(I.getContext(),
2195 RHS->getValue() + Op0CI->getValue());
2196 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2197
2198 }
2199 } else if (Op0I->getOpcode() == Instruction::Or) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002200 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00002201 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
2202 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2203 // Anything in both C1 and C2 is known to be zero, remove it from
2204 // NewRHS.
2205 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
Craig Topper9d4171a2012-12-20 07:09:41 +00002206 NewRHS = ConstantExpr::getAnd(NewRHS,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002207 ConstantExpr::getNot(CommonBits));
2208 Worklist.Add(Op0I);
2209 I.setOperand(0, Op0I->getOperand(0));
2210 I.setOperand(1, NewRHS);
2211 return &I;
2212 }
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002213 } else if (Op0I->getOpcode() == Instruction::LShr) {
2214 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2215 // E1 = "X ^ C1"
Craig Topper9d4171a2012-12-20 07:09:41 +00002216 BinaryOperator *E1;
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002217 ConstantInt *C1;
2218 if (Op0I->hasOneUse() &&
2219 (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2220 E1->getOpcode() == Instruction::Xor &&
2221 (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2222 // fold (C1 >> C2) ^ C3
2223 ConstantInt *C2 = Op0CI, *C3 = RHS;
2224 APInt FoldConst = C1->getValue().lshr(C2->getValue());
2225 FoldConst ^= C3->getValue();
2226 // Prepare the two operands.
2227 Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2228 Opnd0->takeName(Op0I);
2229 cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2230 Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2231
2232 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2233 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002234 }
2235 }
2236 }
2237
2238 // Try to fold constant and into select arguments.
2239 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2240 if (Instruction *R = FoldOpIntoSelect(I, SI))
2241 return R;
2242 if (isa<PHINode>(Op0))
2243 if (Instruction *NV = FoldOpIntoPhi(I))
2244 return NV;
2245 }
2246
Chris Lattner0a8191e2010-01-05 07:50:36 +00002247 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2248 if (Op1I) {
2249 Value *A, *B;
2250 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2251 if (A == Op0) { // B^(B|A) == (A|B)^B
2252 Op1I->swapOperands();
2253 I.swapOperands();
2254 std::swap(Op0, Op1);
2255 } else if (B == Op0) { // B^(A|B) == (A|B)^B
2256 I.swapOperands(); // Simplified below.
2257 std::swap(Op0, Op1);
2258 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002259 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002260 Op1I->hasOneUse()){
2261 if (A == Op0) { // A^(A&B) -> A^(B&A)
2262 Op1I->swapOperands();
2263 std::swap(A, B);
2264 }
2265 if (B == Op0) { // A^(B&A) -> (B&A)^A
2266 I.swapOperands(); // Simplified below.
2267 std::swap(Op0, Op1);
2268 }
2269 }
2270 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002271
Chris Lattner0a8191e2010-01-05 07:50:36 +00002272 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2273 if (Op0I) {
2274 Value *A, *B;
2275 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2276 Op0I->hasOneUse()) {
2277 if (A == Op1) // (B|A)^B == (A|B)^B
2278 std::swap(A, B);
2279 if (B == Op1) // (A|B)^B == A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002280 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
Craig Topper9d4171a2012-12-20 07:09:41 +00002281 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002282 Op0I->hasOneUse()){
2283 if (A == Op1) // (A&B)^A -> (B&A)^A
2284 std::swap(A, B);
2285 if (B == Op1 && // (B&A)^A == ~B & A
2286 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002287 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002288 }
2289 }
2290 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002291
Chris Lattner0a8191e2010-01-05 07:50:36 +00002292 // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
Craig Topper9d4171a2012-12-20 07:09:41 +00002293 if (Op0I && Op1I && Op0I->isShift() &&
2294 Op0I->getOpcode() == Op1I->getOpcode() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002295 Op0I->getOperand(1) == Op1I->getOperand(1) &&
Benjamin Kramer9d5849f2012-05-28 20:52:48 +00002296 (Op0I->hasOneUse() || Op1I->hasOneUse())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002297 Value *NewOp =
2298 Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
2299 Op0I->getName());
Craig Topper9d4171a2012-12-20 07:09:41 +00002300 return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002301 Op1I->getOperand(1));
2302 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002303
Chris Lattner0a8191e2010-01-05 07:50:36 +00002304 if (Op0I && Op1I) {
2305 Value *A, *B, *C, *D;
2306 // (A & B)^(A | B) -> A ^ B
2307 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2308 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002309 if ((A == C && B == D) || (A == D && B == C))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002310 return BinaryOperator::CreateXor(A, B);
2311 }
2312 // (A | B)^(A & B) -> A ^ B
2313 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2314 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002315 if ((A == C && B == D) || (A == D && B == C))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002316 return BinaryOperator::CreateXor(A, B);
2317 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002318 }
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002319
Chris Lattner0a8191e2010-01-05 07:50:36 +00002320 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
2321 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2322 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
2323 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2324 if (LHS->getOperand(0) == RHS->getOperand(1) &&
2325 LHS->getOperand(1) == RHS->getOperand(0))
2326 LHS->swapOperands();
2327 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2328 LHS->getOperand(1) == RHS->getOperand(1)) {
2329 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2330 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2331 bool isSigned = LHS->isSigned() || RHS->isSigned();
Craig Topper9d4171a2012-12-20 07:09:41 +00002332 return ReplaceInstUsesWith(I,
Pete Cooperebf98c12011-12-17 01:20:32 +00002333 getNewICmpValue(isSigned, Code, Op0, Op1,
2334 Builder));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002335 }
2336 }
2337
2338 // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
2339 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2340 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
2341 if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
Chris Lattner229907c2011-07-18 04:54:35 +00002342 Type *SrcTy = Op0C->getOperand(0)->getType();
Duncan Sands9dff9be2010-02-15 16:12:20 +00002343 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegerTy() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002344 // Only do this if the casts both really cause code to be generated.
Craig Topper9d4171a2012-12-20 07:09:41 +00002345 ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
Chris Lattner4e8137d2010-02-11 06:26:33 +00002346 I.getType()) &&
Craig Topper9d4171a2012-12-20 07:09:41 +00002347 ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
Chris Lattner4e8137d2010-02-11 06:26:33 +00002348 I.getType())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002349 Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
2350 Op1C->getOperand(0), I.getName());
2351 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2352 }
2353 }
2354 }
2355
2356 return Changed ? &I : 0;
2357}