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 "InstCombineInternal.h"
15 #include "llvm/Analysis/InstructionSimplify.h"
16 #include "llvm/IR/ConstantRange.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/PatternMatch.h"
19 #include "llvm/Transforms/Utils/CmpInstAnalysis.h"
21 using namespace PatternMatch;
23 #define DEBUG_TYPE "instcombine"
25 /// isFreeToInvert - Return true if the specified value is free to invert (apply
26 /// ~ to). This happens in cases where the ~ can be eliminated.
27 static inline bool isFreeToInvert(Value *V) {
29 if (BinaryOperator::isNot(V))
32 // Constants can be considered to be not'ed values.
33 if (isa<ConstantInt>(V))
36 // Compares can be inverted if they have a single use.
37 if (CmpInst *CI = dyn_cast<CmpInst>(V))
38 return CI->hasOneUse();
43 static inline Value *dyn_castNotVal(Value *V) {
44 // If this is not(not(x)) don't return that this is a not: we want the two
45 // not's to be folded first.
46 if (BinaryOperator::isNot(V)) {
47 Value *Operand = BinaryOperator::getNotArgument(V);
48 if (!isFreeToInvert(Operand))
52 // Constants can be considered to be not'ed values...
53 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
54 return ConstantInt::get(C->getType(), ~C->getValue());
58 /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp
59 /// predicate into a three bit mask. It also returns whether it is an ordered
60 /// predicate by reference.
61 static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) {
64 case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000
65 case FCmpInst::FCMP_UNO: return 0; // 000
66 case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001
67 case FCmpInst::FCMP_UGT: return 1; // 001
68 case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010
69 case FCmpInst::FCMP_UEQ: return 2; // 010
70 case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011
71 case FCmpInst::FCMP_UGE: return 3; // 011
72 case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100
73 case FCmpInst::FCMP_ULT: return 4; // 100
74 case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101
75 case FCmpInst::FCMP_UNE: return 5; // 101
76 case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110
77 case FCmpInst::FCMP_ULE: return 6; // 110
80 // Not expecting FCMP_FALSE and FCMP_TRUE;
81 llvm_unreachable("Unexpected FCmp predicate!");
85 /// getNewICmpValue - This is the complement of getICmpCode, which turns an
86 /// opcode and two operands into either a constant true or false, or a brand
87 /// new ICmp instruction. The sign is passed in to determine which kind
88 /// of predicate to use in the new icmp instruction.
89 static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
90 InstCombiner::BuilderTy *Builder) {
91 ICmpInst::Predicate NewPred;
92 if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred))
94 return Builder->CreateICmp(NewPred, LHS, RHS);
97 /// getFCmpValue - This is the complement of getFCmpCode, which turns an
98 /// opcode and two operands into either a FCmp instruction. isordered is passed
99 /// in to determine which kind of predicate to use in the new fcmp instruction.
100 static Value *getFCmpValue(bool isordered, unsigned code,
101 Value *LHS, Value *RHS,
102 InstCombiner::BuilderTy *Builder) {
103 CmpInst::Predicate Pred;
105 default: llvm_unreachable("Illegal FCmp code!");
106 case 0: Pred = isordered ? FCmpInst::FCMP_ORD : FCmpInst::FCMP_UNO; break;
107 case 1: Pred = isordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; break;
108 case 2: Pred = isordered ? FCmpInst::FCMP_OEQ : FCmpInst::FCMP_UEQ; break;
109 case 3: Pred = isordered ? FCmpInst::FCMP_OGE : FCmpInst::FCMP_UGE; break;
110 case 4: Pred = isordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; break;
111 case 5: Pred = isordered ? FCmpInst::FCMP_ONE : FCmpInst::FCMP_UNE; break;
112 case 6: Pred = isordered ? FCmpInst::FCMP_OLE : FCmpInst::FCMP_ULE; break;
114 if (!isordered) return ConstantInt::getTrue(LHS->getContext());
115 Pred = FCmpInst::FCMP_ORD; break;
117 return Builder->CreateFCmp(Pred, LHS, RHS);
120 /// \brief Transform BITWISE_OP(BSWAP(A),BSWAP(B)) to BSWAP(BITWISE_OP(A, B))
121 /// \param I Binary operator to transform.
122 /// \return Pointer to node that must replace the original binary operator, or
123 /// null pointer if no transformation was made.
124 Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) {
125 IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
128 if (I.getType()->isVectorTy()) return nullptr;
130 // Can only do bitwise ops.
131 unsigned Op = I.getOpcode();
132 if (Op != Instruction::And && Op != Instruction::Or &&
133 Op != Instruction::Xor)
136 Value *OldLHS = I.getOperand(0);
137 Value *OldRHS = I.getOperand(1);
138 ConstantInt *ConstLHS = dyn_cast<ConstantInt>(OldLHS);
139 ConstantInt *ConstRHS = dyn_cast<ConstantInt>(OldRHS);
140 IntrinsicInst *IntrLHS = dyn_cast<IntrinsicInst>(OldLHS);
141 IntrinsicInst *IntrRHS = dyn_cast<IntrinsicInst>(OldRHS);
142 bool IsBswapLHS = (IntrLHS && IntrLHS->getIntrinsicID() == Intrinsic::bswap);
143 bool IsBswapRHS = (IntrRHS && IntrRHS->getIntrinsicID() == Intrinsic::bswap);
145 if (!IsBswapLHS && !IsBswapRHS)
148 if (!IsBswapLHS && !ConstLHS)
151 if (!IsBswapRHS && !ConstRHS)
154 /// OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
155 /// OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
156 Value *NewLHS = IsBswapLHS ? IntrLHS->getOperand(0) :
157 Builder->getInt(ConstLHS->getValue().byteSwap());
159 Value *NewRHS = IsBswapRHS ? IntrRHS->getOperand(0) :
160 Builder->getInt(ConstRHS->getValue().byteSwap());
162 Value *BinOp = nullptr;
163 if (Op == Instruction::And)
164 BinOp = Builder->CreateAnd(NewLHS, NewRHS);
165 else if (Op == Instruction::Or)
166 BinOp = Builder->CreateOr(NewLHS, NewRHS);
167 else //if (Op == Instruction::Xor)
168 BinOp = Builder->CreateXor(NewLHS, NewRHS);
170 Module *M = I.getParent()->getParent()->getParent();
171 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, ITy);
172 return Builder->CreateCall(F, BinOp);
175 // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where
176 // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is
177 // guaranteed to be a binary operator.
178 Instruction *InstCombiner::OptAndOp(Instruction *Op,
181 BinaryOperator &TheAnd) {
182 Value *X = Op->getOperand(0);
183 Constant *Together = nullptr;
185 Together = ConstantExpr::getAnd(AndRHS, OpRHS);
187 switch (Op->getOpcode()) {
188 case Instruction::Xor:
189 if (Op->hasOneUse()) {
190 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
191 Value *And = Builder->CreateAnd(X, AndRHS);
193 return BinaryOperator::CreateXor(And, Together);
196 case Instruction::Or:
197 if (Op->hasOneUse()){
198 if (Together != OpRHS) {
199 // (X | C1) & C2 --> (X | (C1&C2)) & C2
200 Value *Or = Builder->CreateOr(X, Together);
202 return BinaryOperator::CreateAnd(Or, AndRHS);
205 ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
206 if (TogetherCI && !TogetherCI->isZero()){
207 // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
208 // NOTE: This reduces the number of bits set in the & mask, which
209 // can expose opportunities for store narrowing.
210 Together = ConstantExpr::getXor(AndRHS, Together);
211 Value *And = Builder->CreateAnd(X, Together);
213 return BinaryOperator::CreateOr(And, OpRHS);
218 case Instruction::Add:
219 if (Op->hasOneUse()) {
220 // Adding a one to a single bit bit-field should be turned into an XOR
221 // of the bit. First thing to check is to see if this AND is with a
222 // single bit constant.
223 const APInt &AndRHSV = AndRHS->getValue();
225 // If there is only one bit set.
226 if (AndRHSV.isPowerOf2()) {
227 // Ok, at this point, we know that we are masking the result of the
228 // ADD down to exactly one bit. If the constant we are adding has
229 // no bits set below this bit, then we can eliminate the ADD.
230 const APInt& AddRHS = OpRHS->getValue();
232 // Check to see if any bits below the one bit set in AndRHSV are set.
233 if ((AddRHS & (AndRHSV-1)) == 0) {
234 // If not, the only thing that can effect the output of the AND is
235 // the bit specified by AndRHSV. If that bit is set, the effect of
236 // the XOR is to toggle the bit. If it is clear, then the ADD has
238 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
239 TheAnd.setOperand(0, X);
242 // Pull the XOR out of the AND.
243 Value *NewAnd = Builder->CreateAnd(X, AndRHS);
244 NewAnd->takeName(Op);
245 return BinaryOperator::CreateXor(NewAnd, AndRHS);
252 case Instruction::Shl: {
253 // We know that the AND will not produce any of the bits shifted in, so if
254 // the anded constant includes them, clear them now!
256 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
257 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
258 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
259 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask);
261 if (CI->getValue() == ShlMask)
262 // Masking out bits that the shift already masks.
263 return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
265 if (CI != AndRHS) { // Reducing bits set in and.
266 TheAnd.setOperand(1, CI);
271 case Instruction::LShr: {
272 // We know that the AND will not produce any of the bits shifted in, so if
273 // the anded constant includes them, clear them now! This only applies to
274 // unsigned shifts, because a signed shr may bring in set bits!
276 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
277 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
278 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
279 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask);
281 if (CI->getValue() == ShrMask)
282 // Masking out bits that the shift already masks.
283 return ReplaceInstUsesWith(TheAnd, Op);
286 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
291 case Instruction::AShr:
293 // See if this is shifting in some sign extension, then masking it out
295 if (Op->hasOneUse()) {
296 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
297 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
298 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
299 Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask);
300 if (C == AndRHS) { // Masking out bits shifted in.
301 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
302 // Make the argument unsigned.
303 Value *ShVal = Op->getOperand(0);
304 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
305 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
313 /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
314 /// (V < Lo || V >= Hi). In practice, we emit the more efficient
315 /// (V-Lo) \<u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates
316 /// whether to treat the V, Lo and HI as signed or not. IB is the location to
317 /// insert new instructions.
318 Value *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi,
319 bool isSigned, bool Inside) {
320 assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
321 ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
322 "Lo is not <= Hi in range emission code!");
325 if (Lo == Hi) // Trivially false.
326 return Builder->getFalse();
328 // V >= Min && V < Hi --> V < Hi
329 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
330 ICmpInst::Predicate pred = (isSigned ?
331 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
332 return Builder->CreateICmp(pred, V, Hi);
335 // Emit V-Lo <u Hi-Lo
336 Constant *NegLo = ConstantExpr::getNeg(Lo);
337 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
338 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
339 return Builder->CreateICmpULT(Add, UpperBound);
342 if (Lo == Hi) // Trivially true.
343 return Builder->getTrue();
345 // V < Min || V >= Hi -> V > Hi-1
346 Hi = SubOne(cast<ConstantInt>(Hi));
347 if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
348 ICmpInst::Predicate pred = (isSigned ?
349 ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
350 return Builder->CreateICmp(pred, V, Hi);
353 // Emit V-Lo >u Hi-1-Lo
354 // Note that Hi has already had one subtracted from it, above.
355 ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
356 Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
357 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
358 return Builder->CreateICmpUGT(Add, LowerBound);
361 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
362 // any number of 0s on either side. The 1s are allowed to wrap from LSB to
363 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
364 // not, since all 1s are not contiguous.
365 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
366 const APInt& V = Val->getValue();
367 uint32_t BitWidth = Val->getType()->getBitWidth();
368 if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
370 // look for the first zero bit after the run of ones
371 MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
372 // look for the first non-zero bit
373 ME = V.getActiveBits();
377 /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask,
378 /// where isSub determines whether the operator is a sub. If we can fold one of
379 /// the following xforms:
381 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
382 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
383 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
385 /// return (A +/- B).
387 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
388 ConstantInt *Mask, bool isSub,
390 Instruction *LHSI = dyn_cast<Instruction>(LHS);
391 if (!LHSI || LHSI->getNumOperands() != 2 ||
392 !isa<ConstantInt>(LHSI->getOperand(1))) return nullptr;
394 ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
396 switch (LHSI->getOpcode()) {
397 default: return nullptr;
398 case Instruction::And:
399 if (ConstantExpr::getAnd(N, Mask) == Mask) {
400 // If the AndRHS is a power of two minus one (0+1+), this is simple.
401 if ((Mask->getValue().countLeadingZeros() +
402 Mask->getValue().countPopulation()) ==
403 Mask->getValue().getBitWidth())
406 // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
407 // part, we don't need any explicit masks to take them out of A. If that
408 // is all N is, ignore it.
409 uint32_t MB = 0, ME = 0;
410 if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
411 uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
412 APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
413 if (MaskedValueIsZero(RHS, Mask, 0, &I))
418 case Instruction::Or:
419 case Instruction::Xor:
420 // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
421 if ((Mask->getValue().countLeadingZeros() +
422 Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
423 && ConstantExpr::getAnd(N, Mask)->isNullValue())
429 return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
430 return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
433 /// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C)
434 /// One of A and B is considered the mask, the other the value. This is
435 /// described as the "AMask" or "BMask" part of the enum. If the enum
436 /// contains only "Mask", then both A and B can be considered masks.
437 /// If A is the mask, then it was proven, that (A & C) == C. This
438 /// is trivial if C == A, or C == 0. If both A and C are constants, this
439 /// proof is also easy.
440 /// For the following explanations we assume that A is the mask.
441 /// The part "AllOnes" declares, that the comparison is true only
442 /// if (A & B) == A, or all bits of A are set in B.
443 /// Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes
444 /// The part "AllZeroes" declares, that the comparison is true only
445 /// if (A & B) == 0, or all bits of A are cleared in B.
446 /// Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes
447 /// The part "Mixed" declares, that (A & B) == C and C might or might not
448 /// contain any number of one bits and zero bits.
449 /// Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed
450 /// The Part "Not" means, that in above descriptions "==" should be replaced
452 /// Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes
453 /// If the mask A contains a single bit, then the following is equivalent:
454 /// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
455 /// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
456 enum MaskedICmpType {
457 FoldMskICmp_AMask_AllOnes = 1,
458 FoldMskICmp_AMask_NotAllOnes = 2,
459 FoldMskICmp_BMask_AllOnes = 4,
460 FoldMskICmp_BMask_NotAllOnes = 8,
461 FoldMskICmp_Mask_AllZeroes = 16,
462 FoldMskICmp_Mask_NotAllZeroes = 32,
463 FoldMskICmp_AMask_Mixed = 64,
464 FoldMskICmp_AMask_NotMixed = 128,
465 FoldMskICmp_BMask_Mixed = 256,
466 FoldMskICmp_BMask_NotMixed = 512
469 /// return the set of pattern classes (from MaskedICmpType)
470 /// that (icmp SCC (A & B), C) satisfies
471 static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C,
472 ICmpInst::Predicate SCC)
474 ConstantInt *ACst = dyn_cast<ConstantInt>(A);
475 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
476 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
477 bool icmp_eq = (SCC == ICmpInst::ICMP_EQ);
478 bool icmp_abit = (ACst && !ACst->isZero() &&
479 ACst->getValue().isPowerOf2());
480 bool icmp_bbit = (BCst && !BCst->isZero() &&
481 BCst->getValue().isPowerOf2());
483 if (CCst && CCst->isZero()) {
484 // if C is zero, then both A and B qualify as mask
485 result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes |
486 FoldMskICmp_Mask_AllZeroes |
487 FoldMskICmp_AMask_Mixed |
488 FoldMskICmp_BMask_Mixed)
489 : (FoldMskICmp_Mask_NotAllZeroes |
490 FoldMskICmp_Mask_NotAllZeroes |
491 FoldMskICmp_AMask_NotMixed |
492 FoldMskICmp_BMask_NotMixed));
494 result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes |
495 FoldMskICmp_AMask_NotMixed)
496 : (FoldMskICmp_AMask_AllOnes |
497 FoldMskICmp_AMask_Mixed));
499 result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes |
500 FoldMskICmp_BMask_NotMixed)
501 : (FoldMskICmp_BMask_AllOnes |
502 FoldMskICmp_BMask_Mixed));
506 result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes |
507 FoldMskICmp_AMask_Mixed)
508 : (FoldMskICmp_AMask_NotAllOnes |
509 FoldMskICmp_AMask_NotMixed));
511 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
512 FoldMskICmp_AMask_NotMixed)
513 : (FoldMskICmp_Mask_AllZeroes |
514 FoldMskICmp_AMask_Mixed));
515 } else if (ACst && CCst &&
516 ConstantExpr::getAnd(ACst, CCst) == CCst) {
517 result |= (icmp_eq ? FoldMskICmp_AMask_Mixed
518 : FoldMskICmp_AMask_NotMixed);
521 result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes |
522 FoldMskICmp_BMask_Mixed)
523 : (FoldMskICmp_BMask_NotAllOnes |
524 FoldMskICmp_BMask_NotMixed));
526 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
527 FoldMskICmp_BMask_NotMixed)
528 : (FoldMskICmp_Mask_AllZeroes |
529 FoldMskICmp_BMask_Mixed));
530 } else if (BCst && CCst &&
531 ConstantExpr::getAnd(BCst, CCst) == CCst) {
532 result |= (icmp_eq ? FoldMskICmp_BMask_Mixed
533 : FoldMskICmp_BMask_NotMixed);
538 /// Convert an analysis of a masked ICmp into its equivalent if all boolean
539 /// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
540 /// is adjacent to the corresponding normal flag (recording ==), this just
541 /// involves swapping those bits over.
542 static unsigned conjugateICmpMask(unsigned Mask) {
544 NewMask = (Mask & (FoldMskICmp_AMask_AllOnes | FoldMskICmp_BMask_AllOnes |
545 FoldMskICmp_Mask_AllZeroes | FoldMskICmp_AMask_Mixed |
546 FoldMskICmp_BMask_Mixed))
550 (Mask & (FoldMskICmp_AMask_NotAllOnes | FoldMskICmp_BMask_NotAllOnes |
551 FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_AMask_NotMixed |
552 FoldMskICmp_BMask_NotMixed))
558 /// decomposeBitTestICmp - Decompose an icmp into the form ((X & Y) pred Z)
559 /// if possible. The returned predicate is either == or !=. Returns false if
560 /// decomposition fails.
561 static bool decomposeBitTestICmp(const ICmpInst *I, ICmpInst::Predicate &Pred,
562 Value *&X, Value *&Y, Value *&Z) {
563 ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1));
567 switch (I->getPredicate()) {
570 case ICmpInst::ICMP_SLT:
571 // X < 0 is equivalent to (X & SignBit) != 0.
574 Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth()));
575 Pred = ICmpInst::ICMP_NE;
577 case ICmpInst::ICMP_SGT:
578 // X > -1 is equivalent to (X & SignBit) == 0.
579 if (!C->isAllOnesValue())
581 Y = ConstantInt::get(I->getContext(), APInt::getSignBit(C->getBitWidth()));
582 Pred = ICmpInst::ICMP_EQ;
584 case ICmpInst::ICMP_ULT:
585 // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
586 if (!C->getValue().isPowerOf2())
588 Y = ConstantInt::get(I->getContext(), -C->getValue());
589 Pred = ICmpInst::ICMP_EQ;
591 case ICmpInst::ICMP_UGT:
592 // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
593 if (!(C->getValue() + 1).isPowerOf2())
595 Y = ConstantInt::get(I->getContext(), ~C->getValue());
596 Pred = ICmpInst::ICMP_NE;
600 X = I->getOperand(0);
601 Z = ConstantInt::getNullValue(C->getType());
605 /// foldLogOpOfMaskedICmpsHelper:
606 /// handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
607 /// return the set of pattern classes (from MaskedICmpType)
608 /// that both LHS and RHS satisfy
609 static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A,
610 Value*& B, Value*& C,
611 Value*& D, Value*& E,
612 ICmpInst *LHS, ICmpInst *RHS,
613 ICmpInst::Predicate &LHSCC,
614 ICmpInst::Predicate &RHSCC) {
615 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0;
616 // vectors are not (yet?) supported
617 if (LHS->getOperand(0)->getType()->isVectorTy()) return 0;
619 // Here comes the tricky part:
620 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
621 // and L11 & L12 == L21 & L22. The same goes for RHS.
622 // Now we must find those components L** and R**, that are equal, so
623 // that we can extract the parameters A, B, C, D, and E for the canonical
625 Value *L1 = LHS->getOperand(0);
626 Value *L2 = LHS->getOperand(1);
627 Value *L11,*L12,*L21,*L22;
628 // Check whether the icmp can be decomposed into a bit test.
629 if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) {
630 L21 = L22 = L1 = nullptr;
632 // Look for ANDs in the LHS icmp.
633 if (!L1->getType()->isIntegerTy()) {
634 // You can icmp pointers, for example. They really aren't masks.
636 } else if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
637 // Any icmp can be viewed as being trivially masked; if it allows us to
638 // remove one, it's worth it.
640 L12 = Constant::getAllOnesValue(L1->getType());
643 if (!L2->getType()->isIntegerTy()) {
644 // You can icmp pointers, for example. They really aren't masks.
646 } else if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
648 L22 = Constant::getAllOnesValue(L2->getType());
652 // Bail if LHS was a icmp that can't be decomposed into an equality.
653 if (!ICmpInst::isEquality(LHSCC))
656 Value *R1 = RHS->getOperand(0);
657 Value *R2 = RHS->getOperand(1);
660 if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) {
661 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
663 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
668 E = R2; R1 = nullptr; ok = true;
669 } else if (R1->getType()->isIntegerTy()) {
670 if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
671 // As before, model no mask as a trivial mask if it'll let us do an
674 R12 = Constant::getAllOnesValue(R1->getType());
677 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
678 A = R11; D = R12; E = R2; ok = true;
679 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
680 A = R12; D = R11; E = R2; ok = true;
684 // Bail if RHS was a icmp that can't be decomposed into an equality.
685 if (!ICmpInst::isEquality(RHSCC))
688 // Look for ANDs in on the right side of the RHS icmp.
689 if (!ok && R2->getType()->isIntegerTy()) {
690 if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
692 R12 = Constant::getAllOnesValue(R2->getType());
695 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
696 A = R11; D = R12; E = R1; ok = true;
697 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
698 A = R12; D = R11; E = R1; ok = true;
708 } else if (L12 == A) {
710 } else if (L21 == A) {
712 } else if (L22 == A) {
716 unsigned left_type = getTypeOfMaskedICmp(A, B, C, LHSCC);
717 unsigned right_type = getTypeOfMaskedICmp(A, D, E, RHSCC);
718 return left_type & right_type;
720 /// foldLogOpOfMaskedICmps:
721 /// try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
722 /// into a single (icmp(A & X) ==/!= Y)
723 static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
724 llvm::InstCombiner::BuilderTy *Builder) {
725 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
726 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
727 unsigned mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS,
729 if (mask == 0) return nullptr;
730 assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) &&
731 "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
733 // In full generality:
734 // (icmp (A & B) Op C) | (icmp (A & D) Op E)
735 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
737 // If the latter can be converted into (icmp (A & X) Op Y) then the former is
738 // equivalent to (icmp (A & X) !Op Y).
740 // Therefore, we can pretend for the rest of this function that we're dealing
741 // with the conjunction, provided we flip the sense of any comparisons (both
742 // input and output).
744 // In most cases we're going to produce an EQ for the "&&" case.
745 ICmpInst::Predicate NEWCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
747 // Convert the masking analysis into its equivalent with negated
749 mask = conjugateICmpMask(mask);
752 if (mask & FoldMskICmp_Mask_AllZeroes) {
753 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
754 // -> (icmp eq (A & (B|D)), 0)
755 Value *newOr = Builder->CreateOr(B, D);
756 Value *newAnd = Builder->CreateAnd(A, newOr);
757 // we can't use C as zero, because we might actually handle
758 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
759 // with B and D, having a single bit set
760 Value *zero = Constant::getNullValue(A->getType());
761 return Builder->CreateICmp(NEWCC, newAnd, zero);
763 if (mask & FoldMskICmp_BMask_AllOnes) {
764 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
765 // -> (icmp eq (A & (B|D)), (B|D))
766 Value *newOr = Builder->CreateOr(B, D);
767 Value *newAnd = Builder->CreateAnd(A, newOr);
768 return Builder->CreateICmp(NEWCC, newAnd, newOr);
770 if (mask & FoldMskICmp_AMask_AllOnes) {
771 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
772 // -> (icmp eq (A & (B&D)), A)
773 Value *newAnd1 = Builder->CreateAnd(B, D);
774 Value *newAnd = Builder->CreateAnd(A, newAnd1);
775 return Builder->CreateICmp(NEWCC, newAnd, A);
778 // Remaining cases assume at least that B and D are constant, and depend on
779 // their actual values. This isn't strictly, necessary, just a "handle the
780 // easy cases for now" decision.
781 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
782 if (!BCst) return nullptr;
783 ConstantInt *DCst = dyn_cast<ConstantInt>(D);
784 if (!DCst) return nullptr;
786 if (mask & (FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_BMask_NotAllOnes)) {
787 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
788 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
789 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
790 // Only valid if one of the masks is a superset of the other (check "B&D" is
791 // the same as either B or D).
792 APInt NewMask = BCst->getValue() & DCst->getValue();
794 if (NewMask == BCst->getValue())
796 else if (NewMask == DCst->getValue())
799 if (mask & FoldMskICmp_AMask_NotAllOnes) {
800 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
801 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
802 // Only valid if one of the masks is a superset of the other (check "B|D" is
803 // the same as either B or D).
804 APInt NewMask = BCst->getValue() | DCst->getValue();
806 if (NewMask == BCst->getValue())
808 else if (NewMask == DCst->getValue())
811 if (mask & FoldMskICmp_BMask_Mixed) {
812 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
813 // We already know that B & C == C && D & E == E.
814 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
815 // C and E, which are shared by both the mask B and the mask D, don't
816 // contradict, then we can transform to
817 // -> (icmp eq (A & (B|D)), (C|E))
818 // Currently, we only handle the case of B, C, D, and E being constant.
819 // we can't simply use C and E, because we might actually handle
820 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
821 // with B and D, having a single bit set
822 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
823 if (!CCst) return nullptr;
824 ConstantInt *ECst = dyn_cast<ConstantInt>(E);
825 if (!ECst) return nullptr;
827 CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst));
829 ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst));
830 // if there is a conflict we should actually return a false for the
832 if (((BCst->getValue() & DCst->getValue()) &
833 (CCst->getValue() ^ ECst->getValue())) != 0)
834 return ConstantInt::get(LHS->getType(), !IsAnd);
835 Value *newOr1 = Builder->CreateOr(B, D);
836 Value *newOr2 = ConstantExpr::getOr(CCst, ECst);
837 Value *newAnd = Builder->CreateAnd(A, newOr1);
838 return Builder->CreateICmp(NEWCC, newAnd, newOr2);
843 /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
844 /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
845 /// If \p Inverted is true then the check is for the inverted range, e.g.
846 /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
847 Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
849 // Check the lower range comparison, e.g. x >= 0
850 // InstCombine already ensured that if there is a constant it's on the RHS.
851 ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
855 ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
856 Cmp0->getPredicate());
858 // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
859 if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
860 (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
863 ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
864 Cmp1->getPredicate());
866 Value *Input = Cmp0->getOperand(0);
868 if (Cmp1->getOperand(0) == Input) {
869 // For the upper range compare we have: icmp x, n
870 RangeEnd = Cmp1->getOperand(1);
871 } else if (Cmp1->getOperand(1) == Input) {
872 // For the upper range compare we have: icmp n, x
873 RangeEnd = Cmp1->getOperand(0);
874 Pred1 = ICmpInst::getSwappedPredicate(Pred1);
879 // Check the upper range comparison, e.g. x < n
880 ICmpInst::Predicate NewPred;
882 case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
883 case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
884 default: return nullptr;
887 // This simplification is only valid if the upper range is not negative.
888 bool IsNegative, IsNotNegative;
889 ComputeSignBit(RangeEnd, IsNotNegative, IsNegative, /*Depth=*/0, Cmp1);
894 NewPred = ICmpInst::getInversePredicate(NewPred);
896 return Builder->CreateICmp(NewPred, Input, RangeEnd);
899 /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
900 Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
901 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
903 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
904 if (PredicatesFoldable(LHSCC, RHSCC)) {
905 if (LHS->getOperand(0) == RHS->getOperand(1) &&
906 LHS->getOperand(1) == RHS->getOperand(0))
908 if (LHS->getOperand(0) == RHS->getOperand(0) &&
909 LHS->getOperand(1) == RHS->getOperand(1)) {
910 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
911 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
912 bool isSigned = LHS->isSigned() || RHS->isSigned();
913 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
917 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
918 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
921 // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
922 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false))
925 // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
926 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
929 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
930 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
931 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
932 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
933 if (!LHSCst || !RHSCst) return nullptr;
935 if (LHSCst == RHSCst && LHSCC == RHSCC) {
936 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
937 // where C is a power of 2
938 if (LHSCC == ICmpInst::ICMP_ULT &&
939 LHSCst->getValue().isPowerOf2()) {
940 Value *NewOr = Builder->CreateOr(Val, Val2);
941 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
944 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
945 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
946 Value *NewOr = Builder->CreateOr(Val, Val2);
947 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
951 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
952 // where CMAX is the all ones value for the truncated type,
953 // iff the lower bits of C2 and CA are zero.
954 if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
955 LHS->hasOneUse() && RHS->hasOneUse()) {
957 ConstantInt *AndCst, *SmallCst = nullptr, *BigCst = nullptr;
959 // (trunc x) == C1 & (and x, CA) == C2
960 // (and x, CA) == C2 & (trunc x) == C1
961 if (match(Val2, m_Trunc(m_Value(V))) &&
962 match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
965 } else if (match(Val, m_Trunc(m_Value(V))) &&
966 match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
971 if (SmallCst && BigCst) {
972 unsigned BigBitSize = BigCst->getType()->getBitWidth();
973 unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
975 // Check that the low bits are zero.
976 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
977 if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
978 Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
979 APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
980 Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
981 return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
986 // From here on, we only handle:
987 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
988 if (Val != Val2) return nullptr;
990 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
991 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
992 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
993 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
994 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
997 // Make a constant range that's the intersection of the two icmp ranges.
998 // If the intersection is empty, we know that the result is false.
999 ConstantRange LHSRange =
1000 ConstantRange::makeICmpRegion(LHSCC, LHSCst->getValue());
1001 ConstantRange RHSRange =
1002 ConstantRange::makeICmpRegion(RHSCC, RHSCst->getValue());
1004 if (LHSRange.intersectWith(RHSRange).isEmptySet())
1005 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
1007 // We can't fold (ugt x, C) & (sgt x, C2).
1008 if (!PredicatesFoldable(LHSCC, RHSCC))
1011 // Ensure that the larger constant is on the RHS.
1013 if (CmpInst::isSigned(LHSCC) ||
1014 (ICmpInst::isEquality(LHSCC) &&
1015 CmpInst::isSigned(RHSCC)))
1016 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1018 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
1021 std::swap(LHS, RHS);
1022 std::swap(LHSCst, RHSCst);
1023 std::swap(LHSCC, RHSCC);
1026 // At this point, we know we have two icmp instructions
1027 // comparing a value against two constants and and'ing the result
1028 // together. Because of the above check, we know that we only have
1029 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
1030 // (from the icmp folding check above), that the two constants
1031 // are not equal and that the larger constant is on the RHS
1032 assert(LHSCst != RHSCst && "Compares not folded above?");
1035 default: llvm_unreachable("Unknown integer condition code!");
1036 case ICmpInst::ICMP_EQ:
1038 default: llvm_unreachable("Unknown integer condition code!");
1039 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
1040 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
1041 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
1044 case ICmpInst::ICMP_NE:
1046 default: llvm_unreachable("Unknown integer condition code!");
1047 case ICmpInst::ICMP_ULT:
1048 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
1049 return Builder->CreateICmpULT(Val, LHSCst);
1050 if (LHSCst->isNullValue()) // (X != 0 & X u< 14) -> X-1 u< 13
1051 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
1052 break; // (X != 13 & X u< 15) -> no change
1053 case ICmpInst::ICMP_SLT:
1054 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
1055 return Builder->CreateICmpSLT(Val, LHSCst);
1056 break; // (X != 13 & X s< 15) -> no change
1057 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
1058 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
1059 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
1061 case ICmpInst::ICMP_NE:
1062 // Special case to get the ordering right when the values wrap around
1064 if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue())
1065 std::swap(LHSCst, RHSCst);
1066 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
1067 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1068 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1069 return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1),
1070 Val->getName()+".cmp");
1072 break; // (X != 13 & X != 15) -> no change
1075 case ICmpInst::ICMP_ULT:
1077 default: llvm_unreachable("Unknown integer condition code!");
1078 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
1079 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
1080 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
1081 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
1083 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
1084 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
1086 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
1090 case ICmpInst::ICMP_SLT:
1092 default: llvm_unreachable("Unknown integer condition code!");
1093 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
1095 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
1096 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
1098 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
1102 case ICmpInst::ICMP_UGT:
1104 default: llvm_unreachable("Unknown integer condition code!");
1105 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15
1106 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
1108 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
1110 case ICmpInst::ICMP_NE:
1111 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
1112 return Builder->CreateICmp(LHSCC, Val, RHSCst);
1113 break; // (X u> 13 & X != 15) -> no change
1114 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
1115 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
1116 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
1120 case ICmpInst::ICMP_SGT:
1122 default: llvm_unreachable("Unknown integer condition code!");
1123 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
1124 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
1126 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
1128 case ICmpInst::ICMP_NE:
1129 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
1130 return Builder->CreateICmp(LHSCC, Val, RHSCst);
1131 break; // (X s> 13 & X != 15) -> no change
1132 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
1133 return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true);
1134 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
1143 /// FoldAndOfFCmps - Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of
1144 /// instcombine, this returns a Value which should already be inserted into the
1146 Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
1147 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
1148 RHS->getPredicate() == FCmpInst::FCMP_ORD) {
1149 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
1152 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
1153 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1154 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1155 // If either of the constants are nans, then the whole thing returns
1157 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1158 return Builder->getFalse();
1159 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1162 // Handle vector zeros. This occurs because the canonical form of
1163 // "fcmp ord x,x" is "fcmp ord x, 0".
1164 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1165 isa<ConstantAggregateZero>(RHS->getOperand(1)))
1166 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1170 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1171 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1172 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1175 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1176 // Swap RHS operands to match LHS.
1177 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1178 std::swap(Op1LHS, Op1RHS);
1181 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1182 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1184 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
1185 if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
1186 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
1187 if (Op0CC == FCmpInst::FCMP_TRUE)
1189 if (Op1CC == FCmpInst::FCMP_TRUE)
1194 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1195 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
1196 // uno && ord -> false
1197 if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered)
1198 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
1200 std::swap(LHS, RHS);
1201 std::swap(Op0Pred, Op1Pred);
1202 std::swap(Op0Ordered, Op1Ordered);
1205 // uno && ueq -> uno && (uno || eq) -> uno
1206 // ord && olt -> ord && (ord && lt) -> olt
1207 if (!Op0Ordered && (Op0Ordered == Op1Ordered))
1209 if (Op0Ordered && (Op0Ordered == Op1Ordered))
1212 // uno && oeq -> uno && (ord && eq) -> false
1214 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
1215 // ord && ueq -> ord && (uno || eq) -> oeq
1216 return getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS, Builder);
1223 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
1224 bool Changed = SimplifyAssociativeOrCommutative(I);
1225 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1227 if (Value *V = SimplifyVectorOp(I))
1228 return ReplaceInstUsesWith(I, V);
1230 if (Value *V = SimplifyAndInst(Op0, Op1, DL, TLI, DT, AC))
1231 return ReplaceInstUsesWith(I, V);
1233 // (A|B)&(A|C) -> A|(B&C) etc
1234 if (Value *V = SimplifyUsingDistributiveLaws(I))
1235 return ReplaceInstUsesWith(I, V);
1237 // See if we can simplify any instructions used by the instruction whose sole
1238 // purpose is to compute bits we don't care about.
1239 if (SimplifyDemandedInstructionBits(I))
1242 if (Value *V = SimplifyBSwap(I))
1243 return ReplaceInstUsesWith(I, V);
1245 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1246 const APInt &AndRHSMask = AndRHS->getValue();
1248 // Optimize a variety of ((val OP C1) & C2) combinations...
1249 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1250 Value *Op0LHS = Op0I->getOperand(0);
1251 Value *Op0RHS = Op0I->getOperand(1);
1252 switch (Op0I->getOpcode()) {
1254 case Instruction::Xor:
1255 case Instruction::Or: {
1256 // If the mask is only needed on one incoming arm, push it up.
1257 if (!Op0I->hasOneUse()) break;
1259 APInt NotAndRHS(~AndRHSMask);
1260 if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) {
1261 // Not masking anything out for the LHS, move to RHS.
1262 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1263 Op0RHS->getName()+".masked");
1264 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1266 if (!isa<Constant>(Op0RHS) &&
1267 MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) {
1268 // Not masking anything out for the RHS, move to LHS.
1269 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1270 Op0LHS->getName()+".masked");
1271 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1276 case Instruction::Add:
1277 // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
1278 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1279 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1280 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
1281 return BinaryOperator::CreateAnd(V, AndRHS);
1282 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
1283 return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes
1286 case Instruction::Sub:
1287 // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
1288 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1289 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1290 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
1291 return BinaryOperator::CreateAnd(V, AndRHS);
1293 // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
1294 // has 1's for all bits that the subtraction with A might affect.
1295 if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
1296 uint32_t BitWidth = AndRHSMask.getBitWidth();
1297 uint32_t Zeros = AndRHSMask.countLeadingZeros();
1298 APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
1300 if (MaskedValueIsZero(Op0LHS, Mask, 0, &I)) {
1301 Value *NewNeg = Builder->CreateNeg(Op0RHS);
1302 return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1307 case Instruction::Shl:
1308 case Instruction::LShr:
1309 // (1 << x) & 1 --> zext(x == 0)
1310 // (1 >> x) & 1 --> zext(x == 0)
1311 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1313 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1314 return new ZExtInst(NewICmp, I.getType());
1319 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1320 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1324 // If this is an integer truncation, and if the source is an 'and' with
1325 // immediate, transform it. This frequently occurs for bitfield accesses.
1327 Value *X = nullptr; ConstantInt *YC = nullptr;
1328 if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1329 // Change: and (trunc (and X, YC) to T), C2
1330 // into : and (trunc X to T), trunc(YC) & C2
1331 // This will fold the two constants together, which may allow
1332 // other simplifications.
1333 Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1334 Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1335 C3 = ConstantExpr::getAnd(C3, AndRHS);
1336 return BinaryOperator::CreateAnd(NewCast, C3);
1340 // Try to fold constant and into select arguments.
1341 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1342 if (Instruction *R = FoldOpIntoSelect(I, SI))
1344 if (isa<PHINode>(Op0))
1345 if (Instruction *NV = FoldOpIntoPhi(I))
1350 // (~A & ~B) == (~(A | B)) - De Morgan's Law
1351 if (Value *Op0NotVal = dyn_castNotVal(Op0))
1352 if (Value *Op1NotVal = dyn_castNotVal(Op1))
1353 if (Op0->hasOneUse() && Op1->hasOneUse()) {
1354 Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
1355 I.getName()+".demorgan");
1356 return BinaryOperator::CreateNot(Or);
1360 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
1361 // (A|B) & ~(A&B) -> A^B
1362 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1363 match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1364 ((A == C && B == D) || (A == D && B == C)))
1365 return BinaryOperator::CreateXor(A, B);
1367 // ~(A&B) & (A|B) -> A^B
1368 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1369 match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1370 ((A == C && B == D) || (A == D && B == C)))
1371 return BinaryOperator::CreateXor(A, B);
1373 // A&(A^B) => A & ~B
1375 Value *tmpOp0 = Op0;
1376 Value *tmpOp1 = Op1;
1377 if (Op0->hasOneUse() &&
1378 match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
1379 if (A == Op1 || B == Op1 ) {
1386 if (tmpOp1->hasOneUse() &&
1387 match(tmpOp1, m_Xor(m_Value(A), m_Value(B)))) {
1391 // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if
1392 // A is originally -1 (or a vector of -1 and undefs), then we enter
1393 // an endless loop. By checking that A is non-constant we ensure that
1394 // we will never get to the loop.
1395 if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
1396 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
1400 // (A&((~A)|B)) -> A&B
1401 if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1402 match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1403 return BinaryOperator::CreateAnd(A, Op1);
1404 if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1405 match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1406 return BinaryOperator::CreateAnd(A, Op0);
1408 // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
1409 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
1410 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
1411 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
1412 return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C));
1414 // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
1415 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
1416 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
1417 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
1418 return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C));
1420 // (A | B) & ((~A) ^ B) -> (A & B)
1421 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1422 match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B))))
1423 return BinaryOperator::CreateAnd(A, B);
1425 // ((~A) ^ B) & (A | B) -> (A & B)
1426 if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
1427 match(Op1, m_Or(m_Specific(A), m_Specific(B))))
1428 return BinaryOperator::CreateAnd(A, B);
1432 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
1433 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
1435 if (Value *Res = FoldAndOfICmps(LHS, RHS))
1436 return ReplaceInstUsesWith(I, Res);
1438 // TODO: Make this recursive; it's a little tricky because an arbitrary
1439 // number of 'and' instructions might have to be created.
1441 if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1442 if (auto *Cmp = dyn_cast<ICmpInst>(X))
1443 if (Value *Res = FoldAndOfICmps(LHS, Cmp))
1444 return ReplaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
1445 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1446 if (Value *Res = FoldAndOfICmps(LHS, Cmp))
1447 return ReplaceInstUsesWith(I, Builder->CreateAnd(Res, X));
1449 if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1450 if (auto *Cmp = dyn_cast<ICmpInst>(X))
1451 if (Value *Res = FoldAndOfICmps(Cmp, RHS))
1452 return ReplaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
1453 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1454 if (Value *Res = FoldAndOfICmps(Cmp, RHS))
1455 return ReplaceInstUsesWith(I, Builder->CreateAnd(Res, X));
1459 // If and'ing two fcmp, try combine them into one.
1460 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1461 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
1462 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1463 return ReplaceInstUsesWith(I, Res);
1466 // fold (and (cast A), (cast B)) -> (cast (and A, B))
1467 if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
1468 if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
1469 Type *SrcTy = Op0C->getOperand(0)->getType();
1470 if (Op0C->getOpcode() == Op1C->getOpcode() && // same cast kind ?
1471 SrcTy == Op1C->getOperand(0)->getType() &&
1472 SrcTy->isIntOrIntVectorTy()) {
1473 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
1475 // Only do this if the casts both really cause code to be generated.
1476 if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
1477 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
1478 Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.getName());
1479 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
1482 // If this is and(cast(icmp), cast(icmp)), try to fold this even if the
1483 // cast is otherwise not optimizable. This happens for vector sexts.
1484 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
1485 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
1486 if (Value *Res = FoldAndOfICmps(LHS, RHS))
1487 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
1489 // If this is and(cast(fcmp), cast(fcmp)), try to fold this even if the
1490 // cast is otherwise not optimizable. This happens for vector sexts.
1491 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
1492 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
1493 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1494 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
1500 bool OpsSwapped = false;
1501 // Canonicalize SExt or Not to the LHS
1502 if (match(Op1, m_SExt(m_Value())) ||
1503 match(Op1, m_Not(m_Value()))) {
1504 std::swap(Op0, Op1);
1508 // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1509 if (match(Op0, m_SExt(m_Value(X))) &&
1510 X->getType()->getScalarType()->isIntegerTy(1)) {
1511 Value *Zero = Constant::getNullValue(Op1->getType());
1512 return SelectInst::Create(X, Op1, Zero);
1515 // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1516 if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1517 X->getType()->getScalarType()->isIntegerTy(1)) {
1518 Value *Zero = Constant::getNullValue(Op0->getType());
1519 return SelectInst::Create(X, Zero, Op1);
1523 std::swap(Op0, Op1);
1526 return Changed ? &I : nullptr;
1529 /// CollectBSwapParts - Analyze the specified subexpression and see if it is
1530 /// capable of providing pieces of a bswap. The subexpression provides pieces
1531 /// of a bswap if it is proven that each of the non-zero bytes in the output of
1532 /// the expression came from the corresponding "byte swapped" byte in some other
1533 /// value. For example, if the current subexpression is "(shl i32 %X, 24)" then
1534 /// we know that the expression deposits the low byte of %X into the high byte
1535 /// of the bswap result and that all other bytes are zero. This expression is
1536 /// accepted, the high byte of ByteValues is set to X to indicate a correct
1539 /// This function returns true if the match was unsuccessful and false if so.
1540 /// On entry to the function the "OverallLeftShift" is a signed integer value
1541 /// indicating the number of bytes that the subexpression is later shifted. For
1542 /// example, if the expression is later right shifted by 16 bits, the
1543 /// OverallLeftShift value would be -2 on entry. This is used to specify which
1544 /// byte of ByteValues is actually being set.
1546 /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding
1547 /// byte is masked to zero by a user. For example, in (X & 255), X will be
1548 /// processed with a bytemask of 1. Because bytemask is 32-bits, this limits
1549 /// this function to working on up to 32-byte (256 bit) values. ByteMask is
1550 /// always in the local (OverallLeftShift) coordinate space.
1552 static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask,
1553 SmallVectorImpl<Value *> &ByteValues) {
1554 if (Instruction *I = dyn_cast<Instruction>(V)) {
1555 // If this is an or instruction, it may be an inner node of the bswap.
1556 if (I->getOpcode() == Instruction::Or) {
1557 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1559 CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask,
1563 // If this is a logical shift by a constant multiple of 8, recurse with
1564 // OverallLeftShift and ByteMask adjusted.
1565 if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) {
1567 cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U);
1568 // Ensure the shift amount is defined and of a byte value.
1569 if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size()))
1572 unsigned ByteShift = ShAmt >> 3;
1573 if (I->getOpcode() == Instruction::Shl) {
1574 // X << 2 -> collect(X, +2)
1575 OverallLeftShift += ByteShift;
1576 ByteMask >>= ByteShift;
1578 // X >>u 2 -> collect(X, -2)
1579 OverallLeftShift -= ByteShift;
1580 ByteMask <<= ByteShift;
1581 ByteMask &= (~0U >> (32-ByteValues.size()));
1584 if (OverallLeftShift >= (int)ByteValues.size()) return true;
1585 if (OverallLeftShift <= -(int)ByteValues.size()) return true;
1587 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1591 // If this is a logical 'and' with a mask that clears bytes, clear the
1592 // corresponding bytes in ByteMask.
1593 if (I->getOpcode() == Instruction::And &&
1594 isa<ConstantInt>(I->getOperand(1))) {
1595 // Scan every byte of the and mask, seeing if the byte is either 0 or 255.
1596 unsigned NumBytes = ByteValues.size();
1597 APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255);
1598 const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue();
1600 for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) {
1601 // If this byte is masked out by a later operation, we don't care what
1603 if ((ByteMask & (1 << i)) == 0)
1606 // If the AndMask is all zeros for this byte, clear the bit.
1607 APInt MaskB = AndMask & Byte;
1609 ByteMask &= ~(1U << i);
1613 // If the AndMask is not all ones for this byte, it's not a bytezap.
1617 // Otherwise, this byte is kept.
1620 return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask,
1625 // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be
1626 // the input value to the bswap. Some observations: 1) if more than one byte
1627 // is demanded from this input, then it could not be successfully assembled
1628 // into a byteswap. At least one of the two bytes would not be aligned with
1629 // their ultimate destination.
1630 if (!isPowerOf2_32(ByteMask)) return true;
1631 unsigned InputByteNo = countTrailingZeros(ByteMask);
1633 // 2) The input and ultimate destinations must line up: if byte 3 of an i32
1634 // is demanded, it needs to go into byte 0 of the result. This means that the
1635 // byte needs to be shifted until it lands in the right byte bucket. The
1636 // shift amount depends on the position: if the byte is coming from the high
1637 // part of the value (e.g. byte 3) then it must be shifted right. If from the
1638 // low part, it must be shifted left.
1639 unsigned DestByteNo = InputByteNo + OverallLeftShift;
1640 if (ByteValues.size()-1-DestByteNo != InputByteNo)
1643 // If the destination byte value is already defined, the values are or'd
1644 // together, which isn't a bswap (unless it's an or of the same bits).
1645 if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V)
1647 ByteValues[DestByteNo] = V;
1651 /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom.
1652 /// If so, insert the new bswap intrinsic and return it.
1653 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
1654 IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
1655 if (!ITy || ITy->getBitWidth() % 16 ||
1656 // ByteMask only allows up to 32-byte values.
1657 ITy->getBitWidth() > 32*8)
1658 return nullptr; // Can only bswap pairs of bytes. Can't do vectors.
1660 /// ByteValues - For each byte of the result, we keep track of which value
1661 /// defines each byte.
1662 SmallVector<Value*, 8> ByteValues;
1663 ByteValues.resize(ITy->getBitWidth()/8);
1665 // Try to find all the pieces corresponding to the bswap.
1666 uint32_t ByteMask = ~0U >> (32-ByteValues.size());
1667 if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
1670 // Check to see if all of the bytes come from the same value.
1671 Value *V = ByteValues[0];
1672 if (!V) return nullptr; // Didn't find a byte? Must be zero.
1674 // Check to make sure that all of the bytes come from the same value.
1675 for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
1676 if (ByteValues[i] != V)
1678 Module *M = I.getParent()->getParent()->getParent();
1679 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, ITy);
1680 return CallInst::Create(F, V);
1683 /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check
1684 /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then
1685 /// we can simplify this expression to "cond ? C : D or B".
1686 static Instruction *MatchSelectFromAndOr(Value *A, Value *B,
1687 Value *C, Value *D) {
1688 // If A is not a select of -1/0, this cannot match.
1689 Value *Cond = nullptr;
1690 if (!match(A, m_SExt(m_Value(Cond))) ||
1691 !Cond->getType()->isIntegerTy(1))
1694 // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
1695 if (match(D, m_Not(m_SExt(m_Specific(Cond)))))
1696 return SelectInst::Create(Cond, C, B);
1697 if (match(D, m_SExt(m_Not(m_Specific(Cond)))))
1698 return SelectInst::Create(Cond, C, B);
1700 // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
1701 if (match(B, m_Not(m_SExt(m_Specific(Cond)))))
1702 return SelectInst::Create(Cond, C, D);
1703 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
1704 return SelectInst::Create(Cond, C, D);
1708 /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
1709 Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
1710 Instruction *CxtI) {
1711 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1713 // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
1714 // if K1 and K2 are a one-bit mask.
1715 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1716 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1718 if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero() &&
1719 RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1721 BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0));
1722 BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0));
1723 if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
1724 LAnd->getOpcode() == Instruction::And &&
1725 RAnd->getOpcode() == Instruction::And) {
1727 Value *Mask = nullptr;
1728 Value *Masked = nullptr;
1729 if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
1730 isKnownToBeAPowerOfTwo(LAnd->getOperand(1), false, 0, AC, CxtI, DT) &&
1731 isKnownToBeAPowerOfTwo(RAnd->getOperand(1), false, 0, AC, CxtI, DT)) {
1732 Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
1733 Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
1734 } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
1735 isKnownToBeAPowerOfTwo(LAnd->getOperand(0), false, 0, AC, CxtI,
1737 isKnownToBeAPowerOfTwo(RAnd->getOperand(0), false, 0, AC, CxtI,
1739 Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
1740 Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
1744 return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
1748 // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3)
1749 // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3)
1750 // The original condition actually refers to the following two ranges:
1751 // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3]
1752 // We can fold these two ranges if:
1753 // 1) C1 and C2 is unsigned greater than C3.
1754 // 2) The two ranges are separated.
1755 // 3) C1 ^ C2 is one-bit mask.
1756 // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask.
1757 // This implies all values in the two ranges differ by exactly one bit.
1759 if ((LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_ULE) &&
1760 LHSCC == RHSCC && LHSCst && RHSCst && LHS->hasOneUse() &&
1761 RHS->hasOneUse() && LHSCst->getType() == RHSCst->getType() &&
1762 LHSCst->getValue() == (RHSCst->getValue())) {
1764 Value *LAdd = LHS->getOperand(0);
1765 Value *RAdd = RHS->getOperand(0);
1767 Value *LAddOpnd, *RAddOpnd;
1768 ConstantInt *LAddCst, *RAddCst;
1769 if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddCst))) &&
1770 match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddCst))) &&
1771 LAddCst->getValue().ugt(LHSCst->getValue()) &&
1772 RAddCst->getValue().ugt(LHSCst->getValue())) {
1774 APInt DiffCst = LAddCst->getValue() ^ RAddCst->getValue();
1775 if (LAddOpnd == RAddOpnd && DiffCst.isPowerOf2()) {
1776 ConstantInt *MaxAddCst = nullptr;
1777 if (LAddCst->getValue().ult(RAddCst->getValue()))
1778 MaxAddCst = RAddCst;
1780 MaxAddCst = LAddCst;
1782 APInt RRangeLow = -RAddCst->getValue();
1783 APInt RRangeHigh = RRangeLow + LHSCst->getValue();
1784 APInt LRangeLow = -LAddCst->getValue();
1785 APInt LRangeHigh = LRangeLow + LHSCst->getValue();
1786 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1787 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1788 APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow
1789 : RRangeLow - LRangeLow;
1791 if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff &&
1792 RangeDiff.ugt(LHSCst->getValue())) {
1793 Value *MaskCst = ConstantInt::get(LAddCst->getType(), ~DiffCst);
1795 Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskCst);
1796 Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddCst);
1797 return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSCst));
1803 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1804 if (PredicatesFoldable(LHSCC, RHSCC)) {
1805 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1806 LHS->getOperand(1) == RHS->getOperand(0))
1807 LHS->swapOperands();
1808 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1809 LHS->getOperand(1) == RHS->getOperand(1)) {
1810 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1811 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1812 bool isSigned = LHS->isSigned() || RHS->isSigned();
1813 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
1817 // handle (roughly):
1818 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
1819 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
1822 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1823 if (LHS->hasOneUse() || RHS->hasOneUse()) {
1824 // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1825 // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
1826 Value *A = nullptr, *B = nullptr;
1827 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) {
1829 if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1))
1831 else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2)
1832 A = RHS->getOperand(1);
1834 // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1835 // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
1836 else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1838 if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1))
1840 else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val)
1841 A = LHS->getOperand(1);
1844 return Builder->CreateICmp(
1846 Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1849 // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
1850 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true))
1853 // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
1854 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
1857 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
1858 if (!LHSCst || !RHSCst) return nullptr;
1860 if (LHSCst == RHSCst && LHSCC == RHSCC) {
1861 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1862 if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1863 Value *NewOr = Builder->CreateOr(Val, Val2);
1864 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1868 // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
1869 // iff C2 + CA == C1.
1870 if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
1871 ConstantInt *AddCst;
1872 if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1873 if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
1874 return Builder->CreateICmpULE(Val, LHSCst);
1877 // From here on, we only handle:
1878 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1879 if (Val != Val2) return nullptr;
1881 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1882 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1883 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1884 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1885 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1888 // We can't fold (ugt x, C) | (sgt x, C2).
1889 if (!PredicatesFoldable(LHSCC, RHSCC))
1892 // Ensure that the larger constant is on the RHS.
1894 if (CmpInst::isSigned(LHSCC) ||
1895 (ICmpInst::isEquality(LHSCC) &&
1896 CmpInst::isSigned(RHSCC)))
1897 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1899 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
1902 std::swap(LHS, RHS);
1903 std::swap(LHSCst, RHSCst);
1904 std::swap(LHSCC, RHSCC);
1907 // At this point, we know we have two icmp instructions
1908 // comparing a value against two constants and or'ing the result
1909 // together. Because of the above check, we know that we only have
1910 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1911 // icmp folding check above), that the two constants are not
1913 assert(LHSCst != RHSCst && "Compares not folded above?");
1916 default: llvm_unreachable("Unknown integer condition code!");
1917 case ICmpInst::ICMP_EQ:
1919 default: llvm_unreachable("Unknown integer condition code!");
1920 case ICmpInst::ICMP_EQ:
1921 if (LHS->getOperand(0) == RHS->getOperand(0)) {
1922 // if LHSCst and RHSCst differ only by one bit:
1923 // (A == C1 || A == C2) -> (A & ~(C1 ^ C2)) == C1
1924 assert(LHSCst->getValue().ule(LHSCst->getValue()));
1926 APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1927 if (Xor.isPowerOf2()) {
1928 Value *NegCst = Builder->getInt(~Xor);
1929 Value *And = Builder->CreateAnd(LHS->getOperand(0), NegCst);
1930 return Builder->CreateICmp(ICmpInst::ICMP_EQ, And, LHSCst);
1934 if (LHSCst == SubOne(RHSCst)) {
1935 // (X == 13 | X == 14) -> X-13 <u 2
1936 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1937 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1938 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1939 return Builder->CreateICmpULT(Add, AddCST);
1942 break; // (X == 13 | X == 15) -> no change
1943 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1944 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
1946 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
1947 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
1948 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
1952 case ICmpInst::ICMP_NE:
1954 default: llvm_unreachable("Unknown integer condition code!");
1955 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
1956 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
1957 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
1959 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
1960 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
1961 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
1962 return Builder->getTrue();
1964 case ICmpInst::ICMP_ULT:
1966 default: llvm_unreachable("Unknown integer condition code!");
1967 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
1969 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
1970 // If RHSCst is [us]MAXINT, it is always false. Not handling
1971 // this can cause overflow.
1972 if (RHSCst->isMaxValue(false))
1974 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false);
1975 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
1977 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
1978 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
1980 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
1984 case ICmpInst::ICMP_SLT:
1986 default: llvm_unreachable("Unknown integer condition code!");
1987 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
1989 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
1990 // If RHSCst is [us]MAXINT, it is always false. Not handling
1991 // this can cause overflow.
1992 if (RHSCst->isMaxValue(true))
1994 return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false);
1995 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
1997 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
1998 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
2000 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
2004 case ICmpInst::ICMP_UGT:
2006 default: llvm_unreachable("Unknown integer condition code!");
2007 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
2008 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
2010 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
2012 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
2013 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
2014 return Builder->getTrue();
2015 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
2019 case ICmpInst::ICMP_SGT:
2021 default: llvm_unreachable("Unknown integer condition code!");
2022 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
2023 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
2025 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
2027 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
2028 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
2029 return Builder->getTrue();
2030 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
2038 /// FoldOrOfFCmps - Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of
2039 /// instcombine, this returns a Value which should already be inserted into the
2041 Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
2042 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
2043 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
2044 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
2045 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
2046 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
2047 // If either of the constants are nans, then the whole thing returns
2049 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
2050 return Builder->getTrue();
2052 // Otherwise, no need to compare the two constants, compare the
2054 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
2057 // Handle vector zeros. This occurs because the canonical form of
2058 // "fcmp uno x,x" is "fcmp uno x, 0".
2059 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
2060 isa<ConstantAggregateZero>(RHS->getOperand(1)))
2061 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
2066 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
2067 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
2068 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
2070 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
2071 // Swap RHS operands to match LHS.
2072 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
2073 std::swap(Op1LHS, Op1RHS);
2075 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
2076 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
2078 return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
2079 if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
2080 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
2081 if (Op0CC == FCmpInst::FCMP_FALSE)
2083 if (Op1CC == FCmpInst::FCMP_FALSE)
2087 unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
2088 unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
2089 if (Op0Ordered == Op1Ordered) {
2090 // If both are ordered or unordered, return a new fcmp with
2091 // or'ed predicates.
2092 return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder);
2098 /// FoldOrWithConstants - This helper function folds:
2100 /// ((A | B) & C1) | (B & C2)
2106 /// when the XOR of the two constants is "all ones" (-1).
2107 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
2108 Value *A, Value *B, Value *C) {
2109 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
2110 if (!CI1) return nullptr;
2112 Value *V1 = nullptr;
2113 ConstantInt *CI2 = nullptr;
2114 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr;
2116 APInt Xor = CI1->getValue() ^ CI2->getValue();
2117 if (!Xor.isAllOnesValue()) return nullptr;
2119 if (V1 == A || V1 == B) {
2120 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
2121 return BinaryOperator::CreateOr(NewOp, V1);
2127 /// \brief This helper function folds:
2129 /// ((A | B) & C1) ^ (B & C2)
2135 /// when the XOR of the two constants is "all ones" (-1).
2136 Instruction *InstCombiner::FoldXorWithConstants(BinaryOperator &I, Value *Op,
2137 Value *A, Value *B, Value *C) {
2138 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
2142 Value *V1 = nullptr;
2143 ConstantInt *CI2 = nullptr;
2144 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2))))
2147 APInt Xor = CI1->getValue() ^ CI2->getValue();
2148 if (!Xor.isAllOnesValue())
2151 if (V1 == A || V1 == B) {
2152 Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1);
2153 return BinaryOperator::CreateXor(NewOp, V1);
2159 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
2160 bool Changed = SimplifyAssociativeOrCommutative(I);
2161 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2163 if (Value *V = SimplifyVectorOp(I))
2164 return ReplaceInstUsesWith(I, V);
2166 if (Value *V = SimplifyOrInst(Op0, Op1, DL, TLI, DT, AC))
2167 return ReplaceInstUsesWith(I, V);
2169 // (A&B)|(A&C) -> A&(B|C) etc
2170 if (Value *V = SimplifyUsingDistributiveLaws(I))
2171 return ReplaceInstUsesWith(I, V);
2173 // See if we can simplify any instructions used by the instruction whose sole
2174 // purpose is to compute bits we don't care about.
2175 if (SimplifyDemandedInstructionBits(I))
2178 if (Value *V = SimplifyBSwap(I))
2179 return ReplaceInstUsesWith(I, V);
2181 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2182 ConstantInt *C1 = nullptr; Value *X = nullptr;
2183 // (X & C1) | C2 --> (X | C2) & (C1|C2)
2184 // iff (C1 & C2) == 0.
2185 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
2186 (RHS->getValue() & C1->getValue()) != 0 &&
2188 Value *Or = Builder->CreateOr(X, RHS);
2190 return BinaryOperator::CreateAnd(Or,
2191 Builder->getInt(RHS->getValue() | C1->getValue()));
2194 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
2195 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
2197 Value *Or = Builder->CreateOr(X, RHS);
2199 return BinaryOperator::CreateXor(Or,
2200 Builder->getInt(C1->getValue() & ~RHS->getValue()));
2203 // Try to fold constant and into select arguments.
2204 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2205 if (Instruction *R = FoldOpIntoSelect(I, SI))
2208 if (isa<PHINode>(Op0))
2209 if (Instruction *NV = FoldOpIntoPhi(I))
2213 Value *A = nullptr, *B = nullptr;
2214 ConstantInt *C1 = nullptr, *C2 = nullptr;
2216 // (A | B) | C and A | (B | C) -> bswap if possible.
2217 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
2218 if (match(Op0, m_Or(m_Value(), m_Value())) ||
2219 match(Op1, m_Or(m_Value(), m_Value())) ||
2220 (match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
2221 match(Op1, m_LogicalShift(m_Value(), m_Value())))) {
2222 if (Instruction *BSwap = MatchBSwap(I))
2226 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
2227 if (Op0->hasOneUse() &&
2228 match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
2229 MaskedValueIsZero(Op1, C1->getValue(), 0, &I)) {
2230 Value *NOr = Builder->CreateOr(A, Op1);
2232 return BinaryOperator::CreateXor(NOr, C1);
2235 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
2236 if (Op1->hasOneUse() &&
2237 match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
2238 MaskedValueIsZero(Op0, C1->getValue(), 0, &I)) {
2239 Value *NOr = Builder->CreateOr(A, Op0);
2241 return BinaryOperator::CreateXor(NOr, C1);
2244 // ((~A & B) | A) -> (A | B)
2245 if (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2246 match(Op1, m_Specific(A)))
2247 return BinaryOperator::CreateOr(A, B);
2249 // ((A & B) | ~A) -> (~A | B)
2250 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
2251 match(Op1, m_Not(m_Specific(A))))
2252 return BinaryOperator::CreateOr(Builder->CreateNot(A), B);
2254 // (A & (~B)) | (A ^ B) -> (A ^ B)
2255 if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2256 match(Op1, m_Xor(m_Specific(A), m_Specific(B))))
2257 return BinaryOperator::CreateXor(A, B);
2259 // (A ^ B) | ( A & (~B)) -> (A ^ B)
2260 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
2261 match(Op1, m_And(m_Specific(A), m_Not(m_Specific(B)))))
2262 return BinaryOperator::CreateXor(A, B);
2265 Value *C = nullptr, *D = nullptr;
2266 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2267 match(Op1, m_And(m_Value(B), m_Value(D)))) {
2268 Value *V1 = nullptr, *V2 = nullptr;
2269 C1 = dyn_cast<ConstantInt>(C);
2270 C2 = dyn_cast<ConstantInt>(D);
2271 if (C1 && C2) { // (A & C1)|(B & C2)
2272 if ((C1->getValue() & C2->getValue()) == 0) {
2273 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
2274 // iff (C1&C2) == 0 and (N&~C1) == 0
2275 if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
2277 MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N)
2279 MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V)
2280 return BinaryOperator::CreateAnd(A,
2281 Builder->getInt(C1->getValue()|C2->getValue()));
2282 // Or commutes, try both ways.
2283 if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
2285 MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
2287 MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V)
2288 return BinaryOperator::CreateAnd(B,
2289 Builder->getInt(C1->getValue()|C2->getValue()));
2291 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
2292 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
2293 ConstantInt *C3 = nullptr, *C4 = nullptr;
2294 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
2295 (C3->getValue() & ~C1->getValue()) == 0 &&
2296 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
2297 (C4->getValue() & ~C2->getValue()) == 0) {
2298 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
2299 return BinaryOperator::CreateAnd(V2,
2300 Builder->getInt(C1->getValue()|C2->getValue()));
2305 // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants.
2306 // Don't do this for vector select idioms, the code generator doesn't handle
2308 if (!I.getType()->isVectorTy()) {
2309 if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
2311 if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
2313 if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
2315 if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
2319 // ((A&~B)|(~A&B)) -> A^B
2320 if ((match(C, m_Not(m_Specific(D))) &&
2321 match(B, m_Not(m_Specific(A)))))
2322 return BinaryOperator::CreateXor(A, D);
2323 // ((~B&A)|(~A&B)) -> A^B
2324 if ((match(A, m_Not(m_Specific(D))) &&
2325 match(B, m_Not(m_Specific(C)))))
2326 return BinaryOperator::CreateXor(C, D);
2327 // ((A&~B)|(B&~A)) -> A^B
2328 if ((match(C, m_Not(m_Specific(B))) &&
2329 match(D, m_Not(m_Specific(A)))))
2330 return BinaryOperator::CreateXor(A, B);
2331 // ((~B&A)|(B&~A)) -> A^B
2332 if ((match(A, m_Not(m_Specific(B))) &&
2333 match(D, m_Not(m_Specific(C)))))
2334 return BinaryOperator::CreateXor(C, B);
2336 // ((A|B)&1)|(B&-2) -> (A&1) | B
2337 if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2338 match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2339 Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2340 if (Ret) return Ret;
2342 // (B&-2)|((A|B)&1) -> (A&1) | B
2343 if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2344 match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2345 Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2346 if (Ret) return Ret;
2348 // ((A^B)&1)|(B&-2) -> (A&1) ^ B
2349 if (match(A, m_Xor(m_Value(V1), m_Specific(B))) ||
2350 match(A, m_Xor(m_Specific(B), m_Value(V1)))) {
2351 Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C);
2352 if (Ret) return Ret;
2354 // (B&-2)|((A^B)&1) -> (A&1) ^ B
2355 if (match(B, m_Xor(m_Specific(A), m_Value(V1))) ||
2356 match(B, m_Xor(m_Value(V1), m_Specific(A)))) {
2357 Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D);
2358 if (Ret) return Ret;
2362 // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2363 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2364 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2365 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
2366 return BinaryOperator::CreateOr(Op0, C);
2368 // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2369 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2370 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2371 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
2372 return BinaryOperator::CreateOr(Op1, C);
2374 // ((B | C) & A) | B -> B | (A & C)
2375 if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2376 return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C));
2378 // (~A | ~B) == (~(A & B)) - De Morgan's Law
2379 if (Value *Op0NotVal = dyn_castNotVal(Op0))
2380 if (Value *Op1NotVal = dyn_castNotVal(Op1))
2381 if (Op0->hasOneUse() && Op1->hasOneUse()) {
2382 Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
2383 I.getName()+".demorgan");
2384 return BinaryOperator::CreateNot(And);
2387 // Canonicalize xor to the RHS.
2388 bool SwappedForXor = false;
2389 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2390 std::swap(Op0, Op1);
2391 SwappedForXor = true;
2394 // A | ( A ^ B) -> A | B
2395 // A | (~A ^ B) -> A | ~B
2396 // (A & B) | (A ^ B)
2397 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2398 if (Op0 == A || Op0 == B)
2399 return BinaryOperator::CreateOr(A, B);
2401 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2402 match(Op0, m_And(m_Specific(B), m_Specific(A))))
2403 return BinaryOperator::CreateOr(A, B);
2405 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2406 Value *Not = Builder->CreateNot(B, B->getName()+".not");
2407 return BinaryOperator::CreateOr(Not, Op0);
2409 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2410 Value *Not = Builder->CreateNot(A, A->getName()+".not");
2411 return BinaryOperator::CreateOr(Not, Op0);
2415 // A | ~(A | B) -> A | ~B
2416 // A | ~(A ^ B) -> A | ~B
2417 if (match(Op1, m_Not(m_Value(A))))
2418 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2419 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2420 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2421 B->getOpcode() == Instruction::Xor)) {
2422 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2424 Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2425 return BinaryOperator::CreateOr(Not, Op0);
2428 // (A & B) | ((~A) ^ B) -> (~A ^ B)
2429 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
2430 match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B))))
2431 return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2433 // ((~A) ^ B) | (A & B) -> (~A ^ B)
2434 if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2435 match(Op1, m_And(m_Specific(A), m_Specific(B))))
2436 return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2439 std::swap(Op0, Op1);
2442 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2443 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2445 if (Value *Res = FoldOrOfICmps(LHS, RHS, &I))
2446 return ReplaceInstUsesWith(I, Res);
2448 // TODO: Make this recursive; it's a little tricky because an arbitrary
2449 // number of 'or' instructions might have to be created.
2451 if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2452 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2453 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
2454 return ReplaceInstUsesWith(I, Builder->CreateOr(Res, Y));
2455 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2456 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
2457 return ReplaceInstUsesWith(I, Builder->CreateOr(Res, X));
2459 if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2460 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2461 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
2462 return ReplaceInstUsesWith(I, Builder->CreateOr(Res, Y));
2463 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2464 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
2465 return ReplaceInstUsesWith(I, Builder->CreateOr(Res, X));
2469 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
2470 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2471 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2472 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2473 return ReplaceInstUsesWith(I, Res);
2475 // fold (or (cast A), (cast B)) -> (cast (or A, B))
2476 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2477 CastInst *Op1C = dyn_cast<CastInst>(Op1);
2478 if (Op1C && Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
2479 Type *SrcTy = Op0C->getOperand(0)->getType();
2480 if (SrcTy == Op1C->getOperand(0)->getType() &&
2481 SrcTy->isIntOrIntVectorTy()) {
2482 Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
2484 if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
2485 // Only do this if the casts both really cause code to be
2487 ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
2488 ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
2489 Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.getName());
2490 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2493 // If this is or(cast(icmp), cast(icmp)), try to fold this even if the
2494 // cast is otherwise not optimizable. This happens for vector sexts.
2495 if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
2496 if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
2497 if (Value *Res = FoldOrOfICmps(LHS, RHS, &I))
2498 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
2500 // If this is or(cast(fcmp), cast(fcmp)), try to fold this even if the
2501 // cast is otherwise not optimizable. This happens for vector sexts.
2502 if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
2503 if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
2504 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2505 return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
2510 // or(sext(A), B) -> A ? -1 : B where A is an i1
2511 // or(A, sext(B)) -> B ? -1 : A where B is an i1
2512 if (match(Op0, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2513 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
2514 if (match(Op1, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2515 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2517 // Note: If we've gotten to the point of visiting the outer OR, then the
2518 // inner one couldn't be simplified. If it was a constant, then it won't
2519 // be simplified by a later pass either, so we try swapping the inner/outer
2520 // ORs in the hopes that we'll be able to simplify it this way.
2521 // (X|C) | V --> (X|V) | C
2522 if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2523 match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2524 Value *Inner = Builder->CreateOr(A, Op1);
2525 Inner->takeName(Op0);
2526 return BinaryOperator::CreateOr(Inner, C1);
2529 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2530 // Since this OR statement hasn't been optimized further yet, we hope
2531 // that this transformation will allow the new ORs to be optimized.
2533 Value *X = nullptr, *Y = nullptr;
2534 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2535 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2536 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2537 Value *orTrue = Builder->CreateOr(A, C);
2538 Value *orFalse = Builder->CreateOr(B, D);
2539 return SelectInst::Create(X, orTrue, orFalse);
2543 return Changed ? &I : nullptr;
2546 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
2547 bool Changed = SimplifyAssociativeOrCommutative(I);
2548 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2550 if (Value *V = SimplifyVectorOp(I))
2551 return ReplaceInstUsesWith(I, V);
2553 if (Value *V = SimplifyXorInst(Op0, Op1, DL, TLI, DT, AC))
2554 return ReplaceInstUsesWith(I, V);
2556 // (A&B)^(A&C) -> A&(B^C) etc
2557 if (Value *V = SimplifyUsingDistributiveLaws(I))
2558 return ReplaceInstUsesWith(I, V);
2560 // See if we can simplify any instructions used by the instruction whose sole
2561 // purpose is to compute bits we don't care about.
2562 if (SimplifyDemandedInstructionBits(I))
2565 if (Value *V = SimplifyBSwap(I))
2566 return ReplaceInstUsesWith(I, V);
2568 // Is this a ~ operation?
2569 if (Value *NotOp = dyn_castNotVal(&I)) {
2570 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
2571 if (Op0I->getOpcode() == Instruction::And ||
2572 Op0I->getOpcode() == Instruction::Or) {
2573 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2574 // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2575 if (dyn_castNotVal(Op0I->getOperand(1)))
2576 Op0I->swapOperands();
2577 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2579 Builder->CreateNot(Op0I->getOperand(1),
2580 Op0I->getOperand(1)->getName()+".not");
2581 if (Op0I->getOpcode() == Instruction::And)
2582 return BinaryOperator::CreateOr(Op0NotVal, NotY);
2583 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2586 // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2587 // ~(X | Y) === (~X & ~Y) - De Morgan's Law
2588 if (isFreeToInvert(Op0I->getOperand(0)) &&
2589 isFreeToInvert(Op0I->getOperand(1))) {
2591 Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2593 Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2594 if (Op0I->getOpcode() == Instruction::And)
2595 return BinaryOperator::CreateOr(NotX, NotY);
2596 return BinaryOperator::CreateAnd(NotX, NotY);
2599 } else if (Op0I->getOpcode() == Instruction::AShr) {
2600 // ~(~X >>s Y) --> (X >>s Y)
2601 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2602 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
2607 if (Constant *RHS = dyn_cast<Constant>(Op1)) {
2608 if (RHS->isAllOnesValue() && Op0->hasOneUse())
2609 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
2610 if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2611 return CmpInst::Create(CI->getOpcode(),
2612 CI->getInversePredicate(),
2613 CI->getOperand(0), CI->getOperand(1));
2616 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2617 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2618 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2619 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2620 if (CI->hasOneUse() && Op0C->hasOneUse()) {
2621 Instruction::CastOps Opcode = Op0C->getOpcode();
2622 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
2623 (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(),
2624 Op0C->getDestTy()))) {
2625 CI->setPredicate(CI->getInversePredicate());
2626 return CastInst::Create(Opcode, CI, Op0C->getType());
2632 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2633 // ~(c-X) == X-c-1 == X+(-c-1)
2634 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2635 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2636 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2637 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2638 ConstantInt::get(I.getType(), 1));
2639 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2642 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2643 if (Op0I->getOpcode() == Instruction::Add) {
2644 // ~(X-c) --> (-c-1)-X
2645 if (RHS->isAllOnesValue()) {
2646 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2647 return BinaryOperator::CreateSub(
2648 ConstantExpr::getSub(NegOp0CI,
2649 ConstantInt::get(I.getType(), 1)),
2650 Op0I->getOperand(0));
2651 } else if (RHS->getValue().isSignBit()) {
2652 // (X + C) ^ signbit -> (X + C + signbit)
2653 Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
2654 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2657 } else if (Op0I->getOpcode() == Instruction::Or) {
2658 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
2659 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(),
2661 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2662 // Anything in both C1 and C2 is known to be zero, remove it from
2664 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
2665 NewRHS = ConstantExpr::getAnd(NewRHS,
2666 ConstantExpr::getNot(CommonBits));
2668 I.setOperand(0, Op0I->getOperand(0));
2669 I.setOperand(1, NewRHS);
2672 } else if (Op0I->getOpcode() == Instruction::LShr) {
2673 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2677 if (Op0I->hasOneUse() &&
2678 (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2679 E1->getOpcode() == Instruction::Xor &&
2680 (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2681 // fold (C1 >> C2) ^ C3
2682 ConstantInt *C2 = Op0CI, *C3 = RHS;
2683 APInt FoldConst = C1->getValue().lshr(C2->getValue());
2684 FoldConst ^= C3->getValue();
2685 // Prepare the two operands.
2686 Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2687 Opnd0->takeName(Op0I);
2688 cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2689 Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2691 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2697 // Try to fold constant and into select arguments.
2698 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2699 if (Instruction *R = FoldOpIntoSelect(I, SI))
2701 if (isa<PHINode>(Op0))
2702 if (Instruction *NV = FoldOpIntoPhi(I))
2706 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2709 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2710 if (A == Op0) { // B^(B|A) == (A|B)^B
2711 Op1I->swapOperands();
2713 std::swap(Op0, Op1);
2714 } else if (B == Op0) { // B^(A|B) == (A|B)^B
2715 I.swapOperands(); // Simplified below.
2716 std::swap(Op0, Op1);
2718 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
2720 if (A == Op0) { // A^(A&B) -> A^(B&A)
2721 Op1I->swapOperands();
2724 if (B == Op0) { // A^(B&A) -> (B&A)^A
2725 I.swapOperands(); // Simplified below.
2726 std::swap(Op0, Op1);
2731 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2734 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2735 Op0I->hasOneUse()) {
2736 if (A == Op1) // (B|A)^B == (A|B)^B
2738 if (B == Op1) // (A|B)^B == A & ~B
2739 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
2740 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2742 if (A == Op1) // (A&B)^A -> (B&A)^A
2744 if (B == Op1 && // (B&A)^A == ~B & A
2745 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
2746 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
2752 Value *A, *B, *C, *D;
2753 // (A & B)^(A | B) -> A ^ B
2754 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2755 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
2756 if ((A == C && B == D) || (A == D && B == C))
2757 return BinaryOperator::CreateXor(A, B);
2759 // (A | B)^(A & B) -> A ^ B
2760 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2761 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
2762 if ((A == C && B == D) || (A == D && B == C))
2763 return BinaryOperator::CreateXor(A, B);
2765 // (A | ~B) ^ (~A | B) -> A ^ B
2766 if (match(Op0I, m_Or(m_Value(A), m_Not(m_Value(B)))) &&
2767 match(Op1I, m_Or(m_Not(m_Specific(A)), m_Specific(B)))) {
2768 return BinaryOperator::CreateXor(A, B);
2770 // (~A | B) ^ (A | ~B) -> A ^ B
2771 if (match(Op0I, m_Or(m_Not(m_Value(A)), m_Value(B))) &&
2772 match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) {
2773 return BinaryOperator::CreateXor(A, B);
2775 // (A & ~B) ^ (~A & B) -> A ^ B
2776 if (match(Op0I, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2777 match(Op1I, m_And(m_Not(m_Specific(A)), m_Specific(B)))) {
2778 return BinaryOperator::CreateXor(A, B);
2780 // (~A & B) ^ (A & ~B) -> A ^ B
2781 if (match(Op0I, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2782 match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B))))) {
2783 return BinaryOperator::CreateXor(A, B);
2785 // (A ^ C)^(A | B) -> ((~A) & B) ^ C
2786 if (match(Op0I, m_Xor(m_Value(D), m_Value(C))) &&
2787 match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2789 return BinaryOperator::CreateXor(
2790 Builder->CreateAnd(Builder->CreateNot(A), B), C);
2792 return BinaryOperator::CreateXor(
2793 Builder->CreateAnd(Builder->CreateNot(B), A), C);
2795 // (A | B)^(A ^ C) -> ((~A) & B) ^ C
2796 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2797 match(Op1I, m_Xor(m_Value(D), m_Value(C)))) {
2799 return BinaryOperator::CreateXor(
2800 Builder->CreateAnd(Builder->CreateNot(A), B), C);
2802 return BinaryOperator::CreateXor(
2803 Builder->CreateAnd(Builder->CreateNot(B), A), C);
2805 // (A & B) ^ (A ^ B) -> (A | B)
2806 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2807 match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))
2808 return BinaryOperator::CreateOr(A, B);
2809 // (A ^ B) ^ (A & B) -> (A | B)
2810 if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
2811 match(Op1I, m_And(m_Specific(A), m_Specific(B))))
2812 return BinaryOperator::CreateOr(A, B);
2815 Value *A = nullptr, *B = nullptr;
2816 // (A & ~B) ^ (~A) -> ~(A & B)
2817 if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2818 match(Op1, m_Not(m_Specific(A))))
2819 return BinaryOperator::CreateNot(Builder->CreateAnd(A, B));
2821 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
2822 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2823 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
2824 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2825 if (LHS->getOperand(0) == RHS->getOperand(1) &&
2826 LHS->getOperand(1) == RHS->getOperand(0))
2827 LHS->swapOperands();
2828 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2829 LHS->getOperand(1) == RHS->getOperand(1)) {
2830 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2831 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2832 bool isSigned = LHS->isSigned() || RHS->isSigned();
2833 return ReplaceInstUsesWith(I,
2834 getNewICmpValue(isSigned, Code, Op0, Op1,
2839 // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
2840 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2841 if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
2842 if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
2843 Type *SrcTy = Op0C->getOperand(0)->getType();
2844 if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegerTy() &&
2845 // Only do this if the casts both really cause code to be generated.
2846 ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
2848 ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
2850 Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
2851 Op1C->getOperand(0), I.getName());
2852 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2857 return Changed ? &I : nullptr;