//===- PatternMatchHelpers.h - Helpers for PatternMatch -------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file provides helpers that are used across the IR PatternMatch, // ScalarEvolutionPatternMatch, and VPlanPatternMatch. // //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_PATTERNMATCHHELPERS_H #define LLVM_SUPPORT_PATTERNMATCHHELPERS_H #include "llvm/Support/Casting.h" namespace llvm::PatternMatchHelpers { /// Matching or combinator leaf case. template struct match_combine_or { // NOLINT template bool match(ITy *) const { return false; } }; /// Matching or combinator. template struct match_combine_or : match_combine_or { Ty P; match_combine_or(const Ty &P, const Tys &...Ps) : match_combine_or(Ps...), P(P) {} template bool match(ITy *V) const { return P.match(V) || match_combine_or::match(V); } }; /// Matching and combinator leaf case. template struct match_combine_and { // NOLINT template bool match(ITy *) const { return true; } }; /// Matching and combinator. template struct match_combine_and : match_combine_and { Ty P; match_combine_and(const Ty &P, const Tys &...Ps) : match_combine_and(Ps...), P(P) {} template bool match(ITy *V) const { return P.match(V) && match_combine_and::match(V); } }; /// Combine pattern matchers matching any of Ps patterns. template inline match_combine_or m_CombineOr(const Ty &...Ps) { // NOLINT return {Ps...}; } /// Combine pattern matchers matching all of Ps patterns. template inline match_combine_and m_CombineAnd(const Ty &...Ps) { // NOLINT return {Ps...}; } /// A match-wrapper around isa. template struct match_isa { // NOLINT template bool match(const ArgTy *V) const { return isa(V); } }; template inline match_isa m_Isa() { // NOLINT return match_isa(); } /// A variant of m_Isa that also matches SubPattern. template inline auto m_Isa(const SubPattern &P) { // NOLINT return m_CombineAnd(m_Isa(), P); } /// Matcher for a specific value, but stores a reference to the value, not the /// value itself. template struct match_deferred { // NOLINT Ty *const &Val; match_deferred(Ty *const &V) : Val(V) {} template bool match(ITy *const V) const { return V == Val; } }; /// Matcher to bind the captured value. template struct match_bind { // NOLINT Ty *&VR; match_bind(Ty *&V) : VR(V) {} template bool match(ITy *V) const { if (auto *CV = dyn_cast(V)) { VR = CV; return true; } return false; } }; } // namespace llvm::PatternMatchHelpers #endif // LLVM_SUPPORT_PATTERNMATCHHELPERS_H