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