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 /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
737 /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
738 /// If \p Inverted is true then the check is for the inverted range, e.g.
739 /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
740 Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
741                                         bool Inverted) {
742   // Check the lower range comparison, e.g. x >= 0
743   // InstCombine already ensured that if there is a constant it's on the RHS.
744   ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
745   if (!RangeStart)
746     return nullptr;
747 
748   ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
749                                Cmp0->getPredicate());
750 
751   // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
752   if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
753         (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
754     return nullptr;
755 
756   ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
757                                Cmp1->getPredicate());
758 
759   Value *Input = Cmp0->getOperand(0);
760   Value *RangeEnd;
761   if (Cmp1->getOperand(0) == Input) {
762     // For the upper range compare we have: icmp x, n
763     RangeEnd = Cmp1->getOperand(1);
764   } else if (Cmp1->getOperand(1) == Input) {
765     // For the upper range compare we have: icmp n, x
766     RangeEnd = Cmp1->getOperand(0);
767     Pred1 = ICmpInst::getSwappedPredicate(Pred1);
768   } else {
769     return nullptr;
770   }
771 
772   // Check the upper range comparison, e.g. x < n
773   ICmpInst::Predicate NewPred;
774   switch (Pred1) {
775     case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
776     case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
777     default: return nullptr;
778   }
779 
780   // This simplification is only valid if the upper range is not negative.
781   bool IsNegative, IsNotNegative;
782   ComputeSignBit(RangeEnd, IsNotNegative, IsNegative, /*Depth=*/0, Cmp1);
783   if (!IsNotNegative)
784     return nullptr;
785 
786   if (Inverted)
787     NewPred = ICmpInst::getInversePredicate(NewPred);
788 
789   return Builder->CreateICmp(NewPred, Input, RangeEnd);
790 }
791 
792 /// Fold (icmp)&(icmp) if possible.
793 Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
794   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
795 
796   // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
797   if (PredicatesFoldable(LHSCC, RHSCC)) {
798     if (LHS->getOperand(0) == RHS->getOperand(1) &&
799         LHS->getOperand(1) == RHS->getOperand(0))
800       LHS->swapOperands();
801     if (LHS->getOperand(0) == RHS->getOperand(0) &&
802         LHS->getOperand(1) == RHS->getOperand(1)) {
803       Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
804       unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
805       bool isSigned = LHS->isSigned() || RHS->isSigned();
806       return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
807     }
808   }
809 
810   // handle (roughly):  (icmp eq (A & B), C) & (icmp eq (A & D), E)
811   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
812     return V;
813 
814   // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
815   if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false))
816     return V;
817 
818   // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
819   if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
820     return V;
821 
822   // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
823   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
824   ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
825   ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
826   if (!LHSCst || !RHSCst) return nullptr;
827 
828   if (LHSCst == RHSCst && LHSCC == RHSCC) {
829     // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
830     // where C is a power of 2 or
831     // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
832     if ((LHSCC == ICmpInst::ICMP_ULT && LHSCst->getValue().isPowerOf2()) ||
833         (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero())) {
834       Value *NewOr = Builder->CreateOr(Val, Val2);
835       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
836     }
837   }
838 
839   // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
840   // where CMAX is the all ones value for the truncated type,
841   // iff the lower bits of C2 and CA are zero.
842   if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
843       LHS->hasOneUse() && RHS->hasOneUse()) {
844     Value *V;
845     ConstantInt *AndCst, *SmallCst = nullptr, *BigCst = nullptr;
846 
847     // (trunc x) == C1 & (and x, CA) == C2
848     // (and x, CA) == C2 & (trunc x) == C1
849     if (match(Val2, m_Trunc(m_Value(V))) &&
850         match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
851       SmallCst = RHSCst;
852       BigCst = LHSCst;
853     } else if (match(Val, m_Trunc(m_Value(V))) &&
854                match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
855       SmallCst = LHSCst;
856       BigCst = RHSCst;
857     }
858 
859     if (SmallCst && BigCst) {
860       unsigned BigBitSize = BigCst->getType()->getBitWidth();
861       unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
862 
863       // Check that the low bits are zero.
864       APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
865       if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
866         Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
867         APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
868         Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
869         return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
870       }
871     }
872   }
873 
874   // From here on, we only handle:
875   //    (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
876   if (Val != Val2) return nullptr;
877 
878   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
879   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
880       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
881       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
882       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
883     return nullptr;
884 
885   // We can't fold (ugt x, C) & (sgt x, C2).
886   if (!PredicatesFoldable(LHSCC, RHSCC))
887     return nullptr;
888 
889   // Ensure that the larger constant is on the RHS.
890   bool ShouldSwap;
891   if (CmpInst::isSigned(LHSCC) ||
892       (ICmpInst::isEquality(LHSCC) &&
893        CmpInst::isSigned(RHSCC)))
894     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
895   else
896     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
897 
898   if (ShouldSwap) {
899     std::swap(LHS, RHS);
900     std::swap(LHSCst, RHSCst);
901     std::swap(LHSCC, RHSCC);
902   }
903 
904   // At this point, we know we have two icmp instructions
905   // comparing a value against two constants and and'ing the result
906   // together.  Because of the above check, we know that we only have
907   // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
908   // (from the icmp folding check above), that the two constants
909   // are not equal and that the larger constant is on the RHS
910   assert(LHSCst != RHSCst && "Compares not folded above?");
911 
912   switch (LHSCC) {
913   default: llvm_unreachable("Unknown integer condition code!");
914   case ICmpInst::ICMP_EQ:
915     switch (RHSCC) {
916     default: llvm_unreachable("Unknown integer condition code!");
917     case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13
918     case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13
919     case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13
920       return LHS;
921     }
922   case ICmpInst::ICMP_NE:
923     switch (RHSCC) {
924     default: llvm_unreachable("Unknown integer condition code!");
925     case ICmpInst::ICMP_ULT:
926       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
927         return Builder->CreateICmpULT(Val, LHSCst);
928       if (LHSCst->isNullValue())    // (X !=  0 & X u< 14) -> X-1 u< 13
929         return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
930                                false, true);
931       break;                        // (X != 13 & X u< 15) -> no change
932     case ICmpInst::ICMP_SLT:
933       if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
934         return Builder->CreateICmpSLT(Val, LHSCst);
935       break;                        // (X != 13 & X s< 15) -> no change
936     case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15
937     case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15
938     case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15
939       return RHS;
940     case ICmpInst::ICMP_NE:
941       // Special case to get the ordering right when the values wrap around
942       // zero.
943       if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue())
944         std::swap(LHSCst, RHSCst);
945       if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
946         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
947         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
948         return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1),
949                                       Val->getName()+".cmp");
950       }
951       break;                        // (X != 13 & X != 15) -> no change
952     }
953     break;
954   case ICmpInst::ICMP_ULT:
955     switch (RHSCC) {
956     default: llvm_unreachable("Unknown integer condition code!");
957     case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false
958     case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false
959       return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
960     case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change
961       break;
962     case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13
963     case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13
964       return LHS;
965     case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change
966       break;
967     }
968     break;
969   case ICmpInst::ICMP_SLT:
970     switch (RHSCC) {
971     default: llvm_unreachable("Unknown integer condition code!");
972     case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change
973       break;
974     case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13
975     case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13
976       return LHS;
977     case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change
978       break;
979     }
980     break;
981   case ICmpInst::ICMP_UGT:
982     switch (RHSCC) {
983     default: llvm_unreachable("Unknown integer condition code!");
984     case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X == 15
985     case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15
986       return RHS;
987     case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change
988       break;
989     case ICmpInst::ICMP_NE:
990       if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
991         return Builder->CreateICmp(LHSCC, Val, RHSCst);
992       break;                        // (X u> 13 & X != 15) -> no change
993     case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) -> (X-14) <u 1
994       return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
995                              false, true);
996     case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change
997       break;
998     }
999     break;
1000   case ICmpInst::ICMP_SGT:
1001     switch (RHSCC) {
1002     default: llvm_unreachable("Unknown integer condition code!");
1003     case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
1004     case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
1005       return RHS;
1006     case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
1007       break;
1008     case ICmpInst::ICMP_NE:
1009       if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
1010         return Builder->CreateICmp(LHSCC, Val, RHSCst);
1011       break;                        // (X s> 13 & X != 15) -> no change
1012     case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) -> (X-14) s< 1
1013       return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
1014                              true, true);
1015     case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change
1016       break;
1017     }
1018     break;
1019   }
1020 
1021   return nullptr;
1022 }
1023 
1024 /// Optimize (fcmp)&(fcmp).  NOTE: Unlike the rest of instcombine, this returns
1025 /// a Value which should already be inserted into the function.
1026 Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
1027   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1028   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1029   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1030 
1031   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1032     // Swap RHS operands to match LHS.
1033     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1034     std::swap(Op1LHS, Op1RHS);
1035   }
1036 
1037   // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1038   // Suppose the relation between x and y is R, where R is one of
1039   // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
1040   // testing the desired relations.
1041   //
1042   // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1043   //    bool(R & CC0) && bool(R & CC1)
1044   //  = bool((R & CC0) & (R & CC1))
1045   //  = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
1046   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1047     return getFCmpValue(getFCmpCode(Op0CC) & getFCmpCode(Op1CC), Op0LHS, Op0RHS,
1048                         Builder);
1049 
1050   if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
1051       RHS->getPredicate() == FCmpInst::FCMP_ORD) {
1052     if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
1053       return nullptr;
1054 
1055     // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
1056     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1057       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1058         // If either of the constants are nans, then the whole thing returns
1059         // false.
1060         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1061           return Builder->getFalse();
1062         return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1063       }
1064 
1065     // Handle vector zeros.  This occurs because the canonical form of
1066     // "fcmp ord x,x" is "fcmp ord x, 0".
1067     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1068         isa<ConstantAggregateZero>(RHS->getOperand(1)))
1069       return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1070     return nullptr;
1071   }
1072 
1073   return nullptr;
1074 }
1075 
1076 /// Match De Morgan's Laws:
1077 /// (~A & ~B) == (~(A | B))
1078 /// (~A | ~B) == (~(A & B))
1079 static Instruction *matchDeMorgansLaws(BinaryOperator &I,
1080                                        InstCombiner::BuilderTy *Builder) {
1081   auto Opcode = I.getOpcode();
1082   assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1083          "Trying to match De Morgan's Laws with something other than and/or");
1084   // Flip the logic operation.
1085   if (Opcode == Instruction::And)
1086     Opcode = Instruction::Or;
1087   else
1088     Opcode = Instruction::And;
1089 
1090   Value *Op0 = I.getOperand(0);
1091   Value *Op1 = I.getOperand(1);
1092   // TODO: Use pattern matchers instead of dyn_cast.
1093   if (Value *Op0NotVal = dyn_castNotVal(Op0))
1094     if (Value *Op1NotVal = dyn_castNotVal(Op1))
1095       if (Op0->hasOneUse() && Op1->hasOneUse()) {
1096         Value *LogicOp = Builder->CreateBinOp(Opcode, Op0NotVal, Op1NotVal,
1097                                               I.getName() + ".demorgan");
1098         return BinaryOperator::CreateNot(LogicOp);
1099       }
1100 
1101   return nullptr;
1102 }
1103 
1104 bool InstCombiner::shouldOptimizeCast(CastInst *CI) {
1105   Value *CastSrc = CI->getOperand(0);
1106 
1107   // Noop casts and casts of constants should be eliminated trivially.
1108   if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1109     return false;
1110 
1111   // If this cast is paired with another cast that can be eliminated, we prefer
1112   // to have it eliminated.
1113   if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1114     if (isEliminableCastPair(PrecedingCI, CI))
1115       return false;
1116 
1117   // If this is a vector sext from a compare, then we don't want to break the
1118   // idiom where each element of the extended vector is either zero or all ones.
1119   if (CI->getOpcode() == Instruction::SExt &&
1120       isa<CmpInst>(CastSrc) && CI->getDestTy()->isVectorTy())
1121     return false;
1122 
1123   return true;
1124 }
1125 
1126 /// Fold {and,or,xor} (cast X), C.
1127 static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1128                                           InstCombiner::BuilderTy *Builder) {
1129   Constant *C;
1130   if (!match(Logic.getOperand(1), m_Constant(C)))
1131     return nullptr;
1132 
1133   auto LogicOpc = Logic.getOpcode();
1134   Type *DestTy = Logic.getType();
1135   Type *SrcTy = Cast->getSrcTy();
1136 
1137   // If the first operand is bitcast, move the logic operation ahead of the
1138   // bitcast (do the logic operation in the original type). This can eliminate
1139   // bitcasts and allow combines that would otherwise be impeded by the bitcast.
1140   Value *X;
1141   if (match(Cast, m_BitCast(m_Value(X)))) {
1142     Value *NewConstant = ConstantExpr::getBitCast(C, SrcTy);
1143     Value *NewOp = Builder->CreateBinOp(LogicOpc, X, NewConstant);
1144     return CastInst::CreateBitOrPointerCast(NewOp, DestTy);
1145   }
1146 
1147   // Similarly, move the logic operation ahead of a zext if the constant is
1148   // unchanged in the smaller source type. Performing the logic in a smaller
1149   // type may provide more information to later folds, and the smaller logic
1150   // instruction may be cheaper (particularly in the case of vectors).
1151   if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1152     Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1153     Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1154     if (ZextTruncC == C) {
1155       // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1156       Value *NewOp = Builder->CreateBinOp(LogicOpc, X, TruncC);
1157       return new ZExtInst(NewOp, DestTy);
1158     }
1159   }
1160 
1161   return nullptr;
1162 }
1163 
1164 /// Fold {and,or,xor} (cast X), Y.
1165 Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) {
1166   auto LogicOpc = I.getOpcode();
1167   assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
1168 
1169   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1170   CastInst *Cast0 = dyn_cast<CastInst>(Op0);
1171   if (!Cast0)
1172     return nullptr;
1173 
1174   // This must be a cast from an integer or integer vector source type to allow
1175   // transformation of the logic operation to the source type.
1176   Type *DestTy = I.getType();
1177   Type *SrcTy = Cast0->getSrcTy();
1178   if (!SrcTy->isIntOrIntVectorTy())
1179     return nullptr;
1180 
1181   if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1182     return Ret;
1183 
1184   CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1185   if (!Cast1)
1186     return nullptr;
1187 
1188   // Both operands of the logic operation are casts. The casts must be of the
1189   // same type for reduction.
1190   auto CastOpcode = Cast0->getOpcode();
1191   if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy())
1192     return nullptr;
1193 
1194   Value *Cast0Src = Cast0->getOperand(0);
1195   Value *Cast1Src = Cast1->getOperand(0);
1196 
1197   // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
1198   if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1199     Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1200                                         I.getName());
1201     return CastInst::Create(CastOpcode, NewOp, DestTy);
1202   }
1203 
1204   // For now, only 'and'/'or' have optimizations after this.
1205   if (LogicOpc == Instruction::Xor)
1206     return nullptr;
1207 
1208   // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the
1209   // cast is otherwise not optimizable.  This happens for vector sexts.
1210   ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src);
1211   ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src);
1212   if (ICmp0 && ICmp1) {
1213     Value *Res = LogicOpc == Instruction::And ? FoldAndOfICmps(ICmp0, ICmp1)
1214                                               : FoldOrOfICmps(ICmp0, ICmp1, &I);
1215     if (Res)
1216       return CastInst::Create(CastOpcode, Res, DestTy);
1217     return nullptr;
1218   }
1219 
1220   // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the
1221   // cast is otherwise not optimizable.  This happens for vector sexts.
1222   FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src);
1223   FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src);
1224   if (FCmp0 && FCmp1) {
1225     Value *Res = LogicOpc == Instruction::And ? FoldAndOfFCmps(FCmp0, FCmp1)
1226                                               : FoldOrOfFCmps(FCmp0, FCmp1);
1227     if (Res)
1228       return CastInst::Create(CastOpcode, Res, DestTy);
1229     return nullptr;
1230   }
1231 
1232   return nullptr;
1233 }
1234 
1235 static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) {
1236   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1237 
1238   // Canonicalize SExt or Not to the LHS
1239   if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) {
1240     std::swap(Op0, Op1);
1241   }
1242 
1243   // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1244   Value *X = nullptr;
1245   if (match(Op0, m_SExt(m_Value(X))) &&
1246       X->getType()->getScalarType()->isIntegerTy(1)) {
1247     Value *Zero = Constant::getNullValue(Op1->getType());
1248     return SelectInst::Create(X, Op1, Zero);
1249   }
1250 
1251   // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1252   if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1253       X->getType()->getScalarType()->isIntegerTy(1)) {
1254     Value *Zero = Constant::getNullValue(Op0->getType());
1255     return SelectInst::Create(X, Zero, Op1);
1256   }
1257 
1258   return nullptr;
1259 }
1260 
1261 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
1262   bool Changed = SimplifyAssociativeOrCommutative(I);
1263   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1264 
1265   if (Value *V = SimplifyVectorOp(I))
1266     return replaceInstUsesWith(I, V);
1267 
1268   if (Value *V = SimplifyAndInst(Op0, Op1, DL, &TLI, &DT, &AC))
1269     return replaceInstUsesWith(I, V);
1270 
1271   // (A|B)&(A|C) -> A|(B&C) etc
1272   if (Value *V = SimplifyUsingDistributiveLaws(I))
1273     return replaceInstUsesWith(I, V);
1274 
1275   // See if we can simplify any instructions used by the instruction whose sole
1276   // purpose is to compute bits we don't care about.
1277   if (SimplifyDemandedInstructionBits(I))
1278     return &I;
1279 
1280   if (Value *V = SimplifyBSwap(I))
1281     return replaceInstUsesWith(I, V);
1282 
1283   if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1284     const APInt &AndRHSMask = AndRHS->getValue();
1285 
1286     // Optimize a variety of ((val OP C1) & C2) combinations...
1287     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1288       Value *Op0LHS = Op0I->getOperand(0);
1289       Value *Op0RHS = Op0I->getOperand(1);
1290       switch (Op0I->getOpcode()) {
1291       default: break;
1292       case Instruction::Xor:
1293       case Instruction::Or: {
1294         // If the mask is only needed on one incoming arm, push it up.
1295         if (!Op0I->hasOneUse()) break;
1296 
1297         APInt NotAndRHS(~AndRHSMask);
1298         if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) {
1299           // Not masking anything out for the LHS, move to RHS.
1300           Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1301                                              Op0RHS->getName()+".masked");
1302           return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1303         }
1304         if (!isa<Constant>(Op0RHS) &&
1305             MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) {
1306           // Not masking anything out for the RHS, move to LHS.
1307           Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1308                                              Op0LHS->getName()+".masked");
1309           return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1310         }
1311 
1312         break;
1313       }
1314       case Instruction::Add:
1315         // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
1316         // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1317         // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1318         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
1319           return BinaryOperator::CreateAnd(V, AndRHS);
1320         if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
1321           return BinaryOperator::CreateAnd(V, AndRHS);  // Add commutes
1322         break;
1323 
1324       case Instruction::Sub:
1325         // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
1326         // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1327         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1328         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
1329           return BinaryOperator::CreateAnd(V, AndRHS);
1330 
1331         // -x & 1 -> x & 1
1332         if (AndRHSMask == 1 && match(Op0LHS, m_Zero()))
1333           return BinaryOperator::CreateAnd(Op0RHS, AndRHS);
1334 
1335         // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
1336         // has 1's for all bits that the subtraction with A might affect.
1337         if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
1338           uint32_t BitWidth = AndRHSMask.getBitWidth();
1339           uint32_t Zeros = AndRHSMask.countLeadingZeros();
1340           APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
1341 
1342           if (MaskedValueIsZero(Op0LHS, Mask, 0, &I)) {
1343             Value *NewNeg = Builder->CreateNeg(Op0RHS);
1344             return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1345           }
1346         }
1347         break;
1348 
1349       case Instruction::Shl:
1350       case Instruction::LShr:
1351         // (1 << x) & 1 --> zext(x == 0)
1352         // (1 >> x) & 1 --> zext(x == 0)
1353         if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1354           Value *NewICmp =
1355             Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1356           return new ZExtInst(NewICmp, I.getType());
1357         }
1358         break;
1359       }
1360 
1361       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1362         if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1363           return Res;
1364     }
1365 
1366     // If this is an integer truncation, and if the source is an 'and' with
1367     // immediate, transform it.  This frequently occurs for bitfield accesses.
1368     {
1369       Value *X = nullptr; ConstantInt *YC = nullptr;
1370       if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1371         // Change: and (trunc (and X, YC) to T), C2
1372         // into  : and (trunc X to T), trunc(YC) & C2
1373         // This will fold the two constants together, which may allow
1374         // other simplifications.
1375         Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1376         Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1377         C3 = ConstantExpr::getAnd(C3, AndRHS);
1378         return BinaryOperator::CreateAnd(NewCast, C3);
1379       }
1380     }
1381 
1382     // Try to fold constant and into select arguments.
1383     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1384       if (Instruction *R = FoldOpIntoSelect(I, SI))
1385         return R;
1386     if (isa<PHINode>(Op0))
1387       if (Instruction *NV = FoldOpIntoPhi(I))
1388         return NV;
1389   }
1390 
1391   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
1392     return DeMorgan;
1393 
1394   {
1395     Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
1396     // (A|B) & ~(A&B) -> A^B
1397     if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1398         match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1399         ((A == C && B == D) || (A == D && B == C)))
1400       return BinaryOperator::CreateXor(A, B);
1401 
1402     // ~(A&B) & (A|B) -> A^B
1403     if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1404         match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1405         ((A == C && B == D) || (A == D && B == C)))
1406       return BinaryOperator::CreateXor(A, B);
1407 
1408     // A&(A^B) => A & ~B
1409     {
1410       Value *tmpOp0 = Op0;
1411       Value *tmpOp1 = Op1;
1412       if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
1413         if (A == Op1 || B == Op1 ) {
1414           tmpOp1 = Op0;
1415           tmpOp0 = Op1;
1416           // Simplify below
1417         }
1418       }
1419 
1420       if (match(tmpOp1, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
1421         if (B == tmpOp0) {
1422           std::swap(A, B);
1423         }
1424         // Notice that the pattern (A&(~B)) is actually (A&(-1^B)), so if
1425         // A is originally -1 (or a vector of -1 and undefs), then we enter
1426         // an endless loop. By checking that A is non-constant we ensure that
1427         // we will never get to the loop.
1428         if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
1429           return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
1430       }
1431     }
1432 
1433     // (A&((~A)|B)) -> A&B
1434     if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1435         match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1436       return BinaryOperator::CreateAnd(A, Op1);
1437     if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1438         match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1439       return BinaryOperator::CreateAnd(A, Op0);
1440 
1441     // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
1442     if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
1443       if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
1444         if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
1445           return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C));
1446 
1447     // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
1448     if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
1449       if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
1450         if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
1451           return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C));
1452 
1453     // (A | B) & ((~A) ^ B) -> (A & B)
1454     if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1455         match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B))))
1456       return BinaryOperator::CreateAnd(A, B);
1457 
1458     // ((~A) ^ B) & (A | B) -> (A & B)
1459     if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
1460         match(Op1, m_Or(m_Specific(A), m_Specific(B))))
1461       return BinaryOperator::CreateAnd(A, B);
1462   }
1463 
1464   {
1465     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
1466     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
1467     if (LHS && RHS)
1468       if (Value *Res = FoldAndOfICmps(LHS, RHS))
1469         return replaceInstUsesWith(I, Res);
1470 
1471     // TODO: Make this recursive; it's a little tricky because an arbitrary
1472     // number of 'and' instructions might have to be created.
1473     Value *X, *Y;
1474     if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1475       if (auto *Cmp = dyn_cast<ICmpInst>(X))
1476         if (Value *Res = FoldAndOfICmps(LHS, Cmp))
1477           return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
1478       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1479         if (Value *Res = FoldAndOfICmps(LHS, Cmp))
1480           return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
1481     }
1482     if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1483       if (auto *Cmp = dyn_cast<ICmpInst>(X))
1484         if (Value *Res = FoldAndOfICmps(Cmp, RHS))
1485           return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
1486       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1487         if (Value *Res = FoldAndOfICmps(Cmp, RHS))
1488           return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
1489     }
1490   }
1491 
1492   // If and'ing two fcmp, try combine them into one.
1493   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1494     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
1495       if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1496         return replaceInstUsesWith(I, Res);
1497 
1498   if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1499     return CastedAnd;
1500 
1501   if (Instruction *Select = foldBoolSextMaskToSelect(I))
1502     return Select;
1503 
1504   return Changed ? &I : nullptr;
1505 }
1506 
1507 /// Given an OR instruction, check to see if this is a bswap idiom. If so,
1508 /// insert the new intrinsic and return it.
1509 Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
1510   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1511 
1512   // Look through zero extends.
1513   if (Instruction *Ext = dyn_cast<ZExtInst>(Op0))
1514     Op0 = Ext->getOperand(0);
1515 
1516   if (Instruction *Ext = dyn_cast<ZExtInst>(Op1))
1517     Op1 = Ext->getOperand(0);
1518 
1519   // (A | B) | C  and  A | (B | C)                  -> bswap if possible.
1520   bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) ||
1521                  match(Op1, m_Or(m_Value(), m_Value()));
1522 
1523   // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible.
1524   bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1525                     match(Op1, m_LogicalShift(m_Value(), m_Value()));
1526 
1527   // (A & B) | (C & D)                              -> bswap if possible.
1528   bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) &&
1529                   match(Op1, m_And(m_Value(), m_Value()));
1530 
1531   if (!OrOfOrs && !OrOfShifts && !OrOfAnds)
1532     return nullptr;
1533 
1534   SmallVector<Instruction*, 4> Insts;
1535   if (!recognizeBSwapOrBitReverseIdiom(&I, true, false, Insts))
1536     return nullptr;
1537   Instruction *LastInst = Insts.pop_back_val();
1538   LastInst->removeFromParent();
1539 
1540   for (auto *Inst : Insts)
1541     Worklist.Add(Inst);
1542   return LastInst;
1543 }
1544 
1545 /// If all elements of two constant vectors are 0/-1 and inverses, return true.
1546 static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
1547   unsigned NumElts = C1->getType()->getVectorNumElements();
1548   for (unsigned i = 0; i != NumElts; ++i) {
1549     Constant *EltC1 = C1->getAggregateElement(i);
1550     Constant *EltC2 = C2->getAggregateElement(i);
1551     if (!EltC1 || !EltC2)
1552       return false;
1553 
1554     // One element must be all ones, and the other must be all zeros.
1555     // FIXME: Allow undef elements.
1556     if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
1557           (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
1558       return false;
1559   }
1560   return true;
1561 }
1562 
1563 /// We have an expression of the form (A & C) | (B & D). If A is a scalar or
1564 /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
1565 /// B, it can be used as the condition operand of a select instruction.
1566 static Value *getSelectCondition(Value *A, Value *B,
1567                                  InstCombiner::BuilderTy &Builder) {
1568   // If these are scalars or vectors of i1, A can be used directly.
1569   Type *Ty = A->getType();
1570   if (match(A, m_Not(m_Specific(B))) && Ty->getScalarType()->isIntegerTy(1))
1571     return A;
1572 
1573   // If A and B are sign-extended, look through the sexts to find the booleans.
1574   Value *Cond;
1575   if (match(A, m_SExt(m_Value(Cond))) &&
1576       Cond->getType()->getScalarType()->isIntegerTy(1) &&
1577       match(B, m_CombineOr(m_Not(m_SExt(m_Specific(Cond))),
1578                            m_SExt(m_Not(m_Specific(Cond))))))
1579     return Cond;
1580 
1581   // All scalar (and most vector) possibilities should be handled now.
1582   // Try more matches that only apply to non-splat constant vectors.
1583   if (!Ty->isVectorTy())
1584     return nullptr;
1585 
1586   // If both operands are constants, see if the constants are inverse bitmasks.
1587   Constant *AC, *BC;
1588   if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) &&
1589       areInverseVectorBitmasks(AC, BC))
1590     return ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1591 
1592   // If both operands are xor'd with constants using the same sexted boolean
1593   // operand, see if the constants are inverse bitmasks.
1594   if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AC)))) &&
1595       match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BC)))) &&
1596       Cond->getType()->getScalarType()->isIntegerTy(1) &&
1597       areInverseVectorBitmasks(AC, BC)) {
1598     AC = ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1599     return Builder.CreateXor(Cond, AC);
1600   }
1601   return nullptr;
1602 }
1603 
1604 /// We have an expression of the form (A & C) | (B & D). Try to simplify this
1605 /// to "A' ? C : D", where A' is a boolean or vector of booleans.
1606 static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D,
1607                                    InstCombiner::BuilderTy &Builder) {
1608   // The potential condition of the select may be bitcasted. In that case, look
1609   // through its bitcast and the corresponding bitcast of the 'not' condition.
1610   Type *OrigType = A->getType();
1611   Value *SrcA, *SrcB;
1612   if (match(A, m_OneUse(m_BitCast(m_Value(SrcA)))) &&
1613       match(B, m_OneUse(m_BitCast(m_Value(SrcB))))) {
1614     A = SrcA;
1615     B = SrcB;
1616   }
1617 
1618   if (Value *Cond = getSelectCondition(A, B, Builder)) {
1619     // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
1620     // The bitcasts will either all exist or all not exist. The builder will
1621     // not create unnecessary casts if the types already match.
1622     Value *BitcastC = Builder.CreateBitCast(C, A->getType());
1623     Value *BitcastD = Builder.CreateBitCast(D, A->getType());
1624     Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
1625     return Builder.CreateBitCast(Select, OrigType);
1626   }
1627 
1628   return nullptr;
1629 }
1630 
1631 /// Fold (icmp)|(icmp) if possible.
1632 Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
1633                                    Instruction *CxtI) {
1634   ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1635 
1636   // Fold (iszero(A & K1) | iszero(A & K2)) ->  (A & (K1 | K2)) != (K1 | K2)
1637   // if K1 and K2 are a one-bit mask.
1638   ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1639   ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1640 
1641   if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero() &&
1642       RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1643 
1644     BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0));
1645     BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0));
1646     if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
1647         LAnd->getOpcode() == Instruction::And &&
1648         RAnd->getOpcode() == Instruction::And) {
1649 
1650       Value *Mask = nullptr;
1651       Value *Masked = nullptr;
1652       if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
1653           isKnownToBeAPowerOfTwo(LAnd->getOperand(1), DL, false, 0, &AC, CxtI,
1654                                  &DT) &&
1655           isKnownToBeAPowerOfTwo(RAnd->getOperand(1), DL, false, 0, &AC, CxtI,
1656                                  &DT)) {
1657         Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
1658         Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
1659       } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
1660                  isKnownToBeAPowerOfTwo(LAnd->getOperand(0), DL, false, 0, &AC,
1661                                         CxtI, &DT) &&
1662                  isKnownToBeAPowerOfTwo(RAnd->getOperand(0), DL, false, 0, &AC,
1663                                         CxtI, &DT)) {
1664         Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
1665         Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
1666       }
1667 
1668       if (Masked)
1669         return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
1670     }
1671   }
1672 
1673   // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3)
1674   //                   -->  (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3)
1675   // The original condition actually refers to the following two ranges:
1676   // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3]
1677   // We can fold these two ranges if:
1678   // 1) C1 and C2 is unsigned greater than C3.
1679   // 2) The two ranges are separated.
1680   // 3) C1 ^ C2 is one-bit mask.
1681   // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask.
1682   // This implies all values in the two ranges differ by exactly one bit.
1683 
1684   if ((LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_ULE) &&
1685       LHSCC == RHSCC && LHSCst && RHSCst && LHS->hasOneUse() &&
1686       RHS->hasOneUse() && LHSCst->getType() == RHSCst->getType() &&
1687       LHSCst->getValue() == (RHSCst->getValue())) {
1688 
1689     Value *LAdd = LHS->getOperand(0);
1690     Value *RAdd = RHS->getOperand(0);
1691 
1692     Value *LAddOpnd, *RAddOpnd;
1693     ConstantInt *LAddCst, *RAddCst;
1694     if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddCst))) &&
1695         match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddCst))) &&
1696         LAddCst->getValue().ugt(LHSCst->getValue()) &&
1697         RAddCst->getValue().ugt(LHSCst->getValue())) {
1698 
1699       APInt DiffCst = LAddCst->getValue() ^ RAddCst->getValue();
1700       if (LAddOpnd == RAddOpnd && DiffCst.isPowerOf2()) {
1701         ConstantInt *MaxAddCst = nullptr;
1702         if (LAddCst->getValue().ult(RAddCst->getValue()))
1703           MaxAddCst = RAddCst;
1704         else
1705           MaxAddCst = LAddCst;
1706 
1707         APInt RRangeLow = -RAddCst->getValue();
1708         APInt RRangeHigh = RRangeLow + LHSCst->getValue();
1709         APInt LRangeLow = -LAddCst->getValue();
1710         APInt LRangeHigh = LRangeLow + LHSCst->getValue();
1711         APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1712         APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1713         APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow
1714                                                    : RRangeLow - LRangeLow;
1715 
1716         if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff &&
1717             RangeDiff.ugt(LHSCst->getValue())) {
1718           Value *MaskCst = ConstantInt::get(LAddCst->getType(), ~DiffCst);
1719 
1720           Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskCst);
1721           Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddCst);
1722           return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSCst));
1723         }
1724       }
1725     }
1726   }
1727 
1728   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1729   if (PredicatesFoldable(LHSCC, RHSCC)) {
1730     if (LHS->getOperand(0) == RHS->getOperand(1) &&
1731         LHS->getOperand(1) == RHS->getOperand(0))
1732       LHS->swapOperands();
1733     if (LHS->getOperand(0) == RHS->getOperand(0) &&
1734         LHS->getOperand(1) == RHS->getOperand(1)) {
1735       Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1736       unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1737       bool isSigned = LHS->isSigned() || RHS->isSigned();
1738       return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
1739     }
1740   }
1741 
1742   // handle (roughly):
1743   // (icmp ne (A & B), C) | (icmp ne (A & D), E)
1744   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
1745     return V;
1746 
1747   Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1748   if (LHS->hasOneUse() || RHS->hasOneUse()) {
1749     // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1750     // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
1751     Value *A = nullptr, *B = nullptr;
1752     if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) {
1753       B = Val;
1754       if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1))
1755         A = Val2;
1756       else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2)
1757         A = RHS->getOperand(1);
1758     }
1759     // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1760     // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
1761     else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1762       B = Val2;
1763       if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1))
1764         A = Val;
1765       else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val)
1766         A = LHS->getOperand(1);
1767     }
1768     if (A && B)
1769       return Builder->CreateICmp(
1770           ICmpInst::ICMP_UGE,
1771           Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1772   }
1773 
1774   // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
1775   if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true))
1776     return V;
1777 
1778   // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
1779   if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
1780     return V;
1781 
1782   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
1783   if (!LHSCst || !RHSCst) return nullptr;
1784 
1785   if (LHSCst == RHSCst && LHSCC == RHSCC) {
1786     // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1787     if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1788       Value *NewOr = Builder->CreateOr(Val, Val2);
1789       return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1790     }
1791   }
1792 
1793   // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
1794   //   iff C2 + CA == C1.
1795   if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
1796     ConstantInt *AddCst;
1797     if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1798       if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
1799         return Builder->CreateICmpULE(Val, LHSCst);
1800   }
1801 
1802   // From here on, we only handle:
1803   //    (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1804   if (Val != Val2) return nullptr;
1805 
1806   // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1807   if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1808       RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1809       LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1810       RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1811     return nullptr;
1812 
1813   // We can't fold (ugt x, C) | (sgt x, C2).
1814   if (!PredicatesFoldable(LHSCC, RHSCC))
1815     return nullptr;
1816 
1817   // Ensure that the larger constant is on the RHS.
1818   bool ShouldSwap;
1819   if (CmpInst::isSigned(LHSCC) ||
1820       (ICmpInst::isEquality(LHSCC) &&
1821        CmpInst::isSigned(RHSCC)))
1822     ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1823   else
1824     ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
1825 
1826   if (ShouldSwap) {
1827     std::swap(LHS, RHS);
1828     std::swap(LHSCst, RHSCst);
1829     std::swap(LHSCC, RHSCC);
1830   }
1831 
1832   // At this point, we know we have two icmp instructions
1833   // comparing a value against two constants and or'ing the result
1834   // together.  Because of the above check, we know that we only have
1835   // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1836   // icmp folding check above), that the two constants are not
1837   // equal.
1838   assert(LHSCst != RHSCst && "Compares not folded above?");
1839 
1840   switch (LHSCC) {
1841   default: llvm_unreachable("Unknown integer condition code!");
1842   case ICmpInst::ICMP_EQ:
1843     switch (RHSCC) {
1844     default: llvm_unreachable("Unknown integer condition code!");
1845     case ICmpInst::ICMP_EQ:
1846       if (LHS->getOperand(0) == RHS->getOperand(0)) {
1847         // if LHSCst and RHSCst differ only by one bit:
1848         // (A == C1 || A == C2) -> (A | (C1 ^ C2)) == C2
1849         assert(LHSCst->getValue().ule(LHSCst->getValue()));
1850 
1851         APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1852         if (Xor.isPowerOf2()) {
1853           Value *Cst = Builder->getInt(Xor);
1854           Value *Or = Builder->CreateOr(LHS->getOperand(0), Cst);
1855           return Builder->CreateICmp(ICmpInst::ICMP_EQ, Or, RHSCst);
1856         }
1857       }
1858 
1859       if (LHSCst == SubOne(RHSCst)) {
1860         // (X == 13 | X == 14) -> X-13 <u 2
1861         Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1862         Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1863         AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1864         return Builder->CreateICmpULT(Add, AddCST);
1865       }
1866 
1867       break;                         // (X == 13 | X == 15) -> no change
1868     case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change
1869     case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change
1870       break;
1871     case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15
1872     case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15
1873     case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15
1874       return RHS;
1875     }
1876     break;
1877   case ICmpInst::ICMP_NE:
1878     switch (RHSCC) {
1879     default: llvm_unreachable("Unknown integer condition code!");
1880     case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13
1881     case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13
1882     case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13
1883       return LHS;
1884     case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true
1885     case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true
1886     case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true
1887       return Builder->getTrue();
1888     }
1889   case ICmpInst::ICMP_ULT:
1890     switch (RHSCC) {
1891     default: llvm_unreachable("Unknown integer condition code!");
1892     case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
1893       break;
1894     case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) -> (X-13) u> 2
1895       // If RHSCst is [us]MAXINT, it is always false.  Not handling
1896       // this can cause overflow.
1897       if (RHSCst->isMaxValue(false))
1898         return LHS;
1899       return insertRangeTest(Val, LHSCst->getValue(), RHSCst->getValue() + 1,
1900                              false, false);
1901     case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
1902       break;
1903     case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15
1904     case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15
1905       return RHS;
1906     case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change
1907       break;
1908     }
1909     break;
1910   case ICmpInst::ICMP_SLT:
1911     switch (RHSCC) {
1912     default: llvm_unreachable("Unknown integer condition code!");
1913     case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
1914       break;
1915     case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) -> (X-13) s> 2
1916       // If RHSCst is [us]MAXINT, it is always false.  Not handling
1917       // this can cause overflow.
1918       if (RHSCst->isMaxValue(true))
1919         return LHS;
1920       return insertRangeTest(Val, LHSCst->getValue(), RHSCst->getValue() + 1,
1921                              true, false);
1922     case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
1923       break;
1924     case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15
1925     case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15
1926       return RHS;
1927     case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change
1928       break;
1929     }
1930     break;
1931   case ICmpInst::ICMP_UGT:
1932     switch (RHSCC) {
1933     default: llvm_unreachable("Unknown integer condition code!");
1934     case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13
1935     case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13
1936       return LHS;
1937     case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change
1938       break;
1939     case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true
1940     case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true
1941       return Builder->getTrue();
1942     case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change
1943       break;
1944     }
1945     break;
1946   case ICmpInst::ICMP_SGT:
1947     switch (RHSCC) {
1948     default: llvm_unreachable("Unknown integer condition code!");
1949     case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13
1950     case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13
1951       return LHS;
1952     case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change
1953       break;
1954     case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true
1955     case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true
1956       return Builder->getTrue();
1957     case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change
1958       break;
1959     }
1960     break;
1961   }
1962   return nullptr;
1963 }
1964 
1965 /// Optimize (fcmp)|(fcmp).  NOTE: Unlike the rest of instcombine, this returns
1966 /// a Value which should already be inserted into the function.
1967 Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
1968   Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1969   Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1970   FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1971 
1972   if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1973     // Swap RHS operands to match LHS.
1974     Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1975     std::swap(Op1LHS, Op1RHS);
1976   }
1977 
1978   // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1979   // This is a similar transformation to the one in FoldAndOfFCmps.
1980   //
1981   // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1982   //    bool(R & CC0) || bool(R & CC1)
1983   //  = bool((R & CC0) | (R & CC1))
1984   //  = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
1985   if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1986     return getFCmpValue(getFCmpCode(Op0CC) | getFCmpCode(Op1CC), Op0LHS, Op0RHS,
1987                         Builder);
1988 
1989   if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
1990       RHS->getPredicate() == FCmpInst::FCMP_UNO &&
1991       LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1992     if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1993       if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1994         // If either of the constants are nans, then the whole thing returns
1995         // true.
1996         if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1997           return Builder->getTrue();
1998 
1999         // Otherwise, no need to compare the two constants, compare the
2000         // rest.
2001         return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
2002       }
2003 
2004     // Handle vector zeros.  This occurs because the canonical form of
2005     // "fcmp uno x,x" is "fcmp uno x, 0".
2006     if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
2007         isa<ConstantAggregateZero>(RHS->getOperand(1)))
2008       return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
2009 
2010     return nullptr;
2011   }
2012 
2013   return nullptr;
2014 }
2015 
2016 /// This helper function folds:
2017 ///
2018 ///     ((A | B) & C1) | (B & C2)
2019 ///
2020 /// into:
2021 ///
2022 ///     (A & C1) | B
2023 ///
2024 /// when the XOR of the two constants is "all ones" (-1).
2025 Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
2026                                                Value *A, Value *B, Value *C) {
2027   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
2028   if (!CI1) return nullptr;
2029 
2030   Value *V1 = nullptr;
2031   ConstantInt *CI2 = nullptr;
2032   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr;
2033 
2034   APInt Xor = CI1->getValue() ^ CI2->getValue();
2035   if (!Xor.isAllOnesValue()) return nullptr;
2036 
2037   if (V1 == A || V1 == B) {
2038     Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
2039     return BinaryOperator::CreateOr(NewOp, V1);
2040   }
2041 
2042   return nullptr;
2043 }
2044 
2045 /// \brief This helper function folds:
2046 ///
2047 ///     ((A | B) & C1) ^ (B & C2)
2048 ///
2049 /// into:
2050 ///
2051 ///     (A & C1) ^ B
2052 ///
2053 /// when the XOR of the two constants is "all ones" (-1).
2054 Instruction *InstCombiner::FoldXorWithConstants(BinaryOperator &I, Value *Op,
2055                                                 Value *A, Value *B, Value *C) {
2056   ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
2057   if (!CI1)
2058     return nullptr;
2059 
2060   Value *V1 = nullptr;
2061   ConstantInt *CI2 = nullptr;
2062   if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2))))
2063     return nullptr;
2064 
2065   APInt Xor = CI1->getValue() ^ CI2->getValue();
2066   if (!Xor.isAllOnesValue())
2067     return nullptr;
2068 
2069   if (V1 == A || V1 == B) {
2070     Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1);
2071     return BinaryOperator::CreateXor(NewOp, V1);
2072   }
2073 
2074   return nullptr;
2075 }
2076 
2077 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
2078   bool Changed = SimplifyAssociativeOrCommutative(I);
2079   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2080 
2081   if (Value *V = SimplifyVectorOp(I))
2082     return replaceInstUsesWith(I, V);
2083 
2084   if (Value *V = SimplifyOrInst(Op0, Op1, DL, &TLI, &DT, &AC))
2085     return replaceInstUsesWith(I, V);
2086 
2087   // (A&B)|(A&C) -> A&(B|C) etc
2088   if (Value *V = SimplifyUsingDistributiveLaws(I))
2089     return replaceInstUsesWith(I, V);
2090 
2091   // See if we can simplify any instructions used by the instruction whose sole
2092   // purpose is to compute bits we don't care about.
2093   if (SimplifyDemandedInstructionBits(I))
2094     return &I;
2095 
2096   if (Value *V = SimplifyBSwap(I))
2097     return replaceInstUsesWith(I, V);
2098 
2099   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2100     ConstantInt *C1 = nullptr; Value *X = nullptr;
2101     // (X & C1) | C2 --> (X | C2) & (C1|C2)
2102     // iff (C1 & C2) == 0.
2103     if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
2104         (RHS->getValue() & C1->getValue()) != 0 &&
2105         Op0->hasOneUse()) {
2106       Value *Or = Builder->CreateOr(X, RHS);
2107       Or->takeName(Op0);
2108       return BinaryOperator::CreateAnd(Or,
2109                              Builder->getInt(RHS->getValue() | C1->getValue()));
2110     }
2111 
2112     // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
2113     if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
2114         Op0->hasOneUse()) {
2115       Value *Or = Builder->CreateOr(X, RHS);
2116       Or->takeName(Op0);
2117       return BinaryOperator::CreateXor(Or,
2118                             Builder->getInt(C1->getValue() & ~RHS->getValue()));
2119     }
2120 
2121     // Try to fold constant and into select arguments.
2122     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2123       if (Instruction *R = FoldOpIntoSelect(I, SI))
2124         return R;
2125 
2126     if (isa<PHINode>(Op0))
2127       if (Instruction *NV = FoldOpIntoPhi(I))
2128         return NV;
2129   }
2130 
2131   // Given an OR instruction, check to see if this is a bswap.
2132   if (Instruction *BSwap = MatchBSwap(I))
2133     return BSwap;
2134 
2135   Value *A = nullptr, *B = nullptr;
2136   ConstantInt *C1 = nullptr, *C2 = nullptr;
2137 
2138   // (X^C)|Y -> (X|Y)^C iff Y&C == 0
2139   if (Op0->hasOneUse() &&
2140       match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
2141       MaskedValueIsZero(Op1, C1->getValue(), 0, &I)) {
2142     Value *NOr = Builder->CreateOr(A, Op1);
2143     NOr->takeName(Op0);
2144     return BinaryOperator::CreateXor(NOr, C1);
2145   }
2146 
2147   // Y|(X^C) -> (X|Y)^C iff Y&C == 0
2148   if (Op1->hasOneUse() &&
2149       match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
2150       MaskedValueIsZero(Op0, C1->getValue(), 0, &I)) {
2151     Value *NOr = Builder->CreateOr(A, Op0);
2152     NOr->takeName(Op0);
2153     return BinaryOperator::CreateXor(NOr, C1);
2154   }
2155 
2156   // ((~A & B) | A) -> (A | B)
2157   if (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2158       match(Op1, m_Specific(A)))
2159     return BinaryOperator::CreateOr(A, B);
2160 
2161   // ((A & B) | ~A) -> (~A | B)
2162   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
2163       match(Op1, m_Not(m_Specific(A))))
2164     return BinaryOperator::CreateOr(Builder->CreateNot(A), B);
2165 
2166   // (A & (~B)) | (A ^ B) -> (A ^ B)
2167   if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2168       match(Op1, m_Xor(m_Specific(A), m_Specific(B))))
2169     return BinaryOperator::CreateXor(A, B);
2170 
2171   // (A ^ B) | ( A & (~B)) -> (A ^ B)
2172   if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
2173       match(Op1, m_And(m_Specific(A), m_Not(m_Specific(B)))))
2174     return BinaryOperator::CreateXor(A, B);
2175 
2176   // (A & C)|(B & D)
2177   Value *C = nullptr, *D = nullptr;
2178   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2179       match(Op1, m_And(m_Value(B), m_Value(D)))) {
2180     Value *V1 = nullptr, *V2 = nullptr;
2181     C1 = dyn_cast<ConstantInt>(C);
2182     C2 = dyn_cast<ConstantInt>(D);
2183     if (C1 && C2) {  // (A & C1)|(B & C2)
2184       if ((C1->getValue() & C2->getValue()) == 0) {
2185         // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
2186         // iff (C1&C2) == 0 and (N&~C1) == 0
2187         if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
2188             ((V1 == B &&
2189               MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N)
2190              (V2 == B &&
2191               MaskedValueIsZero(V1, ~C1->getValue(), 0, &I))))  // (N|V)
2192           return BinaryOperator::CreateAnd(A,
2193                                 Builder->getInt(C1->getValue()|C2->getValue()));
2194         // Or commutes, try both ways.
2195         if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
2196             ((V1 == A &&
2197               MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
2198              (V2 == A &&
2199               MaskedValueIsZero(V1, ~C2->getValue(), 0, &I))))  // (N|V)
2200           return BinaryOperator::CreateAnd(B,
2201                                 Builder->getInt(C1->getValue()|C2->getValue()));
2202 
2203         // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
2204         // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
2205         ConstantInt *C3 = nullptr, *C4 = nullptr;
2206         if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
2207             (C3->getValue() & ~C1->getValue()) == 0 &&
2208             match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
2209             (C4->getValue() & ~C2->getValue()) == 0) {
2210           V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
2211           return BinaryOperator::CreateAnd(V2,
2212                                 Builder->getInt(C1->getValue()|C2->getValue()));
2213         }
2214       }
2215     }
2216 
2217     // Don't try to form a select if it's unlikely that we'll get rid of at
2218     // least one of the operands. A select is generally more expensive than the
2219     // 'or' that it is replacing.
2220     if (Op0->hasOneUse() || Op1->hasOneUse()) {
2221       // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2222       if (Value *V = matchSelectFromAndOr(A, C, B, D, *Builder))
2223         return replaceInstUsesWith(I, V);
2224       if (Value *V = matchSelectFromAndOr(A, C, D, B, *Builder))
2225         return replaceInstUsesWith(I, V);
2226       if (Value *V = matchSelectFromAndOr(C, A, B, D, *Builder))
2227         return replaceInstUsesWith(I, V);
2228       if (Value *V = matchSelectFromAndOr(C, A, D, B, *Builder))
2229         return replaceInstUsesWith(I, V);
2230       if (Value *V = matchSelectFromAndOr(B, D, A, C, *Builder))
2231         return replaceInstUsesWith(I, V);
2232       if (Value *V = matchSelectFromAndOr(B, D, C, A, *Builder))
2233         return replaceInstUsesWith(I, V);
2234       if (Value *V = matchSelectFromAndOr(D, B, A, C, *Builder))
2235         return replaceInstUsesWith(I, V);
2236       if (Value *V = matchSelectFromAndOr(D, B, C, A, *Builder))
2237         return replaceInstUsesWith(I, V);
2238     }
2239 
2240     // ((A&~B)|(~A&B)) -> A^B
2241     if ((match(C, m_Not(m_Specific(D))) &&
2242          match(B, m_Not(m_Specific(A)))))
2243       return BinaryOperator::CreateXor(A, D);
2244     // ((~B&A)|(~A&B)) -> A^B
2245     if ((match(A, m_Not(m_Specific(D))) &&
2246          match(B, m_Not(m_Specific(C)))))
2247       return BinaryOperator::CreateXor(C, D);
2248     // ((A&~B)|(B&~A)) -> A^B
2249     if ((match(C, m_Not(m_Specific(B))) &&
2250          match(D, m_Not(m_Specific(A)))))
2251       return BinaryOperator::CreateXor(A, B);
2252     // ((~B&A)|(B&~A)) -> A^B
2253     if ((match(A, m_Not(m_Specific(B))) &&
2254          match(D, m_Not(m_Specific(C)))))
2255       return BinaryOperator::CreateXor(C, B);
2256 
2257     // ((A|B)&1)|(B&-2) -> (A&1) | B
2258     if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2259         match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2260       Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2261       if (Ret) return Ret;
2262     }
2263     // (B&-2)|((A|B)&1) -> (A&1) | B
2264     if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2265         match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2266       Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2267       if (Ret) return Ret;
2268     }
2269     // ((A^B)&1)|(B&-2) -> (A&1) ^ B
2270     if (match(A, m_Xor(m_Value(V1), m_Specific(B))) ||
2271         match(A, m_Xor(m_Specific(B), m_Value(V1)))) {
2272       Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C);
2273       if (Ret) return Ret;
2274     }
2275     // (B&-2)|((A^B)&1) -> (A&1) ^ B
2276     if (match(B, m_Xor(m_Specific(A), m_Value(V1))) ||
2277         match(B, m_Xor(m_Value(V1), m_Specific(A)))) {
2278       Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D);
2279       if (Ret) return Ret;
2280     }
2281   }
2282 
2283   // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2284   if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2285     if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2286       if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
2287         return BinaryOperator::CreateOr(Op0, C);
2288 
2289   // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2290   if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2291     if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2292       if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
2293         return BinaryOperator::CreateOr(Op1, C);
2294 
2295   // ((B | C) & A) | B -> B | (A & C)
2296   if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2297     return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C));
2298 
2299   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2300     return DeMorgan;
2301 
2302   // Canonicalize xor to the RHS.
2303   bool SwappedForXor = false;
2304   if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2305     std::swap(Op0, Op1);
2306     SwappedForXor = true;
2307   }
2308 
2309   // A | ( A ^ B) -> A |  B
2310   // A | (~A ^ B) -> A | ~B
2311   // (A & B) | (A ^ B)
2312   if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2313     if (Op0 == A || Op0 == B)
2314       return BinaryOperator::CreateOr(A, B);
2315 
2316     if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2317         match(Op0, m_And(m_Specific(B), m_Specific(A))))
2318       return BinaryOperator::CreateOr(A, B);
2319 
2320     if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2321       Value *Not = Builder->CreateNot(B, B->getName()+".not");
2322       return BinaryOperator::CreateOr(Not, Op0);
2323     }
2324     if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2325       Value *Not = Builder->CreateNot(A, A->getName()+".not");
2326       return BinaryOperator::CreateOr(Not, Op0);
2327     }
2328   }
2329 
2330   // A | ~(A | B) -> A | ~B
2331   // A | ~(A ^ B) -> A | ~B
2332   if (match(Op1, m_Not(m_Value(A))))
2333     if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2334       if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2335           Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2336                                B->getOpcode() == Instruction::Xor)) {
2337         Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2338                                                  B->getOperand(0);
2339         Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2340         return BinaryOperator::CreateOr(Not, Op0);
2341       }
2342 
2343   // (A & B) | ((~A) ^ B) -> (~A ^ B)
2344   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
2345       match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B))))
2346     return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2347 
2348   // ((~A) ^ B) | (A & B) -> (~A ^ B)
2349   if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2350       match(Op1, m_And(m_Specific(A), m_Specific(B))))
2351     return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2352 
2353   if (SwappedForXor)
2354     std::swap(Op0, Op1);
2355 
2356   {
2357     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2358     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2359     if (LHS && RHS)
2360       if (Value *Res = FoldOrOfICmps(LHS, RHS, &I))
2361         return replaceInstUsesWith(I, Res);
2362 
2363     // TODO: Make this recursive; it's a little tricky because an arbitrary
2364     // number of 'or' instructions might have to be created.
2365     Value *X, *Y;
2366     if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2367       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2368         if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
2369           return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
2370       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2371         if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
2372           return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
2373     }
2374     if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2375       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2376         if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
2377           return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
2378       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2379         if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
2380           return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
2381     }
2382   }
2383 
2384   // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
2385   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2386     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2387       if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2388         return replaceInstUsesWith(I, Res);
2389 
2390   if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2391     return CastedOr;
2392 
2393   // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
2394   if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2395       A->getType()->getScalarType()->isIntegerTy(1))
2396     return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
2397   if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2398       A->getType()->getScalarType()->isIntegerTy(1))
2399     return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2400 
2401   // Note: If we've gotten to the point of visiting the outer OR, then the
2402   // inner one couldn't be simplified.  If it was a constant, then it won't
2403   // be simplified by a later pass either, so we try swapping the inner/outer
2404   // ORs in the hopes that we'll be able to simplify it this way.
2405   // (X|C) | V --> (X|V) | C
2406   if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2407       match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2408     Value *Inner = Builder->CreateOr(A, Op1);
2409     Inner->takeName(Op0);
2410     return BinaryOperator::CreateOr(Inner, C1);
2411   }
2412 
2413   // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2414   // Since this OR statement hasn't been optimized further yet, we hope
2415   // that this transformation will allow the new ORs to be optimized.
2416   {
2417     Value *X = nullptr, *Y = nullptr;
2418     if (Op0->hasOneUse() && Op1->hasOneUse() &&
2419         match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2420         match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2421       Value *orTrue = Builder->CreateOr(A, C);
2422       Value *orFalse = Builder->CreateOr(B, D);
2423       return SelectInst::Create(X, orTrue, orFalse);
2424     }
2425   }
2426 
2427   return Changed ? &I : nullptr;
2428 }
2429 
2430 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
2431   bool Changed = SimplifyAssociativeOrCommutative(I);
2432   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2433 
2434   if (Value *V = SimplifyVectorOp(I))
2435     return replaceInstUsesWith(I, V);
2436 
2437   if (Value *V = SimplifyXorInst(Op0, Op1, DL, &TLI, &DT, &AC))
2438     return replaceInstUsesWith(I, V);
2439 
2440   // (A&B)^(A&C) -> A&(B^C) etc
2441   if (Value *V = SimplifyUsingDistributiveLaws(I))
2442     return replaceInstUsesWith(I, V);
2443 
2444   // See if we can simplify any instructions used by the instruction whose sole
2445   // purpose is to compute bits we don't care about.
2446   if (SimplifyDemandedInstructionBits(I))
2447     return &I;
2448 
2449   if (Value *V = SimplifyBSwap(I))
2450     return replaceInstUsesWith(I, V);
2451 
2452   // Is this a ~ operation?
2453   if (Value *NotOp = dyn_castNotVal(&I)) {
2454     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
2455       if (Op0I->getOpcode() == Instruction::And ||
2456           Op0I->getOpcode() == Instruction::Or) {
2457         // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2458         // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2459         if (dyn_castNotVal(Op0I->getOperand(1)))
2460           Op0I->swapOperands();
2461         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2462           Value *NotY =
2463             Builder->CreateNot(Op0I->getOperand(1),
2464                                Op0I->getOperand(1)->getName()+".not");
2465           if (Op0I->getOpcode() == Instruction::And)
2466             return BinaryOperator::CreateOr(Op0NotVal, NotY);
2467           return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2468         }
2469 
2470         // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2471         // ~(X | Y) === (~X & ~Y) - De Morgan's Law
2472         if (IsFreeToInvert(Op0I->getOperand(0),
2473                            Op0I->getOperand(0)->hasOneUse()) &&
2474             IsFreeToInvert(Op0I->getOperand(1),
2475                            Op0I->getOperand(1)->hasOneUse())) {
2476           Value *NotX =
2477             Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2478           Value *NotY =
2479             Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2480           if (Op0I->getOpcode() == Instruction::And)
2481             return BinaryOperator::CreateOr(NotX, NotY);
2482           return BinaryOperator::CreateAnd(NotX, NotY);
2483         }
2484 
2485       } else if (Op0I->getOpcode() == Instruction::AShr) {
2486         // ~(~X >>s Y) --> (X >>s Y)
2487         if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2488           return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
2489       }
2490     }
2491   }
2492 
2493   if (Constant *RHS = dyn_cast<Constant>(Op1)) {
2494     if (RHS->isAllOnesValue() && Op0->hasOneUse())
2495       // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
2496       if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2497         return CmpInst::Create(CI->getOpcode(),
2498                                CI->getInversePredicate(),
2499                                CI->getOperand(0), CI->getOperand(1));
2500   }
2501 
2502   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2503     // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2504     if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2505       if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2506         if (CI->hasOneUse() && Op0C->hasOneUse()) {
2507           Instruction::CastOps Opcode = Op0C->getOpcode();
2508           if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
2509               (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(),
2510                                             Op0C->getDestTy()))) {
2511             CI->setPredicate(CI->getInversePredicate());
2512             return CastInst::Create(Opcode, CI, Op0C->getType());
2513           }
2514         }
2515       }
2516     }
2517 
2518     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2519       // ~(c-X) == X-c-1 == X+(-c-1)
2520       if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2521         if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2522           Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2523           Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2524                                       ConstantInt::get(I.getType(), 1));
2525           return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2526         }
2527 
2528       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2529         if (Op0I->getOpcode() == Instruction::Add) {
2530           // ~(X-c) --> (-c-1)-X
2531           if (RHS->isAllOnesValue()) {
2532             Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2533             return BinaryOperator::CreateSub(
2534                            ConstantExpr::getSub(NegOp0CI,
2535                                       ConstantInt::get(I.getType(), 1)),
2536                                       Op0I->getOperand(0));
2537           } else if (RHS->getValue().isSignBit()) {
2538             // (X + C) ^ signbit -> (X + C + signbit)
2539             Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
2540             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2541 
2542           }
2543         } else if (Op0I->getOpcode() == Instruction::Or) {
2544           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
2545           if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(),
2546                                 0, &I)) {
2547             Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2548             // Anything in both C1 and C2 is known to be zero, remove it from
2549             // NewRHS.
2550             Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
2551             NewRHS = ConstantExpr::getAnd(NewRHS,
2552                                        ConstantExpr::getNot(CommonBits));
2553             Worklist.Add(Op0I);
2554             I.setOperand(0, Op0I->getOperand(0));
2555             I.setOperand(1, NewRHS);
2556             return &I;
2557           }
2558         } else if (Op0I->getOpcode() == Instruction::LShr) {
2559           // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2560           // E1 = "X ^ C1"
2561           BinaryOperator *E1;
2562           ConstantInt *C1;
2563           if (Op0I->hasOneUse() &&
2564               (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2565               E1->getOpcode() == Instruction::Xor &&
2566               (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2567             // fold (C1 >> C2) ^ C3
2568             ConstantInt *C2 = Op0CI, *C3 = RHS;
2569             APInt FoldConst = C1->getValue().lshr(C2->getValue());
2570             FoldConst ^= C3->getValue();
2571             // Prepare the two operands.
2572             Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2573             Opnd0->takeName(Op0I);
2574             cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2575             Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2576 
2577             return BinaryOperator::CreateXor(Opnd0, FoldVal);
2578           }
2579         }
2580       }
2581     }
2582 
2583     // Try to fold constant and into select arguments.
2584     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2585       if (Instruction *R = FoldOpIntoSelect(I, SI))
2586         return R;
2587     if (isa<PHINode>(Op0))
2588       if (Instruction *NV = FoldOpIntoPhi(I))
2589         return NV;
2590   }
2591 
2592   BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2593   if (Op1I) {
2594     Value *A, *B;
2595     if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2596       if (A == Op0) {              // B^(B|A) == (A|B)^B
2597         Op1I->swapOperands();
2598         I.swapOperands();
2599         std::swap(Op0, Op1);
2600       } else if (B == Op0) {       // B^(A|B) == (A|B)^B
2601         I.swapOperands();     // Simplified below.
2602         std::swap(Op0, Op1);
2603       }
2604     } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
2605                Op1I->hasOneUse()){
2606       if (A == Op0) {                                      // A^(A&B) -> A^(B&A)
2607         Op1I->swapOperands();
2608         std::swap(A, B);
2609       }
2610       if (B == Op0) {                                      // A^(B&A) -> (B&A)^A
2611         I.swapOperands();     // Simplified below.
2612         std::swap(Op0, Op1);
2613       }
2614     }
2615   }
2616 
2617   BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2618   if (Op0I) {
2619     Value *A, *B;
2620     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2621         Op0I->hasOneUse()) {
2622       if (A == Op1)                                  // (B|A)^B == (A|B)^B
2623         std::swap(A, B);
2624       if (B == Op1)                                  // (A|B)^B == A & ~B
2625         return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
2626     } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2627                Op0I->hasOneUse()){
2628       if (A == Op1)                                        // (A&B)^A -> (B&A)^A
2629         std::swap(A, B);
2630       if (B == Op1 &&                                      // (B&A)^A == ~B & A
2631           !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C
2632         return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
2633       }
2634     }
2635   }
2636 
2637   if (Op0I && Op1I) {
2638     Value *A, *B, *C, *D;
2639     // (A & B)^(A | B) -> A ^ B
2640     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2641         match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
2642       if ((A == C && B == D) || (A == D && B == C))
2643         return BinaryOperator::CreateXor(A, B);
2644     }
2645     // (A | B)^(A & B) -> A ^ B
2646     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2647         match(Op1I, m_And(m_Value(C), m_Value(D)))) {
2648       if ((A == C && B == D) || (A == D && B == C))
2649         return BinaryOperator::CreateXor(A, B);
2650     }
2651     // (A | ~B) ^ (~A | B) -> A ^ B
2652     if (match(Op0I, m_Or(m_Value(A), m_Not(m_Value(B)))) &&
2653         match(Op1I, m_Or(m_Not(m_Specific(A)), m_Specific(B)))) {
2654       return BinaryOperator::CreateXor(A, B);
2655     }
2656     // (~A | B) ^ (A | ~B) -> A ^ B
2657     if (match(Op0I, m_Or(m_Not(m_Value(A)), m_Value(B))) &&
2658         match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) {
2659       return BinaryOperator::CreateXor(A, B);
2660     }
2661     // (A & ~B) ^ (~A & B) -> A ^ B
2662     if (match(Op0I, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2663         match(Op1I, m_And(m_Not(m_Specific(A)), m_Specific(B)))) {
2664       return BinaryOperator::CreateXor(A, B);
2665     }
2666     // (~A & B) ^ (A & ~B) -> A ^ B
2667     if (match(Op0I, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2668         match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B))))) {
2669       return BinaryOperator::CreateXor(A, B);
2670     }
2671     // (A ^ C)^(A | B) -> ((~A) & B) ^ C
2672     if (match(Op0I, m_Xor(m_Value(D), m_Value(C))) &&
2673         match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2674       if (D == A)
2675         return BinaryOperator::CreateXor(
2676             Builder->CreateAnd(Builder->CreateNot(A), B), C);
2677       if (D == B)
2678         return BinaryOperator::CreateXor(
2679             Builder->CreateAnd(Builder->CreateNot(B), A), C);
2680     }
2681     // (A | B)^(A ^ C) -> ((~A) & B) ^ C
2682     if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2683         match(Op1I, m_Xor(m_Value(D), m_Value(C)))) {
2684       if (D == A)
2685         return BinaryOperator::CreateXor(
2686             Builder->CreateAnd(Builder->CreateNot(A), B), C);
2687       if (D == B)
2688         return BinaryOperator::CreateXor(
2689             Builder->CreateAnd(Builder->CreateNot(B), A), C);
2690     }
2691     // (A & B) ^ (A ^ B) -> (A | B)
2692     if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2693         match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))
2694       return BinaryOperator::CreateOr(A, B);
2695     // (A ^ B) ^ (A & B) -> (A | B)
2696     if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
2697         match(Op1I, m_And(m_Specific(A), m_Specific(B))))
2698       return BinaryOperator::CreateOr(A, B);
2699   }
2700 
2701   Value *A = nullptr, *B = nullptr;
2702   // (A & ~B) ^ (~A) -> ~(A & B)
2703   if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
2704       match(Op1, m_Not(m_Specific(A))))
2705     return BinaryOperator::CreateNot(Builder->CreateAnd(A, B));
2706 
2707   // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
2708   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2709     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
2710       if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2711         if (LHS->getOperand(0) == RHS->getOperand(1) &&
2712             LHS->getOperand(1) == RHS->getOperand(0))
2713           LHS->swapOperands();
2714         if (LHS->getOperand(0) == RHS->getOperand(0) &&
2715             LHS->getOperand(1) == RHS->getOperand(1)) {
2716           Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2717           unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2718           bool isSigned = LHS->isSigned() || RHS->isSigned();
2719           return replaceInstUsesWith(I,
2720                                getNewICmpValue(isSigned, Code, Op0, Op1,
2721                                                Builder));
2722         }
2723       }
2724 
2725   if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
2726     return CastedXor;
2727 
2728   return Changed ? &I : nullptr;
2729 }
2730