1 //===- InstCombineAndOrXor.cpp --------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the visitAnd, visitOr, and visitXor functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "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"
20 #include "llvm/Transforms/Utils/Local.h"
21 using namespace llvm;
22 using namespace PatternMatch;
23 
24 #define DEBUG_TYPE "instcombine"
25 
26 static inline Value *dyn_castNotVal(Value *V) {
27   // If this is not(not(x)) don't return that this is a not: we want the two
28   // not's to be folded first.
29   if (BinaryOperator::isNot(V)) {
30     Value *Operand = BinaryOperator::getNotArgument(V);
31     if (!IsFreeToInvert(Operand, Operand->hasOneUse()))
32       return Operand;
33   }
34 
35   // Constants can be considered to be not'ed values...
36   if (ConstantInt *C = dyn_cast<ConstantInt>(V))
37     return ConstantInt::get(C->getType(), ~C->getValue());
38   return nullptr;
39 }
40 
41 /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into
42 /// a four bit mask.
43 static unsigned getFCmpCode(FCmpInst::Predicate CC) {
44   assert(FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE &&
45          "Unexpected FCmp predicate!");
46   // Take advantage of the bit pattern of FCmpInst::Predicate here.
47   //                                                 U L G E
48   static_assert(FCmpInst::FCMP_FALSE ==  0, "");  // 0 0 0 0
49   static_assert(FCmpInst::FCMP_OEQ   ==  1, "");  // 0 0 0 1
50   static_assert(FCmpInst::FCMP_OGT   ==  2, "");  // 0 0 1 0
51   static_assert(FCmpInst::FCMP_OGE   ==  3, "");  // 0 0 1 1
52   static_assert(FCmpInst::FCMP_OLT   ==  4, "");  // 0 1 0 0
53   static_assert(FCmpInst::FCMP_OLE   ==  5, "");  // 0 1 0 1
54   static_assert(FCmpInst::FCMP_ONE   ==  6, "");  // 0 1 1 0
55   static_assert(FCmpInst::FCMP_ORD   ==  7, "");  // 0 1 1 1
56   static_assert(FCmpInst::FCMP_UNO   ==  8, "");  // 1 0 0 0
57   static_assert(FCmpInst::FCMP_UEQ   ==  9, "");  // 1 0 0 1
58   static_assert(FCmpInst::FCMP_UGT   == 10, "");  // 1 0 1 0
59   static_assert(FCmpInst::FCMP_UGE   == 11, "");  // 1 0 1 1
60   static_assert(FCmpInst::FCMP_ULT   == 12, "");  // 1 1 0 0
61   static_assert(FCmpInst::FCMP_ULE   == 13, "");  // 1 1 0 1
62   static_assert(FCmpInst::FCMP_UNE   == 14, "");  // 1 1 1 0
63   static_assert(FCmpInst::FCMP_TRUE  == 15, "");  // 1 1 1 1
64   return CC;
65 }
66 
67 /// This is the complement of getICmpCode, which turns an opcode and two
68 /// operands into either a constant true or false, or a brand new ICmp
69 /// instruction. The sign is passed in to determine which kind of predicate to
70 /// use in the new icmp instruction.
71 static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
72                               InstCombiner::BuilderTy *Builder) {
73   ICmpInst::Predicate NewPred;
74   if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred))
75     return NewConstant;
76   return Builder->CreateICmp(NewPred, LHS, RHS);
77 }
78 
79 /// This is the complement of getFCmpCode, which turns an opcode and two
80 /// operands into either a FCmp instruction, or a true/false constant.
81 static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
82                            InstCombiner::BuilderTy *Builder) {
83   const auto Pred = static_cast<FCmpInst::Predicate>(Code);
84   assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE &&
85          "Unexpected FCmp predicate!");
86   if (Pred == FCmpInst::FCMP_FALSE)
87     return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
88   if (Pred == FCmpInst::FCMP_TRUE)
89     return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
90   return Builder->CreateFCmp(Pred, LHS, RHS);
91 }
92 
93 /// \brief Transform BITWISE_OP(BSWAP(A),BSWAP(B)) to BSWAP(BITWISE_OP(A, B))
94 /// \param I Binary operator to transform.
95 /// \return Pointer to node that must replace the original binary operator, or
96 ///         null pointer if no transformation was made.
97 Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) {
98   IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
99 
100   // Can't do vectors.
101   if (I.getType()->isVectorTy())
102     return nullptr;
103 
104   // Can only do bitwise ops.
105   if (!I.isBitwiseLogicOp())
106     return nullptr;
107 
108   Value *OldLHS = I.getOperand(0);
109   Value *OldRHS = I.getOperand(1);
110   ConstantInt *ConstLHS = dyn_cast<ConstantInt>(OldLHS);
111   ConstantInt *ConstRHS = dyn_cast<ConstantInt>(OldRHS);
112   IntrinsicInst *IntrLHS = dyn_cast<IntrinsicInst>(OldLHS);
113   IntrinsicInst *IntrRHS = dyn_cast<IntrinsicInst>(OldRHS);
114   bool IsBswapLHS = (IntrLHS && IntrLHS->getIntrinsicID() == Intrinsic::bswap);
115   bool IsBswapRHS = (IntrRHS && IntrRHS->getIntrinsicID() == Intrinsic::bswap);
116 
117   if (!IsBswapLHS && !IsBswapRHS)
118     return nullptr;
119 
120   if (!IsBswapLHS && !ConstLHS)
121     return nullptr;
122 
123   if (!IsBswapRHS && !ConstRHS)
124     return nullptr;
125 
126   /// OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
127   /// OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
128   Value *NewLHS = IsBswapLHS ? IntrLHS->getOperand(0) :
129                   Builder->getInt(ConstLHS->getValue().byteSwap());
130 
131   Value *NewRHS = IsBswapRHS ? IntrRHS->getOperand(0) :
132                   Builder->getInt(ConstRHS->getValue().byteSwap());
133 
134   Value *BinOp = Builder->CreateBinOp(I.getOpcode(), NewLHS, NewRHS);
135   Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, ITy);
136   return Builder->CreateCall(F, BinOp);
137 }
138 
139 /// This handles expressions of the form ((val OP C1) & C2).  Where
140 /// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is
141 /// guaranteed to be a binary operator.
142 Instruction *InstCombiner::OptAndOp(Instruction *Op,
143                                     ConstantInt *OpRHS,
144                                     ConstantInt *AndRHS,
145                                     BinaryOperator &TheAnd) {
146   Value *X = Op->getOperand(0);
147   Constant *Together = nullptr;
148   if (!Op->isShift())
149     Together = ConstantExpr::getAnd(AndRHS, OpRHS);
150 
151   switch (Op->getOpcode()) {
152   case Instruction::Xor:
153     if (Op->hasOneUse()) {
154       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
155       Value *And = Builder->CreateAnd(X, AndRHS);
156       And->takeName(Op);
157       return BinaryOperator::CreateXor(And, Together);
158     }
159     break;
160   case Instruction::Or:
161     if (Op->hasOneUse()){
162       if (Together != OpRHS) {
163         // (X | C1) & C2 --> (X | (C1&C2)) & C2
164         Value *Or = Builder->CreateOr(X, Together);
165         Or->takeName(Op);
166         return BinaryOperator::CreateAnd(Or, AndRHS);
167       }
168 
169       ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
170       if (TogetherCI && !TogetherCI->isZero()){
171         // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
172         // NOTE: This reduces the number of bits set in the & mask, which
173         // can expose opportunities for store narrowing.
174         Together = ConstantExpr::getXor(AndRHS, Together);
175         Value *And = Builder->CreateAnd(X, Together);
176         And->takeName(Op);
177         return BinaryOperator::CreateOr(And, OpRHS);
178       }
179     }
180 
181     break;
182   case Instruction::Add:
183     if (Op->hasOneUse()) {
184       // Adding a one to a single bit bit-field should be turned into an XOR
185       // of the bit.  First thing to check is to see if this AND is with a
186       // single bit constant.
187       const APInt &AndRHSV = AndRHS->getValue();
188 
189       // If there is only one bit set.
190       if (AndRHSV.isPowerOf2()) {
191         // Ok, at this point, we know that we are masking the result of the
192         // ADD down to exactly one bit.  If the constant we are adding has
193         // no bits set below this bit, then we can eliminate the ADD.
194         const APInt& AddRHS = OpRHS->getValue();
195 
196         // Check to see if any bits below the one bit set in AndRHSV are set.
197         if ((AddRHS & (AndRHSV-1)) == 0) {
198           // If not, the only thing that can effect the output of the AND is
199           // the bit specified by AndRHSV.  If that bit is set, the effect of
200           // the XOR is to toggle the bit.  If it is clear, then the ADD has
201           // no effect.
202           if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
203             TheAnd.setOperand(0, X);
204             return &TheAnd;
205           } else {
206             // Pull the XOR out of the AND.
207             Value *NewAnd = Builder->CreateAnd(X, AndRHS);
208             NewAnd->takeName(Op);
209             return BinaryOperator::CreateXor(NewAnd, AndRHS);
210           }
211         }
212       }
213     }
214     break;
215 
216   case Instruction::Shl: {
217     // We know that the AND will not produce any of the bits shifted in, so if
218     // the anded constant includes them, clear them now!
219     //
220     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
221     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
222     APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
223     ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask);
224 
225     if (CI->getValue() == ShlMask)
226       // Masking out bits that the shift already masks.
227       return replaceInstUsesWith(TheAnd, Op);   // No need for the and.
228 
229     if (CI != AndRHS) {                  // Reducing bits set in and.
230       TheAnd.setOperand(1, CI);
231       return &TheAnd;
232     }
233     break;
234   }
235   case Instruction::LShr: {
236     // We know that the AND will not produce any of the bits shifted in, so if
237     // the anded constant includes them, clear them now!  This only applies to
238     // unsigned shifts, because a signed shr may bring in set bits!
239     //
240     uint32_t BitWidth = AndRHS->getType()->getBitWidth();
241     uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
242     APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
243     ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask);
244 
245     if (CI->getValue() == ShrMask)
246       // Masking out bits that the shift already masks.
247       return replaceInstUsesWith(TheAnd, Op);
248 
249     if (CI != AndRHS) {
250       TheAnd.setOperand(1, CI);  // Reduce bits set in and cst.
251       return &TheAnd;
252     }
253     break;
254   }
255   case Instruction::AShr:
256     // Signed shr.
257     // See if this is shifting in some sign extension, then masking it out
258     // with an and.
259     if (Op->hasOneUse()) {
260       uint32_t BitWidth = AndRHS->getType()->getBitWidth();
261       uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
262       APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
263       Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask);
264       if (C == AndRHS) {          // Masking out bits shifted in.
265         // (Val ashr C1) & C2 -> (Val lshr C1) & C2
266         // Make the argument unsigned.
267         Value *ShVal = Op->getOperand(0);
268         ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
269         return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
270       }
271     }
272     break;
273   }
274   return nullptr;
275 }
276 
277 /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
278 /// (V < Lo || V >= Hi). This method expects that Lo <= Hi. IsSigned indicates
279 /// whether to treat V, Lo, and Hi as signed or not.
280 Value *InstCombiner::insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
281                                      bool isSigned, bool Inside) {
282   assert((isSigned ? Lo.sle(Hi) : Lo.ule(Hi)) &&
283          "Lo is not <= Hi in range emission code!");
284 
285   Type *Ty = V->getType();
286   if (Lo == Hi)
287     return Inside ? ConstantInt::getFalse(Ty) : ConstantInt::getTrue(Ty);
288 
289   // V >= Min && V <  Hi --> V <  Hi
290   // V <  Min || V >= Hi --> V >= Hi
291   ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
292   if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
293     Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
294     return Builder->CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));
295   }
296 
297   // V >= Lo && V <  Hi --> V - Lo u<  Hi - Lo
298   // V <  Lo || V >= Hi --> V - Lo u>= Hi - Lo
299   Value *VMinusLo =
300       Builder->CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off");
301   Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo);
302   return Builder->CreateICmp(Pred, VMinusLo, HiMinusLo);
303 }
304 
305 /// Returns true iff Val consists of one contiguous run of 1s with any number
306 /// of 0s on either side.  The 1s are allowed to wrap from LSB to MSB,
307 /// so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is
308 /// not, since all 1s are not contiguous.
309 static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) {
310   const APInt& V = Val->getValue();
311   uint32_t BitWidth = Val->getType()->getBitWidth();
312   if (!APIntOps::isShiftedMask(BitWidth, V)) return false;
313 
314   // look for the first zero bit after the run of ones
315   MB = BitWidth - ((V - 1) ^ V).countLeadingZeros();
316   // look for the first non-zero bit
317   ME = V.getActiveBits();
318   return true;
319 }
320 
321 /// This is part of an expression (LHS +/- RHS) & Mask, where isSub determines
322 /// whether the operator is a sub. If we can fold one of the following xforms:
323 ///
324 /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask
325 /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
326 /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0
327 ///
328 /// return (A +/- B).
329 ///
330 Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
331                                         ConstantInt *Mask, bool isSub,
332                                         Instruction &I) {
333   Instruction *LHSI = dyn_cast<Instruction>(LHS);
334   if (!LHSI || LHSI->getNumOperands() != 2 ||
335       !isa<ConstantInt>(LHSI->getOperand(1))) return nullptr;
336 
337   ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
338 
339   switch (LHSI->getOpcode()) {
340   default: return nullptr;
341   case Instruction::And:
342     if (ConstantExpr::getAnd(N, Mask) == Mask) {
343       // If the AndRHS is a power of two minus one (0+1+), this is simple.
344       if ((Mask->getValue().countLeadingZeros() +
345            Mask->getValue().countPopulation()) ==
346           Mask->getValue().getBitWidth())
347         break;
348 
349       // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
350       // part, we don't need any explicit masks to take them out of A.  If that
351       // is all N is, ignore it.
352       uint32_t MB = 0, ME = 0;
353       if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive
354         uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
355         APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
356         if (MaskedValueIsZero(RHS, Mask, 0, &I))
357           break;
358       }
359     }
360     return nullptr;
361   case Instruction::Or:
362   case Instruction::Xor:
363     // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
364     if ((Mask->getValue().countLeadingZeros() +
365          Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
366         && ConstantExpr::getAnd(N, Mask)->isNullValue())
367       break;
368     return nullptr;
369   }
370 
371   if (isSub)
372     return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
373   return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
374 }
375 
376 /// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C)
377 /// One of A and B is considered the mask, the other the value. This is
378 /// described as the "AMask" or "BMask" part of the enum. If the enum
379 /// contains only "Mask", then both A and B can be considered masks.
380 /// If A is the mask, then it was proven, that (A & C) == C. This
381 /// is trivial if C == A, or C == 0. If both A and C are constants, this
382 /// proof is also easy.
383 /// For the following explanations we assume that A is the mask.
384 /// The part "AllOnes" declares, that the comparison is true only
385 /// if (A & B) == A, or all bits of A are set in B.
386 ///   Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes
387 /// The part "AllZeroes" declares, that the comparison is true only
388 /// if (A & B) == 0, or all bits of A are cleared in B.
389 ///   Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes
390 /// The part "Mixed" declares, that (A & B) == C and C might or might not
391 /// contain any number of one bits and zero bits.
392 ///   Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed
393 /// The Part "Not" means, that in above descriptions "==" should be replaced
394 /// by "!=".
395 ///   Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes
396 /// If the mask A contains a single bit, then the following is equivalent:
397 ///    (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
398 ///    (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
399 enum MaskedICmpType {
400   FoldMskICmp_AMask_AllOnes           =     1,
401   FoldMskICmp_AMask_NotAllOnes        =     2,
402   FoldMskICmp_BMask_AllOnes           =     4,
403   FoldMskICmp_BMask_NotAllOnes        =     8,
404   FoldMskICmp_Mask_AllZeroes          =    16,
405   FoldMskICmp_Mask_NotAllZeroes       =    32,
406   FoldMskICmp_AMask_Mixed             =    64,
407   FoldMskICmp_AMask_NotMixed          =   128,
408   FoldMskICmp_BMask_Mixed             =   256,
409   FoldMskICmp_BMask_NotMixed          =   512
410 };
411 
412 /// Return the set of pattern classes (from MaskedICmpType)
413 /// that (icmp SCC (A & B), C) satisfies.
414 static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C,
415                                     ICmpInst::Predicate SCC)
416 {
417   ConstantInt *ACst = dyn_cast<ConstantInt>(A);
418   ConstantInt *BCst = dyn_cast<ConstantInt>(B);
419   ConstantInt *CCst = dyn_cast<ConstantInt>(C);
420   bool icmp_eq = (SCC == ICmpInst::ICMP_EQ);
421   bool icmp_abit = (ACst && !ACst->isZero() &&
422                     ACst->getValue().isPowerOf2());
423   bool icmp_bbit = (BCst && !BCst->isZero() &&
424                     BCst->getValue().isPowerOf2());
425   unsigned result = 0;
426   if (CCst && CCst->isZero()) {
427     // if C is zero, then both A and B qualify as mask
428     result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes |
429                           FoldMskICmp_AMask_Mixed |
430                           FoldMskICmp_BMask_Mixed)
431                        : (FoldMskICmp_Mask_NotAllZeroes |
432                           FoldMskICmp_AMask_NotMixed |
433                           FoldMskICmp_BMask_NotMixed));
434     if (icmp_abit)
435       result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes |
436                             FoldMskICmp_AMask_NotMixed)
437                          : (FoldMskICmp_AMask_AllOnes |
438                             FoldMskICmp_AMask_Mixed));
439     if (icmp_bbit)
440       result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes |
441                             FoldMskICmp_BMask_NotMixed)
442                          : (FoldMskICmp_BMask_AllOnes |
443                             FoldMskICmp_BMask_Mixed));
444     return result;
445   }
446   if (A == C) {
447     result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes |
448                           FoldMskICmp_AMask_Mixed)
449                        : (FoldMskICmp_AMask_NotAllOnes |
450                           FoldMskICmp_AMask_NotMixed));
451     if (icmp_abit)
452       result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
453                             FoldMskICmp_AMask_NotMixed)
454                          : (FoldMskICmp_Mask_AllZeroes |
455                             FoldMskICmp_AMask_Mixed));
456   } else if (ACst && CCst &&
457              ConstantExpr::getAnd(ACst, CCst) == CCst) {
458     result |= (icmp_eq ? FoldMskICmp_AMask_Mixed
459                        : FoldMskICmp_AMask_NotMixed);
460   }
461   if (B == C) {
462     result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes |
463                           FoldMskICmp_BMask_Mixed)
464                        : (FoldMskICmp_BMask_NotAllOnes |
465                           FoldMskICmp_BMask_NotMixed));
466     if (icmp_bbit)
467       result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
468                             FoldMskICmp_BMask_NotMixed)
469                          : (FoldMskICmp_Mask_AllZeroes |
470                             FoldMskICmp_BMask_Mixed));
471   } else if (BCst && CCst &&
472              ConstantExpr::getAnd(BCst, CCst) == CCst) {
473     result |= (icmp_eq ? FoldMskICmp_BMask_Mixed
474                        : FoldMskICmp_BMask_NotMixed);
475   }
476   return result;
477 }
478 
479 /// Convert an analysis of a masked ICmp into its equivalent if all boolean
480 /// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
481 /// is adjacent to the corresponding normal flag (recording ==), this just
482 /// involves swapping those bits over.
483 static unsigned conjugateICmpMask(unsigned Mask) {
484   unsigned NewMask;
485   NewMask = (Mask & (FoldMskICmp_AMask_AllOnes | FoldMskICmp_BMask_AllOnes |
486                      FoldMskICmp_Mask_AllZeroes | FoldMskICmp_AMask_Mixed |
487                      FoldMskICmp_BMask_Mixed))
488             << 1;
489 
490   NewMask |=
491       (Mask & (FoldMskICmp_AMask_NotAllOnes | FoldMskICmp_BMask_NotAllOnes |
492                FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_AMask_NotMixed |
493                FoldMskICmp_BMask_NotMixed))
494       >> 1;
495 
496   return NewMask;
497 }
498 
499 /// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
500 /// Return the set of pattern classes (from MaskedICmpType)
501 /// that both LHS and RHS satisfy.
502 static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A,
503                                              Value*& B, Value*& C,
504                                              Value*& D, Value*& E,
505                                              ICmpInst *LHS, ICmpInst *RHS,
506                                              ICmpInst::Predicate &LHSCC,
507                                              ICmpInst::Predicate &RHSCC) {
508   if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0;
509   // vectors are not (yet?) supported
510   if (LHS->getOperand(0)->getType()->isVectorTy()) return 0;
511 
512   // Here comes the tricky part:
513   // LHS might be of the form L11 & L12 == X, X == L21 & L22,
514   // and L11 & L12 == L21 & L22. The same goes for RHS.
515   // Now we must find those components L** and R**, that are equal, so
516   // that we can extract the parameters A, B, C, D, and E for the canonical
517   // above.
518   Value *L1 = LHS->getOperand(0);
519   Value *L2 = LHS->getOperand(1);
520   Value *L11,*L12,*L21,*L22;
521   // Check whether the icmp can be decomposed into a bit test.
522   if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) {
523     L21 = L22 = L1 = nullptr;
524   } else {
525     // Look for ANDs in the LHS icmp.
526     if (!L1->getType()->isIntegerTy()) {
527       // You can icmp pointers, for example. They really aren't masks.
528       L11 = L12 = nullptr;
529     } else if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
530       // Any icmp can be viewed as being trivially masked; if it allows us to
531       // remove one, it's worth it.
532       L11 = L1;
533       L12 = Constant::getAllOnesValue(L1->getType());
534     }
535 
536     if (!L2->getType()->isIntegerTy()) {
537       // You can icmp pointers, for example. They really aren't masks.
538       L21 = L22 = nullptr;
539     } else if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
540       L21 = L2;
541       L22 = Constant::getAllOnesValue(L2->getType());
542     }
543   }
544 
545   // Bail if LHS was a icmp that can't be decomposed into an equality.
546   if (!ICmpInst::isEquality(LHSCC))
547     return 0;
548 
549   Value *R1 = RHS->getOperand(0);
550   Value *R2 = RHS->getOperand(1);
551   Value *R11,*R12;
552   bool ok = false;
553   if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) {
554     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
555       A = R11; D = R12;
556     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
557       A = R12; D = R11;
558     } else {
559       return 0;
560     }
561     E = R2; R1 = nullptr; ok = true;
562   } else if (R1->getType()->isIntegerTy()) {
563     if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
564       // As before, model no mask as a trivial mask if it'll let us do an
565       // optimization.
566       R11 = R1;
567       R12 = Constant::getAllOnesValue(R1->getType());
568     }
569 
570     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
571       A = R11; D = R12; E = R2; ok = true;
572     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
573       A = R12; D = R11; E = R2; ok = true;
574     }
575   }
576 
577   // Bail if RHS was a icmp that can't be decomposed into an equality.
578   if (!ICmpInst::isEquality(RHSCC))
579     return 0;
580 
581   // Look for ANDs on the right side of the RHS icmp.
582   if (!ok && R2->getType()->isIntegerTy()) {
583     if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
584       R11 = R2;
585       R12 = Constant::getAllOnesValue(R2->getType());
586     }
587 
588     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
589       A = R11; D = R12; E = R1; ok = true;
590     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
591       A = R12; D = R11; E = R1; ok = true;
592     } else {
593       return 0;
594     }
595   }
596   if (!ok)
597     return 0;
598 
599   if (L11 == A) {
600     B = L12; C = L2;
601   } else if (L12 == A) {
602     B = L11; C = L2;
603   } else if (L21 == A) {
604     B = L22; C = L1;
605   } else if (L22 == A) {
606     B = L21; C = L1;
607   }
608 
609   unsigned LeftType = getTypeOfMaskedICmp(A, B, C, LHSCC);
610   unsigned RightType = getTypeOfMaskedICmp(A, D, E, RHSCC);
611   return LeftType & RightType;
612 }
613 
614 /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
615 /// into a single (icmp(A & X) ==/!= Y).
616 static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
617                                      llvm::InstCombiner::BuilderTy *Builder) {
618   Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
619   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
620   unsigned Mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS,
621                                                LHSCC, RHSCC);
622   if (Mask == 0) return nullptr;
623   assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) &&
624          "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
625 
626   // In full generality:
627   //     (icmp (A & B) Op C) | (icmp (A & D) Op E)
628   // ==  ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
629   //
630   // If the latter can be converted into (icmp (A & X) Op Y) then the former is
631   // equivalent to (icmp (A & X) !Op Y).
632   //
633   // Therefore, we can pretend for the rest of this function that we're dealing
634   // with the conjunction, provided we flip the sense of any comparisons (both
635   // input and output).
636 
637   // In most cases we're going to produce an EQ for the "&&" case.
638   ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
639   if (!IsAnd) {
640     // Convert the masking analysis into its equivalent with negated
641     // comparisons.
642     Mask = conjugateICmpMask(Mask);
643   }
644 
645   if (Mask & FoldMskICmp_Mask_AllZeroes) {
646     // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
647     // -> (icmp eq (A & (B|D)), 0)
648     Value *NewOr = Builder->CreateOr(B, D);
649     Value *NewAnd = Builder->CreateAnd(A, NewOr);
650     // We can't use C as zero because we might actually handle
651     //   (icmp ne (A & B), B) & (icmp ne (A & D), D)
652     // with B and D, having a single bit set.
653     Value *Zero = Constant::getNullValue(A->getType());
654     return Builder->CreateICmp(NewCC, NewAnd, Zero);
655   }
656   if (Mask & FoldMskICmp_BMask_AllOnes) {
657     // (icmp eq (A & B), B) & (icmp eq (A & D), D)
658     // -> (icmp eq (A & (B|D)), (B|D))
659     Value *NewOr = Builder->CreateOr(B, D);
660     Value *NewAnd = Builder->CreateAnd(A, NewOr);
661     return Builder->CreateICmp(NewCC, NewAnd, NewOr);
662   }
663   if (Mask & FoldMskICmp_AMask_AllOnes) {
664     // (icmp eq (A & B), A) & (icmp eq (A & D), A)
665     // -> (icmp eq (A & (B&D)), A)
666     Value *NewAnd1 = Builder->CreateAnd(B, D);
667     Value *NewAnd2 = Builder->CreateAnd(A, NewAnd1);
668     return Builder->CreateICmp(NewCC, NewAnd2, A);
669   }
670 
671   // Remaining cases assume at least that B and D are constant, and depend on
672   // their actual values. This isn't strictly necessary, just a "handle the
673   // easy cases for now" decision.
674   ConstantInt *BCst = dyn_cast<ConstantInt>(B);
675   if (!BCst) return nullptr;
676   ConstantInt *DCst = dyn_cast<ConstantInt>(D);
677   if (!DCst) return nullptr;
678 
679   if (Mask & (FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_BMask_NotAllOnes)) {
680     // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
681     // (icmp ne (A & B), B) & (icmp ne (A & D), D)
682     //     -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
683     // Only valid if one of the masks is a superset of the other (check "B&D" is
684     // the same as either B or D).
685     APInt NewMask = BCst->getValue() & DCst->getValue();
686 
687     if (NewMask == BCst->getValue())
688       return LHS;
689     else if (NewMask == DCst->getValue())
690       return RHS;
691   }
692   if (Mask & FoldMskICmp_AMask_NotAllOnes) {
693     // (icmp ne (A & B), B) & (icmp ne (A & D), D)
694     //     -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
695     // Only valid if one of the masks is a superset of the other (check "B|D" is
696     // the same as either B or D).
697     APInt NewMask = BCst->getValue() | DCst->getValue();
698 
699     if (NewMask == BCst->getValue())
700       return LHS;
701     else if (NewMask == DCst->getValue())
702       return RHS;
703   }
704   if (Mask & FoldMskICmp_BMask_Mixed) {
705     // (icmp eq (A & B), C) & (icmp eq (A & D), E)
706     // We already know that B & C == C && D & E == E.
707     // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
708     // C and E, which are shared by both the mask B and the mask D, don't
709     // contradict, then we can transform to
710     // -> (icmp eq (A & (B|D)), (C|E))
711     // Currently, we only handle the case of B, C, D, and E being constant.
712     // We can't simply use C and E because we might actually handle
713     //   (icmp ne (A & B), B) & (icmp eq (A & D), D)
714     // with B and D, having a single bit set.
715     ConstantInt *CCst = dyn_cast<ConstantInt>(C);
716     if (!CCst) return nullptr;
717     ConstantInt *ECst = dyn_cast<ConstantInt>(E);
718     if (!ECst) return nullptr;
719     if (LHSCC != NewCC)
720       CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst));
721     if (RHSCC != NewCC)
722       ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst));
723     // If there is a conflict, we should actually return a false for the
724     // whole construct.
725     if (((BCst->getValue() & DCst->getValue()) &
726          (CCst->getValue() ^ ECst->getValue())) != 0)
727       return ConstantInt::get(LHS->getType(), !IsAnd);
728     Value *NewOr1 = Builder->CreateOr(B, D);
729     Value *NewOr2 = ConstantExpr::getOr(CCst, ECst);
730     Value *NewAnd = Builder->CreateAnd(A, NewOr1);
731     return Builder->CreateICmp(NewCC, NewAnd, NewOr2);
732   }
733   return nullptr;
734 }
735 
736 namespace {
737 
738 struct BitGroupCheck {
739   // If the Cmp, checks the bits in the group are nonzero?
740   bool CheckIfSet {false};
741   // The mask that identifies the bitgroup in question.
742   const APInt *Mask {nullptr};
743 };
744 }
745 /// For an ICMP where RHS is zero, we want to check if the ICMP is equivalent to
746 /// comparing a group of bits in an integer value against zero.
747 BitGroupCheck isAnyBitSet(Value *LHS, ICmpInst::Predicate CC) {
748 
749   BitGroupCheck BGC;
750   auto *Inst = dyn_cast<Instruction>(LHS);
751 
752   if (!Inst || Inst->getOpcode() != Instruction::And)
753     return BGC;
754 
755   // TODO Currently this does not work for vectors.
756   ConstantInt *Mask;
757   if (!match(LHS, m_And(m_Value(), m_ConstantInt(Mask))))
758     return BGC;
759   // At this point we know that LHS of ICMP is "and" of a value with a constant.
760   // Also we know that the RHS is zero. That means we are checking if a certain
761   // group of bits in a given integer value are all zero or at least one of them
762   // is set to one.
763   if (CC == ICmpInst::ICMP_EQ)
764     BGC.CheckIfSet = false;
765   else if (CC == ICmpInst::ICMP_NE)
766     BGC.CheckIfSet = true;
767   else
768     return BGC;
769 
770   BGC.Mask = &Mask->getValue();
771   return BGC;
772 }
773 
774 /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
775 /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
776 /// If \p Inverted is true then the check is for the inverted range, e.g.
777 /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
778 Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
779                                         bool Inverted) {
780   // Check the lower range comparison, e.g. x >= 0
781   // InstCombine already ensured that if there is a constant it's on the RHS.
782   ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
783   if (!RangeStart)
784     return nullptr;
785 
786   ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
787                                Cmp0->getPredicate());
788 
789   // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
790   if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
791         (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
792     return nullptr;
793 
794   ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
795                                Cmp1->getPredicate());
796 
797   Value *Input = Cmp0->getOperand(0);
798   Value *RangeEnd;
799   if (Cmp1->getOperand(0) == Input) {
800     // For the upper range compare we have: icmp x, n
801     RangeEnd = Cmp1->getOperand(1);
802   } else if (Cmp1->getOperand(1) == Input) {
803     // For the upper range compare we have: icmp n, x
804     RangeEnd = Cmp1->getOperand(0);
805     Pred1 = ICmpInst::getSwappedPredicate(Pred1);
806   } else {
807     return nullptr;
808   }
809 
810   // Check the upper range comparison, e.g. x < n
811   ICmpInst::Predicate NewPred;
812   switch (Pred1) {
813     case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
814     case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
815     default: return nullptr;
816   }
817 
818   // This simplification is only valid if the upper range is not negative.
819   bool IsNegative, IsNotNegative;
820   ComputeSignBit(RangeEnd, IsNotNegative, IsNegative, /*Depth=*/0, Cmp1);
821   if (!IsNotNegative)
822     return nullptr;
823 
824   if (Inverted)
825     NewPred = ICmpInst::getInversePredicate(NewPred);
826 
827   return Builder->CreateICmp(NewPred, Input, RangeEnd);
828 }
829 
830 Value *InstCombiner::FoldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
831 
832   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
833   // TODO The lines below does not work for vectors. ConstantInt is scalar.
834   auto *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
835   auto *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
836   if (!LHSCst || !RHSCst)
837     return nullptr;
838   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
839 
840   // E.g. (icmp ne %x, 0) ^ (icmp ne %y, 0) => icmp ne %x, %y if the following
841   // conditions hold:
842   // 1- (%x = and %a, %mask) and (%y = and %b, %mask)
843   // 2- %mask is a power of 2.
844   if (RHSCst->isZero() && LHSCst == RHSCst) {
845 
846     BitGroupCheck BGC1 = isAnyBitSet(Val, LHSCC);
847     BitGroupCheck BGC2 = isAnyBitSet(Val2, RHSCC);
848     if (BGC1.Mask && BGC2.Mask && BGC1.CheckIfSet == BGC2.CheckIfSet &&
849         *BGC1.Mask == *BGC2.Mask && BGC1.Mask->isPowerOf2()) {
850       return Builder->CreateICmp(ICmpInst::ICMP_NE, Val2, Val);
851     }
852   }
853   return nullptr;
854 }
855 
856 /// Fold (icmp)&(icmp) if possible.
857 Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
858   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
859 
860   // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
861   if (PredicatesFoldable(LHSCC, RHSCC)) {
862     if (LHS->getOperand(0) == RHS->getOperand(1) &&
863         LHS->getOperand(1) == RHS->getOperand(0))
864       LHS->swapOperands();
865     if (LHS->getOperand(0) == RHS->getOperand(0) &&
866         LHS->getOperand(1) == RHS->getOperand(1)) {
867       Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
868       unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
869       bool isSigned = LHS->isSigned() || RHS->isSigned();
870       return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
871     }
872   }
873 
874   // handle (roughly):  (icmp eq (A & B), C) & (icmp eq (A & D), E)
875   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
876     return V;
877 
878   // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
879   if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false))
880     return V;
881 
882   // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
883   if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
884     return V;
885 
886   // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
887   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
888   ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
889   ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
890   if (!LHSCst || !RHSCst) return nullptr;
891 
892   if (LHSCst == RHSCst && LHSCC == RHSCC) {
893     // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
894     // where C is a power of 2 or
895     // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
896     if ((LHSCC == ICmpInst::ICMP_ULT && LHSCst->getValue().isPowerOf2()) ||
897         (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero())) {
898       Value *NewOr = Builder->CreateOr(Val, Val2);
899       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
900     }
901   }
902 
903   // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
904   // where CMAX is the all ones value for the truncated type,
905   // iff the lower bits of C2 and CA are zero.
906   if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
907       LHS->hasOneUse() && RHS->hasOneUse()) {
908     Value *V;
909     ConstantInt *AndCst, *SmallCst = nullptr, *BigCst = nullptr;
910 
911     // (trunc x) == C1 & (and x, CA) == C2
912     // (and x, CA) == C2 & (trunc x) == C1
913     if (match(Val2, m_Trunc(m_Value(V))) &&
914         match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
915       SmallCst = RHSCst;
916       BigCst = LHSCst;
917     } else if (match(Val, m_Trunc(m_Value(V))) &&
918                match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
919       SmallCst = LHSCst;
920       BigCst = RHSCst;
921     }
922 
923     if (SmallCst && BigCst) {
924       unsigned BigBitSize = BigCst->getType()->getBitWidth();
925       unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
926 
927       // Check that the low bits are zero.
928       APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
929       if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
930         Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
931         APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
932         Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
933         return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
934       }
935     }
936   }
937 
938   // E.g. (icmp eq %x, 0) & (icmp ne %y, 0) => icmp ult %x, %y if the following
939   // conditions hold:
940   // 1- (%x = and %a, %mask1) and (%y = and %b, %mask2)
941   // 2- Let %t be the smallest power of 2 where %mask1 & %t != 0. Then for any
942   //    %s that is a power of 2 and %s & %mask2 != 0, we must have %s <= %t.
943   // For example if %mask1 = 24 and %mask2 = 16, setting %s = 16 and %t = 8
944   // violates condition (2) above. So this optimization cannot be applied.
945   if (RHSCst->isZero() && LHSCst == RHSCst) {
946     BitGroupCheck BGC1 = isAnyBitSet(Val, LHSCC);
947     BitGroupCheck BGC2 = isAnyBitSet(Val2, RHSCC);
948 
949     if (BGC1.Mask && BGC2.Mask && (BGC1.CheckIfSet != BGC2.CheckIfSet)) {
950       if (!BGC1.CheckIfSet &&
951           BGC1.Mask->countTrailingZeros() >=
952           BGC2.Mask->getBitWidth() - BGC2.Mask->countLeadingZeros() - 1)
953         return Builder->CreateICmp(ICmpInst::ICMP_ULT, Val, Val2);
954       else if (!BGC2.CheckIfSet &&
955           BGC2.Mask->countTrailingZeros() >=
956           BGC1.Mask->getBitWidth() - BGC1.Mask->countLeadingZeros() - 1)
957         return Builder->CreateICmp(ICmpInst::ICMP_ULT, Val2, Val);
958     }
959   }
960 
961   // From here on, we only handle:
962   //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
963   if (Val != Val2) return nullptr;
964 
965   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
966   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
967       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
968       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
969       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
970     return nullptr;
971 
972   // We can't fold (ugt x, C) & (sgt x, C2).
973   if (!PredicatesFoldable(LHSCC, RHSCC))
974     return nullptr;
975 
976   // Ensure that the larger constant is on the RHS.
977   bool ShouldSwap;
978   if (CmpInst::isSigned(LHSCC) ||
979       (ICmpInst::isEquality(LHSCC) &&
980        CmpInst::isSigned(RHSCC)))
981     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
982   else
983     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
984 
985   if (ShouldSwap) {
986     std::swap(LHS, RHS);
987     std::swap(LHSCst, RHSCst);
988     std::swap(LHSCC, RHSCC);
989   }
990 
991   // At this point, we know we have two icmp instructions
992   // comparing a value against two constants and and'ing the result
993   // together.  Because of the above check, we know that we only have
994   // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
995   // (from the icmp folding check above), that the two constants
996   // are not equal and that the larger constant is on the RHS
997   assert(LHSCst != RHSCst && "Compares not folded above?");
998 
999   switch (LHSCC) {
1000   default: llvm_unreachable("Unknown integer condition code!");
1001   case ICmpInst::ICMP_EQ:
1002     switch (RHSCC) {
1003     default: llvm_unreachable("Unknown integer condition code!");
1004     case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13
1005     case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13
1006     case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13
1007       return LHS;
1008     }
1009   case ICmpInst::ICMP_NE:
1010     switch (RHSCC) {
1011     default: llvm_unreachable("Unknown integer condition code!");
1012     case ICmpInst::ICMP_ULT:
1013       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
1014         return Builder->CreateICmpULT(Val, LHSCst);
1015       if (LHSCst->isNullValue())    // (X !=  0 & X u< 14) -> X-1 u< 13
1016         return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
1017                                false, true);
1018       break;                        // (X != 13 & X u< 15) -> no change
1019     case ICmpInst::ICMP_SLT:
1020       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
1021         return Builder->CreateICmpSLT(Val, LHSCst);
1022       break;                        // (X != 13 & X s< 15) -> no change
1023     case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15
1024     case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15
1025     case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15
1026       return RHS;
1027     case ICmpInst::ICMP_NE:
1028       // Special case to get the ordering right when the values wrap around
1029       // zero.
1030       if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue())
1031         std::swap(LHSCst, RHSCst);
1032       if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
1033         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1034         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1035         return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1),
1036                                       Val->getName()+".cmp");
1037       }
1038       break;                        // (X != 13 & X != 15) -> no change
1039     }
1040     break;
1041   case ICmpInst::ICMP_ULT:
1042     switch (RHSCC) {
1043     default: llvm_unreachable("Unknown integer condition code!");
1044     case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false
1045     case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false
1046       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
1047     case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change
1048       break;
1049     case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13
1050     case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13
1051       return LHS;
1052     case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change
1053       break;
1054     }
1055     break;
1056   case ICmpInst::ICMP_SLT:
1057     switch (RHSCC) {
1058     default: llvm_unreachable("Unknown integer condition code!");
1059     case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change
1060       break;
1061     case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13
1062     case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13
1063       return LHS;
1064     case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change
1065       break;
1066     }
1067     break;
1068   case ICmpInst::ICMP_UGT:
1069     switch (RHSCC) {
1070     default: llvm_unreachable("Unknown integer condition code!");
1071     case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X == 15
1072     case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15
1073       return RHS;
1074     case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change
1075       break;
1076     case ICmpInst::ICMP_NE:
1077       if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
1078         return Builder->CreateICmp(LHSCC, Val, RHSCst);
1079       break;                        // (X u> 13 & X != 15) -> no change
1080     case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) -> (X-14) <u 1
1081       return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
1082                              false, true);
1083     case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change
1084       break;
1085     }
1086     break;
1087   case ICmpInst::ICMP_SGT:
1088     switch (RHSCC) {
1089     default: llvm_unreachable("Unknown integer condition code!");
1090     case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
1091     case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
1092       return RHS;
1093     case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
1094       break;
1095     case ICmpInst::ICMP_NE:
1096       if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
1097         return Builder->CreateICmp(LHSCC, Val, RHSCst);
1098       break;                        // (X s> 13 & X != 15) -> no change
1099     case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) -> (X-14) s< 1
1100       return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
1101                              true, true);
1102     case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change
1103       break;
1104     }
1105     break;
1106   }
1107 
1108   return nullptr;
1109 }
1110 
1111 /// Optimize (fcmp)&(fcmp).  NOTE: Unlike the rest of instcombine, this returns
1112 /// a Value which should already be inserted into the function.
1113 Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
1114   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1115   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1116   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1117 
1118   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1119     // Swap RHS operands to match LHS.
1120     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1121     std::swap(Op1LHS, Op1RHS);
1122   }
1123 
1124   // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1125   // Suppose the relation between x and y is R, where R is one of
1126   // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
1127   // testing the desired relations.
1128   //
1129   // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1130   //    bool(R & CC0) && bool(R & CC1)
1131   //  = bool((R & CC0) & (R & CC1))
1132   //  = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
1133   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1134     return getFCmpValue(getFCmpCode(Op0CC) & getFCmpCode(Op1CC), Op0LHS, Op0RHS,
1135                         Builder);
1136 
1137   if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
1138       RHS->getPredicate() == FCmpInst::FCMP_ORD) {
1139     if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
1140       return nullptr;
1141 
1142     // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
1143     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1144       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1145         // If either of the constants are nans, then the whole thing returns
1146         // false.
1147         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1148           return Builder->getFalse();
1149         return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1150       }
1151 
1152     // Handle vector zeros.  This occurs because the canonical form of
1153     // "fcmp ord x,x" is "fcmp ord x, 0".
1154     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1155         isa<ConstantAggregateZero>(RHS->getOperand(1)))
1156       return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1157     return nullptr;
1158   }
1159 
1160   return nullptr;
1161 }
1162 
1163 /// Match De Morgan's Laws:
1164 /// (~A & ~B) == (~(A | B))
1165 /// (~A | ~B) == (~(A & B))
1166 static Instruction *matchDeMorgansLaws(BinaryOperator &I,
1167                                        InstCombiner::BuilderTy *Builder) {
1168   auto Opcode = I.getOpcode();
1169   assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1170          "Trying to match De Morgan's Laws with something other than and/or");
1171   // Flip the logic operation.
1172   if (Opcode == Instruction::And)
1173     Opcode = Instruction::Or;
1174   else
1175     Opcode = Instruction::And;
1176 
1177   Value *Op0 = I.getOperand(0);
1178   Value *Op1 = I.getOperand(1);
1179   // TODO: Use pattern matchers instead of dyn_cast.
1180   if (Value *Op0NotVal = dyn_castNotVal(Op0))
1181     if (Value *Op1NotVal = dyn_castNotVal(Op1))
1182       if (Op0->hasOneUse() && Op1->hasOneUse()) {
1183         Value *LogicOp = Builder->CreateBinOp(Opcode, Op0NotVal, Op1NotVal,
1184                                               I.getName() + ".demorgan");
1185         return BinaryOperator::CreateNot(LogicOp);
1186       }
1187 
1188   return nullptr;
1189 }
1190 
1191 bool InstCombiner::shouldOptimizeCast(CastInst *CI) {
1192   Value *CastSrc = CI->getOperand(0);
1193 
1194   // Noop casts and casts of constants should be eliminated trivially.
1195   if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1196     return false;
1197 
1198   // If this cast is paired with another cast that can be eliminated, we prefer
1199   // to have it eliminated.
1200   if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1201     if (isEliminableCastPair(PrecedingCI, CI))
1202       return false;
1203 
1204   // If this is a vector sext from a compare, then we don't want to break the
1205   // idiom where each element of the extended vector is either zero or all ones.
1206   if (CI->getOpcode() == Instruction::SExt &&
1207       isa<CmpInst>(CastSrc) && CI->getDestTy()->isVectorTy())
1208     return false;
1209 
1210   return true;
1211 }
1212 
1213 /// Fold {and,or,xor} (cast X), C.
1214 static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1215                                           InstCombiner::BuilderTy *Builder) {
1216   Constant *C;
1217   if (!match(Logic.getOperand(1), m_Constant(C)))
1218     return nullptr;
1219 
1220   auto LogicOpc = Logic.getOpcode();
1221   Type *DestTy = Logic.getType();
1222   Type *SrcTy = Cast->getSrcTy();
1223 
1224   // If the first operand is bitcast, move the logic operation ahead of the
1225   // bitcast (do the logic operation in the original type). This can eliminate
1226   // bitcasts and allow combines that would otherwise be impeded by the bitcast.
1227   Value *X;
1228   if (match(Cast, m_BitCast(m_Value(X)))) {
1229     Value *NewConstant = ConstantExpr::getBitCast(C, SrcTy);
1230     Value *NewOp = Builder->CreateBinOp(LogicOpc, X, NewConstant);
1231     return CastInst::CreateBitOrPointerCast(NewOp, DestTy);
1232   }
1233 
1234   // Similarly, move the logic operation ahead of a zext if the constant is
1235   // unchanged in the smaller source type. Performing the logic in a smaller
1236   // type may provide more information to later folds, and the smaller logic
1237   // instruction may be cheaper (particularly in the case of vectors).
1238   if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1239     Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1240     Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1241     if (ZextTruncC == C) {
1242       // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1243       Value *NewOp = Builder->CreateBinOp(LogicOpc, X, TruncC);
1244       return new ZExtInst(NewOp, DestTy);
1245     }
1246   }
1247 
1248   return nullptr;
1249 }
1250 
1251 /// Fold {and,or,xor} (cast X), Y.
1252 Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) {
1253   auto LogicOpc = I.getOpcode();
1254   assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
1255 
1256   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1257   CastInst *Cast0 = dyn_cast<CastInst>(Op0);
1258   if (!Cast0)
1259     return nullptr;
1260 
1261   // This must be a cast from an integer or integer vector source type to allow
1262   // transformation of the logic operation to the source type.
1263   Type *DestTy = I.getType();
1264   Type *SrcTy = Cast0->getSrcTy();
1265   if (!SrcTy->isIntOrIntVectorTy())
1266     return nullptr;
1267 
1268   if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1269     return Ret;
1270 
1271   CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1272   if (!Cast1)
1273     return nullptr;
1274 
1275   // Both operands of the logic operation are casts. The casts must be of the
1276   // same type for reduction.
1277   auto CastOpcode = Cast0->getOpcode();
1278   if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy())
1279     return nullptr;
1280 
1281   Value *Cast0Src = Cast0->getOperand(0);
1282   Value *Cast1Src = Cast1->getOperand(0);
1283 
1284   // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
1285   if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1286     Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1287                                         I.getName());
1288     return CastInst::Create(CastOpcode, NewOp, DestTy);
1289   }
1290 
1291   // For now, only 'and'/'or' have optimizations after this.
1292   if (LogicOpc == Instruction::Xor)
1293     return nullptr;
1294 
1295   // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the
1296   // cast is otherwise not optimizable.  This happens for vector sexts.
1297   ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src);
1298   ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src);
1299   if (ICmp0 && ICmp1) {
1300     Value *Res = LogicOpc == Instruction::And ? FoldAndOfICmps(ICmp0, ICmp1)
1301                                               : FoldOrOfICmps(ICmp0, ICmp1, &I);
1302     if (Res)
1303       return CastInst::Create(CastOpcode, Res, DestTy);
1304     return nullptr;
1305   }
1306 
1307   // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the
1308   // cast is otherwise not optimizable.  This happens for vector sexts.
1309   FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src);
1310   FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src);
1311   if (FCmp0 && FCmp1) {
1312     Value *Res = LogicOpc == Instruction::And ? FoldAndOfFCmps(FCmp0, FCmp1)
1313                                               : FoldOrOfFCmps(FCmp0, FCmp1);
1314     if (Res)
1315       return CastInst::Create(CastOpcode, Res, DestTy);
1316     return nullptr;
1317   }
1318 
1319   return nullptr;
1320 }
1321 
1322 static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) {
1323   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1324 
1325   // Canonicalize SExt or Not to the LHS
1326   if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) {
1327     std::swap(Op0, Op1);
1328   }
1329 
1330   // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1331   Value *X = nullptr;
1332   if (match(Op0, m_SExt(m_Value(X))) &&
1333       X->getType()->getScalarType()->isIntegerTy(1)) {
1334     Value *Zero = Constant::getNullValue(Op1->getType());
1335     return SelectInst::Create(X, Op1, Zero);
1336   }
1337 
1338   // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1339   if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1340       X->getType()->getScalarType()->isIntegerTy(1)) {
1341     Value *Zero = Constant::getNullValue(Op0->getType());
1342     return SelectInst::Create(X, Zero, Op1);
1343   }
1344 
1345   return nullptr;
1346 }
1347 
1348 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
1349 // here. We should standardize that construct where it is needed or choose some
1350 // other way to ensure that commutated variants of patterns are not missed.
1351 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
1352   bool Changed = SimplifyAssociativeOrCommutative(I);
1353   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1354 
1355   if (Value *V = SimplifyVectorOp(I))
1356     return replaceInstUsesWith(I, V);
1357 
1358   if (Value *V = SimplifyAndInst(Op0, Op1, DL, &TLI, &DT, &AC))
1359     return replaceInstUsesWith(I, V);
1360 
1361   // (A|B)&(A|C) -> A|(B&C) etc
1362   if (Value *V = SimplifyUsingDistributiveLaws(I))
1363     return replaceInstUsesWith(I, V);
1364 
1365   // See if we can simplify any instructions used by the instruction whose sole
1366   // purpose is to compute bits we don't care about.
1367   if (SimplifyDemandedInstructionBits(I))
1368     return &I;
1369 
1370   if (Value *V = SimplifyBSwap(I))
1371     return replaceInstUsesWith(I, V);
1372 
1373   if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1374     const APInt &AndRHSMask = AndRHS->getValue();
1375 
1376     // Optimize a variety of ((val OP C1) & C2) combinations...
1377     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1378       Value *Op0LHS = Op0I->getOperand(0);
1379       Value *Op0RHS = Op0I->getOperand(1);
1380       switch (Op0I->getOpcode()) {
1381       default: break;
1382       case Instruction::Xor:
1383       case Instruction::Or: {
1384         // If the mask is only needed on one incoming arm, push it up.
1385         if (!Op0I->hasOneUse()) break;
1386 
1387         APInt NotAndRHS(~AndRHSMask);
1388         if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) {
1389           // Not masking anything out for the LHS, move to RHS.
1390           Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1391                                              Op0RHS->getName()+".masked");
1392           return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1393         }
1394         if (!isa<Constant>(Op0RHS) &&
1395             MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) {
1396           // Not masking anything out for the RHS, move to LHS.
1397           Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1398                                              Op0LHS->getName()+".masked");
1399           return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1400         }
1401 
1402         break;
1403       }
1404       case Instruction::Add:
1405         // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
1406         // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1407         // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1408         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
1409           return BinaryOperator::CreateAnd(V, AndRHS);
1410         if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
1411           return BinaryOperator::CreateAnd(V, AndRHS);  // Add commutes
1412         break;
1413 
1414       case Instruction::Sub:
1415         // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
1416         // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1417         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1418         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
1419           return BinaryOperator::CreateAnd(V, AndRHS);
1420 
1421         // -x & 1 -> x & 1
1422         if (AndRHSMask == 1 && match(Op0LHS, m_Zero()))
1423           return BinaryOperator::CreateAnd(Op0RHS, AndRHS);
1424 
1425         // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
1426         // has 1's for all bits that the subtraction with A might affect.
1427         if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
1428           uint32_t BitWidth = AndRHSMask.getBitWidth();
1429           uint32_t Zeros = AndRHSMask.countLeadingZeros();
1430           APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
1431 
1432           if (MaskedValueIsZero(Op0LHS, Mask, 0, &I)) {
1433             Value *NewNeg = Builder->CreateNeg(Op0RHS);
1434             return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1435           }
1436         }
1437         break;
1438 
1439       case Instruction::Shl:
1440       case Instruction::LShr:
1441         // (1 << x) & 1 --> zext(x == 0)
1442         // (1 >> x) & 1 --> zext(x == 0)
1443         if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1444           Value *NewICmp =
1445             Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1446           return new ZExtInst(NewICmp, I.getType());
1447         }
1448         break;
1449       }
1450 
1451       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1452         if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1453           return Res;
1454     }
1455 
1456     // If this is an integer truncation, and if the source is an 'and' with
1457     // immediate, transform it.  This frequently occurs for bitfield accesses.
1458     {
1459       Value *X = nullptr; ConstantInt *YC = nullptr;
1460       if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1461         // Change: and (trunc (and X, YC) to T), C2
1462         // into  : and (trunc X to T), trunc(YC) & C2
1463         // This will fold the two constants together, which may allow
1464         // other simplifications.
1465         Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1466         Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1467         C3 = ConstantExpr::getAnd(C3, AndRHS);
1468         return BinaryOperator::CreateAnd(NewCast, C3);
1469       }
1470     }
1471 
1472     // Try to fold constant and into select arguments.
1473     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1474       if (Instruction *R = FoldOpIntoSelect(I, SI))
1475         return R;
1476     if (isa<PHINode>(Op0))
1477       if (Instruction *NV = FoldOpIntoPhi(I))
1478         return NV;
1479   }
1480 
1481   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
1482     return DeMorgan;
1483 
1484   {
1485     Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
1486     // (A|B) & ~(A&B) -> A^B
1487     if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1488         match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1489         ((A == C && B == D) || (A == D && B == C)))
1490       return BinaryOperator::CreateXor(A, B);
1491 
1492     // ~(A&B) & (A|B) -> A^B
1493     if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1494         match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1495         ((A == C && B == D) || (A == D && B == C)))
1496       return BinaryOperator::CreateXor(A, B);
1497 
1498     // A&(A^B) => A & ~B
1499     {
1500       Value *tmpOp0 = Op0;
1501       Value *tmpOp1 = Op1;
1502       if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
1503         if (A == Op1 || B == Op1 ) {
1504           tmpOp1 = Op0;
1505           tmpOp0 = Op1;
1506           // Simplify below
1507         }
1508       }
1509 
1510       if (match(tmpOp1, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
1511         if (B == tmpOp0) {
1512           std::swap(A, B);
1513         }
1514         // Notice that the pattern (A&(~B)) is actually (A&(-1^B)), so if
1515         // A is originally -1 (or a vector of -1 and undefs), then we enter
1516         // an endless loop. By checking that A is non-constant we ensure that
1517         // we will never get to the loop.
1518         if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
1519           return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
1520       }
1521     }
1522 
1523     // (A&((~A)|B)) -> A&B
1524     if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1525         match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1526       return BinaryOperator::CreateAnd(A, Op1);
1527     if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1528         match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1529       return BinaryOperator::CreateAnd(A, Op0);
1530 
1531     // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
1532     if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
1533       if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
1534         if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
1535           return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C));
1536 
1537     // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
1538     if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
1539       if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
1540         if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
1541           return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C));
1542 
1543     // (A | B) & ((~A) ^ B) -> (A & B)
1544     if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1545         match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B))))
1546       return BinaryOperator::CreateAnd(A, B);
1547 
1548     // ((~A) ^ B) & (A | B) -> (A & B)
1549     // ((~A) ^ B) & (B | A) -> (A & B)
1550     if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
1551         match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
1552       return BinaryOperator::CreateAnd(A, B);
1553   }
1554 
1555   {
1556     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
1557     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
1558     if (LHS && RHS)
1559       if (Value *Res = FoldAndOfICmps(LHS, RHS))
1560         return replaceInstUsesWith(I, Res);
1561 
1562     // TODO: Make this recursive; it's a little tricky because an arbitrary
1563     // number of 'and' instructions might have to be created.
1564     Value *X, *Y;
1565     if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1566       if (auto *Cmp = dyn_cast<ICmpInst>(X))
1567         if (Value *Res = FoldAndOfICmps(LHS, Cmp))
1568           return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
1569       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1570         if (Value *Res = FoldAndOfICmps(LHS, Cmp))
1571           return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
1572     }
1573     if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1574       if (auto *Cmp = dyn_cast<ICmpInst>(X))
1575         if (Value *Res = FoldAndOfICmps(Cmp, RHS))
1576           return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
1577       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1578         if (Value *Res = FoldAndOfICmps(Cmp, RHS))
1579           return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
1580     }
1581   }
1582 
1583   // If and'ing two fcmp, try combine them into one.
1584   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1585     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
1586       if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1587         return replaceInstUsesWith(I, Res);
1588 
1589   if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1590     return CastedAnd;
1591 
1592   if (Instruction *Select = foldBoolSextMaskToSelect(I))
1593     return Select;
1594 
1595   return Changed ? &I : nullptr;
1596 }
1597 
1598 /// Given an OR instruction, check to see if this is a bswap idiom. If so,
1599 /// insert the new intrinsic and return it.
1600 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
1601   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1602 
1603   // Look through zero extends.
1604   if (Instruction *Ext = dyn_cast<ZExtInst>(Op0))
1605     Op0 = Ext->getOperand(0);
1606 
1607   if (Instruction *Ext = dyn_cast<ZExtInst>(Op1))
1608     Op1 = Ext->getOperand(0);
1609 
1610   // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
1611   bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) ||
1612                  match(Op1, m_Or(m_Value(), m_Value()));
1613 
1614   // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
1615   bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1616                     match(Op1, m_LogicalShift(m_Value(), m_Value()));
1617 
1618   // (A & B) | (C & D)                              -> bswap if possible.
1619   bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) &&
1620                   match(Op1, m_And(m_Value(), m_Value()));
1621 
1622   if (!OrOfOrs && !OrOfShifts && !OrOfAnds)
1623     return nullptr;
1624 
1625   SmallVector<Instruction*, 4> Insts;
1626   if (!recognizeBSwapOrBitReverseIdiom(&I, true, false, Insts))
1627     return nullptr;
1628   Instruction *LastInst = Insts.pop_back_val();
1629   LastInst->removeFromParent();
1630 
1631   for (auto *Inst : Insts)
1632     Worklist.Add(Inst);
1633   return LastInst;
1634 }
1635 
1636 /// If all elements of two constant vectors are 0/-1 and inverses, return true.
1637 static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
1638   unsigned NumElts = C1->getType()->getVectorNumElements();
1639   for (unsigned i = 0; i != NumElts; ++i) {
1640     Constant *EltC1 = C1->getAggregateElement(i);
1641     Constant *EltC2 = C2->getAggregateElement(i);
1642     if (!EltC1 || !EltC2)
1643       return false;
1644 
1645     // One element must be all ones, and the other must be all zeros.
1646     // FIXME: Allow undef elements.
1647     if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
1648           (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
1649       return false;
1650   }
1651   return true;
1652 }
1653 
1654 /// We have an expression of the form (A & C) | (B & D). If A is a scalar or
1655 /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
1656 /// B, it can be used as the condition operand of a select instruction.
1657 static Value *getSelectCondition(Value *A, Value *B,
1658                                  InstCombiner::BuilderTy &Builder) {
1659   // If these are scalars or vectors of i1, A can be used directly.
1660   Type *Ty = A->getType();
1661   if (match(A, m_Not(m_Specific(B))) && Ty->getScalarType()->isIntegerTy(1))
1662     return A;
1663 
1664   // If A and B are sign-extended, look through the sexts to find the booleans.
1665   Value *Cond;
1666   if (match(A, m_SExt(m_Value(Cond))) &&
1667       Cond->getType()->getScalarType()->isIntegerTy(1) &&
1668       match(B, m_CombineOr(m_Not(m_SExt(m_Specific(Cond))),
1669                            m_SExt(m_Not(m_Specific(Cond))))))
1670     return Cond;
1671 
1672   // All scalar (and most vector) possibilities should be handled now.
1673   // Try more matches that only apply to non-splat constant vectors.
1674   if (!Ty->isVectorTy())
1675     return nullptr;
1676 
1677   // If both operands are constants, see if the constants are inverse bitmasks.
1678   Constant *AC, *BC;
1679   if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) &&
1680       areInverseVectorBitmasks(AC, BC))
1681     return ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1682 
1683   // If both operands are xor'd with constants using the same sexted boolean
1684   // operand, see if the constants are inverse bitmasks.
1685   if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AC)))) &&
1686       match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BC)))) &&
1687       Cond->getType()->getScalarType()->isIntegerTy(1) &&
1688       areInverseVectorBitmasks(AC, BC)) {
1689     AC = ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1690     return Builder.CreateXor(Cond, AC);
1691   }
1692   return nullptr;
1693 }
1694 
1695 /// We have an expression of the form (A & C) | (B & D). Try to simplify this
1696 /// to "A' ? C : D", where A' is a boolean or vector of booleans.
1697 static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D,
1698                                    InstCombiner::BuilderTy &Builder) {
1699   // The potential condition of the select may be bitcasted. In that case, look
1700   // through its bitcast and the corresponding bitcast of the 'not' condition.
1701   Type *OrigType = A->getType();
1702   Value *SrcA, *SrcB;
1703   if (match(A, m_OneUse(m_BitCast(m_Value(SrcA)))) &&
1704       match(B, m_OneUse(m_BitCast(m_Value(SrcB))))) {
1705     A = SrcA;
1706     B = SrcB;
1707   }
1708 
1709   if (Value *Cond = getSelectCondition(A, B, Builder)) {
1710     // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
1711     // The bitcasts will either all exist or all not exist. The builder will
1712     // not create unnecessary casts if the types already match.
1713     Value *BitcastC = Builder.CreateBitCast(C, A->getType());
1714     Value *BitcastD = Builder.CreateBitCast(D, A->getType());
1715     Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
1716     return Builder.CreateBitCast(Select, OrigType);
1717   }
1718 
1719   return nullptr;
1720 }
1721 
1722 /// Fold (icmp)|(icmp) if possible.
1723 Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
1724                                    Instruction *CxtI) {
1725   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1726 
1727   // Fold (iszero(A & K1) | iszero(A & K2)) ->  (A & (K1 | K2)) != (K1 | K2)
1728   // if K1 and K2 are a one-bit mask.
1729   ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1730   ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1731 
1732   if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero() &&
1733       RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1734 
1735     BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0));
1736     BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0));
1737     if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
1738         LAnd->getOpcode() == Instruction::And &&
1739         RAnd->getOpcode() == Instruction::And) {
1740 
1741       Value *Mask = nullptr;
1742       Value *Masked = nullptr;
1743       if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
1744           isKnownToBeAPowerOfTwo(LAnd->getOperand(1), DL, false, 0, &AC, CxtI,
1745                                  &DT) &&
1746           isKnownToBeAPowerOfTwo(RAnd->getOperand(1), DL, false, 0, &AC, CxtI,
1747                                  &DT)) {
1748         Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
1749         Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
1750       } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
1751                  isKnownToBeAPowerOfTwo(LAnd->getOperand(0), DL, false, 0, &AC,
1752                                         CxtI, &DT) &&
1753                  isKnownToBeAPowerOfTwo(RAnd->getOperand(0), DL, false, 0, &AC,
1754                                         CxtI, &DT)) {
1755         Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
1756         Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
1757       }
1758 
1759       if (Masked)
1760         return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
1761     }
1762   }
1763 
1764   // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3)
1765   //                   -->  (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3)
1766   // The original condition actually refers to the following two ranges:
1767   // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3]
1768   // We can fold these two ranges if:
1769   // 1) C1 and C2 is unsigned greater than C3.
1770   // 2) The two ranges are separated.
1771   // 3) C1 ^ C2 is one-bit mask.
1772   // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask.
1773   // This implies all values in the two ranges differ by exactly one bit.
1774 
1775   if ((LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_ULE) &&
1776       LHSCC == RHSCC && LHSCst && RHSCst && LHS->hasOneUse() &&
1777       RHS->hasOneUse() && LHSCst->getType() == RHSCst->getType() &&
1778       LHSCst->getValue() == (RHSCst->getValue())) {
1779 
1780     Value *LAdd = LHS->getOperand(0);
1781     Value *RAdd = RHS->getOperand(0);
1782 
1783     Value *LAddOpnd, *RAddOpnd;
1784     ConstantInt *LAddCst, *RAddCst;
1785     if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddCst))) &&
1786         match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddCst))) &&
1787         LAddCst->getValue().ugt(LHSCst->getValue()) &&
1788         RAddCst->getValue().ugt(LHSCst->getValue())) {
1789 
1790       APInt DiffCst = LAddCst->getValue() ^ RAddCst->getValue();
1791       if (LAddOpnd == RAddOpnd && DiffCst.isPowerOf2()) {
1792         ConstantInt *MaxAddCst = nullptr;
1793         if (LAddCst->getValue().ult(RAddCst->getValue()))
1794           MaxAddCst = RAddCst;
1795         else
1796           MaxAddCst = LAddCst;
1797 
1798         APInt RRangeLow = -RAddCst->getValue();
1799         APInt RRangeHigh = RRangeLow + LHSCst->getValue();
1800         APInt LRangeLow = -LAddCst->getValue();
1801         APInt LRangeHigh = LRangeLow + LHSCst->getValue();
1802         APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1803         APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1804         APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow
1805                                                    : RRangeLow - LRangeLow;
1806 
1807         if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff &&
1808             RangeDiff.ugt(LHSCst->getValue())) {
1809           Value *MaskCst = ConstantInt::get(LAddCst->getType(), ~DiffCst);
1810 
1811           Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskCst);
1812           Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddCst);
1813           return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSCst));
1814         }
1815       }
1816     }
1817   }
1818 
1819   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1820   if (PredicatesFoldable(LHSCC, RHSCC)) {
1821     if (LHS->getOperand(0) == RHS->getOperand(1) &&
1822         LHS->getOperand(1) == RHS->getOperand(0))
1823       LHS->swapOperands();
1824     if (LHS->getOperand(0) == RHS->getOperand(0) &&
1825         LHS->getOperand(1) == RHS->getOperand(1)) {
1826       Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1827       unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1828       bool isSigned = LHS->isSigned() || RHS->isSigned();
1829       return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
1830     }
1831   }
1832 
1833   // handle (roughly):
1834   // (icmp ne (A & B), C) | (icmp ne (A & D), E)
1835   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
1836     return V;
1837 
1838   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1839   if (LHS->hasOneUse() || RHS->hasOneUse()) {
1840     // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1841     // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
1842     Value *A = nullptr, *B = nullptr;
1843     if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) {
1844       B = Val;
1845       if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1))
1846         A = Val2;
1847       else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2)
1848         A = RHS->getOperand(1);
1849     }
1850     // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1851     // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
1852     else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1853       B = Val2;
1854       if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1))
1855         A = Val;
1856       else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val)
1857         A = LHS->getOperand(1);
1858     }
1859     if (A && B)
1860       return Builder->CreateICmp(
1861           ICmpInst::ICMP_UGE,
1862           Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1863   }
1864 
1865   // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
1866   if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true))
1867     return V;
1868 
1869   // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
1870   if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
1871     return V;
1872 
1873   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
1874   if (!LHSCst || !RHSCst) return nullptr;
1875 
1876   if (LHSCst == RHSCst && LHSCC == RHSCC) {
1877     // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1878     if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1879       Value *NewOr = Builder->CreateOr(Val, Val2);
1880       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1881     }
1882   }
1883 
1884   // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
1885   //   iff C2 + CA == C1.
1886   if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
1887     ConstantInt *AddCst;
1888     if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1889       if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
1890         return Builder->CreateICmpULE(Val, LHSCst);
1891   }
1892 
1893   // From here on, we only handle:
1894   //    (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1895   if (Val != Val2) return nullptr;
1896 
1897   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1898   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1899       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1900       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1901       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1902     return nullptr;
1903 
1904   // We can't fold (ugt x, C) | (sgt x, C2).
1905   if (!PredicatesFoldable(LHSCC, RHSCC))
1906     return nullptr;
1907 
1908   // Ensure that the larger constant is on the RHS.
1909   bool ShouldSwap;
1910   if (CmpInst::isSigned(LHSCC) ||
1911       (ICmpInst::isEquality(LHSCC) &&
1912        CmpInst::isSigned(RHSCC)))
1913     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1914   else
1915     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
1916 
1917   if (ShouldSwap) {
1918     std::swap(LHS, RHS);
1919     std::swap(LHSCst, RHSCst);
1920     std::swap(LHSCC, RHSCC);
1921   }
1922 
1923   // At this point, we know we have two icmp instructions
1924   // comparing a value against two constants and or'ing the result
1925   // together.  Because of the above check, we know that we only have
1926   // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1927   // icmp folding check above), that the two constants are not
1928   // equal.
1929   assert(LHSCst != RHSCst && "Compares not folded above?");
1930 
1931   switch (LHSCC) {
1932   default: llvm_unreachable("Unknown integer condition code!");
1933   case ICmpInst::ICMP_EQ:
1934     switch (RHSCC) {
1935     default: llvm_unreachable("Unknown integer condition code!");
1936     case ICmpInst::ICMP_EQ:
1937       if (LHS->getOperand(0) == RHS->getOperand(0)) {
1938         // if LHSCst and RHSCst differ only by one bit:
1939         // (A == C1 || A == C2) -> (A | (C1 ^ C2)) == C2
1940         assert(LHSCst->getValue().ule(LHSCst->getValue()));
1941 
1942         APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1943         if (Xor.isPowerOf2()) {
1944           Value *Cst = Builder->getInt(Xor);
1945           Value *Or = Builder->CreateOr(LHS->getOperand(0), Cst);
1946           return Builder->CreateICmp(ICmpInst::ICMP_EQ, Or, RHSCst);
1947         }
1948       }
1949 
1950       if (LHSCst == SubOne(RHSCst)) {
1951         // (X == 13 | X == 14) -> X-13 <u 2
1952         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1953         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1954         AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1955         return Builder->CreateICmpULT(Add, AddCST);
1956       }
1957 
1958       break;                         // (X == 13 | X == 15) -> no change
1959     case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change
1960     case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change
1961       break;
1962     case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15
1963     case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15
1964     case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15
1965       return RHS;
1966     }
1967     break;
1968   case ICmpInst::ICMP_NE:
1969     switch (RHSCC) {
1970     default: llvm_unreachable("Unknown integer condition code!");
1971     case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13
1972     case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13
1973     case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13
1974       return LHS;
1975     case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true
1976     case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true
1977     case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true
1978       return Builder->getTrue();
1979     }
1980   case ICmpInst::ICMP_ULT:
1981     switch (RHSCC) {
1982     default: llvm_unreachable("Unknown integer condition code!");
1983     case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
1984       break;
1985     case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) -> (X-13) u> 2
1986       // If RHSCst is [us]MAXINT, it is always false.  Not handling
1987       // this can cause overflow.
1988       if (RHSCst->isMaxValue(false))
1989         return LHS;
1990       return insertRangeTest(Val, LHSCst->getValue(), RHSCst->getValue() + 1,
1991                              false, false);
1992     case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
1993       break;
1994     case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15
1995     case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15
1996       return RHS;
1997     case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change
1998       break;
1999     }
2000     break;
2001   case ICmpInst::ICMP_SLT:
2002     switch (RHSCC) {
2003     default: llvm_unreachable("Unknown integer condition code!");
2004     case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
2005       break;
2006     case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) -> (X-13) s> 2
2007       // If RHSCst is [us]MAXINT, it is always false.  Not handling
2008       // this can cause overflow.
2009       if (RHSCst->isMaxValue(true))
2010         return LHS;
2011       return insertRangeTest(Val, LHSCst->getValue(), RHSCst->getValue() + 1,
2012                              true, false);
2013     case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
2014       break;
2015     case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15
2016     case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15
2017       return RHS;
2018     case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change
2019       break;
2020     }
2021     break;
2022   case ICmpInst::ICMP_UGT:
2023     switch (RHSCC) {
2024     default: llvm_unreachable("Unknown integer condition code!");
2025     case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13
2026     case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13
2027       return LHS;
2028     case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change
2029       break;
2030     case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true
2031     case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true
2032       return Builder->getTrue();
2033     case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change
2034       break;
2035     }
2036     break;
2037   case ICmpInst::ICMP_SGT:
2038     switch (RHSCC) {
2039     default: llvm_unreachable("Unknown integer condition code!");
2040     case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13
2041     case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13
2042       return LHS;
2043     case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change
2044       break;
2045     case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true
2046     case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true
2047       return Builder->getTrue();
2048     case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change
2049       break;
2050     }
2051     break;
2052   }
2053   return nullptr;
2054 }
2055 
2056 /// Optimize (fcmp)|(fcmp).  NOTE: Unlike the rest of instcombine, this returns
2057 /// a Value which should already be inserted into the function.
2058 Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
2059   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
2060   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
2061   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
2062 
2063   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
2064     // Swap RHS operands to match LHS.
2065     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
2066     std::swap(Op1LHS, Op1RHS);
2067   }
2068 
2069   // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
2070   // This is a similar transformation to the one in FoldAndOfFCmps.
2071   //
2072   // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
2073   //    bool(R & CC0) || bool(R & CC1)
2074   //  = bool((R & CC0) | (R & CC1))
2075   //  = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
2076   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
2077     return getFCmpValue(getFCmpCode(Op0CC) | getFCmpCode(Op1CC), Op0LHS, Op0RHS,
2078                         Builder);
2079 
2080   if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
2081       RHS->getPredicate() == FCmpInst::FCMP_UNO &&
2082       LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
2083     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
2084       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
2085         // If either of the constants are nans, then the whole thing returns
2086         // true.
2087         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
2088           return Builder->getTrue();
2089 
2090         // Otherwise, no need to compare the two constants, compare the
2091         // rest.
2092         return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
2093       }
2094 
2095     // Handle vector zeros.  This occurs because the canonical form of
2096     // "fcmp uno x,x" is "fcmp uno x, 0".
2097     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
2098         isa<ConstantAggregateZero>(RHS->getOperand(1)))
2099       return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
2100 
2101     return nullptr;
2102   }
2103 
2104   return nullptr;
2105 }
2106 
2107 /// This helper function folds:
2108 ///
2109 ///     ((A | B) & C1) | (B & C2)
2110 ///
2111 /// into:
2112 ///
2113 ///     (A & C1) | B
2114 ///
2115 /// when the XOR of the two constants is "all ones" (-1).
2116 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
2117                                                Value *A, Value *B, Value *C) {
2118   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
2119   if (!CI1) return nullptr;
2120 
2121   Value *V1 = nullptr;
2122   ConstantInt *CI2 = nullptr;
2123   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr;
2124 
2125   APInt Xor = CI1->getValue() ^ CI2->getValue();
2126   if (!Xor.isAllOnesValue()) return nullptr;
2127 
2128   if (V1 == A || V1 == B) {
2129     Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
2130     return BinaryOperator::CreateOr(NewOp, V1);
2131   }
2132 
2133   return nullptr;
2134 }
2135 
2136 /// \brief This helper function folds:
2137 ///
2138 ///     ((A | B) & C1) ^ (B & C2)
2139 ///
2140 /// into:
2141 ///
2142 ///     (A & C1) ^ B
2143 ///
2144 /// when the XOR of the two constants is "all ones" (-1).
2145 Instruction *InstCombiner::FoldXorWithConstants(BinaryOperator &I, Value *Op,
2146                                                 Value *A, Value *B, Value *C) {
2147   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
2148   if (!CI1)
2149     return nullptr;
2150 
2151   Value *V1 = nullptr;
2152   ConstantInt *CI2 = nullptr;
2153   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2))))
2154     return nullptr;
2155 
2156   APInt Xor = CI1->getValue() ^ CI2->getValue();
2157   if (!Xor.isAllOnesValue())
2158     return nullptr;
2159 
2160   if (V1 == A || V1 == B) {
2161     Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1);
2162     return BinaryOperator::CreateXor(NewOp, V1);
2163   }
2164 
2165   return nullptr;
2166 }
2167 
2168 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2169 // here. We should standardize that construct where it is needed or choose some
2170 // other way to ensure that commutated variants of patterns are not missed.
2171 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
2172   bool Changed = SimplifyAssociativeOrCommutative(I);
2173   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2174 
2175   if (Value *V = SimplifyVectorOp(I))
2176     return replaceInstUsesWith(I, V);
2177 
2178   if (Value *V = SimplifyOrInst(Op0, Op1, DL, &TLI, &DT, &AC))
2179     return replaceInstUsesWith(I, V);
2180 
2181   // (A&B)|(A&C) -> A&(B|C) etc
2182   if (Value *V = SimplifyUsingDistributiveLaws(I))
2183     return replaceInstUsesWith(I, V);
2184 
2185   // See if we can simplify any instructions used by the instruction whose sole
2186   // purpose is to compute bits we don't care about.
2187   if (SimplifyDemandedInstructionBits(I))
2188     return &I;
2189 
2190   if (Value *V = SimplifyBSwap(I))
2191     return replaceInstUsesWith(I, V);
2192 
2193   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2194     ConstantInt *C1 = nullptr; Value *X = nullptr;
2195     // (X & C1) | C2 --> (X | C2) & (C1|C2)
2196     // iff (C1 & C2) == 0.
2197     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
2198         (RHS->getValue() & C1->getValue()) != 0 &&
2199         Op0->hasOneUse()) {
2200       Value *Or = Builder->CreateOr(X, RHS);
2201       Or->takeName(Op0);
2202       return BinaryOperator::CreateAnd(Or,
2203                              Builder->getInt(RHS->getValue() | C1->getValue()));
2204     }
2205 
2206     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
2207     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
2208         Op0->hasOneUse()) {
2209       Value *Or = Builder->CreateOr(X, RHS);
2210       Or->takeName(Op0);
2211       return BinaryOperator::CreateXor(Or,
2212                             Builder->getInt(C1->getValue() & ~RHS->getValue()));
2213     }
2214 
2215     // Try to fold constant and into select arguments.
2216     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2217       if (Instruction *R = FoldOpIntoSelect(I, SI))
2218         return R;
2219 
2220     if (isa<PHINode>(Op0))
2221       if (Instruction *NV = FoldOpIntoPhi(I))
2222         return NV;
2223   }
2224 
2225   // Given an OR instruction, check to see if this is a bswap.
2226   if (Instruction *BSwap = MatchBSwap(I))
2227     return BSwap;
2228 
2229   Value *A = nullptr, *B = nullptr;
2230   ConstantInt *C1 = nullptr, *C2 = nullptr;
2231 
2232   // (X^C)|Y -> (X|Y)^C iff Y&C == 0
2233   if (Op0->hasOneUse() &&
2234       match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
2235       MaskedValueIsZero(Op1, C1->getValue(), 0, &I)) {
2236     Value *NOr = Builder->CreateOr(A, Op1);
2237     NOr->takeName(Op0);
2238     return BinaryOperator::CreateXor(NOr, C1);
2239   }
2240 
2241   // Y|(X^C) -> (X|Y)^C iff Y&C == 0
2242   if (Op1->hasOneUse() &&
2243       match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
2244       MaskedValueIsZero(Op0, C1->getValue(), 0, &I)) {
2245     Value *NOr = Builder->CreateOr(A, Op0);
2246     NOr->takeName(Op0);
2247     return BinaryOperator::CreateXor(NOr, C1);
2248   }
2249 
2250   // ((~A & B) | A) -> (A | B)
2251   if (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2252       match(Op1, m_Specific(A)))
2253     return BinaryOperator::CreateOr(A, B);
2254 
2255   // ((A & B) | ~A) -> (~A | B)
2256   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
2257       match(Op1, m_Not(m_Specific(A))))
2258     return BinaryOperator::CreateOr(Builder->CreateNot(A), B);
2259 
2260   // (A & ~B) | (A ^ B) -> (A ^ B)
2261   // (~B & A) | (A ^ B) -> (A ^ B)
2262   if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2263       match(Op1, m_Xor(m_Specific(A), m_Specific(B))))
2264     return BinaryOperator::CreateXor(A, B);
2265 
2266   // Commute the 'or' operands.
2267   // (A ^ B) | (A & ~B) -> (A ^ B)
2268   // (A ^ B) | (~B & A) -> (A ^ B)
2269   if (match(Op1, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2270       match(Op0, m_Xor(m_Specific(A), m_Specific(B))))
2271     return BinaryOperator::CreateXor(A, B);
2272 
2273   // (A & C)|(B & D)
2274   Value *C = nullptr, *D = nullptr;
2275   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2276       match(Op1, m_And(m_Value(B), m_Value(D)))) {
2277     Value *V1 = nullptr, *V2 = nullptr;
2278     C1 = dyn_cast<ConstantInt>(C);
2279     C2 = dyn_cast<ConstantInt>(D);
2280     if (C1 && C2) {  // (A & C1)|(B & C2)
2281       if ((C1->getValue() & C2->getValue()) == 0) {
2282         // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
2283         // iff (C1&C2) == 0 and (N&~C1) == 0
2284         if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
2285             ((V1 == B &&
2286               MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N)
2287              (V2 == B &&
2288               MaskedValueIsZero(V1, ~C1->getValue(), 0, &I))))  // (N|V)
2289           return BinaryOperator::CreateAnd(A,
2290                                 Builder->getInt(C1->getValue()|C2->getValue()));
2291         // Or commutes, try both ways.
2292         if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
2293             ((V1 == A &&
2294               MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
2295              (V2 == A &&
2296               MaskedValueIsZero(V1, ~C2->getValue(), 0, &I))))  // (N|V)
2297           return BinaryOperator::CreateAnd(B,
2298                                 Builder->getInt(C1->getValue()|C2->getValue()));
2299 
2300         // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
2301         // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
2302         ConstantInt *C3 = nullptr, *C4 = nullptr;
2303         if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
2304             (C3->getValue() & ~C1->getValue()) == 0 &&
2305             match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
2306             (C4->getValue() & ~C2->getValue()) == 0) {
2307           V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
2308           return BinaryOperator::CreateAnd(V2,
2309                                 Builder->getInt(C1->getValue()|C2->getValue()));
2310         }
2311       }
2312     }
2313 
2314     // Don't try to form a select if it's unlikely that we'll get rid of at
2315     // least one of the operands. A select is generally more expensive than the
2316     // 'or' that it is replacing.
2317     if (Op0->hasOneUse() || Op1->hasOneUse()) {
2318       // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2319       if (Value *V = matchSelectFromAndOr(A, C, B, D, *Builder))
2320         return replaceInstUsesWith(I, V);
2321       if (Value *V = matchSelectFromAndOr(A, C, D, B, *Builder))
2322         return replaceInstUsesWith(I, V);
2323       if (Value *V = matchSelectFromAndOr(C, A, B, D, *Builder))
2324         return replaceInstUsesWith(I, V);
2325       if (Value *V = matchSelectFromAndOr(C, A, D, B, *Builder))
2326         return replaceInstUsesWith(I, V);
2327       if (Value *V = matchSelectFromAndOr(B, D, A, C, *Builder))
2328         return replaceInstUsesWith(I, V);
2329       if (Value *V = matchSelectFromAndOr(B, D, C, A, *Builder))
2330         return replaceInstUsesWith(I, V);
2331       if (Value *V = matchSelectFromAndOr(D, B, A, C, *Builder))
2332         return replaceInstUsesWith(I, V);
2333       if (Value *V = matchSelectFromAndOr(D, B, C, A, *Builder))
2334         return replaceInstUsesWith(I, V);
2335     }
2336 
2337     // ((A&~B)|(~A&B)) -> A^B
2338     if ((match(C, m_Not(m_Specific(D))) &&
2339          match(B, m_Not(m_Specific(A)))))
2340       return BinaryOperator::CreateXor(A, D);
2341     // ((~B&A)|(~A&B)) -> A^B
2342     if ((match(A, m_Not(m_Specific(D))) &&
2343          match(B, m_Not(m_Specific(C)))))
2344       return BinaryOperator::CreateXor(C, D);
2345     // ((A&~B)|(B&~A)) -> A^B
2346     if ((match(C, m_Not(m_Specific(B))) &&
2347          match(D, m_Not(m_Specific(A)))))
2348       return BinaryOperator::CreateXor(A, B);
2349     // ((~B&A)|(B&~A)) -> A^B
2350     if ((match(A, m_Not(m_Specific(B))) &&
2351          match(D, m_Not(m_Specific(C)))))
2352       return BinaryOperator::CreateXor(C, B);
2353 
2354     // ((A|B)&1)|(B&-2) -> (A&1) | B
2355     if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2356         match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2357       Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2358       if (Ret) return Ret;
2359     }
2360     // (B&-2)|((A|B)&1) -> (A&1) | B
2361     if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2362         match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2363       Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2364       if (Ret) return Ret;
2365     }
2366     // ((A^B)&1)|(B&-2) -> (A&1) ^ B
2367     if (match(A, m_Xor(m_Value(V1), m_Specific(B))) ||
2368         match(A, m_Xor(m_Specific(B), m_Value(V1)))) {
2369       Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C);
2370       if (Ret) return Ret;
2371     }
2372     // (B&-2)|((A^B)&1) -> (A&1) ^ B
2373     if (match(B, m_Xor(m_Specific(A), m_Value(V1))) ||
2374         match(B, m_Xor(m_Value(V1), m_Specific(A)))) {
2375       Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D);
2376       if (Ret) return Ret;
2377     }
2378   }
2379 
2380   // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2381   if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2382     if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2383       if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
2384         return BinaryOperator::CreateOr(Op0, C);
2385 
2386   // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2387   if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2388     if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2389       if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
2390         return BinaryOperator::CreateOr(Op1, C);
2391 
2392   // ((B | C) & A) | B -> B | (A & C)
2393   if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2394     return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C));
2395 
2396   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2397     return DeMorgan;
2398 
2399   // Canonicalize xor to the RHS.
2400   bool SwappedForXor = false;
2401   if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2402     std::swap(Op0, Op1);
2403     SwappedForXor = true;
2404   }
2405 
2406   // A | ( A ^ B) -> A |  B
2407   // A | (~A ^ B) -> A | ~B
2408   // (A & B) | (A ^ B)
2409   if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2410     if (Op0 == A || Op0 == B)
2411       return BinaryOperator::CreateOr(A, B);
2412 
2413     if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2414         match(Op0, m_And(m_Specific(B), m_Specific(A))))
2415       return BinaryOperator::CreateOr(A, B);
2416 
2417     if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2418       Value *Not = Builder->CreateNot(B, B->getName()+".not");
2419       return BinaryOperator::CreateOr(Not, Op0);
2420     }
2421     if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2422       Value *Not = Builder->CreateNot(A, A->getName()+".not");
2423       return BinaryOperator::CreateOr(Not, Op0);
2424     }
2425   }
2426 
2427   // A | ~(A | B) -> A | ~B
2428   // A | ~(A ^ B) -> A | ~B
2429   if (match(Op1, m_Not(m_Value(A))))
2430     if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2431       if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2432           Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2433                                B->getOpcode() == Instruction::Xor)) {
2434         Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2435                                                  B->getOperand(0);
2436         Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2437         return BinaryOperator::CreateOr(Not, Op0);
2438       }
2439 
2440   // (A & B) | (~A ^ B) -> (~A ^ B)
2441   // (A & B) | (B ^ ~A) -> (~A ^ B)
2442   // (B & A) | (~A ^ B) -> (~A ^ B)
2443   // (B & A) | (B ^ ~A) -> (~A ^ B)
2444   // The match order is important: match the xor first because the 'not'
2445   // operation defines 'A'. We do not need to match the xor as Op0 because the
2446   // xor was canonicalized to Op1 above.
2447   if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2448       match(Op0, m_c_And(m_Specific(A), m_Specific(B))))
2449     return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2450 
2451   if (SwappedForXor)
2452     std::swap(Op0, Op1);
2453 
2454   {
2455     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2456     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2457     if (LHS && RHS)
2458       if (Value *Res = FoldOrOfICmps(LHS, RHS, &I))
2459         return replaceInstUsesWith(I, Res);
2460 
2461     // TODO: Make this recursive; it's a little tricky because an arbitrary
2462     // number of 'or' instructions might have to be created.
2463     Value *X, *Y;
2464     if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2465       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2466         if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
2467           return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
2468       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2469         if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
2470           return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
2471     }
2472     if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2473       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2474         if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
2475           return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
2476       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2477         if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
2478           return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
2479     }
2480   }
2481 
2482   // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
2483   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2484     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2485       if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2486         return replaceInstUsesWith(I, Res);
2487 
2488   if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2489     return CastedOr;
2490 
2491   // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
2492   if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2493       A->getType()->getScalarType()->isIntegerTy(1))
2494     return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
2495   if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2496       A->getType()->getScalarType()->isIntegerTy(1))
2497     return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2498 
2499   // Note: If we've gotten to the point of visiting the outer OR, then the
2500   // inner one couldn't be simplified.  If it was a constant, then it won't
2501   // be simplified by a later pass either, so we try swapping the inner/outer
2502   // ORs in the hopes that we'll be able to simplify it this way.
2503   // (X|C) | V --> (X|V) | C
2504   if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2505       match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2506     Value *Inner = Builder->CreateOr(A, Op1);
2507     Inner->takeName(Op0);
2508     return BinaryOperator::CreateOr(Inner, C1);
2509   }
2510 
2511   // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2512   // Since this OR statement hasn't been optimized further yet, we hope
2513   // that this transformation will allow the new ORs to be optimized.
2514   {
2515     Value *X = nullptr, *Y = nullptr;
2516     if (Op0->hasOneUse() && Op1->hasOneUse() &&
2517         match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2518         match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2519       Value *orTrue = Builder->CreateOr(A, C);
2520       Value *orFalse = Builder->CreateOr(B, D);
2521       return SelectInst::Create(X, orTrue, orFalse);
2522     }
2523   }
2524 
2525   return Changed ? &I : nullptr;
2526 }
2527 
2528 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2529 // here. We should standardize that construct where it is needed or choose some
2530 // other way to ensure that commutated variants of patterns are not missed.
2531 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
2532   bool Changed = SimplifyAssociativeOrCommutative(I);
2533   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2534 
2535   if (Value *V = SimplifyVectorOp(I))
2536     return replaceInstUsesWith(I, V);
2537 
2538   if (Value *V = SimplifyXorInst(Op0, Op1, DL, &TLI, &DT, &AC))
2539     return replaceInstUsesWith(I, V);
2540 
2541   // (A&B)^(A&C) -> A&(B^C) etc
2542   if (Value *V = SimplifyUsingDistributiveLaws(I))
2543     return replaceInstUsesWith(I, V);
2544 
2545   // See if we can simplify any instructions used by the instruction whose sole
2546   // purpose is to compute bits we don't care about.
2547   if (SimplifyDemandedInstructionBits(I))
2548     return &I;
2549 
2550   if (Value *V = SimplifyBSwap(I))
2551     return replaceInstUsesWith(I, V);
2552 
2553   // Is this a ~ operation?
2554   if (Value *NotOp = dyn_castNotVal(&I)) {
2555     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
2556       if (Op0I->getOpcode() == Instruction::And ||
2557           Op0I->getOpcode() == Instruction::Or) {
2558         // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2559         // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2560         if (dyn_castNotVal(Op0I->getOperand(1)))
2561           Op0I->swapOperands();
2562         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2563           Value *NotY =
2564             Builder->CreateNot(Op0I->getOperand(1),
2565                                Op0I->getOperand(1)->getName()+".not");
2566           if (Op0I->getOpcode() == Instruction::And)
2567             return BinaryOperator::CreateOr(Op0NotVal, NotY);
2568           return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2569         }
2570 
2571         // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2572         // ~(X | Y) === (~X & ~Y) - De Morgan's Law
2573         if (IsFreeToInvert(Op0I->getOperand(0),
2574                            Op0I->getOperand(0)->hasOneUse()) &&
2575             IsFreeToInvert(Op0I->getOperand(1),
2576                            Op0I->getOperand(1)->hasOneUse())) {
2577           Value *NotX =
2578             Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2579           Value *NotY =
2580             Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2581           if (Op0I->getOpcode() == Instruction::And)
2582             return BinaryOperator::CreateOr(NotX, NotY);
2583           return BinaryOperator::CreateAnd(NotX, NotY);
2584         }
2585 
2586       } else if (Op0I->getOpcode() == Instruction::AShr) {
2587         // ~(~X >>s Y) --> (X >>s Y)
2588         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2589           return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
2590       }
2591     }
2592   }
2593 
2594   if (Constant *RHS = dyn_cast<Constant>(Op1)) {
2595     if (RHS->isAllOnesValue() && Op0->hasOneUse())
2596       // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
2597       if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2598         return CmpInst::Create(CI->getOpcode(),
2599                                CI->getInversePredicate(),
2600                                CI->getOperand(0), CI->getOperand(1));
2601   }
2602 
2603   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2604     // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2605     if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2606       if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2607         if (CI->hasOneUse() && Op0C->hasOneUse()) {
2608           Instruction::CastOps Opcode = Op0C->getOpcode();
2609           if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
2610               (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(),
2611                                             Op0C->getDestTy()))) {
2612             CI->setPredicate(CI->getInversePredicate());
2613             return CastInst::Create(Opcode, CI, Op0C->getType());
2614           }
2615         }
2616       }
2617     }
2618 
2619     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2620       // ~(c-X) == X-c-1 == X+(-c-1)
2621       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2622         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2623           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2624           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2625                                       ConstantInt::get(I.getType(), 1));
2626           return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2627         }
2628 
2629       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2630         if (Op0I->getOpcode() == Instruction::Add) {
2631           // ~(X-c) --> (-c-1)-X
2632           if (RHS->isAllOnesValue()) {
2633             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2634             return BinaryOperator::CreateSub(
2635                            ConstantExpr::getSub(NegOp0CI,
2636                                       ConstantInt::get(I.getType(), 1)),
2637                                       Op0I->getOperand(0));
2638           } else if (RHS->getValue().isSignBit()) {
2639             // (X + C) ^ signbit -> (X + C + signbit)
2640             Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
2641             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2642 
2643           }
2644         } else if (Op0I->getOpcode() == Instruction::Or) {
2645           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
2646           if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(),
2647                                 0, &I)) {
2648             Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2649             // Anything in both C1 and C2 is known to be zero, remove it from
2650             // NewRHS.
2651             Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
2652             NewRHS = ConstantExpr::getAnd(NewRHS,
2653                                        ConstantExpr::getNot(CommonBits));
2654             Worklist.Add(Op0I);
2655             I.setOperand(0, Op0I->getOperand(0));
2656             I.setOperand(1, NewRHS);
2657             return &I;
2658           }
2659         } else if (Op0I->getOpcode() == Instruction::LShr) {
2660           // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2661           // E1 = "X ^ C1"
2662           BinaryOperator *E1;
2663           ConstantInt *C1;
2664           if (Op0I->hasOneUse() &&
2665               (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2666               E1->getOpcode() == Instruction::Xor &&
2667               (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2668             // fold (C1 >> C2) ^ C3
2669             ConstantInt *C2 = Op0CI, *C3 = RHS;
2670             APInt FoldConst = C1->getValue().lshr(C2->getValue());
2671             FoldConst ^= C3->getValue();
2672             // Prepare the two operands.
2673             Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2674             Opnd0->takeName(Op0I);
2675             cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2676             Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2677 
2678             return BinaryOperator::CreateXor(Opnd0, FoldVal);
2679           }
2680         }
2681       }
2682     }
2683 
2684     // Try to fold constant and into select arguments.
2685     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2686       if (Instruction *R = FoldOpIntoSelect(I, SI))
2687         return R;
2688     if (isa<PHINode>(Op0))
2689       if (Instruction *NV = FoldOpIntoPhi(I))
2690         return NV;
2691   }
2692 
2693   BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2694   if (Op1I) {
2695     Value *A, *B;
2696     if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2697       if (A == Op0) {              // B^(B|A) == (A|B)^B
2698         Op1I->swapOperands();
2699         I.swapOperands();
2700         std::swap(Op0, Op1);
2701       } else if (B == Op0) {       // B^(A|B) == (A|B)^B
2702         I.swapOperands();     // Simplified below.
2703         std::swap(Op0, Op1);
2704       }
2705     } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
2706                Op1I->hasOneUse()){
2707       if (A == Op0) {                                      // A^(A&B) -> A^(B&A)
2708         Op1I->swapOperands();
2709         std::swap(A, B);
2710       }
2711       if (B == Op0) {                                      // A^(B&A) -> (B&A)^A
2712         I.swapOperands();     // Simplified below.
2713         std::swap(Op0, Op1);
2714       }
2715     }
2716   }
2717 
2718   BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2719   if (Op0I) {
2720     Value *A, *B;
2721     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2722         Op0I->hasOneUse()) {
2723       if (A == Op1)                                  // (B|A)^B == (A|B)^B
2724         std::swap(A, B);
2725       if (B == Op1)                                  // (A|B)^B == A & ~B
2726         return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
2727     } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2728                Op0I->hasOneUse()){
2729       if (A == Op1)                                        // (A&B)^A -> (B&A)^A
2730         std::swap(A, B);
2731       if (B == Op1 &&                                      // (B&A)^A == ~B & A
2732           !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
2733         return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
2734       }
2735     }
2736   }
2737 
2738   if (Op0I && Op1I) {
2739     Value *A, *B, *C, *D;
2740     // (A & B)^(A | B) -> A ^ B
2741     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2742         match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
2743       if ((A == C && B == D) || (A == D && B == C))
2744         return BinaryOperator::CreateXor(A, B);
2745     }
2746     // (A | B)^(A & B) -> A ^ B
2747     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2748         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
2749       if ((A == C && B == D) || (A == D && B == C))
2750         return BinaryOperator::CreateXor(A, B);
2751     }
2752     // (A | ~B) ^ (~A | B) -> A ^ B
2753     // (~B | A) ^ (~A | B) -> A ^ B
2754     if (match(Op0I, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
2755         match(Op1I, m_Or(m_Not(m_Specific(A)), m_Specific(B))))
2756       return BinaryOperator::CreateXor(A, B);
2757 
2758     // (~A | B) ^ (A | ~B) -> A ^ B
2759     if (match(Op0I, m_Or(m_Not(m_Value(A)), m_Value(B))) &&
2760         match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) {
2761       return BinaryOperator::CreateXor(A, B);
2762     }
2763     // (A & ~B) ^ (~A & B) -> A ^ B
2764     // (~B & A) ^ (~A & B) -> A ^ B
2765     if (match(Op0I, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2766         match(Op1I, m_And(m_Not(m_Specific(A)), m_Specific(B))))
2767       return BinaryOperator::CreateXor(A, B);
2768 
2769     // (~A & B) ^ (A & ~B) -> A ^ B
2770     if (match(Op0I, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2771         match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B))))) {
2772       return BinaryOperator::CreateXor(A, B);
2773     }
2774     // (A ^ C)^(A | B) -> ((~A) & B) ^ C
2775     if (match(Op0I, m_Xor(m_Value(D), m_Value(C))) &&
2776         match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2777       if (D == A)
2778         return BinaryOperator::CreateXor(
2779             Builder->CreateAnd(Builder->CreateNot(A), B), C);
2780       if (D == B)
2781         return BinaryOperator::CreateXor(
2782             Builder->CreateAnd(Builder->CreateNot(B), A), C);
2783     }
2784     // (A | B)^(A ^ C) -> ((~A) & B) ^ C
2785     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2786         match(Op1I, m_Xor(m_Value(D), m_Value(C)))) {
2787       if (D == A)
2788         return BinaryOperator::CreateXor(
2789             Builder->CreateAnd(Builder->CreateNot(A), B), C);
2790       if (D == B)
2791         return BinaryOperator::CreateXor(
2792             Builder->CreateAnd(Builder->CreateNot(B), A), C);
2793     }
2794     // (A & B) ^ (A ^ B) -> (A | B)
2795     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2796         match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))
2797       return BinaryOperator::CreateOr(A, B);
2798     // (A ^ B) ^ (A & B) -> (A | B)
2799     if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
2800         match(Op1I, m_And(m_Specific(A), m_Specific(B))))
2801       return BinaryOperator::CreateOr(A, B);
2802   }
2803 
2804   // (A & ~B) ^ ~A -> ~(A & B)
2805   // (~B & A) ^ ~A -> ~(A & B)
2806   Value *A, *B;
2807   if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2808       match(Op1, m_Not(m_Specific(A))))
2809     return BinaryOperator::CreateNot(Builder->CreateAnd(A, B));
2810 
2811   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2812     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) {
2813 
2814       // E.g. if we have xor (icmp eq %A, 0), (icmp eq %B, 0)
2815       // and we know both A and B are either 8 (power of 2) or 0
2816       // we can simplify to (icmp ne A, B).
2817       if (Value *Res = FoldXorOfICmps(LHS, RHS))
2818         return replaceInstUsesWith(I, Res);
2819 
2820       // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
2821       if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2822         if (LHS->getOperand(0) == RHS->getOperand(1) &&
2823             LHS->getOperand(1) == RHS->getOperand(0))
2824           LHS->swapOperands();
2825         if (LHS->getOperand(0) == RHS->getOperand(0) &&
2826             LHS->getOperand(1) == RHS->getOperand(1)) {
2827           Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2828           unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2829           bool isSigned = LHS->isSigned() || RHS->isSigned();
2830           return replaceInstUsesWith(I,
2831                                getNewICmpValue(isSigned, Code, Op0, Op1,
2832                                                Builder));
2833         }
2834       }
2835     }
2836 
2837   if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
2838     return CastedXor;
2839 
2840   return Changed ? &I : nullptr;
2841 }
2842