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