[llvm] [CodeGen] Check entire block if no threshold was given for neighbors (PR #83526)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 4 10:00:31 PST 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/83526

>From d1c4e490aedcf439550924d771346cfb070e68a5 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Fri, 1 Mar 2024 00:19:21 -0500
Subject: [PATCH] [CodeGen] Check entire block if no threshold was given for
 neighbors

We could pass MBB.size(), but we should compute liveness the correct way if we have the entire block.
---
 llvm/include/llvm/CodeGen/MachineBasicBlock.h |  5 ++-
 llvm/lib/CodeGen/MachineBasicBlock.cpp        | 31 +++++++++++++++++--
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
index dc2035fa598c46..05fa92304cbfbb 100644
--- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h
+++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
@@ -1147,10 +1147,13 @@ class MachineBasicBlock
   /// after (searching just for defs) \p Before.
   ///
   /// \p Reg must be a physical register.
+  LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
+                                              MCRegister Reg,
+                                              const_iterator Before) const;
   LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
                                               MCRegister Reg,
                                               const_iterator Before,
-                                              unsigned Neighborhood = 10) const;
+                                              unsigned Neighborhood) const;
 
   // Debugging methods.
   void dump() const;
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index 4410fb7ecd23b6..861518d8addf25 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/CodeGen/LiveIntervals.h"
 #include "llvm/CodeGen/LivePhysRegs.h"
+#include "llvm/CodeGen/LiveRegUnits.h"
 #include "llvm/CodeGen/LiveVariables.h"
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunction.h"
@@ -1598,15 +1599,42 @@ MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) {
   return Probs.begin() + index;
 }
 
+MachineBasicBlock::LivenessQueryResult
+MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
+                                           MCRegister Reg,
+                                           const_iterator Before) const {
+  LiveRegUnits UsedRegs(*TRI);
+  UsedRegs.addLiveOuts(*this);
+
+  const MachineFunction *MF = getParent();
+  const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(MF);
+  for (unsigned i = 0; CSRegs[i]; ++i)
+    UsedRegs.addReg(CSRegs[i]);
+
+  auto InstUpToBefore = end();
+
+  while (InstUpToBefore != Before)
+    // The pre-decrement is on purpose here.
+    // We want to have the liveness right before Before.
+    UsedRegs.stepBackward(*--InstUpToBefore);
+
+  if (UsedRegs.available(Reg))
+    return LQR_Dead;
+
+  return LQR_Live;
+}
+
 /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
 /// as of just before "MI".
 ///
 /// Search is localised to a neighborhood of
 /// Neighborhood instructions before (searching for defs or kills) and N
 /// instructions after (searching just for defs) MI.
+
 MachineBasicBlock::LivenessQueryResult
 MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
-                                           MCRegister Reg, const_iterator Before,
+                                           MCRegister Reg,
+                                           const_iterator Before,
                                            unsigned Neighborhood) const {
   unsigned N = Neighborhood;
 
@@ -1641,7 +1669,6 @@ MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
     return LQR_Dead;
   }
 
-
   N = Neighborhood;
 
   // Start by searching backwards from Before, looking for kills, reads or defs.



More information about the llvm-commits mailing list