Skip to content

Commit b191c1f

Browse files
committed
[NFC][regalloc] Pull out some AllocationOrder/CostPerUseLimit eviction logic
We are reusing that logic in the ML implementation. Differential Revision: https://reviews.llvm.org/D116075
1 parent 5265ac7 commit b191c1f

File tree

2 files changed

+55
-26
lines changed

2 files changed

+55
-26
lines changed

llvm/lib/CodeGen/RegAllocEvictionAdvisor.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ class RegAllocEvictionAdvisor {
119119

120120
Register canReassign(LiveInterval &VirtReg, Register PrevReg) const;
121121

122+
// Get the upper limit of elements in the given Order we need to analize.
123+
// TODO: is this heuristic, we could consider learning it.
124+
Optional<unsigned> getOrderLimit(const LiveInterval &VirtReg,
125+
const AllocationOrder &Order,
126+
unsigned CostPerUseLimit) const;
127+
128+
// Determine if it's worth trying to allocate this reg, given the
129+
// CostPerUseLimit
130+
// TODO: this is a heuristic component we could consider learning, too.
131+
bool canAllocatePhysReg(unsigned CostPerUseLimit, MCRegister PhysReg) const;
132+
122133
const MachineFunction &MF;
123134
const RAGreedy &RA;
124135
LiveRegMatrix *const Matrix;

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,28 +714,20 @@ bool RegAllocEvictionAdvisor::isUnusedCalleeSavedReg(MCRegister PhysReg) const {
714714
return !Matrix->isPhysRegUsed(PhysReg);
715715
}
716716

717-
MCRegister DefaultEvictionAdvisor::tryFindEvictionCandidate(
718-
LiveInterval &VirtReg, const AllocationOrder &Order,
719-
uint8_t CostPerUseLimit, const SmallVirtRegSet &FixedRegisters) const {
720-
// Keep track of the cheapest interference seen so far.
721-
EvictionCost BestCost;
722-
BestCost.setMax();
723-
MCRegister BestPhys;
717+
Optional<unsigned>
718+
RegAllocEvictionAdvisor::getOrderLimit(const LiveInterval &VirtReg,
719+
const AllocationOrder &Order,
720+
unsigned CostPerUseLimit) const {
724721
unsigned OrderLimit = Order.getOrder().size();
725722

726-
// When we are just looking for a reduced cost per use, don't break any
727-
// hints, and only evict smaller spill weights.
728723
if (CostPerUseLimit < uint8_t(~0u)) {
729-
BestCost.BrokenHints = 0;
730-
BestCost.MaxWeight = VirtReg.weight();
731-
732724
// Check of any registers in RC are below CostPerUseLimit.
733725
const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg());
734726
uint8_t MinCost = RegClassInfo.getMinCost(RC);
735727
if (MinCost >= CostPerUseLimit) {
736728
LLVM_DEBUG(dbgs() << TRI->getRegClassName(RC) << " minimum cost = "
737729
<< MinCost << ", no cheaper registers to be found.\n");
738-
return 0;
730+
return None;
739731
}
740732

741733
// It is normal for register classes to have a long tail of registers with
@@ -746,24 +738,50 @@ MCRegister DefaultEvictionAdvisor::tryFindEvictionCandidate(
746738
<< " regs.\n");
747739
}
748740
}
741+
return OrderLimit;
742+
}
743+
744+
bool RegAllocEvictionAdvisor::canAllocatePhysReg(unsigned CostPerUseLimit,
745+
MCRegister PhysReg) const {
746+
if (RegCosts[PhysReg] >= CostPerUseLimit)
747+
return false;
748+
// The first use of a callee-saved register in a function has cost 1.
749+
// Don't start using a CSR when the CostPerUseLimit is low.
750+
if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) {
751+
LLVM_DEBUG(
752+
dbgs() << printReg(PhysReg, TRI) << " would clobber CSR "
753+
<< printReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI)
754+
<< '\n');
755+
return false;
756+
}
757+
return true;
758+
}
759+
760+
MCRegister DefaultEvictionAdvisor::tryFindEvictionCandidate(
761+
LiveInterval &VirtReg, const AllocationOrder &Order,
762+
uint8_t CostPerUseLimit, const SmallVirtRegSet &FixedRegisters) const {
763+
// Keep track of the cheapest interference seen so far.
764+
EvictionCost BestCost;
765+
BestCost.setMax();
766+
MCRegister BestPhys;
767+
auto MaybeOrderLimit = getOrderLimit(VirtReg, Order, CostPerUseLimit);
768+
if (!MaybeOrderLimit)
769+
return MCRegister::NoRegister;
770+
unsigned OrderLimit = *MaybeOrderLimit;
771+
772+
// When we are just looking for a reduced cost per use, don't break any
773+
// hints, and only evict smaller spill weights.
774+
if (CostPerUseLimit < uint8_t(~0u)) {
775+
BestCost.BrokenHints = 0;
776+
BestCost.MaxWeight = VirtReg.weight();
777+
}
749778

750779
for (auto I = Order.begin(), E = Order.getOrderLimitEnd(OrderLimit); I != E;
751780
++I) {
752781
MCRegister PhysReg = *I;
753782
assert(PhysReg);
754-
if (RegCosts[PhysReg] >= CostPerUseLimit)
755-
continue;
756-
// The first use of a callee-saved register in a function has cost 1.
757-
// Don't start using a CSR when the CostPerUseLimit is low.
758-
if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) {
759-
LLVM_DEBUG(
760-
dbgs() << printReg(PhysReg, TRI) << " would clobber CSR "
761-
<< printReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI)
762-
<< '\n');
763-
continue;
764-
}
765-
766-
if (!canEvictInterferenceBasedOnCost(VirtReg, PhysReg, false, BestCost,
783+
if (!canAllocatePhysReg(CostPerUseLimit, PhysReg) ||
784+
!canEvictInterferenceBasedOnCost(VirtReg, PhysReg, false, BestCost,
767785
FixedRegisters))
768786
continue;
769787

0 commit comments

Comments
 (0)