blob: 099a78085b74af0eb3588a454d0e385b96232d32 [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.
Jakub Staszak9de494e2013-06-06 00:49:57 +0000176 const APInt &AndRHSV = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000177
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.
Jakub Staszak9de494e2013-06-06 00:49:57 +0000183 const APInt& AddRHS = OpRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000184
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));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000212 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000213
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000214 if (CI->getValue() == ShlMask)
215 // Masking out bits that the shift already masks.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000216 return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
Craig Topper9d4171a2012-12-20 07:09:41 +0000217
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000218 if (CI != AndRHS) { // Reducing bits set in and.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000219 TheAnd.setOperand(1, CI);
220 return &TheAnd;
221 }
222 break;
223 }
224 case Instruction::LShr: {
225 // We know that the AND will not produce any of the bits shifted in, so if
226 // the anded constant includes them, clear them now! This only applies to
227 // unsigned shifts, because a signed shr may bring in set bits!
228 //
229 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
230 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
231 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000232 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000233
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000234 if (CI->getValue() == ShrMask)
235 // Masking out bits that the shift already masks.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000236 return ReplaceInstUsesWith(TheAnd, Op);
Craig Topper9d4171a2012-12-20 07:09:41 +0000237
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000238 if (CI != AndRHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000239 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
240 return &TheAnd;
241 }
242 break;
243 }
244 case Instruction::AShr:
245 // Signed shr.
246 // See if this is shifting in some sign extension, then masking it out
247 // with an and.
248 if (Op->hasOneUse()) {
249 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
250 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
251 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000252 Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000253 if (C == AndRHS) { // Masking out bits shifted in.
254 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
255 // Make the argument unsigned.
256 Value *ShVal = Op->getOperand(0);
257 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
258 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
259 }
260 }
261 break;
262 }
263 return 0;
264}
265
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000266/// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
267/// (V < Lo || V >= Hi). In practice, we emit the more efficient
NAKAMURA Takumi00d2a102012-11-15 00:35:50 +0000268/// (V-Lo) \<u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates
Chris Lattner0a8191e2010-01-05 07:50:36 +0000269/// whether to treat the V, Lo and HI as signed or not. IB is the location to
270/// insert new instructions.
Chris Lattner067459c2010-03-05 08:46:26 +0000271Value *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
272 bool isSigned, bool Inside) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000273 assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
Chris Lattner0a8191e2010-01-05 07:50:36 +0000274 ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
275 "Lo is not <= Hi in range emission code!");
Craig Topper9d4171a2012-12-20 07:09:41 +0000276
Chris Lattner0a8191e2010-01-05 07:50:36 +0000277 if (Inside) {
278 if (Lo == Hi) // Trivially false.
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000279 return Builder->getFalse();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000280
281 // V >= Min && V < Hi --> V < Hi
282 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000283 ICmpInst::Predicate pred = (isSigned ?
Chris Lattner0a8191e2010-01-05 07:50:36 +0000284 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
Chris Lattner067459c2010-03-05 08:46:26 +0000285 return Builder->CreateICmp(pred, V, Hi);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000286 }
287
288 // Emit V-Lo <u Hi-Lo
289 Constant *NegLo = ConstantExpr::getNeg(Lo);
290 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
291 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
Chris Lattner067459c2010-03-05 08:46:26 +0000292 return Builder->CreateICmpULT(Add, UpperBound);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000293 }
294
295 if (Lo == Hi) // Trivially true.
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000296 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000297
298 // V < Min || V >= Hi -> V > Hi-1
299 Hi = SubOne(cast<ConstantInt>(Hi));
300 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000301 ICmpInst::Predicate pred = (isSigned ?
Chris Lattner0a8191e2010-01-05 07:50:36 +0000302 ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
Chris Lattner067459c2010-03-05 08:46:26 +0000303 return Builder->CreateICmp(pred, V, Hi);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000304 }
305
306 // Emit V-Lo >u Hi-1-Lo
307 // Note that Hi has already had one subtracted from it, above.
308 ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
309 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
310 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
Chris Lattner067459c2010-03-05 08:46:26 +0000311 return Builder->CreateICmpUGT(Add, LowerBound);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000312}
313
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000314// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
Chris Lattner0a8191e2010-01-05 07:50:36 +0000315// any number of 0s on either side. The 1s are allowed to wrap from LSB to
316// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
317// not, since all 1s are not contiguous.
318static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
319 const APInt& V = Val->getValue();
320 uint32_t BitWidth = Val->getType()->getBitWidth();
321 if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
322
323 // look for the first zero bit after the run of ones
324 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
325 // look for the first non-zero bit
Craig Topper9d4171a2012-12-20 07:09:41 +0000326 ME = V.getActiveBits();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000327 return true;
328}
329
330/// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
331/// where isSub determines whether the operator is a sub. If we can fold one of
332/// the following xforms:
Craig Topper9d4171a2012-12-20 07:09:41 +0000333///
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000334/// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
335/// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
336/// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +0000337///
338/// return (A +/- B).
339///
340Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
341 ConstantInt *Mask, bool isSub,
342 Instruction &I) {
343 Instruction *LHSI = dyn_cast<Instruction>(LHS);
344 if (!LHSI || LHSI->getNumOperands() != 2 ||
345 !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
346
347 ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
348
349 switch (LHSI->getOpcode()) {
350 default: return 0;
351 case Instruction::And:
352 if (ConstantExpr::getAnd(N, Mask) == Mask) {
353 // If the AndRHS is a power of two minus one (0+1+), this is simple.
Craig Topper9d4171a2012-12-20 07:09:41 +0000354 if ((Mask->getValue().countLeadingZeros() +
355 Mask->getValue().countPopulation()) ==
Chris Lattner0a8191e2010-01-05 07:50:36 +0000356 Mask->getValue().getBitWidth())
357 break;
358
359 // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
360 // part, we don't need any explicit masks to take them out of A. If that
361 // is all N is, ignore it.
362 uint32_t MB = 0, ME = 0;
363 if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
364 uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
365 APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
366 if (MaskedValueIsZero(RHS, Mask))
367 break;
368 }
369 }
370 return 0;
371 case Instruction::Or:
372 case Instruction::Xor:
373 // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
Craig Topper9d4171a2012-12-20 07:09:41 +0000374 if ((Mask->getValue().countLeadingZeros() +
Chris Lattner0a8191e2010-01-05 07:50:36 +0000375 Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
376 && ConstantExpr::getAnd(N, Mask)->isNullValue())
377 break;
378 return 0;
379 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000380
Chris Lattner0a8191e2010-01-05 07:50:36 +0000381 if (isSub)
382 return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
383 return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
384}
385
Owen Anderson3fe002d2010-09-08 22:16:17 +0000386/// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C)
Craig Topper9d4171a2012-12-20 07:09:41 +0000387/// One of A and B is considered the mask, the other the value. This is
388/// described as the "AMask" or "BMask" part of the enum. If the enum
Owen Anderson3fe002d2010-09-08 22:16:17 +0000389/// contains only "Mask", then both A and B can be considered masks.
390/// If A is the mask, then it was proven, that (A & C) == C. This
391/// is trivial if C == A, or C == 0. If both A and C are constants, this
392/// proof is also easy.
393/// For the following explanations we assume that A is the mask.
Craig Topper9d4171a2012-12-20 07:09:41 +0000394/// The part "AllOnes" declares, that the comparison is true only
Owen Anderson3fe002d2010-09-08 22:16:17 +0000395/// if (A & B) == A, or all bits of A are set in B.
396/// Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes
Craig Topper9d4171a2012-12-20 07:09:41 +0000397/// The part "AllZeroes" declares, that the comparison is true only
Owen Anderson3fe002d2010-09-08 22:16:17 +0000398/// if (A & B) == 0, or all bits of A are cleared in B.
399/// Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes
Craig Topper9d4171a2012-12-20 07:09:41 +0000400/// The part "Mixed" declares, that (A & B) == C and C might or might not
Owen Anderson3fe002d2010-09-08 22:16:17 +0000401/// contain any number of one bits and zero bits.
402/// Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed
403/// The Part "Not" means, that in above descriptions "==" should be replaced
404/// by "!=".
405/// Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes
406/// If the mask A contains a single bit, then the following is equivalent:
407/// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
408/// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
409enum MaskedICmpType {
410 FoldMskICmp_AMask_AllOnes = 1,
411 FoldMskICmp_AMask_NotAllOnes = 2,
412 FoldMskICmp_BMask_AllOnes = 4,
413 FoldMskICmp_BMask_NotAllOnes = 8,
414 FoldMskICmp_Mask_AllZeroes = 16,
415 FoldMskICmp_Mask_NotAllZeroes = 32,
416 FoldMskICmp_AMask_Mixed = 64,
417 FoldMskICmp_AMask_NotMixed = 128,
418 FoldMskICmp_BMask_Mixed = 256,
419 FoldMskICmp_BMask_NotMixed = 512
420};
421
422/// return the set of pattern classes (from MaskedICmpType)
423/// that (icmp SCC (A & B), C) satisfies
Craig Topper9d4171a2012-12-20 07:09:41 +0000424static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000425 ICmpInst::Predicate SCC)
426{
427 ConstantInt *ACst = dyn_cast<ConstantInt>(A);
428 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
429 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
430 bool icmp_eq = (SCC == ICmpInst::ICMP_EQ);
Craig Topper9d4171a2012-12-20 07:09:41 +0000431 bool icmp_abit = (ACst != 0 && !ACst->isZero() &&
Owen Anderson3fe002d2010-09-08 22:16:17 +0000432 ACst->getValue().isPowerOf2());
Craig Topper9d4171a2012-12-20 07:09:41 +0000433 bool icmp_bbit = (BCst != 0 && !BCst->isZero() &&
Owen Anderson3fe002d2010-09-08 22:16:17 +0000434 BCst->getValue().isPowerOf2());
435 unsigned result = 0;
436 if (CCst != 0 && CCst->isZero()) {
437 // if C is zero, then both A and B qualify as mask
438 result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes |
439 FoldMskICmp_Mask_AllZeroes |
440 FoldMskICmp_AMask_Mixed |
441 FoldMskICmp_BMask_Mixed)
442 : (FoldMskICmp_Mask_NotAllZeroes |
443 FoldMskICmp_Mask_NotAllZeroes |
444 FoldMskICmp_AMask_NotMixed |
445 FoldMskICmp_BMask_NotMixed));
446 if (icmp_abit)
447 result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000448 FoldMskICmp_AMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000449 : (FoldMskICmp_AMask_AllOnes |
450 FoldMskICmp_AMask_Mixed));
451 if (icmp_bbit)
452 result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000453 FoldMskICmp_BMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000454 : (FoldMskICmp_BMask_AllOnes |
455 FoldMskICmp_BMask_Mixed));
456 return result;
457 }
458 if (A == C) {
459 result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes |
460 FoldMskICmp_AMask_Mixed)
461 : (FoldMskICmp_AMask_NotAllOnes |
462 FoldMskICmp_AMask_NotMixed));
463 if (icmp_abit)
464 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
465 FoldMskICmp_AMask_NotMixed)
466 : (FoldMskICmp_Mask_AllZeroes |
467 FoldMskICmp_AMask_Mixed));
Craig Topperae48cb22012-12-20 07:15:54 +0000468 } else if (ACst != 0 && CCst != 0 &&
469 ConstantExpr::getAnd(ACst, CCst) == CCst) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000470 result |= (icmp_eq ? FoldMskICmp_AMask_Mixed
471 : FoldMskICmp_AMask_NotMixed);
472 }
Craig Topperae48cb22012-12-20 07:15:54 +0000473 if (B == C) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000474 result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes |
475 FoldMskICmp_BMask_Mixed)
476 : (FoldMskICmp_BMask_NotAllOnes |
477 FoldMskICmp_BMask_NotMixed));
478 if (icmp_bbit)
479 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000480 FoldMskICmp_BMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000481 : (FoldMskICmp_Mask_AllZeroes |
482 FoldMskICmp_BMask_Mixed));
Craig Topperae48cb22012-12-20 07:15:54 +0000483 } else if (BCst != 0 && CCst != 0 &&
484 ConstantExpr::getAnd(BCst, CCst) == CCst) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000485 result |= (icmp_eq ? FoldMskICmp_BMask_Mixed
486 : FoldMskICmp_BMask_NotMixed);
487 }
488 return result;
489}
490
Tim Northoverc0756c42013-09-04 11:57:13 +0000491/// Convert an analysis of a masked ICmp into its equivalent if all boolean
492/// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
493/// is adjacent to the corresponding normal flag (recording ==), this just
494/// involves swapping those bits over.
495static unsigned conjugateICmpMask(unsigned Mask) {
496 unsigned NewMask;
497 NewMask = (Mask & (FoldMskICmp_AMask_AllOnes | FoldMskICmp_BMask_AllOnes |
498 FoldMskICmp_Mask_AllZeroes | FoldMskICmp_AMask_Mixed |
499 FoldMskICmp_BMask_Mixed))
500 << 1;
501
502 NewMask |=
503 (Mask & (FoldMskICmp_AMask_NotAllOnes | FoldMskICmp_BMask_NotAllOnes |
504 FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_AMask_NotMixed |
505 FoldMskICmp_BMask_NotMixed))
506 >> 1;
507
508 return NewMask;
509}
510
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000511/// decomposeBitTestICmp - Decompose an icmp into the form ((X & Y) pred Z)
512/// if possible. The returned predicate is either == or !=. Returns false if
513/// decomposition fails.
514static bool decomposeBitTestICmp(const ICmpInst *I, ICmpInst::Predicate &Pred,
515 Value *&X, Value *&Y, Value *&Z) {
516 // X < 0 is equivalent to (X & SignBit) != 0.
517 if (I->getPredicate() == ICmpInst::ICMP_SLT)
518 if (ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
519 if (C->isZero()) {
520 X = I->getOperand(0);
521 Y = ConstantInt::get(I->getContext(),
522 APInt::getSignBit(C->getBitWidth()));
523 Pred = ICmpInst::ICMP_NE;
524 Z = C;
525 return true;
526 }
527
528 // X > -1 is equivalent to (X & SignBit) == 0.
529 if (I->getPredicate() == ICmpInst::ICMP_SGT)
530 if (ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
531 if (C->isAllOnesValue()) {
532 X = I->getOperand(0);
533 Y = ConstantInt::get(I->getContext(),
534 APInt::getSignBit(C->getBitWidth()));
535 Pred = ICmpInst::ICMP_EQ;
536 Z = ConstantInt::getNullValue(C->getType());
537 return true;
538 }
539
540 return false;
541}
542
Owen Anderson3fe002d2010-09-08 22:16:17 +0000543/// foldLogOpOfMaskedICmpsHelper:
544/// handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
545/// return the set of pattern classes (from MaskedICmpType)
546/// that both LHS and RHS satisfy
Craig Topper9d4171a2012-12-20 07:09:41 +0000547static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000548 Value*& B, Value*& C,
549 Value*& D, Value*& E,
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000550 ICmpInst *LHS, ICmpInst *RHS,
551 ICmpInst::Predicate &LHSCC,
552 ICmpInst::Predicate &RHSCC) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000553 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0;
554 // vectors are not (yet?) supported
555 if (LHS->getOperand(0)->getType()->isVectorTy()) return 0;
556
557 // Here comes the tricky part:
Craig Topper9d4171a2012-12-20 07:09:41 +0000558 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000559 // and L11 & L12 == L21 & L22. The same goes for RHS.
560 // Now we must find those components L** and R**, that are equal, so
Craig Topper9d4171a2012-12-20 07:09:41 +0000561 // that we can extract the parameters A, B, C, D, and E for the canonical
Owen Anderson3fe002d2010-09-08 22:16:17 +0000562 // above.
563 Value *L1 = LHS->getOperand(0);
564 Value *L2 = LHS->getOperand(1);
565 Value *L11,*L12,*L21,*L22;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000566 // Check whether the icmp can be decomposed into a bit test.
567 if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) {
568 L21 = L22 = L1 = 0;
569 } else {
570 // Look for ANDs in the LHS icmp.
571 if (match(L1, m_And(m_Value(L11), m_Value(L12)))) {
572 if (!match(L2, m_And(m_Value(L21), m_Value(L22))))
573 L21 = L22 = 0;
574 } else {
575 if (!match(L2, m_And(m_Value(L11), m_Value(L12))))
576 return 0;
577 std::swap(L1, L2);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000578 L21 = L22 = 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000579 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000580 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000581
582 // Bail if LHS was a icmp that can't be decomposed into an equality.
583 if (!ICmpInst::isEquality(LHSCC))
584 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000585
586 Value *R1 = RHS->getOperand(0);
587 Value *R2 = RHS->getOperand(1);
588 Value *R11,*R12;
589 bool ok = false;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000590 if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) {
591 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
592 A = R11; D = R12;
593 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
594 A = R12; D = R11;
595 } else {
596 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000597 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000598 E = R2; R1 = 0; ok = true;
599 } else if (match(R1, m_And(m_Value(R11), m_Value(R12)))) {
600 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
601 A = R11; D = R12; E = R2; ok = true;
602 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000603 A = R12; D = R11; E = R2; ok = true;
604 }
605 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000606
607 // Bail if RHS was a icmp that can't be decomposed into an equality.
608 if (!ICmpInst::isEquality(RHSCC))
609 return 0;
610
611 // Look for ANDs in on the right side of the RHS icmp.
Owen Anderson3fe002d2010-09-08 22:16:17 +0000612 if (!ok && match(R2, m_And(m_Value(R11), m_Value(R12)))) {
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000613 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
614 A = R11; D = R12; E = R1; ok = true;
615 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000616 A = R12; D = R11; E = R1; ok = true;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000617 } else {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000618 return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000619 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000620 }
621 if (!ok)
622 return 0;
623
624 if (L11 == A) {
625 B = L12; C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000626 } else if (L12 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000627 B = L11; C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000628 } else if (L21 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000629 B = L22; C = L1;
Craig Topperae48cb22012-12-20 07:15:54 +0000630 } else if (L22 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000631 B = L21; C = L1;
632 }
633
634 unsigned left_type = getTypeOfMaskedICmp(A, B, C, LHSCC);
635 unsigned right_type = getTypeOfMaskedICmp(A, D, E, RHSCC);
636 return left_type & right_type;
637}
638/// foldLogOpOfMaskedICmps:
639/// try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
640/// into a single (icmp(A & X) ==/!= Y)
Tim Northoverc0756c42013-09-04 11:57:13 +0000641static Value* foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000642 llvm::InstCombiner::BuilderTy* Builder) {
643 Value *A = 0, *B = 0, *C = 0, *D = 0, *E = 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000644 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
645 unsigned mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS,
646 LHSCC, RHSCC);
Benjamin Kramerf7fe24f2012-01-09 17:36:29 +0000647 if (mask == 0) return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000648 assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) &&
649 "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
Owen Anderson3fe002d2010-09-08 22:16:17 +0000650
Tim Northoverc0756c42013-09-04 11:57:13 +0000651 // In full generality:
652 // (icmp (A & B) Op C) | (icmp (A & D) Op E)
653 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
654 //
655 // If the latter can be converted into (icmp (A & X) Op Y) then the former is
656 // equivalent to (icmp (A & X) !Op Y).
657 //
658 // Therefore, we can pretend for the rest of this function that we're dealing
659 // with the conjunction, provided we flip the sense of any comparisons (both
660 // input and output).
661
662 // In most cases we're going to produce an EQ for the "&&" case.
663 ICmpInst::Predicate NEWCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
664 if (!IsAnd) {
665 // Convert the masking analysis into its equivalent with negated
666 // comparisons.
667 mask = conjugateICmpMask(mask);
668 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000669
670 if (mask & FoldMskICmp_Mask_AllZeroes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000671 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000672 // -> (icmp eq (A & (B|D)), 0)
673 Value* newOr = Builder->CreateOr(B, D);
674 Value* newAnd = Builder->CreateAnd(A, newOr);
675 // we can't use C as zero, because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000676 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000677 // with B and D, having a single bit set
678 Value* zero = Constant::getNullValue(A->getType());
679 return Builder->CreateICmp(NEWCC, newAnd, zero);
680 }
Craig Topperae48cb22012-12-20 07:15:54 +0000681 if (mask & FoldMskICmp_BMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000682 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000683 // -> (icmp eq (A & (B|D)), (B|D))
684 Value* newOr = Builder->CreateOr(B, D);
685 Value* newAnd = Builder->CreateAnd(A, newOr);
686 return Builder->CreateICmp(NEWCC, newAnd, newOr);
Craig Topper9d4171a2012-12-20 07:09:41 +0000687 }
Craig Topperae48cb22012-12-20 07:15:54 +0000688 if (mask & FoldMskICmp_AMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000689 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000690 // -> (icmp eq (A & (B&D)), A)
691 Value* newAnd1 = Builder->CreateAnd(B, D);
692 Value* newAnd = Builder->CreateAnd(A, newAnd1);
693 return Builder->CreateICmp(NEWCC, newAnd, A);
694 }
Tim Northoverc0756c42013-09-04 11:57:13 +0000695
696 // Remaining cases assume at least that B and D are constant, and depend on
697 // their actual values. This isn't strictly, necessary, just a "handle the
698 // easy cases for now" decision.
699 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
700 if (BCst == 0) return 0;
701 ConstantInt *DCst = dyn_cast<ConstantInt>(D);
702 if (DCst == 0) return 0;
703
704 if (mask & (FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_BMask_NotAllOnes)) {
705 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
706 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
707 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
708 // Only valid if one of the masks is a superset of the other (check "B&D" is
709 // the same as either B or D).
710 APInt NewMask = BCst->getValue() & DCst->getValue();
711
712 if (NewMask == BCst->getValue())
713 return LHS;
714 else if (NewMask == DCst->getValue())
715 return RHS;
716 }
717 if (mask & FoldMskICmp_AMask_NotAllOnes) {
718 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
719 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
720 // Only valid if one of the masks is a superset of the other (check "B|D" is
721 // the same as either B or D).
722 APInt NewMask = BCst->getValue() | DCst->getValue();
723
724 if (NewMask == BCst->getValue())
725 return LHS;
726 else if (NewMask == DCst->getValue())
727 return RHS;
728 }
Craig Topperae48cb22012-12-20 07:15:54 +0000729 if (mask & FoldMskICmp_BMask_Mixed) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000730 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000731 // We already know that B & C == C && D & E == E.
732 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
733 // C and E, which are shared by both the mask B and the mask D, don't
734 // contradict, then we can transform to
735 // -> (icmp eq (A & (B|D)), (C|E))
736 // Currently, we only handle the case of B, C, D, and E being constant.
Owen Anderson3fe002d2010-09-08 22:16:17 +0000737 // we can't simply use C and E, because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000738 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000739 // with B and D, having a single bit set
Owen Anderson3fe002d2010-09-08 22:16:17 +0000740 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
741 if (CCst == 0) return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000742 if (LHSCC != NEWCC)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000743 CCst = dyn_cast<ConstantInt>( ConstantExpr::getXor(BCst, CCst) );
744 ConstantInt *ECst = dyn_cast<ConstantInt>(E);
745 if (ECst == 0) return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000746 if (RHSCC != NEWCC)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000747 ECst = dyn_cast<ConstantInt>( ConstantExpr::getXor(DCst, ECst) );
748 ConstantInt* MCst = dyn_cast<ConstantInt>(
749 ConstantExpr::getAnd(ConstantExpr::getAnd(BCst, DCst),
750 ConstantExpr::getXor(CCst, ECst)) );
751 // if there is a conflict we should actually return a false for the
752 // whole construct
753 if (!MCst->isZero())
754 return 0;
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000755 Value *newOr1 = Builder->CreateOr(B, D);
756 Value *newOr2 = ConstantExpr::getOr(CCst, ECst);
757 Value *newAnd = Builder->CreateAnd(A, newOr1);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000758 return Builder->CreateICmp(NEWCC, newAnd, newOr2);
759 }
760 return 0;
761}
762
Chris Lattner0a8191e2010-01-05 07:50:36 +0000763/// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
Chris Lattner067459c2010-03-05 08:46:26 +0000764Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000765 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
766
767 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
768 if (PredicatesFoldable(LHSCC, RHSCC)) {
769 if (LHS->getOperand(0) == RHS->getOperand(1) &&
770 LHS->getOperand(1) == RHS->getOperand(0))
771 LHS->swapOperands();
772 if (LHS->getOperand(0) == RHS->getOperand(0) &&
773 LHS->getOperand(1) == RHS->getOperand(1)) {
774 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
775 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
776 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +0000777 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000778 }
779 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000780
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000781 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
Tim Northoverc0756c42013-09-04 11:57:13 +0000782 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000783 return V;
Craig Topper9d4171a2012-12-20 07:09:41 +0000784
Chris Lattner0a8191e2010-01-05 07:50:36 +0000785 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
786 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
787 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
788 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
789 if (LHSCst == 0 || RHSCst == 0) return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +0000790
Chris Lattner0a8191e2010-01-05 07:50:36 +0000791 if (LHSCst == RHSCst && LHSCC == RHSCC) {
792 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
793 // where C is a power of 2
794 if (LHSCC == ICmpInst::ICMP_ULT &&
795 LHSCst->getValue().isPowerOf2()) {
796 Value *NewOr = Builder->CreateOr(Val, Val2);
Chris Lattner067459c2010-03-05 08:46:26 +0000797 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000798 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000799
Chris Lattner0a8191e2010-01-05 07:50:36 +0000800 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
801 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
802 Value *NewOr = Builder->CreateOr(Val, Val2);
Chris Lattner067459c2010-03-05 08:46:26 +0000803 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000804 }
805 }
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000806
Benjamin Kramer101720f2011-04-28 20:09:57 +0000807 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000808 // where CMAX is the all ones value for the truncated type,
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000809 // iff the lower bits of C2 and CA are zero.
Bill Wendlingf2c78f32012-02-29 01:46:50 +0000810 if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000811 LHS->hasOneUse() && RHS->hasOneUse()) {
812 Value *V;
813 ConstantInt *AndCst, *SmallCst = 0, *BigCst = 0;
814
815 // (trunc x) == C1 & (and x, CA) == C2
Craig Topperae48cb22012-12-20 07:15:54 +0000816 // (and x, CA) == C2 & (trunc x) == C1
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000817 if (match(Val2, m_Trunc(m_Value(V))) &&
818 match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
819 SmallCst = RHSCst;
820 BigCst = LHSCst;
Craig Topperae48cb22012-12-20 07:15:54 +0000821 } else if (match(Val, m_Trunc(m_Value(V))) &&
822 match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000823 SmallCst = LHSCst;
824 BigCst = RHSCst;
825 }
826
827 if (SmallCst && BigCst) {
828 unsigned BigBitSize = BigCst->getType()->getBitWidth();
829 unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
830
831 // Check that the low bits are zero.
832 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
Benjamin Kramercf9d1ad2011-04-28 21:38:51 +0000833 if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000834 Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
835 APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
836 Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
837 return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
838 }
839 }
840 }
Benjamin Kramerda37e152012-01-08 18:32:24 +0000841
Chris Lattner0a8191e2010-01-05 07:50:36 +0000842 // From here on, we only handle:
843 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
844 if (Val != Val2) return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +0000845
Chris Lattner0a8191e2010-01-05 07:50:36 +0000846 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
847 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
848 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
849 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
850 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
851 return 0;
Anders Carlssonda80afe2011-03-01 15:05:01 +0000852
853 // Make a constant range that's the intersection of the two icmp ranges.
854 // If the intersection is empty, we know that the result is false.
Craig Topper9d4171a2012-12-20 07:09:41 +0000855 ConstantRange LHSRange =
Anders Carlssonda80afe2011-03-01 15:05:01 +0000856 ConstantRange::makeICmpRegion(LHSCC, LHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +0000857 ConstantRange RHSRange =
Anders Carlssonda80afe2011-03-01 15:05:01 +0000858 ConstantRange::makeICmpRegion(RHSCC, RHSCst->getValue());
859
860 if (LHSRange.intersectWith(RHSRange).isEmptySet())
861 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
862
Chris Lattner0a8191e2010-01-05 07:50:36 +0000863 // We can't fold (ugt x, C) & (sgt x, C2).
864 if (!PredicatesFoldable(LHSCC, RHSCC))
865 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +0000866
Chris Lattner0a8191e2010-01-05 07:50:36 +0000867 // Ensure that the larger constant is on the RHS.
868 bool ShouldSwap;
869 if (CmpInst::isSigned(LHSCC) ||
Craig Topper9d4171a2012-12-20 07:09:41 +0000870 (ICmpInst::isEquality(LHSCC) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +0000871 CmpInst::isSigned(RHSCC)))
872 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
873 else
874 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +0000875
Chris Lattner0a8191e2010-01-05 07:50:36 +0000876 if (ShouldSwap) {
877 std::swap(LHS, RHS);
878 std::swap(LHSCst, RHSCst);
879 std::swap(LHSCC, RHSCC);
880 }
881
Dan Gohman4a618822010-02-10 16:03:48 +0000882 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +0000883 // comparing a value against two constants and and'ing the result
884 // together. Because of the above check, we know that we only have
Craig Topper9d4171a2012-12-20 07:09:41 +0000885 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
886 // (from the icmp folding check above), that the two constants
Chris Lattner0a8191e2010-01-05 07:50:36 +0000887 // are not equal and that the larger constant is on the RHS
888 assert(LHSCst != RHSCst && "Compares not folded above?");
889
890 switch (LHSCC) {
891 default: llvm_unreachable("Unknown integer condition code!");
892 case ICmpInst::ICMP_EQ:
893 switch (RHSCC) {
894 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000895 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
896 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
897 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
Chris Lattner067459c2010-03-05 08:46:26 +0000898 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000899 }
900 case ICmpInst::ICMP_NE:
901 switch (RHSCC) {
902 default: llvm_unreachable("Unknown integer condition code!");
903 case ICmpInst::ICMP_ULT:
904 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000905 return Builder->CreateICmpULT(Val, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000906 break; // (X != 13 & X u< 15) -> no change
907 case ICmpInst::ICMP_SLT:
908 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000909 return Builder->CreateICmpSLT(Val, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000910 break; // (X != 13 & X s< 15) -> no change
911 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
912 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
913 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000914 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000915 case ICmpInst::ICMP_NE:
Jim Grosbach20e3b9a2013-08-16 00:15:20 +0000916 // Special case to get the ordering right when the values wrap around
917 // zero.
Jim Grosbachd0de8ac2013-08-16 17:03:36 +0000918 if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue())
Jim Grosbach20e3b9a2013-08-16 00:15:20 +0000919 std::swap(LHSCst, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000920 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
921 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
922 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
Jim Grosbach20e3b9a2013-08-16 00:15:20 +0000923 return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1),
924 Val->getName()+".cmp");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000925 }
926 break; // (X != 13 & X != 15) -> no change
927 }
928 break;
929 case ICmpInst::ICMP_ULT:
930 switch (RHSCC) {
931 default: llvm_unreachable("Unknown integer condition code!");
932 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
933 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
Chris Lattner067459c2010-03-05 08:46:26 +0000934 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000935 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
936 break;
937 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
938 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
Chris Lattner067459c2010-03-05 08:46:26 +0000939 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000940 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
941 break;
942 }
943 break;
944 case ICmpInst::ICMP_SLT:
945 switch (RHSCC) {
946 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000947 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
948 break;
949 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
950 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000951 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000952 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
953 break;
954 }
955 break;
956 case ICmpInst::ICMP_UGT:
957 switch (RHSCC) {
958 default: llvm_unreachable("Unknown integer condition code!");
959 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15
960 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000961 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000962 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
963 break;
964 case ICmpInst::ICMP_NE:
965 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
Chris Lattner067459c2010-03-05 08:46:26 +0000966 return Builder->CreateICmp(LHSCC, Val, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000967 break; // (X u> 13 & X != 15) -> no change
968 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
Chris Lattner067459c2010-03-05 08:46:26 +0000969 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000970 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
971 break;
972 }
973 break;
974 case ICmpInst::ICMP_SGT:
975 switch (RHSCC) {
976 default: llvm_unreachable("Unknown integer condition code!");
977 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
978 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000979 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000980 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
981 break;
982 case ICmpInst::ICMP_NE:
983 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
Chris Lattner067459c2010-03-05 08:46:26 +0000984 return Builder->CreateICmp(LHSCC, Val, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000985 break; // (X s> 13 & X != 15) -> no change
986 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
Chris Lattner067459c2010-03-05 08:46:26 +0000987 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000988 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
989 break;
990 }
991 break;
992 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000993
Chris Lattner0a8191e2010-01-05 07:50:36 +0000994 return 0;
995}
996
Chris Lattner067459c2010-03-05 08:46:26 +0000997/// FoldAndOfFCmps - Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of
998/// instcombine, this returns a Value which should already be inserted into the
999/// function.
1000Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001001 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
1002 RHS->getPredicate() == FCmpInst::FCMP_ORD) {
Benjamin Kramere89c7052013-04-12 21:56:23 +00001003 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
1004 return 0;
1005
Chris Lattner0a8191e2010-01-05 07:50:36 +00001006 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
1007 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1008 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1009 // If either of the constants are nans, then the whole thing returns
1010 // false.
1011 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001012 return Builder->getFalse();
Chris Lattner067459c2010-03-05 08:46:26 +00001013 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001014 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001015
Chris Lattner0a8191e2010-01-05 07:50:36 +00001016 // Handle vector zeros. This occurs because the canonical form of
1017 // "fcmp ord x,x" is "fcmp ord x, 0".
1018 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1019 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001020 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001021 return 0;
1022 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001023
Chris Lattner0a8191e2010-01-05 07:50:36 +00001024 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1025 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1026 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
Craig Topper9d4171a2012-12-20 07:09:41 +00001027
1028
Chris Lattner0a8191e2010-01-05 07:50:36 +00001029 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1030 // Swap RHS operands to match LHS.
1031 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1032 std::swap(Op1LHS, Op1RHS);
1033 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001034
Chris Lattner0a8191e2010-01-05 07:50:36 +00001035 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1036 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1037 if (Op0CC == Op1CC)
Chris Lattner067459c2010-03-05 08:46:26 +00001038 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001039 if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
Chris Lattner067459c2010-03-05 08:46:26 +00001040 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001041 if (Op0CC == FCmpInst::FCMP_TRUE)
Chris Lattner067459c2010-03-05 08:46:26 +00001042 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001043 if (Op1CC == FCmpInst::FCMP_TRUE)
Chris Lattner067459c2010-03-05 08:46:26 +00001044 return LHS;
Craig Topper9d4171a2012-12-20 07:09:41 +00001045
Chris Lattner0a8191e2010-01-05 07:50:36 +00001046 bool Op0Ordered;
1047 bool Op1Ordered;
1048 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1049 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
Chad Rosierfaa38942012-06-06 17:22:40 +00001050 // uno && ord -> false
1051 if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered)
1052 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001053 if (Op1Pred == 0) {
1054 std::swap(LHS, RHS);
1055 std::swap(Op0Pred, Op1Pred);
1056 std::swap(Op0Ordered, Op1Ordered);
1057 }
1058 if (Op0Pred == 0) {
Manman Renc2bc2d12012-06-14 05:57:42 +00001059 // uno && ueq -> uno && (uno || eq) -> uno
Chris Lattner0a8191e2010-01-05 07:50:36 +00001060 // ord && olt -> ord && (ord && lt) -> olt
Manman Renc2bc2d12012-06-14 05:57:42 +00001061 if (!Op0Ordered && (Op0Ordered == Op1Ordered))
1062 return LHS;
1063 if (Op0Ordered && (Op0Ordered == Op1Ordered))
Chris Lattner067459c2010-03-05 08:46:26 +00001064 return RHS;
Craig Topper9d4171a2012-12-20 07:09:41 +00001065
Chris Lattner0a8191e2010-01-05 07:50:36 +00001066 // uno && oeq -> uno && (ord && eq) -> false
Chris Lattner0a8191e2010-01-05 07:50:36 +00001067 if (!Op0Ordered)
Chris Lattner067459c2010-03-05 08:46:26 +00001068 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001069 // ord && ueq -> ord && (uno || eq) -> oeq
Chris Lattner067459c2010-03-05 08:46:26 +00001070 return getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001071 }
1072 }
1073
1074 return 0;
1075}
1076
1077
1078Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001079 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001080 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1081
1082 if (Value *V = SimplifyAndInst(Op0, Op1, TD))
1083 return ReplaceInstUsesWith(I, V);
1084
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00001085 // (A|B)&(A|C) -> A|(B&C) etc
1086 if (Value *V = SimplifyUsingDistributiveLaws(I))
1087 return ReplaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001088
Craig Topper9d4171a2012-12-20 07:09:41 +00001089 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001090 // purpose is to compute bits we don't care about.
1091 if (SimplifyDemandedInstructionBits(I))
Craig Topper9d4171a2012-12-20 07:09:41 +00001092 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001093
1094 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1095 const APInt &AndRHSMask = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001096
1097 // Optimize a variety of ((val OP C1) & C2) combinations...
1098 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1099 Value *Op0LHS = Op0I->getOperand(0);
1100 Value *Op0RHS = Op0I->getOperand(1);
1101 switch (Op0I->getOpcode()) {
1102 default: break;
1103 case Instruction::Xor:
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001104 case Instruction::Or: {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001105 // If the mask is only needed on one incoming arm, push it up.
1106 if (!Op0I->hasOneUse()) break;
Craig Topper9d4171a2012-12-20 07:09:41 +00001107
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001108 APInt NotAndRHS(~AndRHSMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001109 if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
1110 // Not masking anything out for the LHS, move to RHS.
1111 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1112 Op0RHS->getName()+".masked");
1113 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1114 }
1115 if (!isa<Constant>(Op0RHS) &&
1116 MaskedValueIsZero(Op0RHS, NotAndRHS)) {
1117 // Not masking anything out for the RHS, move to LHS.
1118 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1119 Op0LHS->getName()+".masked");
1120 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1121 }
1122
1123 break;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001124 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001125 case Instruction::Add:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001126 // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
1127 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1128 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001129 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
1130 return BinaryOperator::CreateAnd(V, AndRHS);
1131 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
1132 return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes
1133 break;
1134
1135 case Instruction::Sub:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001136 // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
1137 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1138 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001139 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
1140 return BinaryOperator::CreateAnd(V, AndRHS);
1141
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001142 // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
Chris Lattner0a8191e2010-01-05 07:50:36 +00001143 // has 1's for all bits that the subtraction with A might affect.
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001144 if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001145 uint32_t BitWidth = AndRHSMask.getBitWidth();
1146 uint32_t Zeros = AndRHSMask.countLeadingZeros();
1147 APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
1148
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001149 if (MaskedValueIsZero(Op0LHS, Mask)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001150 Value *NewNeg = Builder->CreateNeg(Op0RHS);
1151 return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1152 }
1153 }
1154 break;
1155
1156 case Instruction::Shl:
1157 case Instruction::LShr:
1158 // (1 << x) & 1 --> zext(x == 0)
1159 // (1 >> x) & 1 --> zext(x == 0)
1160 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1161 Value *NewICmp =
1162 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1163 return new ZExtInst(NewICmp, I.getType());
1164 }
1165 break;
1166 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001167
Chris Lattner0a8191e2010-01-05 07:50:36 +00001168 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1169 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1170 return Res;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001171 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001172
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001173 // If this is an integer truncation, and if the source is an 'and' with
1174 // immediate, transform it. This frequently occurs for bitfield accesses.
1175 {
1176 Value *X = 0; ConstantInt *YC = 0;
1177 if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1178 // Change: and (trunc (and X, YC) to T), C2
1179 // into : and (trunc X to T), trunc(YC) & C2
Craig Topper9d4171a2012-12-20 07:09:41 +00001180 // This will fold the two constants together, which may allow
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001181 // other simplifications.
1182 Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1183 Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1184 C3 = ConstantExpr::getAnd(C3, AndRHS);
1185 return BinaryOperator::CreateAnd(NewCast, C3);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001186 }
1187 }
1188
1189 // Try to fold constant and into select arguments.
1190 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1191 if (Instruction *R = FoldOpIntoSelect(I, SI))
1192 return R;
1193 if (isa<PHINode>(Op0))
1194 if (Instruction *NV = FoldOpIntoPhi(I))
1195 return NV;
1196 }
1197
1198
1199 // (~A & ~B) == (~(A | B)) - De Morgan's Law
1200 if (Value *Op0NotVal = dyn_castNotVal(Op0))
1201 if (Value *Op1NotVal = dyn_castNotVal(Op1))
1202 if (Op0->hasOneUse() && Op1->hasOneUse()) {
1203 Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
1204 I.getName()+".demorgan");
1205 return BinaryOperator::CreateNot(Or);
1206 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001207
Chris Lattner0a8191e2010-01-05 07:50:36 +00001208 {
1209 Value *A = 0, *B = 0, *C = 0, *D = 0;
1210 // (A|B) & ~(A&B) -> A^B
1211 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1212 match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1213 ((A == C && B == D) || (A == D && B == C)))
1214 return BinaryOperator::CreateXor(A, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001215
Chris Lattner0a8191e2010-01-05 07:50:36 +00001216 // ~(A&B) & (A|B) -> A^B
1217 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1218 match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1219 ((A == C && B == D) || (A == D && B == C)))
1220 return BinaryOperator::CreateXor(A, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001221
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001222 // A&(A^B) => A & ~B
1223 {
1224 Value *tmpOp0 = Op0;
1225 Value *tmpOp1 = Op1;
1226 if (Op0->hasOneUse() &&
1227 match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
1228 if (A == Op1 || B == Op1 ) {
1229 tmpOp1 = Op0;
1230 tmpOp0 = Op1;
1231 // Simplify below
1232 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001233 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001234
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001235 if (tmpOp1->hasOneUse() &&
1236 match(tmpOp1, m_Xor(m_Value(A), m_Value(B)))) {
1237 if (B == tmpOp0) {
1238 std::swap(A, B);
1239 }
1240 // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if
1241 // A is originally -1 (or a vector of -1 and undefs), then we enter
1242 // an endless loop. By checking that A is non-constant we ensure that
1243 // we will never get to the loop.
1244 if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00001245 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001246 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001247 }
1248
1249 // (A&((~A)|B)) -> A&B
1250 if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1251 match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1252 return BinaryOperator::CreateAnd(A, Op1);
1253 if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1254 match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1255 return BinaryOperator::CreateAnd(A, Op0);
1256 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001257
Chris Lattner0a8191e2010-01-05 07:50:36 +00001258 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1))
1259 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
Chris Lattner067459c2010-03-05 08:46:26 +00001260 if (Value *Res = FoldAndOfICmps(LHS, RHS))
1261 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001262
Chris Lattner4e8137d2010-02-11 06:26:33 +00001263 // If and'ing two fcmp, try combine them into one.
1264 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1265 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001266 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1267 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001268
1269
Chris Lattner0a8191e2010-01-05 07:50:36 +00001270 // fold (and (cast A), (cast B)) -> (cast (and A, B))
1271 if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
Chris Lattner4e8137d2010-02-11 06:26:33 +00001272 if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001273 Type *SrcTy = Op0C->getOperand(0)->getType();
Chris Lattner4e8137d2010-02-11 06:26:33 +00001274 if (Op0C->getOpcode() == Op1C->getOpcode() && // same cast kind ?
1275 SrcTy == Op1C->getOperand(0)->getType() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001276 SrcTy->isIntOrIntVectorTy()) {
Chris Lattner4e8137d2010-02-11 06:26:33 +00001277 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
Craig Topper9d4171a2012-12-20 07:09:41 +00001278
Chris Lattner4e8137d2010-02-11 06:26:33 +00001279 // Only do this if the casts both really cause code to be generated.
1280 if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
1281 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
1282 Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.getName());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001283 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
1284 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001285
Chris Lattner4e8137d2010-02-11 06:26:33 +00001286 // If this is and(cast(icmp), cast(icmp)), try to fold this even if the
1287 // cast is otherwise not optimizable. This happens for vector sexts.
1288 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
1289 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
Chris Lattner067459c2010-03-05 08:46:26 +00001290 if (Value *Res = FoldAndOfICmps(LHS, RHS))
Chris Lattner4e8137d2010-02-11 06:26:33 +00001291 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Craig Topper9d4171a2012-12-20 07:09:41 +00001292
Chris Lattner4e8137d2010-02-11 06:26:33 +00001293 // If this is and(cast(fcmp), cast(fcmp)), try to fold this even if the
1294 // cast is otherwise not optimizable. This happens for vector sexts.
1295 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
1296 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
Chris Lattner067459c2010-03-05 08:46:26 +00001297 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
Chris Lattner4e8137d2010-02-11 06:26:33 +00001298 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Chris Lattner0a8191e2010-01-05 07:50:36 +00001299 }
Chris Lattner4e8137d2010-02-11 06:26:33 +00001300 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001301
Chris Lattner0a8191e2010-01-05 07:50:36 +00001302 // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
1303 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
1304 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
Craig Topper9d4171a2012-12-20 07:09:41 +00001305 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001306 SI0->getOperand(1) == SI1->getOperand(1) &&
1307 (SI0->hasOneUse() || SI1->hasOneUse())) {
1308 Value *NewOp =
1309 Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
1310 SI0->getName());
Craig Topper9d4171a2012-12-20 07:09:41 +00001311 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001312 SI1->getOperand(1));
1313 }
1314 }
1315
Nadav Rotem513bd8a2013-01-30 06:35:22 +00001316 {
1317 Value *X = 0;
1318 bool OpsSwapped = false;
1319 // Canonicalize SExt or Not to the LHS
1320 if (match(Op1, m_SExt(m_Value())) ||
1321 match(Op1, m_Not(m_Value()))) {
1322 std::swap(Op0, Op1);
1323 OpsSwapped = true;
1324 }
1325
1326 // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1327 if (match(Op0, m_SExt(m_Value(X))) &&
1328 X->getType()->getScalarType()->isIntegerTy(1)) {
1329 Value *Zero = Constant::getNullValue(Op1->getType());
1330 return SelectInst::Create(X, Op1, Zero);
1331 }
1332
1333 // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1334 if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1335 X->getType()->getScalarType()->isIntegerTy(1)) {
1336 Value *Zero = Constant::getNullValue(Op0->getType());
1337 return SelectInst::Create(X, Zero, Op1);
1338 }
1339
1340 if (OpsSwapped)
1341 std::swap(Op0, Op1);
1342 }
1343
Chris Lattner0a8191e2010-01-05 07:50:36 +00001344 return Changed ? &I : 0;
1345}
1346
1347/// CollectBSwapParts - Analyze the specified subexpression and see if it is
1348/// capable of providing pieces of a bswap. The subexpression provides pieces
1349/// of a bswap if it is proven that each of the non-zero bytes in the output of
1350/// the expression came from the corresponding "byte swapped" byte in some other
1351/// value. For example, if the current subexpression is "(shl i32 %X, 24)" then
1352/// we know that the expression deposits the low byte of %X into the high byte
1353/// of the bswap result and that all other bytes are zero. This expression is
1354/// accepted, the high byte of ByteValues is set to X to indicate a correct
1355/// match.
1356///
1357/// This function returns true if the match was unsuccessful and false if so.
1358/// On entry to the function the "OverallLeftShift" is a signed integer value
1359/// indicating the number of bytes that the subexpression is later shifted. For
1360/// example, if the expression is later right shifted by 16 bits, the
1361/// OverallLeftShift value would be -2 on entry. This is used to specify which
1362/// byte of ByteValues is actually being set.
1363///
1364/// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
1365/// byte is masked to zero by a user. For example, in (X & 255), X will be
1366/// processed with a bytemask of 1. Because bytemask is 32-bits, this limits
1367/// this function to working on up to 32-byte (256 bit) values. ByteMask is
1368/// always in the local (OverallLeftShift) coordinate space.
1369///
1370static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
Craig Topperb94011f2013-07-14 04:42:23 +00001371 SmallVectorImpl<Value *> &ByteValues) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001372 if (Instruction *I = dyn_cast<Instruction>(V)) {
1373 // If this is an or instruction, it may be an inner node of the bswap.
1374 if (I->getOpcode() == Instruction::Or) {
1375 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1376 ByteValues) ||
1377 CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
1378 ByteValues);
1379 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001380
Chris Lattner0a8191e2010-01-05 07:50:36 +00001381 // If this is a logical shift by a constant multiple of 8, recurse with
1382 // OverallLeftShift and ByteMask adjusted.
1383 if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00001384 unsigned ShAmt =
Chris Lattner0a8191e2010-01-05 07:50:36 +00001385 cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
1386 // Ensure the shift amount is defined and of a byte value.
1387 if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
1388 return true;
1389
1390 unsigned ByteShift = ShAmt >> 3;
1391 if (I->getOpcode() == Instruction::Shl) {
1392 // X << 2 -> collect(X, +2)
1393 OverallLeftShift += ByteShift;
1394 ByteMask >>= ByteShift;
1395 } else {
1396 // X >>u 2 -> collect(X, -2)
1397 OverallLeftShift -= ByteShift;
1398 ByteMask <<= ByteShift;
1399 ByteMask &= (~0U >> (32-ByteValues.size()));
1400 }
1401
1402 if (OverallLeftShift >= (int)ByteValues.size()) return true;
1403 if (OverallLeftShift <= -(int)ByteValues.size()) return true;
1404
Craig Topper9d4171a2012-12-20 07:09:41 +00001405 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001406 ByteValues);
1407 }
1408
1409 // If this is a logical 'and' with a mask that clears bytes, clear the
1410 // corresponding bytes in ByteMask.
1411 if (I->getOpcode() == Instruction::And &&
1412 isa<ConstantInt>(I->getOperand(1))) {
1413 // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
1414 unsigned NumBytes = ByteValues.size();
1415 APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
1416 const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
Craig Topper9d4171a2012-12-20 07:09:41 +00001417
Chris Lattner0a8191e2010-01-05 07:50:36 +00001418 for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
1419 // If this byte is masked out by a later operation, we don't care what
1420 // the and mask is.
1421 if ((ByteMask & (1 << i)) == 0)
1422 continue;
Craig Topper9d4171a2012-12-20 07:09:41 +00001423
Chris Lattner0a8191e2010-01-05 07:50:36 +00001424 // If the AndMask is all zeros for this byte, clear the bit.
1425 APInt MaskB = AndMask & Byte;
1426 if (MaskB == 0) {
1427 ByteMask &= ~(1U << i);
1428 continue;
1429 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001430
Chris Lattner0a8191e2010-01-05 07:50:36 +00001431 // If the AndMask is not all ones for this byte, it's not a bytezap.
1432 if (MaskB != Byte)
1433 return true;
1434
1435 // Otherwise, this byte is kept.
1436 }
1437
Craig Topper9d4171a2012-12-20 07:09:41 +00001438 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
Chris Lattner0a8191e2010-01-05 07:50:36 +00001439 ByteValues);
1440 }
1441 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001442
Chris Lattner0a8191e2010-01-05 07:50:36 +00001443 // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be
1444 // the input value to the bswap. Some observations: 1) if more than one byte
1445 // is demanded from this input, then it could not be successfully assembled
1446 // into a byteswap. At least one of the two bytes would not be aligned with
1447 // their ultimate destination.
1448 if (!isPowerOf2_32(ByteMask)) return true;
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00001449 unsigned InputByteNo = countTrailingZeros(ByteMask);
Craig Topper9d4171a2012-12-20 07:09:41 +00001450
Chris Lattner0a8191e2010-01-05 07:50:36 +00001451 // 2) The input and ultimate destinations must line up: if byte 3 of an i32
1452 // is demanded, it needs to go into byte 0 of the result. This means that the
1453 // byte needs to be shifted until it lands in the right byte bucket. The
1454 // shift amount depends on the position: if the byte is coming from the high
1455 // part of the value (e.g. byte 3) then it must be shifted right. If from the
1456 // low part, it must be shifted left.
1457 unsigned DestByteNo = InputByteNo + OverallLeftShift;
Chris Lattnerb1e2e1e2012-03-26 19:13:57 +00001458 if (ByteValues.size()-1-DestByteNo != InputByteNo)
1459 return true;
Craig Topper9d4171a2012-12-20 07:09:41 +00001460
Chris Lattner0a8191e2010-01-05 07:50:36 +00001461 // If the destination byte value is already defined, the values are or'd
1462 // together, which isn't a bswap (unless it's an or of the same bits).
1463 if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
1464 return true;
1465 ByteValues[DestByteNo] = V;
1466 return false;
1467}
1468
1469/// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
1470/// If so, insert the new bswap intrinsic and return it.
1471Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
Jay Foadb804a2b2011-07-12 14:06:48 +00001472 IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
Craig Topper9d4171a2012-12-20 07:09:41 +00001473 if (!ITy || ITy->getBitWidth() % 16 ||
Chris Lattner0a8191e2010-01-05 07:50:36 +00001474 // ByteMask only allows up to 32-byte values.
Craig Topper9d4171a2012-12-20 07:09:41 +00001475 ITy->getBitWidth() > 32*8)
Chris Lattner0a8191e2010-01-05 07:50:36 +00001476 return 0; // Can only bswap pairs of bytes. Can't do vectors.
Craig Topper9d4171a2012-12-20 07:09:41 +00001477
Chris Lattner0a8191e2010-01-05 07:50:36 +00001478 /// ByteValues - For each byte of the result, we keep track of which value
1479 /// defines each byte.
1480 SmallVector<Value*, 8> ByteValues;
1481 ByteValues.resize(ITy->getBitWidth()/8);
Craig Topper9d4171a2012-12-20 07:09:41 +00001482
Chris Lattner0a8191e2010-01-05 07:50:36 +00001483 // Try to find all the pieces corresponding to the bswap.
1484 uint32_t ByteMask = ~0U >> (32-ByteValues.size());
1485 if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
1486 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001487
Chris Lattner0a8191e2010-01-05 07:50:36 +00001488 // Check to see if all of the bytes come from the same value.
1489 Value *V = ByteValues[0];
1490 if (V == 0) return 0; // Didn't find a byte? Must be zero.
Craig Topper9d4171a2012-12-20 07:09:41 +00001491
Chris Lattner0a8191e2010-01-05 07:50:36 +00001492 // Check to make sure that all of the bytes come from the same value.
1493 for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
1494 if (ByteValues[i] != V)
1495 return 0;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001496 Module *M = I.getParent()->getParent()->getParent();
Benjamin Kramere6e19332011-07-14 17:45:39 +00001497 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, ITy);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001498 return CallInst::Create(F, V);
1499}
1500
1501/// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check
1502/// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
1503/// we can simplify this expression to "cond ? C : D or B".
1504static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
1505 Value *C, Value *D) {
1506 // If A is not a select of -1/0, this cannot match.
1507 Value *Cond = 0;
Chris Lattner9b6a1782010-02-09 01:12:41 +00001508 if (!match(A, m_SExt(m_Value(Cond))) ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001509 !Cond->getType()->isIntegerTy(1))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001510 return 0;
1511
1512 // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001513 if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001514 return SelectInst::Create(Cond, C, B);
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001515 if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
Chris Lattner64ffd112010-02-05 19:53:02 +00001516 return SelectInst::Create(Cond, C, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001517
Chris Lattner0a8191e2010-01-05 07:50:36 +00001518 // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001519 if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
Chris Lattner64ffd112010-02-05 19:53:02 +00001520 return SelectInst::Create(Cond, C, D);
Chris Lattnerf4c8d3c2010-02-09 01:14:06 +00001521 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001522 return SelectInst::Create(Cond, C, D);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001523 return 0;
1524}
1525
Chris Lattner067459c2010-03-05 08:46:26 +00001526/// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
1527Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001528 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1529
1530 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1531 if (PredicatesFoldable(LHSCC, RHSCC)) {
1532 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1533 LHS->getOperand(1) == RHS->getOperand(0))
1534 LHS->swapOperands();
1535 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1536 LHS->getOperand(1) == RHS->getOperand(1)) {
1537 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1538 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1539 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +00001540 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001541 }
1542 }
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001543
1544 // handle (roughly):
1545 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
Tim Northoverc0756c42013-09-04 11:57:13 +00001546 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001547 return V;
Owen Anderson3fe002d2010-09-08 22:16:17 +00001548
Chris Lattner0a8191e2010-01-05 07:50:36 +00001549 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1550 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1551 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
David Majnemerc2a990b2013-07-05 00:31:17 +00001552
1553 if (LHS->hasOneUse() || RHS->hasOneUse()) {
1554 // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1555 // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
1556 Value *A = 0, *B = 0;
1557 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) {
1558 B = Val;
1559 if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1))
1560 A = Val2;
1561 else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2)
1562 A = RHS->getOperand(1);
1563 }
1564 // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1565 // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
1566 else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1567 B = Val2;
1568 if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1))
1569 A = Val;
1570 else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val)
1571 A = LHS->getOperand(1);
1572 }
1573 if (A && B)
1574 return Builder->CreateICmp(
1575 ICmpInst::ICMP_UGE,
1576 Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1577 }
1578
1579 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
Chris Lattner0a8191e2010-01-05 07:50:36 +00001580 if (LHSCst == 0 || RHSCst == 0) return 0;
1581
Owen Anderson8f306a72010-08-02 09:32:13 +00001582 if (LHSCst == RHSCst && LHSCC == RHSCC) {
1583 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1584 if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1585 Value *NewOr = Builder->CreateOr(Val, Val2);
1586 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1587 }
Benjamin Kramerda37e152012-01-08 18:32:24 +00001588 }
1589
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001590 // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001591 // iff C2 + CA == C1.
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001592 if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001593 ConstantInt *AddCst;
1594 if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1595 if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001596 return Builder->CreateICmpULE(Val, LHSCst);
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001597 }
1598
Chris Lattner0a8191e2010-01-05 07:50:36 +00001599 // From here on, we only handle:
1600 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1601 if (Val != Val2) return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001602
Chris Lattner0a8191e2010-01-05 07:50:36 +00001603 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1604 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1605 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1606 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1607 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1608 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001609
Chris Lattner0a8191e2010-01-05 07:50:36 +00001610 // We can't fold (ugt x, C) | (sgt x, C2).
1611 if (!PredicatesFoldable(LHSCC, RHSCC))
1612 return 0;
Craig Topper9d4171a2012-12-20 07:09:41 +00001613
Chris Lattner0a8191e2010-01-05 07:50:36 +00001614 // Ensure that the larger constant is on the RHS.
1615 bool ShouldSwap;
1616 if (CmpInst::isSigned(LHSCC) ||
Craig Topper9d4171a2012-12-20 07:09:41 +00001617 (ICmpInst::isEquality(LHSCC) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001618 CmpInst::isSigned(RHSCC)))
1619 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1620 else
1621 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +00001622
Chris Lattner0a8191e2010-01-05 07:50:36 +00001623 if (ShouldSwap) {
1624 std::swap(LHS, RHS);
1625 std::swap(LHSCst, RHSCst);
1626 std::swap(LHSCC, RHSCC);
1627 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001628
Dan Gohman4a618822010-02-10 16:03:48 +00001629 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +00001630 // comparing a value against two constants and or'ing the result
1631 // together. Because of the above check, we know that we only have
1632 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1633 // icmp folding check above), that the two constants are not
1634 // equal.
1635 assert(LHSCst != RHSCst && "Compares not folded above?");
1636
1637 switch (LHSCC) {
1638 default: llvm_unreachable("Unknown integer condition code!");
1639 case ICmpInst::ICMP_EQ:
1640 switch (RHSCC) {
1641 default: llvm_unreachable("Unknown integer condition code!");
1642 case ICmpInst::ICMP_EQ:
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001643 if (LHS->getOperand(0) == RHS->getOperand(0)) {
Jakub Staszakf5849772012-12-31 01:40:44 +00001644 // if LHSCst and RHSCst differ only by one bit:
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001645 // (A == C1 || A == C2) -> (A & ~(C1 ^ C2)) == C1
Jakub Staszakc48bbe72012-12-31 18:26:42 +00001646 assert(LHSCst->getValue().ule(LHSCst->getValue()));
1647
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001648 APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1649 if (Xor.isPowerOf2()) {
1650 Value *NegCst = Builder->getInt(~Xor);
1651 Value *And = Builder->CreateAnd(LHS->getOperand(0), NegCst);
1652 return Builder->CreateICmp(ICmpInst::ICMP_EQ, And, LHSCst);
1653 }
1654 }
1655
David Majnemer1fae1952013-04-14 21:15:43 +00001656 if (LHSCst == SubOne(RHSCst)) {
1657 // (X == 13 | X == 14) -> X-13 <u 2
1658 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1659 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1660 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1661 return Builder->CreateICmpULT(Add, AddCST);
1662 }
1663
Chris Lattner0a8191e2010-01-05 07:50:36 +00001664 break; // (X == 13 | X == 15) -> no change
1665 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1666 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
1667 break;
1668 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
1669 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
1670 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001671 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001672 }
1673 break;
1674 case ICmpInst::ICMP_NE:
1675 switch (RHSCC) {
1676 default: llvm_unreachable("Unknown integer condition code!");
1677 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
1678 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
1679 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
Chris Lattner067459c2010-03-05 08:46:26 +00001680 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001681 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
1682 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
1683 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001684 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001685 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001686 case ICmpInst::ICMP_ULT:
1687 switch (RHSCC) {
1688 default: llvm_unreachable("Unknown integer condition code!");
1689 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
1690 break;
1691 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
1692 // If RHSCst is [us]MAXINT, it is always false. Not handling
1693 // this can cause overflow.
1694 if (RHSCst->isMaxValue(false))
Chris Lattner067459c2010-03-05 08:46:26 +00001695 return LHS;
1696 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001697 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
1698 break;
1699 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
1700 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001701 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001702 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
1703 break;
1704 }
1705 break;
1706 case ICmpInst::ICMP_SLT:
1707 switch (RHSCC) {
1708 default: llvm_unreachable("Unknown integer condition code!");
1709 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
1710 break;
1711 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
1712 // If RHSCst is [us]MAXINT, it is always false. Not handling
1713 // this can cause overflow.
1714 if (RHSCst->isMaxValue(true))
Chris Lattner067459c2010-03-05 08:46:26 +00001715 return LHS;
1716 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001717 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
1718 break;
1719 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
1720 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001721 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001722 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
1723 break;
1724 }
1725 break;
1726 case ICmpInst::ICMP_UGT:
1727 switch (RHSCC) {
1728 default: llvm_unreachable("Unknown integer condition code!");
1729 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
1730 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
Chris Lattner067459c2010-03-05 08:46:26 +00001731 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001732 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
1733 break;
1734 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
1735 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001736 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001737 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
1738 break;
1739 }
1740 break;
1741 case ICmpInst::ICMP_SGT:
1742 switch (RHSCC) {
1743 default: llvm_unreachable("Unknown integer condition code!");
1744 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
1745 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
Chris Lattner067459c2010-03-05 08:46:26 +00001746 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001747 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
1748 break;
1749 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
1750 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001751 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001752 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
1753 break;
1754 }
1755 break;
1756 }
1757 return 0;
1758}
1759
Chris Lattner067459c2010-03-05 08:46:26 +00001760/// FoldOrOfFCmps - Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of
1761/// instcombine, this returns a Value which should already be inserted into the
1762/// function.
1763Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001764 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
Craig Topper9d4171a2012-12-20 07:09:41 +00001765 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001766 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1767 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1768 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1769 // If either of the constants are nans, then the whole thing returns
1770 // true.
1771 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001772 return Builder->getTrue();
Craig Topper9d4171a2012-12-20 07:09:41 +00001773
Chris Lattner0a8191e2010-01-05 07:50:36 +00001774 // Otherwise, no need to compare the two constants, compare the
1775 // rest.
Chris Lattner067459c2010-03-05 08:46:26 +00001776 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001777 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001778
Chris Lattner0a8191e2010-01-05 07:50:36 +00001779 // Handle vector zeros. This occurs because the canonical form of
1780 // "fcmp uno x,x" is "fcmp uno x, 0".
1781 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1782 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001783 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Craig Topper9d4171a2012-12-20 07:09:41 +00001784
Chris Lattner0a8191e2010-01-05 07:50:36 +00001785 return 0;
1786 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001787
Chris Lattner0a8191e2010-01-05 07:50:36 +00001788 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1789 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1790 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
Craig Topper9d4171a2012-12-20 07:09:41 +00001791
Chris Lattner0a8191e2010-01-05 07:50:36 +00001792 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1793 // Swap RHS operands to match LHS.
1794 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1795 std::swap(Op1LHS, Op1RHS);
1796 }
1797 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1798 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1799 if (Op0CC == Op1CC)
Chris Lattner067459c2010-03-05 08:46:26 +00001800 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001801 if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
Chris Lattner067459c2010-03-05 08:46:26 +00001802 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001803 if (Op0CC == FCmpInst::FCMP_FALSE)
Chris Lattner067459c2010-03-05 08:46:26 +00001804 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001805 if (Op1CC == FCmpInst::FCMP_FALSE)
Chris Lattner067459c2010-03-05 08:46:26 +00001806 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001807 bool Op0Ordered;
1808 bool Op1Ordered;
1809 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1810 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
1811 if (Op0Ordered == Op1Ordered) {
1812 // If both are ordered or unordered, return a new fcmp with
1813 // or'ed predicates.
Chris Lattner067459c2010-03-05 08:46:26 +00001814 return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001815 }
1816 }
1817 return 0;
1818}
1819
1820/// FoldOrWithConstants - This helper function folds:
1821///
1822/// ((A | B) & C1) | (B & C2)
1823///
1824/// into:
Craig Topper9d4171a2012-12-20 07:09:41 +00001825///
Chris Lattner0a8191e2010-01-05 07:50:36 +00001826/// (A & C1) | B
1827///
1828/// when the XOR of the two constants is "all ones" (-1).
1829Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
1830 Value *A, Value *B, Value *C) {
1831 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
1832 if (!CI1) return 0;
1833
1834 Value *V1 = 0;
1835 ConstantInt *CI2 = 0;
1836 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
1837
1838 APInt Xor = CI1->getValue() ^ CI2->getValue();
1839 if (!Xor.isAllOnesValue()) return 0;
1840
1841 if (V1 == A || V1 == B) {
1842 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1843 return BinaryOperator::CreateOr(NewOp, V1);
1844 }
1845
1846 return 0;
1847}
1848
1849Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001850 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001851 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1852
1853 if (Value *V = SimplifyOrInst(Op0, Op1, TD))
1854 return ReplaceInstUsesWith(I, V);
Bill Wendlingaf13d822010-03-03 00:35:56 +00001855
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00001856 // (A&B)|(A&C) -> A&(B|C) etc
1857 if (Value *V = SimplifyUsingDistributiveLaws(I))
1858 return ReplaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001859
Craig Topper9d4171a2012-12-20 07:09:41 +00001860 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001861 // purpose is to compute bits we don't care about.
1862 if (SimplifyDemandedInstructionBits(I))
1863 return &I;
1864
1865 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1866 ConstantInt *C1 = 0; Value *X = 0;
1867 // (X & C1) | C2 --> (X | C2) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001868 // iff (C1 & C2) == 0.
Chris Lattner0a8191e2010-01-05 07:50:36 +00001869 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
Bill Wendlingaf13d822010-03-03 00:35:56 +00001870 (RHS->getValue() & C1->getValue()) != 0 &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001871 Op0->hasOneUse()) {
1872 Value *Or = Builder->CreateOr(X, RHS);
1873 Or->takeName(Op0);
Craig Topper9d4171a2012-12-20 07:09:41 +00001874 return BinaryOperator::CreateAnd(Or,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001875 Builder->getInt(RHS->getValue() | C1->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001876 }
1877
1878 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1879 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
1880 Op0->hasOneUse()) {
1881 Value *Or = Builder->CreateOr(X, RHS);
1882 Or->takeName(Op0);
1883 return BinaryOperator::CreateXor(Or,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001884 Builder->getInt(C1->getValue() & ~RHS->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001885 }
1886
1887 // Try to fold constant and into select arguments.
1888 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1889 if (Instruction *R = FoldOpIntoSelect(I, SI))
1890 return R;
Bill Wendlingaf13d822010-03-03 00:35:56 +00001891
Chris Lattner0a8191e2010-01-05 07:50:36 +00001892 if (isa<PHINode>(Op0))
1893 if (Instruction *NV = FoldOpIntoPhi(I))
1894 return NV;
1895 }
1896
1897 Value *A = 0, *B = 0;
1898 ConstantInt *C1 = 0, *C2 = 0;
1899
1900 // (A | B) | C and A | (B | C) -> bswap if possible.
1901 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
1902 if (match(Op0, m_Or(m_Value(), m_Value())) ||
1903 match(Op1, m_Or(m_Value(), m_Value())) ||
Chris Lattnerb9400912011-02-09 17:00:45 +00001904 (match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1905 match(Op1, m_LogicalShift(m_Value(), m_Value())))) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001906 if (Instruction *BSwap = MatchBSwap(I))
1907 return BSwap;
1908 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001909
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001910 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001911 if (Op0->hasOneUse() &&
1912 match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1913 MaskedValueIsZero(Op1, C1->getValue())) {
1914 Value *NOr = Builder->CreateOr(A, Op1);
1915 NOr->takeName(Op0);
1916 return BinaryOperator::CreateXor(NOr, C1);
1917 }
1918
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001919 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001920 if (Op1->hasOneUse() &&
1921 match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1922 MaskedValueIsZero(Op0, C1->getValue())) {
1923 Value *NOr = Builder->CreateOr(A, Op0);
1924 NOr->takeName(Op0);
1925 return BinaryOperator::CreateXor(NOr, C1);
1926 }
1927
1928 // (A & C)|(B & D)
1929 Value *C = 0, *D = 0;
1930 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
1931 match(Op1, m_And(m_Value(B), m_Value(D)))) {
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001932 Value *V1 = 0, *V2 = 0;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001933 C1 = dyn_cast<ConstantInt>(C);
1934 C2 = dyn_cast<ConstantInt>(D);
1935 if (C1 && C2) { // (A & C1)|(B & C2)
1936 // If we have: ((V + N) & C1) | (V & C2)
1937 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
1938 // replace with V+N.
1939 if (C1->getValue() == ~C2->getValue()) {
1940 if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
1941 match(A, m_Add(m_Value(V1), m_Value(V2)))) {
1942 // Add commutes, try both ways.
1943 if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
1944 return ReplaceInstUsesWith(I, A);
1945 if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
1946 return ReplaceInstUsesWith(I, A);
1947 }
1948 // Or commutes, try both ways.
1949 if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
1950 match(B, m_Add(m_Value(V1), m_Value(V2)))) {
1951 // Add commutes, try both ways.
1952 if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
1953 return ReplaceInstUsesWith(I, B);
1954 if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
1955 return ReplaceInstUsesWith(I, B);
1956 }
1957 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001958
Chris Lattner0a8191e2010-01-05 07:50:36 +00001959 if ((C1->getValue() & C2->getValue()) == 0) {
Chris Lattner95188692010-01-11 06:55:24 +00001960 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001961 // iff (C1&C2) == 0 and (N&~C1) == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00001962 if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
1963 ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) || // (V|N)
1964 (V2 == B && MaskedValueIsZero(V1, ~C1->getValue())))) // (N|V)
1965 return BinaryOperator::CreateAnd(A,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001966 Builder->getInt(C1->getValue()|C2->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001967 // Or commutes, try both ways.
1968 if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
1969 ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) || // (V|N)
1970 (V2 == A && MaskedValueIsZero(V1, ~C2->getValue())))) // (N|V)
1971 return BinaryOperator::CreateAnd(B,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001972 Builder->getInt(C1->getValue()|C2->getValue()));
Craig Topper9d4171a2012-12-20 07:09:41 +00001973
Chris Lattner95188692010-01-11 06:55:24 +00001974 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001975 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
Chris Lattner95188692010-01-11 06:55:24 +00001976 ConstantInt *C3 = 0, *C4 = 0;
1977 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
1978 (C3->getValue() & ~C1->getValue()) == 0 &&
1979 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
1980 (C4->getValue() & ~C2->getValue()) == 0) {
1981 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
1982 return BinaryOperator::CreateAnd(V2,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001983 Builder->getInt(C1->getValue()|C2->getValue()));
Chris Lattner95188692010-01-11 06:55:24 +00001984 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001985 }
1986 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001987
Chris Lattner8e2c4712010-02-02 02:43:51 +00001988 // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants.
1989 // Don't do this for vector select idioms, the code generator doesn't handle
1990 // them well yet.
Duncan Sands19d0b472010-02-16 11:11:14 +00001991 if (!I.getType()->isVectorTy()) {
Chris Lattner8e2c4712010-02-02 02:43:51 +00001992 if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
1993 return Match;
1994 if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
1995 return Match;
1996 if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
1997 return Match;
1998 if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
1999 return Match;
2000 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002001
2002 // ((A&~B)|(~A&B)) -> A^B
2003 if ((match(C, m_Not(m_Specific(D))) &&
2004 match(B, m_Not(m_Specific(A)))))
2005 return BinaryOperator::CreateXor(A, D);
2006 // ((~B&A)|(~A&B)) -> A^B
2007 if ((match(A, m_Not(m_Specific(D))) &&
2008 match(B, m_Not(m_Specific(C)))))
2009 return BinaryOperator::CreateXor(C, D);
2010 // ((A&~B)|(B&~A)) -> A^B
2011 if ((match(C, m_Not(m_Specific(B))) &&
2012 match(D, m_Not(m_Specific(A)))))
2013 return BinaryOperator::CreateXor(A, B);
2014 // ((~B&A)|(B&~A)) -> A^B
2015 if ((match(A, m_Not(m_Specific(B))) &&
2016 match(D, m_Not(m_Specific(C)))))
2017 return BinaryOperator::CreateXor(C, B);
Benjamin Kramer11743242010-07-12 13:34:22 +00002018
2019 // ((A|B)&1)|(B&-2) -> (A&1) | B
2020 if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2021 match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2022 Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2023 if (Ret) return Ret;
2024 }
2025 // (B&-2)|((A|B)&1) -> (A&1) | B
2026 if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2027 match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2028 Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2029 if (Ret) return Ret;
2030 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002031 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002032
Chris Lattner0a8191e2010-01-05 07:50:36 +00002033 // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
2034 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
2035 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
Craig Topper9d4171a2012-12-20 07:09:41 +00002036 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002037 SI0->getOperand(1) == SI1->getOperand(1) &&
2038 (SI0->hasOneUse() || SI1->hasOneUse())) {
2039 Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
2040 SI0->getName());
Craig Topper9d4171a2012-12-20 07:09:41 +00002041 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002042 SI1->getOperand(1));
2043 }
2044 }
2045
Chris Lattner0a8191e2010-01-05 07:50:36 +00002046 // (~A | ~B) == (~(A & B)) - De Morgan's Law
2047 if (Value *Op0NotVal = dyn_castNotVal(Op0))
2048 if (Value *Op1NotVal = dyn_castNotVal(Op1))
2049 if (Op0->hasOneUse() && Op1->hasOneUse()) {
2050 Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
2051 I.getName()+".demorgan");
2052 return BinaryOperator::CreateNot(And);
2053 }
2054
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002055 // Canonicalize xor to the RHS.
Eli Friedmane06535b2012-03-16 00:52:42 +00002056 bool SwappedForXor = false;
2057 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002058 std::swap(Op0, Op1);
Eli Friedmane06535b2012-03-16 00:52:42 +00002059 SwappedForXor = true;
2060 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002061
2062 // A | ( A ^ B) -> A | B
2063 // A | (~A ^ B) -> A | ~B
Chad Rosier7813dce2012-04-26 23:29:14 +00002064 // (A & B) | (A ^ B)
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002065 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2066 if (Op0 == A || Op0 == B)
2067 return BinaryOperator::CreateOr(A, B);
2068
Chad Rosier7813dce2012-04-26 23:29:14 +00002069 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2070 match(Op0, m_And(m_Specific(B), m_Specific(A))))
2071 return BinaryOperator::CreateOr(A, B);
2072
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002073 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2074 Value *Not = Builder->CreateNot(B, B->getName()+".not");
2075 return BinaryOperator::CreateOr(Not, Op0);
2076 }
2077 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2078 Value *Not = Builder->CreateNot(A, A->getName()+".not");
2079 return BinaryOperator::CreateOr(Not, Op0);
2080 }
2081 }
2082
2083 // A | ~(A | B) -> A | ~B
2084 // A | ~(A ^ B) -> A | ~B
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002085 if (match(Op1, m_Not(m_Value(A))))
2086 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
Benjamin Kramer5b7a4e02011-02-20 15:20:01 +00002087 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2088 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2089 B->getOpcode() == Instruction::Xor)) {
2090 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2091 B->getOperand(0);
2092 Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2093 return BinaryOperator::CreateOr(Not, Op0);
2094 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002095
Eli Friedmane06535b2012-03-16 00:52:42 +00002096 if (SwappedForXor)
2097 std::swap(Op0, Op1);
2098
Chris Lattner0a8191e2010-01-05 07:50:36 +00002099 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2100 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
Chris Lattner067459c2010-03-05 08:46:26 +00002101 if (Value *Res = FoldOrOfICmps(LHS, RHS))
2102 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002103
Chris Lattner4e8137d2010-02-11 06:26:33 +00002104 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
2105 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2106 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00002107 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2108 return ReplaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002109
Chris Lattner0a8191e2010-01-05 07:50:36 +00002110 // fold (or (cast A), (cast B)) -> (cast (or A, B))
2111 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
Chris Lattner311aa632011-01-15 05:40:29 +00002112 CastInst *Op1C = dyn_cast<CastInst>(Op1);
2113 if (Op1C && Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
Chris Lattner229907c2011-07-18 04:54:35 +00002114 Type *SrcTy = Op0C->getOperand(0)->getType();
Chris Lattner311aa632011-01-15 05:40:29 +00002115 if (SrcTy == Op1C->getOperand(0)->getType() &&
2116 SrcTy->isIntOrIntVectorTy()) {
2117 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
Chris Lattner4e8137d2010-02-11 06:26:33 +00002118
Chris Lattner311aa632011-01-15 05:40:29 +00002119 if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
2120 // Only do this if the casts both really cause code to be
2121 // generated.
2122 ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
2123 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
2124 Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.getName());
2125 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002126 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002127
Chris Lattner311aa632011-01-15 05:40:29 +00002128 // If this is or(cast(icmp), cast(icmp)), try to fold this even if the
2129 // cast is otherwise not optimizable. This happens for vector sexts.
2130 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
2131 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
2132 if (Value *Res = FoldOrOfICmps(LHS, RHS))
2133 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Craig Topper9d4171a2012-12-20 07:09:41 +00002134
Chris Lattner311aa632011-01-15 05:40:29 +00002135 // If this is or(cast(fcmp), cast(fcmp)), try to fold this even if the
2136 // cast is otherwise not optimizable. This happens for vector sexts.
2137 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
2138 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
2139 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2140 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002141 }
Chris Lattner311aa632011-01-15 05:40:29 +00002142 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002143 }
Eli Friedman23956262011-04-14 22:41:27 +00002144
2145 // or(sext(A), B) -> A ? -1 : B where A is an i1
2146 // or(A, sext(B)) -> B ? -1 : A where B is an i1
2147 if (match(Op0, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2148 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
2149 if (match(Op1, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2150 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2151
Owen Andersonc237a842010-09-13 17:59:27 +00002152 // Note: If we've gotten to the point of visiting the outer OR, then the
2153 // inner one couldn't be simplified. If it was a constant, then it won't
2154 // be simplified by a later pass either, so we try swapping the inner/outer
2155 // ORs in the hopes that we'll be able to simplify it this way.
2156 // (X|C) | V --> (X|V) | C
2157 if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2158 match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2159 Value *Inner = Builder->CreateOr(A, Op1);
2160 Inner->takeName(Op0);
2161 return BinaryOperator::CreateOr(Inner, C1);
2162 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002163
Bill Wendling23242092013-02-16 23:41:36 +00002164 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2165 // Since this OR statement hasn't been optimized further yet, we hope
2166 // that this transformation will allow the new ORs to be optimized.
2167 {
2168 Value *X = 0, *Y = 0;
2169 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2170 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2171 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2172 Value *orTrue = Builder->CreateOr(A, C);
2173 Value *orFalse = Builder->CreateOr(B, D);
2174 return SelectInst::Create(X, orTrue, orFalse);
2175 }
2176 }
2177
Chris Lattner0a8191e2010-01-05 07:50:36 +00002178 return Changed ? &I : 0;
2179}
2180
2181Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00002182 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002183 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2184
Duncan Sandsc89ac072010-11-17 18:52:15 +00002185 if (Value *V = SimplifyXorInst(Op0, Op1, TD))
2186 return ReplaceInstUsesWith(I, V);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002187
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00002188 // (A&B)^(A&C) -> A&(B^C) etc
2189 if (Value *V = SimplifyUsingDistributiveLaws(I))
2190 return ReplaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002191
Craig Topper9d4171a2012-12-20 07:09:41 +00002192 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00002193 // purpose is to compute bits we don't care about.
2194 if (SimplifyDemandedInstructionBits(I))
2195 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002196
2197 // Is this a ~ operation?
2198 if (Value *NotOp = dyn_castNotVal(&I)) {
2199 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002200 if (Op0I->getOpcode() == Instruction::And ||
Chris Lattner0a8191e2010-01-05 07:50:36 +00002201 Op0I->getOpcode() == Instruction::Or) {
2202 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2203 // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2204 if (dyn_castNotVal(Op0I->getOperand(1)))
2205 Op0I->swapOperands();
2206 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2207 Value *NotY =
2208 Builder->CreateNot(Op0I->getOperand(1),
2209 Op0I->getOperand(1)->getName()+".not");
2210 if (Op0I->getOpcode() == Instruction::And)
2211 return BinaryOperator::CreateOr(Op0NotVal, NotY);
2212 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2213 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002214
Chris Lattner0a8191e2010-01-05 07:50:36 +00002215 // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2216 // ~(X | Y) === (~X & ~Y) - De Morgan's Law
Craig Topper9d4171a2012-12-20 07:09:41 +00002217 if (isFreeToInvert(Op0I->getOperand(0)) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002218 isFreeToInvert(Op0I->getOperand(1))) {
2219 Value *NotX =
2220 Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2221 Value *NotY =
2222 Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2223 if (Op0I->getOpcode() == Instruction::And)
2224 return BinaryOperator::CreateOr(NotX, NotY);
2225 return BinaryOperator::CreateAnd(NotX, NotY);
2226 }
Chris Lattner18f49ce2010-01-19 18:16:19 +00002227
2228 } else if (Op0I->getOpcode() == Instruction::AShr) {
2229 // ~(~X >>s Y) --> (X >>s Y)
2230 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2231 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002232 }
2233 }
2234 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002235
2236
Chris Lattner0a8191e2010-01-05 07:50:36 +00002237 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Dan Gohman0a8175d2010-04-09 14:53:59 +00002238 if (RHS->isOne() && Op0->hasOneUse())
Chris Lattner0a8191e2010-01-05 07:50:36 +00002239 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
Dan Gohman0a8175d2010-04-09 14:53:59 +00002240 if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2241 return CmpInst::Create(CI->getOpcode(),
2242 CI->getInversePredicate(),
2243 CI->getOperand(0), CI->getOperand(1));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002244
2245 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2246 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2247 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2248 if (CI->hasOneUse() && Op0C->hasOneUse()) {
2249 Instruction::CastOps Opcode = Op0C->getOpcode();
2250 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002251 (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(),
Chris Lattner0a8191e2010-01-05 07:50:36 +00002252 Op0C->getDestTy()))) {
2253 CI->setPredicate(CI->getInversePredicate());
2254 return CastInst::Create(Opcode, CI, Op0C->getType());
2255 }
2256 }
2257 }
2258 }
2259
2260 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2261 // ~(c-X) == X-c-1 == X+(-c-1)
2262 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2263 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2264 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2265 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2266 ConstantInt::get(I.getType(), 1));
2267 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2268 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002269
Chris Lattner0a8191e2010-01-05 07:50:36 +00002270 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2271 if (Op0I->getOpcode() == Instruction::Add) {
2272 // ~(X-c) --> (-c-1)-X
2273 if (RHS->isAllOnesValue()) {
2274 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2275 return BinaryOperator::CreateSub(
2276 ConstantExpr::getSub(NegOp0CI,
2277 ConstantInt::get(I.getType(), 1)),
2278 Op0I->getOperand(0));
2279 } else if (RHS->getValue().isSignBit()) {
2280 // (X + C) ^ signbit -> (X + C + signbit)
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002281 Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002282 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2283
2284 }
2285 } else if (Op0I->getOpcode() == Instruction::Or) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002286 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00002287 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
2288 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2289 // Anything in both C1 and C2 is known to be zero, remove it from
2290 // NewRHS.
2291 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
Craig Topper9d4171a2012-12-20 07:09:41 +00002292 NewRHS = ConstantExpr::getAnd(NewRHS,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002293 ConstantExpr::getNot(CommonBits));
2294 Worklist.Add(Op0I);
2295 I.setOperand(0, Op0I->getOperand(0));
2296 I.setOperand(1, NewRHS);
2297 return &I;
2298 }
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002299 } else if (Op0I->getOpcode() == Instruction::LShr) {
2300 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2301 // E1 = "X ^ C1"
Craig Topper9d4171a2012-12-20 07:09:41 +00002302 BinaryOperator *E1;
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002303 ConstantInt *C1;
2304 if (Op0I->hasOneUse() &&
2305 (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2306 E1->getOpcode() == Instruction::Xor &&
2307 (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2308 // fold (C1 >> C2) ^ C3
2309 ConstantInt *C2 = Op0CI, *C3 = RHS;
2310 APInt FoldConst = C1->getValue().lshr(C2->getValue());
2311 FoldConst ^= C3->getValue();
2312 // Prepare the two operands.
2313 Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2314 Opnd0->takeName(Op0I);
2315 cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2316 Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2317
2318 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2319 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002320 }
2321 }
2322 }
2323
2324 // Try to fold constant and into select arguments.
2325 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2326 if (Instruction *R = FoldOpIntoSelect(I, SI))
2327 return R;
2328 if (isa<PHINode>(Op0))
2329 if (Instruction *NV = FoldOpIntoPhi(I))
2330 return NV;
2331 }
2332
Chris Lattner0a8191e2010-01-05 07:50:36 +00002333 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2334 if (Op1I) {
2335 Value *A, *B;
2336 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2337 if (A == Op0) { // B^(B|A) == (A|B)^B
2338 Op1I->swapOperands();
2339 I.swapOperands();
2340 std::swap(Op0, Op1);
2341 } else if (B == Op0) { // B^(A|B) == (A|B)^B
2342 I.swapOperands(); // Simplified below.
2343 std::swap(Op0, Op1);
2344 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002345 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002346 Op1I->hasOneUse()){
2347 if (A == Op0) { // A^(A&B) -> A^(B&A)
2348 Op1I->swapOperands();
2349 std::swap(A, B);
2350 }
2351 if (B == Op0) { // A^(B&A) -> (B&A)^A
2352 I.swapOperands(); // Simplified below.
2353 std::swap(Op0, Op1);
2354 }
2355 }
2356 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002357
Chris Lattner0a8191e2010-01-05 07:50:36 +00002358 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2359 if (Op0I) {
2360 Value *A, *B;
2361 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2362 Op0I->hasOneUse()) {
2363 if (A == Op1) // (B|A)^B == (A|B)^B
2364 std::swap(A, B);
2365 if (B == Op1) // (A|B)^B == A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002366 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
Craig Topper9d4171a2012-12-20 07:09:41 +00002367 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002368 Op0I->hasOneUse()){
2369 if (A == Op1) // (A&B)^A -> (B&A)^A
2370 std::swap(A, B);
2371 if (B == Op1 && // (B&A)^A == ~B & A
2372 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002373 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002374 }
2375 }
2376 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002377
Chris Lattner0a8191e2010-01-05 07:50:36 +00002378 // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
Craig Topper9d4171a2012-12-20 07:09:41 +00002379 if (Op0I && Op1I && Op0I->isShift() &&
2380 Op0I->getOpcode() == Op1I->getOpcode() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002381 Op0I->getOperand(1) == Op1I->getOperand(1) &&
Benjamin Kramer9d5849f2012-05-28 20:52:48 +00002382 (Op0I->hasOneUse() || Op1I->hasOneUse())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002383 Value *NewOp =
2384 Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
2385 Op0I->getName());
Craig Topper9d4171a2012-12-20 07:09:41 +00002386 return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002387 Op1I->getOperand(1));
2388 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002389
Chris Lattner0a8191e2010-01-05 07:50:36 +00002390 if (Op0I && Op1I) {
2391 Value *A, *B, *C, *D;
2392 // (A & B)^(A | B) -> A ^ B
2393 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2394 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002395 if ((A == C && B == D) || (A == D && B == C))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002396 return BinaryOperator::CreateXor(A, B);
2397 }
2398 // (A | B)^(A & B) -> A ^ B
2399 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2400 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002401 if ((A == C && B == D) || (A == D && B == C))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002402 return BinaryOperator::CreateXor(A, B);
2403 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002404 }
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002405
Chris Lattner0a8191e2010-01-05 07:50:36 +00002406 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
2407 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2408 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
2409 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2410 if (LHS->getOperand(0) == RHS->getOperand(1) &&
2411 LHS->getOperand(1) == RHS->getOperand(0))
2412 LHS->swapOperands();
2413 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2414 LHS->getOperand(1) == RHS->getOperand(1)) {
2415 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2416 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2417 bool isSigned = LHS->isSigned() || RHS->isSigned();
Craig Topper9d4171a2012-12-20 07:09:41 +00002418 return ReplaceInstUsesWith(I,
Pete Cooperebf98c12011-12-17 01:20:32 +00002419 getNewICmpValue(isSigned, Code, Op0, Op1,
2420 Builder));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002421 }
2422 }
2423
2424 // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
2425 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2426 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
2427 if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
Chris Lattner229907c2011-07-18 04:54:35 +00002428 Type *SrcTy = Op0C->getOperand(0)->getType();
Duncan Sands9dff9be2010-02-15 16:12:20 +00002429 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegerTy() &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002430 // Only do this if the casts both really cause code to be generated.
Craig Topper9d4171a2012-12-20 07:09:41 +00002431 ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
Chris Lattner4e8137d2010-02-11 06:26:33 +00002432 I.getType()) &&
Craig Topper9d4171a2012-12-20 07:09:41 +00002433 ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
Chris Lattner4e8137d2010-02-11 06:26:33 +00002434 I.getType())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002435 Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
2436 Op1C->getOperand(0), I.getName());
2437 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2438 }
2439 }
2440 }
2441
2442 return Changed ? &I : 0;
2443}