xref: /llvm-project/llvm/lib/Analysis/InstructionSimplify.cpp (revision cc995ad064ffe22566270fe95e974a368c71ba22)
1 //===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
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 routines for folding instructions into simpler forms
10 // that do not require creating new instructions.  This does constant folding
11 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13 // ("and i32 %x, %x" -> "%x").  All operands are assumed to have already been
14 // simplified: This is usually true and assuming it simplifies the logic (if
15 // they have not been simplified then results are correct but maybe suboptimal).
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Analysis/InstructionSimplify.h"
20 
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/Analysis/AssumptionCache.h"
26 #include "llvm/Analysis/CaptureTracking.h"
27 #include "llvm/Analysis/CmpInstAnalysis.h"
28 #include "llvm/Analysis/ConstantFolding.h"
29 #include "llvm/Analysis/InstSimplifyFolder.h"
30 #include "llvm/Analysis/LoopAnalysisManager.h"
31 #include "llvm/Analysis/MemoryBuiltins.h"
32 #include "llvm/Analysis/OverflowInstAnalysis.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/Analysis/VectorUtils.h"
36 #include "llvm/IR/ConstantRange.h"
37 #include "llvm/IR/DataLayout.h"
38 #include "llvm/IR/Dominators.h"
39 #include "llvm/IR/InstrTypes.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/Operator.h"
42 #include "llvm/IR/PatternMatch.h"
43 #include "llvm/IR/Statepoint.h"
44 #include "llvm/Support/KnownBits.h"
45 #include <algorithm>
46 #include <optional>
47 using namespace llvm;
48 using namespace llvm::PatternMatch;
49 
50 #define DEBUG_TYPE "instsimplify"
51 
52 enum { RecursionLimit = 3 };
53 
54 STATISTIC(NumExpand, "Number of expansions");
55 STATISTIC(NumReassoc, "Number of reassociations");
56 
57 static Value *simplifyAndInst(Value *, Value *, const SimplifyQuery &,
58                               unsigned);
59 static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
60 static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
61                              const SimplifyQuery &, unsigned);
62 static Value *simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
63                             unsigned);
64 static Value *simplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &,
65                             const SimplifyQuery &, unsigned);
66 static Value *simplifyCmpInst(CmpPredicate, Value *, Value *,
67                               const SimplifyQuery &, unsigned);
68 static Value *simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
69                                const SimplifyQuery &Q, unsigned MaxRecurse);
70 static Value *simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
71 static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &,
72                               unsigned);
73 static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &,
74                                unsigned);
75 static Value *simplifyGEPInst(Type *, Value *, ArrayRef<Value *>,
76                               GEPNoWrapFlags, const SimplifyQuery &, unsigned);
77 static Value *simplifySelectInst(Value *, Value *, Value *,
78                                  const SimplifyQuery &, unsigned);
79 static Value *simplifyInstructionWithOperands(Instruction *I,
80                                               ArrayRef<Value *> NewOps,
81                                               const SimplifyQuery &SQ,
82                                               unsigned MaxRecurse);
83 
84 /// For a boolean type or a vector of boolean type, return false or a vector
85 /// with every element false.
86 static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); }
87 
88 /// For a boolean type or a vector of boolean type, return true or a vector
89 /// with every element true.
90 static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); }
91 
92 /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
93 static bool isSameCompare(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS) {
94   CmpInst *Cmp = dyn_cast<CmpInst>(V);
95   if (!Cmp)
96     return false;
97   CmpInst::Predicate CPred = Cmp->getPredicate();
98   Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
99   if (CPred == Pred && CLHS == LHS && CRHS == RHS)
100     return true;
101   return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
102          CRHS == LHS;
103 }
104 
105 /// Simplify comparison with true or false branch of select:
106 ///  %sel = select i1 %cond, i32 %tv, i32 %fv
107 ///  %cmp = icmp sle i32 %sel, %rhs
108 /// Compose new comparison by substituting %sel with either %tv or %fv
109 /// and see if it simplifies.
110 static Value *simplifyCmpSelCase(CmpPredicate Pred, Value *LHS, Value *RHS,
111                                  Value *Cond, const SimplifyQuery &Q,
112                                  unsigned MaxRecurse, Constant *TrueOrFalse) {
113   Value *SimplifiedCmp = simplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse);
114   if (SimplifiedCmp == Cond) {
115     // %cmp simplified to the select condition (%cond).
116     return TrueOrFalse;
117   } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)) {
118     // It didn't simplify. However, if composed comparison is equivalent
119     // to the select condition (%cond) then we can replace it.
120     return TrueOrFalse;
121   }
122   return SimplifiedCmp;
123 }
124 
125 /// Simplify comparison with true branch of select
126 static Value *simplifyCmpSelTrueCase(CmpPredicate Pred, Value *LHS, Value *RHS,
127                                      Value *Cond, const SimplifyQuery &Q,
128                                      unsigned MaxRecurse) {
129   return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
130                             getTrue(Cond->getType()));
131 }
132 
133 /// Simplify comparison with false branch of select
134 static Value *simplifyCmpSelFalseCase(CmpPredicate Pred, Value *LHS, Value *RHS,
135                                       Value *Cond, const SimplifyQuery &Q,
136                                       unsigned MaxRecurse) {
137   return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
138                             getFalse(Cond->getType()));
139 }
140 
141 /// We know comparison with both branches of select can be simplified, but they
142 /// are not equal. This routine handles some logical simplifications.
143 static Value *handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp,
144                                                Value *Cond,
145                                                const SimplifyQuery &Q,
146                                                unsigned MaxRecurse) {
147   // If the false value simplified to false, then the result of the compare
148   // is equal to "Cond && TCmp".  This also catches the case when the false
149   // value simplified to false and the true value to true, returning "Cond".
150   // Folding select to and/or isn't poison-safe in general; impliesPoison
151   // checks whether folding it does not convert a well-defined value into
152   // poison.
153   if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond))
154     if (Value *V = simplifyAndInst(Cond, TCmp, Q, MaxRecurse))
155       return V;
156   // If the true value simplified to true, then the result of the compare
157   // is equal to "Cond || FCmp".
158   if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond))
159     if (Value *V = simplifyOrInst(Cond, FCmp, Q, MaxRecurse))
160       return V;
161   // Finally, if the false value simplified to true and the true value to
162   // false, then the result of the compare is equal to "!Cond".
163   if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
164     if (Value *V = simplifyXorInst(
165             Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
166       return V;
167   return nullptr;
168 }
169 
170 /// Does the given value dominate the specified phi node?
171 static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
172   Instruction *I = dyn_cast<Instruction>(V);
173   if (!I)
174     // Arguments and constants dominate all instructions.
175     return true;
176 
177   // If we have a DominatorTree then do a precise test.
178   if (DT)
179     return DT->dominates(I, P);
180 
181   // Otherwise, if the instruction is in the entry block and is not an invoke,
182   // then it obviously dominates all phi nodes.
183   if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
184       !isa<CallBrInst>(I))
185     return true;
186 
187   return false;
188 }
189 
190 /// Try to simplify a binary operator of form "V op OtherOp" where V is
191 /// "(B0 opex B1)" by distributing 'op' across 'opex' as
192 /// "(B0 op OtherOp) opex (B1 op OtherOp)".
193 static Value *expandBinOp(Instruction::BinaryOps Opcode, Value *V,
194                           Value *OtherOp, Instruction::BinaryOps OpcodeToExpand,
195                           const SimplifyQuery &Q, unsigned MaxRecurse) {
196   auto *B = dyn_cast<BinaryOperator>(V);
197   if (!B || B->getOpcode() != OpcodeToExpand)
198     return nullptr;
199   Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
200   Value *L =
201       simplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse);
202   if (!L)
203     return nullptr;
204   Value *R =
205       simplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse);
206   if (!R)
207     return nullptr;
208 
209   // Does the expanded pair of binops simplify to the existing binop?
210   if ((L == B0 && R == B1) ||
211       (Instruction::isCommutative(OpcodeToExpand) && L == B1 && R == B0)) {
212     ++NumExpand;
213     return B;
214   }
215 
216   // Otherwise, return "L op' R" if it simplifies.
217   Value *S = simplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse);
218   if (!S)
219     return nullptr;
220 
221   ++NumExpand;
222   return S;
223 }
224 
225 /// Try to simplify binops of form "A op (B op' C)" or the commuted variant by
226 /// distributing op over op'.
227 static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L,
228                                      Value *R,
229                                      Instruction::BinaryOps OpcodeToExpand,
230                                      const SimplifyQuery &Q,
231                                      unsigned MaxRecurse) {
232   // Recursion is always used, so bail out at once if we already hit the limit.
233   if (!MaxRecurse--)
234     return nullptr;
235 
236   if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse))
237     return V;
238   if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse))
239     return V;
240   return nullptr;
241 }
242 
243 /// Generic simplifications for associative binary operations.
244 /// Returns the simpler value, or null if none was found.
245 static Value *simplifyAssociativeBinOp(Instruction::BinaryOps Opcode,
246                                        Value *LHS, Value *RHS,
247                                        const SimplifyQuery &Q,
248                                        unsigned MaxRecurse) {
249   assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
250 
251   // Recursion is always used, so bail out at once if we already hit the limit.
252   if (!MaxRecurse--)
253     return nullptr;
254 
255   BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
256   BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
257 
258   // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
259   if (Op0 && Op0->getOpcode() == Opcode) {
260     Value *A = Op0->getOperand(0);
261     Value *B = Op0->getOperand(1);
262     Value *C = RHS;
263 
264     // Does "B op C" simplify?
265     if (Value *V = simplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
266       // It does!  Return "A op V" if it simplifies or is already available.
267       // If V equals B then "A op V" is just the LHS.
268       if (V == B)
269         return LHS;
270       // Otherwise return "A op V" if it simplifies.
271       if (Value *W = simplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
272         ++NumReassoc;
273         return W;
274       }
275     }
276   }
277 
278   // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
279   if (Op1 && Op1->getOpcode() == Opcode) {
280     Value *A = LHS;
281     Value *B = Op1->getOperand(0);
282     Value *C = Op1->getOperand(1);
283 
284     // Does "A op B" simplify?
285     if (Value *V = simplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
286       // It does!  Return "V op C" if it simplifies or is already available.
287       // If V equals B then "V op C" is just the RHS.
288       if (V == B)
289         return RHS;
290       // Otherwise return "V op C" if it simplifies.
291       if (Value *W = simplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
292         ++NumReassoc;
293         return W;
294       }
295     }
296   }
297 
298   // The remaining transforms require commutativity as well as associativity.
299   if (!Instruction::isCommutative(Opcode))
300     return nullptr;
301 
302   // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
303   if (Op0 && Op0->getOpcode() == Opcode) {
304     Value *A = Op0->getOperand(0);
305     Value *B = Op0->getOperand(1);
306     Value *C = RHS;
307 
308     // Does "C op A" simplify?
309     if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
310       // It does!  Return "V op B" if it simplifies or is already available.
311       // If V equals A then "V op B" is just the LHS.
312       if (V == A)
313         return LHS;
314       // Otherwise return "V op B" if it simplifies.
315       if (Value *W = simplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
316         ++NumReassoc;
317         return W;
318       }
319     }
320   }
321 
322   // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
323   if (Op1 && Op1->getOpcode() == Opcode) {
324     Value *A = LHS;
325     Value *B = Op1->getOperand(0);
326     Value *C = Op1->getOperand(1);
327 
328     // Does "C op A" simplify?
329     if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
330       // It does!  Return "B op V" if it simplifies or is already available.
331       // If V equals C then "B op V" is just the RHS.
332       if (V == C)
333         return RHS;
334       // Otherwise return "B op V" if it simplifies.
335       if (Value *W = simplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
336         ++NumReassoc;
337         return W;
338       }
339     }
340   }
341 
342   return nullptr;
343 }
344 
345 /// In the case of a binary operation with a select instruction as an operand,
346 /// try to simplify the binop by seeing whether evaluating it on both branches
347 /// of the select results in the same value. Returns the common value if so,
348 /// otherwise returns null.
349 static Value *threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS,
350                                     Value *RHS, const SimplifyQuery &Q,
351                                     unsigned MaxRecurse) {
352   // Recursion is always used, so bail out at once if we already hit the limit.
353   if (!MaxRecurse--)
354     return nullptr;
355 
356   SelectInst *SI;
357   if (isa<SelectInst>(LHS)) {
358     SI = cast<SelectInst>(LHS);
359   } else {
360     assert(isa<SelectInst>(RHS) && "No select instruction operand!");
361     SI = cast<SelectInst>(RHS);
362   }
363 
364   // Evaluate the BinOp on the true and false branches of the select.
365   Value *TV;
366   Value *FV;
367   if (SI == LHS) {
368     TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
369     FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
370   } else {
371     TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
372     FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
373   }
374 
375   // If they simplified to the same value, then return the common value.
376   // If they both failed to simplify then return null.
377   if (TV == FV)
378     return TV;
379 
380   // If one branch simplified to undef, return the other one.
381   if (TV && Q.isUndefValue(TV))
382     return FV;
383   if (FV && Q.isUndefValue(FV))
384     return TV;
385 
386   // If applying the operation did not change the true and false select values,
387   // then the result of the binop is the select itself.
388   if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
389     return SI;
390 
391   // If one branch simplified and the other did not, and the simplified
392   // value is equal to the unsimplified one, return the simplified value.
393   // For example, select (cond, X, X & Z) & Z -> X & Z.
394   if ((FV && !TV) || (TV && !FV)) {
395     // Check that the simplified value has the form "X op Y" where "op" is the
396     // same as the original operation.
397     Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
398     if (Simplified && Simplified->getOpcode() == unsigned(Opcode) &&
399         !Simplified->hasPoisonGeneratingFlags()) {
400       // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
401       // We already know that "op" is the same as for the simplified value.  See
402       // if the operands match too.  If so, return the simplified value.
403       Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
404       Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
405       Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
406       if (Simplified->getOperand(0) == UnsimplifiedLHS &&
407           Simplified->getOperand(1) == UnsimplifiedRHS)
408         return Simplified;
409       if (Simplified->isCommutative() &&
410           Simplified->getOperand(1) == UnsimplifiedLHS &&
411           Simplified->getOperand(0) == UnsimplifiedRHS)
412         return Simplified;
413     }
414   }
415 
416   return nullptr;
417 }
418 
419 /// In the case of a comparison with a select instruction, try to simplify the
420 /// comparison by seeing whether both branches of the select result in the same
421 /// value. Returns the common value if so, otherwise returns null.
422 /// For example, if we have:
423 ///  %tmp = select i1 %cmp, i32 1, i32 2
424 ///  %cmp1 = icmp sle i32 %tmp, 3
425 /// We can simplify %cmp1 to true, because both branches of select are
426 /// less than 3. We compose new comparison by substituting %tmp with both
427 /// branches of select and see if it can be simplified.
428 static Value *threadCmpOverSelect(CmpPredicate Pred, Value *LHS, Value *RHS,
429                                   const SimplifyQuery &Q, unsigned MaxRecurse) {
430   // Recursion is always used, so bail out at once if we already hit the limit.
431   if (!MaxRecurse--)
432     return nullptr;
433 
434   // Make sure the select is on the LHS.
435   if (!isa<SelectInst>(LHS)) {
436     std::swap(LHS, RHS);
437     Pred = CmpInst::getSwappedPredicate(Pred);
438   }
439   assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
440   SelectInst *SI = cast<SelectInst>(LHS);
441   Value *Cond = SI->getCondition();
442   Value *TV = SI->getTrueValue();
443   Value *FV = SI->getFalseValue();
444 
445   // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
446   // Does "cmp TV, RHS" simplify?
447   Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse);
448   if (!TCmp)
449     return nullptr;
450 
451   // Does "cmp FV, RHS" simplify?
452   Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse);
453   if (!FCmp)
454     return nullptr;
455 
456   // If both sides simplified to the same value, then use it as the result of
457   // the original comparison.
458   if (TCmp == FCmp)
459     return TCmp;
460 
461   // The remaining cases only make sense if the select condition has the same
462   // type as the result of the comparison, so bail out if this is not so.
463   if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
464     return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse);
465 
466   return nullptr;
467 }
468 
469 /// In the case of a binary operation with an operand that is a PHI instruction,
470 /// try to simplify the binop by seeing whether evaluating it on the incoming
471 /// phi values yields the same result for every value. If so returns the common
472 /// value, otherwise returns null.
473 static Value *threadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS,
474                                  Value *RHS, const SimplifyQuery &Q,
475                                  unsigned MaxRecurse) {
476   // Recursion is always used, so bail out at once if we already hit the limit.
477   if (!MaxRecurse--)
478     return nullptr;
479 
480   PHINode *PI;
481   if (isa<PHINode>(LHS)) {
482     PI = cast<PHINode>(LHS);
483     // Bail out if RHS and the phi may be mutually interdependent due to a loop.
484     if (!valueDominatesPHI(RHS, PI, Q.DT))
485       return nullptr;
486   } else {
487     assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
488     PI = cast<PHINode>(RHS);
489     // Bail out if LHS and the phi may be mutually interdependent due to a loop.
490     if (!valueDominatesPHI(LHS, PI, Q.DT))
491       return nullptr;
492   }
493 
494   // Evaluate the BinOp on the incoming phi values.
495   Value *CommonValue = nullptr;
496   for (Use &Incoming : PI->incoming_values()) {
497     // If the incoming value is the phi node itself, it can safely be skipped.
498     if (Incoming == PI)
499       continue;
500     Instruction *InTI = PI->getIncomingBlock(Incoming)->getTerminator();
501     Value *V = PI == LHS
502                    ? simplifyBinOp(Opcode, Incoming, RHS,
503                                    Q.getWithInstruction(InTI), MaxRecurse)
504                    : simplifyBinOp(Opcode, LHS, Incoming,
505                                    Q.getWithInstruction(InTI), MaxRecurse);
506     // If the operation failed to simplify, or simplified to a different value
507     // to previously, then give up.
508     if (!V || (CommonValue && V != CommonValue))
509       return nullptr;
510     CommonValue = V;
511   }
512 
513   return CommonValue;
514 }
515 
516 /// In the case of a comparison with a PHI instruction, try to simplify the
517 /// comparison by seeing whether comparing with all of the incoming phi values
518 /// yields the same result every time. If so returns the common result,
519 /// otherwise returns null.
520 static Value *threadCmpOverPHI(CmpPredicate Pred, Value *LHS, Value *RHS,
521                                const SimplifyQuery &Q, unsigned MaxRecurse) {
522   // Recursion is always used, so bail out at once if we already hit the limit.
523   if (!MaxRecurse--)
524     return nullptr;
525 
526   // Make sure the phi is on the LHS.
527   if (!isa<PHINode>(LHS)) {
528     std::swap(LHS, RHS);
529     Pred = CmpInst::getSwappedPredicate(Pred);
530   }
531   assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
532   PHINode *PI = cast<PHINode>(LHS);
533 
534   // Bail out if RHS and the phi may be mutually interdependent due to a loop.
535   if (!valueDominatesPHI(RHS, PI, Q.DT))
536     return nullptr;
537 
538   // Evaluate the BinOp on the incoming phi values.
539   Value *CommonValue = nullptr;
540   for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
541     Value *Incoming = PI->getIncomingValue(u);
542     Instruction *InTI = PI->getIncomingBlock(u)->getTerminator();
543     // If the incoming value is the phi node itself, it can safely be skipped.
544     if (Incoming == PI)
545       continue;
546     // Change the context instruction to the "edge" that flows into the phi.
547     // This is important because that is where incoming is actually "evaluated"
548     // even though it is used later somewhere else.
549     Value *V = simplifyCmpInst(Pred, Incoming, RHS, Q.getWithInstruction(InTI),
550                                MaxRecurse);
551     // If the operation failed to simplify, or simplified to a different value
552     // to previously, then give up.
553     if (!V || (CommonValue && V != CommonValue))
554       return nullptr;
555     CommonValue = V;
556   }
557 
558   return CommonValue;
559 }
560 
561 static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode,
562                                        Value *&Op0, Value *&Op1,
563                                        const SimplifyQuery &Q) {
564   if (auto *CLHS = dyn_cast<Constant>(Op0)) {
565     if (auto *CRHS = dyn_cast<Constant>(Op1)) {
566       switch (Opcode) {
567       default:
568         break;
569       case Instruction::FAdd:
570       case Instruction::FSub:
571       case Instruction::FMul:
572       case Instruction::FDiv:
573       case Instruction::FRem:
574         if (Q.CxtI != nullptr)
575           return ConstantFoldFPInstOperands(Opcode, CLHS, CRHS, Q.DL, Q.CxtI);
576       }
577       return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
578     }
579 
580     // Canonicalize the constant to the RHS if this is a commutative operation.
581     if (Instruction::isCommutative(Opcode))
582       std::swap(Op0, Op1);
583   }
584   return nullptr;
585 }
586 
587 /// Given operands for an Add, see if we can fold the result.
588 /// If not, this returns null.
589 static Value *simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
590                               const SimplifyQuery &Q, unsigned MaxRecurse) {
591   if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
592     return C;
593 
594   // X + poison -> poison
595   if (isa<PoisonValue>(Op1))
596     return Op1;
597 
598   // X + undef -> undef
599   if (Q.isUndefValue(Op1))
600     return Op1;
601 
602   // X + 0 -> X
603   if (match(Op1, m_Zero()))
604     return Op0;
605 
606   // If two operands are negative, return 0.
607   if (isKnownNegation(Op0, Op1))
608     return Constant::getNullValue(Op0->getType());
609 
610   // X + (Y - X) -> Y
611   // (Y - X) + X -> Y
612   // Eg: X + -X -> 0
613   Value *Y = nullptr;
614   if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
615       match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
616     return Y;
617 
618   // X + ~X -> -1   since   ~X = -X-1
619   Type *Ty = Op0->getType();
620   if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
621     return Constant::getAllOnesValue(Ty);
622 
623   // add nsw/nuw (xor Y, signmask), signmask --> Y
624   // The no-wrapping add guarantees that the top bit will be set by the add.
625   // Therefore, the xor must be clearing the already set sign bit of Y.
626   if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) &&
627       match(Op0, m_Xor(m_Value(Y), m_SignMask())))
628     return Y;
629 
630   // add nuw %x, -1  ->  -1, because %x can only be 0.
631   if (IsNUW && match(Op1, m_AllOnes()))
632     return Op1; // Which is -1.
633 
634   /// i1 add -> xor.
635   if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
636     if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
637       return V;
638 
639   // Try some generic simplifications for associative operations.
640   if (Value *V =
641           simplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse))
642     return V;
643 
644   // Threading Add over selects and phi nodes is pointless, so don't bother.
645   // Threading over the select in "A + select(cond, B, C)" means evaluating
646   // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
647   // only if B and C are equal.  If B and C are equal then (since we assume
648   // that operands have already been simplified) "select(cond, B, C)" should
649   // have been simplified to the common value of B and C already.  Analysing
650   // "A+B" and "A+C" thus gains nothing, but costs compile time.  Similarly
651   // for threading over phi nodes.
652 
653   return nullptr;
654 }
655 
656 Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
657                              const SimplifyQuery &Query) {
658   return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
659 }
660 
661 /// Compute the base pointer and cumulative constant offsets for V.
662 ///
663 /// This strips all constant offsets off of V, leaving it the base pointer, and
664 /// accumulates the total constant offset applied in the returned constant.
665 /// It returns zero if there are no constant offsets applied.
666 ///
667 /// This is very similar to stripAndAccumulateConstantOffsets(), except it
668 /// normalizes the offset bitwidth to the stripped pointer type, not the
669 /// original pointer type.
670 static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
671                                             bool AllowNonInbounds = false) {
672   assert(V->getType()->isPtrOrPtrVectorTy());
673 
674   APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
675   V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
676   // As that strip may trace through `addrspacecast`, need to sext or trunc
677   // the offset calculated.
678   return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
679 }
680 
681 /// Compute the constant difference between two pointer values.
682 /// If the difference is not a constant, returns zero.
683 static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
684                                           Value *RHS) {
685   APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
686   APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
687 
688   // If LHS and RHS are not related via constant offsets to the same base
689   // value, there is nothing we can do here.
690   if (LHS != RHS)
691     return nullptr;
692 
693   // Otherwise, the difference of LHS - RHS can be computed as:
694   //    LHS - RHS
695   //  = (LHSOffset + Base) - (RHSOffset + Base)
696   //  = LHSOffset - RHSOffset
697   Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset);
698   if (auto *VecTy = dyn_cast<VectorType>(LHS->getType()))
699     Res = ConstantVector::getSplat(VecTy->getElementCount(), Res);
700   return Res;
701 }
702 
703 /// Test if there is a dominating equivalence condition for the
704 /// two operands. If there is, try to reduce the binary operation
705 /// between the two operands.
706 /// Example: Op0 - Op1 --> 0 when Op0 == Op1
707 static Value *simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1,
708                               const SimplifyQuery &Q, unsigned MaxRecurse) {
709   // Recursive run it can not get any benefit
710   if (MaxRecurse != RecursionLimit)
711     return nullptr;
712 
713   std::optional<bool> Imp =
714       isImpliedByDomCondition(CmpInst::ICMP_EQ, Op0, Op1, Q.CxtI, Q.DL);
715   if (Imp && *Imp) {
716     Type *Ty = Op0->getType();
717     switch (Opcode) {
718     case Instruction::Sub:
719     case Instruction::Xor:
720     case Instruction::URem:
721     case Instruction::SRem:
722       return Constant::getNullValue(Ty);
723 
724     case Instruction::SDiv:
725     case Instruction::UDiv:
726       return ConstantInt::get(Ty, 1);
727 
728     case Instruction::And:
729     case Instruction::Or:
730       // Could be either one - choose Op1 since that's more likely a constant.
731       return Op1;
732     default:
733       break;
734     }
735   }
736   return nullptr;
737 }
738 
739 /// Given operands for a Sub, see if we can fold the result.
740 /// If not, this returns null.
741 static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
742                               const SimplifyQuery &Q, unsigned MaxRecurse) {
743   if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
744     return C;
745 
746   // X - poison -> poison
747   // poison - X -> poison
748   if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
749     return PoisonValue::get(Op0->getType());
750 
751   // X - undef -> undef
752   // undef - X -> undef
753   if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
754     return UndefValue::get(Op0->getType());
755 
756   // X - 0 -> X
757   if (match(Op1, m_Zero()))
758     return Op0;
759 
760   // X - X -> 0
761   if (Op0 == Op1)
762     return Constant::getNullValue(Op0->getType());
763 
764   // Is this a negation?
765   if (match(Op0, m_Zero())) {
766     // 0 - X -> 0 if the sub is NUW.
767     if (IsNUW)
768       return Constant::getNullValue(Op0->getType());
769 
770     KnownBits Known = computeKnownBits(Op1, /* Depth */ 0, Q);
771     if (Known.Zero.isMaxSignedValue()) {
772       // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
773       // Op1 must be 0 because negating the minimum signed value is undefined.
774       if (IsNSW)
775         return Constant::getNullValue(Op0->getType());
776 
777       // 0 - X -> X if X is 0 or the minimum signed value.
778       return Op1;
779     }
780   }
781 
782   // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
783   // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
784   Value *X = nullptr, *Y = nullptr, *Z = Op1;
785   if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
786     // See if "V === Y - Z" simplifies.
787     if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1))
788       // It does!  Now see if "X + V" simplifies.
789       if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) {
790         // It does, we successfully reassociated!
791         ++NumReassoc;
792         return W;
793       }
794     // See if "V === X - Z" simplifies.
795     if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
796       // It does!  Now see if "Y + V" simplifies.
797       if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) {
798         // It does, we successfully reassociated!
799         ++NumReassoc;
800         return W;
801       }
802   }
803 
804   // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
805   // For example, X - (X + 1) -> -1
806   X = Op0;
807   if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
808     // See if "V === X - Y" simplifies.
809     if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
810       // It does!  Now see if "V - Z" simplifies.
811       if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) {
812         // It does, we successfully reassociated!
813         ++NumReassoc;
814         return W;
815       }
816     // See if "V === X - Z" simplifies.
817     if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
818       // It does!  Now see if "V - Y" simplifies.
819       if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) {
820         // It does, we successfully reassociated!
821         ++NumReassoc;
822         return W;
823       }
824   }
825 
826   // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
827   // For example, X - (X - Y) -> Y.
828   Z = Op0;
829   if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
830     // See if "V === Z - X" simplifies.
831     if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1))
832       // It does!  Now see if "V + Y" simplifies.
833       if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) {
834         // It does, we successfully reassociated!
835         ++NumReassoc;
836         return W;
837       }
838 
839   // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
840   if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
841       match(Op1, m_Trunc(m_Value(Y))))
842     if (X->getType() == Y->getType())
843       // See if "V === X - Y" simplifies.
844       if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
845         // It does!  Now see if "trunc V" simplifies.
846         if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(),
847                                         Q, MaxRecurse - 1))
848           // It does, return the simplified "trunc V".
849           return W;
850 
851   // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
852   if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y))))
853     if (Constant *Result = computePointerDifference(Q.DL, X, Y))
854       return ConstantFoldIntegerCast(Result, Op0->getType(), /*IsSigned*/ true,
855                                      Q.DL);
856 
857   // i1 sub -> xor.
858   if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
859     if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
860       return V;
861 
862   // Threading Sub over selects and phi nodes is pointless, so don't bother.
863   // Threading over the select in "A - select(cond, B, C)" means evaluating
864   // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
865   // only if B and C are equal.  If B and C are equal then (since we assume
866   // that operands have already been simplified) "select(cond, B, C)" should
867   // have been simplified to the common value of B and C already.  Analysing
868   // "A-B" and "A-C" thus gains nothing, but costs compile time.  Similarly
869   // for threading over phi nodes.
870 
871   if (Value *V = simplifyByDomEq(Instruction::Sub, Op0, Op1, Q, MaxRecurse))
872     return V;
873 
874   // (sub nuw C_Mask, (xor X, C_Mask)) -> X
875   if (IsNUW) {
876     Value *X;
877     if (match(Op1, m_Xor(m_Value(X), m_Specific(Op0))) &&
878         match(Op0, m_LowBitMask()))
879       return X;
880   }
881 
882   return nullptr;
883 }
884 
885 Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
886                              const SimplifyQuery &Q) {
887   return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
888 }
889 
890 /// Given operands for a Mul, see if we can fold the result.
891 /// If not, this returns null.
892 static Value *simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
893                               const SimplifyQuery &Q, unsigned MaxRecurse) {
894   if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
895     return C;
896 
897   // X * poison -> poison
898   if (isa<PoisonValue>(Op1))
899     return Op1;
900 
901   // X * undef -> 0
902   // X * 0 -> 0
903   if (Q.isUndefValue(Op1) || match(Op1, m_Zero()))
904     return Constant::getNullValue(Op0->getType());
905 
906   // X * 1 -> X
907   if (match(Op1, m_One()))
908     return Op0;
909 
910   // (X / Y) * Y -> X if the division is exact.
911   Value *X = nullptr;
912   if (Q.IIQ.UseInstrInfo &&
913       (match(Op0,
914              m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) ||     // (X / Y) * Y
915        match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
916     return X;
917 
918    if (Op0->getType()->isIntOrIntVectorTy(1)) {
919     // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not
920     // representable). All other cases reduce to 0, so just return 0.
921     if (IsNSW)
922       return ConstantInt::getNullValue(Op0->getType());
923 
924     // Treat "mul i1" as "and i1".
925     if (MaxRecurse)
926       if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
927         return V;
928   }
929 
930   // Try some generic simplifications for associative operations.
931   if (Value *V =
932           simplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
933     return V;
934 
935   // Mul distributes over Add. Try some generic simplifications based on this.
936   if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1,
937                                         Instruction::Add, Q, MaxRecurse))
938     return V;
939 
940   // If the operation is with the result of a select instruction, check whether
941   // operating on either branch of the select always yields the same value.
942   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
943     if (Value *V =
944             threadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
945       return V;
946 
947   // If the operation is with the result of a phi instruction, check whether
948   // operating on all incoming values of the phi always yields the same value.
949   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
950     if (Value *V =
951             threadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
952       return V;
953 
954   return nullptr;
955 }
956 
957 Value *llvm::simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
958                              const SimplifyQuery &Q) {
959   return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
960 }
961 
962 /// Given a predicate and two operands, return true if the comparison is true.
963 /// This is a helper for div/rem simplification where we return some other value
964 /// when we can prove a relationship between the operands.
965 static bool isICmpTrue(CmpPredicate Pred, Value *LHS, Value *RHS,
966                        const SimplifyQuery &Q, unsigned MaxRecurse) {
967   Value *V = simplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
968   Constant *C = dyn_cast_or_null<Constant>(V);
969   return (C && C->isAllOnesValue());
970 }
971 
972 /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
973 /// to simplify X % Y to X.
974 static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
975                       unsigned MaxRecurse, bool IsSigned) {
976   // Recursion is always used, so bail out at once if we already hit the limit.
977   if (!MaxRecurse--)
978     return false;
979 
980   if (IsSigned) {
981     // (X srem Y) sdiv Y --> 0
982     if (match(X, m_SRem(m_Value(), m_Specific(Y))))
983       return true;
984 
985     // |X| / |Y| --> 0
986     //
987     // We require that 1 operand is a simple constant. That could be extended to
988     // 2 variables if we computed the sign bit for each.
989     //
990     // Make sure that a constant is not the minimum signed value because taking
991     // the abs() of that is undefined.
992     Type *Ty = X->getType();
993     const APInt *C;
994     if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
995       // Is the variable divisor magnitude always greater than the constant
996       // dividend magnitude?
997       // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
998       Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
999       Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
1000       if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
1001           isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
1002         return true;
1003     }
1004     if (match(Y, m_APInt(C))) {
1005       // Special-case: we can't take the abs() of a minimum signed value. If
1006       // that's the divisor, then all we have to do is prove that the dividend
1007       // is also not the minimum signed value.
1008       if (C->isMinSignedValue())
1009         return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
1010 
1011       // Is the variable dividend magnitude always less than the constant
1012       // divisor magnitude?
1013       // |X| < |C| --> X > -abs(C) and X < abs(C)
1014       Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
1015       Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
1016       if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
1017           isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse))
1018         return true;
1019     }
1020     return false;
1021   }
1022 
1023   // IsSigned == false.
1024 
1025   // Is the unsigned dividend known to be less than a constant divisor?
1026   // TODO: Convert this (and above) to range analysis
1027   //      ("computeConstantRangeIncludingKnownBits")?
1028   const APInt *C;
1029   if (match(Y, m_APInt(C)) &&
1030       computeKnownBits(X, /* Depth */ 0, Q).getMaxValue().ult(*C))
1031     return true;
1032 
1033   // Try again for any divisor:
1034   // Is the dividend unsigned less than the divisor?
1035   return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
1036 }
1037 
1038 /// Check for common or similar folds of integer division or integer remainder.
1039 /// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
1040 static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0,
1041                              Value *Op1, const SimplifyQuery &Q,
1042                              unsigned MaxRecurse) {
1043   bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1044   bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1045 
1046   Type *Ty = Op0->getType();
1047 
1048   // X / undef -> poison
1049   // X % undef -> poison
1050   if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1))
1051     return PoisonValue::get(Ty);
1052 
1053   // X / 0 -> poison
1054   // X % 0 -> poison
1055   // We don't need to preserve faults!
1056   if (match(Op1, m_Zero()))
1057     return PoisonValue::get(Ty);
1058 
1059   // poison / X -> poison
1060   // poison % X -> poison
1061   if (isa<PoisonValue>(Op0))
1062     return Op0;
1063 
1064   // undef / X -> 0
1065   // undef % X -> 0
1066   if (Q.isUndefValue(Op0))
1067     return Constant::getNullValue(Ty);
1068 
1069   // 0 / X -> 0
1070   // 0 % X -> 0
1071   if (match(Op0, m_Zero()))
1072     return Constant::getNullValue(Op0->getType());
1073 
1074   // X / X -> 1
1075   // X % X -> 0
1076   if (Op0 == Op1)
1077     return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty);
1078 
1079   KnownBits Known = computeKnownBits(Op1, /* Depth */ 0, Q);
1080   // X / 0 -> poison
1081   // X % 0 -> poison
1082   // If the divisor is known to be zero, just return poison. This can happen in
1083   // some cases where its provable indirectly the denominator is zero but it's
1084   // not trivially simplifiable (i.e known zero through a phi node).
1085   if (Known.isZero())
1086     return PoisonValue::get(Ty);
1087 
1088   // X / 1 -> X
1089   // X % 1 -> 0
1090   // If the divisor can only be zero or one, we can't have division-by-zero
1091   // or remainder-by-zero, so assume the divisor is 1.
1092   //   e.g. 1, zext (i8 X), sdiv X (Y and 1)
1093   if (Known.countMinLeadingZeros() == Known.getBitWidth() - 1)
1094     return IsDiv ? Op0 : Constant::getNullValue(Ty);
1095 
1096   // If X * Y does not overflow, then:
1097   //   X * Y / Y -> X
1098   //   X * Y % Y -> 0
1099   Value *X;
1100   if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
1101     auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1102     // The multiplication can't overflow if it is defined not to, or if
1103     // X == A / Y for some A.
1104     if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)) ||
1105         (!IsSigned && Q.IIQ.hasNoUnsignedWrap(Mul)) ||
1106         (IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
1107         (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) {
1108       return IsDiv ? X : Constant::getNullValue(Op0->getType());
1109     }
1110   }
1111 
1112   if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1113     return IsDiv ? Constant::getNullValue(Op0->getType()) : Op0;
1114 
1115   if (Value *V = simplifyByDomEq(Opcode, Op0, Op1, Q, MaxRecurse))
1116     return V;
1117 
1118   // If the operation is with the result of a select instruction, check whether
1119   // operating on either branch of the select always yields the same value.
1120   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1121     if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1122       return V;
1123 
1124   // If the operation is with the result of a phi instruction, check whether
1125   // operating on all incoming values of the phi always yields the same value.
1126   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1127     if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1128       return V;
1129 
1130   return nullptr;
1131 }
1132 
1133 /// These are simplifications common to SDiv and UDiv.
1134 static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1135                           bool IsExact, const SimplifyQuery &Q,
1136                           unsigned MaxRecurse) {
1137   if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1138     return C;
1139 
1140   if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1141     return V;
1142 
1143   const APInt *DivC;
1144   if (IsExact && match(Op1, m_APInt(DivC))) {
1145     // If this is an exact divide by a constant, then the dividend (Op0) must
1146     // have at least as many trailing zeros as the divisor to divide evenly. If
1147     // it has less trailing zeros, then the result must be poison.
1148     if (DivC->countr_zero()) {
1149       KnownBits KnownOp0 = computeKnownBits(Op0, /* Depth */ 0, Q);
1150       if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero())
1151         return PoisonValue::get(Op0->getType());
1152     }
1153 
1154     // udiv exact (mul nsw X, C), C --> X
1155     // sdiv exact (mul nuw X, C), C --> X
1156     // where C is not a power of 2.
1157     Value *X;
1158     if (!DivC->isPowerOf2() &&
1159         (Opcode == Instruction::UDiv
1160              ? match(Op0, m_NSWMul(m_Value(X), m_Specific(Op1)))
1161              : match(Op0, m_NUWMul(m_Value(X), m_Specific(Op1)))))
1162       return X;
1163   }
1164 
1165   return nullptr;
1166 }
1167 
1168 /// These are simplifications common to SRem and URem.
1169 static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1170                           const SimplifyQuery &Q, unsigned MaxRecurse) {
1171   if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1172     return C;
1173 
1174   if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1175     return V;
1176 
1177   // (X << Y) % X -> 0
1178   if (Q.IIQ.UseInstrInfo) {
1179     if ((Opcode == Instruction::SRem &&
1180          match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) ||
1181         (Opcode == Instruction::URem &&
1182          match(Op0, m_NUWShl(m_Specific(Op1), m_Value()))))
1183       return Constant::getNullValue(Op0->getType());
1184 
1185     const APInt *C0;
1186     if (match(Op1, m_APInt(C0))) {
1187       // (srem (mul nsw X, C1), C0) -> 0 if C1 s% C0 == 0
1188       // (urem (mul nuw X, C1), C0) -> 0 if C1 u% C0 == 0
1189       if (Opcode == Instruction::SRem
1190               ? match(Op0,
1191                       m_NSWMul(m_Value(), m_CheckedInt([C0](const APInt &C) {
1192                                  return C.srem(*C0).isZero();
1193                                })))
1194               : match(Op0,
1195                       m_NUWMul(m_Value(), m_CheckedInt([C0](const APInt &C) {
1196                                  return C.urem(*C0).isZero();
1197                                }))))
1198         return Constant::getNullValue(Op0->getType());
1199     }
1200   }
1201   return nullptr;
1202 }
1203 
1204 /// Given operands for an SDiv, see if we can fold the result.
1205 /// If not, this returns null.
1206 static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1207                                const SimplifyQuery &Q, unsigned MaxRecurse) {
1208   // If two operands are negated and no signed overflow, return -1.
1209   if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
1210     return Constant::getAllOnesValue(Op0->getType());
1211 
1212   return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1213 }
1214 
1215 Value *llvm::simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1216                               const SimplifyQuery &Q) {
1217   return ::simplifySDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1218 }
1219 
1220 /// Given operands for a UDiv, see if we can fold the result.
1221 /// If not, this returns null.
1222 static Value *simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1223                                const SimplifyQuery &Q, unsigned MaxRecurse) {
1224   return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1225 }
1226 
1227 Value *llvm::simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1228                               const SimplifyQuery &Q) {
1229   return ::simplifyUDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1230 }
1231 
1232 /// Given operands for an SRem, see if we can fold the result.
1233 /// If not, this returns null.
1234 static Value *simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1235                                unsigned MaxRecurse) {
1236   // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1237   // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1238   Value *X;
1239   if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
1240     return ConstantInt::getNullValue(Op0->getType());
1241 
1242   // If the two operands are negated, return 0.
1243   if (isKnownNegation(Op0, Op1))
1244     return ConstantInt::getNullValue(Op0->getType());
1245 
1246   return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1247 }
1248 
1249 Value *llvm::simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1250   return ::simplifySRemInst(Op0, Op1, Q, RecursionLimit);
1251 }
1252 
1253 /// Given operands for a URem, see if we can fold the result.
1254 /// If not, this returns null.
1255 static Value *simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1256                                unsigned MaxRecurse) {
1257   return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1258 }
1259 
1260 Value *llvm::simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1261   return ::simplifyURemInst(Op0, Op1, Q, RecursionLimit);
1262 }
1263 
1264 /// Returns true if a shift by \c Amount always yields poison.
1265 static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) {
1266   Constant *C = dyn_cast<Constant>(Amount);
1267   if (!C)
1268     return false;
1269 
1270   // X shift by undef -> poison because it may shift by the bitwidth.
1271   if (Q.isUndefValue(C))
1272     return true;
1273 
1274   // Shifting by the bitwidth or more is poison. This covers scalars and
1275   // fixed/scalable vectors with splat constants.
1276   const APInt *AmountC;
1277   if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth()))
1278     return true;
1279 
1280   // Try harder for fixed-length vectors:
1281   // If all lanes of a vector shift are poison, the whole shift is poison.
1282   if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) {
1283     for (unsigned I = 0,
1284                   E = cast<FixedVectorType>(C->getType())->getNumElements();
1285          I != E; ++I)
1286       if (!isPoisonShift(C->getAggregateElement(I), Q))
1287         return false;
1288     return true;
1289   }
1290 
1291   return false;
1292 }
1293 
1294 /// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1295 /// If not, this returns null.
1296 static Value *simplifyShift(Instruction::BinaryOps Opcode, Value *Op0,
1297                             Value *Op1, bool IsNSW, const SimplifyQuery &Q,
1298                             unsigned MaxRecurse) {
1299   if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1300     return C;
1301 
1302   // poison shift by X -> poison
1303   if (isa<PoisonValue>(Op0))
1304     return Op0;
1305 
1306   // 0 shift by X -> 0
1307   if (match(Op0, m_Zero()))
1308     return Constant::getNullValue(Op0->getType());
1309 
1310   // X shift by 0 -> X
1311   // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1312   // would be poison.
1313   Value *X;
1314   if (match(Op1, m_Zero()) ||
1315       (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1316     return Op0;
1317 
1318   // Fold undefined shifts.
1319   if (isPoisonShift(Op1, Q))
1320     return PoisonValue::get(Op0->getType());
1321 
1322   // If the operation is with the result of a select instruction, check whether
1323   // operating on either branch of the select always yields the same value.
1324   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1325     if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1326       return V;
1327 
1328   // If the operation is with the result of a phi instruction, check whether
1329   // operating on all incoming values of the phi always yields the same value.
1330   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1331     if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1332       return V;
1333 
1334   // If any bits in the shift amount make that value greater than or equal to
1335   // the number of bits in the type, the shift is undefined.
1336   KnownBits KnownAmt = computeKnownBits(Op1, /* Depth */ 0, Q);
1337   if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth()))
1338     return PoisonValue::get(Op0->getType());
1339 
1340   // If all valid bits in the shift amount are known zero, the first operand is
1341   // unchanged.
1342   unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth());
1343   if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits)
1344     return Op0;
1345 
1346   // Check for nsw shl leading to a poison value.
1347   if (IsNSW) {
1348     assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction");
1349     KnownBits KnownVal = computeKnownBits(Op0, /* Depth */ 0, Q);
1350     KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt);
1351 
1352     if (KnownVal.Zero.isSignBitSet())
1353       KnownShl.Zero.setSignBit();
1354     if (KnownVal.One.isSignBitSet())
1355       KnownShl.One.setSignBit();
1356 
1357     if (KnownShl.hasConflict())
1358       return PoisonValue::get(Op0->getType());
1359   }
1360 
1361   return nullptr;
1362 }
1363 
1364 /// Given operands for an LShr or AShr, see if we can fold the result.  If not,
1365 /// this returns null.
1366 static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
1367                                  Value *Op1, bool IsExact,
1368                                  const SimplifyQuery &Q, unsigned MaxRecurse) {
1369   if (Value *V =
1370           simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
1371     return V;
1372 
1373   // X >> X -> 0
1374   if (Op0 == Op1)
1375     return Constant::getNullValue(Op0->getType());
1376 
1377   // undef >> X -> 0
1378   // undef >> X -> undef (if it's exact)
1379   if (Q.isUndefValue(Op0))
1380     return IsExact ? Op0 : Constant::getNullValue(Op0->getType());
1381 
1382   // The low bit cannot be shifted out of an exact shift if it is set.
1383   // TODO: Generalize by counting trailing zeros (see fold for exact division).
1384   if (IsExact) {
1385     KnownBits Op0Known = computeKnownBits(Op0, /* Depth */ 0, Q);
1386     if (Op0Known.One[0])
1387       return Op0;
1388   }
1389 
1390   return nullptr;
1391 }
1392 
1393 /// Given operands for an Shl, see if we can fold the result.
1394 /// If not, this returns null.
1395 static Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1396                               const SimplifyQuery &Q, unsigned MaxRecurse) {
1397   if (Value *V =
1398           simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1399     return V;
1400 
1401   Type *Ty = Op0->getType();
1402   // undef << X -> 0
1403   // undef << X -> undef if (if it's NSW/NUW)
1404   if (Q.isUndefValue(Op0))
1405     return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Ty);
1406 
1407   // (X >> A) << A -> X
1408   Value *X;
1409   if (Q.IIQ.UseInstrInfo &&
1410       match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
1411     return X;
1412 
1413   // shl nuw i8 C, %x  ->  C  iff C has sign bit set.
1414   if (IsNUW && match(Op0, m_Negative()))
1415     return Op0;
1416   // NOTE: could use computeKnownBits() / LazyValueInfo,
1417   // but the cost-benefit analysis suggests it isn't worth it.
1418 
1419   // "nuw" guarantees that only zeros are shifted out, and "nsw" guarantees
1420   // that the sign-bit does not change, so the only input that does not
1421   // produce poison is 0, and "0 << (bitwidth-1) --> 0".
1422   if (IsNSW && IsNUW &&
1423       match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1)))
1424     return Constant::getNullValue(Ty);
1425 
1426   return nullptr;
1427 }
1428 
1429 Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1430                              const SimplifyQuery &Q) {
1431   return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
1432 }
1433 
1434 /// Given operands for an LShr, see if we can fold the result.
1435 /// If not, this returns null.
1436 static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1437                                const SimplifyQuery &Q, unsigned MaxRecurse) {
1438   if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, IsExact, Q,
1439                                     MaxRecurse))
1440     return V;
1441 
1442   // (X << A) >> A -> X
1443   Value *X;
1444   if (Q.IIQ.UseInstrInfo && match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1445     return X;
1446 
1447   // ((X << A) | Y) >> A -> X  if effective width of Y is not larger than A.
1448   // We can return X as we do in the above case since OR alters no bits in X.
1449   // SimplifyDemandedBits in InstCombine can do more general optimization for
1450   // bit manipulation. This pattern aims to provide opportunities for other
1451   // optimizers by supporting a simple but common case in InstSimplify.
1452   Value *Y;
1453   const APInt *ShRAmt, *ShLAmt;
1454   if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(ShRAmt)) &&
1455       match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y))) &&
1456       *ShRAmt == *ShLAmt) {
1457     const KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
1458     const unsigned EffWidthY = YKnown.countMaxActiveBits();
1459     if (ShRAmt->uge(EffWidthY))
1460       return X;
1461   }
1462 
1463   return nullptr;
1464 }
1465 
1466 Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1467                               const SimplifyQuery &Q) {
1468   return ::simplifyLShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1469 }
1470 
1471 /// Given operands for an AShr, see if we can fold the result.
1472 /// If not, this returns null.
1473 static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1474                                const SimplifyQuery &Q, unsigned MaxRecurse) {
1475   if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, IsExact, Q,
1476                                     MaxRecurse))
1477     return V;
1478 
1479   // -1 >>a X --> -1
1480   // (-1 << X) a>> X --> -1
1481   // We could return the original -1 constant to preserve poison elements.
1482   if (match(Op0, m_AllOnes()) ||
1483       match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1))))
1484     return Constant::getAllOnesValue(Op0->getType());
1485 
1486   // (X << A) >> A -> X
1487   Value *X;
1488   if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
1489     return X;
1490 
1491   // Arithmetic shifting an all-sign-bit value is a no-op.
1492   unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1493   if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1494     return Op0;
1495 
1496   return nullptr;
1497 }
1498 
1499 Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1500                               const SimplifyQuery &Q) {
1501   return ::simplifyAShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1502 }
1503 
1504 /// Commuted variants are assumed to be handled by calling this function again
1505 /// with the parameters swapped.
1506 static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
1507                                          ICmpInst *UnsignedICmp, bool IsAnd,
1508                                          const SimplifyQuery &Q) {
1509   Value *X, *Y;
1510 
1511   CmpPredicate EqPred;
1512   if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1513       !ICmpInst::isEquality(EqPred))
1514     return nullptr;
1515 
1516   CmpPredicate UnsignedPred;
1517 
1518   Value *A, *B;
1519   // Y = (A - B);
1520   if (match(Y, m_Sub(m_Value(A), m_Value(B)))) {
1521     if (match(UnsignedICmp,
1522               m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) &&
1523         ICmpInst::isUnsigned(UnsignedPred)) {
1524       // A >=/<= B || (A - B) != 0  <-->  true
1525       if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1526            UnsignedPred == ICmpInst::ICMP_ULE) &&
1527           EqPred == ICmpInst::ICMP_NE && !IsAnd)
1528         return ConstantInt::getTrue(UnsignedICmp->getType());
1529       // A </> B && (A - B) == 0  <-->  false
1530       if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1531            UnsignedPred == ICmpInst::ICMP_UGT) &&
1532           EqPred == ICmpInst::ICMP_EQ && IsAnd)
1533         return ConstantInt::getFalse(UnsignedICmp->getType());
1534 
1535       // A </> B && (A - B) != 0  <-->  A </> B
1536       // A </> B || (A - B) != 0  <-->  (A - B) != 0
1537       if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1538                                           UnsignedPred == ICmpInst::ICMP_UGT))
1539         return IsAnd ? UnsignedICmp : ZeroICmp;
1540 
1541       // A <=/>= B && (A - B) == 0  <-->  (A - B) == 0
1542       // A <=/>= B || (A - B) == 0  <-->  A <=/>= B
1543       if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1544                                           UnsignedPred == ICmpInst::ICMP_UGE))
1545         return IsAnd ? ZeroICmp : UnsignedICmp;
1546     }
1547 
1548     // Given  Y = (A - B)
1549     //   Y >= A && Y != 0  --> Y >= A  iff B != 0
1550     //   Y <  A || Y == 0  --> Y <  A  iff B != 0
1551     if (match(UnsignedICmp,
1552               m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) {
1553       if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1554           EqPred == ICmpInst::ICMP_NE && isKnownNonZero(B, Q))
1555         return UnsignedICmp;
1556       if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1557           EqPred == ICmpInst::ICMP_EQ && isKnownNonZero(B, Q))
1558         return UnsignedICmp;
1559     }
1560   }
1561 
1562   if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1563       ICmpInst::isUnsigned(UnsignedPred))
1564     ;
1565   else if (match(UnsignedICmp,
1566                  m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
1567            ICmpInst::isUnsigned(UnsignedPred))
1568     UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1569   else
1570     return nullptr;
1571 
1572   // X > Y && Y == 0  -->  Y == 0  iff X != 0
1573   // X > Y || Y == 0  -->  X > Y   iff X != 0
1574   if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1575       isKnownNonZero(X, Q))
1576     return IsAnd ? ZeroICmp : UnsignedICmp;
1577 
1578   // X <= Y && Y != 0  -->  X <= Y  iff X != 0
1579   // X <= Y || Y != 0  -->  Y != 0  iff X != 0
1580   if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1581       isKnownNonZero(X, Q))
1582     return IsAnd ? UnsignedICmp : ZeroICmp;
1583 
1584   // The transforms below here are expected to be handled more generally with
1585   // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's
1586   // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap,
1587   // these are candidates for removal.
1588 
1589   // X < Y && Y != 0  -->  X < Y
1590   // X < Y || Y != 0  -->  Y != 0
1591   if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1592     return IsAnd ? UnsignedICmp : ZeroICmp;
1593 
1594   // X >= Y && Y == 0  -->  Y == 0
1595   // X >= Y || Y == 0  -->  X >= Y
1596   if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1597     return IsAnd ? ZeroICmp : UnsignedICmp;
1598 
1599   // X < Y && Y == 0  -->  false
1600   if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1601       IsAnd)
1602     return getFalse(UnsignedICmp->getType());
1603 
1604   // X >= Y || Y != 0  -->  true
1605   if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1606       !IsAnd)
1607     return getTrue(UnsignedICmp->getType());
1608 
1609   return nullptr;
1610 }
1611 
1612 /// Test if a pair of compares with a shared operand and 2 constants has an
1613 /// empty set intersection, full set union, or if one compare is a superset of
1614 /// the other.
1615 static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1,
1616                                                 bool IsAnd) {
1617   // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
1618   if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1619     return nullptr;
1620 
1621   const APInt *C0, *C1;
1622   if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1623       !match(Cmp1->getOperand(1), m_APInt(C1)))
1624     return nullptr;
1625 
1626   auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1627   auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1628 
1629   // For and-of-compares, check if the intersection is empty:
1630   // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1631   if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1632     return getFalse(Cmp0->getType());
1633 
1634   // For or-of-compares, check if the union is full:
1635   // (icmp X, C0) || (icmp X, C1) --> full set --> true
1636   if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1637     return getTrue(Cmp0->getType());
1638 
1639   // Is one range a superset of the other?
1640   // If this is and-of-compares, take the smaller set:
1641   // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1642   // If this is or-of-compares, take the larger set:
1643   // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1644   if (Range0.contains(Range1))
1645     return IsAnd ? Cmp1 : Cmp0;
1646   if (Range1.contains(Range0))
1647     return IsAnd ? Cmp0 : Cmp1;
1648 
1649   return nullptr;
1650 }
1651 
1652 static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1653                                         const InstrInfoQuery &IIQ) {
1654   // (icmp (add V, C0), C1) & (icmp V, C0)
1655   CmpPredicate Pred0, Pred1;
1656   const APInt *C0, *C1;
1657   Value *V;
1658   if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1659     return nullptr;
1660 
1661   if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1662     return nullptr;
1663 
1664   auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1665   if (AddInst->getOperand(1) != Op1->getOperand(1))
1666     return nullptr;
1667 
1668   Type *ITy = Op0->getType();
1669   bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1670   bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1671 
1672   const APInt Delta = *C1 - *C0;
1673   if (C0->isStrictlyPositive()) {
1674     if (Delta == 2) {
1675       if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1676         return getFalse(ITy);
1677       if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1678         return getFalse(ITy);
1679     }
1680     if (Delta == 1) {
1681       if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1682         return getFalse(ITy);
1683       if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1684         return getFalse(ITy);
1685     }
1686   }
1687   if (C0->getBoolValue() && IsNUW) {
1688     if (Delta == 2)
1689       if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1690         return getFalse(ITy);
1691     if (Delta == 1)
1692       if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1693         return getFalse(ITy);
1694   }
1695 
1696   return nullptr;
1697 }
1698 
1699 /// Try to simplify and/or of icmp with ctpop intrinsic.
1700 static Value *simplifyAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1,
1701                                             bool IsAnd) {
1702   CmpPredicate Pred0, Pred1;
1703   Value *X;
1704   const APInt *C;
1705   if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
1706                           m_APInt(C))) ||
1707       !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())) || C->isZero())
1708     return nullptr;
1709 
1710   // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0
1711   if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1712     return Cmp1;
1713   // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0
1714   if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1715     return Cmp1;
1716 
1717   return nullptr;
1718 }
1719 
1720 static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1721                                  const SimplifyQuery &Q) {
1722   if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q))
1723     return X;
1724   if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q))
1725     return X;
1726 
1727   if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
1728     return X;
1729 
1730   if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, true))
1731     return X;
1732   if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, true))
1733     return X;
1734 
1735   if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1736     return X;
1737   if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1738     return X;
1739 
1740   return nullptr;
1741 }
1742 
1743 static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1744                                        const InstrInfoQuery &IIQ) {
1745   // (icmp (add V, C0), C1) | (icmp V, C0)
1746   CmpPredicate Pred0, Pred1;
1747   const APInt *C0, *C1;
1748   Value *V;
1749   if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1750     return nullptr;
1751 
1752   if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1753     return nullptr;
1754 
1755   auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1756   if (AddInst->getOperand(1) != Op1->getOperand(1))
1757     return nullptr;
1758 
1759   Type *ITy = Op0->getType();
1760   bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1761   bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1762 
1763   const APInt Delta = *C1 - *C0;
1764   if (C0->isStrictlyPositive()) {
1765     if (Delta == 2) {
1766       if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1767         return getTrue(ITy);
1768       if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1769         return getTrue(ITy);
1770     }
1771     if (Delta == 1) {
1772       if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1773         return getTrue(ITy);
1774       if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1775         return getTrue(ITy);
1776     }
1777   }
1778   if (C0->getBoolValue() && IsNUW) {
1779     if (Delta == 2)
1780       if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1781         return getTrue(ITy);
1782     if (Delta == 1)
1783       if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1784         return getTrue(ITy);
1785   }
1786 
1787   return nullptr;
1788 }
1789 
1790 static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1791                                 const SimplifyQuery &Q) {
1792   if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q))
1793     return X;
1794   if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q))
1795     return X;
1796 
1797   if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
1798     return X;
1799 
1800   if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, false))
1801     return X;
1802   if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, false))
1803     return X;
1804 
1805   if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1806     return X;
1807   if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1808     return X;
1809 
1810   return nullptr;
1811 }
1812 
1813 static Value *simplifyAndOrOfFCmps(const SimplifyQuery &Q, FCmpInst *LHS,
1814                                    FCmpInst *RHS, bool IsAnd) {
1815   Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1816   Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1817   if (LHS0->getType() != RHS0->getType())
1818     return nullptr;
1819 
1820   FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1821   auto AbsOrSelfLHS0 = m_CombineOr(m_Specific(LHS0), m_FAbs(m_Specific(LHS0)));
1822   if ((PredL == FCmpInst::FCMP_ORD || PredL == FCmpInst::FCMP_UNO) &&
1823       ((FCmpInst::isOrdered(PredR) && IsAnd) ||
1824        (FCmpInst::isUnordered(PredR) && !IsAnd))) {
1825     // (fcmp ord X, 0) & (fcmp o** X/abs(X), Y) --> fcmp o** X/abs(X), Y
1826     // (fcmp uno X, 0) & (fcmp o** X/abs(X), Y) --> false
1827     // (fcmp uno X, 0) | (fcmp u** X/abs(X), Y) --> fcmp u** X/abs(X), Y
1828     // (fcmp ord X, 0) | (fcmp u** X/abs(X), Y) --> true
1829     if ((match(RHS0, AbsOrSelfLHS0) || match(RHS1, AbsOrSelfLHS0)) &&
1830         match(LHS1, m_PosZeroFP()))
1831       return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1832                  ? static_cast<Value *>(RHS)
1833                  : ConstantInt::getBool(LHS->getType(), !IsAnd);
1834   }
1835 
1836   auto AbsOrSelfRHS0 = m_CombineOr(m_Specific(RHS0), m_FAbs(m_Specific(RHS0)));
1837   if ((PredR == FCmpInst::FCMP_ORD || PredR == FCmpInst::FCMP_UNO) &&
1838       ((FCmpInst::isOrdered(PredL) && IsAnd) ||
1839        (FCmpInst::isUnordered(PredL) && !IsAnd))) {
1840     // (fcmp o** X/abs(X), Y) & (fcmp ord X, 0) --> fcmp o** X/abs(X), Y
1841     // (fcmp o** X/abs(X), Y) & (fcmp uno X, 0) --> false
1842     // (fcmp u** X/abs(X), Y) | (fcmp uno X, 0) --> fcmp u** X/abs(X), Y
1843     // (fcmp u** X/abs(X), Y) | (fcmp ord X, 0) --> true
1844     if ((match(LHS0, AbsOrSelfRHS0) || match(LHS1, AbsOrSelfRHS0)) &&
1845         match(RHS1, m_PosZeroFP()))
1846       return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1847                  ? static_cast<Value *>(LHS)
1848                  : ConstantInt::getBool(LHS->getType(), !IsAnd);
1849   }
1850 
1851   return nullptr;
1852 }
1853 
1854 static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0,
1855                                   Value *Op1, bool IsAnd) {
1856   // Look through casts of the 'and' operands to find compares.
1857   auto *Cast0 = dyn_cast<CastInst>(Op0);
1858   auto *Cast1 = dyn_cast<CastInst>(Op1);
1859   if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1860       Cast0->getSrcTy() == Cast1->getSrcTy()) {
1861     Op0 = Cast0->getOperand(0);
1862     Op1 = Cast1->getOperand(0);
1863   }
1864 
1865   Value *V = nullptr;
1866   auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1867   auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1868   if (ICmp0 && ICmp1)
1869     V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)
1870               : simplifyOrOfICmps(ICmp0, ICmp1, Q);
1871 
1872   auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1873   auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1874   if (FCmp0 && FCmp1)
1875     V = simplifyAndOrOfFCmps(Q, FCmp0, FCmp1, IsAnd);
1876 
1877   if (!V)
1878     return nullptr;
1879   if (!Cast0)
1880     return V;
1881 
1882   // If we looked through casts, we can only handle a constant simplification
1883   // because we are not allowed to create a cast instruction here.
1884   if (auto *C = dyn_cast<Constant>(V))
1885     return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(),
1886                                    Q.DL);
1887 
1888   return nullptr;
1889 }
1890 
1891 static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
1892                                      const SimplifyQuery &Q,
1893                                      bool AllowRefinement,
1894                                      SmallVectorImpl<Instruction *> *DropFlags,
1895                                      unsigned MaxRecurse);
1896 
1897 static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,
1898                                       const SimplifyQuery &Q,
1899                                       unsigned MaxRecurse) {
1900   assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1901          "Must be and/or");
1902   CmpPredicate Pred;
1903   Value *A, *B;
1904   if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) ||
1905       !ICmpInst::isEquality(Pred))
1906     return nullptr;
1907 
1908   auto Simplify = [&](Value *Res) -> Value * {
1909     Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Res->getType());
1910 
1911     // and (icmp eq a, b), x implies (a==b) inside x.
1912     // or (icmp ne a, b), x implies (a==b) inside x.
1913     // If x simplifies to true/false, we can simplify the and/or.
1914     if (Pred ==
1915         (Opcode == Instruction::And ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
1916       if (Res == Absorber)
1917         return Absorber;
1918       if (Res == ConstantExpr::getBinOpIdentity(Opcode, Res->getType()))
1919         return Op0;
1920       return nullptr;
1921     }
1922 
1923     // If we have and (icmp ne a, b), x and for a==b we can simplify x to false,
1924     // then we can drop the icmp, as x will already be false in the case where
1925     // the icmp is false. Similar for or and true.
1926     if (Res == Absorber)
1927       return Op1;
1928     return nullptr;
1929   };
1930 
1931   // In the final case (Res == Absorber with inverted predicate), it is safe to
1932   // refine poison during simplification, but not undef. For simplicity always
1933   // disable undef-based folds here.
1934   if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(),
1935                                           /* AllowRefinement */ true,
1936                                           /* DropFlags */ nullptr, MaxRecurse))
1937     return Simplify(Res);
1938   if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(),
1939                                           /* AllowRefinement */ true,
1940                                           /* DropFlags */ nullptr, MaxRecurse))
1941     return Simplify(Res);
1942 
1943   return nullptr;
1944 }
1945 
1946 /// Given a bitwise logic op, check if the operands are add/sub with a common
1947 /// source value and inverted constant (identity: C - X -> ~(X + ~C)).
1948 static Value *simplifyLogicOfAddSub(Value *Op0, Value *Op1,
1949                                     Instruction::BinaryOps Opcode) {
1950   assert(Op0->getType() == Op1->getType() && "Mismatched binop types");
1951   assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op");
1952   Value *X;
1953   Constant *C1, *C2;
1954   if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) &&
1955        match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))) ||
1956       (match(Op1, m_Add(m_Value(X), m_Constant(C1))) &&
1957        match(Op0, m_Sub(m_Constant(C2), m_Specific(X))))) {
1958     if (ConstantExpr::getNot(C1) == C2) {
1959       // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
1960       // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
1961       // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
1962       Type *Ty = Op0->getType();
1963       return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
1964                                         : ConstantInt::getAllOnesValue(Ty);
1965     }
1966   }
1967   return nullptr;
1968 }
1969 
1970 // Commutative patterns for and that will be tried with both operand orders.
1971 static Value *simplifyAndCommutative(Value *Op0, Value *Op1,
1972                                      const SimplifyQuery &Q,
1973                                      unsigned MaxRecurse) {
1974   // ~A & A =  0
1975   if (match(Op0, m_Not(m_Specific(Op1))))
1976     return Constant::getNullValue(Op0->getType());
1977 
1978   // (A | ?) & A = A
1979   if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
1980     return Op1;
1981 
1982   // (X | ~Y) & (X | Y) --> X
1983   Value *X, *Y;
1984   if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
1985       match(Op1, m_c_Or(m_Specific(X), m_Specific(Y))))
1986     return X;
1987 
1988   // If we have a multiplication overflow check that is being 'and'ed with a
1989   // check that one of the multipliers is not zero, we can omit the 'and', and
1990   // only keep the overflow check.
1991   if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true))
1992     return Op1;
1993 
1994   // -A & A = A if A is a power of two or zero.
1995   if (match(Op0, m_Neg(m_Specific(Op1))) &&
1996       isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
1997     return Op1;
1998 
1999   // This is a similar pattern used for checking if a value is a power-of-2:
2000   // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
2001   if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
2002       isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
2003     return Constant::getNullValue(Op1->getType());
2004 
2005   // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and
2006   // M <= N.
2007   const APInt *Shift1, *Shift2;
2008   if (match(Op0, m_Shl(m_Value(X), m_APInt(Shift1))) &&
2009       match(Op1, m_Add(m_Shl(m_Specific(X), m_APInt(Shift2)), m_AllOnes())) &&
2010       isKnownToBeAPowerOfTwo(X, Q.DL, /*OrZero*/ true, /*Depth*/ 0, Q.AC,
2011                              Q.CxtI) &&
2012       Shift1->uge(*Shift2))
2013     return Constant::getNullValue(Op0->getType());
2014 
2015   if (Value *V =
2016           simplifyAndOrWithICmpEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2017     return V;
2018 
2019   return nullptr;
2020 }
2021 
2022 /// Given operands for an And, see if we can fold the result.
2023 /// If not, this returns null.
2024 static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2025                               unsigned MaxRecurse) {
2026   if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
2027     return C;
2028 
2029   // X & poison -> poison
2030   if (isa<PoisonValue>(Op1))
2031     return Op1;
2032 
2033   // X & undef -> 0
2034   if (Q.isUndefValue(Op1))
2035     return Constant::getNullValue(Op0->getType());
2036 
2037   // X & X = X
2038   if (Op0 == Op1)
2039     return Op0;
2040 
2041   // X & 0 = 0
2042   if (match(Op1, m_Zero()))
2043     return Constant::getNullValue(Op0->getType());
2044 
2045   // X & -1 = X
2046   if (match(Op1, m_AllOnes()))
2047     return Op0;
2048 
2049   if (Value *Res = simplifyAndCommutative(Op0, Op1, Q, MaxRecurse))
2050     return Res;
2051   if (Value *Res = simplifyAndCommutative(Op1, Op0, Q, MaxRecurse))
2052     return Res;
2053 
2054   if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And))
2055     return V;
2056 
2057   // A mask that only clears known zeros of a shifted value is a no-op.
2058   const APInt *Mask;
2059   const APInt *ShAmt;
2060   Value *X, *Y;
2061   if (match(Op1, m_APInt(Mask))) {
2062     // If all bits in the inverted and shifted mask are clear:
2063     // and (shl X, ShAmt), Mask --> shl X, ShAmt
2064     if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
2065         (~(*Mask)).lshr(*ShAmt).isZero())
2066       return Op0;
2067 
2068     // If all bits in the inverted and shifted mask are clear:
2069     // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
2070     if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
2071         (~(*Mask)).shl(*ShAmt).isZero())
2072       return Op0;
2073   }
2074 
2075   // and 2^x-1, 2^C --> 0 where x <= C.
2076   const APInt *PowerC;
2077   Value *Shift;
2078   if (match(Op1, m_Power2(PowerC)) &&
2079       match(Op0, m_Add(m_Value(Shift), m_AllOnes())) &&
2080       isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
2081                              Q.DT)) {
2082     KnownBits Known = computeKnownBits(Shift, /* Depth */ 0, Q);
2083     // Use getActiveBits() to make use of the additional power of two knowledge
2084     if (PowerC->getActiveBits() >= Known.getMaxValue().getActiveBits())
2085       return ConstantInt::getNullValue(Op1->getType());
2086   }
2087 
2088   if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
2089     return V;
2090 
2091   // Try some generic simplifications for associative operations.
2092   if (Value *V =
2093           simplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse))
2094     return V;
2095 
2096   // And distributes over Or.  Try some generic simplifications based on this.
2097   if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2098                                         Instruction::Or, Q, MaxRecurse))
2099     return V;
2100 
2101   // And distributes over Xor.  Try some generic simplifications based on this.
2102   if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2103                                         Instruction::Xor, Q, MaxRecurse))
2104     return V;
2105 
2106   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2107     if (Op0->getType()->isIntOrIntVectorTy(1)) {
2108       // A & (A && B) -> A && B
2109       if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero())))
2110         return Op1;
2111       else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero())))
2112         return Op0;
2113     }
2114     // If the operation is with the result of a select instruction, check
2115     // whether operating on either branch of the select always yields the same
2116     // value.
2117     if (Value *V =
2118             threadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse))
2119       return V;
2120   }
2121 
2122   // If the operation is with the result of a phi instruction, check whether
2123   // operating on all incoming values of the phi always yields the same value.
2124   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2125     if (Value *V =
2126             threadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse))
2127       return V;
2128 
2129   // Assuming the effective width of Y is not larger than A, i.e. all bits
2130   // from X and Y are disjoint in (X << A) | Y,
2131   // if the mask of this AND op covers all bits of X or Y, while it covers
2132   // no bits from the other, we can bypass this AND op. E.g.,
2133   // ((X << A) | Y) & Mask -> Y,
2134   //     if Mask = ((1 << effective_width_of(Y)) - 1)
2135   // ((X << A) | Y) & Mask -> X << A,
2136   //     if Mask = ((1 << effective_width_of(X)) - 1) << A
2137   // SimplifyDemandedBits in InstCombine can optimize the general case.
2138   // This pattern aims to help other passes for a common case.
2139   Value *XShifted;
2140   if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(Mask)) &&
2141       match(Op0, m_c_Or(m_CombineAnd(m_NUWShl(m_Value(X), m_APInt(ShAmt)),
2142                                      m_Value(XShifted)),
2143                         m_Value(Y)))) {
2144     const unsigned Width = Op0->getType()->getScalarSizeInBits();
2145     const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
2146     const KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
2147     const unsigned EffWidthY = YKnown.countMaxActiveBits();
2148     if (EffWidthY <= ShftCnt) {
2149       const KnownBits XKnown = computeKnownBits(X, /* Depth */ 0, Q);
2150       const unsigned EffWidthX = XKnown.countMaxActiveBits();
2151       const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
2152       const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
2153       // If the mask is extracting all bits from X or Y as is, we can skip
2154       // this AND op.
2155       if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask))
2156         return Y;
2157       if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask))
2158         return XShifted;
2159     }
2160   }
2161 
2162   // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2163   // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2164   BinaryOperator *Or;
2165   if (match(Op0, m_c_Xor(m_Value(X),
2166                          m_CombineAnd(m_BinOp(Or),
2167                                       m_c_Or(m_Deferred(X), m_Value(Y))))) &&
2168       match(Op1, m_c_Xor(m_Specific(Or), m_Specific(Y))))
2169     return Constant::getNullValue(Op0->getType());
2170 
2171   const APInt *C1;
2172   Value *A;
2173   // (A ^ C) & (A ^ ~C) -> 0
2174   if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2175       match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2176     return Constant::getNullValue(Op0->getType());
2177 
2178   if (Op0->getType()->isIntOrIntVectorTy(1)) {
2179     if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
2180       // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
2181       if (*Implied == true)
2182         return Op0;
2183       // If Op0 is true implies Op1 is false, then they are not true together.
2184       if (*Implied == false)
2185         return ConstantInt::getFalse(Op0->getType());
2186     }
2187     if (std::optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL)) {
2188       // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0.
2189       if (*Implied)
2190         return Op1;
2191       // If Op1 is true implies Op0 is false, then they are not true together.
2192       if (!*Implied)
2193         return ConstantInt::getFalse(Op1->getType());
2194     }
2195   }
2196 
2197   if (Value *V = simplifyByDomEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2198     return V;
2199 
2200   return nullptr;
2201 }
2202 
2203 Value *llvm::simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2204   return ::simplifyAndInst(Op0, Op1, Q, RecursionLimit);
2205 }
2206 
2207 // TODO: Many of these folds could use LogicalAnd/LogicalOr.
2208 static Value *simplifyOrLogic(Value *X, Value *Y) {
2209   assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
2210   Type *Ty = X->getType();
2211 
2212   // X | ~X --> -1
2213   if (match(Y, m_Not(m_Specific(X))))
2214     return ConstantInt::getAllOnesValue(Ty);
2215 
2216   // X | ~(X & ?) = -1
2217   if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value()))))
2218     return ConstantInt::getAllOnesValue(Ty);
2219 
2220   // X | (X & ?) --> X
2221   if (match(Y, m_c_And(m_Specific(X), m_Value())))
2222     return X;
2223 
2224   Value *A, *B;
2225 
2226   // (A ^ B) | (A | B) --> A | B
2227   // (A ^ B) | (B | A) --> B | A
2228   if (match(X, m_Xor(m_Value(A), m_Value(B))) &&
2229       match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2230     return Y;
2231 
2232   // ~(A ^ B) | (A | B) --> -1
2233   // ~(A ^ B) | (B | A) --> -1
2234   if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
2235       match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2236     return ConstantInt::getAllOnesValue(Ty);
2237 
2238   // (A & ~B) | (A ^ B) --> A ^ B
2239   // (~B & A) | (A ^ B) --> A ^ B
2240   // (A & ~B) | (B ^ A) --> B ^ A
2241   // (~B & A) | (B ^ A) --> B ^ A
2242   if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2243       match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2244     return Y;
2245 
2246   // (~A ^ B) | (A & B) --> ~A ^ B
2247   // (B ^ ~A) | (A & B) --> B ^ ~A
2248   // (~A ^ B) | (B & A) --> ~A ^ B
2249   // (B ^ ~A) | (B & A) --> B ^ ~A
2250   if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2251       match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2252     return X;
2253 
2254   // (~A | B) | (A ^ B) --> -1
2255   // (~A | B) | (B ^ A) --> -1
2256   // (B | ~A) | (A ^ B) --> -1
2257   // (B | ~A) | (B ^ A) --> -1
2258   if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2259       match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2260     return ConstantInt::getAllOnesValue(Ty);
2261 
2262   // (~A & B) | ~(A | B) --> ~A
2263   // (~A & B) | ~(B | A) --> ~A
2264   // (B & ~A) | ~(A | B) --> ~A
2265   // (B & ~A) | ~(B | A) --> ~A
2266   Value *NotA;
2267   if (match(X, m_c_And(m_CombineAnd(m_Value(NotA), m_Not(m_Value(A))),
2268                        m_Value(B))) &&
2269       match(Y, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
2270     return NotA;
2271   // The same is true of Logical And
2272   // TODO: This could share the logic of the version above if there was a
2273   // version of LogicalAnd that allowed more than just i1 types.
2274   if (match(X, m_c_LogicalAnd(m_CombineAnd(m_Value(NotA), m_Not(m_Value(A))),
2275                               m_Value(B))) &&
2276       match(Y, m_Not(m_c_LogicalOr(m_Specific(A), m_Specific(B)))))
2277     return NotA;
2278 
2279   // ~(A ^ B) | (A & B) --> ~(A ^ B)
2280   // ~(A ^ B) | (B & A) --> ~(A ^ B)
2281   Value *NotAB;
2282   if (match(X, m_CombineAnd(m_Not(m_Xor(m_Value(A), m_Value(B))),
2283                             m_Value(NotAB))) &&
2284       match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2285     return NotAB;
2286 
2287   // ~(A & B) | (A ^ B) --> ~(A & B)
2288   // ~(A & B) | (B ^ A) --> ~(A & B)
2289   if (match(X, m_CombineAnd(m_Not(m_And(m_Value(A), m_Value(B))),
2290                             m_Value(NotAB))) &&
2291       match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2292     return NotAB;
2293 
2294   return nullptr;
2295 }
2296 
2297 /// Given operands for an Or, see if we can fold the result.
2298 /// If not, this returns null.
2299 static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2300                              unsigned MaxRecurse) {
2301   if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
2302     return C;
2303 
2304   // X | poison -> poison
2305   if (isa<PoisonValue>(Op1))
2306     return Op1;
2307 
2308   // X | undef -> -1
2309   // X | -1 = -1
2310   // Do not return Op1 because it may contain undef elements if it's a vector.
2311   if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes()))
2312     return Constant::getAllOnesValue(Op0->getType());
2313 
2314   // X | X = X
2315   // X | 0 = X
2316   if (Op0 == Op1 || match(Op1, m_Zero()))
2317     return Op0;
2318 
2319   if (Value *R = simplifyOrLogic(Op0, Op1))
2320     return R;
2321   if (Value *R = simplifyOrLogic(Op1, Op0))
2322     return R;
2323 
2324   if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or))
2325     return V;
2326 
2327   // Rotated -1 is still -1:
2328   // (-1 << X) | (-1 >> (C - X)) --> -1
2329   // (-1 >> X) | (-1 << (C - X)) --> -1
2330   // ...with C <= bitwidth (and commuted variants).
2331   Value *X, *Y;
2332   if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) &&
2333        match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))) ||
2334       (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) &&
2335        match(Op0, m_LShr(m_AllOnes(), m_Value(Y))))) {
2336     const APInt *C;
2337     if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) ||
2338          match(Y, m_Sub(m_APInt(C), m_Specific(X)))) &&
2339         C->ule(X->getType()->getScalarSizeInBits())) {
2340       return ConstantInt::getAllOnesValue(X->getType());
2341     }
2342   }
2343 
2344   // A funnel shift (rotate) can be decomposed into simpler shifts. See if we
2345   // are mixing in another shift that is redundant with the funnel shift.
2346 
2347   // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2348   // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2349   if (match(Op0,
2350             m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2351       match(Op1, m_Shl(m_Specific(X), m_Specific(Y))))
2352     return Op0;
2353   if (match(Op1,
2354             m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2355       match(Op0, m_Shl(m_Specific(X), m_Specific(Y))))
2356     return Op1;
2357 
2358   // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2359   // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2360   if (match(Op0,
2361             m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2362       match(Op1, m_LShr(m_Specific(X), m_Specific(Y))))
2363     return Op0;
2364   if (match(Op1,
2365             m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2366       match(Op0, m_LShr(m_Specific(X), m_Specific(Y))))
2367     return Op1;
2368 
2369   if (Value *V =
2370           simplifyAndOrWithICmpEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2371     return V;
2372   if (Value *V =
2373           simplifyAndOrWithICmpEq(Instruction::Or, Op1, Op0, Q, MaxRecurse))
2374     return V;
2375 
2376   if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
2377     return V;
2378 
2379   // If we have a multiplication overflow check that is being 'and'ed with a
2380   // check that one of the multipliers is not zero, we can omit the 'and', and
2381   // only keep the overflow check.
2382   if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false))
2383     return Op1;
2384   if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false))
2385     return Op0;
2386 
2387   // Try some generic simplifications for associative operations.
2388   if (Value *V =
2389           simplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2390     return V;
2391 
2392   // Or distributes over And.  Try some generic simplifications based on this.
2393   if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1,
2394                                         Instruction::And, Q, MaxRecurse))
2395     return V;
2396 
2397   if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2398     if (Op0->getType()->isIntOrIntVectorTy(1)) {
2399       // A | (A || B) -> A || B
2400       if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value())))
2401         return Op1;
2402       else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value())))
2403         return Op0;
2404     }
2405     // If the operation is with the result of a select instruction, check
2406     // whether operating on either branch of the select always yields the same
2407     // value.
2408     if (Value *V =
2409             threadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2410       return V;
2411   }
2412 
2413   // (A & C1)|(B & C2)
2414   Value *A, *B;
2415   const APInt *C1, *C2;
2416   if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
2417       match(Op1, m_And(m_Value(B), m_APInt(C2)))) {
2418     if (*C1 == ~*C2) {
2419       // (A & C1)|(B & C2)
2420       // If we have: ((V + N) & C1) | (V & C2)
2421       // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2422       // replace with V+N.
2423       Value *N;
2424       if (C2->isMask() && // C2 == 0+1+
2425           match(A, m_c_Add(m_Specific(B), m_Value(N)))) {
2426         // Add commutes, try both ways.
2427         if (MaskedValueIsZero(N, *C2, Q))
2428           return A;
2429       }
2430       // Or commutes, try both ways.
2431       if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
2432         // Add commutes, try both ways.
2433         if (MaskedValueIsZero(N, *C1, Q))
2434           return B;
2435       }
2436     }
2437   }
2438 
2439   // If the operation is with the result of a phi instruction, check whether
2440   // operating on all incoming values of the phi always yields the same value.
2441   if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2442     if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2443       return V;
2444 
2445   // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2446   if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2447       match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2448     return Constant::getAllOnesValue(Op0->getType());
2449 
2450   if (Op0->getType()->isIntOrIntVectorTy(1)) {
2451     if (std::optional<bool> Implied =
2452             isImpliedCondition(Op0, Op1, Q.DL, false)) {
2453       // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0.
2454       if (*Implied == false)
2455         return Op0;
2456       // If Op0 is false implies Op1 is true, then at least one is always true.
2457       if (*Implied == true)
2458         return ConstantInt::getTrue(Op0->getType());
2459     }
2460     if (std::optional<bool> Implied =
2461             isImpliedCondition(Op1, Op0, Q.DL, false)) {
2462       // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1.
2463       if (*Implied == false)
2464         return Op1;
2465       // If Op1 is false implies Op0 is true, then at least one is always true.
2466       if (*Implied == true)
2467         return ConstantInt::getTrue(Op1->getType());
2468     }
2469   }
2470 
2471   if (Value *V = simplifyByDomEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2472     return V;
2473 
2474   return nullptr;
2475 }
2476 
2477 Value *llvm::simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2478   return ::simplifyOrInst(Op0, Op1, Q, RecursionLimit);
2479 }
2480 
2481 /// Given operands for a Xor, see if we can fold the result.
2482 /// If not, this returns null.
2483 static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2484                               unsigned MaxRecurse) {
2485   if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
2486     return C;
2487 
2488   // X ^ poison -> poison
2489   if (isa<PoisonValue>(Op1))
2490     return Op1;
2491 
2492   // A ^ undef -> undef
2493   if (Q.isUndefValue(Op1))
2494     return Op1;
2495 
2496   // A ^ 0 = A
2497   if (match(Op1, m_Zero()))
2498     return Op0;
2499 
2500   // A ^ A = 0
2501   if (Op0 == Op1)
2502     return Constant::getNullValue(Op0->getType());
2503 
2504   // A ^ ~A  =  ~A ^ A  =  -1
2505   if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
2506     return Constant::getAllOnesValue(Op0->getType());
2507 
2508   auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2509     Value *A, *B;
2510     // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2511     if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
2512         match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2513       return A;
2514 
2515     // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2516     // The 'not' op must contain a complete -1 operand (no undef elements for
2517     // vector) for the transform to be safe.
2518     Value *NotA;
2519     if (match(X, m_c_Or(m_CombineAnd(m_Not(m_Value(A)), m_Value(NotA)),
2520                         m_Value(B))) &&
2521         match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2522       return NotA;
2523 
2524     return nullptr;
2525   };
2526   if (Value *R = foldAndOrNot(Op0, Op1))
2527     return R;
2528   if (Value *R = foldAndOrNot(Op1, Op0))
2529     return R;
2530 
2531   if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
2532     return V;
2533 
2534   // Try some generic simplifications for associative operations.
2535   if (Value *V =
2536           simplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2537     return V;
2538 
2539   // Threading Xor over selects and phi nodes is pointless, so don't bother.
2540   // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2541   // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2542   // only if B and C are equal.  If B and C are equal then (since we assume
2543   // that operands have already been simplified) "select(cond, B, C)" should
2544   // have been simplified to the common value of B and C already.  Analysing
2545   // "A^B" and "A^C" thus gains nothing, but costs compile time.  Similarly
2546   // for threading over phi nodes.
2547 
2548   if (Value *V = simplifyByDomEq(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2549     return V;
2550 
2551   // (xor (sub nuw C_Mask, X), C_Mask) -> X
2552   {
2553     Value *X;
2554     if (match(Op0, m_NUWSub(m_Specific(Op1), m_Value(X))) &&
2555         match(Op1, m_LowBitMask()))
2556       return X;
2557   }
2558 
2559   return nullptr;
2560 }
2561 
2562 Value *llvm::simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2563   return ::simplifyXorInst(Op0, Op1, Q, RecursionLimit);
2564 }
2565 
2566 static Type *getCompareTy(Value *Op) {
2567   return CmpInst::makeCmpResultType(Op->getType());
2568 }
2569 
2570 /// Rummage around inside V looking for something equivalent to the comparison
2571 /// "LHS Pred RHS". Return such a value if found, otherwise return null.
2572 /// Helper function for analyzing max/min idioms.
2573 static Value *extractEquivalentCondition(Value *V, CmpPredicate Pred,
2574                                          Value *LHS, Value *RHS) {
2575   SelectInst *SI = dyn_cast<SelectInst>(V);
2576   if (!SI)
2577     return nullptr;
2578   CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2579   if (!Cmp)
2580     return nullptr;
2581   Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2582   if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2583     return Cmp;
2584   if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2585       LHS == CmpRHS && RHS == CmpLHS)
2586     return Cmp;
2587   return nullptr;
2588 }
2589 
2590 /// Return true if the underlying object (storage) must be disjoint from
2591 /// storage returned by any noalias return call.
2592 static bool isAllocDisjoint(const Value *V) {
2593   // For allocas, we consider only static ones (dynamic
2594   // allocas might be transformed into calls to malloc not simultaneously
2595   // live with the compared-to allocation). For globals, we exclude symbols
2596   // that might be resolve lazily to symbols in another dynamically-loaded
2597   // library (and, thus, could be malloc'ed by the implementation).
2598   if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2599     return AI->isStaticAlloca();
2600   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2601     return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2602             GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2603            !GV->isThreadLocal();
2604   if (const Argument *A = dyn_cast<Argument>(V))
2605     return A->hasByValAttr();
2606   return false;
2607 }
2608 
2609 /// Return true if V1 and V2 are each the base of some distict storage region
2610 /// [V, object_size(V)] which do not overlap.  Note that zero sized regions
2611 /// *are* possible, and that zero sized regions do not overlap with any other.
2612 static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
2613   // Global variables always exist, so they always exist during the lifetime
2614   // of each other and all allocas.  Global variables themselves usually have
2615   // non-overlapping storage, but since their addresses are constants, the
2616   // case involving two globals does not reach here and is instead handled in
2617   // constant folding.
2618   //
2619   // Two different allocas usually have different addresses...
2620   //
2621   // However, if there's an @llvm.stackrestore dynamically in between two
2622   // allocas, they may have the same address. It's tempting to reduce the
2623   // scope of the problem by only looking at *static* allocas here. That would
2624   // cover the majority of allocas while significantly reducing the likelihood
2625   // of having an @llvm.stackrestore pop up in the middle. However, it's not
2626   // actually impossible for an @llvm.stackrestore to pop up in the middle of
2627   // an entry block. Also, if we have a block that's not attached to a
2628   // function, we can't tell if it's "static" under the current definition.
2629   // Theoretically, this problem could be fixed by creating a new kind of
2630   // instruction kind specifically for static allocas. Such a new instruction
2631   // could be required to be at the top of the entry block, thus preventing it
2632   // from being subject to a @llvm.stackrestore. Instcombine could even
2633   // convert regular allocas into these special allocas. It'd be nifty.
2634   // However, until then, this problem remains open.
2635   //
2636   // So, we'll assume that two non-empty allocas have different addresses
2637   // for now.
2638   auto isByValArg = [](const Value *V) {
2639     const Argument *A = dyn_cast<Argument>(V);
2640     return A && A->hasByValAttr();
2641   };
2642 
2643   // Byval args are backed by store which does not overlap with each other,
2644   // allocas, or globals.
2645   if (isByValArg(V1))
2646     return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2647   if (isByValArg(V2))
2648     return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2649 
2650   return isa<AllocaInst>(V1) &&
2651          (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2652 }
2653 
2654 // A significant optimization not implemented here is assuming that alloca
2655 // addresses are not equal to incoming argument values. They don't *alias*,
2656 // as we say, but that doesn't mean they aren't equal, so we take a
2657 // conservative approach.
2658 //
2659 // This is inspired in part by C++11 5.10p1:
2660 //   "Two pointers of the same type compare equal if and only if they are both
2661 //    null, both point to the same function, or both represent the same
2662 //    address."
2663 //
2664 // This is pretty permissive.
2665 //
2666 // It's also partly due to C11 6.5.9p6:
2667 //   "Two pointers compare equal if and only if both are null pointers, both are
2668 //    pointers to the same object (including a pointer to an object and a
2669 //    subobject at its beginning) or function, both are pointers to one past the
2670 //    last element of the same array object, or one is a pointer to one past the
2671 //    end of one array object and the other is a pointer to the start of a
2672 //    different array object that happens to immediately follow the first array
2673 //    object in the address space.)
2674 //
2675 // C11's version is more restrictive, however there's no reason why an argument
2676 // couldn't be a one-past-the-end value for a stack object in the caller and be
2677 // equal to the beginning of a stack object in the callee.
2678 //
2679 // If the C and C++ standards are ever made sufficiently restrictive in this
2680 // area, it may be possible to update LLVM's semantics accordingly and reinstate
2681 // this optimization.
2682 static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS,
2683                                     const SimplifyQuery &Q) {
2684   assert(LHS->getType() == RHS->getType() && "Must have same types");
2685   const DataLayout &DL = Q.DL;
2686   const TargetLibraryInfo *TLI = Q.TLI;
2687 
2688   // We can only fold certain predicates on pointer comparisons.
2689   switch (Pred) {
2690   default:
2691     return nullptr;
2692 
2693     // Equality comparisons are easy to fold.
2694   case CmpInst::ICMP_EQ:
2695   case CmpInst::ICMP_NE:
2696     break;
2697 
2698     // We can only handle unsigned relational comparisons because 'inbounds' on
2699     // a GEP only protects against unsigned wrapping.
2700   case CmpInst::ICMP_UGT:
2701   case CmpInst::ICMP_UGE:
2702   case CmpInst::ICMP_ULT:
2703   case CmpInst::ICMP_ULE:
2704     // However, we have to switch them to their signed variants to handle
2705     // negative indices from the base pointer.
2706     Pred = ICmpInst::getSignedPredicate(Pred);
2707     break;
2708   }
2709 
2710   // Strip off any constant offsets so that we can reason about them.
2711   // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2712   // here and compare base addresses like AliasAnalysis does, however there are
2713   // numerous hazards. AliasAnalysis and its utilities rely on special rules
2714   // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2715   // doesn't need to guarantee pointer inequality when it says NoAlias.
2716 
2717   // Even if an non-inbounds GEP occurs along the path we can still optimize
2718   // equality comparisons concerning the result.
2719   bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2720   unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2721   APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2722   LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2723   RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
2724 
2725   // If LHS and RHS are related via constant offsets to the same base
2726   // value, we can replace it with an icmp which just compares the offsets.
2727   if (LHS == RHS)
2728     return ConstantInt::get(getCompareTy(LHS),
2729                             ICmpInst::compare(LHSOffset, RHSOffset, Pred));
2730 
2731   // Various optimizations for (in)equality comparisons.
2732   if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
2733     // Different non-empty allocations that exist at the same time have
2734     // different addresses (if the program can tell). If the offsets are
2735     // within the bounds of their allocations (and not one-past-the-end!
2736     // so we can't use inbounds!), and their allocations aren't the same,
2737     // the pointers are not equal.
2738     if (haveNonOverlappingStorage(LHS, RHS)) {
2739       uint64_t LHSSize, RHSSize;
2740       ObjectSizeOpts Opts;
2741       Opts.EvalMode = ObjectSizeOpts::Mode::Min;
2742       auto *F = [](Value *V) -> Function * {
2743         if (auto *I = dyn_cast<Instruction>(V))
2744           return I->getFunction();
2745         if (auto *A = dyn_cast<Argument>(V))
2746           return A->getParent();
2747         return nullptr;
2748       }(LHS);
2749       Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2750       if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 &&
2751           getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) {
2752         APInt Dist = LHSOffset - RHSOffset;
2753         if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
2754           return ConstantInt::get(getCompareTy(LHS),
2755                                   !CmpInst::isTrueWhenEqual(Pred));
2756       }
2757     }
2758 
2759     // If one side of the equality comparison must come from a noalias call
2760     // (meaning a system memory allocation function), and the other side must
2761     // come from a pointer that cannot overlap with dynamically-allocated
2762     // memory within the lifetime of the current function (allocas, byval
2763     // arguments, globals), then determine the comparison result here.
2764     SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2765     getUnderlyingObjects(LHS, LHSUObjs);
2766     getUnderlyingObjects(RHS, RHSUObjs);
2767 
2768     // Is the set of underlying objects all noalias calls?
2769     auto IsNAC = [](ArrayRef<const Value *> Objects) {
2770       return all_of(Objects, isNoAliasCall);
2771     };
2772 
2773     // Is the set of underlying objects all things which must be disjoint from
2774     // noalias calls.  We assume that indexing from such disjoint storage
2775     // into the heap is undefined, and thus offsets can be safely ignored.
2776     auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2777       return all_of(Objects, ::isAllocDisjoint);
2778     };
2779 
2780     if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2781         (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2782       return ConstantInt::get(getCompareTy(LHS),
2783                               !CmpInst::isTrueWhenEqual(Pred));
2784 
2785     // Fold comparisons for non-escaping pointer even if the allocation call
2786     // cannot be elided. We cannot fold malloc comparison to null. Also, the
2787     // dynamic allocation call could be either of the operands.  Note that
2788     // the other operand can not be based on the alloc - if it were, then
2789     // the cmp itself would be a capture.
2790     Value *MI = nullptr;
2791     if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonZero(RHS, Q))
2792       MI = LHS;
2793     else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonZero(LHS, Q))
2794       MI = RHS;
2795     if (MI) {
2796       // FIXME: This is incorrect, see PR54002. While we can assume that the
2797       // allocation is at an address that makes the comparison false, this
2798       // requires that *all* comparisons to that address be false, which
2799       // InstSimplify cannot guarantee.
2800       struct CustomCaptureTracker : public CaptureTracker {
2801         bool Captured = false;
2802         void tooManyUses() override { Captured = true; }
2803         bool captured(const Use *U) override {
2804           if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2805             // Comparison against value stored in global variable. Given the
2806             // pointer does not escape, its value cannot be guessed and stored
2807             // separately in a global variable.
2808             unsigned OtherIdx = 1 - U->getOperandNo();
2809             auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2810             if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2811               return false;
2812           }
2813 
2814           Captured = true;
2815           return true;
2816         }
2817       };
2818       CustomCaptureTracker Tracker;
2819       PointerMayBeCaptured(MI, &Tracker);
2820       if (!Tracker.Captured)
2821         return ConstantInt::get(getCompareTy(LHS),
2822                                 CmpInst::isFalseWhenEqual(Pred));
2823     }
2824   }
2825 
2826   // Otherwise, fail.
2827   return nullptr;
2828 }
2829 
2830 /// Fold an icmp when its operands have i1 scalar type.
2831 static Value *simplifyICmpOfBools(CmpPredicate Pred, Value *LHS, Value *RHS,
2832                                   const SimplifyQuery &Q) {
2833   Type *ITy = getCompareTy(LHS); // The return type.
2834   Type *OpTy = LHS->getType();   // The operand type.
2835   if (!OpTy->isIntOrIntVectorTy(1))
2836     return nullptr;
2837 
2838   // A boolean compared to true/false can be reduced in 14 out of the 20
2839   // (10 predicates * 2 constants) possible combinations. The other
2840   // 6 cases require a 'not' of the LHS.
2841 
2842   auto ExtractNotLHS = [](Value *V) -> Value * {
2843     Value *X;
2844     if (match(V, m_Not(m_Value(X))))
2845       return X;
2846     return nullptr;
2847   };
2848 
2849   if (match(RHS, m_Zero())) {
2850     switch (Pred) {
2851     case CmpInst::ICMP_NE:  // X !=  0 -> X
2852     case CmpInst::ICMP_UGT: // X >u  0 -> X
2853     case CmpInst::ICMP_SLT: // X <s  0 -> X
2854       return LHS;
2855 
2856     case CmpInst::ICMP_EQ:  // not(X) ==  0 -> X != 0 -> X
2857     case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2858     case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2859       if (Value *X = ExtractNotLHS(LHS))
2860         return X;
2861       break;
2862 
2863     case CmpInst::ICMP_ULT: // X <u  0 -> false
2864     case CmpInst::ICMP_SGT: // X >s  0 -> false
2865       return getFalse(ITy);
2866 
2867     case CmpInst::ICMP_UGE: // X >=u 0 -> true
2868     case CmpInst::ICMP_SLE: // X <=s 0 -> true
2869       return getTrue(ITy);
2870 
2871     default:
2872       break;
2873     }
2874   } else if (match(RHS, m_One())) {
2875     switch (Pred) {
2876     case CmpInst::ICMP_EQ:  // X ==   1 -> X
2877     case CmpInst::ICMP_UGE: // X >=u  1 -> X
2878     case CmpInst::ICMP_SLE: // X <=s -1 -> X
2879       return LHS;
2880 
2881     case CmpInst::ICMP_NE:  // not(X) !=  1 -> X ==   1 -> X
2882     case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u  1 -> X
2883     case CmpInst::ICMP_SGT: // not(X) >s  1 -> X <=s -1 -> X
2884       if (Value *X = ExtractNotLHS(LHS))
2885         return X;
2886       break;
2887 
2888     case CmpInst::ICMP_UGT: // X >u   1 -> false
2889     case CmpInst::ICMP_SLT: // X <s  -1 -> false
2890       return getFalse(ITy);
2891 
2892     case CmpInst::ICMP_ULE: // X <=u  1 -> true
2893     case CmpInst::ICMP_SGE: // X >=s -1 -> true
2894       return getTrue(ITy);
2895 
2896     default:
2897       break;
2898     }
2899   }
2900 
2901   switch (Pred) {
2902   default:
2903     break;
2904   case ICmpInst::ICMP_UGE:
2905     if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2906       return getTrue(ITy);
2907     break;
2908   case ICmpInst::ICMP_SGE:
2909     /// For signed comparison, the values for an i1 are 0 and -1
2910     /// respectively. This maps into a truth table of:
2911     /// LHS | RHS | LHS >=s RHS   | LHS implies RHS
2912     ///  0  |  0  |  1 (0 >= 0)   |  1
2913     ///  0  |  1  |  1 (0 >= -1)  |  1
2914     ///  1  |  0  |  0 (-1 >= 0)  |  0
2915     ///  1  |  1  |  1 (-1 >= -1) |  1
2916     if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2917       return getTrue(ITy);
2918     break;
2919   case ICmpInst::ICMP_ULE:
2920     if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2921       return getTrue(ITy);
2922     break;
2923   case ICmpInst::ICMP_SLE:
2924     /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2925     if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2926       return getTrue(ITy);
2927     break;
2928   }
2929 
2930   return nullptr;
2931 }
2932 
2933 /// Try hard to fold icmp with zero RHS because this is a common case.
2934 static Value *simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS,
2935                                    const SimplifyQuery &Q) {
2936   if (!match(RHS, m_Zero()))
2937     return nullptr;
2938 
2939   Type *ITy = getCompareTy(LHS); // The return type.
2940   switch (Pred) {
2941   default:
2942     llvm_unreachable("Unknown ICmp predicate!");
2943   case ICmpInst::ICMP_ULT:
2944     return getFalse(ITy);
2945   case ICmpInst::ICMP_UGE:
2946     return getTrue(ITy);
2947   case ICmpInst::ICMP_EQ:
2948   case ICmpInst::ICMP_ULE:
2949     if (isKnownNonZero(LHS, Q))
2950       return getFalse(ITy);
2951     break;
2952   case ICmpInst::ICMP_NE:
2953   case ICmpInst::ICMP_UGT:
2954     if (isKnownNonZero(LHS, Q))
2955       return getTrue(ITy);
2956     break;
2957   case ICmpInst::ICMP_SLT: {
2958     KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2959     if (LHSKnown.isNegative())
2960       return getTrue(ITy);
2961     if (LHSKnown.isNonNegative())
2962       return getFalse(ITy);
2963     break;
2964   }
2965   case ICmpInst::ICMP_SLE: {
2966     KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2967     if (LHSKnown.isNegative())
2968       return getTrue(ITy);
2969     if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2970       return getFalse(ITy);
2971     break;
2972   }
2973   case ICmpInst::ICMP_SGE: {
2974     KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2975     if (LHSKnown.isNegative())
2976       return getFalse(ITy);
2977     if (LHSKnown.isNonNegative())
2978       return getTrue(ITy);
2979     break;
2980   }
2981   case ICmpInst::ICMP_SGT: {
2982     KnownBits LHSKnown = computeKnownBits(LHS, /* Depth */ 0, Q);
2983     if (LHSKnown.isNegative())
2984       return getFalse(ITy);
2985     if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2986       return getTrue(ITy);
2987     break;
2988   }
2989   }
2990 
2991   return nullptr;
2992 }
2993 
2994 static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
2995                                        Value *RHS, const InstrInfoQuery &IIQ) {
2996   Type *ITy = getCompareTy(RHS); // The return type.
2997 
2998   Value *X;
2999   const APInt *C;
3000   if (!match(RHS, m_APIntAllowPoison(C)))
3001     return nullptr;
3002 
3003   // Sign-bit checks can be optimized to true/false after unsigned
3004   // floating-point casts:
3005   // icmp slt (bitcast (uitofp X)),  0 --> false
3006   // icmp sgt (bitcast (uitofp X)), -1 --> true
3007   if (match(LHS, m_ElementWiseBitCast(m_UIToFP(m_Value(X))))) {
3008     bool TrueIfSigned;
3009     if (isSignBitCheck(Pred, *C, TrueIfSigned))
3010       return ConstantInt::getBool(ITy, !TrueIfSigned);
3011   }
3012 
3013   // Rule out tautological comparisons (eg., ult 0 or uge 0).
3014   ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
3015   if (RHS_CR.isEmptySet())
3016     return ConstantInt::getFalse(ITy);
3017   if (RHS_CR.isFullSet())
3018     return ConstantInt::getTrue(ITy);
3019 
3020   ConstantRange LHS_CR =
3021       computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
3022   if (!LHS_CR.isFullSet()) {
3023     if (RHS_CR.contains(LHS_CR))
3024       return ConstantInt::getTrue(ITy);
3025     if (RHS_CR.inverse().contains(LHS_CR))
3026       return ConstantInt::getFalse(ITy);
3027   }
3028 
3029   // (mul nuw/nsw X, MulC) != C --> true  (if C is not a multiple of MulC)
3030   // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3031   const APInt *MulC;
3032   if (IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3033       ((match(LHS, m_NUWMul(m_Value(), m_APIntAllowPoison(MulC))) &&
3034         *MulC != 0 && C->urem(*MulC) != 0) ||
3035        (match(LHS, m_NSWMul(m_Value(), m_APIntAllowPoison(MulC))) &&
3036         *MulC != 0 && C->srem(*MulC) != 0)))
3037     return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
3038 
3039   return nullptr;
3040 }
3041 
3042 enum class MonotonicType { GreaterEq, LowerEq };
3043 
3044 /// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
3045 static void getUnsignedMonotonicValues(SmallPtrSetImpl<Value *> &Res, Value *V,
3046                                        MonotonicType Type, unsigned Depth = 0) {
3047   if (!Res.insert(V).second)
3048     return;
3049 
3050   // Can be increased if useful.
3051   if (++Depth > 1)
3052     return;
3053 
3054   auto *I = dyn_cast<Instruction>(V);
3055   if (!I)
3056     return;
3057 
3058   Value *X, *Y;
3059   if (Type == MonotonicType::GreaterEq) {
3060     if (match(I, m_Or(m_Value(X), m_Value(Y))) ||
3061         match(I, m_Intrinsic<Intrinsic::uadd_sat>(m_Value(X), m_Value(Y)))) {
3062       getUnsignedMonotonicValues(Res, X, Type, Depth);
3063       getUnsignedMonotonicValues(Res, Y, Type, Depth);
3064     }
3065   } else {
3066     assert(Type == MonotonicType::LowerEq);
3067     switch (I->getOpcode()) {
3068     case Instruction::And:
3069       getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Depth);
3070       getUnsignedMonotonicValues(Res, I->getOperand(1), Type, Depth);
3071       break;
3072     case Instruction::URem:
3073     case Instruction::UDiv:
3074     case Instruction::LShr:
3075       getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Depth);
3076       break;
3077     case Instruction::Call:
3078       if (match(I, m_Intrinsic<Intrinsic::usub_sat>(m_Value(X))))
3079         getUnsignedMonotonicValues(Res, X, Type, Depth);
3080       break;
3081     default:
3082       break;
3083     }
3084   }
3085 }
3086 
3087 static Value *simplifyICmpUsingMonotonicValues(CmpPredicate Pred, Value *LHS,
3088                                                Value *RHS) {
3089   if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT)
3090     return nullptr;
3091 
3092   // We have LHS uge GreaterValues and LowerValues uge RHS. If any of the
3093   // GreaterValues and LowerValues are the same, it follows that LHS uge RHS.
3094   SmallPtrSet<Value *, 4> GreaterValues;
3095   SmallPtrSet<Value *, 4> LowerValues;
3096   getUnsignedMonotonicValues(GreaterValues, LHS, MonotonicType::GreaterEq);
3097   getUnsignedMonotonicValues(LowerValues, RHS, MonotonicType::LowerEq);
3098   for (Value *GV : GreaterValues)
3099     if (LowerValues.contains(GV))
3100       return ConstantInt::getBool(getCompareTy(LHS),
3101                                   Pred == ICmpInst::ICMP_UGE);
3102   return nullptr;
3103 }
3104 
3105 static Value *simplifyICmpWithBinOpOnLHS(CmpPredicate Pred, BinaryOperator *LBO,
3106                                          Value *RHS, const SimplifyQuery &Q,
3107                                          unsigned MaxRecurse) {
3108   Type *ITy = getCompareTy(RHS); // The return type.
3109 
3110   Value *Y = nullptr;
3111   // icmp pred (or X, Y), X
3112   if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
3113     if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3114       KnownBits RHSKnown = computeKnownBits(RHS, /* Depth */ 0, Q);
3115       KnownBits YKnown = computeKnownBits(Y, /* Depth */ 0, Q);
3116       if (RHSKnown.isNonNegative() && YKnown.isNegative())
3117         return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
3118       if (RHSKnown.isNegative() || YKnown.isNonNegative())
3119         return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
3120     }
3121   }
3122 
3123   // icmp pred (urem X, Y), Y
3124   if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
3125     switch (Pred) {
3126     default:
3127       break;
3128     case ICmpInst::ICMP_SGT:
3129     case ICmpInst::ICMP_SGE: {
3130       KnownBits Known = computeKnownBits(RHS, /* Depth */ 0, Q);
3131       if (!Known.isNonNegative())
3132         break;
3133       [[fallthrough]];
3134     }
3135     case ICmpInst::ICMP_EQ:
3136     case ICmpInst::ICMP_UGT:
3137     case ICmpInst::ICMP_UGE:
3138       return getFalse(ITy);
3139     case ICmpInst::ICMP_SLT:
3140     case ICmpInst::ICMP_SLE: {
3141       KnownBits Known = computeKnownBits(RHS, /* Depth */ 0, Q);
3142       if (!Known.isNonNegative())
3143         break;
3144       [[fallthrough]];
3145     }
3146     case ICmpInst::ICMP_NE:
3147     case ICmpInst::ICMP_ULT:
3148     case ICmpInst::ICMP_ULE:
3149       return getTrue(ITy);
3150     }
3151   }
3152 
3153   // If x is nonzero:
3154   // x >>u C <u  x --> true  for C != 0.
3155   // x >>u C !=  x --> true  for C != 0.
3156   // x >>u C >=u x --> false for C != 0.
3157   // x >>u C ==  x --> false for C != 0.
3158   // x udiv C <u  x --> true  for C != 1.
3159   // x udiv C !=  x --> true  for C != 1.
3160   // x udiv C >=u x --> false for C != 1.
3161   // x udiv C ==  x --> false for C != 1.
3162   // TODO: allow non-constant shift amount/divisor
3163   const APInt *C;
3164   if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
3165       (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
3166     if (isKnownNonZero(RHS, Q)) {
3167       switch (Pred) {
3168       default:
3169         break;
3170       case ICmpInst::ICMP_EQ:
3171       case ICmpInst::ICMP_UGE:
3172       case ICmpInst::ICMP_UGT:
3173         return getFalse(ITy);
3174       case ICmpInst::ICMP_NE:
3175       case ICmpInst::ICMP_ULT:
3176       case ICmpInst::ICMP_ULE:
3177         return getTrue(ITy);
3178       }
3179     }
3180   }
3181 
3182   // (x*C1)/C2 <= x for C1 <= C2.
3183   // This holds even if the multiplication overflows: Assume that x != 0 and
3184   // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3185   // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3186   //
3187   // Additionally, either the multiplication and division might be represented
3188   // as shifts:
3189   // (x*C1)>>C2 <= x for C1 < 2**C2.
3190   // (x<<C1)/C2 <= x for 2**C1 < C2.
3191   const APInt *C1, *C2;
3192   if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3193        C1->ule(*C2)) ||
3194       (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3195        C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3196       (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3197        (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3198     if (Pred == ICmpInst::ICMP_UGT)
3199       return getFalse(ITy);
3200     if (Pred == ICmpInst::ICMP_ULE)
3201       return getTrue(ITy);
3202   }
3203 
3204   // (sub C, X) == X, C is odd  --> false
3205   // (sub C, X) != X, C is odd  --> true
3206   if (match(LBO, m_Sub(m_APIntAllowPoison(C), m_Specific(RHS))) &&
3207       (*C & 1) == 1 && ICmpInst::isEquality(Pred))
3208     return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy);
3209 
3210   return nullptr;
3211 }
3212 
3213 // If only one of the icmp's operands has NSW flags, try to prove that:
3214 //
3215 //   icmp slt (x + C1), (x +nsw C2)
3216 //
3217 // is equivalent to:
3218 //
3219 //   icmp slt C1, C2
3220 //
3221 // which is true if x + C2 has the NSW flags set and:
3222 // *) C1 < C2 && C1 >= 0, or
3223 // *) C2 < C1 && C1 <= 0.
3224 //
3225 static bool trySimplifyICmpWithAdds(CmpPredicate Pred, Value *LHS, Value *RHS,
3226                                     const InstrInfoQuery &IIQ) {
3227   // TODO: only support icmp slt for now.
3228   if (Pred != CmpInst::ICMP_SLT || !IIQ.UseInstrInfo)
3229     return false;
3230 
3231   // Canonicalize nsw add as RHS.
3232   if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3233     std::swap(LHS, RHS);
3234   if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3235     return false;
3236 
3237   Value *X;
3238   const APInt *C1, *C2;
3239   if (!match(LHS, m_Add(m_Value(X), m_APInt(C1))) ||
3240       !match(RHS, m_Add(m_Specific(X), m_APInt(C2))))
3241     return false;
3242 
3243   return (C1->slt(*C2) && C1->isNonNegative()) ||
3244          (C2->slt(*C1) && C1->isNonPositive());
3245 }
3246 
3247 /// TODO: A large part of this logic is duplicated in InstCombine's
3248 /// foldICmpBinOp(). We should be able to share that and avoid the code
3249 /// duplication.
3250 static Value *simplifyICmpWithBinOp(CmpPredicate Pred, Value *LHS, Value *RHS,
3251                                     const SimplifyQuery &Q,
3252                                     unsigned MaxRecurse) {
3253   BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
3254   BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
3255   if (MaxRecurse && (LBO || RBO)) {
3256     // Analyze the case when either LHS or RHS is an add instruction.
3257     Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3258     // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3259     bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3260     if (LBO && LBO->getOpcode() == Instruction::Add) {
3261       A = LBO->getOperand(0);
3262       B = LBO->getOperand(1);
3263       NoLHSWrapProblem =
3264           ICmpInst::isEquality(Pred) ||
3265           (CmpInst::isUnsigned(Pred) &&
3266            Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))) ||
3267           (CmpInst::isSigned(Pred) &&
3268            Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)));
3269     }
3270     if (RBO && RBO->getOpcode() == Instruction::Add) {
3271       C = RBO->getOperand(0);
3272       D = RBO->getOperand(1);
3273       NoRHSWrapProblem =
3274           ICmpInst::isEquality(Pred) ||
3275           (CmpInst::isUnsigned(Pred) &&
3276            Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))) ||
3277           (CmpInst::isSigned(Pred) &&
3278            Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO)));
3279     }
3280 
3281     // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3282     if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3283       if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A,
3284                                       Constant::getNullValue(RHS->getType()), Q,
3285                                       MaxRecurse - 1))
3286         return V;
3287 
3288     // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3289     if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3290       if (Value *V =
3291               simplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
3292                                C == LHS ? D : C, Q, MaxRecurse - 1))
3293         return V;
3294 
3295     // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3296     bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3297                        trySimplifyICmpWithAdds(Pred, LHS, RHS, Q.IIQ);
3298     if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3299       // Determine Y and Z in the form icmp (X+Y), (X+Z).
3300       Value *Y, *Z;
3301       if (A == C) {
3302         // C + B == C + D  ->  B == D
3303         Y = B;
3304         Z = D;
3305       } else if (A == D) {
3306         // D + B == C + D  ->  B == C
3307         Y = B;
3308         Z = C;
3309       } else if (B == C) {
3310         // A + C == C + D  ->  A == D
3311         Y = A;
3312         Z = D;
3313       } else {
3314         assert(B == D);
3315         // A + D == C + D  ->  A == C
3316         Y = A;
3317         Z = C;
3318       }
3319       if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3320         return V;
3321     }
3322   }
3323 
3324   if (LBO)
3325     if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3326       return V;
3327 
3328   if (RBO)
3329     if (Value *V = simplifyICmpWithBinOpOnLHS(
3330             ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
3331       return V;
3332 
3333   // 0 - (zext X) pred C
3334   if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
3335     const APInt *C;
3336     if (match(RHS, m_APInt(C))) {
3337       if (C->isStrictlyPositive()) {
3338         if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3339           return ConstantInt::getTrue(getCompareTy(RHS));
3340         if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3341           return ConstantInt::getFalse(getCompareTy(RHS));
3342       }
3343       if (C->isNonNegative()) {
3344         if (Pred == ICmpInst::ICMP_SLE)
3345           return ConstantInt::getTrue(getCompareTy(RHS));
3346         if (Pred == ICmpInst::ICMP_SGT)
3347           return ConstantInt::getFalse(getCompareTy(RHS));
3348       }
3349     }
3350   }
3351 
3352   //   If C2 is a power-of-2 and C is not:
3353   //   (C2 << X) == C --> false
3354   //   (C2 << X) != C --> true
3355   const APInt *C;
3356   if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
3357       match(RHS, m_APIntAllowPoison(C)) && !C->isPowerOf2()) {
3358     // C2 << X can equal zero in some circumstances.
3359     // This simplification might be unsafe if C is zero.
3360     //
3361     // We know it is safe if:
3362     // - The shift is nsw. We can't shift out the one bit.
3363     // - The shift is nuw. We can't shift out the one bit.
3364     // - C2 is one.
3365     // - C isn't zero.
3366     if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3367         Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3368         match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3369       if (Pred == ICmpInst::ICMP_EQ)
3370         return ConstantInt::getFalse(getCompareTy(RHS));
3371       if (Pred == ICmpInst::ICMP_NE)
3372         return ConstantInt::getTrue(getCompareTy(RHS));
3373     }
3374   }
3375 
3376   // If C is a power-of-2:
3377   // (C << X)  >u 0x8000 --> false
3378   // (C << X) <=u 0x8000 --> true
3379   if (match(LHS, m_Shl(m_Power2(), m_Value())) && match(RHS, m_SignMask())) {
3380     if (Pred == ICmpInst::ICMP_UGT)
3381       return ConstantInt::getFalse(getCompareTy(RHS));
3382     if (Pred == ICmpInst::ICMP_ULE)
3383       return ConstantInt::getTrue(getCompareTy(RHS));
3384   }
3385 
3386   if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3387     return nullptr;
3388 
3389   if (LBO->getOperand(0) == RBO->getOperand(0)) {
3390     switch (LBO->getOpcode()) {
3391     default:
3392       break;
3393     case Instruction::Shl: {
3394       bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3395       bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3396       if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3397           !isKnownNonZero(LBO->getOperand(0), Q))
3398         break;
3399       if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
3400                                       RBO->getOperand(1), Q, MaxRecurse - 1))
3401         return V;
3402       break;
3403     }
3404     // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2:
3405     // icmp ule A, B -> true
3406     // icmp ugt A, B -> false
3407     // icmp sle A, B -> true (C1 and C2 are the same sign)
3408     // icmp sgt A, B -> false (C1 and C2 are the same sign)
3409     case Instruction::And:
3410     case Instruction::Or: {
3411       const APInt *C1, *C2;
3412       if (ICmpInst::isRelational(Pred) &&
3413           match(LBO->getOperand(1), m_APInt(C1)) &&
3414           match(RBO->getOperand(1), m_APInt(C2))) {
3415         if (!C1->isSubsetOf(*C2)) {
3416           std::swap(C1, C2);
3417           Pred = ICmpInst::getSwappedPredicate(Pred);
3418         }
3419         if (C1->isSubsetOf(*C2)) {
3420           if (Pred == ICmpInst::ICMP_ULE)
3421             return ConstantInt::getTrue(getCompareTy(LHS));
3422           if (Pred == ICmpInst::ICMP_UGT)
3423             return ConstantInt::getFalse(getCompareTy(LHS));
3424           if (C1->isNonNegative() == C2->isNonNegative()) {
3425             if (Pred == ICmpInst::ICMP_SLE)
3426               return ConstantInt::getTrue(getCompareTy(LHS));
3427             if (Pred == ICmpInst::ICMP_SGT)
3428               return ConstantInt::getFalse(getCompareTy(LHS));
3429           }
3430         }
3431       }
3432       break;
3433     }
3434     }
3435   }
3436 
3437   if (LBO->getOperand(1) == RBO->getOperand(1)) {
3438     switch (LBO->getOpcode()) {
3439     default:
3440       break;
3441     case Instruction::UDiv:
3442     case Instruction::LShr:
3443       if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
3444           !Q.IIQ.isExact(RBO))
3445         break;
3446       if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3447                                       RBO->getOperand(0), Q, MaxRecurse - 1))
3448         return V;
3449       break;
3450     case Instruction::SDiv:
3451       if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
3452           !Q.IIQ.isExact(RBO))
3453         break;
3454       if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3455                                       RBO->getOperand(0), Q, MaxRecurse - 1))
3456         return V;
3457       break;
3458     case Instruction::AShr:
3459       if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
3460         break;
3461       if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3462                                       RBO->getOperand(0), Q, MaxRecurse - 1))
3463         return V;
3464       break;
3465     case Instruction::Shl: {
3466       bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3467       bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3468       if (!NUW && !NSW)
3469         break;
3470       if (!NSW && ICmpInst::isSigned(Pred))
3471         break;
3472       if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3473                                       RBO->getOperand(0), Q, MaxRecurse - 1))
3474         return V;
3475       break;
3476     }
3477     }
3478   }
3479   return nullptr;
3480 }
3481 
3482 /// simplify integer comparisons where at least one operand of the compare
3483 /// matches an integer min/max idiom.
3484 static Value *simplifyICmpWithMinMax(CmpPredicate Pred, Value *LHS, Value *RHS,
3485                                      const SimplifyQuery &Q,
3486                                      unsigned MaxRecurse) {
3487   Type *ITy = getCompareTy(LHS); // The return type.
3488   Value *A, *B;
3489   CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
3490   CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3491 
3492   // Signed variants on "max(a,b)>=a -> true".
3493   if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3494     if (A != RHS)
3495       std::swap(A, B);       // smax(A, B) pred A.
3496     EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3497     // We analyze this as smax(A, B) pred A.
3498     P = Pred;
3499   } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
3500              (A == LHS || B == LHS)) {
3501     if (A != LHS)
3502       std::swap(A, B);       // A pred smax(A, B).
3503     EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3504     // We analyze this as smax(A, B) swapped-pred A.
3505     P = CmpInst::getSwappedPredicate(Pred);
3506   } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
3507              (A == RHS || B == RHS)) {
3508     if (A != RHS)
3509       std::swap(A, B);       // smin(A, B) pred A.
3510     EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3511     // We analyze this as smax(-A, -B) swapped-pred -A.
3512     // Note that we do not need to actually form -A or -B thanks to EqP.
3513     P = CmpInst::getSwappedPredicate(Pred);
3514   } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
3515              (A == LHS || B == LHS)) {
3516     if (A != LHS)
3517       std::swap(A, B);       // A pred smin(A, B).
3518     EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3519     // We analyze this as smax(-A, -B) pred -A.
3520     // Note that we do not need to actually form -A or -B thanks to EqP.
3521     P = Pred;
3522   }
3523   if (P != CmpInst::BAD_ICMP_PREDICATE) {
3524     // Cases correspond to "max(A, B) p A".
3525     switch (P) {
3526     default:
3527       break;
3528     case CmpInst::ICMP_EQ:
3529     case CmpInst::ICMP_SLE:
3530       // Equivalent to "A EqP B".  This may be the same as the condition tested
3531       // in the max/min; if so, we can just return that.
3532       if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3533         return V;
3534       if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3535         return V;
3536       // Otherwise, see if "A EqP B" simplifies.
3537       if (MaxRecurse)
3538         if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3539           return V;
3540       break;
3541     case CmpInst::ICMP_NE:
3542     case CmpInst::ICMP_SGT: {
3543       CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
3544       // Equivalent to "A InvEqP B".  This may be the same as the condition
3545       // tested in the max/min; if so, we can just return that.
3546       if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3547         return V;
3548       if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3549         return V;
3550       // Otherwise, see if "A InvEqP B" simplifies.
3551       if (MaxRecurse)
3552         if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3553           return V;
3554       break;
3555     }
3556     case CmpInst::ICMP_SGE:
3557       // Always true.
3558       return getTrue(ITy);
3559     case CmpInst::ICMP_SLT:
3560       // Always false.
3561       return getFalse(ITy);
3562     }
3563   }
3564 
3565   // Unsigned variants on "max(a,b)>=a -> true".
3566   P = CmpInst::BAD_ICMP_PREDICATE;
3567   if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3568     if (A != RHS)
3569       std::swap(A, B);       // umax(A, B) pred A.
3570     EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3571     // We analyze this as umax(A, B) pred A.
3572     P = Pred;
3573   } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
3574              (A == LHS || B == LHS)) {
3575     if (A != LHS)
3576       std::swap(A, B);       // A pred umax(A, B).
3577     EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3578     // We analyze this as umax(A, B) swapped-pred A.
3579     P = CmpInst::getSwappedPredicate(Pred);
3580   } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3581              (A == RHS || B == RHS)) {
3582     if (A != RHS)
3583       std::swap(A, B);       // umin(A, B) pred A.
3584     EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3585     // We analyze this as umax(-A, -B) swapped-pred -A.
3586     // Note that we do not need to actually form -A or -B thanks to EqP.
3587     P = CmpInst::getSwappedPredicate(Pred);
3588   } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
3589              (A == LHS || B == LHS)) {
3590     if (A != LHS)
3591       std::swap(A, B);       // A pred umin(A, B).
3592     EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3593     // We analyze this as umax(-A, -B) pred -A.
3594     // Note that we do not need to actually form -A or -B thanks to EqP.
3595     P = Pred;
3596   }
3597   if (P != CmpInst::BAD_ICMP_PREDICATE) {
3598     // Cases correspond to "max(A, B) p A".
3599     switch (P) {
3600     default:
3601       break;
3602     case CmpInst::ICMP_EQ:
3603     case CmpInst::ICMP_ULE:
3604       // Equivalent to "A EqP B".  This may be the same as the condition tested
3605       // in the max/min; if so, we can just return that.
3606       if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3607         return V;
3608       if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3609         return V;
3610       // Otherwise, see if "A EqP B" simplifies.
3611       if (MaxRecurse)
3612         if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3613           return V;
3614       break;
3615     case CmpInst::ICMP_NE:
3616     case CmpInst::ICMP_UGT: {
3617       CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
3618       // Equivalent to "A InvEqP B".  This may be the same as the condition
3619       // tested in the max/min; if so, we can just return that.
3620       if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3621         return V;
3622       if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3623         return V;
3624       // Otherwise, see if "A InvEqP B" simplifies.
3625       if (MaxRecurse)
3626         if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3627           return V;
3628       break;
3629     }
3630     case CmpInst::ICMP_UGE:
3631       return getTrue(ITy);
3632     case CmpInst::ICMP_ULT:
3633       return getFalse(ITy);
3634     }
3635   }
3636 
3637   // Comparing 1 each of min/max with a common operand?
3638   // Canonicalize min operand to RHS.
3639   if (match(LHS, m_UMin(m_Value(), m_Value())) ||
3640       match(LHS, m_SMin(m_Value(), m_Value()))) {
3641     std::swap(LHS, RHS);
3642     Pred = ICmpInst::getSwappedPredicate(Pred);
3643   }
3644 
3645   Value *C, *D;
3646   if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
3647       match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
3648       (A == C || A == D || B == C || B == D)) {
3649     // smax(A, B) >=s smin(A, D) --> true
3650     if (Pred == CmpInst::ICMP_SGE)
3651       return getTrue(ITy);
3652     // smax(A, B) <s smin(A, D) --> false
3653     if (Pred == CmpInst::ICMP_SLT)
3654       return getFalse(ITy);
3655   } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3656              match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3657              (A == C || A == D || B == C || B == D)) {
3658     // umax(A, B) >=u umin(A, D) --> true
3659     if (Pred == CmpInst::ICMP_UGE)
3660       return getTrue(ITy);
3661     // umax(A, B) <u umin(A, D) --> false
3662     if (Pred == CmpInst::ICMP_ULT)
3663       return getFalse(ITy);
3664   }
3665 
3666   return nullptr;
3667 }
3668 
3669 static Value *simplifyICmpWithDominatingAssume(CmpPredicate Predicate,
3670                                                Value *LHS, Value *RHS,
3671                                                const SimplifyQuery &Q) {
3672   // Gracefully handle instructions that have not been inserted yet.
3673   if (!Q.AC || !Q.CxtI)
3674     return nullptr;
3675 
3676   for (Value *AssumeBaseOp : {LHS, RHS}) {
3677     for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3678       if (!AssumeVH)
3679         continue;
3680 
3681       CallInst *Assume = cast<CallInst>(AssumeVH);
3682       if (std::optional<bool> Imp = isImpliedCondition(
3683               Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL))
3684         if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
3685           return ConstantInt::get(getCompareTy(LHS), *Imp);
3686     }
3687   }
3688 
3689   return nullptr;
3690 }
3691 
3692 static Value *simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred, Value *LHS,
3693                                              Value *RHS) {
3694   auto *II = dyn_cast<IntrinsicInst>(LHS);
3695   if (!II)
3696     return nullptr;
3697 
3698   switch (II->getIntrinsicID()) {
3699   case Intrinsic::uadd_sat:
3700     // uadd.sat(X, Y) uge X + Y
3701     if (match(RHS, m_c_Add(m_Specific(II->getArgOperand(0)),
3702                            m_Specific(II->getArgOperand(1))))) {
3703       if (Pred == ICmpInst::ICMP_UGE)
3704         return ConstantInt::getTrue(getCompareTy(II));
3705       if (Pred == ICmpInst::ICMP_ULT)
3706         return ConstantInt::getFalse(getCompareTy(II));
3707     }
3708     return nullptr;
3709   case Intrinsic::usub_sat:
3710     // usub.sat(X, Y) ule X - Y
3711     if (match(RHS, m_Sub(m_Specific(II->getArgOperand(0)),
3712                          m_Specific(II->getArgOperand(1))))) {
3713       if (Pred == ICmpInst::ICMP_ULE)
3714         return ConstantInt::getTrue(getCompareTy(II));
3715       if (Pred == ICmpInst::ICMP_UGT)
3716         return ConstantInt::getFalse(getCompareTy(II));
3717     }
3718     return nullptr;
3719   default:
3720     return nullptr;
3721   }
3722 }
3723 
3724 /// Helper method to get range from metadata or attribute.
3725 static std::optional<ConstantRange> getRange(Value *V,
3726                                              const InstrInfoQuery &IIQ) {
3727   if (Instruction *I = dyn_cast<Instruction>(V))
3728     if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
3729       return getConstantRangeFromMetadata(*MD);
3730 
3731   if (const Argument *A = dyn_cast<Argument>(V))
3732     return A->getRange();
3733   else if (const CallBase *CB = dyn_cast<CallBase>(V))
3734     return CB->getRange();
3735 
3736   return std::nullopt;
3737 }
3738 
3739 /// Given operands for an ICmpInst, see if we can fold the result.
3740 /// If not, this returns null.
3741 static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
3742                                const SimplifyQuery &Q, unsigned MaxRecurse) {
3743   assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3744 
3745   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3746     if (Constant *CRHS = dyn_cast<Constant>(RHS))
3747       return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3748 
3749     // If we have a constant, make sure it is on the RHS.
3750     std::swap(LHS, RHS);
3751     Pred = CmpInst::getSwappedPredicate(Pred);
3752   }
3753   assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3754 
3755   Type *ITy = getCompareTy(LHS); // The return type.
3756 
3757   // icmp poison, X -> poison
3758   if (isa<PoisonValue>(RHS))
3759     return PoisonValue::get(ITy);
3760 
3761   // For EQ and NE, we can always pick a value for the undef to make the
3762   // predicate pass or fail, so we can return undef.
3763   // Matches behavior in llvm::ConstantFoldCompareInstruction.
3764   if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
3765     return UndefValue::get(ITy);
3766 
3767   // icmp X, X -> true/false
3768   // icmp X, undef -> true/false because undef could be X.
3769   if (LHS == RHS || Q.isUndefValue(RHS))
3770     return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3771 
3772   if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3773     return V;
3774 
3775   // TODO: Sink/common this with other potentially expensive calls that use
3776   //       ValueTracking? See comment below for isKnownNonEqual().
3777   if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3778     return V;
3779 
3780   if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3781     return V;
3782 
3783   // If both operands have range metadata, use the metadata
3784   // to simplify the comparison.
3785   if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ))
3786     if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) {
3787       if (LhsCr->icmp(Pred, *RhsCr))
3788         return ConstantInt::getTrue(ITy);
3789 
3790       if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
3791         return ConstantInt::getFalse(ITy);
3792     }
3793 
3794   // Compare of cast, for example (zext X) != 0 -> X != 0
3795   if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3796     Instruction *LI = cast<CastInst>(LHS);
3797     Value *SrcOp = LI->getOperand(0);
3798     Type *SrcTy = SrcOp->getType();
3799     Type *DstTy = LI->getType();
3800 
3801     // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3802     // if the integer type is the same size as the pointer type.
3803     if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3804         Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3805       if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3806         // Transfer the cast to the constant.
3807         if (Value *V = simplifyICmpInst(Pred, SrcOp,
3808                                         ConstantExpr::getIntToPtr(RHSC, SrcTy),
3809                                         Q, MaxRecurse - 1))
3810           return V;
3811       } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3812         if (RI->getOperand(0)->getType() == SrcTy)
3813           // Compare without the cast.
3814           if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3815                                           MaxRecurse - 1))
3816             return V;
3817       }
3818     }
3819 
3820     if (isa<ZExtInst>(LHS)) {
3821       // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3822       // same type.
3823       if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3824         if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3825           // Compare X and Y.  Note that signed predicates become unsigned.
3826           if (Value *V =
3827                   simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), SrcOp,
3828                                    RI->getOperand(0), Q, MaxRecurse - 1))
3829             return V;
3830       }
3831       // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3832       else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3833         if (SrcOp == RI->getOperand(0)) {
3834           if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3835             return ConstantInt::getTrue(ITy);
3836           if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3837             return ConstantInt::getFalse(ITy);
3838         }
3839       }
3840       // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3841       // too.  If not, then try to deduce the result of the comparison.
3842       else if (match(RHS, m_ImmConstant())) {
3843         Constant *C = dyn_cast<Constant>(RHS);
3844         assert(C != nullptr);
3845 
3846         // Compute the constant that would happen if we truncated to SrcTy then
3847         // reextended to DstTy.
3848         Constant *Trunc =
3849             ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3850         assert(Trunc && "Constant-fold of ImmConstant should not fail");
3851         Constant *RExt =
3852             ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL);
3853         assert(RExt && "Constant-fold of ImmConstant should not fail");
3854         Constant *AnyEq =
3855             ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3856         assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3857 
3858         // If the re-extended constant didn't change any of the elements then
3859         // this is effectively also a case of comparing two zero-extended
3860         // values.
3861         if (AnyEq->isAllOnesValue() && MaxRecurse)
3862           if (Value *V = simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3863                                           SrcOp, Trunc, Q, MaxRecurse - 1))
3864             return V;
3865 
3866         // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3867         // there.  Use this to work out the result of the comparison.
3868         if (AnyEq->isNullValue()) {
3869           switch (Pred) {
3870           default:
3871             llvm_unreachable("Unknown ICmp predicate!");
3872           // LHS <u RHS.
3873           case ICmpInst::ICMP_EQ:
3874           case ICmpInst::ICMP_UGT:
3875           case ICmpInst::ICMP_UGE:
3876             return Constant::getNullValue(ITy);
3877 
3878           case ICmpInst::ICMP_NE:
3879           case ICmpInst::ICMP_ULT:
3880           case ICmpInst::ICMP_ULE:
3881             return Constant::getAllOnesValue(ITy);
3882 
3883           // LHS is non-negative.  If RHS is negative then LHS >s LHS.  If RHS
3884           // is non-negative then LHS <s RHS.
3885           case ICmpInst::ICMP_SGT:
3886           case ICmpInst::ICMP_SGE:
3887             return ConstantFoldCompareInstOperands(
3888                 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3889                 Q.DL);
3890           case ICmpInst::ICMP_SLT:
3891           case ICmpInst::ICMP_SLE:
3892             return ConstantFoldCompareInstOperands(
3893                 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3894                 Q.DL);
3895           }
3896         }
3897       }
3898     }
3899 
3900     if (isa<SExtInst>(LHS)) {
3901       // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3902       // same type.
3903       if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3904         if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3905           // Compare X and Y.  Note that the predicate does not change.
3906           if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3907                                           MaxRecurse - 1))
3908             return V;
3909       }
3910       // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
3911       else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3912         if (SrcOp == RI->getOperand(0)) {
3913           if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3914             return ConstantInt::getTrue(ITy);
3915           if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3916             return ConstantInt::getFalse(ITy);
3917         }
3918       }
3919       // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3920       // too.  If not, then try to deduce the result of the comparison.
3921       else if (match(RHS, m_ImmConstant())) {
3922         Constant *C = cast<Constant>(RHS);
3923 
3924         // Compute the constant that would happen if we truncated to SrcTy then
3925         // reextended to DstTy.
3926         Constant *Trunc =
3927             ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3928         assert(Trunc && "Constant-fold of ImmConstant should not fail");
3929         Constant *RExt =
3930             ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL);
3931         assert(RExt && "Constant-fold of ImmConstant should not fail");
3932         Constant *AnyEq =
3933             ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3934         assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3935 
3936         // If the re-extended constant didn't change then this is effectively
3937         // also a case of comparing two sign-extended values.
3938         if (AnyEq->isAllOnesValue() && MaxRecurse)
3939           if (Value *V =
3940                   simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3941             return V;
3942 
3943         // Otherwise the upper bits of LHS are all equal, while RHS has varying
3944         // bits there.  Use this to work out the result of the comparison.
3945         if (AnyEq->isNullValue()) {
3946           switch (Pred) {
3947           default:
3948             llvm_unreachable("Unknown ICmp predicate!");
3949           case ICmpInst::ICMP_EQ:
3950             return Constant::getNullValue(ITy);
3951           case ICmpInst::ICMP_NE:
3952             return Constant::getAllOnesValue(ITy);
3953 
3954           // If RHS is non-negative then LHS <s RHS.  If RHS is negative then
3955           // LHS >s RHS.
3956           case ICmpInst::ICMP_SGT:
3957           case ICmpInst::ICMP_SGE:
3958             return ConstantFoldCompareInstOperands(
3959                 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3960                 Q.DL);
3961           case ICmpInst::ICMP_SLT:
3962           case ICmpInst::ICMP_SLE:
3963             return ConstantFoldCompareInstOperands(
3964                 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3965                 Q.DL);
3966 
3967           // If LHS is non-negative then LHS <u RHS.  If LHS is negative then
3968           // LHS >u RHS.
3969           case ICmpInst::ICMP_UGT:
3970           case ICmpInst::ICMP_UGE:
3971             // Comparison is true iff the LHS <s 0.
3972             if (MaxRecurse)
3973               if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3974                                               Constant::getNullValue(SrcTy), Q,
3975                                               MaxRecurse - 1))
3976                 return V;
3977             break;
3978           case ICmpInst::ICMP_ULT:
3979           case ICmpInst::ICMP_ULE:
3980             // Comparison is true iff the LHS >=s 0.
3981             if (MaxRecurse)
3982               if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3983                                               Constant::getNullValue(SrcTy), Q,
3984                                               MaxRecurse - 1))
3985                 return V;
3986             break;
3987           }
3988         }
3989       }
3990     }
3991   }
3992 
3993   // icmp eq|ne X, Y -> false|true if X != Y
3994   // This is potentially expensive, and we have already computedKnownBits for
3995   // compares with 0 above here, so only try this for a non-zero compare.
3996   if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
3997       isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo)) {
3998     return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
3999   }
4000 
4001   if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
4002     return V;
4003 
4004   if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
4005     return V;
4006 
4007   if (Value *V = simplifyICmpWithIntrinsicOnLHS(Pred, LHS, RHS))
4008     return V;
4009   if (Value *V = simplifyICmpWithIntrinsicOnLHS(
4010           ICmpInst::getSwappedPredicate(Pred), RHS, LHS))
4011     return V;
4012 
4013   if (Value *V = simplifyICmpUsingMonotonicValues(Pred, LHS, RHS))
4014     return V;
4015   if (Value *V = simplifyICmpUsingMonotonicValues(
4016           ICmpInst::getSwappedPredicate(Pred), RHS, LHS))
4017     return V;
4018 
4019   if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
4020     return V;
4021 
4022   if (std::optional<bool> Res =
4023           isImpliedByDomCondition(Pred, LHS, RHS, Q.CxtI, Q.DL))
4024     return ConstantInt::getBool(ITy, *Res);
4025 
4026   // Simplify comparisons of related pointers using a powerful, recursive
4027   // GEP-walk when we have target data available..
4028   if (LHS->getType()->isPointerTy())
4029     if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
4030       return C;
4031   if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
4032     if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
4033       if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4034           Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4035               Q.DL.getTypeSizeInBits(CLHS->getType()))
4036         if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
4037                                          CRHS->getPointerOperand(), Q))
4038           return C;
4039 
4040   // If the comparison is with the result of a select instruction, check whether
4041   // comparing with either branch of the select always yields the same value.
4042   if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4043     if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4044       return V;
4045 
4046   // If the comparison is with the result of a phi instruction, check whether
4047   // doing the compare with each incoming phi value yields a common result.
4048   if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4049     if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4050       return V;
4051 
4052   return nullptr;
4053 }
4054 
4055 Value *llvm::simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
4056                               const SimplifyQuery &Q) {
4057   return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
4058 }
4059 
4060 /// Given operands for an FCmpInst, see if we can fold the result.
4061 /// If not, this returns null.
4062 static Value *simplifyFCmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
4063                                FastMathFlags FMF, const SimplifyQuery &Q,
4064                                unsigned MaxRecurse) {
4065   assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
4066 
4067   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
4068     if (Constant *CRHS = dyn_cast<Constant>(RHS))
4069       return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI,
4070                                              Q.CxtI);
4071 
4072     // If we have a constant, make sure it is on the RHS.
4073     std::swap(LHS, RHS);
4074     Pred = CmpInst::getSwappedPredicate(Pred);
4075   }
4076 
4077   // Fold trivial predicates.
4078   Type *RetTy = getCompareTy(LHS);
4079   if (Pred == FCmpInst::FCMP_FALSE)
4080     return getFalse(RetTy);
4081   if (Pred == FCmpInst::FCMP_TRUE)
4082     return getTrue(RetTy);
4083 
4084   // fcmp pred x, poison and  fcmp pred poison, x
4085   // fold to poison
4086   if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS))
4087     return PoisonValue::get(RetTy);
4088 
4089   // fcmp pred x, undef  and  fcmp pred undef, x
4090   // fold to true if unordered, false if ordered
4091   if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
4092     // Choosing NaN for the undef will always make unordered comparison succeed
4093     // and ordered comparison fail.
4094     return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4095   }
4096 
4097   // fcmp x,x -> true/false.  Not all compares are foldable.
4098   if (LHS == RHS) {
4099     if (CmpInst::isTrueWhenEqual(Pred))
4100       return getTrue(RetTy);
4101     if (CmpInst::isFalseWhenEqual(Pred))
4102       return getFalse(RetTy);
4103   }
4104 
4105   // Fold (un)ordered comparison if we can determine there are no NaNs.
4106   //
4107   // This catches the 2 variable input case, constants are handled below as a
4108   // class-like compare.
4109   if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4110     KnownFPClass RHSClass =
4111         computeKnownFPClass(RHS, fcAllFlags, /*Depth=*/0, Q);
4112     KnownFPClass LHSClass =
4113         computeKnownFPClass(LHS, fcAllFlags, /*Depth=*/0, Q);
4114 
4115     if (FMF.noNaNs() ||
4116         (RHSClass.isKnownNeverNaN() && LHSClass.isKnownNeverNaN()))
4117       return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
4118 
4119     if (RHSClass.isKnownAlwaysNaN() || LHSClass.isKnownAlwaysNaN())
4120       return ConstantInt::get(RetTy, Pred == CmpInst::FCMP_UNO);
4121   }
4122 
4123   const APFloat *C = nullptr;
4124   match(RHS, m_APFloatAllowPoison(C));
4125   std::optional<KnownFPClass> FullKnownClassLHS;
4126 
4127   // Lazily compute the possible classes for LHS. Avoid computing it twice if
4128   // RHS is a 0.
4129   auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags =
4130                                                      fcAllFlags) {
4131     if (FullKnownClassLHS)
4132       return *FullKnownClassLHS;
4133     return computeKnownFPClass(LHS, FMF, InterestedFlags, 0, Q);
4134   };
4135 
4136   if (C && Q.CxtI) {
4137     // Fold out compares that express a class test.
4138     //
4139     // FIXME: Should be able to perform folds without context
4140     // instruction. Always pass in the context function?
4141 
4142     const Function *ParentF = Q.CxtI->getFunction();
4143     auto [ClassVal, ClassTest] = fcmpToClassTest(Pred, *ParentF, LHS, C);
4144     if (ClassVal) {
4145       FullKnownClassLHS = computeLHSClass();
4146       if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone)
4147         return getFalse(RetTy);
4148       if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone)
4149         return getTrue(RetTy);
4150     }
4151   }
4152 
4153   // Handle fcmp with constant RHS.
4154   if (C) {
4155     // TODO: If we always required a context function, we wouldn't need to
4156     // special case nans.
4157     if (C->isNaN())
4158       return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4159 
4160     // TODO: Need version fcmpToClassTest which returns implied class when the
4161     // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
4162     // isn't implementable as a class call.
4163     if (C->isNegative() && !C->isNegZero()) {
4164       FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4165 
4166       // TODO: We can catch more cases by using a range check rather than
4167       //       relying on CannotBeOrderedLessThanZero.
4168       switch (Pred) {
4169       case FCmpInst::FCMP_UGE:
4170       case FCmpInst::FCMP_UGT:
4171       case FCmpInst::FCMP_UNE: {
4172         KnownFPClass KnownClass = computeLHSClass(Interested);
4173 
4174         // (X >= 0) implies (X > C) when (C < 0)
4175         if (KnownClass.cannotBeOrderedLessThanZero())
4176           return getTrue(RetTy);
4177         break;
4178       }
4179       case FCmpInst::FCMP_OEQ:
4180       case FCmpInst::FCMP_OLE:
4181       case FCmpInst::FCMP_OLT: {
4182         KnownFPClass KnownClass = computeLHSClass(Interested);
4183 
4184         // (X >= 0) implies !(X < C) when (C < 0)
4185         if (KnownClass.cannotBeOrderedLessThanZero())
4186           return getFalse(RetTy);
4187         break;
4188       }
4189       default:
4190         break;
4191       }
4192     }
4193     // Check comparison of [minnum/maxnum with constant] with other constant.
4194     const APFloat *C2;
4195     if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
4196          *C2 < *C) ||
4197         (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
4198          *C2 > *C)) {
4199       bool IsMaxNum =
4200           cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
4201       // The ordered relationship and minnum/maxnum guarantee that we do not
4202       // have NaN constants, so ordered/unordered preds are handled the same.
4203       switch (Pred) {
4204       case FCmpInst::FCMP_OEQ:
4205       case FCmpInst::FCMP_UEQ:
4206         // minnum(X, LesserC)  == C --> false
4207         // maxnum(X, GreaterC) == C --> false
4208         return getFalse(RetTy);
4209       case FCmpInst::FCMP_ONE:
4210       case FCmpInst::FCMP_UNE:
4211         // minnum(X, LesserC)  != C --> true
4212         // maxnum(X, GreaterC) != C --> true
4213         return getTrue(RetTy);
4214       case FCmpInst::FCMP_OGE:
4215       case FCmpInst::FCMP_UGE:
4216       case FCmpInst::FCMP_OGT:
4217       case FCmpInst::FCMP_UGT:
4218         // minnum(X, LesserC)  >= C --> false
4219         // minnum(X, LesserC)  >  C --> false
4220         // maxnum(X, GreaterC) >= C --> true
4221         // maxnum(X, GreaterC) >  C --> true
4222         return ConstantInt::get(RetTy, IsMaxNum);
4223       case FCmpInst::FCMP_OLE:
4224       case FCmpInst::FCMP_ULE:
4225       case FCmpInst::FCMP_OLT:
4226       case FCmpInst::FCMP_ULT:
4227         // minnum(X, LesserC)  <= C --> true
4228         // minnum(X, LesserC)  <  C --> true
4229         // maxnum(X, GreaterC) <= C --> false
4230         // maxnum(X, GreaterC) <  C --> false
4231         return ConstantInt::get(RetTy, !IsMaxNum);
4232       default:
4233         // TRUE/FALSE/ORD/UNO should be handled before this.
4234         llvm_unreachable("Unexpected fcmp predicate");
4235       }
4236     }
4237   }
4238 
4239   // TODO: Could fold this with above if there were a matcher which returned all
4240   // classes in a non-splat vector.
4241   if (match(RHS, m_AnyZeroFP())) {
4242     switch (Pred) {
4243     case FCmpInst::FCMP_OGE:
4244     case FCmpInst::FCMP_ULT: {
4245       FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4246       if (!FMF.noNaNs())
4247         Interested |= fcNan;
4248 
4249       KnownFPClass Known = computeLHSClass(Interested);
4250 
4251       // Positive or zero X >= 0.0 --> true
4252       // Positive or zero X <  0.0 --> false
4253       if ((FMF.noNaNs() || Known.isKnownNeverNaN()) &&
4254           Known.cannotBeOrderedLessThanZero())
4255         return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
4256       break;
4257     }
4258     case FCmpInst::FCMP_UGE:
4259     case FCmpInst::FCMP_OLT: {
4260       FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4261       KnownFPClass Known = computeLHSClass(Interested);
4262 
4263       // Positive or zero or nan X >= 0.0 --> true
4264       // Positive or zero or nan X <  0.0 --> false
4265       if (Known.cannotBeOrderedLessThanZero())
4266         return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
4267       break;
4268     }
4269     default:
4270       break;
4271     }
4272   }
4273 
4274   // If the comparison is with the result of a select instruction, check whether
4275   // comparing with either branch of the select always yields the same value.
4276   if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4277     if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4278       return V;
4279 
4280   // If the comparison is with the result of a phi instruction, check whether
4281   // doing the compare with each incoming phi value yields a common result.
4282   if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4283     if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4284       return V;
4285 
4286   return nullptr;
4287 }
4288 
4289 Value *llvm::simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
4290                               FastMathFlags FMF, const SimplifyQuery &Q) {
4291   return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
4292 }
4293 
4294 static Value *simplifyWithOpsReplaced(Value *V,
4295                                       ArrayRef<std::pair<Value *, Value *>> Ops,
4296                                       const SimplifyQuery &Q,
4297                                       bool AllowRefinement,
4298                                       SmallVectorImpl<Instruction *> *DropFlags,
4299                                       unsigned MaxRecurse) {
4300   assert((AllowRefinement || !Q.CanUseUndef) &&
4301          "If AllowRefinement=false then CanUseUndef=false");
4302   for (const auto &OpAndRepOp : Ops) {
4303     // We cannot replace a constant, and shouldn't even try.
4304     if (isa<Constant>(OpAndRepOp.first))
4305       return nullptr;
4306 
4307     // Trivial replacement.
4308     if (V == OpAndRepOp.first)
4309       return OpAndRepOp.second;
4310   }
4311 
4312   if (!MaxRecurse--)
4313     return nullptr;
4314 
4315   auto *I = dyn_cast<Instruction>(V);
4316   if (!I)
4317     return nullptr;
4318 
4319   // The arguments of a phi node might refer to a value from a previous
4320   // cycle iteration.
4321   if (isa<PHINode>(I))
4322     return nullptr;
4323 
4324   // Don't fold away llvm.is.constant checks based on assumptions.
4325   if (match(I, m_Intrinsic<Intrinsic::is_constant>()))
4326     return nullptr;
4327 
4328   // Don't simplify freeze.
4329   if (isa<FreezeInst>(I))
4330     return nullptr;
4331 
4332   for (const auto &OpAndRepOp : Ops) {
4333     // For vector types, the simplification must hold per-lane, so forbid
4334     // potentially cross-lane operations like shufflevector.
4335     if (OpAndRepOp.first->getType()->isVectorTy() &&
4336         !isNotCrossLaneOperation(I))
4337       return nullptr;
4338   }
4339 
4340   // Replace Op with RepOp in instruction operands.
4341   SmallVector<Value *, 8> NewOps;
4342   bool AnyReplaced = false;
4343   for (Value *InstOp : I->operands()) {
4344     if (Value *NewInstOp = simplifyWithOpsReplaced(
4345             InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) {
4346       NewOps.push_back(NewInstOp);
4347       AnyReplaced = InstOp != NewInstOp;
4348     } else {
4349       NewOps.push_back(InstOp);
4350     }
4351 
4352     // Bail out if any operand is undef and SimplifyQuery disables undef
4353     // simplification. Constant folding currently doesn't respect this option.
4354     if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef)
4355       return nullptr;
4356   }
4357 
4358   if (!AnyReplaced)
4359     return nullptr;
4360 
4361   if (!AllowRefinement) {
4362     // General InstSimplify functions may refine the result, e.g. by returning
4363     // a constant for a potentially poison value. To avoid this, implement only
4364     // a few non-refining but profitable transforms here.
4365 
4366     if (auto *BO = dyn_cast<BinaryOperator>(I)) {
4367       unsigned Opcode = BO->getOpcode();
4368       // id op x -> x, x op id -> x
4369       // Exclude floats, because x op id may produce a different NaN value.
4370       if (!BO->getType()->isFPOrFPVectorTy()) {
4371         if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
4372           return NewOps[1];
4373         if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
4374                                                         /* RHS */ true))
4375           return NewOps[0];
4376       }
4377 
4378       // x & x -> x, x | x -> x
4379       if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4380           NewOps[0] == NewOps[1]) {
4381         // or disjoint x, x results in poison.
4382         if (auto *PDI = dyn_cast<PossiblyDisjointInst>(BO)) {
4383           if (PDI->isDisjoint()) {
4384             if (!DropFlags)
4385               return nullptr;
4386             DropFlags->push_back(BO);
4387           }
4388         }
4389         return NewOps[0];
4390       }
4391 
4392       // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
4393       // by assumption and this case never wraps, so nowrap flags can be
4394       // ignored.
4395       if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
4396           NewOps[0] == NewOps[1] &&
4397           any_of(Ops, [=](const auto &Rep) { return NewOps[0] == Rep.second; }))
4398         return Constant::getNullValue(I->getType());
4399 
4400       // If we are substituting an absorber constant into a binop and extra
4401       // poison can't leak if we remove the select -- because both operands of
4402       // the binop are based on the same value -- then it may be safe to replace
4403       // the value with the absorber constant. Examples:
4404       // (Op == 0) ? 0 : (Op & -Op)            --> Op & -Op
4405       // (Op == 0) ? 0 : (Op * (binop Op, C))  --> Op * (binop Op, C)
4406       // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op)
4407       Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType());
4408       if ((NewOps[0] == Absorber || NewOps[1] == Absorber) &&
4409           any_of(Ops,
4410                  [=](const auto &Rep) { return impliesPoison(BO, Rep.first); }))
4411         return Absorber;
4412     }
4413 
4414     if (isa<GetElementPtrInst>(I)) {
4415       // getelementptr x, 0 -> x.
4416       // This never returns poison, even if inbounds is set.
4417       if (NewOps.size() == 2 && match(NewOps[1], m_Zero()))
4418         return NewOps[0];
4419     }
4420   } else {
4421     // The simplification queries below may return the original value. Consider:
4422     //   %div = udiv i32 %arg, %arg2
4423     //   %mul = mul nsw i32 %div, %arg2
4424     //   %cmp = icmp eq i32 %mul, %arg
4425     //   %sel = select i1 %cmp, i32 %div, i32 undef
4426     // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
4427     // simplifies back to %arg. This can only happen because %mul does not
4428     // dominate %div. To ensure a consistent return value contract, we make sure
4429     // that this case returns nullptr as well.
4430     auto PreventSelfSimplify = [V](Value *Simplified) {
4431       return Simplified != V ? Simplified : nullptr;
4432     };
4433 
4434     return PreventSelfSimplify(
4435         ::simplifyInstructionWithOperands(I, NewOps, Q, MaxRecurse));
4436   }
4437 
4438   // If all operands are constant after substituting Op for RepOp then we can
4439   // constant fold the instruction.
4440   SmallVector<Constant *, 8> ConstOps;
4441   for (Value *NewOp : NewOps) {
4442     if (Constant *ConstOp = dyn_cast<Constant>(NewOp))
4443       ConstOps.push_back(ConstOp);
4444     else
4445       return nullptr;
4446   }
4447 
4448   // Consider:
4449   //   %cmp = icmp eq i32 %x, 2147483647
4450   //   %add = add nsw i32 %x, 1
4451   //   %sel = select i1 %cmp, i32 -2147483648, i32 %add
4452   //
4453   // We can't replace %sel with %add unless we strip away the flags (which
4454   // will be done in InstCombine).
4455   // TODO: This may be unsound, because it only catches some forms of
4456   // refinement.
4457   if (!AllowRefinement) {
4458     if (canCreatePoison(cast<Operator>(I), !DropFlags)) {
4459       // abs cannot create poison if the value is known to never be int_min.
4460       if (auto *II = dyn_cast<IntrinsicInst>(I);
4461           II && II->getIntrinsicID() == Intrinsic::abs) {
4462         if (!ConstOps[0]->isNotMinSignedValue())
4463           return nullptr;
4464       } else
4465         return nullptr;
4466     }
4467     Constant *Res = ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4468                                              /*AllowNonDeterministic=*/false);
4469     if (DropFlags && Res && I->hasPoisonGeneratingAnnotations())
4470       DropFlags->push_back(I);
4471     return Res;
4472   }
4473 
4474   return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4475                                   /*AllowNonDeterministic=*/false);
4476 }
4477 
4478 static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4479                                      const SimplifyQuery &Q,
4480                                      bool AllowRefinement,
4481                                      SmallVectorImpl<Instruction *> *DropFlags,
4482                                      unsigned MaxRecurse) {
4483   return simplifyWithOpsReplaced(V, {{Op, RepOp}}, Q, AllowRefinement,
4484                                  DropFlags, MaxRecurse);
4485 }
4486 
4487 Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4488                                     const SimplifyQuery &Q,
4489                                     bool AllowRefinement,
4490                                     SmallVectorImpl<Instruction *> *DropFlags) {
4491   // If refinement is disabled, also disable undef simplifications (which are
4492   // always refinements) in SimplifyQuery.
4493   if (!AllowRefinement)
4494     return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(),
4495                                     AllowRefinement, DropFlags, RecursionLimit);
4496   return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,
4497                                   RecursionLimit);
4498 }
4499 
4500 /// Try to simplify a select instruction when its condition operand is an
4501 /// integer comparison where one operand of the compare is a constant.
4502 static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
4503                                     const APInt *Y, bool TrueWhenUnset) {
4504   const APInt *C;
4505 
4506   // (X & Y) == 0 ? X & ~Y : X  --> X
4507   // (X & Y) != 0 ? X & ~Y : X  --> X & ~Y
4508   if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
4509       *Y == ~*C)
4510     return TrueWhenUnset ? FalseVal : TrueVal;
4511 
4512   // (X & Y) == 0 ? X : X & ~Y  --> X & ~Y
4513   // (X & Y) != 0 ? X : X & ~Y  --> X
4514   if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
4515       *Y == ~*C)
4516     return TrueWhenUnset ? FalseVal : TrueVal;
4517 
4518   if (Y->isPowerOf2()) {
4519     // (X & Y) == 0 ? X | Y : X  --> X | Y
4520     // (X & Y) != 0 ? X | Y : X  --> X
4521     if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
4522         *Y == *C) {
4523       // We can't return the or if it has the disjoint flag.
4524       if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint())
4525         return nullptr;
4526       return TrueWhenUnset ? TrueVal : FalseVal;
4527     }
4528 
4529     // (X & Y) == 0 ? X : X | Y  --> X
4530     // (X & Y) != 0 ? X : X | Y  --> X | Y
4531     if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
4532         *Y == *C) {
4533       // We can't return the or if it has the disjoint flag.
4534       if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint())
4535         return nullptr;
4536       return TrueWhenUnset ? TrueVal : FalseVal;
4537     }
4538   }
4539 
4540   return nullptr;
4541 }
4542 
4543 static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
4544                                      CmpPredicate Pred, Value *TVal,
4545                                      Value *FVal) {
4546   // Canonicalize common cmp+sel operand as CmpLHS.
4547   if (CmpRHS == TVal || CmpRHS == FVal) {
4548     std::swap(CmpLHS, CmpRHS);
4549     Pred = ICmpInst::getSwappedPredicate(Pred);
4550   }
4551 
4552   // Canonicalize common cmp+sel operand as TVal.
4553   if (CmpLHS == FVal) {
4554     std::swap(TVal, FVal);
4555     Pred = ICmpInst::getInversePredicate(Pred);
4556   }
4557 
4558   // A vector select may be shuffling together elements that are equivalent
4559   // based on the max/min/select relationship.
4560   Value *X = CmpLHS, *Y = CmpRHS;
4561   bool PeekedThroughSelectShuffle = false;
4562   auto *Shuf = dyn_cast<ShuffleVectorInst>(FVal);
4563   if (Shuf && Shuf->isSelect()) {
4564     if (Shuf->getOperand(0) == Y)
4565       FVal = Shuf->getOperand(1);
4566     else if (Shuf->getOperand(1) == Y)
4567       FVal = Shuf->getOperand(0);
4568     else
4569       return nullptr;
4570     PeekedThroughSelectShuffle = true;
4571   }
4572 
4573   // (X pred Y) ? X : max/min(X, Y)
4574   auto *MMI = dyn_cast<MinMaxIntrinsic>(FVal);
4575   if (!MMI || TVal != X ||
4576       !match(FVal, m_c_MaxOrMin(m_Specific(X), m_Specific(Y))))
4577     return nullptr;
4578 
4579   // (X >  Y) ? X : max(X, Y) --> max(X, Y)
4580   // (X >= Y) ? X : max(X, Y) --> max(X, Y)
4581   // (X <  Y) ? X : min(X, Y) --> min(X, Y)
4582   // (X <= Y) ? X : min(X, Y) --> min(X, Y)
4583   //
4584   // The equivalence allows a vector select (shuffle) of max/min and Y. Ex:
4585   // (X > Y) ? X : (Z ? max(X, Y) : Y)
4586   // If Z is true, this reduces as above, and if Z is false:
4587   // (X > Y) ? X : Y --> max(X, Y)
4588   ICmpInst::Predicate MMPred = MMI->getPredicate();
4589   if (MMPred == CmpInst::getStrictPredicate(Pred))
4590     return MMI;
4591 
4592   // Other transforms are not valid with a shuffle.
4593   if (PeekedThroughSelectShuffle)
4594     return nullptr;
4595 
4596   // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
4597   if (Pred == CmpInst::ICMP_EQ)
4598     return MMI;
4599 
4600   // (X != Y) ? X : max/min(X, Y) --> X
4601   if (Pred == CmpInst::ICMP_NE)
4602     return X;
4603 
4604   // (X <  Y) ? X : max(X, Y) --> X
4605   // (X <= Y) ? X : max(X, Y) --> X
4606   // (X >  Y) ? X : min(X, Y) --> X
4607   // (X >= Y) ? X : min(X, Y) --> X
4608   ICmpInst::Predicate InvPred = CmpInst::getInversePredicate(Pred);
4609   if (MMPred == CmpInst::getStrictPredicate(InvPred))
4610     return X;
4611 
4612   return nullptr;
4613 }
4614 
4615 /// An alternative way to test if a bit is set or not uses sgt/slt instead of
4616 /// eq/ne.
4617 static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
4618                                            CmpPredicate Pred, Value *TrueVal,
4619                                            Value *FalseVal) {
4620   if (auto Res = decomposeBitTestICmp(CmpLHS, CmpRHS, Pred))
4621     return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask,
4622                                  Res->Pred == ICmpInst::ICMP_EQ);
4623 
4624   return nullptr;
4625 }
4626 
4627 /// Try to simplify a select instruction when its condition operand is an
4628 /// integer equality or floating-point equivalence comparison.
4629 static Value *simplifySelectWithEquivalence(
4630     ArrayRef<std::pair<Value *, Value *>> Replacements, Value *TrueVal,
4631     Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse) {
4632   Value *SimplifiedFalseVal =
4633       simplifyWithOpsReplaced(FalseVal, Replacements, Q.getWithoutUndef(),
4634                               /* AllowRefinement */ false,
4635                               /* DropFlags */ nullptr, MaxRecurse);
4636   if (!SimplifiedFalseVal)
4637     SimplifiedFalseVal = FalseVal;
4638 
4639   Value *SimplifiedTrueVal =
4640       simplifyWithOpsReplaced(TrueVal, Replacements, Q,
4641                               /* AllowRefinement */ true,
4642                               /* DropFlags */ nullptr, MaxRecurse);
4643   if (!SimplifiedTrueVal)
4644     SimplifiedTrueVal = TrueVal;
4645 
4646   if (SimplifiedFalseVal == SimplifiedTrueVal)
4647     return FalseVal;
4648 
4649   return nullptr;
4650 }
4651 
4652 /// Try to simplify a select instruction when its condition operand is an
4653 /// integer comparison.
4654 static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4655                                          Value *FalseVal,
4656                                          const SimplifyQuery &Q,
4657                                          unsigned MaxRecurse) {
4658   CmpPredicate Pred;
4659   Value *CmpLHS, *CmpRHS;
4660   if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4661     return nullptr;
4662 
4663   if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
4664     return V;
4665 
4666   // Canonicalize ne to eq predicate.
4667   if (Pred == ICmpInst::ICMP_NE) {
4668     Pred = ICmpInst::ICMP_EQ;
4669     std::swap(TrueVal, FalseVal);
4670   }
4671 
4672   // Check for integer min/max with a limit constant:
4673   // X > MIN_INT ? X : MIN_INT --> X
4674   // X < MAX_INT ? X : MAX_INT --> X
4675   if (TrueVal->getType()->isIntOrIntVectorTy()) {
4676     Value *X, *Y;
4677     SelectPatternFlavor SPF =
4678         matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
4679                                      X, Y)
4680             .Flavor;
4681     if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
4682       APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF),
4683                                     X->getType()->getScalarSizeInBits());
4684       if (match(Y, m_SpecificInt(LimitC)))
4685         return X;
4686     }
4687   }
4688 
4689   if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
4690     Value *X;
4691     const APInt *Y;
4692     if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
4693       if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
4694                                            /*TrueWhenUnset=*/true))
4695         return V;
4696 
4697     // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4698     Value *ShAmt;
4699     auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)),
4700                              m_FShr(m_Value(), m_Value(X), m_Value(ShAmt)));
4701     // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4702     // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4703     if (match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt)
4704       return X;
4705 
4706     // Test for a zero-shift-guard-op around rotates. These are used to
4707     // avoid UB from oversized shifts in raw IR rotate patterns, but the
4708     // intrinsics do not have that problem.
4709     // We do not allow this transform for the general funnel shift case because
4710     // that would not preserve the poison safety of the original code.
4711     auto isRotate =
4712         m_CombineOr(m_FShl(m_Value(X), m_Deferred(X), m_Value(ShAmt)),
4713                     m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt)));
4714     // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4715     // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4716     if (match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
4717         Pred == ICmpInst::ICMP_EQ)
4718       return FalseVal;
4719 
4720     // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4721     // X == 0 ? -abs(X) : abs(X) --> abs(X)
4722     if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) &&
4723         match(FalseVal, m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))))
4724       return FalseVal;
4725     if (match(TrueVal,
4726               m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))) &&
4727         match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))
4728       return FalseVal;
4729   }
4730 
4731   // Check for other compares that behave like bit test.
4732   if (Value *V =
4733           simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
4734     return V;
4735 
4736   // If we have a scalar equality comparison, then we know the value in one of
4737   // the arms of the select. See if substituting this value into the arm and
4738   // simplifying the result yields the same value as the other arm.
4739   if (Pred == ICmpInst::ICMP_EQ) {
4740     if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4741                                                  FalseVal, Q, MaxRecurse))
4742       return V;
4743     if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4744                                                  FalseVal, Q, MaxRecurse))
4745       return V;
4746 
4747     Value *X;
4748     Value *Y;
4749     // select((X | Y) == 0 ?  X : 0) --> 0 (commuted 2 ways)
4750     if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
4751         match(CmpRHS, m_Zero())) {
4752       // (X | Y) == 0 implies X == 0 and Y == 0.
4753       if (Value *V = simplifySelectWithEquivalence(
4754               {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4755         return V;
4756     }
4757 
4758     // select((X & Y) == -1 ?  X : -1) --> -1 (commuted 2 ways)
4759     if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) &&
4760         match(CmpRHS, m_AllOnes())) {
4761       // (X & Y) == -1 implies X == -1 and Y == -1.
4762       if (Value *V = simplifySelectWithEquivalence(
4763               {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4764         return V;
4765     }
4766   }
4767 
4768   return nullptr;
4769 }
4770 
4771 /// Try to simplify a select instruction when its condition operand is a
4772 /// floating-point comparison.
4773 static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
4774                                      const SimplifyQuery &Q,
4775                                      unsigned MaxRecurse) {
4776   CmpPredicate Pred;
4777   Value *CmpLHS, *CmpRHS;
4778   if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4779     return nullptr;
4780   FCmpInst *I = cast<FCmpInst>(Cond);
4781 
4782   bool IsEquiv = I->isEquivalence();
4783   if (I->isEquivalence(/*Invert=*/true)) {
4784     std::swap(T, F);
4785     Pred = FCmpInst::getInversePredicate(Pred);
4786     IsEquiv = true;
4787   }
4788 
4789   // This transforms is safe if at least one operand is known to not be zero.
4790   // Otherwise, the select can change the sign of a zero operand.
4791   if (IsEquiv) {
4792     if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, T, F, Q,
4793                                                  MaxRecurse))
4794       return V;
4795     if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, T, F, Q,
4796                                                  MaxRecurse))
4797       return V;
4798   }
4799 
4800   // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
4801   if (CmpLHS == F && CmpRHS == T)
4802     std::swap(CmpLHS, CmpRHS);
4803 
4804   if (CmpLHS != T || CmpRHS != F)
4805     return nullptr;
4806 
4807   // This transform is also safe if we do not have (do not care about) -0.0.
4808   if (Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros()) {
4809     // (T == F) ? T : F --> F
4810     if (Pred == FCmpInst::FCMP_OEQ)
4811       return F;
4812 
4813     // (T != F) ? T : F --> T
4814     if (Pred == FCmpInst::FCMP_UNE)
4815       return T;
4816   }
4817 
4818   return nullptr;
4819 }
4820 
4821 /// Given operands for a SelectInst, see if we can fold the result.
4822 /// If not, this returns null.
4823 static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4824                                  const SimplifyQuery &Q, unsigned MaxRecurse) {
4825   if (auto *CondC = dyn_cast<Constant>(Cond)) {
4826     if (auto *TrueC = dyn_cast<Constant>(TrueVal))
4827       if (auto *FalseC = dyn_cast<Constant>(FalseVal))
4828         if (Constant *C = ConstantFoldSelectInstruction(CondC, TrueC, FalseC))
4829           return C;
4830 
4831     // select poison, X, Y -> poison
4832     if (isa<PoisonValue>(CondC))
4833       return PoisonValue::get(TrueVal->getType());
4834 
4835     // select undef, X, Y -> X or Y
4836     if (Q.isUndefValue(CondC))
4837       return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
4838 
4839     // select true,  X, Y --> X
4840     // select false, X, Y --> Y
4841     // For vectors, allow undef/poison elements in the condition to match the
4842     // defined elements, so we can eliminate the select.
4843     if (match(CondC, m_One()))
4844       return TrueVal;
4845     if (match(CondC, m_Zero()))
4846       return FalseVal;
4847   }
4848 
4849   assert(Cond->getType()->isIntOrIntVectorTy(1) &&
4850          "Select must have bool or bool vector condition");
4851   assert(TrueVal->getType() == FalseVal->getType() &&
4852          "Select must have same types for true/false ops");
4853 
4854   if (Cond->getType() == TrueVal->getType()) {
4855     // select i1 Cond, i1 true, i1 false --> i1 Cond
4856     if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
4857       return Cond;
4858 
4859     // (X && Y) ? X : Y --> Y (commuted 2 ways)
4860     if (match(Cond, m_c_LogicalAnd(m_Specific(TrueVal), m_Specific(FalseVal))))
4861       return FalseVal;
4862 
4863     // (X || Y) ? X : Y --> X (commuted 2 ways)
4864     if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
4865       return TrueVal;
4866 
4867     // (X || Y) ? false : X --> false (commuted 2 ways)
4868     if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
4869         match(TrueVal, m_ZeroInt()))
4870       return ConstantInt::getFalse(Cond->getType());
4871 
4872     // Match patterns that end in logical-and.
4873     if (match(FalseVal, m_ZeroInt())) {
4874       // !(X || Y) && X --> false (commuted 2 ways)
4875       if (match(Cond, m_Not(m_c_LogicalOr(m_Specific(TrueVal), m_Value()))))
4876         return ConstantInt::getFalse(Cond->getType());
4877       // X && !(X || Y) --> false (commuted 2 ways)
4878       if (match(TrueVal, m_Not(m_c_LogicalOr(m_Specific(Cond), m_Value()))))
4879         return ConstantInt::getFalse(Cond->getType());
4880 
4881       // (X || Y) && Y --> Y (commuted 2 ways)
4882       if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Value())))
4883         return TrueVal;
4884       // Y && (X || Y) --> Y (commuted 2 ways)
4885       if (match(TrueVal, m_c_LogicalOr(m_Specific(Cond), m_Value())))
4886         return Cond;
4887 
4888       // (X || Y) && (X || !Y) --> X (commuted 8 ways)
4889       Value *X, *Y;
4890       if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4891           match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
4892         return X;
4893       if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4894           match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
4895         return X;
4896     }
4897 
4898     // Match patterns that end in logical-or.
4899     if (match(TrueVal, m_One())) {
4900       // !(X && Y) || X --> true (commuted 2 ways)
4901       if (match(Cond, m_Not(m_c_LogicalAnd(m_Specific(FalseVal), m_Value()))))
4902         return ConstantInt::getTrue(Cond->getType());
4903       // X || !(X && Y) --> true (commuted 2 ways)
4904       if (match(FalseVal, m_Not(m_c_LogicalAnd(m_Specific(Cond), m_Value()))))
4905         return ConstantInt::getTrue(Cond->getType());
4906 
4907       // (X && Y) || Y --> Y (commuted 2 ways)
4908       if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value())))
4909         return FalseVal;
4910       // Y || (X && Y) --> Y (commuted 2 ways)
4911       if (match(FalseVal, m_c_LogicalAnd(m_Specific(Cond), m_Value())))
4912         return Cond;
4913     }
4914   }
4915 
4916   // select ?, X, X -> X
4917   if (TrueVal == FalseVal)
4918     return TrueVal;
4919 
4920   if (Cond == TrueVal) {
4921     // select i1 X, i1 X, i1 false --> X (logical-and)
4922     if (match(FalseVal, m_ZeroInt()))
4923       return Cond;
4924     // select i1 X, i1 X, i1 true --> true
4925     if (match(FalseVal, m_One()))
4926       return ConstantInt::getTrue(Cond->getType());
4927   }
4928   if (Cond == FalseVal) {
4929     // select i1 X, i1 true, i1 X --> X (logical-or)
4930     if (match(TrueVal, m_One()))
4931       return Cond;
4932     // select i1 X, i1 false, i1 X --> false
4933     if (match(TrueVal, m_ZeroInt()))
4934       return ConstantInt::getFalse(Cond->getType());
4935   }
4936 
4937   // If the true or false value is poison, we can fold to the other value.
4938   // If the true or false value is undef, we can fold to the other value as
4939   // long as the other value isn't poison.
4940   // select ?, poison, X -> X
4941   // select ?, undef,  X -> X
4942   if (isa<PoisonValue>(TrueVal) ||
4943       (Q.isUndefValue(TrueVal) && impliesPoison(FalseVal, Cond)))
4944     return FalseVal;
4945   // select ?, X, poison -> X
4946   // select ?, X, undef  -> X
4947   if (isa<PoisonValue>(FalseVal) ||
4948       (Q.isUndefValue(FalseVal) && impliesPoison(TrueVal, Cond)))
4949     return TrueVal;
4950 
4951   // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
4952   Constant *TrueC, *FalseC;
4953   if (isa<FixedVectorType>(TrueVal->getType()) &&
4954       match(TrueVal, m_Constant(TrueC)) &&
4955       match(FalseVal, m_Constant(FalseC))) {
4956     unsigned NumElts =
4957         cast<FixedVectorType>(TrueC->getType())->getNumElements();
4958     SmallVector<Constant *, 16> NewC;
4959     for (unsigned i = 0; i != NumElts; ++i) {
4960       // Bail out on incomplete vector constants.
4961       Constant *TEltC = TrueC->getAggregateElement(i);
4962       Constant *FEltC = FalseC->getAggregateElement(i);
4963       if (!TEltC || !FEltC)
4964         break;
4965 
4966       // If the elements match (undef or not), that value is the result. If only
4967       // one element is undef, choose the defined element as the safe result.
4968       if (TEltC == FEltC)
4969         NewC.push_back(TEltC);
4970       else if (isa<PoisonValue>(TEltC) ||
4971                (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)))
4972         NewC.push_back(FEltC);
4973       else if (isa<PoisonValue>(FEltC) ||
4974                (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)))
4975         NewC.push_back(TEltC);
4976       else
4977         break;
4978     }
4979     if (NewC.size() == NumElts)
4980       return ConstantVector::get(NewC);
4981   }
4982 
4983   if (Value *V =
4984           simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4985     return V;
4986 
4987   if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4988     return V;
4989 
4990   std::optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
4991   if (Imp)
4992     return *Imp ? TrueVal : FalseVal;
4993 
4994   return nullptr;
4995 }
4996 
4997 Value *llvm::simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4998                                 const SimplifyQuery &Q) {
4999   return ::simplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
5000 }
5001 
5002 /// Given operands for an GetElementPtrInst, see if we can fold the result.
5003 /// If not, this returns null.
5004 static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
5005                               ArrayRef<Value *> Indices, GEPNoWrapFlags NW,
5006                               const SimplifyQuery &Q, unsigned) {
5007   // The type of the GEP pointer operand.
5008   unsigned AS =
5009       cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
5010 
5011   // getelementptr P -> P.
5012   if (Indices.empty())
5013     return Ptr;
5014 
5015   // Compute the (pointer) type returned by the GEP instruction.
5016   Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices);
5017   Type *GEPTy = Ptr->getType();
5018   if (!GEPTy->isVectorTy()) {
5019     for (Value *Op : Indices) {
5020       // If one of the operands is a vector, the result type is a vector of
5021       // pointers. All vector operands must have the same number of elements.
5022       if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
5023         GEPTy = VectorType::get(GEPTy, VT->getElementCount());
5024         break;
5025       }
5026     }
5027   }
5028 
5029   // All-zero GEP is a no-op, unless it performs a vector splat.
5030   if (Ptr->getType() == GEPTy &&
5031       all_of(Indices, [](const auto *V) { return match(V, m_Zero()); }))
5032     return Ptr;
5033 
5034   // getelementptr poison, idx -> poison
5035   // getelementptr baseptr, poison -> poison
5036   if (isa<PoisonValue>(Ptr) ||
5037       any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
5038     return PoisonValue::get(GEPTy);
5039 
5040   // getelementptr undef, idx -> undef
5041   if (Q.isUndefValue(Ptr))
5042     return UndefValue::get(GEPTy);
5043 
5044   bool IsScalableVec =
5045       SrcTy->isScalableTy() || any_of(Indices, [](const Value *V) {
5046         return isa<ScalableVectorType>(V->getType());
5047       });
5048 
5049   if (Indices.size() == 1) {
5050     Type *Ty = SrcTy;
5051     if (!IsScalableVec && Ty->isSized()) {
5052       Value *P;
5053       uint64_t C;
5054       uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
5055       // getelementptr P, N -> P if P points to a type of zero size.
5056       if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
5057         return Ptr;
5058 
5059       // The following transforms are only safe if the ptrtoint cast
5060       // doesn't truncate the pointers.
5061       if (Indices[0]->getType()->getScalarSizeInBits() ==
5062           Q.DL.getPointerSizeInBits(AS)) {
5063         auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
5064           return P->getType() == GEPTy &&
5065                  getUnderlyingObject(P) == getUnderlyingObject(Ptr);
5066         };
5067         // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
5068         if (TyAllocSize == 1 &&
5069             match(Indices[0],
5070                   m_Sub(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Specific(Ptr)))) &&
5071             CanSimplify())
5072           return P;
5073 
5074         // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
5075         // size 1 << C.
5076         if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)),
5077                                            m_PtrToInt(m_Specific(Ptr))),
5078                                      m_ConstantInt(C))) &&
5079             TyAllocSize == 1ULL << C && CanSimplify())
5080           return P;
5081 
5082         // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
5083         // size C.
5084         if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)),
5085                                            m_PtrToInt(m_Specific(Ptr))),
5086                                      m_SpecificInt(TyAllocSize))) &&
5087             CanSimplify())
5088           return P;
5089       }
5090     }
5091   }
5092 
5093   if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
5094       all_of(Indices.drop_back(1),
5095              [](Value *Idx) { return match(Idx, m_Zero()); })) {
5096     unsigned IdxWidth =
5097         Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
5098     if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
5099       APInt BasePtrOffset(IdxWidth, 0);
5100       Value *StrippedBasePtr =
5101           Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
5102 
5103       // Avoid creating inttoptr of zero here: While LLVMs treatment of
5104       // inttoptr is generally conservative, this particular case is folded to
5105       // a null pointer, which will have incorrect provenance.
5106 
5107       // gep (gep V, C), (sub 0, V) -> C
5108       if (match(Indices.back(),
5109                 m_Neg(m_PtrToInt(m_Specific(StrippedBasePtr)))) &&
5110           !BasePtrOffset.isZero()) {
5111         auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
5112         return ConstantExpr::getIntToPtr(CI, GEPTy);
5113       }
5114       // gep (gep V, C), (xor V, -1) -> C-1
5115       if (match(Indices.back(),
5116                 m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) &&
5117           !BasePtrOffset.isOne()) {
5118         auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
5119         return ConstantExpr::getIntToPtr(CI, GEPTy);
5120       }
5121     }
5122   }
5123 
5124   // Check to see if this is constant foldable.
5125   if (!isa<Constant>(Ptr) ||
5126       !all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
5127     return nullptr;
5128 
5129   if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
5130     return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), std::nullopt,
5131                                      Indices);
5132 
5133   auto *CE =
5134       ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices, NW);
5135   return ConstantFoldConstant(CE, Q.DL);
5136 }
5137 
5138 Value *llvm::simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
5139                              GEPNoWrapFlags NW, const SimplifyQuery &Q) {
5140   return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit);
5141 }
5142 
5143 /// Given operands for an InsertValueInst, see if we can fold the result.
5144 /// If not, this returns null.
5145 static Value *simplifyInsertValueInst(Value *Agg, Value *Val,
5146                                       ArrayRef<unsigned> Idxs,
5147                                       const SimplifyQuery &Q, unsigned) {
5148   if (Constant *CAgg = dyn_cast<Constant>(Agg))
5149     if (Constant *CVal = dyn_cast<Constant>(Val))
5150       return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
5151 
5152   // insertvalue x, poison, n -> x
5153   // insertvalue x, undef, n -> x if x cannot be poison
5154   if (isa<PoisonValue>(Val) ||
5155       (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Agg)))
5156     return Agg;
5157 
5158   // insertvalue x, (extractvalue y, n), n
5159   if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
5160     if (EV->getAggregateOperand()->getType() == Agg->getType() &&
5161         EV->getIndices() == Idxs) {
5162       // insertvalue poison, (extractvalue y, n), n -> y
5163       // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison
5164       if (isa<PoisonValue>(Agg) ||
5165           (Q.isUndefValue(Agg) &&
5166            isGuaranteedNotToBePoison(EV->getAggregateOperand())))
5167         return EV->getAggregateOperand();
5168 
5169       // insertvalue y, (extractvalue y, n), n -> y
5170       if (Agg == EV->getAggregateOperand())
5171         return Agg;
5172     }
5173 
5174   return nullptr;
5175 }
5176 
5177 Value *llvm::simplifyInsertValueInst(Value *Agg, Value *Val,
5178                                      ArrayRef<unsigned> Idxs,
5179                                      const SimplifyQuery &Q) {
5180   return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
5181 }
5182 
5183 Value *llvm::simplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
5184                                        const SimplifyQuery &Q) {
5185   // Try to constant fold.
5186   auto *VecC = dyn_cast<Constant>(Vec);
5187   auto *ValC = dyn_cast<Constant>(Val);
5188   auto *IdxC = dyn_cast<Constant>(Idx);
5189   if (VecC && ValC && IdxC)
5190     return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
5191 
5192   // For fixed-length vector, fold into poison if index is out of bounds.
5193   if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
5194     if (isa<FixedVectorType>(Vec->getType()) &&
5195         CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
5196       return PoisonValue::get(Vec->getType());
5197   }
5198 
5199   // If index is undef, it might be out of bounds (see above case)
5200   if (Q.isUndefValue(Idx))
5201     return PoisonValue::get(Vec->getType());
5202 
5203   // If the scalar is poison, or it is undef and there is no risk of
5204   // propagating poison from the vector value, simplify to the vector value.
5205   if (isa<PoisonValue>(Val) ||
5206       (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
5207     return Vec;
5208 
5209   // Inserting the splatted value into a constant splat does nothing.
5210   if (VecC && ValC && VecC->getSplatValue() == ValC)
5211     return Vec;
5212 
5213   // If we are extracting a value from a vector, then inserting it into the same
5214   // place, that's the input vector:
5215   // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
5216   if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx))))
5217     return Vec;
5218 
5219   return nullptr;
5220 }
5221 
5222 /// Given operands for an ExtractValueInst, see if we can fold the result.
5223 /// If not, this returns null.
5224 static Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
5225                                        const SimplifyQuery &, unsigned) {
5226   if (auto *CAgg = dyn_cast<Constant>(Agg))
5227     return ConstantFoldExtractValueInstruction(CAgg, Idxs);
5228 
5229   // extractvalue x, (insertvalue y, elt, n), n -> elt
5230   unsigned NumIdxs = Idxs.size();
5231   for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
5232        IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
5233     ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
5234     unsigned NumInsertValueIdxs = InsertValueIdxs.size();
5235     unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
5236     if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
5237         Idxs.slice(0, NumCommonIdxs)) {
5238       if (NumIdxs == NumInsertValueIdxs)
5239         return IVI->getInsertedValueOperand();
5240       break;
5241     }
5242   }
5243 
5244   return nullptr;
5245 }
5246 
5247 Value *llvm::simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
5248                                       const SimplifyQuery &Q) {
5249   return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
5250 }
5251 
5252 /// Given operands for an ExtractElementInst, see if we can fold the result.
5253 /// If not, this returns null.
5254 static Value *simplifyExtractElementInst(Value *Vec, Value *Idx,
5255                                          const SimplifyQuery &Q, unsigned) {
5256   auto *VecVTy = cast<VectorType>(Vec->getType());
5257   if (auto *CVec = dyn_cast<Constant>(Vec)) {
5258     if (auto *CIdx = dyn_cast<Constant>(Idx))
5259       return ConstantExpr::getExtractElement(CVec, CIdx);
5260 
5261     if (Q.isUndefValue(Vec))
5262       return UndefValue::get(VecVTy->getElementType());
5263   }
5264 
5265   // An undef extract index can be arbitrarily chosen to be an out-of-range
5266   // index value, which would result in the instruction being poison.
5267   if (Q.isUndefValue(Idx))
5268     return PoisonValue::get(VecVTy->getElementType());
5269 
5270   // If extracting a specified index from the vector, see if we can recursively
5271   // find a previously computed scalar that was inserted into the vector.
5272   if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
5273     // For fixed-length vector, fold into undef if index is out of bounds.
5274     unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5275     if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
5276       return PoisonValue::get(VecVTy->getElementType());
5277     // Handle case where an element is extracted from a splat.
5278     if (IdxC->getValue().ult(MinNumElts))
5279       if (auto *Splat = getSplatValue(Vec))
5280         return Splat;
5281     if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
5282       return Elt;
5283   } else {
5284     // extractelt x, (insertelt y, elt, n), n -> elt
5285     // If the possibly-variable indices are trivially known to be equal
5286     // (because they are the same operand) then use the value that was
5287     // inserted directly.
5288     auto *IE = dyn_cast<InsertElementInst>(Vec);
5289     if (IE && IE->getOperand(2) == Idx)
5290       return IE->getOperand(1);
5291 
5292     // The index is not relevant if our vector is a splat.
5293     if (Value *Splat = getSplatValue(Vec))
5294       return Splat;
5295   }
5296   return nullptr;
5297 }
5298 
5299 Value *llvm::simplifyExtractElementInst(Value *Vec, Value *Idx,
5300                                         const SimplifyQuery &Q) {
5301   return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
5302 }
5303 
5304 /// See if we can fold the given phi. If not, returns null.
5305 static Value *simplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
5306                               const SimplifyQuery &Q) {
5307   // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
5308   //          here, because the PHI we may succeed simplifying to was not
5309   //          def-reachable from the original PHI!
5310 
5311   // If all of the PHI's incoming values are the same then replace the PHI node
5312   // with the common value.
5313   Value *CommonValue = nullptr;
5314   bool HasPoisonInput = false;
5315   bool HasUndefInput = false;
5316   for (Value *Incoming : IncomingValues) {
5317     // If the incoming value is the phi node itself, it can safely be skipped.
5318     if (Incoming == PN)
5319       continue;
5320     if (isa<PoisonValue>(Incoming)) {
5321       HasPoisonInput = true;
5322       continue;
5323     }
5324     if (Q.isUndefValue(Incoming)) {
5325       // Remember that we saw an undef value, but otherwise ignore them.
5326       HasUndefInput = true;
5327       continue;
5328     }
5329     if (CommonValue && Incoming != CommonValue)
5330       return nullptr; // Not the same, bail out.
5331     CommonValue = Incoming;
5332   }
5333 
5334   // If CommonValue is null then all of the incoming values were either undef,
5335   // poison or equal to the phi node itself.
5336   if (!CommonValue)
5337     return HasUndefInput ? UndefValue::get(PN->getType())
5338                          : PoisonValue::get(PN->getType());
5339 
5340   if (HasPoisonInput || HasUndefInput) {
5341     // If we have a PHI node like phi(X, undef, X), where X is defined by some
5342     // instruction, we cannot return X as the result of the PHI node unless it
5343     // dominates the PHI block.
5344     if (!valueDominatesPHI(CommonValue, PN, Q.DT))
5345       return nullptr;
5346 
5347     // Make sure we do not replace an undef value with poison.
5348     if (HasUndefInput &&
5349         !isGuaranteedNotToBePoison(CommonValue, Q.AC, Q.CxtI, Q.DT))
5350       return nullptr;
5351     return CommonValue;
5352   }
5353 
5354   return CommonValue;
5355 }
5356 
5357 static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5358                                const SimplifyQuery &Q, unsigned MaxRecurse) {
5359   if (auto *C = dyn_cast<Constant>(Op))
5360     return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
5361 
5362   if (auto *CI = dyn_cast<CastInst>(Op)) {
5363     auto *Src = CI->getOperand(0);
5364     Type *SrcTy = Src->getType();
5365     Type *MidTy = CI->getType();
5366     Type *DstTy = Ty;
5367     if (Src->getType() == Ty) {
5368       auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
5369       auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
5370       Type *SrcIntPtrTy =
5371           SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
5372       Type *MidIntPtrTy =
5373           MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
5374       Type *DstIntPtrTy =
5375           DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
5376       if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
5377                                          SrcIntPtrTy, MidIntPtrTy,
5378                                          DstIntPtrTy) == Instruction::BitCast)
5379         return Src;
5380     }
5381   }
5382 
5383   // bitcast x -> x
5384   if (CastOpc == Instruction::BitCast)
5385     if (Op->getType() == Ty)
5386       return Op;
5387 
5388   // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
5389   Value *Ptr, *X;
5390   if (CastOpc == Instruction::PtrToInt &&
5391       match(Op, m_PtrAdd(m_Value(Ptr),
5392                          m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
5393       X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
5394     return X;
5395 
5396   return nullptr;
5397 }
5398 
5399 Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5400                               const SimplifyQuery &Q) {
5401   return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
5402 }
5403 
5404 /// For the given destination element of a shuffle, peek through shuffles to
5405 /// match a root vector source operand that contains that element in the same
5406 /// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
5407 static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
5408                                    int MaskVal, Value *RootVec,
5409                                    unsigned MaxRecurse) {
5410   if (!MaxRecurse--)
5411     return nullptr;
5412 
5413   // Bail out if any mask value is undefined. That kind of shuffle may be
5414   // simplified further based on demanded bits or other folds.
5415   if (MaskVal == -1)
5416     return nullptr;
5417 
5418   // The mask value chooses which source operand we need to look at next.
5419   int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
5420   int RootElt = MaskVal;
5421   Value *SourceOp = Op0;
5422   if (MaskVal >= InVecNumElts) {
5423     RootElt = MaskVal - InVecNumElts;
5424     SourceOp = Op1;
5425   }
5426 
5427   // If the source operand is a shuffle itself, look through it to find the
5428   // matching root vector.
5429   if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
5430     return foldIdentityShuffles(
5431         DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
5432         SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
5433   }
5434 
5435   // The source operand is not a shuffle. Initialize the root vector value for
5436   // this shuffle if that has not been done yet.
5437   if (!RootVec)
5438     RootVec = SourceOp;
5439 
5440   // Give up as soon as a source operand does not match the existing root value.
5441   if (RootVec != SourceOp)
5442     return nullptr;
5443 
5444   // The element must be coming from the same lane in the source vector
5445   // (although it may have crossed lanes in intermediate shuffles).
5446   if (RootElt != DestElt)
5447     return nullptr;
5448 
5449   return RootVec;
5450 }
5451 
5452 static Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1,
5453                                         ArrayRef<int> Mask, Type *RetTy,
5454                                         const SimplifyQuery &Q,
5455                                         unsigned MaxRecurse) {
5456   if (all_of(Mask, [](int Elem) { return Elem == PoisonMaskElem; }))
5457     return PoisonValue::get(RetTy);
5458 
5459   auto *InVecTy = cast<VectorType>(Op0->getType());
5460   unsigned MaskNumElts = Mask.size();
5461   ElementCount InVecEltCount = InVecTy->getElementCount();
5462 
5463   bool Scalable = InVecEltCount.isScalable();
5464 
5465   SmallVector<int, 32> Indices;
5466   Indices.assign(Mask.begin(), Mask.end());
5467 
5468   // Canonicalization: If mask does not select elements from an input vector,
5469   // replace that input vector with poison.
5470   if (!Scalable) {
5471     bool MaskSelects0 = false, MaskSelects1 = false;
5472     unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
5473     for (unsigned i = 0; i != MaskNumElts; ++i) {
5474       if (Indices[i] == -1)
5475         continue;
5476       if ((unsigned)Indices[i] < InVecNumElts)
5477         MaskSelects0 = true;
5478       else
5479         MaskSelects1 = true;
5480     }
5481     if (!MaskSelects0)
5482       Op0 = PoisonValue::get(InVecTy);
5483     if (!MaskSelects1)
5484       Op1 = PoisonValue::get(InVecTy);
5485   }
5486 
5487   auto *Op0Const = dyn_cast<Constant>(Op0);
5488   auto *Op1Const = dyn_cast<Constant>(Op1);
5489 
5490   // If all operands are constant, constant fold the shuffle. This
5491   // transformation depends on the value of the mask which is not known at
5492   // compile time for scalable vectors
5493   if (Op0Const && Op1Const)
5494     return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
5495 
5496   // Canonicalization: if only one input vector is constant, it shall be the
5497   // second one. This transformation depends on the value of the mask which
5498   // is not known at compile time for scalable vectors
5499   if (!Scalable && Op0Const && !Op1Const) {
5500     std::swap(Op0, Op1);
5501     ShuffleVectorInst::commuteShuffleMask(Indices,
5502                                           InVecEltCount.getKnownMinValue());
5503   }
5504 
5505   // A splat of an inserted scalar constant becomes a vector constant:
5506   // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
5507   // NOTE: We may have commuted above, so analyze the updated Indices, not the
5508   //       original mask constant.
5509   // NOTE: This transformation depends on the value of the mask which is not
5510   // known at compile time for scalable vectors
5511   Constant *C;
5512   ConstantInt *IndexC;
5513   if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C),
5514                                           m_ConstantInt(IndexC)))) {
5515     // Match a splat shuffle mask of the insert index allowing undef elements.
5516     int InsertIndex = IndexC->getZExtValue();
5517     if (all_of(Indices, [InsertIndex](int MaskElt) {
5518           return MaskElt == InsertIndex || MaskElt == -1;
5519         })) {
5520       assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
5521 
5522       // Shuffle mask poisons become poison constant result elements.
5523       SmallVector<Constant *, 16> VecC(MaskNumElts, C);
5524       for (unsigned i = 0; i != MaskNumElts; ++i)
5525         if (Indices[i] == -1)
5526           VecC[i] = PoisonValue::get(C->getType());
5527       return ConstantVector::get(VecC);
5528     }
5529   }
5530 
5531   // A shuffle of a splat is always the splat itself. Legal if the shuffle's
5532   // value type is same as the input vectors' type.
5533   if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
5534     if (Q.isUndefValue(Op1) && RetTy == InVecTy &&
5535         all_equal(OpShuf->getShuffleMask()))
5536       return Op0;
5537 
5538   // All remaining transformation depend on the value of the mask, which is
5539   // not known at compile time for scalable vectors.
5540   if (Scalable)
5541     return nullptr;
5542 
5543   // Don't fold a shuffle with undef mask elements. This may get folded in a
5544   // better way using demanded bits or other analysis.
5545   // TODO: Should we allow this?
5546   if (is_contained(Indices, -1))
5547     return nullptr;
5548 
5549   // Check if every element of this shuffle can be mapped back to the
5550   // corresponding element of a single root vector. If so, we don't need this
5551   // shuffle. This handles simple identity shuffles as well as chains of
5552   // shuffles that may widen/narrow and/or move elements across lanes and back.
5553   Value *RootVec = nullptr;
5554   for (unsigned i = 0; i != MaskNumElts; ++i) {
5555     // Note that recursion is limited for each vector element, so if any element
5556     // exceeds the limit, this will fail to simplify.
5557     RootVec =
5558         foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
5559 
5560     // We can't replace a widening/narrowing shuffle with one of its operands.
5561     if (!RootVec || RootVec->getType() != RetTy)
5562       return nullptr;
5563   }
5564   return RootVec;
5565 }
5566 
5567 /// Given operands for a ShuffleVectorInst, fold the result or return null.
5568 Value *llvm::simplifyShuffleVectorInst(Value *Op0, Value *Op1,
5569                                        ArrayRef<int> Mask, Type *RetTy,
5570                                        const SimplifyQuery &Q) {
5571   return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
5572 }
5573 
5574 static Constant *foldConstant(Instruction::UnaryOps Opcode, Value *&Op,
5575                               const SimplifyQuery &Q) {
5576   if (auto *C = dyn_cast<Constant>(Op))
5577     return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
5578   return nullptr;
5579 }
5580 
5581 /// Given the operand for an FNeg, see if we can fold the result.  If not, this
5582 /// returns null.
5583 static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF,
5584                                const SimplifyQuery &Q, unsigned MaxRecurse) {
5585   if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
5586     return C;
5587 
5588   Value *X;
5589   // fneg (fneg X) ==> X
5590   if (match(Op, m_FNeg(m_Value(X))))
5591     return X;
5592 
5593   return nullptr;
5594 }
5595 
5596 Value *llvm::simplifyFNegInst(Value *Op, FastMathFlags FMF,
5597                               const SimplifyQuery &Q) {
5598   return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
5599 }
5600 
5601 /// Try to propagate existing NaN values when possible. If not, replace the
5602 /// constant or elements in the constant with a canonical NaN.
5603 static Constant *propagateNaN(Constant *In) {
5604   Type *Ty = In->getType();
5605   if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
5606     unsigned NumElts = VecTy->getNumElements();
5607     SmallVector<Constant *, 32> NewC(NumElts);
5608     for (unsigned i = 0; i != NumElts; ++i) {
5609       Constant *EltC = In->getAggregateElement(i);
5610       // Poison elements propagate. NaN propagates except signaling is quieted.
5611       // Replace unknown or undef elements with canonical NaN.
5612       if (EltC && isa<PoisonValue>(EltC))
5613         NewC[i] = EltC;
5614       else if (EltC && EltC->isNaN())
5615         NewC[i] = ConstantFP::get(
5616             EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
5617       else
5618         NewC[i] = ConstantFP::getNaN(VecTy->getElementType());
5619     }
5620     return ConstantVector::get(NewC);
5621   }
5622 
5623   // If it is not a fixed vector, but not a simple NaN either, return a
5624   // canonical NaN.
5625   if (!In->isNaN())
5626     return ConstantFP::getNaN(Ty);
5627 
5628   // If we known this is a NaN, and it's scalable vector, we must have a splat
5629   // on our hands. Grab that before splatting a QNaN constant.
5630   if (isa<ScalableVectorType>(Ty)) {
5631     auto *Splat = In->getSplatValue();
5632     assert(Splat && Splat->isNaN() &&
5633            "Found a scalable-vector NaN but not a splat");
5634     In = Splat;
5635   }
5636 
5637   // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but
5638   // preserve the sign/payload.
5639   return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());
5640 }
5641 
5642 /// Perform folds that are common to any floating-point operation. This implies
5643 /// transforms based on poison/undef/NaN because the operation itself makes no
5644 /// difference to the result.
5645 static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
5646                               const SimplifyQuery &Q,
5647                               fp::ExceptionBehavior ExBehavior,
5648                               RoundingMode Rounding) {
5649   // Poison is independent of anything else. It always propagates from an
5650   // operand to a math result.
5651   if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5652     return PoisonValue::get(Ops[0]->getType());
5653 
5654   for (Value *V : Ops) {
5655     bool IsNan = match(V, m_NaN());
5656     bool IsInf = match(V, m_Inf());
5657     bool IsUndef = Q.isUndefValue(V);
5658 
5659     // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
5660     // (an undef operand can be chosen to be Nan/Inf), then the result of
5661     // this operation is poison.
5662     if (FMF.noNaNs() && (IsNan || IsUndef))
5663       return PoisonValue::get(V->getType());
5664     if (FMF.noInfs() && (IsInf || IsUndef))
5665       return PoisonValue::get(V->getType());
5666 
5667     if (isDefaultFPEnvironment(ExBehavior, Rounding)) {
5668       // Undef does not propagate because undef means that all bits can take on
5669       // any value. If this is undef * NaN for example, then the result values
5670       // (at least the exponent bits) are limited. Assume the undef is a
5671       // canonical NaN and propagate that.
5672       if (IsUndef)
5673         return ConstantFP::getNaN(V->getType());
5674       if (IsNan)
5675         return propagateNaN(cast<Constant>(V));
5676     } else if (ExBehavior != fp::ebStrict) {
5677       if (IsNan)
5678         return propagateNaN(cast<Constant>(V));
5679     }
5680   }
5681   return nullptr;
5682 }
5683 
5684 /// Given operands for an FAdd, see if we can fold the result.  If not, this
5685 /// returns null.
5686 static Value *
5687 simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5688                  const SimplifyQuery &Q, unsigned MaxRecurse,
5689                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5690                  RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5691   if (isDefaultFPEnvironment(ExBehavior, Rounding))
5692     if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
5693       return C;
5694 
5695   if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5696     return C;
5697 
5698   // fadd X, -0 ==> X
5699   // With strict/constrained FP, we have these possible edge cases that do
5700   // not simplify to Op0:
5701   // fadd SNaN, -0.0 --> QNaN
5702   // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
5703   if (canIgnoreSNaN(ExBehavior, FMF) &&
5704       (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5705        FMF.noSignedZeros()))
5706     if (match(Op1, m_NegZeroFP()))
5707       return Op0;
5708 
5709   // fadd X, 0 ==> X, when we know X is not -0
5710   if (canIgnoreSNaN(ExBehavior, FMF))
5711     if (match(Op1, m_PosZeroFP()) &&
5712         (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, /*Depth=*/0, Q)))
5713       return Op0;
5714 
5715   if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5716     return nullptr;
5717 
5718   if (FMF.noNaNs()) {
5719     // With nnan: X + {+/-}Inf --> {+/-}Inf
5720     if (match(Op1, m_Inf()))
5721       return Op1;
5722 
5723     // With nnan: -X + X --> 0.0 (and commuted variant)
5724     // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
5725     // Negative zeros are allowed because we always end up with positive zero:
5726     // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5727     // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5728     // X =  0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
5729     // X =  0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
5730     if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
5731         match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
5732       return ConstantFP::getZero(Op0->getType());
5733 
5734     if (match(Op0, m_FNeg(m_Specific(Op1))) ||
5735         match(Op1, m_FNeg(m_Specific(Op0))))
5736       return ConstantFP::getZero(Op0->getType());
5737   }
5738 
5739   // (X - Y) + Y --> X
5740   // Y + (X - Y) --> X
5741   Value *X;
5742   if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5743       (match(Op0, m_FSub(m_Value(X), m_Specific(Op1))) ||
5744        match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))))
5745     return X;
5746 
5747   return nullptr;
5748 }
5749 
5750 /// Given operands for an FSub, see if we can fold the result.  If not, this
5751 /// returns null.
5752 static Value *
5753 simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5754                  const SimplifyQuery &Q, unsigned MaxRecurse,
5755                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5756                  RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5757   if (isDefaultFPEnvironment(ExBehavior, Rounding))
5758     if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
5759       return C;
5760 
5761   if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5762     return C;
5763 
5764   // fsub X, +0 ==> X
5765   if (canIgnoreSNaN(ExBehavior, FMF) &&
5766       (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5767        FMF.noSignedZeros()))
5768     if (match(Op1, m_PosZeroFP()))
5769       return Op0;
5770 
5771   // fsub X, -0 ==> X, when we know X is not -0
5772   if (canIgnoreSNaN(ExBehavior, FMF))
5773     if (match(Op1, m_NegZeroFP()) &&
5774         (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, /*Depth=*/0, Q)))
5775       return Op0;
5776 
5777   // fsub -0.0, (fsub -0.0, X) ==> X
5778   // fsub -0.0, (fneg X) ==> X
5779   Value *X;
5780   if (canIgnoreSNaN(ExBehavior, FMF))
5781     if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X))))
5782       return X;
5783 
5784   // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
5785   // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
5786   if (canIgnoreSNaN(ExBehavior, FMF))
5787     if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) &&
5788         (match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X))) ||
5789          match(Op1, m_FNeg(m_Value(X)))))
5790       return X;
5791 
5792   if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5793     return nullptr;
5794 
5795   if (FMF.noNaNs()) {
5796     // fsub nnan x, x ==> 0.0
5797     if (Op0 == Op1)
5798       return Constant::getNullValue(Op0->getType());
5799 
5800     // With nnan: {+/-}Inf - X --> {+/-}Inf
5801     if (match(Op0, m_Inf()))
5802       return Op0;
5803 
5804     // With nnan: X - {+/-}Inf --> {-/+}Inf
5805     if (match(Op1, m_Inf()))
5806       return foldConstant(Instruction::FNeg, Op1, Q);
5807   }
5808 
5809   // Y - (Y - X) --> X
5810   // (X + Y) - Y --> X
5811   if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5812       (match(Op1, m_FSub(m_Specific(Op0), m_Value(X))) ||
5813        match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))))
5814     return X;
5815 
5816   return nullptr;
5817 }
5818 
5819 static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
5820                               const SimplifyQuery &Q, unsigned MaxRecurse,
5821                               fp::ExceptionBehavior ExBehavior,
5822                               RoundingMode Rounding) {
5823   if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5824     return C;
5825 
5826   if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5827     return nullptr;
5828 
5829   // Canonicalize special constants as operand 1.
5830   if (match(Op0, m_FPOne()) || match(Op0, m_AnyZeroFP()))
5831     std::swap(Op0, Op1);
5832 
5833   // X * 1.0 --> X
5834   if (match(Op1, m_FPOne()))
5835     return Op0;
5836 
5837   if (match(Op1, m_AnyZeroFP())) {
5838     // X * 0.0 --> 0.0 (with nnan and nsz)
5839     if (FMF.noNaNs() && FMF.noSignedZeros())
5840       return ConstantFP::getZero(Op0->getType());
5841 
5842     KnownFPClass Known =
5843         computeKnownFPClass(Op0, FMF, fcInf | fcNan, /*Depth=*/0, Q);
5844     if (Known.isKnownNever(fcInf | fcNan)) {
5845       // +normal number * (-)0.0 --> (-)0.0
5846       if (Known.SignBit == false)
5847         return Op1;
5848       // -normal number * (-)0.0 --> -(-)0.0
5849       if (Known.SignBit == true)
5850         return foldConstant(Instruction::FNeg, Op1, Q);
5851     }
5852   }
5853 
5854   // sqrt(X) * sqrt(X) --> X, if we can:
5855   // 1. Remove the intermediate rounding (reassociate).
5856   // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
5857   // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
5858   Value *X;
5859   if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X))) && FMF.allowReassoc() &&
5860       FMF.noNaNs() && FMF.noSignedZeros())
5861     return X;
5862 
5863   return nullptr;
5864 }
5865 
5866 /// Given the operands for an FMul, see if we can fold the result
5867 static Value *
5868 simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5869                  const SimplifyQuery &Q, unsigned MaxRecurse,
5870                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5871                  RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5872   if (isDefaultFPEnvironment(ExBehavior, Rounding))
5873     if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
5874       return C;
5875 
5876   // Now apply simplifications that do not require rounding.
5877   return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
5878 }
5879 
5880 Value *llvm::simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5881                               const SimplifyQuery &Q,
5882                               fp::ExceptionBehavior ExBehavior,
5883                               RoundingMode Rounding) {
5884   return ::simplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5885                             Rounding);
5886 }
5887 
5888 Value *llvm::simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5889                               const SimplifyQuery &Q,
5890                               fp::ExceptionBehavior ExBehavior,
5891                               RoundingMode Rounding) {
5892   return ::simplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5893                             Rounding);
5894 }
5895 
5896 Value *llvm::simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5897                               const SimplifyQuery &Q,
5898                               fp::ExceptionBehavior ExBehavior,
5899                               RoundingMode Rounding) {
5900   return ::simplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5901                             Rounding);
5902 }
5903 
5904 Value *llvm::simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
5905                              const SimplifyQuery &Q,
5906                              fp::ExceptionBehavior ExBehavior,
5907                              RoundingMode Rounding) {
5908   return ::simplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5909                            Rounding);
5910 }
5911 
5912 static Value *
5913 simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5914                  const SimplifyQuery &Q, unsigned,
5915                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5916                  RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5917   if (isDefaultFPEnvironment(ExBehavior, Rounding))
5918     if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
5919       return C;
5920 
5921   if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5922     return C;
5923 
5924   if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5925     return nullptr;
5926 
5927   // X / 1.0 -> X
5928   if (match(Op1, m_FPOne()))
5929     return Op0;
5930 
5931   // 0 / X -> 0
5932   // Requires that NaNs are off (X could be zero) and signed zeroes are
5933   // ignored (X could be positive or negative, so the output sign is unknown).
5934   if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
5935     return ConstantFP::getZero(Op0->getType());
5936 
5937   if (FMF.noNaNs()) {
5938     // X / X -> 1.0 is legal when NaNs are ignored.
5939     // We can ignore infinities because INF/INF is NaN.
5940     if (Op0 == Op1)
5941       return ConstantFP::get(Op0->getType(), 1.0);
5942 
5943     // (X * Y) / Y --> X if we can reassociate to the above form.
5944     Value *X;
5945     if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1))))
5946       return X;
5947 
5948     // -X /  X -> -1.0 and
5949     //  X / -X -> -1.0 are legal when NaNs are ignored.
5950     // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
5951     if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
5952         match(Op1, m_FNegNSZ(m_Specific(Op0))))
5953       return ConstantFP::get(Op0->getType(), -1.0);
5954 
5955     // nnan ninf X / [-]0.0 -> poison
5956     if (FMF.noInfs() && match(Op1, m_AnyZeroFP()))
5957       return PoisonValue::get(Op1->getType());
5958   }
5959 
5960   return nullptr;
5961 }
5962 
5963 Value *llvm::simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5964                               const SimplifyQuery &Q,
5965                               fp::ExceptionBehavior ExBehavior,
5966                               RoundingMode Rounding) {
5967   return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5968                             Rounding);
5969 }
5970 
5971 static Value *
5972 simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5973                  const SimplifyQuery &Q, unsigned,
5974                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5975                  RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5976   if (isDefaultFPEnvironment(ExBehavior, Rounding))
5977     if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
5978       return C;
5979 
5980   if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5981     return C;
5982 
5983   if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5984     return nullptr;
5985 
5986   // Unlike fdiv, the result of frem always matches the sign of the dividend.
5987   // The constant match may include undef elements in a vector, so return a full
5988   // zero constant as the result.
5989   if (FMF.noNaNs()) {
5990     // +0 % X -> 0
5991     if (match(Op0, m_PosZeroFP()))
5992       return ConstantFP::getZero(Op0->getType());
5993     // -0 % X -> -0
5994     if (match(Op0, m_NegZeroFP()))
5995       return ConstantFP::getNegativeZero(Op0->getType());
5996   }
5997 
5998   return nullptr;
5999 }
6000 
6001 Value *llvm::simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6002                               const SimplifyQuery &Q,
6003                               fp::ExceptionBehavior ExBehavior,
6004                               RoundingMode Rounding) {
6005   return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6006                             Rounding);
6007 }
6008 
6009 //=== Helper functions for higher up the class hierarchy.
6010 
6011 /// Given the operand for a UnaryOperator, see if we can fold the result.
6012 /// If not, this returns null.
6013 static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
6014                            unsigned MaxRecurse) {
6015   switch (Opcode) {
6016   case Instruction::FNeg:
6017     return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
6018   default:
6019     llvm_unreachable("Unexpected opcode");
6020   }
6021 }
6022 
6023 /// Given the operand for a UnaryOperator, see if we can fold the result.
6024 /// If not, this returns null.
6025 /// Try to use FastMathFlags when folding the result.
6026 static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
6027                              const FastMathFlags &FMF, const SimplifyQuery &Q,
6028                              unsigned MaxRecurse) {
6029   switch (Opcode) {
6030   case Instruction::FNeg:
6031     return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
6032   default:
6033     return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
6034   }
6035 }
6036 
6037 Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
6038   return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
6039 }
6040 
6041 Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
6042                           const SimplifyQuery &Q) {
6043   return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
6044 }
6045 
6046 /// Given operands for a BinaryOperator, see if we can fold the result.
6047 /// If not, this returns null.
6048 static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6049                             const SimplifyQuery &Q, unsigned MaxRecurse) {
6050   switch (Opcode) {
6051   case Instruction::Add:
6052     return simplifyAddInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6053                            MaxRecurse);
6054   case Instruction::Sub:
6055     return simplifySubInst(LHS, RHS,  /* IsNSW */ false, /* IsNUW */ false, Q,
6056                            MaxRecurse);
6057   case Instruction::Mul:
6058     return simplifyMulInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6059                            MaxRecurse);
6060   case Instruction::SDiv:
6061     return simplifySDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6062   case Instruction::UDiv:
6063     return simplifyUDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6064   case Instruction::SRem:
6065     return simplifySRemInst(LHS, RHS, Q, MaxRecurse);
6066   case Instruction::URem:
6067     return simplifyURemInst(LHS, RHS, Q, MaxRecurse);
6068   case Instruction::Shl:
6069     return simplifyShlInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6070                            MaxRecurse);
6071   case Instruction::LShr:
6072     return simplifyLShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6073   case Instruction::AShr:
6074     return simplifyAShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6075   case Instruction::And:
6076     return simplifyAndInst(LHS, RHS, Q, MaxRecurse);
6077   case Instruction::Or:
6078     return simplifyOrInst(LHS, RHS, Q, MaxRecurse);
6079   case Instruction::Xor:
6080     return simplifyXorInst(LHS, RHS, Q, MaxRecurse);
6081   case Instruction::FAdd:
6082     return simplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6083   case Instruction::FSub:
6084     return simplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6085   case Instruction::FMul:
6086     return simplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6087   case Instruction::FDiv:
6088     return simplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6089   case Instruction::FRem:
6090     return simplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6091   default:
6092     llvm_unreachable("Unexpected opcode");
6093   }
6094 }
6095 
6096 /// Given operands for a BinaryOperator, see if we can fold the result.
6097 /// If not, this returns null.
6098 /// Try to use FastMathFlags when folding the result.
6099 static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6100                             const FastMathFlags &FMF, const SimplifyQuery &Q,
6101                             unsigned MaxRecurse) {
6102   switch (Opcode) {
6103   case Instruction::FAdd:
6104     return simplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
6105   case Instruction::FSub:
6106     return simplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
6107   case Instruction::FMul:
6108     return simplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
6109   case Instruction::FDiv:
6110     return simplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
6111   default:
6112     return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
6113   }
6114 }
6115 
6116 Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6117                            const SimplifyQuery &Q) {
6118   return ::simplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
6119 }
6120 
6121 Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6122                            FastMathFlags FMF, const SimplifyQuery &Q) {
6123   return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
6124 }
6125 
6126 /// Given operands for a CmpInst, see if we can fold the result.
6127 static Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
6128                               const SimplifyQuery &Q, unsigned MaxRecurse) {
6129   if (CmpInst::isIntPredicate(Predicate))
6130     return simplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
6131   return simplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6132 }
6133 
6134 Value *llvm::simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
6135                              const SimplifyQuery &Q) {
6136   return ::simplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
6137 }
6138 
6139 static bool isIdempotent(Intrinsic::ID ID) {
6140   switch (ID) {
6141   default:
6142     return false;
6143 
6144   // Unary idempotent: f(f(x)) = f(x)
6145   case Intrinsic::fabs:
6146   case Intrinsic::floor:
6147   case Intrinsic::ceil:
6148   case Intrinsic::trunc:
6149   case Intrinsic::rint:
6150   case Intrinsic::nearbyint:
6151   case Intrinsic::round:
6152   case Intrinsic::roundeven:
6153   case Intrinsic::canonicalize:
6154   case Intrinsic::arithmetic_fence:
6155     return true;
6156   }
6157 }
6158 
6159 /// Return true if the intrinsic rounds a floating-point value to an integral
6160 /// floating-point value (not an integer type).
6161 static bool removesFPFraction(Intrinsic::ID ID) {
6162   switch (ID) {
6163   default:
6164     return false;
6165 
6166   case Intrinsic::floor:
6167   case Intrinsic::ceil:
6168   case Intrinsic::trunc:
6169   case Intrinsic::rint:
6170   case Intrinsic::nearbyint:
6171   case Intrinsic::round:
6172   case Intrinsic::roundeven:
6173     return true;
6174   }
6175 }
6176 
6177 static Value *simplifyRelativeLoad(Constant *Ptr, Constant *Offset,
6178                                    const DataLayout &DL) {
6179   GlobalValue *PtrSym;
6180   APInt PtrOffset;
6181   if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
6182     return nullptr;
6183 
6184   Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
6185 
6186   auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
6187   if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6188     return nullptr;
6189 
6190   APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(
6191       DL.getIndexTypeSizeInBits(Ptr->getType()));
6192   if (OffsetInt.srem(4) != 0)
6193     return nullptr;
6194 
6195   Constant *Loaded =
6196       ConstantFoldLoadFromConstPtr(Ptr, Int32Ty, std::move(OffsetInt), DL);
6197   if (!Loaded)
6198     return nullptr;
6199 
6200   auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
6201   if (!LoadedCE)
6202     return nullptr;
6203 
6204   if (LoadedCE->getOpcode() == Instruction::Trunc) {
6205     LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6206     if (!LoadedCE)
6207       return nullptr;
6208   }
6209 
6210   if (LoadedCE->getOpcode() != Instruction::Sub)
6211     return nullptr;
6212 
6213   auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6214   if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6215     return nullptr;
6216   auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
6217 
6218   Constant *LoadedRHS = LoadedCE->getOperand(1);
6219   GlobalValue *LoadedRHSSym;
6220   APInt LoadedRHSOffset;
6221   if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
6222                                   DL) ||
6223       PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
6224     return nullptr;
6225 
6226   return LoadedLHSPtr;
6227 }
6228 
6229 // TODO: Need to pass in FastMathFlags
6230 static Value *simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q,
6231                             bool IsStrict) {
6232   // ldexp(poison, x) -> poison
6233   // ldexp(x, poison) -> poison
6234   if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6235     return Op0;
6236 
6237   // ldexp(undef, x) -> nan
6238   if (Q.isUndefValue(Op0))
6239     return ConstantFP::getNaN(Op0->getType());
6240 
6241   if (!IsStrict) {
6242     // TODO: Could insert a canonicalize for strict
6243 
6244     // ldexp(x, undef) -> x
6245     if (Q.isUndefValue(Op1))
6246       return Op0;
6247   }
6248 
6249   const APFloat *C = nullptr;
6250   match(Op0, PatternMatch::m_APFloat(C));
6251 
6252   // These cases should be safe, even with strictfp.
6253   // ldexp(0.0, x) -> 0.0
6254   // ldexp(-0.0, x) -> -0.0
6255   // ldexp(inf, x) -> inf
6256   // ldexp(-inf, x) -> -inf
6257   if (C && (C->isZero() || C->isInfinity()))
6258     return Op0;
6259 
6260   // These are canonicalization dropping, could do it if we knew how we could
6261   // ignore denormal flushes and target handling of nan payload bits.
6262   if (IsStrict)
6263     return nullptr;
6264 
6265   // TODO: Could quiet this with strictfp if the exception mode isn't strict.
6266   if (C && C->isNaN())
6267     return ConstantFP::get(Op0->getType(), C->makeQuiet());
6268 
6269   // ldexp(x, 0) -> x
6270 
6271   // TODO: Could fold this if we know the exception mode isn't
6272   // strict, we know the denormal mode and other target modes.
6273   if (match(Op1, PatternMatch::m_ZeroInt()))
6274     return Op0;
6275 
6276   return nullptr;
6277 }
6278 
6279 static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
6280                                      const SimplifyQuery &Q,
6281                                      const CallBase *Call) {
6282   // Idempotent functions return the same result when called repeatedly.
6283   Intrinsic::ID IID = F->getIntrinsicID();
6284   if (isIdempotent(IID))
6285     if (auto *II = dyn_cast<IntrinsicInst>(Op0))
6286       if (II->getIntrinsicID() == IID)
6287         return II;
6288 
6289   if (removesFPFraction(IID)) {
6290     // Converting from int or calling a rounding function always results in a
6291     // finite integral number or infinity. For those inputs, rounding functions
6292     // always return the same value, so the (2nd) rounding is eliminated. Ex:
6293     // floor (sitofp x) -> sitofp x
6294     // round (ceil x) -> ceil x
6295     auto *II = dyn_cast<IntrinsicInst>(Op0);
6296     if ((II && removesFPFraction(II->getIntrinsicID())) ||
6297         match(Op0, m_SIToFP(m_Value())) || match(Op0, m_UIToFP(m_Value())))
6298       return Op0;
6299   }
6300 
6301   Value *X;
6302   switch (IID) {
6303   case Intrinsic::fabs:
6304     if (computeKnownFPSignBit(Op0, /*Depth=*/0, Q) == false)
6305       return Op0;
6306     break;
6307   case Intrinsic::bswap:
6308     // bswap(bswap(x)) -> x
6309     if (match(Op0, m_BSwap(m_Value(X))))
6310       return X;
6311     break;
6312   case Intrinsic::bitreverse:
6313     // bitreverse(bitreverse(x)) -> x
6314     if (match(Op0, m_BitReverse(m_Value(X))))
6315       return X;
6316     break;
6317   case Intrinsic::ctpop: {
6318     // ctpop(X) -> 1 iff X is non-zero power of 2.
6319     if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
6320                                Q.DT))
6321       return ConstantInt::get(Op0->getType(), 1);
6322     // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
6323     // ctpop(and X, 1) --> and X, 1
6324     unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6325     if (MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, BitWidth - 1),
6326                           Q))
6327       return Op0;
6328     break;
6329   }
6330   case Intrinsic::exp:
6331     // exp(log(x)) -> x
6332     if (Call->hasAllowReassoc() &&
6333         match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X))))
6334       return X;
6335     break;
6336   case Intrinsic::exp2:
6337     // exp2(log2(x)) -> x
6338     if (Call->hasAllowReassoc() &&
6339         match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X))))
6340       return X;
6341     break;
6342   case Intrinsic::exp10:
6343     // exp10(log10(x)) -> x
6344     if (Call->hasAllowReassoc() &&
6345         match(Op0, m_Intrinsic<Intrinsic::log10>(m_Value(X))))
6346       return X;
6347     break;
6348   case Intrinsic::log:
6349     // log(exp(x)) -> x
6350     if (Call->hasAllowReassoc() &&
6351         match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))))
6352       return X;
6353     break;
6354   case Intrinsic::log2:
6355     // log2(exp2(x)) -> x
6356     if (Call->hasAllowReassoc() &&
6357         (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) ||
6358          match(Op0,
6359                m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0), m_Value(X)))))
6360       return X;
6361     break;
6362   case Intrinsic::log10:
6363     // log10(pow(10.0, x)) -> x
6364     // log10(exp10(x)) -> x
6365     if (Call->hasAllowReassoc() &&
6366         (match(Op0, m_Intrinsic<Intrinsic::exp10>(m_Value(X))) ||
6367          match(Op0,
6368                m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0), m_Value(X)))))
6369       return X;
6370     break;
6371   case Intrinsic::vector_reverse:
6372     // vector.reverse(vector.reverse(x)) -> x
6373     if (match(Op0, m_VecReverse(m_Value(X))))
6374       return X;
6375     // vector.reverse(splat(X)) -> splat(X)
6376     if (isSplatValue(Op0))
6377       return Op0;
6378     break;
6379   case Intrinsic::frexp: {
6380     // Frexp is idempotent with the added complication of the struct return.
6381     if (match(Op0, m_ExtractValue<0>(m_Value(X)))) {
6382       if (match(X, m_Intrinsic<Intrinsic::frexp>(m_Value())))
6383         return X;
6384     }
6385 
6386     break;
6387   }
6388   default:
6389     break;
6390   }
6391 
6392   return nullptr;
6393 }
6394 
6395 /// Given a min/max intrinsic, see if it can be removed based on having an
6396 /// operand that is another min/max intrinsic with shared operand(s). The caller
6397 /// is expected to swap the operand arguments to handle commutation.
6398 static Value *foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1) {
6399   Value *X, *Y;
6400   if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y))))
6401     return nullptr;
6402 
6403   auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
6404   if (!MM0)
6405     return nullptr;
6406   Intrinsic::ID IID0 = MM0->getIntrinsicID();
6407 
6408   if (Op1 == X || Op1 == Y ||
6409       match(Op1, m_c_MaxOrMin(m_Specific(X), m_Specific(Y)))) {
6410     // max (max X, Y), X --> max X, Y
6411     if (IID0 == IID)
6412       return MM0;
6413     // max (min X, Y), X --> X
6414     if (IID0 == getInverseMinMaxIntrinsic(IID))
6415       return Op1;
6416   }
6417   return nullptr;
6418 }
6419 
6420 /// Given a min/max intrinsic, see if it can be removed based on having an
6421 /// operand that is another min/max intrinsic with shared operand(s). The caller
6422 /// is expected to swap the operand arguments to handle commutation.
6423 static Value *foldMinimumMaximumSharedOp(Intrinsic::ID IID, Value *Op0,
6424                                          Value *Op1) {
6425   assert((IID == Intrinsic::maxnum || IID == Intrinsic::minnum ||
6426           IID == Intrinsic::maximum || IID == Intrinsic::minimum) &&
6427          "Unsupported intrinsic");
6428 
6429   auto *M0 = dyn_cast<IntrinsicInst>(Op0);
6430   // If Op0 is not the same intrinsic as IID, do not process.
6431   // This is a difference with integer min/max handling. We do not process the
6432   // case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN.
6433   if (!M0 || M0->getIntrinsicID() != IID)
6434     return nullptr;
6435   Value *X0 = M0->getOperand(0);
6436   Value *Y0 = M0->getOperand(1);
6437   // Simple case, m(m(X,Y), X) => m(X, Y)
6438   //              m(m(X,Y), Y) => m(X, Y)
6439   // For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN.
6440   // For minimum/maximum, Y is NaN => m(X, NaN) == NaN  and m(NaN, NaN) == NaN.
6441   // For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y.
6442   // For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X.
6443   if (X0 == Op1 || Y0 == Op1)
6444     return M0;
6445 
6446   auto *M1 = dyn_cast<IntrinsicInst>(Op1);
6447   if (!M1)
6448     return nullptr;
6449   Value *X1 = M1->getOperand(0);
6450   Value *Y1 = M1->getOperand(1);
6451   Intrinsic::ID IID1 = M1->getIntrinsicID();
6452   // we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative.
6453   // if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y).
6454   // For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN.
6455   // For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN.
6456   // For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y.
6457   // For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X.
6458   if ((X0 == X1 && Y0 == Y1) || (X0 == Y1 && Y0 == X1))
6459     if (IID1 == IID || getInverseMinMaxIntrinsic(IID1) == IID)
6460       return M0;
6461 
6462   return nullptr;
6463 }
6464 
6465 Value *llvm::simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
6466                                      Value *Op0, Value *Op1,
6467                                      const SimplifyQuery &Q,
6468                                      const CallBase *Call) {
6469   unsigned BitWidth = ReturnType->getScalarSizeInBits();
6470   switch (IID) {
6471   case Intrinsic::abs:
6472     // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
6473     // It is always ok to pick the earlier abs. We'll just lose nsw if its only
6474     // on the outer abs.
6475     if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(), m_Value())))
6476       return Op0;
6477     break;
6478 
6479   case Intrinsic::cttz: {
6480     Value *X;
6481     if (match(Op0, m_Shl(m_One(), m_Value(X))))
6482       return X;
6483     break;
6484   }
6485   case Intrinsic::ctlz: {
6486     Value *X;
6487     if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
6488       return X;
6489     if (match(Op0, m_AShr(m_Negative(), m_Value())))
6490       return Constant::getNullValue(ReturnType);
6491     break;
6492   }
6493   case Intrinsic::ptrmask: {
6494     if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6495       return PoisonValue::get(Op0->getType());
6496 
6497     // NOTE: We can't apply this simplifications based on the value of Op1
6498     // because we need to preserve provenance.
6499     if (Q.isUndefValue(Op0) || match(Op0, m_Zero()))
6500       return Constant::getNullValue(Op0->getType());
6501 
6502     assert(Op1->getType()->getScalarSizeInBits() ==
6503                Q.DL.getIndexTypeSizeInBits(Op0->getType()) &&
6504            "Invalid mask width");
6505     // If index-width (mask size) is less than pointer-size then mask is
6506     // 1-extended.
6507     if (match(Op1, m_PtrToInt(m_Specific(Op0))))
6508       return Op0;
6509 
6510     // NOTE: We may have attributes associated with the return value of the
6511     // llvm.ptrmask intrinsic that will be lost when we just return the
6512     // operand. We should try to preserve them.
6513     if (match(Op1, m_AllOnes()) || Q.isUndefValue(Op1))
6514       return Op0;
6515 
6516     Constant *C;
6517     if (match(Op1, m_ImmConstant(C))) {
6518       KnownBits PtrKnown = computeKnownBits(Op0, /*Depth=*/0, Q);
6519       // See if we only masking off bits we know are already zero due to
6520       // alignment.
6521       APInt IrrelevantPtrBits =
6522           PtrKnown.Zero.zextOrTrunc(C->getType()->getScalarSizeInBits());
6523       C = ConstantFoldBinaryOpOperands(
6524           Instruction::Or, C, ConstantInt::get(C->getType(), IrrelevantPtrBits),
6525           Q.DL);
6526       if (C != nullptr && C->isAllOnesValue())
6527         return Op0;
6528     }
6529     break;
6530   }
6531   case Intrinsic::smax:
6532   case Intrinsic::smin:
6533   case Intrinsic::umax:
6534   case Intrinsic::umin: {
6535     // If the arguments are the same, this is a no-op.
6536     if (Op0 == Op1)
6537       return Op0;
6538 
6539     // Canonicalize immediate constant operand as Op1.
6540     if (match(Op0, m_ImmConstant()))
6541       std::swap(Op0, Op1);
6542 
6543     // Assume undef is the limit value.
6544     if (Q.isUndefValue(Op1))
6545       return ConstantInt::get(
6546           ReturnType, MinMaxIntrinsic::getSaturationPoint(IID, BitWidth));
6547 
6548     const APInt *C;
6549     if (match(Op1, m_APIntAllowPoison(C))) {
6550       // Clamp to limit value. For example:
6551       // umax(i8 %x, i8 255) --> 255
6552       if (*C == MinMaxIntrinsic::getSaturationPoint(IID, BitWidth))
6553         return ConstantInt::get(ReturnType, *C);
6554 
6555       // If the constant op is the opposite of the limit value, the other must
6556       // be larger/smaller or equal. For example:
6557       // umin(i8 %x, i8 255) --> %x
6558       if (*C == MinMaxIntrinsic::getSaturationPoint(
6559                     getInverseMinMaxIntrinsic(IID), BitWidth))
6560         return Op0;
6561 
6562       // Remove nested call if constant operands allow it. Example:
6563       // max (max X, 7), 5 -> max X, 7
6564       auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
6565       if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
6566         // TODO: loosen undef/splat restrictions for vector constants.
6567         Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
6568         const APInt *InnerC;
6569         if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))) &&
6570             ICmpInst::compare(*InnerC, *C,
6571                               ICmpInst::getNonStrictPredicate(
6572                                   MinMaxIntrinsic::getPredicate(IID))))
6573           return Op0;
6574       }
6575     }
6576 
6577     if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
6578       return V;
6579     if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0))
6580       return V;
6581 
6582     ICmpInst::Predicate Pred =
6583         ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID));
6584     if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit))
6585       return Op0;
6586     if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
6587       return Op1;
6588 
6589     break;
6590   }
6591   case Intrinsic::scmp:
6592   case Intrinsic::ucmp: {
6593     // Fold to a constant if the relationship between operands can be
6594     // established with certainty
6595     if (isICmpTrue(CmpInst::ICMP_EQ, Op0, Op1, Q, RecursionLimit))
6596       return Constant::getNullValue(ReturnType);
6597 
6598     ICmpInst::Predicate PredGT =
6599         IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
6600     if (isICmpTrue(PredGT, Op0, Op1, Q, RecursionLimit))
6601       return ConstantInt::get(ReturnType, 1);
6602 
6603     ICmpInst::Predicate PredLT =
6604         IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
6605     if (isICmpTrue(PredLT, Op0, Op1, Q, RecursionLimit))
6606       return ConstantInt::getSigned(ReturnType, -1);
6607 
6608     break;
6609   }
6610   case Intrinsic::usub_with_overflow:
6611   case Intrinsic::ssub_with_overflow:
6612     // X - X -> { 0, false }
6613     // X - undef -> { 0, false }
6614     // undef - X -> { 0, false }
6615     if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6616       return Constant::getNullValue(ReturnType);
6617     break;
6618   case Intrinsic::uadd_with_overflow:
6619   case Intrinsic::sadd_with_overflow:
6620     // X + undef -> { -1, false }
6621     // undef + x -> { -1, false }
6622     if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) {
6623       return ConstantStruct::get(
6624           cast<StructType>(ReturnType),
6625           {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
6626            Constant::getNullValue(ReturnType->getStructElementType(1))});
6627     }
6628     break;
6629   case Intrinsic::umul_with_overflow:
6630   case Intrinsic::smul_with_overflow:
6631     // 0 * X -> { 0, false }
6632     // X * 0 -> { 0, false }
6633     if (match(Op0, m_Zero()) || match(Op1, m_Zero()))
6634       return Constant::getNullValue(ReturnType);
6635     // undef * X -> { 0, false }
6636     // X * undef -> { 0, false }
6637     if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6638       return Constant::getNullValue(ReturnType);
6639     break;
6640   case Intrinsic::uadd_sat:
6641     // sat(MAX + X) -> MAX
6642     // sat(X + MAX) -> MAX
6643     if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes()))
6644       return Constant::getAllOnesValue(ReturnType);
6645     [[fallthrough]];
6646   case Intrinsic::sadd_sat:
6647     // sat(X + undef) -> -1
6648     // sat(undef + X) -> -1
6649     // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
6650     // For signed: Assume undef is ~X, in which case X + ~X = -1.
6651     if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6652       return Constant::getAllOnesValue(ReturnType);
6653 
6654     // X + 0 -> X
6655     if (match(Op1, m_Zero()))
6656       return Op0;
6657     // 0 + X -> X
6658     if (match(Op0, m_Zero()))
6659       return Op1;
6660     break;
6661   case Intrinsic::usub_sat:
6662     // sat(0 - X) -> 0, sat(X - MAX) -> 0
6663     if (match(Op0, m_Zero()) || match(Op1, m_AllOnes()))
6664       return Constant::getNullValue(ReturnType);
6665     [[fallthrough]];
6666   case Intrinsic::ssub_sat:
6667     // X - X -> 0, X - undef -> 0, undef - X -> 0
6668     if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6669       return Constant::getNullValue(ReturnType);
6670     // X - 0 -> X
6671     if (match(Op1, m_Zero()))
6672       return Op0;
6673     break;
6674   case Intrinsic::load_relative:
6675     if (auto *C0 = dyn_cast<Constant>(Op0))
6676       if (auto *C1 = dyn_cast<Constant>(Op1))
6677         return simplifyRelativeLoad(C0, C1, Q.DL);
6678     break;
6679   case Intrinsic::powi:
6680     if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
6681       // powi(x, 0) -> 1.0
6682       if (Power->isZero())
6683         return ConstantFP::get(Op0->getType(), 1.0);
6684       // powi(x, 1) -> x
6685       if (Power->isOne())
6686         return Op0;
6687     }
6688     break;
6689   case Intrinsic::ldexp:
6690     return simplifyLdexp(Op0, Op1, Q, false);
6691   case Intrinsic::copysign:
6692     // copysign X, X --> X
6693     if (Op0 == Op1)
6694       return Op0;
6695     // copysign -X, X --> X
6696     // copysign X, -X --> -X
6697     if (match(Op0, m_FNeg(m_Specific(Op1))) ||
6698         match(Op1, m_FNeg(m_Specific(Op0))))
6699       return Op1;
6700     break;
6701   case Intrinsic::is_fpclass: {
6702     if (isa<PoisonValue>(Op0))
6703       return PoisonValue::get(ReturnType);
6704 
6705     uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue();
6706     // If all tests are made, it doesn't matter what the value is.
6707     if ((Mask & fcAllFlags) == fcAllFlags)
6708       return ConstantInt::get(ReturnType, true);
6709     if ((Mask & fcAllFlags) == 0)
6710       return ConstantInt::get(ReturnType, false);
6711     if (Q.isUndefValue(Op0))
6712       return UndefValue::get(ReturnType);
6713     break;
6714   }
6715   case Intrinsic::maxnum:
6716   case Intrinsic::minnum:
6717   case Intrinsic::maximum:
6718   case Intrinsic::minimum: {
6719     // If the arguments are the same, this is a no-op.
6720     if (Op0 == Op1)
6721       return Op0;
6722 
6723     // Canonicalize constant operand as Op1.
6724     if (isa<Constant>(Op0))
6725       std::swap(Op0, Op1);
6726 
6727     // If an argument is undef, return the other argument.
6728     if (Q.isUndefValue(Op1))
6729       return Op0;
6730 
6731     bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
6732     bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
6733 
6734     // minnum(X, nan) -> X
6735     // maxnum(X, nan) -> X
6736     // minimum(X, nan) -> nan
6737     // maximum(X, nan) -> nan
6738     if (match(Op1, m_NaN()))
6739       return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0;
6740 
6741     // In the following folds, inf can be replaced with the largest finite
6742     // float, if the ninf flag is set.
6743     const APFloat *C;
6744     if (match(Op1, m_APFloat(C)) &&
6745         (C->isInfinity() || (Call && Call->hasNoInfs() && C->isLargest()))) {
6746       // minnum(X, -inf) -> -inf
6747       // maxnum(X, +inf) -> +inf
6748       // minimum(X, -inf) -> -inf if nnan
6749       // maximum(X, +inf) -> +inf if nnan
6750       if (C->isNegative() == IsMin &&
6751           (!PropagateNaN || (Call && Call->hasNoNaNs())))
6752         return ConstantFP::get(ReturnType, *C);
6753 
6754       // minnum(X, +inf) -> X if nnan
6755       // maxnum(X, -inf) -> X if nnan
6756       // minimum(X, +inf) -> X
6757       // maximum(X, -inf) -> X
6758       if (C->isNegative() != IsMin &&
6759           (PropagateNaN || (Call && Call->hasNoNaNs())))
6760         return Op0;
6761     }
6762 
6763     // Min/max of the same operation with common operand:
6764     // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
6765     if (Value *V = foldMinimumMaximumSharedOp(IID, Op0, Op1))
6766       return V;
6767     if (Value *V = foldMinimumMaximumSharedOp(IID, Op1, Op0))
6768       return V;
6769 
6770     break;
6771   }
6772   case Intrinsic::vector_extract: {
6773     // (extract_vector (insert_vector _, X, 0), 0) -> X
6774     unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
6775     Value *X = nullptr;
6776     if (match(Op0, m_Intrinsic<Intrinsic::vector_insert>(m_Value(), m_Value(X),
6777                                                          m_Zero())) &&
6778         IdxN == 0 && X->getType() == ReturnType)
6779       return X;
6780 
6781     break;
6782   }
6783   default:
6784     break;
6785   }
6786 
6787   return nullptr;
6788 }
6789 
6790 static Value *simplifyIntrinsic(CallBase *Call, Value *Callee,
6791                                 ArrayRef<Value *> Args,
6792                                 const SimplifyQuery &Q) {
6793   // Operand bundles should not be in Args.
6794   assert(Call->arg_size() == Args.size());
6795   unsigned NumOperands = Args.size();
6796   Function *F = cast<Function>(Callee);
6797   Intrinsic::ID IID = F->getIntrinsicID();
6798 
6799   // Most of the intrinsics with no operands have some kind of side effect.
6800   // Don't simplify.
6801   if (!NumOperands) {
6802     switch (IID) {
6803     case Intrinsic::vscale: {
6804       Type *RetTy = F->getReturnType();
6805       ConstantRange CR = getVScaleRange(Call->getFunction(), 64);
6806       if (const APInt *C = CR.getSingleElement())
6807         return ConstantInt::get(RetTy, C->getZExtValue());
6808       return nullptr;
6809     }
6810     default:
6811       return nullptr;
6812     }
6813   }
6814 
6815   if (NumOperands == 1)
6816     return simplifyUnaryIntrinsic(F, Args[0], Q, Call);
6817 
6818   if (NumOperands == 2)
6819     return simplifyBinaryIntrinsic(IID, F->getReturnType(), Args[0], Args[1], Q,
6820                                    Call);
6821 
6822   // Handle intrinsics with 3 or more arguments.
6823   switch (IID) {
6824   case Intrinsic::masked_load:
6825   case Intrinsic::masked_gather: {
6826     Value *MaskArg = Args[2];
6827     Value *PassthruArg = Args[3];
6828     // If the mask is all zeros or undef, the "passthru" argument is the result.
6829     if (maskIsAllZeroOrUndef(MaskArg))
6830       return PassthruArg;
6831     return nullptr;
6832   }
6833   case Intrinsic::fshl:
6834   case Intrinsic::fshr: {
6835     Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2];
6836 
6837     // If both operands are undef, the result is undef.
6838     if (Q.isUndefValue(Op0) && Q.isUndefValue(Op1))
6839       return UndefValue::get(F->getReturnType());
6840 
6841     // If shift amount is undef, assume it is zero.
6842     if (Q.isUndefValue(ShAmtArg))
6843       return Args[IID == Intrinsic::fshl ? 0 : 1];
6844 
6845     const APInt *ShAmtC;
6846     if (match(ShAmtArg, m_APInt(ShAmtC))) {
6847       // If there's effectively no shift, return the 1st arg or 2nd arg.
6848       APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
6849       if (ShAmtC->urem(BitWidth).isZero())
6850         return Args[IID == Intrinsic::fshl ? 0 : 1];
6851     }
6852 
6853     // Rotating zero by anything is zero.
6854     if (match(Op0, m_Zero()) && match(Op1, m_Zero()))
6855       return ConstantInt::getNullValue(F->getReturnType());
6856 
6857     // Rotating -1 by anything is -1.
6858     if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes()))
6859       return ConstantInt::getAllOnesValue(F->getReturnType());
6860 
6861     return nullptr;
6862   }
6863   case Intrinsic::experimental_constrained_fma: {
6864     auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6865     if (Value *V = simplifyFPOp(Args, {}, Q, *FPI->getExceptionBehavior(),
6866                                 *FPI->getRoundingMode()))
6867       return V;
6868     return nullptr;
6869   }
6870   case Intrinsic::fma:
6871   case Intrinsic::fmuladd: {
6872     if (Value *V = simplifyFPOp(Args, {}, Q, fp::ebIgnore,
6873                                 RoundingMode::NearestTiesToEven))
6874       return V;
6875     return nullptr;
6876   }
6877   case Intrinsic::smul_fix:
6878   case Intrinsic::smul_fix_sat: {
6879     Value *Op0 = Args[0];
6880     Value *Op1 = Args[1];
6881     Value *Op2 = Args[2];
6882     Type *ReturnType = F->getReturnType();
6883 
6884     // Canonicalize constant operand as Op1 (ConstantFolding handles the case
6885     // when both Op0 and Op1 are constant so we do not care about that special
6886     // case here).
6887     if (isa<Constant>(Op0))
6888       std::swap(Op0, Op1);
6889 
6890     // X * 0 -> 0
6891     if (match(Op1, m_Zero()))
6892       return Constant::getNullValue(ReturnType);
6893 
6894     // X * undef -> 0
6895     if (Q.isUndefValue(Op1))
6896       return Constant::getNullValue(ReturnType);
6897 
6898     // X * (1 << Scale) -> X
6899     APInt ScaledOne =
6900         APInt::getOneBitSet(ReturnType->getScalarSizeInBits(),
6901                             cast<ConstantInt>(Op2)->getZExtValue());
6902     if (ScaledOne.isNonNegative() && match(Op1, m_SpecificInt(ScaledOne)))
6903       return Op0;
6904 
6905     return nullptr;
6906   }
6907   case Intrinsic::vector_insert: {
6908     Value *Vec = Args[0];
6909     Value *SubVec = Args[1];
6910     Value *Idx = Args[2];
6911     Type *ReturnType = F->getReturnType();
6912 
6913     // (insert_vector Y, (extract_vector X, 0), 0) -> X
6914     // where: Y is X, or Y is undef
6915     unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
6916     Value *X = nullptr;
6917     if (match(SubVec,
6918               m_Intrinsic<Intrinsic::vector_extract>(m_Value(X), m_Zero())) &&
6919         (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 &&
6920         X->getType() == ReturnType)
6921       return X;
6922 
6923     return nullptr;
6924   }
6925   case Intrinsic::experimental_constrained_fadd: {
6926     auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6927     return simplifyFAddInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6928                             *FPI->getExceptionBehavior(),
6929                             *FPI->getRoundingMode());
6930   }
6931   case Intrinsic::experimental_constrained_fsub: {
6932     auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6933     return simplifyFSubInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6934                             *FPI->getExceptionBehavior(),
6935                             *FPI->getRoundingMode());
6936   }
6937   case Intrinsic::experimental_constrained_fmul: {
6938     auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6939     return simplifyFMulInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6940                             *FPI->getExceptionBehavior(),
6941                             *FPI->getRoundingMode());
6942   }
6943   case Intrinsic::experimental_constrained_fdiv: {
6944     auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6945     return simplifyFDivInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6946                             *FPI->getExceptionBehavior(),
6947                             *FPI->getRoundingMode());
6948   }
6949   case Intrinsic::experimental_constrained_frem: {
6950     auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6951     return simplifyFRemInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6952                             *FPI->getExceptionBehavior(),
6953                             *FPI->getRoundingMode());
6954   }
6955   case Intrinsic::experimental_constrained_ldexp:
6956     return simplifyLdexp(Args[0], Args[1], Q, true);
6957   case Intrinsic::experimental_gc_relocate: {
6958     GCRelocateInst &GCR = *cast<GCRelocateInst>(Call);
6959     Value *DerivedPtr = GCR.getDerivedPtr();
6960     Value *BasePtr = GCR.getBasePtr();
6961 
6962     // Undef is undef, even after relocation.
6963     if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
6964       return UndefValue::get(GCR.getType());
6965     }
6966 
6967     if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
6968       // For now, the assumption is that the relocation of null will be null
6969       // for most any collector. If this ever changes, a corresponding hook
6970       // should be added to GCStrategy and this code should check it first.
6971       if (isa<ConstantPointerNull>(DerivedPtr)) {
6972         // Use null-pointer of gc_relocate's type to replace it.
6973         return ConstantPointerNull::get(PT);
6974       }
6975     }
6976     return nullptr;
6977   }
6978   default:
6979     return nullptr;
6980   }
6981 }
6982 
6983 static Value *tryConstantFoldCall(CallBase *Call, Value *Callee,
6984                                   ArrayRef<Value *> Args,
6985                                   const SimplifyQuery &Q) {
6986   auto *F = dyn_cast<Function>(Callee);
6987   if (!F || !canConstantFoldCallTo(Call, F))
6988     return nullptr;
6989 
6990   SmallVector<Constant *, 4> ConstantArgs;
6991   ConstantArgs.reserve(Args.size());
6992   for (Value *Arg : Args) {
6993     Constant *C = dyn_cast<Constant>(Arg);
6994     if (!C) {
6995       if (isa<MetadataAsValue>(Arg))
6996         continue;
6997       return nullptr;
6998     }
6999     ConstantArgs.push_back(C);
7000   }
7001 
7002   return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI);
7003 }
7004 
7005 Value *llvm::simplifyCall(CallBase *Call, Value *Callee, ArrayRef<Value *> Args,
7006                           const SimplifyQuery &Q) {
7007   // Args should not contain operand bundle operands.
7008   assert(Call->arg_size() == Args.size());
7009 
7010   // musttail calls can only be simplified if they are also DCEd.
7011   // As we can't guarantee this here, don't simplify them.
7012   if (Call->isMustTailCall())
7013     return nullptr;
7014 
7015   // call undef -> poison
7016   // call null -> poison
7017   if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
7018     return PoisonValue::get(Call->getType());
7019 
7020   if (Value *V = tryConstantFoldCall(Call, Callee, Args, Q))
7021     return V;
7022 
7023   auto *F = dyn_cast<Function>(Callee);
7024   if (F && F->isIntrinsic())
7025     if (Value *Ret = simplifyIntrinsic(Call, Callee, Args, Q))
7026       return Ret;
7027 
7028   return nullptr;
7029 }
7030 
7031 Value *llvm::simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q) {
7032   assert(isa<ConstrainedFPIntrinsic>(Call));
7033   SmallVector<Value *, 4> Args(Call->args());
7034   if (Value *V = tryConstantFoldCall(Call, Call->getCalledOperand(), Args, Q))
7035     return V;
7036   if (Value *Ret = simplifyIntrinsic(Call, Call->getCalledOperand(), Args, Q))
7037     return Ret;
7038   return nullptr;
7039 }
7040 
7041 /// Given operands for a Freeze, see if we can fold the result.
7042 static Value *simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
7043   // Use a utility function defined in ValueTracking.
7044   if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT))
7045     return Op0;
7046   // We have room for improvement.
7047   return nullptr;
7048 }
7049 
7050 Value *llvm::simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
7051   return ::simplifyFreezeInst(Op0, Q);
7052 }
7053 
7054 Value *llvm::simplifyLoadInst(LoadInst *LI, Value *PtrOp,
7055                               const SimplifyQuery &Q) {
7056   if (LI->isVolatile())
7057     return nullptr;
7058 
7059   if (auto *PtrOpC = dyn_cast<Constant>(PtrOp))
7060     return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL);
7061 
7062   // We can only fold the load if it is from a constant global with definitive
7063   // initializer. Skip expensive logic if this is not the case.
7064   auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(PtrOp));
7065   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7066     return nullptr;
7067 
7068   // If GlobalVariable's initializer is uniform, then return the constant
7069   // regardless of its offset.
7070   if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(),
7071                                                      LI->getType(), Q.DL))
7072     return C;
7073 
7074   // Try to convert operand into a constant by stripping offsets while looking
7075   // through invariant.group intrinsics.
7076   APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
7077   PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
7078       Q.DL, Offset, /* AllowNonInbounts */ true,
7079       /* AllowInvariantGroup */ true);
7080   if (PtrOp == GV) {
7081     // Index size may have changed due to address space casts.
7082     Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()));
7083     return ConstantFoldLoadFromConstPtr(GV, LI->getType(), std::move(Offset),
7084                                         Q.DL);
7085   }
7086 
7087   return nullptr;
7088 }
7089 
7090 /// See if we can compute a simplified version of this instruction.
7091 /// If not, this returns null.
7092 
7093 static Value *simplifyInstructionWithOperands(Instruction *I,
7094                                               ArrayRef<Value *> NewOps,
7095                                               const SimplifyQuery &SQ,
7096                                               unsigned MaxRecurse) {
7097   assert(I->getFunction() && "instruction should be inserted in a function");
7098   assert((!SQ.CxtI || SQ.CxtI->getFunction() == I->getFunction()) &&
7099          "context instruction should be in the same function");
7100 
7101   const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I);
7102 
7103   switch (I->getOpcode()) {
7104   default:
7105     if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
7106       SmallVector<Constant *, 8> NewConstOps(NewOps.size());
7107       transform(NewOps, NewConstOps.begin(),
7108                 [](Value *V) { return cast<Constant>(V); });
7109       return ConstantFoldInstOperands(I, NewConstOps, Q.DL, Q.TLI);
7110     }
7111     return nullptr;
7112   case Instruction::FNeg:
7113     return simplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q, MaxRecurse);
7114   case Instruction::FAdd:
7115     return simplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7116                             MaxRecurse);
7117   case Instruction::Add:
7118     return simplifyAddInst(
7119         NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7120         Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7121   case Instruction::FSub:
7122     return simplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7123                             MaxRecurse);
7124   case Instruction::Sub:
7125     return simplifySubInst(
7126         NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7127         Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7128   case Instruction::FMul:
7129     return simplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7130                             MaxRecurse);
7131   case Instruction::Mul:
7132     return simplifyMulInst(
7133         NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7134         Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7135   case Instruction::SDiv:
7136     return simplifySDivInst(NewOps[0], NewOps[1],
7137                             Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7138                             MaxRecurse);
7139   case Instruction::UDiv:
7140     return simplifyUDivInst(NewOps[0], NewOps[1],
7141                             Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7142                             MaxRecurse);
7143   case Instruction::FDiv:
7144     return simplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7145                             MaxRecurse);
7146   case Instruction::SRem:
7147     return simplifySRemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7148   case Instruction::URem:
7149     return simplifyURemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7150   case Instruction::FRem:
7151     return simplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7152                             MaxRecurse);
7153   case Instruction::Shl:
7154     return simplifyShlInst(
7155         NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7156         Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7157   case Instruction::LShr:
7158     return simplifyLShrInst(NewOps[0], NewOps[1],
7159                             Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7160                             MaxRecurse);
7161   case Instruction::AShr:
7162     return simplifyAShrInst(NewOps[0], NewOps[1],
7163                             Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7164                             MaxRecurse);
7165   case Instruction::And:
7166     return simplifyAndInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7167   case Instruction::Or:
7168     return simplifyOrInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7169   case Instruction::Xor:
7170     return simplifyXorInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7171   case Instruction::ICmp:
7172     return simplifyICmpInst(cast<ICmpInst>(I)->getCmpPredicate(), NewOps[0],
7173                             NewOps[1], Q, MaxRecurse);
7174   case Instruction::FCmp:
7175     return simplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0],
7176                             NewOps[1], I->getFastMathFlags(), Q, MaxRecurse);
7177   case Instruction::Select:
7178     return simplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse);
7179   case Instruction::GetElementPtr: {
7180     auto *GEPI = cast<GetElementPtrInst>(I);
7181     return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
7182                            ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
7183                            MaxRecurse);
7184   }
7185   case Instruction::InsertValue: {
7186     InsertValueInst *IV = cast<InsertValueInst>(I);
7187     return simplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q,
7188                                    MaxRecurse);
7189   }
7190   case Instruction::InsertElement:
7191     return simplifyInsertElementInst(NewOps[0], NewOps[1], NewOps[2], Q);
7192   case Instruction::ExtractValue: {
7193     auto *EVI = cast<ExtractValueInst>(I);
7194     return simplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q,
7195                                     MaxRecurse);
7196   }
7197   case Instruction::ExtractElement:
7198     return simplifyExtractElementInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7199   case Instruction::ShuffleVector: {
7200     auto *SVI = cast<ShuffleVectorInst>(I);
7201     return simplifyShuffleVectorInst(NewOps[0], NewOps[1],
7202                                      SVI->getShuffleMask(), SVI->getType(), Q,
7203                                      MaxRecurse);
7204   }
7205   case Instruction::PHI:
7206     return simplifyPHINode(cast<PHINode>(I), NewOps, Q);
7207   case Instruction::Call:
7208     return simplifyCall(
7209         cast<CallInst>(I), NewOps.back(),
7210         NewOps.drop_back(1 + cast<CallInst>(I)->getNumTotalBundleOperands()), Q);
7211   case Instruction::Freeze:
7212     return llvm::simplifyFreezeInst(NewOps[0], Q);
7213 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
7214 #include "llvm/IR/Instruction.def"
7215 #undef HANDLE_CAST_INST
7216     return simplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q,
7217                             MaxRecurse);
7218   case Instruction::Alloca:
7219     // No simplifications for Alloca and it can't be constant folded.
7220     return nullptr;
7221   case Instruction::Load:
7222     return simplifyLoadInst(cast<LoadInst>(I), NewOps[0], Q);
7223   }
7224 }
7225 
7226 Value *llvm::simplifyInstructionWithOperands(Instruction *I,
7227                                              ArrayRef<Value *> NewOps,
7228                                              const SimplifyQuery &SQ) {
7229   assert(NewOps.size() == I->getNumOperands() &&
7230          "Number of operands should match the instruction!");
7231   return ::simplifyInstructionWithOperands(I, NewOps, SQ, RecursionLimit);
7232 }
7233 
7234 Value *llvm::simplifyInstruction(Instruction *I, const SimplifyQuery &SQ) {
7235   SmallVector<Value *, 8> Ops(I->operands());
7236   Value *Result = ::simplifyInstructionWithOperands(I, Ops, SQ, RecursionLimit);
7237 
7238   /// If called on unreachable code, the instruction may simplify to itself.
7239   /// Make life easier for users by detecting that case here, and returning a
7240   /// safe value instead.
7241   return Result == I ? PoisonValue::get(I->getType()) : Result;
7242 }
7243 
7244 /// Implementation of recursive simplification through an instruction's
7245 /// uses.
7246 ///
7247 /// This is the common implementation of the recursive simplification routines.
7248 /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
7249 /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
7250 /// instructions to process and attempt to simplify it using
7251 /// InstructionSimplify. Recursively visited users which could not be
7252 /// simplified themselves are to the optional UnsimplifiedUsers set for
7253 /// further processing by the caller.
7254 ///
7255 /// This routine returns 'true' only when *it* simplifies something. The passed
7256 /// in simplified value does not count toward this.
7257 static bool replaceAndRecursivelySimplifyImpl(
7258     Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7259     const DominatorTree *DT, AssumptionCache *AC,
7260     SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
7261   bool Simplified = false;
7262   SmallSetVector<Instruction *, 8> Worklist;
7263   const DataLayout &DL = I->getDataLayout();
7264 
7265   // If we have an explicit value to collapse to, do that round of the
7266   // simplification loop by hand initially.
7267   if (SimpleV) {
7268     for (User *U : I->users())
7269       if (U != I)
7270         Worklist.insert(cast<Instruction>(U));
7271 
7272     // Replace the instruction with its simplified value.
7273     I->replaceAllUsesWith(SimpleV);
7274 
7275     if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7276       I->eraseFromParent();
7277   } else {
7278     Worklist.insert(I);
7279   }
7280 
7281   // Note that we must test the size on each iteration, the worklist can grow.
7282   for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
7283     I = Worklist[Idx];
7284 
7285     // See if this instruction simplifies.
7286     SimpleV = simplifyInstruction(I, {DL, TLI, DT, AC});
7287     if (!SimpleV) {
7288       if (UnsimplifiedUsers)
7289         UnsimplifiedUsers->insert(I);
7290       continue;
7291     }
7292 
7293     Simplified = true;
7294 
7295     // Stash away all the uses of the old instruction so we can check them for
7296     // recursive simplifications after a RAUW. This is cheaper than checking all
7297     // uses of To on the recursive step in most cases.
7298     for (User *U : I->users())
7299       Worklist.insert(cast<Instruction>(U));
7300 
7301     // Replace the instruction with its simplified value.
7302     I->replaceAllUsesWith(SimpleV);
7303 
7304     if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7305       I->eraseFromParent();
7306   }
7307   return Simplified;
7308 }
7309 
7310 bool llvm::replaceAndRecursivelySimplify(
7311     Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7312     const DominatorTree *DT, AssumptionCache *AC,
7313     SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) {
7314   assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
7315   assert(SimpleV && "Must provide a simplified value.");
7316   return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC,
7317                                            UnsimplifiedUsers);
7318 }
7319 
7320 namespace llvm {
7321 const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) {
7322   auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>();
7323   auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
7324   auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
7325   auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
7326   auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
7327   auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
7328   return {F.getDataLayout(), TLI, DT, AC};
7329 }
7330 
7331 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR,
7332                                          const DataLayout &DL) {
7333   return {DL, &AR.TLI, &AR.DT, &AR.AC};
7334 }
7335 
7336 template <class T, class... TArgs>
7337 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM,
7338                                          Function &F) {
7339   auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
7340   auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
7341   auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
7342   return {F.getDataLayout(), TLI, DT, AC};
7343 }
7344 template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &,
7345                                                   Function &);
7346 
7347 bool SimplifyQuery::isUndefValue(Value *V) const {
7348   if (!CanUseUndef)
7349     return false;
7350 
7351   return match(V, m_Undef());
7352 }
7353 
7354 } // namespace llvm
7355 
7356 void InstSimplifyFolder::anchor() {}
7357