Skip to content

Commit

Permalink
[greedyalloc] Return early when there is no register to allocate.
Browse files Browse the repository at this point in the history
In X86 we split greddy register allocation into 2 passes. The 1st pass
is to allocate tile register, and the 2nd pass is to allocate the rest
of virtual register. In most cases there is no tile register, so the 1st
pass is unnecessary. To improve the compiling time, we check if there is
any register need to be allocated by invoking callback
`ShouldAllocateClass`. If there is no register to be allocated, just
return false in the pass. This would improve the 1st greed RA pass for
normal cases.

Differential Revision: https://reviews.llvm.org/D128804
  • Loading branch information
LuoYuanke committed Jun 30, 2022
1 parent 1b8cde9 commit fa8656d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions llvm/lib/CodeGen/RegAllocGreedy.cpp
Expand Up @@ -2488,6 +2488,21 @@ void RAGreedy::reportStats() {
}
}

bool RAGreedy::hasVirtRegAlloc() {
for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
Register Reg = Register::index2VirtReg(I);
if (MRI->reg_nodbg_empty(Reg))
continue;
const TargetRegisterClass *RC = MRI->getRegClass(Reg);
if (!RC)
continue;
if (ShouldAllocateClass(*TRI, *RC))
return true;
}

return false;
}

bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
LLVM_DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
<< "********** Function: " << mf.getName() << '\n');
Expand All @@ -2501,6 +2516,12 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
RegAllocBase::init(getAnalysis<VirtRegMap>(),
getAnalysis<LiveIntervals>(),
getAnalysis<LiveRegMatrix>());

// Early return if there is no virtual register to be allocated to a
// physical register.
if (!hasVirtRegAlloc())
return false;

Indexes = &getAnalysis<SlotIndexes>();
MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
DomTree = &getAnalysis<MachineDominatorTree>();
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/RegAllocGreedy.h
Expand Up @@ -315,6 +315,7 @@ class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
void enqueue(PQueue &CurQueue, const LiveInterval *LI);
const LiveInterval *dequeue(PQueue &CurQueue);

bool hasVirtRegAlloc();
BlockFrequency calcSpillCost();
bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency &);
bool addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
Expand Down

0 comments on commit fa8656d

Please sign in to comment.