From 65e6d4a3f84da042972671dda29134ec66940b53 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Sat, 17 Oct 2015 01:03:44 +0000 Subject: [PATCH] RegisterPressure: Unify the sparse sets in LiveRegsSet; NFC Also do some cleanups comment improvements. llvm-svn: 250598 --- llvm/include/llvm/CodeGen/RegisterPressure.h | 56 ++++++++++++++------ llvm/lib/CodeGen/RegisterPressure.cpp | 31 ++++++----- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/llvm/include/llvm/CodeGen/RegisterPressure.h b/llvm/include/llvm/CodeGen/RegisterPressure.h index 1903ba768dde..67ae00762b24 100644 --- a/llvm/include/llvm/CodeGen/RegisterPressure.h +++ b/llvm/include/llvm/CodeGen/RegisterPressure.h @@ -193,30 +193,56 @@ struct RegPressureDelta { } }; -/// \brief A set of live virtual registers and physical register units. +/// A set of live virtual registers and physical register units. /// -/// Virtual and physical register numbers require separate sparse sets, but most -/// of the RegisterPressureTracker handles them uniformly. -struct LiveRegSet { - SparseSet PhysRegs; - SparseSet VirtRegs; +/// This is a wrapper around a SparseSet which deals with mapping register unit +/// and virtual register indexes to an index usable by the sparse set. +class LiveRegSet { +private: + SparseSet Regs; + unsigned NumRegUnits; + + unsigned getSparseIndexFromReg(unsigned Reg) const { + if (TargetRegisterInfo::isVirtualRegister(Reg)) + return TargetRegisterInfo::virtReg2Index(Reg) + NumRegUnits; + assert(Reg < NumRegUnits); + return Reg; + } + unsigned getRegFromSparseIndex(unsigned SparseIndex) const { + if (SparseIndex >= NumRegUnits) + return TargetRegisterInfo::index2VirtReg(SparseIndex-NumRegUnits); + return SparseIndex; + } + +public: + void clear(); + void init(const MachineRegisterInfo &MRI); bool contains(unsigned Reg) const { - if (TargetRegisterInfo::isVirtualRegister(Reg)) - return VirtRegs.count(Reg); - return PhysRegs.count(Reg); + unsigned SparseIndex = getSparseIndexFromReg(Reg); + return Regs.count(SparseIndex); } bool insert(unsigned Reg) { - if (TargetRegisterInfo::isVirtualRegister(Reg)) - return VirtRegs.insert(Reg).second; - return PhysRegs.insert(Reg).second; + unsigned SparseIndex = getSparseIndexFromReg(Reg); + return Regs.insert(SparseIndex).second; } bool erase(unsigned Reg) { - if (TargetRegisterInfo::isVirtualRegister(Reg)) - return VirtRegs.erase(Reg); - return PhysRegs.erase(Reg); + unsigned SparseIndex = getSparseIndexFromReg(Reg); + return Regs.erase(SparseIndex); + } + + size_t size() const { + return Regs.size(); + } + + template + void appendTo(ContainerT &To) const { + for (unsigned I : Regs) { + unsigned Reg = getRegFromSparseIndex(I); + To.push_back(Reg); + } } }; diff --git a/llvm/lib/CodeGen/RegisterPressure.cpp b/llvm/lib/CodeGen/RegisterPressure.cpp index 8172c558ac72..2ef23c7541de 100644 --- a/llvm/lib/CodeGen/RegisterPressure.cpp +++ b/llvm/lib/CodeGen/RegisterPressure.cpp @@ -157,6 +157,18 @@ void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) { LiveInRegs.clear(); } +void LiveRegSet::init(const MachineRegisterInfo &MRI) { + const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); + unsigned NumRegUnits = TRI.getNumRegs(); + unsigned NumVirtRegs = MRI.getNumVirtRegs(); + Regs.setUniverse(NumRegUnits + NumVirtRegs); + this->NumRegUnits = NumRegUnits; +} + +void LiveRegSet::clear() { + Regs.clear(); +} + const LiveRange *RegPressureTracker::getLiveRange(unsigned Reg) const { if (TargetRegisterInfo::isVirtualRegister(Reg)) return &LIS->getInterval(Reg); @@ -176,8 +188,7 @@ void RegPressureTracker::reset() { else static_cast(P).reset(); - LiveRegs.PhysRegs.clear(); - LiveRegs.VirtRegs.clear(); + LiveRegs.clear(); UntiedDefs.clear(); } @@ -210,8 +221,7 @@ void RegPressureTracker::init(const MachineFunction *mf, P.MaxSetPressure = CurrSetPressure; - LiveRegs.PhysRegs.setUniverse(TRI->getNumRegUnits()); - LiveRegs.VirtRegs.setUniverse(MRI->getNumVirtRegs()); + LiveRegs.init(*MRI); if (TrackUntiedDefs) UntiedDefs.setUniverse(MRI->getNumVirtRegs()); } @@ -250,9 +260,8 @@ void RegPressureTracker::closeTop() { static_cast(P).TopPos = CurrPos; assert(P.LiveInRegs.empty() && "inconsistent max pressure result"); - P.LiveInRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size()); - P.LiveInRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end()); - P.LiveInRegs.append(LiveRegs.VirtRegs.begin(), LiveRegs.VirtRegs.end()); + P.LiveInRegs.reserve(LiveRegs.size()); + LiveRegs.appendTo(P.LiveInRegs); } /// Set the boundary for the bottom of the region and summarize live outs. @@ -263,16 +272,14 @@ void RegPressureTracker::closeBottom() { static_cast(P).BottomPos = CurrPos; assert(P.LiveOutRegs.empty() && "inconsistent max pressure result"); - P.LiveOutRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size()); - P.LiveOutRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end()); - P.LiveOutRegs.append(LiveRegs.VirtRegs.begin(), LiveRegs.VirtRegs.end()); + P.LiveOutRegs.reserve(LiveRegs.size()); + LiveRegs.appendTo(P.LiveOutRegs); } /// Finalize the region boundaries and record live ins and live outs. void RegPressureTracker::closeRegion() { if (!isTopClosed() && !isBottomClosed()) { - assert(LiveRegs.PhysRegs.empty() && LiveRegs.VirtRegs.empty() && - "no region boundary"); + assert(LiveRegs.size() == 0 && "no region boundary"); return; } if (!isBottomClosed())