[llvm] [PatternMatch] Unify ap(int|float)_match (NFC) (PR #159575)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 18 07:17:26 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Ramkumar Ramachandra (artagnon)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/159575.diff
1 Files Affected:
- (modified) llvm/include/llvm/IR/PatternMatch.h (+22-43)
``````````diff
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<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
return match_combine_and<LTy, RTy>(L, R);
}
-struct apint_match {
- const APInt *&Res;
+template <typename APTy> struct ap_match {
+ static_assert(std::is_same_v<APTy, APInt> || std::is_same_v<APTy, APFloat>);
+ using ConstantTy =
+ std::conditional_t<std::is_same_v<APTy, APInt>, 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 <typename ITy> bool match(ITy *V) const {
- if (auto *CI = dyn_cast<ConstantInt>(V)) {
+ if (auto *CI = dyn_cast<ConstantTy>(V)) {
Res = &CI->getValue();
return true;
}
if (V->getType()->isVectorTy())
if (const auto *C = dyn_cast<Constant>(V))
if (auto *CI =
- dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison))) {
+ dyn_cast_or_null<ConstantTy>(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 <typename ITy> bool match(ITy *V) const {
- if (auto *CI = dyn_cast<ConstantFP>(V)) {
- Res = &CI->getValueAPF();
- return true;
- }
- if (V->getType()->isVectorTy())
- if (const auto *C = dyn_cast<Constant>(V))
- if (auto *CI =
- dyn_cast_or_null<ConstantFP>(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<APInt> m_APInt(const APInt *&Res) {
// Forbid poison by default to maintain previous behavior.
- return apint_match(Res, /* AllowPoison */ false);
+ return ap_match<APInt>(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<APInt> m_APIntAllowPoison(const APInt *&Res) {
+ return ap_match<APInt>(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<APInt> m_APIntForbidPoison(const APInt *&Res) {
+ return ap_match<APInt>(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<APFloat> m_APFloat(const APFloat *&Res) {
// Forbid undefs by default to maintain previous behavior.
- return apfloat_match(Res, /* AllowPoison */ false);
+ return ap_match<APFloat>(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<APFloat> m_APFloatAllowPoison(const APFloat *&Res) {
+ return ap_match<APFloat>(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<APFloat> m_APFloatForbidPoison(const APFloat *&Res) {
+ return ap_match<APFloat>(Res, /* AllowPoison */ false);
}
template <int64_t Val> struct constantint_match {
@@ -1027,7 +1006,7 @@ struct bind_const_intval_ty {
template <typename ITy> bool match(ITy *V) const {
const APInt *ConstInt;
- if (!apint_match(ConstInt, /*AllowPoison=*/false).match(V))
+ if (!ap_match<APInt>(ConstInt, /*AllowPoison=*/false).match(V))
return false;
if (ConstInt->getActiveBits() > 64)
return false;
``````````
</details>
https://github.com/llvm/llvm-project/pull/159575
More information about the llvm-commits
mailing list