2010-10-23 01:09:15 +02:00
|
|
|
//===-- RegAllocBasic.cpp - basic register allocator ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the RABasic function pass, which provides a minimal
|
|
|
|
// implementation of the basic register allocator.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "regalloc"
|
2010-10-26 20:34:01 +02:00
|
|
|
#include "LiveIntervalUnion.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "RegAllocBase.h"
|
|
|
|
#include "RenderMachineFunction.h"
|
|
|
|
#include "Spiller.h"
|
2010-11-08 19:02:08 +01:00
|
|
|
#include "VirtRegMap.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "VirtRegRewriter.h"
|
2010-12-08 00:18:47 +01:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2010-11-11 18:46:29 +01:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/PassAnalysisSupport.h"
|
|
|
|
#include "llvm/CodeGen/CalcSpillWeights.h"
|
2010-11-08 19:02:08 +01:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
|
|
|
#include "llvm/CodeGen/RegisterCoalescer.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2010-11-08 19:02:08 +01:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2010-11-09 22:04:34 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/ADT/SparseBitVector.h"
|
|
|
|
#endif
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-11-08 19:02:08 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-11 01:19:56 +01:00
|
|
|
#include "llvm/Support/Timer.h"
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-12-08 00:18:47 +01:00
|
|
|
#include <cstdlib>
|
2010-10-26 20:34:01 +02:00
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
|
|
|
|
createBasicRegisterAllocator);
|
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
// Temporary verification option until we can put verification inside
|
|
|
|
// MachineVerifier.
|
2010-12-18 00:16:35 +01:00
|
|
|
static cl::opt<bool, true>
|
|
|
|
VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
|
|
|
|
cl::desc("Verify during register allocation"));
|
2010-11-09 22:04:34 +01:00
|
|
|
|
2010-12-11 01:19:56 +01:00
|
|
|
const char *RegAllocBase::TimerGroupName = "Register Allocation";
|
2010-12-18 00:16:35 +01:00
|
|
|
bool RegAllocBase::VerifyEnabled = false;
|
2010-12-11 01:19:56 +01:00
|
|
|
|
2010-11-25 17:42:51 +01:00
|
|
|
namespace {
|
2010-10-23 01:09:15 +02:00
|
|
|
/// RABasic provides a minimal implementation of the basic register allocation
|
|
|
|
/// algorithm. It prioritizes live virtual registers by spill weight and spills
|
|
|
|
/// whenever a register is unavailable. This is not practical in production but
|
|
|
|
/// provides a useful baseline both for measuring other allocators and comparing
|
|
|
|
/// the speed of the basic algorithm against other styles of allocators.
|
|
|
|
class RABasic : public MachineFunctionPass, public RegAllocBase
|
|
|
|
{
|
|
|
|
// context
|
2010-12-01 00:18:47 +01:00
|
|
|
MachineFunction *MF;
|
|
|
|
BitVector ReservedRegs;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
// analyses
|
2010-12-01 00:18:47 +01:00
|
|
|
LiveStacks *LS;
|
|
|
|
RenderMachineFunction *RMF;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
// state
|
2010-12-01 00:18:47 +01:00
|
|
|
std::auto_ptr<Spiller> SpillerInstance;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
RABasic();
|
|
|
|
|
|
|
|
/// Return the pass name.
|
|
|
|
virtual const char* getPassName() const {
|
|
|
|
return "Basic Register Allocator";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RABasic analysis usage.
|
2010-12-01 00:18:47 +01:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
virtual void releaseMemory();
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
virtual Spiller &spiller() { return *SpillerInstance; }
|
2010-11-10 20:18:47 +01:00
|
|
|
|
2010-12-08 23:22:41 +01:00
|
|
|
virtual float getPriority(LiveInterval *LI) { return LI->weight; }
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
virtual unsigned selectOrSplit(LiveInterval &VirtReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs);
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
/// Perform register allocation.
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &mf);
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
};
|
|
|
|
|
|
|
|
char RABasic::ID = 0;
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
RABasic::RABasic(): MachineFunctionPass(ID) {
|
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry());
|
|
|
|
initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeLiveStacksPass(*PassRegistry::getPassRegistry());
|
2010-11-03 21:39:26 +01:00
|
|
|
initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
|
2010-10-23 01:09:15 +02:00
|
|
|
initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
|
|
|
AU.addRequired<LiveIntervals>();
|
|
|
|
AU.addPreserved<SlotIndexes>();
|
2010-10-23 01:09:15 +02:00
|
|
|
if (StrongPHIElim)
|
2010-12-01 00:18:47 +01:00
|
|
|
AU.addRequiredID(StrongPHIEliminationID);
|
|
|
|
AU.addRequiredTransitive<RegisterCoalescer>();
|
|
|
|
AU.addRequired<CalculateSpillWeights>();
|
|
|
|
AU.addRequired<LiveStacks>();
|
|
|
|
AU.addPreserved<LiveStacks>();
|
|
|
|
AU.addRequiredID(MachineDominatorsID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addRequired<VirtRegMap>();
|
|
|
|
AU.addPreserved<VirtRegMap>();
|
|
|
|
DEBUG(AU.addRequired<RenderMachineFunction>());
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RABasic::releaseMemory() {
|
2010-12-01 00:18:47 +01:00
|
|
|
SpillerInstance.reset(0);
|
2010-10-23 01:09:15 +02:00
|
|
|
RegAllocBase::releaseMemory();
|
|
|
|
}
|
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify each LiveIntervalUnion.
|
|
|
|
void RegAllocBase::verify() {
|
2010-12-01 00:18:47 +01:00
|
|
|
LiveVirtRegBitSet VisitedVRegs;
|
|
|
|
OwningArrayPtr<LiveVirtRegBitSet>
|
|
|
|
unionVRegs(new LiveVirtRegBitSet[PhysReg2LiveUnion.numRegs()]);
|
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
// Verify disjoint unions.
|
2010-12-01 00:18:47 +01:00
|
|
|
for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) {
|
2010-12-14 19:53:47 +01:00
|
|
|
DEBUG(PhysReg2LiveUnion[PhysReg].print(dbgs(), TRI));
|
2010-12-01 00:18:47 +01:00
|
|
|
LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg];
|
|
|
|
PhysReg2LiveUnion[PhysReg].verify(VRegs);
|
2010-11-09 22:04:34 +01:00
|
|
|
// Union + intersection test could be done efficiently in one pass, but
|
|
|
|
// don't add a method to SparseBitVector unless we really need it.
|
2010-12-01 00:18:47 +01:00
|
|
|
assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions");
|
|
|
|
VisitedVRegs |= VRegs;
|
2010-11-09 22:04:34 +01:00
|
|
|
}
|
2010-12-01 00:18:47 +01:00
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
// Verify vreg coverage.
|
2010-12-01 00:18:47 +01:00
|
|
|
for (LiveIntervals::iterator liItr = LIS->begin(), liEnd = LIS->end();
|
2010-11-09 22:04:34 +01:00
|
|
|
liItr != liEnd; ++liItr) {
|
|
|
|
unsigned reg = liItr->first;
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(reg)) continue;
|
2010-12-01 00:18:47 +01:00
|
|
|
if (!VRM->hasPhys(reg)) continue; // spilled?
|
|
|
|
unsigned PhysReg = VRM->getPhys(reg);
|
|
|
|
if (!unionVRegs[PhysReg].test(reg)) {
|
2010-11-09 22:04:34 +01:00
|
|
|
dbgs() << "LiveVirtReg " << reg << " not in union " <<
|
2010-12-01 00:18:47 +01:00
|
|
|
TRI->getName(PhysReg) << "\n";
|
2010-11-09 22:04:34 +01:00
|
|
|
llvm_unreachable("unallocated live vreg");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME: I'm not sure how to verify spilled intervals.
|
|
|
|
}
|
|
|
|
#endif //!NDEBUG
|
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegAllocBase Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Instantiate a LiveIntervalUnion for each physical register.
|
2010-12-08 00:18:47 +01:00
|
|
|
void RegAllocBase::LiveUnionArray::init(LiveIntervalUnion::Allocator &allocator,
|
|
|
|
unsigned NRegs) {
|
2010-12-01 00:18:47 +01:00
|
|
|
NumRegs = NRegs;
|
2010-12-08 00:18:47 +01:00
|
|
|
Array =
|
|
|
|
static_cast<LiveIntervalUnion*>(malloc(sizeof(LiveIntervalUnion)*NRegs));
|
|
|
|
for (unsigned r = 0; r != NRegs; ++r)
|
|
|
|
new(Array + r) LiveIntervalUnion(r, allocator);
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
2010-12-11 00:49:00 +01:00
|
|
|
void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
|
2010-12-11 01:19:56 +01:00
|
|
|
NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
|
2010-12-11 00:49:00 +01:00
|
|
|
TRI = &vrm.getTargetRegInfo();
|
|
|
|
MRI = &vrm.getRegInfo();
|
2010-12-01 00:18:47 +01:00
|
|
|
VRM = &vrm;
|
|
|
|
LIS = &lis;
|
2010-12-08 00:18:47 +01:00
|
|
|
PhysReg2LiveUnion.init(UnionAllocator, TRI->getNumRegs());
|
2010-11-08 19:02:08 +01:00
|
|
|
// Cache an interferece query for each physical reg
|
2010-12-01 00:18:47 +01:00
|
|
|
Queries.reset(new LiveIntervalUnion::Query[PhysReg2LiveUnion.numRegs()]);
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
void RegAllocBase::LiveUnionArray::clear() {
|
2010-12-08 00:18:47 +01:00
|
|
|
if (!Array)
|
|
|
|
return;
|
|
|
|
for (unsigned r = 0; r != NumRegs; ++r)
|
|
|
|
Array[r].~LiveIntervalUnion();
|
|
|
|
free(Array);
|
2010-12-01 00:18:47 +01:00
|
|
|
NumRegs = 0;
|
2010-12-08 00:18:47 +01:00
|
|
|
Array = 0;
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAllocBase::releaseMemory() {
|
2010-12-01 00:18:47 +01:00
|
|
|
PhysReg2LiveUnion.clear();
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
2010-10-26 20:34:01 +02:00
|
|
|
// Visit all the live virtual registers. If they are already assigned to a
|
|
|
|
// physical register, unify them with the corresponding LiveIntervalUnion,
|
|
|
|
// otherwise push them on the priority queue for later assignment.
|
2010-12-08 23:22:41 +01:00
|
|
|
void RegAllocBase::
|
|
|
|
seedLiveVirtRegs(std::priority_queue<std::pair<float, unsigned> > &VirtRegQ) {
|
2010-12-01 00:18:47 +01:00
|
|
|
for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end(); I != E; ++I) {
|
|
|
|
unsigned RegNum = I->first;
|
|
|
|
LiveInterval &VirtReg = *I->second;
|
2010-12-08 23:22:41 +01:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(RegNum))
|
2010-12-01 00:18:47 +01:00
|
|
|
PhysReg2LiveUnion[RegNum].unify(VirtReg);
|
2010-12-08 23:22:41 +01:00
|
|
|
else
|
|
|
|
VirtRegQ.push(std::make_pair(getPriority(&VirtReg), RegNum));
|
2010-10-26 20:34:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-09 02:14:03 +01:00
|
|
|
void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) {
|
|
|
|
assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
|
|
|
|
VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
|
|
|
|
PhysReg2LiveUnion[PhysReg].unify(VirtReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) {
|
|
|
|
assert(VRM->getPhys(VirtReg.reg) == PhysReg && "Inconsistent unassign");
|
|
|
|
PhysReg2LiveUnion[PhysReg].extract(VirtReg);
|
|
|
|
VRM->clearVirt(VirtReg.reg);
|
|
|
|
}
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// Top-level driver to manage the queue of unassigned VirtRegs and call the
|
2010-10-26 20:34:01 +02:00
|
|
|
// selectOrSplit implementation.
|
|
|
|
void RegAllocBase::allocatePhysRegs() {
|
2010-12-01 00:18:47 +01:00
|
|
|
|
|
|
|
// Push each vreg onto a queue or "precolor" by adding it to a physreg union.
|
2010-12-08 23:22:41 +01:00
|
|
|
std::priority_queue<std::pair<float, unsigned> > VirtRegQ;
|
2010-12-01 00:18:47 +01:00
|
|
|
seedLiveVirtRegs(VirtRegQ);
|
|
|
|
|
|
|
|
// Continue assigning vregs one at a time to available physical registers.
|
|
|
|
while (!VirtRegQ.empty()) {
|
|
|
|
// Pop the highest priority vreg.
|
2010-12-08 23:22:41 +01:00
|
|
|
LiveInterval &VirtReg = LIS->getInterval(VirtRegQ.top().second);
|
|
|
|
VirtRegQ.pop();
|
2010-12-01 00:18:47 +01:00
|
|
|
|
|
|
|
// selectOrSplit requests the allocator to return an available physical
|
|
|
|
// register if possible and populate a list of new live intervals that
|
|
|
|
// result from splitting.
|
2010-12-11 00:49:00 +01:00
|
|
|
DEBUG(dbgs() << "\nselectOrSplit " << MRI->getRegClass(VirtReg.reg)->getName()
|
|
|
|
<< ':' << VirtReg << '\n');
|
2010-12-01 00:18:47 +01:00
|
|
|
typedef SmallVector<LiveInterval*, 4> VirtRegVec;
|
|
|
|
VirtRegVec SplitVRegs;
|
2010-12-08 23:22:41 +01:00
|
|
|
unsigned AvailablePhysReg = selectOrSplit(VirtReg, SplitVRegs);
|
2010-12-01 00:18:47 +01:00
|
|
|
|
|
|
|
if (AvailablePhysReg) {
|
2010-12-11 00:49:00 +01:00
|
|
|
DEBUG(dbgs() << "allocating: " << TRI->getName(AvailablePhysReg)
|
|
|
|
<< " for " << VirtReg << '\n');
|
2011-02-09 02:14:03 +01:00
|
|
|
assign(VirtReg, AvailablePhysReg);
|
2010-10-26 20:34:01 +02:00
|
|
|
}
|
2010-12-01 00:18:47 +01:00
|
|
|
for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
LiveInterval* SplitVirtReg = *I;
|
|
|
|
if (SplitVirtReg->empty()) continue;
|
|
|
|
DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
|
2010-11-08 19:02:08 +01:00
|
|
|
"expect split value in virtual register");
|
2010-12-08 23:22:41 +01:00
|
|
|
VirtRegQ.push(std::make_pair(getPriority(SplitVirtReg),
|
|
|
|
SplitVirtReg->reg));
|
2010-10-26 20:34:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// Check if this live virtual register interferes with a physical register. If
|
|
|
|
// not, then check for interference on each register that aliases with the
|
|
|
|
// physical register. Return the interfering register.
|
|
|
|
unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg,
|
|
|
|
unsigned PhysReg) {
|
2010-12-15 00:10:48 +01:00
|
|
|
for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI)
|
2010-12-01 00:18:47 +01:00
|
|
|
if (query(VirtReg, *AliasI).checkInterference())
|
|
|
|
return *AliasI;
|
2010-11-08 19:02:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// Helper for spillInteferences() that spills all interfering vregs currently
|
|
|
|
// assigned to this physical register.
|
|
|
|
void RegAllocBase::spillReg(LiveInterval& VirtReg, unsigned PhysReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs) {
|
|
|
|
LiveIntervalUnion::Query &Q = query(VirtReg, PhysReg);
|
|
|
|
assert(Q.seenAllInterferences() && "need collectInterferences()");
|
|
|
|
const SmallVectorImpl<LiveInterval*> &PendingSpills = Q.interferingVRegs();
|
|
|
|
|
|
|
|
for (SmallVectorImpl<LiveInterval*>::const_iterator I = PendingSpills.begin(),
|
|
|
|
E = PendingSpills.end(); I != E; ++I) {
|
|
|
|
LiveInterval &SpilledVReg = **I;
|
2010-11-11 18:46:29 +01:00
|
|
|
DEBUG(dbgs() << "extracting from " <<
|
2010-12-01 00:18:47 +01:00
|
|
|
TRI->getName(PhysReg) << " " << SpilledVReg << '\n');
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-11-10 20:18:47 +01:00
|
|
|
// Deallocate the interfering vreg by removing it from the union.
|
|
|
|
// A LiveInterval instance may not be in a union during modification!
|
2011-02-09 02:14:03 +01:00
|
|
|
unassign(SpilledVReg, PhysReg);
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-11-10 20:18:47 +01:00
|
|
|
// Spill the extracted interval.
|
2010-12-01 00:18:47 +01:00
|
|
|
spiller().spill(&SpilledVReg, SplitVRegs, PendingSpills);
|
2010-11-10 20:18:47 +01:00
|
|
|
}
|
2010-11-11 18:46:29 +01:00
|
|
|
// After extracting segments, the query's results are invalid. But keep the
|
|
|
|
// contents valid until we're done accessing pendingSpills.
|
|
|
|
Q.clear();
|
2010-11-10 20:18:47 +01:00
|
|
|
}
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// Spill or split all live virtual registers currently unified under PhysReg
|
|
|
|
// that interfere with VirtReg. The newly spilled or split live intervals are
|
|
|
|
// returned by appending them to SplitVRegs.
|
2010-11-10 20:18:47 +01:00
|
|
|
bool
|
2010-12-01 00:18:47 +01:00
|
|
|
RegAllocBase::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs) {
|
2010-11-10 20:18:47 +01:00
|
|
|
// Record each interference and determine if all are spillable before mutating
|
|
|
|
// either the union or live intervals.
|
2010-12-15 00:10:48 +01:00
|
|
|
unsigned NumInterferences = 0;
|
2010-11-11 18:46:29 +01:00
|
|
|
// Collect interferences assigned to any alias of the physical register.
|
2010-12-15 00:10:48 +01:00
|
|
|
for (const unsigned *asI = TRI->getOverlaps(PhysReg); *asI; ++asI) {
|
2010-12-01 00:18:47 +01:00
|
|
|
LiveIntervalUnion::Query &QAlias = query(VirtReg, *asI);
|
|
|
|
NumInterferences += QAlias.collectInterferingVRegs();
|
2010-11-11 18:46:29 +01:00
|
|
|
if (QAlias.seenUnspillableVReg()) {
|
2010-11-10 20:18:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-08 19:02:08 +01:00
|
|
|
}
|
2010-12-01 00:18:47 +01:00
|
|
|
DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
|
|
|
|
" interferences with " << VirtReg << "\n");
|
|
|
|
assert(NumInterferences > 0 && "expect interference");
|
|
|
|
|
|
|
|
// Spill each interfering vreg allocated to PhysReg or an alias.
|
2010-12-15 00:10:48 +01:00
|
|
|
for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI)
|
2010-12-01 00:18:47 +01:00
|
|
|
spillReg(VirtReg, *AliasI, SplitVRegs);
|
2010-11-10 20:18:47 +01:00
|
|
|
return true;
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 02:06:06 +01:00
|
|
|
// Add newly allocated physical registers to the MBB live in sets.
|
|
|
|
void RegAllocBase::addMBBLiveIns(MachineFunction *MF) {
|
2010-12-11 01:19:56 +01:00
|
|
|
NamedRegionTimer T("MBB Live Ins", TimerGroupName, TimePassesIsEnabled);
|
2010-12-08 02:06:06 +01:00
|
|
|
typedef SmallVector<MachineBasicBlock*, 8> MBBVec;
|
|
|
|
MBBVec liveInMBBs;
|
|
|
|
MachineBasicBlock &entryMBB = *MF->begin();
|
|
|
|
|
|
|
|
for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) {
|
|
|
|
LiveIntervalUnion &LiveUnion = PhysReg2LiveUnion[PhysReg];
|
|
|
|
if (LiveUnion.empty())
|
|
|
|
continue;
|
|
|
|
for (LiveIntervalUnion::SegmentIter SI = LiveUnion.begin(); SI.valid();
|
|
|
|
++SI) {
|
|
|
|
|
|
|
|
// Find the set of basic blocks which this range is live into...
|
|
|
|
liveInMBBs.clear();
|
|
|
|
if (!LIS->findLiveInMBBs(SI.start(), SI.stop(), liveInMBBs)) continue;
|
|
|
|
|
|
|
|
// And add the physreg for this interval to their live-in sets.
|
|
|
|
for (MBBVec::iterator I = liveInMBBs.begin(), E = liveInMBBs.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineBasicBlock *MBB = *I;
|
|
|
|
if (MBB == &entryMBB) continue;
|
|
|
|
if (MBB->isLiveIn(PhysReg)) continue;
|
|
|
|
MBB->addLiveIn(PhysReg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RABasic Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Driver for the register assignment and splitting heuristics.
|
|
|
|
// Manages iteration over the LiveIntervalUnions.
|
2010-11-20 03:43:55 +01:00
|
|
|
//
|
2010-12-01 00:18:47 +01:00
|
|
|
// This is a minimal implementation of register assignment and splitting that
|
|
|
|
// spills whenever we run out of registers.
|
2010-10-23 01:09:15 +02:00
|
|
|
//
|
|
|
|
// selectOrSplit can only be called once per live virtual register. We then do a
|
|
|
|
// single interference test for each register the correct class until we find an
|
|
|
|
// available register. So, the number of interference tests in the worst case is
|
|
|
|
// |vregs| * |machineregs|. And since the number of interference tests is
|
2010-12-01 00:18:47 +01:00
|
|
|
// minimal, there is no value in caching them outside the scope of
|
|
|
|
// selectOrSplit().
|
|
|
|
unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs) {
|
2010-11-10 20:18:47 +01:00
|
|
|
// Populate a list of physical register spill candidates.
|
2010-12-01 00:18:47 +01:00
|
|
|
SmallVector<unsigned, 8> PhysRegSpillCands;
|
2010-11-08 19:02:08 +01:00
|
|
|
|
2010-11-20 03:43:55 +01:00
|
|
|
// Check for an available register in this class.
|
2010-12-01 00:18:47 +01:00
|
|
|
const TargetRegisterClass *TRC = MRI->getRegClass(VirtReg.reg);
|
|
|
|
|
|
|
|
for (TargetRegisterClass::iterator I = TRC->allocation_order_begin(*MF),
|
|
|
|
E = TRC->allocation_order_end(*MF);
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
unsigned PhysReg = *I;
|
|
|
|
if (ReservedRegs.test(PhysReg)) continue;
|
|
|
|
|
|
|
|
// Check interference and as a side effect, intialize queries for this
|
|
|
|
// VirtReg and its aliases.
|
|
|
|
unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg);
|
2010-11-08 19:02:08 +01:00
|
|
|
if (interfReg == 0) {
|
2010-11-10 20:18:47 +01:00
|
|
|
// Found an available register.
|
2010-12-01 00:18:47 +01:00
|
|
|
return PhysReg;
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
2010-11-10 20:18:47 +01:00
|
|
|
LiveInterval *interferingVirtReg =
|
2010-12-08 00:18:47 +01:00
|
|
|
Queries[interfReg].firstInterference().liveUnionPos().value();
|
2010-11-10 20:18:47 +01:00
|
|
|
|
2010-12-09 19:15:21 +01:00
|
|
|
// The current VirtReg must either be spillable, or one of its interferences
|
2010-12-01 00:18:47 +01:00
|
|
|
// must have less spill weight.
|
|
|
|
if (interferingVirtReg->weight < VirtReg.weight ) {
|
|
|
|
PhysRegSpillCands.push_back(PhysReg);
|
2010-11-08 19:02:08 +01:00
|
|
|
}
|
2010-10-23 01:09:15 +02:00
|
|
|
}
|
2010-11-10 20:18:47 +01:00
|
|
|
// Try to spill another interfering reg with less spill weight.
|
2010-12-01 00:18:47 +01:00
|
|
|
for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
|
|
|
|
PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
|
2010-11-10 20:18:47 +01:00
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue;
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-12-07 19:51:27 +01:00
|
|
|
assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 &&
|
|
|
|
"Interference after spill.");
|
2010-11-10 20:18:47 +01:00
|
|
|
// Tell the caller to allocate to this newly freed physical register.
|
2010-12-01 00:18:47 +01:00
|
|
|
return *PhysRegI;
|
2010-11-08 19:02:08 +01:00
|
|
|
}
|
2010-12-01 00:18:47 +01:00
|
|
|
// No other spill candidates were found, so spill the current VirtReg.
|
|
|
|
DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
|
2010-11-10 20:18:47 +01:00
|
|
|
SmallVector<LiveInterval*, 1> pendingSpills;
|
2010-12-01 00:18:47 +01:00
|
|
|
|
|
|
|
spiller().spill(&VirtReg, SplitVRegs, pendingSpills);
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-11-10 20:18:47 +01:00
|
|
|
// The live virtual register requesting allocation was spilled, so tell
|
|
|
|
// the caller not to allocate anything during this round.
|
|
|
|
return 0;
|
2010-11-08 19:02:08 +01:00
|
|
|
}
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
bool RABasic::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
|
|
|
|
<< "********** Function: "
|
|
|
|
<< ((Value*)mf.getFunction())->getName() << '\n');
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
MF = &mf;
|
|
|
|
DEBUG(RMF = &getAnalysis<RenderMachineFunction>());
|
2010-11-11 18:46:29 +01:00
|
|
|
|
2010-12-11 00:49:00 +01:00
|
|
|
RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
ReservedRegs = TRI->getReservedRegs(*MF);
|
2010-11-11 18:46:29 +01:00
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
SpillerInstance.reset(createSpiller(*this, *MF, *VRM));
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-10-26 20:34:01 +02:00
|
|
|
allocatePhysRegs();
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-12-08 02:06:06 +01:00
|
|
|
addMBBLiveIns(MF);
|
2010-11-20 03:57:05 +01:00
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
// Diagnostic output before rewriting
|
2010-12-01 00:18:47 +01:00
|
|
|
DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
|
2010-10-23 01:09:15 +02:00
|
|
|
|
|
|
|
// optional HTML output
|
2010-12-01 00:18:47 +01:00
|
|
|
DEBUG(RMF->renderMachineFunction("After basic register allocation.", VRM));
|
2010-10-23 01:09:15 +02:00
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
// FIXME: Verification currently must run before VirtRegRewriter. We should
|
|
|
|
// make the rewriter a separate pass and override verifyAnalysis instead. When
|
|
|
|
// that happens, verification naturally falls under VerifyMachineCode.
|
|
|
|
#ifndef NDEBUG
|
2010-12-18 00:16:35 +01:00
|
|
|
if (VerifyEnabled) {
|
2010-11-09 22:04:34 +01:00
|
|
|
// Verify accuracy of LiveIntervals. The standard machine code verifier
|
|
|
|
// ensures that each LiveIntervals covers all uses of the virtual reg.
|
|
|
|
|
2010-12-01 00:18:47 +01:00
|
|
|
// FIXME: MachineVerifier is badly broken when using the standard
|
|
|
|
// spiller. Always use -spiller=inline with -verify-regalloc. Even with the
|
|
|
|
// inline spiller, some tests fail to verify because the coalescer does not
|
|
|
|
// always generate verifiable code.
|
2010-12-18 01:06:56 +01:00
|
|
|
MF->verify(this, "In RABasic::verify");
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-11-09 22:04:34 +01:00
|
|
|
// Verify that LiveIntervals are partitioned into unions and disjoint within
|
|
|
|
// the unions.
|
|
|
|
verify();
|
|
|
|
}
|
|
|
|
#endif // !NDEBUG
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
// Run rewriter
|
|
|
|
std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter());
|
2010-12-01 00:18:47 +01:00
|
|
|
rewriter->runOnMachineFunction(*MF, *VRM, LIS);
|
2010-10-26 20:34:01 +02:00
|
|
|
|
|
|
|
// The pass output is in VirtRegMap. Release all the transient data.
|
|
|
|
releaseMemory();
|
2010-11-20 03:43:55 +01:00
|
|
|
|
2010-10-23 01:09:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-20 03:43:55 +01:00
|
|
|
FunctionPass* llvm::createBasicRegisterAllocator()
|
2010-10-23 01:09:15 +02:00
|
|
|
{
|
|
|
|
return new RABasic();
|
|
|
|
}
|