LLVM 19.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/InstrTypes.h"
37#include "llvm/IR/Instruction.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Operator.h"
42#include "llvm/IR/Value.h"
44#include <cstdint>
45
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return const_cast<Pattern &>(P).match(V);
51}
52
53template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
54 return const_cast<Pattern &>(P).match(Mask);
55}
56
57template <typename SubPattern_t> struct OneUse_match {
58 SubPattern_t SubPattern;
59
60 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
61
62 template <typename OpTy> bool match(OpTy *V) {
63 return V->hasOneUse() && SubPattern.match(V);
64 }
65};
66
67template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
68 return SubPattern;
69}
70
71template <typename SubPattern_t> struct AllowReassoc_match {
72 SubPattern_t SubPattern;
73
74 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
75
76 template <typename OpTy> bool match(OpTy *V) {
77 auto *I = dyn_cast<FPMathOperator>(V);
78 return I && I->hasAllowReassoc() && SubPattern.match(I);
79 }
80};
81
82template <typename T>
83inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
84 return SubPattern;
85}
86
87template <typename Class> struct class_match {
88 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
89};
90
91/// Match an arbitrary value and ignore it.
93
94/// Match an arbitrary unary operation and ignore it.
97}
98
99/// Match an arbitrary binary operation and ignore it.
102}
103
104/// Matches any compare instruction and ignore it.
106
108 static bool check(const Value *V) {
109 if (isa<UndefValue>(V))
110 return true;
111
112 const auto *CA = dyn_cast<ConstantAggregate>(V);
113 if (!CA)
114 return false;
115
118
119 // Either UndefValue, PoisonValue, or an aggregate that only contains
120 // these is accepted by matcher.
121 // CheckValue returns false if CA cannot satisfy this constraint.
122 auto CheckValue = [&](const ConstantAggregate *CA) {
123 for (const Value *Op : CA->operand_values()) {
124 if (isa<UndefValue>(Op))
125 continue;
126
127 const auto *CA = dyn_cast<ConstantAggregate>(Op);
128 if (!CA)
129 return false;
130 if (Seen.insert(CA).second)
131 Worklist.emplace_back(CA);
132 }
133
134 return true;
135 };
136
137 if (!CheckValue(CA))
138 return false;
139
140 while (!Worklist.empty()) {
141 if (!CheckValue(Worklist.pop_back_val()))
142 return false;
143 }
144 return true;
145 }
146 template <typename ITy> bool match(ITy *V) { return check(V); }
147};
148
149/// Match an arbitrary undef constant. This matches poison as well.
150/// If this is an aggregate and contains a non-aggregate element that is
151/// neither undef nor poison, the aggregate is not matched.
152inline auto m_Undef() { return undef_match(); }
153
154/// Match an arbitrary UndefValue constant.
157}
158
159/// Match an arbitrary poison constant.
162}
163
164/// Match an arbitrary Constant and ignore it.
166
167/// Match an arbitrary ConstantInt and ignore it.
170}
171
172/// Match an arbitrary ConstantFP and ignore it.
175}
176
178 template <typename ITy> bool match(ITy *V) {
179 auto *C = dyn_cast<Constant>(V);
180 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
181 }
182};
183
184/// Match a constant expression or a constant that contains a constant
185/// expression.
187
188/// Match an arbitrary basic block value and ignore it.
191}
192
193/// Inverting matcher
194template <typename Ty> struct match_unless {
195 Ty M;
196
197 match_unless(const Ty &Matcher) : M(Matcher) {}
198
199 template <typename ITy> bool match(ITy *V) { return !M.match(V); }
200};
201
202/// Match if the inner matcher does *NOT* match.
203template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
204 return match_unless<Ty>(M);
205}
206
207/// Matching combinators
208template <typename LTy, typename RTy> struct match_combine_or {
209 LTy L;
210 RTy R;
211
212 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
213
214 template <typename ITy> bool match(ITy *V) {
215 if (L.match(V))
216 return true;
217 if (R.match(V))
218 return true;
219 return false;
220 }
221};
222
223template <typename LTy, typename RTy> struct match_combine_and {
224 LTy L;
225 RTy R;
226
227 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
228
229 template <typename ITy> bool match(ITy *V) {
230 if (L.match(V))
231 if (R.match(V))
232 return true;
233 return false;
234 }
235};
236
237/// Combine two pattern matchers matching L || R
238template <typename LTy, typename RTy>
239inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
240 return match_combine_or<LTy, RTy>(L, R);
241}
242
243/// Combine two pattern matchers matching L && R
244template <typename LTy, typename RTy>
245inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
246 return match_combine_and<LTy, RTy>(L, R);
247}
248
250 const APInt *&Res;
252
255
256 template <typename ITy> bool match(ITy *V) {
257 if (auto *CI = dyn_cast<ConstantInt>(V)) {
258 Res = &CI->getValue();
259 return true;
260 }
261 if (V->getType()->isVectorTy())
262 if (const auto *C = dyn_cast<Constant>(V))
263 if (auto *CI =
264 dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison))) {
265 Res = &CI->getValue();
266 return true;
267 }
268 return false;
269 }
270};
271// Either constexpr if or renaming ConstantFP::getValueAPF to
272// ConstantFP::getValue is needed to do it via single template
273// function for both apint/apfloat.
275 const APFloat *&Res;
277
280
281 template <typename ITy> bool match(ITy *V) {
282 if (auto *CI = dyn_cast<ConstantFP>(V)) {
283 Res = &CI->getValueAPF();
284 return true;
285 }
286 if (V->getType()->isVectorTy())
287 if (const auto *C = dyn_cast<Constant>(V))
288 if (auto *CI =
289 dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowPoison))) {
290 Res = &CI->getValueAPF();
291 return true;
292 }
293 return false;
294 }
295};
296
297/// Match a ConstantInt or splatted ConstantVector, binding the
298/// specified pointer to the contained APInt.
299inline apint_match m_APInt(const APInt *&Res) {
300 // Forbid poison by default to maintain previous behavior.
301 return apint_match(Res, /* AllowPoison */ false);
302}
303
304/// Match APInt while allowing poison in splat vector constants.
306 return apint_match(Res, /* AllowPoison */ true);
307}
308
309/// Match APInt while forbidding poison in splat vector constants.
311 return apint_match(Res, /* AllowPoison */ false);
312}
313
314/// Match a ConstantFP or splatted ConstantVector, binding the
315/// specified pointer to the contained APFloat.
316inline apfloat_match m_APFloat(const APFloat *&Res) {
317 // Forbid undefs by default to maintain previous behavior.
318 return apfloat_match(Res, /* AllowPoison */ false);
319}
320
321/// Match APFloat while allowing poison in splat vector constants.
323 return apfloat_match(Res, /* AllowPoison */ true);
324}
325
326/// Match APFloat while forbidding poison in splat vector constants.
328 return apfloat_match(Res, /* AllowPoison */ false);
329}
330
331template <int64_t Val> struct constantint_match {
332 template <typename ITy> bool match(ITy *V) {
333 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
334 const APInt &CIV = CI->getValue();
335 if (Val >= 0)
336 return CIV == static_cast<uint64_t>(Val);
337 // If Val is negative, and CI is shorter than it, truncate to the right
338 // number of bits. If it is larger, then we have to sign extend. Just
339 // compare their negated values.
340 return -CIV == -Val;
341 }
342 return false;
343 }
344};
345
346/// Match a ConstantInt with a specific value.
347template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
348 return constantint_match<Val>();
349}
350
351/// This helper class is used to match constant scalars, vector splats,
352/// and fixed width vectors that satisfy a specified predicate.
353/// For fixed width vector constants, poison elements are ignored if AllowPoison
354/// is true.
355template <typename Predicate, typename ConstantVal, bool AllowPoison>
356struct cstval_pred_ty : public Predicate {
357 template <typename ITy> bool match(ITy *V) {
358 if (const auto *CV = dyn_cast<ConstantVal>(V))
359 return this->isValue(CV->getValue());
360 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
361 if (const auto *C = dyn_cast<Constant>(V)) {
362 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
363 return this->isValue(CV->getValue());
364
365 // Number of elements of a scalable vector unknown at compile time
366 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
367 if (!FVTy)
368 return false;
369
370 // Non-splat vector constant: check each element for a match.
371 unsigned NumElts = FVTy->getNumElements();
372 assert(NumElts != 0 && "Constant vector with no elements?");
373 bool HasNonPoisonElements = false;
374 for (unsigned i = 0; i != NumElts; ++i) {
375 Constant *Elt = C->getAggregateElement(i);
376 if (!Elt)
377 return false;
378 if (AllowPoison && isa<PoisonValue>(Elt))
379 continue;
380 auto *CV = dyn_cast<ConstantVal>(Elt);
381 if (!CV || !this->isValue(CV->getValue()))
382 return false;
383 HasNonPoisonElements = true;
384 }
385 return HasNonPoisonElements;
386 }
387 }
388 return false;
389 }
390};
391
392/// specialization of cstval_pred_ty for ConstantInt
393template <typename Predicate, bool AllowPoison = true>
395
396/// specialization of cstval_pred_ty for ConstantFP
397template <typename Predicate>
399 /*AllowPoison=*/true>;
400
401/// This helper class is used to match scalar and vector constants that
402/// satisfy a specified predicate, and bind them to an APInt.
403template <typename Predicate> struct api_pred_ty : public Predicate {
404 const APInt *&Res;
405
406 api_pred_ty(const APInt *&R) : Res(R) {}
407
408 template <typename ITy> bool match(ITy *V) {
409 if (const auto *CI = dyn_cast<ConstantInt>(V))
410 if (this->isValue(CI->getValue())) {
411 Res = &CI->getValue();
412 return true;
413 }
414 if (V->getType()->isVectorTy())
415 if (const auto *C = dyn_cast<Constant>(V))
416 if (auto *CI = dyn_cast_or_null<ConstantInt>(
417 C->getSplatValue(/*AllowPoison=*/true)))
418 if (this->isValue(CI->getValue())) {
419 Res = &CI->getValue();
420 return true;
421 }
422
423 return false;
424 }
425};
426
427/// This helper class is used to match scalar and vector constants that
428/// satisfy a specified predicate, and bind them to an APFloat.
429/// Poison is allowed in splat vector constants.
430template <typename Predicate> struct apf_pred_ty : public Predicate {
431 const APFloat *&Res;
432
433 apf_pred_ty(const APFloat *&R) : Res(R) {}
434
435 template <typename ITy> bool match(ITy *V) {
436 if (const auto *CI = dyn_cast<ConstantFP>(V))
437 if (this->isValue(CI->getValue())) {
438 Res = &CI->getValue();
439 return true;
440 }
441 if (V->getType()->isVectorTy())
442 if (const auto *C = dyn_cast<Constant>(V))
443 if (auto *CI = dyn_cast_or_null<ConstantFP>(
444 C->getSplatValue(/* AllowPoison */ true)))
445 if (this->isValue(CI->getValue())) {
446 Res = &CI->getValue();
447 return true;
448 }
449
450 return false;
451 }
452};
453
454///////////////////////////////////////////////////////////////////////////////
455//
456// Encapsulate constant value queries for use in templated predicate matchers.
457// This allows checking if constants match using compound predicates and works
458// with vector constants, possibly with relaxed constraints. For example, ignore
459// undef values.
460//
461///////////////////////////////////////////////////////////////////////////////
462
463template <typename APTy> struct custom_checkfn {
464 function_ref<bool(const APTy &)> CheckFn;
465 bool isValue(const APTy &C) { return CheckFn(C); }
466};
467
468/// Match an integer or vector where CheckFn(ele) for each element is true.
469/// For vectors, poison elements are assumed to match.
471m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
472 return cst_pred_ty<custom_checkfn<APInt>>{CheckFn};
473}
474
476m_CheckedInt(const APInt *&V, function_ref<bool(const APInt &)> CheckFn) {
478 P.CheckFn = CheckFn;
479 return P;
480}
481
482/// Match a float or vector where CheckFn(ele) for each element is true.
483/// For vectors, poison elements are assumed to match.
485m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
487}
488
490m_CheckedFp(const APFloat *&V, function_ref<bool(const APFloat &)> CheckFn) {
492 P.CheckFn = CheckFn;
493 return P;
494}
495
497 bool isValue(const APInt &C) { return true; }
498};
499/// Match an integer or vector with any integral constant.
500/// For vectors, this includes constants with undefined elements.
503}
504
506 bool isValue(const APInt &C) { return C.isShiftedMask(); }
507};
508
511}
512
514 bool isValue(const APInt &C) { return C.isAllOnes(); }
515};
516/// Match an integer or vector with all bits set.
517/// For vectors, this includes constants with undefined elements.
520}
521
524}
525
527 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
528};
529/// Match an integer or vector with values having all bits except for the high
530/// bit set (0x7f...).
531/// For vectors, this includes constants with undefined elements.
534}
536 return V;
537}
538
540 bool isValue(const APInt &C) { return C.isNegative(); }
541};
542/// Match an integer or vector of negative values.
543/// For vectors, this includes constants with undefined elements.
546}
547inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
548
550 bool isValue(const APInt &C) { return C.isNonNegative(); }
551};
552/// Match an integer or vector of non-negative values.
553/// For vectors, this includes constants with undefined elements.
556}
557inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
558
560 bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
561};
562/// Match an integer or vector of strictly positive values.
563/// For vectors, this includes constants with undefined elements.
566}
568 return V;
569}
570
572 bool isValue(const APInt &C) { return C.isNonPositive(); }
573};
574/// Match an integer or vector of non-positive values.
575/// For vectors, this includes constants with undefined elements.
578}
579inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
580
581struct is_one {
582 bool isValue(const APInt &C) { return C.isOne(); }
583};
584/// Match an integer 1 or a vector with all elements equal to 1.
585/// For vectors, this includes constants with undefined elements.
587
589 bool isValue(const APInt &C) { return C.isZero(); }
590};
591/// Match an integer 0 or a vector with all elements equal to 0.
592/// For vectors, this includes constants with undefined elements.
595}
596
597struct is_zero {
598 template <typename ITy> bool match(ITy *V) {
599 auto *C = dyn_cast<Constant>(V);
600 // FIXME: this should be able to do something for scalable vectors
601 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
602 }
603};
604/// Match any null constant or a vector with all elements equal to 0.
605/// For vectors, this includes constants with undefined elements.
606inline is_zero m_Zero() { return is_zero(); }
607
608struct is_power2 {
609 bool isValue(const APInt &C) { return C.isPowerOf2(); }
610};
611/// Match an integer or vector power-of-2.
612/// For vectors, this includes constants with undefined elements.
614inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
615
617 bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); }
618};
619/// Match a integer or vector negated power-of-2.
620/// For vectors, this includes constants with undefined elements.
623}
625 return V;
626}
627
629 bool isValue(const APInt &C) { return !C || C.isNegatedPowerOf2(); }
630};
631/// Match a integer or vector negated power-of-2.
632/// For vectors, this includes constants with undefined elements.
635}
638 return V;
639}
640
642 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
643};
644/// Match an integer or vector of 0 or power-of-2 values.
645/// For vectors, this includes constants with undefined elements.
648}
650 return V;
651}
652
654 bool isValue(const APInt &C) { return C.isSignMask(); }
655};
656/// Match an integer or vector with only the sign bit(s) set.
657/// For vectors, this includes constants with undefined elements.
660}
661
663 bool isValue(const APInt &C) { return C.isMask(); }
664};
665/// Match an integer or vector with only the low bit(s) set.
666/// For vectors, this includes constants with undefined elements.
669}
670inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
671
673 bool isValue(const APInt &C) { return !C || C.isMask(); }
674};
675/// Match an integer or vector with only the low bit(s) set.
676/// For vectors, this includes constants with undefined elements.
679}
681 return V;
682}
683
686 const APInt *Thr;
687 bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); }
688};
689/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
690/// to Threshold. For vectors, this includes constants with undefined elements.
692m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
694 P.Pred = Predicate;
695 P.Thr = &Threshold;
696 return P;
697}
698
699struct is_nan {
700 bool isValue(const APFloat &C) { return C.isNaN(); }
701};
702/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
703/// For vectors, this includes constants with undefined elements.
705
706struct is_nonnan {
707 bool isValue(const APFloat &C) { return !C.isNaN(); }
708};
709/// Match a non-NaN FP constant.
710/// For vectors, this includes constants with undefined elements.
713}
714
715struct is_inf {
716 bool isValue(const APFloat &C) { return C.isInfinity(); }
717};
718/// Match a positive or negative infinity FP constant.
719/// For vectors, this includes constants with undefined elements.
721
722struct is_noninf {
723 bool isValue(const APFloat &C) { return !C.isInfinity(); }
724};
725/// Match a non-infinity FP constant, i.e. finite or NaN.
726/// For vectors, this includes constants with undefined elements.
729}
730
731struct is_finite {
732 bool isValue(const APFloat &C) { return C.isFinite(); }
733};
734/// Match a finite FP constant, i.e. not infinity or NaN.
735/// For vectors, this includes constants with undefined elements.
738}
739inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
740
742 bool isValue(const APFloat &C) { return C.isFiniteNonZero(); }
743};
744/// Match a finite non-zero FP constant.
745/// For vectors, this includes constants with undefined elements.
748}
750 return V;
751}
752
754 bool isValue(const APFloat &C) { return C.isZero(); }
755};
756/// Match a floating-point negative zero or positive zero.
757/// For vectors, this includes constants with undefined elements.
760}
761
763 bool isValue(const APFloat &C) { return C.isPosZero(); }
764};
765/// Match a floating-point positive zero.
766/// For vectors, this includes constants with undefined elements.
769}
770
772 bool isValue(const APFloat &C) { return C.isNegZero(); }
773};
774/// Match a floating-point negative zero.
775/// For vectors, this includes constants with undefined elements.
778}
779
781 bool isValue(const APFloat &C) { return C.isNonZero(); }
782};
783/// Match a floating-point non-zero.
784/// For vectors, this includes constants with undefined elements.
787}
788
789///////////////////////////////////////////////////////////////////////////////
790
791template <typename Class> struct bind_ty {
792 Class *&VR;
793
794 bind_ty(Class *&V) : VR(V) {}
795
796 template <typename ITy> bool match(ITy *V) {
797 if (auto *CV = dyn_cast<Class>(V)) {
798 VR = CV;
799 return true;
800 }
801 return false;
802 }
803};
804
805/// Match a value, capturing it if we match.
806inline bind_ty<Value> m_Value(Value *&V) { return V; }
807inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
808
809/// Match an instruction, capturing it if we match.
811/// Match a unary operator, capturing it if we match.
813/// Match a binary operator, capturing it if we match.
815/// Match a with overflow intrinsic, capturing it if we match.
817 return I;
818}
821 return I;
822}
823
824/// Match an UndefValue, capturing the value if we match.
826
827/// Match a Constant, capturing the value if we match.
829
830/// Match a ConstantInt, capturing the value if we match.
832
833/// Match a ConstantFP, capturing the value if we match.
835
836/// Match a ConstantExpr, capturing the value if we match.
838
839/// Match a basic block value, capturing it if we match.
842 return V;
843}
844
845/// Match an arbitrary immediate Constant and ignore it.
850}
851
852/// Match an immediate Constant, capturing the value if we match.
857}
858
859/// Match a specified Value*.
861 const Value *Val;
862
863 specificval_ty(const Value *V) : Val(V) {}
864
865 template <typename ITy> bool match(ITy *V) { return V == Val; }
866};
867
868/// Match if we have a specific specified value.
869inline specificval_ty m_Specific(const Value *V) { return V; }
870
871/// Stores a reference to the Value *, not the Value * itself,
872/// thus can be used in commutative matchers.
873template <typename Class> struct deferredval_ty {
874 Class *const &Val;
875
876 deferredval_ty(Class *const &V) : Val(V) {}
877
878 template <typename ITy> bool match(ITy *const V) { return V == Val; }
879};
880
881/// Like m_Specific(), but works if the specific value to match is determined
882/// as part of the same match() expression. For example:
883/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
884/// bind X before the pattern match starts.
885/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
886/// whichever value m_Value(X) populated.
887inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
889 return V;
890}
891
892/// Match a specified floating point value or vector of all elements of
893/// that value.
895 double Val;
896
897 specific_fpval(double V) : Val(V) {}
898
899 template <typename ITy> bool match(ITy *V) {
900 if (const auto *CFP = dyn_cast<ConstantFP>(V))
901 return CFP->isExactlyValue(Val);
902 if (V->getType()->isVectorTy())
903 if (const auto *C = dyn_cast<Constant>(V))
904 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
905 return CFP->isExactlyValue(Val);
906 return false;
907 }
908};
909
910/// Match a specific floating point value or vector with all elements
911/// equal to the value.
912inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
913
914/// Match a float 1.0 or vector with all elements equal to 1.0.
915inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
916
919
921
922 template <typename ITy> bool match(ITy *V) {
923 if (const auto *CV = dyn_cast<ConstantInt>(V))
924 if (CV->getValue().ule(UINT64_MAX)) {
925 VR = CV->getZExtValue();
926 return true;
927 }
928 return false;
929 }
930};
931
932/// Match a specified integer value or vector of all elements of that
933/// value.
934template <bool AllowPoison> struct specific_intval {
935 const APInt &Val;
936
937 specific_intval(const APInt &V) : Val(V) {}
938
939 template <typename ITy> bool match(ITy *V) {
940 const auto *CI = dyn_cast<ConstantInt>(V);
941 if (!CI && V->getType()->isVectorTy())
942 if (const auto *C = dyn_cast<Constant>(V))
943 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
944
945 return CI && APInt::isSameValue(CI->getValue(), Val);
946 }
947};
948
949template <bool AllowPoison> struct specific_intval64 {
951
953
954 template <typename ITy> bool match(ITy *V) {
955 const auto *CI = dyn_cast<ConstantInt>(V);
956 if (!CI && V->getType()->isVectorTy())
957 if (const auto *C = dyn_cast<Constant>(V))
958 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
959
960 return CI && CI->getValue() == Val;
961 }
962};
963
964/// Match a specific integer value or vector with all elements equal to
965/// the value.
967 return specific_intval<false>(V);
968}
969
971 return specific_intval64<false>(V);
972}
973
975 return specific_intval<true>(V);
976}
977
979 return specific_intval64<true>(V);
980}
981
982/// Match a ConstantInt and bind to its value. This does not match
983/// ConstantInts wider than 64-bits.
985
986/// Match a specified basic block value.
989
991
992 template <typename ITy> bool match(ITy *V) {
993 const auto *BB = dyn_cast<BasicBlock>(V);
994 return BB && BB == Val;
995 }
996};
997
998/// Match a specific basic block value.
1000 return specific_bbval(BB);
1001}
1002
1003/// A commutative-friendly version of m_Specific().
1005 return BB;
1006}
1008m_Deferred(const BasicBlock *const &BB) {
1009 return BB;
1010}
1011
1012//===----------------------------------------------------------------------===//
1013// Matcher for any binary operator.
1014//
1015template <typename LHS_t, typename RHS_t, bool Commutable = false>
1019
1020 // The evaluation order is always stable, regardless of Commutability.
1021 // The LHS is always matched first.
1022 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1023
1024 template <typename OpTy> bool match(OpTy *V) {
1025 if (auto *I = dyn_cast<BinaryOperator>(V))
1026 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1027 (Commutable && L.match(I->getOperand(1)) &&
1028 R.match(I->getOperand(0)));
1029 return false;
1030 }
1031};
1032
1033template <typename LHS, typename RHS>
1034inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1035 return AnyBinaryOp_match<LHS, RHS>(L, R);
1036}
1037
1038//===----------------------------------------------------------------------===//
1039// Matcher for any unary operator.
1040// TODO fuse unary, binary matcher into n-ary matcher
1041//
1042template <typename OP_t> struct AnyUnaryOp_match {
1043 OP_t X;
1044
1045 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1046
1047 template <typename OpTy> bool match(OpTy *V) {
1048 if (auto *I = dyn_cast<UnaryOperator>(V))
1049 return X.match(I->getOperand(0));
1050 return false;
1051 }
1052};
1053
1054template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1055 return AnyUnaryOp_match<OP_t>(X);
1056}
1057
1058//===----------------------------------------------------------------------===//
1059// Matchers for specific binary operators.
1060//
1061
1062template <typename LHS_t, typename RHS_t, unsigned Opcode,
1063 bool Commutable = false>
1067
1068 // The evaluation order is always stable, regardless of Commutability.
1069 // The LHS is always matched first.
1070 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1071
1072 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) {
1073 if (V->getValueID() == Value::InstructionVal + Opc) {
1074 auto *I = cast<BinaryOperator>(V);
1075 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1076 (Commutable && L.match(I->getOperand(1)) &&
1077 R.match(I->getOperand(0)));
1078 }
1079 return false;
1080 }
1081
1082 template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); }
1083};
1084
1085template <typename LHS, typename RHS>
1087 const RHS &R) {
1089}
1090
1091template <typename LHS, typename RHS>
1093 const RHS &R) {
1095}
1096
1097template <typename LHS, typename RHS>
1099 const RHS &R) {
1101}
1102
1103template <typename LHS, typename RHS>
1105 const RHS &R) {
1107}
1108
1109template <typename Op_t> struct FNeg_match {
1110 Op_t X;
1111
1112 FNeg_match(const Op_t &Op) : X(Op) {}
1113 template <typename OpTy> bool match(OpTy *V) {
1114 auto *FPMO = dyn_cast<FPMathOperator>(V);
1115 if (!FPMO)
1116 return false;
1117
1118 if (FPMO->getOpcode() == Instruction::FNeg)
1119 return X.match(FPMO->getOperand(0));
1120
1121 if (FPMO->getOpcode() == Instruction::FSub) {
1122 if (FPMO->hasNoSignedZeros()) {
1123 // With 'nsz', any zero goes.
1124 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1125 return false;
1126 } else {
1127 // Without 'nsz', we need fsub -0.0, X exactly.
1128 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1129 return false;
1130 }
1131
1132 return X.match(FPMO->getOperand(1));
1133 }
1134
1135 return false;
1136 }
1137};
1138
1139/// Match 'fneg X' as 'fsub -0.0, X'.
1140template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1141 return FNeg_match<OpTy>(X);
1142}
1143
1144/// Match 'fneg X' as 'fsub +-0.0, X'.
1145template <typename RHS>
1146inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1147m_FNegNSZ(const RHS &X) {
1148 return m_FSub(m_AnyZeroFP(), X);
1149}
1150
1151template <typename LHS, typename RHS>
1153 const RHS &R) {
1155}
1156
1157template <typename LHS, typename RHS>
1159 const RHS &R) {
1161}
1162
1163template <typename LHS, typename RHS>
1165 const RHS &R) {
1167}
1168
1169template <typename LHS, typename RHS>
1171 const RHS &R) {
1173}
1174
1175template <typename LHS, typename RHS>
1177 const RHS &R) {
1179}
1180
1181template <typename LHS, typename RHS>
1183 const RHS &R) {
1185}
1186
1187template <typename LHS, typename RHS>
1189 const RHS &R) {
1191}
1192
1193template <typename LHS, typename RHS>
1195 const RHS &R) {
1197}
1198
1199template <typename LHS, typename RHS>
1201 const RHS &R) {
1203}
1204
1205template <typename LHS, typename RHS>
1207 const RHS &R) {
1209}
1210
1211template <typename LHS, typename RHS>
1213 const RHS &R) {
1215}
1216
1217template <typename LHS, typename RHS>
1219 const RHS &R) {
1221}
1222
1223template <typename LHS, typename RHS>
1225 const RHS &R) {
1227}
1228
1229template <typename LHS, typename RHS>
1231 const RHS &R) {
1233}
1234
1235template <typename LHS_t, typename RHS_t, unsigned Opcode,
1236 unsigned WrapFlags = 0, bool Commutable = false>
1240
1242 : L(LHS), R(RHS) {}
1243
1244 template <typename OpTy> bool match(OpTy *V) {
1245 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1246 if (Op->getOpcode() != Opcode)
1247 return false;
1249 !Op->hasNoUnsignedWrap())
1250 return false;
1251 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1252 !Op->hasNoSignedWrap())
1253 return false;
1254 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1255 (Commutable && L.match(Op->getOperand(1)) &&
1256 R.match(Op->getOperand(0)));
1257 }
1258 return false;
1259 }
1260};
1261
1262template <typename LHS, typename RHS>
1263inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1265m_NSWAdd(const LHS &L, const RHS &R) {
1266 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1268 R);
1269}
1270template <typename LHS, typename RHS>
1271inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1273m_NSWSub(const LHS &L, const RHS &R) {
1274 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1276 R);
1277}
1278template <typename LHS, typename RHS>
1279inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1281m_NSWMul(const LHS &L, const RHS &R) {
1282 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1284 R);
1285}
1286template <typename LHS, typename RHS>
1287inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1289m_NSWShl(const LHS &L, const RHS &R) {
1290 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1292 R);
1293}
1294
1295template <typename LHS, typename RHS>
1296inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1298m_NUWAdd(const LHS &L, const RHS &R) {
1299 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1301 L, R);
1302}
1303
1304template <typename LHS, typename RHS>
1306 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1307m_c_NUWAdd(const LHS &L, const RHS &R) {
1308 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1310 true>(L, R);
1311}
1312
1313template <typename LHS, typename RHS>
1314inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1316m_NUWSub(const LHS &L, const RHS &R) {
1317 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1319 L, R);
1320}
1321template <typename LHS, typename RHS>
1322inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1324m_NUWMul(const LHS &L, const RHS &R) {
1325 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1327 L, R);
1328}
1329template <typename LHS, typename RHS>
1330inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1332m_NUWShl(const LHS &L, const RHS &R) {
1333 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1335 L, R);
1336}
1337
1338template <typename LHS_t, typename RHS_t, bool Commutable = false>
1340 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1341 unsigned Opcode;
1342
1344 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1345
1346 template <typename OpTy> bool match(OpTy *V) {
1348 }
1349};
1350
1351/// Matches a specific opcode.
1352template <typename LHS, typename RHS>
1353inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1354 const RHS &R) {
1355 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1356}
1357
1358template <typename LHS, typename RHS, bool Commutable = false>
1362
1363 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1364
1365 template <typename OpTy> bool match(OpTy *V) {
1366 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1367 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1368 if (!PDI->isDisjoint())
1369 return false;
1370 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1371 (Commutable && L.match(PDI->getOperand(1)) &&
1372 R.match(PDI->getOperand(0)));
1373 }
1374 return false;
1375 }
1376};
1377
1378template <typename LHS, typename RHS>
1380 return DisjointOr_match<LHS, RHS>(L, R);
1381}
1382
1383template <typename LHS, typename RHS>
1385 const RHS &R) {
1387}
1388
1389/// Match either "add" or "or disjoint".
1390template <typename LHS, typename RHS>
1393m_AddLike(const LHS &L, const RHS &R) {
1394 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1395}
1396
1397/// Match either "add nsw" or "or disjoint"
1398template <typename LHS, typename RHS>
1399inline match_combine_or<
1400 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1403m_NSWAddLike(const LHS &L, const RHS &R) {
1404 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1405}
1406
1407/// Match either "add nuw" or "or disjoint"
1408template <typename LHS, typename RHS>
1409inline match_combine_or<
1410 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1413m_NUWAddLike(const LHS &L, const RHS &R) {
1414 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1415}
1416
1417//===----------------------------------------------------------------------===//
1418// Class that matches a group of binary opcodes.
1419//
1420template <typename LHS_t, typename RHS_t, typename Predicate,
1421 bool Commutable = false>
1422struct BinOpPred_match : Predicate {
1425
1426 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1427
1428 template <typename OpTy> bool match(OpTy *V) {
1429 if (auto *I = dyn_cast<Instruction>(V))
1430 return this->isOpType(I->getOpcode()) &&
1431 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1432 (Commutable && L.match(I->getOperand(1)) &&
1433 R.match(I->getOperand(0))));
1434 return false;
1435 }
1436};
1437
1439 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
1440};
1441
1443 bool isOpType(unsigned Opcode) {
1444 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1445 }
1446};
1447
1449 bool isOpType(unsigned Opcode) {
1450 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1451 }
1452};
1453
1455 bool isOpType(unsigned Opcode) {
1456 return Instruction::isBitwiseLogicOp(Opcode);
1457 }
1458};
1459
1461 bool isOpType(unsigned Opcode) {
1462 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1463 }
1464};
1465
1467 bool isOpType(unsigned Opcode) {
1468 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1469 }
1470};
1471
1472/// Matches shift operations.
1473template <typename LHS, typename RHS>
1475 const RHS &R) {
1477}
1478
1479/// Matches logical shift operations.
1480template <typename LHS, typename RHS>
1482 const RHS &R) {
1484}
1485
1486/// Matches logical shift operations.
1487template <typename LHS, typename RHS>
1489m_LogicalShift(const LHS &L, const RHS &R) {
1491}
1492
1493/// Matches bitwise logic operations.
1494template <typename LHS, typename RHS>
1496m_BitwiseLogic(const LHS &L, const RHS &R) {
1498}
1499
1500/// Matches bitwise logic operations in either order.
1501template <typename LHS, typename RHS>
1503m_c_BitwiseLogic(const LHS &L, const RHS &R) {
1505}
1506
1507/// Matches integer division operations.
1508template <typename LHS, typename RHS>
1510 const RHS &R) {
1512}
1513
1514/// Matches integer remainder operations.
1515template <typename LHS, typename RHS>
1517 const RHS &R) {
1519}
1520
1521//===----------------------------------------------------------------------===//
1522// Class that matches exact binary ops.
1523//
1524template <typename SubPattern_t> struct Exact_match {
1525 SubPattern_t SubPattern;
1526
1527 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1528
1529 template <typename OpTy> bool match(OpTy *V) {
1530 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1531 return PEO->isExact() && SubPattern.match(V);
1532 return false;
1533 }
1534};
1535
1536template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1537 return SubPattern;
1538}
1539
1540//===----------------------------------------------------------------------===//
1541// Matchers for CmpInst classes
1542//
1543
1544template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1545 bool Commutable = false>
1547 PredicateTy &Predicate;
1550
1551 // The evaluation order is always stable, regardless of Commutability.
1552 // The LHS is always matched first.
1553 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1554 : Predicate(Pred), L(LHS), R(RHS) {}
1555
1556 template <typename OpTy> bool match(OpTy *V) {
1557 if (auto *I = dyn_cast<Class>(V)) {
1558 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1559 Predicate = I->getPredicate();
1560 return true;
1561 } else if (Commutable && L.match(I->getOperand(1)) &&
1562 R.match(I->getOperand(0))) {
1563 Predicate = I->getSwappedPredicate();
1564 return true;
1565 }
1566 }
1567 return false;
1568 }
1569};
1570
1571template <typename LHS, typename RHS>
1573m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1575}
1576
1577template <typename LHS, typename RHS>
1579m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1581}
1582
1583template <typename LHS, typename RHS>
1585m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1587}
1588
1589//===----------------------------------------------------------------------===//
1590// Matchers for instructions with a given opcode and number of operands.
1591//
1592
1593/// Matches instructions with Opcode and three operands.
1594template <typename T0, unsigned Opcode> struct OneOps_match {
1596
1597 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1598
1599 template <typename OpTy> bool match(OpTy *V) {
1600 if (V->getValueID() == Value::InstructionVal + Opcode) {
1601 auto *I = cast<Instruction>(V);
1602 return Op1.match(I->getOperand(0));
1603 }
1604 return false;
1605 }
1606};
1607
1608/// Matches instructions with Opcode and three operands.
1609template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1612
1613 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1614
1615 template <typename OpTy> bool match(OpTy *V) {
1616 if (V->getValueID() == Value::InstructionVal + Opcode) {
1617 auto *I = cast<Instruction>(V);
1618 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1619 }
1620 return false;
1621 }
1622};
1623
1624/// Matches instructions with Opcode and three operands.
1625template <typename T0, typename T1, typename T2, unsigned Opcode>
1630
1631 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1632 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1633
1634 template <typename OpTy> bool match(OpTy *V) {
1635 if (V->getValueID() == Value::InstructionVal + Opcode) {
1636 auto *I = cast<Instruction>(V);
1637 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1638 Op3.match(I->getOperand(2));
1639 }
1640 return false;
1641 }
1642};
1643
1644/// Matches instructions with Opcode and any number of operands
1645template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1646 std::tuple<OperandTypes...> Operands;
1647
1648 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1649
1650 // Operand matching works by recursively calling match_operands, matching the
1651 // operands left to right. The first version is called for each operand but
1652 // the last, for which the second version is called. The second version of
1653 // match_operands is also used to match each individual operand.
1654 template <int Idx, int Last>
1655 std::enable_if_t<Idx != Last, bool> match_operands(const Instruction *I) {
1656 return match_operands<Idx, Idx>(I) && match_operands<Idx + 1, Last>(I);
1657 }
1658
1659 template <int Idx, int Last>
1660 std::enable_if_t<Idx == Last, bool> match_operands(const Instruction *I) {
1661 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1662 }
1663
1664 template <typename OpTy> bool match(OpTy *V) {
1665 if (V->getValueID() == Value::InstructionVal + Opcode) {
1666 auto *I = cast<Instruction>(V);
1667 return I->getNumOperands() == sizeof...(OperandTypes) &&
1668 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1669 }
1670 return false;
1671 }
1672};
1673
1674/// Matches SelectInst.
1675template <typename Cond, typename LHS, typename RHS>
1677m_Select(const Cond &C, const LHS &L, const RHS &R) {
1679}
1680
1681/// This matches a select of two constants, e.g.:
1682/// m_SelectCst<-1, 0>(m_Value(V))
1683template <int64_t L, int64_t R, typename Cond>
1685 Instruction::Select>
1687 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1688}
1689
1690/// Matches FreezeInst.
1691template <typename OpTy>
1694}
1695
1696/// Matches InsertElementInst.
1697template <typename Val_t, typename Elt_t, typename Idx_t>
1699m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1701 Val, Elt, Idx);
1702}
1703
1704/// Matches ExtractElementInst.
1705template <typename Val_t, typename Idx_t>
1707m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1709}
1710
1711/// Matches shuffle.
1712template <typename T0, typename T1, typename T2> struct Shuffle_match {
1716
1717 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1718 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1719
1720 template <typename OpTy> bool match(OpTy *V) {
1721 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1722 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1723 Mask.match(I->getShuffleMask());
1724 }
1725 return false;
1726 }
1727};
1728
1729struct m_Mask {
1733 MaskRef = Mask;
1734 return true;
1735 }
1736};
1737
1740 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1741 }
1742};
1743
1747 bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
1748};
1749
1754 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
1755 if (First == Mask.end())
1756 return false;
1757 SplatIndex = *First;
1758 return all_of(Mask,
1759 [First](int Elem) { return Elem == *First || Elem == -1; });
1760 }
1761};
1762
1763template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1764 PointerOpTy PointerOp;
1765 OffsetOpTy OffsetOp;
1766
1767 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1769
1770 template <typename OpTy> bool match(OpTy *V) {
1771 auto *GEP = dyn_cast<GEPOperator>(V);
1772 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
1773 PointerOp.match(GEP->getPointerOperand()) &&
1774 OffsetOp.match(GEP->idx_begin()->get());
1775 }
1776};
1777
1778/// Matches ShuffleVectorInst independently of mask value.
1779template <typename V1_t, typename V2_t>
1781m_Shuffle(const V1_t &v1, const V2_t &v2) {
1783}
1784
1785template <typename V1_t, typename V2_t, typename Mask_t>
1787m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
1788 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
1789}
1790
1791/// Matches LoadInst.
1792template <typename OpTy>
1795}
1796
1797/// Matches StoreInst.
1798template <typename ValueOpTy, typename PointerOpTy>
1800m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1802 PointerOp);
1803}
1804
1805/// Matches GetElementPtrInst.
1806template <typename... OperandTypes>
1807inline auto m_GEP(const OperandTypes &...Ops) {
1808 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
1809}
1810
1811/// Matches GEP with i8 source element type
1812template <typename PointerOpTy, typename OffsetOpTy>
1814m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
1815 return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
1816}
1817
1818//===----------------------------------------------------------------------===//
1819// Matchers for CastInst classes
1820//
1821
1822template <typename Op_t, unsigned Opcode> struct CastOperator_match {
1823 Op_t Op;
1824
1825 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
1826
1827 template <typename OpTy> bool match(OpTy *V) {
1828 if (auto *O = dyn_cast<Operator>(V))
1829 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1830 return false;
1831 }
1832};
1833
1834template <typename Op_t, typename Class> struct CastInst_match {
1835 Op_t Op;
1836
1837 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
1838
1839 template <typename OpTy> bool match(OpTy *V) {
1840 if (auto *I = dyn_cast<Class>(V))
1841 return Op.match(I->getOperand(0));
1842 return false;
1843 }
1844};
1845
1846template <typename Op_t> struct PtrToIntSameSize_match {
1848 Op_t Op;
1849
1850 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
1851 : DL(DL), Op(OpMatch) {}
1852
1853 template <typename OpTy> bool match(OpTy *V) {
1854 if (auto *O = dyn_cast<Operator>(V))
1855 return O->getOpcode() == Instruction::PtrToInt &&
1856 DL.getTypeSizeInBits(O->getType()) ==
1857 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
1858 Op.match(O->getOperand(0));
1859 return false;
1860 }
1861};
1862
1863template <typename Op_t> struct NNegZExt_match {
1864 Op_t Op;
1865
1866 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
1867
1868 template <typename OpTy> bool match(OpTy *V) {
1869 if (auto *I = dyn_cast<ZExtInst>(V))
1870 return I->hasNonNeg() && Op.match(I->getOperand(0));
1871 return false;
1872 }
1873};
1874
1875template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
1876 Op_t Op;
1877
1878 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
1879
1880 template <typename OpTy> bool match(OpTy *V) {
1881 if (auto *I = dyn_cast<TruncInst>(V))
1882 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
1883 Op.match(I->getOperand(0));
1884 return false;
1885 }
1886};
1887
1888/// Matches BitCast.
1889template <typename OpTy>
1891m_BitCast(const OpTy &Op) {
1893}
1894
1895template <typename Op_t> struct ElementWiseBitCast_match {
1896 Op_t Op;
1897
1898 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
1899
1900 template <typename OpTy> bool match(OpTy *V) {
1901 BitCastInst *I = dyn_cast<BitCastInst>(V);
1902 if (!I)
1903 return false;
1904 Type *SrcType = I->getSrcTy();
1905 Type *DstType = I->getType();
1906 // Make sure the bitcast doesn't change between scalar and vector and
1907 // doesn't change the number of vector elements.
1908 if (SrcType->isVectorTy() != DstType->isVectorTy())
1909 return false;
1910 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
1911 SrcVecTy && SrcVecTy->getElementCount() !=
1912 cast<VectorType>(DstType)->getElementCount())
1913 return false;
1914 return Op.match(I->getOperand(0));
1915 }
1916};
1917
1918template <typename OpTy>
1921}
1922
1923/// Matches PtrToInt.
1924template <typename OpTy>
1926m_PtrToInt(const OpTy &Op) {
1928}
1929
1930template <typename OpTy>
1932 const OpTy &Op) {
1934}
1935
1936/// Matches IntToPtr.
1937template <typename OpTy>
1939m_IntToPtr(const OpTy &Op) {
1941}
1942
1943/// Matches Trunc.
1944template <typename OpTy>
1947}
1948
1949/// Matches trunc nuw.
1950template <typename OpTy>
1952m_NUWTrunc(const OpTy &Op) {
1954}
1955
1956/// Matches trunc nsw.
1957template <typename OpTy>
1959m_NSWTrunc(const OpTy &Op) {
1961}
1962
1963template <typename OpTy>
1965m_TruncOrSelf(const OpTy &Op) {
1966 return m_CombineOr(m_Trunc(Op), Op);
1967}
1968
1969/// Matches SExt.
1970template <typename OpTy>
1973}
1974
1975/// Matches ZExt.
1976template <typename OpTy>
1979}
1980
1981template <typename OpTy>
1983 return NNegZExt_match<OpTy>(Op);
1984}
1985
1986template <typename OpTy>
1988m_ZExtOrSelf(const OpTy &Op) {
1989 return m_CombineOr(m_ZExt(Op), Op);
1990}
1991
1992template <typename OpTy>
1994m_SExtOrSelf(const OpTy &Op) {
1995 return m_CombineOr(m_SExt(Op), Op);
1996}
1997
1998/// Match either "sext" or "zext nneg".
1999template <typename OpTy>
2001m_SExtLike(const OpTy &Op) {
2002 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2003}
2004
2005template <typename OpTy>
2008m_ZExtOrSExt(const OpTy &Op) {
2009 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2010}
2011
2012template <typename OpTy>
2015 OpTy>
2017 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2018}
2019
2020template <typename OpTy>
2023}
2024
2025template <typename OpTy>
2028}
2029
2030template <typename OpTy>
2033}
2034
2035template <typename OpTy>
2038}
2039
2040template <typename OpTy>
2043}
2044
2045template <typename OpTy>
2048}
2049
2050//===----------------------------------------------------------------------===//
2051// Matchers for control flow.
2052//
2053
2054struct br_match {
2056
2058
2059 template <typename OpTy> bool match(OpTy *V) {
2060 if (auto *BI = dyn_cast<BranchInst>(V))
2061 if (BI->isUnconditional()) {
2062 Succ = BI->getSuccessor(0);
2063 return true;
2064 }
2065 return false;
2066 }
2067};
2068
2069inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2070
2071template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2073 Cond_t Cond;
2074 TrueBlock_t T;
2075 FalseBlock_t F;
2076
2077 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2078 : Cond(C), T(t), F(f) {}
2079
2080 template <typename OpTy> bool match(OpTy *V) {
2081 if (auto *BI = dyn_cast<BranchInst>(V))
2082 if (BI->isConditional() && Cond.match(BI->getCondition()))
2083 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2084 return false;
2085 }
2086};
2087
2088template <typename Cond_t>
2090m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
2093}
2094
2095template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2097m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2099}
2100
2101//===----------------------------------------------------------------------===//
2102// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2103//
2104
2105template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2106 bool Commutable = false>
2108 using PredType = Pred_t;
2111
2112 // The evaluation order is always stable, regardless of Commutability.
2113 // The LHS is always matched first.
2114 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2115
2116 template <typename OpTy> bool match(OpTy *V) {
2117 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2118 Intrinsic::ID IID = II->getIntrinsicID();
2119 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2120 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2121 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2122 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2123 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2124 return (L.match(LHS) && R.match(RHS)) ||
2125 (Commutable && L.match(RHS) && R.match(LHS));
2126 }
2127 }
2128 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2129 auto *SI = dyn_cast<SelectInst>(V);
2130 if (!SI)
2131 return false;
2132 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2133 if (!Cmp)
2134 return false;
2135 // At this point we have a select conditioned on a comparison. Check that
2136 // it is the values returned by the select that are being compared.
2137 auto *TrueVal = SI->getTrueValue();
2138 auto *FalseVal = SI->getFalseValue();
2139 auto *LHS = Cmp->getOperand(0);
2140 auto *RHS = Cmp->getOperand(1);
2141 if ((TrueVal != LHS || FalseVal != RHS) &&
2142 (TrueVal != RHS || FalseVal != LHS))
2143 return false;
2144 typename CmpInst_t::Predicate Pred =
2145 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2146 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2147 if (!Pred_t::match(Pred))
2148 return false;
2149 // It does! Bind the operands.
2150 return (L.match(LHS) && R.match(RHS)) ||
2151 (Commutable && L.match(RHS) && R.match(LHS));
2152 }
2153};
2154
2155/// Helper class for identifying signed max predicates.
2157 static bool match(ICmpInst::Predicate Pred) {
2158 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2159 }
2160};
2161
2162/// Helper class for identifying signed min predicates.
2164 static bool match(ICmpInst::Predicate Pred) {
2165 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2166 }
2167};
2168
2169/// Helper class for identifying unsigned max predicates.
2171 static bool match(ICmpInst::Predicate Pred) {
2172 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2173 }
2174};
2175
2176/// Helper class for identifying unsigned min predicates.
2178 static bool match(ICmpInst::Predicate Pred) {
2179 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2180 }
2181};
2182
2183/// Helper class for identifying ordered max predicates.
2185 static bool match(FCmpInst::Predicate Pred) {
2186 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2187 }
2188};
2189
2190/// Helper class for identifying ordered min predicates.
2192 static bool match(FCmpInst::Predicate Pred) {
2193 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2194 }
2195};
2196
2197/// Helper class for identifying unordered max predicates.
2199 static bool match(FCmpInst::Predicate Pred) {
2200 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2201 }
2202};
2203
2204/// Helper class for identifying unordered min predicates.
2206 static bool match(FCmpInst::Predicate Pred) {
2207 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2208 }
2209};
2210
2211template <typename LHS, typename RHS>
2213 const RHS &R) {
2215}
2216
2217template <typename LHS, typename RHS>
2219 const RHS &R) {
2221}
2222
2223template <typename LHS, typename RHS>
2225 const RHS &R) {
2227}
2228
2229template <typename LHS, typename RHS>
2231 const RHS &R) {
2233}
2234
2235template <typename LHS, typename RHS>
2236inline match_combine_or<
2241m_MaxOrMin(const LHS &L, const RHS &R) {
2242 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2243 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2244}
2245
2246/// Match an 'ordered' floating point maximum function.
2247/// Floating point has one special value 'NaN'. Therefore, there is no total
2248/// order. However, if we can ignore the 'NaN' value (for example, because of a
2249/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2250/// semantics. In the presence of 'NaN' we have to preserve the original
2251/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2252///
2253/// max(L, R) iff L and R are not NaN
2254/// m_OrdFMax(L, R) = R iff L or R are NaN
2255template <typename LHS, typename RHS>
2257 const RHS &R) {
2259}
2260
2261/// Match an 'ordered' floating point minimum function.
2262/// Floating point has one special value 'NaN'. Therefore, there is no total
2263/// order. However, if we can ignore the 'NaN' value (for example, because of a
2264/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2265/// semantics. In the presence of 'NaN' we have to preserve the original
2266/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2267///
2268/// min(L, R) iff L and R are not NaN
2269/// m_OrdFMin(L, R) = R iff L or R are NaN
2270template <typename LHS, typename RHS>
2272 const RHS &R) {
2274}
2275
2276/// Match an 'unordered' floating point maximum function.
2277/// Floating point has one special value 'NaN'. Therefore, there is no total
2278/// order. However, if we can ignore the 'NaN' value (for example, because of a
2279/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2280/// semantics. In the presence of 'NaN' we have to preserve the original
2281/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2282///
2283/// max(L, R) iff L and R are not NaN
2284/// m_UnordFMax(L, R) = L iff L or R are NaN
2285template <typename LHS, typename RHS>
2287m_UnordFMax(const LHS &L, const RHS &R) {
2289}
2290
2291/// Match an 'unordered' floating point minimum function.
2292/// Floating point has one special value 'NaN'. Therefore, there is no total
2293/// order. However, if we can ignore the 'NaN' value (for example, because of a
2294/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2295/// semantics. In the presence of 'NaN' we have to preserve the original
2296/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2297///
2298/// min(L, R) iff L and R are not NaN
2299/// m_UnordFMin(L, R) = L iff L or R are NaN
2300template <typename LHS, typename RHS>
2302m_UnordFMin(const LHS &L, const RHS &R) {
2304}
2305
2306//===----------------------------------------------------------------------===//
2307// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2308// Note that S might be matched to other instructions than AddInst.
2309//
2310
2311template <typename LHS_t, typename RHS_t, typename Sum_t>
2315 Sum_t S;
2316
2317 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2318 : L(L), R(R), S(S) {}
2319
2320 template <typename OpTy> bool match(OpTy *V) {
2321 Value *ICmpLHS, *ICmpRHS;
2323 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2324 return false;
2325
2326 Value *AddLHS, *AddRHS;
2327 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2328
2329 // (a + b) u< a, (a + b) u< b
2330 if (Pred == ICmpInst::ICMP_ULT)
2331 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2332 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2333
2334 // a >u (a + b), b >u (a + b)
2335 if (Pred == ICmpInst::ICMP_UGT)
2336 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2337 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2338
2339 Value *Op1;
2340 auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
2341 // (a ^ -1) <u b
2342 if (Pred == ICmpInst::ICMP_ULT) {
2343 if (XorExpr.match(ICmpLHS))
2344 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2345 }
2346 // b > u (a ^ -1)
2347 if (Pred == ICmpInst::ICMP_UGT) {
2348 if (XorExpr.match(ICmpRHS))
2349 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2350 }
2351
2352 // Match special-case for increment-by-1.
2353 if (Pred == ICmpInst::ICMP_EQ) {
2354 // (a + 1) == 0
2355 // (1 + a) == 0
2356 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2357 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2358 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2359 // 0 == (a + 1)
2360 // 0 == (1 + a)
2361 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2362 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2363 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2364 }
2365
2366 return false;
2367 }
2368};
2369
2370/// Match an icmp instruction checking for unsigned overflow on addition.
2371///
2372/// S is matched to the addition whose result is being checked for overflow, and
2373/// L and R are matched to the LHS and RHS of S.
2374template <typename LHS_t, typename RHS_t, typename Sum_t>
2376m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2378}
2379
2380template <typename Opnd_t> struct Argument_match {
2381 unsigned OpI;
2382 Opnd_t Val;
2383
2384 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2385
2386 template <typename OpTy> bool match(OpTy *V) {
2387 // FIXME: Should likely be switched to use `CallBase`.
2388 if (const auto *CI = dyn_cast<CallInst>(V))
2389 return Val.match(CI->getArgOperand(OpI));
2390 return false;
2391 }
2392};
2393
2394/// Match an argument.
2395template <unsigned OpI, typename Opnd_t>
2396inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2397 return Argument_match<Opnd_t>(OpI, Op);
2398}
2399
2400/// Intrinsic matchers.
2402 unsigned ID;
2403
2405
2406 template <typename OpTy> bool match(OpTy *V) {
2407 if (const auto *CI = dyn_cast<CallInst>(V))
2408 if (const auto *F = CI->getCalledFunction())
2409 return F->getIntrinsicID() == ID;
2410 return false;
2411 }
2412};
2413
2414/// Intrinsic matches are combinations of ID matchers, and argument
2415/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2416/// them with lower arity matchers. Here's some convenient typedefs for up to
2417/// several arguments, and more can be added as needed
2418template <typename T0 = void, typename T1 = void, typename T2 = void,
2419 typename T3 = void, typename T4 = void, typename T5 = void,
2420 typename T6 = void, typename T7 = void, typename T8 = void,
2421 typename T9 = void, typename T10 = void>
2423template <typename T0> struct m_Intrinsic_Ty<T0> {
2425};
2426template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2427 using Ty =
2429};
2430template <typename T0, typename T1, typename T2>
2431struct m_Intrinsic_Ty<T0, T1, T2> {
2434};
2435template <typename T0, typename T1, typename T2, typename T3>
2436struct m_Intrinsic_Ty<T0, T1, T2, T3> {
2439};
2440
2441template <typename T0, typename T1, typename T2, typename T3, typename T4>
2442struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
2445};
2446
2447template <typename T0, typename T1, typename T2, typename T3, typename T4,
2448 typename T5>
2449struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> {
2452};
2453
2454/// Match intrinsic calls like this:
2455/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2456template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2457 return IntrinsicID_match(IntrID);
2458}
2459
2460/// Matches MaskedLoad Intrinsic.
2461template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2463m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2464 const Opnd3 &Op3) {
2465 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
2466}
2467
2468/// Matches MaskedGather Intrinsic.
2469template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2471m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2472 const Opnd3 &Op3) {
2473 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
2474}
2475
2476template <Intrinsic::ID IntrID, typename T0>
2477inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2478 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
2479}
2480
2481template <Intrinsic::ID IntrID, typename T0, typename T1>
2482inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2483 const T1 &Op1) {
2484 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
2485}
2486
2487template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2488inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2489m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2490 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2491}
2492
2493template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2494 typename T3>
2496m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2497 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2498}
2499
2500template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2501 typename T3, typename T4>
2503m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2504 const T4 &Op4) {
2505 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2506 m_Argument<4>(Op4));
2507}
2508
2509template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2510 typename T3, typename T4, typename T5>
2512m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2513 const T4 &Op4, const T5 &Op5) {
2514 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2515 m_Argument<5>(Op5));
2516}
2517
2518// Helper intrinsic matching specializations.
2519template <typename Opnd0>
2520inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2521 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
2522}
2523
2524template <typename Opnd0>
2525inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2526 return m_Intrinsic<Intrinsic::bswap>(Op0);
2527}
2528
2529template <typename Opnd0>
2530inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2531 return m_Intrinsic<Intrinsic::fabs>(Op0);
2532}
2533
2534template <typename Opnd0>
2535inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2536 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
2537}
2538
2539template <typename Opnd0, typename Opnd1>
2540inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
2541 const Opnd1 &Op1) {
2542 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2543}
2544
2545template <typename Opnd0, typename Opnd1>
2546inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
2547 const Opnd1 &Op1) {
2548 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2549}
2550
2551template <typename Opnd0, typename Opnd1, typename Opnd2>
2553m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2554 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2555}
2556
2557template <typename Opnd0, typename Opnd1, typename Opnd2>
2559m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2560 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2561}
2562
2563template <typename Opnd0>
2564inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2565 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2566}
2567
2568template <typename Opnd0, typename Opnd1>
2569inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2570 const Opnd1 &Op1) {
2571 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2572}
2573
2574template <typename Opnd0>
2575inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2576 return m_Intrinsic<Intrinsic::vector_reverse>(Op0);
2577}
2578
2579//===----------------------------------------------------------------------===//
2580// Matchers for two-operands operators with the operators in either order
2581//
2582
2583/// Matches a BinaryOperator with LHS and RHS in either order.
2584template <typename LHS, typename RHS>
2587}
2588
2589/// Matches an ICmp with a predicate over LHS and RHS in either order.
2590/// Swaps the predicate if operands are commuted.
2591template <typename LHS, typename RHS>
2593m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
2595 R);
2596}
2597
2598/// Matches a specific opcode with LHS and RHS in either order.
2599template <typename LHS, typename RHS>
2601m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2602 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2603}
2604
2605/// Matches a Add with LHS and RHS in either order.
2606template <typename LHS, typename RHS>
2608 const RHS &R) {
2610}
2611
2612/// Matches a Mul with LHS and RHS in either order.
2613template <typename LHS, typename RHS>
2615 const RHS &R) {
2617}
2618
2619/// Matches an And with LHS and RHS in either order.
2620template <typename LHS, typename RHS>
2622 const RHS &R) {
2624}
2625
2626/// Matches an Or with LHS and RHS in either order.
2627template <typename LHS, typename RHS>
2629 const RHS &R) {
2631}
2632
2633/// Matches an Xor with LHS and RHS in either order.
2634template <typename LHS, typename RHS>
2636 const RHS &R) {
2638}
2639
2640/// Matches a 'Neg' as 'sub 0, V'.
2641template <typename ValTy>
2642inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2643m_Neg(const ValTy &V) {
2644 return m_Sub(m_ZeroInt(), V);
2645}
2646
2647/// Matches a 'Neg' as 'sub nsw 0, V'.
2648template <typename ValTy>
2650 Instruction::Sub,
2652m_NSWNeg(const ValTy &V) {
2653 return m_NSWSub(m_ZeroInt(), V);
2654}
2655
2656/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2657/// NOTE: we first match the 'Not' (by matching '-1'),
2658/// and only then match the inner matcher!
2659template <typename ValTy>
2660inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2661m_Not(const ValTy &V) {
2662 return m_c_Xor(m_AllOnes(), V);
2663}
2664
2665template <typename ValTy>
2666inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2667 true>
2668m_NotForbidPoison(const ValTy &V) {
2669 return m_c_Xor(m_AllOnesForbidPoison(), V);
2670}
2671
2672/// Matches an SMin with LHS and RHS in either order.
2673template <typename LHS, typename RHS>
2675m_c_SMin(const LHS &L, const RHS &R) {
2677}
2678/// Matches an SMax with LHS and RHS in either order.
2679template <typename LHS, typename RHS>
2681m_c_SMax(const LHS &L, const RHS &R) {
2683}
2684/// Matches a UMin with LHS and RHS in either order.
2685template <typename LHS, typename RHS>
2687m_c_UMin(const LHS &L, const RHS &R) {
2689}
2690/// Matches a UMax with LHS and RHS in either order.
2691template <typename LHS, typename RHS>
2693m_c_UMax(const LHS &L, const RHS &R) {
2695}
2696
2697template <typename LHS, typename RHS>
2698inline match_combine_or<
2703m_c_MaxOrMin(const LHS &L, const RHS &R) {
2704 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
2705 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
2706}
2707
2708template <Intrinsic::ID IntrID, typename T0, typename T1>
2711m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
2712 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
2713 m_Intrinsic<IntrID>(Op1, Op0));
2714}
2715
2716/// Matches FAdd with LHS and RHS in either order.
2717template <typename LHS, typename RHS>
2719m_c_FAdd(const LHS &L, const RHS &R) {
2721}
2722
2723/// Matches FMul with LHS and RHS in either order.
2724template <typename LHS, typename RHS>
2726m_c_FMul(const LHS &L, const RHS &R) {
2728}
2729
2730template <typename Opnd_t> struct Signum_match {
2731 Opnd_t Val;
2732 Signum_match(const Opnd_t &V) : Val(V) {}
2733
2734 template <typename OpTy> bool match(OpTy *V) {
2735 unsigned TypeSize = V->getType()->getScalarSizeInBits();
2736 if (TypeSize == 0)
2737 return false;
2738
2739 unsigned ShiftWidth = TypeSize - 1;
2740 Value *OpL = nullptr, *OpR = nullptr;
2741
2742 // This is the representation of signum we match:
2743 //
2744 // signum(x) == (x >> 63) | (-x >>u 63)
2745 //
2746 // An i1 value is its own signum, so it's correct to match
2747 //
2748 // signum(x) == (x >> 0) | (-x >>u 0)
2749 //
2750 // for i1 values.
2751
2752 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
2753 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
2754 auto Signum = m_Or(LHS, RHS);
2755
2756 return Signum.match(V) && OpL == OpR && Val.match(OpL);
2757 }
2758};
2759
2760/// Matches a signum pattern.
2761///
2762/// signum(x) =
2763/// x > 0 -> 1
2764/// x == 0 -> 0
2765/// x < 0 -> -1
2766template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
2767 return Signum_match<Val_t>(V);
2768}
2769
2770template <int Ind, typename Opnd_t> struct ExtractValue_match {
2771 Opnd_t Val;
2772 ExtractValue_match(const Opnd_t &V) : Val(V) {}
2773
2774 template <typename OpTy> bool match(OpTy *V) {
2775 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
2776 // If Ind is -1, don't inspect indices
2777 if (Ind != -1 &&
2778 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
2779 return false;
2780 return Val.match(I->getAggregateOperand());
2781 }
2782 return false;
2783 }
2784};
2785
2786/// Match a single index ExtractValue instruction.
2787/// For example m_ExtractValue<1>(...)
2788template <int Ind, typename Val_t>
2791}
2792
2793/// Match an ExtractValue instruction with any index.
2794/// For example m_ExtractValue(...)
2795template <typename Val_t>
2796inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
2797 return ExtractValue_match<-1, Val_t>(V);
2798}
2799
2800/// Matcher for a single index InsertValue instruction.
2801template <int Ind, typename T0, typename T1> struct InsertValue_match {
2804
2805 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
2806
2807 template <typename OpTy> bool match(OpTy *V) {
2808 if (auto *I = dyn_cast<InsertValueInst>(V)) {
2809 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
2810 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
2811 }
2812 return false;
2813 }
2814};
2815
2816/// Matches a single index InsertValue instruction.
2817template <int Ind, typename Val_t, typename Elt_t>
2819 const Elt_t &Elt) {
2820 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
2821}
2822
2823/// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
2824/// the constant expression
2825/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
2826/// under the right conditions determined by DataLayout.
2828 template <typename ITy> bool match(ITy *V) {
2829 if (m_Intrinsic<Intrinsic::vscale>().match(V))
2830 return true;
2831
2832 Value *Ptr;
2833 if (m_PtrToInt(m_Value(Ptr)).match(V)) {
2834 if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
2835 auto *DerefTy =
2836 dyn_cast<ScalableVectorType>(GEP->getSourceElementType());
2837 if (GEP->getNumIndices() == 1 && DerefTy &&
2838 DerefTy->getElementType()->isIntegerTy(8) &&
2839 m_Zero().match(GEP->getPointerOperand()) &&
2840 m_SpecificInt(1).match(GEP->idx_begin()->get()))
2841 return true;
2842 }
2843 }
2844
2845 return false;
2846 }
2847};
2848
2850 return VScaleVal_match();
2851}
2852
2853template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
2857
2858 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
2859
2860 template <typename T> bool match(T *V) {
2861 auto *I = dyn_cast<Instruction>(V);
2862 if (!I || !I->getType()->isIntOrIntVectorTy(1))
2863 return false;
2864
2865 if (I->getOpcode() == Opcode) {
2866 auto *Op0 = I->getOperand(0);
2867 auto *Op1 = I->getOperand(1);
2868 return (L.match(Op0) && R.match(Op1)) ||
2869 (Commutable && L.match(Op1) && R.match(Op0));
2870 }
2871
2872 if (auto *Select = dyn_cast<SelectInst>(I)) {
2873 auto *Cond = Select->getCondition();
2874 auto *TVal = Select->getTrueValue();
2875 auto *FVal = Select->getFalseValue();
2876
2877 // Don't match a scalar select of bool vectors.
2878 // Transforms expect a single type for operands if this matches.
2879 if (Cond->getType() != Select->getType())
2880 return false;
2881
2882 if (Opcode == Instruction::And) {
2883 auto *C = dyn_cast<Constant>(FVal);
2884 if (C && C->isNullValue())
2885 return (L.match(Cond) && R.match(TVal)) ||
2886 (Commutable && L.match(TVal) && R.match(Cond));
2887 } else {
2888 assert(Opcode == Instruction::Or);
2889 auto *C = dyn_cast<Constant>(TVal);
2890 if (C && C->isOneValue())
2891 return (L.match(Cond) && R.match(FVal)) ||
2892 (Commutable && L.match(FVal) && R.match(Cond));
2893 }
2894 }
2895
2896 return false;
2897 }
2898};
2899
2900/// Matches L && R either in the form of L & R or L ? R : false.
2901/// Note that the latter form is poison-blocking.
2902template <typename LHS, typename RHS>
2904 const RHS &R) {
2906}
2907
2908/// Matches L && R where L and R are arbitrary values.
2909inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
2910
2911/// Matches L && R with LHS and RHS in either order.
2912template <typename LHS, typename RHS>
2914m_c_LogicalAnd(const LHS &L, const RHS &R) {
2916}
2917
2918/// Matches L || R either in the form of L | R or L ? true : R.
2919/// Note that the latter form is poison-blocking.
2920template <typename LHS, typename RHS>
2922 const RHS &R) {
2924}
2925
2926/// Matches L || R where L and R are arbitrary values.
2927inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
2928
2929/// Matches L || R with LHS and RHS in either order.
2930template <typename LHS, typename RHS>
2932m_c_LogicalOr(const LHS &L, const RHS &R) {
2934}
2935
2936/// Matches either L && R or L || R,
2937/// either one being in the either binary or logical form.
2938/// Note that the latter form is poison-blocking.
2939template <typename LHS, typename RHS, bool Commutable = false>
2940inline auto m_LogicalOp(const LHS &L, const RHS &R) {
2941 return m_CombineOr(
2944}
2945
2946/// Matches either L && R or L || R where L and R are arbitrary values.
2947inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
2948
2949/// Matches either L && R or L || R with LHS and RHS in either order.
2950template <typename LHS, typename RHS>
2951inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
2952 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
2953}
2954
2955} // end namespace PatternMatch
2956} // end namespace llvm
2957
2958#endif // LLVM_IR_PATTERNMATCH_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu 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...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define check(cond)
Hexagon Common GEP
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define T1
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:76
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition: APInt.h:531
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
This class represents a no-op cast from one type to another.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
Base class for aggregate constants (with operands).
Definition: Constants.h:399
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1017
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:672
static 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.
Definition: Instruction.h:306
bool isShift() const
Definition: Instruction.h:259
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
'undef' values are things that do not have specified contents.
Definition: Constants.h:1348
LLVM Value Representation.
Definition: Value.h:74
Base class of all SIMD vector types.
Definition: DerivedTypes.h:403
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:641
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
#define UINT64_MAX
Definition: DataTypes.h:77
@ 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.
Definition: PatternMatch.h:518
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
Definition: PatternMatch.h:160
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:667
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.
apfloat_match m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
Definition: PatternMatch.h:327
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:544
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.
Definition: PatternMatch.h:100
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
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.
Definition: PatternMatch.h:658
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)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
Definition: PatternMatch.h:720
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.
Definition: PatternMatch.h:613
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)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
AllowReassoc_match< T > m_AllowReassoc(const T &SubPattern)
Definition: PatternMatch.h:83
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
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.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
Definition: PatternMatch.h:646
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
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.
Definition: PatternMatch.h:966
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)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMax(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
Definition: PatternMatch.h:509
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:810
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:758
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:869
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.
Definition: PatternMatch.h:186
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
Definition: PatternMatch.h:974
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'.
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.
Definition: PatternMatch.h:736
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:554
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:586
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.
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Definition: PatternMatch.h:776
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.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
Definition: PatternMatch.h:912
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.
Definition: PatternMatch.h:245
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
Definition: PatternMatch.h:501
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:816
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)
match_combine_or< typename m_Intrinsic_Ty< T0, T1 >::Ty, typename m_Intrinsic_Ty< T1, T0 >::Ty > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
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()...
Definition: PatternMatch.h:887
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:593
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
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)
Definition: PatternMatch.h:67
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'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:848
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
Definition: PatternMatch.h:999
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.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
Definition: PatternMatch.h:564
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.
Definition: PatternMatch.h:173
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
Definition: PatternMatch.h:711
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedLoad Intrinsic.
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
apint_match m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
Definition: PatternMatch.h:310
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)
cst_pred_ty< is_all_ones, false > m_AllOnesForbidPoison()
Definition: PatternMatch.h:522
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.
Definition: PatternMatch.h:155
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:105
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:621
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:633
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.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
Definition: PatternMatch.h:471
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:677
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:915
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.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
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".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
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.
match_combine_or< CastOperator_match< OpTy, Instruction::Trunc >, OpTy > m_TruncOrSelf(const OpTy &Op)
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.
Definition: PatternMatch.h:746
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
Definition: PatternMatch.h:95
VScaleVal_match m_VScale()
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.
Definition: PatternMatch.h:485
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)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
Definition: PatternMatch.h:532
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.
Definition: PatternMatch.h:92
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)
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.
Definition: PatternMatch.h:767
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.
Definition: PatternMatch.h:785
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.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
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)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition: PatternMatch.h:189
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
Definition: PatternMatch.h:576
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
Definition: PatternMatch.h:704
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'.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMin(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.
Definition: PatternMatch.h:606
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.
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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
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.
Definition: PatternMatch.h:727
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedGather Intrinsic.
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
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.
Definition: PatternMatch.h:692
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1722
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
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:1749
AllowReassoc_match(const SubPattern_t &SP)
Definition: PatternMatch.h:74
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)
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I)
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)
CastInst_match(const Op_t &OpMatch)
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
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)
NoWrapTrunc_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
Definition: PatternMatch.h:60
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)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
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)
Matches patterns for vscale.
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Definition: PatternMatch.h:430
apf_pred_ty(const APFloat *&R)
Definition: PatternMatch.h:433
apfloat_match(const APFloat *&Res, bool AllowPoison)
Definition: PatternMatch.h:278
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Definition: PatternMatch.h:403
apint_match(const APInt *&Res, bool AllowPoison)
Definition: PatternMatch.h:253
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...
Definition: PatternMatch.h:356
function_ref< bool(const APTy &)> CheckFn
Definition: PatternMatch.h:464
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
Definition: PatternMatch.h:873
bool isValue(const APInt &C)
Definition: PatternMatch.h:514
bool isValue(const APInt &C)
Definition: PatternMatch.h:497
bool isValue(const APFloat &C)
Definition: PatternMatch.h:754
bool isValue(const APFloat &C)
Definition: PatternMatch.h:732
bool isValue(const APFloat &C)
Definition: PatternMatch.h:742
bool isOpType(unsigned Opcode)
bool isValue(const APFloat &C)
Definition: PatternMatch.h:716
bool isOpType(unsigned Opcode)
bool isValue(const APFloat &C)
Definition: PatternMatch.h:700
bool isValue(const APFloat &C)
Definition: PatternMatch.h:772
bool isValue(const APInt &C)
Definition: PatternMatch.h:540
bool isValue(const APFloat &C)
Definition: PatternMatch.h:781
bool isValue(const APFloat &C)
Definition: PatternMatch.h:723
bool isValue(const APFloat &C)
Definition: PatternMatch.h:707
bool isValue(const APInt &C)
Definition: PatternMatch.h:582
bool isValue(const APFloat &C)
Definition: PatternMatch.h:763
bool isValue(const APInt &C)
Definition: PatternMatch.h:609
bool isOpType(unsigned Opcode)
bool isValue(const APInt &C)
Definition: PatternMatch.h:654
bool isValue(const APInt &C)
Definition: PatternMatch.h:589
Intrinsic matches are combinations of ID matchers, and argument matchers.
bool match(ArrayRef< int > Mask)
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask)
m_SpecificMask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask)
bool match(ArrayRef< int > Mask)
match_combine_and(const LTy &Left, const RTy &Right)
Definition: PatternMatch.h:227
match_combine_or(const LTy &Left, const RTy &Right)
Definition: PatternMatch.h:212
match_unless(const Ty &Matcher)
Definition: PatternMatch.h:197
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.
Definition: PatternMatch.h:987
Match a specified floating point value or vector of all elements of that value.
Definition: PatternMatch.h:894
Match a specified integer value or vector of all elements of that value.
Definition: PatternMatch.h:934
Match a specified Value*.
Definition: PatternMatch.h:860
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)
Definition: PatternMatch.h:108