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
201template <typename SubPattern_t> struct Splat_match {
202 SubPattern_t SubPattern;
203 Splat_match(const SubPattern_t &SP) : SubPattern(SP) {}
204
205 template <typename OpTy> bool match(OpTy *V) const {
206 if (auto *C = dyn_cast<Constant>(V)) {
207 auto *Splat = C->getSplatValue();
208 return Splat ? SubPattern.match(Splat) : false;
209 }
210 // TODO: Extend to other cases (e.g. shufflevectors).
211 return false;
212 }
213};
214
215/// Match a constant splat. TODO: Extend this to non-constant splats.
216template <typename T>
217inline Splat_match<T> m_ConstantSplat(const T &SubPattern) {
218 return SubPattern;
219}
220
221/// Match an arbitrary basic block value and ignore it.
225
226/// Inverting matcher
227template <typename Ty> struct match_unless {
228 Ty M;
229
230 match_unless(const Ty &Matcher) : M(Matcher) {}
231
232 template <typename ITy> bool match(ITy *V) const { return !M.match(V); }
233};
234
235/// Match if the inner matcher does *NOT* match.
236template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
237 return match_unless<Ty>(M);
238}
239
240/// Matching combinators
241template <typename LTy, typename RTy> struct match_combine_or {
242 LTy L;
243 RTy R;
244
245 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
246
247 template <typename ITy> bool match(ITy *V) const {
248 if (L.match(V))
249 return true;
250 if (R.match(V))
251 return true;
252 return false;
253 }
254};
255
256template <typename LTy, typename RTy> struct match_combine_and {
257 LTy L;
258 RTy R;
259
260 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
261
262 template <typename ITy> bool match(ITy *V) const {
263 if (L.match(V))
264 if (R.match(V))
265 return true;
266 return false;
267 }
268};
269
270/// Combine two pattern matchers matching L || R
271template <typename LTy, typename RTy>
272inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
273 return match_combine_or<LTy, RTy>(L, R);
274}
275
276/// Combine two pattern matchers matching L && R
277template <typename LTy, typename RTy>
278inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
279 return match_combine_and<LTy, RTy>(L, R);
280}
281
282template <typename APTy> struct ap_match {
283 static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
285 std::conditional_t<std::is_same_v<APTy, APInt>, ConstantInt, ConstantFP>;
286
287 const APTy *&Res;
289
290 ap_match(const APTy *&Res, bool AllowPoison)
292
293 template <typename ITy> bool match(ITy *V) const {
294 if (auto *CI = dyn_cast<ConstantTy>(V)) {
295 Res = &CI->getValue();
296 return true;
297 }
298 if (V->getType()->isVectorTy())
299 if (const auto *C = dyn_cast<Constant>(V))
300 if (auto *CI =
301 dyn_cast_or_null<ConstantTy>(C->getSplatValue(AllowPoison))) {
302 Res = &CI->getValue();
303 return true;
304 }
305 return false;
306 }
307};
308
309/// Match a ConstantInt or splatted ConstantVector, binding the
310/// specified pointer to the contained APInt.
311inline ap_match<APInt> m_APInt(const APInt *&Res) {
312 // Forbid poison by default to maintain previous behavior.
313 return ap_match<APInt>(Res, /* AllowPoison */ false);
314}
315
316/// Match APInt while allowing poison in splat vector constants.
318 return ap_match<APInt>(Res, /* AllowPoison */ true);
319}
320
321/// Match APInt while forbidding poison in splat vector constants.
323 return ap_match<APInt>(Res, /* AllowPoison */ false);
324}
325
326/// Match a ConstantFP or splatted ConstantVector, binding the
327/// specified pointer to the contained APFloat.
329 // Forbid undefs by default to maintain previous behavior.
330 return ap_match<APFloat>(Res, /* AllowPoison */ false);
331}
332
333/// Match APFloat while allowing poison in splat vector constants.
335 return ap_match<APFloat>(Res, /* AllowPoison */ true);
336}
337
338/// Match APFloat while forbidding poison in splat vector constants.
340 return ap_match<APFloat>(Res, /* AllowPoison */ false);
341}
342
343template <int64_t Val> struct constantint_match {
344 template <typename ITy> bool match(ITy *V) const {
345 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
346 const APInt &CIV = CI->getValue();
347 if (Val >= 0)
348 return CIV == static_cast<uint64_t>(Val);
349 // If Val is negative, and CI is shorter than it, truncate to the right
350 // number of bits. If it is larger, then we have to sign extend. Just
351 // compare their negated values.
352 return -CIV == -Val;
353 }
354 return false;
355 }
356};
357
358/// Match a ConstantInt with a specific value.
359template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
360 return constantint_match<Val>();
361}
362
363/// This helper class is used to match constant scalars, vector splats,
364/// and fixed width vectors that satisfy a specified predicate.
365/// For fixed width vector constants, poison elements are ignored if AllowPoison
366/// is true.
367template <typename Predicate, typename ConstantVal, bool AllowPoison>
368struct cstval_pred_ty : public Predicate {
369 const Constant **Res = nullptr;
370 template <typename ITy> bool match_impl(ITy *V) const {
371 if (const auto *CV = dyn_cast<ConstantVal>(V))
372 return this->isValue(CV->getValue());
373 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
374 if (const auto *C = dyn_cast<Constant>(V)) {
375 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
376 return this->isValue(CV->getValue());
377
378 // Number of elements of a scalable vector unknown at compile time
379 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
380 if (!FVTy)
381 return false;
382
383 // Non-splat vector constant: check each element for a match.
384 unsigned NumElts = FVTy->getNumElements();
385 assert(NumElts != 0 && "Constant vector with no elements?");
386 bool HasNonPoisonElements = false;
387 for (unsigned i = 0; i != NumElts; ++i) {
388 Constant *Elt = C->getAggregateElement(i);
389 if (!Elt)
390 return false;
391 if (AllowPoison && isa<PoisonValue>(Elt))
392 continue;
393 auto *CV = dyn_cast<ConstantVal>(Elt);
394 if (!CV || !this->isValue(CV->getValue()))
395 return false;
396 HasNonPoisonElements = true;
397 }
398 return HasNonPoisonElements;
399 }
400 }
401 return false;
402 }
403
404 template <typename ITy> bool match(ITy *V) const {
405 if (this->match_impl(V)) {
406 if (Res)
407 *Res = cast<Constant>(V);
408 return true;
409 }
410 return false;
411 }
412};
413
414/// specialization of cstval_pred_ty for ConstantInt
415template <typename Predicate, bool AllowPoison = true>
417
418/// specialization of cstval_pred_ty for ConstantFP
419template <typename Predicate>
421 /*AllowPoison=*/true>;
422
423/// This helper class is used to match scalar and vector constants that
424/// satisfy a specified predicate, and bind them to an APInt.
425template <typename Predicate> struct api_pred_ty : public Predicate {
426 const APInt *&Res;
427
428 api_pred_ty(const APInt *&R) : Res(R) {}
429
430 template <typename ITy> bool match(ITy *V) const {
431 if (const auto *CI = dyn_cast<ConstantInt>(V))
432 if (this->isValue(CI->getValue())) {
433 Res = &CI->getValue();
434 return true;
435 }
436 if (V->getType()->isVectorTy())
437 if (const auto *C = dyn_cast<Constant>(V))
438 if (auto *CI = dyn_cast_or_null<ConstantInt>(
439 C->getSplatValue(/*AllowPoison=*/true)))
440 if (this->isValue(CI->getValue())) {
441 Res = &CI->getValue();
442 return true;
443 }
444
445 return false;
446 }
447};
448
449/// This helper class is used to match scalar and vector constants that
450/// satisfy a specified predicate, and bind them to an APFloat.
451/// Poison is allowed in splat vector constants.
452template <typename Predicate> struct apf_pred_ty : public Predicate {
453 const APFloat *&Res;
454
455 apf_pred_ty(const APFloat *&R) : Res(R) {}
456
457 template <typename ITy> bool match(ITy *V) const {
458 if (const auto *CI = dyn_cast<ConstantFP>(V))
459 if (this->isValue(CI->getValue())) {
460 Res = &CI->getValue();
461 return true;
462 }
463 if (V->getType()->isVectorTy())
464 if (const auto *C = dyn_cast<Constant>(V))
465 if (auto *CI = dyn_cast_or_null<ConstantFP>(
466 C->getSplatValue(/* AllowPoison */ true)))
467 if (this->isValue(CI->getValue())) {
468 Res = &CI->getValue();
469 return true;
470 }
471
472 return false;
473 }
474};
475
476///////////////////////////////////////////////////////////////////////////////
477//
478// Encapsulate constant value queries for use in templated predicate matchers.
479// This allows checking if constants match using compound predicates and works
480// with vector constants, possibly with relaxed constraints. For example, ignore
481// undef values.
482//
483///////////////////////////////////////////////////////////////////////////////
484
485template <typename APTy> struct custom_checkfn {
486 function_ref<bool(const APTy &)> CheckFn;
487 bool isValue(const APTy &C) const { return CheckFn(C); }
488};
489
490/// Match an integer or vector where CheckFn(ele) for each element is true.
491/// For vectors, poison elements are assumed to match.
493m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
494 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}};
495}
496
498m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) {
499 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V};
500}
501
502/// Match a float or vector where CheckFn(ele) for each element is true.
503/// For vectors, poison elements are assumed to match.
505m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) {
506 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}};
507}
508
510m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) {
511 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V};
512}
513
515 bool isValue(const APInt &C) const { return true; }
516};
517/// Match an integer or vector with any integral constant.
518/// For vectors, this includes constants with undefined elements.
522
524 bool isValue(const APInt &C) const { return C.isShiftedMask(); }
525};
526
530
532 bool isValue(const APInt &C) const { return C.isAllOnes(); }
533};
534/// Match an integer or vector with all bits set.
535/// For vectors, this includes constants with undefined elements.
539
543
545 bool isValue(const APInt &C) const { return C.isMaxSignedValue(); }
546};
547/// Match an integer or vector with values having all bits except for the high
548/// bit set (0x7f...).
549/// For vectors, this includes constants with undefined elements.
554 return V;
555}
556
558 bool isValue(const APInt &C) const { return C.isNegative(); }
559};
560/// Match an integer or vector of negative values.
561/// For vectors, this includes constants with undefined elements.
565inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
566
568 bool isValue(const APInt &C) const { return C.isNonNegative(); }
569};
570/// Match an integer or vector of non-negative values.
571/// For vectors, this includes constants with undefined elements.
575inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
576
578 bool isValue(const APInt &C) const { return C.isStrictlyPositive(); }
579};
580/// Match an integer or vector of strictly positive values.
581/// For vectors, this includes constants with undefined elements.
586 return V;
587}
588
590 bool isValue(const APInt &C) const { return C.isNonPositive(); }
591};
592/// Match an integer or vector of non-positive values.
593/// For vectors, this includes constants with undefined elements.
597inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
598
599struct is_one {
600 bool isValue(const APInt &C) const { return C.isOne(); }
601};
602/// Match an integer 1 or a vector with all elements equal to 1.
603/// For vectors, this includes constants with undefined elements.
605
607 bool isValue(const APInt &C) const { return C.isZero(); }
608};
609/// Match an integer 0 or a vector with all elements equal to 0.
610/// For vectors, this includes constants with undefined elements.
614
615struct is_zero {
616 template <typename ITy> bool match(ITy *V) const {
617 auto *C = dyn_cast<Constant>(V);
618 // FIXME: this should be able to do something for scalable vectors
619 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
620 }
621};
622/// Match any null constant or a vector with all elements equal to 0.
623/// For vectors, this includes constants with undefined elements.
624inline is_zero m_Zero() { return is_zero(); }
625
626struct is_power2 {
627 bool isValue(const APInt &C) const { return C.isPowerOf2(); }
628};
629/// Match an integer or vector power-of-2.
630/// For vectors, this includes constants with undefined elements.
632inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
633
635 bool isValue(const APInt &C) const { return C.isNegatedPowerOf2(); }
636};
637/// Match a integer or vector negated power-of-2.
638/// For vectors, this includes constants with undefined elements.
643 return V;
644}
645
647 bool isValue(const APInt &C) const { return !C || C.isNegatedPowerOf2(); }
648};
649/// Match a integer or vector negated power-of-2.
650/// For vectors, this includes constants with undefined elements.
656 return V;
657}
658
660 bool isValue(const APInt &C) const { return !C || C.isPowerOf2(); }
661};
662/// Match an integer or vector of 0 or power-of-2 values.
663/// For vectors, this includes constants with undefined elements.
668 return V;
669}
670
672 bool isValue(const APInt &C) const { return C.isSignMask(); }
673};
674/// Match an integer or vector with only the sign bit(s) set.
675/// For vectors, this includes constants with undefined elements.
679
681 bool isValue(const APInt &C) const { return C.isMask(); }
682};
683/// Match an integer or vector with only the low bit(s) set.
684/// For vectors, this includes constants with undefined elements.
688inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
689
691 bool isValue(const APInt &C) const { return !C || C.isMask(); }
692};
693/// Match an integer or vector with only the low bit(s) set.
694/// For vectors, this includes constants with undefined elements.
699 return V;
700}
701
704 const APInt *Thr;
705 bool isValue(const APInt &C) const {
706 return ICmpInst::compare(C, *Thr, Pred);
707 }
708};
709/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
710/// to Threshold. For vectors, this includes constants with undefined elements.
714 P.Pred = Predicate;
715 P.Thr = &Threshold;
716 return P;
717}
718
719struct is_nan {
720 bool isValue(const APFloat &C) const { return C.isNaN(); }
721};
722/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
723/// For vectors, this includes constants with undefined elements.
725
726struct is_nonnan {
727 bool isValue(const APFloat &C) const { return !C.isNaN(); }
728};
729/// Match a non-NaN FP constant.
730/// For vectors, this includes constants with undefined elements.
734
735struct is_inf {
736 bool isValue(const APFloat &C) const { return C.isInfinity(); }
737};
738/// Match a positive or negative infinity FP constant.
739/// For vectors, this includes constants with undefined elements.
741
742struct is_noninf {
743 bool isValue(const APFloat &C) const { return !C.isInfinity(); }
744};
745/// Match a non-infinity FP constant, i.e. finite or NaN.
746/// For vectors, this includes constants with undefined elements.
750
751struct is_finite {
752 bool isValue(const APFloat &C) const { return C.isFinite(); }
753};
754/// Match a finite FP constant, i.e. not infinity or NaN.
755/// For vectors, this includes constants with undefined elements.
759inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
760
762 bool isValue(const APFloat &C) const { return C.isFiniteNonZero(); }
763};
764/// Match a finite non-zero FP constant.
765/// For vectors, this includes constants with undefined elements.
770 return V;
771}
772
774 bool isValue(const APFloat &C) const { return C.isZero(); }
775};
776/// Match a floating-point negative zero or positive zero.
777/// For vectors, this includes constants with undefined elements.
781
783 bool isValue(const APFloat &C) const { return C.isPosZero(); }
784};
785/// Match a floating-point positive zero.
786/// For vectors, this includes constants with undefined elements.
790
792 bool isValue(const APFloat &C) const { return C.isNegZero(); }
793};
794/// Match a floating-point negative zero.
795/// For vectors, this includes constants with undefined elements.
799
801 bool isValue(const APFloat &C) const { return C.isNonZero(); }
802};
803/// Match a floating-point non-zero.
804/// For vectors, this includes constants with undefined elements.
808
810 bool isValue(const APFloat &C) const {
811 return !C.isDenormal() && C.isNonZero();
812 }
813};
814
815/// Match a floating-point non-zero that is not a denormal.
816/// For vectors, this includes constants with undefined elements.
820
821///////////////////////////////////////////////////////////////////////////////
822
823template <typename Class> struct bind_ty {
824 Class *&VR;
825
826 bind_ty(Class *&V) : VR(V) {}
827
828 template <typename ITy> bool match(ITy *V) const {
829 if (auto *CV = dyn_cast<Class>(V)) {
830 VR = CV;
831 return true;
832 }
833 return false;
834 }
835};
836
837/// Check whether the value has the given Class and matches the nested
838/// pattern. Capture it into the provided variable if successful.
839template <typename Class, typename MatchTy> struct bind_and_match_ty {
840 Class *&VR;
841 MatchTy Match;
842
843 bind_and_match_ty(Class *&V, const MatchTy &Match) : VR(V), Match(Match) {}
844
845 template <typename ITy> bool match(ITy *V) const {
846 auto *CV = dyn_cast<Class>(V);
847 if (CV && Match.match(V)) {
848 VR = CV;
849 return true;
850 }
851 return false;
852 }
853};
854
855/// Match a value, capturing it if we match.
856inline bind_ty<Value> m_Value(Value *&V) { return V; }
857inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
858
859/// Match against the nested pattern, and capture the value if we match.
860template <typename MatchTy>
862 const MatchTy &Match) {
863 return {V, Match};
864}
865
866/// Match against the nested pattern, and capture the value if we match.
867template <typename MatchTy>
869 const MatchTy &Match) {
870 return {V, Match};
871}
872
873/// Match an instruction, capturing it if we match.
876 return I;
877}
878
879/// Match against the nested pattern, and capture the instruction if we match.
880template <typename MatchTy>
882m_Instruction(Instruction *&I, const MatchTy &Match) {
883 return {I, Match};
884}
885template <typename MatchTy>
887m_Instruction(const Instruction *&I, const MatchTy &Match) {
888 return {I, Match};
889}
890
891/// Match a unary operator, capturing it if we match.
894 return I;
895}
896/// Match a binary operator, capturing it if we match.
899 return I;
900}
901/// Match a with overflow intrinsic, capturing it if we match.
907 return I;
908}
909
910/// Match an UndefValue, capturing the value if we match.
912
913/// Match a Constant, capturing the value if we match.
915
916/// Match a ConstantInt, capturing the value if we match.
918
919/// Match a ConstantFP, capturing the value if we match.
921
922/// Match a ConstantExpr, capturing the value if we match.
924
925/// Match a basic block value, capturing it if we match.
928 return V;
929}
930
931// TODO: Remove once UseConstant{Int,FP}ForScalableSplat is enabled by default,
932// and use m_Unless(m_ConstantExpr).
934 template <typename ITy> static bool isImmConstant(ITy *V) {
935 if (auto *CV = dyn_cast<Constant>(V)) {
936 if (!isa<ConstantExpr>(CV) && !CV->containsConstantExpression())
937 return true;
938
939 if (CV->getType()->isVectorTy()) {
940 if (auto *Splat = CV->getSplatValue(/*AllowPoison=*/true)) {
941 if (!isa<ConstantExpr>(Splat) &&
942 !Splat->containsConstantExpression()) {
943 return true;
944 }
945 }
946 }
947 }
948 return false;
949 }
950};
951
953 template <typename ITy> bool match(ITy *V) const { return isImmConstant(V); }
954};
955
956/// Match an arbitrary immediate Constant and ignore it.
958
961
963
964 template <typename ITy> bool match(ITy *V) const {
965 if (isImmConstant(V)) {
966 VR = cast<Constant>(V);
967 return true;
968 }
969 return false;
970 }
971};
972
973/// Match an immediate Constant, capturing the value if we match.
977
978/// Match a specified Value*.
980 const Value *Val;
981
982 specificval_ty(const Value *V) : Val(V) {}
983
984 template <typename ITy> bool match(ITy *V) const { return V == Val; }
985};
986
987/// Match if we have a specific specified value.
988inline specificval_ty m_Specific(const Value *V) { return V; }
989
990/// Stores a reference to the Value *, not the Value * itself,
991/// thus can be used in commutative matchers.
992template <typename Class> struct deferredval_ty {
993 Class *const &Val;
994
995 deferredval_ty(Class *const &V) : Val(V) {}
996
997 template <typename ITy> bool match(ITy *const V) const { return V == Val; }
998};
999
1000/// Like m_Specific(), but works if the specific value to match is determined
1001/// as part of the same match() expression. For example:
1002/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
1003/// bind X before the pattern match starts.
1004/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
1005/// whichever value m_Value(X) populated.
1006inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
1008 return V;
1009}
1010
1011/// Match a specified floating point value or vector of all elements of
1012/// that value.
1014 double Val;
1015
1016 specific_fpval(double V) : Val(V) {}
1017
1018 template <typename ITy> bool match(ITy *V) const {
1019 if (const auto *CFP = dyn_cast<ConstantFP>(V))
1020 return CFP->isExactlyValue(Val);
1021 if (V->getType()->isVectorTy())
1022 if (const auto *C = dyn_cast<Constant>(V))
1023 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
1024 return CFP->isExactlyValue(Val);
1025 return false;
1026 }
1027};
1028
1029/// Match a specific floating point value or vector with all elements
1030/// equal to the value.
1031inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
1032
1033/// Match a float 1.0 or vector with all elements equal to 1.0.
1034inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
1035
1038
1040
1041 template <typename ITy> bool match(ITy *V) const {
1042 const APInt *ConstInt;
1043 if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
1044 return false;
1045 if (ConstInt->getActiveBits() > 64)
1046 return false;
1047 VR = ConstInt->getZExtValue();
1048 return true;
1049 }
1050};
1051
1052/// Match a specified integer value or vector of all elements of that
1053/// value.
1054template <bool AllowPoison> struct specific_intval {
1055 const APInt &Val;
1056
1057 specific_intval(const APInt &V) : Val(V) {}
1058
1059 template <typename ITy> bool match(ITy *V) const {
1060 const auto *CI = dyn_cast<ConstantInt>(V);
1061 if (!CI && V->getType()->isVectorTy())
1062 if (const auto *C = dyn_cast<Constant>(V))
1063 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1064
1065 return CI && APInt::isSameValue(CI->getValue(), Val);
1066 }
1067};
1068
1069template <bool AllowPoison> struct specific_intval64 {
1071
1073
1074 template <typename ITy> bool match(ITy *V) const {
1075 const auto *CI = dyn_cast<ConstantInt>(V);
1076 if (!CI && V->getType()->isVectorTy())
1077 if (const auto *C = dyn_cast<Constant>(V))
1078 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison));
1079
1080 return CI && CI->getValue() == Val;
1081 }
1082};
1083
1084/// Match a specific integer value or vector with all elements equal to
1085/// the value.
1087 return specific_intval<false>(V);
1088}
1089
1093
1097
1101
1102/// Match a ConstantInt and bind to its value. This does not match
1103/// ConstantInts wider than 64-bits.
1105
1106/// Match a specified basic block value.
1109
1111
1112 template <typename ITy> bool match(ITy *V) const {
1113 const auto *BB = dyn_cast<BasicBlock>(V);
1114 return BB && BB == Val;
1115 }
1116};
1117
1118/// Match a specific basic block value.
1120 return specific_bbval(BB);
1121}
1122
1123/// A commutative-friendly version of m_Specific().
1125 return BB;
1126}
1128m_Deferred(const BasicBlock *const &BB) {
1129 return BB;
1130}
1131
1132//===----------------------------------------------------------------------===//
1133// Matcher for any binary operator.
1134//
1135template <typename LHS_t, typename RHS_t, bool Commutable = false>
1139
1140 // The evaluation order is always stable, regardless of Commutability.
1141 // The LHS is always matched first.
1142 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1143
1144 template <typename OpTy> bool match(OpTy *V) const {
1145 if (auto *I = dyn_cast<BinaryOperator>(V))
1146 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1147 (Commutable && L.match(I->getOperand(1)) &&
1148 R.match(I->getOperand(0)));
1149 return false;
1150 }
1151};
1152
1153template <typename LHS, typename RHS>
1154inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
1155 return AnyBinaryOp_match<LHS, RHS>(L, R);
1156}
1157
1158//===----------------------------------------------------------------------===//
1159// Matcher for any unary operator.
1160// TODO fuse unary, binary matcher into n-ary matcher
1161//
1162template <typename OP_t> struct AnyUnaryOp_match {
1163 OP_t X;
1164
1165 AnyUnaryOp_match(const OP_t &X) : X(X) {}
1166
1167 template <typename OpTy> bool match(OpTy *V) const {
1168 if (auto *I = dyn_cast<UnaryOperator>(V))
1169 return X.match(I->getOperand(0));
1170 return false;
1171 }
1172};
1173
1174template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
1175 return AnyUnaryOp_match<OP_t>(X);
1176}
1177
1178//===----------------------------------------------------------------------===//
1179// Matchers for specific binary operators.
1180//
1181
1182template <typename LHS_t, typename RHS_t, unsigned Opcode,
1183 bool Commutable = false>
1187
1188 // The evaluation order is always stable, regardless of Commutability.
1189 // The LHS is always matched first.
1190 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1191
1192 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) const {
1193 if (V->getValueID() == Value::InstructionVal + Opc) {
1194 auto *I = cast<BinaryOperator>(V);
1195 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1196 (Commutable && L.match(I->getOperand(1)) &&
1197 R.match(I->getOperand(0)));
1198 }
1199 return false;
1200 }
1201
1202 template <typename OpTy> bool match(OpTy *V) const {
1203 return match(Opcode, V);
1204 }
1205};
1206
1207template <typename LHS, typename RHS>
1212
1213template <typename LHS, typename RHS>
1218
1219template <typename LHS, typename RHS>
1224
1225template <typename LHS, typename RHS>
1230
1231template <typename Op_t> struct FNeg_match {
1232 Op_t X;
1233
1234 FNeg_match(const Op_t &Op) : X(Op) {}
1235 template <typename OpTy> bool match(OpTy *V) const {
1236 auto *FPMO = dyn_cast<FPMathOperator>(V);
1237 if (!FPMO)
1238 return false;
1239
1240 if (FPMO->getOpcode() == Instruction::FNeg)
1241 return X.match(FPMO->getOperand(0));
1242
1243 if (FPMO->getOpcode() == Instruction::FSub) {
1244 if (FPMO->hasNoSignedZeros()) {
1245 // With 'nsz', any zero goes.
1246 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1247 return false;
1248 } else {
1249 // Without 'nsz', we need fsub -0.0, X exactly.
1250 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1251 return false;
1252 }
1253
1254 return X.match(FPMO->getOperand(1));
1255 }
1256
1257 return false;
1258 }
1259};
1260
1261/// Match 'fneg X' as 'fsub -0.0, X'.
1262template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
1263 return FNeg_match<OpTy>(X);
1264}
1265
1266/// Match 'fneg X' as 'fsub +-0.0, X'.
1267template <typename RHS>
1268inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1269m_FNegNSZ(const RHS &X) {
1270 return m_FSub(m_AnyZeroFP(), X);
1271}
1272
1273template <typename LHS, typename RHS>
1278
1279template <typename LHS, typename RHS>
1284
1285template <typename LHS, typename RHS>
1290
1291template <typename LHS, typename RHS>
1296
1297template <typename LHS, typename RHS>
1302
1303template <typename LHS, typename RHS>
1308
1309template <typename LHS, typename RHS>
1314
1315template <typename LHS, typename RHS>
1320
1321template <typename LHS, typename RHS>
1326
1327template <typename LHS, typename RHS>
1332
1333template <typename LHS, typename RHS>
1338
1339template <typename LHS, typename RHS>
1344
1345template <typename LHS, typename RHS>
1350
1351template <typename LHS, typename RHS>
1356
1357template <typename LHS_t, unsigned Opcode> struct ShiftLike_match {
1360
1362
1363 template <typename OpTy> bool match(OpTy *V) const {
1364 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1365 if (Op->getOpcode() == Opcode)
1366 return m_ConstantInt(R).match(Op->getOperand(1)) &&
1367 L.match(Op->getOperand(0));
1368 }
1369 // Interpreted as shiftop V, 0
1370 R = 0;
1371 return L.match(V);
1372 }
1373};
1374
1375/// Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
1376template <typename LHS>
1381
1382/// Matches lshr L, ConstShAmt or L itself (R will be set to zero in this case).
1383template <typename LHS>
1388
1389/// Matches ashr L, ConstShAmt or L itself (R will be set to zero in this case).
1390template <typename LHS>
1395
1396template <typename LHS_t, typename RHS_t, unsigned Opcode,
1397 unsigned WrapFlags = 0, bool Commutable = false>
1401
1403 : L(LHS), R(RHS) {}
1404
1405 template <typename OpTy> bool match(OpTy *V) const {
1406 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1407 if (Op->getOpcode() != Opcode)
1408 return false;
1410 !Op->hasNoUnsignedWrap())
1411 return false;
1412 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
1413 !Op->hasNoSignedWrap())
1414 return false;
1415 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1416 (Commutable && L.match(Op->getOperand(1)) &&
1417 R.match(Op->getOperand(0)));
1418 }
1419 return false;
1420 }
1421};
1422
1423template <typename LHS, typename RHS>
1424inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1426m_NSWAdd(const LHS &L, const RHS &R) {
1427 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1429 R);
1430}
1431template <typename LHS, typename RHS>
1432inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1434m_c_NSWAdd(const LHS &L, const RHS &R) {
1435 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1437 true>(L, R);
1438}
1439template <typename LHS, typename RHS>
1440inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1442m_NSWSub(const LHS &L, const RHS &R) {
1443 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1445 R);
1446}
1447template <typename LHS, typename RHS>
1448inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1450m_NSWMul(const LHS &L, const RHS &R) {
1451 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1453 R);
1454}
1455template <typename LHS, typename RHS>
1456inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1458m_NSWShl(const LHS &L, const RHS &R) {
1459 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1461 R);
1462}
1463
1464template <typename LHS, typename RHS>
1465inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1467m_NUWAdd(const LHS &L, const RHS &R) {
1468 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1470 L, R);
1471}
1472
1473template <typename LHS, typename RHS>
1475 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true>
1476m_c_NUWAdd(const LHS &L, const RHS &R) {
1477 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1479 true>(L, R);
1480}
1481
1482template <typename LHS, typename RHS>
1483inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1485m_NUWSub(const LHS &L, const RHS &R) {
1486 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1488 L, R);
1489}
1490template <typename LHS, typename RHS>
1491inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1493m_NUWMul(const LHS &L, const RHS &R) {
1494 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1496 L, R);
1497}
1498template <typename LHS, typename RHS>
1499inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1501m_NUWShl(const LHS &L, const RHS &R) {
1502 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1504 L, R);
1505}
1506
1507template <typename LHS_t, typename RHS_t, bool Commutable = false>
1509 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
1510 unsigned Opcode;
1511
1513 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
1514
1515 template <typename OpTy> bool match(OpTy *V) const {
1517 }
1518};
1519
1520/// Matches a specific opcode.
1521template <typename LHS, typename RHS>
1522inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
1523 const RHS &R) {
1524 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
1525}
1526
1527template <typename LHS, typename RHS, bool Commutable = false>
1531
1532 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1533
1534 template <typename OpTy> bool match(OpTy *V) const {
1535 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1536 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint");
1537 if (!PDI->isDisjoint())
1538 return false;
1539 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) ||
1540 (Commutable && L.match(PDI->getOperand(1)) &&
1541 R.match(PDI->getOperand(0)));
1542 }
1543 return false;
1544 }
1545};
1546
1547template <typename LHS, typename RHS>
1549 return DisjointOr_match<LHS, RHS>(L, R);
1550}
1551
1552template <typename LHS, typename RHS>
1554 const RHS &R) {
1556}
1557
1558/// Match either "add" or "or disjoint".
1559template <typename LHS, typename RHS>
1562m_AddLike(const LHS &L, const RHS &R) {
1563 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R));
1564}
1565
1566/// Match either "add nsw" or "or disjoint"
1567template <typename LHS, typename RHS>
1568inline match_combine_or<
1569 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1572m_NSWAddLike(const LHS &L, const RHS &R) {
1573 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R));
1574}
1575
1576/// Match either "add nuw" or "or disjoint"
1577template <typename LHS, typename RHS>
1578inline match_combine_or<
1579 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1582m_NUWAddLike(const LHS &L, const RHS &R) {
1583 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
1584}
1585
1586template <typename LHS, typename RHS>
1590
1591 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
1592
1593 template <typename OpTy> bool match(OpTy *V) const {
1594 if (auto *Op = dyn_cast<BinaryOperator>(V)) {
1595 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
1596 PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
1597 ; // Pass
1598 else if (Op->getOpcode() != Instruction::Xor)
1599 return false;
1600 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
1601 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
1602 }
1603 return false;
1604 }
1605};
1606
1607/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
1608/// Only commutative matcher as the `sub` will need to swap the L and R.
1609template <typename LHS, typename RHS>
1610inline auto m_c_XorLike(const LHS &L, const RHS &R) {
1611 return XorLike_match<LHS, RHS>(L, R);
1612}
1613
1614//===----------------------------------------------------------------------===//
1615// Class that matches a group of binary opcodes.
1616//
1617template <typename LHS_t, typename RHS_t, typename Predicate,
1618 bool Commutable = false>
1619struct BinOpPred_match : Predicate {
1622
1623 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1624
1625 template <typename OpTy> bool match(OpTy *V) const {
1626 if (auto *I = dyn_cast<Instruction>(V))
1627 return this->isOpType(I->getOpcode()) &&
1628 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
1629 (Commutable && L.match(I->getOperand(1)) &&
1630 R.match(I->getOperand(0))));
1631 return false;
1632 }
1633};
1634
1636 bool isOpType(unsigned Opcode) const { return Instruction::isShift(Opcode); }
1637};
1638
1640 bool isOpType(unsigned Opcode) const {
1641 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1642 }
1643};
1644
1646 bool isOpType(unsigned Opcode) const {
1647 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1648 }
1649};
1650
1652 bool isOpType(unsigned Opcode) const {
1653 return Instruction::isBitwiseLogicOp(Opcode);
1654 }
1655};
1656
1658 bool isOpType(unsigned Opcode) const {
1659 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1660 }
1661};
1662
1664 bool isOpType(unsigned Opcode) const {
1665 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1666 }
1667};
1668
1669/// Matches shift operations.
1670template <typename LHS, typename RHS>
1672 const RHS &R) {
1674}
1675
1676/// Matches logical shift operations.
1677template <typename LHS, typename RHS>
1682
1683/// Matches logical shift operations.
1684template <typename LHS, typename RHS>
1686m_LogicalShift(const LHS &L, const RHS &R) {
1688}
1689
1690/// Matches bitwise logic operations.
1691template <typename LHS, typename RHS>
1693m_BitwiseLogic(const LHS &L, const RHS &R) {
1695}
1696
1697/// Matches bitwise logic operations in either order.
1698template <typename LHS, typename RHS>
1703
1704/// Matches integer division operations.
1705template <typename LHS, typename RHS>
1707 const RHS &R) {
1709}
1710
1711/// Matches integer remainder operations.
1712template <typename LHS, typename RHS>
1714 const RHS &R) {
1716}
1717
1718//===----------------------------------------------------------------------===//
1719// Class that matches exact binary ops.
1720//
1721template <typename SubPattern_t> struct Exact_match {
1722 SubPattern_t SubPattern;
1723
1724 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1725
1726 template <typename OpTy> bool match(OpTy *V) const {
1727 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1728 return PEO->isExact() && SubPattern.match(V);
1729 return false;
1730 }
1731};
1732
1733template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1734 return SubPattern;
1735}
1736
1737//===----------------------------------------------------------------------===//
1738// Matchers for CmpInst classes
1739//
1740
1741template <typename LHS_t, typename RHS_t, typename Class,
1742 bool Commutable = false>
1747
1748 // The evaluation order is always stable, regardless of Commutability.
1749 // The LHS is always matched first.
1751 : Predicate(&Pred), L(LHS), R(RHS) {}
1753 : Predicate(nullptr), L(LHS), R(RHS) {}
1754
1755 template <typename OpTy> bool match(OpTy *V) const {
1756 if (auto *I = dyn_cast<Class>(V)) {
1757 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1758 if (Predicate)
1760 return true;
1761 }
1762 if (Commutable && L.match(I->getOperand(1)) &&
1763 R.match(I->getOperand(0))) {
1764 if (Predicate)
1766 return true;
1767 }
1768 }
1769 return false;
1770 }
1771};
1772
1773template <typename LHS, typename RHS>
1775 const RHS &R) {
1776 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R);
1777}
1778
1779template <typename LHS, typename RHS>
1781 const LHS &L, const RHS &R) {
1782 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R);
1783}
1784
1785template <typename LHS, typename RHS>
1787 const LHS &L, const RHS &R) {
1788 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R);
1789}
1790
1791template <typename LHS, typename RHS>
1794}
1795
1796template <typename LHS, typename RHS>
1799}
1800
1801template <typename LHS, typename RHS>
1804}
1805
1806// Same as CmpClass, but instead of saving Pred as out output variable, match a
1807// specific input pred for equality.
1808template <typename LHS_t, typename RHS_t, typename Class,
1809 bool Commutable = false>
1814
1816 : Predicate(Pred), L(LHS), R(RHS) {}
1817
1818 template <typename OpTy> bool match(OpTy *V) const {
1819 if (auto *I = dyn_cast<Class>(V)) {
1821 L.match(I->getOperand(0)) && R.match(I->getOperand(1)))
1822 return true;
1823 if constexpr (Commutable) {
1826 L.match(I->getOperand(1)) && R.match(I->getOperand(0)))
1827 return true;
1828 }
1829 }
1830
1831 return false;
1832 }
1833};
1834
1835template <typename LHS, typename RHS>
1837m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1838 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R);
1839}
1840
1841template <typename LHS, typename RHS>
1843m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1844 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R);
1845}
1846
1847template <typename LHS, typename RHS>
1849m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1851}
1852
1853template <typename LHS, typename RHS>
1855m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) {
1856 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R);
1857}
1858
1859//===----------------------------------------------------------------------===//
1860// Matchers for instructions with a given opcode and number of operands.
1861//
1862
1863/// Matches instructions with Opcode and three operands.
1864template <typename T0, unsigned Opcode> struct OneOps_match {
1866
1867 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1868
1869 template <typename OpTy> bool match(OpTy *V) const {
1870 if (V->getValueID() == Value::InstructionVal + Opcode) {
1871 auto *I = cast<Instruction>(V);
1872 return Op1.match(I->getOperand(0));
1873 }
1874 return false;
1875 }
1876};
1877
1878/// Matches instructions with Opcode and three operands.
1879template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1882
1883 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1884
1885 template <typename OpTy> bool match(OpTy *V) const {
1886 if (V->getValueID() == Value::InstructionVal + Opcode) {
1887 auto *I = cast<Instruction>(V);
1888 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1889 }
1890 return false;
1891 }
1892};
1893
1894/// Matches instructions with Opcode and three operands.
1895template <typename T0, typename T1, typename T2, unsigned Opcode,
1896 bool CommutableOp2Op3 = false>
1901
1902 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1903 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1904
1905 template <typename OpTy> bool match(OpTy *V) const {
1906 if (V->getValueID() == Value::InstructionVal + Opcode) {
1907 auto *I = cast<Instruction>(V);
1908 if (!Op1.match(I->getOperand(0)))
1909 return false;
1910 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1911 return true;
1912 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1913 Op3.match(I->getOperand(1));
1914 }
1915 return false;
1916 }
1917};
1918
1919/// Matches instructions with Opcode and any number of operands
1920template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match {
1921 std::tuple<OperandTypes...> Operands;
1922
1923 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {}
1924
1925 // Operand matching works by recursively calling match_operands, matching the
1926 // operands left to right. The first version is called for each operand but
1927 // the last, for which the second version is called. The second version of
1928 // match_operands is also used to match each individual operand.
1929 template <int Idx, int Last>
1930 std::enable_if_t<Idx != Last, bool>
1934
1935 template <int Idx, int Last>
1936 std::enable_if_t<Idx == Last, bool>
1938 return std::get<Idx>(Operands).match(I->getOperand(Idx));
1939 }
1940
1941 template <typename OpTy> bool match(OpTy *V) const {
1942 if (V->getValueID() == Value::InstructionVal + Opcode) {
1943 auto *I = cast<Instruction>(V);
1944 return I->getNumOperands() == sizeof...(OperandTypes) &&
1945 match_operands<0, sizeof...(OperandTypes) - 1>(I);
1946 }
1947 return false;
1948 }
1949};
1950
1951/// Matches SelectInst.
1952template <typename Cond, typename LHS, typename RHS>
1954m_Select(const Cond &C, const LHS &L, const RHS &R) {
1956}
1957
1958/// This matches a select of two constants, e.g.:
1959/// m_SelectCst<-1, 0>(m_Value(V))
1960template <int64_t L, int64_t R, typename Cond>
1962 Instruction::Select>
1965}
1966
1967/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1968template <typename LHS, typename RHS>
1969inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1970m_c_Select(const LHS &L, const RHS &R) {
1971 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1972 true>(m_Value(), L, R);
1973}
1974
1975/// Matches FreezeInst.
1976template <typename OpTy>
1980
1981/// Matches InsertElementInst.
1982template <typename Val_t, typename Elt_t, typename Idx_t>
1984m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1986 Val, Elt, Idx);
1987}
1988
1989/// Matches ExtractElementInst.
1990template <typename Val_t, typename Idx_t>
1992m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1994}
1995
1996/// Matches shuffle.
1997template <typename T0, typename T1, typename T2> struct Shuffle_match {
2001
2002 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
2003 : Op1(Op1), Op2(Op2), Mask(Mask) {}
2004
2005 template <typename OpTy> bool match(OpTy *V) const {
2006 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
2007 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
2008 Mask.match(I->getShuffleMask());
2009 }
2010 return false;
2011 }
2012};
2013
2014struct m_Mask {
2017 bool match(ArrayRef<int> Mask) const {
2018 MaskRef = Mask;
2019 return true;
2020 }
2021};
2022
2024 bool match(ArrayRef<int> Mask) const {
2025 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
2026 }
2027};
2028
2032 bool match(ArrayRef<int> Mask) const { return Val == Mask; }
2033};
2034
2038 bool match(ArrayRef<int> Mask) const {
2039 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; });
2040 if (First == Mask.end())
2041 return false;
2042 SplatIndex = *First;
2043 return all_of(Mask,
2044 [First](int Elem) { return Elem == *First || Elem == -1; });
2045 }
2046};
2047
2048template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2049 PointerOpTy PointerOp;
2050 OffsetOpTy OffsetOp;
2051
2052 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2054
2055 template <typename OpTy> bool match(OpTy *V) const {
2056 auto *GEP = dyn_cast<GEPOperator>(V);
2057 return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2058 PointerOp.match(GEP->getPointerOperand()) &&
2059 OffsetOp.match(GEP->idx_begin()->get());
2060 }
2061};
2062
2063/// Matches ShuffleVectorInst independently of mask value.
2064template <typename V1_t, typename V2_t>
2066m_Shuffle(const V1_t &v1, const V2_t &v2) {
2068}
2069
2070template <typename V1_t, typename V2_t, typename Mask_t>
2072m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
2074}
2075
2076/// Matches LoadInst.
2077template <typename OpTy>
2081
2082/// Matches StoreInst.
2083template <typename ValueOpTy, typename PointerOpTy>
2085m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
2087 PointerOp);
2088}
2089
2090/// Matches GetElementPtrInst.
2091template <typename... OperandTypes>
2092inline auto m_GEP(const OperandTypes &...Ops) {
2093 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
2094}
2095
2096/// Matches GEP with i8 source element type
2097template <typename PointerOpTy, typename OffsetOpTy>
2099m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2101}
2102
2103//===----------------------------------------------------------------------===//
2104// Matchers for CastInst classes
2105//
2106
2107template <typename Op_t, unsigned Opcode> struct CastOperator_match {
2108 Op_t Op;
2109
2110 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {}
2111
2112 template <typename OpTy> bool match(OpTy *V) const {
2113 if (auto *O = dyn_cast<Operator>(V))
2114 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
2115 return false;
2116 }
2117};
2118
2119template <typename Op_t, typename Class> struct CastInst_match {
2120 Op_t Op;
2121
2122 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
2123
2124 template <typename OpTy> bool match(OpTy *V) const {
2125 if (auto *I = dyn_cast<Class>(V))
2126 return Op.match(I->getOperand(0));
2127 return false;
2128 }
2129};
2130
2131template <typename Op_t> struct PtrToIntSameSize_match {
2133 Op_t Op;
2134
2135 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
2136 : DL(DL), Op(OpMatch) {}
2137
2138 template <typename OpTy> bool match(OpTy *V) const {
2139 if (auto *O = dyn_cast<Operator>(V))
2140 return O->getOpcode() == Instruction::PtrToInt &&
2141 DL.getTypeSizeInBits(O->getType()) ==
2142 DL.getTypeSizeInBits(O->getOperand(0)->getType()) &&
2143 Op.match(O->getOperand(0));
2144 return false;
2145 }
2146};
2147
2148template <typename Op_t> struct NNegZExt_match {
2149 Op_t Op;
2150
2151 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
2152
2153 template <typename OpTy> bool match(OpTy *V) const {
2154 if (auto *I = dyn_cast<ZExtInst>(V))
2155 return I->hasNonNeg() && Op.match(I->getOperand(0));
2156 return false;
2157 }
2158};
2159
2160template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match {
2161 Op_t Op;
2162
2163 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {}
2164
2165 template <typename OpTy> bool match(OpTy *V) const {
2166 if (auto *I = dyn_cast<TruncInst>(V))
2167 return (I->getNoWrapKind() & WrapFlags) == WrapFlags &&
2168 Op.match(I->getOperand(0));
2169 return false;
2170 }
2171};
2172
2173/// Matches BitCast.
2174template <typename OpTy>
2179
2180template <typename Op_t> struct ElementWiseBitCast_match {
2181 Op_t Op;
2182
2183 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
2184
2185 template <typename OpTy> bool match(OpTy *V) const {
2186 auto *I = dyn_cast<BitCastInst>(V);
2187 if (!I)
2188 return false;
2189 Type *SrcType = I->getSrcTy();
2190 Type *DstType = I->getType();
2191 // Make sure the bitcast doesn't change between scalar and vector and
2192 // doesn't change the number of vector elements.
2193 if (SrcType->isVectorTy() != DstType->isVectorTy())
2194 return false;
2195 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType);
2196 SrcVecTy && SrcVecTy->getElementCount() !=
2197 cast<VectorType>(DstType)->getElementCount())
2198 return false;
2199 return Op.match(I->getOperand(0));
2200 }
2201};
2202
2203template <typename OpTy>
2207
2208/// Matches PtrToInt.
2209template <typename OpTy>
2214
2215template <typename OpTy>
2220
2221/// Matches PtrToAddr.
2222template <typename OpTy>
2227
2228/// Matches PtrToInt or PtrToAddr.
2229template <typename OpTy> inline auto m_PtrToIntOrAddr(const OpTy &Op) {
2231}
2232
2233/// Matches IntToPtr.
2234template <typename OpTy>
2239
2240/// Matches any cast or self. Used to ignore casts.
2241template <typename OpTy>
2243m_CastOrSelf(const OpTy &Op) {
2245}
2246
2247/// Matches Trunc.
2248template <typename OpTy>
2252
2253/// Matches trunc nuw.
2254template <typename OpTy>
2259
2260/// Matches trunc nsw.
2261template <typename OpTy>
2266
2267template <typename OpTy>
2269m_TruncOrSelf(const OpTy &Op) {
2270 return m_CombineOr(m_Trunc(Op), Op);
2271}
2272
2273/// Matches SExt.
2274template <typename OpTy>
2278
2279/// Matches ZExt.
2280template <typename OpTy>
2284
2285template <typename OpTy>
2287 return NNegZExt_match<OpTy>(Op);
2288}
2289
2290template <typename OpTy>
2292m_ZExtOrSelf(const OpTy &Op) {
2293 return m_CombineOr(m_ZExt(Op), Op);
2294}
2295
2296template <typename OpTy>
2298m_SExtOrSelf(const OpTy &Op) {
2299 return m_CombineOr(m_SExt(Op), Op);
2300}
2301
2302/// Match either "sext" or "zext nneg".
2303template <typename OpTy>
2305m_SExtLike(const OpTy &Op) {
2306 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
2307}
2308
2309template <typename OpTy>
2312m_ZExtOrSExt(const OpTy &Op) {
2313 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
2314}
2315
2316template <typename OpTy>
2319 OpTy>
2321 return m_CombineOr(m_ZExtOrSExt(Op), Op);
2322}
2323
2324template <typename OpTy>
2327 OpTy>
2330}
2331
2332template <typename OpTy>
2336
2337template <typename OpTy>
2341
2342template <typename OpTy>
2346
2347template <typename OpTy>
2351
2352template <typename OpTy>
2356
2357template <typename OpTy>
2361
2362//===----------------------------------------------------------------------===//
2363// Matchers for control flow.
2364//
2365
2366struct br_match {
2368
2370
2371 template <typename OpTy> bool match(OpTy *V) const {
2372 if (auto *BI = dyn_cast<BranchInst>(V))
2373 if (BI->isUnconditional()) {
2374 Succ = BI->getSuccessor(0);
2375 return true;
2376 }
2377 return false;
2378 }
2379};
2380
2381inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
2382
2383template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2385 Cond_t Cond;
2386 TrueBlock_t T;
2387 FalseBlock_t F;
2388
2389 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
2390 : Cond(C), T(t), F(f) {}
2391
2392 template <typename OpTy> bool match(OpTy *V) const {
2393 if (auto *BI = dyn_cast<BranchInst>(V))
2394 if (BI->isConditional() && Cond.match(BI->getCondition()))
2395 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
2396 return false;
2397 }
2398};
2399
2400template <typename Cond_t>
2406
2407template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
2409m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
2411}
2412
2413//===----------------------------------------------------------------------===//
2414// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
2415//
2416
2417template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
2418 bool Commutable = false>
2420 using PredType = Pred_t;
2423
2424 // The evaluation order is always stable, regardless of Commutability.
2425 // The LHS is always matched first.
2426 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
2427
2428 template <typename OpTy> bool match(OpTy *V) const {
2429 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
2430 Intrinsic::ID IID = II->getIntrinsicID();
2431 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
2432 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
2433 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
2434 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
2435 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
2436 return (L.match(LHS) && R.match(RHS)) ||
2437 (Commutable && L.match(RHS) && R.match(LHS));
2438 }
2439 }
2440 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
2441 auto *SI = dyn_cast<SelectInst>(V);
2442 if (!SI)
2443 return false;
2444 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
2445 if (!Cmp)
2446 return false;
2447 // At this point we have a select conditioned on a comparison. Check that
2448 // it is the values returned by the select that are being compared.
2449 auto *TrueVal = SI->getTrueValue();
2450 auto *FalseVal = SI->getFalseValue();
2451 auto *LHS = Cmp->getOperand(0);
2452 auto *RHS = Cmp->getOperand(1);
2453 if ((TrueVal != LHS || FalseVal != RHS) &&
2454 (TrueVal != RHS || FalseVal != LHS))
2455 return false;
2456 typename CmpInst_t::Predicate Pred =
2457 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
2458 // Does "(x pred y) ? x : y" represent the desired max/min operation?
2459 if (!Pred_t::match(Pred))
2460 return false;
2461 // It does! Bind the operands.
2462 return (L.match(LHS) && R.match(RHS)) ||
2463 (Commutable && L.match(RHS) && R.match(LHS));
2464 }
2465};
2466
2467/// Helper class for identifying signed max predicates.
2469 static bool match(ICmpInst::Predicate Pred) {
2470 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
2471 }
2472};
2473
2474/// Helper class for identifying signed min predicates.
2476 static bool match(ICmpInst::Predicate Pred) {
2477 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
2478 }
2479};
2480
2481/// Helper class for identifying unsigned max predicates.
2483 static bool match(ICmpInst::Predicate Pred) {
2484 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
2485 }
2486};
2487
2488/// Helper class for identifying unsigned min predicates.
2490 static bool match(ICmpInst::Predicate Pred) {
2491 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
2492 }
2493};
2494
2495/// Helper class for identifying ordered max predicates.
2497 static bool match(FCmpInst::Predicate Pred) {
2498 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
2499 }
2500};
2501
2502/// Helper class for identifying ordered min predicates.
2504 static bool match(FCmpInst::Predicate Pred) {
2505 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
2506 }
2507};
2508
2509/// Helper class for identifying unordered max predicates.
2511 static bool match(FCmpInst::Predicate Pred) {
2512 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
2513 }
2514};
2515
2516/// Helper class for identifying unordered min predicates.
2518 static bool match(FCmpInst::Predicate Pred) {
2519 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
2520 }
2521};
2522
2523template <typename LHS, typename RHS>
2528
2529template <typename LHS, typename RHS>
2534
2535template <typename LHS, typename RHS>
2540
2541template <typename LHS, typename RHS>
2546
2547template <typename LHS, typename RHS>
2548inline match_combine_or<
2553m_MaxOrMin(const LHS &L, const RHS &R) {
2554 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
2555 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
2556}
2557
2558/// Match an 'ordered' floating point maximum function.
2559/// Floating point has one special value 'NaN'. Therefore, there is no total
2560/// order. However, if we can ignore the 'NaN' value (for example, because of a
2561/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2562/// semantics. In the presence of 'NaN' we have to preserve the original
2563/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
2564///
2565/// max(L, R) iff L and R are not NaN
2566/// m_OrdFMax(L, R) = R iff L or R are NaN
2567template <typename LHS, typename RHS>
2572
2573/// Match an 'ordered' floating point minimum function.
2574/// Floating point has one special value 'NaN'. Therefore, there is no total
2575/// order. However, if we can ignore the 'NaN' value (for example, because of a
2576/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2577/// semantics. In the presence of 'NaN' we have to preserve the original
2578/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
2579///
2580/// min(L, R) iff L and R are not NaN
2581/// m_OrdFMin(L, R) = R iff L or R are NaN
2582template <typename LHS, typename RHS>
2587
2588/// Match an 'unordered' floating point maximum function.
2589/// Floating point has one special value 'NaN'. Therefore, there is no total
2590/// order. However, if we can ignore the 'NaN' value (for example, because of a
2591/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2592/// semantics. In the presence of 'NaN' we have to preserve the original
2593/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
2594///
2595/// max(L, R) iff L and R are not NaN
2596/// m_UnordFMax(L, R) = L iff L or R are NaN
2597template <typename LHS, typename RHS>
2599m_UnordFMax(const LHS &L, const RHS &R) {
2601}
2602
2603/// Match an 'unordered' floating point minimum function.
2604/// Floating point has one special value 'NaN'. Therefore, there is no total
2605/// order. However, if we can ignore the 'NaN' value (for example, because of a
2606/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2607/// semantics. In the presence of 'NaN' we have to preserve the original
2608/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
2609///
2610/// min(L, R) iff L and R are not NaN
2611/// m_UnordFMin(L, R) = L iff L or R are NaN
2612template <typename LHS, typename RHS>
2614m_UnordFMin(const LHS &L, const RHS &R) {
2616}
2617
2618/// Match an 'ordered' or 'unordered' floating point maximum function.
2619/// Floating point has one special value 'NaN'. Therefore, there is no total
2620/// order. However, if we can ignore the 'NaN' value (for example, because of a
2621/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
2622/// semantics.
2623template <typename LHS, typename RHS>
2630
2631/// Match an 'ordered' or 'unordered' floating point minimum function.
2632/// Floating point has one special value 'NaN'. Therefore, there is no total
2633/// order. However, if we can ignore the 'NaN' value (for example, because of a
2634/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
2635/// semantics.
2636template <typename LHS, typename RHS>
2643
2644/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2645/// NOTE: we first match the 'Not' (by matching '-1'),
2646/// and only then match the inner matcher!
2647template <typename ValTy>
2648inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
2649m_Not(const ValTy &V) {
2650 return m_c_Xor(m_AllOnes(), V);
2651}
2652
2653template <typename ValTy>
2654inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor,
2655 true>
2656m_NotForbidPoison(const ValTy &V) {
2657 return m_c_Xor(m_AllOnesForbidPoison(), V);
2658}
2659
2660//===----------------------------------------------------------------------===//
2661// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
2662// Note that S might be matched to other instructions than AddInst.
2663//
2664
2665template <typename LHS_t, typename RHS_t, typename Sum_t>
2669 Sum_t S;
2670
2671 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
2672 : L(L), R(R), S(S) {}
2673
2674 template <typename OpTy> bool match(OpTy *V) const {
2675 Value *ICmpLHS, *ICmpRHS;
2676 CmpPredicate Pred;
2677 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
2678 return false;
2679
2680 Value *AddLHS, *AddRHS;
2681 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
2682
2683 // (a + b) u< a, (a + b) u< b
2684 if (Pred == ICmpInst::ICMP_ULT)
2685 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
2686 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2687
2688 // a >u (a + b), b >u (a + b)
2689 if (Pred == ICmpInst::ICMP_UGT)
2690 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
2691 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2692
2693 Value *Op1;
2694 auto XorExpr = m_OneUse(m_Not(m_Value(Op1)));
2695 // (~a) <u b
2696 if (Pred == ICmpInst::ICMP_ULT) {
2697 if (XorExpr.match(ICmpLHS))
2698 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
2699 }
2700 // b > u (~a)
2701 if (Pred == ICmpInst::ICMP_UGT) {
2702 if (XorExpr.match(ICmpRHS))
2703 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2704 }
2705
2706 // Match special-case for increment-by-1.
2707 if (Pred == ICmpInst::ICMP_EQ) {
2708 // (a + 1) == 0
2709 // (1 + a) == 0
2710 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2711 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2712 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2713 // 0 == (a + 1)
2714 // 0 == (1 + a)
2715 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2716 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2717 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2718 }
2719
2720 return false;
2721 }
2722};
2723
2724/// Match an icmp instruction checking for unsigned overflow on addition.
2725///
2726/// S is matched to the addition whose result is being checked for overflow, and
2727/// L and R are matched to the LHS and RHS of S.
2728template <typename LHS_t, typename RHS_t, typename Sum_t>
2730m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2732}
2733
2734template <typename Opnd_t> struct Argument_match {
2735 unsigned OpI;
2736 Opnd_t Val;
2737
2738 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2739
2740 template <typename OpTy> bool match(OpTy *V) const {
2741 // FIXME: Should likely be switched to use `CallBase`.
2742 if (const auto *CI = dyn_cast<CallInst>(V))
2743 return Val.match(CI->getArgOperand(OpI));
2744 return false;
2745 }
2746};
2747
2748/// Match an argument.
2749template <unsigned OpI, typename Opnd_t>
2750inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2751 return Argument_match<Opnd_t>(OpI, Op);
2752}
2753
2754/// Intrinsic matchers.
2756 unsigned ID;
2757
2759
2760 template <typename OpTy> bool match(OpTy *V) const {
2761 if (const auto *CI = dyn_cast<CallInst>(V))
2762 if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
2763 return F->getIntrinsicID() == ID;
2764 return false;
2765 }
2766};
2767
2768/// Intrinsic matches are combinations of ID matchers, and argument
2769/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2770/// them with lower arity matchers. Here's some convenient typedefs for up to
2771/// several arguments, and more can be added as needed
2772template <typename T0 = void, typename T1 = void, typename T2 = void,
2773 typename T3 = void, typename T4 = void, typename T5 = void,
2774 typename T6 = void, typename T7 = void, typename T8 = void,
2775 typename T9 = void, typename T10 = void>
2777template <typename T0> struct m_Intrinsic_Ty<T0> {
2779};
2780template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2781 using Ty =
2783};
2784template <typename T0, typename T1, typename T2>
2789template <typename T0, typename T1, typename T2, typename T3>
2794
2795template <typename T0, typename T1, typename T2, typename T3, typename T4>
2800
2801template <typename T0, typename T1, typename T2, typename T3, typename T4,
2802 typename T5>
2807
2808/// Match intrinsic calls like this:
2809/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2810template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2811 return IntrinsicID_match(IntrID);
2812}
2813
2814/// Matches MaskedLoad Intrinsic.
2815template <typename Opnd0, typename Opnd1, typename Opnd2>
2817m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2818 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2);
2819}
2820
2821/// Matches MaskedStore Intrinsic.
2822template <typename Opnd0, typename Opnd1, typename Opnd2>
2824m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2825 return m_Intrinsic<Intrinsic::masked_store>(Op0, Op1, Op2);
2826}
2827
2828/// Matches MaskedGather Intrinsic.
2829template <typename Opnd0, typename Opnd1, typename Opnd2>
2831m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2832 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2);
2833}
2834
2835template <Intrinsic::ID IntrID, typename T0>
2836inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2838}
2839
2840template <Intrinsic::ID IntrID, typename T0, typename T1>
2841inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2842 const T1 &Op1) {
2844}
2845
2846template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2847inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2848m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2849 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2850}
2851
2852template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2853 typename T3>
2855m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2856 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2857}
2858
2859template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2860 typename T3, typename T4>
2862m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2863 const T4 &Op4) {
2864 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2865 m_Argument<4>(Op4));
2866}
2867
2868template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2869 typename T3, typename T4, typename T5>
2871m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2872 const T4 &Op4, const T5 &Op5) {
2873 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2874 m_Argument<5>(Op5));
2875}
2876
2877// Helper intrinsic matching specializations.
2878template <typename Opnd0>
2879inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2881}
2882
2883template <typename Opnd0>
2884inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2886}
2887
2888template <typename Opnd0>
2889inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2890 return m_Intrinsic<Intrinsic::fabs>(Op0);
2891}
2892
2893template <typename Opnd0>
2894inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2896}
2897
2898template <typename Opnd0, typename Opnd1>
2899inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinNum(const Opnd0 &Op0,
2900 const Opnd1 &Op1) {
2901 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2902}
2903
2904template <typename Opnd0, typename Opnd1>
2905inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMinimum(const Opnd0 &Op0,
2906 const Opnd1 &Op1) {
2907 return m_Intrinsic<Intrinsic::minimum>(Op0, Op1);
2908}
2909
2910template <typename Opnd0, typename Opnd1>
2912m_FMinimumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2913 return m_Intrinsic<Intrinsic::minimumnum>(Op0, Op1);
2914}
2915
2916template <typename Opnd0, typename Opnd1>
2917inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaxNum(const Opnd0 &Op0,
2918 const Opnd1 &Op1) {
2919 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2920}
2921
2922template <typename Opnd0, typename Opnd1>
2923inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMaximum(const Opnd0 &Op0,
2924 const Opnd1 &Op1) {
2925 return m_Intrinsic<Intrinsic::maximum>(Op0, Op1);
2926}
2927
2928template <typename Opnd0, typename Opnd1>
2930m_FMaximumNum(const Opnd0 &Op0, const Opnd1 &Op1) {
2931 return m_Intrinsic<Intrinsic::maximumnum>(Op0, Op1);
2932}
2933
2934template <typename Opnd0, typename Opnd1, typename Opnd2>
2936m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2937 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2938}
2939
2940template <typename Opnd0, typename Opnd1, typename Opnd2>
2942m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2943 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2944}
2945
2946template <typename Opnd0>
2947inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
2948 return m_Intrinsic<Intrinsic::sqrt>(Op0);
2949}
2950
2951template <typename Opnd0, typename Opnd1>
2952inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
2953 const Opnd1 &Op1) {
2954 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
2955}
2956
2957template <typename Opnd0>
2958inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
2960}
2961
2962template <typename Opnd0, typename Opnd1, typename Opnd2>
2964m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2965 return m_Intrinsic<Intrinsic::vector_insert>(Op0, Op1, Op2);
2966}
2967
2968//===----------------------------------------------------------------------===//
2969// Matchers for two-operands operators with the operators in either order
2970//
2971
2972/// Matches a BinaryOperator with LHS and RHS in either order.
2973template <typename LHS, typename RHS>
2976}
2977
2978/// Matches an ICmp with a predicate over LHS and RHS in either order.
2979/// Swaps the predicate if operands are commuted.
2980template <typename LHS, typename RHS>
2982m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) {
2984}
2985
2986template <typename LHS, typename RHS>
2991
2992/// Matches a specific opcode with LHS and RHS in either order.
2993template <typename LHS, typename RHS>
2995m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
2996 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
2997}
2998
2999/// Matches a Add with LHS and RHS in either order.
3000template <typename LHS, typename RHS>
3005
3006/// Matches a Mul with LHS and RHS in either order.
3007template <typename LHS, typename RHS>
3012
3013/// Matches an And with LHS and RHS in either order.
3014template <typename LHS, typename RHS>
3019
3020/// Matches an Or with LHS and RHS in either order.
3021template <typename LHS, typename RHS>
3026
3027/// Matches an Xor with LHS and RHS in either order.
3028template <typename LHS, typename RHS>
3033
3034/// Matches a 'Neg' as 'sub 0, V'.
3035template <typename ValTy>
3036inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
3037m_Neg(const ValTy &V) {
3038 return m_Sub(m_ZeroInt(), V);
3039}
3040
3041/// Matches a 'Neg' as 'sub nsw 0, V'.
3042template <typename ValTy>
3044 Instruction::Sub,
3046m_NSWNeg(const ValTy &V) {
3047 return m_NSWSub(m_ZeroInt(), V);
3048}
3049
3050/// Matches an SMin with LHS and RHS in either order.
3051template <typename LHS, typename RHS>
3053m_c_SMin(const LHS &L, const RHS &R) {
3055}
3056/// Matches an SMax with LHS and RHS in either order.
3057template <typename LHS, typename RHS>
3059m_c_SMax(const LHS &L, const RHS &R) {
3061}
3062/// Matches a UMin with LHS and RHS in either order.
3063template <typename LHS, typename RHS>
3065m_c_UMin(const LHS &L, const RHS &R) {
3067}
3068/// Matches a UMax with LHS and RHS in either order.
3069template <typename LHS, typename RHS>
3071m_c_UMax(const LHS &L, const RHS &R) {
3073}
3074
3075template <typename LHS, typename RHS>
3076inline match_combine_or<
3081m_c_MaxOrMin(const LHS &L, const RHS &R) {
3082 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
3083 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
3084}
3085
3086template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3090
3091 CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3092
3093 template <typename OpTy> bool match(OpTy *V) const {
3094 const auto *II = dyn_cast<IntrinsicInst>(V);
3095 if (!II || II->getIntrinsicID() != IntrID)
3096 return false;
3097 return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3098 (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3099 }
3100};
3101
3102template <Intrinsic::ID IntrID, typename T0, typename T1>
3104m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3106}
3107
3108/// Matches FAdd with LHS and RHS in either order.
3109template <typename LHS, typename RHS>
3111m_c_FAdd(const LHS &L, const RHS &R) {
3113}
3114
3115/// Matches FMul with LHS and RHS in either order.
3116template <typename LHS, typename RHS>
3118m_c_FMul(const LHS &L, const RHS &R) {
3120}
3121
3122template <typename Opnd_t> struct Signum_match {
3123 Opnd_t Val;
3124 Signum_match(const Opnd_t &V) : Val(V) {}
3125
3126 template <typename OpTy> bool match(OpTy *V) const {
3127 unsigned TypeSize = V->getType()->getScalarSizeInBits();
3128 if (TypeSize == 0)
3129 return false;
3130
3131 unsigned ShiftWidth = TypeSize - 1;
3132 Value *Op;
3133
3134 // This is the representation of signum we match:
3135 //
3136 // signum(x) == (x >> 63) | (-x >>u 63)
3137 //
3138 // An i1 value is its own signum, so it's correct to match
3139 //
3140 // signum(x) == (x >> 0) | (-x >>u 0)
3141 //
3142 // for i1 values.
3143
3144 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
3145 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
3146 auto Signum = m_c_Or(LHS, RHS);
3147
3148 return Signum.match(V) && Val.match(Op);
3149 }
3150};
3151
3152/// Matches a signum pattern.
3153///
3154/// signum(x) =
3155/// x > 0 -> 1
3156/// x == 0 -> 0
3157/// x < 0 -> -1
3158template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
3159 return Signum_match<Val_t>(V);
3160}
3161
3162template <int Ind, typename Opnd_t> struct ExtractValue_match {
3163 Opnd_t Val;
3164 ExtractValue_match(const Opnd_t &V) : Val(V) {}
3165
3166 template <typename OpTy> bool match(OpTy *V) const {
3167 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
3168 // If Ind is -1, don't inspect indices
3169 if (Ind != -1 &&
3170 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
3171 return false;
3172 return Val.match(I->getAggregateOperand());
3173 }
3174 return false;
3175 }
3176};
3177
3178/// Match a single index ExtractValue instruction.
3179/// For example m_ExtractValue<1>(...)
3180template <int Ind, typename Val_t>
3184
3185/// Match an ExtractValue instruction with any index.
3186/// For example m_ExtractValue(...)
3187template <typename Val_t>
3188inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
3189 return ExtractValue_match<-1, Val_t>(V);
3190}
3191
3192/// Matcher for a single index InsertValue instruction.
3193template <int Ind, typename T0, typename T1> struct InsertValue_match {
3196
3197 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
3198
3199 template <typename OpTy> bool match(OpTy *V) const {
3200 if (auto *I = dyn_cast<InsertValueInst>(V)) {
3201 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
3202 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
3203 }
3204 return false;
3205 }
3206};
3207
3208/// Matches a single index InsertValue instruction.
3209template <int Ind, typename Val_t, typename Elt_t>
3211 const Elt_t &Elt) {
3212 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
3213}
3214
3215/// Matches a call to `llvm.vscale()`.
3217
3218template <typename Opnd0, typename Opnd1>
3220m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) {
3222}
3223
3224template <typename Opnd>
3228
3229template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
3233
3234 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3235
3236 template <typename T> bool match(T *V) const {
3237 auto *I = dyn_cast<Instruction>(V);
3238 if (!I || !I->getType()->isIntOrIntVectorTy(1))
3239 return false;
3240
3241 if (I->getOpcode() == Opcode) {
3242 auto *Op0 = I->getOperand(0);
3243 auto *Op1 = I->getOperand(1);
3244 return (L.match(Op0) && R.match(Op1)) ||
3245 (Commutable && L.match(Op1) && R.match(Op0));
3246 }
3247
3248 if (auto *Select = dyn_cast<SelectInst>(I)) {
3249 auto *Cond = Select->getCondition();
3250 auto *TVal = Select->getTrueValue();
3251 auto *FVal = Select->getFalseValue();
3252
3253 // Don't match a scalar select of bool vectors.
3254 // Transforms expect a single type for operands if this matches.
3255 if (Cond->getType() != Select->getType())
3256 return false;
3257
3258 if (Opcode == Instruction::And) {
3259 auto *C = dyn_cast<Constant>(FVal);
3260 if (C && C->isNullValue())
3261 return (L.match(Cond) && R.match(TVal)) ||
3262 (Commutable && L.match(TVal) && R.match(Cond));
3263 } else {
3264 assert(Opcode == Instruction::Or);
3265 auto *C = dyn_cast<Constant>(TVal);
3266 if (C && C->isOneValue())
3267 return (L.match(Cond) && R.match(FVal)) ||
3268 (Commutable && L.match(FVal) && R.match(Cond));
3269 }
3270 }
3271
3272 return false;
3273 }
3274};
3275
3276/// Matches L && R either in the form of L & R or L ? R : false.
3277/// Note that the latter form is poison-blocking.
3278template <typename LHS, typename RHS>
3283
3284/// Matches L && R where L and R are arbitrary values.
3285inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
3286
3287/// Matches L && R with LHS and RHS in either order.
3288template <typename LHS, typename RHS>
3290m_c_LogicalAnd(const LHS &L, const RHS &R) {
3292}
3293
3294/// Matches L || R either in the form of L | R or L ? true : R.
3295/// Note that the latter form is poison-blocking.
3296template <typename LHS, typename RHS>
3301
3302/// Matches L || R where L and R are arbitrary values.
3303inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
3304
3305/// Matches L || R with LHS and RHS in either order.
3306template <typename LHS, typename RHS>
3308m_c_LogicalOr(const LHS &L, const RHS &R) {
3310}
3311
3312/// Matches either L && R or L || R,
3313/// either one being in the either binary or logical form.
3314/// Note that the latter form is poison-blocking.
3315template <typename LHS, typename RHS, bool Commutable = false>
3321
3322/// Matches either L && R or L || R where L and R are arbitrary values.
3323inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
3324
3325/// Matches either L && R or L || R with LHS and RHS in either order.
3326template <typename LHS, typename RHS>
3327inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
3328 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
3329}
3330
3331} // end namespace PatternMatch
3332} // end namespace llvm
3333
3334#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
static constexpr unsigned long long mask(BlockVerifier::State S)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp: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:1541
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1513
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:554
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:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI CmpPredicate get(const CmpInst *Cmp)
Do a ICmpInst::getCmpPredicate() or CmpInst::getPredicate(), as appropriate.
static LLVM_ABI CmpPredicate getSwapped(CmpPredicate P)
Get the swapped predicate of a CmpPredicate.
Base class for aggregate constants (with operands).
Definition Constants.h: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)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, true > m_c_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, CastInst >, OpTy > m_CastOrSelf(const OpTy &Op)
Matches any cast or self. Used to ignore casts.
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
CommutativeBinaryIntrinsic_match< IntrID, T0, T1 > m_c_Intrinsic(const T0 &Op0, const T1 &Op1)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
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)
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
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'.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R, either one being in the either binary or logical form.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinimum(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
InsertValue_match< Ind, Val_t, Elt_t > m_InsertValue(const Val_t &Val, const Elt_t &Elt)
Matches a single index InsertValue instruction.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1)
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaximum(const Opnd0 &Op0, const Opnd1 &Op1)
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
ShiftLike_match< LHS, Instruction::Shl > m_ShlOrSelf(const LHS &L, uint64_t &R)
Matches shl L, ConstShAmt or L itself (R will be set to zero in this case).
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
match_combine_or< match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > >, OpTy > m_ZExtOrSExtOrSelf(const OpTy &Op)
OneUse_match< T > m_OneUse(const T &SubPattern)
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
Splat_match< T > m_ConstantSplat(const T &SubPattern)
Match a constant splat. TODO: Extend this to non-constant splats.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
ap_match< APInt > m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
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.
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".
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedGather Intrinsic.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
cstval_pred_ty< Predicate, ConstantFP, true > cstfp_pred_ty
specialization of cstval_pred_ty for ConstantFP
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
cstfp_pred_ty< is_finitenonzero > m_FiniteNonZero()
Match a finite non-zero FP constant.
class_match< UnaryOperator > m_UnOp()
Match an arbitrary unary operation and ignore it.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Signum_match< Val_t > m_Signum(const Val_t &V)
Matches a signum pattern.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Argument_match< Opnd_t > m_Argument(const Opnd_t &Op)
Match an argument.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
ThreeOps_match< Cond, constantint_match< L >, constantint_match< R >, Instruction::Select > m_SelectCst(const Cond &C)
This matches a select of two constants, e.g.: m_SelectCst<-1, 0>(m_Value(V))
BinaryOp_match< LHS, RHS, Instruction::FRem > m_FRem(const LHS &L, const RHS &R)
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.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
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)
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:1725
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1758
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)
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
DisjointOr_match(const LHS &L, const RHS &R)
Exact_match(const SubPattern_t &SP)
Matcher for a single index InsertValue instruction.
InsertValue_match(const T0 &Op0, const T1 &Op1)
IntrinsicID_match(Intrinsic::ID IntrID)
LogicalOp_match(const LHS &L, const RHS &R)
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
NNegZExt_match(const Op_t &OpMatch)
Matches instructions with Opcode and three operands.
OneUse_match(const SubPattern_t &SP)
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch)
ShiftLike_match(const LHS_t &LHS, uint64_t &RHS)
Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS)
Splat_match(const SubPattern_t &SP)
Matches instructions with Opcode and three operands.
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
Matches instructions with Opcode and three operands.
TwoOps_match(const T0 &Op1, const T1 &Op2)
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
XorLike_match(const LHS &L, const RHS &R)
ap_match(const APTy *&Res, bool AllowPoison)
std::conditional_t< std::is_same_v< APTy, APInt >, ConstantInt, ConstantFP > ConstantTy
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
This helper class is used to match scalar and vector constants that satisfy a specified predicate,...
Check whether the value has the given Class and matches the nested pattern.
bind_and_match_ty(Class *&V, const MatchTy &Match)
bool match(ITy *V) const
bool match(OpTy *V) const
br_match(BasicBlock *&Succ)
brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
This helper class is used to match constant scalars, vector splats, and fixed width vectors that sati...
bool isValue(const APTy &C) const
function_ref< bool(const APTy &)> CheckFn
Stores a reference to the Value *, not the Value * itself, thus can be used in commutative matchers.
bool match(ITy *const V) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isValue(const APFloat &C) const
bool isOpType(unsigned Opcode) const
bool isOpType(unsigned Opcode) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const APFloat &C) const
bool isValue(const APInt &C) const
bool isValue(const APInt &C) const
bool isValue(const APFloat &C) const
bool isValue(const 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)