From c23821dda7f7158a4114cf4ad3bb1a4370f7944a Mon Sep 17 00:00:00 2001 From: Kai Nacke Date: Tue, 16 Apr 2024 10:52:23 -0400 Subject: [PATCH 1/4] [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 9574a6f0c7c00..3782fce46f7d2 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 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 dc2035fa598c4..8074c9504762a 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 &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 getLiveIns() const { return LiveIns; } + const std::vector getLiveIns() const { return LiveIns; } class liveout_iterator { public: diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index b2114c250ac09..18e3815a72b1c 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 &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) && From d8919915abb0b8d0743aca3490c444134dafe2b8 Mon Sep 17 00:00:00 2001 From: Kai Nacke Date: Wed, 17 Apr 2024 14:50:17 -0400 Subject: [PATCH 2/4] Update llvm/include/llvm/CodeGen/MachineBasicBlock.h Co-authored-by: Nikita Popov --- llvm/include/llvm/CodeGen/MachineBasicBlock.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h index 8074c9504762a..5b6be3a96b2fb 100644 --- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h +++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h @@ -481,7 +481,7 @@ class MachineBasicBlock /// Remove entry from the livein set and return iterator to the next. livein_iterator removeLiveIn(livein_iterator I); - const std::vector getLiveIns() const { return LiveIns; } + const std::vector &getLiveIns() const { return LiveIns; } class liveout_iterator { public: From 3180bc706f8cdad2a1d7d55660954cb21e3bcd18 Mon Sep 17 00:00:00 2001 From: Kai Nacke Date: Wed, 17 Apr 2024 15:17:19 -0400 Subject: [PATCH 3/4] Update based on review comments. --- llvm/include/llvm/CodeGen/LivePhysRegs.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/CodeGen/LivePhysRegs.h b/llvm/include/llvm/CodeGen/LivePhysRegs.h index 3782fce46f7d2..d315e4ff6f3ab 100644 --- a/llvm/include/llvm/CodeGen/LivePhysRegs.h +++ b/llvm/include/llvm/CodeGen/LivePhysRegs.h @@ -197,7 +197,7 @@ void computeAndAddLiveIns(LivePhysRegs &LiveRegs, /// Convenience function for recomputing live-in's for a MBB. Returns true if /// any changes were made. -inline bool recomputeLiveIns(MachineBasicBlock &MBB) { +static inline bool recomputeLiveIns(MachineBasicBlock &MBB) { LivePhysRegs LPR; std::vector OldLiveIns; @@ -205,7 +205,8 @@ inline bool recomputeLiveIns(MachineBasicBlock &MBB) { computeAndAddLiveIns(LPR, MBB); MBB.sortUniqueLiveIns(); - auto NewLiveIns = MBB.getLiveIns(); + const std::vector &NewLiveIns = + MBB.getLiveIns(); return OldLiveIns != NewLiveIns; } From 82071412943dbf296ae464b4af6344910fd153f6 Mon Sep 17 00:00:00 2001 From: Kai Nacke Date: Wed, 17 Apr 2024 15:35:33 -0400 Subject: [PATCH 4/4] Fix formatting --- llvm/lib/CodeGen/MachineBasicBlock.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index 18e3815a72b1c..0bd5f09564ec0 100644 --- a/llvm/lib/CodeGen/MachineBasicBlock.cpp +++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -1728,7 +1728,8 @@ void MachineBasicBlock::clearLiveIns() { LiveIns.clear(); } -void MachineBasicBlock::clearLiveIns(std::vector &OldLiveIns) { +void MachineBasicBlock::clearLiveIns( + std::vector &OldLiveIns) { assert(OldLiveIns.empty() && "Vector must be empty"); std::swap(LiveIns, OldLiveIns); }