LLVM 23.0.0git
PatternMatch.h
Go to the documentation of this file.
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/FMF.h"
37#include "llvm/IR/InstrTypes.h"
38#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Intrinsics.h"
42#include "llvm/IR/Operator.h"
43#include "llvm/IR/Value.h"
45#include <cstdint>
46
47namespace llvm {
48namespace PatternMatch {
49
50template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
51 return P.match(V);
52}
53
54/// A match functor that can be used as a UnaryPredicate in functional
55/// algorithms like all_of.
56template <typename Val = const Value, typename Pattern>
57auto match_fn(const Pattern &P) {
59}
60
61template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
62 return P.match(Mask);
63}
64
65template <typename SubPattern_t> struct OneUse_match {
66 SubPattern_t SubPattern;
67
68 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
69
70 template <typename OpTy> bool match(OpTy *V) const {
71 return V->hasOneUse() && SubPattern.match(V);
72 }
73};
74
75template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
76 return SubPattern;
77}
78
79template <typename SubPattern_t, int Flag> struct AllowFmf_match {
80 SubPattern_t SubPattern;
82
83 AllowFmf_match(const SubPattern_t &SP) : SubPattern(SP), FMF(Flag) {}
84
85 template <typename OpTy> bool match(OpTy *V) const {
86 auto *I = dyn_cast<FPMathOperator>(V);
87 return I && ((I->getFastMathFlags() & FMF) == FMF) && SubPattern.match(I);
88 }
89};
90
91template <typename T>
93m_AllowReassoc(const T &SubPattern) {
94 return SubPattern;
95}
96
97template <typename T>
99m_AllowReciprocal(const T &SubPattern) {
100 return SubPattern;
101}
102
103template <typename T>
105m_AllowContract(const T &SubPattern) {
106 return SubPattern;
107}
108
109template <typename T>
111m_ApproxFunc(const T &SubPattern) {
112 return SubPattern;
113}
114
115template <typename T>
117 return SubPattern;
118}
119
120template <typename T>
122 return SubPattern;
123}
124
125template <typename T>
127m_NoSignedZeros(const T &SubPattern) {
128 return SubPattern;
129}
130
131template <typename Class> struct class_match {
132 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
133};
134
135/// Match an arbitrary value and ignore it.
137
138/// Match an arbitrary unary operation and ignore it.
142
143/// Match an arbitrary binary operation and ignore it.
147
148/// Matches any compare instruction and ignore it.
150
151/// Matches any intrinsic call and ignore it.
155
157private:
158 static bool checkAggregate(const ConstantAggregate *CA);
159
160public:
161 static bool check(const Value *V) {
162 if (isa<UndefValue>(V))
163 return true;
164 if (const auto *CA = dyn_cast<ConstantAggregate>(V))
165 return checkAggregate(CA);
166 return false;
167 }
168 template <typename ITy> bool match(ITy *V) const { return check(V); }
169};
170
171/// Match an arbitrary undef constant. This matches poison as well.
172/// If this is an aggregate and contains a non-aggregate element that is
173/// neither undef nor poison, the aggregate is not matched.
174inline auto m_Undef() { return undef_match(); }
175
176/// Match an arbitrary UndefValue constant.
180
181/// Match an arbitrary poison constant.
185
186/// Match an arbitrary Constant and ignore it.
188
189/// Match an arbitrary ConstantInt and ignore it.
193
194/// Match an arbitrary ConstantFP and ignore it.
198
200 template <typename ITy> bool match(ITy *V) const {
201 auto *C = dyn_cast<Constant>(V);
202 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
203 }
204};
205
206/// Match a constant expression or a constant that contains a constant
207/// expression.
209
210template <typename SubPattern_t> struct Splat_match {
211 SubPattern_t SubPattern;
212 Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
213
214 template <typename OpTy> bool match(OpTy *V) const {
215 if (auto *C = dyn_cast<Constant>(V)) {
216 auto *Splat = C->getSplatValue();
217 return Splat ? SubPattern.match(Splat) : false;
218 }
219 // TODO: Extend to other cases (e.g. shufflevectors).
220 return false;
221 }
222};
223
224/// Match a constant splat. TODO: Extend this to non-constant splats.
225template <typename T>
226inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
227 return SubPattern;
228}
229
230/// Match an arbitrary basic block value and ignore it.
234
235/// Inverting matcher
236template <typename Ty> struct match_unless {
237 Ty M;
238
239 match_unless(const Ty &Matcher) : M(Matcher) {}
240
241 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
242};
243
244/// Match if the inner matcher does *NOT* match.
245template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
246 return match_unless<Ty>(M);
247}
248
249/// Matching combinators
250template <typename LTy, typename RTy> struct match_combine_or {
251 LTy L;
252 RTy R;
253
254 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
255
256 template <typename ITy> bool match(ITy *V) const {
257 if (L.match(V))
258 return true;
259 if (R.match(V))
260 return true;
261 return false;
262 }
263};
264
265template <typename LTy, typename RTy> struct match_combine_and {
266 LTy L;
267 RTy R;
268
269 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
270
271 template <typename ITy> bool match(ITy *V) const {
272 if (L.match(V))
273 if (R.match(V))
274 return true;
275 return false;
276 }
277};
278
279/// Combine two pattern matchers matching L || R
280template <typename LTy, typename RTy>
281inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
282 return match_combine_or<LTy, RTy>(L, R);
283}
284
285/// Combine two pattern matchers matching L && R
286template <typename LTy, typename RTy>
287inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
288 return match_combine_and<LTy, RTy>(L, R);
289}
290
291template <typename APTy> struct ap_match {
292 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
294 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
295
296 const APTy *&Res;
298
299 ap_match(const APTy *&Res, bool AllowPoison)
301
302 template <typename ITy> bool match(ITy *V) const {
303 if (auto *CI = dyn_cast<ConstantTy>(V)) {
304 Res = &CI->getValue();
305 return true;
306 }
307 if (V->getType()->isVectorTy())
308 if (const auto *C = dyn_cast<Constant>(V))
309 if (auto *CI =
310 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
311 Res = &CI->getValue();
312 return true;
313 }
314 return false;
315 }
316};
317
318/// Match a ConstantInt or splatted ConstantVector, binding the
319/// specified pointer to the contained APInt.
320inline ap_match<APInt> m_APInt(const APInt *&Res) {
321 // Forbid poison by default to maintain previous behavior.
322 return ap_match<APInt>(Res, /* AllowPoison */ false);
323}
324
325/// Match APInt while allowing poison in splat vector constants.
327 return ap_match<APInt>(Res, /* AllowPoison */ true);
328}
329
330/// Match APInt while forbidding poison in splat vector constants.
332 return ap_match<APInt>(Res, /* AllowPoison */ false);
333}
334
335/// Match a ConstantFP or splatted ConstantVector, binding the
336/// specified pointer to the contained APFloat.
338 // Forbid undefs by default to maintain previous behavior.
339 return ap_match<APFloat>(Res, /* AllowPoison */ false);
340}
341
342/// Match APFloat while allowing poison in splat vector constants.
344 return ap_match<APFloat>(Res, /* AllowPoison */ true);
345}
346
347/// Match APFloat while forbidding poison in splat vector constants.
349 return ap_match<APFloat>(Res, /* AllowPoison */ false);
350}
351
352template <int64_t Val> struct constantint_match {
353 template <typename ITy> bool match(ITy *V) const {
354 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
355 const APInt &CIV = CI->getValue();
356 if (Val >= 0)
357 return CIV == static_cast<uint64_t>(Val);
358 // If Val is negative, and CI is shorter than it, truncate to the right
359 // number of bits. If it is larger, then we have to sign extend. Just
360 // compare their negated values.
361 return -CIV == -Val;
362 }
363 return false;
364 }
365};
366
367/// Match a ConstantInt with a specific value.
368template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
369 return constantint_match<Val>();
370}
371
372/// This helper class is used to match constant scalars, vector splats,
373/// and fixed width vectors that satisfy a specified predicate.
374/// For fixed width vector constants, poison elements are ignored if AllowPoison
375/// is true.
376template <typename Predicate, typename ConstantVal, bool AllowPoison>
377struct cstval_pred_ty : public Predicate {
378private:
379 bool matchVector(const Value *V) const {
380 if (const auto *C = dyn_cast<Constant>(V)) {
381 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
382 return this->isValue(CV->getValue());
383
384 // Number of elements of a scalable vector unknown at compile time
385 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
386 if (!FVTy)
387 return false;
388
389 // Non-splat vector constant: check each element for a match.
390 unsigned NumElts = FVTy->getNumElements();
391 assert(NumElts != 0 && "Constant vector with no elements?");
392 bool HasNonPoisonElements = false;
393 for (unsigned i = 0; i != NumElts; ++i) {
394 Constant *Elt = C->getAggregateElement(i);
395 if (!Elt)
396 return false;
397 if (AllowPoison && isa<PoisonValue>(Elt))
398 continue;
399 auto *CV = dyn_cast<ConstantVal>(Elt);
400 if (!CV || !this->isValue(CV->getValue()))
401 return false;
402 HasNonPoisonElements = true;
403 }
404 return HasNonPoisonElements;
405 }
406 return false;
407 }
408
409public:
410 const Constant **Res = nullptr;
411 template <typename ITy> bool match_impl(ITy *V) const {
412 if (const auto *CV = dyn_cast<ConstantVal>(V))
413 return this->isValue(CV->getValue());
414 if (isa<VectorType>(V->getType()))
415 return matchVector(V);
416 return false;
417 }
418
419 template <typename ITy> bool match(ITy *V) const {
420 if (this->match_impl(V)) {
421 if (Res)
422 *Res = cast<Constant>(V);
423 return true;
424 }
425 return false;
426 }
427};
428
429/// specialization of cstval_pred_ty for ConstantInt
430template <typename Predicate, bool AllowPoison = true>
432
433/// specialization of cstval_pred_ty for ConstantFP
434template <typename Predicate>
436 /*AllowPoison=*/true>;
437
438/// This helper class is used to match scalar and vector constants that
439/// satisfy a specified predicate, and bind them to an APInt.
440template <typename Predicate> struct api_pred_ty : public Predicate {
441 const APInt *&Res;
442
443 api_pred_ty(const APInt *&R) : Res(R) {}
444
445 template <typename ITy> bool match(ITy *V) const {
446 if (const auto *CI = dyn_cast<ConstantInt>(V))
447 if (this->isValue(CI->getValue())) {
448 Res = &CI->getValue();
449 return true;
450 }
451 if (V->getType()->isVectorTy())
452 if (const auto *C = dyn_cast<Constant>(V))
453 if (auto *CI = dyn_cast_or_null<ConstantInt>(
454 C->getSplatValue(/*AllowPoison=*/true)))
455 if (this->isValue(CI->getValue())) {
456 Res = &CI->getValue();
457 return true;
458 }
459
460 return false;
461 }
462};
463
464/// This helper class is used to match scalar and vector constants that
465/// satisfy a specified predicate, and bind them to an APFloat.
466/// Poison is allowed in splat vector constants.
467template <typename Predicate> struct apf_pred_ty : public Predicate {
468 const APFloat *&Res;
469
470 apf_pred_ty(const APFloat *&R) : Res(R) {}
471
472 template <typename ITy> bool match(ITy *V) const {
473 if (const auto *CI = dyn_cast<ConstantFP>(V))
474 if (this->isValue(CI->getValue())) {
475 Res = &CI->getValue();
476 return true;
477 }
478 if (V->getType()->isVectorTy())
479 if (const auto *C = dyn_cast<Constant>(V))
480 if (auto *CI = dyn_cast_or_null<ConstantFP>(
481 C->getSplatValue(/* AllowPoison */ true)))
482 if (this->isValue(CI->getValue())) {
483 Res = &CI->getValue();
484 return true;
485 }
486
487 return false;
488 }
489};
490
491///////////////////////////////////////////////////////////////////////////////
492//
493// Encapsulate constant value queries for use in templated predicate matchers.
494// This allows checking if constants match using compound predicates and works
495// with vector constants, possibly with relaxed constraints. For example, ignore
496// undef values.
497//
498///////////////////////////////////////////////////////////////////////////////
499
500template <typename APTy> struct custom_checkfn {
501 function_ref<bool(const APTy &)> CheckFn;
502 bool isValue(const APTy &C) const { return CheckFn(C); }
503};
504
505/// Match an integer or vector where CheckFn(ele) for each element is true.
506/// For vectors, poison elements are assumed to match.
508m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
509 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
510}
511
513m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
514 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
515}
516
517/// Match a float or vector where CheckFn(ele) for each element is true.
518/// For vectors, poison elements are assumed to match.
520m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
521 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
522}
523
525m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
526 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
527}
528
530 bool isValue(const APInt &C) const { return true; }
531};
532/// Match an integer or vector with any integral constant.
533/// For vectors, this includes constants with undefined elements.
537
539 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
540};
541
545
547 bool isValue(const APInt &C) const { return C.isAllOnes(); }
548};
549/// Match an integer or vector with all bits set.
550/// For vectors, this includes constants with undefined elements.
554
558
560 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
561};
562/// Match an integer or vector with values having all bits except for the high
563/// bit set (0x7f...).
564/// For vectors, this includes constants with undefined elements.
569 return V;
570}
571
573 bool isValue(const APInt &C) const { return C.isNegative(); }
574};
575/// Match an integer or vector of negative values.
576/// For vectors, this includes constants with undefined elements.
580inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
581
583 bool isValue(const APInt &C) const { return C.isNonNegative(); }
584};
585/// Match an integer or vector of non-negative values.
586/// For vectors, this includes constants with undefined elements.
590inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
591
593 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
594};
595/// Match an integer or vector of strictly positive values.
596/// For vectors, this includes constants with undefined elements.
601 return V;
602}
603
605 bool isValue(const APInt &C) const { return C.isNonPositive(); }
606};
607/// Match an integer or vector of non-positive values.
608/// For vectors, this includes constants with undefined elements.
612inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
613
614struct is_one {
615 bool isValue(const APInt &C) const { return C.isOne(); }
616};
617/// Match an integer 1 or a vector with all elements equal to 1.
618/// For vectors, this includes constants with undefined elements.
620
622 bool isValue(const APInt &C) const { return C.isZero(); }
623};
624/// Match an integer 0 or a vector with all elements equal to 0.
625/// For vectors, this includes constants with undefined elements.
629
631 bool isValue(const APInt &C) const { return !C.isZero(); }
632};
633/// Match a non-zero integer or a vector with all non-zero elements.
634/// For vectors, this includes constants with undefined elements.
638
639struct is_zero {
640 template <typename ITy> bool match(ITy *V) const {
641 auto *C = dyn_cast<Constant>(V);
642 // FIXME: this should be able to do something for scalable vectors
643 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
644 }
645};
646/// Match any null constant or a vector with all elements equal to 0.
647/// For vectors, this includes constants with undefined elements.
648inline is_zero m_Zero() { return is_zero(); }
649
650struct is_power2 {
651 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
652};
653/// Match an integer or vector power-of-2.
654/// For vectors, this includes constants with undefined elements.
656inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
657
659 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
660};
661/// Match a integer or vector negated power-of-2.
662/// For vectors, this includes constants with undefined elements.
667 return V;
668}
669
671 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
672};
673/// Match a integer or vector negated power-of-2.
674/// For vectors, this includes constants with undefined elements.
680 return V;
681}
682
684 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
685};
686/// Match an integer or vector of 0 or power-of-2 values.
687/// For vectors, this includes constants with undefined elements.
692 return V;
693}
694
696 bool isValue(const APInt &C) const { return C.isSignMask(); }
697};
698/// Match an integer or vector with only the sign bit(s) set.
699/// For vectors, this includes constants with undefined elements.
703
705 bool isValue(const APInt &C) const { return C.isMask(); }
706};
707/// Match an integer or vector with only the low bit(s) set.
708/// For vectors, this includes constants with undefined elements.
712inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
713
715 bool isValue(const APInt &C) const { return !C || C.isMask(); }
716};
717/// Match an integer or vector with only the low bit(s) set.
718/// For vectors, this includes constants with undefined elements.
723 return V;
724}
725
728 const APInt *Thr;
729 bool isValue(const APInt &C) const {
730 return ICmpInst::compare(C, *Thr, Pred);
731 }
732};
733/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
734/// to Threshold. For vectors, this includes constants with undefined elements.
738 P.Pred = Predicate;
739 P.Thr = &Threshold;
740 return P;
741}
742
743struct is_nan {
744 bool isValue(const APFloat &C) const { return C.isNaN(); }
745};
746/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
747/// For vectors, this includes constants with undefined elements.
749
750struct is_nonnan {
751 bool isValue(const APFloat &C) const { return !C.isNaN(); }
752};
753/// Match a non-NaN FP constant.
754/// For vectors, this includes constants with undefined elements.
758
759struct is_inf {
760 bool isValue(const APFloat &C) const { return C.isInfinity(); }
761};
762/// Match a positive or negative infinity FP constant.
763/// For vectors, this includes constants with undefined elements.
765
766struct is_noninf {
767 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
768};
769/// Match a non-infinity FP constant, i.e. finite or NaN.
770/// For vectors, this includes constants with undefined elements.
774
775struct is_finite {
776 bool isValue(const APFloat &C) const { return C.isFinite(); }
777};
778/// Match a finite FP constant, i.e. not infinity or NaN.
779/// For vectors, this includes constants with undefined elements.
783inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
784
786 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
787};
788/// Match a finite non-zero FP constant.
789/// For vectors, this includes constants with undefined elements.
794 return V;
795}
796
798 bool isValue(const APFloat &C) const { return C.isZero(); }
799};
800/// Match a floating-point negative zero or positive zero.
801/// For vectors, this includes constants with undefined elements.
805
807 bool isValue(const APFloat &C) const { return C.isPosZero(); }
808};
809/// Match a floating-point positive zero.
810/// For vectors, this includes constants with undefined elements.
814
816 bool isValue(const APFloat &C) const { return C.isNegZero(); }
817};
818/// Match a floating-point negative zero.
819/// For vectors, this includes constants with undefined elements.
823
825 bool isValue(const APFloat &C) const { return C.isNonZero(); }
826};
827/// Match a floating-point non-zero.
828/// For vectors, this includes constants with undefined elements.
832
834 bool isValue(const APFloat &C) const {
835 return !C.isDenormal() && C.isNonZero();
836 }
837};
838
839/// Match a floating-point non-zero that is not a denormal.
840/// For vectors, this includes constants with undefined elements.
844
845///////////////////////////////////////////////////////////////////////////////
846
847template <typename Class> struct bind_ty {
848 Class *&VR;
849
850 bind_ty(Class *&V) : VR(V) {}
851
852 template <typename ITy> bool match(ITy *V) const {
853 if (auto *CV = dyn_cast<Class>(V)) {
854 VR = CV;
855 return true;
856 }
857 return false;
858 }
859};
860
861/// Check whether the value has the given Class and matches the nested
862/// pattern. Capture it into the provided variable if successful.
863template <typename Class, typename MatchTy> struct bind_and_match_ty {
864 Class *&VR;
865 MatchTy Match;
866
867 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
868
869 template <typename ITy> bool match(ITy *V) const {
870 auto *CV = dyn_cast<Class>(V);
871 if (CV && Match.match(V)) {
872 VR = CV;
873 return true;
874 }
875 return false;
876 }
877};
878
879/// Match a value, capturing it if we match.
880inline bind_ty<Value> m_Value(Value *&V) { return V; }
881inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
882
883/// Match against the nested pattern, and capture the value if we match.
884template <typename MatchTy>
886 const MatchTy &Match) {
887 return {V, Match};
888}
889
890/// Match against the nested pattern, and capture the value if we match.
891template <typename MatchTy>
893 const MatchTy &Match) {
894 return {V, Match};
895}
896
897/// Match an instruction, capturing it if we match.
900 return I;
901}
902
903/// Match against the nested pattern, and capture the instruction if we match.
904template <typename MatchTy>
906m_Instruction(Instruction *&I, const MatchTy &Match) {
907 return {I, Match};
908}
909template <typename MatchTy>
911m_Instruction(const Instruction *&I, const MatchTy &Match) {
912 return {I, Match};
913}
914
915/// Match a unary operator, capturing it if we match.
918 return I;
919}
920/// Match a binary operator, capturing it if we match.
923 return I;
924}
925/// Match any intrinsic call, capturing it if we match.
930/// Match a with overflow intrinsic, capturing it if we match.
936 return I;
937}
938
939/// Match an UndefValue, capturing the value if we match.
941
942/// Match a Constant, capturing the value if we match.
944
945/// Match a ConstantInt, capturing the value if we match.
947
948/// Match a ConstantFP, capturing the value if we match.
950
951/// Match a ConstantExpr, capturing the value if we match.
953
954/// Match a basic block value, capturing it if we match.
957 return V;
958}
959
960// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
961// and use m_Unless(m_ConstantExpr).
963 template <typename ITy> static bool isImmConstant(ITy *V) {
964 if (auto *CV = dyn_cast<Constant>(V)) {
965 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
966 return true;
967
968 if (CV->getType()->isVectorTy()) {
969 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
970 if (!isa<ConstantExpr>(Splat) &&
971 !Splat->containsConstantExpression()) {
972 return true;
973 }
974 }
975 }
976 }
977 return false;
978 }
979};
980
982 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
983};
984
985/// Match an arbitrary immediate Constant and ignore it.
987
990
992
993 template <typename ITy> bool match(ITy *V) const {
994 if (isImmConstant(V)) {
995 VR = cast<Constant>(V);
996 return true;
997 }
998 return false;
999 }
1000};
1001
1002/// Match an immediate Constant, capturing the value if we match.
1006
1007/// Match a specified Value*.
1009 const Value *Val;
1010
1011 specificval_ty(const Value *V) : Val(V) {}
1012
1013 template <typename ITy> bool match(ITy *V) const { return V == Val; }
1014};
1015
1016/// Match if we have a specific specified value.
1017inline specificval_ty m_Specific(const Value *V) { return V; }
1018
1019/// Stores a reference to the Value *, not the Value * itself,
1020/// thus can be used in commutative matchers.
1021template <typename Class> struct deferredval_ty {
1022 Class *const &Val;
1023
1024 deferredval_ty(Class *const &V) : Val(V) {}
1025
1026 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
1027};
1028
1029/// Like m_Specific(), but works if the specific value to match is determined
1030/// as part of the same match() expression. For example:
1031/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
1032/// bind X before the pattern match starts.
1033/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
1034/// whichever value m_Value(X) populated.
1035inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
1037 return V;
1038}
1039
1040/// Match a specified floating point value or vector of all elements of
1041/// that value.
1043 double Val;
1044
1045 specific_fpval(double V) : Val(V) {}
1046
1047 template <typename ITy> bool match(ITy *V) const {
1048 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1049 return CFP->isExactlyValue(Val);
1050 if (V->getType()->isVectorTy())
1051 if (const auto *C = dyn_cast<Constant>(V))
1052 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1053 return CFP->isExactlyValue(Val);
1054 return false;
1055 }
1056};
1057
1058/// Match a specific floating point value or vector with all elements
1059/// equal to the value.
1060inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1061
1062/// Match a float 1.0 or vector with all elements equal to 1.0.
1063inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1064
1067
1069
1070 template <typename ITy> bool match(ITy *V) const {
1071 const APInt *ConstInt;
1072 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1073 return false;
1074 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
1075 if (!ZExtVal)
1076 return false;
1077 VR = *ZExtVal;
1078 return true;
1079 }
1080};
1081
1082/// Match a specified integer value or vector of all elements of that
1083/// value.
1084template <bool AllowPoison> struct specific_intval {
1085 const APInt &Val;
1086
1087 specific_intval(const APInt &V) : Val(V) {}
1088
1089 template <typename ITy> bool match(ITy *V) const {
1090 const auto *CI = dyn_cast<ConstantInt>(V);
1091 if (!CI && V->getType()->isVectorTy())
1092 if (const auto *C = dyn_cast<Constant>(V))
1093 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1094
1095 return CI && APInt::isSameValue(CI->getValue(), Val);
1096 }
1097};
1098
1099template <bool AllowPoison> struct specific_intval64 {
1101
1103
1104 template <typename ITy> bool match(ITy *V) const {
1105 const auto *CI = dyn_cast<ConstantInt>(V);
1106 if (!CI && V->getType()->isVectorTy())
1107 if (const auto *C = dyn_cast<Constant>(V))
1108 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1109
1110 return CI && CI->getValue() == Val;
1111 }
1112};
1113
1114/// Match a specific integer value or vector with all elements equal to
1115/// the value.
1117 return specific_intval<false>(V);
1118}
1119
1123
1127
1131
1132/// Match a ConstantInt and bind to its value. This does not match
1133/// ConstantInts wider than 64-bits.
1135
1136/// Match a specified basic block value.
1139
1141
1142 template <typename ITy> bool match(ITy *V) const {
1143 const auto *BB = dyn_cast<BasicBlock>(V);
1144 return BB && BB == Val;
1145 }
1146};
1147
1148/// Match a specific basic block value.
1150 return specific_bbval(BB);
1151}
1152
1153/// A commutative-friendly version of m_Specific().
1155 return BB;
1156}
1158m_Deferred(const BasicBlock *const &BB) {
1159 return BB;
1160}
1161
1162//===----------------------------------------------------------------------===//
1163// Matcher for any binary operator.
1164//
1165template <typename LHS_t, typename RHS_t, bool Commutable = false>
1169
1170 // The evaluation order is always stable, regardless of Commutability.
1171 // The LHS is always matched first.
1172 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1173
1174 template <typename OpTy> bool match(OpTy *V) const {
1175 if (auto *I = dyn_cast<BinaryOperator>(V))
1176 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1177 (Commutable && L.match(I->getOperand(1)) &&
1178 R.match(I->getOperand(0)));
1179 return false;
1180 }
1181};
1182
1183template <typename LHS, typename RHS>
1184inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1185 return AnyBinaryOp_match<LHS, RHS>(L, R);
1186}
1187
1188//===----------------------------------------------------------------------===//
1189// Matcher for any unary operator.
1190// TODO fuse unary, binary matcher into n-ary matcher
1191//
1192template <typename OP_t> struct AnyUnaryOp_match {
1193 OP_t X;
1194
1195 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1196
1197 template <typename OpTy> bool match(OpTy *V) const {
1198 if (auto *I = dyn_cast<UnaryOperator>(V))
1199 return X.match(I->getOperand(0));
1200 return false;
1201 }
1202};
1203
1204template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1205 return AnyUnaryOp_match<OP_t>(X);
1206}
1207
1208//===----------------------------------------------------------------------===//
1209// Matchers for specific binary operators.
1210//
1211
1212template <typename LHS_t, typename RHS_t, unsigned Opcode,
1213 bool Commutable = false>
1217
1218 // The evaluation order is always stable, regardless of Commutability.
1219 // The LHS is always matched first.
1220 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1221
1222 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1223 if (V->getValueID() == Value::InstructionVal + Opc) {
1224 auto *I = cast<BinaryOperator>(V);
1225 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1226 (Commutable && L.match(I->getOperand(1)) &&
1227 R.match(I->getOperand(0)));
1228 }
1229 return false;
1230 }
1231
1232 template <typename OpTy> bool match(OpTy *V) const {
1233 return match(Opcode, V);
1234 }
1235};
1236
1237template <typename LHS, typename RHS>
1242
1243template <typename LHS, typename RHS>
1248
1249template <typename LHS, typename RHS>
1254
1255template <typename LHS, typename RHS>
1260
1261template <typename Op_t> struct FNeg_match {
1262 Op_t X;
1263
1264 FNeg_match(const Op_t &Op) : X(Op) {}
1265 template <typename OpTy> bool match(OpTy *V) const {
1266 auto *FPMO = dyn_cast<FPMathOperator>(V);
1267 if (!FPMO)
1268 return false;
1269
1270 if (FPMO->getOpcode() == Instruction::FNeg)
1271 return X.match(FPMO->getOperand(0));
1272
1273 if (FPMO->getOpcode() == Instruction::FSub) {
1274 if (FPMO->hasNoSignedZeros()) {
1275 // With 'nsz', any zero goes.
1276 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1277 return false;
1278 } else {
1279 // Without 'nsz', we need fsub -0.0, X exactly.
1280 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1281 return false;
1282 }
1283
1284 return X.match(FPMO->getOperand(1));
1285 }
1286
1287 return false;
1288 }
1289};
1290
1291/// Match 'fneg X' as 'fsub -0.0, X'.
1292template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1293 return FNeg_match<OpTy>(X);
1294}
1295
1296/// Match 'fneg X' as 'fsub +-0.0, X'.
1297template <typename RHS>
1298inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1299m_FNegNSZ(const RHS &X) {
1300 return m_FSub(m_AnyZeroFP(), X);
1301}
1302
1303template <typename LHS, typename RHS>
1308
1309template <typename LHS, typename RHS>
1314
1315template <typename LHS, typename RHS>
1320
1321template <typename LHS, typename RHS>
1326
1327template <typename LHS, typename RHS>
1332
1333template <typename LHS, typename RHS>
1338
1339template <typename LHS, typename RHS>
1344
1345template <typename LHS, typename RHS>
1350
1351template <typename LHS, typename RHS>
1356
1357template <typename LHS, typename RHS>
1362
1363template <typename LHS, typename RHS>
1368
1369template <typename LHS, typename RHS>
1374
1375template <typename LHS, typename RHS>
1380
1381template <typename LHS, typename RHS>
1386
1387template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1390
1392
1393 template <typename OpTy> bool match(OpTy *V) const {
1394 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1395 if (Op->getOpcode() == Opcode)
1396 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1397 L.match(Op->getOperand(0));
1398 }
1399 // Interpreted as shiftop V, 0
1400 R = 0;
1401 return L.match(V);
1402 }
1403};
1404
1405/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1406template <typename LHS>
1411
1412/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1413template <typename LHS>
1418
1419/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1420template <typename LHS>
1425
1426template <typename LHS_t, typename RHS_t, unsigned Opcode,
1427 unsigned WrapFlags = 0, bool Commutable = false>
1431
1433 : L(LHS), R(RHS) {}
1434
1435 template <typename OpTy> bool match(OpTy *V) const {
1436 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1437 if (Op->getOpcode() != Opcode)
1438 return false;
1440 !Op->hasNoUnsignedWrap())
1441 return false;
1442 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1443 !Op->hasNoSignedWrap())
1444 return false;
1445 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1446 (Commutable && L.match(Op->getOperand(1)) &&
1447 R.match(Op->getOperand(0)));
1448 }
1449 return false;
1450 }
1451};
1452
1453template <typename LHS, typename RHS>
1454inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1456m_NSWAdd(const LHS &L, const RHS &R) {
1457 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1459 R);
1460}
1461template <typename LHS, typename RHS>
1462inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1464m_c_NSWAdd(const LHS &L, const RHS &R) {
1465 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1467 true>(L, R);
1468}
1469template <typename LHS, typename RHS>
1470inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1472m_NSWSub(const LHS &L, const RHS &R) {
1473 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1475 R);
1476}
1477template <typename LHS, typename RHS>
1478inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1480m_NSWMul(const LHS &L, const RHS &R) {
1481 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1483 R);
1484}
1485template <typename LHS, typename RHS>
1486inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1488m_NSWShl(const LHS &L, const RHS &R) {
1489 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1491 R);
1492}
1493
1494template <typename LHS, typename RHS>
1495inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1497m_NUWAdd(const LHS &L, const RHS &R) {
1498 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1500 L, R);
1501}
1502
1503template <typename LHS, typename RHS>
1505 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1506m_c_NUWAdd(const LHS &L, const RHS &R) {
1507 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1509 true>(L, R);
1510}
1511
1512template <typename LHS, typename RHS>
1513inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1515m_NUWSub(const LHS &L, const RHS &R) {
1516 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1518 L, R);
1519}
1520template <typename LHS, typename RHS>
1521inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1523m_NUWMul(const LHS &L, const RHS &R) {
1524 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1526 L, R);
1527}
1528template <typename LHS, typename RHS>
1529inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1531m_NUWShl(const LHS &L, const RHS &R) {
1532 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1534 L, R);
1535}
1536
1537template <typename LHS_t, typename RHS_t, bool Commutable = false>
1539 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1540 unsigned Opcode;
1541
1543 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1544
1545 template <typename OpTy> bool match(OpTy *V) const {
1547 }
1548};
1549
1550/// Matches a specific opcode.
1551template <typename LHS, typename RHS>
1552inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1553 const RHS &R) {
1554 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1555}
1556
1557template <typename LHS, typename RHS, bool Commutable = false>
1561
1562 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1563
1564 template <typename OpTy> bool match(OpTy *V) const {
1565 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1566 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1567 if (!PDI->isDisjoint())
1568 return false;
1569 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1570 (Commutable && L.match(PDI->getOperand(1)) &&
1571 R.match(PDI->getOperand(0)));
1572 }
1573 return false;
1574 }
1575};
1576
1577template <typename LHS, typename RHS>
1579 return DisjointOr_match<LHS, RHS>(L, R);
1580}
1581
1582template <typename LHS, typename RHS>
1584 const RHS &R) {
1586}
1587
1588/// Match either "add" or "or disjoint".
1589template <typename LHS, typename RHS>
1592m_AddLike(const LHS &L, const RHS &R) {
1593 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1594}
1595
1596/// Match either "add nsw" or "or disjoint"
1597template <typename LHS, typename RHS>
1598inline match_combine_or<
1599 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1602m_NSWAddLike(const LHS &L, const RHS &R) {
1603 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1604}
1605
1606/// Match either "add nuw" or "or disjoint"
1607template <typename LHS, typename RHS>
1608inline match_combine_or<
1609 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1612m_NUWAddLike(const LHS &L, const RHS &R) {
1613 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1614}
1615
1616template <typename LHS, typename RHS>
1620
1621 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1622
1623 template <typename OpTy> bool match(OpTy *V) const {
1624 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1625 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1626 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1627 ; // Pass
1628 else if (Op->getOpcode() != Instruction::Xor)
1629 return false;
1630 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1631 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1632 }
1633 return false;
1634 }
1635};
1636
1637/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1638/// Only commutative matcher as the `sub` will need to swap the L and R.
1639template <typename LHS, typename RHS>
1640inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1641 return XorLike_match<LHS, RHS>(L, R);
1642}
1643
1644//===----------------------------------------------------------------------===//
1645// Class that matches a group of binary opcodes.
1646//
1647template <typename LHS_t, typename RHS_t, typename Predicate,
1648 bool Commutable = false>
1649struct BinOpPred_match : Predicate {
1652
1653 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1654
1655 template <typename OpTy> bool match(OpTy *V) const {
1656 if (auto *I = dyn_cast<Instruction>(V))
1657 return this->isOpType(I->getOpcode()) &&
1658 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1659 (Commutable && L.match(I->getOperand(1)) &&
1660 R.match(I->getOperand(0))));
1661 return false;
1662 }
1663};
1664
1666 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1667};
1668
1670 bool isOpType(unsigned Opcode) const {
1671 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1672 }
1673};
1674
1676 bool isOpType(unsigned Opcode) const {
1677 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1678 }
1679};
1680
1682 bool isOpType(unsigned Opcode) const {
1683 return Instruction::isBitwiseLogicOp(Opcode);
1684 }
1685};
1686
1688 bool isOpType(unsigned Opcode) const {
1689 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1690 }
1691};
1692
1694 bool isOpType(unsigned Opcode) const {
1695 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1696 }
1697};
1698
1699/// Matches shift operations.
1700template <typename LHS, typename RHS>
1702 const RHS &R) {
1704}
1705
1706/// Matches logical shift operations.
1707template <typename LHS, typename RHS>
1712
1713/// Matches logical shift operations.
1714template <typename LHS, typename RHS>
1716m_LogicalShift(const LHS &L, const RHS &R) {
1718}
1719
1720/// Matches bitwise logic operations.
1721template <typename LHS, typename RHS>
1723m_BitwiseLogic(const LHS &L, const RHS &R) {
1725}
1726
1727/// Matches bitwise logic operations in either order.
1728template <typename LHS, typename RHS>
1733
1734/// Matches integer division operations.
1735template <typename LHS, typename RHS>
1737 const RHS &R) {
1739}
1740
1741/// Matches integer remainder operations.
1742template <typename LHS, typename RHS>
1744 const RHS &R) {
1746}
1747
1748//===----------------------------------------------------------------------===//
1749// Class that matches exact binary ops.
1750//
1751template <typename SubPattern_t> struct Exact_match {
1752 SubPattern_t SubPattern;
1753
1754 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1755
1756 template <typename OpTy> bool match(OpTy *V) const {
1757 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1758 return PEO->isExact() && SubPattern.match(V);
1759 return false;
1760 }
1761};
1762
1763template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1764 return SubPattern;
1765}
1766
1767//===----------------------------------------------------------------------===//
1768// Matchers for CmpInst classes
1769//
1770
1771template <typename LHS_t, typename RHS_t, typename Class,
1772 bool Commutable = false>
1777
1778 // The evaluation order is always stable, regardless of Commutability.
1779 // The LHS is always matched first.
1781 : Predicate(&Pred), L(LHS), R(RHS) {}
1783 : Predicate(nullptr), L(LHS), R(RHS) {}
1784
1785 template <typename OpTy> bool match(OpTy *V) const {
1786 if (auto *I = dyn_cast<Class>(V)) {
1787 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1788 if (Predicate)
1790 return true;
1791 }
1792 if (Commutable && L.match(I->getOperand(1)) &&
1793 R.match(I->getOperand(0))) {
1794 if (Predicate)
1796 return true;
1797 }
1798 }
1799 return false;
1800 }
1801};
1802
1803template <typename LHS, typename RHS>
1805 const RHS &R) {
1806 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1807}
1808
1809template <typename LHS, typename RHS>
1811 const LHS &L, const RHS &R) {
1812 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1813}
1814
1815template <typename LHS, typename RHS>
1817 const LHS &L, const RHS &R) {
1818 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1819}
1820
1821template <typename LHS, typename RHS>
1824}
1825
1826template <typename LHS, typename RHS>
1829}
1830
1831template <typename LHS, typename RHS>
1834}
1835
1836// Same as CmpClass, but instead of saving Pred as out output variable, match a
1837// specific input pred for equality.
1838template <typename LHS_t, typename RHS_t, typename Class,
1839 bool Commutable = false>
1844
1846 : Predicate(Pred), L(LHS), R(RHS) {}
1847
1848 template <typename OpTy> bool match(OpTy *V) const {
1849 if (auto *I = dyn_cast<Class>(V)) {
1851 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1852 return true;
1853 if constexpr (Commutable) {
1856 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1857 return true;
1858 }
1859 }
1860
1861 return false;
1862 }
1863};
1864
1865template <typename LHS, typename RHS>
1867m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1868 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1869}
1870
1871template <typename LHS, typename RHS>
1873m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1874 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1875}
1876
1877template <typename LHS, typename RHS>
1879m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1881}
1882
1883template <typename LHS, typename RHS>
1885m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1886 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1887}
1888
1889//===----------------------------------------------------------------------===//
1890// Matchers for instructions with a given opcode and number of operands.
1891//
1892
1893/// Matches instructions with Opcode and three operands.
1894template <typename T0, unsigned Opcode> struct OneOps_match {
1896
1897 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1898
1899 template <typename OpTy> bool match(OpTy *V) const {
1900 if (V->getValueID() == Value::InstructionVal + Opcode) {
1901 auto *I = cast<Instruction>(V);
1902 return Op1.match(I->getOperand(0));
1903 }
1904 return false;
1905 }
1906};
1907
1908/// Matches instructions with Opcode and three operands.
1909template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1912
1913 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1914
1915 template <typename OpTy> bool match(OpTy *V) const {
1916 if (V->getValueID() == Value::InstructionVal + Opcode) {
1917 auto *I = cast<Instruction>(V);
1918 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1919 }
1920 return false;
1921 }
1922};
1923
1924/// Matches instructions with Opcode and three operands.
1925template <typename T0, typename T1, typename T2, unsigned Opcode,
1926 bool CommutableOp2Op3 = false>
1931
1932 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1933 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1934
1935 template <typename OpTy> bool match(OpTy *V) const {
1936 if (V->getValueID() == Value::InstructionVal + Opcode) {
1937 auto *I = cast<Instruction>(V);
1938 if (!Op1.match(I->getOperand(0)))
1939 return false;
1940 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1941 return true;
1942 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1943 Op3.match(I->getOperand(1));
1944 }
1945 return false;
1946 }
1947};
1948
1949/// Matches instructions with Opcode and any number of operands
1950template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1951 std::tuple<OperandTypes...> Operands;
1952
1953 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1954
1955 // Operand matching works by recursively calling match_operands, matching the
1956 // operands left to right. The first version is called for each operand but
1957 // the last, for which the second version is called. The second version of
1958 // match_operands is also used to match each individual operand.
1959 template <int Idx, int Last>
1960 std::enable_if_t<Idx != Last, bool>
1964
1965 template <int Idx, int Last>
1966 std::enable_if_t<Idx == Last, bool>
1968 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1969 }
1970
1971 template <typename OpTy> bool match(OpTy *V) const {
1972 if (V->getValueID() == Value::InstructionVal + Opcode) {
1973 auto *I = cast<Instruction>(V);
1974 return I->getNumOperands() == sizeof...(OperandTypes) &&
1975 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1976 }
1977 return false;
1978 }
1979};
1980
1981/// Matches SelectInst.
1982template <typename Cond, typename LHS, typename RHS>
1984m_Select(const Cond &C, const LHS &L, const RHS &R) {
1986}
1987
1988/// This matches a select of two constants, e.g.:
1989/// m_SelectCst<-1, 0>(m_Value(V))
1990template <int64_t L, int64_t R, typename Cond>
1992 Instruction::Select>
1995}
1996
1997/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1998template <typename LHS, typename RHS>
1999inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
2000m_c_Select(const LHS &L, const RHS &R) {
2001 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
2002 true>(m_Value(), L, R);
2003}
2004
2005/// Matches FreezeInst.
2006template <typename OpTy>
2010
2011/// Matches InsertElementInst.
2012template <typename Val_t, typename Elt_t, typename Idx_t>
2014m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
2016 Val, Elt, Idx);
2017}
2018
2019/// Matches ExtractElementInst.
2020template <typename Val_t, typename Idx_t>
2022m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
2024}
2025
2026/// Matches shuffle.
2027template <typename T0, typename T1, typename T2> struct Shuffle_match {
2031
2032 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
2033 : Op1(Op1), Op2(Op2), Mask(Mask) {}
2034
2035 template <typename OpTy> bool match(OpTy *V) const {
2036 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
2037 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
2038 Mask.match(I->getShuffleMask());
2039 }
2040 return false;
2041 }
2042};
2043
2044struct m_Mask {
2047 bool match(ArrayRef<int> Mask) const {
2048 MaskRef = Mask;
2049 return true;
2050 }
2051};
2052
2054 bool match(ArrayRef<int> Mask) const {
2055 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2056 }
2057};
2058
2062 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2063};
2064
2068 bool match(ArrayRef<int> Mask) const {
2069 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2070 if (First == Mask.end())
2071 return false;
2072 SplatIndex = *First;
2073 return all_of(Mask,
2074 [First](int Elem) { return Elem == *First || Elem == -1; });
2075 }
2076};
2077
2078template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2079 PointerOpTy PointerOp;
2080 OffsetOpTy OffsetOp;
2081
2082 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2084
2085 template <typename OpTy> bool match(OpTy *V) const {
2086 auto *GEP = dyn_cast<GEPOperator>(V);
2087 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2088 PointerOp.match(GEP->getPointerOperand()) &&
2089 OffsetOp.match(GEP->idx_begin()->get());
2090 }
2091};
2092
2093/// Matches ShuffleVectorInst independently of mask value.
2094template <typename V1_t, typename V2_t>
2096m_Shuffle(const V1_t &v1, const V2_t &v2) {
2098}
2099
2100template <typename V1_t, typename V2_t, typename Mask_t>
2102m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2104}
2105
2106/// Matches LoadInst.
2107template <typename OpTy>
2111
2112/// Matches StoreInst.
2113template <typename ValueOpTy, typename PointerOpTy>
2115m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2117 PointerOp);
2118}
2119
2120/// Matches GetElementPtrInst.
2121template <typename... OperandTypes>
2122inline auto m_GEP(const OperandTypes &...Ops) {
2123 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2124}
2125
2126/// Matches GEP with i8 source element type
2127template <typename PointerOpTy, typename OffsetOpTy>
2129m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2131}
2132
2133//===----------------------------------------------------------------------===//
2134// Matchers for CastInst classes
2135//
2136
2137template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2138 Op_t Op;
2139
2140 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2141
2142 template <typename OpTy> bool match(OpTy *V) const {
2143 if (auto *O = dyn_cast<Operator>(V))
2144 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2145 return false;
2146 }
2147};
2148
2149template <typename Op_t, typename Class> struct CastInst_match {
2150 Op_t Op;
2151
2152 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2153
2154 template <typename OpTy> bool match(OpTy *V) const {
2155 if (auto *I = dyn_cast<Class>(V))
2156 return Op.match(I->getOperand(0));
2157 return false;
2158 }
2159};
2160
2161template <typename Op_t> struct PtrToIntSameSize_match {
2163 Op_t Op;
2164
2165 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2166 : DL(DL), Op(OpMatch) {}
2167
2168 template <typename OpTy> bool match(OpTy *V) const {
2169 if (auto *O = dyn_cast<Operator>(V))
2170 return O->getOpcode() == Instruction::PtrToInt &&
2171 DL.getTypeSizeInBits(O->getType()) ==
2172 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2173 Op.match(O->getOperand(0));
2174 return false;
2175 }
2176};
2177
2178template <typename Op_t> struct NNegZExt_match {
2179 Op_t Op;
2180
2181 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2182
2183 template <typename OpTy> bool match(OpTy *V) const {
2184 if (auto *I = dyn_cast<ZExtInst>(V))
2185 return I->hasNonNeg() && Op.match(I->getOperand(0));
2186 return false;
2187 }
2188};
2189
2190template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2191 Op_t Op;
2192
2193 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2194
2195 template <typename OpTy> bool match(OpTy *V) const {
2196 if (auto *I = dyn_cast<TruncInst>(V))
2197 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2198 Op.match(I->getOperand(0));
2199 return false;
2200 }
2201};
2202
2203/// Matches BitCast.
2204template <typename OpTy>
2209
2210template <typename Op_t> struct ElementWiseBitCast_match {
2211 Op_t Op;
2212
2213 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2214
2215 template <typename OpTy> bool match(OpTy *V) const {
2216 auto *I = dyn_cast<BitCastInst>(V);
2217 if (!I)
2218 return false;
2219 Type *SrcType = I->getSrcTy();
2220 Type *DstType = I->getType();
2221 // Make sure the bitcast doesn't change between scalar and vector and
2222 // doesn't change the number of vector elements.
2223 if (SrcType->isVectorTy() != DstType->isVectorTy())
2224 return false;
2225 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2226 SrcVecTy && SrcVecTy->getElementCount() !=
2227 cast<VectorType>(DstType)->getElementCount())
2228 return false;
2229 return Op.match(I->getOperand(0));
2230 }
2231};
2232
2233template <typename OpTy>
2237
2238/// Matches PtrToInt.
2239template <typename OpTy>
2244
2245template <typename OpTy>
2250
2251/// Matches PtrToAddr.
2252template <typename OpTy>
2257
2258/// Matches PtrToInt or PtrToAddr.
2259template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2261}
2262
2263/// Matches IntToPtr.
2264template <typename OpTy>
2269
2270/// Matches any cast or self. Used to ignore casts.
2271template <typename OpTy>
2273m_CastOrSelf(const OpTy &Op) {
2275}
2276
2277/// Matches Trunc.
2278template <typename OpTy>
2282
2283/// Matches trunc nuw.
2284template <typename OpTy>
2289
2290/// Matches trunc nsw.
2291template <typename OpTy>
2296
2297template <typename OpTy>
2299m_TruncOrSelf(const OpTy &Op) {
2300 return m_CombineOr(m_Trunc(Op), Op);
2301}
2302
2303/// Matches SExt.
2304template <typename OpTy>
2308
2309/// Matches ZExt.
2310template <typename OpTy>
2314
2315template <typename OpTy>
2317 return NNegZExt_match<OpTy>(Op);
2318}
2319
2320template <typename OpTy>
2322m_ZExtOrSelf(const OpTy &Op) {
2323 return m_CombineOr(m_ZExt(Op), Op);
2324}
2325
2326template <typename OpTy>
2328m_SExtOrSelf(const OpTy &Op) {
2329 return m_CombineOr(m_SExt(Op), Op);
2330}
2331
2332/// Match either "sext" or "zext nneg".
2333template <typename OpTy>
2335m_SExtLike(const OpTy &Op) {
2336 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2337}
2338
2339template <typename OpTy>
2342m_ZExtOrSExt(const OpTy &Op) {
2343 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2344}
2345
2346template <typename OpTy>
2349 OpTy>
2351 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2352}
2353
2354template <typename OpTy>
2357 OpTy>
2360}
2361
2362template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2363 CondTy Cond;
2366
2367 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2368 : Cond(C), TrueC(TC), FalseC(FC) {}
2369
2370 template <typename OpTy> bool match(OpTy *V) const {
2371 // select(Cond, TrueC, FalseC) — captures both constants directly
2373 return true;
2374
2375 Type *Ty = V->getType();
2376 Value *CondV = nullptr;
2377
2378 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2379 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2380 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2381 TrueC.match(ConstantInt::get(Ty, 1)) &&
2382 FalseC.match(ConstantInt::get(Ty, 0)))
2383 return true;
2384
2385 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2386 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2387 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2388 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2389 FalseC.match(ConstantInt::get(Ty, 0)))
2390 return true;
2391
2392 return false;
2393 }
2394};
2395
2396/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2397/// select i1 Cond, TrueC, FalseC
2398/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2399/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2400///
2401/// The condition is matched against \p Cond, and the true/false constants
2402/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2403/// constants are bound to \p TrueC and \p FalseC via their matchers.
2404template <typename CondTy, typename LTy, typename RTy>
2406m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2407 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2408}
2409
2410template <typename OpTy>
2414
2415template <typename OpTy>
2419
2420template <typename OpTy>
2423m_IToFP(const OpTy &Op) {
2424 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2425}
2426
2427template <typename OpTy>
2431
2432template <typename OpTy>
2436
2437template <typename OpTy>
2440m_FPToI(const OpTy &Op) {
2441 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2442}
2443
2444template <typename OpTy>
2448
2449template <typename OpTy>
2453
2454//===----------------------------------------------------------------------===//
2455// Matchers for control flow.
2456//
2457
2458struct br_match {
2460
2462
2463 template <typename OpTy> bool match(OpTy *V) const {
2464 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2465 Succ = BI->getSuccessor();
2466 return true;
2467 }
2468 return false;
2469 }
2470};
2471
2472inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2473
2474template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2476 Cond_t Cond;
2477 TrueBlock_t T;
2478 FalseBlock_t F;
2479
2480 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2481 : Cond(C), T(t), F(f) {}
2482
2483 template <typename OpTy> bool match(OpTy *V) const {
2484 if (auto *BI = dyn_cast<CondBrInst>(V))
2485 if (Cond.match(BI->getCondition()))
2486 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2487 return false;
2488 }
2489};
2490
2491template <typename Cond_t>
2497
2498template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2500m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2502}
2503
2504//===----------------------------------------------------------------------===//
2505// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2506//
2507
2508template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2509 bool Commutable = false>
2511 using PredType = Pred_t;
2514
2515 // The evaluation order is always stable, regardless of Commutability.
2516 // The LHS is always matched first.
2517 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2518
2519 template <typename OpTy> bool match(OpTy *V) const {
2520 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2521 Intrinsic::ID IID = II->getIntrinsicID();
2522 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2523 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2524 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2525 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2526 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2527 return (L.match(LHS) && R.match(RHS)) ||
2528 (Commutable && L.match(RHS) && R.match(LHS));
2529 }
2530 }
2531 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2532 auto *SI = dyn_cast<SelectInst>(V);
2533 if (!SI)
2534 return false;
2535 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2536 if (!Cmp)
2537 return false;
2538 // At this point we have a select conditioned on a comparison. Check that
2539 // it is the values returned by the select that are being compared.
2540 auto *TrueVal = SI->getTrueValue();
2541 auto *FalseVal = SI->getFalseValue();
2542 auto *LHS = Cmp->getOperand(0);
2543 auto *RHS = Cmp->getOperand(1);
2544 if ((TrueVal != LHS || FalseVal != RHS) &&
2545 (TrueVal != RHS || FalseVal != LHS))
2546 return false;
2547 typename CmpInst_t::Predicate Pred =
2548 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2549 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2550 if (!Pred_t::match(Pred))
2551 return false;
2552 // It does! Bind the operands.
2553 return (L.match(LHS) && R.match(RHS)) ||
2554 (Commutable && L.match(RHS) && R.match(LHS));
2555 }
2556};
2557
2558/// Helper class for identifying signed max predicates.
2560 static bool match(ICmpInst::Predicate Pred) {
2561 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2562 }
2563};
2564
2565/// Helper class for identifying signed min predicates.
2567 static bool match(ICmpInst::Predicate Pred) {
2568 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2569 }
2570};
2571
2572/// Helper class for identifying unsigned max predicates.
2574 static bool match(ICmpInst::Predicate Pred) {
2575 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2576 }
2577};
2578
2579/// Helper class for identifying unsigned min predicates.
2581 static bool match(ICmpInst::Predicate Pred) {
2582 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2583 }
2584};
2585
2586/// Helper class for identifying ordered max predicates.
2588 static bool match(FCmpInst::Predicate Pred) {
2589 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2590 }
2591};
2592
2593/// Helper class for identifying ordered min predicates.
2595 static bool match(FCmpInst::Predicate Pred) {
2596 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2597 }
2598};
2599
2600/// Helper class for identifying unordered max predicates.
2602 static bool match(FCmpInst::Predicate Pred) {
2603 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2604 }
2605};
2606
2607/// Helper class for identifying unordered min predicates.
2609 static bool match(FCmpInst::Predicate Pred) {
2610 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2611 }
2612};
2613
2614template <typename LHS, typename RHS>
2619
2620template <typename LHS, typename RHS>
2625
2626template <typename LHS, typename RHS>
2631
2632template <typename LHS, typename RHS>
2637
2638template <typename LHS, typename RHS>
2639inline match_combine_or<
2644m_MaxOrMin(const LHS &L, const RHS &R) {
2645 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2646 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2647}
2648
2649/// Match an 'ordered' floating point maximum function.
2650/// Floating point has one special value 'NaN'. Therefore, there is no total
2651/// order. However, if we can ignore the 'NaN' value (for example, because of a
2652/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2653/// semantics. In the presence of 'NaN' we have to preserve the original
2654/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2655///
2656/// max(L, R) iff L and R are not NaN
2657/// m_OrdFMax(L, R) = R iff L or R are NaN
2658template <typename LHS, typename RHS>
2663
2664/// Match an 'ordered' floating point minimum function.
2665/// Floating point has one special value 'NaN'. Therefore, there is no total
2666/// order. However, if we can ignore the 'NaN' value (for example, because of a
2667/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2668/// semantics. In the presence of 'NaN' we have to preserve the original
2669/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2670///
2671/// min(L, R) iff L and R are not NaN
2672/// m_OrdFMin(L, R) = R iff L or R are NaN
2673template <typename LHS, typename RHS>
2678
2679/// Match an 'unordered' floating point maximum function.
2680/// Floating point has one special value 'NaN'. Therefore, there is no total
2681/// order. However, if we can ignore the 'NaN' value (for example, because of a
2682/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2683/// semantics. In the presence of 'NaN' we have to preserve the original
2684/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2685///
2686/// max(L, R) iff L and R are not NaN
2687/// m_UnordFMax(L, R) = L iff L or R are NaN
2688template <typename LHS, typename RHS>
2690m_UnordFMax(const LHS &L, const RHS &R) {
2692}
2693
2694/// Match an 'unordered' floating point minimum function.
2695/// Floating point has one special value 'NaN'. Therefore, there is no total
2696/// order. However, if we can ignore the 'NaN' value (for example, because of a
2697/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2698/// semantics. In the presence of 'NaN' we have to preserve the original
2699/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2700///
2701/// min(L, R) iff L and R are not NaN
2702/// m_UnordFMin(L, R) = L iff L or R are NaN
2703template <typename LHS, typename RHS>
2705m_UnordFMin(const LHS &L, const RHS &R) {
2707}
2708
2709/// Match an 'ordered' or 'unordered' floating point maximum function.
2710/// Floating point has one special value 'NaN'. Therefore, there is no total
2711/// order. However, if we can ignore the 'NaN' value (for example, because of a
2712/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2713/// semantics.
2714template <typename LHS, typename RHS>
2721
2722/// Match an 'ordered' or 'unordered' floating point minimum function.
2723/// Floating point has one special value 'NaN'. Therefore, there is no total
2724/// order. However, if we can ignore the 'NaN' value (for example, because of a
2725/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2726/// semantics.
2727template <typename LHS, typename RHS>
2734
2735/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2736/// NOTE: we first match the 'Not' (by matching '-1'),
2737/// and only then match the inner matcher!
2738template <typename ValTy>
2739inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2740m_Not(const ValTy &V) {
2741 return m_c_Xor(m_AllOnes(), V);
2742}
2743
2744template <typename ValTy>
2745inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2746 true>
2747m_NotForbidPoison(const ValTy &V) {
2748 return m_c_Xor(m_AllOnesForbidPoison(), V);
2749}
2750
2751//===----------------------------------------------------------------------===//
2752// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2753// Note that S might be matched to other instructions than AddInst.
2754//
2755
2756template <typename LHS_t, typename RHS_t, typename Sum_t>
2760 Sum_t S;
2761
2762 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2763 : L(L), R(R), S(S) {}
2764
2765 template <typename OpTy> bool match(OpTy *V) const {
2766 Value *ICmpLHS, *ICmpRHS;
2767 CmpPredicate Pred;
2768 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2769 return false;
2770
2771 Value *AddLHS, *AddRHS;
2772 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2773
2774 // (a + b) u< a, (a + b) u< b
2775 if (Pred == ICmpInst::ICMP_ULT)
2776 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2777 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2778
2779 // a >u (a + b), b >u (a + b)
2780 if (Pred == ICmpInst::ICMP_UGT)
2781 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2782 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2783
2784 Value *Op1;
2785 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2786 // (~a) <u b
2787 if (Pred == ICmpInst::ICMP_ULT) {
2788 if (XorExpr.match(ICmpLHS))
2789 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2790 }
2791 // b > u (~a)
2792 if (Pred == ICmpInst::ICMP_UGT) {
2793 if (XorExpr.match(ICmpRHS))
2794 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2795 }
2796
2797 // Match special-case for increment-by-1.
2798 if (Pred == ICmpInst::ICMP_EQ) {
2799 // (a + 1) == 0
2800 // (1 + a) == 0
2801 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2802 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2803 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2804 // 0 == (a + 1)
2805 // 0 == (1 + a)
2806 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2807 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2808 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2809 }
2810
2811 return false;
2812 }
2813};
2814
2815/// Match an icmp instruction checking for unsigned overflow on addition.
2816///
2817/// S is matched to the addition whose result is being checked for overflow, and
2818/// L and R are matched to the LHS and RHS of S.
2819template <typename LHS_t, typename RHS_t, typename Sum_t>
2821m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2823}
2824
2825template <typename Opnd_t> struct Argument_match {
2826 unsigned OpI;
2827 Opnd_t Val;
2828
2829 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2830
2831 template <typename OpTy> bool match(OpTy *V) const {
2832 // FIXME: Should likely be switched to use `CallBase`.
2833 if (const auto *CI = dyn_cast<CallInst>(V))
2834 return Val.match(CI->getArgOperand(OpI));
2835 return false;
2836 }
2837};
2838
2839/// Match an argument.
2840template <unsigned OpI, typename Opnd_t>
2841inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2842 return Argument_match<Opnd_t>(OpI, Op);
2843}
2844
2845/// Intrinsic matchers.
2847 unsigned ID;
2848
2850
2851 template <typename OpTy> bool match(OpTy *V) const {
2852 if (const auto *CI = dyn_cast<CallInst>(V))
2853 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2854 return F->getIntrinsicID() == ID;
2855 return false;
2856 }
2857};
2858
2859/// Intrinsic matches are combinations of ID matchers, and argument
2860/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2861/// them with lower arity matchers. Here's some convenient typedefs for up to
2862/// several arguments, and more can be added as needed
2863template <typename T0 = void, typename T1 = void, typename T2 = void,
2864 typename T3 = void, typename T4 = void, typename T5 = void,
2865 typename T6 = void, typename T7 = void, typename T8 = void,
2866 typename T9 = void, typename T10 = void>
2868template <typename T0> struct m_Intrinsic_Ty<T0> {
2870};
2871template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2872 using Ty =
2874};
2875template <typename T0, typename T1, typename T2>
2880template <typename T0, typename T1, typename T2, typename T3>
2885
2886template <typename T0, typename T1, typename T2, typename T3, typename T4>
2891
2892template <typename T0, typename T1, typename T2, typename T3, typename T4,
2893 typename T5>
2898
2899/// Match intrinsic calls like this:
2900/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2901template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2902 return IntrinsicID_match(IntrID);
2903}
2904
2905/// Matches MaskedLoad Intrinsic.
2906template <typename Opnd0, typename Opnd1, typename Opnd2>
2908m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2909 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2910}
2911
2912/// Matches MaskedStore Intrinsic.
2913template <typename Opnd0, typename Opnd1, typename Opnd2>
2915m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2916 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2917}
2918
2919/// Matches MaskedGather Intrinsic.
2920template <typename Opnd0, typename Opnd1, typename Opnd2>
2922m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2923 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2924}
2925
2926template <Intrinsic::ID IntrID, typename T0>
2927inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2929}
2930
2931template <Intrinsic::ID IntrID, typename T0, typename T1>
2932inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2933 const T1 &Op1) {
2935}
2936
2937template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2938inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2939m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2940 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2941}
2942
2943template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2944 typename T3>
2946m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2947 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2948}
2949
2950template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2951 typename T3, typename T4>
2953m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2954 const T4 &Op4) {
2955 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2956 m_Argument<4>(Op4));
2957}
2958
2959template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2960 typename T3, typename T4, typename T5>
2962m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2963 const T4 &Op4, const T5 &Op5) {
2964 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2965 m_Argument<5>(Op5));
2966}
2967
2968// Helper intrinsic matching specializations.
2969template <typename Opnd0>
2970inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2972}
2973
2974template <typename Opnd0>
2975inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2977}
2978
2979template <typename Opnd0>
2980inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2981 return m_Intrinsic<Intrinsic::fabs>(Op0);
2982}
2983
2984template <typename Opnd0>
2985inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2987}
2988
2989template <typename Opnd0, typename Opnd1>
2990inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2991 const Opnd1 &Op1) {
2992 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2993}
2994
2995template <typename Opnd0, typename Opnd1>
2996inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2997 const Opnd1 &Op1) {
2998 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2999}
3000
3001template <typename Opnd0, typename Opnd1>
3003m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3004 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
3005}
3006
3007template <typename Opnd0, typename Opnd1>
3008inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
3009 const Opnd1 &Op1) {
3010 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
3011}
3012
3013template <typename Opnd0, typename Opnd1>
3014inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
3015 const Opnd1 &Op1) {
3016 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
3017}
3018
3019template <typename Opnd0, typename Opnd1>
3021m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3022 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
3023}
3024
3025template <typename Opnd0, typename Opnd1>
3028m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3029 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
3030}
3031
3032template <typename Opnd0, typename Opnd1>
3035m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3036 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
3037}
3038
3039template <typename Opnd0, typename Opnd1, typename Opnd2>
3041m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3042 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
3043}
3044
3045template <typename Opnd0, typename Opnd1, typename Opnd2>
3047m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3048 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
3049}
3050
3051template <typename Opnd0>
3052inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3053 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3054}
3055
3056template <typename Opnd0, typename Opnd1>
3057inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3058 const Opnd1 &Op1) {
3059 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3060}
3061
3062template <typename Opnd0>
3063inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3065}
3066
3067template <typename Opnd0, typename Opnd1, typename Opnd2>
3069m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3070 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3071}
3072
3073//===----------------------------------------------------------------------===//
3074// Matchers for two-operands operators with the operators in either order
3075//
3076
3077/// Matches a BinaryOperator with LHS and RHS in either order.
3078template <typename LHS, typename RHS>
3081}
3082
3083/// Matches an ICmp with a predicate over LHS and RHS in either order.
3084/// Swaps the predicate if operands are commuted.
3085template <typename LHS, typename RHS>
3087m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3089}
3090
3091template <typename LHS, typename RHS>
3096
3097/// Matches a specific opcode with LHS and RHS in either order.
3098template <typename LHS, typename RHS>
3100m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3101 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3102}
3103
3104/// Matches a Add with LHS and RHS in either order.
3105template <typename LHS, typename RHS>
3110
3111/// Matches a Mul with LHS and RHS in either order.
3112template <typename LHS, typename RHS>
3117
3118/// Matches an And with LHS and RHS in either order.
3119template <typename LHS, typename RHS>
3124
3125/// Matches an Or with LHS and RHS in either order.
3126template <typename LHS, typename RHS>
3131
3132/// Matches an Xor with LHS and RHS in either order.
3133template <typename LHS, typename RHS>
3138
3139/// Matches a 'Neg' as 'sub 0, V'.
3140template <typename ValTy>
3141inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3142m_Neg(const ValTy &V) {
3143 return m_Sub(m_ZeroInt(), V);
3144}
3145
3146/// Matches a 'Neg' as 'sub nsw 0, V'.
3147template <typename ValTy>
3149 Instruction::Sub,
3151m_NSWNeg(const ValTy &V) {
3152 return m_NSWSub(m_ZeroInt(), V);
3153}
3154
3155/// Matches an SMin with LHS and RHS in either order.
3156template <typename LHS, typename RHS>
3158m_c_SMin(const LHS &L, const RHS &R) {
3160}
3161/// Matches an SMax with LHS and RHS in either order.
3162template <typename LHS, typename RHS>
3164m_c_SMax(const LHS &L, const RHS &R) {
3166}
3167/// Matches a UMin with LHS and RHS in either order.
3168template <typename LHS, typename RHS>
3170m_c_UMin(const LHS &L, const RHS &R) {
3172}
3173/// Matches a UMax with LHS and RHS in either order.
3174template <typename LHS, typename RHS>
3176m_c_UMax(const LHS &L, const RHS &R) {
3178}
3179
3180template <typename LHS, typename RHS>
3181inline match_combine_or<
3186m_c_MaxOrMin(const LHS &L, const RHS &R) {
3187 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3188 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3189}
3190
3191template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3195
3196 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3197
3198 template <typename OpTy> bool match(OpTy *V) const {
3199 const auto *II = dyn_cast<IntrinsicInst>(V);
3200 if (!II || II->getIntrinsicID() != IntrID)
3201 return false;
3202 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3203 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3204 }
3205};
3206
3207template <Intrinsic::ID IntrID, typename T0, typename T1>
3209m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3211}
3212
3213/// Matches FAdd with LHS and RHS in either order.
3214template <typename LHS, typename RHS>
3216m_c_FAdd(const LHS &L, const RHS &R) {
3218}
3219
3220/// Matches FMul with LHS and RHS in either order.
3221template <typename LHS, typename RHS>
3223m_c_FMul(const LHS &L, const RHS &R) {
3225}
3226
3227template <typename Opnd_t> struct Signum_match {
3228 Opnd_t Val;
3229 Signum_match(const Opnd_t &V) : Val(V) {}
3230
3231 template <typename OpTy> bool match(OpTy *V) const {
3232 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3233 if (TypeSize == 0)
3234 return false;
3235
3236 unsigned ShiftWidth = TypeSize - 1;
3237 Value *Op;
3238
3239 // This is the representation of signum we match:
3240 //
3241 // signum(x) == (x >> 63) | (-x >>u 63)
3242 //
3243 // An i1 value is its own signum, so it's correct to match
3244 //
3245 // signum(x) == (x >> 0) | (-x >>u 0)
3246 //
3247 // for i1 values.
3248
3249 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3250 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3251 auto Signum = m_c_Or(LHS, RHS);
3252
3253 return Signum.match(V) && Val.match(Op);
3254 }
3255};
3256
3257/// Matches a signum pattern.
3258///
3259/// signum(x) =
3260/// x > 0 -> 1
3261/// x == 0 -> 0
3262/// x < 0 -> -1
3263template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3264 return Signum_match<Val_t>(V);
3265}
3266
3267template <int Ind, typename Opnd_t> struct ExtractValue_match {
3268 Opnd_t Val;
3269 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3270
3271 template <typename OpTy> bool match(OpTy *V) const {
3272 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3273 // If Ind is -1, don't inspect indices
3274 if (Ind != -1 &&
3275 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3276 return false;
3277 return Val.match(I->getAggregateOperand());
3278 }
3279 return false;
3280 }
3281};
3282
3283/// Match a single index ExtractValue instruction.
3284/// For example m_ExtractValue<1>(...)
3285template <int Ind, typename Val_t>
3289
3290/// Match an ExtractValue instruction with any index.
3291/// For example m_ExtractValue(...)
3292template <typename Val_t>
3293inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3294 return ExtractValue_match<-1, Val_t>(V);
3295}
3296
3297/// Matcher for a single index InsertValue instruction.
3298template <int Ind, typename T0, typename T1> struct InsertValue_match {
3301
3302 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3303
3304 template <typename OpTy> bool match(OpTy *V) const {
3305 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3306 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3307 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3308 }
3309 return false;
3310 }
3311};
3312
3313/// Matches a single index InsertValue instruction.
3314template <int Ind, typename Val_t, typename Elt_t>
3316 const Elt_t &Elt) {
3317 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3318}
3319
3320/// Matches a call to `llvm.vscale()`.
3322
3323template <typename Opnd0, typename Opnd1>
3325m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3327}
3328
3329template <typename Opnd>
3333
3334template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3338
3339 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3340
3341 template <typename T> bool match(T *V) const {
3342 auto *I = dyn_cast<Instruction>(V);
3343 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3344 return false;
3345
3346 if (I->getOpcode() == Opcode) {
3347 auto *Op0 = I->getOperand(0);
3348 auto *Op1 = I->getOperand(1);
3349 return (L.match(Op0) && R.match(Op1)) ||
3350 (Commutable && L.match(Op1) && R.match(Op0));
3351 }
3352
3353 if (auto *Select = dyn_cast<SelectInst>(I)) {
3354 auto *Cond = Select->getCondition();
3355 auto *TVal = Select->getTrueValue();
3356 auto *FVal = Select->getFalseValue();
3357
3358 // Don't match a scalar select of bool vectors.
3359 // Transforms expect a single type for operands if this matches.
3360 if (Cond->getType() != Select->getType())
3361 return false;
3362
3363 if (Opcode == Instruction::And) {
3364 auto *C = dyn_cast<Constant>(FVal);
3365 if (C && C->isNullValue())
3366 return (L.match(Cond) && R.match(TVal)) ||
3367 (Commutable && L.match(TVal) && R.match(Cond));
3368 } else {
3369 assert(Opcode == Instruction::Or);
3370 auto *C = dyn_cast<Constant>(TVal);
3371 if (C && C->isOneValue())
3372 return (L.match(Cond) && R.match(FVal)) ||
3373 (Commutable && L.match(FVal) && R.match(Cond));
3374 }
3375 }
3376
3377 return false;
3378 }
3379};
3380
3381/// Matches L && R either in the form of L & R or L ? R : false.
3382/// Note that the latter form is poison-blocking.
3383template <typename LHS, typename RHS>
3388
3389/// Matches L && R where L and R are arbitrary values.
3390inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3391
3392/// Matches L && R with LHS and RHS in either order.
3393template <typename LHS, typename RHS>
3395m_c_LogicalAnd(const LHS &L, const RHS &R) {
3397}
3398
3399/// Matches L || R either in the form of L | R or L ? true : R.
3400/// Note that the latter form is poison-blocking.
3401template <typename LHS, typename RHS>
3406
3407/// Matches L || R where L and R are arbitrary values.
3408inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3409
3410/// Matches L || R with LHS and RHS in either order.
3411template <typename LHS, typename RHS>
3413m_c_LogicalOr(const LHS &L, const RHS &R) {
3415}
3416
3417/// Matches either L && R or L || R,
3418/// either one being in the either binary or logical form.
3419/// Note that the latter form is poison-blocking.
3420template <typename LHS, typename RHS, bool Commutable = false>
3426
3427/// Matches either L && R or L || R where L and R are arbitrary values.
3428inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3429
3430/// Matches either L && R or L || R with LHS and RHS in either order.
3431template <typename LHS, typename RHS>
3432inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3433 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3434}
3435
3436} // end namespace PatternMatch
3437} // end namespace llvm
3438
3439#endif // LLVM_IR_PATTERNMATCH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:849
static constexpr unsigned long long mask(BlockVerifier::State S)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define T1
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1567
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h:551
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1291
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
bool isShift() const
A wrapper class for inspecting calls to intrinsic functions.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
'undef' values are things that do not have specified contents.
Definition Constants.h:1606
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
Base class of all SIMD vector types.
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
ShiftLike_match< LHS, Instruction::LShr > m_LShrOrSelf(const LHS &L, uint64_t &R)
Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
AllowFmf_match< T, FastMathFlags::NoSignedZeros > m_NoSignedZeros(const T &SubPattern)
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
AllowFmf_match< T, FastMathFlags::NoInfs > m_NoInfs(const T &SubPattern)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
match_combine_or< typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty, typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty > m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, true > m_c_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, CastInst >, OpTy > m_CastOrSelf(const OpTy &Op)
Matches any cast or self. Used to ignore casts.
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
CommutativeBinaryIntrinsic_match< IntrID, T0, T1 > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
ap_match< APFloat > m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
class_match< IntrinsicInst > m_AnyIntrinsic()
Matches any intrinsic call and ignore it.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_c_XorLike(const LHS &L, const RHS &R)
Match either (xor L, R), (xor R, L) or (sub nuw R, L) iff R.isMask() Only commutative matcher as the ...
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
ap_match< APFloat > m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R, either one being in the either binary or logical form.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, UIToFPInst >, CastInst_match< OpTy, SIToFPInst > > m_IToFP(const OpTy &Op)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > >, OpTy > m_ZExtOrSExtOrSelf(const OpTy &Op)
OneUse_match< T > m_OneUse(const T &SubPattern)
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
Splat_match< T > m_ConstantSplat(const T &SubPattern)
Match a constant splat. TODO: Extend this to non-constant splats.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
ap_match< APInt > m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
AllowFmf_match< T, FastMathFlags::NoNaNs > m_NoNaNs(const T &SubPattern)
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
match_combine_or< CastInst_match< OpTy, FPToUIInst >, CastInst_match< OpTy, FPToSIInst > > m_FPToI(const OpTy &Op)
cst_pred_ty< is_non_zero_int > m_NonZeroInt()
Match a non-zero integer or a vector with all non-zero elements.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
class_match< ConstantFP > m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
AllowFmf_match< T, FastMathFlags::AllowReassoc > m_AllowReassoc(const T &SubPattern)
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_not_denormal_fp > m_NonZeroNotDenormalFP()
Match a floating-point non-zero that is not a denormal.
cst_pred_ty< is_all_ones, false > m_AllOnesForbidPoison()
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
class_match< UndefValue > m_UndefValue()
Match an arbitrary UndefValue constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
AllowFmf_match< T, FastMathFlags::ApproxFunc > m_ApproxFunc(const T &SubPattern)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
ShiftLike_match< LHS, Instruction::AShr > m_AShrOrSelf(const LHS &L, uint64_t &R)
Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedGather Intrinsic.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
ThreeOps_match< Cond, constantint_match< L >, constantint_match< R >, Instruction::Select > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.: m_SelectCst<-1, 0>(m_Value(V))
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
AllowFmf_match< T, FastMathFlags::AllowContract > m_AllowContract(const T &SubPattern)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
match_combine_or< typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty, typename m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty > m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
SpecificCmpClass_match< LHS, RHS, ICmpInst, true > m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
AllowFmf_match< T, FastMathFlags::AllowReciprocal > m_AllowReciprocal(const T &SubPattern)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, TruncInst > >, OpTy > m_ZExtOrTruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
m_Intrinsic_Ty< Opnd >::Ty m_Deinterleave2(const Opnd &Op)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr auto bind_back(FnT &&Fn, BindArgsT &&...BindArgs)
C++23 bind_back.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
AllowFmf_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3, T4 >::Ty, Argument_match< T5 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3 >::Ty, Argument_match< T4 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1 >::Ty, Argument_match< T2 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0 >::Ty, Argument_match< T1 > > Ty
match_combine_and< IntrinsicID_match, Argument_match< T0 > > Ty
Intrinsic matches are combinations of ID matchers, and argument matchers.
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
match_combine_and(const LTy &Left, const RTy &Right)
match_combine_or(const LTy &Left, const RTy &Right)
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying signed max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying signed min predicates.
static bool match(ICmpInst::Predicate Pred)
Match a specified basic block value.
Match a specified floating point value or vector of all elements of that value.
Match a specified integer value or vector of all elements of that value.
Match a specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)