xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/PatternMatch.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
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 provides a simple and efficient mechanism for performing general
10 // tree-based pattern matches on the LLVM IR. The power of these routines is
11 // that it allows you to write concise patterns that are expressive and easy to
12 // understand. The other major advantage of this is that it allows you to
13 // trivially capture/bind elements in the pattern to variables. For example,
14 // you can do something like this:
15 //
16 //  Value *Exp = ...
17 //  Value *X, *Y;  ConstantInt *C1, *C2;      // (X & C1) | (Y & C2)
18 //  if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19 //                      m_And(m_Value(Y), m_ConstantInt(C2))))) {
20 //    ... Pattern is matched and variables are bound ...
21 //  }
22 //
23 // This is primarily useful to things like the instruction combiner, but can
24 // also be useful for static analysis tools or code generators.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #ifndef LLVM_IR_PATTERNMATCH_H
29 #define LLVM_IR_PATTERNMATCH_H
30 
31 #include "llvm/ADT/APFloat.h"
32 #include "llvm/ADT/APInt.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/InstrTypes.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/IntrinsicInst.h"
40 #include "llvm/IR/Intrinsics.h"
41 #include "llvm/IR/Operator.h"
42 #include "llvm/IR/Value.h"
43 #include "llvm/Support/Casting.h"
44 #include <cstdint>
45 
46 namespace llvm {
47 namespace PatternMatch {
48 
49 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50   return const_cast<Pattern &>(P).match(V);
51 }
52 
53 template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
54   return const_cast<Pattern &>(P).match(Mask);
55 }
56 
57 template <typename SubPattern_t> struct OneUse_match {
58   SubPattern_t SubPattern;
59 
60   OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
61 
62   template <typename OpTy> bool match(OpTy *V) {
63     return V->hasOneUse() && SubPattern.match(V);
64   }
65 };
66 
67 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
68   return SubPattern;
69 }
70 
71 template <typename Class> struct class_match {
72   template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
73 };
74 
75 /// Match an arbitrary value and ignore it.
76 inline class_match<Value> m_Value() { return class_match<Value>(); }
77 
78 /// Match an arbitrary unary operation and ignore it.
79 inline class_match<UnaryOperator> m_UnOp() {
80   return class_match<UnaryOperator>();
81 }
82 
83 /// Match an arbitrary binary operation and ignore it.
84 inline class_match<BinaryOperator> m_BinOp() {
85   return class_match<BinaryOperator>();
86 }
87 
88 /// Matches any compare instruction and ignore it.
89 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
90 
91 struct undef_match {
92   static bool check(const Value *V) {
93     if (isa<UndefValue>(V))
94       return true;
95 
96     const auto *CA = dyn_cast<ConstantAggregate>(V);
97     if (!CA)
98       return false;
99 
100     SmallPtrSet<const ConstantAggregate *, 8> Seen;
101     SmallVector<const ConstantAggregate *, 8> Worklist;
102 
103     // Either UndefValue, PoisonValue, or an aggregate that only contains
104     // these is accepted by matcher.
105     // CheckValue returns false if CA cannot satisfy this constraint.
106     auto CheckValue = [&](const ConstantAggregate *CA) {
107       for (const Value *Op : CA->operand_values()) {
108         if (isa<UndefValue>(Op))
109           continue;
110 
111         const auto *CA = dyn_cast<ConstantAggregate>(Op);
112         if (!CA)
113           return false;
114         if (Seen.insert(CA).second)
115           Worklist.emplace_back(CA);
116       }
117 
118       return true;
119     };
120 
121     if (!CheckValue(CA))
122       return false;
123 
124     while (!Worklist.empty()) {
125       if (!CheckValue(Worklist.pop_back_val()))
126         return false;
127     }
128     return true;
129   }
130   template <typename ITy> bool match(ITy *V) { return check(V); }
131 };
132 
133 /// Match an arbitrary undef constant. This matches poison as well.
134 /// If this is an aggregate and contains a non-aggregate element that is
135 /// neither undef nor poison, the aggregate is not matched.
136 inline auto m_Undef() { return undef_match(); }
137 
138 /// Match an arbitrary poison constant.
139 inline class_match<PoisonValue> m_Poison() {
140   return class_match<PoisonValue>();
141 }
142 
143 /// Match an arbitrary Constant and ignore it.
144 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
145 
146 /// Match an arbitrary ConstantInt and ignore it.
147 inline class_match<ConstantInt> m_ConstantInt() {
148   return class_match<ConstantInt>();
149 }
150 
151 /// Match an arbitrary ConstantFP and ignore it.
152 inline class_match<ConstantFP> m_ConstantFP() {
153   return class_match<ConstantFP>();
154 }
155 
156 struct constantexpr_match {
157   template <typename ITy> bool match(ITy *V) {
158     auto *C = dyn_cast<Constant>(V);
159     return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
160   }
161 };
162 
163 /// Match a constant expression or a constant that contains a constant
164 /// expression.
165 inline constantexpr_match m_ConstantExpr() { return constantexpr_match(); }
166 
167 /// Match an arbitrary basic block value and ignore it.
168 inline class_match<BasicBlock> m_BasicBlock() {
169   return class_match<BasicBlock>();
170 }
171 
172 /// Inverting matcher
173 template <typename Ty> struct match_unless {
174   Ty M;
175 
176   match_unless(const Ty &Matcher) : M(Matcher) {}
177 
178   template <typename ITy> bool match(ITy *V) { return !M.match(V); }
179 };
180 
181 /// Match if the inner matcher does *NOT* match.
182 template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
183   return match_unless<Ty>(M);
184 }
185 
186 /// Matching combinators
187 template <typename LTy, typename RTy> struct match_combine_or {
188   LTy L;
189   RTy R;
190 
191   match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
192 
193   template <typename ITy> bool match(ITy *V) {
194     if (L.match(V))
195       return true;
196     if (R.match(V))
197       return true;
198     return false;
199   }
200 };
201 
202 template <typename LTy, typename RTy> struct match_combine_and {
203   LTy L;
204   RTy R;
205 
206   match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
207 
208   template <typename ITy> bool match(ITy *V) {
209     if (L.match(V))
210       if (R.match(V))
211         return true;
212     return false;
213   }
214 };
215 
216 /// Combine two pattern matchers matching L || R
217 template <typename LTy, typename RTy>
218 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
219   return match_combine_or<LTy, RTy>(L, R);
220 }
221 
222 /// Combine two pattern matchers matching L && R
223 template <typename LTy, typename RTy>
224 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
225   return match_combine_and<LTy, RTy>(L, R);
226 }
227 
228 struct apint_match {
229   const APInt *&Res;
230   bool AllowUndef;
231 
232   apint_match(const APInt *&Res, bool AllowUndef)
233       : Res(Res), AllowUndef(AllowUndef) {}
234 
235   template <typename ITy> bool match(ITy *V) {
236     if (auto *CI = dyn_cast<ConstantInt>(V)) {
237       Res = &CI->getValue();
238       return true;
239     }
240     if (V->getType()->isVectorTy())
241       if (const auto *C = dyn_cast<Constant>(V))
242         if (auto *CI =
243                 dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndef))) {
244           Res = &CI->getValue();
245           return true;
246         }
247     return false;
248   }
249 };
250 // Either constexpr if or renaming ConstantFP::getValueAPF to
251 // ConstantFP::getValue is needed to do it via single template
252 // function for both apint/apfloat.
253 struct apfloat_match {
254   const APFloat *&Res;
255   bool AllowUndef;
256 
257   apfloat_match(const APFloat *&Res, bool AllowUndef)
258       : Res(Res), AllowUndef(AllowUndef) {}
259 
260   template <typename ITy> bool match(ITy *V) {
261     if (auto *CI = dyn_cast<ConstantFP>(V)) {
262       Res = &CI->getValueAPF();
263       return true;
264     }
265     if (V->getType()->isVectorTy())
266       if (const auto *C = dyn_cast<Constant>(V))
267         if (auto *CI =
268                 dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowUndef))) {
269           Res = &CI->getValueAPF();
270           return true;
271         }
272     return false;
273   }
274 };
275 
276 /// Match a ConstantInt or splatted ConstantVector, binding the
277 /// specified pointer to the contained APInt.
278 inline apint_match m_APInt(const APInt *&Res) {
279   // Forbid undefs by default to maintain previous behavior.
280   return apint_match(Res, /* AllowUndef */ false);
281 }
282 
283 /// Match APInt while allowing undefs in splat vector constants.
284 inline apint_match m_APIntAllowUndef(const APInt *&Res) {
285   return apint_match(Res, /* AllowUndef */ true);
286 }
287 
288 /// Match APInt while forbidding undefs in splat vector constants.
289 inline apint_match m_APIntForbidUndef(const APInt *&Res) {
290   return apint_match(Res, /* AllowUndef */ false);
291 }
292 
293 /// Match a ConstantFP or splatted ConstantVector, binding the
294 /// specified pointer to the contained APFloat.
295 inline apfloat_match m_APFloat(const APFloat *&Res) {
296   // Forbid undefs by default to maintain previous behavior.
297   return apfloat_match(Res, /* AllowUndef */ false);
298 }
299 
300 /// Match APFloat while allowing undefs in splat vector constants.
301 inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) {
302   return apfloat_match(Res, /* AllowUndef */ true);
303 }
304 
305 /// Match APFloat while forbidding undefs in splat vector constants.
306 inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) {
307   return apfloat_match(Res, /* AllowUndef */ false);
308 }
309 
310 template <int64_t Val> struct constantint_match {
311   template <typename ITy> bool match(ITy *V) {
312     if (const auto *CI = dyn_cast<ConstantInt>(V)) {
313       const APInt &CIV = CI->getValue();
314       if (Val >= 0)
315         return CIV == static_cast<uint64_t>(Val);
316       // If Val is negative, and CI is shorter than it, truncate to the right
317       // number of bits.  If it is larger, then we have to sign extend.  Just
318       // compare their negated values.
319       return -CIV == -Val;
320     }
321     return false;
322   }
323 };
324 
325 /// Match a ConstantInt with a specific value.
326 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
327   return constantint_match<Val>();
328 }
329 
330 /// This helper class is used to match constant scalars, vector splats,
331 /// and fixed width vectors that satisfy a specified predicate.
332 /// For fixed width vector constants, undefined elements are ignored.
333 template <typename Predicate, typename ConstantVal>
334 struct cstval_pred_ty : public Predicate {
335   template <typename ITy> bool match(ITy *V) {
336     if (const auto *CV = dyn_cast<ConstantVal>(V))
337       return this->isValue(CV->getValue());
338     if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
339       if (const auto *C = dyn_cast<Constant>(V)) {
340         if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
341           return this->isValue(CV->getValue());
342 
343         // Number of elements of a scalable vector unknown at compile time
344         auto *FVTy = dyn_cast<FixedVectorType>(VTy);
345         if (!FVTy)
346           return false;
347 
348         // Non-splat vector constant: check each element for a match.
349         unsigned NumElts = FVTy->getNumElements();
350         assert(NumElts != 0 && "Constant vector with no elements?");
351         bool HasNonUndefElements = false;
352         for (unsigned i = 0; i != NumElts; ++i) {
353           Constant *Elt = C->getAggregateElement(i);
354           if (!Elt)
355             return false;
356           if (isa<UndefValue>(Elt))
357             continue;
358           auto *CV = dyn_cast<ConstantVal>(Elt);
359           if (!CV || !this->isValue(CV->getValue()))
360             return false;
361           HasNonUndefElements = true;
362         }
363         return HasNonUndefElements;
364       }
365     }
366     return false;
367   }
368 };
369 
370 /// specialization of cstval_pred_ty for ConstantInt
371 template <typename Predicate>
372 using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>;
373 
374 /// specialization of cstval_pred_ty for ConstantFP
375 template <typename Predicate>
376 using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>;
377 
378 /// This helper class is used to match scalar and vector constants that
379 /// satisfy a specified predicate, and bind them to an APInt.
380 template <typename Predicate> struct api_pred_ty : public Predicate {
381   const APInt *&Res;
382 
383   api_pred_ty(const APInt *&R) : Res(R) {}
384 
385   template <typename ITy> bool match(ITy *V) {
386     if (const auto *CI = dyn_cast<ConstantInt>(V))
387       if (this->isValue(CI->getValue())) {
388         Res = &CI->getValue();
389         return true;
390       }
391     if (V->getType()->isVectorTy())
392       if (const auto *C = dyn_cast<Constant>(V))
393         if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
394           if (this->isValue(CI->getValue())) {
395             Res = &CI->getValue();
396             return true;
397           }
398 
399     return false;
400   }
401 };
402 
403 /// This helper class is used to match scalar and vector constants that
404 /// satisfy a specified predicate, and bind them to an APFloat.
405 /// Undefs are allowed in splat vector constants.
406 template <typename Predicate> struct apf_pred_ty : public Predicate {
407   const APFloat *&Res;
408 
409   apf_pred_ty(const APFloat *&R) : Res(R) {}
410 
411   template <typename ITy> bool match(ITy *V) {
412     if (const auto *CI = dyn_cast<ConstantFP>(V))
413       if (this->isValue(CI->getValue())) {
414         Res = &CI->getValue();
415         return true;
416       }
417     if (V->getType()->isVectorTy())
418       if (const auto *C = dyn_cast<Constant>(V))
419         if (auto *CI = dyn_cast_or_null<ConstantFP>(
420                 C->getSplatValue(/* AllowUndef */ true)))
421           if (this->isValue(CI->getValue())) {
422             Res = &CI->getValue();
423             return true;
424           }
425 
426     return false;
427   }
428 };
429 
430 ///////////////////////////////////////////////////////////////////////////////
431 //
432 // Encapsulate constant value queries for use in templated predicate matchers.
433 // This allows checking if constants match using compound predicates and works
434 // with vector constants, possibly with relaxed constraints. For example, ignore
435 // undef values.
436 //
437 ///////////////////////////////////////////////////////////////////////////////
438 
439 struct is_any_apint {
440   bool isValue(const APInt &C) { return true; }
441 };
442 /// Match an integer or vector with any integral constant.
443 /// For vectors, this includes constants with undefined elements.
444 inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
445   return cst_pred_ty<is_any_apint>();
446 }
447 
448 struct is_shifted_mask {
449   bool isValue(const APInt &C) { return C.isShiftedMask(); }
450 };
451 
452 inline cst_pred_ty<is_shifted_mask> m_ShiftedMask() {
453   return cst_pred_ty<is_shifted_mask>();
454 }
455 
456 struct is_all_ones {
457   bool isValue(const APInt &C) { return C.isAllOnes(); }
458 };
459 /// Match an integer or vector with all bits set.
460 /// For vectors, this includes constants with undefined elements.
461 inline cst_pred_ty<is_all_ones> m_AllOnes() {
462   return cst_pred_ty<is_all_ones>();
463 }
464 
465 struct is_maxsignedvalue {
466   bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
467 };
468 /// Match an integer or vector with values having all bits except for the high
469 /// bit set (0x7f...).
470 /// For vectors, this includes constants with undefined elements.
471 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
472   return cst_pred_ty<is_maxsignedvalue>();
473 }
474 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
475   return V;
476 }
477 
478 struct is_negative {
479   bool isValue(const APInt &C) { return C.isNegative(); }
480 };
481 /// Match an integer or vector of negative values.
482 /// For vectors, this includes constants with undefined elements.
483 inline cst_pred_ty<is_negative> m_Negative() {
484   return cst_pred_ty<is_negative>();
485 }
486 inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
487 
488 struct is_nonnegative {
489   bool isValue(const APInt &C) { return C.isNonNegative(); }
490 };
491 /// Match an integer or vector of non-negative values.
492 /// For vectors, this includes constants with undefined elements.
493 inline cst_pred_ty<is_nonnegative> m_NonNegative() {
494   return cst_pred_ty<is_nonnegative>();
495 }
496 inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
497 
498 struct is_strictlypositive {
499   bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
500 };
501 /// Match an integer or vector of strictly positive values.
502 /// For vectors, this includes constants with undefined elements.
503 inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
504   return cst_pred_ty<is_strictlypositive>();
505 }
506 inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
507   return V;
508 }
509 
510 struct is_nonpositive {
511   bool isValue(const APInt &C) { return C.isNonPositive(); }
512 };
513 /// Match an integer or vector of non-positive values.
514 /// For vectors, this includes constants with undefined elements.
515 inline cst_pred_ty<is_nonpositive> m_NonPositive() {
516   return cst_pred_ty<is_nonpositive>();
517 }
518 inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
519 
520 struct is_one {
521   bool isValue(const APInt &C) { return C.isOne(); }
522 };
523 /// Match an integer 1 or a vector with all elements equal to 1.
524 /// For vectors, this includes constants with undefined elements.
525 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
526 
527 struct is_zero_int {
528   bool isValue(const APInt &C) { return C.isZero(); }
529 };
530 /// Match an integer 0 or a vector with all elements equal to 0.
531 /// For vectors, this includes constants with undefined elements.
532 inline cst_pred_ty<is_zero_int> m_ZeroInt() {
533   return cst_pred_ty<is_zero_int>();
534 }
535 
536 struct is_zero {
537   template <typename ITy> bool match(ITy *V) {
538     auto *C = dyn_cast<Constant>(V);
539     // FIXME: this should be able to do something for scalable vectors
540     return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
541   }
542 };
543 /// Match any null constant or a vector with all elements equal to 0.
544 /// For vectors, this includes constants with undefined elements.
545 inline is_zero m_Zero() { return is_zero(); }
546 
547 struct is_power2 {
548   bool isValue(const APInt &C) { return C.isPowerOf2(); }
549 };
550 /// Match an integer or vector power-of-2.
551 /// For vectors, this includes constants with undefined elements.
552 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
553 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
554 
555 struct is_negated_power2 {
556   bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); }
557 };
558 /// Match a integer or vector negated power-of-2.
559 /// For vectors, this includes constants with undefined elements.
560 inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
561   return cst_pred_ty<is_negated_power2>();
562 }
563 inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
564   return V;
565 }
566 
567 struct is_power2_or_zero {
568   bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
569 };
570 /// Match an integer or vector of 0 or power-of-2 values.
571 /// For vectors, this includes constants with undefined elements.
572 inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
573   return cst_pred_ty<is_power2_or_zero>();
574 }
575 inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
576   return V;
577 }
578 
579 struct is_sign_mask {
580   bool isValue(const APInt &C) { return C.isSignMask(); }
581 };
582 /// Match an integer or vector with only the sign bit(s) set.
583 /// For vectors, this includes constants with undefined elements.
584 inline cst_pred_ty<is_sign_mask> m_SignMask() {
585   return cst_pred_ty<is_sign_mask>();
586 }
587 
588 struct is_lowbit_mask {
589   bool isValue(const APInt &C) { return C.isMask(); }
590 };
591 /// Match an integer or vector with only the low bit(s) set.
592 /// For vectors, this includes constants with undefined elements.
593 inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
594   return cst_pred_ty<is_lowbit_mask>();
595 }
596 inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
597 
598 struct icmp_pred_with_threshold {
599   ICmpInst::Predicate Pred;
600   const APInt *Thr;
601   bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); }
602 };
603 /// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
604 /// to Threshold. For vectors, this includes constants with undefined elements.
605 inline cst_pred_ty<icmp_pred_with_threshold>
606 m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
607   cst_pred_ty<icmp_pred_with_threshold> P;
608   P.Pred = Predicate;
609   P.Thr = &Threshold;
610   return P;
611 }
612 
613 struct is_nan {
614   bool isValue(const APFloat &C) { return C.isNaN(); }
615 };
616 /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
617 /// For vectors, this includes constants with undefined elements.
618 inline cstfp_pred_ty<is_nan> m_NaN() { return cstfp_pred_ty<is_nan>(); }
619 
620 struct is_nonnan {
621   bool isValue(const APFloat &C) { return !C.isNaN(); }
622 };
623 /// Match a non-NaN FP constant.
624 /// For vectors, this includes constants with undefined elements.
625 inline cstfp_pred_ty<is_nonnan> m_NonNaN() {
626   return cstfp_pred_ty<is_nonnan>();
627 }
628 
629 struct is_inf {
630   bool isValue(const APFloat &C) { return C.isInfinity(); }
631 };
632 /// Match a positive or negative infinity FP constant.
633 /// For vectors, this includes constants with undefined elements.
634 inline cstfp_pred_ty<is_inf> m_Inf() { return cstfp_pred_ty<is_inf>(); }
635 
636 struct is_noninf {
637   bool isValue(const APFloat &C) { return !C.isInfinity(); }
638 };
639 /// Match a non-infinity FP constant, i.e. finite or NaN.
640 /// For vectors, this includes constants with undefined elements.
641 inline cstfp_pred_ty<is_noninf> m_NonInf() {
642   return cstfp_pred_ty<is_noninf>();
643 }
644 
645 struct is_finite {
646   bool isValue(const APFloat &C) { return C.isFinite(); }
647 };
648 /// Match a finite FP constant, i.e. not infinity or NaN.
649 /// For vectors, this includes constants with undefined elements.
650 inline cstfp_pred_ty<is_finite> m_Finite() {
651   return cstfp_pred_ty<is_finite>();
652 }
653 inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
654 
655 struct is_finitenonzero {
656   bool isValue(const APFloat &C) { return C.isFiniteNonZero(); }
657 };
658 /// Match a finite non-zero FP constant.
659 /// For vectors, this includes constants with undefined elements.
660 inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() {
661   return cstfp_pred_ty<is_finitenonzero>();
662 }
663 inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) {
664   return V;
665 }
666 
667 struct is_any_zero_fp {
668   bool isValue(const APFloat &C) { return C.isZero(); }
669 };
670 /// Match a floating-point negative zero or positive zero.
671 /// For vectors, this includes constants with undefined elements.
672 inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
673   return cstfp_pred_ty<is_any_zero_fp>();
674 }
675 
676 struct is_pos_zero_fp {
677   bool isValue(const APFloat &C) { return C.isPosZero(); }
678 };
679 /// Match a floating-point positive zero.
680 /// For vectors, this includes constants with undefined elements.
681 inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
682   return cstfp_pred_ty<is_pos_zero_fp>();
683 }
684 
685 struct is_neg_zero_fp {
686   bool isValue(const APFloat &C) { return C.isNegZero(); }
687 };
688 /// Match a floating-point negative zero.
689 /// For vectors, this includes constants with undefined elements.
690 inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
691   return cstfp_pred_ty<is_neg_zero_fp>();
692 }
693 
694 struct is_non_zero_fp {
695   bool isValue(const APFloat &C) { return C.isNonZero(); }
696 };
697 /// Match a floating-point non-zero.
698 /// For vectors, this includes constants with undefined elements.
699 inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() {
700   return cstfp_pred_ty<is_non_zero_fp>();
701 }
702 
703 ///////////////////////////////////////////////////////////////////////////////
704 
705 template <typename Class> struct bind_ty {
706   Class *&VR;
707 
708   bind_ty(Class *&V) : VR(V) {}
709 
710   template <typename ITy> bool match(ITy *V) {
711     if (auto *CV = dyn_cast<Class>(V)) {
712       VR = CV;
713       return true;
714     }
715     return false;
716   }
717 };
718 
719 /// Match a value, capturing it if we match.
720 inline bind_ty<Value> m_Value(Value *&V) { return V; }
721 inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
722 
723 /// Match an instruction, capturing it if we match.
724 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
725 /// Match a unary operator, capturing it if we match.
726 inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; }
727 /// Match a binary operator, capturing it if we match.
728 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
729 /// Match a with overflow intrinsic, capturing it if we match.
730 inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) {
731   return I;
732 }
733 inline bind_ty<const WithOverflowInst>
734 m_WithOverflowInst(const WithOverflowInst *&I) {
735   return I;
736 }
737 
738 /// Match a Constant, capturing the value if we match.
739 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
740 
741 /// Match a ConstantInt, capturing the value if we match.
742 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
743 
744 /// Match a ConstantFP, capturing the value if we match.
745 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
746 
747 /// Match a ConstantExpr, capturing the value if we match.
748 inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; }
749 
750 /// Match a basic block value, capturing it if we match.
751 inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; }
752 inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
753   return V;
754 }
755 
756 /// Match an arbitrary immediate Constant and ignore it.
757 inline match_combine_and<class_match<Constant>,
758                          match_unless<constantexpr_match>>
759 m_ImmConstant() {
760   return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr()));
761 }
762 
763 /// Match an immediate Constant, capturing the value if we match.
764 inline match_combine_and<bind_ty<Constant>,
765                          match_unless<constantexpr_match>>
766 m_ImmConstant(Constant *&C) {
767   return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr()));
768 }
769 
770 /// Match a specified Value*.
771 struct specificval_ty {
772   const Value *Val;
773 
774   specificval_ty(const Value *V) : Val(V) {}
775 
776   template <typename ITy> bool match(ITy *V) { return V == Val; }
777 };
778 
779 /// Match if we have a specific specified value.
780 inline specificval_ty m_Specific(const Value *V) { return V; }
781 
782 /// Stores a reference to the Value *, not the Value * itself,
783 /// thus can be used in commutative matchers.
784 template <typename Class> struct deferredval_ty {
785   Class *const &Val;
786 
787   deferredval_ty(Class *const &V) : Val(V) {}
788 
789   template <typename ITy> bool match(ITy *const V) { return V == Val; }
790 };
791 
792 /// Like m_Specific(), but works if the specific value to match is determined
793 /// as part of the same match() expression. For example:
794 /// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
795 /// bind X before the pattern match starts.
796 /// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
797 /// whichever value m_Value(X) populated.
798 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
799 inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
800   return V;
801 }
802 
803 /// Match a specified floating point value or vector of all elements of
804 /// that value.
805 struct specific_fpval {
806   double Val;
807 
808   specific_fpval(double V) : Val(V) {}
809 
810   template <typename ITy> bool match(ITy *V) {
811     if (const auto *CFP = dyn_cast<ConstantFP>(V))
812       return CFP->isExactlyValue(Val);
813     if (V->getType()->isVectorTy())
814       if (const auto *C = dyn_cast<Constant>(V))
815         if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
816           return CFP->isExactlyValue(Val);
817     return false;
818   }
819 };
820 
821 /// Match a specific floating point value or vector with all elements
822 /// equal to the value.
823 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
824 
825 /// Match a float 1.0 or vector with all elements equal to 1.0.
826 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
827 
828 struct bind_const_intval_ty {
829   uint64_t &VR;
830 
831   bind_const_intval_ty(uint64_t &V) : VR(V) {}
832 
833   template <typename ITy> bool match(ITy *V) {
834     if (const auto *CV = dyn_cast<ConstantInt>(V))
835       if (CV->getValue().ule(UINT64_MAX)) {
836         VR = CV->getZExtValue();
837         return true;
838       }
839     return false;
840   }
841 };
842 
843 /// Match a specified integer value or vector of all elements of that
844 /// value.
845 template <bool AllowUndefs> struct specific_intval {
846   APInt Val;
847 
848   specific_intval(APInt V) : Val(std::move(V)) {}
849 
850   template <typename ITy> bool match(ITy *V) {
851     const auto *CI = dyn_cast<ConstantInt>(V);
852     if (!CI && V->getType()->isVectorTy())
853       if (const auto *C = dyn_cast<Constant>(V))
854         CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs));
855 
856     return CI && APInt::isSameValue(CI->getValue(), Val);
857   }
858 };
859 
860 /// Match a specific integer value or vector with all elements equal to
861 /// the value.
862 inline specific_intval<false> m_SpecificInt(APInt V) {
863   return specific_intval<false>(std::move(V));
864 }
865 
866 inline specific_intval<false> m_SpecificInt(uint64_t V) {
867   return m_SpecificInt(APInt(64, V));
868 }
869 
870 inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) {
871   return specific_intval<true>(std::move(V));
872 }
873 
874 inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) {
875   return m_SpecificIntAllowUndef(APInt(64, V));
876 }
877 
878 /// Match a ConstantInt and bind to its value.  This does not match
879 /// ConstantInts wider than 64-bits.
880 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
881 
882 /// Match a specified basic block value.
883 struct specific_bbval {
884   BasicBlock *Val;
885 
886   specific_bbval(BasicBlock *Val) : Val(Val) {}
887 
888   template <typename ITy> bool match(ITy *V) {
889     const auto *BB = dyn_cast<BasicBlock>(V);
890     return BB && BB == Val;
891   }
892 };
893 
894 /// Match a specific basic block value.
895 inline specific_bbval m_SpecificBB(BasicBlock *BB) {
896   return specific_bbval(BB);
897 }
898 
899 /// A commutative-friendly version of m_Specific().
900 inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) {
901   return BB;
902 }
903 inline deferredval_ty<const BasicBlock>
904 m_Deferred(const BasicBlock *const &BB) {
905   return BB;
906 }
907 
908 //===----------------------------------------------------------------------===//
909 // Matcher for any binary operator.
910 //
911 template <typename LHS_t, typename RHS_t, bool Commutable = false>
912 struct AnyBinaryOp_match {
913   LHS_t L;
914   RHS_t R;
915 
916   // The evaluation order is always stable, regardless of Commutability.
917   // The LHS is always matched first.
918   AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
919 
920   template <typename OpTy> bool match(OpTy *V) {
921     if (auto *I = dyn_cast<BinaryOperator>(V))
922       return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
923              (Commutable && L.match(I->getOperand(1)) &&
924               R.match(I->getOperand(0)));
925     return false;
926   }
927 };
928 
929 template <typename LHS, typename RHS>
930 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
931   return AnyBinaryOp_match<LHS, RHS>(L, R);
932 }
933 
934 //===----------------------------------------------------------------------===//
935 // Matcher for any unary operator.
936 // TODO fuse unary, binary matcher into n-ary matcher
937 //
938 template <typename OP_t> struct AnyUnaryOp_match {
939   OP_t X;
940 
941   AnyUnaryOp_match(const OP_t &X) : X(X) {}
942 
943   template <typename OpTy> bool match(OpTy *V) {
944     if (auto *I = dyn_cast<UnaryOperator>(V))
945       return X.match(I->getOperand(0));
946     return false;
947   }
948 };
949 
950 template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
951   return AnyUnaryOp_match<OP_t>(X);
952 }
953 
954 //===----------------------------------------------------------------------===//
955 // Matchers for specific binary operators.
956 //
957 
958 template <typename LHS_t, typename RHS_t, unsigned Opcode,
959           bool Commutable = false>
960 struct BinaryOp_match {
961   LHS_t L;
962   RHS_t R;
963 
964   // The evaluation order is always stable, regardless of Commutability.
965   // The LHS is always matched first.
966   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
967 
968   template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) {
969     if (V->getValueID() == Value::InstructionVal + Opc) {
970       auto *I = cast<BinaryOperator>(V);
971       return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
972              (Commutable && L.match(I->getOperand(1)) &&
973               R.match(I->getOperand(0)));
974     }
975     return false;
976   }
977 
978   template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); }
979 };
980 
981 template <typename LHS, typename RHS>
982 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
983                                                         const RHS &R) {
984   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
985 }
986 
987 template <typename LHS, typename RHS>
988 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
989                                                           const RHS &R) {
990   return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
991 }
992 
993 template <typename LHS, typename RHS>
994 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
995                                                         const RHS &R) {
996   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
997 }
998 
999 template <typename LHS, typename RHS>
1000 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
1001                                                           const RHS &R) {
1002   return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
1003 }
1004 
1005 template <typename Op_t> struct FNeg_match {
1006   Op_t X;
1007 
1008   FNeg_match(const Op_t &Op) : X(Op) {}
1009   template <typename OpTy> bool match(OpTy *V) {
1010     auto *FPMO = dyn_cast<FPMathOperator>(V);
1011     if (!FPMO)
1012       return false;
1013 
1014     if (FPMO->getOpcode() == Instruction::FNeg)
1015       return X.match(FPMO->getOperand(0));
1016 
1017     if (FPMO->getOpcode() == Instruction::FSub) {
1018       if (FPMO->hasNoSignedZeros()) {
1019         // With 'nsz', any zero goes.
1020         if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1021           return false;
1022       } else {
1023         // Without 'nsz', we need fsub -0.0, X exactly.
1024         if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1025           return false;
1026       }
1027 
1028       return X.match(FPMO->getOperand(1));
1029     }
1030 
1031     return false;
1032   }
1033 };
1034 
1035 /// Match 'fneg X' as 'fsub -0.0, X'.
1036 template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1037   return FNeg_match<OpTy>(X);
1038 }
1039 
1040 /// Match 'fneg X' as 'fsub +-0.0, X'.
1041 template <typename RHS>
1042 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1043 m_FNegNSZ(const RHS &X) {
1044   return m_FSub(m_AnyZeroFP(), X);
1045 }
1046 
1047 template <typename LHS, typename RHS>
1048 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
1049                                                         const RHS &R) {
1050   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
1051 }
1052 
1053 template <typename LHS, typename RHS>
1054 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
1055                                                           const RHS &R) {
1056   return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
1057 }
1058 
1059 template <typename LHS, typename RHS>
1060 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
1061                                                           const RHS &R) {
1062   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
1063 }
1064 
1065 template <typename LHS, typename RHS>
1066 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
1067                                                           const RHS &R) {
1068   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
1069 }
1070 
1071 template <typename LHS, typename RHS>
1072 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
1073                                                           const RHS &R) {
1074   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
1075 }
1076 
1077 template <typename LHS, typename RHS>
1078 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
1079                                                           const RHS &R) {
1080   return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
1081 }
1082 
1083 template <typename LHS, typename RHS>
1084 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
1085                                                           const RHS &R) {
1086   return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
1087 }
1088 
1089 template <typename LHS, typename RHS>
1090 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
1091                                                           const RHS &R) {
1092   return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
1093 }
1094 
1095 template <typename LHS, typename RHS>
1096 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
1097                                                         const RHS &R) {
1098   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
1099 }
1100 
1101 template <typename LHS, typename RHS>
1102 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
1103                                                       const RHS &R) {
1104   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
1105 }
1106 
1107 template <typename LHS, typename RHS>
1108 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
1109                                                         const RHS &R) {
1110   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
1111 }
1112 
1113 template <typename LHS, typename RHS>
1114 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
1115                                                         const RHS &R) {
1116   return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
1117 }
1118 
1119 template <typename LHS, typename RHS>
1120 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
1121                                                           const RHS &R) {
1122   return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
1123 }
1124 
1125 template <typename LHS, typename RHS>
1126 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
1127                                                           const RHS &R) {
1128   return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
1129 }
1130 
1131 template <typename LHS_t, typename RHS_t, unsigned Opcode,
1132           unsigned WrapFlags = 0>
1133 struct OverflowingBinaryOp_match {
1134   LHS_t L;
1135   RHS_t R;
1136 
1137   OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1138       : L(LHS), R(RHS) {}
1139 
1140   template <typename OpTy> bool match(OpTy *V) {
1141     if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1142       if (Op->getOpcode() != Opcode)
1143         return false;
1144       if ((WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap) &&
1145           !Op->hasNoUnsignedWrap())
1146         return false;
1147       if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1148           !Op->hasNoSignedWrap())
1149         return false;
1150       return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
1151     }
1152     return false;
1153   }
1154 };
1155 
1156 template <typename LHS, typename RHS>
1157 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1158                                  OverflowingBinaryOperator::NoSignedWrap>
1159 m_NSWAdd(const LHS &L, const RHS &R) {
1160   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1161                                    OverflowingBinaryOperator::NoSignedWrap>(L,
1162                                                                             R);
1163 }
1164 template <typename LHS, typename RHS>
1165 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1166                                  OverflowingBinaryOperator::NoSignedWrap>
1167 m_NSWSub(const LHS &L, const RHS &R) {
1168   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1169                                    OverflowingBinaryOperator::NoSignedWrap>(L,
1170                                                                             R);
1171 }
1172 template <typename LHS, typename RHS>
1173 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1174                                  OverflowingBinaryOperator::NoSignedWrap>
1175 m_NSWMul(const LHS &L, const RHS &R) {
1176   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1177                                    OverflowingBinaryOperator::NoSignedWrap>(L,
1178                                                                             R);
1179 }
1180 template <typename LHS, typename RHS>
1181 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1182                                  OverflowingBinaryOperator::NoSignedWrap>
1183 m_NSWShl(const LHS &L, const RHS &R) {
1184   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1185                                    OverflowingBinaryOperator::NoSignedWrap>(L,
1186                                                                             R);
1187 }
1188 
1189 template <typename LHS, typename RHS>
1190 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1191                                  OverflowingBinaryOperator::NoUnsignedWrap>
1192 m_NUWAdd(const LHS &L, const RHS &R) {
1193   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1194                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1195       L, R);
1196 }
1197 template <typename LHS, typename RHS>
1198 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1199                                  OverflowingBinaryOperator::NoUnsignedWrap>
1200 m_NUWSub(const LHS &L, const RHS &R) {
1201   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1202                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1203       L, R);
1204 }
1205 template <typename LHS, typename RHS>
1206 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1207                                  OverflowingBinaryOperator::NoUnsignedWrap>
1208 m_NUWMul(const LHS &L, const RHS &R) {
1209   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1210                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1211       L, R);
1212 }
1213 template <typename LHS, typename RHS>
1214 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1215                                  OverflowingBinaryOperator::NoUnsignedWrap>
1216 m_NUWShl(const LHS &L, const RHS &R) {
1217   return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1218                                    OverflowingBinaryOperator::NoUnsignedWrap>(
1219       L, R);
1220 }
1221 
1222 template <typename LHS_t, typename RHS_t, bool Commutable = false>
1223 struct SpecificBinaryOp_match
1224     : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1225   unsigned Opcode;
1226 
1227   SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
1228       : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1229 
1230   template <typename OpTy> bool match(OpTy *V) {
1231     return BinaryOp_match<LHS_t, RHS_t, 0, Commutable>::match(Opcode, V);
1232   }
1233 };
1234 
1235 /// Matches a specific opcode.
1236 template <typename LHS, typename RHS>
1237 inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1238                                                 const RHS &R) {
1239   return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1240 }
1241 
1242 template <typename LHS, typename RHS, bool Commutable = false>
1243 struct DisjointOr_match {
1244   LHS L;
1245   RHS R;
1246 
1247   DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1248 
1249   template <typename OpTy> bool match(OpTy *V) {
1250     if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1251       assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1252       if (!PDI->isDisjoint())
1253         return false;
1254       return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1255              (Commutable && L.match(PDI->getOperand(1)) &&
1256               R.match(PDI->getOperand(0)));
1257     }
1258     return false;
1259   }
1260 };
1261 
1262 template <typename LHS, typename RHS>
1263 inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) {
1264   return DisjointOr_match<LHS, RHS>(L, R);
1265 }
1266 
1267 template <typename LHS, typename RHS>
1268 inline DisjointOr_match<LHS, RHS, true> m_c_DisjointOr(const LHS &L,
1269                                                        const RHS &R) {
1270   return DisjointOr_match<LHS, RHS, true>(L, R);
1271 }
1272 
1273 /// Match either "add" or "or disjoint".
1274 template <typename LHS, typename RHS>
1275 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Add>,
1276                         DisjointOr_match<LHS, RHS>>
1277 m_AddLike(const LHS &L, const RHS &R) {
1278   return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1279 }
1280 
1281 //===----------------------------------------------------------------------===//
1282 // Class that matches a group of binary opcodes.
1283 //
1284 template <typename LHS_t, typename RHS_t, typename Predicate>
1285 struct BinOpPred_match : Predicate {
1286   LHS_t L;
1287   RHS_t R;
1288 
1289   BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1290 
1291   template <typename OpTy> bool match(OpTy *V) {
1292     if (auto *I = dyn_cast<Instruction>(V))
1293       return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
1294              R.match(I->getOperand(1));
1295     return false;
1296   }
1297 };
1298 
1299 struct is_shift_op {
1300   bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
1301 };
1302 
1303 struct is_right_shift_op {
1304   bool isOpType(unsigned Opcode) {
1305     return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1306   }
1307 };
1308 
1309 struct is_logical_shift_op {
1310   bool isOpType(unsigned Opcode) {
1311     return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1312   }
1313 };
1314 
1315 struct is_bitwiselogic_op {
1316   bool isOpType(unsigned Opcode) {
1317     return Instruction::isBitwiseLogicOp(Opcode);
1318   }
1319 };
1320 
1321 struct is_idiv_op {
1322   bool isOpType(unsigned Opcode) {
1323     return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1324   }
1325 };
1326 
1327 struct is_irem_op {
1328   bool isOpType(unsigned Opcode) {
1329     return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1330   }
1331 };
1332 
1333 /// Matches shift operations.
1334 template <typename LHS, typename RHS>
1335 inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
1336                                                       const RHS &R) {
1337   return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
1338 }
1339 
1340 /// Matches logical shift operations.
1341 template <typename LHS, typename RHS>
1342 inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
1343                                                           const RHS &R) {
1344   return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
1345 }
1346 
1347 /// Matches logical shift operations.
1348 template <typename LHS, typename RHS>
1349 inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
1350 m_LogicalShift(const LHS &L, const RHS &R) {
1351   return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
1352 }
1353 
1354 /// Matches bitwise logic operations.
1355 template <typename LHS, typename RHS>
1356 inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
1357 m_BitwiseLogic(const LHS &L, const RHS &R) {
1358   return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
1359 }
1360 
1361 /// Matches integer division operations.
1362 template <typename LHS, typename RHS>
1363 inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
1364                                                     const RHS &R) {
1365   return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
1366 }
1367 
1368 /// Matches integer remainder operations.
1369 template <typename LHS, typename RHS>
1370 inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L,
1371                                                     const RHS &R) {
1372   return BinOpPred_match<LHS, RHS, is_irem_op>(L, R);
1373 }
1374 
1375 //===----------------------------------------------------------------------===//
1376 // Class that matches exact binary ops.
1377 //
1378 template <typename SubPattern_t> struct Exact_match {
1379   SubPattern_t SubPattern;
1380 
1381   Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1382 
1383   template <typename OpTy> bool match(OpTy *V) {
1384     if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1385       return PEO->isExact() && SubPattern.match(V);
1386     return false;
1387   }
1388 };
1389 
1390 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1391   return SubPattern;
1392 }
1393 
1394 //===----------------------------------------------------------------------===//
1395 // Matchers for CmpInst classes
1396 //
1397 
1398 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1399           bool Commutable = false>
1400 struct CmpClass_match {
1401   PredicateTy &Predicate;
1402   LHS_t L;
1403   RHS_t R;
1404 
1405   // The evaluation order is always stable, regardless of Commutability.
1406   // The LHS is always matched first.
1407   CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1408       : Predicate(Pred), L(LHS), R(RHS) {}
1409 
1410   template <typename OpTy> bool match(OpTy *V) {
1411     if (auto *I = dyn_cast<Class>(V)) {
1412       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1413         Predicate = I->getPredicate();
1414         return true;
1415       } else if (Commutable && L.match(I->getOperand(1)) &&
1416                  R.match(I->getOperand(0))) {
1417         Predicate = I->getSwappedPredicate();
1418         return true;
1419       }
1420     }
1421     return false;
1422   }
1423 };
1424 
1425 template <typename LHS, typename RHS>
1426 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
1427 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1428   return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
1429 }
1430 
1431 template <typename LHS, typename RHS>
1432 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
1433 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1434   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
1435 }
1436 
1437 template <typename LHS, typename RHS>
1438 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
1439 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1440   return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
1441 }
1442 
1443 //===----------------------------------------------------------------------===//
1444 // Matchers for instructions with a given opcode and number of operands.
1445 //
1446 
1447 /// Matches instructions with Opcode and three operands.
1448 template <typename T0, unsigned Opcode> struct OneOps_match {
1449   T0 Op1;
1450 
1451   OneOps_match(const T0 &Op1) : Op1(Op1) {}
1452 
1453   template <typename OpTy> bool match(OpTy *V) {
1454     if (V->getValueID() == Value::InstructionVal + Opcode) {
1455       auto *I = cast<Instruction>(V);
1456       return Op1.match(I->getOperand(0));
1457     }
1458     return false;
1459   }
1460 };
1461 
1462 /// Matches instructions with Opcode and three operands.
1463 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1464   T0 Op1;
1465   T1 Op2;
1466 
1467   TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1468 
1469   template <typename OpTy> bool match(OpTy *V) {
1470     if (V->getValueID() == Value::InstructionVal + Opcode) {
1471       auto *I = cast<Instruction>(V);
1472       return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1473     }
1474     return false;
1475   }
1476 };
1477 
1478 /// Matches instructions with Opcode and three operands.
1479 template <typename T0, typename T1, typename T2, unsigned Opcode>
1480 struct ThreeOps_match {
1481   T0 Op1;
1482   T1 Op2;
1483   T2 Op3;
1484 
1485   ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1486       : Op1(Op1), Op2(Op2), Op3(Op3) {}
1487 
1488   template <typename OpTy> bool match(OpTy *V) {
1489     if (V->getValueID() == Value::InstructionVal + Opcode) {
1490       auto *I = cast<Instruction>(V);
1491       return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1492              Op3.match(I->getOperand(2));
1493     }
1494     return false;
1495   }
1496 };
1497 
1498 /// Matches instructions with Opcode and any number of operands
1499 template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1500   std::tuple<OperandTypes...> Operands;
1501 
1502   AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1503 
1504   // Operand matching works by recursively calling match_operands, matching the
1505   // operands left to right. The first version is called for each operand but
1506   // the last, for which the second version is called. The second version of
1507   // match_operands is also used to match each individual operand.
1508   template <int Idx, int Last>
1509   std::enable_if_t<Idx != Last, bool> match_operands(const Instruction *I) {
1510     return match_operands<Idx, Idx>(I) && match_operands<Idx + 1, Last>(I);
1511   }
1512 
1513   template <int Idx, int Last>
1514   std::enable_if_t<Idx == Last, bool> match_operands(const Instruction *I) {
1515     return std::get<Idx>(Operands).match(I->getOperand(Idx));
1516   }
1517 
1518   template <typename OpTy> bool match(OpTy *V) {
1519     if (V->getValueID() == Value::InstructionVal + Opcode) {
1520       auto *I = cast<Instruction>(V);
1521       return I->getNumOperands() == sizeof...(OperandTypes) &&
1522              match_operands<0, sizeof...(OperandTypes) - 1>(I);
1523     }
1524     return false;
1525   }
1526 };
1527 
1528 /// Matches SelectInst.
1529 template <typename Cond, typename LHS, typename RHS>
1530 inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
1531 m_Select(const Cond &C, const LHS &L, const RHS &R) {
1532   return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
1533 }
1534 
1535 /// This matches a select of two constants, e.g.:
1536 /// m_SelectCst<-1, 0>(m_Value(V))
1537 template <int64_t L, int64_t R, typename Cond>
1538 inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1539                       Instruction::Select>
1540 m_SelectCst(const Cond &C) {
1541   return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1542 }
1543 
1544 /// Matches FreezeInst.
1545 template <typename OpTy>
1546 inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
1547   return OneOps_match<OpTy, Instruction::Freeze>(Op);
1548 }
1549 
1550 /// Matches InsertElementInst.
1551 template <typename Val_t, typename Elt_t, typename Idx_t>
1552 inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
1553 m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1554   return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1555       Val, Elt, Idx);
1556 }
1557 
1558 /// Matches ExtractElementInst.
1559 template <typename Val_t, typename Idx_t>
1560 inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
1561 m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1562   return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
1563 }
1564 
1565 /// Matches shuffle.
1566 template <typename T0, typename T1, typename T2> struct Shuffle_match {
1567   T0 Op1;
1568   T1 Op2;
1569   T2 Mask;
1570 
1571   Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1572       : Op1(Op1), Op2(Op2), Mask(Mask) {}
1573 
1574   template <typename OpTy> bool match(OpTy *V) {
1575     if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1576       return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1577              Mask.match(I->getShuffleMask());
1578     }
1579     return false;
1580   }
1581 };
1582 
1583 struct m_Mask {
1584   ArrayRef<int> &MaskRef;
1585   m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
1586   bool match(ArrayRef<int> Mask) {
1587     MaskRef = Mask;
1588     return true;
1589   }
1590 };
1591 
1592 struct m_ZeroMask {
1593   bool match(ArrayRef<int> Mask) {
1594     return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1595   }
1596 };
1597 
1598 struct m_SpecificMask {
1599   ArrayRef<int> &MaskRef;
1600   m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
1601   bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
1602 };
1603 
1604 struct m_SplatOrUndefMask {
1605   int &SplatIndex;
1606   m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {}
1607   bool match(ArrayRef<int> Mask) {
1608     const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
1609     if (First == Mask.end())
1610       return false;
1611     SplatIndex = *First;
1612     return all_of(Mask,
1613                   [First](int Elem) { return Elem == *First || Elem == -1; });
1614   }
1615 };
1616 
1617 /// Matches ShuffleVectorInst independently of mask value.
1618 template <typename V1_t, typename V2_t>
1619 inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>
1620 m_Shuffle(const V1_t &v1, const V2_t &v2) {
1621   return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2);
1622 }
1623 
1624 template <typename V1_t, typename V2_t, typename Mask_t>
1625 inline Shuffle_match<V1_t, V2_t, Mask_t>
1626 m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
1627   return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
1628 }
1629 
1630 /// Matches LoadInst.
1631 template <typename OpTy>
1632 inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1633   return OneOps_match<OpTy, Instruction::Load>(Op);
1634 }
1635 
1636 /// Matches StoreInst.
1637 template <typename ValueOpTy, typename PointerOpTy>
1638 inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
1639 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1640   return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1641                                                                   PointerOp);
1642 }
1643 
1644 /// Matches GetElementPtrInst.
1645 template <typename... OperandTypes>
1646 inline auto m_GEP(const OperandTypes &...Ops) {
1647   return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
1648 }
1649 
1650 //===----------------------------------------------------------------------===//
1651 // Matchers for CastInst classes
1652 //
1653 
1654 template <typename Op_t, unsigned Opcode> struct CastOperator_match {
1655   Op_t Op;
1656 
1657   CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
1658 
1659   template <typename OpTy> bool match(OpTy *V) {
1660     if (auto *O = dyn_cast<Operator>(V))
1661       return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1662     return false;
1663   }
1664 };
1665 
1666 template <typename Op_t, unsigned Opcode> struct CastInst_match {
1667   Op_t Op;
1668 
1669   CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
1670 
1671   template <typename OpTy> bool match(OpTy *V) {
1672     if (auto *I = dyn_cast<Instruction>(V))
1673       return I->getOpcode() == Opcode && Op.match(I->getOperand(0));
1674     return false;
1675   }
1676 };
1677 
1678 template <typename Op_t> struct PtrToIntSameSize_match {
1679   const DataLayout &DL;
1680   Op_t Op;
1681 
1682   PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
1683       : DL(DL), Op(OpMatch) {}
1684 
1685   template <typename OpTy> bool match(OpTy *V) {
1686     if (auto *O = dyn_cast<Operator>(V))
1687       return O->getOpcode() == Instruction::PtrToInt &&
1688              DL.getTypeSizeInBits(O->getType()) ==
1689                  DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
1690              Op.match(O->getOperand(0));
1691     return false;
1692   }
1693 };
1694 
1695 template <typename Op_t> struct NNegZExt_match {
1696   Op_t Op;
1697 
1698   NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
1699 
1700   template <typename OpTy> bool match(OpTy *V) {
1701     if (auto *I = dyn_cast<Instruction>(V))
1702       return I->getOpcode() == Instruction::ZExt && I->hasNonNeg() &&
1703              Op.match(I->getOperand(0));
1704     return false;
1705   }
1706 };
1707 
1708 /// Matches BitCast.
1709 template <typename OpTy>
1710 inline CastOperator_match<OpTy, Instruction::BitCast>
1711 m_BitCast(const OpTy &Op) {
1712   return CastOperator_match<OpTy, Instruction::BitCast>(Op);
1713 }
1714 
1715 /// Matches PtrToInt.
1716 template <typename OpTy>
1717 inline CastOperator_match<OpTy, Instruction::PtrToInt>
1718 m_PtrToInt(const OpTy &Op) {
1719   return CastOperator_match<OpTy, Instruction::PtrToInt>(Op);
1720 }
1721 
1722 template <typename OpTy>
1723 inline PtrToIntSameSize_match<OpTy> m_PtrToIntSameSize(const DataLayout &DL,
1724                                                        const OpTy &Op) {
1725   return PtrToIntSameSize_match<OpTy>(DL, Op);
1726 }
1727 
1728 /// Matches IntToPtr.
1729 template <typename OpTy>
1730 inline CastOperator_match<OpTy, Instruction::IntToPtr>
1731 m_IntToPtr(const OpTy &Op) {
1732   return CastOperator_match<OpTy, Instruction::IntToPtr>(Op);
1733 }
1734 
1735 /// Matches Trunc.
1736 template <typename OpTy>
1737 inline CastOperator_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1738   return CastOperator_match<OpTy, Instruction::Trunc>(Op);
1739 }
1740 
1741 template <typename OpTy>
1742 inline match_combine_or<CastOperator_match<OpTy, Instruction::Trunc>, OpTy>
1743 m_TruncOrSelf(const OpTy &Op) {
1744   return m_CombineOr(m_Trunc(Op), Op);
1745 }
1746 
1747 /// Matches SExt.
1748 template <typename OpTy>
1749 inline CastInst_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1750   return CastInst_match<OpTy, Instruction::SExt>(Op);
1751 }
1752 
1753 /// Matches ZExt.
1754 template <typename OpTy>
1755 inline CastInst_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1756   return CastInst_match<OpTy, Instruction::ZExt>(Op);
1757 }
1758 
1759 template <typename OpTy>
1760 inline NNegZExt_match<OpTy> m_NNegZExt(const OpTy &Op) {
1761   return NNegZExt_match<OpTy>(Op);
1762 }
1763 
1764 template <typename OpTy>
1765 inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>, OpTy>
1766 m_ZExtOrSelf(const OpTy &Op) {
1767   return m_CombineOr(m_ZExt(Op), Op);
1768 }
1769 
1770 template <typename OpTy>
1771 inline match_combine_or<CastInst_match<OpTy, Instruction::SExt>, OpTy>
1772 m_SExtOrSelf(const OpTy &Op) {
1773   return m_CombineOr(m_SExt(Op), Op);
1774 }
1775 
1776 /// Match either "sext" or "zext nneg".
1777 template <typename OpTy>
1778 inline match_combine_or<CastInst_match<OpTy, Instruction::SExt>,
1779                         NNegZExt_match<OpTy>>
1780 m_SExtLike(const OpTy &Op) {
1781   return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
1782 }
1783 
1784 template <typename OpTy>
1785 inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>,
1786                         CastInst_match<OpTy, Instruction::SExt>>
1787 m_ZExtOrSExt(const OpTy &Op) {
1788   return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1789 }
1790 
1791 template <typename OpTy>
1792 inline match_combine_or<
1793     match_combine_or<CastInst_match<OpTy, Instruction::ZExt>,
1794                      CastInst_match<OpTy, Instruction::SExt>>,
1795     OpTy>
1796 m_ZExtOrSExtOrSelf(const OpTy &Op) {
1797   return m_CombineOr(m_ZExtOrSExt(Op), Op);
1798 }
1799 
1800 template <typename OpTy>
1801 inline CastInst_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1802   return CastInst_match<OpTy, Instruction::UIToFP>(Op);
1803 }
1804 
1805 template <typename OpTy>
1806 inline CastInst_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1807   return CastInst_match<OpTy, Instruction::SIToFP>(Op);
1808 }
1809 
1810 template <typename OpTy>
1811 inline CastInst_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) {
1812   return CastInst_match<OpTy, Instruction::FPToUI>(Op);
1813 }
1814 
1815 template <typename OpTy>
1816 inline CastInst_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) {
1817   return CastInst_match<OpTy, Instruction::FPToSI>(Op);
1818 }
1819 
1820 template <typename OpTy>
1821 inline CastInst_match<OpTy, Instruction::FPTrunc>
1822 m_FPTrunc(const OpTy &Op) {
1823   return CastInst_match<OpTy, Instruction::FPTrunc>(Op);
1824 }
1825 
1826 template <typename OpTy>
1827 inline CastInst_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1828   return CastInst_match<OpTy, Instruction::FPExt>(Op);
1829 }
1830 
1831 //===----------------------------------------------------------------------===//
1832 // Matchers for control flow.
1833 //
1834 
1835 struct br_match {
1836   BasicBlock *&Succ;
1837 
1838   br_match(BasicBlock *&Succ) : Succ(Succ) {}
1839 
1840   template <typename OpTy> bool match(OpTy *V) {
1841     if (auto *BI = dyn_cast<BranchInst>(V))
1842       if (BI->isUnconditional()) {
1843         Succ = BI->getSuccessor(0);
1844         return true;
1845       }
1846     return false;
1847   }
1848 };
1849 
1850 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1851 
1852 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1853 struct brc_match {
1854   Cond_t Cond;
1855   TrueBlock_t T;
1856   FalseBlock_t F;
1857 
1858   brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
1859       : Cond(C), T(t), F(f) {}
1860 
1861   template <typename OpTy> bool match(OpTy *V) {
1862     if (auto *BI = dyn_cast<BranchInst>(V))
1863       if (BI->isConditional() && Cond.match(BI->getCondition()))
1864         return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
1865     return false;
1866   }
1867 };
1868 
1869 template <typename Cond_t>
1870 inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>
1871 m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1872   return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>(
1873       C, m_BasicBlock(T), m_BasicBlock(F));
1874 }
1875 
1876 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1877 inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t>
1878 m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
1879   return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F);
1880 }
1881 
1882 //===----------------------------------------------------------------------===//
1883 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1884 //
1885 
1886 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1887           bool Commutable = false>
1888 struct MaxMin_match {
1889   using PredType = Pred_t;
1890   LHS_t L;
1891   RHS_t R;
1892 
1893   // The evaluation order is always stable, regardless of Commutability.
1894   // The LHS is always matched first.
1895   MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1896 
1897   template <typename OpTy> bool match(OpTy *V) {
1898     if (auto *II = dyn_cast<IntrinsicInst>(V)) {
1899       Intrinsic::ID IID = II->getIntrinsicID();
1900       if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
1901           (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
1902           (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
1903           (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
1904         Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
1905         return (L.match(LHS) && R.match(RHS)) ||
1906                (Commutable && L.match(RHS) && R.match(LHS));
1907       }
1908     }
1909     // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1910     auto *SI = dyn_cast<SelectInst>(V);
1911     if (!SI)
1912       return false;
1913     auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1914     if (!Cmp)
1915       return false;
1916     // At this point we have a select conditioned on a comparison.  Check that
1917     // it is the values returned by the select that are being compared.
1918     auto *TrueVal = SI->getTrueValue();
1919     auto *FalseVal = SI->getFalseValue();
1920     auto *LHS = Cmp->getOperand(0);
1921     auto *RHS = Cmp->getOperand(1);
1922     if ((TrueVal != LHS || FalseVal != RHS) &&
1923         (TrueVal != RHS || FalseVal != LHS))
1924       return false;
1925     typename CmpInst_t::Predicate Pred =
1926         LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1927     // Does "(x pred y) ? x : y" represent the desired max/min operation?
1928     if (!Pred_t::match(Pred))
1929       return false;
1930     // It does!  Bind the operands.
1931     return (L.match(LHS) && R.match(RHS)) ||
1932            (Commutable && L.match(RHS) && R.match(LHS));
1933   }
1934 };
1935 
1936 /// Helper class for identifying signed max predicates.
1937 struct smax_pred_ty {
1938   static bool match(ICmpInst::Predicate Pred) {
1939     return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1940   }
1941 };
1942 
1943 /// Helper class for identifying signed min predicates.
1944 struct smin_pred_ty {
1945   static bool match(ICmpInst::Predicate Pred) {
1946     return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1947   }
1948 };
1949 
1950 /// Helper class for identifying unsigned max predicates.
1951 struct umax_pred_ty {
1952   static bool match(ICmpInst::Predicate Pred) {
1953     return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1954   }
1955 };
1956 
1957 /// Helper class for identifying unsigned min predicates.
1958 struct umin_pred_ty {
1959   static bool match(ICmpInst::Predicate Pred) {
1960     return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1961   }
1962 };
1963 
1964 /// Helper class for identifying ordered max predicates.
1965 struct ofmax_pred_ty {
1966   static bool match(FCmpInst::Predicate Pred) {
1967     return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1968   }
1969 };
1970 
1971 /// Helper class for identifying ordered min predicates.
1972 struct ofmin_pred_ty {
1973   static bool match(FCmpInst::Predicate Pred) {
1974     return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1975   }
1976 };
1977 
1978 /// Helper class for identifying unordered max predicates.
1979 struct ufmax_pred_ty {
1980   static bool match(FCmpInst::Predicate Pred) {
1981     return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1982   }
1983 };
1984 
1985 /// Helper class for identifying unordered min predicates.
1986 struct ufmin_pred_ty {
1987   static bool match(FCmpInst::Predicate Pred) {
1988     return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1989   }
1990 };
1991 
1992 template <typename LHS, typename RHS>
1993 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1994                                                              const RHS &R) {
1995   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1996 }
1997 
1998 template <typename LHS, typename RHS>
1999 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
2000                                                              const RHS &R) {
2001   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
2002 }
2003 
2004 template <typename LHS, typename RHS>
2005 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
2006                                                              const RHS &R) {
2007   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
2008 }
2009 
2010 template <typename LHS, typename RHS>
2011 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
2012                                                              const RHS &R) {
2013   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
2014 }
2015 
2016 template <typename LHS, typename RHS>
2017 inline match_combine_or<
2018     match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>,
2019                      MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>,
2020     match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>,
2021                      MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>>
2022 m_MaxOrMin(const LHS &L, const RHS &R) {
2023   return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2024                      m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2025 }
2026 
2027 /// Match an 'ordered' floating point maximum function.
2028 /// Floating point has one special value 'NaN'. Therefore, there is no total
2029 /// order. However, if we can ignore the 'NaN' value (for example, because of a
2030 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2031 /// semantics. In the presence of 'NaN' we have to preserve the original
2032 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2033 ///
2034 ///                         max(L, R)  iff L and R are not NaN
2035 ///  m_OrdFMax(L, R) =      R          iff L or R are NaN
2036 template <typename LHS, typename RHS>
2037 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
2038                                                                  const RHS &R) {
2039   return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
2040 }
2041 
2042 /// Match an 'ordered' floating point minimum function.
2043 /// Floating point has one special value 'NaN'. Therefore, there is no total
2044 /// order. However, if we can ignore the 'NaN' value (for example, because of a
2045 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2046 /// semantics. In the presence of 'NaN' we have to preserve the original
2047 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2048 ///
2049 ///                         min(L, R)  iff L and R are not NaN
2050 ///  m_OrdFMin(L, R) =      R          iff L or R are NaN
2051 template <typename LHS, typename RHS>
2052 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
2053                                                                  const RHS &R) {
2054   return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
2055 }
2056 
2057 /// Match an 'unordered' floating point maximum function.
2058 /// Floating point has one special value 'NaN'. Therefore, there is no total
2059 /// order. However, if we can ignore the 'NaN' value (for example, because of a
2060 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2061 /// semantics. In the presence of 'NaN' we have to preserve the original
2062 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2063 ///
2064 ///                         max(L, R)  iff L and R are not NaN
2065 ///  m_UnordFMax(L, R) =    L          iff L or R are NaN
2066 template <typename LHS, typename RHS>
2067 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
2068 m_UnordFMax(const LHS &L, const RHS &R) {
2069   return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
2070 }
2071 
2072 /// Match an 'unordered' floating point minimum function.
2073 /// Floating point has one special value 'NaN'. Therefore, there is no total
2074 /// order. However, if we can ignore the 'NaN' value (for example, because of a
2075 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2076 /// semantics. In the presence of 'NaN' we have to preserve the original
2077 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2078 ///
2079 ///                          min(L, R)  iff L and R are not NaN
2080 ///  m_UnordFMin(L, R) =     L          iff L or R are NaN
2081 template <typename LHS, typename RHS>
2082 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
2083 m_UnordFMin(const LHS &L, const RHS &R) {
2084   return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
2085 }
2086 
2087 //===----------------------------------------------------------------------===//
2088 // Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2089 // Note that S might be matched to other instructions than AddInst.
2090 //
2091 
2092 template <typename LHS_t, typename RHS_t, typename Sum_t>
2093 struct UAddWithOverflow_match {
2094   LHS_t L;
2095   RHS_t R;
2096   Sum_t S;
2097 
2098   UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2099       : L(L), R(R), S(S) {}
2100 
2101   template <typename OpTy> bool match(OpTy *V) {
2102     Value *ICmpLHS, *ICmpRHS;
2103     ICmpInst::Predicate Pred;
2104     if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2105       return false;
2106 
2107     Value *AddLHS, *AddRHS;
2108     auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2109 
2110     // (a + b) u< a, (a + b) u< b
2111     if (Pred == ICmpInst::ICMP_ULT)
2112       if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2113         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2114 
2115     // a >u (a + b), b >u (a + b)
2116     if (Pred == ICmpInst::ICMP_UGT)
2117       if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2118         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2119 
2120     Value *Op1;
2121     auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
2122     // (a ^ -1) <u b
2123     if (Pred == ICmpInst::ICMP_ULT) {
2124       if (XorExpr.match(ICmpLHS))
2125         return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2126     }
2127     //  b > u (a ^ -1)
2128     if (Pred == ICmpInst::ICMP_UGT) {
2129       if (XorExpr.match(ICmpRHS))
2130         return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2131     }
2132 
2133     // Match special-case for increment-by-1.
2134     if (Pred == ICmpInst::ICMP_EQ) {
2135       // (a + 1) == 0
2136       // (1 + a) == 0
2137       if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2138           (m_One().match(AddLHS) || m_One().match(AddRHS)))
2139         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2140       // 0 == (a + 1)
2141       // 0 == (1 + a)
2142       if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2143           (m_One().match(AddLHS) || m_One().match(AddRHS)))
2144         return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2145     }
2146 
2147     return false;
2148   }
2149 };
2150 
2151 /// Match an icmp instruction checking for unsigned overflow on addition.
2152 ///
2153 /// S is matched to the addition whose result is being checked for overflow, and
2154 /// L and R are matched to the LHS and RHS of S.
2155 template <typename LHS_t, typename RHS_t, typename Sum_t>
2156 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
2157 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2158   return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
2159 }
2160 
2161 template <typename Opnd_t> struct Argument_match {
2162   unsigned OpI;
2163   Opnd_t Val;
2164 
2165   Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2166 
2167   template <typename OpTy> bool match(OpTy *V) {
2168     // FIXME: Should likely be switched to use `CallBase`.
2169     if (const auto *CI = dyn_cast<CallInst>(V))
2170       return Val.match(CI->getArgOperand(OpI));
2171     return false;
2172   }
2173 };
2174 
2175 /// Match an argument.
2176 template <unsigned OpI, typename Opnd_t>
2177 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2178   return Argument_match<Opnd_t>(OpI, Op);
2179 }
2180 
2181 /// Intrinsic matchers.
2182 struct IntrinsicID_match {
2183   unsigned ID;
2184 
2185   IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
2186 
2187   template <typename OpTy> bool match(OpTy *V) {
2188     if (const auto *CI = dyn_cast<CallInst>(V))
2189       if (const auto *F = CI->getCalledFunction())
2190         return F->getIntrinsicID() == ID;
2191     return false;
2192   }
2193 };
2194 
2195 /// Intrinsic matches are combinations of ID matchers, and argument
2196 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
2197 /// them with lower arity matchers. Here's some convenient typedefs for up to
2198 /// several arguments, and more can be added as needed
2199 template <typename T0 = void, typename T1 = void, typename T2 = void,
2200           typename T3 = void, typename T4 = void, typename T5 = void,
2201           typename T6 = void, typename T7 = void, typename T8 = void,
2202           typename T9 = void, typename T10 = void>
2203 struct m_Intrinsic_Ty;
2204 template <typename T0> struct m_Intrinsic_Ty<T0> {
2205   using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
2206 };
2207 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2208   using Ty =
2209       match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
2210 };
2211 template <typename T0, typename T1, typename T2>
2212 struct m_Intrinsic_Ty<T0, T1, T2> {
2213   using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
2214                                Argument_match<T2>>;
2215 };
2216 template <typename T0, typename T1, typename T2, typename T3>
2217 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
2218   using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
2219                                Argument_match<T3>>;
2220 };
2221 
2222 template <typename T0, typename T1, typename T2, typename T3, typename T4>
2223 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
2224   using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty,
2225                                Argument_match<T4>>;
2226 };
2227 
2228 template <typename T0, typename T1, typename T2, typename T3, typename T4,
2229           typename T5>
2230 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> {
2231   using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty,
2232                                Argument_match<T5>>;
2233 };
2234 
2235 /// Match intrinsic calls like this:
2236 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2237 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2238   return IntrinsicID_match(IntrID);
2239 }
2240 
2241 /// Matches MaskedLoad Intrinsic.
2242 template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2243 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty
2244 m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2245              const Opnd3 &Op3) {
2246   return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
2247 }
2248 
2249 /// Matches MaskedGather Intrinsic.
2250 template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2251 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty
2252 m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2253                const Opnd3 &Op3) {
2254   return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
2255 }
2256 
2257 template <Intrinsic::ID IntrID, typename T0>
2258 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2259   return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
2260 }
2261 
2262 template <Intrinsic::ID IntrID, typename T0, typename T1>
2263 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2264                                                        const T1 &Op1) {
2265   return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
2266 }
2267 
2268 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2269 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2270 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2271   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2272 }
2273 
2274 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2275           typename T3>
2276 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
2277 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2278   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2279 }
2280 
2281 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2282           typename T3, typename T4>
2283 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty
2284 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2285             const T4 &Op4) {
2286   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2287                       m_Argument<4>(Op4));
2288 }
2289 
2290 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2291           typename T3, typename T4, typename T5>
2292 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty
2293 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2294             const T4 &Op4, const T5 &Op5) {
2295   return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2296                       m_Argument<5>(Op5));
2297 }
2298 
2299 // Helper intrinsic matching specializations.
2300 template <typename Opnd0>
2301 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2302   return m_Intrinsic<Intrinsic::bitreverse>(Op0);
2303 }
2304 
2305 template <typename Opnd0>
2306 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2307   return m_Intrinsic<Intrinsic::bswap>(Op0);
2308 }
2309 
2310 template <typename Opnd0>
2311 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2312   return m_Intrinsic<Intrinsic::fabs>(Op0);
2313 }
2314 
2315 template <typename Opnd0>
2316 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2317   return m_Intrinsic<Intrinsic::canonicalize>(Op0);
2318 }
2319 
2320 template <typename Opnd0, typename Opnd1>
2321 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
2322                                                         const Opnd1 &Op1) {
2323   return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2324 }
2325 
2326 template <typename Opnd0, typename Opnd1>
2327 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
2328                                                         const Opnd1 &Op1) {
2329   return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2330 }
2331 
2332 template <typename Opnd0, typename Opnd1, typename Opnd2>
2333 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
2334 m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2335   return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2336 }
2337 
2338 template <typename Opnd0, typename Opnd1, typename Opnd2>
2339 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
2340 m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2341   return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2342 }
2343 
2344 template <typename Opnd0>
2345 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2346   return m_Intrinsic<Intrinsic::sqrt>(Op0);
2347 }
2348 
2349 template <typename Opnd0, typename Opnd1>
2350 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2351                                                             const Opnd1 &Op1) {
2352   return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2353 }
2354 
2355 template <typename Opnd0>
2356 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2357   return m_Intrinsic<Intrinsic::experimental_vector_reverse>(Op0);
2358 }
2359 
2360 //===----------------------------------------------------------------------===//
2361 // Matchers for two-operands operators with the operators in either order
2362 //
2363 
2364 /// Matches a BinaryOperator with LHS and RHS in either order.
2365 template <typename LHS, typename RHS>
2366 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
2367   return AnyBinaryOp_match<LHS, RHS, true>(L, R);
2368 }
2369 
2370 /// Matches an ICmp with a predicate over LHS and RHS in either order.
2371 /// Swaps the predicate if operands are commuted.
2372 template <typename LHS, typename RHS>
2373 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
2374 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
2375   return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
2376                                                                        R);
2377 }
2378 
2379 /// Matches a specific opcode with LHS and RHS in either order.
2380 template <typename LHS, typename RHS>
2381 inline SpecificBinaryOp_match<LHS, RHS, true>
2382 m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2383   return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2384 }
2385 
2386 /// Matches a Add with LHS and RHS in either order.
2387 template <typename LHS, typename RHS>
2388 inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
2389                                                                 const RHS &R) {
2390   return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
2391 }
2392 
2393 /// Matches a Mul with LHS and RHS in either order.
2394 template <typename LHS, typename RHS>
2395 inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
2396                                                                 const RHS &R) {
2397   return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
2398 }
2399 
2400 /// Matches an And with LHS and RHS in either order.
2401 template <typename LHS, typename RHS>
2402 inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
2403                                                                 const RHS &R) {
2404   return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
2405 }
2406 
2407 /// Matches an Or with LHS and RHS in either order.
2408 template <typename LHS, typename RHS>
2409 inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
2410                                                               const RHS &R) {
2411   return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
2412 }
2413 
2414 /// Matches an Xor with LHS and RHS in either order.
2415 template <typename LHS, typename RHS>
2416 inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
2417                                                                 const RHS &R) {
2418   return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
2419 }
2420 
2421 /// Matches a 'Neg' as 'sub 0, V'.
2422 template <typename ValTy>
2423 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2424 m_Neg(const ValTy &V) {
2425   return m_Sub(m_ZeroInt(), V);
2426 }
2427 
2428 /// Matches a 'Neg' as 'sub nsw 0, V'.
2429 template <typename ValTy>
2430 inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy,
2431                                  Instruction::Sub,
2432                                  OverflowingBinaryOperator::NoSignedWrap>
2433 m_NSWNeg(const ValTy &V) {
2434   return m_NSWSub(m_ZeroInt(), V);
2435 }
2436 
2437 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2438 /// NOTE: we first match the 'Not' (by matching '-1'),
2439 /// and only then match the inner matcher!
2440 template <typename ValTy>
2441 inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2442 m_Not(const ValTy &V) {
2443   return m_c_Xor(m_AllOnes(), V);
2444 }
2445 
2446 template <typename ValTy> struct NotForbidUndef_match {
2447   ValTy Val;
2448   NotForbidUndef_match(const ValTy &V) : Val(V) {}
2449 
2450   template <typename OpTy> bool match(OpTy *V) {
2451     // We do not use m_c_Xor because that could match an arbitrary APInt that is
2452     // not -1 as C and then fail to match the other operand if it is -1.
2453     // This code should still work even when both operands are constants.
2454     Value *X;
2455     const APInt *C;
2456     if (m_Xor(m_Value(X), m_APIntForbidUndef(C)).match(V) && C->isAllOnes())
2457       return Val.match(X);
2458     if (m_Xor(m_APIntForbidUndef(C), m_Value(X)).match(V) && C->isAllOnes())
2459       return Val.match(X);
2460     return false;
2461   }
2462 };
2463 
2464 /// Matches a bitwise 'not' as 'xor V, -1' or 'xor -1, V'. For vectors, the
2465 /// constant value must be composed of only -1 scalar elements.
2466 template <typename ValTy>
2467 inline NotForbidUndef_match<ValTy> m_NotForbidUndef(const ValTy &V) {
2468   return NotForbidUndef_match<ValTy>(V);
2469 }
2470 
2471 /// Matches an SMin with LHS and RHS in either order.
2472 template <typename LHS, typename RHS>
2473 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
2474 m_c_SMin(const LHS &L, const RHS &R) {
2475   return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
2476 }
2477 /// Matches an SMax with LHS and RHS in either order.
2478 template <typename LHS, typename RHS>
2479 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
2480 m_c_SMax(const LHS &L, const RHS &R) {
2481   return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
2482 }
2483 /// Matches a UMin with LHS and RHS in either order.
2484 template <typename LHS, typename RHS>
2485 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
2486 m_c_UMin(const LHS &L, const RHS &R) {
2487   return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
2488 }
2489 /// Matches a UMax with LHS and RHS in either order.
2490 template <typename LHS, typename RHS>
2491 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
2492 m_c_UMax(const LHS &L, const RHS &R) {
2493   return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
2494 }
2495 
2496 template <typename LHS, typename RHS>
2497 inline match_combine_or<
2498     match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>,
2499                      MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>,
2500     match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>,
2501                      MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>>
2502 m_c_MaxOrMin(const LHS &L, const RHS &R) {
2503   return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
2504                      m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
2505 }
2506 
2507 template <Intrinsic::ID IntrID, typename T0, typename T1>
2508 inline match_combine_or<typename m_Intrinsic_Ty<T0, T1>::Ty,
2509                         typename m_Intrinsic_Ty<T1, T0>::Ty>
2510 m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
2511   return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
2512                      m_Intrinsic<IntrID>(Op1, Op0));
2513 }
2514 
2515 /// Matches FAdd with LHS and RHS in either order.
2516 template <typename LHS, typename RHS>
2517 inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
2518 m_c_FAdd(const LHS &L, const RHS &R) {
2519   return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
2520 }
2521 
2522 /// Matches FMul with LHS and RHS in either order.
2523 template <typename LHS, typename RHS>
2524 inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
2525 m_c_FMul(const LHS &L, const RHS &R) {
2526   return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
2527 }
2528 
2529 template <typename Opnd_t> struct Signum_match {
2530   Opnd_t Val;
2531   Signum_match(const Opnd_t &V) : Val(V) {}
2532 
2533   template <typename OpTy> bool match(OpTy *V) {
2534     unsigned TypeSize = V->getType()->getScalarSizeInBits();
2535     if (TypeSize == 0)
2536       return false;
2537 
2538     unsigned ShiftWidth = TypeSize - 1;
2539     Value *OpL = nullptr, *OpR = nullptr;
2540 
2541     // This is the representation of signum we match:
2542     //
2543     //  signum(x) == (x >> 63) | (-x >>u 63)
2544     //
2545     // An i1 value is its own signum, so it's correct to match
2546     //
2547     //  signum(x) == (x >> 0)  | (-x >>u 0)
2548     //
2549     // for i1 values.
2550 
2551     auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
2552     auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
2553     auto Signum = m_Or(LHS, RHS);
2554 
2555     return Signum.match(V) && OpL == OpR && Val.match(OpL);
2556   }
2557 };
2558 
2559 /// Matches a signum pattern.
2560 ///
2561 /// signum(x) =
2562 ///      x >  0  ->  1
2563 ///      x == 0  ->  0
2564 ///      x <  0  -> -1
2565 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
2566   return Signum_match<Val_t>(V);
2567 }
2568 
2569 template <int Ind, typename Opnd_t> struct ExtractValue_match {
2570   Opnd_t Val;
2571   ExtractValue_match(const Opnd_t &V) : Val(V) {}
2572 
2573   template <typename OpTy> bool match(OpTy *V) {
2574     if (auto *I = dyn_cast<ExtractValueInst>(V)) {
2575       // If Ind is -1, don't inspect indices
2576       if (Ind != -1 &&
2577           !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
2578         return false;
2579       return Val.match(I->getAggregateOperand());
2580     }
2581     return false;
2582   }
2583 };
2584 
2585 /// Match a single index ExtractValue instruction.
2586 /// For example m_ExtractValue<1>(...)
2587 template <int Ind, typename Val_t>
2588 inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) {
2589   return ExtractValue_match<Ind, Val_t>(V);
2590 }
2591 
2592 /// Match an ExtractValue instruction with any index.
2593 /// For example m_ExtractValue(...)
2594 template <typename Val_t>
2595 inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
2596   return ExtractValue_match<-1, Val_t>(V);
2597 }
2598 
2599 /// Matcher for a single index InsertValue instruction.
2600 template <int Ind, typename T0, typename T1> struct InsertValue_match {
2601   T0 Op0;
2602   T1 Op1;
2603 
2604   InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
2605 
2606   template <typename OpTy> bool match(OpTy *V) {
2607     if (auto *I = dyn_cast<InsertValueInst>(V)) {
2608       return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
2609              I->getNumIndices() == 1 && Ind == I->getIndices()[0];
2610     }
2611     return false;
2612   }
2613 };
2614 
2615 /// Matches a single index InsertValue instruction.
2616 template <int Ind, typename Val_t, typename Elt_t>
2617 inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
2618                                                           const Elt_t &Elt) {
2619   return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
2620 }
2621 
2622 /// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
2623 /// the constant expression
2624 ///  `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
2625 /// under the right conditions determined by DataLayout.
2626 struct VScaleVal_match {
2627   template <typename ITy> bool match(ITy *V) {
2628     if (m_Intrinsic<Intrinsic::vscale>().match(V))
2629       return true;
2630 
2631     Value *Ptr;
2632     if (m_PtrToInt(m_Value(Ptr)).match(V)) {
2633       if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
2634         auto *DerefTy =
2635             dyn_cast<ScalableVectorType>(GEP->getSourceElementType());
2636         if (GEP->getNumIndices() == 1 && DerefTy &&
2637             DerefTy->getElementType()->isIntegerTy(8) &&
2638             m_Zero().match(GEP->getPointerOperand()) &&
2639             m_SpecificInt(1).match(GEP->idx_begin()->get()))
2640           return true;
2641       }
2642     }
2643 
2644     return false;
2645   }
2646 };
2647 
2648 inline VScaleVal_match m_VScale() {
2649   return VScaleVal_match();
2650 }
2651 
2652 template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
2653 struct LogicalOp_match {
2654   LHS L;
2655   RHS R;
2656 
2657   LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
2658 
2659   template <typename T> bool match(T *V) {
2660     auto *I = dyn_cast<Instruction>(V);
2661     if (!I || !I->getType()->isIntOrIntVectorTy(1))
2662       return false;
2663 
2664     if (I->getOpcode() == Opcode) {
2665       auto *Op0 = I->getOperand(0);
2666       auto *Op1 = I->getOperand(1);
2667       return (L.match(Op0) && R.match(Op1)) ||
2668              (Commutable && L.match(Op1) && R.match(Op0));
2669     }
2670 
2671     if (auto *Select = dyn_cast<SelectInst>(I)) {
2672       auto *Cond = Select->getCondition();
2673       auto *TVal = Select->getTrueValue();
2674       auto *FVal = Select->getFalseValue();
2675 
2676       // Don't match a scalar select of bool vectors.
2677       // Transforms expect a single type for operands if this matches.
2678       if (Cond->getType() != Select->getType())
2679         return false;
2680 
2681       if (Opcode == Instruction::And) {
2682         auto *C = dyn_cast<Constant>(FVal);
2683         if (C && C->isNullValue())
2684           return (L.match(Cond) && R.match(TVal)) ||
2685                  (Commutable && L.match(TVal) && R.match(Cond));
2686       } else {
2687         assert(Opcode == Instruction::Or);
2688         auto *C = dyn_cast<Constant>(TVal);
2689         if (C && C->isOneValue())
2690           return (L.match(Cond) && R.match(FVal)) ||
2691                  (Commutable && L.match(FVal) && R.match(Cond));
2692       }
2693     }
2694 
2695     return false;
2696   }
2697 };
2698 
2699 /// Matches L && R either in the form of L & R or L ? R : false.
2700 /// Note that the latter form is poison-blocking.
2701 template <typename LHS, typename RHS>
2702 inline LogicalOp_match<LHS, RHS, Instruction::And> m_LogicalAnd(const LHS &L,
2703                                                                 const RHS &R) {
2704   return LogicalOp_match<LHS, RHS, Instruction::And>(L, R);
2705 }
2706 
2707 /// Matches L && R where L and R are arbitrary values.
2708 inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
2709 
2710 /// Matches L && R with LHS and RHS in either order.
2711 template <typename LHS, typename RHS>
2712 inline LogicalOp_match<LHS, RHS, Instruction::And, true>
2713 m_c_LogicalAnd(const LHS &L, const RHS &R) {
2714   return LogicalOp_match<LHS, RHS, Instruction::And, true>(L, R);
2715 }
2716 
2717 /// Matches L || R either in the form of L | R or L ? true : R.
2718 /// Note that the latter form is poison-blocking.
2719 template <typename LHS, typename RHS>
2720 inline LogicalOp_match<LHS, RHS, Instruction::Or> m_LogicalOr(const LHS &L,
2721                                                               const RHS &R) {
2722   return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R);
2723 }
2724 
2725 /// Matches L || R where L and R are arbitrary values.
2726 inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
2727 
2728 /// Matches L || R with LHS and RHS in either order.
2729 template <typename LHS, typename RHS>
2730 inline LogicalOp_match<LHS, RHS, Instruction::Or, true>
2731 m_c_LogicalOr(const LHS &L, const RHS &R) {
2732   return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R);
2733 }
2734 
2735 /// Matches either L && R or L || R,
2736 /// either one being in the either binary or logical form.
2737 /// Note that the latter form is poison-blocking.
2738 template <typename LHS, typename RHS, bool Commutable = false>
2739 inline auto m_LogicalOp(const LHS &L, const RHS &R) {
2740   return m_CombineOr(
2741       LogicalOp_match<LHS, RHS, Instruction::And, Commutable>(L, R),
2742       LogicalOp_match<LHS, RHS, Instruction::Or, Commutable>(L, R));
2743 }
2744 
2745 /// Matches either L && R or L || R where L and R are arbitrary values.
2746 inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
2747 
2748 /// Matches either L && R or L || R with LHS and RHS in either order.
2749 template <typename LHS, typename RHS>
2750 inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
2751   return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
2752 }
2753 
2754 } // end namespace PatternMatch
2755 } // end namespace llvm
2756 
2757 #endif // LLVM_IR_PATTERNMATCH_H
2758