LLVM 22.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 P.match(V);
51}
52
53template <typename Val, typename Pattern> struct MatchFunctor {
54 const Pattern &P;
55 MatchFunctor(const Pattern &P) : P(P) {}
56 bool operator()(Val *V) const { return P.match(V); }
57};
58
59/// A match functor that can be used as a UnaryPredicate in functional
60/// algorithms like all_of.
61template <typename Val = const Value, typename Pattern>
65
66template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
67 return P.match(Mask);
68}
69
70template <typename SubPattern_t> struct OneUse_match {
71 SubPattern_t SubPattern;
72
73 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
74
75 template <typename OpTy> bool match(OpTy *V) const {
76 return V->hasOneUse() && SubPattern.match(V);
77 }
78};
79
80template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
81 return SubPattern;
82}
83
84template <typename SubPattern_t> struct AllowReassoc_match {
85 SubPattern_t SubPattern;
86
87 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
88
89 template <typename OpTy> bool match(OpTy *V) const {
90 auto *I = dyn_cast<FPMathOperator>(V);
91 return I && I->hasAllowReassoc() && SubPattern.match(I);
92 }
93};
94
95template <typename T>
96inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
97 return SubPattern;
98}
99
100template <typename Class> struct class_match {
101 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
102};
103
104/// Match an arbitrary value and ignore it.
106
107/// Match an arbitrary unary operation and ignore it.
111
112/// Match an arbitrary binary operation and ignore it.
116
117/// Matches any compare instruction and ignore it.
119
121 static bool check(const Value *V) {
122 if (isa<UndefValue>(V))
123 return true;
124
125 const auto *CA = dyn_cast<ConstantAggregate>(V);
126 if (!CA)
127 return false;
128
131
132 // Either UndefValue, PoisonValue, or an aggregate that only contains
133 // these is accepted by matcher.
134 // CheckValue returns false if CA cannot satisfy this constraint.
135 auto CheckValue = [&](const ConstantAggregate *CA) {
136 for (const Value *Op : CA->operand_values()) {
137 if (isa<UndefValue>(Op))
138 continue;
139
140 const auto *CA = dyn_cast<ConstantAggregate>(Op);
141 if (!CA)
142 return false;
143 if (Seen.insert(CA).second)
144 Worklist.emplace_back(CA);
145 }
146
147 return true;
148 };
149
150 if (!CheckValue(CA))
151 return false;
152
153 while (!Worklist.empty()) {
154 if (!CheckValue(Worklist.pop_back_val()))
155 return false;
156 }
157 return true;
158 }
159 template <typename ITy> bool match(ITy *V) const { return check(V); }
160};
161
162/// Match an arbitrary undef constant. This matches poison as well.
163/// If this is an aggregate and contains a non-aggregate element that is
164/// neither undef nor poison, the aggregate is not matched.
165inline auto m_Undef() { return undef_match(); }
166
167/// Match an arbitrary UndefValue constant.
171
172/// Match an arbitrary poison constant.
176
177/// Match an arbitrary Constant and ignore it.
179
180/// Match an arbitrary ConstantInt and ignore it.
184
185/// Match an arbitrary ConstantFP and ignore it.
189
191 template <typename ITy> bool match(ITy *V) const {
192 auto *C = dyn_cast<Constant>(V);
193 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
194 }
195};
196
197/// Match a constant expression or a constant that contains a constant
198/// expression.
200
201/// Match an arbitrary basic block value and ignore it.
205
206/// Inverting matcher
207template <typename Ty> struct match_unless {
208 Ty M;
209
210 match_unless(const Ty &Matcher) : M(Matcher) {}
211
212 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
213};
214
215/// Match if the inner matcher does *NOT* match.
216template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
217 return match_unless<Ty>(M);
218}
219
220/// Matching combinators
221template <typename LTy, typename RTy> struct match_combine_or {
222 LTy L;
223 RTy R;
224
225 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
226
227 template <typename ITy> bool match(ITy *V) const {
228 if (L.match(V))
229 return true;
230 if (R.match(V))
231 return true;
232 return false;
233 }
234};
235
236template <typename LTy, typename RTy> struct match_combine_and {
237 LTy L;
238 RTy R;
239
240 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
241
242 template <typename ITy> bool match(ITy *V) const {
243 if (L.match(V))
244 if (R.match(V))
245 return true;
246 return false;
247 }
248};
249
250/// Combine two pattern matchers matching L || R
251template <typename LTy, typename RTy>
252inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
253 return match_combine_or<LTy, RTy>(L, R);
254}
255
256/// Combine two pattern matchers matching L && R
257template <typename LTy, typename RTy>
258inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
259 return match_combine_and<LTy, RTy>(L, R);
260}
261
263 const APInt *&Res;
265
268
269 template <typename ITy> bool match(ITy *V) const {
270 if (auto *CI = dyn_cast<ConstantInt>(V)) {
271 Res = &CI->getValue();
272 return true;
273 }
274 if (V->getType()->isVectorTy())
275 if (const auto *C = dyn_cast<Constant>(V))
276 if (auto *CI =
278 Res = &CI->getValue();
279 return true;
280 }
281 return false;
282 }
283};
284// Either constexpr if or renaming ConstantFP::getValueAPF to
285// ConstantFP::getValue is needed to do it via single template
286// function for both apint/apfloat.
288 const APFloat *&Res;
290
293
294 template <typename ITy> bool match(ITy *V) const {
295 if (auto *CI = dyn_cast<ConstantFP>(V)) {
296 Res = &CI->getValueAPF();
297 return true;
298 }
299 if (V->getType()->isVectorTy())
300 if (const auto *C = dyn_cast<Constant>(V))
301 if (auto *CI =
302 dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowPoison))) {
303 Res = &CI->getValueAPF();
304 return true;
305 }
306 return false;
307 }
308};
309
310/// Match a ConstantInt or splatted ConstantVector, binding the
311/// specified pointer to the contained APInt.
312inline apint_match m_APInt(const APInt *&Res) {
313 // Forbid poison by default to maintain previous behavior.
314 return apint_match(Res, /* AllowPoison */ false);
315}
316
317/// Match APInt while allowing poison in splat vector constants.
319 return apint_match(Res, /* AllowPoison */ true);
320}
321
322/// Match APInt while forbidding poison in splat vector constants.
324 return apint_match(Res, /* AllowPoison */ false);
325}
326
327/// Match a ConstantFP or splatted ConstantVector, binding the
328/// specified pointer to the contained APFloat.
329inline apfloat_match m_APFloat(const APFloat *&Res) {
330 // Forbid undefs by default to maintain previous behavior.
331 return apfloat_match(Res, /* AllowPoison */ false);
332}
333
334/// Match APFloat while allowing poison in splat vector constants.
336 return apfloat_match(Res, /* AllowPoison */ true);
337}
338
339/// Match APFloat while forbidding poison in splat vector constants.
341 return apfloat_match(Res, /* AllowPoison */ false);
342}
343
344template <int64_t Val> struct constantint_match {
345 template <typename ITy> bool match(ITy *V) const {
346 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
347 const APInt &CIV = CI->getValue();
348 if (Val >= 0)
349 return CIV == static_cast<uint64_t>(Val);
350 // If Val is negative, and CI is shorter than it, truncate to the right
351 // number of bits. If it is larger, then we have to sign extend. Just
352 // compare their negated values.
353 return -CIV == -Val;
354 }
355 return false;
356 }
357};
358
359/// Match a ConstantInt with a specific value.
360template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
361 return constantint_match<Val>();
362}
363
364/// This helper class is used to match constant scalars, vector splats,
365/// and fixed width vectors that satisfy a specified predicate.
366/// For fixed width vector constants, poison elements are ignored if AllowPoison
367/// is true.
368template <typename Predicate, typename ConstantVal, bool AllowPoison>
369struct cstval_pred_ty : public Predicate {
370 const Constant **Res = nullptr;
371 template <typename ITy> bool match_impl(ITy *V) const {
372 if (const auto *CV = dyn_cast<ConstantVal>(V))
373 return this->isValue(CV->getValue());
374 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
375 if (const auto *C = dyn_cast<Constant>(V)) {
376 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
377 return this->isValue(CV->getValue());
378
379 // Number of elements of a scalable vector unknown at compile time
380 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
381 if (!FVTy)
382 return false;
383
384 // Non-splat vector constant: check each element for a match.
385 unsigned NumElts = FVTy->getNumElements();
386 assert(NumElts != 0 && "Constant vector with no elements?");
387 bool HasNonPoisonElements = false;
388 for (unsigned i = 0; i != NumElts; ++i) {
389 Constant *Elt = C->getAggregateElement(i);
390 if (!Elt)
391 return false;
392 if (AllowPoison && isa<PoisonValue>(Elt))
393 continue;
394 auto *CV = dyn_cast<ConstantVal>(Elt);
395 if (!CV || !this->isValue(CV->getValue()))
396 return false;
397 HasNonPoisonElements = true;
398 }
399 return HasNonPoisonElements;
400 }
401 }
402 return false;
403 }
404
405 template <typename ITy> bool match(ITy *V) const {
406 if (this->match_impl(V)) {
407 if (Res)
408 *Res = cast<Constant>(V);
409 return true;
410 }
411 return false;
412 }
413};
414
415/// specialization of cstval_pred_ty for ConstantInt
416template <typename Predicate, bool AllowPoison = true>
418
419/// specialization of cstval_pred_ty for ConstantFP
420template <typename Predicate>
422 /*AllowPoison=*/true>;
423
424/// This helper class is used to match scalar and vector constants that
425/// satisfy a specified predicate, and bind them to an APInt.
426template <typename Predicate> struct api_pred_ty : public Predicate {
427 const APInt *&Res;
428
429 api_pred_ty(const APInt *&R) : Res(R) {}
430
431 template <typename ITy> bool match(ITy *V) const {
432 if (const auto *CI = dyn_cast<ConstantInt>(V))
433 if (this->isValue(CI->getValue())) {
434 Res = &CI->getValue();
435 return true;
436 }
437 if (V->getType()->isVectorTy())
438 if (const auto *C = dyn_cast<Constant>(V))
439 if (auto *CI = dyn_cast_or_null<ConstantInt>(
440 C->getSplatValue(/*AllowPoison=*/true)))
441 if (this->isValue(CI->getValue())) {
442 Res = &CI->getValue();
443 return true;
444 }
445
446 return false;
447 }
448};
449
450/// This helper class is used to match scalar and vector constants that
451/// satisfy a specified predicate, and bind them to an APFloat.
452/// Poison is allowed in splat vector constants.
453template <typename Predicate> struct apf_pred_ty : public Predicate {
454 const APFloat *&Res;
455
456 apf_pred_ty(const APFloat *&R) : Res(R) {}
457
458 template <typename ITy> bool match(ITy *V) const {
459 if (const auto *CI = dyn_cast<ConstantFP>(V))
460 if (this->isValue(CI->getValue())) {
461 Res = &CI->getValue();
462 return true;
463 }
464 if (V->getType()->isVectorTy())
465 if (const auto *C = dyn_cast<Constant>(V))
466 if (auto *CI = dyn_cast_or_null<ConstantFP>(
467 C->getSplatValue(/* AllowPoison */ true)))
468 if (this->isValue(CI->getValue())) {
469 Res = &CI->getValue();
470 return true;
471 }
472
473 return false;
474 }
475};
476
477///////////////////////////////////////////////////////////////////////////////
478//
479// Encapsulate constant value queries for use in templated predicate matchers.
480// This allows checking if constants match using compound predicates and works
481// with vector constants, possibly with relaxed constraints. For example, ignore
482// undef values.
483//
484///////////////////////////////////////////////////////////////////////////////
485
486template <typename APTy> struct custom_checkfn {
487 function_ref<bool(const APTy &)> CheckFn;
488 bool isValue(const APTy &C) const { return CheckFn(C); }
489};
490
491/// Match an integer or vector where CheckFn(ele) for each element is true.
492/// For vectors, poison elements are assumed to match.
494m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
495 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
496}
497
499m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
500 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
501}
502
503/// Match a float or vector where CheckFn(ele) for each element is true.
504/// For vectors, poison elements are assumed to match.
506m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
507 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
508}
509
511m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
512 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
513}
514
516 bool isValue(const APInt &C) const { return true; }
517};
518/// Match an integer or vector with any integral constant.
519/// For vectors, this includes constants with undefined elements.
523
525 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
526};
527
531
533 bool isValue(const APInt &C) const { return C.isAllOnes(); }
534};
535/// Match an integer or vector with all bits set.
536/// For vectors, this includes constants with undefined elements.
540
544
546 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
547};
548/// Match an integer or vector with values having all bits except for the high
549/// bit set (0x7f...).
550/// For vectors, this includes constants with undefined elements.
555 return V;
556}
557
559 bool isValue(const APInt &C) const { return C.isNegative(); }
560};
561/// Match an integer or vector of negative values.
562/// For vectors, this includes constants with undefined elements.
566inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
567
569 bool isValue(const APInt &C) const { return C.isNonNegative(); }
570};
571/// Match an integer or vector of non-negative values.
572/// For vectors, this includes constants with undefined elements.
576inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
577
579 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
580};
581/// Match an integer or vector of strictly positive values.
582/// For vectors, this includes constants with undefined elements.
587 return V;
588}
589
591 bool isValue(const APInt &C) const { return C.isNonPositive(); }
592};
593/// Match an integer or vector of non-positive values.
594/// For vectors, this includes constants with undefined elements.
598inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
599
600struct is_one {
601 bool isValue(const APInt &C) const { return C.isOne(); }
602};
603/// Match an integer 1 or a vector with all elements equal to 1.
604/// For vectors, this includes constants with undefined elements.
606
608 bool isValue(const APInt &C) const { return C.isZero(); }
609};
610/// Match an integer 0 or a vector with all elements equal to 0.
611/// For vectors, this includes constants with undefined elements.
615
616struct is_zero {
617 template <typename ITy> bool match(ITy *V) const {
618 auto *C = dyn_cast<Constant>(V);
619 // FIXME: this should be able to do something for scalable vectors
620 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
621 }
622};
623/// Match any null constant or a vector with all elements equal to 0.
624/// For vectors, this includes constants with undefined elements.
625inline is_zero m_Zero() { return is_zero(); }
626
627struct is_power2 {
628 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
629};
630/// Match an integer or vector power-of-2.
631/// For vectors, this includes constants with undefined elements.
633inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
634
636 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
637};
638/// Match a integer or vector negated power-of-2.
639/// For vectors, this includes constants with undefined elements.
644 return V;
645}
646
648 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
649};
650/// Match a integer or vector negated power-of-2.
651/// For vectors, this includes constants with undefined elements.
657 return V;
658}
659
661 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
662};
663/// Match an integer or vector of 0 or power-of-2 values.
664/// For vectors, this includes constants with undefined elements.
669 return V;
670}
671
673 bool isValue(const APInt &C) const { return C.isSignMask(); }
674};
675/// Match an integer or vector with only the sign bit(s) set.
676/// For vectors, this includes constants with undefined elements.
680
682 bool isValue(const APInt &C) const { return C.isMask(); }
683};
684/// Match an integer or vector with only the low bit(s) set.
685/// For vectors, this includes constants with undefined elements.
689inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
690
692 bool isValue(const APInt &C) const { return !C || C.isMask(); }
693};
694/// Match an integer or vector with only the low bit(s) set.
695/// For vectors, this includes constants with undefined elements.
700 return V;
701}
702
705 const APInt *Thr;
706 bool isValue(const APInt &C) const {
707 return ICmpInst::compare(C, *Thr, Pred);
708 }
709};
710/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
711/// to Threshold. For vectors, this includes constants with undefined elements.
715 P.Pred = Predicate;
716 P.Thr = &Threshold;
717 return P;
718}
719
720struct is_nan {
721 bool isValue(const APFloat &C) const { return C.isNaN(); }
722};
723/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
724/// For vectors, this includes constants with undefined elements.
726
727struct is_nonnan {
728 bool isValue(const APFloat &C) const { return !C.isNaN(); }
729};
730/// Match a non-NaN FP constant.
731/// For vectors, this includes constants with undefined elements.
735
736struct is_inf {
737 bool isValue(const APFloat &C) const { return C.isInfinity(); }
738};
739/// Match a positive or negative infinity FP constant.
740/// For vectors, this includes constants with undefined elements.
742
743struct is_noninf {
744 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
745};
746/// Match a non-infinity FP constant, i.e. finite or NaN.
747/// For vectors, this includes constants with undefined elements.
751
752struct is_finite {
753 bool isValue(const APFloat &C) const { return C.isFinite(); }
754};
755/// Match a finite FP constant, i.e. not infinity or NaN.
756/// For vectors, this includes constants with undefined elements.
760inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
761
763 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
764};
765/// Match a finite non-zero FP constant.
766/// For vectors, this includes constants with undefined elements.
771 return V;
772}
773
775 bool isValue(const APFloat &C) const { return C.isZero(); }
776};
777/// Match a floating-point negative zero or positive zero.
778/// For vectors, this includes constants with undefined elements.
782
784 bool isValue(const APFloat &C) const { return C.isPosZero(); }
785};
786/// Match a floating-point positive zero.
787/// For vectors, this includes constants with undefined elements.
791
793 bool isValue(const APFloat &C) const { return C.isNegZero(); }
794};
795/// Match a floating-point negative zero.
796/// For vectors, this includes constants with undefined elements.
800
802 bool isValue(const APFloat &C) const { return C.isNonZero(); }
803};
804/// Match a floating-point non-zero.
805/// For vectors, this includes constants with undefined elements.
809
811 bool isValue(const APFloat &C) const {
812 return !C.isDenormal() && C.isNonZero();
813 }
814};
815
816/// Match a floating-point non-zero that is not a denormal.
817/// For vectors, this includes constants with undefined elements.
821
822///////////////////////////////////////////////////////////////////////////////
823
824template <typename Class> struct bind_ty {
825 Class *&VR;
826
827 bind_ty(Class *&V) : VR(V) {}
828
829 template <typename ITy> bool match(ITy *V) const {
830 if (auto *CV = dyn_cast<Class>(V)) {
831 VR = CV;
832 return true;
833 }
834 return false;
835 }
836};
837
838/// Check whether the value has the given Class and matches the nested
839/// pattern. Capture it into the provided variable if successful.
840template <typename Class, typename MatchTy> struct bind_and_match_ty {
841 Class *&VR;
842 MatchTy Match;
843
844 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
845
846 template <typename ITy> bool match(ITy *V) const {
847 auto *CV = dyn_cast<Class>(V);
848 if (CV && Match.match(V)) {
849 VR = CV;
850 return true;
851 }
852 return false;
853 }
854};
855
856/// Match a value, capturing it if we match.
857inline bind_ty<Value> m_Value(Value *&V) { return V; }
858inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
859
860/// Match against the nested pattern, and capture the value if we match.
861template <typename MatchTy>
863 const MatchTy &Match) {
864 return {V, Match};
865}
866
867/// Match against the nested pattern, and capture the value if we match.
868template <typename MatchTy>
870 const MatchTy &Match) {
871 return {V, Match};
872}
873
874/// Match an instruction, capturing it if we match.
876
877/// Match against the nested pattern, and capture the instruction if we match.
878template <typename MatchTy>
880m_Instruction(Instruction *&I, const MatchTy &Match) {
881 return {I, Match};
882}
883
884/// Match a unary operator, capturing it if we match.
886/// Match a binary operator, capturing it if we match.
888/// Match a with overflow intrinsic, capturing it if we match.
894 return I;
895}
896
897/// Match an UndefValue, capturing the value if we match.
899
900/// Match a Constant, capturing the value if we match.
902
903/// Match a ConstantInt, capturing the value if we match.
905
906/// Match a ConstantFP, capturing the value if we match.
908
909/// Match a ConstantExpr, capturing the value if we match.
911
912/// Match a basic block value, capturing it if we match.
915 return V;
916}
917
918// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
919// and use m_Unless(m_ConstantExpr).
921 template <typename ITy> static bool isImmConstant(ITy *V) {
922 if (auto *CV = dyn_cast<Constant>(V)) {
923 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
924 return true;
925
926 if (CV->getType()->isVectorTy()) {
927 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
928 if (!isa<ConstantExpr>(Splat) &&
929 !Splat->containsConstantExpression()) {
930 return true;
931 }
932 }
933 }
934 }
935 return false;
936 }
937};
938
940 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
941};
942
943/// Match an arbitrary immediate Constant and ignore it.
945
948
950
951 template <typename ITy> bool match(ITy *V) const {
952 if (isImmConstant(V)) {
953 VR = cast<Constant>(V);
954 return true;
955 }
956 return false;
957 }
958};
959
960/// Match an immediate Constant, capturing the value if we match.
964
965/// Match a specified Value*.
967 const Value *Val;
968
969 specificval_ty(const Value *V) : Val(V) {}
970
971 template <typename ITy> bool match(ITy *V) const { return V == Val; }
972};
973
974/// Match if we have a specific specified value.
975inline specificval_ty m_Specific(const Value *V) { return V; }
976
977/// Stores a reference to the Value *, not the Value * itself,
978/// thus can be used in commutative matchers.
979template <typename Class> struct deferredval_ty {
980 Class *const &Val;
981
982 deferredval_ty(Class *const &V) : Val(V) {}
983
984 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
985};
986
987/// Like m_Specific(), but works if the specific value to match is determined
988/// as part of the same match() expression. For example:
989/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
990/// bind X before the pattern match starts.
991/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
992/// whichever value m_Value(X) populated.
993inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
995 return V;
996}
997
998/// Match a specified floating point value or vector of all elements of
999/// that value.
1001 double Val;
1002
1003 specific_fpval(double V) : Val(V) {}
1004
1005 template <typename ITy> bool match(ITy *V) const {
1006 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1007 return CFP->isExactlyValue(Val);
1008 if (V->getType()->isVectorTy())
1009 if (const auto *C = dyn_cast<Constant>(V))
1010 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1011 return CFP->isExactlyValue(Val);
1012 return false;
1013 }
1014};
1015
1016/// Match a specific floating point value or vector with all elements
1017/// equal to the value.
1018inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1019
1020/// Match a float 1.0 or vector with all elements equal to 1.0.
1021inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1022
1025
1027
1028 template <typename ITy> bool match(ITy *V) const {
1029 const APInt *ConstInt;
1030 if (!apint_match(ConstInt, /*AllowPoison=*/false).match(V))
1031 return false;
1032 if (ConstInt->getActiveBits() > 64)
1033 return false;
1034 VR = ConstInt->getZExtValue();
1035 return true;
1036 }
1037};
1038
1039/// Match a specified integer value or vector of all elements of that
1040/// value.
1041template <bool AllowPoison> struct specific_intval {
1042 const APInt &Val;
1043
1044 specific_intval(const APInt &V) : Val(V) {}
1045
1046 template <typename ITy> bool match(ITy *V) const {
1047 const auto *CI = dyn_cast<ConstantInt>(V);
1048 if (!CI && V->getType()->isVectorTy())
1049 if (const auto *C = dyn_cast<Constant>(V))
1050 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1051
1052 return CI && APInt::isSameValue(CI->getValue(), Val);
1053 }
1054};
1055
1056template <bool AllowPoison> struct specific_intval64 {
1058
1060
1061 template <typename ITy> bool match(ITy *V) const {
1062 const auto *CI = dyn_cast<ConstantInt>(V);
1063 if (!CI && V->getType()->isVectorTy())
1064 if (const auto *C = dyn_cast<Constant>(V))
1065 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1066
1067 return CI && CI->getValue() == Val;
1068 }
1069};
1070
1071/// Match a specific integer value or vector with all elements equal to
1072/// the value.
1074 return specific_intval<false>(V);
1075}
1076
1080
1084
1088
1089/// Match a ConstantInt and bind to its value. This does not match
1090/// ConstantInts wider than 64-bits.
1092
1093/// Match a specified basic block value.
1096
1098
1099 template <typename ITy> bool match(ITy *V) const {
1100 const auto *BB = dyn_cast<BasicBlock>(V);
1101 return BB && BB == Val;
1102 }
1103};
1104
1105/// Match a specific basic block value.
1107 return specific_bbval(BB);
1108}
1109
1110/// A commutative-friendly version of m_Specific().
1112 return BB;
1113}
1115m_Deferred(const BasicBlock *const &BB) {
1116 return BB;
1117}
1118
1119//===----------------------------------------------------------------------===//
1120// Matcher for any binary operator.
1121//
1122template <typename LHS_t, typename RHS_t, bool Commutable = false>
1126
1127 // The evaluation order is always stable, regardless of Commutability.
1128 // The LHS is always matched first.
1129 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1130
1131 template <typename OpTy> bool match(OpTy *V) const {
1132 if (auto *I = dyn_cast<BinaryOperator>(V))
1133 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1134 (Commutable && L.match(I->getOperand(1)) &&
1135 R.match(I->getOperand(0)));
1136 return false;
1137 }
1138};
1139
1140template <typename LHS, typename RHS>
1141inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1142 return AnyBinaryOp_match<LHS, RHS>(L, R);
1143}
1144
1145//===----------------------------------------------------------------------===//
1146// Matcher for any unary operator.
1147// TODO fuse unary, binary matcher into n-ary matcher
1148//
1149template <typename OP_t> struct AnyUnaryOp_match {
1150 OP_t X;
1151
1152 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1153
1154 template <typename OpTy> bool match(OpTy *V) const {
1155 if (auto *I = dyn_cast<UnaryOperator>(V))
1156 return X.match(I->getOperand(0));
1157 return false;
1158 }
1159};
1160
1161template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1162 return AnyUnaryOp_match<OP_t>(X);
1163}
1164
1165//===----------------------------------------------------------------------===//
1166// Matchers for specific binary operators.
1167//
1168
1169template <typename LHS_t, typename RHS_t, unsigned Opcode,
1170 bool Commutable = false>
1174
1175 // The evaluation order is always stable, regardless of Commutability.
1176 // The LHS is always matched first.
1177 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1178
1179 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1180 if (V->getValueID() == Value::InstructionVal + Opc) {
1181 auto *I = cast<BinaryOperator>(V);
1182 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1183 (Commutable && L.match(I->getOperand(1)) &&
1184 R.match(I->getOperand(0)));
1185 }
1186 return false;
1187 }
1188
1189 template <typename OpTy> bool match(OpTy *V) const {
1190 return match(Opcode, V);
1191 }
1192};
1193
1194template <typename LHS, typename RHS>
1199
1200template <typename LHS, typename RHS>
1205
1206template <typename LHS, typename RHS>
1211
1212template <typename LHS, typename RHS>
1217
1218template <typename Op_t> struct FNeg_match {
1219 Op_t X;
1220
1221 FNeg_match(const Op_t &Op) : X(Op) {}
1222 template <typename OpTy> bool match(OpTy *V) const {
1223 auto *FPMO = dyn_cast<FPMathOperator>(V);
1224 if (!FPMO)
1225 return false;
1226
1227 if (FPMO->getOpcode() == Instruction::FNeg)
1228 return X.match(FPMO->getOperand(0));
1229
1230 if (FPMO->getOpcode() == Instruction::FSub) {
1231 if (FPMO->hasNoSignedZeros()) {
1232 // With 'nsz', any zero goes.
1233 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1234 return false;
1235 } else {
1236 // Without 'nsz', we need fsub -0.0, X exactly.
1237 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1238 return false;
1239 }
1240
1241 return X.match(FPMO->getOperand(1));
1242 }
1243
1244 return false;
1245 }
1246};
1247
1248/// Match 'fneg X' as 'fsub -0.0, X'.
1249template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1250 return FNeg_match<OpTy>(X);
1251}
1252
1253/// Match 'fneg X' as 'fsub +-0.0, X'.
1254template <typename RHS>
1255inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1256m_FNegNSZ(const RHS &X) {
1257 return m_FSub(m_AnyZeroFP(), X);
1258}
1259
1260template <typename LHS, typename RHS>
1265
1266template <typename LHS, typename RHS>
1271
1272template <typename LHS, typename RHS>
1277
1278template <typename LHS, typename RHS>
1283
1284template <typename LHS, typename RHS>
1289
1290template <typename LHS, typename RHS>
1295
1296template <typename LHS, typename RHS>
1301
1302template <typename LHS, typename RHS>
1307
1308template <typename LHS, typename RHS>
1313
1314template <typename LHS, typename RHS>
1319
1320template <typename LHS, typename RHS>
1325
1326template <typename LHS, typename RHS>
1331
1332template <typename LHS, typename RHS>
1337
1338template <typename LHS, typename RHS>
1343
1344template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1347
1349
1350 template <typename OpTy> bool match(OpTy *V) const {
1351 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1352 if (Op->getOpcode() == Opcode)
1353 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1354 L.match(Op->getOperand(0));
1355 }
1356 // Interpreted as shiftop V, 0
1357 R = 0;
1358 return L.match(V);
1359 }
1360};
1361
1362/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1363template <typename LHS>
1368
1369/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1370template <typename LHS>
1375
1376/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1377template <typename LHS>
1382
1383template <typename LHS_t, typename RHS_t, unsigned Opcode,
1384 unsigned WrapFlags = 0, bool Commutable = false>
1388
1390 : L(LHS), R(RHS) {}
1391
1392 template <typename OpTy> bool match(OpTy *V) const {
1393 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1394 if (Op->getOpcode() != Opcode)
1395 return false;
1397 !Op->hasNoUnsignedWrap())
1398 return false;
1399 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1400 !Op->hasNoSignedWrap())
1401 return false;
1402 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1403 (Commutable && L.match(Op->getOperand(1)) &&
1404 R.match(Op->getOperand(0)));
1405 }
1406 return false;
1407 }
1408};
1409
1410template <typename LHS, typename RHS>
1411inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1413m_NSWAdd(const LHS &L, const RHS &R) {
1414 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1416 R);
1417}
1418template <typename LHS, typename RHS>
1419inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1421m_c_NSWAdd(const LHS &L, const RHS &R) {
1422 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1424 true>(L, R);
1425}
1426template <typename LHS, typename RHS>
1427inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1429m_NSWSub(const LHS &L, const RHS &R) {
1430 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1432 R);
1433}
1434template <typename LHS, typename RHS>
1435inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1437m_NSWMul(const LHS &L, const RHS &R) {
1438 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1440 R);
1441}
1442template <typename LHS, typename RHS>
1443inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1445m_NSWShl(const LHS &L, const RHS &R) {
1446 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1448 R);
1449}
1450
1451template <typename LHS, typename RHS>
1452inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1454m_NUWAdd(const LHS &L, const RHS &R) {
1455 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1457 L, R);
1458}
1459
1460template <typename LHS, typename RHS>
1462 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1463m_c_NUWAdd(const LHS &L, const RHS &R) {
1464 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1466 true>(L, R);
1467}
1468
1469template <typename LHS, typename RHS>
1470inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1472m_NUWSub(const LHS &L, const RHS &R) {
1473 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1475 L, R);
1476}
1477template <typename LHS, typename RHS>
1478inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1480m_NUWMul(const LHS &L, const RHS &R) {
1481 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1483 L, R);
1484}
1485template <typename LHS, typename RHS>
1486inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1488m_NUWShl(const LHS &L, const RHS &R) {
1489 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1491 L, R);
1492}
1493
1494template <typename LHS_t, typename RHS_t, bool Commutable = false>
1496 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1497 unsigned Opcode;
1498
1500 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1501
1502 template <typename OpTy> bool match(OpTy *V) const {
1504 }
1505};
1506
1507/// Matches a specific opcode.
1508template <typename LHS, typename RHS>
1509inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1510 const RHS &R) {
1511 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1512}
1513
1514template <typename LHS, typename RHS, bool Commutable = false>
1518
1519 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1520
1521 template <typename OpTy> bool match(OpTy *V) const {
1522 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1523 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1524 if (!PDI->isDisjoint())
1525 return false;
1526 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1527 (Commutable && L.match(PDI->getOperand(1)) &&
1528 R.match(PDI->getOperand(0)));
1529 }
1530 return false;
1531 }
1532};
1533
1534template <typename LHS, typename RHS>
1536 return DisjointOr_match<LHS, RHS>(L, R);
1537}
1538
1539template <typename LHS, typename RHS>
1541 const RHS &R) {
1543}
1544
1545/// Match either "add" or "or disjoint".
1546template <typename LHS, typename RHS>
1549m_AddLike(const LHS &L, const RHS &R) {
1550 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1551}
1552
1553/// Match either "add nsw" or "or disjoint"
1554template <typename LHS, typename RHS>
1555inline match_combine_or<
1556 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1559m_NSWAddLike(const LHS &L, const RHS &R) {
1560 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1561}
1562
1563/// Match either "add nuw" or "or disjoint"
1564template <typename LHS, typename RHS>
1565inline match_combine_or<
1566 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1569m_NUWAddLike(const LHS &L, const RHS &R) {
1570 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1571}
1572
1573template <typename LHS, typename RHS>
1577
1578 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1579
1580 template <typename OpTy> bool match(OpTy *V) const {
1581 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1582 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1583 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1584 ; // Pass
1585 else if (Op->getOpcode() != Instruction::Xor)
1586 return false;
1587 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1588 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1589 }
1590 return false;
1591 }
1592};
1593
1594/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1595/// Only commutative matcher as the `sub` will need to swap the L and R.
1596template <typename LHS, typename RHS>
1597inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1598 return XorLike_match<LHS, RHS>(L, R);
1599}
1600
1601//===----------------------------------------------------------------------===//
1602// Class that matches a group of binary opcodes.
1603//
1604template <typename LHS_t, typename RHS_t, typename Predicate,
1605 bool Commutable = false>
1606struct BinOpPred_match : Predicate {
1609
1610 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1611
1612 template <typename OpTy> bool match(OpTy *V) const {
1613 if (auto *I = dyn_cast<Instruction>(V))
1614 return this->isOpType(I->getOpcode()) &&
1615 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1616 (Commutable && L.match(I->getOperand(1)) &&
1617 R.match(I->getOperand(0))));
1618 return false;
1619 }
1620};
1621
1623 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1624};
1625
1627 bool isOpType(unsigned Opcode) const {
1628 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1629 }
1630};
1631
1633 bool isOpType(unsigned Opcode) const {
1634 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1635 }
1636};
1637
1639 bool isOpType(unsigned Opcode) const {
1640 return Instruction::isBitwiseLogicOp(Opcode);
1641 }
1642};
1643
1645 bool isOpType(unsigned Opcode) const {
1646 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1647 }
1648};
1649
1651 bool isOpType(unsigned Opcode) const {
1652 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1653 }
1654};
1655
1656/// Matches shift operations.
1657template <typename LHS, typename RHS>
1659 const RHS &R) {
1661}
1662
1663/// Matches logical shift operations.
1664template <typename LHS, typename RHS>
1669
1670/// Matches logical shift operations.
1671template <typename LHS, typename RHS>
1673m_LogicalShift(const LHS &L, const RHS &R) {
1675}
1676
1677/// Matches bitwise logic operations.
1678template <typename LHS, typename RHS>
1680m_BitwiseLogic(const LHS &L, const RHS &R) {
1682}
1683
1684/// Matches bitwise logic operations in either order.
1685template <typename LHS, typename RHS>
1690
1691/// Matches integer division operations.
1692template <typename LHS, typename RHS>
1694 const RHS &R) {
1696}
1697
1698/// Matches integer remainder operations.
1699template <typename LHS, typename RHS>
1701 const RHS &R) {
1703}
1704
1705//===----------------------------------------------------------------------===//
1706// Class that matches exact binary ops.
1707//
1708template <typename SubPattern_t> struct Exact_match {
1709 SubPattern_t SubPattern;
1710
1711 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1712
1713 template <typename OpTy> bool match(OpTy *V) const {
1714 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1715 return PEO->isExact() && SubPattern.match(V);
1716 return false;
1717 }
1718};
1719
1720template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1721 return SubPattern;
1722}
1723
1724//===----------------------------------------------------------------------===//
1725// Matchers for CmpInst classes
1726//
1727
1728template <typename LHS_t, typename RHS_t, typename Class,
1729 bool Commutable = false>
1734
1735 // The evaluation order is always stable, regardless of Commutability.
1736 // The LHS is always matched first.
1738 : Predicate(&Pred), L(LHS), R(RHS) {}
1740 : Predicate(nullptr), L(LHS), R(RHS) {}
1741
1742 template <typename OpTy> bool match(OpTy *V) const {
1743 if (auto *I = dyn_cast<Class>(V)) {
1744 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1745 if (Predicate)
1747 return true;
1748 }
1749 if (Commutable && L.match(I->getOperand(1)) &&
1750 R.match(I->getOperand(0))) {
1751 if (Predicate)
1753 return true;
1754 }
1755 }
1756 return false;
1757 }
1758};
1759
1760template <typename LHS, typename RHS>
1762 const RHS &R) {
1763 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1764}
1765
1766template <typename LHS, typename RHS>
1768 const LHS &L, const RHS &R) {
1769 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1770}
1771
1772template <typename LHS, typename RHS>
1774 const LHS &L, const RHS &R) {
1775 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1776}
1777
1778template <typename LHS, typename RHS>
1781}
1782
1783template <typename LHS, typename RHS>
1786}
1787
1788template <typename LHS, typename RHS>
1791}
1792
1793// Same as CmpClass, but instead of saving Pred as out output variable, match a
1794// specific input pred for equality.
1795template <typename LHS_t, typename RHS_t, typename Class,
1796 bool Commutable = false>
1801
1803 : Predicate(Pred), L(LHS), R(RHS) {}
1804
1805 template <typename OpTy> bool match(OpTy *V) const {
1806 if (auto *I = dyn_cast<Class>(V)) {
1808 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1809 return true;
1810 if constexpr (Commutable) {
1813 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1814 return true;
1815 }
1816 }
1817
1818 return false;
1819 }
1820};
1821
1822template <typename LHS, typename RHS>
1824m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1825 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1826}
1827
1828template <typename LHS, typename RHS>
1830m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1831 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1832}
1833
1834template <typename LHS, typename RHS>
1836m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1838}
1839
1840template <typename LHS, typename RHS>
1842m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1843 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1844}
1845
1846//===----------------------------------------------------------------------===//
1847// Matchers for instructions with a given opcode and number of operands.
1848//
1849
1850/// Matches instructions with Opcode and three operands.
1851template <typename T0, unsigned Opcode> struct OneOps_match {
1853
1854 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1855
1856 template <typename OpTy> bool match(OpTy *V) const {
1857 if (V->getValueID() == Value::InstructionVal + Opcode) {
1858 auto *I = cast<Instruction>(V);
1859 return Op1.match(I->getOperand(0));
1860 }
1861 return false;
1862 }
1863};
1864
1865/// Matches instructions with Opcode and three operands.
1866template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1869
1870 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1871
1872 template <typename OpTy> bool match(OpTy *V) const {
1873 if (V->getValueID() == Value::InstructionVal + Opcode) {
1874 auto *I = cast<Instruction>(V);
1875 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1876 }
1877 return false;
1878 }
1879};
1880
1881/// Matches instructions with Opcode and three operands.
1882template <typename T0, typename T1, typename T2, unsigned Opcode,
1883 bool CommutableOp2Op3 = false>
1888
1889 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1890 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1891
1892 template <typename OpTy> bool match(OpTy *V) const {
1893 if (V->getValueID() == Value::InstructionVal + Opcode) {
1894 auto *I = cast<Instruction>(V);
1895 if (!Op1.match(I->getOperand(0)))
1896 return false;
1897 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1898 return true;
1899 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1900 Op3.match(I->getOperand(1));
1901 }
1902 return false;
1903 }
1904};
1905
1906/// Matches instructions with Opcode and any number of operands
1907template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1908 std::tuple<OperandTypes...> Operands;
1909
1910 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1911
1912 // Operand matching works by recursively calling match_operands, matching the
1913 // operands left to right. The first version is called for each operand but
1914 // the last, for which the second version is called. The second version of
1915 // match_operands is also used to match each individual operand.
1916 template <int Idx, int Last>
1917 std::enable_if_t<Idx != Last, bool>
1921
1922 template <int Idx, int Last>
1923 std::enable_if_t<Idx == Last, bool>
1925 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1926 }
1927
1928 template <typename OpTy> bool match(OpTy *V) const {
1929 if (V->getValueID() == Value::InstructionVal + Opcode) {
1930 auto *I = cast<Instruction>(V);
1931 return I->getNumOperands() == sizeof...(OperandTypes) &&
1932 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1933 }
1934 return false;
1935 }
1936};
1937
1938/// Matches SelectInst.
1939template <typename Cond, typename LHS, typename RHS>
1941m_Select(const Cond &C, const LHS &L, const RHS &R) {
1943}
1944
1945/// This matches a select of two constants, e.g.:
1946/// m_SelectCst<-1, 0>(m_Value(V))
1947template <int64_t L, int64_t R, typename Cond>
1949 Instruction::Select>
1952}
1953
1954/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1955template <typename LHS, typename RHS>
1956inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1957m_c_Select(const LHS &L, const RHS &R) {
1958 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1959 true>(m_Value(), L, R);
1960}
1961
1962/// Matches FreezeInst.
1963template <typename OpTy>
1967
1968/// Matches InsertElementInst.
1969template <typename Val_t, typename Elt_t, typename Idx_t>
1971m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1973 Val, Elt, Idx);
1974}
1975
1976/// Matches ExtractElementInst.
1977template <typename Val_t, typename Idx_t>
1979m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1981}
1982
1983/// Matches shuffle.
1984template <typename T0, typename T1, typename T2> struct Shuffle_match {
1988
1989 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1990 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1991
1992 template <typename OpTy> bool match(OpTy *V) const {
1993 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1994 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1995 Mask.match(I->getShuffleMask());
1996 }
1997 return false;
1998 }
1999};
2000
2001struct m_Mask {
2004 bool match(ArrayRef<int> Mask) const {
2005 MaskRef = Mask;
2006 return true;
2007 }
2008};
2009
2011 bool match(ArrayRef<int> Mask) const {
2012 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2013 }
2014};
2015
2019 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2020};
2021
2025 bool match(ArrayRef<int> Mask) const {
2026 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2027 if (First == Mask.end())
2028 return false;
2029 SplatIndex = *First;
2030 return all_of(Mask,
2031 [First](int Elem) { return Elem == *First || Elem == -1; });
2032 }
2033};
2034
2035template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2036 PointerOpTy PointerOp;
2037 OffsetOpTy OffsetOp;
2038
2039 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2041
2042 template <typename OpTy> bool match(OpTy *V) const {
2043 auto *GEP = dyn_cast<GEPOperator>(V);
2044 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2045 PointerOp.match(GEP->getPointerOperand()) &&
2046 OffsetOp.match(GEP->idx_begin()->get());
2047 }
2048};
2049
2050/// Matches ShuffleVectorInst independently of mask value.
2051template <typename V1_t, typename V2_t>
2053m_Shuffle(const V1_t &v1, const V2_t &v2) {
2055}
2056
2057template <typename V1_t, typename V2_t, typename Mask_t>
2059m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2060 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
2061}
2062
2063/// Matches LoadInst.
2064template <typename OpTy>
2068
2069/// Matches StoreInst.
2070template <typename ValueOpTy, typename PointerOpTy>
2072m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2074 PointerOp);
2075}
2076
2077/// Matches GetElementPtrInst.
2078template <typename... OperandTypes>
2079inline auto m_GEP(const OperandTypes &...Ops) {
2080 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2081}
2082
2083/// Matches GEP with i8 source element type
2084template <typename PointerOpTy, typename OffsetOpTy>
2086m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2088}
2089
2090//===----------------------------------------------------------------------===//
2091// Matchers for CastInst classes
2092//
2093
2094template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2095 Op_t Op;
2096
2097 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2098
2099 template <typename OpTy> bool match(OpTy *V) const {
2100 if (auto *O = dyn_cast<Operator>(V))
2101 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2102 return false;
2103 }
2104};
2105
2106template <typename Op_t, typename Class> struct CastInst_match {
2107 Op_t Op;
2108
2109 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2110
2111 template <typename OpTy> bool match(OpTy *V) const {
2112 if (auto *I = dyn_cast<Class>(V))
2113 return Op.match(I->getOperand(0));
2114 return false;
2115 }
2116};
2117
2118template <typename Op_t> struct PtrToIntSameSize_match {
2120 Op_t Op;
2121
2122 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2123 : DL(DL), Op(OpMatch) {}
2124
2125 template <typename OpTy> bool match(OpTy *V) const {
2126 if (auto *O = dyn_cast<Operator>(V))
2127 return O->getOpcode() == Instruction::PtrToInt &&
2128 DL.getTypeSizeInBits(O->getType()) ==
2129 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2130 Op.match(O->getOperand(0));
2131 return false;
2132 }
2133};
2134
2135template <typename Op_t> struct NNegZExt_match {
2136 Op_t Op;
2137
2138 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2139
2140 template <typename OpTy> bool match(OpTy *V) const {
2141 if (auto *I = dyn_cast<ZExtInst>(V))
2142 return I->hasNonNeg() && Op.match(I->getOperand(0));
2143 return false;
2144 }
2145};
2146
2147template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2148 Op_t Op;
2149
2150 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2151
2152 template <typename OpTy> bool match(OpTy *V) const {
2153 if (auto *I = dyn_cast<TruncInst>(V))
2154 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2155 Op.match(I->getOperand(0));
2156 return false;
2157 }
2158};
2159
2160/// Matches BitCast.
2161template <typename OpTy>
2166
2167template <typename Op_t> struct ElementWiseBitCast_match {
2168 Op_t Op;
2169
2170 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2171
2172 template <typename OpTy> bool match(OpTy *V) const {
2173 auto *I = dyn_cast<BitCastInst>(V);
2174 if (!I)
2175 return false;
2176 Type *SrcType = I->getSrcTy();
2177 Type *DstType = I->getType();
2178 // Make sure the bitcast doesn't change between scalar and vector and
2179 // doesn't change the number of vector elements.
2180 if (SrcType->isVectorTy() != DstType->isVectorTy())
2181 return false;
2182 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2183 SrcVecTy && SrcVecTy->getElementCount() !=
2184 cast<VectorType>(DstType)->getElementCount())
2185 return false;
2186 return Op.match(I->getOperand(0));
2187 }
2188};
2189
2190template <typename OpTy>
2194
2195/// Matches PtrToInt.
2196template <typename OpTy>
2201
2202template <typename OpTy>
2207
2208/// Matches IntToPtr.
2209template <typename OpTy>
2214
2215/// Matches any cast or self. Used to ignore casts.
2216template <typename OpTy>
2218m_CastOrSelf(const OpTy &Op) {
2220}
2221
2222/// Matches Trunc.
2223template <typename OpTy>
2227
2228/// Matches trunc nuw.
2229template <typename OpTy>
2234
2235/// Matches trunc nsw.
2236template <typename OpTy>
2241
2242template <typename OpTy>
2244m_TruncOrSelf(const OpTy &Op) {
2245 return m_CombineOr(m_Trunc(Op), Op);
2246}
2247
2248/// Matches SExt.
2249template <typename OpTy>
2253
2254/// Matches ZExt.
2255template <typename OpTy>
2259
2260template <typename OpTy>
2262 return NNegZExt_match<OpTy>(Op);
2263}
2264
2265template <typename OpTy>
2267m_ZExtOrSelf(const OpTy &Op) {
2268 return m_CombineOr(m_ZExt(Op), Op);
2269}
2270
2271template <typename OpTy>
2273m_SExtOrSelf(const OpTy &Op) {
2274 return m_CombineOr(m_SExt(Op), Op);
2275}
2276
2277/// Match either "sext" or "zext nneg".
2278template <typename OpTy>
2280m_SExtLike(const OpTy &Op) {
2281 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2282}
2283
2284template <typename OpTy>
2287m_ZExtOrSExt(const OpTy &Op) {
2288 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2289}
2290
2291template <typename OpTy>
2294 OpTy>
2296 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2297}
2298
2299template <typename OpTy>
2302 OpTy>
2305}
2306
2307template <typename OpTy>
2311
2312template <typename OpTy>
2316
2317template <typename OpTy>
2321
2322template <typename OpTy>
2326
2327template <typename OpTy>
2331
2332template <typename OpTy>
2336
2337//===----------------------------------------------------------------------===//
2338// Matchers for control flow.
2339//
2340
2341struct br_match {
2343
2345
2346 template <typename OpTy> bool match(OpTy *V) const {
2347 if (auto *BI = dyn_cast<BranchInst>(V))
2348 if (BI->isUnconditional()) {
2349 Succ = BI->getSuccessor(0);
2350 return true;
2351 }
2352 return false;
2353 }
2354};
2355
2356inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2357
2358template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2360 Cond_t Cond;
2361 TrueBlock_t T;
2362 FalseBlock_t F;
2363
2364 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2365 : Cond(C), T(t), F(f) {}
2366
2367 template <typename OpTy> bool match(OpTy *V) const {
2368 if (auto *BI = dyn_cast<BranchInst>(V))
2369 if (BI->isConditional() && Cond.match(BI->getCondition()))
2370 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2371 return false;
2372 }
2373};
2374
2375template <typename Cond_t>
2381
2382template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2384m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2386}
2387
2388//===----------------------------------------------------------------------===//
2389// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2390//
2391
2392template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2393 bool Commutable = false>
2395 using PredType = Pred_t;
2398
2399 // The evaluation order is always stable, regardless of Commutability.
2400 // The LHS is always matched first.
2401 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2402
2403 template <typename OpTy> bool match(OpTy *V) const {
2404 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2405 Intrinsic::ID IID = II->getIntrinsicID();
2406 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2407 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2408 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2409 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2410 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2411 return (L.match(LHS) && R.match(RHS)) ||
2412 (Commutable && L.match(RHS) && R.match(LHS));
2413 }
2414 }
2415 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2416 auto *SI = dyn_cast<SelectInst>(V);
2417 if (!SI)
2418 return false;
2419 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2420 if (!Cmp)
2421 return false;
2422 // At this point we have a select conditioned on a comparison. Check that
2423 // it is the values returned by the select that are being compared.
2424 auto *TrueVal = SI->getTrueValue();
2425 auto *FalseVal = SI->getFalseValue();
2426 auto *LHS = Cmp->getOperand(0);
2427 auto *RHS = Cmp->getOperand(1);
2428 if ((TrueVal != LHS || FalseVal != RHS) &&
2429 (TrueVal != RHS || FalseVal != LHS))
2430 return false;
2431 typename CmpInst_t::Predicate Pred =
2432 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2433 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2434 if (!Pred_t::match(Pred))
2435 return false;
2436 // It does! Bind the operands.
2437 return (L.match(LHS) && R.match(RHS)) ||
2438 (Commutable && L.match(RHS) && R.match(LHS));
2439 }
2440};
2441
2442/// Helper class for identifying signed max predicates.
2444 static bool match(ICmpInst::Predicate Pred) {
2445 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2446 }
2447};
2448
2449/// Helper class for identifying signed min predicates.
2451 static bool match(ICmpInst::Predicate Pred) {
2452 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2453 }
2454};
2455
2456/// Helper class for identifying unsigned max predicates.
2458 static bool match(ICmpInst::Predicate Pred) {
2459 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2460 }
2461};
2462
2463/// Helper class for identifying unsigned min predicates.
2465 static bool match(ICmpInst::Predicate Pred) {
2466 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2467 }
2468};
2469
2470/// Helper class for identifying ordered max predicates.
2472 static bool match(FCmpInst::Predicate Pred) {
2473 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2474 }
2475};
2476
2477/// Helper class for identifying ordered min predicates.
2479 static bool match(FCmpInst::Predicate Pred) {
2480 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2481 }
2482};
2483
2484/// Helper class for identifying unordered max predicates.
2486 static bool match(FCmpInst::Predicate Pred) {
2487 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2488 }
2489};
2490
2491/// Helper class for identifying unordered min predicates.
2493 static bool match(FCmpInst::Predicate Pred) {
2494 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2495 }
2496};
2497
2498template <typename LHS, typename RHS>
2503
2504template <typename LHS, typename RHS>
2509
2510template <typename LHS, typename RHS>
2515
2516template <typename LHS, typename RHS>
2521
2522template <typename LHS, typename RHS>
2523inline match_combine_or<
2528m_MaxOrMin(const LHS &L, const RHS &R) {
2529 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2530 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2531}
2532
2533/// Match an 'ordered' floating point maximum function.
2534/// Floating point has one special value 'NaN'. Therefore, there is no total
2535/// order. However, if we can ignore the 'NaN' value (for example, because of a
2536/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2537/// semantics. In the presence of 'NaN' we have to preserve the original
2538/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2539///
2540/// max(L, R) iff L and R are not NaN
2541/// m_OrdFMax(L, R) = R iff L or R are NaN
2542template <typename LHS, typename RHS>
2547
2548/// Match an 'ordered' floating point minimum function.
2549/// Floating point has one special value 'NaN'. Therefore, there is no total
2550/// order. However, if we can ignore the 'NaN' value (for example, because of a
2551/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2552/// semantics. In the presence of 'NaN' we have to preserve the original
2553/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2554///
2555/// min(L, R) iff L and R are not NaN
2556/// m_OrdFMin(L, R) = R iff L or R are NaN
2557template <typename LHS, typename RHS>
2562
2563/// Match an 'unordered' floating point maximum function.
2564/// Floating point has one special value 'NaN'. Therefore, there is no total
2565/// order. However, if we can ignore the 'NaN' value (for example, because of a
2566/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2567/// semantics. In the presence of 'NaN' we have to preserve the original
2568/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2569///
2570/// max(L, R) iff L and R are not NaN
2571/// m_UnordFMax(L, R) = L iff L or R are NaN
2572template <typename LHS, typename RHS>
2574m_UnordFMax(const LHS &L, const RHS &R) {
2576}
2577
2578/// Match an 'unordered' floating point minimum function.
2579/// Floating point has one special value 'NaN'. Therefore, there is no total
2580/// order. However, if we can ignore the 'NaN' value (for example, because of a
2581/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2582/// semantics. In the presence of 'NaN' we have to preserve the original
2583/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2584///
2585/// min(L, R) iff L and R are not NaN
2586/// m_UnordFMin(L, R) = L iff L or R are NaN
2587template <typename LHS, typename RHS>
2589m_UnordFMin(const LHS &L, const RHS &R) {
2591}
2592
2593/// Match an 'ordered' or 'unordered' floating point maximum function.
2594/// Floating point has one special value 'NaN'. Therefore, there is no total
2595/// order. However, if we can ignore the 'NaN' value (for example, because of a
2596/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2597/// semantics.
2598template <typename LHS, typename RHS>
2605
2606/// Match an 'ordered' or 'unordered' floating point minimum function.
2607/// Floating point has one special value 'NaN'. Therefore, there is no total
2608/// order. However, if we can ignore the 'NaN' value (for example, because of a
2609/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2610/// semantics.
2611template <typename LHS, typename RHS>
2618
2619/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2620/// NOTE: we first match the 'Not' (by matching '-1'),
2621/// and only then match the inner matcher!
2622template <typename ValTy>
2623inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2624m_Not(const ValTy &V) {
2625 return m_c_Xor(m_AllOnes(), V);
2626}
2627
2628template <typename ValTy>
2629inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2630 true>
2631m_NotForbidPoison(const ValTy &V) {
2632 return m_c_Xor(m_AllOnesForbidPoison(), V);
2633}
2634
2635//===----------------------------------------------------------------------===//
2636// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2637// Note that S might be matched to other instructions than AddInst.
2638//
2639
2640template <typename LHS_t, typename RHS_t, typename Sum_t>
2644 Sum_t S;
2645
2646 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2647 : L(L), R(R), S(S) {}
2648
2649 template <typename OpTy> bool match(OpTy *V) const {
2650 Value *ICmpLHS, *ICmpRHS;
2651 CmpPredicate Pred;
2652 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2653 return false;
2654
2655 Value *AddLHS, *AddRHS;
2656 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2657
2658 // (a + b) u< a, (a + b) u< b
2659 if (Pred == ICmpInst::ICMP_ULT)
2660 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2661 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2662
2663 // a >u (a + b), b >u (a + b)
2664 if (Pred == ICmpInst::ICMP_UGT)
2665 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2666 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2667
2668 Value *Op1;
2669 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2670 // (~a) <u b
2671 if (Pred == ICmpInst::ICMP_ULT) {
2672 if (XorExpr.match(ICmpLHS))
2673 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2674 }
2675 // b > u (~a)
2676 if (Pred == ICmpInst::ICMP_UGT) {
2677 if (XorExpr.match(ICmpRHS))
2678 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2679 }
2680
2681 // Match special-case for increment-by-1.
2682 if (Pred == ICmpInst::ICMP_EQ) {
2683 // (a + 1) == 0
2684 // (1 + a) == 0
2685 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2686 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2687 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2688 // 0 == (a + 1)
2689 // 0 == (1 + a)
2690 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2691 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2692 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2693 }
2694
2695 return false;
2696 }
2697};
2698
2699/// Match an icmp instruction checking for unsigned overflow on addition.
2700///
2701/// S is matched to the addition whose result is being checked for overflow, and
2702/// L and R are matched to the LHS and RHS of S.
2703template <typename LHS_t, typename RHS_t, typename Sum_t>
2705m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2707}
2708
2709template <typename Opnd_t> struct Argument_match {
2710 unsigned OpI;
2711 Opnd_t Val;
2712
2713 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2714
2715 template <typename OpTy> bool match(OpTy *V) const {
2716 // FIXME: Should likely be switched to use `CallBase`.
2717 if (const auto *CI = dyn_cast<CallInst>(V))
2718 return Val.match(CI->getArgOperand(OpI));
2719 return false;
2720 }
2721};
2722
2723/// Match an argument.
2724template <unsigned OpI, typename Opnd_t>
2725inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2726 return Argument_match<Opnd_t>(OpI, Op);
2727}
2728
2729/// Intrinsic matchers.
2731 unsigned ID;
2732
2734
2735 template <typename OpTy> bool match(OpTy *V) const {
2736 if (const auto *CI = dyn_cast<CallInst>(V))
2737 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2738 return F->getIntrinsicID() == ID;
2739 return false;
2740 }
2741};
2742
2743/// Intrinsic matches are combinations of ID matchers, and argument
2744/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2745/// them with lower arity matchers. Here's some convenient typedefs for up to
2746/// several arguments, and more can be added as needed
2747template <typename T0 = void, typename T1 = void, typename T2 = void,
2748 typename T3 = void, typename T4 = void, typename T5 = void,
2749 typename T6 = void, typename T7 = void, typename T8 = void,
2750 typename T9 = void, typename T10 = void>
2752template <typename T0> struct m_Intrinsic_Ty<T0> {
2754};
2755template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2756 using Ty =
2758};
2759template <typename T0, typename T1, typename T2>
2764template <typename T0, typename T1, typename T2, typename T3>
2769
2770template <typename T0, typename T1, typename T2, typename T3, typename T4>
2775
2776template <typename T0, typename T1, typename T2, typename T3, typename T4,
2777 typename T5>
2782
2783/// Match intrinsic calls like this:
2784/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2785template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2786 return IntrinsicID_match(IntrID);
2787}
2788
2789/// Matches MaskedLoad Intrinsic.
2790template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2792m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2793 const Opnd3 &Op3) {
2794 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
2795}
2796
2797/// Matches MaskedGather Intrinsic.
2798template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2800m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2801 const Opnd3 &Op3) {
2802 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
2803}
2804
2805template <Intrinsic::ID IntrID, typename T0>
2806inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2808}
2809
2810template <Intrinsic::ID IntrID, typename T0, typename T1>
2811inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2812 const T1 &Op1) {
2814}
2815
2816template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2817inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2818m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2819 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2820}
2821
2822template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2823 typename T3>
2825m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2826 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2827}
2828
2829template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2830 typename T3, typename T4>
2832m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2833 const T4 &Op4) {
2834 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2835 m_Argument<4>(Op4));
2836}
2837
2838template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2839 typename T3, typename T4, typename T5>
2841m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2842 const T4 &Op4, const T5 &Op5) {
2843 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2844 m_Argument<5>(Op5));
2845}
2846
2847// Helper intrinsic matching specializations.
2848template <typename Opnd0>
2849inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2851}
2852
2853template <typename Opnd0>
2854inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2856}
2857
2858template <typename Opnd0>
2859inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2860 return m_Intrinsic<Intrinsic::fabs>(Op0);
2861}
2862
2863template <typename Opnd0>
2864inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2866}
2867
2868template <typename Opnd0, typename Opnd1>
2869inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2870 const Opnd1 &Op1) {
2871 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2872}
2873
2874template <typename Opnd0, typename Opnd1>
2875inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2876 const Opnd1 &Op1) {
2877 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2878}
2879
2880template <typename Opnd0, typename Opnd1>
2882m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2883 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2884}
2885
2886template <typename Opnd0, typename Opnd1>
2887inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2888 const Opnd1 &Op1) {
2889 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2890}
2891
2892template <typename Opnd0, typename Opnd1>
2893inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2894 const Opnd1 &Op1) {
2895 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2896}
2897
2898template <typename Opnd0, typename Opnd1>
2900m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2901 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2902}
2903
2904template <typename Opnd0, typename Opnd1, typename Opnd2>
2906m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2907 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2908}
2909
2910template <typename Opnd0, typename Opnd1, typename Opnd2>
2912m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2913 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2914}
2915
2916template <typename Opnd0>
2917inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2918 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2919}
2920
2921template <typename Opnd0, typename Opnd1>
2922inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2923 const Opnd1 &Op1) {
2924 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2925}
2926
2927template <typename Opnd0>
2928inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2930}
2931
2932//===----------------------------------------------------------------------===//
2933// Matchers for two-operands operators with the operators in either order
2934//
2935
2936/// Matches a BinaryOperator with LHS and RHS in either order.
2937template <typename LHS, typename RHS>
2940}
2941
2942/// Matches an ICmp with a predicate over LHS and RHS in either order.
2943/// Swaps the predicate if operands are commuted.
2944template <typename LHS, typename RHS>
2946m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2948}
2949
2950template <typename LHS, typename RHS>
2955
2956/// Matches a specific opcode with LHS and RHS in either order.
2957template <typename LHS, typename RHS>
2959m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2960 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2961}
2962
2963/// Matches a Add with LHS and RHS in either order.
2964template <typename LHS, typename RHS>
2969
2970/// Matches a Mul with LHS and RHS in either order.
2971template <typename LHS, typename RHS>
2976
2977/// Matches an And with LHS and RHS in either order.
2978template <typename LHS, typename RHS>
2983
2984/// Matches an Or with LHS and RHS in either order.
2985template <typename LHS, typename RHS>
2990
2991/// Matches an Xor with LHS and RHS in either order.
2992template <typename LHS, typename RHS>
2997
2998/// Matches a 'Neg' as 'sub 0, V'.
2999template <typename ValTy>
3000inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3001m_Neg(const ValTy &V) {
3002 return m_Sub(m_ZeroInt(), V);
3003}
3004
3005/// Matches a 'Neg' as 'sub nsw 0, V'.
3006template <typename ValTy>
3008 Instruction::Sub,
3010m_NSWNeg(const ValTy &V) {
3011 return m_NSWSub(m_ZeroInt(), V);
3012}
3013
3014/// Matches an SMin with LHS and RHS in either order.
3015template <typename LHS, typename RHS>
3017m_c_SMin(const LHS &L, const RHS &R) {
3019}
3020/// Matches an SMax with LHS and RHS in either order.
3021template <typename LHS, typename RHS>
3023m_c_SMax(const LHS &L, const RHS &R) {
3025}
3026/// Matches a UMin with LHS and RHS in either order.
3027template <typename LHS, typename RHS>
3029m_c_UMin(const LHS &L, const RHS &R) {
3031}
3032/// Matches a UMax with LHS and RHS in either order.
3033template <typename LHS, typename RHS>
3035m_c_UMax(const LHS &L, const RHS &R) {
3037}
3038
3039template <typename LHS, typename RHS>
3040inline match_combine_or<
3045m_c_MaxOrMin(const LHS &L, const RHS &R) {
3046 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3047 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3048}
3049
3050template <Intrinsic::ID IntrID, typename T0, typename T1>
3053m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3054 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
3055 m_Intrinsic<IntrID>(Op1, Op0));
3056}
3057
3058/// Matches FAdd with LHS and RHS in either order.
3059template <typename LHS, typename RHS>
3061m_c_FAdd(const LHS &L, const RHS &R) {
3063}
3064
3065/// Matches FMul with LHS and RHS in either order.
3066template <typename LHS, typename RHS>
3068m_c_FMul(const LHS &L, const RHS &R) {
3070}
3071
3072template <typename Opnd_t> struct Signum_match {
3073 Opnd_t Val;
3074 Signum_match(const Opnd_t &V) : Val(V) {}
3075
3076 template <typename OpTy> bool match(OpTy *V) const {
3077 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3078 if (TypeSize == 0)
3079 return false;
3080
3081 unsigned ShiftWidth = TypeSize - 1;
3082 Value *Op;
3083
3084 // This is the representation of signum we match:
3085 //
3086 // signum(x) == (x >> 63) | (-x >>u 63)
3087 //
3088 // An i1 value is its own signum, so it's correct to match
3089 //
3090 // signum(x) == (x >> 0) | (-x >>u 0)
3091 //
3092 // for i1 values.
3093
3094 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3095 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3096 auto Signum = m_c_Or(LHS, RHS);
3097
3098 return Signum.match(V) && Val.match(Op);
3099 }
3100};
3101
3102/// Matches a signum pattern.
3103///
3104/// signum(x) =
3105/// x > 0 -> 1
3106/// x == 0 -> 0
3107/// x < 0 -> -1
3108template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3109 return Signum_match<Val_t>(V);
3110}
3111
3112template <int Ind, typename Opnd_t> struct ExtractValue_match {
3113 Opnd_t Val;
3114 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3115
3116 template <typename OpTy> bool match(OpTy *V) const {
3117 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3118 // If Ind is -1, don't inspect indices
3119 if (Ind != -1 &&
3120 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3121 return false;
3122 return Val.match(I->getAggregateOperand());
3123 }
3124 return false;
3125 }
3126};
3127
3128/// Match a single index ExtractValue instruction.
3129/// For example m_ExtractValue<1>(...)
3130template <int Ind, typename Val_t>
3134
3135/// Match an ExtractValue instruction with any index.
3136/// For example m_ExtractValue(...)
3137template <typename Val_t>
3138inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3139 return ExtractValue_match<-1, Val_t>(V);
3140}
3141
3142/// Matcher for a single index InsertValue instruction.
3143template <int Ind, typename T0, typename T1> struct InsertValue_match {
3146
3147 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3148
3149 template <typename OpTy> bool match(OpTy *V) const {
3150 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3151 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3152 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3153 }
3154 return false;
3155 }
3156};
3157
3158/// Matches a single index InsertValue instruction.
3159template <int Ind, typename Val_t, typename Elt_t>
3161 const Elt_t &Elt) {
3162 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3163}
3164
3165/// Matches a call to `llvm.vscale()`.
3167
3168template <typename Opnd0, typename Opnd1>
3170m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3172}
3173
3174template <typename Opnd>
3178
3179template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3183
3184 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3185
3186 template <typename T> bool match(T *V) const {
3187 auto *I = dyn_cast<Instruction>(V);
3188 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3189 return false;
3190
3191 if (I->getOpcode() == Opcode) {
3192 auto *Op0 = I->getOperand(0);
3193 auto *Op1 = I->getOperand(1);
3194 return (L.match(Op0) && R.match(Op1)) ||
3195 (Commutable && L.match(Op1) && R.match(Op0));
3196 }
3197
3198 if (auto *Select = dyn_cast<SelectInst>(I)) {
3199 auto *Cond = Select->getCondition();
3200 auto *TVal = Select->getTrueValue();
3201 auto *FVal = Select->getFalseValue();
3202
3203 // Don't match a scalar select of bool vectors.
3204 // Transforms expect a single type for operands if this matches.
3205 if (Cond->getType() != Select->getType())
3206 return false;
3207
3208 if (Opcode == Instruction::And) {
3209 auto *C = dyn_cast<Constant>(FVal);
3210 if (C && C->isNullValue())
3211 return (L.match(Cond) && R.match(TVal)) ||
3212 (Commutable && L.match(TVal) && R.match(Cond));
3213 } else {
3214 assert(Opcode == Instruction::Or);
3215 auto *C = dyn_cast<Constant>(TVal);
3216 if (C && C->isOneValue())
3217 return (L.match(Cond) && R.match(FVal)) ||
3218 (Commutable && L.match(FVal) && R.match(Cond));
3219 }
3220 }
3221
3222 return false;
3223 }
3224};
3225
3226/// Matches L && R either in the form of L & R or L ? R : false.
3227/// Note that the latter form is poison-blocking.
3228template <typename LHS, typename RHS>
3233
3234/// Matches L && R where L and R are arbitrary values.
3235inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3236
3237/// Matches L && R with LHS and RHS in either order.
3238template <typename LHS, typename RHS>
3240m_c_LogicalAnd(const LHS &L, const RHS &R) {
3242}
3243
3244/// Matches L || R either in the form of L | R or L ? true : R.
3245/// Note that the latter form is poison-blocking.
3246template <typename LHS, typename RHS>
3251
3252/// Matches L || R where L and R are arbitrary values.
3253inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3254
3255/// Matches L || R with LHS and RHS in either order.
3256template <typename LHS, typename RHS>
3258m_c_LogicalOr(const LHS &L, const RHS &R) {
3260}
3261
3262/// Matches either L && R or L || R,
3263/// either one being in the either binary or logical form.
3264/// Note that the latter form is poison-blocking.
3265template <typename LHS, typename RHS, bool Commutable = false>
3271
3272/// Matches either L && R or L || R where L and R are arbitrary values.
3273inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3274
3275/// Matches either L && R or L || R with LHS and RHS in either order.
3276template <typename LHS, typename RHS>
3277inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3278 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3279}
3280
3281} // end namespace PatternMatch
3282} // end namespace llvm
3283
3284#endif // LLVM_IR_PATTERNMATCH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define T
#define T1
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1512
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:553
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:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:692
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:685
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:691
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h:408
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1120
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
bool isShift() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
'undef' values are things that do not have specified contents.
Definition Constants.h:1420
LLVM Value Representation.
Definition Value.h:75
Base class of all SIMD vector types.
Represents an op.with.overflow intrinsic.
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
TwoOps_match< ValueOpTy, PointerOpTy, Instruction::Store > m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp)
Matches StoreInst.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
apfloat_match m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
ShiftLike_match< LHS, Instruction::LShr > m_LShrOrSelf(const LHS &L, uint64_t &R)
Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
m_Intrinsic_Ty< Opnd0 >::Ty m_FCanonicalize(const Opnd0 &Op0)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, true > m_c_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, CastInst >, OpTy > m_CastOrSelf(const OpTy &Op)
Matches any cast or self. Used to ignore casts.
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
AllowReassoc_match< T > m_AllowReassoc(const T &SubPattern)
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.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_c_XorLike(const LHS &L, const RHS &R)
Match either (xor L, R), (xor R, L) or (sub nuw R, L) iff R.isMask() Only commutative matcher as the ...
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R, either one being in the either binary or logical form.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
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()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > >, OpTy > m_ZExtOrSExtOrSelf(const OpTy &Op)
OneUse_match< T > m_OneUse(const T &SubPattern)
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
class_match< ConstantFP > m_ConstantFP()
Match an arbitrary ConstantFP and ignore it.
cstfp_pred_ty< is_nonnan > m_NonNaN()
Match a non-NaN FP constant.
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.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
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.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_not_denormal_fp > m_NonZeroNotDenormalFP()
Match a floating-point non-zero that is not a denormal.
cst_pred_ty< is_all_ones, false > m_AllOnesForbidPoison()
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
class_match< UndefValue > m_UndefValue()
Match an arbitrary UndefValue constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
cst_pred_ty< is_negated_power2_or_zero > m_NegatedPower2OrZero()
Match a integer or vector negated power-of-2.
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
ShiftLike_match< LHS, Instruction::AShr > m_AShrOrSelf(const LHS &L, uint64_t &R)
Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
cst_pred_ty< is_lowbit_mask_or_zero > m_LowBitMaskOrZero()
Match an integer or vector with only the low bit(s) set.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
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)
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
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.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
SpecificCmpClass_match< LHS, RHS, ICmpInst, true > m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, TruncInst > >, OpTy > m_ZExtOrTruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
m_Intrinsic_Ty< Opnd >::Ty m_Deinterleave2(const Opnd &Op)
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.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
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:1705
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
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:1738
AllowReassoc_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
apfloat_match(const APFloat *&Res, bool AllowPoison)
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
apint_match(const APInt *&Res, bool AllowPoison)
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool match(ITy *V) const
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3, T4 >::Ty, Argument_match< T5 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2, T3 >::Ty, Argument_match< T4 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1, T2 >::Ty, Argument_match< T3 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0, T1 >::Ty, Argument_match< T2 > > Ty
match_combine_and< typename m_Intrinsic_Ty< T0 >::Ty, Argument_match< T1 > > Ty
match_combine_and< IntrinsicID_match, Argument_match< T0 > > Ty
Intrinsic matches are combinations of ID matchers, and argument matchers.
ArrayRef< int > & MaskRef
m_Mask(ArrayRef< int > &MaskRef)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
m_SpecificMask(ArrayRef< int > Val)
bool match(ArrayRef< int > Mask) const
bool match(ArrayRef< int > Mask) const
match_combine_and(const LTy &Left, const RTy &Right)
match_combine_or(const LTy &Left, const RTy &Right)
Helper class for identifying ordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying ordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying signed max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying signed min predicates.
static bool match(ICmpInst::Predicate Pred)
Match a specified basic block value.
Match a specified floating point value or vector of all elements of that value.
Match a specified integer value or vector of all elements of that value.
Match a specified Value*.
Helper class for identifying unordered max predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unordered min predicates.
static bool match(FCmpInst::Predicate Pred)
Helper class for identifying unsigned max predicates.
static bool match(ICmpInst::Predicate Pred)
Helper class for identifying unsigned min predicates.
static bool match(ICmpInst::Predicate Pred)
static bool check(const Value *V)