Skip to content

Commit d0c51f7

Browse files
redstarnikic
andauthored
[LiveIns] Improve recomputeLiveIns() (#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. --------- Co-authored-by: Nikita Popov <[email protected]>
1 parent 2583b2e commit d0c51f7

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

llvm/include/llvm/CodeGen/LivePhysRegs.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,15 @@ void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
199199
/// any changes were made.
200200
static inline bool recomputeLiveIns(MachineBasicBlock &MBB) {
201201
LivePhysRegs LPR;
202-
auto oldLiveIns = MBB.getLiveIns();
202+
std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIns;
203203

204-
MBB.clearLiveIns();
204+
MBB.clearLiveIns(OldLiveIns);
205205
computeAndAddLiveIns(LPR, MBB);
206206
MBB.sortUniqueLiveIns();
207207

208-
auto newLiveIns = MBB.getLiveIns();
209-
return oldLiveIns != newLiveIns;
208+
const std::vector<MachineBasicBlock::RegisterMaskPair> &NewLiveIns =
209+
MBB.getLiveIns();
210+
return OldLiveIns != NewLiveIns;
210211
}
211212

212213
/// Convenience function for recomputing live-in's for a set of MBBs until the

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ class MachineBasicBlock
441441
/// Clear live in list.
442442
void clearLiveIns();
443443

444+
/// Clear the live in list, and return the removed live in's in \p OldLiveIns.
445+
/// Requires that the vector \p OldLiveIns is empty.
446+
void clearLiveIns(std::vector<RegisterMaskPair> &OldLiveIns);
447+
444448
/// Add PhysReg as live in to this block, and ensure that there is a copy of
445449
/// PhysReg to a virtual register of class RC. Return the virtual register
446450
/// that is a copy of the live in PhysReg.
@@ -477,7 +481,7 @@ class MachineBasicBlock
477481
/// Remove entry from the livein set and return iterator to the next.
478482
livein_iterator removeLiveIn(livein_iterator I);
479483

480-
std::vector<RegisterMaskPair> getLiveIns() const { return LiveIns; }
484+
const std::vector<RegisterMaskPair> &getLiveIns() const { return LiveIns; }
481485

482486
class liveout_iterator {
483487
public:

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,12 @@ void MachineBasicBlock::clearLiveIns() {
17281728
LiveIns.clear();
17291729
}
17301730

1731+
void MachineBasicBlock::clearLiveIns(
1732+
std::vector<RegisterMaskPair> &OldLiveIns) {
1733+
assert(OldLiveIns.empty() && "Vector must be empty");
1734+
std::swap(LiveIns, OldLiveIns);
1735+
}
1736+
17311737
MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const {
17321738
assert(getParent()->getProperties().hasProperty(
17331739
MachineFunctionProperties::Property::TracksLiveness) &&

0 commit comments

Comments
 (0)