-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[PatternMatch] Refactor m_SpecificInt
to avoid constructing APInt. NFC.
#86259
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
Conversation
@llvm/pr-subscribers-llvm-ir Author: Yingwei Zheng (dtcxzyw) ChangesThis patch passes APInt by const reference in m_SpecificInt instead of by value. Specifically, it refactors I believe it is safe to pass the APInt by const reference into Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=7edf459b95ab2be33b70ec67faf87b3b8cc84f09&stat=instructions:u Full diff: https://github.com/llvm/llvm-project/pull/86259.diff 1 Files Affected:
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 382009d9df785d..d857030014cd3b 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -884,9 +884,9 @@ struct bind_const_intval_ty {
/// Match a specified integer value or vector of all elements of that
/// value.
template <bool AllowUndefs> struct specific_intval {
- APInt Val;
+ const APInt &Val;
- specific_intval(APInt V) : Val(std::move(V)) {}
+ specific_intval(const APInt &V) : Val(V) {}
template <typename ITy> bool match(ITy *V) {
const auto *CI = dyn_cast<ConstantInt>(V);
@@ -898,22 +898,37 @@ template <bool AllowUndefs> struct specific_intval {
}
};
+template <bool AllowUndefs> struct specific_intval64 {
+ uint64_t Val;
+
+ specific_intval64(uint64_t V) : Val(V) {}
+
+ template <typename ITy> bool match(ITy *V) {
+ const auto *CI = dyn_cast<ConstantInt>(V);
+ if (!CI && V->getType()->isVectorTy())
+ if (const auto *C = dyn_cast<Constant>(V))
+ CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs));
+
+ return CI && CI->getValue() == Val;
+ }
+};
+
/// Match a specific integer value or vector with all elements equal to
/// the value.
-inline specific_intval<false> m_SpecificInt(APInt V) {
- return specific_intval<false>(std::move(V));
+inline specific_intval<false> m_SpecificInt(const APInt &V) {
+ return specific_intval<false>(V);
}
-inline specific_intval<false> m_SpecificInt(uint64_t V) {
- return m_SpecificInt(APInt(64, V));
+inline specific_intval64<false> m_SpecificInt(uint64_t V) {
+ return specific_intval64<false>(V);
}
-inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) {
- return specific_intval<true>(std::move(V));
+inline specific_intval<true> m_SpecificIntAllowUndef(const APInt &V) {
+ return specific_intval<true>(V);
}
-inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) {
- return m_SpecificIntAllowUndef(APInt(64, V));
+inline specific_intval64<true> m_SpecificIntAllowUndef(uint64_t V) {
+ return specific_intval64<true>(V);
}
/// Match a ConstantInt and bind to its value. This does not match
|
Should this be done for SDPatternMatch as well? I don't think MIPatternMatch has an equivalent though |
SDPatternMatch always copies APInt :( We should refactor llvm-project/llvm/include/llvm/CodeGen/SDPatternMatch.h Lines 667 to 688 in 4d69855
llvm-project/llvm/include/llvm/CodeGen/SDPatternMatch.h Lines 695 to 707 in 4d69855
|
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.
If this is a problem sanitizers should catch it
This patch passes APInt by const reference in m_SpecificInt instead of by value. Specifically, it refactors
m_SpecificInt(uint64_t V)
to avoid APInt construction and dangling reference.I believe it is safe to pass the APInt by const reference into
m_SpecificInt
even if it is a temporary.See also https://en.cppreference.com/w/cpp/language/lifetime
Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=7edf459b95ab2be33b70ec67faf87b3b8cc84f09&stat=instructions:u