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       Constant *Log2C3 = ConstantInt::get(Ty, C3->countTrailingZeros());
1922       if (match(Op0, m_OneUse(m_LShr(m_Shl(m_ImmConstant(C1), m_Value(X)),
1923                                      m_ImmConstant(C2)))) &&
1924           match(C1, m_Power2())) {
1925         Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
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       }
1937 
1938       if (match(Op0, m_OneUse(m_Shl(m_LShr(m_ImmConstant(C1), m_Value(X)),
1939                                     m_ImmConstant(C2)))) &&
1940           match(C1, m_Power2())) {
1941         Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
1942         Constant *Cmp =
1943             ConstantExpr::getCompare(ICmpInst::ICMP_ULT, Log2C3, C2);
1944         if (Cmp->isZeroValue()) {
1945           // iff C1,C3 is pow2 and Log2(C3) >= C2:
1946           // ((C1 >> X) << C2) & C3 -> X == (cttz(C1)+C2-cttz(C3)) ? C3 : 0
1947           Constant *ShlC = ConstantExpr::getAdd(C2, Log2C1);
1948           Constant *CmpC = ConstantExpr::getSub(ShlC, Log2C3);
1949           Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
1950           return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
1951                                     ConstantInt::getNullValue(Ty));
1952         }
1953       }
1954     }
1955   }
1956 
1957   if (match(&I, m_And(m_OneUse(m_Shl(m_ZExt(m_Value(X)), m_Value(Y))),
1958                       m_SignMask())) &&
1959       match(Y, m_SpecificInt_ICMP(
1960                    ICmpInst::Predicate::ICMP_EQ,
1961                    APInt(Ty->getScalarSizeInBits(),
1962                          Ty->getScalarSizeInBits() -
1963                              X->getType()->getScalarSizeInBits())))) {
1964     auto *SExt = Builder.CreateSExt(X, Ty, X->getName() + ".signext");
1965     auto *SanitizedSignMask = cast<Constant>(Op1);
1966     // We must be careful with the undef elements of the sign bit mask, however:
1967     // the mask elt can be undef iff the shift amount for that lane was undef,
1968     // otherwise we need to sanitize undef masks to zero.
1969     SanitizedSignMask = Constant::replaceUndefsWith(
1970         SanitizedSignMask, ConstantInt::getNullValue(Ty->getScalarType()));
1971     SanitizedSignMask =
1972         Constant::mergeUndefsWith(SanitizedSignMask, cast<Constant>(Y));
1973     return BinaryOperator::CreateAnd(SExt, SanitizedSignMask);
1974   }
1975 
1976   if (Instruction *Z = narrowMaskedBinOp(I))
1977     return Z;
1978 
1979   if (I.getType()->isIntOrIntVectorTy(1)) {
1980     if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
1981       if (auto *I =
1982               foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ true))
1983         return I;
1984     }
1985     if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
1986       if (auto *I =
1987               foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ true))
1988         return I;
1989     }
1990   }
1991 
1992   if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
1993     return FoldedLogic;
1994 
1995   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
1996     return DeMorgan;
1997 
1998   {
1999     Value *A, *B, *C;
2000     // A & (A ^ B) --> A & ~B
2001     if (match(Op1, m_OneUse(m_c_Xor(m_Specific(Op0), m_Value(B)))))
2002       return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(B));
2003     // (A ^ B) & A --> A & ~B
2004     if (match(Op0, m_OneUse(m_c_Xor(m_Specific(Op1), m_Value(B)))))
2005       return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(B));
2006 
2007     // A & ~(A ^ B) --> A & B
2008     if (match(Op1, m_Not(m_c_Xor(m_Specific(Op0), m_Value(B)))))
2009       return BinaryOperator::CreateAnd(Op0, B);
2010     // ~(A ^ B) & A --> A & B
2011     if (match(Op0, m_Not(m_c_Xor(m_Specific(Op1), m_Value(B)))))
2012       return BinaryOperator::CreateAnd(Op1, B);
2013 
2014     // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
2015     if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2016       if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2017         if (Op1->hasOneUse() || isFreeToInvert(C, C->hasOneUse()))
2018           return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C));
2019 
2020     // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
2021     if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2022       if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2023         if (Op0->hasOneUse() || isFreeToInvert(C, C->hasOneUse()))
2024           return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
2025 
2026     // (A | B) & (~A ^ B) -> A & B
2027     // (A | B) & (B ^ ~A) -> A & B
2028     // (B | A) & (~A ^ B) -> A & B
2029     // (B | A) & (B ^ ~A) -> A & B
2030     if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2031         match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
2032       return BinaryOperator::CreateAnd(A, B);
2033 
2034     // (~A ^ B) & (A | B) -> A & B
2035     // (~A ^ B) & (B | A) -> A & B
2036     // (B ^ ~A) & (A | B) -> A & B
2037     // (B ^ ~A) & (B | A) -> A & B
2038     if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2039         match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
2040       return BinaryOperator::CreateAnd(A, B);
2041 
2042     // (~A | B) & (A ^ B) -> ~A & B
2043     // (~A | B) & (B ^ A) -> ~A & B
2044     // (B | ~A) & (A ^ B) -> ~A & B
2045     // (B | ~A) & (B ^ A) -> ~A & B
2046     if (match(Op0, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2047         match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
2048       return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2049 
2050     // (A ^ B) & (~A | B) -> ~A & B
2051     // (B ^ A) & (~A | B) -> ~A & B
2052     // (A ^ B) & (B | ~A) -> ~A & B
2053     // (B ^ A) & (B | ~A) -> ~A & B
2054     if (match(Op1, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2055         match(Op0, m_c_Xor(m_Specific(A), m_Specific(B))))
2056       return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2057   }
2058 
2059   {
2060     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2061     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2062     if (LHS && RHS)
2063       if (Value *Res = foldAndOrOfICmps(LHS, RHS, I, /* IsAnd */ true))
2064         return replaceInstUsesWith(I, Res);
2065 
2066     // TODO: Make this recursive; it's a little tricky because an arbitrary
2067     // number of 'and' instructions might have to be created.
2068     if (LHS && match(Op1, m_OneUse(m_LogicalAnd(m_Value(X), m_Value(Y))))) {
2069       bool IsLogical = isa<SelectInst>(Op1);
2070       // LHS & (X && Y) --> (LHS && X) && Y
2071       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2072         if (Value *Res =
2073                 foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ true, IsLogical))
2074           return replaceInstUsesWith(I, IsLogical
2075                                             ? Builder.CreateLogicalAnd(Res, Y)
2076                                             : Builder.CreateAnd(Res, Y));
2077       // LHS & (X && Y) --> X && (LHS & Y)
2078       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2079         if (Value *Res = foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ true,
2080                                           /* IsLogical */ false))
2081           return replaceInstUsesWith(I, IsLogical
2082                                             ? Builder.CreateLogicalAnd(X, Res)
2083                                             : Builder.CreateAnd(X, Res));
2084     }
2085     if (RHS && match(Op0, m_OneUse(m_LogicalAnd(m_Value(X), m_Value(Y))))) {
2086       bool IsLogical = isa<SelectInst>(Op0);
2087       // (X && Y) & RHS --> (X && RHS) && Y
2088       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2089         if (Value *Res =
2090                 foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ true, IsLogical))
2091           return replaceInstUsesWith(I, IsLogical
2092                                             ? Builder.CreateLogicalAnd(Res, Y)
2093                                             : Builder.CreateAnd(Res, Y));
2094       // (X && Y) & RHS --> X && (Y & RHS)
2095       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2096         if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ true,
2097                                           /* IsLogical */ false))
2098           return replaceInstUsesWith(I, IsLogical
2099                                             ? Builder.CreateLogicalAnd(X, Res)
2100                                             : Builder.CreateAnd(X, Res));
2101     }
2102   }
2103 
2104   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2105     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2106       if (Value *Res = foldLogicOfFCmps(LHS, RHS, /*IsAnd*/ true))
2107         return replaceInstUsesWith(I, Res);
2108 
2109   if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2110     return FoldedFCmps;
2111 
2112   if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
2113     return CastedAnd;
2114 
2115   if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2116     return Sel;
2117 
2118   // and(sext(A), B) / and(B, sext(A)) --> A ? B : 0, where A is i1 or <N x i1>.
2119   // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2120   //       with binop identity constant. But creating a select with non-constant
2121   //       arm may not be reversible due to poison semantics. Is that a good
2122   //       canonicalization?
2123   Value *A;
2124   if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2125       A->getType()->isIntOrIntVectorTy(1))
2126     return SelectInst::Create(A, Op1, Constant::getNullValue(Ty));
2127   if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2128       A->getType()->isIntOrIntVectorTy(1))
2129     return SelectInst::Create(A, Op0, Constant::getNullValue(Ty));
2130 
2131   // (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0
2132   unsigned FullShift = Ty->getScalarSizeInBits() - 1;
2133   if (match(&I, m_c_And(m_OneUse(m_AShr(m_Value(X), m_SpecificInt(FullShift))),
2134                         m_Value(Y)))) {
2135     Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
2136     return SelectInst::Create(IsNeg, Y, ConstantInt::getNullValue(Ty));
2137   }
2138   // If there's a 'not' of the shifted value, swap the select operands:
2139   // ~(iN X s>> (N-1)) & Y --> (X s< 0) ? 0 : Y
2140   if (match(&I, m_c_And(m_OneUse(m_Not(
2141                             m_AShr(m_Value(X), m_SpecificInt(FullShift)))),
2142                         m_Value(Y)))) {
2143     Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
2144     return SelectInst::Create(IsNeg, ConstantInt::getNullValue(Ty), Y);
2145   }
2146 
2147   // (~x) & y  -->  ~(x | (~y))  iff that gets rid of inversions
2148   if (sinkNotIntoOtherHandOfAndOrOr(I))
2149     return &I;
2150 
2151   // An and recurrence w/loop invariant step is equivelent to (and start, step)
2152   PHINode *PN = nullptr;
2153   Value *Start = nullptr, *Step = nullptr;
2154   if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
2155     return replaceInstUsesWith(I, Builder.CreateAnd(Start, Step));
2156 
2157   return nullptr;
2158 }
2159 
2160 Instruction *InstCombinerImpl::matchBSwapOrBitReverse(Instruction &I,
2161                                                       bool MatchBSwaps,
2162                                                       bool MatchBitReversals) {
2163   SmallVector<Instruction *, 4> Insts;
2164   if (!recognizeBSwapOrBitReverseIdiom(&I, MatchBSwaps, MatchBitReversals,
2165                                        Insts))
2166     return nullptr;
2167   Instruction *LastInst = Insts.pop_back_val();
2168   LastInst->removeFromParent();
2169 
2170   for (auto *Inst : Insts)
2171     Worklist.push(Inst);
2172   return LastInst;
2173 }
2174 
2175 /// Match UB-safe variants of the funnel shift intrinsic.
2176 static Instruction *matchFunnelShift(Instruction &Or, InstCombinerImpl &IC) {
2177   // TODO: Can we reduce the code duplication between this and the related
2178   // rotate matching code under visitSelect and visitTrunc?
2179   unsigned Width = Or.getType()->getScalarSizeInBits();
2180 
2181   // First, find an or'd pair of opposite shifts:
2182   // or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1)
2183   BinaryOperator *Or0, *Or1;
2184   if (!match(Or.getOperand(0), m_BinOp(Or0)) ||
2185       !match(Or.getOperand(1), m_BinOp(Or1)))
2186     return nullptr;
2187 
2188   Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
2189   if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) ||
2190       !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) ||
2191       Or0->getOpcode() == Or1->getOpcode())
2192     return nullptr;
2193 
2194   // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)).
2195   if (Or0->getOpcode() == BinaryOperator::LShr) {
2196     std::swap(Or0, Or1);
2197     std::swap(ShVal0, ShVal1);
2198     std::swap(ShAmt0, ShAmt1);
2199   }
2200   assert(Or0->getOpcode() == BinaryOperator::Shl &&
2201          Or1->getOpcode() == BinaryOperator::LShr &&
2202          "Illegal or(shift,shift) pair");
2203 
2204   // Match the shift amount operands for a funnel shift pattern. This always
2205   // matches a subtraction on the R operand.
2206   auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
2207     // Check for constant shift amounts that sum to the bitwidth.
2208     const APInt *LI, *RI;
2209     if (match(L, m_APIntAllowUndef(LI)) && match(R, m_APIntAllowUndef(RI)))
2210       if (LI->ult(Width) && RI->ult(Width) && (*LI + *RI) == Width)
2211         return ConstantInt::get(L->getType(), *LI);
2212 
2213     Constant *LC, *RC;
2214     if (match(L, m_Constant(LC)) && match(R, m_Constant(RC)) &&
2215         match(L, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2216         match(R, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2217         match(ConstantExpr::getAdd(LC, RC), m_SpecificIntAllowUndef(Width)))
2218       return ConstantExpr::mergeUndefsWith(LC, RC);
2219 
2220     // (shl ShVal, X) | (lshr ShVal, (Width - x)) iff X < Width.
2221     // We limit this to X < Width in case the backend re-expands the intrinsic,
2222     // and has to reintroduce a shift modulo operation (InstCombine might remove
2223     // it after this fold). This still doesn't guarantee that the final codegen
2224     // will match this original pattern.
2225     if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L))))) {
2226       KnownBits KnownL = IC.computeKnownBits(L, /*Depth*/ 0, &Or);
2227       return KnownL.getMaxValue().ult(Width) ? L : nullptr;
2228     }
2229 
2230     // For non-constant cases, the following patterns currently only work for
2231     // rotation patterns.
2232     // TODO: Add general funnel-shift compatible patterns.
2233     if (ShVal0 != ShVal1)
2234       return nullptr;
2235 
2236     // For non-constant cases we don't support non-pow2 shift masks.
2237     // TODO: Is it worth matching urem as well?
2238     if (!isPowerOf2_32(Width))
2239       return nullptr;
2240 
2241     // The shift amount may be masked with negation:
2242     // (shl ShVal, (X & (Width - 1))) | (lshr ShVal, ((-X) & (Width - 1)))
2243     Value *X;
2244     unsigned Mask = Width - 1;
2245     if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) &&
2246         match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask))))
2247       return X;
2248 
2249     // Similar to above, but the shift amount may be extended after masking,
2250     // so return the extended value as the parameter for the intrinsic.
2251     if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2252         match(R, m_And(m_Neg(m_ZExt(m_And(m_Specific(X), m_SpecificInt(Mask)))),
2253                        m_SpecificInt(Mask))))
2254       return L;
2255 
2256     if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2257         match(R, m_ZExt(m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask)))))
2258       return L;
2259 
2260     return nullptr;
2261   };
2262 
2263   Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width);
2264   bool IsFshl = true; // Sub on LSHR.
2265   if (!ShAmt) {
2266     ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width);
2267     IsFshl = false; // Sub on SHL.
2268   }
2269   if (!ShAmt)
2270     return nullptr;
2271 
2272   Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2273   Function *F = Intrinsic::getDeclaration(Or.getModule(), IID, Or.getType());
2274   return CallInst::Create(F, {ShVal0, ShVal1, ShAmt});
2275 }
2276 
2277 /// Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns.
2278 static Instruction *matchOrConcat(Instruction &Or,
2279                                   InstCombiner::BuilderTy &Builder) {
2280   assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'");
2281   Value *Op0 = Or.getOperand(0), *Op1 = Or.getOperand(1);
2282   Type *Ty = Or.getType();
2283 
2284   unsigned Width = Ty->getScalarSizeInBits();
2285   if ((Width & 1) != 0)
2286     return nullptr;
2287   unsigned HalfWidth = Width / 2;
2288 
2289   // Canonicalize zext (lower half) to LHS.
2290   if (!isa<ZExtInst>(Op0))
2291     std::swap(Op0, Op1);
2292 
2293   // Find lower/upper half.
2294   Value *LowerSrc, *ShlVal, *UpperSrc;
2295   const APInt *C;
2296   if (!match(Op0, m_OneUse(m_ZExt(m_Value(LowerSrc)))) ||
2297       !match(Op1, m_OneUse(m_Shl(m_Value(ShlVal), m_APInt(C)))) ||
2298       !match(ShlVal, m_OneUse(m_ZExt(m_Value(UpperSrc)))))
2299     return nullptr;
2300   if (*C != HalfWidth || LowerSrc->getType() != UpperSrc->getType() ||
2301       LowerSrc->getType()->getScalarSizeInBits() != HalfWidth)
2302     return nullptr;
2303 
2304   auto ConcatIntrinsicCalls = [&](Intrinsic::ID id, Value *Lo, Value *Hi) {
2305     Value *NewLower = Builder.CreateZExt(Lo, Ty);
2306     Value *NewUpper = Builder.CreateZExt(Hi, Ty);
2307     NewUpper = Builder.CreateShl(NewUpper, HalfWidth);
2308     Value *BinOp = Builder.CreateOr(NewLower, NewUpper);
2309     Function *F = Intrinsic::getDeclaration(Or.getModule(), id, Ty);
2310     return Builder.CreateCall(F, BinOp);
2311   };
2312 
2313   // BSWAP: Push the concat down, swapping the lower/upper sources.
2314   // concat(bswap(x),bswap(y)) -> bswap(concat(x,y))
2315   Value *LowerBSwap, *UpperBSwap;
2316   if (match(LowerSrc, m_BSwap(m_Value(LowerBSwap))) &&
2317       match(UpperSrc, m_BSwap(m_Value(UpperBSwap))))
2318     return ConcatIntrinsicCalls(Intrinsic::bswap, UpperBSwap, LowerBSwap);
2319 
2320   // BITREVERSE: Push the concat down, swapping the lower/upper sources.
2321   // concat(bitreverse(x),bitreverse(y)) -> bitreverse(concat(x,y))
2322   Value *LowerBRev, *UpperBRev;
2323   if (match(LowerSrc, m_BitReverse(m_Value(LowerBRev))) &&
2324       match(UpperSrc, m_BitReverse(m_Value(UpperBRev))))
2325     return ConcatIntrinsicCalls(Intrinsic::bitreverse, UpperBRev, LowerBRev);
2326 
2327   return nullptr;
2328 }
2329 
2330 /// If all elements of two constant vectors are 0/-1 and inverses, return true.
2331 static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
2332   unsigned NumElts = cast<FixedVectorType>(C1->getType())->getNumElements();
2333   for (unsigned i = 0; i != NumElts; ++i) {
2334     Constant *EltC1 = C1->getAggregateElement(i);
2335     Constant *EltC2 = C2->getAggregateElement(i);
2336     if (!EltC1 || !EltC2)
2337       return false;
2338 
2339     // One element must be all ones, and the other must be all zeros.
2340     if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
2341           (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
2342       return false;
2343   }
2344   return true;
2345 }
2346 
2347 /// We have an expression of the form (A & C) | (B & D). If A is a scalar or
2348 /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
2349 /// B, it can be used as the condition operand of a select instruction.
2350 Value *InstCombinerImpl::getSelectCondition(Value *A, Value *B) {
2351   // We may have peeked through bitcasts in the caller.
2352   // Exit immediately if we don't have (vector) integer types.
2353   Type *Ty = A->getType();
2354   if (!Ty->isIntOrIntVectorTy() || !B->getType()->isIntOrIntVectorTy())
2355     return nullptr;
2356 
2357   // If A is the 'not' operand of B and has enough signbits, we have our answer.
2358   if (match(B, m_Not(m_Specific(A)))) {
2359     // If these are scalars or vectors of i1, A can be used directly.
2360     if (Ty->isIntOrIntVectorTy(1))
2361       return A;
2362 
2363     // If we look through a vector bitcast, the caller will bitcast the operands
2364     // to match the condition's number of bits (N x i1).
2365     // To make this poison-safe, disallow bitcast from wide element to narrow
2366     // element. That could allow poison in lanes where it was not present in the
2367     // original code.
2368     A = peekThroughBitcast(A);
2369     if (A->getType()->isIntOrIntVectorTy()) {
2370       unsigned NumSignBits = ComputeNumSignBits(A);
2371       if (NumSignBits == A->getType()->getScalarSizeInBits() &&
2372           NumSignBits <= Ty->getScalarSizeInBits())
2373         return Builder.CreateTrunc(A, CmpInst::makeCmpResultType(A->getType()));
2374     }
2375     return nullptr;
2376   }
2377 
2378   // If both operands are constants, see if the constants are inverse bitmasks.
2379   Constant *AConst, *BConst;
2380   if (match(A, m_Constant(AConst)) && match(B, m_Constant(BConst)))
2381     if (AConst == ConstantExpr::getNot(BConst) &&
2382         ComputeNumSignBits(A) == Ty->getScalarSizeInBits())
2383       return Builder.CreateZExtOrTrunc(A, CmpInst::makeCmpResultType(Ty));
2384 
2385   // Look for more complex patterns. The 'not' op may be hidden behind various
2386   // casts. Look through sexts and bitcasts to find the booleans.
2387   Value *Cond;
2388   Value *NotB;
2389   if (match(A, m_SExt(m_Value(Cond))) &&
2390       Cond->getType()->isIntOrIntVectorTy(1)) {
2391     // A = sext i1 Cond; B = sext (not (i1 Cond))
2392     if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
2393       return Cond;
2394 
2395     // A = sext i1 Cond; B = not ({bitcast} (sext (i1 Cond)))
2396     // TODO: The one-use checks are unnecessary or misplaced. If the caller
2397     //       checked for uses on logic ops/casts, that should be enough to
2398     //       make this transform worthwhile.
2399     if (match(B, m_OneUse(m_Not(m_Value(NotB))))) {
2400       NotB = peekThroughBitcast(NotB, true);
2401       if (match(NotB, m_SExt(m_Specific(Cond))))
2402         return Cond;
2403     }
2404   }
2405 
2406   // All scalar (and most vector) possibilities should be handled now.
2407   // Try more matches that only apply to non-splat constant vectors.
2408   if (!Ty->isVectorTy())
2409     return nullptr;
2410 
2411   // If both operands are xor'd with constants using the same sexted boolean
2412   // operand, see if the constants are inverse bitmasks.
2413   // TODO: Use ConstantExpr::getNot()?
2414   if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AConst)))) &&
2415       match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BConst)))) &&
2416       Cond->getType()->isIntOrIntVectorTy(1) &&
2417       areInverseVectorBitmasks(AConst, BConst)) {
2418     AConst = ConstantExpr::getTrunc(AConst, CmpInst::makeCmpResultType(Ty));
2419     return Builder.CreateXor(Cond, AConst);
2420   }
2421   return nullptr;
2422 }
2423 
2424 /// We have an expression of the form (A & C) | (B & D). Try to simplify this
2425 /// to "A' ? C : D", where A' is a boolean or vector of booleans.
2426 Value *InstCombinerImpl::matchSelectFromAndOr(Value *A, Value *C, Value *B,
2427                                               Value *D) {
2428   // The potential condition of the select may be bitcasted. In that case, look
2429   // through its bitcast and the corresponding bitcast of the 'not' condition.
2430   Type *OrigType = A->getType();
2431   A = peekThroughBitcast(A, true);
2432   B = peekThroughBitcast(B, true);
2433   if (Value *Cond = getSelectCondition(A, B)) {
2434     // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
2435     // If this is a vector, we may need to cast to match the condition's length.
2436     // The bitcasts will either all exist or all not exist. The builder will
2437     // not create unnecessary casts if the types already match.
2438     Type *SelTy = A->getType();
2439     if (auto *VecTy = dyn_cast<VectorType>(Cond->getType())) {
2440       // For a fixed or scalable vector get N from <{vscale x} N x iM>
2441       unsigned Elts = VecTy->getElementCount().getKnownMinValue();
2442       // For a fixed or scalable vector, get the size in bits of N x iM; for a
2443       // scalar this is just M.
2444       unsigned SelEltSize = SelTy->getPrimitiveSizeInBits().getKnownMinSize();
2445       Type *EltTy = Builder.getIntNTy(SelEltSize / Elts);
2446       SelTy = VectorType::get(EltTy, VecTy->getElementCount());
2447     }
2448     Value *BitcastC = Builder.CreateBitCast(C, SelTy);
2449     Value *BitcastD = Builder.CreateBitCast(D, SelTy);
2450     Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
2451     return Builder.CreateBitCast(Select, OrigType);
2452   }
2453 
2454   return nullptr;
2455 }
2456 
2457 // (icmp eq X, 0) | (icmp ult Other, X) -> (icmp ule Other, X-1)
2458 // (icmp ne X, 0) & (icmp uge Other, X) -> (icmp ugt Other, X-1)
2459 Value *foldAndOrOfICmpEqZeroAndICmp(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
2460                                     IRBuilderBase &Builder) {
2461   ICmpInst::Predicate LPred =
2462       IsAnd ? LHS->getInversePredicate() : LHS->getPredicate();
2463   ICmpInst::Predicate RPred =
2464       IsAnd ? RHS->getInversePredicate() : RHS->getPredicate();
2465   Value *LHS0 = LHS->getOperand(0);
2466   if (LPred != ICmpInst::ICMP_EQ || !match(LHS->getOperand(1), m_Zero()) ||
2467       !LHS0->getType()->isIntOrIntVectorTy() ||
2468       !(LHS->hasOneUse() || RHS->hasOneUse()))
2469     return nullptr;
2470 
2471   Value *Other;
2472   if (RPred == ICmpInst::ICMP_ULT && RHS->getOperand(1) == LHS0)
2473     Other = RHS->getOperand(0);
2474   else if (RPred == ICmpInst::ICMP_UGT && RHS->getOperand(0) == LHS0)
2475     Other = RHS->getOperand(1);
2476   else
2477     return nullptr;
2478 
2479   return Builder.CreateICmp(
2480       IsAnd ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE,
2481       Builder.CreateAdd(LHS0, Constant::getAllOnesValue(LHS0->getType())),
2482       Other);
2483 }
2484 
2485 /// Fold (icmp)&(icmp) or (icmp)|(icmp) if possible.
2486 /// If IsLogical is true, then the and/or is in select form and the transform
2487 /// must be poison-safe.
2488 Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
2489                                           Instruction &I, bool IsAnd,
2490                                           bool IsLogical) {
2491   const SimplifyQuery Q = SQ.getWithInstruction(&I);
2492 
2493   // Fold (iszero(A & K1) | iszero(A & K2)) ->  (A & (K1 | K2)) != (K1 | K2)
2494   // Fold (!iszero(A & K1) & !iszero(A & K2)) ->  (A & (K1 | K2)) == (K1 | K2)
2495   // if K1 and K2 are a one-bit mask.
2496   if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, &I, IsAnd, IsLogical))
2497     return V;
2498 
2499   ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
2500   Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
2501   Value *LHS1 = LHS->getOperand(1), *RHS1 = RHS->getOperand(1);
2502   const APInt *LHSC = nullptr, *RHSC = nullptr;
2503   match(LHS1, m_APInt(LHSC));
2504   match(RHS1, m_APInt(RHSC));
2505 
2506   // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
2507   // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
2508   if (predicatesFoldable(PredL, PredR)) {
2509     if (LHS0 == RHS1 && LHS1 == RHS0) {
2510       PredL = ICmpInst::getSwappedPredicate(PredL);
2511       std::swap(LHS0, LHS1);
2512     }
2513     if (LHS0 == RHS0 && LHS1 == RHS1) {
2514       unsigned Code = IsAnd ? getICmpCode(PredL) & getICmpCode(PredR)
2515                             : getICmpCode(PredL) | getICmpCode(PredR);
2516       bool IsSigned = LHS->isSigned() || RHS->isSigned();
2517       return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
2518     }
2519   }
2520 
2521   // handle (roughly):
2522   // (icmp ne (A & B), C) | (icmp ne (A & D), E)
2523   // (icmp eq (A & B), C) & (icmp eq (A & D), E)
2524   if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, IsAnd, IsLogical, Builder))
2525     return V;
2526 
2527   // TODO: One of these directions is fine with logical and/or, the other could
2528   // be supported by inserting freeze.
2529   if (!IsLogical) {
2530     if (Value *V = foldAndOrOfICmpEqZeroAndICmp(LHS, RHS, IsAnd, Builder))
2531       return V;
2532     if (Value *V = foldAndOrOfICmpEqZeroAndICmp(RHS, LHS, IsAnd, Builder))
2533       return V;
2534   }
2535 
2536   // TODO: Verify whether this is safe for logical and/or.
2537   if (!IsLogical) {
2538     if (Value *V = foldAndOrOfICmpsWithConstEq(LHS, RHS, IsAnd, Builder, Q))
2539       return V;
2540     if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, IsAnd, Builder, Q))
2541       return V;
2542   }
2543 
2544   if (Value *V = foldIsPowerOf2OrZero(LHS, RHS, IsAnd, Builder))
2545     return V;
2546   if (Value *V = foldIsPowerOf2OrZero(RHS, LHS, IsAnd, Builder))
2547     return V;
2548 
2549   // TODO: One of these directions is fine with logical and/or, the other could
2550   // be supported by inserting freeze.
2551   if (!IsLogical) {
2552     // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
2553     // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
2554     if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/!IsAnd))
2555       return V;
2556 
2557     // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
2558     // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
2559     if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/!IsAnd))
2560       return V;
2561   }
2562 
2563   // TODO: Add conjugated or fold, check whether it is safe for logical and/or.
2564   if (IsAnd && !IsLogical)
2565     if (Value *V = foldSignedTruncationCheck(LHS, RHS, I, Builder))
2566       return V;
2567 
2568   if (Value *V = foldIsPowerOf2(LHS, RHS, IsAnd, Builder))
2569     return V;
2570 
2571   // TODO: Verify whether this is safe for logical and/or.
2572   if (!IsLogical) {
2573     if (Value *X = foldUnsignedUnderflowCheck(LHS, RHS, IsAnd, Q, Builder))
2574       return X;
2575     if (Value *X = foldUnsignedUnderflowCheck(RHS, LHS, IsAnd, Q, Builder))
2576       return X;
2577   }
2578 
2579   if (Value *X = foldEqOfParts(LHS, RHS, IsAnd))
2580     return X;
2581 
2582   // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
2583   // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
2584   // TODO: Remove this when foldLogOpOfMaskedICmps can handle undefs.
2585   if (!IsLogical && PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
2586       PredL == PredR && match(LHS1, m_ZeroInt()) && match(RHS1, m_ZeroInt()) &&
2587       LHS0->getType() == RHS0->getType()) {
2588     Value *NewOr = Builder.CreateOr(LHS0, RHS0);
2589     return Builder.CreateICmp(PredL, NewOr,
2590                               Constant::getNullValue(NewOr->getType()));
2591   }
2592 
2593   // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
2594   if (!LHSC || !RHSC)
2595     return nullptr;
2596 
2597   // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
2598   // (trunc x) != C1 | (and x, CA) != C2 -> (and x, CA|CMAX) != C1|C2
2599   // where CMAX is the all ones value for the truncated type,
2600   // iff the lower bits of C2 and CA are zero.
2601   if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
2602       PredL == PredR && LHS->hasOneUse() && RHS->hasOneUse()) {
2603     Value *V;
2604     const APInt *AndC, *SmallC = nullptr, *BigC = nullptr;
2605 
2606     // (trunc x) == C1 & (and x, CA) == C2
2607     // (and x, CA) == C2 & (trunc x) == C1
2608     if (match(RHS0, m_Trunc(m_Value(V))) &&
2609         match(LHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
2610       SmallC = RHSC;
2611       BigC = LHSC;
2612     } else if (match(LHS0, m_Trunc(m_Value(V))) &&
2613                match(RHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
2614       SmallC = LHSC;
2615       BigC = RHSC;
2616     }
2617 
2618     if (SmallC && BigC) {
2619       unsigned BigBitSize = BigC->getBitWidth();
2620       unsigned SmallBitSize = SmallC->getBitWidth();
2621 
2622       // Check that the low bits are zero.
2623       APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
2624       if ((Low & *AndC).isZero() && (Low & *BigC).isZero()) {
2625         Value *NewAnd = Builder.CreateAnd(V, Low | *AndC);
2626         APInt N = SmallC->zext(BigBitSize) | *BigC;
2627         Value *NewVal = ConstantInt::get(NewAnd->getType(), N);
2628         return Builder.CreateICmp(PredL, NewAnd, NewVal);
2629       }
2630     }
2631   }
2632 
2633   return foldAndOrOfICmpsUsingRanges(LHS, RHS, IsAnd);
2634 }
2635 
2636 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2637 // here. We should standardize that construct where it is needed or choose some
2638 // other way to ensure that commutated variants of patterns are not missed.
2639 Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
2640   if (Value *V = simplifyOrInst(I.getOperand(0), I.getOperand(1),
2641                                 SQ.getWithInstruction(&I)))
2642     return replaceInstUsesWith(I, V);
2643 
2644   if (SimplifyAssociativeOrCommutative(I))
2645     return &I;
2646 
2647   if (Instruction *X = foldVectorBinop(I))
2648     return X;
2649 
2650   if (Instruction *Phi = foldBinopWithPhiOperands(I))
2651     return Phi;
2652 
2653   // See if we can simplify any instructions used by the instruction whose sole
2654   // purpose is to compute bits we don't care about.
2655   if (SimplifyDemandedInstructionBits(I))
2656     return &I;
2657 
2658   // Do this before using distributive laws to catch simple and/or/not patterns.
2659   if (Instruction *Xor = foldOrToXor(I, Builder))
2660     return Xor;
2661 
2662   if (Instruction *X = foldComplexAndOrPatterns(I, Builder))
2663     return X;
2664 
2665   // (A&B)|(A&C) -> A&(B|C) etc
2666   if (Value *V = SimplifyUsingDistributiveLaws(I))
2667     return replaceInstUsesWith(I, V);
2668 
2669   if (Value *V = SimplifyBSwap(I, Builder))
2670     return replaceInstUsesWith(I, V);
2671 
2672   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2673   Type *Ty = I.getType();
2674   if (Ty->isIntOrIntVectorTy(1)) {
2675     if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
2676       if (auto *I =
2677               foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ false))
2678         return I;
2679     }
2680     if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
2681       if (auto *I =
2682               foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ false))
2683         return I;
2684     }
2685   }
2686 
2687   if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2688     return FoldedLogic;
2689 
2690   if (Instruction *BitOp = matchBSwapOrBitReverse(I, /*MatchBSwaps*/ true,
2691                                                   /*MatchBitReversals*/ true))
2692     return BitOp;
2693 
2694   if (Instruction *Funnel = matchFunnelShift(I, *this))
2695     return Funnel;
2696 
2697   if (Instruction *Concat = matchOrConcat(I, Builder))
2698     return replaceInstUsesWith(I, Concat);
2699 
2700   Value *X, *Y;
2701   const APInt *CV;
2702   if (match(&I, m_c_Or(m_OneUse(m_Xor(m_Value(X), m_APInt(CV))), m_Value(Y))) &&
2703       !CV->isAllOnes() && MaskedValueIsZero(Y, *CV, 0, &I)) {
2704     // (X ^ C) | Y -> (X | Y) ^ C iff Y & C == 0
2705     // The check for a 'not' op is for efficiency (if Y is known zero --> ~X).
2706     Value *Or = Builder.CreateOr(X, Y);
2707     return BinaryOperator::CreateXor(Or, ConstantInt::get(Ty, *CV));
2708   }
2709 
2710   // If the operands have no common bits set:
2711   // or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
2712   if (match(&I,
2713             m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
2714       haveNoCommonBitsSet(Op0, Op1, DL)) {
2715     Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
2716     return BinaryOperator::CreateMul(X, IncrementY);
2717   }
2718 
2719   // (A & C) | (B & D)
2720   Value *A, *B, *C, *D;
2721   if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2722       match(Op1, m_And(m_Value(B), m_Value(D)))) {
2723 
2724     // (A & C0) | (B & C1)
2725     const APInt *C0, *C1;
2726     if (match(C, m_APInt(C0)) && match(D, m_APInt(C1))) {
2727       Value *X;
2728       if (*C0 == ~*C1) {
2729         // ((X | B) & MaskC) | (B & ~MaskC) -> (X & MaskC) | B
2730         if (match(A, m_c_Or(m_Value(X), m_Specific(B))))
2731           return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C0), B);
2732         // (A & MaskC) | ((X | A) & ~MaskC) -> (X & ~MaskC) | A
2733         if (match(B, m_c_Or(m_Specific(A), m_Value(X))))
2734           return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C1), A);
2735 
2736         // ((X ^ B) & MaskC) | (B & ~MaskC) -> (X & MaskC) ^ B
2737         if (match(A, m_c_Xor(m_Value(X), m_Specific(B))))
2738           return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C0), B);
2739         // (A & MaskC) | ((X ^ A) & ~MaskC) -> (X & ~MaskC) ^ A
2740         if (match(B, m_c_Xor(m_Specific(A), m_Value(X))))
2741           return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C1), A);
2742       }
2743 
2744       if ((*C0 & *C1).isZero()) {
2745         // ((X | B) & C0) | (B & C1) --> (X | B) & (C0 | C1)
2746         // iff (C0 & C1) == 0 and (X & ~C0) == 0
2747         if (match(A, m_c_Or(m_Value(X), m_Specific(B))) &&
2748             MaskedValueIsZero(X, ~*C0, 0, &I)) {
2749           Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2750           return BinaryOperator::CreateAnd(A, C01);
2751         }
2752         // (A & C0) | ((X | A) & C1) --> (X | A) & (C0 | C1)
2753         // iff (C0 & C1) == 0 and (X & ~C1) == 0
2754         if (match(B, m_c_Or(m_Value(X), m_Specific(A))) &&
2755             MaskedValueIsZero(X, ~*C1, 0, &I)) {
2756           Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2757           return BinaryOperator::CreateAnd(B, C01);
2758         }
2759         // ((X | C2) & C0) | ((X | C3) & C1) --> (X | C2 | C3) & (C0 | C1)
2760         // iff (C0 & C1) == 0 and (C2 & ~C0) == 0 and (C3 & ~C1) == 0.
2761         const APInt *C2, *C3;
2762         if (match(A, m_Or(m_Value(X), m_APInt(C2))) &&
2763             match(B, m_Or(m_Specific(X), m_APInt(C3))) &&
2764             (*C2 & ~*C0).isZero() && (*C3 & ~*C1).isZero()) {
2765           Value *Or = Builder.CreateOr(X, *C2 | *C3, "bitfield");
2766           Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2767           return BinaryOperator::CreateAnd(Or, C01);
2768         }
2769       }
2770     }
2771 
2772     // Don't try to form a select if it's unlikely that we'll get rid of at
2773     // least one of the operands. A select is generally more expensive than the
2774     // 'or' that it is replacing.
2775     if (Op0->hasOneUse() || Op1->hasOneUse()) {
2776       // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2777       if (Value *V = matchSelectFromAndOr(A, C, B, D))
2778         return replaceInstUsesWith(I, V);
2779       if (Value *V = matchSelectFromAndOr(A, C, D, B))
2780         return replaceInstUsesWith(I, V);
2781       if (Value *V = matchSelectFromAndOr(C, A, B, D))
2782         return replaceInstUsesWith(I, V);
2783       if (Value *V = matchSelectFromAndOr(C, A, D, B))
2784         return replaceInstUsesWith(I, V);
2785       if (Value *V = matchSelectFromAndOr(B, D, A, C))
2786         return replaceInstUsesWith(I, V);
2787       if (Value *V = matchSelectFromAndOr(B, D, C, A))
2788         return replaceInstUsesWith(I, V);
2789       if (Value *V = matchSelectFromAndOr(D, B, A, C))
2790         return replaceInstUsesWith(I, V);
2791       if (Value *V = matchSelectFromAndOr(D, B, C, A))
2792         return replaceInstUsesWith(I, V);
2793     }
2794   }
2795 
2796   // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2797   if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2798     if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2799       return BinaryOperator::CreateOr(Op0, C);
2800 
2801   // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2802   if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2803     if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2804       return BinaryOperator::CreateOr(Op1, C);
2805 
2806   // ((A & B) ^ C) | B -> C | B
2807   if (match(Op0, m_c_Xor(m_c_And(m_Value(A), m_Specific(Op1)), m_Value(C))))
2808     return BinaryOperator::CreateOr(C, Op1);
2809 
2810   // B | ((A & B) ^ C) -> B | C
2811   if (match(Op1, m_c_Xor(m_c_And(m_Value(A), m_Specific(Op0)), m_Value(C))))
2812     return BinaryOperator::CreateOr(Op0, C);
2813 
2814   // ((B | C) & A) | B -> B | (A & C)
2815   if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2816     return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
2817 
2818   if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2819     return DeMorgan;
2820 
2821   // Canonicalize xor to the RHS.
2822   bool SwappedForXor = false;
2823   if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2824     std::swap(Op0, Op1);
2825     SwappedForXor = true;
2826   }
2827 
2828   // A | ( A ^ B) -> A |  B
2829   // A | (~A ^ B) -> A | ~B
2830   // (A & B) | (A ^ B)
2831   // ~A | (A ^ B) -> ~(A & B)
2832   // The swap above should always make Op0 the 'not' for the last case.
2833   if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2834     if (Op0 == A || Op0 == B)
2835       return BinaryOperator::CreateOr(A, B);
2836 
2837     if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2838         match(Op0, m_And(m_Specific(B), m_Specific(A))))
2839       return BinaryOperator::CreateOr(A, B);
2840 
2841     if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
2842         (match(Op0, m_Not(m_Specific(A))) || match(Op0, m_Not(m_Specific(B)))))
2843       return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
2844 
2845     if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2846       Value *Not = Builder.CreateNot(B, B->getName() + ".not");
2847       return BinaryOperator::CreateOr(Not, Op0);
2848     }
2849     if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2850       Value *Not = Builder.CreateNot(A, A->getName() + ".not");
2851       return BinaryOperator::CreateOr(Not, Op0);
2852     }
2853   }
2854 
2855   // A | ~(A | B) -> A | ~B
2856   // A | ~(A ^ B) -> A | ~B
2857   if (match(Op1, m_Not(m_Value(A))))
2858     if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2859       if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2860           Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2861                                B->getOpcode() == Instruction::Xor)) {
2862         Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2863                                                  B->getOperand(0);
2864         Value *Not = Builder.CreateNot(NotOp, NotOp->getName() + ".not");
2865         return BinaryOperator::CreateOr(Not, Op0);
2866       }
2867 
2868   if (SwappedForXor)
2869     std::swap(Op0, Op1);
2870 
2871   {
2872     ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2873     ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2874     if (LHS && RHS)
2875       if (Value *Res = foldAndOrOfICmps(LHS, RHS, I, /* IsAnd */ false))
2876         return replaceInstUsesWith(I, Res);
2877 
2878     // TODO: Make this recursive; it's a little tricky because an arbitrary
2879     // number of 'or' instructions might have to be created.
2880     Value *X, *Y;
2881     if (LHS && match(Op1, m_OneUse(m_LogicalOr(m_Value(X), m_Value(Y))))) {
2882       bool IsLogical = isa<SelectInst>(Op1);
2883       // LHS | (X || Y) --> (LHS || X) || Y
2884       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2885         if (Value *Res =
2886                 foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ false, IsLogical))
2887           return replaceInstUsesWith(I, IsLogical
2888                                             ? Builder.CreateLogicalOr(Res, Y)
2889                                             : Builder.CreateOr(Res, Y));
2890       // LHS | (X || Y) --> X || (LHS | Y)
2891       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2892         if (Value *Res = foldAndOrOfICmps(LHS, Cmp, I, /* IsAnd */ false,
2893                                           /* IsLogical */ false))
2894           return replaceInstUsesWith(I, IsLogical
2895                                             ? Builder.CreateLogicalOr(X, Res)
2896                                             : Builder.CreateOr(X, Res));
2897     }
2898     if (RHS && match(Op0, m_OneUse(m_LogicalOr(m_Value(X), m_Value(Y))))) {
2899       bool IsLogical = isa<SelectInst>(Op0);
2900       // (X || Y) | RHS --> (X || RHS) || Y
2901       if (auto *Cmp = dyn_cast<ICmpInst>(X))
2902         if (Value *Res =
2903                 foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ false, IsLogical))
2904           return replaceInstUsesWith(I, IsLogical
2905                                             ? Builder.CreateLogicalOr(Res, Y)
2906                                             : Builder.CreateOr(Res, Y));
2907       // (X || Y) | RHS --> X || (Y | RHS)
2908       if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2909         if (Value *Res = foldAndOrOfICmps(Cmp, RHS, I, /* IsAnd */ false,
2910                                           /* IsLogical */ false))
2911           return replaceInstUsesWith(I, IsLogical
2912                                             ? Builder.CreateLogicalOr(X, Res)
2913                                             : Builder.CreateOr(X, Res));
2914     }
2915   }
2916 
2917   if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2918     if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2919       if (Value *Res = foldLogicOfFCmps(LHS, RHS, /*IsAnd*/ false))
2920         return replaceInstUsesWith(I, Res);
2921 
2922   if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2923     return FoldedFCmps;
2924 
2925   if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2926     return CastedOr;
2927 
2928   if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2929     return Sel;
2930 
2931   // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
2932   // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2933   //       with binop identity constant. But creating a select with non-constant
2934   //       arm may not be reversible due to poison semantics. Is that a good
2935   //       canonicalization?
2936   if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2937       A->getType()->isIntOrIntVectorTy(1))
2938     return SelectInst::Create(A, ConstantInt::getAllOnesValue(Ty), Op1);
2939   if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2940       A->getType()->isIntOrIntVectorTy(1))
2941     return SelectInst::Create(A, ConstantInt::getAllOnesValue(Ty), Op0);
2942 
2943   // Note: If we've gotten to the point of visiting the outer OR, then the
2944   // inner one couldn't be simplified.  If it was a constant, then it won't
2945   // be simplified by a later pass either, so we try swapping the inner/outer
2946   // ORs in the hopes that we'll be able to simplify it this way.
2947   // (X|C) | V --> (X|V) | C
2948   ConstantInt *CI;
2949   if (Op0->hasOneUse() && !match(Op1, m_ConstantInt()) &&
2950       match(Op0, m_Or(m_Value(A), m_ConstantInt(CI)))) {
2951     Value *Inner = Builder.CreateOr(A, Op1);
2952     Inner->takeName(Op0);
2953     return BinaryOperator::CreateOr(Inner, CI);
2954   }
2955 
2956   // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2957   // Since this OR statement hasn't been optimized further yet, we hope
2958   // that this transformation will allow the new ORs to be optimized.
2959   {
2960     Value *X = nullptr, *Y = nullptr;
2961     if (Op0->hasOneUse() && Op1->hasOneUse() &&
2962         match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2963         match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2964       Value *orTrue = Builder.CreateOr(A, C);
2965       Value *orFalse = Builder.CreateOr(B, D);
2966       return SelectInst::Create(X, orTrue, orFalse);
2967     }
2968   }
2969 
2970   // or(ashr(subNSW(Y, X), ScalarSizeInBits(Y) - 1), X)  --> X s> Y ? -1 : X.
2971   {
2972     Value *X, *Y;
2973     if (match(&I, m_c_Or(m_OneUse(m_AShr(
2974                              m_NSWSub(m_Value(Y), m_Value(X)),
2975                              m_SpecificInt(Ty->getScalarSizeInBits() - 1))),
2976                          m_Deferred(X)))) {
2977       Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
2978       Value *AllOnes = ConstantInt::getAllOnesValue(Ty);
2979       return SelectInst::Create(NewICmpInst, AllOnes, X);
2980     }
2981   }
2982 
2983   if (Instruction *V =
2984           canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I))
2985     return V;
2986 
2987   CmpInst::Predicate Pred;
2988   Value *Mul, *Ov, *MulIsNotZero, *UMulWithOv;
2989   // Check if the OR weakens the overflow condition for umul.with.overflow by
2990   // treating any non-zero result as overflow. In that case, we overflow if both
2991   // umul.with.overflow operands are != 0, as in that case the result can only
2992   // be 0, iff the multiplication overflows.
2993   if (match(&I,
2994             m_c_Or(m_CombineAnd(m_ExtractValue<1>(m_Value(UMulWithOv)),
2995                                 m_Value(Ov)),
2996                    m_CombineAnd(m_ICmp(Pred,
2997                                        m_CombineAnd(m_ExtractValue<0>(
2998                                                         m_Deferred(UMulWithOv)),
2999                                                     m_Value(Mul)),
3000                                        m_ZeroInt()),
3001                                 m_Value(MulIsNotZero)))) &&
3002       (Ov->hasOneUse() || (MulIsNotZero->hasOneUse() && Mul->hasOneUse())) &&
3003       Pred == CmpInst::ICMP_NE) {
3004     Value *A, *B;
3005     if (match(UMulWithOv, m_Intrinsic<Intrinsic::umul_with_overflow>(
3006                               m_Value(A), m_Value(B)))) {
3007       Value *NotNullA = Builder.CreateIsNotNull(A);
3008       Value *NotNullB = Builder.CreateIsNotNull(B);
3009       return BinaryOperator::CreateAnd(NotNullA, NotNullB);
3010     }
3011   }
3012 
3013   // (~x) | y  -->  ~(x & (~y))  iff that gets rid of inversions
3014   if (sinkNotIntoOtherHandOfAndOrOr(I))
3015     return &I;
3016 
3017   // Improve "get low bit mask up to and including bit X" pattern:
3018   //   (1 << X) | ((1 << X) + -1)  -->  -1 l>> (bitwidth(x) - 1 - X)
3019   if (match(&I, m_c_Or(m_Add(m_Shl(m_One(), m_Value(X)), m_AllOnes()),
3020                        m_Shl(m_One(), m_Deferred(X)))) &&
3021       match(&I, m_c_Or(m_OneUse(m_Value()), m_Value()))) {
3022     Value *Sub = Builder.CreateSub(
3023         ConstantInt::get(Ty, Ty->getScalarSizeInBits() - 1), X);
3024     return BinaryOperator::CreateLShr(Constant::getAllOnesValue(Ty), Sub);
3025   }
3026 
3027   // An or recurrence w/loop invariant step is equivelent to (or start, step)
3028   PHINode *PN = nullptr;
3029   Value *Start = nullptr, *Step = nullptr;
3030   if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
3031     return replaceInstUsesWith(I, Builder.CreateOr(Start, Step));
3032 
3033   // (A & B) | (C | D) or (C | D) | (A & B)
3034   // Can be combined if C or D is of type (A/B & X)
3035   if (match(&I, m_c_Or(m_OneUse(m_And(m_Value(A), m_Value(B))),
3036                        m_OneUse(m_Or(m_Value(C), m_Value(D)))))) {
3037     // (A & B) | (C | ?) -> C | (? | (A & B))
3038     // (A & B) | (C | ?) -> C | (? | (A & B))
3039     // (A & B) | (C | ?) -> C | (? | (A & B))
3040     // (A & B) | (C | ?) -> C | (? | (A & B))
3041     // (C | ?) | (A & B) -> C | (? | (A & B))
3042     // (C | ?) | (A & B) -> C | (? | (A & B))
3043     // (C | ?) | (A & B) -> C | (? | (A & B))
3044     // (C | ?) | (A & B) -> C | (? | (A & B))
3045     if (match(D, m_OneUse(m_c_And(m_Specific(A), m_Value()))) ||
3046         match(D, m_OneUse(m_c_And(m_Specific(B), m_Value()))))
3047       return BinaryOperator::CreateOr(
3048           C, Builder.CreateOr(D, Builder.CreateAnd(A, B)));
3049     // (A & B) | (? | D) -> (? | (A & B)) | D
3050     // (A & B) | (? | D) -> (? | (A & B)) | D
3051     // (A & B) | (? | D) -> (? | (A & B)) | D
3052     // (A & B) | (? | D) -> (? | (A & B)) | D
3053     // (? | D) | (A & B) -> (? | (A & B)) | D
3054     // (? | D) | (A & B) -> (? | (A & B)) | D
3055     // (? | D) | (A & B) -> (? | (A & B)) | D
3056     // (? | D) | (A & B) -> (? | (A & B)) | D
3057     if (match(C, m_OneUse(m_c_And(m_Specific(A), m_Value()))) ||
3058         match(C, m_OneUse(m_c_And(m_Specific(B), m_Value()))))
3059       return BinaryOperator::CreateOr(
3060           Builder.CreateOr(C, Builder.CreateAnd(A, B)), D);
3061   }
3062 
3063   return nullptr;
3064 }
3065 
3066 /// A ^ B can be specified using other logic ops in a variety of patterns. We
3067 /// can fold these early and efficiently by morphing an existing instruction.
3068 static Instruction *foldXorToXor(BinaryOperator &I,
3069                                  InstCombiner::BuilderTy &Builder) {
3070   assert(I.getOpcode() == Instruction::Xor);
3071   Value *Op0 = I.getOperand(0);
3072   Value *Op1 = I.getOperand(1);
3073   Value *A, *B;
3074 
3075   // There are 4 commuted variants for each of the basic patterns.
3076 
3077   // (A & B) ^ (A | B) -> A ^ B
3078   // (A & B) ^ (B | A) -> A ^ B
3079   // (A | B) ^ (A & B) -> A ^ B
3080   // (A | B) ^ (B & A) -> A ^ B
3081   if (match(&I, m_c_Xor(m_And(m_Value(A), m_Value(B)),
3082                         m_c_Or(m_Deferred(A), m_Deferred(B)))))
3083     return BinaryOperator::CreateXor(A, B);
3084 
3085   // (A | ~B) ^ (~A | B) -> A ^ B
3086   // (~B | A) ^ (~A | B) -> A ^ B
3087   // (~A | B) ^ (A | ~B) -> A ^ B
3088   // (B | ~A) ^ (A | ~B) -> A ^ B
3089   if (match(&I, m_Xor(m_c_Or(m_Value(A), m_Not(m_Value(B))),
3090                       m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B)))))
3091     return BinaryOperator::CreateXor(A, B);
3092 
3093   // (A & ~B) ^ (~A & B) -> A ^ B
3094   // (~B & A) ^ (~A & B) -> A ^ B
3095   // (~A & B) ^ (A & ~B) -> A ^ B
3096   // (B & ~A) ^ (A & ~B) -> A ^ B
3097   if (match(&I, m_Xor(m_c_And(m_Value(A), m_Not(m_Value(B))),
3098                       m_c_And(m_Not(m_Deferred(A)), m_Deferred(B)))))
3099     return BinaryOperator::CreateXor(A, B);
3100 
3101   // For the remaining cases we need to get rid of one of the operands.
3102   if (!Op0->hasOneUse() && !Op1->hasOneUse())
3103     return nullptr;
3104 
3105   // (A | B) ^ ~(A & B) -> ~(A ^ B)
3106   // (A | B) ^ ~(B & A) -> ~(A ^ B)
3107   // (A & B) ^ ~(A | B) -> ~(A ^ B)
3108   // (A & B) ^ ~(B | A) -> ~(A ^ B)
3109   // Complexity sorting ensures the not will be on the right side.
3110   if ((match(Op0, m_Or(m_Value(A), m_Value(B))) &&
3111        match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) ||
3112       (match(Op0, m_And(m_Value(A), m_Value(B))) &&
3113        match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))))
3114     return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
3115 
3116   return nullptr;
3117 }
3118 
3119 Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
3120                                         BinaryOperator &I) {
3121   assert(I.getOpcode() == Instruction::Xor && I.getOperand(0) == LHS &&
3122          I.getOperand(1) == RHS && "Should be 'xor' with these operands");
3123 
3124   ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
3125   Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
3126   Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
3127 
3128   if (predicatesFoldable(PredL, PredR)) {
3129     if (LHS0 == RHS1 && LHS1 == RHS0) {
3130       std::swap(LHS0, LHS1);
3131       PredL = ICmpInst::getSwappedPredicate(PredL);
3132     }
3133     if (LHS0 == RHS0 && LHS1 == RHS1) {
3134       // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
3135       unsigned Code = getICmpCode(PredL) ^ getICmpCode(PredR);
3136       bool IsSigned = LHS->isSigned() || RHS->isSigned();
3137       return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
3138     }
3139   }
3140 
3141   // TODO: This can be generalized to compares of non-signbits using
3142   // decomposeBitTestICmp(). It could be enhanced more by using (something like)
3143   // foldLogOpOfMaskedICmps().
3144   if ((LHS->hasOneUse() || RHS->hasOneUse()) &&
3145       LHS0->getType() == RHS0->getType() &&
3146       LHS0->getType()->isIntOrIntVectorTy()) {
3147     // (X > -1) ^ (Y > -1) --> (X ^ Y) < 0
3148     // (X <  0) ^ (Y <  0) --> (X ^ Y) < 0
3149     if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) &&
3150          PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes())) ||
3151         (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) &&
3152          PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero())))
3153       return Builder.CreateIsNeg(Builder.CreateXor(LHS0, RHS0));
3154 
3155     // (X > -1) ^ (Y <  0) --> (X ^ Y) > -1
3156     // (X <  0) ^ (Y > -1) --> (X ^ Y) > -1
3157     if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) &&
3158          PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero())) ||
3159         (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) &&
3160          PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes())))
3161       return Builder.CreateIsNotNeg(Builder.CreateXor(LHS0, RHS0));
3162 
3163   }
3164 
3165   // Instead of trying to imitate the folds for and/or, decompose this 'xor'
3166   // into those logic ops. That is, try to turn this into an and-of-icmps
3167   // because we have many folds for that pattern.
3168   //
3169   // This is based on a truth table definition of xor:
3170   // X ^ Y --> (X | Y) & !(X & Y)
3171   if (Value *OrICmp = simplifyBinOp(Instruction::Or, LHS, RHS, SQ)) {
3172     // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y).
3173     // TODO: If OrICmp is false, the whole thing is false (InstSimplify?).
3174     if (Value *AndICmp = simplifyBinOp(Instruction::And, LHS, RHS, SQ)) {
3175       // TODO: Independently handle cases where the 'and' side is a constant.
3176       ICmpInst *X = nullptr, *Y = nullptr;
3177       if (OrICmp == LHS && AndICmp == RHS) {
3178         // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS  --> X & !Y
3179         X = LHS;
3180         Y = RHS;
3181       }
3182       if (OrICmp == RHS && AndICmp == LHS) {
3183         // !(LHS & RHS) & (LHS | RHS) --> !LHS & RHS  --> !Y & X
3184         X = RHS;
3185         Y = LHS;
3186       }
3187       if (X && Y && (Y->hasOneUse() || canFreelyInvertAllUsersOf(Y, &I))) {
3188         // Invert the predicate of 'Y', thus inverting its output.
3189         Y->setPredicate(Y->getInversePredicate());
3190         // So, are there other uses of Y?
3191         if (!Y->hasOneUse()) {
3192           // We need to adapt other uses of Y though. Get a value that matches
3193           // the original value of Y before inversion. While this increases
3194           // immediate instruction count, we have just ensured that all the
3195           // users are freely-invertible, so that 'not' *will* get folded away.
3196           BuilderTy::InsertPointGuard Guard(Builder);
3197           // Set insertion point to right after the Y.
3198           Builder.SetInsertPoint(Y->getParent(), ++(Y->getIterator()));
3199           Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3200           // Replace all uses of Y (excluding the one in NotY!) with NotY.
3201           Worklist.pushUsersToWorkList(*Y);
3202           Y->replaceUsesWithIf(NotY,
3203                                [NotY](Use &U) { return U.getUser() != NotY; });
3204         }
3205         // All done.
3206         return Builder.CreateAnd(LHS, RHS);
3207       }
3208     }
3209   }
3210 
3211   return nullptr;
3212 }
3213 
3214 /// If we have a masked merge, in the canonical form of:
3215 /// (assuming that A only has one use.)
3216 ///   |        A  |  |B|
3217 ///   ((x ^ y) & M) ^ y
3218 ///    |  D  |
3219 /// * If M is inverted:
3220 ///      |  D  |
3221 ///     ((x ^ y) & ~M) ^ y
3222 ///   We can canonicalize by swapping the final xor operand
3223 ///   to eliminate the 'not' of the mask.
3224 ///     ((x ^ y) & M) ^ x
3225 /// * If M is a constant, and D has one use, we transform to 'and' / 'or' ops
3226 ///   because that shortens the dependency chain and improves analysis:
3227 ///     (x & M) | (y & ~M)
3228 static Instruction *visitMaskedMerge(BinaryOperator &I,
3229                                      InstCombiner::BuilderTy &Builder) {
3230   Value *B, *X, *D;
3231   Value *M;
3232   if (!match(&I, m_c_Xor(m_Value(B),
3233                          m_OneUse(m_c_And(
3234                              m_CombineAnd(m_c_Xor(m_Deferred(B), m_Value(X)),
3235                                           m_Value(D)),
3236                              m_Value(M))))))
3237     return nullptr;
3238 
3239   Value *NotM;
3240   if (match(M, m_Not(m_Value(NotM)))) {
3241     // De-invert the mask and swap the value in B part.
3242     Value *NewA = Builder.CreateAnd(D, NotM);
3243     return BinaryOperator::CreateXor(NewA, X);
3244   }
3245 
3246   Constant *C;
3247   if (D->hasOneUse() && match(M, m_Constant(C))) {
3248     // Propagating undef is unsafe. Clamp undef elements to -1.
3249     Type *EltTy = C->getType()->getScalarType();
3250     C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
3251     // Unfold.
3252     Value *LHS = Builder.CreateAnd(X, C);
3253     Value *NotC = Builder.CreateNot(C);
3254     Value *RHS = Builder.CreateAnd(B, NotC);
3255     return BinaryOperator::CreateOr(LHS, RHS);
3256   }
3257 
3258   return nullptr;
3259 }
3260 
3261 // Transform
3262 //   ~(x ^ y)
3263 // into:
3264 //   (~x) ^ y
3265 // or into
3266 //   x ^ (~y)
3267 static Instruction *sinkNotIntoXor(BinaryOperator &I,
3268                                    InstCombiner::BuilderTy &Builder) {
3269   Value *X, *Y;
3270   // FIXME: one-use check is not needed in general, but currently we are unable
3271   // to fold 'not' into 'icmp', if that 'icmp' has multiple uses. (D35182)
3272   if (!match(&I, m_Not(m_OneUse(m_Xor(m_Value(X), m_Value(Y))))))
3273     return nullptr;
3274 
3275   // We only want to do the transform if it is free to do.
3276   if (InstCombiner::isFreeToInvert(X, X->hasOneUse())) {
3277     // Ok, good.
3278   } else if (InstCombiner::isFreeToInvert(Y, Y->hasOneUse())) {
3279     std::swap(X, Y);
3280   } else
3281     return nullptr;
3282 
3283   Value *NotX = Builder.CreateNot(X, X->getName() + ".not");
3284   return BinaryOperator::CreateXor(NotX, Y, I.getName() + ".demorgan");
3285 }
3286 
3287 /// Canonicalize a shifty way to code absolute value to the more common pattern
3288 /// that uses negation and select.
3289 static Instruction *canonicalizeAbs(BinaryOperator &Xor,
3290                                     InstCombiner::BuilderTy &Builder) {
3291   assert(Xor.getOpcode() == Instruction::Xor && "Expected an xor instruction.");
3292 
3293   // There are 4 potential commuted variants. Move the 'ashr' candidate to Op1.
3294   // We're relying on the fact that we only do this transform when the shift has
3295   // exactly 2 uses and the add has exactly 1 use (otherwise, we might increase
3296   // instructions).
3297   Value *Op0 = Xor.getOperand(0), *Op1 = Xor.getOperand(1);
3298   if (Op0->hasNUses(2))
3299     std::swap(Op0, Op1);
3300 
3301   Type *Ty = Xor.getType();
3302   Value *A;
3303   const APInt *ShAmt;
3304   if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
3305       Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
3306       match(Op0, m_OneUse(m_c_Add(m_Specific(A), m_Specific(Op1))))) {
3307     // Op1 = ashr i32 A, 31   ; smear the sign bit
3308     // xor (add A, Op1), Op1  ; add -1 and flip bits if negative
3309     // --> (A < 0) ? -A : A
3310     Value *IsNeg = Builder.CreateIsNeg(A);
3311     // Copy the nuw/nsw flags from the add to the negate.
3312     auto *Add = cast<BinaryOperator>(Op0);
3313     Value *NegA = Builder.CreateNeg(A, "", Add->hasNoUnsignedWrap(),
3314                                    Add->hasNoSignedWrap());
3315     return SelectInst::Create(IsNeg, NegA, A);
3316   }
3317   return nullptr;
3318 }
3319 
3320 // Transform
3321 //   z = (~x) &/| y
3322 // into:
3323 //   z = ~(x |/& (~y))
3324 // iff y is free to invert and all uses of z can be freely updated.
3325 bool InstCombinerImpl::sinkNotIntoOtherHandOfAndOrOr(BinaryOperator &I) {
3326   Instruction::BinaryOps NewOpc;
3327   switch (I.getOpcode()) {
3328   case Instruction::And:
3329     NewOpc = Instruction::Or;
3330     break;
3331   case Instruction::Or:
3332     NewOpc = Instruction::And;
3333     break;
3334   default:
3335     return false;
3336   };
3337 
3338   Value *X, *Y;
3339   if (!match(&I, m_c_BinOp(m_Not(m_Value(X)), m_Value(Y))))
3340     return false;
3341 
3342   // Will we be able to fold the `not` into Y eventually?
3343   if (!InstCombiner::isFreeToInvert(Y, Y->hasOneUse()))
3344     return false;
3345 
3346   // And can our users be adapted?
3347   if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
3348     return false;
3349 
3350   Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3351   Value *NewBinOp =
3352       BinaryOperator::Create(NewOpc, X, NotY, I.getName() + ".not");
3353   Builder.Insert(NewBinOp);
3354   replaceInstUsesWith(I, NewBinOp);
3355   // We can not just create an outer `not`, it will most likely be immediately
3356   // folded back, reconstructing our initial pattern, and causing an
3357   // infinite combine loop, so immediately manually fold it away.
3358   freelyInvertAllUsersOf(NewBinOp);
3359   return true;
3360 }
3361 
3362 Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
3363   Value *NotOp;
3364   if (!match(&I, m_Not(m_Value(NotOp))))
3365     return nullptr;
3366 
3367   // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand.
3368   // We must eliminate the and/or (one-use) for these transforms to not increase
3369   // the instruction count.
3370   //
3371   // ~(~X & Y) --> (X | ~Y)
3372   // ~(Y & ~X) --> (X | ~Y)
3373   //
3374   // Note: The logical matches do not check for the commuted patterns because
3375   //       those are handled via SimplifySelectsFeedingBinaryOp().
3376   Type *Ty = I.getType();
3377   Value *X, *Y;
3378   if (match(NotOp, m_OneUse(m_c_And(m_Not(m_Value(X)), m_Value(Y))))) {
3379     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3380     return BinaryOperator::CreateOr(X, NotY);
3381   }
3382   if (match(NotOp, m_OneUse(m_LogicalAnd(m_Not(m_Value(X)), m_Value(Y))))) {
3383     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3384     return SelectInst::Create(X, ConstantInt::getTrue(Ty), NotY);
3385   }
3386 
3387   // ~(~X | Y) --> (X & ~Y)
3388   // ~(Y | ~X) --> (X & ~Y)
3389   if (match(NotOp, m_OneUse(m_c_Or(m_Not(m_Value(X)), m_Value(Y))))) {
3390     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3391     return BinaryOperator::CreateAnd(X, NotY);
3392   }
3393   if (match(NotOp, m_OneUse(m_LogicalOr(m_Not(m_Value(X)), m_Value(Y))))) {
3394     Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3395     return SelectInst::Create(X, NotY, ConstantInt::getFalse(Ty));
3396   }
3397 
3398   // Is this a 'not' (~) fed by a binary operator?
3399   BinaryOperator *NotVal;
3400   if (match(NotOp, m_BinOp(NotVal))) {
3401     if (NotVal->getOpcode() == Instruction::And ||
3402         NotVal->getOpcode() == Instruction::Or) {
3403       // Apply DeMorgan's Law when inverts are free:
3404       // ~(X & Y) --> (~X | ~Y)
3405       // ~(X | Y) --> (~X & ~Y)
3406       if (isFreeToInvert(NotVal->getOperand(0),
3407                          NotVal->getOperand(0)->hasOneUse()) &&
3408           isFreeToInvert(NotVal->getOperand(1),
3409                          NotVal->getOperand(1)->hasOneUse())) {
3410         Value *NotX = Builder.CreateNot(NotVal->getOperand(0), "notlhs");
3411         Value *NotY = Builder.CreateNot(NotVal->getOperand(1), "notrhs");
3412         if (NotVal->getOpcode() == Instruction::And)
3413           return BinaryOperator::CreateOr(NotX, NotY);
3414         return BinaryOperator::CreateAnd(NotX, NotY);
3415       }
3416     }
3417 
3418     // ~((-X) | Y) --> (X - 1) & (~Y)
3419     if (match(NotVal,
3420               m_OneUse(m_c_Or(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))) {
3421       Value *DecX = Builder.CreateAdd(X, ConstantInt::getAllOnesValue(Ty));
3422       Value *NotY = Builder.CreateNot(Y);
3423       return BinaryOperator::CreateAnd(DecX, NotY);
3424     }
3425 
3426     // ~(~X >>s Y) --> (X >>s Y)
3427     if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y))))
3428       return BinaryOperator::CreateAShr(X, Y);
3429 
3430     // If we are inverting a right-shifted constant, we may be able to eliminate
3431     // the 'not' by inverting the constant and using the opposite shift type.
3432     // Canonicalization rules ensure that only a negative constant uses 'ashr',
3433     // but we must check that in case that transform has not fired yet.
3434 
3435     // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
3436     Constant *C;
3437     if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) &&
3438         match(C, m_Negative())) {
3439       // We matched a negative constant, so propagating undef is unsafe.
3440       // Clamp undef elements to -1.
3441       Type *EltTy = Ty->getScalarType();
3442       C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
3443       return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y);
3444     }
3445 
3446     // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
3447     if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) &&
3448         match(C, m_NonNegative())) {
3449       // We matched a non-negative constant, so propagating undef is unsafe.
3450       // Clamp undef elements to 0.
3451       Type *EltTy = Ty->getScalarType();
3452       C = Constant::replaceUndefsWith(C, ConstantInt::getNullValue(EltTy));
3453       return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y);
3454     }
3455 
3456     // ~(X + C) --> ~C - X
3457     if (match(NotVal, m_c_Add(m_Value(X), m_ImmConstant(C))))
3458       return BinaryOperator::CreateSub(ConstantExpr::getNot(C), X);
3459 
3460     // ~(X - Y) --> ~X + Y
3461     // FIXME: is it really beneficial to sink the `not` here?
3462     if (match(NotVal, m_Sub(m_Value(X), m_Value(Y))))
3463       if (isa<Constant>(X) || NotVal->hasOneUse())
3464         return BinaryOperator::CreateAdd(Builder.CreateNot(X), Y);
3465 
3466     // ~(~X + Y) --> X - Y
3467     if (match(NotVal, m_c_Add(m_Not(m_Value(X)), m_Value(Y))))
3468       return BinaryOperator::CreateWithCopiedFlags(Instruction::Sub, X, Y,
3469                                                    NotVal);
3470   }
3471 
3472   // not (cmp A, B) = !cmp A, B
3473   CmpInst::Predicate Pred;
3474   if (match(NotOp, m_OneUse(m_Cmp(Pred, m_Value(), m_Value())))) {
3475     cast<CmpInst>(NotOp)->setPredicate(CmpInst::getInversePredicate(Pred));
3476     return replaceInstUsesWith(I, NotOp);
3477   }
3478 
3479   // Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max:
3480   // ~min(~X, ~Y) --> max(X, Y)
3481   // ~max(~X, Y) --> min(X, ~Y)
3482   auto *II = dyn_cast<IntrinsicInst>(NotOp);
3483   if (II && II->hasOneUse()) {
3484     if (match(NotOp, m_MaxOrMin(m_Value(X), m_Value(Y))) &&
3485         isFreeToInvert(X, X->hasOneUse()) &&
3486         isFreeToInvert(Y, Y->hasOneUse())) {
3487       Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3488       Value *NotX = Builder.CreateNot(X);
3489       Value *NotY = Builder.CreateNot(Y);
3490       Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, NotX, NotY);
3491       return replaceInstUsesWith(I, InvMaxMin);
3492     }
3493     if (match(NotOp, m_c_MaxOrMin(m_Not(m_Value(X)), m_Value(Y)))) {
3494       Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3495       Value *NotY = Builder.CreateNot(Y);
3496       Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotY);
3497       return replaceInstUsesWith(I, InvMaxMin);
3498     }
3499   }
3500 
3501   if (NotOp->hasOneUse()) {
3502     // Pull 'not' into operands of select if both operands are one-use compares
3503     // or one is one-use compare and the other one is a constant.
3504     // Inverting the predicates eliminates the 'not' operation.
3505     // Example:
3506     //   not (select ?, (cmp TPred, ?, ?), (cmp FPred, ?, ?) -->
3507     //     select ?, (cmp InvTPred, ?, ?), (cmp InvFPred, ?, ?)
3508     //   not (select ?, (cmp TPred, ?, ?), true -->
3509     //     select ?, (cmp InvTPred, ?, ?), false
3510     if (auto *Sel = dyn_cast<SelectInst>(NotOp)) {
3511       Value *TV = Sel->getTrueValue();
3512       Value *FV = Sel->getFalseValue();
3513       auto *CmpT = dyn_cast<CmpInst>(TV);
3514       auto *CmpF = dyn_cast<CmpInst>(FV);
3515       bool InvertibleT = (CmpT && CmpT->hasOneUse()) || isa<Constant>(TV);
3516       bool InvertibleF = (CmpF && CmpF->hasOneUse()) || isa<Constant>(FV);
3517       if (InvertibleT && InvertibleF) {
3518         if (CmpT)
3519           CmpT->setPredicate(CmpT->getInversePredicate());
3520         else
3521           Sel->setTrueValue(ConstantExpr::getNot(cast<Constant>(TV)));
3522         if (CmpF)
3523           CmpF->setPredicate(CmpF->getInversePredicate());
3524         else
3525           Sel->setFalseValue(ConstantExpr::getNot(cast<Constant>(FV)));
3526         return replaceInstUsesWith(I, Sel);
3527       }
3528     }
3529   }
3530 
3531   if (Instruction *NewXor = sinkNotIntoXor(I, Builder))
3532     return NewXor;
3533 
3534   return nullptr;
3535 }
3536 
3537 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
3538 // here. We should standardize that construct where it is needed or choose some
3539 // other way to ensure that commutated variants of patterns are not missed.
3540 Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
3541   if (Value *V = simplifyXorInst(I.getOperand(0), I.getOperand(1),
3542                                  SQ.getWithInstruction(&I)))
3543     return replaceInstUsesWith(I, V);
3544 
3545   if (SimplifyAssociativeOrCommutative(I))
3546     return &I;
3547 
3548   if (Instruction *X = foldVectorBinop(I))
3549     return X;
3550 
3551   if (Instruction *Phi = foldBinopWithPhiOperands(I))
3552     return Phi;
3553 
3554   if (Instruction *NewXor = foldXorToXor(I, Builder))
3555     return NewXor;
3556 
3557   // (A&B)^(A&C) -> A&(B^C) etc
3558   if (Value *V = SimplifyUsingDistributiveLaws(I))
3559     return replaceInstUsesWith(I, V);
3560 
3561   // See if we can simplify any instructions used by the instruction whose sole
3562   // purpose is to compute bits we don't care about.
3563   if (SimplifyDemandedInstructionBits(I))
3564     return &I;
3565 
3566   if (Value *V = SimplifyBSwap(I, Builder))
3567     return replaceInstUsesWith(I, V);
3568 
3569   if (Instruction *R = foldNot(I))
3570     return R;
3571 
3572   // Fold (X & M) ^ (Y & ~M) -> (X & M) | (Y & ~M)
3573   // This it a special case in haveNoCommonBitsSet, but the computeKnownBits
3574   // calls in there are unnecessary as SimplifyDemandedInstructionBits should
3575   // have already taken care of those cases.
3576   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3577   Value *M;
3578   if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()),
3579                         m_c_And(m_Deferred(M), m_Value()))))
3580     return BinaryOperator::CreateOr(Op0, Op1);
3581 
3582   if (Instruction *Xor = visitMaskedMerge(I, Builder))
3583     return Xor;
3584 
3585   Value *X, *Y;
3586   Constant *C1;
3587   if (match(Op1, m_Constant(C1))) {
3588     Constant *C2;
3589 
3590     if (match(Op0, m_OneUse(m_Or(m_Value(X), m_ImmConstant(C2)))) &&
3591         match(C1, m_ImmConstant())) {
3592       // (X | C2) ^ C1 --> (X & ~C2) ^ (C1^C2)
3593       C2 = Constant::replaceUndefsWith(
3594           C2, Constant::getAllOnesValue(C2->getType()->getScalarType()));
3595       Value *And = Builder.CreateAnd(
3596           X, Constant::mergeUndefsWith(ConstantExpr::getNot(C2), C1));
3597       return BinaryOperator::CreateXor(
3598           And, Constant::mergeUndefsWith(ConstantExpr::getXor(C1, C2), C1));
3599     }
3600 
3601     // Use DeMorgan and reassociation to eliminate a 'not' op.
3602     if (match(Op0, m_OneUse(m_Or(m_Not(m_Value(X)), m_Constant(C2))))) {
3603       // (~X | C2) ^ C1 --> ((X & ~C2) ^ -1) ^ C1 --> (X & ~C2) ^ ~C1
3604       Value *And = Builder.CreateAnd(X, ConstantExpr::getNot(C2));
3605       return BinaryOperator::CreateXor(And, ConstantExpr::getNot(C1));
3606     }
3607     if (match(Op0, m_OneUse(m_And(m_Not(m_Value(X)), m_Constant(C2))))) {
3608       // (~X & C2) ^ C1 --> ((X | ~C2) ^ -1) ^ C1 --> (X | ~C2) ^ ~C1
3609       Value *Or = Builder.CreateOr(X, ConstantExpr::getNot(C2));
3610       return BinaryOperator::CreateXor(Or, ConstantExpr::getNot(C1));
3611     }
3612 
3613     // Convert xor ([trunc] (ashr X, BW-1)), C =>
3614     //   select(X >s -1, C, ~C)
3615     // The ashr creates "AllZeroOrAllOne's", which then optionally inverses the
3616     // constant depending on whether this input is less than 0.
3617     const APInt *CA;
3618     if (match(Op0, m_OneUse(m_TruncOrSelf(
3619                        m_AShr(m_Value(X), m_APIntAllowUndef(CA))))) &&
3620         *CA == X->getType()->getScalarSizeInBits() - 1 &&
3621         !match(C1, m_AllOnes())) {
3622       assert(!C1->isZeroValue() && "Unexpected xor with 0");
3623       Value *IsNotNeg = Builder.CreateIsNotNeg(X);
3624       return SelectInst::Create(IsNotNeg, Op1, Builder.CreateNot(Op1));
3625     }
3626   }
3627 
3628   Type *Ty = I.getType();
3629   {
3630     const APInt *RHSC;
3631     if (match(Op1, m_APInt(RHSC))) {
3632       Value *X;
3633       const APInt *C;
3634       // (C - X) ^ signmaskC --> (C + signmaskC) - X
3635       if (RHSC->isSignMask() && match(Op0, m_Sub(m_APInt(C), m_Value(X))))
3636         return BinaryOperator::CreateSub(ConstantInt::get(Ty, *C + *RHSC), X);
3637 
3638       // (X + C) ^ signmaskC --> X + (C + signmaskC)
3639       if (RHSC->isSignMask() && match(Op0, m_Add(m_Value(X), m_APInt(C))))
3640         return BinaryOperator::CreateAdd(X, ConstantInt::get(Ty, *C + *RHSC));
3641 
3642       // (X | C) ^ RHSC --> X ^ (C ^ RHSC) iff X & C == 0
3643       if (match(Op0, m_Or(m_Value(X), m_APInt(C))) &&
3644           MaskedValueIsZero(X, *C, 0, &I))
3645         return BinaryOperator::CreateXor(X, ConstantInt::get(Ty, *C ^ *RHSC));
3646 
3647       // If RHSC is inverting the remaining bits of shifted X,
3648       // canonicalize to a 'not' before the shift to help SCEV and codegen:
3649       // (X << C) ^ RHSC --> ~X << C
3650       if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_APInt(C)))) &&
3651           *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).shl(*C)) {
3652         Value *NotX = Builder.CreateNot(X);
3653         return BinaryOperator::CreateShl(NotX, ConstantInt::get(Ty, *C));
3654       }
3655       // (X >>u C) ^ RHSC --> ~X >>u C
3656       if (match(Op0, m_OneUse(m_LShr(m_Value(X), m_APInt(C)))) &&
3657           *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).lshr(*C)) {
3658         Value *NotX = Builder.CreateNot(X);
3659         return BinaryOperator::CreateLShr(NotX, ConstantInt::get(Ty, *C));
3660       }
3661       // TODO: We could handle 'ashr' here as well. That would be matching
3662       //       a 'not' op and moving it before the shift. Doing that requires
3663       //       preventing the inverse fold in canShiftBinOpWithConstantRHS().
3664     }
3665   }
3666 
3667   // FIXME: This should not be limited to scalar (pull into APInt match above).
3668   {
3669     Value *X;
3670     ConstantInt *C1, *C2, *C3;
3671     // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
3672     if (match(Op1, m_ConstantInt(C3)) &&
3673         match(Op0, m_LShr(m_Xor(m_Value(X), m_ConstantInt(C1)),
3674                           m_ConstantInt(C2))) &&
3675         Op0->hasOneUse()) {
3676       // fold (C1 >> C2) ^ C3
3677       APInt FoldConst = C1->getValue().lshr(C2->getValue());
3678       FoldConst ^= C3->getValue();
3679       // Prepare the two operands.
3680       auto *Opnd0 = Builder.CreateLShr(X, C2);
3681       Opnd0->takeName(Op0);
3682       return BinaryOperator::CreateXor(Opnd0, ConstantInt::get(Ty, FoldConst));
3683     }
3684   }
3685 
3686   if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
3687     return FoldedLogic;
3688 
3689   // Y ^ (X | Y) --> X & ~Y
3690   // Y ^ (Y | X) --> X & ~Y
3691   if (match(Op1, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op0)))))
3692     return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op0));
3693   // (X | Y) ^ Y --> X & ~Y
3694   // (Y | X) ^ Y --> X & ~Y
3695   if (match(Op0, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op1)))))
3696     return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op1));
3697 
3698   // Y ^ (X & Y) --> ~X & Y
3699   // Y ^ (Y & X) --> ~X & Y
3700   if (match(Op1, m_OneUse(m_c_And(m_Value(X), m_Specific(Op0)))))
3701     return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(X));
3702   // (X & Y) ^ Y --> ~X & Y
3703   // (Y & X) ^ Y --> ~X & Y
3704   // Canonical form is (X & C) ^ C; don't touch that.
3705   // TODO: A 'not' op is better for analysis and codegen, but demanded bits must
3706   //       be fixed to prefer that (otherwise we get infinite looping).
3707   if (!match(Op1, m_Constant()) &&
3708       match(Op0, m_OneUse(m_c_And(m_Value(X), m_Specific(Op1)))))
3709     return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(X));
3710 
3711   Value *A, *B, *C;
3712   // (A ^ B) ^ (A | C) --> (~A & C) ^ B -- There are 4 commuted variants.
3713   if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))),
3714                         m_OneUse(m_c_Or(m_Deferred(A), m_Value(C))))))
3715       return BinaryOperator::CreateXor(
3716           Builder.CreateAnd(Builder.CreateNot(A), C), B);
3717 
3718   // (A ^ B) ^ (B | C) --> (~B & C) ^ A -- There are 4 commuted variants.
3719   if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))),
3720                         m_OneUse(m_c_Or(m_Deferred(B), m_Value(C))))))
3721       return BinaryOperator::CreateXor(
3722           Builder.CreateAnd(Builder.CreateNot(B), C), A);
3723 
3724   // (A & B) ^ (A ^ B) -> (A | B)
3725   if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
3726       match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
3727     return BinaryOperator::CreateOr(A, B);
3728   // (A ^ B) ^ (A & B) -> (A | B)
3729   if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
3730       match(Op1, m_c_And(m_Specific(A), m_Specific(B))))
3731     return BinaryOperator::CreateOr(A, B);
3732 
3733   // (A & ~B) ^ ~A -> ~(A & B)
3734   // (~B & A) ^ ~A -> ~(A & B)
3735   if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
3736       match(Op1, m_Not(m_Specific(A))))
3737     return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
3738 
3739   // (~A & B) ^ A --> A | B -- There are 4 commuted variants.
3740   if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(A)), m_Value(B)), m_Deferred(A))))
3741     return BinaryOperator::CreateOr(A, B);
3742 
3743   // (~A | B) ^ A --> ~(A & B)
3744   if (match(Op0, m_OneUse(m_c_Or(m_Not(m_Specific(Op1)), m_Value(B)))))
3745     return BinaryOperator::CreateNot(Builder.CreateAnd(Op1, B));
3746 
3747   // A ^ (~A | B) --> ~(A & B)
3748   if (match(Op1, m_OneUse(m_c_Or(m_Not(m_Specific(Op0)), m_Value(B)))))
3749     return BinaryOperator::CreateNot(Builder.CreateAnd(Op0, B));
3750 
3751   // (A | B) ^ (A | C) --> (B ^ C) & ~A -- There are 4 commuted variants.
3752   // TODO: Loosen one-use restriction if common operand is a constant.
3753   Value *D;
3754   if (match(Op0, m_OneUse(m_Or(m_Value(A), m_Value(B)))) &&
3755       match(Op1, m_OneUse(m_Or(m_Value(C), m_Value(D))))) {
3756     if (B == C || B == D)
3757       std::swap(A, B);
3758     if (A == C)
3759       std::swap(C, D);
3760     if (A == D) {
3761       Value *NotA = Builder.CreateNot(A);
3762       return BinaryOperator::CreateAnd(Builder.CreateXor(B, C), NotA);
3763     }
3764   }
3765 
3766   if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
3767     if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
3768       if (Value *V = foldXorOfICmps(LHS, RHS, I))
3769         return replaceInstUsesWith(I, V);
3770 
3771   if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
3772     return CastedXor;
3773 
3774   if (Instruction *Abs = canonicalizeAbs(I, Builder))
3775     return Abs;
3776 
3777   // Otherwise, if all else failed, try to hoist the xor-by-constant:
3778   //   (X ^ C) ^ Y --> (X ^ Y) ^ C
3779   // Just like we do in other places, we completely avoid the fold
3780   // for constantexprs, at least to avoid endless combine loop.
3781   if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_CombineAnd(m_Value(X),
3782                                                     m_Unless(m_ConstantExpr())),
3783                                        m_ImmConstant(C1))),
3784                         m_Value(Y))))
3785     return BinaryOperator::CreateXor(Builder.CreateXor(X, Y), C1);
3786 
3787   return nullptr;
3788 }
3789