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