[llvm] 93d33a1 - [PatternMatch] Refactor `m_SpecificInt` to avoid constructing APInt. NFC. (#86259)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 06:33:01 PDT 2024


Author: Yingwei Zheng
Date: 2024-03-26T21:32:57+08:00
New Revision: 93d33a1355438638232e66991f3a90c376c61fea

URL: https://github.com/llvm/llvm-project/commit/93d33a1355438638232e66991f3a90c376c61fea
DIFF: https://github.com/llvm/llvm-project/commit/93d33a1355438638232e66991f3a90c376c61fea.diff

LOG: [PatternMatch] Refactor `m_SpecificInt` to avoid constructing APInt. NFC. (#86259)

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

Added: 
    

Modified: 
    llvm/include/llvm/IR/PatternMatch.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 04feb1fd17fd62..49f9ed5f6e7b6b 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


        


More information about the llvm-commits mailing list