1 //===- InstCombineAndOrXor.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the visitAnd, visitOr, and visitXor functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "InstCombineInternal.h"
14 #include "llvm/Analysis/CmpInstAnalysis.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/InstCombine/InstCombiner.h"
20 #include "llvm/Transforms/Utils/Local.h"
21 
22 using namespace llvm;
23 using namespace PatternMatch;
24 
25 #define DEBUG_TYPE "instcombine"
26 
27 /// This is the complement of getICmpCode, which turns an opcode and two
28 /// operands into either a constant true or false, or a brand new ICmp
29 /// instruction. The sign is passed in to determine which kind of predicate to
30 /// use in the new icmp instruction.
31 static Value *getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS,
32                               InstCombiner::BuilderTy &Builder) {
33   ICmpInst::Predicate NewPred;
34   if (Constant *TorF = getPredForICmpCode(Code, Sign, LHS->getType(), NewPred))
35     return TorF;
36   return Builder.CreateICmp(NewPred, LHS, RHS);
37 }
38 
39 /// This is the complement of getFCmpCode, which turns an opcode and two
40 /// operands into either a FCmp instruction, or a true/false constant.
41 static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
42                            InstCombiner::BuilderTy &Builder) {
43   FCmpInst::Predicate NewPred;
44   if (Constant *TorF = getPredForFCmpCode(Code, LHS->getType(), NewPred))
45     return TorF;
46   return Builder.CreateFCmp(NewPred, LHS, RHS);
47 }
48 
49 /// Transform BITWISE_OP(BSWAP(A),BSWAP(B)) or
50 /// BITWISE_OP(BSWAP(A), Constant) to BSWAP(BITWISE_OP(A, B))
51 /// \param I Binary operator to transform.
52 /// \return Pointer to node that must replace the original binary operator, or
53 ///         null pointer if no transformation was made.
54 static Value *SimplifyBSwap(BinaryOperator &I,
55                             InstCombiner::BuilderTy &Builder) {
56   assert(I.isBitwiseLogicOp() && "Unexpected opcode for bswap simplifying");
57 
58   Value *OldLHS = I.getOperand(0);
59   Value *OldRHS = I.getOperand(1);
60 
61   Value *NewLHS;
62   if (!match(OldLHS, m_BSwap(m_Value(NewLHS))))
63     return nullptr;
64 
65   Value *NewRHS;
66   const APInt *C;
67 
68   if (match(OldRHS, m_BSwap(m_Value(NewRHS)))) {
69     // OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
70     if (!OldLHS->hasOneUse() && !OldRHS->hasOneUse())
71       return nullptr;
72     // NewRHS initialized by the matcher.
73   } else if (match(OldRHS, m_APInt(C))) {
74     // OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
75     if (!OldLHS->hasOneUse())
76       return nullptr;
77     NewRHS = ConstantInt::get(I.getType(), C->byteSwap());
78   } else
79     return nullptr;
80 
81   Value *BinOp = Builder.CreateBinOp(I.getOpcode(), NewLHS, NewRHS);
82   Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap,
83                                           I.getType());
84   return Builder.CreateCall(F, BinOp);
85 }
86 
87 /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
88 /// (V < Lo || V >= Hi). This method expects that Lo < Hi. IsSigned indicates
89 /// whether to treat V, Lo, and Hi as signed or not.
90 Value *InstCombinerImpl::insertRangeTest(Value *V, const APInt &Lo,
91                                          const APInt &Hi, bool isSigned,
92                                          bool Inside) {
93   assert((isSigned ? Lo.slt(Hi) : Lo.ult(Hi)) &&
94          "Lo is not < Hi in range emission code!");
95 
96   Type *Ty = V->getType();
97 
98   // V >= Min && V <  Hi --> V <  Hi
99   // V <  Min || V >= Hi --> V >= Hi
100   ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
101   if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
102     Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
103     return Builder.CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));
104   }
105 
106   // V >= Lo && V <  Hi --> V - Lo u<  Hi - Lo
107   // V <  Lo || V >= Hi --> V - Lo u>= Hi - Lo
108   Value *VMinusLo =
109       Builder.CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off");
110   Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo);
111   return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo);
112 }
113 
114 /// Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns
115 /// that can be simplified.
116 /// One of A and B is considered the mask. The other is the value. This is
117 /// described as the "AMask" or "BMask" part of the enum. If the enum contains
118 /// only "Mask", then both A and B can be considered masks. If A is the mask,
119 /// then it was proven that (A & C) == C. This is trivial if C == A or C == 0.
120 /// If both A and C are constants, this proof is also easy.
121 /// For the following explanations, we assume that A is the mask.
122 ///
123 /// "AllOnes" declares that the comparison is true only if (A & B) == A or all
124 /// bits of A are set in B.
125 ///   Example: (icmp eq (A & 3), 3) -> AMask_AllOnes
126 ///
127 /// "AllZeros" declares that the comparison is true only if (A & B) == 0 or all
128 /// bits of A are cleared in B.
129 ///   Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes
130 ///
131 /// "Mixed" declares that (A & B) == C and C might or might not contain any
132 /// number of one bits and zero bits.
133 ///   Example: (icmp eq (A & 3), 1) -> AMask_Mixed
134 ///
135 /// "Not" means that in above descriptions "==" should be replaced by "!=".
136 ///   Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes
137 ///
138 /// If the mask A contains a single bit, then the following is equivalent:
139 ///    (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
140 ///    (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
141 enum MaskedICmpType {
142   AMask_AllOnes           =     1,
143   AMask_NotAllOnes        =     2,
144   BMask_AllOnes           =     4,
145   BMask_NotAllOnes        =     8,
146   Mask_AllZeros           =    16,
147   Mask_NotAllZeros        =    32,
148   AMask_Mixed             =    64,
149   AMask_NotMixed          =   128,
150   BMask_Mixed             =   256,
151   BMask_NotMixed          =   512
152 };
153 
154 /// Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C)
155 /// satisfies.
156 static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
157                                   ICmpInst::Predicate Pred) {
158   const APInt *ConstA = nullptr, *ConstB = nullptr, *ConstC = nullptr;
159   match(A, m_APInt(ConstA));
160   match(B, m_APInt(ConstB));
161   match(C, m_APInt(ConstC));
162   bool IsEq = (Pred == ICmpInst::ICMP_EQ);
163   bool IsAPow2 = ConstA && ConstA->isPowerOf2();
164   bool IsBPow2 = ConstB && ConstB->isPowerOf2();
165   unsigned MaskVal = 0;
166   if (ConstC && ConstC->isZero()) {
167     // if C is zero, then both A and B qualify as mask
168     MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed)
169                      : (Mask_NotAllZeros | AMask_NotMixed | BMask_NotMixed));
170     if (IsAPow2)
171       MaskVal |= (IsEq ? (AMask_NotAllOnes | AMask_NotMixed)
172                        : (AMask_AllOnes | AMask_Mixed));
173     if (IsBPow2)
174       MaskVal |= (IsEq ? (BMask_NotAllOnes | BMask_NotMixed)
175                        : (BMask_AllOnes | BMask_Mixed));
176     return MaskVal;
177   }
178 
179   if (A == C) {
180     MaskVal |= (IsEq ? (AMask_AllOnes | AMask_Mixed)
181                      : (AMask_NotAllOnes | AMask_NotMixed));
182     if (IsAPow2)
183       MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed)
184                        : (Mask_AllZeros | AMask_Mixed));
185   } else if (ConstA && ConstC && ConstC->isSubsetOf(*ConstA)) {
186     MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed);
187   }
188 
189   if (B == C) {
190     MaskVal |= (IsEq ? (BMask_AllOnes | BMask_Mixed)
191                      : (BMask_NotAllOnes | BMask_NotMixed));
192     if (IsBPow2)
193       MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed)
194                        : (Mask_AllZeros | BMask_Mixed));
195   } else if (ConstB && ConstC && ConstC->isSubsetOf(*ConstB)) {
196     MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed);
197   }
198 
199   return MaskVal;
200 }
201 
202 /// Convert an analysis of a masked ICmp into its equivalent if all boolean
203 /// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
204 /// is adjacent to the corresponding normal flag (recording ==), this just
205 /// involves swapping those bits over.
206 static unsigned conjugateICmpMask(unsigned Mask) {
207   unsigned NewMask;
208   NewMask = (Mask & (AMask_AllOnes | BMask_AllOnes | Mask_AllZeros |
209                      AMask_Mixed | BMask_Mixed))
210             << 1;
211 
212   NewMask |= (Mask & (AMask_NotAllOnes | BMask_NotAllOnes | Mask_NotAllZeros |
213                       AMask_NotMixed | BMask_NotMixed))
214              >> 1;
215 
216   return NewMask;
217 }
218 
219 // Adapts the external decomposeBitTestICmp for local use.
220 static bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
221                                  Value *&X, Value *&Y, Value *&Z) {
222   APInt Mask;
223   if (!llvm::decomposeBitTestICmp(LHS, RHS, Pred, X, Mask))
224     return false;
225 
226   Y = ConstantInt::get(X->getType(), Mask);
227   Z = ConstantInt::get(X->getType(), 0);
228   return true;
229 }
230 
231 /// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).
232 /// Return the pattern classes (from MaskedICmpType) for the left hand side and
233 /// the right hand side as a pair.
234 /// LHS and RHS are the left hand side and the right hand side ICmps and PredL
235 /// and PredR are their predicates, respectively.
236 static
237 Optional<std::pair<unsigned, unsigned>>
238 getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C,
239                          Value *&D, Value *&E, ICmpInst *LHS,
240                          ICmpInst *RHS,
241                          ICmpInst::Predicate &PredL,
242                          ICmpInst::Predicate &PredR) {
243   // Don't allow pointers. Splat vectors are fine.
244   if (!LHS->getOperand(0)->getType()->isIntOrIntVectorTy() ||
245       !RHS->getOperand(0)->getType()->isIntOrIntVectorTy())
246     return None;
247 
248   // Here comes the tricky part:
249   // LHS might be of the form L11 & L12 == X, X == L21 & L22,
250   // and L11 & L12 == L21 & L22. The same goes for RHS.
251   // Now we must find those components L** and R**, that are equal, so
252   // that we can extract the parameters A, B, C, D, and E for the canonical
253   // above.
254   Value *L1 = LHS->getOperand(0);
255   Value *L2 = LHS->getOperand(1);
256   Value *L11, *L12, *L21, *L22;
257   // Check whether the icmp can be decomposed into a bit test.
258   if (decomposeBitTestICmp(L1, L2, PredL, L11, L12, L2)) {
259     L21 = L22 = L1 = nullptr;
260   } else {
261     // Look for ANDs in the LHS icmp.
262     if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
263       // Any icmp can be viewed as being trivially masked; if it allows us to
264       // remove one, it's worth it.
265       L11 = L1;
266       L12 = Constant::getAllOnesValue(L1->getType());
267     }
268 
269     if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
270       L21 = L2;
271       L22 = Constant::getAllOnesValue(L2->getType());
272     }
273   }
274 
275   // Bail if LHS was a icmp that can't be decomposed into an equality.
276   if (!ICmpInst::isEquality(PredL))
277     return None;
278 
279   Value *R1 = RHS->getOperand(0);
280   Value *R2 = RHS->getOperand(1);
281   Value *R11, *R12;
282   bool Ok = false;
283   if (decomposeBitTestICmp(R1, R2, PredR, R11, R12, R2)) {
284     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
285       A = R11;
286       D = R12;
287     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
288       A = R12;
289       D = R11;
290     } else {
291       return None;
292     }
293     E = R2;
294     R1 = nullptr;
295     Ok = true;
296   } else {
297     if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
298       // As before, model no mask as a trivial mask if it'll let us do an
299       // optimization.
300       R11 = R1;
301       R12 = Constant::getAllOnesValue(R1->getType());
302     }
303 
304     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
305       A = R11;
306       D = R12;
307       E = R2;
308       Ok = true;
309     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
310       A = R12;
311       D = R11;
312       E = R2;
313       Ok = true;
314     }
315   }
316 
317   // Bail if RHS was a icmp that can't be decomposed into an equality.
318   if (!ICmpInst::isEquality(PredR))
319     return None;
320 
321   // Look for ANDs on the right side of the RHS icmp.
322   if (!Ok) {
323     if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
324       R11 = R2;
325       R12 = Constant::getAllOnesValue(R2->getType());
326     }
327 
328     if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
329       A = R11;
330       D = R12;
331       E = R1;
332       Ok = true;
333     } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
334       A = R12;
335       D = R11;
336       E = R1;
337       Ok = true;
338     } else {
339       return None;
340     }
341 
342     assert(Ok && "Failed to find AND on the right side of the RHS icmp.");
343   }
344 
345   if (L11 == A) {
346     B = L12;
347     C = L2;
348   } else if (L12 == A) {
349     B = L11;
350     C = L2;
351   } else if (L21 == A) {
352     B = L22;
353     C = L1;
354   } else if (L22 == A) {
355     B = L21;
356     C = L1;
357   }
358 
359   unsigned LeftType = getMaskedICmpType(A, B, C, PredL);
360   unsigned RightType = getMaskedICmpType(A, D, E, PredR);
361   return Optional<std::pair<unsigned, unsigned>>(std::make_pair(LeftType, RightType));
362 }
363 
364 /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single
365 /// (icmp(A & X) ==/!= Y), where the left-hand side is of type Mask_NotAllZeros
366 /// and the right hand side is of type BMask_Mixed. For example,
367 /// (icmp (A & 12) != 0) & (icmp (A & 15) == 8) -> (icmp (A & 15) == 8).
368 /// Also used for logical and/or, must be poison safe.
369 static Value *foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
370     ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, Value *A, Value *B, Value *C,
371     Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR,
372     InstCombiner::BuilderTy &Builder) {
373   // We are given the canonical form:
374   //   (icmp ne (A & B), 0) & (icmp eq (A & D), E).
375   // where D & E == E.
376   //
377   // If IsAnd is false, we get it in negated form:
378   //   (icmp eq (A & B), 0) | (icmp ne (A & D), E) ->
379   //      !((icmp ne (A & B), 0) & (icmp eq (A & D), E)).
380   //
381   // We currently handle the case of B, C, D, E are constant.
382   //
383   const APInt *BCst, *CCst, *DCst, *OrigECst;
384   if (!match(B, m_APInt(BCst)) || !match(C, m_APInt(CCst)) ||
385       !match(D, m_APInt(DCst)) || !match(E, m_APInt(OrigECst)))
386     return nullptr;
387 
388   ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
389 
390   // Update E to the canonical form when D is a power of two and RHS is
391   // canonicalized as,
392   // (icmp ne (A & D), 0) -> (icmp eq (A & D), D) or
393   // (icmp ne (A & D), D) -> (icmp eq (A & D), 0).
394   APInt ECst = *OrigECst;
395   if (PredR != NewCC)
396     ECst ^= *DCst;
397 
398   // If B or D is zero, skip because if LHS or RHS can be trivially folded by
399   // other folding rules and this pattern won't apply any more.
400   if (*BCst == 0 || *DCst == 0)
401     return nullptr;
402 
403   // If B and D don't intersect, ie. (B & D) == 0, no folding because we can't
404   // deduce anything from it.
405   // For example,
406   // (icmp ne (A & 12), 0) & (icmp eq (A & 3), 1) -> no folding.
407   if ((*BCst & *DCst) == 0)
408     return nullptr;
409 
410   // If the following two conditions are met:
411   //
412   // 1. mask B covers only a single bit that's not covered by mask D, that is,
413   // (B & (B ^ D)) is a power of 2 (in other words, B minus the intersection of
414   // B and D has only one bit set) and,
415   //
416   // 2. RHS (and E) indicates that the rest of B's bits are zero (in other
417   // words, the intersection of B and D is zero), that is, ((B & D) & E) == 0
418   //
419   // then that single bit in B must be one and thus the whole expression can be
420   // folded to
421   //   (A & (B | D)) == (B & (B ^ D)) | E.
422   //
423   // For example,
424   // (icmp ne (A & 12), 0) & (icmp eq (A & 7), 1) -> (icmp eq (A & 15), 9)
425   // (icmp ne (A & 15), 0) & (icmp eq (A & 7), 0) -> (icmp eq (A & 15), 8)
426   if ((((*BCst & *DCst) & ECst) == 0) &&
427       (*BCst & (*BCst ^ *DCst)).isPowerOf2()) {
428     APInt BorD = *BCst | *DCst;
429     APInt BandBxorDorE = (*BCst & (*BCst ^ *DCst)) | ECst;
430     Value *NewMask = ConstantInt::get(A->getType(), BorD);
431     Value *NewMaskedValue = ConstantInt::get(A->getType(), BandBxorDorE);
432     Value *NewAnd = Builder.CreateAnd(A, NewMask);
433     return Builder.CreateICmp(NewCC, NewAnd, NewMaskedValue);
434   }
435 
436   auto IsSubSetOrEqual = [](const APInt *C1, const APInt *C2) {
437     return (*C1 & *C2) == *C1;
438   };
439   auto IsSuperSetOrEqual = [](const APInt *C1, const APInt *C2) {
440     return (*C1 & *C2) == *C2;
441   };
442 
443   // In the following, we consider only the cases where B is a superset of D, B
444   // is a subset of D, or B == D because otherwise there's at least one bit
445   // covered by B but not D, in which case we can't deduce much from it, so
446   // no folding (aside from the single must-be-one bit case right above.)
447   // For example,
448   // (icmp ne (A & 14), 0) & (icmp eq (A & 3), 1) -> no folding.
449   if (!IsSubSetOrEqual(BCst, DCst) && !IsSuperSetOrEqual(BCst, DCst))
450     return nullptr;
451 
452   // At this point, either B is a superset of D, B is a subset of D or B == D.
453 
454   // If E is zero, if B is a subset of (or equal to) D, LHS and RHS contradict
455   // and the whole expression becomes false (or true if negated), otherwise, no
456   // folding.
457   // For example,
458   // (icmp ne (A & 3), 0) & (icmp eq (A & 7), 0) -> false.
459   // (icmp ne (A & 15), 0) & (icmp eq (A & 3), 0) -> no folding.
460   if (ECst.isZero()) {
461     if (IsSubSetOrEqual(BCst, DCst))
462       return ConstantInt::get(LHS->getType(), !IsAnd);
463     return nullptr;
464   }
465 
466   // At this point, B, D, E aren't zero and (B & D) == B, (B & D) == D or B ==
467   // D. If B is a superset of (or equal to) D, since E is not zero, LHS is
468   // subsumed by RHS (RHS implies LHS.) So the whole expression becomes
469   // RHS. For example,
470   // (icmp ne (A & 255), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
471   // (icmp ne (A & 15), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
472   if (IsSuperSetOrEqual(BCst, DCst))
473     return RHS;
474   // Otherwise, B is a subset of D. If B and E have a common bit set,
475   // ie. (B & E) != 0, then LHS is subsumed by RHS. For example.
476   // (icmp ne (A & 12), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
477   assert(IsSubSetOrEqual(BCst, DCst) && "Precondition due to above code");
478   if ((*BCst & ECst) != 0)
479     return RHS;
480   // Otherwise, LHS and RHS contradict and the whole expression becomes false
481   // (or true if negated.) For example,
482   // (icmp ne (A & 7), 0) & (icmp eq (A & 15), 8) -> false.
483   // (icmp ne (A & 6), 0) & (icmp eq (A & 15), 8) -> false.
484   return ConstantInt::get(LHS->getType(), !IsAnd);
485 }
486 
487 /// Try to fold (icmp(A & B) ==/!= 0) &/| (icmp(A & D) ==/!= E) into a single
488 /// (icmp(A & X) ==/!= Y), where the left-hand side and the right hand side
489 /// aren't of the common mask pattern type.
490 /// Also used for logical and/or, must be poison safe.
491 static Value *foldLogOpOfMaskedICmpsAsymmetric(
492     ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, Value *A, Value *B, Value *C,
493     Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR,
494     unsigned LHSMask, unsigned RHSMask, InstCombiner::BuilderTy &Builder) {
495   assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&
496          "Expected equality predicates for masked type of icmps.");
497   // Handle Mask_NotAllZeros-BMask_Mixed cases.
498   // (icmp ne/eq (A & B), C) &/| (icmp eq/ne (A & D), E), or
499   // (icmp eq/ne (A & B), C) &/| (icmp ne/eq (A & D), E)
500   //    which gets swapped to
501   //    (icmp ne/eq (A & D), E) &/| (icmp eq/ne (A & B), C).
502   if (!IsAnd) {
503     LHSMask = conjugateICmpMask(LHSMask);
504     RHSMask = conjugateICmpMask(RHSMask);
505   }
506   if ((LHSMask & Mask_NotAllZeros) && (RHSMask & BMask_Mixed)) {
507     if (Value *V = foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
508             LHS, RHS, IsAnd, A, B, C, D, E,
509             PredL, PredR, Builder)) {
510       return V;
511     }
512   } else if ((LHSMask & BMask_Mixed) && (RHSMask & Mask_NotAllZeros)) {
513     if (Value *V = foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
514             RHS, LHS, IsAnd, A, D, E, B, C,
515             PredR, PredL, Builder)) {
516       return V;
517     }
518   }
519   return nullptr;
520 }
521 
522 /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
523 /// into a single (icmp(A & X) ==/!= Y).
524 static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
525                                      bool IsLogical,
526                                      InstCombiner::BuilderTy &Builder) {
527   Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
528   ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
529   Optional<std::pair<unsigned, unsigned>> MaskPair =
530       getMaskedTypeForICmpPair(A, B, C, D, E, LHS, RHS, PredL, PredR);
531   if (!MaskPair)
532     return nullptr;
533   assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&
534          "Expected equality predicates for masked type of icmps.");
535   unsigned LHSMask = MaskPair->first;
536   unsigned RHSMask = MaskPair->second;
537   unsigned Mask = LHSMask & RHSMask;
538   if (Mask == 0) {
539     // Even if the two sides don't share a common pattern, check if folding can
540     // still happen.
541     if (Value *V = foldLogOpOfMaskedICmpsAsymmetric(
542             LHS, RHS, IsAnd, A, B, C, D, E, PredL, PredR, LHSMask, RHSMask,
543             Builder))
544       return V;
545     return nullptr;
546   }
547 
548   // In full generality:
549   //     (icmp (A & B) Op C) | (icmp (A & D) Op E)
550   // ==  ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
551   //
552   // If the latter can be converted into (icmp (A & X) Op Y) then the former is
553   // equivalent to (icmp (A & X) !Op Y).
554   //
555   // Therefore, we can pretend for the rest of this function that we're dealing
556   // with the conjunction, provided we flip the sense of any comparisons (both
557   // input and output).
558 
559   // In most cases we're going to produce an EQ for the "&&" case.
560   ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
561   if (!IsAnd) {
562     // Convert the masking analysis into its equivalent with negated
563     // comparisons.
564     Mask = conjugateICmpMask(Mask);
565   }
566 
567   if (Mask & Mask_AllZeros) {
568     // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
569     // -> (icmp eq (A & (B|D)), 0)
570     if (IsLogical && !isGuaranteedNotToBeUndefOrPoison(D))
571       return nullptr; // TODO: Use freeze?
572     Value *NewOr = Builder.CreateOr(B, D);
573     Value *NewAnd = Builder.CreateAnd(A, NewOr);
574     // We can't use C as zero because we might actually handle
575     //   (icmp ne (A & B), B) & (icmp ne (A & D), D)
576     // with B and D, having a single bit set.
577     Value *Zero = Constant::getNullValue(A->getType());
578     return Builder.CreateICmp(NewCC, NewAnd, Zero);
579   }
580   if (Mask & BMask_AllOnes) {
581     // (icmp eq (A & B), B) & (icmp eq (A & D), D)
582     // -> (icmp eq (A & (B|D)), (B|D))
583     if (IsLogical && !isGuaranteedNotToBeUndefOrPoison(D))
584       return nullptr; // TODO: Use freeze?
585     Value *NewOr = Builder.CreateOr(B, D);
586     Value *NewAnd = Builder.CreateAnd(A, NewOr);
587     return Builder.CreateICmp(NewCC, NewAnd, NewOr);
588   }
589   if (Mask & AMask_AllOnes) {
590     // (icmp eq (A & B), A) & (icmp eq (A & D), A)
591     // -> (icmp eq (A & (B&D)), A)
592     if (IsLogical && !isGuaranteedNotToBeUndefOrPoison(D))
593       return nullptr; // TODO: Use freeze?
594     Value *NewAnd1 = Builder.CreateAnd(B, D);
595     Value *NewAnd2 = Builder.CreateAnd(A, NewAnd1);
596     return Builder.CreateICmp(NewCC, NewAnd2, A);
597   }
598 
599   // Remaining cases assume at least that B and D are constant, and depend on
600   // their actual values. This isn't strictly necessary, just a "handle the
601   // easy cases for now" decision.
602   const APInt *ConstB, *ConstD;
603   if (!match(B, m_APInt(ConstB)) || !match(D, m_APInt(ConstD)))
604     return nullptr;
605 
606   if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) {
607     // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
608     // (icmp ne (A & B), B) & (icmp ne (A & D), D)
609     //     -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
610     // Only valid if one of the masks is a superset of the other (check "B&D" is
611     // the same as either B or D).
612     APInt NewMask = *ConstB & *ConstD;
613     if (NewMask == *ConstB)
614       return LHS;
615     else if (NewMask == *ConstD)
616       return RHS;
617   }
618 
619   if (Mask & AMask_NotAllOnes) {
620     // (icmp ne (A & B), B) & (icmp ne (A & D), D)
621     //     -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
622     // Only valid if one of the masks is a superset of the other (check "B|D" is
623     // the same as either B or D).
624     APInt NewMask = *ConstB | *ConstD;
625     if (NewMask == *ConstB)
626       return LHS;
627     else if (NewMask == *ConstD)
628       return RHS;
629   }
630 
631   if (Mask & BMask_Mixed) {
632     // (icmp eq (A & B), C) & (icmp eq (A & D), E)
633     // We already know that B & C == C && D & E == E.
634     // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
635     // C and E, which are shared by both the mask B and the mask D, don't
636     // contradict, then we can transform to
637     // -> (icmp eq (A & (B|D)), (C|E))
638     // Currently, we only handle the case of B, C, D, and E being constant.
639     // We can't simply use C and E because we might actually handle
640     //   (icmp ne (A & B), B) & (icmp eq (A & D), D)
641     // with B and D, having a single bit set.
642     const APInt *OldConstC, *OldConstE;
643     if (!match(C, m_APInt(OldConstC)) || !match(E, m_APInt(OldConstE)))
644       return nullptr;
645 
646     const APInt ConstC = PredL != NewCC ? *ConstB ^ *OldConstC : *OldConstC;
647     const APInt ConstE = PredR != NewCC ? *ConstD ^ *OldConstE : *OldConstE;
648 
649     // If there is a conflict, we should actually return a false for the
650     // whole construct.
651     if (((*ConstB & *ConstD) & (ConstC ^ ConstE)).getBoolValue())
652       return ConstantInt::get(LHS->getType(), !IsAnd);
653 
654     Value *NewOr1 = Builder.CreateOr(B, D);
655     Value *NewAnd = Builder.CreateAnd(A, NewOr1);
656     Constant *NewOr2 = ConstantInt::get(A->getType(), ConstC | ConstE);
657     return Builder.CreateICmp(NewCC, NewAnd, NewOr2);
658   }
659 
660   return nullptr;
661 }
662 
663 /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
664 /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
665 /// If \p Inverted is true then the check is for the inverted range, e.g.
666 /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
667 Value *InstCombinerImpl::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
668                                             bool Inverted) {
669   // Check the lower range comparison, e.g. x >= 0
670   // InstCombine already ensured that if there is a constant it's on the RHS.
671   ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
672   if (!RangeStart)
673     return nullptr;
674 
675   ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
676                                Cmp0->getPredicate());
677 
678   // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
679   if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
680         (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
681     return nullptr;
682 
683   ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
684                                Cmp1->getPredicate());
685 
686   Value *Input = Cmp0->getOperand(0);
687   Value *RangeEnd;
688   if (Cmp1->getOperand(0) == Input) {
689     // For the upper range compare we have: icmp x, n
690     RangeEnd = Cmp1->getOperand(1);
691   } else if (Cmp1->getOperand(1) == Input) {
692     // For the upper range compare we have: icmp n, x
693     RangeEnd = Cmp1->getOperand(0);
694     Pred1 = ICmpInst::getSwappedPredicate(Pred1);
695   } else {
696     return nullptr;
697   }
698 
699   // Check the upper range comparison, e.g. x < n
700   ICmpInst::Predicate NewPred;
701   switch (Pred1) {
702     case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
703     case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
704     default: return nullptr;
705   }
706 
707   // This simplification is only valid if the upper range is not negative.
708   KnownBits Known = computeKnownBits(RangeEnd, /*Depth=*/0, Cmp1);
709   if (!Known.isNonNegative())
710     return nullptr;
711 
712   if (Inverted)
713     NewPred = ICmpInst::getInversePredicate(NewPred);
714 
715   return Builder.CreateICmp(NewPred, Input, RangeEnd);
716 }
717 
718 // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
719 // Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2)
720 Value *InstCombinerImpl::foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS,
721                                                        ICmpInst *RHS,
722                                                        Instruction *CxtI,
723                                                        bool IsAnd,
724                                                        bool IsLogical) {
725   CmpInst::Predicate Pred = IsAnd ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
726   if (LHS->getPredicate() != Pred || RHS->getPredicate() != Pred)
727     return nullptr;
728 
729   if (!match(LHS->getOperand(1), m_Zero()) ||
730       !match(RHS->getOperand(1), m_Zero()))
731     return nullptr;
732 
733   Value *L1, *L2, *R1, *R2;
734   if (match(LHS->getOperand(0), m_And(m_Value(L1), m_Value(L2))) &&
735       match(RHS->getOperand(0), m_And(m_Value(R1), m_Value(R2)))) {
736     if (L1 == R2 || L2 == R2)
737       std::swap(R1, R2);
738     if (L2 == R1)
739       std::swap(L1, L2);
740 
741     if (L1 == R1 &&
742         isKnownToBeAPowerOfTwo(L2, false, 0, CxtI) &&
743         isKnownToBeAPowerOfTwo(R2, false, 0, CxtI)) {
744       // If this is a logical and/or, then we must prevent propagation of a
745       // poison value from the RHS by inserting freeze.
746       if (IsLogical)
747         R2 = Builder.CreateFreeze(R2);
748       Value *Mask = Builder.CreateOr(L2, R2);
749       Value *Masked = Builder.CreateAnd(L1, Mask);
750       auto NewPred = IsAnd ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
751       return Builder.CreateICmp(NewPred, Masked, Mask);
752     }
753   }
754 
755   return nullptr;
756 }
757 
758 /// General pattern:
759 ///   X & Y
760 ///
761 /// Where Y is checking that all the high bits (covered by a mask 4294967168)
762 /// are uniform, i.e.  %arg & 4294967168  can be either  4294967168  or  0
763 /// Pattern can be one of:
764 ///   %t = add        i32 %arg,    128
765 ///   %r = icmp   ult i32 %t,      256
766 /// Or
767 ///   %t0 = shl       i32 %arg,    24
768 ///   %t1 = ashr      i32 %t0,     24
769 ///   %r  = icmp  eq  i32 %t1,     %arg
770 /// Or
771 ///   %t0 = trunc     i32 %arg  to i8
772 ///   %t1 = sext      i8  %t0   to i32
773 ///   %r  = icmp  eq  i32 %t1,     %arg
774 /// This pattern is a signed truncation check.
775 ///
776 /// And X is checking that some bit in that same mask is zero.
777 /// I.e. can be one of:
778 ///   %r = icmp sgt i32   %arg,    -1
779 /// Or
780 ///   %t = and      i32   %arg,    2147483648
781 ///   %r = icmp eq  i32   %t,      0
782 ///
783 /// Since we are checking that all the bits in that mask are the same,
784 /// and a particular bit is zero, what we are really checking is that all the
785 /// masked bits are zero.
786 /// So this should be transformed to:
787 ///   %r = icmp ult i32 %arg, 128
788 static Value *foldSignedTruncationCheck(ICmpInst *ICmp0, ICmpInst *ICmp1,
789                                         Instruction &CxtI,
790                                         InstCombiner::BuilderTy &Builder) {
791   assert(CxtI.getOpcode() == Instruction::And);
792 
793   // Match  icmp ult (add %arg, C01), C1   (C1 == C01 << 1; powers of two)
794   auto tryToMatchSignedTruncationCheck = [](ICmpInst *ICmp, Value *&X,
795                                             APInt &SignBitMask) -> bool {
796     CmpInst::Predicate Pred;
797     const APInt *I01, *I1; // powers of two; I1 == I01 << 1
798     if (!(match(ICmp,
799                 m_ICmp(Pred, m_Add(m_Value(X), m_Power2(I01)), m_Power2(I1))) &&
800           Pred == ICmpInst::ICMP_ULT && I1->ugt(*I01) && I01->shl(1) == *I1))
801       return false;
802     // Which bit is the new sign bit as per the 'signed truncation' pattern?
803     SignBitMask = *I01;
804     return true;
805   };
806 
807   // One icmp needs to be 'signed truncation check'.
808   // We need to match this first, else we will mismatch commutative cases.
809   Value *X1;
810   APInt HighestBit;
811   ICmpInst *OtherICmp;
812   if (tryToMatchSignedTruncationCheck(ICmp1, X1, HighestBit))
813     OtherICmp = ICmp0;
814   else if (tryToMatchSignedTruncationCheck(ICmp0, X1, HighestBit))
815     OtherICmp = ICmp1;
816   else
817     return nullptr;
818 
819   assert(HighestBit.isPowerOf2() && "expected to be power of two (non-zero)");
820 
821   // Try to match/decompose into:  icmp eq (X & Mask), 0
822   auto tryToDecompose = [](ICmpInst *ICmp, Value *&X,
823                            APInt &UnsetBitsMask) -> bool {
824     CmpInst::Predicate Pred = ICmp->getPredicate();
825     // Can it be decomposed into  icmp eq (X & Mask), 0  ?
826     if (llvm::decomposeBitTestICmp(ICmp->getOperand(0), ICmp->getOperand(1),
827                                    Pred, X, UnsetBitsMask,
828                                    /*LookThroughTrunc=*/false) &&
829         Pred == ICmpInst::ICMP_EQ)
830       return true;
831     // Is it  icmp eq (X & Mask), 0  already?
832     const APInt *Mask;
833     if (match(ICmp, m_ICmp(Pred, m_And(m_Value(X), m_APInt(Mask)), m_Zero())) &&
834         Pred == ICmpInst::ICMP_EQ) {
835       UnsetBitsMask = *Mask;
836       return true;
837     }
838     return false;
839   };
840 
841   // And the other icmp needs to be decomposable into a bit test.
842   Value *X0;
843   APInt UnsetBitsMask;
844   if (!tryToDecompose(OtherICmp, X0, UnsetBitsMask))
845     return nullptr;
846 
847   assert(!UnsetBitsMask.isZero() && "empty mask makes no sense.");
848 
849   // Are they working on the same value?
850   Value *X;
851   if (X1 == X0) {
852     // Ok as is.
853     X = X1;
854   } else if (match(X0, m_Trunc(m_Specific(X1)))) {
855     UnsetBitsMask = UnsetBitsMask.zext(X1->getType()->getScalarSizeInBits());
856     X = X1;
857   } else
858     return nullptr;
859 
860   // So which bits should be uniform as per the 'signed truncation check'?
861   // (all the bits starting with (i.e. including) HighestBit)
862   APInt SignBitsMask = ~(HighestBit - 1U);
863 
864   // UnsetBitsMask must have some common bits with SignBitsMask,
865   if (!UnsetBitsMask.intersects(SignBitsMask))
866     return nullptr;
867 
868   // Does UnsetBitsMask contain any bits outside of SignBitsMask?
869   if (!UnsetBitsMask.isSubsetOf(SignBitsMask)) {
870     APInt OtherHighestBit = (~UnsetBitsMask) + 1U;
871     if (!OtherHighestBit.isPowerOf2())
872       return nullptr;
873     HighestBit = APIntOps::umin(HighestBit, OtherHighestBit);
874   }
875   // Else, if it does not, then all is ok as-is.
876 
877   // %r = icmp ult %X, SignBit
878   return Builder.CreateICmpULT(X, ConstantInt::get(X->getType(), HighestBit),
879                                CxtI.getName() + ".simplified");
880 }
881 
882 /// Fold (icmp eq ctpop(X) 1) | (icmp eq X 0) into (icmp ult ctpop(X) 2) and
883 /// fold (icmp ne ctpop(X) 1) & (icmp ne X 0) into (icmp ugt ctpop(X) 1).
884 /// Also used for logical and/or, must be poison safe.
885 static Value *foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd,
886                                    InstCombiner::BuilderTy &Builder) {
887   CmpInst::Predicate Pred0, Pred1;
888   Value *X;
889   if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
890                           m_SpecificInt(1))) ||
891       !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())))
892     return nullptr;
893 
894   Value *CtPop = Cmp0->getOperand(0);
895   if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_NE)
896     return Builder.CreateICmpUGT(CtPop, ConstantInt::get(CtPop->getType(), 1));
897   if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_EQ)
898     return Builder.CreateICmpULT(CtPop, ConstantInt::get(CtPop->getType(), 2));
899 
900   return nullptr;
901 }
902 
903 /// Reduce a pair of compares that check if a value has exactly 1 bit set.
904 /// Also used for logical and/or, must be poison safe.
905 static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
906                              InstCombiner::BuilderTy &Builder) {
907   // Handle 'and' / 'or' commutation: make the equality check the first operand.
908   if (JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_NE)
909     std::swap(Cmp0, Cmp1);
910   else if (!JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_EQ)
911     std::swap(Cmp0, Cmp1);
912 
913   // (X != 0) && (ctpop(X) u< 2) --> ctpop(X) == 1
914   CmpInst::Predicate Pred0, Pred1;
915   Value *X;
916   if (JoinedByAnd && match(Cmp0, m_ICmp(Pred0, m_Value(X), m_ZeroInt())) &&
917       match(Cmp1, m_ICmp(Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
918                          m_SpecificInt(2))) &&
919       Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT) {
920     Value *CtPop = Cmp1->getOperand(0);
921     return Builder.CreateICmpEQ(CtPop, ConstantInt::get(CtPop->getType(), 1));
922   }
923   // (X == 0) || (ctpop(X) u> 1) --> ctpop(X) != 1
924   if (!JoinedByAnd && match(Cmp0, m_ICmp(Pred0, m_Value(X), m_ZeroInt())) &&
925       match(Cmp1, m_ICmp(Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
926                          m_SpecificInt(1))) &&
927       Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_UGT) {
928     Value *CtPop = Cmp1->getOperand(0);
929     return Builder.CreateICmpNE(CtPop, ConstantInt::get(CtPop->getType(), 1));
930   }
931   return nullptr;
932 }
933 
934 /// Commuted variants are assumed to be handled by calling this function again
935 /// with the parameters swapped.
936 static Value *foldUnsignedUnderflowCheck(ICmpInst *ZeroICmp,
937                                          ICmpInst *UnsignedICmp, bool IsAnd,
938                                          const SimplifyQuery &Q,
939                                          InstCombiner::BuilderTy &Builder) {
940   Value *ZeroCmpOp;
941   ICmpInst::Predicate EqPred;
942   if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(ZeroCmpOp), m_Zero())) ||
943       !ICmpInst::isEquality(EqPred))
944     return nullptr;
945 
946   auto IsKnownNonZero = [&](Value *V) {
947     return isKnownNonZero(V, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
948   };
949 
950   ICmpInst::Predicate UnsignedPred;
951 
952   Value *A, *B;
953   if (match(UnsignedICmp,
954             m_c_ICmp(UnsignedPred, m_Specific(ZeroCmpOp), m_Value(A))) &&
955       match(ZeroCmpOp, m_c_Add(m_Specific(A), m_Value(B))) &&
956       (ZeroICmp->hasOneUse() || UnsignedICmp->hasOneUse())) {
957     auto GetKnownNonZeroAndOther = [&](Value *&NonZero, Value *&Other) {
958       if (!IsKnownNonZero(NonZero))
959         std::swap(NonZero, Other);
960       return IsKnownNonZero(NonZero);
961     };
962 
963     // Given  ZeroCmpOp = (A + B)
964     //   ZeroCmpOp <  A && ZeroCmpOp != 0  -->  (0-X) <  Y  iff
965     //   ZeroCmpOp >= A || ZeroCmpOp == 0  -->  (0-X) >= Y  iff
966     //     with X being the value (A/B) that is known to be non-zero,
967     //     and Y being remaining value.
968     if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE &&
969         IsAnd && GetKnownNonZeroAndOther(B, A))
970       return Builder.CreateICmpULT(Builder.CreateNeg(B), A);
971     if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ &&
972         !IsAnd && GetKnownNonZeroAndOther(B, A))
973       return Builder.CreateICmpUGE(Builder.CreateNeg(B), A);
974   }
975 
976   Value *Base, *Offset;
977   if (!match(ZeroCmpOp, m_Sub(m_Value(Base), m_Value(Offset))))
978     return nullptr;
979 
980   if (!match(UnsignedICmp,
981              m_c_ICmp(UnsignedPred, m_Specific(Base), m_Specific(Offset))) ||
982       !ICmpInst::isUnsigned(UnsignedPred))
983     return nullptr;
984 
985   // Base >=/> Offset && (Base - Offset) != 0  <-->  Base > Offset
986   // (no overflow and not null)
987   if ((UnsignedPred == ICmpInst::ICMP_UGE ||
988        UnsignedPred == ICmpInst::ICMP_UGT) &&
989       EqPred == ICmpInst::ICMP_NE && IsAnd)
990     return Builder.CreateICmpUGT(Base, Offset);
991 
992   // Base <=/< Offset || (Base - Offset) == 0  <-->  Base <= Offset
993   // (overflow or null)
994   if ((UnsignedPred == ICmpInst::ICMP_ULE ||
995        UnsignedPred == ICmpInst::ICMP_ULT) &&
996       EqPred == ICmpInst::ICMP_EQ && !IsAnd)
997     return Builder.CreateICmpULE(Base, Offset);
998 
999   // Base <= Offset && (Base - Offset) != 0  -->  Base < Offset
1000   if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1001       IsAnd)
1002     return Builder.CreateICmpULT(Base, Offset);
1003 
1004   // Base > Offset || (Base - Offset) == 0  -->  Base >= Offset
1005   if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1006       !IsAnd)
1007     return Builder.CreateICmpUGE(Base, Offset);
1008 
1009   return nullptr;
1010 }
1011 
1012 struct IntPart {
1013   Value *From;
1014   unsigned StartBit;
1015   unsigned NumBits;
1016 };
1017 
1018 /// Match an extraction of bits from an integer.
1019 static Optional<IntPart> matchIntPart(Value *V) {
1020   Value *X;
1021   if (!match(V, m_OneUse(m_Trunc(m_Value(X)))))
1022     return None;
1023 
1024   unsigned NumOriginalBits = X->getType()->getScalarSizeInBits();
1025   unsigned NumExtractedBits = V->getType()->getScalarSizeInBits();
1026   Value *Y;
1027   const APInt *Shift;
1028   // For a trunc(lshr Y, Shift) pattern, make sure we're only extracting bits
1029   // from Y, not any shifted-in zeroes.
1030   if (match(X, m_OneUse(m_LShr(m_Value(Y), m_APInt(Shift)))) &&
1031       Shift->ule(NumOriginalBits - NumExtractedBits))
1032     return {{Y, (unsigned)Shift->getZExtValue(), NumExtractedBits}};
1033   return {{X, 0, NumExtractedBits}};
1034 }
1035 
1036 /// Materialize an extraction of bits from an integer in IR.
1037 static Value *extractIntPart(const IntPart &P, IRBuilderBase &Builder) {
1038   Value *V = P.From;
1039   if (P.StartBit)
1040     V = Builder.CreateLShr(V, P.StartBit);
1041   Type *TruncTy = V->getType()->getWithNewBitWidth(P.NumBits);
1042   if (TruncTy != V->getType())
1043     V = Builder.CreateTrunc(V, TruncTy);
1044   return V;
1045 }
1046 
1047 /// (icmp eq X0, Y0) & (icmp eq X1, Y1) -> icmp eq X01, Y01
1048 /// (icmp ne X0, Y0) | (icmp ne X1, Y1) -> icmp ne X01, Y01
1049 /// where X0, X1 and Y0, Y1 are adjacent parts extracted from an integer.
1050 Value *InstCombinerImpl::foldEqOfParts(ICmpInst *Cmp0, ICmpInst *Cmp1,
1051                                        bool IsAnd) {
1052   if (!Cmp0->hasOneUse() || !Cmp1->hasOneUse())
1053     return nullptr;
1054 
1055   CmpInst::Predicate Pred = IsAnd ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1056   if (Cmp0->getPredicate() != Pred || Cmp1->getPredicate() != Pred)
1057     return nullptr;
1058 
1059   Optional<IntPart> L0 = matchIntPart(Cmp0->getOperand(0));
1060   Optional<IntPart> R0 = matchIntPart(Cmp0->getOperand(1));
1061   Optional<IntPart> L1 = matchIntPart(Cmp1->getOperand(0));
1062   Optional<IntPart> R1 = matchIntPart(Cmp1->getOperand(1));
1063   if (!L0 || !R0 || !L1 || !R1)
1064     return nullptr;
1065 
1066   // Make sure the LHS/RHS compare a part of the same value, possibly after
1067   // an operand swap.
1068   if (L0->From != L1->From || R0->From != R1->From) {
1069     if (L0->From != R1->From || R0->From != L1->From)
1070       return nullptr;
1071     std::swap(L1, R1);
1072   }
1073 
1074   // Make sure the extracted parts are adjacent, canonicalizing to L0/R0 being
1075   // the low part and L1/R1 being the high part.
1076   if (L0->StartBit + L0->NumBits != L1->StartBit ||
1077       R0->StartBit + R0->NumBits != R1->StartBit) {
1078     if (L1->StartBit + L1->NumBits != L0->StartBit ||
1079         R1->StartBit + R1->NumBits != R0->StartBit)
1080       return nullptr;
1081     std::swap(L0, L1);
1082     std::swap(R0, R1);
1083   }
1084 
1085   // We can simplify to a comparison of these larger parts of the integers.
1086   IntPart L = {L0->From, L0->StartBit, L0->NumBits + L1->NumBits};
1087   IntPart R = {R0->From, R0->StartBit, R0->NumBits + R1->NumBits};
1088   Value *LValue = extractIntPart(L, Builder);
1089   Value *RValue = extractIntPart(R, Builder);
1090   return Builder.CreateICmp(Pred, LValue, RValue);
1091 }
1092 
1093 /// Reduce logic-of-compares with equality to a constant by substituting a
1094 /// common operand with the constant. Callers are expected to call this with
1095 /// Cmp0/Cmp1 switched to handle logic op commutativity.
1096 static Value *foldAndOrOfICmpsWithConstEq(ICmpInst *Cmp0, ICmpInst *Cmp1,
1097                                           bool IsAnd,
1098                                           InstCombiner::BuilderTy &Builder,
1099                                           const SimplifyQuery &Q) {
1100   // Match an equality compare with a non-poison constant as Cmp0.
1101   // Also, give up if the compare can be constant-folded to avoid looping.
1102   ICmpInst::Predicate Pred0;
1103   Value *X;
1104   Constant *C;
1105   if (!match(Cmp0, m_ICmp(Pred0, m_Value(X), m_Constant(C))) ||
1106       !isGuaranteedNotToBeUndefOrPoison(C) || isa<Constant>(X))
1107     return nullptr;
1108   if ((IsAnd && Pred0 != ICmpInst::ICMP_EQ) ||
1109       (!IsAnd && Pred0 != ICmpInst::ICMP_NE))
1110     return nullptr;
1111 
1112   // The other compare must include a common operand (X). Canonicalize the
1113   // common operand as operand 1 (Pred1 is swapped if the common operand was
1114   // operand 0).
1115   Value *Y;
1116   ICmpInst::Predicate Pred1;
1117   if (!match(Cmp1, m_c_ICmp(Pred1, m_Value(Y), m_Deferred(X))))
1118     return nullptr;
1119 
1120   // Replace variable with constant value equivalence to remove a variable use:
1121   // (X == C) && (Y Pred1 X) --> (X == C) && (Y Pred1 C)
1122   // (X != C) || (Y Pred1 X) --> (X != C) || (Y Pred1 C)
1123   // Can think of the 'or' substitution with the 'and' bool equivalent:
1124   // A || B --> A || (!A && B)
1125   Value *SubstituteCmp = simplifyICmpInst(Pred1, Y, C, Q);
1126   if (!SubstituteCmp) {
1127     // If we need to create a new instruction, require that the old compare can
1128     // be removed.
1129     if (!Cmp1->hasOneUse())
1130       return nullptr;
1131     SubstituteCmp = Builder.CreateICmp(Pred1, Y, C);
1132   }
1133   return Builder.CreateBinOp(IsAnd ? Instruction::And : Instruction::Or, Cmp0,
1134                              SubstituteCmp);
1135 }
1136 
1137 /// Fold (icmp Pred1 V1, C1) & (icmp Pred2 V2, C2)
1138 /// or   (icmp Pred1 V1, C1) | (icmp Pred2 V2, C2)
1139 /// into a single comparison using range-based reasoning.
1140 /// NOTE: This is also used for logical and/or, must be poison-safe!
1141 Value *InstCombinerImpl::foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1,
1142                                                      ICmpInst *ICmp2,
1143                                                      bool IsAnd) {
1144   ICmpInst::Predicate Pred1, Pred2;
1145   Value *V1, *V2;
1146   const APInt *C1, *C2;
1147   if (!match(ICmp1, m_ICmp(Pred1, m_Value(V1), m_APInt(C1))) ||
1148       !match(ICmp2, m_ICmp(Pred2, m_Value(V2), m_APInt(C2))))
1149     return nullptr;
1150 
1151   // Look through add of a constant offset on V1, V2, or both operands. This
1152   // allows us to interpret the V + C' < C'' range idiom into a proper range.
1153   const APInt *Offset1 = nullptr, *Offset2 = nullptr;
1154   if (V1 != V2) {
1155     Value *X;
1156     if (match(V1, m_Add(m_Value(X), m_APInt(Offset1))))
1157       V1 = X;
1158     if (match(V2, m_Add(m_Value(X), m_APInt(Offset2))))
1159       V2 = X;
1160   }
1161 
1162   if (V1 != V2)
1163     return nullptr;
1164 
1165   ConstantRange CR1 = ConstantRange::makeExactICmpRegion(
1166       IsAnd ? ICmpInst::getInversePredicate(Pred1) : Pred1, *C1);
1167   if (Offset1)
1168     CR1 = CR1.subtract(*Offset1);
1169 
1170   ConstantRange CR2 = ConstantRange::makeExactICmpRegion(
1171       IsAnd ? ICmpInst::getInversePredicate(Pred2) : Pred2, *C2);
1172   if (Offset2)
1173     CR2 = CR2.subtract(*Offset2);
1174 
1175   Type *Ty = V1->getType();
1176   Value *NewV = V1;
1177   Optional<ConstantRange> CR = CR1.exactUnionWith(CR2);
1178   if (!CR) {
1179     if (!(ICmp1->hasOneUse() && ICmp2->hasOneUse()) || CR1.isWrappedSet() ||
1180         CR2.isWrappedSet())
1181       return nullptr;
1182 
1183     // Check whether we have equal-size ranges that only differ by one bit.
1184     // In that case we can apply a mask to map one range onto the other.
1185     APInt LowerDiff = CR1.getLower() ^ CR2.getLower();
1186     APInt UpperDiff = (CR1.getUpper() - 1) ^ (CR2.getUpper() - 1);
1187     APInt CR1Size = CR1.getUpper() - CR1.getLower();
1188     if (!LowerDiff.isPowerOf2() || LowerDiff != UpperDiff ||
1189         CR1Size != CR2.getUpper() - CR2.getLower())
1190       return nullptr;
1191 
1192     CR = CR1.getLower().ult(CR2.getLower()) ? CR1 : CR2;
1193     NewV = Builder.CreateAnd(NewV, ConstantInt::get(Ty, ~LowerDiff));
1194   }
1195 
1196   if (IsAnd)
1197     CR = CR->inverse();
1198 
1199   CmpInst::Predicate NewPred;
1200   APInt NewC, Offset;
1201   CR->getEquivalentICmp(NewPred, NewC, Offset);
1202 
1203   if (Offset != 0)
1204     NewV = Builder.CreateAdd(NewV, ConstantInt::get(Ty, Offset));
1205   return Builder.CreateICmp(NewPred, NewV, ConstantInt::get(Ty, NewC));
1206 }
1207 
1208 Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
1209                                           bool IsAnd, bool IsLogicalSelect) {
1210   Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1211   Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1212   FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1213 
1214   if (LHS0 == RHS1 && RHS0 == LHS1) {
1215     // Swap RHS operands to match LHS.
1216     PredR = FCmpInst::getSwappedPredicate(PredR);
1217     std::swap(RHS0, RHS1);
1218   }
1219 
1220   // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1221   // Suppose the relation between x and y is R, where R is one of
1222   // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
1223   // testing the desired relations.
1224   //
1225   // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1226   //    bool(R & CC0) && bool(R & CC1)
1227   //  = bool((R & CC0) & (R & CC1))
1228   //  = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
1229   //
1230   // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1231   //    bool(R & CC0) || bool(R & CC1)
1232   //  = bool((R & CC0) | (R & CC1))
1233   //  = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
1234   if (LHS0 == RHS0 && LHS1 == RHS1) {
1235     unsigned FCmpCodeL = getFCmpCode(PredL);
1236     unsigned FCmpCodeR = getFCmpCode(PredR);
1237     unsigned NewPred = IsAnd ? FCmpCodeL & FCmpCodeR : FCmpCodeL | FCmpCodeR;
1238 
1239     // Intersect the fast math flags.
1240     // TODO: We can union the fast math flags unless this is a logical select.
1241     IRBuilder<>::FastMathFlagGuard FMFG(Builder);
1242     FastMathFlags FMF = LHS->getFastMathFlags();
1243     FMF &= RHS->getFastMathFlags();
1244     Builder.setFastMathFlags(FMF);
1245 
1246     return getFCmpValue(NewPred, LHS0, LHS1, Builder);
1247   }
1248 
1249   // This transform is not valid for a logical select.
1250   if (!IsLogicalSelect &&
1251       ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
1252        (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO &&
1253         !IsAnd))) {
1254     if (LHS0->getType() != RHS0->getType())
1255       return nullptr;
1256 
1257     // FCmp canonicalization ensures that (fcmp ord/uno X, X) and
1258     // (fcmp ord/uno X, C) will be transformed to (fcmp X, +0.0).
1259     if (match(LHS1, m_PosZeroFP()) && match(RHS1, m_PosZeroFP()))
1260       // Ignore the constants because they are obviously not NANs:
1261       // (fcmp ord x, 0.0) & (fcmp ord y, 0.0)  -> (fcmp ord x, y)
1262       // (fcmp uno x, 0.0) | (fcmp uno y, 0.0)  -> (fcmp uno x, y)
1263       return Builder.CreateFCmp(PredL, LHS0, RHS0);
1264   }
1265 
1266   return nullptr;
1267 }
1268 
1269 /// This a limited reassociation for a special case (see above) where we are
1270 /// checking if two values are either both NAN (unordered) or not-NAN (ordered).
1271 /// This could be handled more generally in '-reassociation', but it seems like
1272 /// an unlikely pattern for a large number of logic ops and fcmps.
1273 static Instruction *reassociateFCmps(BinaryOperator &BO,
1274                                      InstCombiner::BuilderTy &Builder) {
1275   Instruction::BinaryOps Opcode = BO.getOpcode();
1276   assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1277          "Expecting and/or op for fcmp transform");
1278 
1279   // There are 4 commuted variants of the pattern. Canonicalize operands of this
1280   // logic op so an fcmp is operand 0 and a matching logic op is operand 1.
1281   Value *Op0 = BO.getOperand(0), *Op1 = BO.getOperand(1), *X;
1282   FCmpInst::Predicate Pred;
1283   if (match(Op1, m_FCmp(Pred, m_Value(), m_AnyZeroFP())))
1284     std::swap(Op0, Op1);
1285 
1286   // Match inner binop and the predicate for combining 2 NAN checks into 1.
1287   Value *BO10, *BO11;
1288   FCmpInst::Predicate NanPred = Opcode == Instruction::And ? FCmpInst::FCMP_ORD
1289                                                            : FCmpInst::FCMP_UNO;
1290   if (!match(Op0, m_FCmp(Pred, m_Value(X), m_AnyZeroFP())) || Pred != NanPred ||
1291       !match(Op1, m_BinOp(Opcode, m_Value(BO10), m_Value(BO11))))
1292     return nullptr;
1293 
1294   // The inner logic op must have a matching fcmp operand.
1295   Value *Y;
1296   if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) ||
1297       Pred != NanPred || X->getType() != Y->getType())
1298     std::swap(BO10, BO11);
1299 
1300   if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) ||
1301       Pred != NanPred || X->getType() != Y->getType())
1302     return nullptr;
1303 
1304   // and (fcmp ord X, 0), (and (fcmp ord Y, 0), Z) --> and (fcmp ord X, Y), Z
1305   // or  (fcmp uno X, 0), (or  (fcmp uno Y, 0), Z) --> or  (fcmp uno X, Y), Z
1306   Value *NewFCmp = Builder.CreateFCmp(Pred, X, Y);
1307   if (auto *NewFCmpInst = dyn_cast<FCmpInst>(NewFCmp)) {
1308     // Intersect FMF from the 2 source fcmps.
1309     NewFCmpInst->copyIRFlags(Op0);
1310     NewFCmpInst->andIRFlags(BO10);
1311   }
1312   return BinaryOperator::Create(Opcode, NewFCmp, BO11);
1313 }
1314 
1315 /// Match variations of De Morgan's Laws:
1316 /// (~A & ~B) == (~(A | B))
1317 /// (~A | ~B) == (~(A & B))
1318 static Instruction *matchDeMorgansLaws(BinaryOperator &I,
1319                                        InstCombiner::BuilderTy &Builder) {
1320   const Instruction::BinaryOps Opcode = I.getOpcode();
1321   assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1322          "Trying to match De Morgan's Laws with something other than and/or");
1323 
1324   // Flip the logic operation.
1325   const Instruction::BinaryOps FlippedOpcode =
1326       (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1327 
1328   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1329   Value *A, *B;
1330   if (match(Op0, m_OneUse(m_Not(m_Value(A)))) &&
1331       match(Op1, m_OneUse(m_Not(m_Value(B)))) &&
1332       !InstCombiner::isFreeToInvert(A, A->hasOneUse()) &&
1333       !InstCombiner::isFreeToInvert(B, B->hasOneUse())) {
1334     Value *AndOr =
1335         Builder.CreateBinOp(FlippedOpcode, A, B, I.getName() + ".demorgan");
1336     return BinaryOperator::CreateNot(AndOr);
1337   }
1338 
1339   // The 'not' ops may require reassociation.
1340   // (A & ~B) & ~C --> A & ~(B | C)
1341   // (~B & A) & ~C --> A & ~(B | C)
1342   // (A | ~B) | ~C --> A | ~(B & C)
1343   // (~B | A) | ~C --> A | ~(B & C)
1344   Value *C;
1345   if (match(Op0, m_OneUse(m_c_BinOp(Opcode, m_Value(A), m_Not(m_Value(B))))) &&
1346       match(Op1, m_Not(m_Value(C)))) {
1347     Value *FlippedBO = Builder.CreateBinOp(FlippedOpcode, B, C);
1348     return BinaryOperator::Create(Opcode, A, Builder.CreateNot(FlippedBO));
1349   }
1350 
1351   return nullptr;
1352 }
1353 
1354 bool InstCombinerImpl::shouldOptimizeCast(CastInst *CI) {
1355   Value *CastSrc = CI->getOperand(0);
1356 
1357   // Noop casts and casts of constants should be eliminated trivially.
1358   if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1359     return false;
1360 
1361   // If this cast is paired with another cast that can be eliminated, we prefer
1362   // to have it eliminated.
1363   if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1364     if (isEliminableCastPair(PrecedingCI, CI))
1365       return false;
1366 
1367   return true;
1368 }
1369 
1370 /// Fold {and,or,xor} (cast X), C.
1371 static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1372                                           InstCombiner::BuilderTy &Builder) {
1373   Constant *C = dyn_cast<Constant>(Logic.getOperand(1));
1374   if (!C)
1375     return nullptr;
1376 
1377   auto LogicOpc = Logic.getOpcode();
1378   Type *DestTy = Logic.getType();
1379   Type *SrcTy = Cast->getSrcTy();
1380 
1381   // Move the logic operation ahead of a zext or sext if the constant is
1382   // unchanged in the smaller source type. Performing the logic in a smaller
1383   // type may provide more information to later folds, and the smaller logic
1384   // instruction may be cheaper (particularly in the case of vectors).
1385   Value *X;
1386   if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1387     Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1388     Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1389     if (ZextTruncC == C) {
1390       // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1391       Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC);
1392       return new ZExtInst(NewOp, DestTy);
1393     }
1394   }
1395 
1396   if (match(Cast, m_OneUse(m_SExt(m_Value(X))))) {
1397     Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1398     Constant *SextTruncC = ConstantExpr::getSExt(TruncC, DestTy);
1399     if (SextTruncC == C) {
1400       // LogicOpc (sext X), C --> sext (LogicOpc X, C)
1401       Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC);
1402       return new SExtInst(NewOp, DestTy);
1403     }
1404   }
1405 
1406   return nullptr;
1407 }
1408 
1409 /// Fold {and,or,xor} (cast X), Y.
1410 Instruction *InstCombinerImpl::foldCastedBitwiseLogic(BinaryOperator &I) {
1411   auto LogicOpc = I.getOpcode();
1412   assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
1413 
1414   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1415   CastInst *Cast0 = dyn_cast<CastInst>(Op0);
1416   if (!Cast0)
1417     return nullptr;
1418 
1419   // This must be a cast from an integer or integer vector source type to allow
1420   // transformation of the logic operation to the source type.
1421   Type *DestTy = I.getType();
1422   Type *SrcTy = Cast0->getSrcTy();
1423   if (!SrcTy->isIntOrIntVectorTy())
1424     return nullptr;
1425 
1426   if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1427     return Ret;
1428 
1429   CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1430   if (!Cast1)
1431     return nullptr;
1432 
1433   // Both operands of the logic operation are casts. The casts must be of the
1434   // same type for reduction.
1435   auto CastOpcode = Cast0->getOpcode();
1436   if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy())
1437     return nullptr;
1438 
1439   Value *Cast0Src = Cast0->getOperand(0);
1440   Value *Cast1Src = Cast1->getOperand(0);
1441 
1442   // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
1443   if ((Cast0->hasOneUse() || Cast1->hasOneUse()) &&
1444       shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1445     Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1446                                        I.getName());
1447     return CastInst::Create(CastOpcode, NewOp, DestTy);
1448   }
1449 
1450   // For now, only 'and'/'or' have optimizations after this.
1451   if (LogicOpc == Instruction::Xor)
1452     return nullptr;
1453 
1454   // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the
1455   // cast is otherwise not optimizable.  This happens for vector sexts.
1456   ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src);
1457   ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src);
1458   if (ICmp0 && ICmp1) {
1459     if (Value *Res =
1460             foldAndOrOfICmps(ICmp0, ICmp1, I, LogicOpc == Instruction::And))
1461       return CastInst::Create(CastOpcode, Res, DestTy);
1462     return nullptr;
1463   }
1464 
1465   // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the
1466   // cast is otherwise not optimizable.  This happens for vector sexts.
1467   FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src);
1468   FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src);
1469   if (FCmp0 && FCmp1)
1470     if (Value *R = foldLogicOfFCmps(FCmp0, FCmp1, LogicOpc == Instruction::And))
1471       return CastInst::Create(CastOpcode, R, DestTy);
1472 
1473   return nullptr;
1474 }
1475 
1476 static Instruction *foldAndToXor(BinaryOperator &I,
1477                                  InstCombiner::BuilderTy &Builder) {
1478   assert(I.getOpcode() == Instruction::And);
1479   Value *Op0 = I.getOperand(0);
1480   Value *Op1 = I.getOperand(1);
1481   Value *A, *B;
1482 
1483   // Operand complexity canonicalization guarantees that the 'or' is Op0.
1484   // (A | B) & ~(A & B) --> A ^ B
1485   // (A | B) & ~(B & A) --> A ^ B
1486   if (match(&I, m_BinOp(m_Or(m_Value(A), m_Value(B)),
1487                         m_Not(m_c_And(m_Deferred(A), m_Deferred(B))))))
1488     return BinaryOperator::CreateXor(A, B);
1489 
1490   // (A | ~B) & (~A | B) --> ~(A ^ B)
1491   // (A | ~B) & (B | ~A) --> ~(A ^ B)
1492   // (~B | A) & (~A | B) --> ~(A ^ B)
1493   // (~B | A) & (B | ~A) --> ~(A ^ B)
1494   if (Op0->hasOneUse() || Op1->hasOneUse())
1495     if (match(&I, m_BinOp(m_c_Or(m_Value(A), m_Not(m_Value(B))),
1496                           m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B)))))
1497       return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1498 
1499   return nullptr;
1500 }
1501 
1502 static Instruction *foldOrToXor(BinaryOperator &I,
1503                                 InstCombiner::BuilderTy &Builder) {
1504   assert(I.getOpcode() == Instruction::Or);
1505   Value *Op0 = I.getOperand(0);
1506   Value *Op1 = I.getOperand(1);
1507   Value *A, *B;
1508 
1509   // Operand complexity canonicalization guarantees that the 'and' is Op0.
1510   // (A & B) | ~(A | B) --> ~(A ^ B)
1511   // (A & B) | ~(B | A) --> ~(A ^ B)
1512   if (Op0->hasOneUse() || Op1->hasOneUse())
1513     if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1514         match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1515       return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1516 
1517   // Operand complexity canonicalization guarantees that the 'xor' is Op0.
1518   // (A ^ B) | ~(A | B) --> ~(A & B)
1519   // (A ^ B) | ~(B | A) --> ~(A & B)
1520   if (Op0->hasOneUse() || Op1->hasOneUse())
1521     if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
1522         match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1523       return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
1524 
1525   // (A & ~B) | (~A & B) --> A ^ B
1526   // (A & ~B) | (B & ~A) --> A ^ B
1527   // (~B & A) | (~A & B) --> A ^ B
1528   // (~B & A) | (B & ~A) --> A ^ B
1529   if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
1530       match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B))))
1531     return BinaryOperator::CreateXor(A, B);
1532 
1533   return nullptr;
1534 }
1535 
1536 /// Return true if a constant shift amount is always less than the specified
1537 /// bit-width. If not, the shift could create poison in the narrower type.
1538 static bool canNarrowShiftAmt(Constant *C, unsigned BitWidth) {
1539   APInt Threshold(C->getType()->getScalarSizeInBits(), BitWidth);
1540   return match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
1541 }
1542 
1543 /// Try to use narrower ops (sink zext ops) for an 'and' with binop operand and
1544 /// a common zext operand: and (binop (zext X), C), (zext X).
1545 Instruction *InstCombinerImpl::narrowMaskedBinOp(BinaryOperator &And) {
1546   // This transform could also apply to {or, and, xor}, but there are better
1547   // folds for those cases, so we don't expect those patterns here. AShr is not
1548   // handled because it should always be transformed to LShr in this sequence.
1549   // The subtract transform is different because it has a constant on the left.
1550   // Add/mul commute the constant to RHS; sub with constant RHS becomes add.
1551   Value *Op0 = And.getOperand(0), *Op1 = And.getOperand(1);
1552   Constant *C;
1553   if (!match(Op0, m_OneUse(m_Add(m_Specific(Op1), m_Constant(C)))) &&
1554       !match(Op0, m_OneUse(m_Mul(m_Specific(Op1), m_Constant(C)))) &&
1555       !match(Op0, m_OneUse(m_LShr(m_Specific(Op1), m_Constant(C)))) &&
1556       !match(Op0, m_OneUse(m_Shl(m_Specific(Op1), m_Constant(C)))) &&
1557       !match(Op0, m_OneUse(m_Sub(m_Constant(C), m_Specific(Op1)))))
1558     return nullptr;
1559 
1560   Value *X;
1561   if (!match(Op1, m_ZExt(m_Value(X))) || Op1->hasNUsesOrMore(3))
1562     return nullptr;
1563 
1564   Type *Ty = And.getType();
1565   if (!isa<VectorType>(Ty) && !shouldChangeType(Ty, X->getType()))
1566     return nullptr;
1567 
1568   // If we're narrowing a shift, the shift amount must be safe (less than the
1569   // width) in the narrower type. If the shift amount is greater, instsimplify
1570   // usually handles that case, but we can't guarantee/assert it.
1571   Instruction::BinaryOps Opc = cast<BinaryOperator>(Op0)->getOpcode();
1572   if (Opc == Instruction::LShr || Opc == Instruction::Shl)
1573     if (!canNarrowShiftAmt(C, X->getType()->getScalarSizeInBits()))
1574       return nullptr;
1575 
1576   // and (sub C, (zext X)), (zext X) --> zext (and (sub C', X), X)
1577   // and (binop (zext X), C), (zext X) --> zext (and (binop X, C'), X)
1578   Value *NewC = ConstantExpr::getTrunc(C, X->getType());
1579   Value *NewBO = Opc == Instruction::Sub ? Builder.CreateBinOp(Opc, NewC, X)
1580                                          : Builder.CreateBinOp(Opc, X, NewC);
1581   return new ZExtInst(Builder.CreateAnd(NewBO, X), Ty);
1582 }
1583 
1584 /// Try folding relatively complex patterns for both And and Or operations
1585 /// with all And and Or swapped.
1586 static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
1587                                              InstCombiner::BuilderTy &Builder) {
1588   const Instruction::BinaryOps Opcode = I.getOpcode();
1589   assert(Opcode == Instruction::And || Opcode == Instruction::Or);
1590 
1591   // Flip the logic operation.
1592   const Instruction::BinaryOps FlippedOpcode =
1593       (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1594 
1595   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1596   Value *A, *B, *C, *X, *Y, *Dummy;
1597 
1598   // Match following expressions:
1599   // (~(A | B) & C)
1600   // (~(A & B) | C)
1601   // Captures X = ~(A | B) or ~(A & B)
1602   const auto matchNotOrAnd =
1603       [Opcode, FlippedOpcode](Value *Op, auto m_A, auto m_B, auto m_C,
1604                               Value *&X, bool CountUses = false) -> bool {
1605     if (CountUses && !Op->hasOneUse())
1606       return false;
1607 
1608     if (match(Op, m_c_BinOp(FlippedOpcode,
1609                             m_CombineAnd(m_Value(X),
1610                                          m_Not(m_c_BinOp(Opcode, m_A, m_B))),
1611                             m_C)))
1612       return !CountUses || X->hasOneUse();
1613 
1614     return false;
1615   };
1616 
1617   // (~(A | B) & C) | ... --> ...
1618   // (~(A & B) | C) & ... --> ...
1619   // TODO: One use checks are conservative. We just need to check that a total
1620   //       number of multiple used values does not exceed reduction
1621   //       in operations.
1622   if (matchNotOrAnd(Op0, m_Value(A), m_Value(B), m_Value(C), X)) {
1623     // (~(A | B) & C) | (~(A | C) & B) --> (B ^ C) & ~A
1624     // (~(A & B) | C) & (~(A & C) | B) --> ~((B ^ C) & A)
1625     if (matchNotOrAnd(Op1, m_Specific(A), m_Specific(C), m_Specific(B), Dummy,
1626                       true)) {
1627       Value *Xor = Builder.CreateXor(B, C);
1628       return (Opcode == Instruction::Or)
1629                  ? BinaryOperator::CreateAnd(Xor, Builder.CreateNot(A))
1630                  : BinaryOperator::CreateNot(Builder.CreateAnd(Xor, A));
1631     }
1632 
1633     // (~(A | B) & C) | (~(B | C) & A) --> (A ^ C) & ~B
1634     // (~(A & B) | C) & (~(B & C) | A) --> ~((A ^ C) & B)
1635     if (matchNotOrAnd(Op1, m_Specific(B), m_Specific(C), m_Specific(A), Dummy,
1636                       true)) {
1637       Value *Xor = Builder.CreateXor(A, C);
1638       return (Opcode == Instruction::Or)
1639                  ? BinaryOperator::CreateAnd(Xor, Builder.CreateNot(B))
1640                  : BinaryOperator::CreateNot(Builder.CreateAnd(Xor, B));
1641     }
1642 
1643     // (~(A | B) & C) | ~(A | C) --> ~((B & C) | A)
1644     // (~(A & B) | C) & ~(A & C) --> ~((B | C) & A)
1645     if (match(Op1, m_OneUse(m_Not(m_OneUse(
1646                        m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
1647       return BinaryOperator::CreateNot(Builder.CreateBinOp(
1648           Opcode, Builder.CreateBinOp(FlippedOpcode, B, C), A));
1649 
1650     // (~(A | B) & C) | ~(B | C) --> ~((A & C) | B)
1651     // (~(A & B) | C) & ~(B & C) --> ~((A | C) & B)
1652     if (match(Op1, m_OneUse(m_Not(m_OneUse(
1653                        m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)))))))
1654       return BinaryOperator::CreateNot(Builder.CreateBinOp(
1655           Opcode, Builder.CreateBinOp(FlippedOpcode, A, C), B));
1656 
1657     // (~(A | B) & C) | ~(C | (A ^ B)) --> ~((A | B) & (C | (A ^ B)))
1658     // Note, the pattern with swapped and/or is not handled because the
1659     // result is more undefined than a source:
1660     // (~(A & B) | C) & ~(C & (A ^ B)) --> (A ^ B ^ C) | ~(A | C) is invalid.
1661     if (Opcode == Instruction::Or && Op0->hasOneUse() &&
1662         match(Op1, m_OneUse(m_Not(m_CombineAnd(
1663                        m_Value(Y),
1664                        m_c_BinOp(Opcode, m_Specific(C),
1665                                  m_c_Xor(m_Specific(A), m_Specific(B)))))))) {
1666       // X = ~(A | B)
1667       // Y = (C | (A ^ B)
1668       Value *Or = cast<BinaryOperator>(X)->getOperand(0);
1669       return BinaryOperator::CreateNot(Builder.CreateAnd(Or, Y));
1670     }
1671   }
1672 
1673   // (~A & B & C) | ... --> ...
1674   // (~A | B | C) | ... --> ...
1675   // TODO: One use checks are conservative. We just need to check that a total
1676   //       number of multiple used values does not exceed reduction
1677   //       in operations.
1678   if (match(Op0,
1679             m_OneUse(m_c_BinOp(FlippedOpcode,
1680                                m_BinOp(FlippedOpcode, m_Value(B), m_Value(C)),
1681                                m_CombineAnd(m_Value(X), m_Not(m_Value(A)))))) ||
1682       match(Op0, m_OneUse(m_c_BinOp(
1683                      FlippedOpcode,
1684                      m_c_BinOp(FlippedOpcode, m_Value(C),
1685                                m_CombineAnd(m_Value(X), m_Not(m_Value(A)))),
1686                      m_Value(B))))) {
1687     // X = ~A
1688     // (~A & B & C) | ~(A | B | C) --> ~(A | (B ^ C))
1689     // (~A | B | C) & ~(A & B & C) --> (~A | (B ^ C))
1690     if (match(Op1, m_OneUse(m_Not(m_c_BinOp(
1691                        Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)),
1692                        m_Specific(C))))) ||
1693         match(Op1, m_OneUse(m_Not(m_c_BinOp(
1694                        Opcode, m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)),
1695                        m_Specific(A))))) ||
1696         match(Op1, m_OneUse(m_Not(m_c_BinOp(
1697                        Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)),
1698                        m_Specific(B)))))) {
1699       Value *Xor = Builder.CreateXor(B, C);
1700       return (Opcode == Instruction::Or)
1701                  ? BinaryOperator::CreateNot(Builder.CreateOr(Xor, A))
1702                  : BinaryOperator::CreateOr(Xor, X);
1703     }
1704 
1705     // (~A & B & C) | ~(A | B) --> (C | ~B) & ~A
1706     // (~A | B | C) & ~(A & B) --> (C & ~B) | ~A
1707     if (match(Op1, m_OneUse(m_Not(m_OneUse(
1708                        m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)))))))
1709       return BinaryOperator::Create(
1710           FlippedOpcode, Builder.CreateBinOp(Opcode, C, Builder.CreateNot(B)),
1711           X);
1712 
1713     // (~A & B & C) | ~(A | C) --> (B | ~C) & ~A
1714     // (~A | B | C) & ~(A & C) --> (B & ~C) | ~A
1715     if (match(Op1, m_OneUse(m_Not(m_OneUse(
1716                        m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
1717       return BinaryOperator::Create(
1718           FlippedOpcode, Builder.CreateBinOp(Opcode, B, Builder.CreateNot(C)),
1719           X);
1720   }
1721 
1722   return nullptr;
1723 }
1724 
1725 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
1726 // here. We should standardize that construct where it is needed or choose some
1727 // other way to ensure that commutated variants of patterns are not missed.
1728 Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
1729   Type *Ty = I.getType();
1730 
1731   if (Value *V = simplifyAndInst(I.getOperand(0), I.getOperand(1),
1732                                  SQ.getWithInstruction(&I)))
1733     return replaceInstUsesWith(I, V);
1734 
1735   if (SimplifyAssociativeOrCommutative(I))
1736     return &I;
1737 
1738   if (Instruction *X = foldVectorBinop(I))
1739     return X;
1740 
1741   if (Instruction *Phi = foldBinopWithPhiOperands(I))
1742     return Phi;
1743 
1744   // See if we can simplify any instructions used by the instruction whose sole
1745   // purpose is to compute bits we don't care about.
1746   if (SimplifyDemandedInstructionBits(I))
1747     return &I;
1748 
1749   // Do this before using distributive laws to catch simple and/or/not patterns.
1750   if (Instruction *Xor = foldAndToXor(I, Builder))
1751     return Xor;
1752 
1753   if (Instruction *X = foldComplexAndOrPatterns(I, Builder))
1754     return X;
1755 
1756   // (A|B)&(A|C) -> A|(B&C) etc
1757   if (Value *V = SimplifyUsingDistributiveLaws(I))
1758     return replaceInstUsesWith(I, V);
1759 
1760   if (Value *V = SimplifyBSwap(I, Builder))
1761     return replaceInstUsesWith(I, V);
1762 
1763   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1764 
1765   Value *X, *Y;
1766   if (match(Op0, m_OneUse(m_LogicalShift(m_One(), m_Value(X)))) &&
1767       match(Op1, m_One())) {
1768     // (1 << X) & 1 --> zext(X == 0)
1769     // (1 >> X) & 1 --> zext(X == 0)
1770     Value *IsZero = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, 0));
1771     return new ZExtInst(IsZero, Ty);
1772   }
1773 
1774   const APInt *C;
1775   if (match(Op1, m_APInt(C))) {
1776     const APInt *XorC;
1777     if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_APInt(XorC))))) {
1778       // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
1779       Constant *NewC = ConstantInt::get(Ty, *C & *XorC);
1780       Value *And = Builder.CreateAnd(X, Op1);
1781       And->takeName(Op0);
1782       return BinaryOperator::CreateXor(And, NewC);
1783     }
1784 
1785     const APInt *OrC;
1786     if (match(Op0, m_OneUse(m_Or(m_Value(X), m_APInt(OrC))))) {
1787       // (X | C1) & C2 --> (X & C2^(C1&C2)) | (C1&C2)
1788       // NOTE: This reduces the number of bits set in the & mask, which
1789       // can expose opportunities for store narrowing for scalars.
1790       // NOTE: SimplifyDemandedBits should have already removed bits from C1
1791       // that aren't set in C2. Meaning we can replace (C1&C2) with C1 in
1792       // above, but this feels safer.
1793       APInt Together = *C & *OrC;
1794       Value *And = Builder.CreateAnd(X, ConstantInt::get(Ty, Together ^ *C));
1795       And->takeName(Op0);
1796       return BinaryOperator::CreateOr(And, ConstantInt::get(Ty, Together));
1797     }
1798 
1799     unsigned Width = Ty->getScalarSizeInBits();
1800     const APInt *ShiftC;
1801     if (match(Op0, m_OneUse(m_SExt(m_AShr(m_Value(X), m_APInt(ShiftC)))))) {
1802       if (*C == APInt::getLowBitsSet(Width, Width - ShiftC->getZExtValue())) {
1803         // We are clearing high bits that were potentially set by sext+ashr:
1804         // and (sext (ashr X, ShiftC)), C --> lshr (sext X), ShiftC
1805         Value *Sext = Builder.CreateSExt(X, Ty);
1806         Constant *ShAmtC = ConstantInt::get(Ty, ShiftC->zext(Width));
1807         return BinaryOperator::CreateLShr(Sext, ShAmtC);
1808       }
1809     }
1810 
1811     // If this 'and' clears the sign-bits added by ashr, replace with lshr:
1812     // and (ashr X, ShiftC), C --> lshr X, ShiftC
1813     if (match(Op0, m_AShr(m_Value(X), m_APInt(ShiftC))) && ShiftC->ult(Width) &&
1814         C->isMask(Width - ShiftC->getZExtValue()))
1815       return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, *ShiftC));
1816 
1817     const APInt *AddC;
1818     if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
1819       // If we add zeros to every bit below a mask, the add has no effect:
1820       // (X + AddC) & LowMaskC --> X & LowMaskC
1821       unsigned Ctlz = C->countLeadingZeros();
1822       APInt LowMask(APInt::getLowBitsSet(Width, Width - Ctlz));
1823       if ((*AddC & LowMask).isZero())
1824         return BinaryOperator::CreateAnd(X, Op1);
1825 
1826       // If we are masking the result of the add down to exactly one bit and
1827       // the constant we are adding has no bits set below that bit, then the
1828       // add is flipping a single bit. Example:
1829       // (X + 4) & 4 --> (X & 4) ^ 4
1830       if (Op0->hasOneUse() && C->isPowerOf2() && (*AddC & (*C - 1)) == 0) {
1831         assert((*C & *AddC) != 0 && "Expected common bit");
1832         Value *NewAnd = Builder.CreateAnd(X, Op1);
1833         return BinaryOperator::CreateXor(NewAnd, Op1);
1834       }
1835     }
1836 
1837     // ((C1 OP zext(X)) & C2) -> zext((C1 OP X) & C2) if C2 fits in the
1838     // bitwidth of X and OP behaves well when given trunc(C1) and X.
1839     auto isNarrowableBinOpcode = [](BinaryOperator *B) {
1840       switch (B->getOpcode()) {
1841       case Instruction::Xor:
1842       case Instruction::Or:
1843       case Instruction::Mul:
1844       case Instruction::Add:
1845       case Instruction::Sub:
1846         return true;
1847       default:
1848         return false;
1849       }
1850     };
1851     BinaryOperator *BO;
1852     if (match(Op0, m_OneUse(m_BinOp(BO))) && isNarrowableBinOpcode(BO)) {
1853       Instruction::BinaryOps BOpcode = BO->getOpcode();
1854       Value *X;
1855       const APInt *C1;
1856       // TODO: The one-use restrictions could be relaxed a little if the AND
1857       // is going to be removed.
1858       // Try to narrow the 'and' and a binop with constant operand:
1859       // and (bo (zext X), C1), C --> zext (and (bo X, TruncC1), TruncC)
1860       if (match(BO, m_c_BinOp(m_OneUse(m_ZExt(m_Value(X))), m_APInt(C1))) &&
1861           C->isIntN(X->getType()->getScalarSizeInBits())) {
1862         unsigned XWidth = X->getType()->getScalarSizeInBits();
1863         Constant *TruncC1 = ConstantInt::get(X->getType(), C1->trunc(XWidth));
1864         Value *BinOp = isa<ZExtInst>(BO->getOperand(0))
1865                            ? Builder.CreateBinOp(BOpcode, X, TruncC1)
1866                            : Builder.CreateBinOp(BOpcode, TruncC1, X);
1867         Constant *TruncC = ConstantInt::get(X->getType(), C->trunc(XWidth));
1868         Value *And = Builder.CreateAnd(BinOp, TruncC);
1869         return new ZExtInst(And, Ty);
1870       }
1871 
1872       // Similar to above: if the mask matches the zext input width, then the
1873       // 'and' can be eliminated, so we can truncate the other variable op:
1874       // and (bo (zext X), Y), C --> zext (bo X, (trunc Y))
1875       if (isa<Instruction>(BO->getOperand(0)) &&
1876           match(BO->getOperand(0), m_OneUse(m_ZExt(m_Value(X)))) &&
1877           C->isMask(X->getType()->getScalarSizeInBits())) {
1878         Y = BO->getOperand(1);
1879         Value *TrY = Builder.CreateTrunc(Y, X->getType(), Y->getName() + ".tr");
1880         Value *NewBO =
1881             Builder.CreateBinOp(BOpcode, X, TrY, BO->getName() + ".narrow");
1882         return new ZExtInst(NewBO, Ty);
1883       }
1884       // and (bo Y, (zext X)), C --> zext (bo (trunc Y), X)
1885       if (isa<Instruction>(BO->getOperand(1)) &&
1886           match(BO->getOperand(1), m_OneUse(m_ZExt(m_Value(X)))) &&
1887           C->isMask(X->getType()->getScalarSizeInBits())) {
1888         Y = BO->getOperand(0);
1889         Value *TrY = Builder.CreateTrunc(Y, X->getType(), Y->getName() + ".tr");
1890         Value *NewBO =
1891             Builder.CreateBinOp(BOpcode, TrY, X, BO->getName() + ".narrow");
1892         return new ZExtInst(NewBO, Ty);
1893       }
1894     }
1895 
1896     // This is intentionally placed after the narrowing transforms for
1897     // efficiency (transform directly to the narrow logic op if possible).
1898     // If the mask is only needed on one incoming arm, push the 'and' op up.
1899     if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_Value(Y)))) ||
1900         match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
1901       APInt NotAndMask(~(*C));
1902       BinaryOperator::BinaryOps BinOp = cast<BinaryOperator>(Op0)->getOpcode();
1903       if (MaskedValueIsZero(X, NotAndMask, 0, &I)) {
1904         // Not masking anything out for the LHS, move mask to RHS.
1905         // and ({x}or X, Y), C --> {x}or X, (and Y, C)
1906         Value *NewRHS = Builder.CreateAnd(Y, Op1, Y->getName() + ".masked");
1907         return BinaryOperator::Create(BinOp, X, NewRHS);
1908       }
1909       if (!isa<Constant>(Y) && MaskedValueIsZero(Y, NotAndMask, 0, &I)) {
1910         // Not masking anything out for the RHS, move mask to LHS.
1911         // and ({x}or X, Y), C --> {x}or (and X, C), Y
1912         Value *NewLHS = Builder.CreateAnd(X, Op1, X->getName() + ".masked");
1913         return BinaryOperator::Create(BinOp, NewLHS, Y);
1914       }
1915     }
1916 
1917     Constant *C1, *C2;
1918     const APInt *C3 = C;
1919     Value *X;
1920     if (C3->isPowerOf2() &&
1921         match(Op0, m_OneUse(m_LShr(m_Shl(m_ImmConstant(C1), m_Value(X)),
1922                                    m_ImmConstant(C2)))) &&
1923         match(C1, m_Power2())) {
1924       Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
1925       Constant *Log2C3 = ConstantInt::get(Ty, C3->countTrailingZeros());
1926       Constant *LshrC = ConstantExpr::getAdd(C2, Log2C3);
1927       KnownBits KnownLShrc = computeKnownBits(LshrC, 0, nullptr);
1928       if (KnownLShrc.getMaxValue().ult(Width)) {
1929         // iff C1,C3 is pow2 and C2 + cttz(C3) < BitWidth:
1930         // ((C1 << X) >> C2) & C3 -> X == (cttz(C3)+C2-cttz(C1)) ? C3 : 0
1931         Constant *CmpC = ConstantExpr::getSub(LshrC, Log2C1);
1932         Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
1933         return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
1934                                   ConstantInt::getNullValue(Ty));
1935       }
1936       // TODO: Symmetrical case
1937       // iff C1,C3 is pow2 and Log2(C3) >= C2:
1938       // ((C1 >> X) << C2) & C3 -> X == (cttz(C1)+C2-cttz(C3)) ? C3 : 0
1939     }
1940   }
1941 
1942   if (match(&I, m_And(m_OneUse(m_Shl(m_ZExt(m_Value(X)), m_Value(Y))),
1943                       m_SignMask())) &&
1944       match(Y, m_SpecificInt_ICMP(
1945                    ICmpInst::Predicate::ICMP_EQ,
1946                    APInt(Ty->getScalarSizeInBits(),
1947                          Ty->getScalarSizeInBits() -
1948                              X->getType()->getScalarSizeInBits())))) {
1949     auto *SExt = Builder.CreateSExt(X, Ty, X->getName() + ".signext");
1950     auto *SanitizedSignMask = cast<Constant>(Op1);
1951     // We must be careful with the undef elements of the sign bit mask, however:
1952     // the mask elt can be undef iff the shift amount for that lane was undef,
1953     // otherwise we need to sanitize undef masks to zero.
1954     SanitizedSignMask = Constant::replaceUndefsWith(
1955         SanitizedSignMask, ConstantInt::getNullValue(Ty->getScalarType()));
1956     SanitizedSignMask =
1957         Constant::mergeUndefsWith(SanitizedSignMask, cast<Constant>(Y));
1958     return BinaryOperator::CreateAnd(SExt, SanitizedSignMask);
1959   }
1960 
1961   if (Instruction *Z = narrowMaskedBinOp(I))
1962     return Z;
1963 
1964   if (I.getType()->isIntOrIntVectorTy(1)) {
1965     if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
1966       if (auto *I =
1967               foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ true))
1968         return I;
1969     }
1970     if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
1971       if (auto *I =
1972               foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ true))
1973         return I;
1974     }
1975   }
1976 
1977   if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
1978     return FoldedLogic;
1979 
1980   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
1981     return DeMorgan;
1982 
1983   {
1984     Value *A, *B, *C;
1985     // A & (A ^ B) --> A & ~B
1986     if (match(Op1, m_OneUse(m_c_Xor(m_Specific(Op0), m_Value(B)))))
1987       return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(B));
1988     // (A ^ B) & A --> A & ~B
1989     if (match(Op0, m_OneUse(m_c_Xor(m_Specific(Op1), m_Value(B)))))
1990       return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(B));
1991 
1992     // A & ~(A ^ B) --> A & B
1993     if (match(Op1, m_Not(m_c_Xor(m_Specific(Op0), m_Value(B)))))
1994       return BinaryOperator::CreateAnd(Op0, B);
1995     // ~(A ^ B) & A --> A & B
1996     if (match(Op0, m_Not(m_c_Xor(m_Specific(Op1), m_Value(B)))))
1997       return BinaryOperator::CreateAnd(Op1, B);
1998 
1999     // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
2000     if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2001       if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2002         if (Op1->hasOneUse() || isFreeToInvert(C, C->hasOneUse()))
2003           return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C));
2004 
2005     // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
2006     if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2007       if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2008         if (Op0->hasOneUse() || isFreeToInvert(C, C->hasOneUse()))
2009           return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
2010 
2011     // (A | B) & (~A ^ B) -> A & B
2012     // (A | B) & (B ^ ~A) -> A & B
2013     // (B | A) & (~A ^ B) -> A & B
2014     // (B | A) & (B ^ ~A) -> A & B
2015     if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2016         match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
2017       return BinaryOperator::CreateAnd(A, B);
2018 
2019     // (~A ^ B) & (A | B) -> A & B
2020     // (~A ^ B) & (B | A) -> A & B
2021     // (B ^ ~A) & (A | B) -> A & B
2022     // (B ^ ~A) & (B | A) -> A & B
2023     if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2024         match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
2025       return BinaryOperator::CreateAnd(A, B);
2026 
2027     // (~A | B) & (A ^ B) -> ~A & B
2028     // (~A | B) & (B ^ A) -> ~A & B
2029     // (B | ~A) & (A ^ B) -> ~A & B
2030     // (B | ~A) & (B ^ A) -> ~A & B
2031     if (match(Op0, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2032         match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
2033       return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2034 
2035     // (A ^ B) & (~A | B) -> ~A & B
2036     // (B ^ A) & (~A | B) -> ~A & B
2037     // (A ^ B) & (B | ~A) -> ~A & B
2038     // (B ^ A) & (B | ~A) -> ~A & B
2039     if (match(Op1, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2040         match(Op0, m_c_Xor(m_Specific(A), m_Specific(B))))
2041       return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2042   }
2043 
2044   {
2045     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2046     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2047     if (LHS && RHS)
2048       if (Value *Res = foldAndOrOfICmps(LHS, RHS, I, /* IsAnd */ true))
2049         return replaceInstUsesWith(I, Res);
2050 
2051     // TODO: Make this recursive; it's a little tricky because an arbitrary
2052     // number of 'and' instructions might have to be created.
2053     if (LHS && match(Op1, m_OneUse(m_LogicalAnd(m_Value(X), m_Value(Y))))) {
2054       bool IsLogical = isa<SelectInst>(Op1);
2055       // LHS & (X && Y) --> (LHS && X) && Y
2056       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2057         if (Value *Res =
2058                 foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ true, IsLogical))
2059           return replaceInstUsesWith(I, IsLogical
2060                                             ? Builder.CreateLogicalAnd(Res, Y)
2061                                             : Builder.CreateAnd(Res, Y));
2062       // LHS & (X && Y) --> X && (LHS & Y)
2063       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2064         if (Value *Res = foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ true,
2065                                           /* IsLogical */ false))
2066           return replaceInstUsesWith(I, IsLogical
2067                                             ? Builder.CreateLogicalAnd(X, Res)
2068                                             : Builder.CreateAnd(X, Res));
2069     }
2070     if (RHS && match(Op0, m_OneUse(m_LogicalAnd(m_Value(X), m_Value(Y))))) {
2071       bool IsLogical = isa<SelectInst>(Op0);
2072       // (X && Y) & RHS --> (X && RHS) && Y
2073       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2074         if (Value *Res =
2075                 foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ true, IsLogical))
2076           return replaceInstUsesWith(I, IsLogical
2077                                             ? Builder.CreateLogicalAnd(Res, Y)
2078                                             : Builder.CreateAnd(Res, Y));
2079       // (X && Y) & RHS --> X && (Y & RHS)
2080       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2081         if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ true,
2082                                           /* IsLogical */ false))
2083           return replaceInstUsesWith(I, IsLogical
2084                                             ? Builder.CreateLogicalAnd(X, Res)
2085                                             : Builder.CreateAnd(X, Res));
2086     }
2087   }
2088 
2089   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2090     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2091       if (Value *Res = foldLogicOfFCmps(LHS, RHS, /*IsAnd*/ true))
2092         return replaceInstUsesWith(I, Res);
2093 
2094   if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2095     return FoldedFCmps;
2096 
2097   if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
2098     return CastedAnd;
2099 
2100   if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2101     return Sel;
2102 
2103   // and(sext(A), B) / and(B, sext(A)) --> A ? B : 0, where A is i1 or <N x i1>.
2104   // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2105   //       with binop identity constant. But creating a select with non-constant
2106   //       arm may not be reversible due to poison semantics. Is that a good
2107   //       canonicalization?
2108   Value *A;
2109   if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2110       A->getType()->isIntOrIntVectorTy(1))
2111     return SelectInst::Create(A, Op1, Constant::getNullValue(Ty));
2112   if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2113       A->getType()->isIntOrIntVectorTy(1))
2114     return SelectInst::Create(A, Op0, Constant::getNullValue(Ty));
2115 
2116   // (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0
2117   unsigned FullShift = Ty->getScalarSizeInBits() - 1;
2118   if (match(&I, m_c_And(m_OneUse(m_AShr(m_Value(X), m_SpecificInt(FullShift))),
2119                         m_Value(Y)))) {
2120     Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
2121     return SelectInst::Create(IsNeg, Y, ConstantInt::getNullValue(Ty));
2122   }
2123   // If there's a 'not' of the shifted value, swap the select operands:
2124   // ~(iN X s>> (N-1)) & Y --> (X s< 0) ? 0 : Y
2125   if (match(&I, m_c_And(m_OneUse(m_Not(
2126                             m_AShr(m_Value(X), m_SpecificInt(FullShift)))),
2127                         m_Value(Y)))) {
2128     Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
2129     return SelectInst::Create(IsNeg, ConstantInt::getNullValue(Ty), Y);
2130   }
2131 
2132   // (~x) & y  -->  ~(x | (~y))  iff that gets rid of inversions
2133   if (sinkNotIntoOtherHandOfAndOrOr(I))
2134     return &I;
2135 
2136   // An and recurrence w/loop invariant step is equivelent to (and start, step)
2137   PHINode *PN = nullptr;
2138   Value *Start = nullptr, *Step = nullptr;
2139   if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
2140     return replaceInstUsesWith(I, Builder.CreateAnd(Start, Step));
2141 
2142   return nullptr;
2143 }
2144 
2145 Instruction *InstCombinerImpl::matchBSwapOrBitReverse(Instruction &I,
2146                                                       bool MatchBSwaps,
2147                                                       bool MatchBitReversals) {
2148   SmallVector<Instruction *, 4> Insts;
2149   if (!recognizeBSwapOrBitReverseIdiom(&I, MatchBSwaps, MatchBitReversals,
2150                                        Insts))
2151     return nullptr;
2152   Instruction *LastInst = Insts.pop_back_val();
2153   LastInst->removeFromParent();
2154 
2155   for (auto *Inst : Insts)
2156     Worklist.push(Inst);
2157   return LastInst;
2158 }
2159 
2160 /// Match UB-safe variants of the funnel shift intrinsic.
2161 static Instruction *matchFunnelShift(Instruction &Or, InstCombinerImpl &IC) {
2162   // TODO: Can we reduce the code duplication between this and the related
2163   // rotate matching code under visitSelect and visitTrunc?
2164   unsigned Width = Or.getType()->getScalarSizeInBits();
2165 
2166   // First, find an or'd pair of opposite shifts:
2167   // or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1)
2168   BinaryOperator *Or0, *Or1;
2169   if (!match(Or.getOperand(0), m_BinOp(Or0)) ||
2170       !match(Or.getOperand(1), m_BinOp(Or1)))
2171     return nullptr;
2172 
2173   Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
2174   if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) ||
2175       !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) ||
2176       Or0->getOpcode() == Or1->getOpcode())
2177     return nullptr;
2178 
2179   // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)).
2180   if (Or0->getOpcode() == BinaryOperator::LShr) {
2181     std::swap(Or0, Or1);
2182     std::swap(ShVal0, ShVal1);
2183     std::swap(ShAmt0, ShAmt1);
2184   }
2185   assert(Or0->getOpcode() == BinaryOperator::Shl &&
2186          Or1->getOpcode() == BinaryOperator::LShr &&
2187          "Illegal or(shift,shift) pair");
2188 
2189   // Match the shift amount operands for a funnel shift pattern. This always
2190   // matches a subtraction on the R operand.
2191   auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
2192     // Check for constant shift amounts that sum to the bitwidth.
2193     const APInt *LI, *RI;
2194     if (match(L, m_APIntAllowUndef(LI)) && match(R, m_APIntAllowUndef(RI)))
2195       if (LI->ult(Width) && RI->ult(Width) && (*LI + *RI) == Width)
2196         return ConstantInt::get(L->getType(), *LI);
2197 
2198     Constant *LC, *RC;
2199     if (match(L, m_Constant(LC)) && match(R, m_Constant(RC)) &&
2200         match(L, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2201         match(R, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2202         match(ConstantExpr::getAdd(LC, RC), m_SpecificIntAllowUndef(Width)))
2203       return ConstantExpr::mergeUndefsWith(LC, RC);
2204 
2205     // (shl ShVal, X) | (lshr ShVal, (Width - x)) iff X < Width.
2206     // We limit this to X < Width in case the backend re-expands the intrinsic,
2207     // and has to reintroduce a shift modulo operation (InstCombine might remove
2208     // it after this fold). This still doesn't guarantee that the final codegen
2209     // will match this original pattern.
2210     if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L))))) {
2211       KnownBits KnownL = IC.computeKnownBits(L, /*Depth*/ 0, &Or);
2212       return KnownL.getMaxValue().ult(Width) ? L : nullptr;
2213     }
2214 
2215     // For non-constant cases, the following patterns currently only work for
2216     // rotation patterns.
2217     // TODO: Add general funnel-shift compatible patterns.
2218     if (ShVal0 != ShVal1)
2219       return nullptr;
2220 
2221     // For non-constant cases we don't support non-pow2 shift masks.
2222     // TODO: Is it worth matching urem as well?
2223     if (!isPowerOf2_32(Width))
2224       return nullptr;
2225 
2226     // The shift amount may be masked with negation:
2227     // (shl ShVal, (X & (Width - 1))) | (lshr ShVal, ((-X) & (Width - 1)))
2228     Value *X;
2229     unsigned Mask = Width - 1;
2230     if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) &&
2231         match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask))))
2232       return X;
2233 
2234     // Similar to above, but the shift amount may be extended after masking,
2235     // so return the extended value as the parameter for the intrinsic.
2236     if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2237         match(R, m_And(m_Neg(m_ZExt(m_And(m_Specific(X), m_SpecificInt(Mask)))),
2238                        m_SpecificInt(Mask))))
2239       return L;
2240 
2241     if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2242         match(R, m_ZExt(m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask)))))
2243       return L;
2244 
2245     return nullptr;
2246   };
2247 
2248   Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width);
2249   bool IsFshl = true; // Sub on LSHR.
2250   if (!ShAmt) {
2251     ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width);
2252     IsFshl = false; // Sub on SHL.
2253   }
2254   if (!ShAmt)
2255     return nullptr;
2256 
2257   Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2258   Function *F = Intrinsic::getDeclaration(Or.getModule(), IID, Or.getType());
2259   return CallInst::Create(F, {ShVal0, ShVal1, ShAmt});
2260 }
2261 
2262 /// Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns.
2263 static Instruction *matchOrConcat(Instruction &Or,
2264                                   InstCombiner::BuilderTy &Builder) {
2265   assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'");
2266   Value *Op0 = Or.getOperand(0), *Op1 = Or.getOperand(1);
2267   Type *Ty = Or.getType();
2268 
2269   unsigned Width = Ty->getScalarSizeInBits();
2270   if ((Width & 1) != 0)
2271     return nullptr;
2272   unsigned HalfWidth = Width / 2;
2273 
2274   // Canonicalize zext (lower half) to LHS.
2275   if (!isa<ZExtInst>(Op0))
2276     std::swap(Op0, Op1);
2277 
2278   // Find lower/upper half.
2279   Value *LowerSrc, *ShlVal, *UpperSrc;
2280   const APInt *C;
2281   if (!match(Op0, m_OneUse(m_ZExt(m_Value(LowerSrc)))) ||
2282       !match(Op1, m_OneUse(m_Shl(m_Value(ShlVal), m_APInt(C)))) ||
2283       !match(ShlVal, m_OneUse(m_ZExt(m_Value(UpperSrc)))))
2284     return nullptr;
2285   if (*C != HalfWidth || LowerSrc->getType() != UpperSrc->getType() ||
2286       LowerSrc->getType()->getScalarSizeInBits() != HalfWidth)
2287     return nullptr;
2288 
2289   auto ConcatIntrinsicCalls = [&](Intrinsic::ID id, Value *Lo, Value *Hi) {
2290     Value *NewLower = Builder.CreateZExt(Lo, Ty);
2291     Value *NewUpper = Builder.CreateZExt(Hi, Ty);
2292     NewUpper = Builder.CreateShl(NewUpper, HalfWidth);
2293     Value *BinOp = Builder.CreateOr(NewLower, NewUpper);
2294     Function *F = Intrinsic::getDeclaration(Or.getModule(), id, Ty);
2295     return Builder.CreateCall(F, BinOp);
2296   };
2297 
2298   // BSWAP: Push the concat down, swapping the lower/upper sources.
2299   // concat(bswap(x),bswap(y)) -> bswap(concat(x,y))
2300   Value *LowerBSwap, *UpperBSwap;
2301   if (match(LowerSrc, m_BSwap(m_Value(LowerBSwap))) &&
2302       match(UpperSrc, m_BSwap(m_Value(UpperBSwap))))
2303     return ConcatIntrinsicCalls(Intrinsic::bswap, UpperBSwap, LowerBSwap);
2304 
2305   // BITREVERSE: Push the concat down, swapping the lower/upper sources.
2306   // concat(bitreverse(x),bitreverse(y)) -> bitreverse(concat(x,y))
2307   Value *LowerBRev, *UpperBRev;
2308   if (match(LowerSrc, m_BitReverse(m_Value(LowerBRev))) &&
2309       match(UpperSrc, m_BitReverse(m_Value(UpperBRev))))
2310     return ConcatIntrinsicCalls(Intrinsic::bitreverse, UpperBRev, LowerBRev);
2311 
2312   return nullptr;
2313 }
2314 
2315 /// If all elements of two constant vectors are 0/-1 and inverses, return true.
2316 static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
2317   unsigned NumElts = cast<FixedVectorType>(C1->getType())->getNumElements();
2318   for (unsigned i = 0; i != NumElts; ++i) {
2319     Constant *EltC1 = C1->getAggregateElement(i);
2320     Constant *EltC2 = C2->getAggregateElement(i);
2321     if (!EltC1 || !EltC2)
2322       return false;
2323 
2324     // One element must be all ones, and the other must be all zeros.
2325     if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
2326           (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
2327       return false;
2328   }
2329   return true;
2330 }
2331 
2332 /// We have an expression of the form (A & C) | (B & D). If A is a scalar or
2333 /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
2334 /// B, it can be used as the condition operand of a select instruction.
2335 Value *InstCombinerImpl::getSelectCondition(Value *A, Value *B) {
2336   // We may have peeked through bitcasts in the caller.
2337   // Exit immediately if we don't have (vector) integer types.
2338   Type *Ty = A->getType();
2339   if (!Ty->isIntOrIntVectorTy() || !B->getType()->isIntOrIntVectorTy())
2340     return nullptr;
2341 
2342   // If A is the 'not' operand of B and has enough signbits, we have our answer.
2343   if (match(B, m_Not(m_Specific(A)))) {
2344     // If these are scalars or vectors of i1, A can be used directly.
2345     if (Ty->isIntOrIntVectorTy(1))
2346       return A;
2347 
2348     // If we look through a vector bitcast, the caller will bitcast the operands
2349     // to match the condition's number of bits (N x i1).
2350     // To make this poison-safe, disallow bitcast from wide element to narrow
2351     // element. That could allow poison in lanes where it was not present in the
2352     // original code.
2353     A = peekThroughBitcast(A);
2354     if (A->getType()->isIntOrIntVectorTy()) {
2355       unsigned NumSignBits = ComputeNumSignBits(A);
2356       if (NumSignBits == A->getType()->getScalarSizeInBits() &&
2357           NumSignBits <= Ty->getScalarSizeInBits())
2358         return Builder.CreateTrunc(A, CmpInst::makeCmpResultType(A->getType()));
2359     }
2360     return nullptr;
2361   }
2362 
2363   // If both operands are constants, see if the constants are inverse bitmasks.
2364   Constant *AConst, *BConst;
2365   if (match(A, m_Constant(AConst)) && match(B, m_Constant(BConst)))
2366     if (AConst == ConstantExpr::getNot(BConst) &&
2367         ComputeNumSignBits(A) == Ty->getScalarSizeInBits())
2368       return Builder.CreateZExtOrTrunc(A, CmpInst::makeCmpResultType(Ty));
2369 
2370   // Look for more complex patterns. The 'not' op may be hidden behind various
2371   // casts. Look through sexts and bitcasts to find the booleans.
2372   Value *Cond;
2373   Value *NotB;
2374   if (match(A, m_SExt(m_Value(Cond))) &&
2375       Cond->getType()->isIntOrIntVectorTy(1)) {
2376     // A = sext i1 Cond; B = sext (not (i1 Cond))
2377     if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
2378       return Cond;
2379 
2380     // A = sext i1 Cond; B = not ({bitcast} (sext (i1 Cond)))
2381     // TODO: The one-use checks are unnecessary or misplaced. If the caller
2382     //       checked for uses on logic ops/casts, that should be enough to
2383     //       make this transform worthwhile.
2384     if (match(B, m_OneUse(m_Not(m_Value(NotB))))) {
2385       NotB = peekThroughBitcast(NotB, true);
2386       if (match(NotB, m_SExt(m_Specific(Cond))))
2387         return Cond;
2388     }
2389   }
2390 
2391   // All scalar (and most vector) possibilities should be handled now.
2392   // Try more matches that only apply to non-splat constant vectors.
2393   if (!Ty->isVectorTy())
2394     return nullptr;
2395 
2396   // If both operands are xor'd with constants using the same sexted boolean
2397   // operand, see if the constants are inverse bitmasks.
2398   // TODO: Use ConstantExpr::getNot()?
2399   if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AConst)))) &&
2400       match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BConst)))) &&
2401       Cond->getType()->isIntOrIntVectorTy(1) &&
2402       areInverseVectorBitmasks(AConst, BConst)) {
2403     AConst = ConstantExpr::getTrunc(AConst, CmpInst::makeCmpResultType(Ty));
2404     return Builder.CreateXor(Cond, AConst);
2405   }
2406   return nullptr;
2407 }
2408 
2409 /// We have an expression of the form (A & C) | (B & D). Try to simplify this
2410 /// to "A' ? C : D", where A' is a boolean or vector of booleans.
2411 Value *InstCombinerImpl::matchSelectFromAndOr(Value *A, Value *C, Value *B,
2412                                               Value *D) {
2413   // The potential condition of the select may be bitcasted. In that case, look
2414   // through its bitcast and the corresponding bitcast of the 'not' condition.
2415   Type *OrigType = A->getType();
2416   A = peekThroughBitcast(A, true);
2417   B = peekThroughBitcast(B, true);
2418   if (Value *Cond = getSelectCondition(A, B)) {
2419     // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
2420     // If this is a vector, we may need to cast to match the condition's length.
2421     // The bitcasts will either all exist or all not exist. The builder will
2422     // not create unnecessary casts if the types already match.
2423     Type *SelTy = A->getType();
2424     if (auto *VecTy = dyn_cast<VectorType>(Cond->getType())) {
2425       // For a fixed or scalable vector get N from <{vscale x} N x iM>
2426       unsigned Elts = VecTy->getElementCount().getKnownMinValue();
2427       // For a fixed or scalable vector, get the size in bits of N x iM; for a
2428       // scalar this is just M.
2429       unsigned SelEltSize = SelTy->getPrimitiveSizeInBits().getKnownMinSize();
2430       Type *EltTy = Builder.getIntNTy(SelEltSize / Elts);
2431       SelTy = VectorType::get(EltTy, VecTy->getElementCount());
2432     }
2433     Value *BitcastC = Builder.CreateBitCast(C, SelTy);
2434     Value *BitcastD = Builder.CreateBitCast(D, SelTy);
2435     Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
2436     return Builder.CreateBitCast(Select, OrigType);
2437   }
2438 
2439   return nullptr;
2440 }
2441 
2442 // (icmp eq X, 0) | (icmp ult Other, X) -> (icmp ule Other, X-1)
2443 // (icmp ne X, 0) & (icmp uge Other, X) -> (icmp ugt Other, X-1)
2444 Value *foldAndOrOfICmpEqZeroAndICmp(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
2445                                     IRBuilderBase &Builder) {
2446   ICmpInst::Predicate LPred =
2447       IsAnd ? LHS->getInversePredicate() : LHS->getPredicate();
2448   ICmpInst::Predicate RPred =
2449       IsAnd ? RHS->getInversePredicate() : RHS->getPredicate();
2450   Value *LHS0 = LHS->getOperand(0);
2451   if (LPred != ICmpInst::ICMP_EQ || !match(LHS->getOperand(1), m_Zero()) ||
2452       !LHS0->getType()->isIntOrIntVectorTy() ||
2453       !(LHS->hasOneUse() || RHS->hasOneUse()))
2454     return nullptr;
2455 
2456   Value *Other;
2457   if (RPred == ICmpInst::ICMP_ULT && RHS->getOperand(1) == LHS0)
2458     Other = RHS->getOperand(0);
2459   else if (RPred == ICmpInst::ICMP_UGT && RHS->getOperand(0) == LHS0)
2460     Other = RHS->getOperand(1);
2461   else
2462     return nullptr;
2463 
2464   return Builder.CreateICmp(
2465       IsAnd ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE,
2466       Builder.CreateAdd(LHS0, Constant::getAllOnesValue(LHS0->getType())),
2467       Other);
2468 }
2469 
2470 /// Fold (icmp)&(icmp) or (icmp)|(icmp) if possible.
2471 /// If IsLogical is true, then the and/or is in select form and the transform
2472 /// must be poison-safe.
2473 Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
2474                                           Instruction &I, bool IsAnd,
2475                                           bool IsLogical) {
2476   const SimplifyQuery Q = SQ.getWithInstruction(&I);
2477 
2478   // Fold (iszero(A & K1) | iszero(A & K2)) ->  (A & (K1 | K2)) != (K1 | K2)
2479   // Fold (!iszero(A & K1) & !iszero(A & K2)) ->  (A & (K1 | K2)) == (K1 | K2)
2480   // if K1 and K2 are a one-bit mask.
2481   if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, &I, IsAnd, IsLogical))
2482     return V;
2483 
2484   ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
2485   Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
2486   Value *LHS1 = LHS->getOperand(1), *RHS1 = RHS->getOperand(1);
2487   const APInt *LHSC = nullptr, *RHSC = nullptr;
2488   match(LHS1, m_APInt(LHSC));
2489   match(RHS1, m_APInt(RHSC));
2490 
2491   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
2492   // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
2493   if (predicatesFoldable(PredL, PredR)) {
2494     if (LHS0 == RHS1 && LHS1 == RHS0) {
2495       PredL = ICmpInst::getSwappedPredicate(PredL);
2496       std::swap(LHS0, LHS1);
2497     }
2498     if (LHS0 == RHS0 && LHS1 == RHS1) {
2499       unsigned Code = IsAnd ? getICmpCode(PredL) & getICmpCode(PredR)
2500                             : getICmpCode(PredL) | getICmpCode(PredR);
2501       bool IsSigned = LHS->isSigned() || RHS->isSigned();
2502       return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
2503     }
2504   }
2505 
2506   // handle (roughly):
2507   // (icmp ne (A & B), C) | (icmp ne (A & D), E)
2508   // (icmp eq (A & B), C) & (icmp eq (A & D), E)
2509   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, IsAnd, IsLogical, Builder))
2510     return V;
2511 
2512   // TODO: One of these directions is fine with logical and/or, the other could
2513   // be supported by inserting freeze.
2514   if (!IsLogical) {
2515     if (Value *V = foldAndOrOfICmpEqZeroAndICmp(LHS, RHS, IsAnd, Builder))
2516       return V;
2517     if (Value *V = foldAndOrOfICmpEqZeroAndICmp(RHS, LHS, IsAnd, Builder))
2518       return V;
2519   }
2520 
2521   // TODO: Verify whether this is safe for logical and/or.
2522   if (!IsLogical) {
2523     if (Value *V = foldAndOrOfICmpsWithConstEq(LHS, RHS, IsAnd, Builder, Q))
2524       return V;
2525     if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, IsAnd, Builder, Q))
2526       return V;
2527   }
2528 
2529   if (Value *V = foldIsPowerOf2OrZero(LHS, RHS, IsAnd, Builder))
2530     return V;
2531   if (Value *V = foldIsPowerOf2OrZero(RHS, LHS, IsAnd, Builder))
2532     return V;
2533 
2534   // TODO: One of these directions is fine with logical and/or, the other could
2535   // be supported by inserting freeze.
2536   if (!IsLogical) {
2537     // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
2538     // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
2539     if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/!IsAnd))
2540       return V;
2541 
2542     // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
2543     // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
2544     if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/!IsAnd))
2545       return V;
2546   }
2547 
2548   // TODO: Add conjugated or fold, check whether it is safe for logical and/or.
2549   if (IsAnd && !IsLogical)
2550     if (Value *V = foldSignedTruncationCheck(LHS, RHS, I, Builder))
2551       return V;
2552 
2553   if (Value *V = foldIsPowerOf2(LHS, RHS, IsAnd, Builder))
2554     return V;
2555 
2556   // TODO: Verify whether this is safe for logical and/or.
2557   if (!IsLogical) {
2558     if (Value *X = foldUnsignedUnderflowCheck(LHS, RHS, IsAnd, Q, Builder))
2559       return X;
2560     if (Value *X = foldUnsignedUnderflowCheck(RHS, LHS, IsAnd, Q, Builder))
2561       return X;
2562   }
2563 
2564   if (Value *X = foldEqOfParts(LHS, RHS, IsAnd))
2565     return X;
2566 
2567   // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
2568   // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
2569   // TODO: Remove this when foldLogOpOfMaskedICmps can handle undefs.
2570   if (!IsLogical && PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
2571       PredL == PredR && match(LHS1, m_ZeroInt()) && match(RHS1, m_ZeroInt()) &&
2572       LHS0->getType() == RHS0->getType()) {
2573     Value *NewOr = Builder.CreateOr(LHS0, RHS0);
2574     return Builder.CreateICmp(PredL, NewOr,
2575                               Constant::getNullValue(NewOr->getType()));
2576   }
2577 
2578   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
2579   if (!LHSC || !RHSC)
2580     return nullptr;
2581 
2582   // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
2583   // (trunc x) != C1 | (and x, CA) != C2 -> (and x, CA|CMAX) != C1|C2
2584   // where CMAX is the all ones value for the truncated type,
2585   // iff the lower bits of C2 and CA are zero.
2586   if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
2587       PredL == PredR && LHS->hasOneUse() && RHS->hasOneUse()) {
2588     Value *V;
2589     const APInt *AndC, *SmallC = nullptr, *BigC = nullptr;
2590 
2591     // (trunc x) == C1 & (and x, CA) == C2
2592     // (and x, CA) == C2 & (trunc x) == C1
2593     if (match(RHS0, m_Trunc(m_Value(V))) &&
2594         match(LHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
2595       SmallC = RHSC;
2596       BigC = LHSC;
2597     } else if (match(LHS0, m_Trunc(m_Value(V))) &&
2598                match(RHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
2599       SmallC = LHSC;
2600       BigC = RHSC;
2601     }
2602 
2603     if (SmallC && BigC) {
2604       unsigned BigBitSize = BigC->getBitWidth();
2605       unsigned SmallBitSize = SmallC->getBitWidth();
2606 
2607       // Check that the low bits are zero.
2608       APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
2609       if ((Low & *AndC).isZero() && (Low & *BigC).isZero()) {
2610         Value *NewAnd = Builder.CreateAnd(V, Low | *AndC);
2611         APInt N = SmallC->zext(BigBitSize) | *BigC;
2612         Value *NewVal = ConstantInt::get(NewAnd->getType(), N);
2613         return Builder.CreateICmp(PredL, NewAnd, NewVal);
2614       }
2615     }
2616   }
2617 
2618   return foldAndOrOfICmpsUsingRanges(LHS, RHS, IsAnd);
2619 }
2620 
2621 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2622 // here. We should standardize that construct where it is needed or choose some
2623 // other way to ensure that commutated variants of patterns are not missed.
2624 Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
2625   if (Value *V = simplifyOrInst(I.getOperand(0), I.getOperand(1),
2626                                 SQ.getWithInstruction(&I)))
2627     return replaceInstUsesWith(I, V);
2628 
2629   if (SimplifyAssociativeOrCommutative(I))
2630     return &I;
2631 
2632   if (Instruction *X = foldVectorBinop(I))
2633     return X;
2634 
2635   if (Instruction *Phi = foldBinopWithPhiOperands(I))
2636     return Phi;
2637 
2638   // See if we can simplify any instructions used by the instruction whose sole
2639   // purpose is to compute bits we don't care about.
2640   if (SimplifyDemandedInstructionBits(I))
2641     return &I;
2642 
2643   // Do this before using distributive laws to catch simple and/or/not patterns.
2644   if (Instruction *Xor = foldOrToXor(I, Builder))
2645     return Xor;
2646 
2647   if (Instruction *X = foldComplexAndOrPatterns(I, Builder))
2648     return X;
2649 
2650   // (A&B)|(A&C) -> A&(B|C) etc
2651   if (Value *V = SimplifyUsingDistributiveLaws(I))
2652     return replaceInstUsesWith(I, V);
2653 
2654   if (Value *V = SimplifyBSwap(I, Builder))
2655     return replaceInstUsesWith(I, V);
2656 
2657   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2658   Type *Ty = I.getType();
2659   if (Ty->isIntOrIntVectorTy(1)) {
2660     if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
2661       if (auto *I =
2662               foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ false))
2663         return I;
2664     }
2665     if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
2666       if (auto *I =
2667               foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ false))
2668         return I;
2669     }
2670   }
2671 
2672   if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2673     return FoldedLogic;
2674 
2675   if (Instruction *BitOp = matchBSwapOrBitReverse(I, /*MatchBSwaps*/ true,
2676                                                   /*MatchBitReversals*/ true))
2677     return BitOp;
2678 
2679   if (Instruction *Funnel = matchFunnelShift(I, *this))
2680     return Funnel;
2681 
2682   if (Instruction *Concat = matchOrConcat(I, Builder))
2683     return replaceInstUsesWith(I, Concat);
2684 
2685   Value *X, *Y;
2686   const APInt *CV;
2687   if (match(&I, m_c_Or(m_OneUse(m_Xor(m_Value(X), m_APInt(CV))), m_Value(Y))) &&
2688       !CV->isAllOnes() && MaskedValueIsZero(Y, *CV, 0, &I)) {
2689     // (X ^ C) | Y -> (X | Y) ^ C iff Y & C == 0
2690     // The check for a 'not' op is for efficiency (if Y is known zero --> ~X).
2691     Value *Or = Builder.CreateOr(X, Y);
2692     return BinaryOperator::CreateXor(Or, ConstantInt::get(Ty, *CV));
2693   }
2694 
2695   // If the operands have no common bits set:
2696   // or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
2697   if (match(&I,
2698             m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
2699       haveNoCommonBitsSet(Op0, Op1, DL)) {
2700     Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
2701     return BinaryOperator::CreateMul(X, IncrementY);
2702   }
2703 
2704   // (A & C) | (B & D)
2705   Value *A, *B, *C, *D;
2706   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2707       match(Op1, m_And(m_Value(B), m_Value(D)))) {
2708 
2709     // (A & C0) | (B & C1)
2710     const APInt *C0, *C1;
2711     if (match(C, m_APInt(C0)) && match(D, m_APInt(C1))) {
2712       Value *X;
2713       if (*C0 == ~*C1) {
2714         // ((X | B) & MaskC) | (B & ~MaskC) -> (X & MaskC) | B
2715         if (match(A, m_c_Or(m_Value(X), m_Specific(B))))
2716           return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C0), B);
2717         // (A & MaskC) | ((X | A) & ~MaskC) -> (X & ~MaskC) | A
2718         if (match(B, m_c_Or(m_Specific(A), m_Value(X))))
2719           return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C1), A);
2720 
2721         // ((X ^ B) & MaskC) | (B & ~MaskC) -> (X & MaskC) ^ B
2722         if (match(A, m_c_Xor(m_Value(X), m_Specific(B))))
2723           return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C0), B);
2724         // (A & MaskC) | ((X ^ A) & ~MaskC) -> (X & ~MaskC) ^ A
2725         if (match(B, m_c_Xor(m_Specific(A), m_Value(X))))
2726           return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C1), A);
2727       }
2728 
2729       if ((*C0 & *C1).isZero()) {
2730         // ((X | B) & C0) | (B & C1) --> (X | B) & (C0 | C1)
2731         // iff (C0 & C1) == 0 and (X & ~C0) == 0
2732         if (match(A, m_c_Or(m_Value(X), m_Specific(B))) &&
2733             MaskedValueIsZero(X, ~*C0, 0, &I)) {
2734           Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2735           return BinaryOperator::CreateAnd(A, C01);
2736         }
2737         // (A & C0) | ((X | A) & C1) --> (X | A) & (C0 | C1)
2738         // iff (C0 & C1) == 0 and (X & ~C1) == 0
2739         if (match(B, m_c_Or(m_Value(X), m_Specific(A))) &&
2740             MaskedValueIsZero(X, ~*C1, 0, &I)) {
2741           Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2742           return BinaryOperator::CreateAnd(B, C01);
2743         }
2744         // ((X | C2) & C0) | ((X | C3) & C1) --> (X | C2 | C3) & (C0 | C1)
2745         // iff (C0 & C1) == 0 and (C2 & ~C0) == 0 and (C3 & ~C1) == 0.
2746         const APInt *C2, *C3;
2747         if (match(A, m_Or(m_Value(X), m_APInt(C2))) &&
2748             match(B, m_Or(m_Specific(X), m_APInt(C3))) &&
2749             (*C2 & ~*C0).isZero() && (*C3 & ~*C1).isZero()) {
2750           Value *Or = Builder.CreateOr(X, *C2 | *C3, "bitfield");
2751           Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2752           return BinaryOperator::CreateAnd(Or, C01);
2753         }
2754       }
2755     }
2756 
2757     // Don't try to form a select if it's unlikely that we'll get rid of at
2758     // least one of the operands. A select is generally more expensive than the
2759     // 'or' that it is replacing.
2760     if (Op0->hasOneUse() || Op1->hasOneUse()) {
2761       // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2762       if (Value *V = matchSelectFromAndOr(A, C, B, D))
2763         return replaceInstUsesWith(I, V);
2764       if (Value *V = matchSelectFromAndOr(A, C, D, B))
2765         return replaceInstUsesWith(I, V);
2766       if (Value *V = matchSelectFromAndOr(C, A, B, D))
2767         return replaceInstUsesWith(I, V);
2768       if (Value *V = matchSelectFromAndOr(C, A, D, B))
2769         return replaceInstUsesWith(I, V);
2770       if (Value *V = matchSelectFromAndOr(B, D, A, C))
2771         return replaceInstUsesWith(I, V);
2772       if (Value *V = matchSelectFromAndOr(B, D, C, A))
2773         return replaceInstUsesWith(I, V);
2774       if (Value *V = matchSelectFromAndOr(D, B, A, C))
2775         return replaceInstUsesWith(I, V);
2776       if (Value *V = matchSelectFromAndOr(D, B, C, A))
2777         return replaceInstUsesWith(I, V);
2778     }
2779   }
2780 
2781   // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2782   if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2783     if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2784       return BinaryOperator::CreateOr(Op0, C);
2785 
2786   // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2787   if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2788     if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2789       return BinaryOperator::CreateOr(Op1, C);
2790 
2791   // ((A & B) ^ C) | B -> C | B
2792   if (match(Op0, m_c_Xor(m_c_And(m_Value(A), m_Specific(Op1)), m_Value(C))))
2793     return BinaryOperator::CreateOr(C, Op1);
2794 
2795   // B | ((A & B) ^ C) -> B | C
2796   if (match(Op1, m_c_Xor(m_c_And(m_Value(A), m_Specific(Op0)), m_Value(C))))
2797     return BinaryOperator::CreateOr(Op0, C);
2798 
2799   // ((B | C) & A) | B -> B | (A & C)
2800   if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2801     return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
2802 
2803   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2804     return DeMorgan;
2805 
2806   // Canonicalize xor to the RHS.
2807   bool SwappedForXor = false;
2808   if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2809     std::swap(Op0, Op1);
2810     SwappedForXor = true;
2811   }
2812 
2813   // A | ( A ^ B) -> A |  B
2814   // A | (~A ^ B) -> A | ~B
2815   // (A & B) | (A ^ B)
2816   // ~A | (A ^ B) -> ~(A & B)
2817   // The swap above should always make Op0 the 'not' for the last case.
2818   if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2819     if (Op0 == A || Op0 == B)
2820       return BinaryOperator::CreateOr(A, B);
2821 
2822     if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2823         match(Op0, m_And(m_Specific(B), m_Specific(A))))
2824       return BinaryOperator::CreateOr(A, B);
2825 
2826     if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
2827         (match(Op0, m_Not(m_Specific(A))) || match(Op0, m_Not(m_Specific(B)))))
2828       return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
2829 
2830     if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2831       Value *Not = Builder.CreateNot(B, B->getName() + ".not");
2832       return BinaryOperator::CreateOr(Not, Op0);
2833     }
2834     if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2835       Value *Not = Builder.CreateNot(A, A->getName() + ".not");
2836       return BinaryOperator::CreateOr(Not, Op0);
2837     }
2838   }
2839 
2840   // A | ~(A | B) -> A | ~B
2841   // A | ~(A ^ B) -> A | ~B
2842   if (match(Op1, m_Not(m_Value(A))))
2843     if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2844       if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2845           Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2846                                B->getOpcode() == Instruction::Xor)) {
2847         Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2848                                                  B->getOperand(0);
2849         Value *Not = Builder.CreateNot(NotOp, NotOp->getName() + ".not");
2850         return BinaryOperator::CreateOr(Not, Op0);
2851       }
2852 
2853   if (SwappedForXor)
2854     std::swap(Op0, Op1);
2855 
2856   {
2857     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2858     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2859     if (LHS && RHS)
2860       if (Value *Res = foldAndOrOfICmps(LHS, RHS, I, /* IsAnd */ false))
2861         return replaceInstUsesWith(I, Res);
2862 
2863     // TODO: Make this recursive; it's a little tricky because an arbitrary
2864     // number of 'or' instructions might have to be created.
2865     Value *X, *Y;
2866     if (LHS && match(Op1, m_OneUse(m_LogicalOr(m_Value(X), m_Value(Y))))) {
2867       bool IsLogical = isa<SelectInst>(Op1);
2868       // LHS | (X || Y) --> (LHS || X) || Y
2869       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2870         if (Value *Res =
2871                 foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ false, IsLogical))
2872           return replaceInstUsesWith(I, IsLogical
2873                                             ? Builder.CreateLogicalOr(Res, Y)
2874                                             : Builder.CreateOr(Res, Y));
2875       // LHS | (X || Y) --> X || (LHS | Y)
2876       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2877         if (Value *Res = foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ false,
2878                                           /* IsLogical */ false))
2879           return replaceInstUsesWith(I, IsLogical
2880                                             ? Builder.CreateLogicalOr(X, Res)
2881                                             : Builder.CreateOr(X, Res));
2882     }
2883     if (RHS && match(Op0, m_OneUse(m_LogicalOr(m_Value(X), m_Value(Y))))) {
2884       bool IsLogical = isa<SelectInst>(Op0);
2885       // (X || Y) | RHS --> (X || RHS) || Y
2886       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2887         if (Value *Res =
2888                 foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ false, IsLogical))
2889           return replaceInstUsesWith(I, IsLogical
2890                                             ? Builder.CreateLogicalOr(Res, Y)
2891                                             : Builder.CreateOr(Res, Y));
2892       // (X || Y) | RHS --> X || (Y | RHS)
2893       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2894         if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ false,
2895                                           /* IsLogical */ false))
2896           return replaceInstUsesWith(I, IsLogical
2897                                             ? Builder.CreateLogicalOr(X, Res)
2898                                             : Builder.CreateOr(X, Res));
2899     }
2900   }
2901 
2902   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2903     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2904       if (Value *Res = foldLogicOfFCmps(LHS, RHS, /*IsAnd*/ false))
2905         return replaceInstUsesWith(I, Res);
2906 
2907   if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2908     return FoldedFCmps;
2909 
2910   if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2911     return CastedOr;
2912 
2913   if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2914     return Sel;
2915 
2916   // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
2917   // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2918   //       with binop identity constant. But creating a select with non-constant
2919   //       arm may not be reversible due to poison semantics. Is that a good
2920   //       canonicalization?
2921   if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2922       A->getType()->isIntOrIntVectorTy(1))
2923     return SelectInst::Create(A, ConstantInt::getAllOnesValue(Ty), Op1);
2924   if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2925       A->getType()->isIntOrIntVectorTy(1))
2926     return SelectInst::Create(A, ConstantInt::getAllOnesValue(Ty), Op0);
2927 
2928   // Note: If we've gotten to the point of visiting the outer OR, then the
2929   // inner one couldn't be simplified.  If it was a constant, then it won't
2930   // be simplified by a later pass either, so we try swapping the inner/outer
2931   // ORs in the hopes that we'll be able to simplify it this way.
2932   // (X|C) | V --> (X|V) | C
2933   ConstantInt *CI;
2934   if (Op0->hasOneUse() && !match(Op1, m_ConstantInt()) &&
2935       match(Op0, m_Or(m_Value(A), m_ConstantInt(CI)))) {
2936     Value *Inner = Builder.CreateOr(A, Op1);
2937     Inner->takeName(Op0);
2938     return BinaryOperator::CreateOr(Inner, CI);
2939   }
2940 
2941   // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2942   // Since this OR statement hasn't been optimized further yet, we hope
2943   // that this transformation will allow the new ORs to be optimized.
2944   {
2945     Value *X = nullptr, *Y = nullptr;
2946     if (Op0->hasOneUse() && Op1->hasOneUse() &&
2947         match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2948         match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2949       Value *orTrue = Builder.CreateOr(A, C);
2950       Value *orFalse = Builder.CreateOr(B, D);
2951       return SelectInst::Create(X, orTrue, orFalse);
2952     }
2953   }
2954 
2955   // or(ashr(subNSW(Y, X), ScalarSizeInBits(Y) - 1), X)  --> X s> Y ? -1 : X.
2956   {
2957     Value *X, *Y;
2958     if (match(&I, m_c_Or(m_OneUse(m_AShr(
2959                              m_NSWSub(m_Value(Y), m_Value(X)),
2960                              m_SpecificInt(Ty->getScalarSizeInBits() - 1))),
2961                          m_Deferred(X)))) {
2962       Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
2963       Value *AllOnes = ConstantInt::getAllOnesValue(Ty);
2964       return SelectInst::Create(NewICmpInst, AllOnes, X);
2965     }
2966   }
2967 
2968   if (Instruction *V =
2969           canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I))
2970     return V;
2971 
2972   CmpInst::Predicate Pred;
2973   Value *Mul, *Ov, *MulIsNotZero, *UMulWithOv;
2974   // Check if the OR weakens the overflow condition for umul.with.overflow by
2975   // treating any non-zero result as overflow. In that case, we overflow if both
2976   // umul.with.overflow operands are != 0, as in that case the result can only
2977   // be 0, iff the multiplication overflows.
2978   if (match(&I,
2979             m_c_Or(m_CombineAnd(m_ExtractValue<1>(m_Value(UMulWithOv)),
2980                                 m_Value(Ov)),
2981                    m_CombineAnd(m_ICmp(Pred,
2982                                        m_CombineAnd(m_ExtractValue<0>(
2983                                                         m_Deferred(UMulWithOv)),
2984                                                     m_Value(Mul)),
2985                                        m_ZeroInt()),
2986                                 m_Value(MulIsNotZero)))) &&
2987       (Ov->hasOneUse() || (MulIsNotZero->hasOneUse() && Mul->hasOneUse())) &&
2988       Pred == CmpInst::ICMP_NE) {
2989     Value *A, *B;
2990     if (match(UMulWithOv, m_Intrinsic<Intrinsic::umul_with_overflow>(
2991                               m_Value(A), m_Value(B)))) {
2992       Value *NotNullA = Builder.CreateIsNotNull(A);
2993       Value *NotNullB = Builder.CreateIsNotNull(B);
2994       return BinaryOperator::CreateAnd(NotNullA, NotNullB);
2995     }
2996   }
2997 
2998   // (~x) | y  -->  ~(x & (~y))  iff that gets rid of inversions
2999   if (sinkNotIntoOtherHandOfAndOrOr(I))
3000     return &I;
3001 
3002   // Improve "get low bit mask up to and including bit X" pattern:
3003   //   (1 << X) | ((1 << X) + -1)  -->  -1 l>> (bitwidth(x) - 1 - X)
3004   if (match(&I, m_c_Or(m_Add(m_Shl(m_One(), m_Value(X)), m_AllOnes()),
3005                        m_Shl(m_One(), m_Deferred(X)))) &&
3006       match(&I, m_c_Or(m_OneUse(m_Value()), m_Value()))) {
3007     Value *Sub = Builder.CreateSub(
3008         ConstantInt::get(Ty, Ty->getScalarSizeInBits() - 1), X);
3009     return BinaryOperator::CreateLShr(Constant::getAllOnesValue(Ty), Sub);
3010   }
3011 
3012   // An or recurrence w/loop invariant step is equivelent to (or start, step)
3013   PHINode *PN = nullptr;
3014   Value *Start = nullptr, *Step = nullptr;
3015   if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
3016     return replaceInstUsesWith(I, Builder.CreateOr(Start, Step));
3017 
3018   // (A & B) | (C | D) or (C | D) | (A & B)
3019   // Can be combined if C or D is of type (A/B & X)
3020   if (match(&I, m_c_Or(m_OneUse(m_And(m_Value(A), m_Value(B))),
3021                        m_OneUse(m_Or(m_Value(C), m_Value(D)))))) {
3022     // (A & B) | (C | ?) -> C | (? | (A & B))
3023     // (A & B) | (C | ?) -> C | (? | (A & B))
3024     // (A & B) | (C | ?) -> C | (? | (A & B))
3025     // (A & B) | (C | ?) -> C | (? | (A & B))
3026     // (C | ?) | (A & B) -> C | (? | (A & B))
3027     // (C | ?) | (A & B) -> C | (? | (A & B))
3028     // (C | ?) | (A & B) -> C | (? | (A & B))
3029     // (C | ?) | (A & B) -> C | (? | (A & B))
3030     if (match(D, m_OneUse(m_c_And(m_Specific(A), m_Value()))) ||
3031         match(D, m_OneUse(m_c_And(m_Specific(B), m_Value()))))
3032       return BinaryOperator::CreateOr(
3033           C, Builder.CreateOr(D, Builder.CreateAnd(A, B)));
3034     // (A & B) | (? | D) -> (? | (A & B)) | D
3035     // (A & B) | (? | D) -> (? | (A & B)) | D
3036     // (A & B) | (? | D) -> (? | (A & B)) | D
3037     // (A & B) | (? | D) -> (? | (A & B)) | D
3038     // (? | D) | (A & B) -> (? | (A & B)) | D
3039     // (? | D) | (A & B) -> (? | (A & B)) | D
3040     // (? | D) | (A & B) -> (? | (A & B)) | D
3041     // (? | D) | (A & B) -> (? | (A & B)) | D
3042     if (match(C, m_OneUse(m_c_And(m_Specific(A), m_Value()))) ||
3043         match(C, m_OneUse(m_c_And(m_Specific(B), m_Value()))))
3044       return BinaryOperator::CreateOr(
3045           Builder.CreateOr(C, Builder.CreateAnd(A, B)), D);
3046   }
3047 
3048   return nullptr;
3049 }
3050 
3051 /// A ^ B can be specified using other logic ops in a variety of patterns. We
3052 /// can fold these early and efficiently by morphing an existing instruction.
3053 static Instruction *foldXorToXor(BinaryOperator &I,
3054                                  InstCombiner::BuilderTy &Builder) {
3055   assert(I.getOpcode() == Instruction::Xor);
3056   Value *Op0 = I.getOperand(0);
3057   Value *Op1 = I.getOperand(1);
3058   Value *A, *B;
3059 
3060   // There are 4 commuted variants for each of the basic patterns.
3061 
3062   // (A & B) ^ (A | B) -> A ^ B
3063   // (A & B) ^ (B | A) -> A ^ B
3064   // (A | B) ^ (A & B) -> A ^ B
3065   // (A | B) ^ (B & A) -> A ^ B
3066   if (match(&I, m_c_Xor(m_And(m_Value(A), m_Value(B)),
3067                         m_c_Or(m_Deferred(A), m_Deferred(B)))))
3068     return BinaryOperator::CreateXor(A, B);
3069 
3070   // (A | ~B) ^ (~A | B) -> A ^ B
3071   // (~B | A) ^ (~A | B) -> A ^ B
3072   // (~A | B) ^ (A | ~B) -> A ^ B
3073   // (B | ~A) ^ (A | ~B) -> A ^ B
3074   if (match(&I, m_Xor(m_c_Or(m_Value(A), m_Not(m_Value(B))),
3075                       m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B)))))
3076     return BinaryOperator::CreateXor(A, B);
3077 
3078   // (A & ~B) ^ (~A & B) -> A ^ B
3079   // (~B & A) ^ (~A & B) -> A ^ B
3080   // (~A & B) ^ (A & ~B) -> A ^ B
3081   // (B & ~A) ^ (A & ~B) -> A ^ B
3082   if (match(&I, m_Xor(m_c_And(m_Value(A), m_Not(m_Value(B))),
3083                       m_c_And(m_Not(m_Deferred(A)), m_Deferred(B)))))
3084     return BinaryOperator::CreateXor(A, B);
3085 
3086   // For the remaining cases we need to get rid of one of the operands.
3087   if (!Op0->hasOneUse() && !Op1->hasOneUse())
3088     return nullptr;
3089 
3090   // (A | B) ^ ~(A & B) -> ~(A ^ B)
3091   // (A | B) ^ ~(B & A) -> ~(A ^ B)
3092   // (A & B) ^ ~(A | B) -> ~(A ^ B)
3093   // (A & B) ^ ~(B | A) -> ~(A ^ B)
3094   // Complexity sorting ensures the not will be on the right side.
3095   if ((match(Op0, m_Or(m_Value(A), m_Value(B))) &&
3096        match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) ||
3097       (match(Op0, m_And(m_Value(A), m_Value(B))) &&
3098        match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))))
3099     return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
3100 
3101   return nullptr;
3102 }
3103 
3104 Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
3105                                         BinaryOperator &I) {
3106   assert(I.getOpcode() == Instruction::Xor && I.getOperand(0) == LHS &&
3107          I.getOperand(1) == RHS && "Should be 'xor' with these operands");
3108 
3109   ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
3110   Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
3111   Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
3112 
3113   if (predicatesFoldable(PredL, PredR)) {
3114     if (LHS0 == RHS1 && LHS1 == RHS0) {
3115       std::swap(LHS0, LHS1);
3116       PredL = ICmpInst::getSwappedPredicate(PredL);
3117     }
3118     if (LHS0 == RHS0 && LHS1 == RHS1) {
3119       // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
3120       unsigned Code = getICmpCode(PredL) ^ getICmpCode(PredR);
3121       bool IsSigned = LHS->isSigned() || RHS->isSigned();
3122       return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
3123     }
3124   }
3125 
3126   // TODO: This can be generalized to compares of non-signbits using
3127   // decomposeBitTestICmp(). It could be enhanced more by using (something like)
3128   // foldLogOpOfMaskedICmps().
3129   if ((LHS->hasOneUse() || RHS->hasOneUse()) &&
3130       LHS0->getType() == RHS0->getType() &&
3131       LHS0->getType()->isIntOrIntVectorTy()) {
3132     // (X > -1) ^ (Y > -1) --> (X ^ Y) < 0
3133     // (X <  0) ^ (Y <  0) --> (X ^ Y) < 0
3134     if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) &&
3135          PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes())) ||
3136         (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) &&
3137          PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero())))
3138       return Builder.CreateIsNeg(Builder.CreateXor(LHS0, RHS0));
3139 
3140     // (X > -1) ^ (Y <  0) --> (X ^ Y) > -1
3141     // (X <  0) ^ (Y > -1) --> (X ^ Y) > -1
3142     if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) &&
3143          PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero())) ||
3144         (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) &&
3145          PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes())))
3146       return Builder.CreateIsNotNeg(Builder.CreateXor(LHS0, RHS0));
3147 
3148   }
3149 
3150   // Instead of trying to imitate the folds for and/or, decompose this 'xor'
3151   // into those logic ops. That is, try to turn this into an and-of-icmps
3152   // because we have many folds for that pattern.
3153   //
3154   // This is based on a truth table definition of xor:
3155   // X ^ Y --> (X | Y) & !(X & Y)
3156   if (Value *OrICmp = simplifyBinOp(Instruction::Or, LHS, RHS, SQ)) {
3157     // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y).
3158     // TODO: If OrICmp is false, the whole thing is false (InstSimplify?).
3159     if (Value *AndICmp = simplifyBinOp(Instruction::And, LHS, RHS, SQ)) {
3160       // TODO: Independently handle cases where the 'and' side is a constant.
3161       ICmpInst *X = nullptr, *Y = nullptr;
3162       if (OrICmp == LHS && AndICmp == RHS) {
3163         // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS  --> X & !Y
3164         X = LHS;
3165         Y = RHS;
3166       }
3167       if (OrICmp == RHS && AndICmp == LHS) {
3168         // !(LHS & RHS) & (LHS | RHS) --> !LHS & RHS  --> !Y & X
3169         X = RHS;
3170         Y = LHS;
3171       }
3172       if (X && Y && (Y->hasOneUse() || canFreelyInvertAllUsersOf(Y, &I))) {
3173         // Invert the predicate of 'Y', thus inverting its output.
3174         Y->setPredicate(Y->getInversePredicate());
3175         // So, are there other uses of Y?
3176         if (!Y->hasOneUse()) {
3177           // We need to adapt other uses of Y though. Get a value that matches
3178           // the original value of Y before inversion. While this increases
3179           // immediate instruction count, we have just ensured that all the
3180           // users are freely-invertible, so that 'not' *will* get folded away.
3181           BuilderTy::InsertPointGuard Guard(Builder);
3182           // Set insertion point to right after the Y.
3183           Builder.SetInsertPoint(Y->getParent(), ++(Y->getIterator()));
3184           Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3185           // Replace all uses of Y (excluding the one in NotY!) with NotY.
3186           Worklist.pushUsersToWorkList(*Y);
3187           Y->replaceUsesWithIf(NotY,
3188                                [NotY](Use &U) { return U.getUser() != NotY; });
3189         }
3190         // All done.
3191         return Builder.CreateAnd(LHS, RHS);
3192       }
3193     }
3194   }
3195 
3196   return nullptr;
3197 }
3198 
3199 /// If we have a masked merge, in the canonical form of:
3200 /// (assuming that A only has one use.)
3201 ///   |        A  |  |B|
3202 ///   ((x ^ y) & M) ^ y
3203 ///    |  D  |
3204 /// * If M is inverted:
3205 ///      |  D  |
3206 ///     ((x ^ y) & ~M) ^ y
3207 ///   We can canonicalize by swapping the final xor operand
3208 ///   to eliminate the 'not' of the mask.
3209 ///     ((x ^ y) & M) ^ x
3210 /// * If M is a constant, and D has one use, we transform to 'and' / 'or' ops
3211 ///   because that shortens the dependency chain and improves analysis:
3212 ///     (x & M) | (y & ~M)
3213 static Instruction *visitMaskedMerge(BinaryOperator &I,
3214                                      InstCombiner::BuilderTy &Builder) {
3215   Value *B, *X, *D;
3216   Value *M;
3217   if (!match(&I, m_c_Xor(m_Value(B),
3218                          m_OneUse(m_c_And(
3219                              m_CombineAnd(m_c_Xor(m_Deferred(B), m_Value(X)),
3220                                           m_Value(D)),
3221                              m_Value(M))))))
3222     return nullptr;
3223 
3224   Value *NotM;
3225   if (match(M, m_Not(m_Value(NotM)))) {
3226     // De-invert the mask and swap the value in B part.
3227     Value *NewA = Builder.CreateAnd(D, NotM);
3228     return BinaryOperator::CreateXor(NewA, X);
3229   }
3230 
3231   Constant *C;
3232   if (D->hasOneUse() && match(M, m_Constant(C))) {
3233     // Propagating undef is unsafe. Clamp undef elements to -1.
3234     Type *EltTy = C->getType()->getScalarType();
3235     C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
3236     // Unfold.
3237     Value *LHS = Builder.CreateAnd(X, C);
3238     Value *NotC = Builder.CreateNot(C);
3239     Value *RHS = Builder.CreateAnd(B, NotC);
3240     return BinaryOperator::CreateOr(LHS, RHS);
3241   }
3242 
3243   return nullptr;
3244 }
3245 
3246 // Transform
3247 //   ~(x ^ y)
3248 // into:
3249 //   (~x) ^ y
3250 // or into
3251 //   x ^ (~y)
3252 static Instruction *sinkNotIntoXor(BinaryOperator &I,
3253                                    InstCombiner::BuilderTy &Builder) {
3254   Value *X, *Y;
3255   // FIXME: one-use check is not needed in general, but currently we are unable
3256   // to fold 'not' into 'icmp', if that 'icmp' has multiple uses. (D35182)
3257   if (!match(&I, m_Not(m_OneUse(m_Xor(m_Value(X), m_Value(Y))))))
3258     return nullptr;
3259 
3260   // We only want to do the transform if it is free to do.
3261   if (InstCombiner::isFreeToInvert(X, X->hasOneUse())) {
3262     // Ok, good.
3263   } else if (InstCombiner::isFreeToInvert(Y, Y->hasOneUse())) {
3264     std::swap(X, Y);
3265   } else
3266     return nullptr;
3267 
3268   Value *NotX = Builder.CreateNot(X, X->getName() + ".not");
3269   return BinaryOperator::CreateXor(NotX, Y, I.getName() + ".demorgan");
3270 }
3271 
3272 /// Canonicalize a shifty way to code absolute value to the more common pattern
3273 /// that uses negation and select.
3274 static Instruction *canonicalizeAbs(BinaryOperator &Xor,
3275                                     InstCombiner::BuilderTy &Builder) {
3276   assert(Xor.getOpcode() == Instruction::Xor && "Expected an xor instruction.");
3277 
3278   // There are 4 potential commuted variants. Move the 'ashr' candidate to Op1.
3279   // We're relying on the fact that we only do this transform when the shift has
3280   // exactly 2 uses and the add has exactly 1 use (otherwise, we might increase
3281   // instructions).
3282   Value *Op0 = Xor.getOperand(0), *Op1 = Xor.getOperand(1);
3283   if (Op0->hasNUses(2))
3284     std::swap(Op0, Op1);
3285 
3286   Type *Ty = Xor.getType();
3287   Value *A;
3288   const APInt *ShAmt;
3289   if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
3290       Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
3291       match(Op0, m_OneUse(m_c_Add(m_Specific(A), m_Specific(Op1))))) {
3292     // Op1 = ashr i32 A, 31   ; smear the sign bit
3293     // xor (add A, Op1), Op1  ; add -1 and flip bits if negative
3294     // --> (A < 0) ? -A : A
3295     Value *IsNeg = Builder.CreateIsNeg(A);
3296     // Copy the nuw/nsw flags from the add to the negate.
3297     auto *Add = cast<BinaryOperator>(Op0);
3298     Value *NegA = Builder.CreateNeg(A, "", Add->hasNoUnsignedWrap(),
3299                                    Add->hasNoSignedWrap());
3300     return SelectInst::Create(IsNeg, NegA, A);
3301   }
3302   return nullptr;
3303 }
3304 
3305 // Transform
3306 //   z = (~x) &/| y
3307 // into:
3308 //   z = ~(x |/& (~y))
3309 // iff y is free to invert and all uses of z can be freely updated.
3310 bool InstCombinerImpl::sinkNotIntoOtherHandOfAndOrOr(BinaryOperator &I) {
3311   Instruction::BinaryOps NewOpc;
3312   switch (I.getOpcode()) {
3313   case Instruction::And:
3314     NewOpc = Instruction::Or;
3315     break;
3316   case Instruction::Or:
3317     NewOpc = Instruction::And;
3318     break;
3319   default:
3320     return false;
3321   };
3322 
3323   Value *X, *Y;
3324   if (!match(&I, m_c_BinOp(m_Not(m_Value(X)), m_Value(Y))))
3325     return false;
3326 
3327   // Will we be able to fold the `not` into Y eventually?
3328   if (!InstCombiner::isFreeToInvert(Y, Y->hasOneUse()))
3329     return false;
3330 
3331   // And can our users be adapted?
3332   if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
3333     return false;
3334 
3335   Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3336   Value *NewBinOp =
3337       BinaryOperator::Create(NewOpc, X, NotY, I.getName() + ".not");
3338   Builder.Insert(NewBinOp);
3339   replaceInstUsesWith(I, NewBinOp);
3340   // We can not just create an outer `not`, it will most likely be immediately
3341   // folded back, reconstructing our initial pattern, and causing an
3342   // infinite combine loop, so immediately manually fold it away.
3343   freelyInvertAllUsersOf(NewBinOp);
3344   return true;
3345 }
3346 
3347 Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
3348   Value *NotOp;
3349   if (!match(&I, m_Not(m_Value(NotOp))))
3350     return nullptr;
3351 
3352   // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand.
3353   // We must eliminate the and/or (one-use) for these transforms to not increase
3354   // the instruction count.
3355   //
3356   // ~(~X & Y) --> (X | ~Y)
3357   // ~(Y & ~X) --> (X | ~Y)
3358   //
3359   // Note: The logical matches do not check for the commuted patterns because
3360   //       those are handled via SimplifySelectsFeedingBinaryOp().
3361   Type *Ty = I.getType();
3362   Value *X, *Y;
3363   if (match(NotOp, m_OneUse(m_c_And(m_Not(m_Value(X)), m_Value(Y))))) {
3364     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3365     return BinaryOperator::CreateOr(X, NotY);
3366   }
3367   if (match(NotOp, m_OneUse(m_LogicalAnd(m_Not(m_Value(X)), m_Value(Y))))) {
3368     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3369     return SelectInst::Create(X, ConstantInt::getTrue(Ty), NotY);
3370   }
3371 
3372   // ~(~X | Y) --> (X & ~Y)
3373   // ~(Y | ~X) --> (X & ~Y)
3374   if (match(NotOp, m_OneUse(m_c_Or(m_Not(m_Value(X)), m_Value(Y))))) {
3375     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3376     return BinaryOperator::CreateAnd(X, NotY);
3377   }
3378   if (match(NotOp, m_OneUse(m_LogicalOr(m_Not(m_Value(X)), m_Value(Y))))) {
3379     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3380     return SelectInst::Create(X, NotY, ConstantInt::getFalse(Ty));
3381   }
3382 
3383   // Is this a 'not' (~) fed by a binary operator?
3384   BinaryOperator *NotVal;
3385   if (match(NotOp, m_BinOp(NotVal))) {
3386     if (NotVal->getOpcode() == Instruction::And ||
3387         NotVal->getOpcode() == Instruction::Or) {
3388       // Apply DeMorgan's Law when inverts are free:
3389       // ~(X & Y) --> (~X | ~Y)
3390       // ~(X | Y) --> (~X & ~Y)
3391       if (isFreeToInvert(NotVal->getOperand(0),
3392                          NotVal->getOperand(0)->hasOneUse()) &&
3393           isFreeToInvert(NotVal->getOperand(1),
3394                          NotVal->getOperand(1)->hasOneUse())) {
3395         Value *NotX = Builder.CreateNot(NotVal->getOperand(0), "notlhs");
3396         Value *NotY = Builder.CreateNot(NotVal->getOperand(1), "notrhs");
3397         if (NotVal->getOpcode() == Instruction::And)
3398           return BinaryOperator::CreateOr(NotX, NotY);
3399         return BinaryOperator::CreateAnd(NotX, NotY);
3400       }
3401     }
3402 
3403     // ~((-X) | Y) --> (X - 1) & (~Y)
3404     if (match(NotVal,
3405               m_OneUse(m_c_Or(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))) {
3406       Value *DecX = Builder.CreateAdd(X, ConstantInt::getAllOnesValue(Ty));
3407       Value *NotY = Builder.CreateNot(Y);
3408       return BinaryOperator::CreateAnd(DecX, NotY);
3409     }
3410 
3411     // ~(~X >>s Y) --> (X >>s Y)
3412     if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y))))
3413       return BinaryOperator::CreateAShr(X, Y);
3414 
3415     // If we are inverting a right-shifted constant, we may be able to eliminate
3416     // the 'not' by inverting the constant and using the opposite shift type.
3417     // Canonicalization rules ensure that only a negative constant uses 'ashr',
3418     // but we must check that in case that transform has not fired yet.
3419 
3420     // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
3421     Constant *C;
3422     if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) &&
3423         match(C, m_Negative())) {
3424       // We matched a negative constant, so propagating undef is unsafe.
3425       // Clamp undef elements to -1.
3426       Type *EltTy = Ty->getScalarType();
3427       C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
3428       return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y);
3429     }
3430 
3431     // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
3432     if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) &&
3433         match(C, m_NonNegative())) {
3434       // We matched a non-negative constant, so propagating undef is unsafe.
3435       // Clamp undef elements to 0.
3436       Type *EltTy = Ty->getScalarType();
3437       C = Constant::replaceUndefsWith(C, ConstantInt::getNullValue(EltTy));
3438       return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y);
3439     }
3440 
3441     // ~(X + C) --> ~C - X
3442     if (match(NotVal, m_c_Add(m_Value(X), m_ImmConstant(C))))
3443       return BinaryOperator::CreateSub(ConstantExpr::getNot(C), X);
3444 
3445     // ~(X - Y) --> ~X + Y
3446     // FIXME: is it really beneficial to sink the `not` here?
3447     if (match(NotVal, m_Sub(m_Value(X), m_Value(Y))))
3448       if (isa<Constant>(X) || NotVal->hasOneUse())
3449         return BinaryOperator::CreateAdd(Builder.CreateNot(X), Y);
3450 
3451     // ~(~X + Y) --> X - Y
3452     if (match(NotVal, m_c_Add(m_Not(m_Value(X)), m_Value(Y))))
3453       return BinaryOperator::CreateWithCopiedFlags(Instruction::Sub, X, Y,
3454                                                    NotVal);
3455   }
3456 
3457   // not (cmp A, B) = !cmp A, B
3458   CmpInst::Predicate Pred;
3459   if (match(NotOp, m_OneUse(m_Cmp(Pred, m_Value(), m_Value())))) {
3460     cast<CmpInst>(NotOp)->setPredicate(CmpInst::getInversePredicate(Pred));
3461     return replaceInstUsesWith(I, NotOp);
3462   }
3463 
3464   // Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max:
3465   // ~min(~X, ~Y) --> max(X, Y)
3466   // ~max(~X, Y) --> min(X, ~Y)
3467   auto *II = dyn_cast<IntrinsicInst>(NotOp);
3468   if (II && II->hasOneUse()) {
3469     if (match(NotOp, m_MaxOrMin(m_Value(X), m_Value(Y))) &&
3470         isFreeToInvert(X, X->hasOneUse()) &&
3471         isFreeToInvert(Y, Y->hasOneUse())) {
3472       Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3473       Value *NotX = Builder.CreateNot(X);
3474       Value *NotY = Builder.CreateNot(Y);
3475       Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, NotX, NotY);
3476       return replaceInstUsesWith(I, InvMaxMin);
3477     }
3478     if (match(NotOp, m_c_MaxOrMin(m_Not(m_Value(X)), m_Value(Y)))) {
3479       Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3480       Value *NotY = Builder.CreateNot(Y);
3481       Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotY);
3482       return replaceInstUsesWith(I, InvMaxMin);
3483     }
3484   }
3485 
3486   if (NotOp->hasOneUse()) {
3487     // Pull 'not' into operands of select if both operands are one-use compares
3488     // or one is one-use compare and the other one is a constant.
3489     // Inverting the predicates eliminates the 'not' operation.
3490     // Example:
3491     //   not (select ?, (cmp TPred, ?, ?), (cmp FPred, ?, ?) -->
3492     //     select ?, (cmp InvTPred, ?, ?), (cmp InvFPred, ?, ?)
3493     //   not (select ?, (cmp TPred, ?, ?), true -->
3494     //     select ?, (cmp InvTPred, ?, ?), false
3495     if (auto *Sel = dyn_cast<SelectInst>(NotOp)) {
3496       Value *TV = Sel->getTrueValue();
3497       Value *FV = Sel->getFalseValue();
3498       auto *CmpT = dyn_cast<CmpInst>(TV);
3499       auto *CmpF = dyn_cast<CmpInst>(FV);
3500       bool InvertibleT = (CmpT && CmpT->hasOneUse()) || isa<Constant>(TV);
3501       bool InvertibleF = (CmpF && CmpF->hasOneUse()) || isa<Constant>(FV);
3502       if (InvertibleT && InvertibleF) {
3503         if (CmpT)
3504           CmpT->setPredicate(CmpT->getInversePredicate());
3505         else
3506           Sel->setTrueValue(ConstantExpr::getNot(cast<Constant>(TV)));
3507         if (CmpF)
3508           CmpF->setPredicate(CmpF->getInversePredicate());
3509         else
3510           Sel->setFalseValue(ConstantExpr::getNot(cast<Constant>(FV)));
3511         return replaceInstUsesWith(I, Sel);
3512       }
3513     }
3514   }
3515 
3516   if (Instruction *NewXor = sinkNotIntoXor(I, Builder))
3517     return NewXor;
3518 
3519   return nullptr;
3520 }
3521 
3522 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
3523 // here. We should standardize that construct where it is needed or choose some
3524 // other way to ensure that commutated variants of patterns are not missed.
3525 Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
3526   if (Value *V = simplifyXorInst(I.getOperand(0), I.getOperand(1),
3527                                  SQ.getWithInstruction(&I)))
3528     return replaceInstUsesWith(I, V);
3529 
3530   if (SimplifyAssociativeOrCommutative(I))
3531     return &I;
3532 
3533   if (Instruction *X = foldVectorBinop(I))
3534     return X;
3535 
3536   if (Instruction *Phi = foldBinopWithPhiOperands(I))
3537     return Phi;
3538 
3539   if (Instruction *NewXor = foldXorToXor(I, Builder))
3540     return NewXor;
3541 
3542   // (A&B)^(A&C) -> A&(B^C) etc
3543   if (Value *V = SimplifyUsingDistributiveLaws(I))
3544     return replaceInstUsesWith(I, V);
3545 
3546   // See if we can simplify any instructions used by the instruction whose sole
3547   // purpose is to compute bits we don't care about.
3548   if (SimplifyDemandedInstructionBits(I))
3549     return &I;
3550 
3551   if (Value *V = SimplifyBSwap(I, Builder))
3552     return replaceInstUsesWith(I, V);
3553 
3554   if (Instruction *R = foldNot(I))
3555     return R;
3556 
3557   // Fold (X & M) ^ (Y & ~M) -> (X & M) | (Y & ~M)
3558   // This it a special case in haveNoCommonBitsSet, but the computeKnownBits
3559   // calls in there are unnecessary as SimplifyDemandedInstructionBits should
3560   // have already taken care of those cases.
3561   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3562   Value *M;
3563   if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()),
3564                         m_c_And(m_Deferred(M), m_Value()))))
3565     return BinaryOperator::CreateOr(Op0, Op1);
3566 
3567   if (Instruction *Xor = visitMaskedMerge(I, Builder))
3568     return Xor;
3569 
3570   Value *X, *Y;
3571   Constant *C1;
3572   if (match(Op1, m_Constant(C1))) {
3573     Constant *C2;
3574 
3575     if (match(Op0, m_OneUse(m_Or(m_Value(X), m_ImmConstant(C2)))) &&
3576         match(C1, m_ImmConstant())) {
3577       // (X | C2) ^ C1 --> (X & ~C2) ^ (C1^C2)
3578       C2 = Constant::replaceUndefsWith(
3579           C2, Constant::getAllOnesValue(C2->getType()->getScalarType()));
3580       Value *And = Builder.CreateAnd(
3581           X, Constant::mergeUndefsWith(ConstantExpr::getNot(C2), C1));
3582       return BinaryOperator::CreateXor(
3583           And, Constant::mergeUndefsWith(ConstantExpr::getXor(C1, C2), C1));
3584     }
3585 
3586     // Use DeMorgan and reassociation to eliminate a 'not' op.
3587     if (match(Op0, m_OneUse(m_Or(m_Not(m_Value(X)), m_Constant(C2))))) {
3588       // (~X | C2) ^ C1 --> ((X & ~C2) ^ -1) ^ C1 --> (X & ~C2) ^ ~C1
3589       Value *And = Builder.CreateAnd(X, ConstantExpr::getNot(C2));
3590       return BinaryOperator::CreateXor(And, ConstantExpr::getNot(C1));
3591     }
3592     if (match(Op0, m_OneUse(m_And(m_Not(m_Value(X)), m_Constant(C2))))) {
3593       // (~X & C2) ^ C1 --> ((X | ~C2) ^ -1) ^ C1 --> (X | ~C2) ^ ~C1
3594       Value *Or = Builder.CreateOr(X, ConstantExpr::getNot(C2));
3595       return BinaryOperator::CreateXor(Or, ConstantExpr::getNot(C1));
3596     }
3597 
3598     // Convert xor ([trunc] (ashr X, BW-1)), C =>
3599     //   select(X >s -1, C, ~C)
3600     // The ashr creates "AllZeroOrAllOne's", which then optionally inverses the
3601     // constant depending on whether this input is less than 0.
3602     const APInt *CA;
3603     if (match(Op0, m_OneUse(m_TruncOrSelf(
3604                        m_AShr(m_Value(X), m_APIntAllowUndef(CA))))) &&
3605         *CA == X->getType()->getScalarSizeInBits() - 1 &&
3606         !match(C1, m_AllOnes())) {
3607       assert(!C1->isZeroValue() && "Unexpected xor with 0");
3608       Value *IsNotNeg = Builder.CreateIsNotNeg(X);
3609       return SelectInst::Create(IsNotNeg, Op1, Builder.CreateNot(Op1));
3610     }
3611   }
3612 
3613   Type *Ty = I.getType();
3614   {
3615     const APInt *RHSC;
3616     if (match(Op1, m_APInt(RHSC))) {
3617       Value *X;
3618       const APInt *C;
3619       // (C - X) ^ signmaskC --> (C + signmaskC) - X
3620       if (RHSC->isSignMask() && match(Op0, m_Sub(m_APInt(C), m_Value(X))))
3621         return BinaryOperator::CreateSub(ConstantInt::get(Ty, *C + *RHSC), X);
3622 
3623       // (X + C) ^ signmaskC --> X + (C + signmaskC)
3624       if (RHSC->isSignMask() && match(Op0, m_Add(m_Value(X), m_APInt(C))))
3625         return BinaryOperator::CreateAdd(X, ConstantInt::get(Ty, *C + *RHSC));
3626 
3627       // (X | C) ^ RHSC --> X ^ (C ^ RHSC) iff X & C == 0
3628       if (match(Op0, m_Or(m_Value(X), m_APInt(C))) &&
3629           MaskedValueIsZero(X, *C, 0, &I))
3630         return BinaryOperator::CreateXor(X, ConstantInt::get(Ty, *C ^ *RHSC));
3631 
3632       // If RHSC is inverting the remaining bits of shifted X,
3633       // canonicalize to a 'not' before the shift to help SCEV and codegen:
3634       // (X << C) ^ RHSC --> ~X << C
3635       if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_APInt(C)))) &&
3636           *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).shl(*C)) {
3637         Value *NotX = Builder.CreateNot(X);
3638         return BinaryOperator::CreateShl(NotX, ConstantInt::get(Ty, *C));
3639       }
3640       // (X >>u C) ^ RHSC --> ~X >>u C
3641       if (match(Op0, m_OneUse(m_LShr(m_Value(X), m_APInt(C)))) &&
3642           *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).lshr(*C)) {
3643         Value *NotX = Builder.CreateNot(X);
3644         return BinaryOperator::CreateLShr(NotX, ConstantInt::get(Ty, *C));
3645       }
3646       // TODO: We could handle 'ashr' here as well. That would be matching
3647       //       a 'not' op and moving it before the shift. Doing that requires
3648       //       preventing the inverse fold in canShiftBinOpWithConstantRHS().
3649     }
3650   }
3651 
3652   // FIXME: This should not be limited to scalar (pull into APInt match above).
3653   {
3654     Value *X;
3655     ConstantInt *C1, *C2, *C3;
3656     // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
3657     if (match(Op1, m_ConstantInt(C3)) &&
3658         match(Op0, m_LShr(m_Xor(m_Value(X), m_ConstantInt(C1)),
3659                           m_ConstantInt(C2))) &&
3660         Op0->hasOneUse()) {
3661       // fold (C1 >> C2) ^ C3
3662       APInt FoldConst = C1->getValue().lshr(C2->getValue());
3663       FoldConst ^= C3->getValue();
3664       // Prepare the two operands.
3665       auto *Opnd0 = Builder.CreateLShr(X, C2);
3666       Opnd0->takeName(Op0);
3667       return BinaryOperator::CreateXor(Opnd0, ConstantInt::get(Ty, FoldConst));
3668     }
3669   }
3670 
3671   if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
3672     return FoldedLogic;
3673 
3674   // Y ^ (X | Y) --> X & ~Y
3675   // Y ^ (Y | X) --> X & ~Y
3676   if (match(Op1, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op0)))))
3677     return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op0));
3678   // (X | Y) ^ Y --> X & ~Y
3679   // (Y | X) ^ Y --> X & ~Y
3680   if (match(Op0, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op1)))))
3681     return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op1));
3682 
3683   // Y ^ (X & Y) --> ~X & Y
3684   // Y ^ (Y & X) --> ~X & Y
3685   if (match(Op1, m_OneUse(m_c_And(m_Value(X), m_Specific(Op0)))))
3686     return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(X));
3687   // (X & Y) ^ Y --> ~X & Y
3688   // (Y & X) ^ Y --> ~X & Y
3689   // Canonical form is (X & C) ^ C; don't touch that.
3690   // TODO: A 'not' op is better for analysis and codegen, but demanded bits must
3691   //       be fixed to prefer that (otherwise we get infinite looping).
3692   if (!match(Op1, m_Constant()) &&
3693       match(Op0, m_OneUse(m_c_And(m_Value(X), m_Specific(Op1)))))
3694     return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(X));
3695 
3696   Value *A, *B, *C;
3697   // (A ^ B) ^ (A | C) --> (~A & C) ^ B -- There are 4 commuted variants.
3698   if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))),
3699                         m_OneUse(m_c_Or(m_Deferred(A), m_Value(C))))))
3700       return BinaryOperator::CreateXor(
3701           Builder.CreateAnd(Builder.CreateNot(A), C), B);
3702 
3703   // (A ^ B) ^ (B | C) --> (~B & C) ^ A -- There are 4 commuted variants.
3704   if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))),
3705                         m_OneUse(m_c_Or(m_Deferred(B), m_Value(C))))))
3706       return BinaryOperator::CreateXor(
3707           Builder.CreateAnd(Builder.CreateNot(B), C), A);
3708 
3709   // (A & B) ^ (A ^ B) -> (A | B)
3710   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
3711       match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
3712     return BinaryOperator::CreateOr(A, B);
3713   // (A ^ B) ^ (A & B) -> (A | B)
3714   if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
3715       match(Op1, m_c_And(m_Specific(A), m_Specific(B))))
3716     return BinaryOperator::CreateOr(A, B);
3717 
3718   // (A & ~B) ^ ~A -> ~(A & B)
3719   // (~B & A) ^ ~A -> ~(A & B)
3720   if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
3721       match(Op1, m_Not(m_Specific(A))))
3722     return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
3723 
3724   // (~A & B) ^ A --> A | B -- There are 4 commuted variants.
3725   if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(A)), m_Value(B)), m_Deferred(A))))
3726     return BinaryOperator::CreateOr(A, B);
3727 
3728   // (~A | B) ^ A --> ~(A & B)
3729   if (match(Op0, m_OneUse(m_c_Or(m_Not(m_Specific(Op1)), m_Value(B)))))
3730     return BinaryOperator::CreateNot(Builder.CreateAnd(Op1, B));
3731 
3732   // A ^ (~A | B) --> ~(A & B)
3733   if (match(Op1, m_OneUse(m_c_Or(m_Not(m_Specific(Op0)), m_Value(B)))))
3734     return BinaryOperator::CreateNot(Builder.CreateAnd(Op0, B));
3735 
3736   // (A | B) ^ (A | C) --> (B ^ C) & ~A -- There are 4 commuted variants.
3737   // TODO: Loosen one-use restriction if common operand is a constant.
3738   Value *D;
3739   if (match(Op0, m_OneUse(m_Or(m_Value(A), m_Value(B)))) &&
3740       match(Op1, m_OneUse(m_Or(m_Value(C), m_Value(D))))) {
3741     if (B == C || B == D)
3742       std::swap(A, B);
3743     if (A == C)
3744       std::swap(C, D);
3745     if (A == D) {
3746       Value *NotA = Builder.CreateNot(A);
3747       return BinaryOperator::CreateAnd(Builder.CreateXor(B, C), NotA);
3748     }
3749   }
3750 
3751   if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
3752     if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
3753       if (Value *V = foldXorOfICmps(LHS, RHS, I))
3754         return replaceInstUsesWith(I, V);
3755 
3756   if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
3757     return CastedXor;
3758 
3759   if (Instruction *Abs = canonicalizeAbs(I, Builder))
3760     return Abs;
3761 
3762   // Otherwise, if all else failed, try to hoist the xor-by-constant:
3763   //   (X ^ C) ^ Y --> (X ^ Y) ^ C
3764   // Just like we do in other places, we completely avoid the fold
3765   // for constantexprs, at least to avoid endless combine loop.
3766   if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_CombineAnd(m_Value(X),
3767                                                     m_Unless(m_ConstantExpr())),
3768                                        m_ImmConstant(C1))),
3769                         m_Value(Y))))
3770     return BinaryOperator::CreateXor(Builder.CreateXor(X, Y), C1);
3771 
3772   return nullptr;
3773 }
3774