[llvm] [NFC][GlobalISel] Pass `APInt` by const reference (PR #157827)

Abhishek Kaushik via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 10 04:04:37 PDT 2025


https://github.com/abhishek-kaushik22 updated https://github.com/llvm/llvm-project/pull/157827

>From a1d4ebbd36a7994c620c1eefd074c7da44b4e28c Mon Sep 17 00:00:00 2001
From: Abhishek Kaushik <abhishek.kaushik at intel.com>
Date: Wed, 10 Sep 2025 15:51:12 +0530
Subject: [PATCH 1/2] [NFC][GlobalISel] Pass `APInt` by const reference

Change `SpecificConstantMatch` constructor and
`isBuildVectorConstantSplat` overloads to take `const APInt&` instead of
by value to avoid unnecessary copies, especially for wide integers.
---
 llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h | 2 +-
 llvm/include/llvm/CodeGen/GlobalISel/Utils.h          | 6 ++++--
 llvm/lib/CodeGen/GlobalISel/Utils.cpp                 | 6 ++++--
 3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h b/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
index 827cdbdb23c51..b7ccfbb27e51c 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
@@ -193,7 +193,7 @@ m_GFCstOrSplat(std::optional<FPValueAndVReg> &FPValReg) {
 /// Matcher for a specific constant value.
 struct SpecificConstantMatch {
   APInt RequestedVal;
-  SpecificConstantMatch(const APInt RequestedVal)
+  SpecificConstantMatch(const APInt &RequestedVal)
       : RequestedVal(RequestedVal) {}
   bool match(const MachineRegisterInfo &MRI, Register Reg) {
     APInt MatchedVal;
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/Utils.h b/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
index 5c27605c26883..4dce0d39c110e 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
@@ -457,7 +457,8 @@ getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI,
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
 LLVM_ABI bool isBuildVectorConstantSplat(const Register Reg,
                                          const MachineRegisterInfo &MRI,
-                                         int64_t SplatValue, bool AllowUndef);
+                                         const APInt &SplatValue,
+                                         bool AllowUndef);
 
 /// Return true if the specified register is defined by G_BUILD_VECTOR or
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
@@ -469,7 +470,8 @@ LLVM_ABI bool isBuildVectorConstantSplat(const Register Reg,
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
 LLVM_ABI bool isBuildVectorConstantSplat(const MachineInstr &MI,
                                          const MachineRegisterInfo &MRI,
-                                         int64_t SplatValue, bool AllowUndef);
+                                         const APInt &SplatValue,
+                                         bool AllowUndef);
 
 /// Return true if the specified instruction is a G_BUILD_VECTOR or
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
diff --git a/llvm/lib/CodeGen/GlobalISel/Utils.cpp b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
index 58d631e569b3a..18c363a652414 100644
--- a/llvm/lib/CodeGen/GlobalISel/Utils.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
@@ -1409,7 +1409,8 @@ bool llvm::isBuildVectorConstantSplat(const Register Reg,
 
 bool llvm::isBuildVectorConstantSplat(const Register Reg,
                                       const MachineRegisterInfo &MRI,
-                                      APInt SplatValue, bool AllowUndef) {
+                                      const APInt &SplatValue,
+                                      bool AllowUndef) {
   if (auto SplatValAndReg = getAnyConstantSplat(Reg, MRI, AllowUndef)) {
     if (SplatValAndReg->Value.getBitWidth() < SplatValue.getBitWidth())
       return APInt::isSameValue(
@@ -1431,7 +1432,8 @@ bool llvm::isBuildVectorConstantSplat(const MachineInstr &MI,
 
 bool llvm::isBuildVectorConstantSplat(const MachineInstr &MI,
                                       const MachineRegisterInfo &MRI,
-                                      APInt SplatValue, bool AllowUndef) {
+                                      const APInt &SplatValue,
+                                      bool AllowUndef) {
   return isBuildVectorConstantSplat(MI.getOperand(0).getReg(), MRI, SplatValue,
                                     AllowUndef);
 }

>From bd07acb0ce0977cb1882073e71a4225cc0df7954 Mon Sep 17 00:00:00 2001
From: Abhishek Kaushik <abhishek.kaushik at intel.com>
Date: Wed, 10 Sep 2025 16:34:25 +0530
Subject: [PATCH 2/2] Update Utils.h

---
 llvm/include/llvm/CodeGen/GlobalISel/Utils.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/Utils.h b/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
index 4dce0d39c110e..dfe85f5e34d2d 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
@@ -457,14 +457,14 @@ getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI,
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
 LLVM_ABI bool isBuildVectorConstantSplat(const Register Reg,
                                          const MachineRegisterInfo &MRI,
-                                         const APInt &SplatValue,
-                                         bool AllowUndef);
+                                         int64_t SplatValue, bool AllowUndef);
 
-/// Return true if the specified register is defined by G_BUILD_VECTOR or
+/// Return true if the specified instruction is a G_BUILD_VECTOR or
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
-LLVM_ABI bool isBuildVectorConstantSplat(const Register Reg,
+LLVM_ABI bool isBuildVectorConstantSplat(const MachineInstr &MI,
                                          const MachineRegisterInfo &MRI,
-                                         APInt SplatValue, bool AllowUndef);
+                                         const APInt &SplatValue,
+                                         bool AllowUndef);
 
 /// Return true if the specified instruction is a G_BUILD_VECTOR or
 /// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.



More information about the llvm-commits mailing list