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
766template <bool IsNegative> struct is_signed_inf {
767 bool isValue(const APFloat &C) const {
768 return C.isInfinity() && IsNegative == C.isNegative();
769 }
770};
771
772/// Match a positive infinity FP constant.
773/// For vectors, this includes constants with undefined elements.
777
778/// Match a negative infinity FP constant.
779/// For vectors, this includes constants with undefined elements.
783
784struct is_noninf {
785 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
786};
787/// Match a non-infinity FP constant, i.e. finite or NaN.
788/// For vectors, this includes constants with undefined elements.
792
793struct is_finite {
794 bool isValue(const APFloat &C) const { return C.isFinite(); }
795};
796/// Match a finite FP constant, i.e. not infinity or NaN.
797/// For vectors, this includes constants with undefined elements.
801inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
802
804 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
805};
806/// Match a finite non-zero FP constant.
807/// For vectors, this includes constants with undefined elements.
812 return V;
813}
814
816 bool isValue(const APFloat &C) const { return C.isZero(); }
817};
818/// Match a floating-point negative zero or positive zero.
819/// For vectors, this includes constants with undefined elements.
823
825 bool isValue(const APFloat &C) const { return C.isPosZero(); }
826};
827/// Match a floating-point positive zero.
828/// For vectors, this includes constants with undefined elements.
832
834 bool isValue(const APFloat &C) const { return C.isNegZero(); }
835};
836/// Match a floating-point negative zero.
837/// For vectors, this includes constants with undefined elements.
841
843 bool isValue(const APFloat &C) const { return C.isNonZero(); }
844};
845/// Match a floating-point non-zero.
846/// For vectors, this includes constants with undefined elements.
850
852 bool isValue(const APFloat &C) const {
853 return !C.isDenormal() && C.isNonZero();
854 }
855};
856
857/// Match a floating-point non-zero that is not a denormal.
858/// For vectors, this includes constants with undefined elements.
862
863///////////////////////////////////////////////////////////////////////////////
864
865template <typename Class> struct bind_ty {
866 Class *&VR;
867
868 bind_ty(Class *&V) : VR(V) {}
869
870 template <typename ITy> bool match(ITy *V) const {
871 if (auto *CV = dyn_cast<Class>(V)) {
872 VR = CV;
873 return true;
874 }
875 return false;
876 }
877};
878
879/// Check whether the value has the given Class and matches the nested
880/// pattern. Capture it into the provided variable if successful.
881template <typename Class, typename MatchTy> struct bind_and_match_ty {
882 Class *&VR;
883 MatchTy Match;
884
885 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
886
887 template <typename ITy> bool match(ITy *V) const {
888 auto *CV = dyn_cast<Class>(V);
889 if (CV && Match.match(V)) {
890 VR = CV;
891 return true;
892 }
893 return false;
894 }
895};
896
897/// Match a value, capturing it if we match.
898inline bind_ty<Value> m_Value(Value *&V) { return V; }
899inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
900
901/// Match against the nested pattern, and capture the value if we match.
902template <typename MatchTy>
904 const MatchTy &Match) {
905 return {V, Match};
906}
907
908/// Match against the nested pattern, and capture the value if we match.
909template <typename MatchTy>
911 const MatchTy &Match) {
912 return {V, Match};
913}
914
915/// Match an instruction, capturing it if we match.
918 return I;
919}
920
921/// Match against the nested pattern, and capture the instruction if we match.
922template <typename MatchTy>
924m_Instruction(Instruction *&I, const MatchTy &Match) {
925 return {I, Match};
926}
927template <typename MatchTy>
929m_Instruction(const Instruction *&I, const MatchTy &Match) {
930 return {I, Match};
931}
932
933/// Match a unary operator, capturing it if we match.
936 return I;
937}
938/// Match a binary operator, capturing it if we match.
941 return I;
942}
943/// Match any intrinsic call, capturing it if we match.
948/// Match a with overflow intrinsic, capturing it if we match.
954 return I;
955}
956
957/// Match an UndefValue, capturing the value if we match.
959
960/// Match a Constant, capturing the value if we match.
962
963/// Match a ConstantInt, capturing the value if we match.
965
966/// Match a ConstantFP, capturing the value if we match.
968
969/// Match a ConstantExpr, capturing the value if we match.
971
972/// Match a basic block value, capturing it if we match.
975 return V;
976}
977
978// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
979// and use m_Unless(m_ConstantExpr).
981 template <typename ITy> static bool isImmConstant(ITy *V) {
982 if (auto *CV = dyn_cast<Constant>(V)) {
983 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
984 return true;
985
986 if (CV->getType()->isVectorTy()) {
987 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
988 if (!isa<ConstantExpr>(Splat) &&
989 !Splat->containsConstantExpression()) {
990 return true;
991 }
992 }
993 }
994 }
995 return false;
996 }
997};
998
1000 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
1001};
1002
1003/// Match an arbitrary immediate Constant and ignore it.
1005
1008
1010
1011 template <typename ITy> bool match(ITy *V) const {
1012 if (isImmConstant(V)) {
1013 VR = cast<Constant>(V);
1014 return true;
1015 }
1016 return false;
1017 }
1018};
1019
1020/// Match an immediate Constant, capturing the value if we match.
1024
1025/// Match a specified Value*.
1027 const Value *Val;
1028
1029 specificval_ty(const Value *V) : Val(V) {}
1030
1031 template <typename ITy> bool match(ITy *V) const { return V == Val; }
1032};
1033
1034/// Match if we have a specific specified value.
1035inline specificval_ty m_Specific(const Value *V) { return V; }
1036
1037/// Stores a reference to the Value *, not the Value * itself,
1038/// thus can be used in commutative matchers.
1039template <typename Class> struct deferredval_ty {
1040 Class *const &Val;
1041
1042 deferredval_ty(Class *const &V) : Val(V) {}
1043
1044 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
1045};
1046
1047/// Like m_Specific(), but works if the specific value to match is determined
1048/// as part of the same match() expression. For example:
1049/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
1050/// bind X before the pattern match starts.
1051/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
1052/// whichever value m_Value(X) populated.
1053inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
1055 return V;
1056}
1057
1058/// Match a specified floating point value or vector of all elements of
1059/// that value.
1061 double Val;
1062
1063 specific_fpval(double V) : Val(V) {}
1064
1065 template <typename ITy> bool match(ITy *V) const {
1066 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1067 return CFP->isExactlyValue(Val);
1068 if (V->getType()->isVectorTy())
1069 if (const auto *C = dyn_cast<Constant>(V))
1070 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1071 return CFP->isExactlyValue(Val);
1072 return false;
1073 }
1074};
1075
1076/// Match a specific floating point value or vector with all elements
1077/// equal to the value.
1078inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1079
1080/// Match a float 1.0 or vector with all elements equal to 1.0.
1081inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1082
1085
1087
1088 template <typename ITy> bool match(ITy *V) const {
1089 const APInt *ConstInt;
1090 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1091 return false;
1092 std::optional<uint64_t> ZExtVal = ConstInt->tryZExtValue();
1093 if (!ZExtVal)
1094 return false;
1095 VR = *ZExtVal;
1096 return true;
1097 }
1098};
1099
1100/// Match a specified integer value or vector of all elements of that
1101/// value.
1102template <bool AllowPoison> struct specific_intval {
1103 const APInt &Val;
1104
1105 specific_intval(const APInt &V) : Val(V) {}
1106
1107 template <typename ITy> bool match(ITy *V) const {
1108 const auto *CI = dyn_cast<ConstantInt>(V);
1109 if (!CI && V->getType()->isVectorTy())
1110 if (const auto *C = dyn_cast<Constant>(V))
1111 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1112
1113 return CI && APInt::isSameValue(CI->getValue(), Val);
1114 }
1115};
1116
1117template <bool AllowPoison> struct specific_intval64 {
1119
1121
1122 template <typename ITy> bool match(ITy *V) const {
1123 const auto *CI = dyn_cast<ConstantInt>(V);
1124 if (!CI && V->getType()->isVectorTy())
1125 if (const auto *C = dyn_cast<Constant>(V))
1126 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1127
1128 return CI && CI->getValue() == Val;
1129 }
1130};
1131
1132/// Match a specific integer value or vector with all elements equal to
1133/// the value.
1135 return specific_intval<false>(V);
1136}
1137
1141
1145
1149
1150/// Match a ConstantInt and bind to its value. This does not match
1151/// ConstantInts wider than 64-bits.
1153
1154/// Match a specified basic block value.
1157
1159
1160 template <typename ITy> bool match(ITy *V) const {
1161 const auto *BB = dyn_cast<BasicBlock>(V);
1162 return BB && BB == Val;
1163 }
1164};
1165
1166/// Match a specific basic block value.
1168 return specific_bbval(BB);
1169}
1170
1171/// A commutative-friendly version of m_Specific().
1173 return BB;
1174}
1176m_Deferred(const BasicBlock *const &BB) {
1177 return BB;
1178}
1179
1180//===----------------------------------------------------------------------===//
1181// Matcher for any binary operator.
1182//
1183template <typename LHS_t, typename RHS_t, bool Commutable = false>
1187
1188 // The evaluation order is always stable, regardless of Commutability.
1189 // The LHS is always matched first.
1190 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1191
1192 template <typename OpTy> bool match(OpTy *V) const {
1193 if (auto *I = dyn_cast<BinaryOperator>(V))
1194 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1195 (Commutable && L.match(I->getOperand(1)) &&
1196 R.match(I->getOperand(0)));
1197 return false;
1198 }
1199};
1200
1201template <typename LHS, typename RHS>
1202inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1203 return AnyBinaryOp_match<LHS, RHS>(L, R);
1204}
1205
1206//===----------------------------------------------------------------------===//
1207// Matcher for any unary operator.
1208// TODO fuse unary, binary matcher into n-ary matcher
1209//
1210template <typename OP_t> struct AnyUnaryOp_match {
1211 OP_t X;
1212
1213 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1214
1215 template <typename OpTy> bool match(OpTy *V) const {
1216 if (auto *I = dyn_cast<UnaryOperator>(V))
1217 return X.match(I->getOperand(0));
1218 return false;
1219 }
1220};
1221
1222template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1223 return AnyUnaryOp_match<OP_t>(X);
1224}
1225
1226//===----------------------------------------------------------------------===//
1227// Matchers for specific binary operators.
1228//
1229
1230template <typename LHS_t, typename RHS_t, unsigned Opcode,
1231 bool Commutable = false>
1235
1236 // The evaluation order is always stable, regardless of Commutability.
1237 // The LHS is always matched first.
1238 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1239
1240 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1241 if (V->getValueID() == Value::InstructionVal + Opc) {
1242 auto *I = cast<BinaryOperator>(V);
1243 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1244 (Commutable && L.match(I->getOperand(1)) &&
1245 R.match(I->getOperand(0)));
1246 }
1247 return false;
1248 }
1249
1250 template <typename OpTy> bool match(OpTy *V) const {
1251 return match(Opcode, V);
1252 }
1253};
1254
1255template <typename LHS, typename RHS>
1260
1261template <typename LHS, typename RHS>
1266
1267template <typename LHS, typename RHS>
1272
1273template <typename LHS, typename RHS>
1278
1279template <typename Op_t> struct FNeg_match {
1280 Op_t X;
1281
1282 FNeg_match(const Op_t &Op) : X(Op) {}
1283 template <typename OpTy> bool match(OpTy *V) const {
1284 auto *FPMO = dyn_cast<FPMathOperator>(V);
1285 if (!FPMO)
1286 return false;
1287
1288 if (FPMO->getOpcode() == Instruction::FNeg)
1289 return X.match(FPMO->getOperand(0));
1290
1291 if (FPMO->getOpcode() == Instruction::FSub) {
1292 if (FPMO->hasNoSignedZeros()) {
1293 // With 'nsz', any zero goes.
1294 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1295 return false;
1296 } else {
1297 // Without 'nsz', we need fsub -0.0, X exactly.
1298 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1299 return false;
1300 }
1301
1302 return X.match(FPMO->getOperand(1));
1303 }
1304
1305 return false;
1306 }
1307};
1308
1309/// Match 'fneg X' as 'fsub -0.0, X'.
1310template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1311 return FNeg_match<OpTy>(X);
1312}
1313
1314/// Match 'fneg X' as 'fsub +-0.0, X'.
1315template <typename RHS>
1316inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1317m_FNegNSZ(const RHS &X) {
1318 return m_FSub(m_AnyZeroFP(), X);
1319}
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, typename RHS>
1392
1393template <typename LHS, typename RHS>
1398
1399template <typename LHS, typename RHS>
1404
1405template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1408
1410
1411 template <typename OpTy> bool match(OpTy *V) const {
1412 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1413 if (Op->getOpcode() == Opcode)
1414 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1415 L.match(Op->getOperand(0));
1416 }
1417 // Interpreted as shiftop V, 0
1418 R = 0;
1419 return L.match(V);
1420 }
1421};
1422
1423/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1424template <typename LHS>
1429
1430/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1431template <typename LHS>
1436
1437/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1438template <typename LHS>
1443
1444template <typename LHS_t, typename RHS_t, unsigned Opcode,
1445 unsigned WrapFlags = 0, bool Commutable = false>
1449
1451 : L(LHS), R(RHS) {}
1452
1453 template <typename OpTy> bool match(OpTy *V) const {
1454 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1455 if (Op->getOpcode() != Opcode)
1456 return false;
1458 !Op->hasNoUnsignedWrap())
1459 return false;
1460 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1461 !Op->hasNoSignedWrap())
1462 return false;
1463 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1464 (Commutable && L.match(Op->getOperand(1)) &&
1465 R.match(Op->getOperand(0)));
1466 }
1467 return false;
1468 }
1469};
1470
1471template <typename LHS, typename RHS>
1472inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1474m_NSWAdd(const LHS &L, const RHS &R) {
1475 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1477 R);
1478}
1479template <typename LHS, typename RHS>
1480inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1482m_c_NSWAdd(const LHS &L, const RHS &R) {
1483 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1485 true>(L, R);
1486}
1487template <typename LHS, typename RHS>
1488inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1490m_NSWSub(const LHS &L, const RHS &R) {
1491 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1493 R);
1494}
1495template <typename LHS, typename RHS>
1496inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1498m_NSWMul(const LHS &L, const RHS &R) {
1499 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1501 R);
1502}
1503template <typename LHS, typename RHS>
1504inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1506m_NSWShl(const LHS &L, const RHS &R) {
1507 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1509 R);
1510}
1511
1512template <typename LHS, typename RHS>
1513inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1515m_NUWAdd(const LHS &L, const RHS &R) {
1516 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1518 L, R);
1519}
1520
1521template <typename LHS, typename RHS>
1523 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1524m_c_NUWAdd(const LHS &L, const RHS &R) {
1525 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1527 true>(L, R);
1528}
1529
1530template <typename LHS, typename RHS>
1531inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1533m_NUWSub(const LHS &L, const RHS &R) {
1534 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1536 L, R);
1537}
1538template <typename LHS, typename RHS>
1539inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1541m_NUWMul(const LHS &L, const RHS &R) {
1542 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1544 L, R);
1545}
1546template <typename LHS, typename RHS>
1547inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1549m_NUWShl(const LHS &L, const RHS &R) {
1550 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1552 L, R);
1553}
1554
1555template <typename LHS_t, typename RHS_t, bool Commutable = false>
1557 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1558 unsigned Opcode;
1559
1561 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1562
1563 template <typename OpTy> bool match(OpTy *V) const {
1565 }
1566};
1567
1568/// Matches a specific opcode.
1569template <typename LHS, typename RHS>
1570inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1571 const RHS &R) {
1572 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1573}
1574
1575template <typename LHS, typename RHS, bool Commutable = false>
1579
1580 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1581
1582 template <typename OpTy> bool match(OpTy *V) const {
1583 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1584 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1585 if (!PDI->isDisjoint())
1586 return false;
1587 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1588 (Commutable && L.match(PDI->getOperand(1)) &&
1589 R.match(PDI->getOperand(0)));
1590 }
1591 return false;
1592 }
1593};
1594
1595template <typename LHS, typename RHS>
1597 return DisjointOr_match<LHS, RHS>(L, R);
1598}
1599
1600template <typename LHS, typename RHS>
1602 const RHS &R) {
1604}
1605
1606/// Match either "add" or "or disjoint".
1607template <typename LHS, typename RHS>
1610m_AddLike(const LHS &L, const RHS &R) {
1611 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1612}
1613
1614/// Match either "add nsw" or "or disjoint"
1615template <typename LHS, typename RHS>
1616inline match_combine_or<
1617 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1620m_NSWAddLike(const LHS &L, const RHS &R) {
1621 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1622}
1623
1624/// Match either "add nuw" or "or disjoint"
1625template <typename LHS, typename RHS>
1626inline match_combine_or<
1627 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1630m_NUWAddLike(const LHS &L, const RHS &R) {
1631 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1632}
1633
1634template <typename LHS, typename RHS>
1638
1639 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1640
1641 template <typename OpTy> bool match(OpTy *V) const {
1642 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1643 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1644 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1645 ; // Pass
1646 else if (Op->getOpcode() != Instruction::Xor)
1647 return false;
1648 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1649 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1650 }
1651 return false;
1652 }
1653};
1654
1655/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1656/// Only commutative matcher as the `sub` will need to swap the L and R.
1657template <typename LHS, typename RHS>
1658inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1659 return XorLike_match<LHS, RHS>(L, R);
1660}
1661
1662//===----------------------------------------------------------------------===//
1663// Class that matches a group of binary opcodes.
1664//
1665template <typename LHS_t, typename RHS_t, typename Predicate,
1666 bool Commutable = false>
1667struct BinOpPred_match : Predicate {
1670
1671 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1672
1673 template <typename OpTy> bool match(OpTy *V) const {
1674 if (auto *I = dyn_cast<Instruction>(V))
1675 return this->isOpType(I->getOpcode()) &&
1676 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1677 (Commutable && L.match(I->getOperand(1)) &&
1678 R.match(I->getOperand(0))));
1679 return false;
1680 }
1681};
1682
1684 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1685};
1686
1688 bool isOpType(unsigned Opcode) const {
1689 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1690 }
1691};
1692
1694 bool isOpType(unsigned Opcode) const {
1695 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1696 }
1697};
1698
1700 bool isOpType(unsigned Opcode) const {
1701 return Instruction::isBitwiseLogicOp(Opcode);
1702 }
1703};
1704
1706 bool isOpType(unsigned Opcode) const {
1707 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1708 }
1709};
1710
1712 bool isOpType(unsigned Opcode) const {
1713 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1714 }
1715};
1716
1717/// Matches shift operations.
1718template <typename LHS, typename RHS>
1720 const RHS &R) {
1722}
1723
1724/// Matches logical shift operations.
1725template <typename LHS, typename RHS>
1730
1731/// Matches logical shift operations.
1732template <typename LHS, typename RHS>
1734m_LogicalShift(const LHS &L, const RHS &R) {
1736}
1737
1738/// Matches bitwise logic operations.
1739template <typename LHS, typename RHS>
1741m_BitwiseLogic(const LHS &L, const RHS &R) {
1743}
1744
1745/// Matches bitwise logic operations in either order.
1746template <typename LHS, typename RHS>
1751
1752/// Matches integer division operations.
1753template <typename LHS, typename RHS>
1755 const RHS &R) {
1757}
1758
1759/// Matches integer remainder operations.
1760template <typename LHS, typename RHS>
1762 const RHS &R) {
1764}
1765
1766//===----------------------------------------------------------------------===//
1767// Class that matches exact binary ops.
1768//
1769template <typename SubPattern_t> struct Exact_match {
1770 SubPattern_t SubPattern;
1771
1772 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1773
1774 template <typename OpTy> bool match(OpTy *V) const {
1775 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1776 return PEO->isExact() && SubPattern.match(V);
1777 return false;
1778 }
1779};
1780
1781template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1782 return SubPattern;
1783}
1784
1785//===----------------------------------------------------------------------===//
1786// Matchers for CmpInst classes
1787//
1788
1789template <typename LHS_t, typename RHS_t, typename Class,
1790 bool Commutable = false>
1795
1796 // The evaluation order is always stable, regardless of Commutability.
1797 // The LHS is always matched first.
1799 : Predicate(&Pred), L(LHS), R(RHS) {}
1801 : Predicate(nullptr), L(LHS), R(RHS) {}
1802
1803 template <typename OpTy> bool match(OpTy *V) const {
1804 if (auto *I = dyn_cast<Class>(V)) {
1805 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1806 if (Predicate)
1808 return true;
1809 }
1810 if (Commutable && L.match(I->getOperand(1)) &&
1811 R.match(I->getOperand(0))) {
1812 if (Predicate)
1814 return true;
1815 }
1816 }
1817 return false;
1818 }
1819};
1820
1821template <typename LHS, typename RHS>
1823 const RHS &R) {
1824 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1825}
1826
1827template <typename LHS, typename RHS>
1829 const LHS &L, const RHS &R) {
1830 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1831}
1832
1833template <typename LHS, typename RHS>
1835 const LHS &L, const RHS &R) {
1836 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1837}
1838
1839template <typename LHS, typename RHS>
1842}
1843
1844template <typename LHS, typename RHS>
1847}
1848
1849template <typename LHS, typename RHS>
1852}
1853
1854// Same as CmpClass, but instead of saving Pred as out output variable, match a
1855// specific input pred for equality.
1856template <typename LHS_t, typename RHS_t, typename Class,
1857 bool Commutable = false>
1862
1864 : Predicate(Pred), L(LHS), R(RHS) {}
1865
1866 template <typename OpTy> bool match(OpTy *V) const {
1867 if (auto *I = dyn_cast<Class>(V)) {
1869 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1870 return true;
1871 if constexpr (Commutable) {
1874 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1875 return true;
1876 }
1877 }
1878
1879 return false;
1880 }
1881};
1882
1883template <typename LHS, typename RHS>
1885m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1886 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1887}
1888
1889template <typename LHS, typename RHS>
1891m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1892 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1893}
1894
1895template <typename LHS, typename RHS>
1897m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1899}
1900
1901template <typename LHS, typename RHS>
1903m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1904 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1905}
1906
1907//===----------------------------------------------------------------------===//
1908// Matchers for instructions with a given opcode and number of operands.
1909//
1910
1911/// Matches instructions with Opcode and three operands.
1912template <typename T0, unsigned Opcode> struct OneOps_match {
1914
1915 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1916
1917 template <typename OpTy> bool match(OpTy *V) const {
1918 if (V->getValueID() == Value::InstructionVal + Opcode) {
1919 auto *I = cast<Instruction>(V);
1920 return Op1.match(I->getOperand(0));
1921 }
1922 return false;
1923 }
1924};
1925
1926/// Matches instructions with Opcode and three operands.
1927template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1930
1931 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1932
1933 template <typename OpTy> bool match(OpTy *V) const {
1934 if (V->getValueID() == Value::InstructionVal + Opcode) {
1935 auto *I = cast<Instruction>(V);
1936 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1937 }
1938 return false;
1939 }
1940};
1941
1942/// Matches instructions with Opcode and three operands.
1943template <typename T0, typename T1, typename T2, unsigned Opcode,
1944 bool CommutableOp2Op3 = false>
1949
1950 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1951 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1952
1953 template <typename OpTy> bool match(OpTy *V) const {
1954 if (V->getValueID() == Value::InstructionVal + Opcode) {
1955 auto *I = cast<Instruction>(V);
1956 if (!Op1.match(I->getOperand(0)))
1957 return false;
1958 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1959 return true;
1960 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1961 Op3.match(I->getOperand(1));
1962 }
1963 return false;
1964 }
1965};
1966
1967/// Matches instructions with Opcode and any number of operands
1968template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1969 std::tuple<OperandTypes...> Operands;
1970
1971 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1972
1973 // Operand matching works by recursively calling match_operands, matching the
1974 // operands left to right. The first version is called for each operand but
1975 // the last, for which the second version is called. The second version of
1976 // match_operands is also used to match each individual operand.
1977 template <int Idx, int Last>
1978 std::enable_if_t<Idx != Last, bool>
1982
1983 template <int Idx, int Last>
1984 std::enable_if_t<Idx == Last, bool>
1986 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1987 }
1988
1989 template <typename OpTy> bool match(OpTy *V) const {
1990 if (V->getValueID() == Value::InstructionVal + Opcode) {
1991 auto *I = cast<Instruction>(V);
1992 return I->getNumOperands() == sizeof...(OperandTypes) &&
1993 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1994 }
1995 return false;
1996 }
1997};
1998
1999/// Matches SelectInst.
2000template <typename Cond, typename LHS, typename RHS>
2002m_Select(const Cond &C, const LHS &L, const RHS &R) {
2004}
2005
2006/// This matches a select of two constants, e.g.:
2007/// m_SelectCst<-1, 0>(m_Value(V))
2008template <int64_t L, int64_t R, typename Cond>
2010 Instruction::Select>
2013}
2014
2015/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
2016template <typename LHS, typename RHS>
2017inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
2018m_c_Select(const LHS &L, const RHS &R) {
2019 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
2020 true>(m_Value(), L, R);
2021}
2022
2023/// Matches FreezeInst.
2024template <typename OpTy>
2028
2029/// Matches InsertElementInst.
2030template <typename Val_t, typename Elt_t, typename Idx_t>
2032m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
2034 Val, Elt, Idx);
2035}
2036
2037/// Matches ExtractElementInst.
2038template <typename Val_t, typename Idx_t>
2040m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
2042}
2043
2044/// Matches shuffle.
2045template <typename T0, typename T1, typename T2> struct Shuffle_match {
2049
2050 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
2051 : Op1(Op1), Op2(Op2), Mask(Mask) {}
2052
2053 template <typename OpTy> bool match(OpTy *V) const {
2054 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
2055 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
2056 Mask.match(I->getShuffleMask());
2057 }
2058 return false;
2059 }
2060};
2061
2062struct m_Mask {
2065 bool match(ArrayRef<int> Mask) const {
2066 MaskRef = Mask;
2067 return true;
2068 }
2069};
2070
2072 bool match(ArrayRef<int> Mask) const {
2073 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2074 }
2075};
2076
2080 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2081};
2082
2086 bool match(ArrayRef<int> Mask) const {
2087 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2088 if (First == Mask.end())
2089 return false;
2090 SplatIndex = *First;
2091 return all_of(Mask,
2092 [First](int Elem) { return Elem == *First || Elem == -1; });
2093 }
2094};
2095
2096template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2097 PointerOpTy PointerOp;
2098 OffsetOpTy OffsetOp;
2099
2100 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2102
2103 template <typename OpTy> bool match(OpTy *V) const {
2104 auto *GEP = dyn_cast<GEPOperator>(V);
2105 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2106 PointerOp.match(GEP->getPointerOperand()) &&
2107 OffsetOp.match(GEP->idx_begin()->get());
2108 }
2109};
2110
2111/// Matches ShuffleVectorInst independently of mask value.
2112template <typename V1_t, typename V2_t>
2114m_Shuffle(const V1_t &v1, const V2_t &v2) {
2116}
2117
2118template <typename V1_t, typename V2_t, typename Mask_t>
2120m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2122}
2123
2124/// Matches LoadInst.
2125template <typename OpTy>
2129
2130/// Matches StoreInst.
2131template <typename ValueOpTy, typename PointerOpTy>
2133m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2135 PointerOp);
2136}
2137
2138/// Matches GetElementPtrInst.
2139template <typename... OperandTypes>
2140inline auto m_GEP(const OperandTypes &...Ops) {
2141 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2142}
2143
2144/// Matches GEP with i8 source element type
2145template <typename PointerOpTy, typename OffsetOpTy>
2147m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2149}
2150
2151//===----------------------------------------------------------------------===//
2152// Matchers for CastInst classes
2153//
2154
2155template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2156 Op_t Op;
2157
2158 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2159
2160 template <typename OpTy> bool match(OpTy *V) const {
2161 if (auto *O = dyn_cast<Operator>(V))
2162 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2163 return false;
2164 }
2165};
2166
2167template <typename Op_t, typename Class> struct CastInst_match {
2168 Op_t Op;
2169
2170 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2171
2172 template <typename OpTy> bool match(OpTy *V) const {
2173 if (auto *I = dyn_cast<Class>(V))
2174 return Op.match(I->getOperand(0));
2175 return false;
2176 }
2177};
2178
2179template <typename Op_t> struct PtrToIntSameSize_match {
2181 Op_t Op;
2182
2183 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2184 : DL(DL), Op(OpMatch) {}
2185
2186 template <typename OpTy> bool match(OpTy *V) const {
2187 if (auto *O = dyn_cast<Operator>(V))
2188 return O->getOpcode() == Instruction::PtrToInt &&
2189 DL.getTypeSizeInBits(O->getType()) ==
2190 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2191 Op.match(O->getOperand(0));
2192 return false;
2193 }
2194};
2195
2196template <typename Op_t> struct NNegZExt_match {
2197 Op_t Op;
2198
2199 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2200
2201 template <typename OpTy> bool match(OpTy *V) const {
2202 if (auto *I = dyn_cast<ZExtInst>(V))
2203 return I->hasNonNeg() && Op.match(I->getOperand(0));
2204 return false;
2205 }
2206};
2207
2208template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2209 Op_t Op;
2210
2211 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2212
2213 template <typename OpTy> bool match(OpTy *V) const {
2214 if (auto *I = dyn_cast<TruncInst>(V))
2215 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2216 Op.match(I->getOperand(0));
2217 return false;
2218 }
2219};
2220
2221/// Matches BitCast.
2222template <typename OpTy>
2227
2228template <typename Op_t> struct ElementWiseBitCast_match {
2229 Op_t Op;
2230
2231 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2232
2233 template <typename OpTy> bool match(OpTy *V) const {
2234 auto *I = dyn_cast<BitCastInst>(V);
2235 if (!I)
2236 return false;
2237 Type *SrcType = I->getSrcTy();
2238 Type *DstType = I->getType();
2239 // Make sure the bitcast doesn't change between scalar and vector and
2240 // doesn't change the number of vector elements.
2241 if (SrcType->isVectorTy() != DstType->isVectorTy())
2242 return false;
2243 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2244 SrcVecTy && SrcVecTy->getElementCount() !=
2245 cast<VectorType>(DstType)->getElementCount())
2246 return false;
2247 return Op.match(I->getOperand(0));
2248 }
2249};
2250
2251template <typename OpTy>
2255
2256/// Matches PtrToInt.
2257template <typename OpTy>
2262
2263template <typename OpTy>
2268
2269/// Matches PtrToAddr.
2270template <typename OpTy>
2275
2276/// Matches PtrToInt or PtrToAddr.
2277template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2279}
2280
2281/// Matches IntToPtr.
2282template <typename OpTy>
2287
2288/// Matches any cast or self. Used to ignore casts.
2289template <typename OpTy>
2291m_CastOrSelf(const OpTy &Op) {
2293}
2294
2295/// Matches Trunc.
2296template <typename OpTy>
2300
2301/// Matches trunc nuw.
2302template <typename OpTy>
2307
2308/// Matches trunc nsw.
2309template <typename OpTy>
2314
2315template <typename OpTy>
2317m_TruncOrSelf(const OpTy &Op) {
2318 return m_CombineOr(m_Trunc(Op), Op);
2319}
2320
2321/// Matches SExt.
2322template <typename OpTy>
2326
2327/// Matches ZExt.
2328template <typename OpTy>
2332
2333template <typename OpTy>
2335 return NNegZExt_match<OpTy>(Op);
2336}
2337
2338template <typename OpTy>
2340m_ZExtOrSelf(const OpTy &Op) {
2341 return m_CombineOr(m_ZExt(Op), Op);
2342}
2343
2344template <typename OpTy>
2346m_SExtOrSelf(const OpTy &Op) {
2347 return m_CombineOr(m_SExt(Op), Op);
2348}
2349
2350/// Match either "sext" or "zext nneg".
2351template <typename OpTy>
2353m_SExtLike(const OpTy &Op) {
2354 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2355}
2356
2357template <typename OpTy>
2360m_ZExtOrSExt(const OpTy &Op) {
2361 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2362}
2363
2364template <typename OpTy>
2367 OpTy>
2369 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2370}
2371
2372template <typename OpTy>
2375 OpTy>
2378}
2379
2380template <typename CondTy, typename LTy, typename RTy> struct SelectLike_match {
2381 CondTy Cond;
2384
2385 SelectLike_match(const CondTy &C, const LTy &TC, const RTy &FC)
2386 : Cond(C), TrueC(TC), FalseC(FC) {}
2387
2388 template <typename OpTy> bool match(OpTy *V) const {
2389 // select(Cond, TrueC, FalseC) — captures both constants directly
2391 return true;
2392
2393 Type *Ty = V->getType();
2394 Value *CondV = nullptr;
2395
2396 // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
2397 if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
2398 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2399 TrueC.match(ConstantInt::get(Ty, 1)) &&
2400 FalseC.match(ConstantInt::get(Ty, 0)))
2401 return true;
2402
2403 // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
2404 if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
2405 CondV->getType()->isIntOrIntVectorTy(1) && Cond.match(CondV) &&
2406 TrueC.match(Constant::getAllOnesValue(Ty)) &&
2407 FalseC.match(ConstantInt::get(Ty, 0)))
2408 return true;
2409
2410 return false;
2411 }
2412};
2413
2414/// Matches a value that behaves like a boolean-controlled select, i.e. one of:
2415/// select i1 Cond, TrueC, FalseC
2416/// zext i1 Cond (equivalent to select i1 Cond, 1, 0)
2417/// sext i1 Cond (equivalent to select i1 Cond, -1, 0)
2418///
2419/// The condition is matched against \p Cond, and the true/false constants
2420/// against \p TrueC and \p FalseC respectively. For zext/sext, the synthetic
2421/// constants are bound to \p TrueC and \p FalseC via their matchers.
2422template <typename CondTy, typename LTy, typename RTy>
2424m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC) {
2425 return SelectLike_match<CondTy, LTy, RTy>(C, TrueC, FalseC);
2426}
2427
2428template <typename OpTy>
2432
2433template <typename OpTy>
2437
2438template <typename OpTy>
2441m_IToFP(const OpTy &Op) {
2442 return m_CombineOr(m_UIToFP(Op), m_SIToFP(Op));
2443}
2444
2445template <typename OpTy>
2449
2450template <typename OpTy>
2454
2455template <typename OpTy>
2458m_FPToI(const OpTy &Op) {
2459 return m_CombineOr(m_FPToUI(Op), m_FPToSI(Op));
2460}
2461
2462template <typename OpTy>
2466
2467template <typename OpTy>
2471
2472//===----------------------------------------------------------------------===//
2473// Matchers for control flow.
2474//
2475
2476struct br_match {
2478
2480
2481 template <typename OpTy> bool match(OpTy *V) const {
2482 if (auto *BI = dyn_cast<UncondBrInst>(V)) {
2483 Succ = BI->getSuccessor();
2484 return true;
2485 }
2486 return false;
2487 }
2488};
2489
2490inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2491
2492template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2494 Cond_t Cond;
2495 TrueBlock_t T;
2496 FalseBlock_t F;
2497
2498 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2499 : Cond(C), T(t), F(f) {}
2500
2501 template <typename OpTy> bool match(OpTy *V) const {
2502 if (auto *BI = dyn_cast<CondBrInst>(V))
2503 if (Cond.match(BI->getCondition()))
2504 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2505 return false;
2506 }
2507};
2508
2509template <typename Cond_t>
2515
2516template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2518m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2520}
2521
2522//===----------------------------------------------------------------------===//
2523// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2524//
2525
2526template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2527 bool Commutable = false>
2529 using PredType = Pred_t;
2532
2533 // The evaluation order is always stable, regardless of Commutability.
2534 // The LHS is always matched first.
2535 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2536
2537 template <typename OpTy> bool match(OpTy *V) const {
2538 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2539 Intrinsic::ID IID = II->getIntrinsicID();
2540 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2541 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2542 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2543 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2544 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2545 return (L.match(LHS) && R.match(RHS)) ||
2546 (Commutable && L.match(RHS) && R.match(LHS));
2547 }
2548 }
2549 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2550 auto *SI = dyn_cast<SelectInst>(V);
2551 if (!SI)
2552 return false;
2553 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2554 if (!Cmp)
2555 return false;
2556 // At this point we have a select conditioned on a comparison. Check that
2557 // it is the values returned by the select that are being compared.
2558 auto *TrueVal = SI->getTrueValue();
2559 auto *FalseVal = SI->getFalseValue();
2560 auto *LHS = Cmp->getOperand(0);
2561 auto *RHS = Cmp->getOperand(1);
2562 if ((TrueVal != LHS || FalseVal != RHS) &&
2563 (TrueVal != RHS || FalseVal != LHS))
2564 return false;
2565 typename CmpInst_t::Predicate Pred =
2566 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2567 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2568 if (!Pred_t::match(Pred))
2569 return false;
2570 // It does! Bind the operands.
2571 return (L.match(LHS) && R.match(RHS)) ||
2572 (Commutable && L.match(RHS) && R.match(LHS));
2573 }
2574};
2575
2576/// Helper class for identifying signed max predicates.
2578 static bool match(ICmpInst::Predicate Pred) {
2579 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2580 }
2581};
2582
2583/// Helper class for identifying signed min predicates.
2585 static bool match(ICmpInst::Predicate Pred) {
2586 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2587 }
2588};
2589
2590/// Helper class for identifying unsigned max predicates.
2592 static bool match(ICmpInst::Predicate Pred) {
2593 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2594 }
2595};
2596
2597/// Helper class for identifying unsigned min predicates.
2599 static bool match(ICmpInst::Predicate Pred) {
2600 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2601 }
2602};
2603
2604/// Helper class for identifying ordered max predicates.
2606 static bool match(FCmpInst::Predicate Pred) {
2607 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2608 }
2609};
2610
2611/// Helper class for identifying ordered min predicates.
2613 static bool match(FCmpInst::Predicate Pred) {
2614 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2615 }
2616};
2617
2618/// Helper class for identifying unordered max predicates.
2620 static bool match(FCmpInst::Predicate Pred) {
2621 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2622 }
2623};
2624
2625/// Helper class for identifying unordered min predicates.
2627 static bool match(FCmpInst::Predicate Pred) {
2628 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2629 }
2630};
2631
2632template <typename LHS, typename RHS>
2637
2638template <typename LHS, typename RHS>
2643
2644template <typename LHS, typename RHS>
2649
2650template <typename LHS, typename RHS>
2655
2656template <typename LHS, typename RHS>
2657inline match_combine_or<
2662m_MaxOrMin(const LHS &L, const RHS &R) {
2663 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2664 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2665}
2666
2667/// Match an 'ordered' floating point maximum function.
2668/// Floating point has one special value 'NaN'. Therefore, there is no total
2669/// order. However, if we can ignore the 'NaN' value (for example, because of a
2670/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2671/// semantics. In the presence of 'NaN' we have to preserve the original
2672/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2673///
2674/// max(L, R) iff L and R are not NaN
2675/// m_OrdFMax(L, R) = R iff L or R are NaN
2676template <typename LHS, typename RHS>
2681
2682/// Match an 'ordered' floating point minimum function.
2683/// Floating point has one special value 'NaN'. Therefore, there is no total
2684/// order. However, if we can ignore the 'NaN' value (for example, because of a
2685/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2686/// semantics. In the presence of 'NaN' we have to preserve the original
2687/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2688///
2689/// min(L, R) iff L and R are not NaN
2690/// m_OrdFMin(L, R) = R iff L or R are NaN
2691template <typename LHS, typename RHS>
2696
2697/// Match an 'unordered' floating point maximum function.
2698/// Floating point has one special value 'NaN'. Therefore, there is no total
2699/// order. However, if we can ignore the 'NaN' value (for example, because of a
2700/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2701/// semantics. In the presence of 'NaN' we have to preserve the original
2702/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2703///
2704/// max(L, R) iff L and R are not NaN
2705/// m_UnordFMax(L, R) = L iff L or R are NaN
2706template <typename LHS, typename RHS>
2708m_UnordFMax(const LHS &L, const RHS &R) {
2710}
2711
2712/// Match an 'unordered' floating point minimum function.
2713/// Floating point has one special value 'NaN'. Therefore, there is no total
2714/// order. However, if we can ignore the 'NaN' value (for example, because of a
2715/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2716/// semantics. In the presence of 'NaN' we have to preserve the original
2717/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2718///
2719/// min(L, R) iff L and R are not NaN
2720/// m_UnordFMin(L, R) = L iff L or R are NaN
2721template <typename LHS, typename RHS>
2723m_UnordFMin(const LHS &L, const RHS &R) {
2725}
2726
2727/// Match an 'ordered' or 'unordered' floating point maximum function.
2728/// Floating point has one special value 'NaN'. Therefore, there is no total
2729/// order. However, if we can ignore the 'NaN' value (for example, because of a
2730/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2731/// semantics.
2732template <typename LHS, typename RHS>
2739
2740/// Match an 'ordered' or 'unordered' floating point minimum function.
2741/// Floating point has one special value 'NaN'. Therefore, there is no total
2742/// order. However, if we can ignore the 'NaN' value (for example, because of a
2743/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2744/// semantics.
2745template <typename LHS, typename RHS>
2752
2753/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2754/// NOTE: we first match the 'Not' (by matching '-1'),
2755/// and only then match the inner matcher!
2756template <typename ValTy>
2757inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2758m_Not(const ValTy &V) {
2759 return m_c_Xor(m_AllOnes(), V);
2760}
2761
2762template <typename ValTy>
2763inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2764 true>
2765m_NotForbidPoison(const ValTy &V) {
2766 return m_c_Xor(m_AllOnesForbidPoison(), V);
2767}
2768
2769//===----------------------------------------------------------------------===//
2770// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2771// Note that S might be matched to other instructions than AddInst.
2772//
2773
2774template <typename LHS_t, typename RHS_t, typename Sum_t>
2778 Sum_t S;
2779
2780 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2781 : L(L), R(R), S(S) {}
2782
2783 template <typename OpTy> bool match(OpTy *V) const {
2784 Value *ICmpLHS, *ICmpRHS;
2785 CmpPredicate Pred;
2786 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2787 return false;
2788
2789 Value *AddLHS, *AddRHS;
2790 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2791
2792 // (a + b) u< a, (a + b) u< b
2793 if (Pred == ICmpInst::ICMP_ULT)
2794 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2795 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2796
2797 // a >u (a + b), b >u (a + b)
2798 if (Pred == ICmpInst::ICMP_UGT)
2799 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2800 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2801
2802 Value *Op1;
2803 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2804 // (~a) <u b
2805 if (Pred == ICmpInst::ICMP_ULT) {
2806 if (XorExpr.match(ICmpLHS))
2807 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2808 }
2809 // b > u (~a)
2810 if (Pred == ICmpInst::ICMP_UGT) {
2811 if (XorExpr.match(ICmpRHS))
2812 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2813 }
2814
2815 // Match special-case for increment-by-1.
2816 if (Pred == ICmpInst::ICMP_EQ) {
2817 // (a + 1) == 0
2818 // (1 + a) == 0
2819 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2820 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2821 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2822 // 0 == (a + 1)
2823 // 0 == (1 + a)
2824 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2825 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2826 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2827 }
2828
2829 return false;
2830 }
2831};
2832
2833/// Match an icmp instruction checking for unsigned overflow on addition.
2834///
2835/// S is matched to the addition whose result is being checked for overflow, and
2836/// L and R are matched to the LHS and RHS of S.
2837template <typename LHS_t, typename RHS_t, typename Sum_t>
2839m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2841}
2842
2843template <typename Opnd_t> struct Argument_match {
2844 unsigned OpI;
2845 Opnd_t Val;
2846
2847 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2848
2849 template <typename OpTy> bool match(OpTy *V) const {
2850 // FIXME: Should likely be switched to use `CallBase`.
2851 if (const auto *CI = dyn_cast<CallInst>(V))
2852 return Val.match(CI->getArgOperand(OpI));
2853 return false;
2854 }
2855};
2856
2857/// Match an argument.
2858template <unsigned OpI, typename Opnd_t>
2859inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2860 return Argument_match<Opnd_t>(OpI, Op);
2861}
2862
2863/// Intrinsic matchers.
2865 unsigned ID;
2866
2868
2869 template <typename OpTy> bool match(OpTy *V) const {
2870 if (const auto *CI = dyn_cast<CallInst>(V))
2871 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2872 return F->getIntrinsicID() == ID;
2873 return false;
2874 }
2875};
2876
2877/// Intrinsic matches are combinations of ID matchers, and argument
2878/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2879/// them with lower arity matchers. Here's some convenient typedefs for up to
2880/// several arguments, and more can be added as needed
2881template <typename T0 = void, typename T1 = void, typename T2 = void,
2882 typename T3 = void, typename T4 = void, typename T5 = void,
2883 typename T6 = void, typename T7 = void, typename T8 = void,
2884 typename T9 = void, typename T10 = void>
2886template <typename T0> struct m_Intrinsic_Ty<T0> {
2888};
2889template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2890 using Ty =
2892};
2893template <typename T0, typename T1, typename T2>
2898template <typename T0, typename T1, typename T2, typename T3>
2903
2904template <typename T0, typename T1, typename T2, typename T3, typename T4>
2909
2910template <typename T0, typename T1, typename T2, typename T3, typename T4,
2911 typename T5>
2916
2917/// Match intrinsic calls like this:
2918/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2919template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2920 return IntrinsicID_match(IntrID);
2921}
2922
2923/// Matches MaskedLoad Intrinsic.
2924template <typename Opnd0, typename Opnd1, typename Opnd2>
2926m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2927 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2928}
2929
2930/// Matches MaskedStore Intrinsic.
2931template <typename Opnd0, typename Opnd1, typename Opnd2>
2933m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2934 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2935}
2936
2937/// Matches MaskedGather Intrinsic.
2938template <typename Opnd0, typename Opnd1, typename Opnd2>
2940m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2941 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2942}
2943
2944template <Intrinsic::ID IntrID, typename T0>
2945inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2947}
2948
2949template <Intrinsic::ID IntrID, typename T0, typename T1>
2950inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2951 const T1 &Op1) {
2953}
2954
2955template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2956inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2957m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2958 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2959}
2960
2961template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2962 typename T3>
2964m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2965 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2966}
2967
2968template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2969 typename T3, typename T4>
2971m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2972 const T4 &Op4) {
2973 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2974 m_Argument<4>(Op4));
2975}
2976
2977template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2978 typename T3, typename T4, typename T5>
2980m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2981 const T4 &Op4, const T5 &Op5) {
2982 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2983 m_Argument<5>(Op5));
2984}
2985
2986// Helper intrinsic matching specializations.
2987template <typename Opnd0>
2988inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2990}
2991
2992template <typename Opnd0>
2993inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2995}
2996
2997template <typename Opnd0>
2998inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2999 return m_Intrinsic<Intrinsic::fabs>(Op0);
3000}
3001
3002template <typename Opnd0>
3003inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
3005}
3006
3007template <typename Opnd0, typename Opnd1>
3008inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
3009 const Opnd1 &Op1) {
3010 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
3011}
3012
3013template <typename Opnd0, typename Opnd1>
3014inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
3015 const Opnd1 &Op1) {
3016 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
3017}
3018
3019template <typename Opnd0, typename Opnd1>
3021m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3022 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
3023}
3024
3025template <typename Opnd0, typename Opnd1>
3026inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
3027 const Opnd1 &Op1) {
3028 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
3029}
3030
3031template <typename Opnd0, typename Opnd1>
3032inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
3033 const Opnd1 &Op1) {
3034 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
3035}
3036
3037template <typename Opnd0, typename Opnd1>
3039m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3040 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
3041}
3042
3043template <typename Opnd0, typename Opnd1>
3046m_FMaxNum_or_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3047 return m_CombineOr(m_FMaxNum(Op0, Op1), m_FMaximumNum(Op0, Op1));
3048}
3049
3050template <typename Opnd0, typename Opnd1>
3053m_FMinNum_or_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
3054 return m_CombineOr(m_FMinNum(Op0, Op1), m_FMinimumNum(Op0, Op1));
3055}
3056
3057template <typename Opnd0, typename Opnd1, typename Opnd2>
3059m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3060 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
3061}
3062
3063template <typename Opnd0, typename Opnd1, typename Opnd2>
3065m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3066 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
3067}
3068
3069template <typename Opnd0>
3070inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
3071 return m_Intrinsic<Intrinsic::sqrt>(Op0);
3072}
3073
3074template <typename Opnd0, typename Opnd1>
3075inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
3076 const Opnd1 &Op1) {
3077 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
3078}
3079
3080template <typename Opnd0>
3081inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
3083}
3084
3085template <typename Opnd0, typename Opnd1, typename Opnd2>
3087m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
3088 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
3089}
3090
3091//===----------------------------------------------------------------------===//
3092// Matchers for two-operands operators with the operators in either order
3093//
3094
3095/// Matches a BinaryOperator with LHS and RHS in either order.
3096template <typename LHS, typename RHS>
3099}
3100
3101/// Matches an ICmp with a predicate over LHS and RHS in either order.
3102/// Swaps the predicate if operands are commuted.
3103template <typename LHS, typename RHS>
3105m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
3107}
3108
3109template <typename LHS, typename RHS>
3114
3115/// Matches a specific opcode with LHS and RHS in either order.
3116template <typename LHS, typename RHS>
3118m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
3119 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
3120}
3121
3122/// Matches a Add with LHS and RHS in either order.
3123template <typename LHS, typename RHS>
3128
3129/// Matches a Mul with LHS and RHS in either order.
3130template <typename LHS, typename RHS>
3135
3136/// Matches an And with LHS and RHS in either order.
3137template <typename LHS, typename RHS>
3142
3143/// Matches an Or with LHS and RHS in either order.
3144template <typename LHS, typename RHS>
3149
3150/// Matches an Xor with LHS and RHS in either order.
3151template <typename LHS, typename RHS>
3156
3157/// Matches a 'Neg' as 'sub 0, V'.
3158template <typename ValTy>
3159inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3160m_Neg(const ValTy &V) {
3161 return m_Sub(m_ZeroInt(), V);
3162}
3163
3164/// Matches a 'Neg' as 'sub nsw 0, V'.
3165template <typename ValTy>
3167 Instruction::Sub,
3169m_NSWNeg(const ValTy &V) {
3170 return m_NSWSub(m_ZeroInt(), V);
3171}
3172
3173/// Matches an SMin with LHS and RHS in either order.
3174template <typename LHS, typename RHS>
3176m_c_SMin(const LHS &L, const RHS &R) {
3178}
3179/// Matches an SMax with LHS and RHS in either order.
3180template <typename LHS, typename RHS>
3182m_c_SMax(const LHS &L, const RHS &R) {
3184}
3185/// Matches a UMin with LHS and RHS in either order.
3186template <typename LHS, typename RHS>
3188m_c_UMin(const LHS &L, const RHS &R) {
3190}
3191/// Matches a UMax with LHS and RHS in either order.
3192template <typename LHS, typename RHS>
3194m_c_UMax(const LHS &L, const RHS &R) {
3196}
3197
3198template <typename LHS, typename RHS>
3199inline match_combine_or<
3204m_c_MaxOrMin(const LHS &L, const RHS &R) {
3205 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3206 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3207}
3208
3209template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3213
3214 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3215
3216 template <typename OpTy> bool match(OpTy *V) const {
3217 const auto *II = dyn_cast<IntrinsicInst>(V);
3218 if (!II || II->getIntrinsicID() != IntrID)
3219 return false;
3220 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3221 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3222 }
3223};
3224
3225template <Intrinsic::ID IntrID, typename T0, typename T1>
3227m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3229}
3230
3231/// Matches FAdd with LHS and RHS in either order.
3232template <typename LHS, typename RHS>
3234m_c_FAdd(const LHS &L, const RHS &R) {
3236}
3237
3238/// Matches FMul with LHS and RHS in either order.
3239template <typename LHS, typename RHS>
3241m_c_FMul(const LHS &L, const RHS &R) {
3243}
3244
3245template <typename Opnd_t> struct Signum_match {
3246 Opnd_t Val;
3247 Signum_match(const Opnd_t &V) : Val(V) {}
3248
3249 template <typename OpTy> bool match(OpTy *V) const {
3250 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3251 if (TypeSize == 0)
3252 return false;
3253
3254 unsigned ShiftWidth = TypeSize - 1;
3255 Value *Op;
3256
3257 // This is the representation of signum we match:
3258 //
3259 // signum(x) == (x >> 63) | (-x >>u 63)
3260 //
3261 // An i1 value is its own signum, so it's correct to match
3262 //
3263 // signum(x) == (x >> 0) | (-x >>u 0)
3264 //
3265 // for i1 values.
3266
3267 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3268 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3269 auto Signum = m_c_Or(LHS, RHS);
3270
3271 return Signum.match(V) && Val.match(Op);
3272 }
3273};
3274
3275/// Matches a signum pattern.
3276///
3277/// signum(x) =
3278/// x > 0 -> 1
3279/// x == 0 -> 0
3280/// x < 0 -> -1
3281template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3282 return Signum_match<Val_t>(V);
3283}
3284
3285template <int Ind, typename Opnd_t> struct ExtractValue_match {
3286 Opnd_t Val;
3287 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3288
3289 template <typename OpTy> bool match(OpTy *V) const {
3290 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3291 // If Ind is -1, don't inspect indices
3292 if (Ind != -1 &&
3293 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3294 return false;
3295 return Val.match(I->getAggregateOperand());
3296 }
3297 return false;
3298 }
3299};
3300
3301/// Match a single index ExtractValue instruction.
3302/// For example m_ExtractValue<1>(...)
3303template <int Ind, typename Val_t>
3307
3308/// Match an ExtractValue instruction with any index.
3309/// For example m_ExtractValue(...)
3310template <typename Val_t>
3311inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3312 return ExtractValue_match<-1, Val_t>(V);
3313}
3314
3315/// Matcher for a single index InsertValue instruction.
3316template <int Ind, typename T0, typename T1> struct InsertValue_match {
3319
3320 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3321
3322 template <typename OpTy> bool match(OpTy *V) const {
3323 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3324 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3325 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3326 }
3327 return false;
3328 }
3329};
3330
3331/// Matches a single index InsertValue instruction.
3332template <int Ind, typename Val_t, typename Elt_t>
3334 const Elt_t &Elt) {
3335 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3336}
3337
3338/// Matches a call to `llvm.vscale()`.
3340
3341template <typename Opnd0, typename Opnd1>
3343m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3345}
3346
3347template <typename Opnd>
3351
3352template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3356
3357 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3358
3359 template <typename T> bool match(T *V) const {
3360 auto *I = dyn_cast<Instruction>(V);
3361 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3362 return false;
3363
3364 if (I->getOpcode() == Opcode) {
3365 auto *Op0 = I->getOperand(0);
3366 auto *Op1 = I->getOperand(1);
3367 return (L.match(Op0) && R.match(Op1)) ||
3368 (Commutable && L.match(Op1) && R.match(Op0));
3369 }
3370
3371 if (auto *Select = dyn_cast<SelectInst>(I)) {
3372 auto *Cond = Select->getCondition();
3373 auto *TVal = Select->getTrueValue();
3374 auto *FVal = Select->getFalseValue();
3375
3376 // Don't match a scalar select of bool vectors.
3377 // Transforms expect a single type for operands if this matches.
3378 if (Cond->getType() != Select->getType())
3379 return false;
3380
3381 if (Opcode == Instruction::And) {
3382 auto *C = dyn_cast<Constant>(FVal);
3383 if (C && C->isNullValue())
3384 return (L.match(Cond) && R.match(TVal)) ||
3385 (Commutable && L.match(TVal) && R.match(Cond));
3386 } else {
3387 assert(Opcode == Instruction::Or);
3388 auto *C = dyn_cast<Constant>(TVal);
3389 if (C && C->isOneValue())
3390 return (L.match(Cond) && R.match(FVal)) ||
3391 (Commutable && L.match(FVal) && R.match(Cond));
3392 }
3393 }
3394
3395 return false;
3396 }
3397};
3398
3399/// Matches L && R either in the form of L & R or L ? R : false.
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_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3409
3410/// Matches L && R with LHS and RHS in either order.
3411template <typename LHS, typename RHS>
3413m_c_LogicalAnd(const LHS &L, const RHS &R) {
3415}
3416
3417/// Matches L || R either in the form of L | R or L ? true : R.
3418/// Note that the latter form is poison-blocking.
3419template <typename LHS, typename RHS>
3424
3425/// Matches L || R where L and R are arbitrary values.
3426inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3427
3428/// Matches L || R with LHS and RHS in either order.
3429template <typename LHS, typename RHS>
3431m_c_LogicalOr(const LHS &L, const RHS &R) {
3433}
3434
3435/// Matches either L && R or L || R,
3436/// either one being in the either binary or logical form.
3437/// Note that the latter form is poison-blocking.
3438template <typename LHS, typename RHS, bool Commutable = false>
3444
3445/// Matches either L && R or L || R where L and R are arbitrary values.
3446inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3447
3448/// Matches either L && R or L || R with LHS and RHS in either order.
3449template <typename LHS, typename RHS>
3450inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3451 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3452}
3453
3454} // end namespace PatternMatch
3455} // end namespace llvm
3456
3457#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.
cstfp_pred_ty< is_signed_inf< true > > m_NegInf()
Match a negative infinity FP constant.
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)
cstfp_pred_ty< is_signed_inf< false > > m_PosInf()
Match a positive infinity FP constant.
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 APFloat &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)