1 //===- InstCombineAndOrXor.cpp --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the visitAnd, visitOr, and visitXor functions.
12 //===----------------------------------------------------------------------===//
14 #include "InstCombine.h"
15 #include "llvm/Intrinsics.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Support/PatternMatch.h"
19 using namespace PatternMatch;
22 /// AddOne - Add one to a ConstantInt.
23 static Constant *AddOne(Constant *C) {
24 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
26 /// SubOne - Subtract one from a ConstantInt.
27 static Constant *SubOne(ConstantInt *C) {
28 return ConstantInt::get(C->getContext(), C->getValue()-1);
31 /// isFreeToInvert - Return true if the specified value is free to invert (apply
32 /// ~ to). This happens in cases where the ~ can be eliminated.
33 static inline bool isFreeToInvert(Value *V) {
35 if (BinaryOperator::isNot(V))
38 // Constants can be considered to be not'ed values.
39 if (isa<ConstantInt>(V))
42 // Compares can be inverted if they have a single use.
43 if (CmpInst *CI = dyn_cast<CmpInst>(V))
44 return CI->hasOneUse();
49 static inline Value *dyn_castNotVal(Value *V) {
50 // If this is not(not(x)) don't return that this is a not: we want the two
51 // not's to be folded first.
52 if (BinaryOperator::isNot(V)) {
53 Value *Operand = BinaryOperator::getNotArgument(V);
54 if (!isFreeToInvert(Operand))
58 // Constants can be considered to be not'ed values...
59 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
60 return ConstantInt::get(C->getType(), ~C->getValue());
65 /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits
66 /// are carefully arranged to allow folding of expressions such as:
68 /// (A < B) | (A > B) --> (A != B)
70 /// Note that this is only valid if the first and second predicates have the
71 /// same sign. Is illegal to do: (A u< B) | (A s> B)
73 /// Three bits are used to represent the condition, as follows:
78 /// <=> Value Definition
79 /// 000 0 Always false
88 static unsigned getICmpCode(const ICmpInst *ICI) {
89 switch (ICI->getPredicate()) {
91 case ICmpInst::ICMP_UGT: return 1; // 001
92 case ICmpInst::ICMP_SGT: return 1; // 001
93 case ICmpInst::ICMP_EQ: return 2; // 010
94 case ICmpInst::ICMP_UGE: return 3; // 011
95 case ICmpInst::ICMP_SGE: return 3; // 011
96 case ICmpInst::ICMP_ULT: return 4; // 100
97 case ICmpInst::ICMP_SLT: return 4; // 100
98 case ICmpInst::ICMP_NE: return 5; // 101
99 case ICmpInst::ICMP_ULE: return 6; // 110
100 case ICmpInst::ICMP_SLE: return 6; // 110
103 llvm_unreachable("Invalid ICmp predicate!");
108 /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
109 /// predicate into a three bit mask. It also returns whether it is an ordered
110 /// predicate by reference.
111 static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
114 case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000
115 case FCmpInst::FCMP_UNO: return 0; // 000
116 case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001
117 case FCmpInst::FCMP_UGT: return 1; // 001
118 case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010
119 case FCmpInst::FCMP_UEQ: return 2; // 010
120 case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011
121 case FCmpInst::FCMP_UGE: return 3; // 011
122 case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100
123 case FCmpInst::FCMP_ULT: return 4; // 100
124 case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101
125 case FCmpInst::FCMP_UNE: return 5; // 101
126 case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110
127 case FCmpInst::FCMP_ULE: return 6; // 110
130 // Not expecting FCMP_FALSE and FCMP_TRUE;
131 llvm_unreachable("Unexpected FCmp predicate!");
136 /// getICmpValue - This is the complement of getICmpCode, which turns an
137 /// opcode and two operands into either a constant true or false, or a brand
138 /// new ICmp instruction. The sign is passed in to determine which kind
139 /// of predicate to use in the new icmp instruction.
140 static Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS) {
142 default: assert(0 && "Illegal ICmp code!");
144 return ConstantInt::getFalse(LHS->getContext());
147 return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
148 return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
150 return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS);
153 return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
154 return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
157 return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
158 return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
160 return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS);
163 return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
164 return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
166 return ConstantInt::getTrue(LHS->getContext());
170 /// getFCmpValue - This is the complement of getFCmpCode, which turns an
171 /// opcode and two operands into either a FCmp instruction. isordered is passed
172 /// in to determine which kind of predicate to use in the new fcmp instruction.
173 static Value *getFCmpValue(bool isordered, unsigned code,
174 Value *LHS, Value *RHS) {
176 default: llvm_unreachable("Illegal FCmp code!");
179 return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS);
181 return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS);
184 return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS);
186 return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS);
189 return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS);
191 return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS);
194 return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS);
196 return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS);
199 return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS);
201 return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS);
204 return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS);
206 return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS);
209 return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS);
211 return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS);
212 case 7: return ConstantInt::getTrue(LHS->getContext());
216 /// PredicatesFoldable - Return true if both predicates match sign or if at
217 /// least one of them is an equality comparison (which is signless).
218 static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
219 return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
220 (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
221 (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
224 // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where
225 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is
226 // guaranteed to be a binary operator.
227 Instruction *InstCombiner::OptAndOp(Instruction *Op,
230 BinaryOperator &TheAnd) {
231 Value *X = Op->getOperand(0);
232 Constant *Together = 0;
234 Together = ConstantExpr::getAnd(AndRHS, OpRHS);
236 switch (Op->getOpcode()) {
237 case Instruction::Xor:
238 if (Op->hasOneUse()) {
239 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
240 Value *And = Builder->CreateAnd(X, AndRHS);
242 return BinaryOperator::CreateXor(And, Together);
245 case Instruction::Or:
246 if (Together == AndRHS) // (X | C) & C --> C
247 return ReplaceInstUsesWith(TheAnd, AndRHS);
249 if (Op->hasOneUse() && Together != OpRHS) {
250 // (X | C1) & C2 --> (X | (C1&C2)) & C2
251 Value *Or = Builder->CreateOr(X, Together);
253 return BinaryOperator::CreateAnd(Or, AndRHS);
256 case Instruction::Add:
257 if (Op->hasOneUse()) {
258 // Adding a one to a single bit bit-field should be turned into an XOR
259 // of the bit. First thing to check is to see if this AND is with a
260 // single bit constant.
261 const APInt &AndRHSV = cast<ConstantInt>(AndRHS)->getValue();
263 // If there is only one bit set.
264 if (AndRHSV.isPowerOf2()) {
265 // Ok, at this point, we know that we are masking the result of the
266 // ADD down to exactly one bit. If the constant we are adding has
267 // no bits set below this bit, then we can eliminate the ADD.
268 const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue();
270 // Check to see if any bits below the one bit set in AndRHSV are set.
271 if ((AddRHS & (AndRHSV-1)) == 0) {
272 // If not, the only thing that can effect the output of the AND is
273 // the bit specified by AndRHSV. If that bit is set, the effect of
274 // the XOR is to toggle the bit. If it is clear, then the ADD has
276 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
277 TheAnd.setOperand(0, X);
280 // Pull the XOR out of the AND.
281 Value *NewAnd = Builder->CreateAnd(X, AndRHS);
282 NewAnd->takeName(Op);
283 return BinaryOperator::CreateXor(NewAnd, AndRHS);
290 case Instruction::Shl: {
291 // We know that the AND will not produce any of the bits shifted in, so if
292 // the anded constant includes them, clear them now!
294 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
295 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
296 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
297 ConstantInt *CI = ConstantInt::get(AndRHS->getContext(),
298 AndRHS->getValue() & ShlMask);
300 if (CI->getValue() == ShlMask) {
301 // Masking out bits that the shift already masks
302 return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
303 } else if (CI != AndRHS) { // Reducing bits set in and.
304 TheAnd.setOperand(1, CI);
309 case Instruction::LShr: {
310 // We know that the AND will not produce any of the bits shifted in, so if
311 // the anded constant includes them, clear them now! This only applies to
312 // unsigned shifts, because a signed shr may bring in set bits!
314 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
315 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
316 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
317 ConstantInt *CI = ConstantInt::get(Op->getContext(),
318 AndRHS->getValue() & ShrMask);
320 if (CI->getValue() == ShrMask) {
321 // Masking out bits that the shift already masks.
322 return ReplaceInstUsesWith(TheAnd, Op);
323 } else if (CI != AndRHS) {
324 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
329 case Instruction::AShr:
331 // See if this is shifting in some sign extension, then masking it out
333 if (Op->hasOneUse()) {
334 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
335 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
336 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
337 Constant *C = ConstantInt::get(Op->getContext(),
338 AndRHS->getValue() & ShrMask);
339 if (C == AndRHS) { // Masking out bits shifted in.
340 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
341 // Make the argument unsigned.
342 Value *ShVal = Op->getOperand(0);
343 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
344 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
353 /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is
354 /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient
355 /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates
356 /// whether to treat the V, Lo and HI as signed or not. IB is the location to
357 /// insert new instructions.
358 Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
359 bool isSigned, bool Inside,
361 assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
362 ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
363 "Lo is not <= Hi in range emission code!");
366 if (Lo == Hi) // Trivially false.
367 return new ICmpInst(ICmpInst::ICMP_NE, V, V);
369 // V >= Min && V < Hi --> V < Hi
370 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
371 ICmpInst::Predicate pred = (isSigned ?
372 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
373 return new ICmpInst(pred, V, Hi);
376 // Emit V-Lo <u Hi-Lo
377 Constant *NegLo = ConstantExpr::getNeg(Lo);
378 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
379 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
380 return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
383 if (Lo == Hi) // Trivially true.
384 return new ICmpInst(ICmpInst::ICMP_EQ, V, V);
386 // V < Min || V >= Hi -> V > Hi-1
387 Hi = SubOne(cast<ConstantInt>(Hi));
388 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
389 ICmpInst::Predicate pred = (isSigned ?
390 ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
391 return new ICmpInst(pred, V, Hi);
394 // Emit V-Lo >u Hi-1-Lo
395 // Note that Hi has already had one subtracted from it, above.
396 ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
397 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
398 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
399 return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
402 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
403 // any number of 0s on either side. The 1s are allowed to wrap from LSB to
404 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
405 // not, since all 1s are not contiguous.
406 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
407 const APInt& V = Val->getValue();
408 uint32_t BitWidth = Val->getType()->getBitWidth();
409 if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
411 // look for the first zero bit after the run of ones
412 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
413 // look for the first non-zero bit
414 ME = V.getActiveBits();
418 /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
419 /// where isSub determines whether the operator is a sub. If we can fold one of
420 /// the following xforms:
422 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
423 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
424 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
426 /// return (A +/- B).
428 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
429 ConstantInt *Mask, bool isSub,
431 Instruction *LHSI = dyn_cast<Instruction>(LHS);
432 if (!LHSI || LHSI->getNumOperands() != 2 ||
433 !isa<ConstantInt>(LHSI->getOperand(1))) return 0;
435 ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
437 switch (LHSI->getOpcode()) {
439 case Instruction::And:
440 if (ConstantExpr::getAnd(N, Mask) == Mask) {
441 // If the AndRHS is a power of two minus one (0+1+), this is simple.
442 if ((Mask->getValue().countLeadingZeros() +
443 Mask->getValue().countPopulation()) ==
444 Mask->getValue().getBitWidth())
447 // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
448 // part, we don't need any explicit masks to take them out of A. If that
449 // is all N is, ignore it.
450 uint32_t MB = 0, ME = 0;
451 if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
452 uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
453 APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
454 if (MaskedValueIsZero(RHS, Mask))
459 case Instruction::Or:
460 case Instruction::Xor:
461 // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
462 if ((Mask->getValue().countLeadingZeros() +
463 Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
464 && ConstantExpr::getAnd(N, Mask)->isNullValue())
470 return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
471 return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
474 /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
475 Instruction *InstCombiner::FoldAndOfICmps(Instruction &I,
476 ICmpInst *LHS, ICmpInst *RHS) {
477 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
479 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
480 if (PredicatesFoldable(LHSCC, RHSCC)) {
481 if (LHS->getOperand(0) == RHS->getOperand(1) &&
482 LHS->getOperand(1) == RHS->getOperand(0))
484 if (LHS->getOperand(0) == RHS->getOperand(0) &&
485 LHS->getOperand(1) == RHS->getOperand(1)) {
486 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
487 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
488 bool isSigned = LHS->isSigned() || RHS->isSigned();
489 Value *RV = getICmpValue(isSigned, Code, Op0, Op1);
490 if (Instruction *I = dyn_cast<Instruction>(RV))
492 // Otherwise, it's a constant boolean value.
493 return ReplaceInstUsesWith(I, RV);
497 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
498 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
499 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
500 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
501 if (LHSCst == 0 || RHSCst == 0) return 0;
503 if (LHSCst == RHSCst && LHSCC == RHSCC) {
504 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
505 // where C is a power of 2
506 if (LHSCC == ICmpInst::ICMP_ULT &&
507 LHSCst->getValue().isPowerOf2()) {
508 Value *NewOr = Builder->CreateOr(Val, Val2);
509 return new ICmpInst(LHSCC, NewOr, LHSCst);
512 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
513 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
514 Value *NewOr = Builder->CreateOr(Val, Val2);
515 return new ICmpInst(LHSCC, NewOr, LHSCst);
519 // From here on, we only handle:
520 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
521 if (Val != Val2) return 0;
523 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
524 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
525 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
526 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
527 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
530 // We can't fold (ugt x, C) & (sgt x, C2).
531 if (!PredicatesFoldable(LHSCC, RHSCC))
534 // Ensure that the larger constant is on the RHS.
536 if (CmpInst::isSigned(LHSCC) ||
537 (ICmpInst::isEquality(LHSCC) &&
538 CmpInst::isSigned(RHSCC)))
539 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
541 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
545 std::swap(LHSCst, RHSCst);
546 std::swap(LHSCC, RHSCC);
549 // At this point, we know we have two icmp instructions
550 // comparing a value against two constants and and'ing the result
551 // together. Because of the above check, we know that we only have
552 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
553 // (from the icmp folding check above), that the two constants
554 // are not equal and that the larger constant is on the RHS
555 assert(LHSCst != RHSCst && "Compares not folded above?");
558 default: llvm_unreachable("Unknown integer condition code!");
559 case ICmpInst::ICMP_EQ:
561 default: llvm_unreachable("Unknown integer condition code!");
562 case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false
563 case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false
564 case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false
565 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
566 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
567 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
568 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
569 return ReplaceInstUsesWith(I, LHS);
571 case ICmpInst::ICMP_NE:
573 default: llvm_unreachable("Unknown integer condition code!");
574 case ICmpInst::ICMP_ULT:
575 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
576 return new ICmpInst(ICmpInst::ICMP_ULT, Val, LHSCst);
577 break; // (X != 13 & X u< 15) -> no change
578 case ICmpInst::ICMP_SLT:
579 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
580 return new ICmpInst(ICmpInst::ICMP_SLT, Val, LHSCst);
581 break; // (X != 13 & X s< 15) -> no change
582 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
583 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
584 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
585 return ReplaceInstUsesWith(I, RHS);
586 case ICmpInst::ICMP_NE:
587 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
588 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
589 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
590 return new ICmpInst(ICmpInst::ICMP_UGT, Add,
591 ConstantInt::get(Add->getType(), 1));
593 break; // (X != 13 & X != 15) -> no change
596 case ICmpInst::ICMP_ULT:
598 default: llvm_unreachable("Unknown integer condition code!");
599 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
600 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
601 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
602 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
604 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
605 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
606 return ReplaceInstUsesWith(I, LHS);
607 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
611 case ICmpInst::ICMP_SLT:
613 default: llvm_unreachable("Unknown integer condition code!");
614 case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false
615 case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false
616 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
617 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
619 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
620 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
621 return ReplaceInstUsesWith(I, LHS);
622 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
626 case ICmpInst::ICMP_UGT:
628 default: llvm_unreachable("Unknown integer condition code!");
629 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15
630 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
631 return ReplaceInstUsesWith(I, RHS);
632 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
634 case ICmpInst::ICMP_NE:
635 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
636 return new ICmpInst(LHSCC, Val, RHSCst);
637 break; // (X u> 13 & X != 15) -> no change
638 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
639 return InsertRangeTest(Val, AddOne(LHSCst),
640 RHSCst, false, true, I);
641 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
645 case ICmpInst::ICMP_SGT:
647 default: llvm_unreachable("Unknown integer condition code!");
648 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
649 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
650 return ReplaceInstUsesWith(I, RHS);
651 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
653 case ICmpInst::ICMP_NE:
654 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
655 return new ICmpInst(LHSCC, Val, RHSCst);
656 break; // (X s> 13 & X != 15) -> no change
657 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
658 return InsertRangeTest(Val, AddOne(LHSCst),
659 RHSCst, true, true, I);
660 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
669 Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS,
672 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
673 RHS->getPredicate() == FCmpInst::FCMP_ORD) {
674 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
675 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
676 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
677 // If either of the constants are nans, then the whole thing returns
679 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
680 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
681 return new FCmpInst(FCmpInst::FCMP_ORD,
682 LHS->getOperand(0), RHS->getOperand(0));
685 // Handle vector zeros. This occurs because the canonical form of
686 // "fcmp ord x,x" is "fcmp ord x, 0".
687 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
688 isa<ConstantAggregateZero>(RHS->getOperand(1)))
689 return new FCmpInst(FCmpInst::FCMP_ORD,
690 LHS->getOperand(0), RHS->getOperand(0));
694 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
695 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
696 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
699 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
700 // Swap RHS operands to match LHS.
701 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
702 std::swap(Op1LHS, Op1RHS);
705 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
706 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
708 return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
710 if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
711 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
712 if (Op0CC == FCmpInst::FCMP_TRUE)
713 return ReplaceInstUsesWith(I, RHS);
714 if (Op1CC == FCmpInst::FCMP_TRUE)
715 return ReplaceInstUsesWith(I, LHS);
719 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
720 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
723 std::swap(Op0Pred, Op1Pred);
724 std::swap(Op0Ordered, Op1Ordered);
727 // uno && ueq -> uno && (uno || eq) -> ueq
728 // ord && olt -> ord && (ord && lt) -> olt
729 if (Op0Ordered == Op1Ordered)
730 return ReplaceInstUsesWith(I, RHS);
732 // uno && oeq -> uno && (ord && eq) -> false
733 // uno && ord -> false
735 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
736 // ord && ueq -> ord && (uno || eq) -> oeq
737 return cast<Instruction>(getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS));
745 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
746 bool Changed = SimplifyCommutative(I);
747 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
749 if (Value *V = SimplifyAndInst(Op0, Op1, TD))
750 return ReplaceInstUsesWith(I, V);
752 // See if we can simplify any instructions used by the instruction whose sole
753 // purpose is to compute bits we don't care about.
754 if (SimplifyDemandedInstructionBits(I))
757 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
758 const APInt &AndRHSMask = AndRHS->getValue();
759 APInt NotAndRHS(~AndRHSMask);
761 // Optimize a variety of ((val OP C1) & C2) combinations...
762 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
763 Value *Op0LHS = Op0I->getOperand(0);
764 Value *Op0RHS = Op0I->getOperand(1);
765 switch (Op0I->getOpcode()) {
767 case Instruction::Xor:
768 case Instruction::Or:
769 // If the mask is only needed on one incoming arm, push it up.
770 if (!Op0I->hasOneUse()) break;
772 if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
773 // Not masking anything out for the LHS, move to RHS.
774 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
775 Op0RHS->getName()+".masked");
776 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
778 if (!isa<Constant>(Op0RHS) &&
779 MaskedValueIsZero(Op0RHS, NotAndRHS)) {
780 // Not masking anything out for the RHS, move to LHS.
781 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
782 Op0LHS->getName()+".masked");
783 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
787 case Instruction::Add:
788 // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
789 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
790 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
791 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
792 return BinaryOperator::CreateAnd(V, AndRHS);
793 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
794 return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes
797 case Instruction::Sub:
798 // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
799 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
800 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
801 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
802 return BinaryOperator::CreateAnd(V, AndRHS);
804 // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
805 // has 1's for all bits that the subtraction with A might affect.
806 if (Op0I->hasOneUse()) {
807 uint32_t BitWidth = AndRHSMask.getBitWidth();
808 uint32_t Zeros = AndRHSMask.countLeadingZeros();
809 APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
811 ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
812 if (!(A && A->isZero()) && // avoid infinite recursion.
813 MaskedValueIsZero(Op0LHS, Mask)) {
814 Value *NewNeg = Builder->CreateNeg(Op0RHS);
815 return BinaryOperator::CreateAnd(NewNeg, AndRHS);
820 case Instruction::Shl:
821 case Instruction::LShr:
822 // (1 << x) & 1 --> zext(x == 0)
823 // (1 >> x) & 1 --> zext(x == 0)
824 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
826 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
827 return new ZExtInst(NewICmp, I.getType());
832 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
833 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
835 } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) {
836 // If this is an integer truncation or change from signed-to-unsigned, and
837 // if the source is an and/or with immediate, transform it. This
838 // frequently occurs for bitfield accesses.
839 if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) {
840 if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) &&
841 CastOp->getNumOperands() == 2)
842 if (ConstantInt *AndCI =dyn_cast<ConstantInt>(CastOp->getOperand(1))){
843 if (CastOp->getOpcode() == Instruction::And) {
844 // Change: and (cast (and X, C1) to T), C2
845 // into : and (cast X to T), trunc_or_bitcast(C1)&C2
846 // This will fold the two constants together, which may allow
847 // other simplifications.
848 Value *NewCast = Builder->CreateTruncOrBitCast(
849 CastOp->getOperand(0), I.getType(),
850 CastOp->getName()+".shrunk");
851 // trunc_or_bitcast(C1)&C2
852 Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
853 C3 = ConstantExpr::getAnd(C3, AndRHS);
854 return BinaryOperator::CreateAnd(NewCast, C3);
855 } else if (CastOp->getOpcode() == Instruction::Or) {
856 // Change: and (cast (or X, C1) to T), C2
857 // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2
858 Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
859 if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS)
861 return ReplaceInstUsesWith(I, AndRHS);
867 // Try to fold constant and into select arguments.
868 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
869 if (Instruction *R = FoldOpIntoSelect(I, SI))
871 if (isa<PHINode>(Op0))
872 if (Instruction *NV = FoldOpIntoPhi(I))
877 // (~A & ~B) == (~(A | B)) - De Morgan's Law
878 if (Value *Op0NotVal = dyn_castNotVal(Op0))
879 if (Value *Op1NotVal = dyn_castNotVal(Op1))
880 if (Op0->hasOneUse() && Op1->hasOneUse()) {
881 Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
882 I.getName()+".demorgan");
883 return BinaryOperator::CreateNot(Or);
887 Value *A = 0, *B = 0, *C = 0, *D = 0;
888 // (A|B) & ~(A&B) -> A^B
889 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
890 match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
891 ((A == C && B == D) || (A == D && B == C)))
892 return BinaryOperator::CreateXor(A, B);
894 // ~(A&B) & (A|B) -> A^B
895 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
896 match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
897 ((A == C && B == D) || (A == D && B == C)))
898 return BinaryOperator::CreateXor(A, B);
900 if (Op0->hasOneUse() &&
901 match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
902 if (A == Op1) { // (A^B)&A -> A&(A^B)
903 I.swapOperands(); // Simplify below
905 } else if (B == Op1) { // (A^B)&B -> B&(B^A)
906 cast<BinaryOperator>(Op0)->swapOperands();
907 I.swapOperands(); // Simplify below
912 if (Op1->hasOneUse() &&
913 match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
914 if (B == Op0) { // B&(A^B) -> B&(B^A)
915 cast<BinaryOperator>(Op1)->swapOperands();
918 if (A == Op0) // A&(A^B) -> A & ~B
919 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B, "tmp"));
922 // (A&((~A)|B)) -> A&B
923 if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
924 match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
925 return BinaryOperator::CreateAnd(A, Op1);
926 if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
927 match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
928 return BinaryOperator::CreateAnd(A, Op0);
931 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1))
932 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
933 if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS))
936 // If and'ing two fcmp, try combine them into one.
937 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
938 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
939 if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS))
943 // fold (and (cast A), (cast B)) -> (cast (and A, B))
944 if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
945 if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
946 const Type *SrcTy = Op0C->getOperand(0)->getType();
947 if (Op0C->getOpcode() == Op1C->getOpcode() && // same cast kind ?
948 SrcTy == Op1C->getOperand(0)->getType() &&
949 SrcTy->isIntOrIntVectorTy()) {
950 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
952 // Only do this if the casts both really cause code to be generated.
953 if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
954 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
955 Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.getName());
956 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
959 // If this is and(cast(icmp), cast(icmp)), try to fold this even if the
960 // cast is otherwise not optimizable. This happens for vector sexts.
961 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
962 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
963 if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS)) {
964 InsertNewInstBefore(Res, I);
965 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
968 // If this is and(cast(fcmp), cast(fcmp)), try to fold this even if the
969 // cast is otherwise not optimizable. This happens for vector sexts.
970 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
971 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
972 if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS)) {
973 InsertNewInstBefore(Res, I);
974 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
979 // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
980 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
981 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
982 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
983 SI0->getOperand(1) == SI1->getOperand(1) &&
984 (SI0->hasOneUse() || SI1->hasOneUse())) {
986 Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
988 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
993 return Changed ? &I : 0;
996 /// CollectBSwapParts - Analyze the specified subexpression and see if it is
997 /// capable of providing pieces of a bswap. The subexpression provides pieces
998 /// of a bswap if it is proven that each of the non-zero bytes in the output of
999 /// the expression came from the corresponding "byte swapped" byte in some other
1000 /// value. For example, if the current subexpression is "(shl i32 %X, 24)" then
1001 /// we know that the expression deposits the low byte of %X into the high byte
1002 /// of the bswap result and that all other bytes are zero. This expression is
1003 /// accepted, the high byte of ByteValues is set to X to indicate a correct
1006 /// This function returns true if the match was unsuccessful and false if so.
1007 /// On entry to the function the "OverallLeftShift" is a signed integer value
1008 /// indicating the number of bytes that the subexpression is later shifted. For
1009 /// example, if the expression is later right shifted by 16 bits, the
1010 /// OverallLeftShift value would be -2 on entry. This is used to specify which
1011 /// byte of ByteValues is actually being set.
1013 /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
1014 /// byte is masked to zero by a user. For example, in (X & 255), X will be
1015 /// processed with a bytemask of 1. Because bytemask is 32-bits, this limits
1016 /// this function to working on up to 32-byte (256 bit) values. ByteMask is
1017 /// always in the local (OverallLeftShift) coordinate space.
1019 static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
1020 SmallVector<Value*, 8> &ByteValues) {
1021 if (Instruction *I = dyn_cast<Instruction>(V)) {
1022 // If this is an or instruction, it may be an inner node of the bswap.
1023 if (I->getOpcode() == Instruction::Or) {
1024 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1026 CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
1030 // If this is a logical shift by a constant multiple of 8, recurse with
1031 // OverallLeftShift and ByteMask adjusted.
1032 if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
1034 cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
1035 // Ensure the shift amount is defined and of a byte value.
1036 if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
1039 unsigned ByteShift = ShAmt >> 3;
1040 if (I->getOpcode() == Instruction::Shl) {
1041 // X << 2 -> collect(X, +2)
1042 OverallLeftShift += ByteShift;
1043 ByteMask >>= ByteShift;
1045 // X >>u 2 -> collect(X, -2)
1046 OverallLeftShift -= ByteShift;
1047 ByteMask <<= ByteShift;
1048 ByteMask &= (~0U >> (32-ByteValues.size()));
1051 if (OverallLeftShift >= (int)ByteValues.size()) return true;
1052 if (OverallLeftShift <= -(int)ByteValues.size()) return true;
1054 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1058 // If this is a logical 'and' with a mask that clears bytes, clear the
1059 // corresponding bytes in ByteMask.
1060 if (I->getOpcode() == Instruction::And &&
1061 isa<ConstantInt>(I->getOperand(1))) {
1062 // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
1063 unsigned NumBytes = ByteValues.size();
1064 APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
1065 const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
1067 for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
1068 // If this byte is masked out by a later operation, we don't care what
1070 if ((ByteMask & (1 << i)) == 0)
1073 // If the AndMask is all zeros for this byte, clear the bit.
1074 APInt MaskB = AndMask & Byte;
1076 ByteMask &= ~(1U << i);
1080 // If the AndMask is not all ones for this byte, it's not a bytezap.
1084 // Otherwise, this byte is kept.
1087 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1092 // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be
1093 // the input value to the bswap. Some observations: 1) if more than one byte
1094 // is demanded from this input, then it could not be successfully assembled
1095 // into a byteswap. At least one of the two bytes would not be aligned with
1096 // their ultimate destination.
1097 if (!isPowerOf2_32(ByteMask)) return true;
1098 unsigned InputByteNo = CountTrailingZeros_32(ByteMask);
1100 // 2) The input and ultimate destinations must line up: if byte 3 of an i32
1101 // is demanded, it needs to go into byte 0 of the result. This means that the
1102 // byte needs to be shifted until it lands in the right byte bucket. The
1103 // shift amount depends on the position: if the byte is coming from the high
1104 // part of the value (e.g. byte 3) then it must be shifted right. If from the
1105 // low part, it must be shifted left.
1106 unsigned DestByteNo = InputByteNo + OverallLeftShift;
1107 if (InputByteNo < ByteValues.size()/2) {
1108 if (ByteValues.size()-1-DestByteNo != InputByteNo)
1111 if (ByteValues.size()-1-DestByteNo != InputByteNo)
1115 // If the destination byte value is already defined, the values are or'd
1116 // together, which isn't a bswap (unless it's an or of the same bits).
1117 if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
1119 ByteValues[DestByteNo] = V;
1123 /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
1124 /// If so, insert the new bswap intrinsic and return it.
1125 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
1126 const IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
1127 if (!ITy || ITy->getBitWidth() % 16 ||
1128 // ByteMask only allows up to 32-byte values.
1129 ITy->getBitWidth() > 32*8)
1130 return 0; // Can only bswap pairs of bytes. Can't do vectors.
1132 /// ByteValues - For each byte of the result, we keep track of which value
1133 /// defines each byte.
1134 SmallVector<Value*, 8> ByteValues;
1135 ByteValues.resize(ITy->getBitWidth()/8);
1137 // Try to find all the pieces corresponding to the bswap.
1138 uint32_t ByteMask = ~0U >> (32-ByteValues.size());
1139 if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
1142 // Check to see if all of the bytes come from the same value.
1143 Value *V = ByteValues[0];
1144 if (V == 0) return 0; // Didn't find a byte? Must be zero.
1146 // Check to make sure that all of the bytes come from the same value.
1147 for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
1148 if (ByteValues[i] != V)
1150 const Type *Tys[] = { ITy };
1151 Module *M = I.getParent()->getParent()->getParent();
1152 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
1153 return CallInst::Create(F, V);
1156 /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check
1157 /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
1158 /// we can simplify this expression to "cond ? C : D or B".
1159 static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
1160 Value *C, Value *D) {
1161 // If A is not a select of -1/0, this cannot match.
1163 if (!match(A, m_SExt(m_Value(Cond))) ||
1164 !Cond->getType()->isIntegerTy(1))
1167 // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
1168 if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
1169 return SelectInst::Create(Cond, C, B);
1170 if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
1171 return SelectInst::Create(Cond, C, B);
1173 // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
1174 if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
1175 return SelectInst::Create(Cond, C, D);
1176 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
1177 return SelectInst::Create(Cond, C, D);
1181 /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
1182 Instruction *InstCombiner::FoldOrOfICmps(Instruction &I,
1183 ICmpInst *LHS, ICmpInst *RHS) {
1184 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1186 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1187 if (PredicatesFoldable(LHSCC, RHSCC)) {
1188 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1189 LHS->getOperand(1) == RHS->getOperand(0))
1190 LHS->swapOperands();
1191 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1192 LHS->getOperand(1) == RHS->getOperand(1)) {
1193 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1194 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1195 bool isSigned = LHS->isSigned() || RHS->isSigned();
1196 Value *RV = getICmpValue(isSigned, Code, Op0, Op1);
1197 if (Instruction *I = dyn_cast<Instruction>(RV))
1199 // Otherwise, it's a constant boolean value.
1200 return ReplaceInstUsesWith(I, RV);
1204 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
1205 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1206 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1207 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1208 if (LHSCst == 0 || RHSCst == 0) return 0;
1210 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1211 if (LHSCst == RHSCst && LHSCC == RHSCC &&
1212 LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1213 Value *NewOr = Builder->CreateOr(Val, Val2);
1214 return new ICmpInst(LHSCC, NewOr, LHSCst);
1217 // From here on, we only handle:
1218 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1219 if (Val != Val2) return 0;
1221 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1222 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1223 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1224 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1225 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1228 // We can't fold (ugt x, C) | (sgt x, C2).
1229 if (!PredicatesFoldable(LHSCC, RHSCC))
1232 // Ensure that the larger constant is on the RHS.
1234 if (CmpInst::isSigned(LHSCC) ||
1235 (ICmpInst::isEquality(LHSCC) &&
1236 CmpInst::isSigned(RHSCC)))
1237 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1239 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
1242 std::swap(LHS, RHS);
1243 std::swap(LHSCst, RHSCst);
1244 std::swap(LHSCC, RHSCC);
1247 // At this point, we know we have two icmp instructions
1248 // comparing a value against two constants and or'ing the result
1249 // together. Because of the above check, we know that we only have
1250 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1251 // icmp folding check above), that the two constants are not
1253 assert(LHSCst != RHSCst && "Compares not folded above?");
1256 default: llvm_unreachable("Unknown integer condition code!");
1257 case ICmpInst::ICMP_EQ:
1259 default: llvm_unreachable("Unknown integer condition code!");
1260 case ICmpInst::ICMP_EQ:
1261 if (LHSCst == SubOne(RHSCst)) {
1262 // (X == 13 | X == 14) -> X-13 <u 2
1263 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1264 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1265 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1266 return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
1268 break; // (X == 13 | X == 15) -> no change
1269 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1270 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
1272 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
1273 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
1274 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
1275 return ReplaceInstUsesWith(I, RHS);
1278 case ICmpInst::ICMP_NE:
1280 default: llvm_unreachable("Unknown integer condition code!");
1281 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
1282 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
1283 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
1284 return ReplaceInstUsesWith(I, LHS);
1285 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
1286 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
1287 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
1288 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1291 case ICmpInst::ICMP_ULT:
1293 default: llvm_unreachable("Unknown integer condition code!");
1294 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
1296 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
1297 // If RHSCst is [us]MAXINT, it is always false. Not handling
1298 // this can cause overflow.
1299 if (RHSCst->isMaxValue(false))
1300 return ReplaceInstUsesWith(I, LHS);
1301 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
1303 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
1305 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
1306 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
1307 return ReplaceInstUsesWith(I, RHS);
1308 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
1312 case ICmpInst::ICMP_SLT:
1314 default: llvm_unreachable("Unknown integer condition code!");
1315 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
1317 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
1318 // If RHSCst is [us]MAXINT, it is always false. Not handling
1319 // this can cause overflow.
1320 if (RHSCst->isMaxValue(true))
1321 return ReplaceInstUsesWith(I, LHS);
1322 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
1324 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
1326 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
1327 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
1328 return ReplaceInstUsesWith(I, RHS);
1329 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
1333 case ICmpInst::ICMP_UGT:
1335 default: llvm_unreachable("Unknown integer condition code!");
1336 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
1337 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
1338 return ReplaceInstUsesWith(I, LHS);
1339 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
1341 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
1342 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
1343 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1344 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
1348 case ICmpInst::ICMP_SGT:
1350 default: llvm_unreachable("Unknown integer condition code!");
1351 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
1352 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
1353 return ReplaceInstUsesWith(I, LHS);
1354 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
1356 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
1357 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
1358 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1359 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
1367 Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS,
1369 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
1370 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
1371 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1372 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1373 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1374 // If either of the constants are nans, then the whole thing returns
1376 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1377 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1379 // Otherwise, no need to compare the two constants, compare the
1381 return new FCmpInst(FCmpInst::FCMP_UNO,
1382 LHS->getOperand(0), RHS->getOperand(0));
1385 // Handle vector zeros. This occurs because the canonical form of
1386 // "fcmp uno x,x" is "fcmp uno x, 0".
1387 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1388 isa<ConstantAggregateZero>(RHS->getOperand(1)))
1389 return new FCmpInst(FCmpInst::FCMP_UNO,
1390 LHS->getOperand(0), RHS->getOperand(0));
1395 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1396 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1397 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1399 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1400 // Swap RHS operands to match LHS.
1401 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1402 std::swap(Op1LHS, Op1RHS);
1404 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1405 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1407 return new FCmpInst((FCmpInst::Predicate)Op0CC,
1409 if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
1410 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1411 if (Op0CC == FCmpInst::FCMP_FALSE)
1412 return ReplaceInstUsesWith(I, RHS);
1413 if (Op1CC == FCmpInst::FCMP_FALSE)
1414 return ReplaceInstUsesWith(I, LHS);
1417 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1418 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
1419 if (Op0Ordered == Op1Ordered) {
1420 // If both are ordered or unordered, return a new fcmp with
1421 // or'ed predicates.
1422 Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS);
1423 if (Instruction *I = dyn_cast<Instruction>(RV))
1425 // Otherwise, it's a constant boolean value...
1426 return ReplaceInstUsesWith(I, RV);
1432 /// FoldOrWithConstants - This helper function folds:
1434 /// ((A | B) & C1) | (B & C2)
1440 /// when the XOR of the two constants is "all ones" (-1).
1441 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
1442 Value *A, Value *B, Value *C) {
1443 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
1447 ConstantInt *CI2 = 0;
1448 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
1450 APInt Xor = CI1->getValue() ^ CI2->getValue();
1451 if (!Xor.isAllOnesValue()) return 0;
1453 if (V1 == A || V1 == B) {
1454 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1455 return BinaryOperator::CreateOr(NewOp, V1);
1461 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
1462 bool Changed = SimplifyCommutative(I);
1463 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1465 if (Value *V = SimplifyOrInst(Op0, Op1, TD))
1466 return ReplaceInstUsesWith(I, V);
1469 // See if we can simplify any instructions used by the instruction whose sole
1470 // purpose is to compute bits we don't care about.
1471 if (SimplifyDemandedInstructionBits(I))
1474 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1475 ConstantInt *C1 = 0; Value *X = 0;
1476 // (X & C1) | C2 --> (X | C2) & (C1|C2)
1477 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
1479 Value *Or = Builder->CreateOr(X, RHS);
1481 return BinaryOperator::CreateAnd(Or,
1482 ConstantInt::get(I.getContext(),
1483 RHS->getValue() | C1->getValue()));
1486 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1487 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
1489 Value *Or = Builder->CreateOr(X, RHS);
1491 return BinaryOperator::CreateXor(Or,
1492 ConstantInt::get(I.getContext(),
1493 C1->getValue() & ~RHS->getValue()));
1496 // Try to fold constant and into select arguments.
1497 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1498 if (Instruction *R = FoldOpIntoSelect(I, SI))
1500 if (isa<PHINode>(Op0))
1501 if (Instruction *NV = FoldOpIntoPhi(I))
1505 Value *A = 0, *B = 0;
1506 ConstantInt *C1 = 0, *C2 = 0;
1508 // (A | B) | C and A | (B | C) -> bswap if possible.
1509 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
1510 if (match(Op0, m_Or(m_Value(), m_Value())) ||
1511 match(Op1, m_Or(m_Value(), m_Value())) ||
1512 (match(Op0, m_Shift(m_Value(), m_Value())) &&
1513 match(Op1, m_Shift(m_Value(), m_Value())))) {
1514 if (Instruction *BSwap = MatchBSwap(I))
1518 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
1519 if (Op0->hasOneUse() &&
1520 match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1521 MaskedValueIsZero(Op1, C1->getValue())) {
1522 Value *NOr = Builder->CreateOr(A, Op1);
1524 return BinaryOperator::CreateXor(NOr, C1);
1527 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
1528 if (Op1->hasOneUse() &&
1529 match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1530 MaskedValueIsZero(Op0, C1->getValue())) {
1531 Value *NOr = Builder->CreateOr(A, Op0);
1533 return BinaryOperator::CreateXor(NOr, C1);
1537 Value *C = 0, *D = 0;
1538 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
1539 match(Op1, m_And(m_Value(B), m_Value(D)))) {
1540 Value *V1 = 0, *V2 = 0, *V3 = 0;
1541 C1 = dyn_cast<ConstantInt>(C);
1542 C2 = dyn_cast<ConstantInt>(D);
1543 if (C1 && C2) { // (A & C1)|(B & C2)
1544 // If we have: ((V + N) & C1) | (V & C2)
1545 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
1546 // replace with V+N.
1547 if (C1->getValue() == ~C2->getValue()) {
1548 if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
1549 match(A, m_Add(m_Value(V1), m_Value(V2)))) {
1550 // Add commutes, try both ways.
1551 if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
1552 return ReplaceInstUsesWith(I, A);
1553 if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
1554 return ReplaceInstUsesWith(I, A);
1556 // Or commutes, try both ways.
1557 if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
1558 match(B, m_Add(m_Value(V1), m_Value(V2)))) {
1559 // Add commutes, try both ways.
1560 if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
1561 return ReplaceInstUsesWith(I, B);
1562 if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
1563 return ReplaceInstUsesWith(I, B);
1567 if ((C1->getValue() & C2->getValue()) == 0) {
1568 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
1569 // iff (C1&C2) == 0 and (N&~C1) == 0
1570 if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
1571 ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) || // (V|N)
1572 (V2 == B && MaskedValueIsZero(V1, ~C1->getValue())))) // (N|V)
1573 return BinaryOperator::CreateAnd(A,
1574 ConstantInt::get(A->getContext(),
1575 C1->getValue()|C2->getValue()));
1576 // Or commutes, try both ways.
1577 if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
1578 ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) || // (V|N)
1579 (V2 == A && MaskedValueIsZero(V1, ~C2->getValue())))) // (N|V)
1580 return BinaryOperator::CreateAnd(B,
1581 ConstantInt::get(B->getContext(),
1582 C1->getValue()|C2->getValue()));
1584 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
1585 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
1586 ConstantInt *C3 = 0, *C4 = 0;
1587 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
1588 (C3->getValue() & ~C1->getValue()) == 0 &&
1589 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
1590 (C4->getValue() & ~C2->getValue()) == 0) {
1591 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
1592 return BinaryOperator::CreateAnd(V2,
1593 ConstantInt::get(B->getContext(),
1594 C1->getValue()|C2->getValue()));
1599 // Check to see if we have any common things being and'ed. If so, find the
1600 // terms for V1 & (V2|V3).
1601 if (Op0->hasOneUse() || Op1->hasOneUse()) {
1603 if (A == B) // (A & C)|(A & D) == A & (C|D)
1604 V1 = A, V2 = C, V3 = D;
1605 else if (A == D) // (A & C)|(B & A) == A & (B|C)
1606 V1 = A, V2 = B, V3 = C;
1607 else if (C == B) // (A & C)|(C & D) == C & (A|D)
1608 V1 = C, V2 = A, V3 = D;
1609 else if (C == D) // (A & C)|(B & C) == C & (A|B)
1610 V1 = C, V2 = A, V3 = B;
1613 Value *Or = Builder->CreateOr(V2, V3, "tmp");
1614 return BinaryOperator::CreateAnd(V1, Or);
1618 // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants.
1619 // Don't do this for vector select idioms, the code generator doesn't handle
1621 if (!isa<VectorType>(I.getType())) {
1622 if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
1624 if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
1626 if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
1628 if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
1632 // ((A&~B)|(~A&B)) -> A^B
1633 if ((match(C, m_Not(m_Specific(D))) &&
1634 match(B, m_Not(m_Specific(A)))))
1635 return BinaryOperator::CreateXor(A, D);
1636 // ((~B&A)|(~A&B)) -> A^B
1637 if ((match(A, m_Not(m_Specific(D))) &&
1638 match(B, m_Not(m_Specific(C)))))
1639 return BinaryOperator::CreateXor(C, D);
1640 // ((A&~B)|(B&~A)) -> A^B
1641 if ((match(C, m_Not(m_Specific(B))) &&
1642 match(D, m_Not(m_Specific(A)))))
1643 return BinaryOperator::CreateXor(A, B);
1644 // ((~B&A)|(B&~A)) -> A^B
1645 if ((match(A, m_Not(m_Specific(B))) &&
1646 match(D, m_Not(m_Specific(C)))))
1647 return BinaryOperator::CreateXor(C, B);
1650 // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
1651 if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
1652 if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
1653 if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
1654 SI0->getOperand(1) == SI1->getOperand(1) &&
1655 (SI0->hasOneUse() || SI1->hasOneUse())) {
1656 Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
1658 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
1659 SI1->getOperand(1));
1663 // ((A|B)&1)|(B&-2) -> (A&1) | B
1664 if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
1665 match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
1666 Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C);
1667 if (Ret) return Ret;
1669 // (B&-2)|((A|B)&1) -> (A&1) | B
1670 if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
1671 match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
1672 Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C);
1673 if (Ret) return Ret;
1676 // (~A | ~B) == (~(A & B)) - De Morgan's Law
1677 if (Value *Op0NotVal = dyn_castNotVal(Op0))
1678 if (Value *Op1NotVal = dyn_castNotVal(Op1))
1679 if (Op0->hasOneUse() && Op1->hasOneUse()) {
1680 Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
1681 I.getName()+".demorgan");
1682 return BinaryOperator::CreateNot(And);
1685 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
1686 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
1687 if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS))
1690 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
1691 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1692 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
1693 if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS))
1696 // fold (or (cast A), (cast B)) -> (cast (or A, B))
1697 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
1698 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
1699 if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
1700 const Type *SrcTy = Op0C->getOperand(0)->getType();
1701 if (SrcTy == Op1C->getOperand(0)->getType() &&
1702 SrcTy->isIntOrIntVectorTy()) {
1703 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
1705 if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
1706 // Only do this if the casts both really cause code to be
1708 ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
1709 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
1710 Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.getName());
1711 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
1714 // If this is or(cast(icmp), cast(icmp)), try to fold this even if the
1715 // cast is otherwise not optimizable. This happens for vector sexts.
1716 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
1717 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
1718 if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS)) {
1719 InsertNewInstBefore(Res, I);
1720 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
1723 // If this is or(cast(fcmp), cast(fcmp)), try to fold this even if the
1724 // cast is otherwise not optimizable. This happens for vector sexts.
1725 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
1726 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
1727 if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS)) {
1728 InsertNewInstBefore(Res, I);
1729 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
1735 return Changed ? &I : 0;
1738 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
1739 bool Changed = SimplifyCommutative(I);
1740 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1742 if (isa<UndefValue>(Op1)) {
1743 if (isa<UndefValue>(Op0))
1744 // Handle undef ^ undef -> 0 special case. This is a common
1746 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1747 return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef
1752 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
1754 // See if we can simplify any instructions used by the instruction whose sole
1755 // purpose is to compute bits we don't care about.
1756 if (SimplifyDemandedInstructionBits(I))
1758 if (isa<VectorType>(I.getType()))
1759 if (isa<ConstantAggregateZero>(Op1))
1760 return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X
1762 // Is this a ~ operation?
1763 if (Value *NotOp = dyn_castNotVal(&I)) {
1764 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
1765 if (Op0I->getOpcode() == Instruction::And ||
1766 Op0I->getOpcode() == Instruction::Or) {
1767 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
1768 // ~(~X | Y) === (X & ~Y) - De Morgan's Law
1769 if (dyn_castNotVal(Op0I->getOperand(1)))
1770 Op0I->swapOperands();
1771 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
1773 Builder->CreateNot(Op0I->getOperand(1),
1774 Op0I->getOperand(1)->getName()+".not");
1775 if (Op0I->getOpcode() == Instruction::And)
1776 return BinaryOperator::CreateOr(Op0NotVal, NotY);
1777 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
1780 // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
1781 // ~(X | Y) === (~X & ~Y) - De Morgan's Law
1782 if (isFreeToInvert(Op0I->getOperand(0)) &&
1783 isFreeToInvert(Op0I->getOperand(1))) {
1785 Builder->CreateNot(Op0I->getOperand(0), "notlhs");
1787 Builder->CreateNot(Op0I->getOperand(1), "notrhs");
1788 if (Op0I->getOpcode() == Instruction::And)
1789 return BinaryOperator::CreateOr(NotX, NotY);
1790 return BinaryOperator::CreateAnd(NotX, NotY);
1793 } else if (Op0I->getOpcode() == Instruction::AShr) {
1794 // ~(~X >>s Y) --> (X >>s Y)
1795 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
1796 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
1802 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1803 if (RHS->isOne() && Op0->hasOneUse()) {
1804 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
1805 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
1806 return new ICmpInst(ICI->getInversePredicate(),
1807 ICI->getOperand(0), ICI->getOperand(1));
1809 if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
1810 return new FCmpInst(FCI->getInversePredicate(),
1811 FCI->getOperand(0), FCI->getOperand(1));
1814 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
1815 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
1816 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
1817 if (CI->hasOneUse() && Op0C->hasOneUse()) {
1818 Instruction::CastOps Opcode = Op0C->getOpcode();
1819 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
1820 (RHS == ConstantExpr::getCast(Opcode,
1821 ConstantInt::getTrue(I.getContext()),
1822 Op0C->getDestTy()))) {
1823 CI->setPredicate(CI->getInversePredicate());
1824 return CastInst::Create(Opcode, CI, Op0C->getType());
1830 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1831 // ~(c-X) == X-c-1 == X+(-c-1)
1832 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
1833 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
1834 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
1835 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
1836 ConstantInt::get(I.getType(), 1));
1837 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
1840 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
1841 if (Op0I->getOpcode() == Instruction::Add) {
1842 // ~(X-c) --> (-c-1)-X
1843 if (RHS->isAllOnesValue()) {
1844 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
1845 return BinaryOperator::CreateSub(
1846 ConstantExpr::getSub(NegOp0CI,
1847 ConstantInt::get(I.getType(), 1)),
1848 Op0I->getOperand(0));
1849 } else if (RHS->getValue().isSignBit()) {
1850 // (X + C) ^ signbit -> (X + C + signbit)
1851 Constant *C = ConstantInt::get(I.getContext(),
1852 RHS->getValue() + Op0CI->getValue());
1853 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
1856 } else if (Op0I->getOpcode() == Instruction::Or) {
1857 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
1858 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
1859 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
1860 // Anything in both C1 and C2 is known to be zero, remove it from
1862 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
1863 NewRHS = ConstantExpr::getAnd(NewRHS,
1864 ConstantExpr::getNot(CommonBits));
1866 I.setOperand(0, Op0I->getOperand(0));
1867 I.setOperand(1, NewRHS);
1874 // Try to fold constant and into select arguments.
1875 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1876 if (Instruction *R = FoldOpIntoSelect(I, SI))
1878 if (isa<PHINode>(Op0))
1879 if (Instruction *NV = FoldOpIntoPhi(I))
1883 if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
1885 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
1887 if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
1889 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
1892 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
1895 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
1896 if (A == Op0) { // B^(B|A) == (A|B)^B
1897 Op1I->swapOperands();
1899 std::swap(Op0, Op1);
1900 } else if (B == Op0) { // B^(A|B) == (A|B)^B
1901 I.swapOperands(); // Simplified below.
1902 std::swap(Op0, Op1);
1904 } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) {
1905 return ReplaceInstUsesWith(I, B); // A^(A^B) == B
1906 } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) {
1907 return ReplaceInstUsesWith(I, A); // A^(B^A) == B
1908 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
1910 if (A == Op0) { // A^(A&B) -> A^(B&A)
1911 Op1I->swapOperands();
1914 if (B == Op0) { // A^(B&A) -> (B&A)^A
1915 I.swapOperands(); // Simplified below.
1916 std::swap(Op0, Op1);
1921 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
1924 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
1925 Op0I->hasOneUse()) {
1926 if (A == Op1) // (B|A)^B == (A|B)^B
1928 if (B == Op1) // (A|B)^B == A & ~B
1929 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1, "tmp"));
1930 } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) {
1931 return ReplaceInstUsesWith(I, B); // (A^B)^A == B
1932 } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) {
1933 return ReplaceInstUsesWith(I, A); // (B^A)^A == B
1934 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
1936 if (A == Op1) // (A&B)^A -> (B&A)^A
1938 if (B == Op1 && // (B&A)^A == ~B & A
1939 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
1940 return BinaryOperator::CreateAnd(Builder->CreateNot(A, "tmp"), Op1);
1945 // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
1946 if (Op0I && Op1I && Op0I->isShift() &&
1947 Op0I->getOpcode() == Op1I->getOpcode() &&
1948 Op0I->getOperand(1) == Op1I->getOperand(1) &&
1949 (Op1I->hasOneUse() || Op1I->hasOneUse())) {
1951 Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
1953 return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
1954 Op1I->getOperand(1));
1958 Value *A, *B, *C, *D;
1959 // (A & B)^(A | B) -> A ^ B
1960 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
1961 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
1962 if ((A == C && B == D) || (A == D && B == C))
1963 return BinaryOperator::CreateXor(A, B);
1965 // (A | B)^(A & B) -> A ^ B
1966 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
1967 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
1968 if ((A == C && B == D) || (A == D && B == C))
1969 return BinaryOperator::CreateXor(A, B);
1973 if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
1974 match(Op0I, m_And(m_Value(A), m_Value(B))) &&
1975 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
1976 // (X & Y)^(X & Y) -> (Y^Z) & X
1977 Value *X = 0, *Y = 0, *Z = 0;
1979 X = A, Y = B, Z = D;
1981 X = A, Y = B, Z = C;
1983 X = B, Y = A, Z = D;
1985 X = B, Y = A, Z = C;
1988 Value *NewOp = Builder->CreateXor(Y, Z, Op0->getName());
1989 return BinaryOperator::CreateAnd(NewOp, X);
1994 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
1995 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
1996 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
1997 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
1998 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1999 LHS->getOperand(1) == RHS->getOperand(0))
2000 LHS->swapOperands();
2001 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2002 LHS->getOperand(1) == RHS->getOperand(1)) {
2003 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2004 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2005 bool isSigned = LHS->isSigned() || RHS->isSigned();
2006 Value *RV = getICmpValue(isSigned, Code, Op0, Op1);
2007 if (Instruction *I = dyn_cast<Instruction>(RV))
2009 // Otherwise, it's a constant boolean value.
2010 return ReplaceInstUsesWith(I, RV);
2014 // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
2015 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2016 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
2017 if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
2018 const Type *SrcTy = Op0C->getOperand(0)->getType();
2019 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegerTy() &&
2020 // Only do this if the casts both really cause code to be generated.
2021 ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
2023 ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
2025 Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
2026 Op1C->getOperand(0), I.getName());
2027 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2032 return Changed ? &I : 0;