[llvm] [PatternMatch] Refactor `m_SpecificInt` to avoid constructing APInt. NFC. (PR #86259)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 22 02:10:47 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Yingwei Zheng (dtcxzyw)
<details>
<summary>Changes</summary>
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
> All temporary objects are destroyed as the last step in evaluating the [full-expression](https://en.cppreference.com/w/cpp/language/expressions#Full-expressions) that (lexically) contains the point where they were created
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:
- (modified) llvm/include/llvm/IR/PatternMatch.h (+25-10)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/86259
More information about the llvm-commits
mailing list