-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[PatternMatch] Unify ap(int|float)_match (NFC) #159575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-ir Author: Ramkumar Ramachandra (artagnon) ChangesFull diff: https://github.com/llvm/llvm-project/pull/159575.diff 1 Files Affected:
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;
|
Gentle ping. |
nikic
approved these changes
Sep 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.