LLVM 23.0.0git
PatternMatchHelpers.h
Go to the documentation of this file.
1//===- PatternMatchHelpers.h - Helpers for PatternMatch -------------------===//
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 helpers that are used across the IR PatternMatch,
10// ScalarEvolutionPatternMatch, and VPlanPatternMatch.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_PATTERNMATCHHELPERS_H
15#define LLVM_SUPPORT_PATTERNMATCHHELPERS_H
16
18
20/// Matching or combinator leaf case.
21template <typename... Tys> struct match_combine_or { // NOLINT
22 template <typename ITy> bool match(ITy *) const { return false; }
23};
24
25/// Matching or combinator.
26template <typename Ty, typename... Tys>
27struct match_combine_or<Ty, Tys...> : match_combine_or<Tys...> {
28 Ty P;
29 match_combine_or(const Ty &P, const Tys &...Ps)
30 : match_combine_or<Tys...>(Ps...), P(P) {}
31
32 template <typename ITy> bool match(ITy *V) const {
33 return P.match(V) || match_combine_or<Tys...>::match(V);
34 }
35};
36
37/// Matching and combinator leaf case.
38template <typename... Tys> struct match_combine_and { // NOLINT
39 template <typename ITy> bool match(ITy *) const { return true; }
40};
41
42/// Matching and combinator.
43template <typename Ty, typename... Tys>
44struct match_combine_and<Ty, Tys...> : match_combine_and<Tys...> {
45 Ty P;
46 match_combine_and(const Ty &P, const Tys &...Ps)
47 : match_combine_and<Tys...>(Ps...), P(P) {}
48
49 template <typename ITy> bool match(ITy *V) const {
50 return P.match(V) && match_combine_and<Tys...>::match(V);
51 }
52};
53
54/// Combine pattern matchers matching any of Ps patterns.
55template <typename... Ty>
56inline match_combine_or<Ty...> m_CombineOr(const Ty &...Ps) { // NOLINT
57 return {Ps...};
58}
59
60/// Combine pattern matchers matching all of Ps patterns.
61template <typename... Ty>
62inline match_combine_and<Ty...> m_CombineAnd(const Ty &...Ps) { // NOLINT
63 return {Ps...};
64}
65
66/// A match-wrapper around isa.
67template <typename... To> struct match_isa { // NOLINT
68 template <typename ArgTy> bool match(const ArgTy *V) const {
69 return isa<To...>(V);
70 }
71};
72
73template <typename... To> inline match_isa<To...> m_Isa() { // NOLINT
74 return match_isa<To...>();
75}
76
77/// A variant of m_Isa that also matches SubPattern.
78template <typename... To, typename SubPattern>
79inline auto m_Isa(const SubPattern &P) { // NOLINT
80 return m_CombineAnd(m_Isa<To...>(), P);
81}
82} // namespace llvm::PatternMatchHelpers
83
84#endif // LLVM_SUPPORT_PATTERNMATCHHELPERS_H
#define P(N)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
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