From 026d0e81dde0fa50f6ca86bdb834cdb9f4065d93 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Thu, 18 Sep 2025 14:59:53 +0100 Subject: [PATCH] [PatternMatch] Unify ap(int|float)_match (NFC) --- llvm/include/llvm/IR/PatternMatch.h | 65 ++++++++++------------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h index a16776c62f32b..6168e24569f99 100644 --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -259,86 +259,65 @@ inline match_combine_and m_CombineAnd(const LTy &L, const RTy &R) { return match_combine_and(L, R); } -struct apint_match { - const APInt *&Res; +template struct ap_match { + static_assert(std::is_same_v || std::is_same_v); + using ConstantTy = + std::conditional_t, ConstantInt, ConstantFP>; + + const APTy *&Res; bool AllowPoison; - apint_match(const APInt *&Res, bool AllowPoison) + ap_match(const APTy *&Res, bool AllowPoison) : Res(Res), AllowPoison(AllowPoison) {} template bool match(ITy *V) const { - if (auto *CI = dyn_cast(V)) { + if (auto *CI = dyn_cast(V)) { Res = &CI->getValue(); return true; } if (V->getType()->isVectorTy()) if (const auto *C = dyn_cast(V)) if (auto *CI = - dyn_cast_or_null(C->getSplatValue(AllowPoison))) { + dyn_cast_or_null(C->getSplatValue(AllowPoison))) { Res = &CI->getValue(); return true; } return false; } }; -// Either constexpr if or renaming ConstantFP::getValueAPF to -// ConstantFP::getValue is needed to do it via single template -// function for both apint/apfloat. -struct apfloat_match { - const APFloat *&Res; - bool AllowPoison; - - apfloat_match(const APFloat *&Res, bool AllowPoison) - : Res(Res), AllowPoison(AllowPoison) {} - - template bool match(ITy *V) const { - if (auto *CI = dyn_cast(V)) { - Res = &CI->getValueAPF(); - return true; - } - if (V->getType()->isVectorTy()) - if (const auto *C = dyn_cast(V)) - if (auto *CI = - dyn_cast_or_null(C->getSplatValue(AllowPoison))) { - Res = &CI->getValueAPF(); - return true; - } - return false; - } -}; /// Match a ConstantInt or splatted ConstantVector, binding the /// specified pointer to the contained APInt. -inline apint_match m_APInt(const APInt *&Res) { +inline ap_match m_APInt(const APInt *&Res) { // Forbid poison by default to maintain previous behavior. - return apint_match(Res, /* AllowPoison */ false); + return ap_match(Res, /* AllowPoison */ false); } /// Match APInt while allowing poison in splat vector constants. -inline apint_match m_APIntAllowPoison(const APInt *&Res) { - return apint_match(Res, /* AllowPoison */ true); +inline ap_match m_APIntAllowPoison(const APInt *&Res) { + return ap_match(Res, /* AllowPoison */ true); } /// Match APInt while forbidding poison in splat vector constants. -inline apint_match m_APIntForbidPoison(const APInt *&Res) { - return apint_match(Res, /* AllowPoison */ false); +inline ap_match m_APIntForbidPoison(const APInt *&Res) { + return ap_match(Res, /* AllowPoison */ false); } /// Match a ConstantFP or splatted ConstantVector, binding the /// specified pointer to the contained APFloat. -inline apfloat_match m_APFloat(const APFloat *&Res) { +inline ap_match m_APFloat(const APFloat *&Res) { // Forbid undefs by default to maintain previous behavior. - return apfloat_match(Res, /* AllowPoison */ false); + return ap_match(Res, /* AllowPoison */ false); } /// Match APFloat while allowing poison in splat vector constants. -inline apfloat_match m_APFloatAllowPoison(const APFloat *&Res) { - return apfloat_match(Res, /* AllowPoison */ true); +inline ap_match m_APFloatAllowPoison(const APFloat *&Res) { + return ap_match(Res, /* AllowPoison */ true); } /// Match APFloat while forbidding poison in splat vector constants. -inline apfloat_match m_APFloatForbidPoison(const APFloat *&Res) { - return apfloat_match(Res, /* AllowPoison */ false); +inline ap_match m_APFloatForbidPoison(const APFloat *&Res) { + return ap_match(Res, /* AllowPoison */ false); } template struct constantint_match { @@ -1027,7 +1006,7 @@ struct bind_const_intval_ty { template bool match(ITy *V) const { const APInt *ConstInt; - if (!apint_match(ConstInt, /*AllowPoison=*/false).match(V)) + if (!ap_match(ConstInt, /*AllowPoison=*/false).match(V)) return false; if (ConstInt->getActiveBits() > 64) return false;