From a878dc8f8fb12ed19bdbabc3a55f6aa94259da08 Mon Sep 17 00:00:00 2001 From: Sergei Barannikov Date: Wed, 12 Nov 2025 23:57:04 +0300 Subject: [PATCH 1/2] [CodeGen] Add TRI::regunits() iterating over all register units (NFC) --- llvm/include/llvm/MC/MCRegisterInfo.h | 8 ++++++++ llvm/lib/CodeGen/LiveIntervals.cpp | 7 ++++--- llvm/lib/CodeGen/LiveRegMatrix.cpp | 5 +++-- llvm/lib/CodeGen/LiveRegUnits.cpp | 4 ++-- llvm/lib/CodeGen/MachineVerifier.cpp | 6 +++--- llvm/lib/CodeGen/RDFRegisters.cpp | 4 ++-- llvm/lib/CodeGen/ReachingDefAnalysis.cpp | 8 ++++---- llvm/lib/CodeGen/RegAllocFast.cpp | 3 +-- 8 files changed, 27 insertions(+), 18 deletions(-) diff --git a/llvm/include/llvm/MC/MCRegisterInfo.h b/llvm/include/llvm/MC/MCRegisterInfo.h index e6dbb38dfee67..f1caa077a6d7b 100644 --- a/llvm/include/llvm/MC/MCRegisterInfo.h +++ b/llvm/include/llvm/MC/MCRegisterInfo.h @@ -16,6 +16,7 @@ #define LLVM_MC_MCREGISTERINFO_H #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Sequence.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" #include "llvm/MC/LaneBitmask.h" @@ -259,6 +260,9 @@ class LLVM_ABI MCRegisterInfo { iterator_range> sub_and_superregs_inclusive(MCRegister Reg) const; + /// Returns an iterator range over all regunits. + iota_range regunits() const; + /// Returns an iterator range over all regunits for \p Reg. iterator_range regunits(MCRegister Reg) const; @@ -798,6 +802,10 @@ MCRegisterInfo::sub_and_superregs_inclusive(MCRegister Reg) const { return concat(subregs_inclusive(Reg), superregs(Reg)); } +inline iota_range MCRegisterInfo::regunits() const { + return seq(getNumRegUnits()); +} + inline iterator_range MCRegisterInfo::regunits(MCRegister Reg) const { return make_range({Reg, this}, MCRegUnitIterator()); diff --git a/llvm/lib/CodeGen/LiveIntervals.cpp b/llvm/lib/CodeGen/LiveIntervals.cpp index 27c5addffa4ab..9af1f53e5ced0 100644 --- a/llvm/lib/CodeGen/LiveIntervals.cpp +++ b/llvm/lib/CodeGen/LiveIntervals.cpp @@ -173,8 +173,8 @@ void LiveIntervals::analyze(MachineFunction &fn) { if (EnablePrecomputePhysRegs) { // For stress testing, precompute live ranges of all physical register // units, including reserved registers. - for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) - getRegUnit(i); + for (MCRegUnit Unit : TRI->regunits()) + getRegUnit(Unit); } } @@ -182,7 +182,8 @@ void LiveIntervals::print(raw_ostream &OS) const { OS << "********** INTERVALS **********\n"; // Dump the regunits. - for (unsigned Unit = 0, UnitE = RegUnitRanges.size(); Unit != UnitE; ++Unit) + assert(RegUnitRanges.size() == TRI->getNumRegUnits()); + for (MCRegUnit Unit : TRI->regunits()) if (LiveRange *LR = RegUnitRanges[Unit]) OS << printRegUnit(Unit, TRI) << ' ' << *LR << '\n'; diff --git a/llvm/lib/CodeGen/LiveRegMatrix.cpp b/llvm/lib/CodeGen/LiveRegMatrix.cpp index e3ee8dc325933..38d4810c38061 100644 --- a/llvm/lib/CodeGen/LiveRegMatrix.cpp +++ b/llvm/lib/CodeGen/LiveRegMatrix.cpp @@ -75,8 +75,9 @@ void LiveRegMatrix::init(MachineFunction &MF, LiveIntervals &pLIS, void LiveRegMatrixWrapperLegacy::releaseMemory() { LRM.releaseMemory(); } void LiveRegMatrix::releaseMemory() { - for (unsigned i = 0, e = Matrix.size(); i != e; ++i) { - Matrix[i].clear(); + assert(Matrix.size() == TRI->getNumRegUnits()); + for (MCRegUnit Unit : TRI->regunits()) { + Matrix[Unit].clear(); // No need to clear Queries here, since LiveIntervalUnion::Query doesn't // have anything important to clear and LiveRegMatrix's runOnFunction() // does a std::unique_ptr::reset anyways. diff --git a/llvm/lib/CodeGen/LiveRegUnits.cpp b/llvm/lib/CodeGen/LiveRegUnits.cpp index 0d87062169585..3e7052a9b6245 100644 --- a/llvm/lib/CodeGen/LiveRegUnits.cpp +++ b/llvm/lib/CodeGen/LiveRegUnits.cpp @@ -20,7 +20,7 @@ using namespace llvm; void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) { - for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) { + for (MCRegUnit U : TRI->regunits()) { for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) { if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) { Units.reset(U); @@ -31,7 +31,7 @@ void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) { } void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) { - for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) { + for (MCRegUnit U : TRI->regunits()) { for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) { if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) { Units.set(U); diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp index 013f52938b65c..a2a66d6128348 100644 --- a/llvm/lib/CodeGen/MachineVerifier.cpp +++ b/llvm/lib/CodeGen/MachineVerifier.cpp @@ -3564,9 +3564,9 @@ void MachineVerifier::verifyLiveIntervals() { } // Verify all the cached regunit intervals. - for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) - if (const LiveRange *LR = LiveInts->getCachedRegUnit(i)) - verifyLiveRange(*LR, VirtRegOrUnit(i)); + for (MCRegUnit Unit : TRI->regunits()) + if (const LiveRange *LR = LiveInts->getCachedRegUnit(Unit)) + verifyLiveRange(*LR, VirtRegOrUnit(Unit)); } void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR, diff --git a/llvm/lib/CodeGen/RDFRegisters.cpp b/llvm/lib/CodeGen/RDFRegisters.cpp index 1400699a607ff..e4b63a3a40805 100644 --- a/llvm/lib/CodeGen/RDFRegisters.cpp +++ b/llvm/lib/CodeGen/RDFRegisters.cpp @@ -46,7 +46,7 @@ PhysicalRegisterInfo::PhysicalRegisterInfo(const TargetRegisterInfo &tri, UnitInfos.resize(TRI.getNumRegUnits()); - for (uint32_t U = 0, NU = TRI.getNumRegUnits(); U != NU; ++U) { + for (MCRegUnit U : TRI.regunits()) { if (UnitInfos[U].Reg != 0) continue; MCRegUnitRootIterator R(U, &TRI); @@ -88,7 +88,7 @@ PhysicalRegisterInfo::PhysicalRegisterInfo(const TargetRegisterInfo &tri, } AliasInfos.resize(TRI.getNumRegUnits()); - for (uint32_t U = 0, NU = TRI.getNumRegUnits(); U != NU; ++U) { + for (MCRegUnit U : TRI.regunits()) { BitVector AS(TRI.getNumRegs()); for (MCRegUnitRootIterator R(U, &TRI); R.isValid(); ++R) for (MCPhysReg S : TRI.superregs_inclusive(*R)) diff --git a/llvm/lib/CodeGen/ReachingDefAnalysis.cpp b/llvm/lib/CodeGen/ReachingDefAnalysis.cpp index 61706e13b8e91..c0e7469a7bfd7 100644 --- a/llvm/lib/CodeGen/ReachingDefAnalysis.cpp +++ b/llvm/lib/CodeGen/ReachingDefAnalysis.cpp @@ -155,12 +155,12 @@ void ReachingDefInfo::enterBasicBlock(MachineBasicBlock *MBB) { continue; // Find the most recent reaching definition from a predecessor. - for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) + for (MCRegUnit Unit : TRI->regunits()) LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]); } // Insert the most recent reaching definition we found. - for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) + for (MCRegUnit Unit : TRI->regunits()) if (LiveRegs[Unit] != ReachingDefDefaultVal) MBBReachingDefs.append(MBBNumber, Unit, LiveRegs[Unit]); } @@ -235,7 +235,7 @@ void ReachingDefInfo::reprocessBasicBlock(MachineBasicBlock *MBB) { if (Incoming.empty()) continue; - for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) { + for (MCRegUnit Unit : TRI->regunits()) { int Def = Incoming[Unit]; if (Def == ReachingDefDefaultVal) continue; @@ -368,7 +368,7 @@ void ReachingDefInfo::traverse() { // Make sure reaching defs are sorted and unique. for (unsigned MBBNumber = 0, NumBlockIDs = MF->getNumBlockIDs(); MBBNumber != NumBlockIDs; ++MBBNumber) { - for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) { + for (MCRegUnit Unit : TRI->regunits()) { int LastDef = ReachingDefDefaultVal; for (int Def : MBBReachingDefs.defs(MBBNumber, Unit)) { assert(Def > LastDef && "Defs must be sorted and unique"); diff --git a/llvm/lib/CodeGen/RegAllocFast.cpp b/llvm/lib/CodeGen/RegAllocFast.cpp index 9097728c84e7e..62d7d2f9eed9a 100644 --- a/llvm/lib/CodeGen/RegAllocFast.cpp +++ b/llvm/lib/CodeGen/RegAllocFast.cpp @@ -1291,8 +1291,7 @@ bool RegAllocFastImpl::setPhysReg(MachineInstr &MI, MachineOperand &MO, #ifndef NDEBUG void RegAllocFastImpl::dumpState() const { - for (unsigned Unit = 1, UnitE = TRI->getNumRegUnits(); Unit != UnitE; - ++Unit) { + for (MCRegUnit Unit : TRI->regunits()) { switch (unsigned VirtReg = RegUnitStates[Unit]) { case regFree: break; From ebe745aa231305be07cfd41ac981bf48ba5e119e Mon Sep 17 00:00:00 2001 From: Sergei Barannikov Date: Thu, 13 Nov 2025 19:50:49 +0300 Subject: [PATCH 2/2] Revert some changes --- llvm/lib/CodeGen/LiveIntervals.cpp | 3 +-- llvm/lib/CodeGen/LiveRegMatrix.cpp | 5 ++--- llvm/lib/CodeGen/ReachingDefAnalysis.cpp | 8 ++++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/llvm/lib/CodeGen/LiveIntervals.cpp b/llvm/lib/CodeGen/LiveIntervals.cpp index 9af1f53e5ced0..b600e0411bc48 100644 --- a/llvm/lib/CodeGen/LiveIntervals.cpp +++ b/llvm/lib/CodeGen/LiveIntervals.cpp @@ -182,8 +182,7 @@ void LiveIntervals::print(raw_ostream &OS) const { OS << "********** INTERVALS **********\n"; // Dump the regunits. - assert(RegUnitRanges.size() == TRI->getNumRegUnits()); - for (MCRegUnit Unit : TRI->regunits()) + for (unsigned Unit = 0, UnitE = RegUnitRanges.size(); Unit != UnitE; ++Unit) if (LiveRange *LR = RegUnitRanges[Unit]) OS << printRegUnit(Unit, TRI) << ' ' << *LR << '\n'; diff --git a/llvm/lib/CodeGen/LiveRegMatrix.cpp b/llvm/lib/CodeGen/LiveRegMatrix.cpp index 38d4810c38061..e3ee8dc325933 100644 --- a/llvm/lib/CodeGen/LiveRegMatrix.cpp +++ b/llvm/lib/CodeGen/LiveRegMatrix.cpp @@ -75,9 +75,8 @@ void LiveRegMatrix::init(MachineFunction &MF, LiveIntervals &pLIS, void LiveRegMatrixWrapperLegacy::releaseMemory() { LRM.releaseMemory(); } void LiveRegMatrix::releaseMemory() { - assert(Matrix.size() == TRI->getNumRegUnits()); - for (MCRegUnit Unit : TRI->regunits()) { - Matrix[Unit].clear(); + for (unsigned i = 0, e = Matrix.size(); i != e; ++i) { + Matrix[i].clear(); // No need to clear Queries here, since LiveIntervalUnion::Query doesn't // have anything important to clear and LiveRegMatrix's runOnFunction() // does a std::unique_ptr::reset anyways. diff --git a/llvm/lib/CodeGen/ReachingDefAnalysis.cpp b/llvm/lib/CodeGen/ReachingDefAnalysis.cpp index c0e7469a7bfd7..61706e13b8e91 100644 --- a/llvm/lib/CodeGen/ReachingDefAnalysis.cpp +++ b/llvm/lib/CodeGen/ReachingDefAnalysis.cpp @@ -155,12 +155,12 @@ void ReachingDefInfo::enterBasicBlock(MachineBasicBlock *MBB) { continue; // Find the most recent reaching definition from a predecessor. - for (MCRegUnit Unit : TRI->regunits()) + for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]); } // Insert the most recent reaching definition we found. - for (MCRegUnit Unit : TRI->regunits()) + for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) if (LiveRegs[Unit] != ReachingDefDefaultVal) MBBReachingDefs.append(MBBNumber, Unit, LiveRegs[Unit]); } @@ -235,7 +235,7 @@ void ReachingDefInfo::reprocessBasicBlock(MachineBasicBlock *MBB) { if (Incoming.empty()) continue; - for (MCRegUnit Unit : TRI->regunits()) { + for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) { int Def = Incoming[Unit]; if (Def == ReachingDefDefaultVal) continue; @@ -368,7 +368,7 @@ void ReachingDefInfo::traverse() { // Make sure reaching defs are sorted and unique. for (unsigned MBBNumber = 0, NumBlockIDs = MF->getNumBlockIDs(); MBBNumber != NumBlockIDs; ++MBBNumber) { - for (MCRegUnit Unit : TRI->regunits()) { + for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) { int LastDef = ReachingDefDefaultVal; for (int Def : MBBReachingDefs.defs(MBBNumber, Unit)) { assert(Def > LastDef && "Defs must be sorted and unique");