[llvm] [GISel] Add LookThroughInstrs for getIConstantVRegVal and getIConstan… (PR #68327)

Michael Maitland via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 09:24:06 PDT 2023


https://github.com/michaelmaitland updated https://github.com/llvm/llvm-project/pull/68327

>From d98b90950018c8663f64b481e82b32e55b1b02d6 Mon Sep 17 00:00:00 2001
From: Michael Maitland <michaeltmaitland at gmail.com>
Date: Thu, 5 Oct 2023 08:48:13 -0700
Subject: [PATCH 1/3] [GISel] Add LookThroughInstrs for getIConstantVRegVal and
 getIConstantVRegSExtVal

The implementation of these methods uses getIConstantVRegValWithLookThrough
with LookThroughInstrs argument set to false. By adding the optional
argument to getIConstantVRegVal and getIConstantVRegSExtVal we can take
advantage of the already built look through functionality.
---
 llvm/lib/CodeGen/GlobalISel/Utils.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/GlobalISel/Utils.cpp b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
index 473c3f452f8b1d9..59b3c6b758589de 100644
--- a/llvm/lib/CodeGen/GlobalISel/Utils.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
@@ -290,9 +290,10 @@ void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
 }
 
 std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
-                                               const MachineRegisterInfo &MRI) {
-  std::optional<ValueAndVReg> ValAndVReg = getIConstantVRegValWithLookThrough(
-      VReg, MRI, /*LookThroughInstrs*/ false);
+                                               const MachineRegisterInfo &MRI,
+                                               bool LookThroughInstrs = false) {
+  std::optional<ValueAndVReg> ValAndVReg =
+      getIConstantVRegValWithLookThrough(VReg, MRI, LookThroughInstrs);
   assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
          "Value found while looking through instrs");
   if (!ValAndVReg)
@@ -301,7 +302,8 @@ std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
 }
 
 std::optional<int64_t>
-llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI) {
+llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI,
+                              bool LookThroughInstrs = false) {
   std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI);
   if (Val && Val->getBitWidth() <= 64)
     return Val->getSExtValue();

>From 544b450a5381268794ef9a567e8d6b85893544cf Mon Sep 17 00:00:00 2001
From: Michael Maitland <michaeltmaitland at gmail.com>
Date: Thu, 5 Oct 2023 09:00:40 -0700
Subject: [PATCH 2/3] Pass LookThroughInstrs to getIConstantVRegVal

---
 llvm/lib/CodeGen/GlobalISel/Utils.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/GlobalISel/Utils.cpp b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
index 59b3c6b758589de..2a7768ad75285ef 100644
--- a/llvm/lib/CodeGen/GlobalISel/Utils.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
@@ -304,7 +304,7 @@ std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
 std::optional<int64_t>
 llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI,
                               bool LookThroughInstrs = false) {
-  std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI);
+  std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI, LookThroughInstrs);
   if (Val && Val->getBitWidth() <= 64)
     return Val->getSExtValue();
   return std::nullopt;

>From 583f129adbef17954bc516273142f591827f62d2 Mon Sep 17 00:00:00 2001
From: Michael Maitland <michaeltmaitland at gmail.com>
Date: Thu, 5 Oct 2023 09:23:38 -0700
Subject: [PATCH 3/3] Fix header files

---
 llvm/include/llvm/CodeGen/GlobalISel/Utils.h | 6 ++++--
 llvm/lib/CodeGen/GlobalISel/Utils.cpp        | 4 ++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/GlobalISel/Utils.h b/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
index ffb6e53a0363f94..515fd5975dd144e 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/Utils.h
@@ -171,11 +171,13 @@ void reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC,
 
 /// If \p VReg is defined by a G_CONSTANT, return the corresponding value.
 std::optional<APInt> getIConstantVRegVal(Register VReg,
-                                         const MachineRegisterInfo &MRI);
+                                         const MachineRegisterInfo &MRI,
+                                         bool LookThroughInstrs = false);
 
 /// If \p VReg is defined by a G_CONSTANT fits in int64_t returns it.
 std::optional<int64_t> getIConstantVRegSExtVal(Register VReg,
-                                               const MachineRegisterInfo &MRI);
+                                               const MachineRegisterInfo &MRI,
+                                               bool LookThroughInstrs = false);
 
 /// Simple struct used to hold a constant integer value and a virtual
 /// register.
diff --git a/llvm/lib/CodeGen/GlobalISel/Utils.cpp b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
index 2a7768ad75285ef..36de083b3174384 100644
--- a/llvm/lib/CodeGen/GlobalISel/Utils.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/Utils.cpp
@@ -291,7 +291,7 @@ void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
 
 std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
                                                const MachineRegisterInfo &MRI,
-                                               bool LookThroughInstrs = false) {
+                                               bool LookThroughInstrs) {
   std::optional<ValueAndVReg> ValAndVReg =
       getIConstantVRegValWithLookThrough(VReg, MRI, LookThroughInstrs);
   assert((!ValAndVReg || ValAndVReg->VReg == VReg) &&
@@ -303,7 +303,7 @@ std::optional<APInt> llvm::getIConstantVRegVal(Register VReg,
 
 std::optional<int64_t>
 llvm::getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI,
-                              bool LookThroughInstrs = false) {
+                              bool LookThroughInstrs) {
   std::optional<APInt> Val = getIConstantVRegVal(VReg, MRI, LookThroughInstrs);
   if (Val && Val->getBitWidth() <= 64)
     return Val->getSExtValue();



More information about the llvm-commits mailing list