From 872e361bfa0d787bdc9616b91c4346cda53ae218 Mon Sep 17 00:00:00 2001 From: Sergei Barannikov Date: Thu, 13 Nov 2025 19:08:11 +0300 Subject: [PATCH 1/2] [CodeGen] Hide SparseSet behind a typedef (NFC) So that changing the type of the container (planned in a future patch) is less intrusive. --- .../llvm/CodeGen/MachineTraceMetrics.h | 9 +++--- llvm/lib/CodeGen/MachineCombiner.cpp | 7 ++--- llvm/lib/CodeGen/MachineTraceMetrics.cpp | 31 +++++++++---------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/llvm/include/llvm/CodeGen/MachineTraceMetrics.h b/llvm/include/llvm/CodeGen/MachineTraceMetrics.h index d51de24d64e8d..70a799fc3ea0d 100644 --- a/llvm/include/llvm/CodeGen/MachineTraceMetrics.h +++ b/llvm/include/llvm/CodeGen/MachineTraceMetrics.h @@ -83,6 +83,8 @@ struct LiveRegUnit { LiveRegUnit(unsigned RU) : RegUnit(RU) {} }; +using LiveRegUnitSet = SparseSet; + /// Strategies for selecting traces. enum class MachineTraceStrategy { /// Select the trace through a block that has the fewest instructions. @@ -381,15 +383,14 @@ class MachineTraceMetrics { /// Updates the depth of an machine instruction, given RegUnits. void updateDepth(TraceBlockInfo &TBI, const MachineInstr&, - SparseSet &RegUnits); + LiveRegUnitSet &RegUnits); void updateDepth(const MachineBasicBlock *, const MachineInstr&, - SparseSet &RegUnits); + LiveRegUnitSet &RegUnits); /// Updates the depth of the instructions from Start to End. void updateDepths(MachineBasicBlock::iterator Start, MachineBasicBlock::iterator End, - SparseSet &RegUnits); - + LiveRegUnitSet &RegUnits); }; /// Get the trace ensemble representing the given trace selection strategy. diff --git a/llvm/lib/CodeGen/MachineCombiner.cpp b/llvm/lib/CodeGen/MachineCombiner.cpp index 54e2a009b464d..205c79e71854f 100644 --- a/llvm/lib/CodeGen/MachineCombiner.cpp +++ b/llvm/lib/CodeGen/MachineCombiner.cpp @@ -482,9 +482,8 @@ insertDeleteInstructions(MachineBasicBlock *MBB, MachineInstr &MI, SmallVectorImpl &InsInstrs, SmallVectorImpl &DelInstrs, MachineTraceMetrics::Ensemble *TraceEnsemble, - SparseSet &RegUnits, - const TargetInstrInfo *TII, unsigned Pattern, - bool IncrementalUpdate) { + LiveRegUnitSet &RegUnits, const TargetInstrInfo *TII, + unsigned Pattern, bool IncrementalUpdate) { // If we want to fix up some placeholder for some target, do it now. // We need this because in genAlternativeCodeSequence, we have not decided the // better pattern InsInstrs or DelInstrs, so we don't want generate some @@ -565,7 +564,7 @@ bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) { if (!TraceEnsemble) TraceEnsemble = Traces->getEnsemble(TII->getMachineCombinerTraceStrategy()); - SparseSet RegUnits; + LiveRegUnitSet RegUnits; RegUnits.setUniverse(TRI->getNumRegUnits()); bool OptForSize = llvm::shouldOptimizeForSize(MBB, PSI, MBFI); diff --git a/llvm/lib/CodeGen/MachineTraceMetrics.cpp b/llvm/lib/CodeGen/MachineTraceMetrics.cpp index c40bd1c83f34a..d6326bef7e817 100644 --- a/llvm/lib/CodeGen/MachineTraceMetrics.cpp +++ b/llvm/lib/CodeGen/MachineTraceMetrics.cpp @@ -737,7 +737,7 @@ static void getPHIDeps(const MachineInstr &UseMI, // tracking set when scanning instructions downwards. static void updatePhysDepsDownwards(const MachineInstr *UseMI, SmallVectorImpl &Deps, - SparseSet &RegUnits, + LiveRegUnitSet &RegUnits, const TargetRegisterInfo *TRI) { SmallVector Kills; SmallVector LiveDefOps; @@ -758,7 +758,7 @@ static void updatePhysDepsDownwards(const MachineInstr *UseMI, if (!MO.readsReg()) continue; for (MCRegUnit Unit : TRI->regunits(Reg)) { - SparseSet::iterator I = RegUnits.find(Unit); + LiveRegUnitSet::iterator I = RegUnits.find(Unit); if (I == RegUnits.end()) continue; Deps.push_back(DataDep(I->MI, I->Op, MO.getOperandNo())); @@ -813,9 +813,9 @@ computeCrossBlockCriticalPath(const TraceBlockInfo &TBI) { return MaxLen; } -void MachineTraceMetrics::Ensemble:: -updateDepth(MachineTraceMetrics::TraceBlockInfo &TBI, const MachineInstr &UseMI, - SparseSet &RegUnits) { +void MachineTraceMetrics::Ensemble::updateDepth(TraceBlockInfo &TBI, + const MachineInstr &UseMI, + LiveRegUnitSet &RegUnits) { SmallVector Deps; // Collect all data dependencies. if (UseMI.isPHI()) @@ -852,16 +852,15 @@ updateDepth(MachineTraceMetrics::TraceBlockInfo &TBI, const MachineInstr &UseMI, } } -void MachineTraceMetrics::Ensemble:: -updateDepth(const MachineBasicBlock *MBB, const MachineInstr &UseMI, - SparseSet &RegUnits) { +void MachineTraceMetrics::Ensemble::updateDepth(const MachineBasicBlock *MBB, + const MachineInstr &UseMI, + LiveRegUnitSet &RegUnits) { updateDepth(BlockInfo[MBB->getNumber()], UseMI, RegUnits); } -void MachineTraceMetrics::Ensemble:: -updateDepths(MachineBasicBlock::iterator Start, - MachineBasicBlock::iterator End, - SparseSet &RegUnits) { +void MachineTraceMetrics::Ensemble::updateDepths( + MachineBasicBlock::iterator Start, MachineBasicBlock::iterator End, + LiveRegUnitSet &RegUnits) { for (; Start != End; Start++) updateDepth(Start->getParent(), *Start, RegUnits); } @@ -887,7 +886,7 @@ computeInstrDepths(const MachineBasicBlock *MBB) { // in the trace. We should track any live-out physregs that were defined in // the trace. This is quite rare in SSA form, typically created by CSE // hoisting a compare. - SparseSet RegUnits; + LiveRegUnitSet RegUnits; RegUnits.setUniverse(MTM.TRI->getNumRegUnits()); // Go through trace blocks in top-down order, stopping after the center block. @@ -925,7 +924,7 @@ computeInstrDepths(const MachineBasicBlock *MBB) { // Return the issue height of MI after considering any live regunits. // Height is the issue height computed from virtual register dependencies alone. static unsigned updatePhysDepsUpwards(const MachineInstr &MI, unsigned Height, - SparseSet &RegUnits, + LiveRegUnitSet &RegUnits, const TargetSchedModel &SchedModel, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) { @@ -944,7 +943,7 @@ static unsigned updatePhysDepsUpwards(const MachineInstr &MI, unsigned Height, // This is a def of Reg. Remove corresponding entries from RegUnits, and // update MI Height to consider the physreg dependencies. for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg())) { - SparseSet::iterator I = RegUnits.find(Unit); + LiveRegUnitSet::iterator I = RegUnits.find(Unit); if (I == RegUnits.end()) continue; unsigned DepHeight = I->Cycle; @@ -1048,7 +1047,7 @@ computeInstrHeights(const MachineBasicBlock *MBB) { // For physregs, the def isn't known when we see the use. // Instead, keep track of the highest use of each regunit. - SparseSet RegUnits; + LiveRegUnitSet RegUnits; RegUnits.setUniverse(MTM.TRI->getNumRegUnits()); // If the bottom of the trace was already precomputed, initialize heights From dae8dc59de752fcefb486854e9e6ad8eb4938ecf Mon Sep 17 00:00:00 2001 From: Sergei Barannikov Date: Thu, 13 Nov 2025 19:14:17 +0300 Subject: [PATCH 2/2] clang-format --- llvm/include/llvm/CodeGen/MachineTraceMetrics.h | 4 ++-- llvm/lib/CodeGen/MachineTraceMetrics.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/CodeGen/MachineTraceMetrics.h b/llvm/include/llvm/CodeGen/MachineTraceMetrics.h index 70a799fc3ea0d..74b051d0cddc6 100644 --- a/llvm/include/llvm/CodeGen/MachineTraceMetrics.h +++ b/llvm/include/llvm/CodeGen/MachineTraceMetrics.h @@ -382,9 +382,9 @@ class MachineTraceMetrics { Trace getTrace(const MachineBasicBlock *MBB); /// Updates the depth of an machine instruction, given RegUnits. - void updateDepth(TraceBlockInfo &TBI, const MachineInstr&, + void updateDepth(TraceBlockInfo &TBI, const MachineInstr &, LiveRegUnitSet &RegUnits); - void updateDepth(const MachineBasicBlock *, const MachineInstr&, + void updateDepth(const MachineBasicBlock *, const MachineInstr &, LiveRegUnitSet &RegUnits); /// Updates the depth of the instructions from Start to End. diff --git a/llvm/lib/CodeGen/MachineTraceMetrics.cpp b/llvm/lib/CodeGen/MachineTraceMetrics.cpp index d6326bef7e817..0312a8e33d669 100644 --- a/llvm/lib/CodeGen/MachineTraceMetrics.cpp +++ b/llvm/lib/CodeGen/MachineTraceMetrics.cpp @@ -861,8 +861,8 @@ void MachineTraceMetrics::Ensemble::updateDepth(const MachineBasicBlock *MBB, void MachineTraceMetrics::Ensemble::updateDepths( MachineBasicBlock::iterator Start, MachineBasicBlock::iterator End, LiveRegUnitSet &RegUnits) { - for (; Start != End; Start++) - updateDepth(Start->getParent(), *Start, RegUnits); + for (; Start != End; Start++) + updateDepth(Start->getParent(), *Start, RegUnits); } /// Compute instruction depths for all instructions above or in MBB in its