Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
PatternMatch.h
Go to the documentation of this file.
1//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides a simple and efficient mechanism for performing general
10// tree-based pattern matches on the LLVM IR. The power of these routines is
11// that it allows you to write concise patterns that are expressive and easy to
12// understand. The other major advantage of this is that it allows you to
13// trivially capture/bind elements in the pattern to variables. For example,
14// you can do something like this:
15//
16// Value *Exp = ...
17// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
18// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19// m_And(m_Value(Y), m_ConstantInt(C2))))) {
20// ... Pattern is matched and variables are bound ...
21// }
22//
23// This is primarily useful to things like the instruction combiner, but can
24// also be useful for static analysis tools or code generators.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_IR_PATTERNMATCH_H
29#define LLVM_IR_PATTERNMATCH_H
30
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/InstrTypes.h"
37#include "llvm/IR/Instruction.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Operator.h"
42#include "llvm/IR/Value.h"
44#include <cstdint>
45
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return P.match(V);
51}
52
53template <typename Val, typename Pattern> struct MatchFunctor {
54 const Pattern &P;
55 MatchFunctor(const Pattern &P) : P(P) {}
56 bool operator()(Val *V) const { return P.match(V); }
57};
58
59/// A match functor that can be used as a UnaryPredicate in functional
60/// algorithms like all_of.
61template <typename Val = const Value, typename Pattern>
65
66template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
67 return P.match(Mask);
68}
69
70template <typename SubPattern_t> struct OneUse_match {
71 SubPattern_t SubPattern;
72
73 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
74
75 template <typename OpTy> bool match(OpTy *V) const {
76 return V->hasOneUse() && SubPattern.match(V);
77 }
78};
79
80template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
81 return SubPattern;
82}
83
84template <typename SubPattern_t> struct AllowReassoc_match {
85 SubPattern_t SubPattern;
86
87 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}
88
89 template <typename OpTy> bool match(OpTy *V) const {
90 auto *I = dyn_cast<FPMathOperator>(V);
91 return I && I->hasAllowReassoc() && SubPattern.match(I);
92 }
93};
94
95template <typename T>
96inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
97 return SubPattern;
98}
99
100template <typename Class> struct class_match {
101 template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
102};
103
104/// Match an arbitrary value and ignore it.
106
107/// Match an arbitrary unary operation and ignore it.
111
112/// Match an arbitrary binary operation and ignore it.
116
117/// Matches any compare instruction and ignore it.
119
121 static bool check(const Value *V) {
122 if (isa<UndefValue>(V))
123 return true;
124
125 const auto *CA = dyn_cast<ConstantAggregate>(V);
126 if (!CA)
127 return false;
128
131
132 // Either UndefValue, PoisonValue, or an aggregate that only contains
133 // these is accepted by matcher.
134 // CheckValue returns false if CA cannot satisfy this constraint.
135 auto CheckValue = [&](const ConstantAggregate *CA) {
136 for (const Value *Op : CA->operand_values()) {
137 if (isa<UndefValue>(Op))
138 continue;
139
140 const auto *CA = dyn_cast<ConstantAggregate>(Op);
141 if (!CA)
142 return false;
143 if (Seen.insert(CA).second)
144 Worklist.emplace_back(CA);
145 }
146
147 return true;
148 };
149
150 if (!CheckValue(CA))
151 return false;
152
153 while (!Worklist.empty()) {
154 if (!CheckValue(Worklist.pop_back_val()))
155 return false;
156 }
157 return true;
158 }
159 template <typename ITy> bool match(ITy *V) const { return check(V); }
160};
161
162/// Match an arbitrary undef constant. This matches poison as well.
163/// If this is an aggregate and contains a non-aggregate element that is
164/// neither undef nor poison, the aggregate is not matched.
165inline auto m_Undef() { return undef_match(); }
166
167/// Match an arbitrary UndefValue constant.
171
172/// Match an arbitrary poison constant.
176
177/// Match an arbitrary Constant and ignore it.
179
180/// Match an arbitrary ConstantInt and ignore it.
184
185/// Match an arbitrary ConstantFP and ignore it.
189
191 template <typename ITy> bool match(ITy *V) const {
192 auto *C = dyn_cast<Constant>(V);
193 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
194 }
195};
196
197/// Match a constant expression or a constant that contains a constant
198/// expression.
200
201/// Match an arbitrary basic block value and ignore it.
205
206/// Inverting matcher
207template <typename Ty> struct match_unless {
208 Ty M;
209
210 match_unless(const Ty &Matcher) : M(Matcher) {}
211
212 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
213};
214
215/// Match if the inner matcher does *NOT* match.
216template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
217 return match_unless<Ty>(M);
218}
219
220/// Matching combinators
221template <typename LTy, typename RTy> struct match_combine_or {
222 LTy L;
223 RTy R;
224
225 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
226
227 template <typename ITy> bool match(ITy *V) const {
228 if (L.match(V))
229 return true;
230 if (R.match(V))
231 return true;
232 return false;
233 }
234};
235
236template <typename LTy, typename RTy> struct match_combine_and {
237 LTy L;
238 RTy R;
239
240 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
241
242 template <typename ITy> bool match(ITy *V) const {
243 if (L.match(V))
244 if (R.match(V))
245 return true;
246 return false;
247 }
248};
249
250/// Combine two pattern matchers matching L || R
251template <typename LTy, typename RTy>
252inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
253 return match_combine_or<LTy, RTy>(L, R);
254}
255
256/// Combine two pattern matchers matching L && R
257template <typename LTy, typename RTy>
258inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
259 return match_combine_and<LTy, RTy>(L, R);
260}
261
262template <typename APTy> struct ap_match {
263 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
265 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
266
267 const APTy *&Res;
269
270 ap_match(const APTy *&Res, bool AllowPoison)
272
273 template <typename ITy> bool match(ITy *V) const {
274 if (auto *CI = dyn_cast<ConstantTy>(V)) {
275 Res = &CI->getValue();
276 return true;
277 }
278 if (V->getType()->isVectorTy())
279 if (const auto *C = dyn_cast<Constant>(V))
280 if (auto *CI =
281 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
282 Res = &CI->getValue();
283 return true;
284 }
285 return false;
286 }
287};
288
289/// Match a ConstantInt or splatted ConstantVector, binding the
290/// specified pointer to the contained APInt.
291inline ap_match<APInt> m_APInt(const APInt *&Res) {
292 // Forbid poison by default to maintain previous behavior.
293 return ap_match<APInt>(Res, /* AllowPoison */ false);
294}
295
296/// Match APInt while allowing poison in splat vector constants.
298 return ap_match<APInt>(Res, /* AllowPoison */ true);
299}
300
301/// Match APInt while forbidding poison in splat vector constants.
303 return ap_match<APInt>(Res, /* AllowPoison */ false);
304}
305
306/// Match a ConstantFP or splatted ConstantVector, binding the
307/// specified pointer to the contained APFloat.
309 // Forbid undefs by default to maintain previous behavior.
310 return ap_match<APFloat>(Res, /* AllowPoison */ false);
311}
312
313/// Match APFloat while allowing poison in splat vector constants.
315 return ap_match<APFloat>(Res, /* AllowPoison */ true);
316}
317
318/// Match APFloat while forbidding poison in splat vector constants.
320 return ap_match<APFloat>(Res, /* AllowPoison */ false);
321}
322
323template <int64_t Val> struct constantint_match {
324 template <typename ITy> bool match(ITy *V) const {
325 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
326 const APInt &CIV = CI->getValue();
327 if (Val >= 0)
328 return CIV == static_cast<uint64_t>(Val);
329 // If Val is negative, and CI is shorter than it, truncate to the right
330 // number of bits. If it is larger, then we have to sign extend. Just
331 // compare their negated values.
332 return -CIV == -Val;
333 }
334 return false;
335 }
336};
337
338/// Match a ConstantInt with a specific value.
339template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
340 return constantint_match<Val>();
341}
342
343/// This helper class is used to match constant scalars, vector splats,
344/// and fixed width vectors that satisfy a specified predicate.
345/// For fixed width vector constants, poison elements are ignored if AllowPoison
346/// is true.
347template <typename Predicate, typename ConstantVal, bool AllowPoison>
348struct cstval_pred_ty : public Predicate {
349 const Constant **Res = nullptr;
350 template <typename ITy> bool match_impl(ITy *V) const {
351 if (const auto *CV = dyn_cast<ConstantVal>(V))
352 return this->isValue(CV->getValue());
353 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
354 if (const auto *C = dyn_cast<Constant>(V)) {
355 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
356 return this->isValue(CV->getValue());
357
358 // Number of elements of a scalable vector unknown at compile time
359 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
360 if (!FVTy)
361 return false;
362
363 // Non-splat vector constant: check each element for a match.
364 unsigned NumElts = FVTy->getNumElements();
365 assert(NumElts != 0 && "Constant vector with no elements?");
366 bool HasNonPoisonElements = false;
367 for (unsigned i = 0; i != NumElts; ++i) {
368 Constant *Elt = C->getAggregateElement(i);
369 if (!Elt)
370 return false;
371 if (AllowPoison && isa<PoisonValue>(Elt))
372 continue;
373 auto *CV = dyn_cast<ConstantVal>(Elt);
374 if (!CV || !this->isValue(CV->getValue()))
375 return false;
376 HasNonPoisonElements = true;
377 }
378 return HasNonPoisonElements;
379 }
380 }
381 return false;
382 }
383
384 template <typename ITy> bool match(ITy *V) const {
385 if (this->match_impl(V)) {
386 if (Res)
387 *Res = cast<Constant>(V);
388 return true;
389 }
390 return false;
391 }
392};
393
394/// specialization of cstval_pred_ty for ConstantInt
395template <typename Predicate, bool AllowPoison = true>
397
398/// specialization of cstval_pred_ty for ConstantFP
399template <typename Predicate>
401 /*AllowPoison=*/true>;
402
403/// This helper class is used to match scalar and vector constants that
404/// satisfy a specified predicate, and bind them to an APInt.
405template <typename Predicate> struct api_pred_ty : public Predicate {
406 const APInt *&Res;
407
408 api_pred_ty(const APInt *&R) : Res(R) {}
409
410 template <typename ITy> bool match(ITy *V) const {
411 if (const auto *CI = dyn_cast<ConstantInt>(V))
412 if (this->isValue(CI->getValue())) {
413 Res = &CI->getValue();
414 return true;
415 }
416 if (V->getType()->isVectorTy())
417 if (const auto *C = dyn_cast<Constant>(V))
418 if (auto *CI = dyn_cast_or_null<ConstantInt>(
419 C->getSplatValue(/*AllowPoison=*/true)))
420 if (this->isValue(CI->getValue())) {
421 Res = &CI->getValue();
422 return true;
423 }
424
425 return false;
426 }
427};
428
429/// This helper class is used to match scalar and vector constants that
430/// satisfy a specified predicate, and bind them to an APFloat.
431/// Poison is allowed in splat vector constants.
432template <typename Predicate> struct apf_pred_ty : public Predicate {
433 const APFloat *&Res;
434
435 apf_pred_ty(const APFloat *&R) : Res(R) {}
436
437 template <typename ITy> bool match(ITy *V) const {
438 if (const auto *CI = dyn_cast<ConstantFP>(V))
439 if (this->isValue(CI->getValue())) {
440 Res = &CI->getValue();
441 return true;
442 }
443 if (V->getType()->isVectorTy())
444 if (const auto *C = dyn_cast<Constant>(V))
445 if (auto *CI = dyn_cast_or_null<ConstantFP>(
446 C->getSplatValue(/* AllowPoison */ true)))
447 if (this->isValue(CI->getValue())) {
448 Res = &CI->getValue();
449 return true;
450 }
451
452 return false;
453 }
454};
455
456///////////////////////////////////////////////////////////////////////////////
457//
458// Encapsulate constant value queries for use in templated predicate matchers.
459// This allows checking if constants match using compound predicates and works
460// with vector constants, possibly with relaxed constraints. For example, ignore
461// undef values.
462//
463///////////////////////////////////////////////////////////////////////////////
464
465template <typename APTy> struct custom_checkfn {
466 function_ref<bool(const APTy &)> CheckFn;
467 bool isValue(const APTy &C) const { return CheckFn(C); }
468};
469
470/// Match an integer or vector where CheckFn(ele) for each element is true.
471/// For vectors, poison elements are assumed to match.
473m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
474 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
475}
476
478m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
479 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
480}
481
482/// Match a float or vector where CheckFn(ele) for each element is true.
483/// For vectors, poison elements are assumed to match.
485m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
486 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
487}
488
490m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
491 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
492}
493
495 bool isValue(const APInt &C) const { return true; }
496};
497/// Match an integer or vector with any integral constant.
498/// For vectors, this includes constants with undefined elements.
502
504 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
505};
506
510
512 bool isValue(const APInt &C) const { return C.isAllOnes(); }
513};
514/// Match an integer or vector with all bits set.
515/// For vectors, this includes constants with undefined elements.
519
523
525 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
526};
527/// Match an integer or vector with values having all bits except for the high
528/// bit set (0x7f...).
529/// For vectors, this includes constants with undefined elements.
534 return V;
535}
536
538 bool isValue(const APInt &C) const { return C.isNegative(); }
539};
540/// Match an integer or vector of negative values.
541/// For vectors, this includes constants with undefined elements.
545inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
546
548 bool isValue(const APInt &C) const { return C.isNonNegative(); }
549};
550/// Match an integer or vector of non-negative values.
551/// For vectors, this includes constants with undefined elements.
555inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
556
558 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
559};
560/// Match an integer or vector of strictly positive values.
561/// For vectors, this includes constants with undefined elements.
566 return V;
567}
568
570 bool isValue(const APInt &C) const { return C.isNonPositive(); }
571};
572/// Match an integer or vector of non-positive values.
573/// For vectors, this includes constants with undefined elements.
577inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
578
579struct is_one {
580 bool isValue(const APInt &C) const { return C.isOne(); }
581};
582/// Match an integer 1 or a vector with all elements equal to 1.
583/// For vectors, this includes constants with undefined elements.
585
587 bool isValue(const APInt &C) const { return C.isZero(); }
588};
589/// Match an integer 0 or a vector with all elements equal to 0.
590/// For vectors, this includes constants with undefined elements.
594
595struct is_zero {
596 template <typename ITy> bool match(ITy *V) const {
597 auto *C = dyn_cast<Constant>(V);
598 // FIXME: this should be able to do something for scalable vectors
599 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
600 }
601};
602/// Match any null constant or a vector with all elements equal to 0.
603/// For vectors, this includes constants with undefined elements.
604inline is_zero m_Zero() { return is_zero(); }
605
606struct is_power2 {
607 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
608};
609/// Match an integer or vector power-of-2.
610/// For vectors, this includes constants with undefined elements.
612inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
613
615 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
616};
617/// Match a integer or vector negated power-of-2.
618/// For vectors, this includes constants with undefined elements.
623 return V;
624}
625
627 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
628};
629/// Match a integer or vector negated power-of-2.
630/// For vectors, this includes constants with undefined elements.
636 return V;
637}
638
640 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
641};
642/// Match an integer or vector of 0 or power-of-2 values.
643/// For vectors, this includes constants with undefined elements.
648 return V;
649}
650
652 bool isValue(const APInt &C) const { return C.isSignMask(); }
653};
654/// Match an integer or vector with only the sign bit(s) set.
655/// For vectors, this includes constants with undefined elements.
659
661 bool isValue(const APInt &C) const { return C.isMask(); }
662};
663/// Match an integer or vector with only the low bit(s) set.
664/// For vectors, this includes constants with undefined elements.
668inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
669
671 bool isValue(const APInt &C) const { return !C || C.isMask(); }
672};
673/// Match an integer or vector with only the low bit(s) set.
674/// For vectors, this includes constants with undefined elements.
679 return V;
680}
681
684 const APInt *Thr;
685 bool isValue(const APInt &C) const {
686 return ICmpInst::compare(C, *Thr, Pred);
687 }
688};
689/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
690/// to Threshold. For vectors, this includes constants with undefined elements.
694 P.Pred = Predicate;
695 P.Thr = &Threshold;
696 return P;
697}
698
699struct is_nan {
700 bool isValue(const APFloat &C) const { return C.isNaN(); }
701};
702/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
703/// For vectors, this includes constants with undefined elements.
705
706struct is_nonnan {
707 bool isValue(const APFloat &C) const { return !C.isNaN(); }
708};
709/// Match a non-NaN FP constant.
710/// For vectors, this includes constants with undefined elements.
714
715struct is_inf {
716 bool isValue(const APFloat &C) const { return C.isInfinity(); }
717};
718/// Match a positive or negative infinity FP constant.
719/// For vectors, this includes constants with undefined elements.
721
722struct is_noninf {
723 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
724};
725/// Match a non-infinity FP constant, i.e. finite or NaN.
726/// For vectors, this includes constants with undefined elements.
730
731struct is_finite {
732 bool isValue(const APFloat &C) const { return C.isFinite(); }
733};
734/// Match a finite FP constant, i.e. not infinity or NaN.
735/// For vectors, this includes constants with undefined elements.
739inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
740
742 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
743};
744/// Match a finite non-zero FP constant.
745/// For vectors, this includes constants with undefined elements.
750 return V;
751}
752
754 bool isValue(const APFloat &C) const { return C.isZero(); }
755};
756/// Match a floating-point negative zero or positive zero.
757/// For vectors, this includes constants with undefined elements.
761
763 bool isValue(const APFloat &C) const { return C.isPosZero(); }
764};
765/// Match a floating-point positive zero.
766/// For vectors, this includes constants with undefined elements.
770
772 bool isValue(const APFloat &C) const { return C.isNegZero(); }
773};
774/// Match a floating-point negative zero.
775/// For vectors, this includes constants with undefined elements.
779
781 bool isValue(const APFloat &C) const { return C.isNonZero(); }
782};
783/// Match a floating-point non-zero.
784/// For vectors, this includes constants with undefined elements.
788
790 bool isValue(const APFloat &C) const {
791 return !C.isDenormal() && C.isNonZero();
792 }
793};
794
795/// Match a floating-point non-zero that is not a denormal.
796/// For vectors, this includes constants with undefined elements.
800
801///////////////////////////////////////////////////////////////////////////////
802
803template <typename Class> struct bind_ty {
804 Class *&VR;
805
806 bind_ty(Class *&V) : VR(V) {}
807
808 template <typename ITy> bool match(ITy *V) const {
809 if (auto *CV = dyn_cast<Class>(V)) {
810 VR = CV;
811 return true;
812 }
813 return false;
814 }
815};
816
817/// Check whether the value has the given Class and matches the nested
818/// pattern. Capture it into the provided variable if successful.
819template <typename Class, typename MatchTy> struct bind_and_match_ty {
820 Class *&VR;
821 MatchTy Match;
822
823 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
824
825 template <typename ITy> bool match(ITy *V) const {
826 auto *CV = dyn_cast<Class>(V);
827 if (CV && Match.match(V)) {
828 VR = CV;
829 return true;
830 }
831 return false;
832 }
833};
834
835/// Match a value, capturing it if we match.
836inline bind_ty<Value> m_Value(Value *&V) { return V; }
837inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
838
839/// Match against the nested pattern, and capture the value if we match.
840template <typename MatchTy>
842 const MatchTy &Match) {
843 return {V, Match};
844}
845
846/// Match against the nested pattern, and capture the value if we match.
847template <typename MatchTy>
849 const MatchTy &Match) {
850 return {V, Match};
851}
852
853/// Match an instruction, capturing it if we match.
855
856/// Match against the nested pattern, and capture the instruction if we match.
857template <typename MatchTy>
859m_Instruction(Instruction *&I, const MatchTy &Match) {
860 return {I, Match};
861}
862
863/// Match a unary operator, capturing it if we match.
865/// Match a binary operator, capturing it if we match.
867/// Match a with overflow intrinsic, capturing it if we match.
873 return I;
874}
875
876/// Match an UndefValue, capturing the value if we match.
878
879/// Match a Constant, capturing the value if we match.
881
882/// Match a ConstantInt, capturing the value if we match.
884
885/// Match a ConstantFP, capturing the value if we match.
887
888/// Match a ConstantExpr, capturing the value if we match.
890
891/// Match a basic block value, capturing it if we match.
894 return V;
895}
896
897// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
898// and use m_Unless(m_ConstantExpr).
900 template <typename ITy> static bool isImmConstant(ITy *V) {
901 if (auto *CV = dyn_cast<Constant>(V)) {
902 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
903 return true;
904
905 if (CV->getType()->isVectorTy()) {
906 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
907 if (!isa<ConstantExpr>(Splat) &&
908 !Splat->containsConstantExpression()) {
909 return true;
910 }
911 }
912 }
913 }
914 return false;
915 }
916};
917
919 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
920};
921
922/// Match an arbitrary immediate Constant and ignore it.
924
927
929
930 template <typename ITy> bool match(ITy *V) const {
931 if (isImmConstant(V)) {
932 VR = cast<Constant>(V);
933 return true;
934 }
935 return false;
936 }
937};
938
939/// Match an immediate Constant, capturing the value if we match.
943
944/// Match a specified Value*.
946 const Value *Val;
947
948 specificval_ty(const Value *V) : Val(V) {}
949
950 template <typename ITy> bool match(ITy *V) const { return V == Val; }
951};
952
953/// Match if we have a specific specified value.
954inline specificval_ty m_Specific(const Value *V) { return V; }
955
956/// Stores a reference to the Value *, not the Value * itself,
957/// thus can be used in commutative matchers.
958template <typename Class> struct deferredval_ty {
959 Class *const &Val;
960
961 deferredval_ty(Class *const &V) : Val(V) {}
962
963 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
964};
965
966/// Like m_Specific(), but works if the specific value to match is determined
967/// as part of the same match() expression. For example:
968/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
969/// bind X before the pattern match starts.
970/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
971/// whichever value m_Value(X) populated.
972inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
974 return V;
975}
976
977/// Match a specified floating point value or vector of all elements of
978/// that value.
980 double Val;
981
982 specific_fpval(double V) : Val(V) {}
983
984 template <typename ITy> bool match(ITy *V) const {
985 if (const auto *CFP = dyn_cast<ConstantFP>(V))
986 return CFP->isExactlyValue(Val);
987 if (V->getType()->isVectorTy())
988 if (const auto *C = dyn_cast<Constant>(V))
989 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
990 return CFP->isExactlyValue(Val);
991 return false;
992 }
993};
994
995/// Match a specific floating point value or vector with all elements
996/// equal to the value.
997inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
998
999/// Match a float 1.0 or vector with all elements equal to 1.0.
1000inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1001
1004
1006
1007 template <typename ITy> bool match(ITy *V) const {
1008 const APInt *ConstInt;
1009 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1010 return false;
1011 if (ConstInt->getActiveBits() > 64)
1012 return false;
1013 VR = ConstInt->getZExtValue();
1014 return true;
1015 }
1016};
1017
1018/// Match a specified integer value or vector of all elements of that
1019/// value.
1020template <bool AllowPoison> struct specific_intval {
1021 const APInt &Val;
1022
1023 specific_intval(const APInt &V) : Val(V) {}
1024
1025 template <typename ITy> bool match(ITy *V) const {
1026 const auto *CI = dyn_cast<ConstantInt>(V);
1027 if (!CI && V->getType()->isVectorTy())
1028 if (const auto *C = dyn_cast<Constant>(V))
1029 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1030
1031 return CI && APInt::isSameValue(CI->getValue(), Val);
1032 }
1033};
1034
1035template <bool AllowPoison> struct specific_intval64 {
1037
1039
1040 template <typename ITy> bool match(ITy *V) const {
1041 const auto *CI = dyn_cast<ConstantInt>(V);
1042 if (!CI && V->getType()->isVectorTy())
1043 if (const auto *C = dyn_cast<Constant>(V))
1044 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1045
1046 return CI && CI->getValue() == Val;
1047 }
1048};
1049
1050/// Match a specific integer value or vector with all elements equal to
1051/// the value.
1053 return specific_intval<false>(V);
1054}
1055
1059
1063
1067
1068/// Match a ConstantInt and bind to its value. This does not match
1069/// ConstantInts wider than 64-bits.
1071
1072/// Match a specified basic block value.
1075
1077
1078 template <typename ITy> bool match(ITy *V) const {
1079 const auto *BB = dyn_cast<BasicBlock>(V);
1080 return BB && BB == Val;
1081 }
1082};
1083
1084/// Match a specific basic block value.
1086 return specific_bbval(BB);
1087}
1088
1089/// A commutative-friendly version of m_Specific().
1091 return BB;
1092}
1094m_Deferred(const BasicBlock *const &BB) {
1095 return BB;
1096}
1097
1098//===----------------------------------------------------------------------===//
1099// Matcher for any binary operator.
1100//
1101template <typename LHS_t, typename RHS_t, bool Commutable = false>
1105
1106 // The evaluation order is always stable, regardless of Commutability.
1107 // The LHS is always matched first.
1108 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1109
1110 template <typename OpTy> bool match(OpTy *V) const {
1111 if (auto *I = dyn_cast<BinaryOperator>(V))
1112 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1113 (Commutable && L.match(I->getOperand(1)) &&
1114 R.match(I->getOperand(0)));
1115 return false;
1116 }
1117};
1118
1119template <typename LHS, typename RHS>
1120inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1121 return AnyBinaryOp_match<LHS, RHS>(L, R);
1122}
1123
1124//===----------------------------------------------------------------------===//
1125// Matcher for any unary operator.
1126// TODO fuse unary, binary matcher into n-ary matcher
1127//
1128template <typename OP_t> struct AnyUnaryOp_match {
1129 OP_t X;
1130
1131 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1132
1133 template <typename OpTy> bool match(OpTy *V) const {
1134 if (auto *I = dyn_cast<UnaryOperator>(V))
1135 return X.match(I->getOperand(0));
1136 return false;
1137 }
1138};
1139
1140template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1141 return AnyUnaryOp_match<OP_t>(X);
1142}
1143
1144//===----------------------------------------------------------------------===//
1145// Matchers for specific binary operators.
1146//
1147
1148template <typename LHS_t, typename RHS_t, unsigned Opcode,
1149 bool Commutable = false>
1153
1154 // The evaluation order is always stable, regardless of Commutability.
1155 // The LHS is always matched first.
1156 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1157
1158 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1159 if (V->getValueID() == Value::InstructionVal + Opc) {
1160 auto *I = cast<BinaryOperator>(V);
1161 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1162 (Commutable && L.match(I->getOperand(1)) &&
1163 R.match(I->getOperand(0)));
1164 }
1165 return false;
1166 }
1167
1168 template <typename OpTy> bool match(OpTy *V) const {
1169 return match(Opcode, V);
1170 }
1171};
1172
1173template <typename LHS, typename RHS>
1178
1179template <typename LHS, typename RHS>
1184
1185template <typename LHS, typename RHS>
1190
1191template <typename LHS, typename RHS>
1196
1197template <typename Op_t> struct FNeg_match {
1198 Op_t X;
1199
1200 FNeg_match(const Op_t &Op) : X(Op) {}
1201 template <typename OpTy> bool match(OpTy *V) const {
1202 auto *FPMO = dyn_cast<FPMathOperator>(V);
1203 if (!FPMO)
1204 return false;
1205
1206 if (FPMO->getOpcode() == Instruction::FNeg)
1207 return X.match(FPMO->getOperand(0));
1208
1209 if (FPMO->getOpcode() == Instruction::FSub) {
1210 if (FPMO->hasNoSignedZeros()) {
1211 // With 'nsz', any zero goes.
1212 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1213 return false;
1214 } else {
1215 // Without 'nsz', we need fsub -0.0, X exactly.
1216 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1217 return false;
1218 }
1219
1220 return X.match(FPMO->getOperand(1));
1221 }
1222
1223 return false;
1224 }
1225};
1226
1227/// Match 'fneg X' as 'fsub -0.0, X'.
1228template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1229 return FNeg_match<OpTy>(X);
1230}
1231
1232/// Match 'fneg X' as 'fsub +-0.0, X'.
1233template <typename RHS>
1234inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1235m_FNegNSZ(const RHS &X) {
1236 return m_FSub(m_AnyZeroFP(), X);
1237}
1238
1239template <typename LHS, typename RHS>
1244
1245template <typename LHS, typename RHS>
1250
1251template <typename LHS, typename RHS>
1256
1257template <typename LHS, typename RHS>
1262
1263template <typename LHS, typename RHS>
1268
1269template <typename LHS, typename RHS>
1274
1275template <typename LHS, typename RHS>
1280
1281template <typename LHS, typename RHS>
1286
1287template <typename LHS, typename RHS>
1292
1293template <typename LHS, typename RHS>
1298
1299template <typename LHS, typename RHS>
1304
1305template <typename LHS, typename RHS>
1310
1311template <typename LHS, typename RHS>
1316
1317template <typename LHS, typename RHS>
1322
1323template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1326
1328
1329 template <typename OpTy> bool match(OpTy *V) const {
1330 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1331 if (Op->getOpcode() == Opcode)
1332 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1333 L.match(Op->getOperand(0));
1334 }
1335 // Interpreted as shiftop V, 0
1336 R = 0;
1337 return L.match(V);
1338 }
1339};
1340
1341/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1342template <typename LHS>
1347
1348/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1349template <typename LHS>
1354
1355/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1356template <typename LHS>
1361
1362template <typename LHS_t, typename RHS_t, unsigned Opcode,
1363 unsigned WrapFlags = 0, bool Commutable = false>
1367
1369 : L(LHS), R(RHS) {}
1370
1371 template <typename OpTy> bool match(OpTy *V) const {
1372 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1373 if (Op->getOpcode() != Opcode)
1374 return false;
1376 !Op->hasNoUnsignedWrap())
1377 return false;
1378 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1379 !Op->hasNoSignedWrap())
1380 return false;
1381 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1382 (Commutable && L.match(Op->getOperand(1)) &&
1383 R.match(Op->getOperand(0)));
1384 }
1385 return false;
1386 }
1387};
1388
1389template <typename LHS, typename RHS>
1390inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1392m_NSWAdd(const LHS &L, const RHS &R) {
1393 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1395 R);
1396}
1397template <typename LHS, typename RHS>
1398inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1400m_c_NSWAdd(const LHS &L, const RHS &R) {
1401 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1403 true>(L, R);
1404}
1405template <typename LHS, typename RHS>
1406inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1408m_NSWSub(const LHS &L, const RHS &R) {
1409 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1411 R);
1412}
1413template <typename LHS, typename RHS>
1414inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1416m_NSWMul(const LHS &L, const RHS &R) {
1417 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1419 R);
1420}
1421template <typename LHS, typename RHS>
1422inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1424m_NSWShl(const LHS &L, const RHS &R) {
1425 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1427 R);
1428}
1429
1430template <typename LHS, typename RHS>
1431inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1433m_NUWAdd(const LHS &L, const RHS &R) {
1434 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1436 L, R);
1437}
1438
1439template <typename LHS, typename RHS>
1441 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1442m_c_NUWAdd(const LHS &L, const RHS &R) {
1443 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1445 true>(L, R);
1446}
1447
1448template <typename LHS, typename RHS>
1449inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1451m_NUWSub(const LHS &L, const RHS &R) {
1452 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1454 L, R);
1455}
1456template <typename LHS, typename RHS>
1457inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1459m_NUWMul(const LHS &L, const RHS &R) {
1460 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1462 L, R);
1463}
1464template <typename LHS, typename RHS>
1465inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1467m_NUWShl(const LHS &L, const RHS &R) {
1468 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1470 L, R);
1471}
1472
1473template <typename LHS_t, typename RHS_t, bool Commutable = false>
1475 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1476 unsigned Opcode;
1477
1479 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1480
1481 template <typename OpTy> bool match(OpTy *V) const {
1483 }
1484};
1485
1486/// Matches a specific opcode.
1487template <typename LHS, typename RHS>
1488inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1489 const RHS &R) {
1490 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1491}
1492
1493template <typename LHS, typename RHS, bool Commutable = false>
1497
1498 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1499
1500 template <typename OpTy> bool match(OpTy *V) const {
1501 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1502 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1503 if (!PDI->isDisjoint())
1504 return false;
1505 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1506 (Commutable && L.match(PDI->getOperand(1)) &&
1507 R.match(PDI->getOperand(0)));
1508 }
1509 return false;
1510 }
1511};
1512
1513template <typename LHS, typename RHS>
1515 return DisjointOr_match<LHS, RHS>(L, R);
1516}
1517
1518template <typename LHS, typename RHS>
1520 const RHS &R) {
1522}
1523
1524/// Match either "add" or "or disjoint".
1525template <typename LHS, typename RHS>
1528m_AddLike(const LHS &L, const RHS &R) {
1529 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1530}
1531
1532/// Match either "add nsw" or "or disjoint"
1533template <typename LHS, typename RHS>
1534inline match_combine_or<
1535 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1538m_NSWAddLike(const LHS &L, const RHS &R) {
1539 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1540}
1541
1542/// Match either "add nuw" or "or disjoint"
1543template <typename LHS, typename RHS>
1544inline match_combine_or<
1545 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1548m_NUWAddLike(const LHS &L, const RHS &R) {
1549 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1550}
1551
1552template <typename LHS, typename RHS>
1556
1557 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1558
1559 template <typename OpTy> bool match(OpTy *V) const {
1560 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1561 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1562 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1563 ; // Pass
1564 else if (Op->getOpcode() != Instruction::Xor)
1565 return false;
1566 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1567 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1568 }
1569 return false;
1570 }
1571};
1572
1573/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1574/// Only commutative matcher as the `sub` will need to swap the L and R.
1575template <typename LHS, typename RHS>
1576inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1577 return XorLike_match<LHS, RHS>(L, R);
1578}
1579
1580//===----------------------------------------------------------------------===//
1581// Class that matches a group of binary opcodes.
1582//
1583template <typename LHS_t, typename RHS_t, typename Predicate,
1584 bool Commutable = false>
1585struct BinOpPred_match : Predicate {
1588
1589 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1590
1591 template <typename OpTy> bool match(OpTy *V) const {
1592 if (auto *I = dyn_cast<Instruction>(V))
1593 return this->isOpType(I->getOpcode()) &&
1594 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1595 (Commutable && L.match(I->getOperand(1)) &&
1596 R.match(I->getOperand(0))));
1597 return false;
1598 }
1599};
1600
1602 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1603};
1604
1606 bool isOpType(unsigned Opcode) const {
1607 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1608 }
1609};
1610
1612 bool isOpType(unsigned Opcode) const {
1613 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1614 }
1615};
1616
1618 bool isOpType(unsigned Opcode) const {
1619 return Instruction::isBitwiseLogicOp(Opcode);
1620 }
1621};
1622
1624 bool isOpType(unsigned Opcode) const {
1625 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1626 }
1627};
1628
1630 bool isOpType(unsigned Opcode) const {
1631 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1632 }
1633};
1634
1635/// Matches shift operations.
1636template <typename LHS, typename RHS>
1638 const RHS &R) {
1640}
1641
1642/// Matches logical shift operations.
1643template <typename LHS, typename RHS>
1648
1649/// Matches logical shift operations.
1650template <typename LHS, typename RHS>
1652m_LogicalShift(const LHS &L, const RHS &R) {
1654}
1655
1656/// Matches bitwise logic operations.
1657template <typename LHS, typename RHS>
1659m_BitwiseLogic(const LHS &L, const RHS &R) {
1661}
1662
1663/// Matches bitwise logic operations in either order.
1664template <typename LHS, typename RHS>
1669
1670/// Matches integer division operations.
1671template <typename LHS, typename RHS>
1673 const RHS &R) {
1675}
1676
1677/// Matches integer remainder operations.
1678template <typename LHS, typename RHS>
1680 const RHS &R) {
1682}
1683
1684//===----------------------------------------------------------------------===//
1685// Class that matches exact binary ops.
1686//
1687template <typename SubPattern_t> struct Exact_match {
1688 SubPattern_t SubPattern;
1689
1690 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1691
1692 template <typename OpTy> bool match(OpTy *V) const {
1693 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1694 return PEO->isExact() && SubPattern.match(V);
1695 return false;
1696 }
1697};
1698
1699template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1700 return SubPattern;
1701}
1702
1703//===----------------------------------------------------------------------===//
1704// Matchers for CmpInst classes
1705//
1706
1707template <typename LHS_t, typename RHS_t, typename Class,
1708 bool Commutable = false>
1713
1714 // The evaluation order is always stable, regardless of Commutability.
1715 // The LHS is always matched first.
1717 : Predicate(&Pred), L(LHS), R(RHS) {}
1719 : Predicate(nullptr), L(LHS), R(RHS) {}
1720
1721 template <typename OpTy> bool match(OpTy *V) const {
1722 if (auto *I = dyn_cast<Class>(V)) {
1723 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1724 if (Predicate)
1726 return true;
1727 }
1728 if (Commutable && L.match(I->getOperand(1)) &&
1729 R.match(I->getOperand(0))) {
1730 if (Predicate)
1732 return true;
1733 }
1734 }
1735 return false;
1736 }
1737};
1738
1739template <typename LHS, typename RHS>
1741 const RHS &R) {
1742 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1743}
1744
1745template <typename LHS, typename RHS>
1747 const LHS &L, const RHS &R) {
1748 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1749}
1750
1751template <typename LHS, typename RHS>
1753 const LHS &L, const RHS &R) {
1754 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1755}
1756
1757template <typename LHS, typename RHS>
1760}
1761
1762template <typename LHS, typename RHS>
1765}
1766
1767template <typename LHS, typename RHS>
1770}
1771
1772// Same as CmpClass, but instead of saving Pred as out output variable, match a
1773// specific input pred for equality.
1774template <typename LHS_t, typename RHS_t, typename Class,
1775 bool Commutable = false>
1780
1782 : Predicate(Pred), L(LHS), R(RHS) {}
1783
1784 template <typename OpTy> bool match(OpTy *V) const {
1785 if (auto *I = dyn_cast<Class>(V)) {
1787 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1788 return true;
1789 if constexpr (Commutable) {
1792 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1793 return true;
1794 }
1795 }
1796
1797 return false;
1798 }
1799};
1800
1801template <typename LHS, typename RHS>
1803m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1804 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1805}
1806
1807template <typename LHS, typename RHS>
1809m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1810 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1811}
1812
1813template <typename LHS, typename RHS>
1815m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1817}
1818
1819template <typename LHS, typename RHS>
1821m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1822 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1823}
1824
1825//===----------------------------------------------------------------------===//
1826// Matchers for instructions with a given opcode and number of operands.
1827//
1828
1829/// Matches instructions with Opcode and three operands.
1830template <typename T0, unsigned Opcode> struct OneOps_match {
1832
1833 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1834
1835 template <typename OpTy> bool match(OpTy *V) const {
1836 if (V->getValueID() == Value::InstructionVal + Opcode) {
1837 auto *I = cast<Instruction>(V);
1838 return Op1.match(I->getOperand(0));
1839 }
1840 return false;
1841 }
1842};
1843
1844/// Matches instructions with Opcode and three operands.
1845template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1848
1849 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1850
1851 template <typename OpTy> bool match(OpTy *V) const {
1852 if (V->getValueID() == Value::InstructionVal + Opcode) {
1853 auto *I = cast<Instruction>(V);
1854 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1855 }
1856 return false;
1857 }
1858};
1859
1860/// Matches instructions with Opcode and three operands.
1861template <typename T0, typename T1, typename T2, unsigned Opcode,
1862 bool CommutableOp2Op3 = false>
1867
1868 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1869 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1870
1871 template <typename OpTy> bool match(OpTy *V) const {
1872 if (V->getValueID() == Value::InstructionVal + Opcode) {
1873 auto *I = cast<Instruction>(V);
1874 if (!Op1.match(I->getOperand(0)))
1875 return false;
1876 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1877 return true;
1878 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1879 Op3.match(I->getOperand(1));
1880 }
1881 return false;
1882 }
1883};
1884
1885/// Matches instructions with Opcode and any number of operands
1886template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1887 std::tuple<OperandTypes...> Operands;
1888
1889 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1890
1891 // Operand matching works by recursively calling match_operands, matching the
1892 // operands left to right. The first version is called for each operand but
1893 // the last, for which the second version is called. The second version of
1894 // match_operands is also used to match each individual operand.
1895 template <int Idx, int Last>
1896 std::enable_if_t<Idx != Last, bool>
1900
1901 template <int Idx, int Last>
1902 std::enable_if_t<Idx == Last, bool>
1904 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1905 }
1906
1907 template <typename OpTy> bool match(OpTy *V) const {
1908 if (V->getValueID() == Value::InstructionVal + Opcode) {
1909 auto *I = cast<Instruction>(V);
1910 return I->getNumOperands() == sizeof...(OperandTypes) &&
1911 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1912 }
1913 return false;
1914 }
1915};
1916
1917/// Matches SelectInst.
1918template <typename Cond, typename LHS, typename RHS>
1920m_Select(const Cond &C, const LHS &L, const RHS &R) {
1922}
1923
1924/// This matches a select of two constants, e.g.:
1925/// m_SelectCst<-1, 0>(m_Value(V))
1926template <int64_t L, int64_t R, typename Cond>
1928 Instruction::Select>
1931}
1932
1933/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1934template <typename LHS, typename RHS>
1935inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1936m_c_Select(const LHS &L, const RHS &R) {
1937 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1938 true>(m_Value(), L, R);
1939}
1940
1941/// Matches FreezeInst.
1942template <typename OpTy>
1946
1947/// Matches InsertElementInst.
1948template <typename Val_t, typename Elt_t, typename Idx_t>
1950m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1952 Val, Elt, Idx);
1953}
1954
1955/// Matches ExtractElementInst.
1956template <typename Val_t, typename Idx_t>
1958m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1960}
1961
1962/// Matches shuffle.
1963template <typename T0, typename T1, typename T2> struct Shuffle_match {
1967
1968 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1969 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1970
1971 template <typename OpTy> bool match(OpTy *V) const {
1972 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1973 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1974 Mask.match(I->getShuffleMask());
1975 }
1976 return false;
1977 }
1978};
1979
1980struct m_Mask {
1983 bool match(ArrayRef<int> Mask) const {
1984 MaskRef = Mask;
1985 return true;
1986 }
1987};
1988
1990 bool match(ArrayRef<int> Mask) const {
1991 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1992 }
1993};
1994
1998 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
1999};
2000
2004 bool match(ArrayRef<int> Mask) const {
2005 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2006 if (First == Mask.end())
2007 return false;
2008 SplatIndex = *First;
2009 return all_of(Mask,
2010 [First](int Elem) { return Elem == *First || Elem == -1; });
2011 }
2012};
2013
2014template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2015 PointerOpTy PointerOp;
2016 OffsetOpTy OffsetOp;
2017
2018 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2020
2021 template <typename OpTy> bool match(OpTy *V) const {
2022 auto *GEP = dyn_cast<GEPOperator>(V);
2023 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2024 PointerOp.match(GEP->getPointerOperand()) &&
2025 OffsetOp.match(GEP->idx_begin()->get());
2026 }
2027};
2028
2029/// Matches ShuffleVectorInst independently of mask value.
2030template <typename V1_t, typename V2_t>
2032m_Shuffle(const V1_t &v1, const V2_t &v2) {
2034}
2035
2036template <typename V1_t, typename V2_t, typename Mask_t>
2038m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2039 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
2040}
2041
2042/// Matches LoadInst.
2043template <typename OpTy>
2047
2048/// Matches StoreInst.
2049template <typename ValueOpTy, typename PointerOpTy>
2051m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2053 PointerOp);
2054}
2055
2056/// Matches GetElementPtrInst.
2057template <typename... OperandTypes>
2058inline auto m_GEP(const OperandTypes &...Ops) {
2059 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2060}
2061
2062/// Matches GEP with i8 source element type
2063template <typename PointerOpTy, typename OffsetOpTy>
2065m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2067}
2068
2069//===----------------------------------------------------------------------===//
2070// Matchers for CastInst classes
2071//
2072
2073template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2074 Op_t Op;
2075
2076 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2077
2078 template <typename OpTy> bool match(OpTy *V) const {
2079 if (auto *O = dyn_cast<Operator>(V))
2080 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2081 return false;
2082 }
2083};
2084
2085template <typename Op_t, typename Class> struct CastInst_match {
2086 Op_t Op;
2087
2088 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2089
2090 template <typename OpTy> bool match(OpTy *V) const {
2091 if (auto *I = dyn_cast<Class>(V))
2092 return Op.match(I->getOperand(0));
2093 return false;
2094 }
2095};
2096
2097template <typename Op_t> struct PtrToIntSameSize_match {
2099 Op_t Op;
2100
2101 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2102 : DL(DL), Op(OpMatch) {}
2103
2104 template <typename OpTy> bool match(OpTy *V) const {
2105 if (auto *O = dyn_cast<Operator>(V))
2106 return O->getOpcode() == Instruction::PtrToInt &&
2107 DL.getTypeSizeInBits(O->getType()) ==
2108 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2109 Op.match(O->getOperand(0));
2110 return false;
2111 }
2112};
2113
2114template <typename Op_t> struct NNegZExt_match {
2115 Op_t Op;
2116
2117 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2118
2119 template <typename OpTy> bool match(OpTy *V) const {
2120 if (auto *I = dyn_cast<ZExtInst>(V))
2121 return I->hasNonNeg() && Op.match(I->getOperand(0));
2122 return false;
2123 }
2124};
2125
2126template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2127 Op_t Op;
2128
2129 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2130
2131 template <typename OpTy> bool match(OpTy *V) const {
2132 if (auto *I = dyn_cast<TruncInst>(V))
2133 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2134 Op.match(I->getOperand(0));
2135 return false;
2136 }
2137};
2138
2139/// Matches BitCast.
2140template <typename OpTy>
2145
2146template <typename Op_t> struct ElementWiseBitCast_match {
2147 Op_t Op;
2148
2149 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2150
2151 template <typename OpTy> bool match(OpTy *V) const {
2152 auto *I = dyn_cast<BitCastInst>(V);
2153 if (!I)
2154 return false;
2155 Type *SrcType = I->getSrcTy();
2156 Type *DstType = I->getType();
2157 // Make sure the bitcast doesn't change between scalar and vector and
2158 // doesn't change the number of vector elements.
2159 if (SrcType->isVectorTy() != DstType->isVectorTy())
2160 return false;
2161 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2162 SrcVecTy && SrcVecTy->getElementCount() !=
2163 cast<VectorType>(DstType)->getElementCount())
2164 return false;
2165 return Op.match(I->getOperand(0));
2166 }
2167};
2168
2169template <typename OpTy>
2173
2174/// Matches PtrToInt.
2175template <typename OpTy>
2180
2181template <typename OpTy>
2186
2187/// Matches IntToPtr.
2188template <typename OpTy>
2193
2194/// Matches any cast or self. Used to ignore casts.
2195template <typename OpTy>
2197m_CastOrSelf(const OpTy &Op) {
2199}
2200
2201/// Matches Trunc.
2202template <typename OpTy>
2206
2207/// Matches trunc nuw.
2208template <typename OpTy>
2213
2214/// Matches trunc nsw.
2215template <typename OpTy>
2220
2221template <typename OpTy>
2223m_TruncOrSelf(const OpTy &Op) {
2224 return m_CombineOr(m_Trunc(Op), Op);
2225}
2226
2227/// Matches SExt.
2228template <typename OpTy>
2232
2233/// Matches ZExt.
2234template <typename OpTy>
2238
2239template <typename OpTy>
2241 return NNegZExt_match<OpTy>(Op);
2242}
2243
2244template <typename OpTy>
2246m_ZExtOrSelf(const OpTy &Op) {
2247 return m_CombineOr(m_ZExt(Op), Op);
2248}
2249
2250template <typename OpTy>
2252m_SExtOrSelf(const OpTy &Op) {
2253 return m_CombineOr(m_SExt(Op), Op);
2254}
2255
2256/// Match either "sext" or "zext nneg".
2257template <typename OpTy>
2259m_SExtLike(const OpTy &Op) {
2260 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2261}
2262
2263template <typename OpTy>
2266m_ZExtOrSExt(const OpTy &Op) {
2267 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2268}
2269
2270template <typename OpTy>
2273 OpTy>
2275 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2276}
2277
2278template <typename OpTy>
2281 OpTy>
2284}
2285
2286template <typename OpTy>
2290
2291template <typename OpTy>
2295
2296template <typename OpTy>
2300
2301template <typename OpTy>
2305
2306template <typename OpTy>
2310
2311template <typename OpTy>
2315
2316//===----------------------------------------------------------------------===//
2317// Matchers for control flow.
2318//
2319
2320struct br_match {
2322
2324
2325 template <typename OpTy> bool match(OpTy *V) const {
2326 if (auto *BI = dyn_cast<BranchInst>(V))
2327 if (BI->isUnconditional()) {
2328 Succ = BI->getSuccessor(0);
2329 return true;
2330 }
2331 return false;
2332 }
2333};
2334
2335inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2336
2337template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2339 Cond_t Cond;
2340 TrueBlock_t T;
2341 FalseBlock_t F;
2342
2343 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2344 : Cond(C), T(t), F(f) {}
2345
2346 template <typename OpTy> bool match(OpTy *V) const {
2347 if (auto *BI = dyn_cast<BranchInst>(V))
2348 if (BI->isConditional() && Cond.match(BI->getCondition()))
2349 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2350 return false;
2351 }
2352};
2353
2354template <typename Cond_t>
2360
2361template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2363m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2365}
2366
2367//===----------------------------------------------------------------------===//
2368// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2369//
2370
2371template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2372 bool Commutable = false>
2374 using PredType = Pred_t;
2377
2378 // The evaluation order is always stable, regardless of Commutability.
2379 // The LHS is always matched first.
2380 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2381
2382 template <typename OpTy> bool match(OpTy *V) const {
2383 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2384 Intrinsic::ID IID = II->getIntrinsicID();
2385 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2386 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2387 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2388 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2389 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2390 return (L.match(LHS) && R.match(RHS)) ||
2391 (Commutable && L.match(RHS) && R.match(LHS));
2392 }
2393 }
2394 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2395 auto *SI = dyn_cast<SelectInst>(V);
2396 if (!SI)
2397 return false;
2398 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2399 if (!Cmp)
2400 return false;
2401 // At this point we have a select conditioned on a comparison. Check that
2402 // it is the values returned by the select that are being compared.
2403 auto *TrueVal = SI->getTrueValue();
2404 auto *FalseVal = SI->getFalseValue();
2405 auto *LHS = Cmp->getOperand(0);
2406 auto *RHS = Cmp->getOperand(1);
2407 if ((TrueVal != LHS || FalseVal != RHS) &&
2408 (TrueVal != RHS || FalseVal != LHS))
2409 return false;
2410 typename CmpInst_t::Predicate Pred =
2411 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2412 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2413 if (!Pred_t::match(Pred))
2414 return false;
2415 // It does! Bind the operands.
2416 return (L.match(LHS) && R.match(RHS)) ||
2417 (Commutable && L.match(RHS) && R.match(LHS));
2418 }
2419};
2420
2421/// Helper class for identifying signed max predicates.
2423 static bool match(ICmpInst::Predicate Pred) {
2424 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2425 }
2426};
2427
2428/// Helper class for identifying signed min predicates.
2430 static bool match(ICmpInst::Predicate Pred) {
2431 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2432 }
2433};
2434
2435/// Helper class for identifying unsigned max predicates.
2437 static bool match(ICmpInst::Predicate Pred) {
2438 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2439 }
2440};
2441
2442/// Helper class for identifying unsigned min predicates.
2444 static bool match(ICmpInst::Predicate Pred) {
2445 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2446 }
2447};
2448
2449/// Helper class for identifying ordered max predicates.
2451 static bool match(FCmpInst::Predicate Pred) {
2452 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2453 }
2454};
2455
2456/// Helper class for identifying ordered min predicates.
2458 static bool match(FCmpInst::Predicate Pred) {
2459 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2460 }
2461};
2462
2463/// Helper class for identifying unordered max predicates.
2465 static bool match(FCmpInst::Predicate Pred) {
2466 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2467 }
2468};
2469
2470/// Helper class for identifying unordered min predicates.
2472 static bool match(FCmpInst::Predicate Pred) {
2473 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2474 }
2475};
2476
2477template <typename LHS, typename RHS>
2482
2483template <typename LHS, typename RHS>
2488
2489template <typename LHS, typename RHS>
2494
2495template <typename LHS, typename RHS>
2500
2501template <typename LHS, typename RHS>
2502inline match_combine_or<
2507m_MaxOrMin(const LHS &L, const RHS &R) {
2508 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2509 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2510}
2511
2512/// Match an 'ordered' floating point maximum function.
2513/// Floating point has one special value 'NaN'. Therefore, there is no total
2514/// order. However, if we can ignore the 'NaN' value (for example, because of a
2515/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2516/// semantics. In the presence of 'NaN' we have to preserve the original
2517/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2518///
2519/// max(L, R) iff L and R are not NaN
2520/// m_OrdFMax(L, R) = R iff L or R are NaN
2521template <typename LHS, typename RHS>
2526
2527/// Match an 'ordered' floating point minimum function.
2528/// Floating point has one special value 'NaN'. Therefore, there is no total
2529/// order. However, if we can ignore the 'NaN' value (for example, because of a
2530/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2531/// semantics. In the presence of 'NaN' we have to preserve the original
2532/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2533///
2534/// min(L, R) iff L and R are not NaN
2535/// m_OrdFMin(L, R) = R iff L or R are NaN
2536template <typename LHS, typename RHS>
2541
2542/// Match an 'unordered' floating point maximum function.
2543/// Floating point has one special value 'NaN'. Therefore, there is no total
2544/// order. However, if we can ignore the 'NaN' value (for example, because of a
2545/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2546/// semantics. In the presence of 'NaN' we have to preserve the original
2547/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2548///
2549/// max(L, R) iff L and R are not NaN
2550/// m_UnordFMax(L, R) = L iff L or R are NaN
2551template <typename LHS, typename RHS>
2553m_UnordFMax(const LHS &L, const RHS &R) {
2555}
2556
2557/// Match an 'unordered' floating point minimum function.
2558/// Floating point has one special value 'NaN'. Therefore, there is no total
2559/// order. However, if we can ignore the 'NaN' value (for example, because of a
2560/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2561/// semantics. In the presence of 'NaN' we have to preserve the original
2562/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2563///
2564/// min(L, R) iff L and R are not NaN
2565/// m_UnordFMin(L, R) = L iff L or R are NaN
2566template <typename LHS, typename RHS>
2568m_UnordFMin(const LHS &L, const RHS &R) {
2570}
2571
2572/// Match an 'ordered' or 'unordered' floating point maximum function.
2573/// Floating point has one special value 'NaN'. Therefore, there is no total
2574/// order. However, if we can ignore the 'NaN' value (for example, because of a
2575/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2576/// semantics.
2577template <typename LHS, typename RHS>
2584
2585/// Match an 'ordered' or 'unordered' floating point minimum function.
2586/// Floating point has one special value 'NaN'. Therefore, there is no total
2587/// order. However, if we can ignore the 'NaN' value (for example, because of a
2588/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2589/// semantics.
2590template <typename LHS, typename RHS>
2597
2598/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2599/// NOTE: we first match the 'Not' (by matching '-1'),
2600/// and only then match the inner matcher!
2601template <typename ValTy>
2602inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2603m_Not(const ValTy &V) {
2604 return m_c_Xor(m_AllOnes(), V);
2605}
2606
2607template <typename ValTy>
2608inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2609 true>
2610m_NotForbidPoison(const ValTy &V) {
2611 return m_c_Xor(m_AllOnesForbidPoison(), V);
2612}
2613
2614//===----------------------------------------------------------------------===//
2615// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2616// Note that S might be matched to other instructions than AddInst.
2617//
2618
2619template <typename LHS_t, typename RHS_t, typename Sum_t>
2623 Sum_t S;
2624
2625 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2626 : L(L), R(R), S(S) {}
2627
2628 template <typename OpTy> bool match(OpTy *V) const {
2629 Value *ICmpLHS, *ICmpRHS;
2630 CmpPredicate Pred;
2631 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2632 return false;
2633
2634 Value *AddLHS, *AddRHS;
2635 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2636
2637 // (a + b) u< a, (a + b) u< b
2638 if (Pred == ICmpInst::ICMP_ULT)
2639 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2640 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2641
2642 // a >u (a + b), b >u (a + b)
2643 if (Pred == ICmpInst::ICMP_UGT)
2644 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2645 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2646
2647 Value *Op1;
2648 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2649 // (~a) <u b
2650 if (Pred == ICmpInst::ICMP_ULT) {
2651 if (XorExpr.match(ICmpLHS))
2652 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2653 }
2654 // b > u (~a)
2655 if (Pred == ICmpInst::ICMP_UGT) {
2656 if (XorExpr.match(ICmpRHS))
2657 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2658 }
2659
2660 // Match special-case for increment-by-1.
2661 if (Pred == ICmpInst::ICMP_EQ) {
2662 // (a + 1) == 0
2663 // (1 + a) == 0
2664 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2665 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2666 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2667 // 0 == (a + 1)
2668 // 0 == (1 + a)
2669 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2670 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2671 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2672 }
2673
2674 return false;
2675 }
2676};
2677
2678/// Match an icmp instruction checking for unsigned overflow on addition.
2679///
2680/// S is matched to the addition whose result is being checked for overflow, and
2681/// L and R are matched to the LHS and RHS of S.
2682template <typename LHS_t, typename RHS_t, typename Sum_t>
2684m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2686}
2687
2688template <typename Opnd_t> struct Argument_match {
2689 unsigned OpI;
2690 Opnd_t Val;
2691
2692 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2693
2694 template <typename OpTy> bool match(OpTy *V) const {
2695 // FIXME: Should likely be switched to use `CallBase`.
2696 if (const auto *CI = dyn_cast<CallInst>(V))
2697 return Val.match(CI->getArgOperand(OpI));
2698 return false;
2699 }
2700};
2701
2702/// Match an argument.
2703template <unsigned OpI, typename Opnd_t>
2704inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2705 return Argument_match<Opnd_t>(OpI, Op);
2706}
2707
2708/// Intrinsic matchers.
2710 unsigned ID;
2711
2713
2714 template <typename OpTy> bool match(OpTy *V) const {
2715 if (const auto *CI = dyn_cast<CallInst>(V))
2716 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2717 return F->getIntrinsicID() == ID;
2718 return false;
2719 }
2720};
2721
2722/// Intrinsic matches are combinations of ID matchers, and argument
2723/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2724/// them with lower arity matchers. Here's some convenient typedefs for up to
2725/// several arguments, and more can be added as needed
2726template <typename T0 = void, typename T1 = void, typename T2 = void,
2727 typename T3 = void, typename T4 = void, typename T5 = void,
2728 typename T6 = void, typename T7 = void, typename T8 = void,
2729 typename T9 = void, typename T10 = void>
2731template <typename T0> struct m_Intrinsic_Ty<T0> {
2733};
2734template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2735 using Ty =
2737};
2738template <typename T0, typename T1, typename T2>
2743template <typename T0, typename T1, typename T2, typename T3>
2748
2749template <typename T0, typename T1, typename T2, typename T3, typename T4>
2754
2755template <typename T0, typename T1, typename T2, typename T3, typename T4,
2756 typename T5>
2761
2762/// Match intrinsic calls like this:
2763/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2764template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2765 return IntrinsicID_match(IntrID);
2766}
2767
2768/// Matches MaskedLoad Intrinsic.
2769template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2771m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2772 const Opnd3 &Op3) {
2773 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
2774}
2775
2776/// Matches MaskedGather Intrinsic.
2777template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
2779m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
2780 const Opnd3 &Op3) {
2781 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
2782}
2783
2784template <Intrinsic::ID IntrID, typename T0>
2785inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2787}
2788
2789template <Intrinsic::ID IntrID, typename T0, typename T1>
2790inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2791 const T1 &Op1) {
2793}
2794
2795template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2796inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2797m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2798 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2799}
2800
2801template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2802 typename T3>
2804m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2805 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2806}
2807
2808template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2809 typename T3, typename T4>
2811m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2812 const T4 &Op4) {
2813 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2814 m_Argument<4>(Op4));
2815}
2816
2817template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2818 typename T3, typename T4, typename T5>
2820m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2821 const T4 &Op4, const T5 &Op5) {
2822 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2823 m_Argument<5>(Op5));
2824}
2825
2826// Helper intrinsic matching specializations.
2827template <typename Opnd0>
2828inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2830}
2831
2832template <typename Opnd0>
2833inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2835}
2836
2837template <typename Opnd0>
2838inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2839 return m_Intrinsic<Intrinsic::fabs>(Op0);
2840}
2841
2842template <typename Opnd0>
2843inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2845}
2846
2847template <typename Opnd0, typename Opnd1>
2848inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2849 const Opnd1 &Op1) {
2850 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2851}
2852
2853template <typename Opnd0, typename Opnd1>
2854inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2855 const Opnd1 &Op1) {
2856 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2857}
2858
2859template <typename Opnd0, typename Opnd1>
2861m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2862 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2863}
2864
2865template <typename Opnd0, typename Opnd1>
2866inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2867 const Opnd1 &Op1) {
2868 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2869}
2870
2871template <typename Opnd0, typename Opnd1>
2872inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2873 const Opnd1 &Op1) {
2874 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2875}
2876
2877template <typename Opnd0, typename Opnd1>
2879m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2880 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2881}
2882
2883template <typename Opnd0, typename Opnd1, typename Opnd2>
2885m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2886 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2887}
2888
2889template <typename Opnd0, typename Opnd1, typename Opnd2>
2891m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2892 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2893}
2894
2895template <typename Opnd0>
2896inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2897 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2898}
2899
2900template <typename Opnd0, typename Opnd1>
2901inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2902 const Opnd1 &Op1) {
2903 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2904}
2905
2906template <typename Opnd0>
2907inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2909}
2910
2911//===----------------------------------------------------------------------===//
2912// Matchers for two-operands operators with the operators in either order
2913//
2914
2915/// Matches a BinaryOperator with LHS and RHS in either order.
2916template <typename LHS, typename RHS>
2919}
2920
2921/// Matches an ICmp with a predicate over LHS and RHS in either order.
2922/// Swaps the predicate if operands are commuted.
2923template <typename LHS, typename RHS>
2925m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2927}
2928
2929template <typename LHS, typename RHS>
2934
2935/// Matches a specific opcode with LHS and RHS in either order.
2936template <typename LHS, typename RHS>
2938m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2939 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2940}
2941
2942/// Matches a Add with LHS and RHS in either order.
2943template <typename LHS, typename RHS>
2948
2949/// Matches a Mul with LHS and RHS in either order.
2950template <typename LHS, typename RHS>
2955
2956/// Matches an And with LHS and RHS in either order.
2957template <typename LHS, typename RHS>
2962
2963/// Matches an Or with LHS and RHS in either order.
2964template <typename LHS, typename RHS>
2969
2970/// Matches an Xor with LHS and RHS in either order.
2971template <typename LHS, typename RHS>
2976
2977/// Matches a 'Neg' as 'sub 0, V'.
2978template <typename ValTy>
2979inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2980m_Neg(const ValTy &V) {
2981 return m_Sub(m_ZeroInt(), V);
2982}
2983
2984/// Matches a 'Neg' as 'sub nsw 0, V'.
2985template <typename ValTy>
2987 Instruction::Sub,
2989m_NSWNeg(const ValTy &V) {
2990 return m_NSWSub(m_ZeroInt(), V);
2991}
2992
2993/// Matches an SMin with LHS and RHS in either order.
2994template <typename LHS, typename RHS>
2996m_c_SMin(const LHS &L, const RHS &R) {
2998}
2999/// Matches an SMax with LHS and RHS in either order.
3000template <typename LHS, typename RHS>
3002m_c_SMax(const LHS &L, const RHS &R) {
3004}
3005/// Matches a UMin with LHS and RHS in either order.
3006template <typename LHS, typename RHS>
3008m_c_UMin(const LHS &L, const RHS &R) {
3010}
3011/// Matches a UMax with LHS and RHS in either order.
3012template <typename LHS, typename RHS>
3014m_c_UMax(const LHS &L, const RHS &R) {
3016}
3017
3018template <typename LHS, typename RHS>
3019inline match_combine_or<
3024m_c_MaxOrMin(const LHS &L, const RHS &R) {
3025 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3026 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3027}
3028
3029template <Intrinsic::ID IntrID, typename T0, typename T1>
3032m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3033 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
3034 m_Intrinsic<IntrID>(Op1, Op0));
3035}
3036
3037/// Matches FAdd with LHS and RHS in either order.
3038template <typename LHS, typename RHS>
3040m_c_FAdd(const LHS &L, const RHS &R) {
3042}
3043
3044/// Matches FMul with LHS and RHS in either order.
3045template <typename LHS, typename RHS>
3047m_c_FMul(const LHS &L, const RHS &R) {
3049}
3050
3051template <typename Opnd_t> struct Signum_match {
3052 Opnd_t Val;
3053 Signum_match(const Opnd_t &V) : Val(V) {}
3054
3055 template <typename OpTy> bool match(OpTy *V) const {
3056 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3057 if (TypeSize == 0)
3058 return false;
3059
3060 unsigned ShiftWidth = TypeSize - 1;
3061 Value *Op;
3062
3063 // This is the representation of signum we match:
3064 //
3065 // signum(x) == (x >> 63) | (-x >>u 63)
3066 //
3067 // An i1 value is its own signum, so it's correct to match
3068 //
3069 // signum(x) == (x >> 0) | (-x >>u 0)
3070 //
3071 // for i1 values.
3072
3073 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3074 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3075 auto Signum = m_c_Or(LHS, RHS);
3076
3077 return Signum.match(V) && Val.match(Op);
3078 }
3079};
3080
3081/// Matches a signum pattern.
3082///
3083/// signum(x) =
3084/// x > 0 -> 1
3085/// x == 0 -> 0
3086/// x < 0 -> -1
3087template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3088 return Signum_match<Val_t>(V);
3089}
3090
3091template <int Ind, typename Opnd_t> struct ExtractValue_match {
3092 Opnd_t Val;
3093 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3094
3095 template <typename OpTy> bool match(OpTy *V) const {
3096 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3097 // If Ind is -1, don't inspect indices
3098 if (Ind != -1 &&
3099 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3100 return false;
3101 return Val.match(I->getAggregateOperand());
3102 }
3103 return false;
3104 }
3105};
3106
3107/// Match a single index ExtractValue instruction.
3108/// For example m_ExtractValue<1>(...)
3109template <int Ind, typename Val_t>
3113
3114/// Match an ExtractValue instruction with any index.
3115/// For example m_ExtractValue(...)
3116template <typename Val_t>
3117inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3118 return ExtractValue_match<-1, Val_t>(V);
3119}
3120
3121/// Matcher for a single index InsertValue instruction.
3122template <int Ind, typename T0, typename T1> struct InsertValue_match {
3125
3126 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3127
3128 template <typename OpTy> bool match(OpTy *V) const {
3129 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3130 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3131 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3132 }
3133 return false;
3134 }
3135};
3136
3137/// Matches a single index InsertValue instruction.
3138template <int Ind, typename Val_t, typename Elt_t>
3140 const Elt_t &Elt) {
3141 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3142}
3143
3144/// Matches a call to `llvm.vscale()`.
3146
3147template <typename Opnd0, typename Opnd1>
3149m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3151}
3152
3153template <typename Opnd>
3157
3158template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3162
3163 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3164
3165 template <typename T> bool match(T *V) const {
3166 auto *I = dyn_cast<Instruction>(V);
3167 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3168 return false;
3169
3170 if (I->getOpcode() == Opcode) {
3171 auto *Op0 = I->getOperand(0);
3172 auto *Op1 = I->getOperand(1);
3173 return (L.match(Op0) && R.match(Op1)) ||
3174 (Commutable && L.match(Op1) && R.match(Op0));
3175 }
3176
3177 if (auto *Select = dyn_cast<SelectInst>(I)) {
3178 auto *Cond = Select->getCondition();
3179 auto *TVal = Select->getTrueValue();
3180 auto *FVal = Select->getFalseValue();
3181
3182 // Don't match a scalar select of bool vectors.
3183 // Transforms expect a single type for operands if this matches.
3184 if (Cond->getType() != Select->getType())
3185 return false;
3186
3187 if (Opcode == Instruction::And) {
3188 auto *C = dyn_cast<Constant>(FVal);
3189 if (C && C->isNullValue())
3190 return (L.match(Cond) && R.match(TVal)) ||
3191 (Commutable && L.match(TVal) && R.match(Cond));
3192 } else {
3193 assert(Opcode == Instruction::Or);
3194 auto *C = dyn_cast<Constant>(TVal);
3195 if (C && C->isOneValue())
3196 return (L.match(Cond) && R.match(FVal)) ||
3197 (Commutable && L.match(FVal) && R.match(Cond));
3198 }
3199 }
3200
3201 return false;
3202 }
3203};
3204
3205/// Matches L && R either in the form of L & R or L ? R : false.
3206/// Note that the latter form is poison-blocking.
3207template <typename LHS, typename RHS>
3212
3213/// Matches L && R where L and R are arbitrary values.
3214inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3215
3216/// Matches L && R with LHS and RHS in either order.
3217template <typename LHS, typename RHS>
3219m_c_LogicalAnd(const LHS &L, const RHS &R) {
3221}
3222
3223/// Matches L || R either in the form of L | R or L ? true : R.
3224/// Note that the latter form is poison-blocking.
3225template <typename LHS, typename RHS>
3230
3231/// Matches L || R where L and R are arbitrary values.
3232inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3233
3234/// Matches L || R with LHS and RHS in either order.
3235template <typename LHS, typename RHS>
3237m_c_LogicalOr(const LHS &L, const RHS &R) {
3239}
3240
3241/// Matches either L && R or L || R,
3242/// either one being in the either binary or logical form.
3243/// Note that the latter form is poison-blocking.
3244template <typename LHS, typename RHS, bool Commutable = false>
3250
3251/// Matches either L && R or L || R where L and R are arbitrary values.
3252inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3253
3254/// Matches either L && R or L || R with LHS and RHS in either order.
3255template <typename LHS, typename RHS>
3256inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3257 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3258}
3259
3260} // end namespace PatternMatch
3261} // end namespace llvm
3262
3263#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.
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.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
ap_match< APFloat > m_APFloatForbidPoison(const APFloat *&Res)
Match APFloat while forbidding poison in splat vector constants.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
auto m_c_XorLike(const LHS &L, const RHS &R)
Match either (xor L, R), (xor R, L) or (sub nuw R, L) iff R.isMask() Only commutative matcher as the ...
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
ap_match< APFloat > m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
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.
ap_match< APInt > m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
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.
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)
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)
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
ThreeOps_match< Cond, constantint_match< L >, constantint_match< R >, Instruction::Select > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.: m_SelectCst<-1, 0>(m_Value(V))
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
CastInst_match< OpTy, FPTruncInst > m_FPTrunc(const OpTy &Op)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
SpecificCmpClass_match< LHS, RHS, ICmpInst, true > m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, TruncInst > >, OpTy > m_ZExtOrTruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
cstfp_pred_ty< is_noninf > m_NonInf()
Match a non-infinity FP constant, i.e.
m_Intrinsic_Ty< Opnd >::Ty m_Deinterleave2(const Opnd &Op)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedGather Intrinsic.
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
AllowReassoc_match(const SubPattern_t &SP)
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and any number of operands.
std::enable_if_t< Idx==Last, bool > match_operands(const Instruction *I) const
std::enable_if_t< Idx !=Last, bool > match_operands(const Instruction *I) const
std::tuple< OperandTypes... > Operands
AnyOps_match(const OperandTypes &...Ops)
Argument_match(unsigned OpIdx, const Opnd_t &V)
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS)
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
bool match(unsigned Opc, OpTy *V) const
CastInst_match(const Op_t &OpMatch)
CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS)
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const 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)