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 Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
54 return P.match(Mask);
55}
56
57template <typename SubPattern_t> struct OneUse_match {
58 SubPattern_t SubPattern;
59
60 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
61
62 template <typename OpTy> bool match(OpTy *V) const {
63 return V->hasOneUse() && SubPattern.match(V);
64 }
65};
66
67template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
68 return SubPattern;
69}
70
71template <typename SubPattern_t> struct AllowReassoc_match {
72 SubPattern_t SubPattern;
73
74 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
75
76 template <typename OpTy> bool match(OpTy *V) const {
77 auto *I = dyn_cast<FPMathOperator>(V);
78 return I && I->hasAllowReassoc() && SubPattern.match(I);
79 }
80};
81
82template <typename T>
83inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
84 return SubPattern;
85}
86
87template <typename Class> struct class_match {
88 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
89};
90
91/// Match an arbitrary value and ignore it.
93
94/// Match an arbitrary unary operation and ignore it.
98
99/// Match an arbitrary binary operation and ignore it.
103
104/// Matches any compare instruction and ignore it.
106
108 static bool check(const Value *V) {
109 if (isa<UndefValue>(V))
110 return true;
111
112 const auto *CA = dyn_cast<ConstantAggregate>(V);
113 if (!CA)
114 return false;
115
118
119 // Either UndefValue, PoisonValue, or an aggregate that only contains
120 // these is accepted by matcher.
121 // CheckValue returns false if CA cannot satisfy this constraint.
122 auto CheckValue = [&](const ConstantAggregate *CA) {
123 for (const Value *Op : CA->operand_values()) {
124 if (isa<UndefValue>(Op))
125 continue;
126
127 const auto *CA = dyn_cast<ConstantAggregate>(Op);
128 if (!CA)
129 return false;
130 if (Seen.insert(CA).second)
131 Worklist.emplace_back(CA);
132 }
133
134 return true;
135 };
136
137 if (!CheckValue(CA))
138 return false;
139
140 while (!Worklist.empty()) {
141 if (!CheckValue(Worklist.pop_back_val()))
142 return false;
143 }
144 return true;
145 }
146 template <typename ITy> bool match(ITy *V) const { return check(V); }
147};
148
149/// Match an arbitrary undef constant. This matches poison as well.
150/// If this is an aggregate and contains a non-aggregate element that is
151/// neither undef nor poison, the aggregate is not matched.
152inline auto m_Undef() { return undef_match(); }
153
154/// Match an arbitrary UndefValue constant.
158
159/// Match an arbitrary poison constant.
163
164/// Match an arbitrary Constant and ignore it.
166
167/// Match an arbitrary ConstantInt and ignore it.
171
172/// Match an arbitrary ConstantFP and ignore it.
176
178 template <typename ITy> bool match(ITy *V) const {
179 auto *C = dyn_cast<Constant>(V);
180 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
181 }
182};
183
184/// Match a constant expression or a constant that contains a constant
185/// expression.
187
188/// Match an arbitrary basic block value and ignore it.
192
193/// Inverting matcher
194template <typename Ty> struct match_unless {
195 Ty M;
196
197 match_unless(const Ty &Matcher) : M(Matcher) {}
198
199 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
200};
201
202/// Match if the inner matcher does *NOT* match.
203template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
204 return match_unless<Ty>(M);
205}
206
207/// Matching combinators
208template <typename LTy, typename RTy> struct match_combine_or {
209 LTy L;
210 RTy R;
211
212 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
213
214 template <typename ITy> bool match(ITy *V) const {
215 if (L.match(V))
216 return true;
217 if (R.match(V))
218 return true;
219 return false;
220 }
221};
222
223template <typename LTy, typename RTy> struct match_combine_and {
224 LTy L;
225 RTy R;
226
227 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
228
229 template <typename ITy> bool match(ITy *V) const {
230 if (L.match(V))
231 if (R.match(V))
232 return true;
233 return false;
234 }
235};
236
237/// Combine two pattern matchers matching L || R
238template <typename LTy, typename RTy>
239inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
240 return match_combine_or<LTy, RTy>(L, R);
241}
242
243/// Combine two pattern matchers matching L && R
244template <typename LTy, typename RTy>
245inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
246 return match_combine_and<LTy, RTy>(L, R);
247}
248
250 const APInt *&Res;
252
255
256 template <typename ITy> bool match(ITy *V) const {
257 if (auto *CI = dyn_cast<ConstantInt>(V)) {
258 Res = &CI->getValue();
259 return true;
260 }
261 if (V->getType()->isVectorTy())
262 if (const auto *C = dyn_cast<Constant>(V))
263 if (auto *CI =
265 Res = &CI->getValue();
266 return true;
267 }
268 return false;
269 }
270};
271// Either constexpr if or renaming ConstantFP::getValueAPF to
272// ConstantFP::getValue is needed to do it via single template
273// function for both apint/apfloat.
275 const APFloat *&Res;
277
280
281 template <typename ITy> bool match(ITy *V) const {
282 if (auto *CI = dyn_cast<ConstantFP>(V)) {
283 Res = &CI->getValueAPF();
284 return true;
285 }
286 if (V->getType()->isVectorTy())
287 if (const auto *C = dyn_cast<Constant>(V))
288 if (auto *CI =
289 dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowPoison))) {
290 Res = &CI->getValueAPF();
291 return true;
292 }
293 return false;
294 }
295};
296
297/// Match a ConstantInt or splatted ConstantVector, binding the
298/// specified pointer to the contained APInt.
299inline apint_match m_APInt(const APInt *&Res) {
300 // Forbid poison by default to maintain previous behavior.
301 return apint_match(Res, /* AllowPoison */ false);
302}
303
304/// Match APInt while allowing poison in splat vector constants.
306 return apint_match(Res, /* AllowPoison */ true);
307}
308
309/// Match APInt while forbidding poison in splat vector constants.
311 return apint_match(Res, /* AllowPoison */ false);
312}
313
314/// Match a ConstantFP or splatted ConstantVector, binding the
315/// specified pointer to the contained APFloat.
316inline apfloat_match m_APFloat(const APFloat *&Res) {
317 // Forbid undefs by default to maintain previous behavior.
318 return apfloat_match(Res, /* AllowPoison */ false);
319}
320
321/// Match APFloat while allowing poison in splat vector constants.
323 return apfloat_match(Res, /* AllowPoison */ true);
324}
325
326/// Match APFloat while forbidding poison in splat vector constants.
328 return apfloat_match(Res, /* AllowPoison */ false);
329}
330
331template <int64_t Val> struct constantint_match {
332 template <typename ITy> bool match(ITy *V) const {
333 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
334 const APInt &CIV = CI->getValue();
335 if (Val >= 0)
336 return CIV == static_cast<uint64_t>(Val);
337 // If Val is negative, and CI is shorter than it, truncate to the right
338 // number of bits. If it is larger, then we have to sign extend. Just
339 // compare their negated values.
340 return -CIV == -Val;
341 }
342 return false;
343 }
344};
345
346/// Match a ConstantInt with a specific value.
347template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
348 return constantint_match<Val>();
349}
350
351/// This helper class is used to match constant scalars, vector splats,
352/// and fixed width vectors that satisfy a specified predicate.
353/// For fixed width vector constants, poison elements are ignored if AllowPoison
354/// is true.
355template <typename Predicate, typename ConstantVal, bool AllowPoison>
356struct cstval_pred_ty : public Predicate {
357 const Constant **Res = nullptr;
358 template <typename ITy> bool match_impl(ITy *V) const {
359 if (const auto *CV = dyn_cast<ConstantVal>(V))
360 return this->isValue(CV->getValue());
361 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
362 if (const auto *C = dyn_cast<Constant>(V)) {
363 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
364 return this->isValue(CV->getValue());
365
366 // Number of elements of a scalable vector unknown at compile time
367 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
368 if (!FVTy)
369 return false;
370
371 // Non-splat vector constant: check each element for a match.
372 unsigned NumElts = FVTy->getNumElements();
373 assert(NumElts != 0 && "Constant vector with no elements?");
374 bool HasNonPoisonElements = false;
375 for (unsigned i = 0; i != NumElts; ++i) {
376 Constant *Elt = C->getAggregateElement(i);
377 if (!Elt)
378 return false;
379 if (AllowPoison && isa<PoisonValue>(Elt))
380 continue;
381 auto *CV = dyn_cast<ConstantVal>(Elt);
382 if (!CV || !this->isValue(CV->getValue()))
383 return false;
384 HasNonPoisonElements = true;
385 }
386 return HasNonPoisonElements;
387 }
388 }
389 return false;
390 }
391
392 template <typename ITy> bool match(ITy *V) const {
393 if (this->match_impl(V)) {
394 if (Res)
395 *Res = cast<Constant>(V);
396 return true;
397 }
398 return false;
399 }
400};
401
402/// specialization of cstval_pred_ty for ConstantInt
403template <typename Predicate, bool AllowPoison = true>
405
406/// specialization of cstval_pred_ty for ConstantFP
407template <typename Predicate>
409 /*AllowPoison=*/true>;
410
411/// This helper class is used to match scalar and vector constants that
412/// satisfy a specified predicate, and bind them to an APInt.
413template <typename Predicate> struct api_pred_ty : public Predicate {
414 const APInt *&Res;
415
416 api_pred_ty(const APInt *&R) : Res(R) {}
417
418 template <typename ITy> bool match(ITy *V) const {
419 if (const auto *CI = dyn_cast<ConstantInt>(V))
420 if (this->isValue(CI->getValue())) {
421 Res = &CI->getValue();
422 return true;
423 }
424 if (V->getType()->isVectorTy())
425 if (const auto *C = dyn_cast<Constant>(V))
426 if (auto *CI = dyn_cast_or_null<ConstantInt>(
427 C->getSplatValue(/*AllowPoison=*/true)))
428 if (this->isValue(CI->getValue())) {
429 Res = &CI->getValue();
430 return true;
431 }
432
433 return false;
434 }
435};
436
437/// This helper class is used to match scalar and vector constants that
438/// satisfy a specified predicate, and bind them to an APFloat.
439/// Poison is allowed in splat vector constants.
440template <typename Predicate> struct apf_pred_ty : public Predicate {
441 const APFloat *&Res;
442
443 apf_pred_ty(const APFloat *&R) : Res(R) {}
444
445 template <typename ITy> bool match(ITy *V) const {
446 if (const auto *CI = dyn_cast<ConstantFP>(V))
447 if (this->isValue(CI->getValue())) {
448 Res = &CI->getValue();
449 return true;
450 }
451 if (V->getType()->isVectorTy())
452 if (const auto *C = dyn_cast<Constant>(V))
453 if (auto *CI = dyn_cast_or_null<ConstantFP>(
454 C->getSplatValue(/* AllowPoison */ true)))
455 if (this->isValue(CI->getValue())) {
456 Res = &CI->getValue();
457 return true;
458 }
459
460 return false;
461 }
462};
463
464///////////////////////////////////////////////////////////////////////////////
465//
466// Encapsulate constant value queries for use in templated predicate matchers.
467// This allows checking if constants match using compound predicates and works
468// with vector constants, possibly with relaxed constraints. For example, ignore
469// undef values.
470//
471///////////////////////////////////////////////////////////////////////////////
472
473template <typename APTy> struct custom_checkfn {
474 function_ref<bool(const APTy &)> CheckFn;
475 bool isValue(const APTy &C) const { return CheckFn(C); }
476};
477
478/// Match an integer or vector where CheckFn(ele) for each element is true.
479/// For vectors, poison elements are assumed to match.
481m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
482 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
483}
484
486m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
487 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
488}
489
490/// Match a float or vector where CheckFn(ele) for each element is true.
491/// For vectors, poison elements are assumed to match.
493m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
494 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
495}
496
498m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
499 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
500}
501
503 bool isValue(const APInt &C) const { return true; }
504};
505/// Match an integer or vector with any integral constant.
506/// For vectors, this includes constants with undefined elements.
510
512 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
513};
514
518
520 bool isValue(const APInt &C) const { return C.isAllOnes(); }
521};
522/// Match an integer or vector with all bits set.
523/// For vectors, this includes constants with undefined elements.
527
531
533 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
534};
535/// Match an integer or vector with values having all bits except for the high
536/// bit set (0x7f...).
537/// For vectors, this includes constants with undefined elements.
542 return V;
543}
544
546 bool isValue(const APInt &C) const { return C.isNegative(); }
547};
548/// Match an integer or vector of negative values.
549/// For vectors, this includes constants with undefined elements.
553inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
554
556 bool isValue(const APInt &C) const { return C.isNonNegative(); }
557};
558/// Match an integer or vector of non-negative values.
559/// For vectors, this includes constants with undefined elements.
563inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
564
566 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
567};
568/// Match an integer or vector of strictly positive values.
569/// For vectors, this includes constants with undefined elements.
574 return V;
575}
576
578 bool isValue(const APInt &C) const { return C.isNonPositive(); }
579};
580/// Match an integer or vector of non-positive values.
581/// For vectors, this includes constants with undefined elements.
585inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
586
587struct is_one {
588 bool isValue(const APInt &C) const { return C.isOne(); }
589};
590/// Match an integer 1 or a vector with all elements equal to 1.
591/// For vectors, this includes constants with undefined elements.
593
595 bool isValue(const APInt &C) const { return C.isZero(); }
596};
597/// Match an integer 0 or a vector with all elements equal to 0.
598/// For vectors, this includes constants with undefined elements.
602
603struct is_zero {
604 template <typename ITy> bool match(ITy *V) const {
605 auto *C = dyn_cast<Constant>(V);
606 // FIXME: this should be able to do something for scalable vectors
607 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
608 }
609};
610/// Match any null constant or a vector with all elements equal to 0.
611/// For vectors, this includes constants with undefined elements.
612inline is_zero m_Zero() { return is_zero(); }
613
614struct is_power2 {
615 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
616};
617/// Match an integer or vector power-of-2.
618/// For vectors, this includes constants with undefined elements.
620inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
621
623 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
624};
625/// Match a integer or vector negated power-of-2.
626/// For vectors, this includes constants with undefined elements.
631 return V;
632}
633
635 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
636};
637/// Match a integer or vector negated power-of-2.
638/// For vectors, this includes constants with undefined elements.
644 return V;
645}
646
648 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
649};
650/// Match an integer or vector of 0 or power-of-2 values.
651/// For vectors, this includes constants with undefined elements.
656 return V;
657}
658
660 bool isValue(const APInt &C) const { return C.isSignMask(); }
661};
662/// Match an integer or vector with only the sign bit(s) set.
663/// For vectors, this includes constants with undefined elements.
667
669 bool isValue(const APInt &C) const { return C.isMask(); }
670};
671/// Match an integer or vector with only the low bit(s) set.
672/// For vectors, this includes constants with undefined elements.
676inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
677
679 bool isValue(const APInt &C) const { return !C || C.isMask(); }
680};
681/// Match an integer or vector with only the low bit(s) set.
682/// For vectors, this includes constants with undefined elements.
687 return V;
688}
689
692 const APInt *Thr;
693 bool isValue(const APInt &C) const {
694 return ICmpInst::compare(C, *Thr, Pred);
695 }
696};
697/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
698/// to Threshold. For vectors, this includes constants with undefined elements.
702 P.Pred = Predicate;
703 P.Thr = &Threshold;
704 return P;
705}
706
707struct is_nan {
708 bool isValue(const APFloat &C) const { return C.isNaN(); }
709};
710/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
711/// For vectors, this includes constants with undefined elements.
713
714struct is_nonnan {
715 bool isValue(const APFloat &C) const { return !C.isNaN(); }
716};
717/// Match a non-NaN FP constant.
718/// For vectors, this includes constants with undefined elements.
722
723struct is_inf {
724 bool isValue(const APFloat &C) const { return C.isInfinity(); }
725};
726/// Match a positive or negative infinity FP constant.
727/// For vectors, this includes constants with undefined elements.
729
730struct is_noninf {
731 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
732};
733/// Match a non-infinity FP constant, i.e. finite or NaN.
734/// For vectors, this includes constants with undefined elements.
738
739struct is_finite {
740 bool isValue(const APFloat &C) const { return C.isFinite(); }
741};
742/// Match a finite FP constant, i.e. not infinity or NaN.
743/// For vectors, this includes constants with undefined elements.
747inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
748
750 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
751};
752/// Match a finite non-zero FP constant.
753/// For vectors, this includes constants with undefined elements.
758 return V;
759}
760
762 bool isValue(const APFloat &C) const { return C.isZero(); }
763};
764/// Match a floating-point negative zero or positive zero.
765/// For vectors, this includes constants with undefined elements.
769
771 bool isValue(const APFloat &C) const { return C.isPosZero(); }
772};
773/// Match a floating-point positive zero.
774/// For vectors, this includes constants with undefined elements.
778
780 bool isValue(const APFloat &C) const { return C.isNegZero(); }
781};
782/// Match a floating-point negative zero.
783/// For vectors, this includes constants with undefined elements.
787
789 bool isValue(const APFloat &C) const { return C.isNonZero(); }
790};
791/// Match a floating-point non-zero.
792/// For vectors, this includes constants with undefined elements.
796
798 bool isValue(const APFloat &C) const {
799 return !C.isDenormal() && C.isNonZero();
800 }
801};
802
803/// Match a floating-point non-zero that is not a denormal.
804/// For vectors, this includes constants with undefined elements.
808
809///////////////////////////////////////////////////////////////////////////////
810
811template <typename Class> struct bind_ty {
812 Class *&VR;
813
814 bind_ty(Class *&V) : VR(V) {}
815
816 template <typename ITy> bool match(ITy *V) const {
817 if (auto *CV = dyn_cast<Class>(V)) {
818 VR = CV;
819 return true;
820 }
821 return false;
822 }
823};
824
825/// Check whether the value has the given Class and matches the nested
826/// pattern. Capture it into the provided variable if successful.
827template <typename Class, typename MatchTy> struct bind_and_match_ty {
828 Class *&VR;
829 MatchTy Match;
830
831 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
832
833 template <typename ITy> bool match(ITy *V) const {
834 auto *CV = dyn_cast<Class>(V);
835 if (CV && Match.match(V)) {
836 VR = CV;
837 return true;
838 }
839 return false;
840 }
841};
842
843/// Match a value, capturing it if we match.
844inline bind_ty<Value> m_Value(Value *&V) { return V; }
845inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
846
847/// Match against the nested pattern, and capture the value if we match.
848template <typename MatchTy>
850 const MatchTy &Match) {
851 return {V, Match};
852}
853
854/// Match against the nested pattern, and capture the value if we match.
855template <typename MatchTy>
857 const MatchTy &Match) {
858 return {V, Match};
859}
860
861/// Match an instruction, capturing it if we match.
863
864/// Match against the nested pattern, and capture the instruction if we match.
865template <typename MatchTy>
867m_Instruction(Instruction *&I, const MatchTy &Match) {
868 return {I, Match};
869}
870
871/// Match a unary operator, capturing it if we match.
873/// Match a binary operator, capturing it if we match.
875/// Match a with overflow intrinsic, capturing it if we match.
881 return I;
882}
883
884/// Match an UndefValue, capturing the value if we match.
886
887/// Match a Constant, capturing the value if we match.
889
890/// Match a ConstantInt, capturing the value if we match.
892
893/// Match a ConstantFP, capturing the value if we match.
895
896/// Match a ConstantExpr, capturing the value if we match.
898
899/// Match a basic block value, capturing it if we match.
902 return V;
903}
904
905// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
906// and use m_Unless(m_ConstantExpr).
908 template <typename ITy> static bool isImmConstant(ITy *V) {
909 if (auto *CV = dyn_cast<Constant>(V)) {
910 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
911 return true;
912
913 if (CV->getType()->isVectorTy()) {
914 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
915 if (!isa<ConstantExpr>(Splat) &&
916 !Splat->containsConstantExpression()) {
917 return true;
918 }
919 }
920 }
921 }
922 return false;
923 }
924};
925
927 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
928};
929
930/// Match an arbitrary immediate Constant and ignore it.
932
935
937
938 template <typename ITy> bool match(ITy *V) const {
939 if (isImmConstant(V)) {
940 VR = cast<Constant>(V);
941 return true;
942 }
943 return false;
944 }
945};
946
947/// Match an immediate Constant, capturing the value if we match.
951
952/// Match a specified Value*.
954 const Value *Val;
955
956 specificval_ty(const Value *V) : Val(V) {}
957
958 template <typename ITy> bool match(ITy *V) const { return V == Val; }
959};
960
961/// Match if we have a specific specified value.
962inline specificval_ty m_Specific(const Value *V) { return V; }
963
964/// Stores a reference to the Value *, not the Value * itself,
965/// thus can be used in commutative matchers.
966template <typename Class> struct deferredval_ty {
967 Class *const &Val;
968
969 deferredval_ty(Class *const &V) : Val(V) {}
970
971 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
972};
973
974/// Like m_Specific(), but works if the specific value to match is determined
975/// as part of the same match() expression. For example:
976/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
977/// bind X before the pattern match starts.
978/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
979/// whichever value m_Value(X) populated.
980inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
982 return V;
983}
984
985/// Match a specified floating point value or vector of all elements of
986/// that value.
988 double Val;
989
990 specific_fpval(double V) : Val(V) {}
991
992 template <typename ITy> bool match(ITy *V) const {
993 if (const auto *CFP = dyn_cast<ConstantFP>(V))
994 return CFP->isExactlyValue(Val);
995 if (V->getType()->isVectorTy())
996 if (const auto *C = dyn_cast<Constant>(V))
997 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
998 return CFP->isExactlyValue(Val);
999 return false;
1000 }
1001};
1002
1003/// Match a specific floating point value or vector with all elements
1004/// equal to the value.
1005inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1006
1007/// Match a float 1.0 or vector with all elements equal to 1.0.
1008inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1009
1012
1014
1015 template <typename ITy> bool match(ITy *V) const {
1016 const APInt *ConstInt;
1017 if (!apint_match(ConstInt, /*AllowPoison=*/false).match(V))
1018 return false;
1019 if (ConstInt->getActiveBits() > 64)
1020 return false;
1021 VR = ConstInt->getZExtValue();
1022 return true;
1023 }
1024};
1025
1026/// Match a specified integer value or vector of all elements of that
1027/// value.
1028template <bool AllowPoison> struct specific_intval {
1029 const APInt &Val;
1030
1031 specific_intval(const APInt &V) : Val(V) {}
1032
1033 template <typename ITy> bool match(ITy *V) const {
1034 const auto *CI = dyn_cast<ConstantInt>(V);
1035 if (!CI && V->getType()->isVectorTy())
1036 if (const auto *C = dyn_cast<Constant>(V))
1037 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1038
1039 return CI && APInt::isSameValue(CI->getValue(), Val);
1040 }
1041};
1042
1043template <bool AllowPoison> struct specific_intval64 {
1045
1047
1048 template <typename ITy> bool match(ITy *V) const {
1049 const auto *CI = dyn_cast<ConstantInt>(V);
1050 if (!CI && V->getType()->isVectorTy())
1051 if (const auto *C = dyn_cast<Constant>(V))
1052 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1053
1054 return CI && CI->getValue() == Val;
1055 }
1056};
1057
1058/// Match a specific integer value or vector with all elements equal to
1059/// the value.
1061 return specific_intval<false>(V);
1062}
1063
1067
1071
1075
1076/// Match a ConstantInt and bind to its value. This does not match
1077/// ConstantInts wider than 64-bits.
1079
1080/// Match a specified basic block value.
1083
1085
1086 template <typename ITy> bool match(ITy *V) const {
1087 const auto *BB = dyn_cast<BasicBlock>(V);
1088 return BB && BB == Val;
1089 }
1090};
1091
1092/// Match a specific basic block value.
1094 return specific_bbval(BB);
1095}
1096
1097/// A commutative-friendly version of m_Specific().
1099 return BB;
1100}
1102m_Deferred(const BasicBlock *const &BB) {
1103 return BB;
1104}
1105
1106//===----------------------------------------------------------------------===//
1107// Matcher for any binary operator.
1108//
1109template <typename LHS_t, typename RHS_t, bool Commutable = false>
1113
1114 // The evaluation order is always stable, regardless of Commutability.
1115 // The LHS is always matched first.
1116 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1117
1118 template <typename OpTy> bool match(OpTy *V) const {
1119 if (auto *I = dyn_cast<BinaryOperator>(V))
1120 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1121 (Commutable && L.match(I->getOperand(1)) &&
1122 R.match(I->getOperand(0)));
1123 return false;
1124 }
1125};
1126
1127template <typename LHS, typename RHS>
1128inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1129 return AnyBinaryOp_match<LHS, RHS>(L, R);
1130}
1131
1132//===----------------------------------------------------------------------===//
1133// Matcher for any unary operator.
1134// TODO fuse unary, binary matcher into n-ary matcher
1135//
1136template <typename OP_t> struct AnyUnaryOp_match {
1137 OP_t X;
1138
1139 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1140
1141 template <typename OpTy> bool match(OpTy *V) const {
1142 if (auto *I = dyn_cast<UnaryOperator>(V))
1143 return X.match(I->getOperand(0));
1144 return false;
1145 }
1146};
1147
1148template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1149 return AnyUnaryOp_match<OP_t>(X);
1150}
1151
1152//===----------------------------------------------------------------------===//
1153// Matchers for specific binary operators.
1154//
1155
1156template <typename LHS_t, typename RHS_t, unsigned Opcode,
1157 bool Commutable = false>
1161
1162 // The evaluation order is always stable, regardless of Commutability.
1163 // The LHS is always matched first.
1164 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1165
1166 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1167 if (V->getValueID() == Value::InstructionVal + Opc) {
1168 auto *I = cast<BinaryOperator>(V);
1169 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1170 (Commutable && L.match(I->getOperand(1)) &&
1171 R.match(I->getOperand(0)));
1172 }
1173 return false;
1174 }
1175
1176 template <typename OpTy> bool match(OpTy *V) const {
1177 return match(Opcode, V);
1178 }
1179};
1180
1181template <typename LHS, typename RHS>
1186
1187template <typename LHS, typename RHS>
1192
1193template <typename LHS, typename RHS>
1198
1199template <typename LHS, typename RHS>
1204
1205template <typename Op_t> struct FNeg_match {
1206 Op_t X;
1207
1208 FNeg_match(const Op_t &Op) : X(Op) {}
1209 template <typename OpTy> bool match(OpTy *V) const {
1210 auto *FPMO = dyn_cast<FPMathOperator>(V);
1211 if (!FPMO)
1212 return false;
1213
1214 if (FPMO->getOpcode() == Instruction::FNeg)
1215 return X.match(FPMO->getOperand(0));
1216
1217 if (FPMO->getOpcode() == Instruction::FSub) {
1218 if (FPMO->hasNoSignedZeros()) {
1219 // With 'nsz', any zero goes.
1220 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1221 return false;
1222 } else {
1223 // Without 'nsz', we need fsub -0.0, X exactly.
1224 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1225 return false;
1226 }
1227
1228 return X.match(FPMO->getOperand(1));
1229 }
1230
1231 return false;
1232 }
1233};
1234
1235/// Match 'fneg X' as 'fsub -0.0, X'.
1236template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1237 return FNeg_match<OpTy>(X);
1238}
1239
1240/// Match 'fneg X' as 'fsub +-0.0, X'.
1241template <typename RHS>
1242inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1243m_FNegNSZ(const RHS &X) {
1244 return m_FSub(m_AnyZeroFP(), X);
1245}
1246
1247template <typename LHS, typename RHS>
1252
1253template <typename LHS, typename RHS>
1258
1259template <typename LHS, typename RHS>
1264
1265template <typename LHS, typename RHS>
1270
1271template <typename LHS, typename RHS>
1276
1277template <typename LHS, typename RHS>
1282
1283template <typename LHS, typename RHS>
1288
1289template <typename LHS, typename RHS>
1294
1295template <typename LHS, typename RHS>
1300
1301template <typename LHS, typename RHS>
1306
1307template <typename LHS, typename RHS>
1312
1313template <typename LHS, typename RHS>
1318
1319template <typename LHS, typename RHS>
1324
1325template <typename LHS, typename RHS>
1330
1331template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1334
1336
1337 template <typename OpTy> bool match(OpTy *V) const {
1338 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1339 if (Op->getOpcode() == Opcode)
1340 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1341 L.match(Op->getOperand(0));
1342 }
1343 // Interpreted as shiftop V, 0
1344 R = 0;
1345 return L.match(V);
1346 }
1347};
1348
1349/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1350template <typename LHS>
1355
1356/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1357template <typename LHS>
1362
1363/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1364template <typename LHS>
1369
1370template <typename LHS_t, typename RHS_t, unsigned Opcode,
1371 unsigned WrapFlags = 0, bool Commutable = false>
1375
1377 : L(LHS), R(RHS) {}
1378
1379 template <typename OpTy> bool match(OpTy *V) const {
1380 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1381 if (Op->getOpcode() != Opcode)
1382 return false;
1384 !Op->hasNoUnsignedWrap())
1385 return false;
1386 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1387 !Op->hasNoSignedWrap())
1388 return false;
1389 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1390 (Commutable && L.match(Op->getOperand(1)) &&
1391 R.match(Op->getOperand(0)));
1392 }
1393 return false;
1394 }
1395};
1396
1397template <typename LHS, typename RHS>
1398inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1400m_NSWAdd(const LHS &L, const RHS &R) {
1401 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1403 R);
1404}
1405template <typename LHS, typename RHS>
1406inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1408m_c_NSWAdd(const LHS &L, const RHS &R) {
1409 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1411 true>(L, R);
1412}
1413template <typename LHS, typename RHS>
1414inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1416m_NSWSub(const LHS &L, const RHS &R) {
1417 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1419 R);
1420}
1421template <typename LHS, typename RHS>
1422inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1424m_NSWMul(const LHS &L, const RHS &R) {
1425 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1427 R);
1428}
1429template <typename LHS, typename RHS>
1430inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1432m_NSWShl(const LHS &L, const RHS &R) {
1433 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1435 R);
1436}
1437
1438template <typename LHS, typename RHS>
1439inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1441m_NUWAdd(const LHS &L, const RHS &R) {
1442 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1444 L, R);
1445}
1446
1447template <typename LHS, typename RHS>
1449 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1450m_c_NUWAdd(const LHS &L, const RHS &R) {
1451 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1453 true>(L, R);
1454}
1455
1456template <typename LHS, typename RHS>
1457inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1459m_NUWSub(const LHS &L, const RHS &R) {
1460 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1462 L, R);
1463}
1464template <typename LHS, typename RHS>
1465inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1467m_NUWMul(const LHS &L, const RHS &R) {
1468 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1470 L, R);
1471}
1472template <typename LHS, typename RHS>
1473inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1475m_NUWShl(const LHS &L, const RHS &R) {
1476 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1478 L, R);
1479}
1480
1481template <typename LHS_t, typename RHS_t, bool Commutable = false>
1483 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1484 unsigned Opcode;
1485
1487 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1488
1489 template <typename OpTy> bool match(OpTy *V) const {
1491 }
1492};
1493
1494/// Matches a specific opcode.
1495template <typename LHS, typename RHS>
1496inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1497 const RHS &R) {
1498 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1499}
1500
1501template <typename LHS, typename RHS, bool Commutable = false>
1505
1506 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1507
1508 template <typename OpTy> bool match(OpTy *V) const {
1509 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1510 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1511 if (!PDI->isDisjoint())
1512 return false;
1513 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1514 (Commutable && L.match(PDI->getOperand(1)) &&
1515 R.match(PDI->getOperand(0)));
1516 }
1517 return false;
1518 }
1519};
1520
1521template <typename LHS, typename RHS>
1523 return DisjointOr_match<LHS, RHS>(L, R);
1524}
1525
1526template <typename LHS, typename RHS>
1528 const RHS &R) {
1530}
1531
1532/// Match either "add" or "or disjoint".
1533template <typename LHS, typename RHS>
1536m_AddLike(const LHS &L, const RHS &R) {
1537 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1538}
1539
1540/// Match either "add nsw" or "or disjoint"
1541template <typename LHS, typename RHS>
1542inline match_combine_or<
1543 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1546m_NSWAddLike(const LHS &L, const RHS &R) {
1547 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1548}
1549
1550/// Match either "add nuw" or "or disjoint"
1551template <typename LHS, typename RHS>
1552inline match_combine_or<
1553 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1556m_NUWAddLike(const LHS &L, const RHS &R) {
1557 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1558}
1559
1560template <typename LHS, typename RHS>
1564
1565 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1566
1567 template <typename OpTy> bool match(OpTy *V) const {
1568 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1569 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1570 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1571 ; // Pass
1572 else if (Op->getOpcode() != Instruction::Xor)
1573 return false;
1574 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1575 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1576 }
1577 return false;
1578 }
1579};
1580
1581/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1582/// Only commutative matcher as the `sub` will need to swap the L and R.
1583template <typename LHS, typename RHS>
1584inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1585 return XorLike_match<LHS, RHS>(L, R);
1586}
1587
1588//===----------------------------------------------------------------------===//
1589// Class that matches a group of binary opcodes.
1590//
1591template <typename LHS_t, typename RHS_t, typename Predicate,
1592 bool Commutable = false>
1593struct BinOpPred_match : Predicate {
1596
1597 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1598
1599 template <typename OpTy> bool match(OpTy *V) const {
1600 if (auto *I = dyn_cast<Instruction>(V))
1601 return this->isOpType(I->getOpcode()) &&
1602 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1603 (Commutable && L.match(I->getOperand(1)) &&
1604 R.match(I->getOperand(0))));
1605 return false;
1606 }
1607};
1608
1610 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1611};
1612
1614 bool isOpType(unsigned Opcode) const {
1615 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1616 }
1617};
1618
1620 bool isOpType(unsigned Opcode) const {
1621 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1622 }
1623};
1624
1626 bool isOpType(unsigned Opcode) const {
1627 return Instruction::isBitwiseLogicOp(Opcode);
1628 }
1629};
1630
1632 bool isOpType(unsigned Opcode) const {
1633 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1634 }
1635};
1636
1638 bool isOpType(unsigned Opcode) const {
1639 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1640 }
1641};
1642
1643/// Matches shift operations.
1644template <typename LHS, typename RHS>
1646 const RHS &R) {
1648}
1649
1650/// Matches logical shift operations.
1651template <typename LHS, typename RHS>
1656
1657/// Matches logical shift operations.
1658template <typename LHS, typename RHS>
1660m_LogicalShift(const LHS &L, const RHS &R) {
1662}
1663
1664/// Matches bitwise logic operations.
1665template <typename LHS, typename RHS>
1667m_BitwiseLogic(const LHS &L, const RHS &R) {
1669}
1670
1671/// Matches bitwise logic operations in either order.
1672template <typename LHS, typename RHS>
1677
1678/// Matches integer division operations.
1679template <typename LHS, typename RHS>
1681 const RHS &R) {
1683}
1684
1685/// Matches integer remainder operations.
1686template <typename LHS, typename RHS>
1688 const RHS &R) {
1690}
1691
1692//===----------------------------------------------------------------------===//
1693// Class that matches exact binary ops.
1694//
1695template <typename SubPattern_t> struct Exact_match {
1696 SubPattern_t SubPattern;
1697
1698 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1699
1700 template <typename OpTy> bool match(OpTy *V) const {
1701 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1702 return PEO->isExact() && SubPattern.match(V);
1703 return false;
1704 }
1705};
1706
1707template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1708 return SubPattern;
1709}
1710
1711//===----------------------------------------------------------------------===//
1712// Matchers for CmpInst classes
1713//
1714
1715template <typename LHS_t, typename RHS_t, typename Class,
1716 bool Commutable = false>
1721
1722 // The evaluation order is always stable, regardless of Commutability.
1723 // The LHS is always matched first.
1725 : Predicate(&Pred), L(LHS), R(RHS) {}
1727 : Predicate(nullptr), L(LHS), R(RHS) {}
1728
1729 template <typename OpTy> bool match(OpTy *V) const {
1730 if (auto *I = dyn_cast<Class>(V)) {
1731 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1732 if (Predicate)
1734 return true;
1735 }
1736 if (Commutable && L.match(I->getOperand(1)) &&
1737 R.match(I->getOperand(0))) {
1738 if (Predicate)
1740 return true;
1741 }
1742 }
1743 return false;
1744 }
1745};
1746
1747template <typename LHS, typename RHS>
1749 const RHS &R) {
1750 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1751}
1752
1753template <typename LHS, typename RHS>
1755 const LHS &L, const RHS &R) {
1756 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1757}
1758
1759template <typename LHS, typename RHS>
1761 const LHS &L, const RHS &R) {
1762 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1763}
1764
1765template <typename LHS, typename RHS>
1768}
1769
1770template <typename LHS, typename RHS>
1773}
1774
1775template <typename LHS, typename RHS>
1778}
1779
1780// Same as CmpClass, but instead of saving Pred as out output variable, match a
1781// specific input pred for equality.
1782template <typename LHS_t, typename RHS_t, typename Class,
1783 bool Commutable = false>
1788
1790 : Predicate(Pred), L(LHS), R(RHS) {}
1791
1792 template <typename OpTy> bool match(OpTy *V) const {
1793 if (auto *I = dyn_cast<Class>(V)) {
1795 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1796 return true;
1797 if constexpr (Commutable) {
1800 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1801 return true;
1802 }
1803 }
1804
1805 return false;
1806 }
1807};
1808
1809template <typename LHS, typename RHS>
1811m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1812 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1813}
1814
1815template <typename LHS, typename RHS>
1817m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1818 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1819}
1820
1821template <typename LHS, typename RHS>
1823m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1825}
1826
1827template <typename LHS, typename RHS>
1829m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1830 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1831}
1832
1833//===----------------------------------------------------------------------===//
1834// Matchers for instructions with a given opcode and number of operands.
1835//
1836
1837/// Matches instructions with Opcode and three operands.
1838template <typename T0, unsigned Opcode> struct OneOps_match {
1840
1841 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1842
1843 template <typename OpTy> bool match(OpTy *V) const {
1844 if (V->getValueID() == Value::InstructionVal + Opcode) {
1845 auto *I = cast<Instruction>(V);
1846 return Op1.match(I->getOperand(0));
1847 }
1848 return false;
1849 }
1850};
1851
1852/// Matches instructions with Opcode and three operands.
1853template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1856
1857 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1858
1859 template <typename OpTy> bool match(OpTy *V) const {
1860 if (V->getValueID() == Value::InstructionVal + Opcode) {
1861 auto *I = cast<Instruction>(V);
1862 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1863 }
1864 return false;
1865 }
1866};
1867
1868/// Matches instructions with Opcode and three operands.
1869template <typename T0, typename T1, typename T2, unsigned Opcode,
1870 bool CommutableOp2Op3 = false>
1875
1876 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1877 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1878
1879 template <typename OpTy> bool match(OpTy *V) const {
1880 if (V->getValueID() == Value::InstructionVal + Opcode) {
1881 auto *I = cast<Instruction>(V);
1882 if (!Op1.match(I->getOperand(0)))
1883 return false;
1884 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1885 return true;
1886 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1887 Op3.match(I->getOperand(1));
1888 }
1889 return false;
1890 }
1891};
1892
1893/// Matches instructions with Opcode and any number of operands
1894template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1895 std::tuple<OperandTypes...> Operands;
1896
1897 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1898
1899 // Operand matching works by recursively calling match_operands, matching the
1900 // operands left to right. The first version is called for each operand but
1901 // the last, for which the second version is called. The second version of
1902 // match_operands is also used to match each individual operand.
1903 template <int Idx, int Last>
1904 std::enable_if_t<Idx != Last, bool>
1908
1909 template <int Idx, int Last>
1910 std::enable_if_t<Idx == Last, bool>
1912 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1913 }
1914
1915 template <typename OpTy> bool match(OpTy *V) const {
1916 if (V->getValueID() == Value::InstructionVal + Opcode) {
1917 auto *I = cast<Instruction>(V);
1918 return I->getNumOperands() == sizeof...(OperandTypes) &&
1919 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1920 }
1921 return false;
1922 }
1923};
1924
1925/// Matches SelectInst.
1926template <typename Cond, typename LHS, typename RHS>
1928m_Select(const Cond &C, const LHS &L, const RHS &R) {
1930}
1931
1932/// This matches a select of two constants, e.g.:
1933/// m_SelectCst<-1, 0>(m_Value(V))
1934template <int64_t L, int64_t R, typename Cond>
1936 Instruction::Select>
1939}
1940
1941/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1942template <typename LHS, typename RHS>
1943inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1944m_c_Select(const LHS &L, const RHS &R) {
1945 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1946 true>(m_Value(), L, R);
1947}
1948
1949/// Matches FreezeInst.
1950template <typename OpTy>
1954
1955/// Matches InsertElementInst.
1956template <typename Val_t, typename Elt_t, typename Idx_t>
1958m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1960 Val, Elt, Idx);
1961}
1962
1963/// Matches ExtractElementInst.
1964template <typename Val_t, typename Idx_t>
1966m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1968}
1969
1970/// Matches shuffle.
1971template <typename T0, typename T1, typename T2> struct Shuffle_match {
1975
1976 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1977 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1978
1979 template <typename OpTy> bool match(OpTy *V) const {
1980 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1981 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1982 Mask.match(I->getShuffleMask());
1983 }
1984 return false;
1985 }
1986};
1987
1988struct m_Mask {
1991 bool match(ArrayRef<int> Mask) const {
1992 MaskRef = Mask;
1993 return true;
1994 }
1995};
1996
1998 bool match(ArrayRef<int> Mask) const {
1999 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2000 }
2001};
2002
2006 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2007};
2008
2012 bool match(ArrayRef<int> Mask) const {
2013 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2014 if (First == Mask.end())
2015 return false;
2016 SplatIndex = *First;
2017 return all_of(Mask,
2018 [First](int Elem) { return Elem == *First || Elem == -1; });
2019 }
2020};
2021
2022template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2023 PointerOpTy PointerOp;
2024 OffsetOpTy OffsetOp;
2025
2026 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2028
2029 template <typename OpTy> bool match(OpTy *V) const {
2030 auto *GEP = dyn_cast<GEPOperator>(V);
2031 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2032 PointerOp.match(GEP->getPointerOperand()) &&
2033 OffsetOp.match(GEP->idx_begin()->get());
2034 }
2035};
2036
2037/// Matches ShuffleVectorInst independently of mask value.
2038template <typename V1_t, typename V2_t>
2040m_Shuffle(const V1_t &v1, const V2_t &v2) {
2042}
2043
2044template <typename V1_t, typename V2_t, typename Mask_t>
2046m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2047 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
2048}
2049
2050/// Matches LoadInst.
2051template <typename OpTy>
2055
2056/// Matches StoreInst.
2057template <typename ValueOpTy, typename PointerOpTy>
2059m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2061 PointerOp);
2062}
2063
2064/// Matches GetElementPtrInst.
2065template <typename... OperandTypes>
2066inline auto m_GEP(const OperandTypes &...Ops) {
2067 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2068}
2069
2070/// Matches GEP with i8 source element type
2071template <typename PointerOpTy, typename OffsetOpTy>
2073m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2075}
2076
2077//===----------------------------------------------------------------------===//
2078// Matchers for CastInst classes
2079//
2080
2081template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2082 Op_t Op;
2083
2084 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2085
2086 template <typename OpTy> bool match(OpTy *V) const {
2087 if (auto *O = dyn_cast<Operator>(V))
2088 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2089 return false;
2090 }
2091};
2092
2093template <typename Op_t, typename Class> struct CastInst_match {
2094 Op_t Op;
2095
2096 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2097
2098 template <typename OpTy> bool match(OpTy *V) const {
2099 if (auto *I = dyn_cast<Class>(V))
2100 return Op.match(I->getOperand(0));
2101 return false;
2102 }
2103};
2104
2105template <typename Op_t> struct PtrToIntSameSize_match {
2107 Op_t Op;
2108
2109 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2110 : DL(DL), Op(OpMatch) {}
2111
2112 template <typename OpTy> bool match(OpTy *V) const {
2113 if (auto *O = dyn_cast<Operator>(V))
2114 return O->getOpcode() == Instruction::PtrToInt &&
2115 DL.getTypeSizeInBits(O->getType()) ==
2116 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2117 Op.match(O->getOperand(0));
2118 return false;
2119 }
2120};
2121
2122template <typename Op_t> struct NNegZExt_match {
2123 Op_t Op;
2124
2125 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2126
2127 template <typename OpTy> bool match(OpTy *V) const {
2128 if (auto *I = dyn_cast<ZExtInst>(V))
2129 return I->hasNonNeg() && Op.match(I->getOperand(0));
2130 return false;
2131 }
2132};
2133
2134template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2135 Op_t Op;
2136
2137 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2138
2139 template <typename OpTy> bool match(OpTy *V) const {
2140 if (auto *I = dyn_cast<TruncInst>(V))
2141 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2142 Op.match(I->getOperand(0));
2143 return false;
2144 }
2145};
2146
2147/// Matches BitCast.
2148template <typename OpTy>
2153
2154template <typename Op_t> struct ElementWiseBitCast_match {
2155 Op_t Op;
2156
2157 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2158
2159 template <typename OpTy> bool match(OpTy *V) const {
2160 auto *I = dyn_cast<BitCastInst>(V);
2161 if (!I)
2162 return false;
2163 Type *SrcType = I->getSrcTy();
2164 Type *DstType = I->getType();
2165 // Make sure the bitcast doesn't change between scalar and vector and
2166 // doesn't change the number of vector elements.
2167 if (SrcType->isVectorTy() != DstType->isVectorTy())
2168 return false;
2169 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2170 SrcVecTy && SrcVecTy->getElementCount() !=
2171 cast<VectorType>(DstType)->getElementCount())
2172 return false;
2173 return Op.match(I->getOperand(0));
2174 }
2175};
2176
2177template <typename OpTy>
2181
2182/// Matches PtrToInt.
2183template <typename OpTy>
2188
2189template <typename OpTy>
2194
2195/// Matches IntToPtr.
2196template <typename OpTy>
2201
2202/// Matches any cast or self. Used to ignore casts.
2203template <typename OpTy>
2205m_CastOrSelf(const OpTy &Op) {
2207}
2208
2209/// Matches Trunc.
2210template <typename OpTy>
2214
2215/// Matches trunc nuw.
2216template <typename OpTy>
2221
2222/// Matches trunc nsw.
2223template <typename OpTy>
2228
2229template <typename OpTy>
2231m_TruncOrSelf(const OpTy &Op) {
2232 return m_CombineOr(m_Trunc(Op), Op);
2233}
2234
2235/// Matches SExt.
2236template <typename OpTy>
2240
2241/// Matches ZExt.
2242template <typename OpTy>
2246
2247template <typename OpTy>
2249 return NNegZExt_match<OpTy>(Op);
2250}
2251
2252template <typename OpTy>
2254m_ZExtOrSelf(const OpTy &Op) {
2255 return m_CombineOr(m_ZExt(Op), Op);
2256}
2257
2258template <typename OpTy>
2260m_SExtOrSelf(const OpTy &Op) {
2261 return m_CombineOr(m_SExt(Op), Op);
2262}
2263
2264/// Match either "sext" or "zext nneg".
2265template <typename OpTy>
2267m_SExtLike(const OpTy &Op) {
2268 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2269}
2270
2271template <typename OpTy>
2274m_ZExtOrSExt(const OpTy &Op) {
2275 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2276}
2277
2278template <typename OpTy>
2281 OpTy>
2283 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2284}
2285
2286template <typename OpTy>
2289 OpTy>
2292}
2293
2294template <typename OpTy>
2298
2299template <typename OpTy>
2303
2304template <typename OpTy>
2308
2309template <typename OpTy>
2313
2314template <typename OpTy>
2318
2319template <typename OpTy>
2323
2324//===----------------------------------------------------------------------===//
2325// Matchers for control flow.
2326//
2327
2328struct br_match {
2330
2332
2333 template <typename OpTy> bool match(OpTy *V) const {
2334 if (auto *BI = dyn_cast<BranchInst>(V))
2335 if (BI->isUnconditional()) {
2336 Succ = BI->getSuccessor(0);
2337 return true;
2338 }
2339 return false;
2340 }
2341};
2342
2343inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2344
2345template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2347 Cond_t Cond;
2348 TrueBlock_t T;
2349 FalseBlock_t F;
2350
2351 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2352 : Cond(C), T(t), F(f) {}
2353
2354 template <typename OpTy> bool match(OpTy *V) const {
2355 if (auto *BI = dyn_cast<BranchInst>(V))
2356 if (BI->isConditional() && Cond.match(BI->getCondition()))
2357 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2358 return false;
2359 }
2360};
2361
2362template <typename Cond_t>
2368
2369template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2371m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2373}
2374
2375//===----------------------------------------------------------------------===//
2376// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2377//
2378
2379template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2380 bool Commutable = false>
2382 using PredType = Pred_t;
2385
2386 // The evaluation order is always stable, regardless of Commutability.
2387 // The LHS is always matched first.
2388 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2389
2390 template <typename OpTy> bool match(OpTy *V) const {
2391 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2392 Intrinsic::ID IID = II->getIntrinsicID();
2393 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2394 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2395 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2396 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2397 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2398 return (L.match(LHS) && R.match(RHS)) ||
2399 (Commutable && L.match(RHS) && R.match(LHS));
2400 }
2401 }
2402 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2403 auto *SI = dyn_cast<SelectInst>(V);
2404 if (!SI)
2405 return false;
2406 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2407 if (!Cmp)
2408 return false;
2409 // At this point we have a select conditioned on a comparison. Check that
2410 // it is the values returned by the select that are being compared.
2411 auto *TrueVal = SI->getTrueValue();
2412 auto *FalseVal = SI->getFalseValue();
2413 auto *LHS = Cmp->getOperand(0);
2414 auto *RHS = Cmp->getOperand(1);
2415 if ((TrueVal != LHS || FalseVal != RHS) &&
2416 (TrueVal != RHS || FalseVal != LHS))
2417 return false;
2418 typename CmpInst_t::Predicate Pred =
2419 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2420 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2421 if (!Pred_t::match(Pred))
2422 return false;
2423 // It does! Bind the operands.
2424 return (L.match(LHS) && R.match(RHS)) ||
2425 (Commutable && L.match(RHS) && R.match(LHS));
2426 }
2427};
2428
2429/// Helper class for identifying signed max predicates.
2431 static bool match(ICmpInst::Predicate Pred) {
2432 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2433 }
2434};
2435
2436/// Helper class for identifying signed min predicates.
2438 static bool match(ICmpInst::Predicate Pred) {
2439 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2440 }
2441};
2442
2443/// Helper class for identifying unsigned max predicates.
2445 static bool match(ICmpInst::Predicate Pred) {
2446 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2447 }
2448};
2449
2450/// Helper class for identifying unsigned min predicates.
2452 static bool match(ICmpInst::Predicate Pred) {
2453 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2454 }
2455};
2456
2457/// Helper class for identifying ordered max predicates.
2459 static bool match(FCmpInst::Predicate Pred) {
2460 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2461 }
2462};
2463
2464/// Helper class for identifying ordered min predicates.
2466 static bool match(FCmpInst::Predicate Pred) {
2467 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2468 }
2469};
2470
2471/// Helper class for identifying unordered max predicates.
2473 static bool match(FCmpInst::Predicate Pred) {
2474 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2475 }
2476};
2477
2478/// Helper class for identifying unordered min predicates.
2480 static bool match(FCmpInst::Predicate Pred) {
2481 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2482 }
2483};
2484
2485template <typename LHS, typename RHS>
2490
2491template <typename LHS, typename RHS>
2496
2497template <typename LHS, typename RHS>
2502
2503template <typename LHS, typename RHS>
2508
2509template <typename LHS, typename RHS>
2510inline match_combine_or<
2515m_MaxOrMin(const LHS &L, const RHS &R) {
2516 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2517 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2518}
2519
2520/// Match an 'ordered' floating point maximum function.
2521/// Floating point has one special value 'NaN'. Therefore, there is no total
2522/// order. However, if we can ignore the 'NaN' value (for example, because of a
2523/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2524/// semantics. In the presence of 'NaN' we have to preserve the original
2525/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2526///
2527/// max(L, R) iff L and R are not NaN
2528/// m_OrdFMax(L, R) = R iff L or R are NaN
2529template <typename LHS, typename RHS>
2534
2535/// Match an 'ordered' floating point minimum function.
2536/// Floating point has one special value 'NaN'. Therefore, there is no total
2537/// order. However, if we can ignore the 'NaN' value (for example, because of a
2538/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2539/// semantics. In the presence of 'NaN' we have to preserve the original
2540/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2541///
2542/// min(L, R) iff L and R are not NaN
2543/// m_OrdFMin(L, R) = R iff L or R are NaN
2544template <typename LHS, typename RHS>
2549
2550/// Match an 'unordered' floating point maximum function.
2551/// Floating point has one special value 'NaN'. Therefore, there is no total
2552/// order. However, if we can ignore the 'NaN' value (for example, because of a
2553/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2554/// semantics. In the presence of 'NaN' we have to preserve the original
2555/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2556///
2557/// max(L, R) iff L and R are not NaN
2558/// m_UnordFMax(L, R) = L iff L or R are NaN
2559template <typename LHS, typename RHS>
2561m_UnordFMax(const LHS &L, const RHS &R) {
2563}
2564
2565/// Match an 'unordered' floating point minimum function.
2566/// Floating point has one special value 'NaN'. Therefore, there is no total
2567/// order. However, if we can ignore the 'NaN' value (for example, because of a
2568/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2569/// semantics. In the presence of 'NaN' we have to preserve the original
2570/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2571///
2572/// min(L, R) iff L and R are not NaN
2573/// m_UnordFMin(L, R) = L iff L or R are NaN
2574template <typename LHS, typename RHS>
2576m_UnordFMin(const LHS &L, const RHS &R) {
2578}
2579
2580/// Match an 'ordered' or 'unordered' floating point maximum function.
2581/// Floating point has one special value 'NaN'. Therefore, there is no total
2582/// order. However, if we can ignore the 'NaN' value (for example, because of a
2583/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2584/// semantics.
2585template <typename LHS, typename RHS>
2592
2593/// Match an 'ordered' or 'unordered' floating point minimum 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 'minimum'
2597/// semantics.
2598template <typename LHS, typename RHS>
2605
2606/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2607/// NOTE: we first match the 'Not' (by matching '-1'),
2608/// and only then match the inner matcher!
2609template <typename ValTy>
2610inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2611m_Not(const ValTy &V) {
2612 return m_c_Xor(m_AllOnes(), V);
2613}
2614
2615template <typename ValTy>
2616inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2617 true>
2618m_NotForbidPoison(const ValTy &V) {
2619 return m_c_Xor(m_AllOnesForbidPoison(), V);
2620}
2621
2622//===----------------------------------------------------------------------===//
2623// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2624// Note that S might be matched to other instructions than AddInst.
2625//
2626
2627template <typename LHS_t, typename RHS_t, typename Sum_t>
2631 Sum_t S;
2632
2633 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2634 : L(L), R(R), S(S) {}
2635
2636 template <typename OpTy> bool match(OpTy *V) const {
2637 Value *ICmpLHS, *ICmpRHS;
2638 CmpPredicate Pred;
2639 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2640 return false;
2641
2642 Value *AddLHS, *AddRHS;
2643 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2644
2645 // (a + b) u< a, (a + b) u< b
2646 if (Pred == ICmpInst::ICMP_ULT)
2647 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2648 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2649
2650 // a >u (a + b), b >u (a + b)
2651 if (Pred == ICmpInst::ICMP_UGT)
2652 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2653 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2654
2655 Value *Op1;
2656 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2657 // (~a) <u b
2658 if (Pred == ICmpInst::ICMP_ULT) {
2659 if (XorExpr.match(ICmpLHS))
2660 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2661 }
2662 // b > u (~a)
2663 if (Pred == ICmpInst::ICMP_UGT) {
2664 if (XorExpr.match(ICmpRHS))
2665 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2666 }
2667
2668 // Match special-case for increment-by-1.
2669 if (Pred == ICmpInst::ICMP_EQ) {
2670 // (a + 1) == 0
2671 // (1 + a) == 0
2672 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2673 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2674 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2675 // 0 == (a + 1)
2676 // 0 == (1 + a)
2677 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2678 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2679 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2680 }
2681
2682 return false;
2683 }
2684};
2685
2686/// Match an icmp instruction checking for unsigned overflow on addition.
2687///
2688/// S is matched to the addition whose result is being checked for overflow, and
2689/// L and R are matched to the LHS and RHS of S.
2690template <typename LHS_t, typename RHS_t, typename Sum_t>
2692m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2694}
2695
2696template <typename Opnd_t> struct Argument_match {
2697 unsigned OpI;
2698 Opnd_t Val;
2699
2700 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2701
2702 template <typename OpTy> bool match(OpTy *V) const {
2703 // FIXME: Should likely be switched to use `CallBase`.
2704 if (const auto *CI = dyn_cast<CallInst>(V))
2705 return Val.match(CI->getArgOperand(OpI));
2706 return false;
2707 }
2708};
2709
2710/// Match an argument.
2711template <unsigned OpI, typename Opnd_t>
2712inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2713 return Argument_match<Opnd_t>(OpI, Op);
2714}
2715
2716/// Intrinsic matchers.
2718 unsigned ID;
2719
2721
2722 template <typename OpTy> bool match(OpTy *V) const {
2723 if (const auto *CI = dyn_cast<CallInst>(V))
2724 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2725 return F->getIntrinsicID() == ID;
2726 return false;
2727 }
2728};
2729
2730/// Intrinsic matches are combinations of ID matchers, and argument
2731/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2732/// them with lower arity matchers. Here's some convenient typedefs for up to
2733/// several arguments, and more can be added as needed
2734template <typename T0 = void, typename T1 = void, typename T2 = void,
2735 typename T3 = void, typename T4 = void, typename T5 = void,
2736 typename T6 = void, typename T7 = void, typename T8 = void,
2737 typename T9 = void, typename T10 = void>
2739template <typename T0> struct m_Intrinsic_Ty<T0> {
2741};
2742template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2743 using Ty =
2745};
2746template <typename T0, typename T1, typename T2>
2751template <typename T0, typename T1, typename T2, typename T3>
2756
2757template <typename T0, typename T1, typename T2, typename T3, typename T4>
2762
2763template <typename T0, typename T1, typename T2, typename T3, typename T4,
2764 typename T5>
2769
2770/// Match intrinsic calls like this:
2771/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2772template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2773 return IntrinsicID_match(IntrID);
2774}
2775
2776/// Matches MaskedLoad Intrinsic.
2777template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2779m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2780 const Opnd3 &Op3) {
2781 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
2782}
2783
2784/// Matches MaskedGather Intrinsic.
2785template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2787m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2788 const Opnd3 &Op3) {
2789 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
2790}
2791
2792template <Intrinsic::ID IntrID, typename T0>
2793inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2795}
2796
2797template <Intrinsic::ID IntrID, typename T0, typename T1>
2798inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2799 const T1 &Op1) {
2801}
2802
2803template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2804inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2805m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2806 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2807}
2808
2809template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2810 typename T3>
2812m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2813 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2814}
2815
2816template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2817 typename T3, typename T4>
2819m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2820 const T4 &Op4) {
2821 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2822 m_Argument<4>(Op4));
2823}
2824
2825template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2826 typename T3, typename T4, typename T5>
2828m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2829 const T4 &Op4, const T5 &Op5) {
2830 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2831 m_Argument<5>(Op5));
2832}
2833
2834// Helper intrinsic matching specializations.
2835template <typename Opnd0>
2836inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2838}
2839
2840template <typename Opnd0>
2841inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2843}
2844
2845template <typename Opnd0>
2846inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2847 return m_Intrinsic<Intrinsic::fabs>(Op0);
2848}
2849
2850template <typename Opnd0>
2851inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2853}
2854
2855template <typename Opnd0, typename Opnd1>
2856inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2857 const Opnd1 &Op1) {
2858 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2859}
2860
2861template <typename Opnd0, typename Opnd1>
2862inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2863 const Opnd1 &Op1) {
2864 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2865}
2866
2867template <typename Opnd0, typename Opnd1>
2869m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2870 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2871}
2872
2873template <typename Opnd0, typename Opnd1>
2874inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2875 const Opnd1 &Op1) {
2876 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2877}
2878
2879template <typename Opnd0, typename Opnd1>
2880inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2881 const Opnd1 &Op1) {
2882 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2883}
2884
2885template <typename Opnd0, typename Opnd1>
2887m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2888 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2889}
2890
2891template <typename Opnd0, typename Opnd1, typename Opnd2>
2893m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2894 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2895}
2896
2897template <typename Opnd0, typename Opnd1, typename Opnd2>
2899m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2900 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2901}
2902
2903template <typename Opnd0>
2904inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2905 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2906}
2907
2908template <typename Opnd0, typename Opnd1>
2909inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2910 const Opnd1 &Op1) {
2911 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2912}
2913
2914template <typename Opnd0>
2915inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2917}
2918
2919//===----------------------------------------------------------------------===//
2920// Matchers for two-operands operators with the operators in either order
2921//
2922
2923/// Matches a BinaryOperator with LHS and RHS in either order.
2924template <typename LHS, typename RHS>
2927}
2928
2929/// Matches an ICmp with a predicate over LHS and RHS in either order.
2930/// Swaps the predicate if operands are commuted.
2931template <typename LHS, typename RHS>
2933m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2935}
2936
2937template <typename LHS, typename RHS>
2942
2943/// Matches a specific opcode with LHS and RHS in either order.
2944template <typename LHS, typename RHS>
2946m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2947 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2948}
2949
2950/// Matches a Add with LHS and RHS in either order.
2951template <typename LHS, typename RHS>
2956
2957/// Matches a Mul with LHS and RHS in either order.
2958template <typename LHS, typename RHS>
2963
2964/// Matches an And with LHS and RHS in either order.
2965template <typename LHS, typename RHS>
2970
2971/// Matches an Or with LHS and RHS in either order.
2972template <typename LHS, typename RHS>
2977
2978/// Matches an Xor with LHS and RHS in either order.
2979template <typename LHS, typename RHS>
2984
2985/// Matches a 'Neg' as 'sub 0, V'.
2986template <typename ValTy>
2987inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2988m_Neg(const ValTy &V) {
2989 return m_Sub(m_ZeroInt(), V);
2990}
2991
2992/// Matches a 'Neg' as 'sub nsw 0, V'.
2993template <typename ValTy>
2995 Instruction::Sub,
2997m_NSWNeg(const ValTy &V) {
2998 return m_NSWSub(m_ZeroInt(), V);
2999}
3000
3001/// Matches an SMin with LHS and RHS in either order.
3002template <typename LHS, typename RHS>
3004m_c_SMin(const LHS &L, const RHS &R) {
3006}
3007/// Matches an SMax with LHS and RHS in either order.
3008template <typename LHS, typename RHS>
3010m_c_SMax(const LHS &L, const RHS &R) {
3012}
3013/// Matches a UMin with LHS and RHS in either order.
3014template <typename LHS, typename RHS>
3016m_c_UMin(const LHS &L, const RHS &R) {
3018}
3019/// Matches a UMax with LHS and RHS in either order.
3020template <typename LHS, typename RHS>
3022m_c_UMax(const LHS &L, const RHS &R) {
3024}
3025
3026template <typename LHS, typename RHS>
3027inline match_combine_or<
3032m_c_MaxOrMin(const LHS &L, const RHS &R) {
3033 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3034 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3035}
3036
3037template <Intrinsic::ID IntrID, typename T0, typename T1>
3040m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3041 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
3042 m_Intrinsic<IntrID>(Op1, Op0));
3043}
3044
3045/// Matches FAdd with LHS and RHS in either order.
3046template <typename LHS, typename RHS>
3048m_c_FAdd(const LHS &L, const RHS &R) {
3050}
3051
3052/// Matches FMul with LHS and RHS in either order.
3053template <typename LHS, typename RHS>
3055m_c_FMul(const LHS &L, const RHS &R) {
3057}
3058
3059template <typename Opnd_t> struct Signum_match {
3060 Opnd_t Val;
3061 Signum_match(const Opnd_t &V) : Val(V) {}
3062
3063 template <typename OpTy> bool match(OpTy *V) const {
3064 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3065 if (TypeSize == 0)
3066 return false;
3067
3068 unsigned ShiftWidth = TypeSize - 1;
3069 Value *Op;
3070
3071 // This is the representation of signum we match:
3072 //
3073 // signum(x) == (x >> 63) | (-x >>u 63)
3074 //
3075 // An i1 value is its own signum, so it's correct to match
3076 //
3077 // signum(x) == (x >> 0) | (-x >>u 0)
3078 //
3079 // for i1 values.
3080
3081 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3082 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3083 auto Signum = m_c_Or(LHS, RHS);
3084
3085 return Signum.match(V) && Val.match(Op);
3086 }
3087};
3088
3089/// Matches a signum pattern.
3090///
3091/// signum(x) =
3092/// x > 0 -> 1
3093/// x == 0 -> 0
3094/// x < 0 -> -1
3095template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3096 return Signum_match<Val_t>(V);
3097}
3098
3099template <int Ind, typename Opnd_t> struct ExtractValue_match {
3100 Opnd_t Val;
3101 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3102
3103 template <typename OpTy> bool match(OpTy *V) const {
3104 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3105 // If Ind is -1, don't inspect indices
3106 if (Ind != -1 &&
3107 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3108 return false;
3109 return Val.match(I->getAggregateOperand());
3110 }
3111 return false;
3112 }
3113};
3114
3115/// Match a single index ExtractValue instruction.
3116/// For example m_ExtractValue<1>(...)
3117template <int Ind, typename Val_t>
3121
3122/// Match an ExtractValue instruction with any index.
3123/// For example m_ExtractValue(...)
3124template <typename Val_t>
3125inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3126 return ExtractValue_match<-1, Val_t>(V);
3127}
3128
3129/// Matcher for a single index InsertValue instruction.
3130template <int Ind, typename T0, typename T1> struct InsertValue_match {
3133
3134 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3135
3136 template <typename OpTy> bool match(OpTy *V) const {
3137 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3138 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3139 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3140 }
3141 return false;
3142 }
3143};
3144
3145/// Matches a single index InsertValue instruction.
3146template <int Ind, typename Val_t, typename Elt_t>
3148 const Elt_t &Elt) {
3149 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3150}
3151
3152/// Matches a call to `llvm.vscale()`.
3154
3155template <typename Opnd0, typename Opnd1>
3157m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3159}
3160
3161template <typename Opnd>
3165
3166template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3170
3171 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3172
3173 template <typename T> bool match(T *V) const {
3174 auto *I = dyn_cast<Instruction>(V);
3175 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3176 return false;
3177
3178 if (I->getOpcode() == Opcode) {
3179 auto *Op0 = I->getOperand(0);
3180 auto *Op1 = I->getOperand(1);
3181 return (L.match(Op0) && R.match(Op1)) ||
3182 (Commutable && L.match(Op1) && R.match(Op0));
3183 }
3184
3185 if (auto *Select = dyn_cast<SelectInst>(I)) {
3186 auto *Cond = Select->getCondition();
3187 auto *TVal = Select->getTrueValue();
3188 auto *FVal = Select->getFalseValue();
3189
3190 // Don't match a scalar select of bool vectors.
3191 // Transforms expect a single type for operands if this matches.
3192 if (Cond->getType() != Select->getType())
3193 return false;
3194
3195 if (Opcode == Instruction::And) {
3196 auto *C = dyn_cast<Constant>(FVal);
3197 if (C && C->isNullValue())
3198 return (L.match(Cond) && R.match(TVal)) ||
3199 (Commutable && L.match(TVal) && R.match(Cond));
3200 } else {
3201 assert(Opcode == Instruction::Or);
3202 auto *C = dyn_cast<Constant>(TVal);
3203 if (C && C->isOneValue())
3204 return (L.match(Cond) && R.match(FVal)) ||
3205 (Commutable && L.match(FVal) && R.match(Cond));
3206 }
3207 }
3208
3209 return false;
3210 }
3211};
3212
3213/// Matches L && R either in the form of L & R or L ? R : false.
3214/// Note that the latter form is poison-blocking.
3215template <typename LHS, typename RHS>
3220
3221/// Matches L && R where L and R are arbitrary values.
3222inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3223
3224/// Matches L && R with LHS and RHS in either order.
3225template <typename LHS, typename RHS>
3227m_c_LogicalAnd(const LHS &L, const RHS &R) {
3229}
3230
3231/// Matches L || R either in the form of L | R or L ? true : R.
3232/// Note that the latter form is poison-blocking.
3233template <typename LHS, typename RHS>
3238
3239/// Matches L || R where L and R are arbitrary values.
3240inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3241
3242/// Matches L || R with LHS and RHS in either order.
3243template <typename LHS, typename RHS>
3245m_c_LogicalOr(const LHS &L, const RHS &R) {
3247}
3248
3249/// Matches either L && R or L || R,
3250/// either one being in the either binary or logical form.
3251/// Note that the latter form is poison-blocking.
3252template <typename LHS, typename RHS, bool Commutable = false>
3258
3259/// Matches either L && R or L || R where L and R are arbitrary values.
3260inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3261
3262/// Matches either L && R or L || R with LHS and RHS in either order.
3263template <typename LHS, typename RHS>
3264inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3265 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3266}
3267
3268} // end namespace PatternMatch
3269} // end namespace llvm
3270
3271#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.
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:1727
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:1760
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)