[llvm] [LiveIns] Improve recomputeLiveIns() (PR #88951)

Kai Nacke via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 11:22:51 PDT 2024


https://github.com/redstar created https://github.com/llvm/llvm-project/pull/88951

Some small changes to recomputeLiveIns() to improve performance:

- Instead of copying the list of old live-ins, and then clearing
  them, a new method swaps the list for an empty one.
- getLiveIns() now returns a constant reference to the list

As result, the list-data is never copied. Depending on the implementation
details of the vector container, it can also save calls to allocate
and deallocate memory.
I see a small improvement on CTMark with these changes.


>From c23821dda7f7158a4114cf4ad3bb1a4370f7944a Mon Sep 17 00:00:00 2001
From: Kai Nacke <kai.peter.nacke at ibm.com>
Date: Tue, 16 Apr 2024 10:52:23 -0400
Subject: [PATCH] [LiveIns] Improve recomputeLiveIns()

Some small changes to recomputeLiveIns() to improve performance:

- Instead of copying the list of old live-ins, and then clearing
  them, a new method swaps the list for an empty one.
- getLiveIns() now returns a constant reference to the list

As result, the list-data is never copied. Depending on the implementation
details of the vector container, it can also save calls to allocate
and deallocate memory.
I see a small improvement on CTMark with these changes.
---
 llvm/include/llvm/CodeGen/LivePhysRegs.h      | 10 +++++-----
 llvm/include/llvm/CodeGen/MachineBasicBlock.h |  6 +++++-
 llvm/lib/CodeGen/MachineBasicBlock.cpp        |  5 +++++
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/LivePhysRegs.h b/llvm/include/llvm/CodeGen/LivePhysRegs.h
index 9574a6f0c7c005..3782fce46f7d23 100644
--- a/llvm/include/llvm/CodeGen/LivePhysRegs.h
+++ b/llvm/include/llvm/CodeGen/LivePhysRegs.h
@@ -197,16 +197,16 @@ void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
 
 /// Convenience function for recomputing live-in's for a MBB. Returns true if
 /// any changes were made.
-static inline bool recomputeLiveIns(MachineBasicBlock &MBB) {
+inline bool recomputeLiveIns(MachineBasicBlock &MBB) {
   LivePhysRegs LPR;
-  auto oldLiveIns = MBB.getLiveIns();
+  std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIns;
 
-  MBB.clearLiveIns();
+  MBB.clearLiveIns(OldLiveIns);
   computeAndAddLiveIns(LPR, MBB);
   MBB.sortUniqueLiveIns();
 
-  auto newLiveIns = MBB.getLiveIns();
-  return oldLiveIns != newLiveIns;
+  auto NewLiveIns = MBB.getLiveIns();
+  return OldLiveIns != NewLiveIns;
 }
 
 /// Convenience function for recomputing live-in's for a set of MBBs until the
diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
index dc2035fa598c46..8074c9504762a5 100644
--- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h
+++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
@@ -441,6 +441,10 @@ class MachineBasicBlock
   /// Clear live in list.
   void clearLiveIns();
 
+  /// Clear the live in list, and return the removed live in's in \p OldLiveIns.
+  /// Requires that the vector \p OldLiveIns is empty.
+  void clearLiveIns(std::vector<RegisterMaskPair> &OldLiveIns);
+
   /// Add PhysReg as live in to this block, and ensure that there is a copy of
   /// PhysReg to a virtual register of class RC. Return the virtual register
   /// that is a copy of the live in PhysReg.
@@ -477,7 +481,7 @@ class MachineBasicBlock
   /// Remove entry from the livein set and return iterator to the next.
   livein_iterator removeLiveIn(livein_iterator I);
 
-  std::vector<RegisterMaskPair> getLiveIns() const { return LiveIns; }
+  const std::vector<RegisterMaskPair> getLiveIns() const { return LiveIns; }
 
   class liveout_iterator {
   public:
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index b2114c250ac09d..18e3815a72b1c3 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1728,6 +1728,11 @@ void MachineBasicBlock::clearLiveIns() {
   LiveIns.clear();
 }
 
+void MachineBasicBlock::clearLiveIns(std::vector<RegisterMaskPair> &OldLiveIns) {
+  assert(OldLiveIns.empty() && "Vector must be empty");
+  std::swap(LiveIns, OldLiveIns);
+}
+
 MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const {
   assert(getParent()->getProperties().hasProperty(
       MachineFunctionProperties::Property::TracksLiveness) &&



More information about the llvm-commits mailing list