Newer
Older
Jakob Stoklund Olesen
committed
/// trySpillInterferences - Try to spill interfering registers instead of the
/// current one. Only do it if the accumulated spill weight is smaller than the
/// current spill weight.
unsigned RAGreedy::trySpillInterferences(LiveInterval &VirtReg,
AllocationOrder &Order,
SmallVectorImpl<LiveInterval*> &NewVRegs) {
NamedRegionTimer T("Spill Interference", TimerGroupName, TimePassesIsEnabled);
unsigned BestPhys = 0;
float BestWeight = 0;
Jakob Stoklund Olesen
committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
Order.rewind();
while (unsigned PhysReg = Order.next()) {
float Weight = calcInterferenceWeight(VirtReg, PhysReg);
if (Weight == HUGE_VALF || Weight >= VirtReg.weight)
continue;
if (!BestPhys || Weight < BestWeight)
BestPhys = PhysReg, BestWeight = Weight;
}
// No candidates found.
if (!BestPhys)
return 0;
// Collect all interfering registers.
SmallVector<LiveInterval*, 8> Spills;
for (const unsigned *AI = TRI->getOverlaps(BestPhys); *AI; ++AI) {
LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
Spills.append(Q.interferingVRegs().begin(), Q.interferingVRegs().end());
for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
LiveInterval *VReg = Q.interferingVRegs()[i];
PhysReg2LiveUnion[*AI].extract(*VReg);
VRM->clearVirt(VReg->reg);
}
}
// Spill them all.
DEBUG(dbgs() << "spilling " << Spills.size() << " interferences with weight "
<< BestWeight << '\n');
for (unsigned i = 0, e = Spills.size(); i != e; ++i)
spiller().spill(Spills[i], NewVRegs, Spills);
return BestPhys;
}
//===----------------------------------------------------------------------===//
// Main Entry Point
//===----------------------------------------------------------------------===//
unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen
committed
// First try assigning a free register.
Jakob Stoklund Olesen
committed
AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs);
while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen
committed
if (!checkPhysRegInterference(VirtReg, PhysReg))
Jakob Stoklund Olesen
committed
// Try to reassign interferences.
if (unsigned PhysReg = tryReassign(VirtReg, Order))
return PhysReg;
assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
Jakob Stoklund Olesen
committed
// Try splitting VirtReg or interferences.
unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
if (PhysReg || !NewVRegs.empty())
// Try to spill another interfering reg with less spill weight.
PhysReg = trySpillInterferences(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesen
committed
if (PhysReg)
return PhysReg;
Jakob Stoklund Olesen
committed
// Finally spill VirtReg itself.
NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
SmallVector<LiveInterval*, 1> pendingSpills;
spiller().spill(&VirtReg, NewVRegs, pendingSpills);
// The live virtual register requesting allocation was spilled, so tell
// the caller not to allocate anything during this round.
return 0;
}
bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
<< "********** Function: "
<< ((Value*)mf.getFunction())->getName() << '\n');
MF = &mf;
Jakob Stoklund Olesen
committed
if (VerifyEnabled)
MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesen
committed
RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Indexes = &getAnalysis<SlotIndexes>();
DomTree = &getAnalysis<MachineDominatorTree>();
ReservedRegs = TRI->getReservedRegs(*MF);
Jakob Stoklund Olesen
committed
SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesen
committed
Loops = &getAnalysis<MachineLoopInfo>();
LoopRanges = &getAnalysis<MachineLoopRanges>();
Bundles = &getAnalysis<EdgeBundles>();
SpillPlacer = &getAnalysis<SpillPlacement>();
Jakob Stoklund Olesen
committed
SA.reset(new SplitAnalysis(*MF, *LIS, *Loops));
allocatePhysRegs();
addMBBLiveIns(MF);
Jakob Stoklund Olesen
committed
LIS->addKillFlags();
Jakob Stoklund Olesen
committed
{
NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
rewriter->runOnMachineFunction(*MF, *VRM, LIS);
}
// The pass output is in VirtRegMap. Release all the transient data.
releaseMemory();
return true;
}