2010-10-23 07:09:15 +08: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"
|
2011-06-04 04:34:53 +08:00
|
|
|
#include "RegAllocBase.h"
|
2011-04-06 05:40:37 +08:00
|
|
|
#include "LiveDebugVariables.h"
|
2010-10-27 02:34:01 +08:00
|
|
|
#include "LiveIntervalUnion.h"
|
2011-03-10 09:51:42 +08:00
|
|
|
#include "LiveRangeEdit.h"
|
2010-10-23 07:09:15 +08:00
|
|
|
#include "RenderMachineFunction.h"
|
|
|
|
#include "Spiller.h"
|
2010-11-09 02:02:08 +08:00
|
|
|
#include "VirtRegMap.h"
|
2010-12-08 07:18:47 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2011-02-18 06:53:48 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-11-12 01:46:29 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2010-10-23 07:09:15 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/PassAnalysisSupport.h"
|
|
|
|
#include "llvm/CodeGen/CalcSpillWeights.h"
|
2010-11-09 02:02:08 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2010-10-23 07:09:15 +08: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/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2010-11-09 02:02:08 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2010-11-10 05:04:34 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/ADT/SparseBitVector.h"
|
|
|
|
#endif
|
2010-10-23 07:09:15 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-11-09 02:02:08 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-10-23 07:09:15 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-11 08:19:56 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2010-10-23 07:09:15 +08:00
|
|
|
|
2010-12-08 07:18:47 +08:00
|
|
|
#include <cstdlib>
|
2011-02-23 07:01:52 +08:00
|
|
|
#include <queue>
|
2010-10-27 02:34:01 +08:00
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-02-18 06:53:48 +08:00
|
|
|
STATISTIC(NumAssigned , "Number of registers assigned");
|
|
|
|
STATISTIC(NumUnassigned , "Number of registers unassigned");
|
|
|
|
STATISTIC(NumNewQueued , "Number of new live ranges queued");
|
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
|
|
|
|
createBasicRegisterAllocator);
|
|
|
|
|
2010-11-10 05:04:34 +08:00
|
|
|
// Temporary verification option until we can put verification inside
|
|
|
|
// MachineVerifier.
|
2010-12-18 07:16:35 +08:00
|
|
|
static cl::opt<bool, true>
|
|
|
|
VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
|
|
|
|
cl::desc("Verify during register allocation"));
|
2010-11-10 05:04:34 +08:00
|
|
|
|
2010-12-11 08:19:56 +08:00
|
|
|
const char *RegAllocBase::TimerGroupName = "Register Allocation";
|
2010-12-18 07:16:35 +08:00
|
|
|
bool RegAllocBase::VerifyEnabled = false;
|
2010-12-11 08:19:56 +08:00
|
|
|
|
2011-02-23 07:01:52 +08:00
|
|
|
namespace {
|
|
|
|
struct CompSpillWeight {
|
|
|
|
bool operator()(LiveInterval *A, LiveInterval *B) const {
|
|
|
|
return A->weight < B->weight;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-11-26 00:42:51 +08:00
|
|
|
namespace {
|
2010-10-23 07:09:15 +08: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 07:18:47 +08:00
|
|
|
MachineFunction *MF;
|
2010-10-23 07:09:15 +08:00
|
|
|
|
|
|
|
// analyses
|
2010-12-01 07:18:47 +08:00
|
|
|
LiveStacks *LS;
|
|
|
|
RenderMachineFunction *RMF;
|
2010-10-23 07:09:15 +08:00
|
|
|
|
|
|
|
// state
|
2010-12-01 07:18:47 +08:00
|
|
|
std::auto_ptr<Spiller> SpillerInstance;
|
2011-02-23 07:01:52 +08:00
|
|
|
std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
|
|
|
|
CompSpillWeight> Queue;
|
2010-10-23 07:09:15 +08:00
|
|
|
public:
|
|
|
|
RABasic();
|
|
|
|
|
|
|
|
/// Return the pass name.
|
|
|
|
virtual const char* getPassName() const {
|
|
|
|
return "Basic Register Allocator";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RABasic analysis usage.
|
2010-12-01 07:18:47 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
2010-10-23 07:09:15 +08:00
|
|
|
|
|
|
|
virtual void releaseMemory();
|
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
virtual Spiller &spiller() { return *SpillerInstance; }
|
2010-11-11 03:18:47 +08:00
|
|
|
|
2010-12-09 06:22:41 +08:00
|
|
|
virtual float getPriority(LiveInterval *LI) { return LI->weight; }
|
|
|
|
|
2011-02-23 07:01:52 +08:00
|
|
|
virtual void enqueue(LiveInterval *LI) {
|
|
|
|
Queue.push(LI);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual LiveInterval *dequeue() {
|
|
|
|
if (Queue.empty())
|
|
|
|
return 0;
|
|
|
|
LiveInterval *LI = Queue.top();
|
|
|
|
Queue.pop();
|
|
|
|
return LI;
|
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
virtual unsigned selectOrSplit(LiveInterval &VirtReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs);
|
2010-10-23 07:09:15 +08:00
|
|
|
|
|
|
|
/// Perform register allocation.
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &mf);
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
};
|
|
|
|
|
|
|
|
char RABasic::ID = 0;
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
RABasic::RABasic(): MachineFunctionPass(ID) {
|
2011-04-06 05:40:37 +08:00
|
|
|
initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
|
2010-10-23 07:09:15 +08:00
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
|
2011-06-27 06:34:10 +08:00
|
|
|
initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
|
2010-10-23 07:09:15 +08:00
|
|
|
initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeLiveStacksPass(*PassRegistry::getPassRegistry());
|
2010-11-04 04:39:26 +08:00
|
|
|
initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
|
2010-10-23 07:09:15 +08:00
|
|
|
initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
|
|
|
AU.addRequired<LiveIntervals>();
|
|
|
|
AU.addPreserved<SlotIndexes>();
|
2011-04-06 05:40:37 +08:00
|
|
|
AU.addRequired<LiveDebugVariables>();
|
|
|
|
AU.addPreserved<LiveDebugVariables>();
|
2010-10-23 07:09:15 +08:00
|
|
|
if (StrongPHIElim)
|
2010-12-01 07:18:47 +08:00
|
|
|
AU.addRequiredID(StrongPHIEliminationID);
|
2011-08-09 08:29:53 +08:00
|
|
|
AU.addRequiredTransitiveID(RegisterCoalescerPassID);
|
2010-12-01 07:18:47 +08:00
|
|
|
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 07:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RABasic::releaseMemory() {
|
2010-12-01 07:18:47 +08:00
|
|
|
SpillerInstance.reset(0);
|
2010-10-23 07:09:15 +08:00
|
|
|
RegAllocBase::releaseMemory();
|
|
|
|
}
|
|
|
|
|
2010-11-10 05:04:34 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify each LiveIntervalUnion.
|
|
|
|
void RegAllocBase::verify() {
|
2010-12-01 07:18:47 +08:00
|
|
|
LiveVirtRegBitSet VisitedVRegs;
|
|
|
|
OwningArrayPtr<LiveVirtRegBitSet>
|
|
|
|
unionVRegs(new LiveVirtRegBitSet[PhysReg2LiveUnion.numRegs()]);
|
|
|
|
|
2010-11-10 05:04:34 +08:00
|
|
|
// Verify disjoint unions.
|
2010-12-01 07:18:47 +08:00
|
|
|
for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) {
|
2010-12-15 02:53:47 +08:00
|
|
|
DEBUG(PhysReg2LiveUnion[PhysReg].print(dbgs(), TRI));
|
2010-12-01 07:18:47 +08:00
|
|
|
LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg];
|
|
|
|
PhysReg2LiveUnion[PhysReg].verify(VRegs);
|
2010-11-10 05:04:34 +08: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 07:18:47 +08:00
|
|
|
assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions");
|
|
|
|
VisitedVRegs |= VRegs;
|
2010-11-10 05:04:34 +08:00
|
|
|
}
|
2010-12-01 07:18:47 +08:00
|
|
|
|
2010-11-10 05:04:34 +08:00
|
|
|
// Verify vreg coverage.
|
2010-12-01 07:18:47 +08:00
|
|
|
for (LiveIntervals::iterator liItr = LIS->begin(), liEnd = LIS->end();
|
2010-11-10 05:04:34 +08:00
|
|
|
liItr != liEnd; ++liItr) {
|
|
|
|
unsigned reg = liItr->first;
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(reg)) continue;
|
2010-12-01 07:18:47 +08:00
|
|
|
if (!VRM->hasPhys(reg)) continue; // spilled?
|
|
|
|
unsigned PhysReg = VRM->getPhys(reg);
|
|
|
|
if (!unionVRegs[PhysReg].test(reg)) {
|
2010-11-10 05:04:34 +08:00
|
|
|
dbgs() << "LiveVirtReg " << reg << " not in union " <<
|
2010-12-01 07:18:47 +08:00
|
|
|
TRI->getName(PhysReg) << "\n";
|
2010-11-10 05:04:34 +08:00
|
|
|
llvm_unreachable("unallocated live vreg");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME: I'm not sure how to verify spilled intervals.
|
|
|
|
}
|
|
|
|
#endif //!NDEBUG
|
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegAllocBase Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Instantiate a LiveIntervalUnion for each physical register.
|
2010-12-08 07:18:47 +08:00
|
|
|
void RegAllocBase::LiveUnionArray::init(LiveIntervalUnion::Allocator &allocator,
|
|
|
|
unsigned NRegs) {
|
2010-12-01 07:18:47 +08:00
|
|
|
NumRegs = NRegs;
|
2010-12-08 07:18:47 +08: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 07:09:15 +08:00
|
|
|
}
|
|
|
|
|
2010-12-11 07:49:00 +08:00
|
|
|
void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis) {
|
2010-12-11 08:19:56 +08:00
|
|
|
NamedRegionTimer T("Initialize", TimerGroupName, TimePassesIsEnabled);
|
2010-12-11 07:49:00 +08:00
|
|
|
TRI = &vrm.getTargetRegInfo();
|
|
|
|
MRI = &vrm.getRegInfo();
|
2010-12-01 07:18:47 +08:00
|
|
|
VRM = &vrm;
|
|
|
|
LIS = &lis;
|
2011-06-04 04:34:53 +08:00
|
|
|
RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
|
|
|
|
|
2011-04-12 07:57:14 +08:00
|
|
|
const unsigned NumRegs = TRI->getNumRegs();
|
|
|
|
if (NumRegs != PhysReg2LiveUnion.numRegs()) {
|
|
|
|
PhysReg2LiveUnion.init(UnionAllocator, NumRegs);
|
|
|
|
// Cache an interferece query for each physical reg
|
|
|
|
Queries.reset(new LiveIntervalUnion::Query[PhysReg2LiveUnion.numRegs()]);
|
|
|
|
}
|
2010-10-23 07:09:15 +08:00
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
void RegAllocBase::LiveUnionArray::clear() {
|
2010-12-08 07:18:47 +08:00
|
|
|
if (!Array)
|
|
|
|
return;
|
|
|
|
for (unsigned r = 0; r != NumRegs; ++r)
|
|
|
|
Array[r].~LiveIntervalUnion();
|
|
|
|
free(Array);
|
2010-12-01 07:18:47 +08:00
|
|
|
NumRegs = 0;
|
2010-12-08 07:18:47 +08:00
|
|
|
Array = 0;
|
2010-10-23 07:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAllocBase::releaseMemory() {
|
2011-04-12 07:57:14 +08:00
|
|
|
for (unsigned r = 0, e = PhysReg2LiveUnion.numRegs(); r != e; ++r)
|
|
|
|
PhysReg2LiveUnion[r].clear();
|
2010-10-23 07:09:15 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 07:01:52 +08:00
|
|
|
// Visit all the live 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.
|
|
|
|
void RegAllocBase::seedLiveRegs() {
|
2011-04-11 23:00:42 +08:00
|
|
|
NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
|
2010-12-01 07:18:47 +08:00
|
|
|
for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end(); I != E; ++I) {
|
|
|
|
unsigned RegNum = I->first;
|
|
|
|
LiveInterval &VirtReg = *I->second;
|
2010-12-09 06:22:41 +08:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(RegNum))
|
2010-12-01 07:18:47 +08:00
|
|
|
PhysReg2LiveUnion[RegNum].unify(VirtReg);
|
2010-12-09 06:22:41 +08:00
|
|
|
else
|
2011-02-23 07:01:52 +08:00
|
|
|
enqueue(&VirtReg);
|
2010-10-27 02:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-09 09:14:03 +08:00
|
|
|
void RegAllocBase::assign(LiveInterval &VirtReg, unsigned PhysReg) {
|
2011-02-18 08:32:47 +08:00
|
|
|
DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
|
|
|
|
<< " to " << PrintReg(PhysReg, TRI) << '\n');
|
2011-02-09 09:14:03 +08:00
|
|
|
assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
|
|
|
|
VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
|
2011-04-21 02:19:48 +08:00
|
|
|
MRI->setPhysRegUsed(PhysReg);
|
2011-02-09 09:14:03 +08:00
|
|
|
PhysReg2LiveUnion[PhysReg].unify(VirtReg);
|
2011-02-18 06:53:48 +08:00
|
|
|
++NumAssigned;
|
2011-02-09 09:14:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegAllocBase::unassign(LiveInterval &VirtReg, unsigned PhysReg) {
|
2011-02-18 08:32:47 +08:00
|
|
|
DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
|
|
|
|
<< " from " << PrintReg(PhysReg, TRI) << '\n');
|
2011-02-09 09:14:03 +08:00
|
|
|
assert(VRM->getPhys(VirtReg.reg) == PhysReg && "Inconsistent unassign");
|
|
|
|
PhysReg2LiveUnion[PhysReg].extract(VirtReg);
|
|
|
|
VRM->clearVirt(VirtReg.reg);
|
2011-02-18 06:53:48 +08:00
|
|
|
++NumUnassigned;
|
2011-02-09 09:14:03 +08:00
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
// Top-level driver to manage the queue of unassigned VirtRegs and call the
|
2010-10-27 02:34:01 +08:00
|
|
|
// selectOrSplit implementation.
|
|
|
|
void RegAllocBase::allocatePhysRegs() {
|
2011-02-23 07:01:52 +08:00
|
|
|
seedLiveRegs();
|
2010-12-01 07:18:47 +08:00
|
|
|
|
|
|
|
// Continue assigning vregs one at a time to available physical registers.
|
2011-02-23 07:01:52 +08:00
|
|
|
while (LiveInterval *VirtReg = dequeue()) {
|
2011-03-23 12:32:51 +08:00
|
|
|
assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
|
|
|
|
|
2011-03-12 12:17:20 +08:00
|
|
|
// Unused registers can appear when the spiller coalesces snippets.
|
|
|
|
if (MRI->reg_nodbg_empty(VirtReg->reg)) {
|
|
|
|
DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
|
|
|
|
LIS->removeInterval(VirtReg->reg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-17 06:56:11 +08:00
|
|
|
// Invalidate all interference queries, live ranges could have changed.
|
2011-05-11 01:37:41 +08:00
|
|
|
invalidateVirtRegs();
|
2011-03-17 06:56:11 +08:00
|
|
|
|
2010-12-01 07:18:47 +08: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.
|
2011-02-23 07:01:52 +08:00
|
|
|
DEBUG(dbgs() << "\nselectOrSplit "
|
|
|
|
<< MRI->getRegClass(VirtReg->reg)->getName()
|
|
|
|
<< ':' << *VirtReg << '\n');
|
2010-12-01 07:18:47 +08:00
|
|
|
typedef SmallVector<LiveInterval*, 4> VirtRegVec;
|
|
|
|
VirtRegVec SplitVRegs;
|
2011-02-23 07:01:52 +08:00
|
|
|
unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
|
2010-12-01 07:18:47 +08:00
|
|
|
|
2011-05-07 05:58:30 +08:00
|
|
|
if (AvailablePhysReg == ~0u) {
|
|
|
|
// selectOrSplit failed to find a register!
|
2011-07-02 15:17:37 +08:00
|
|
|
const char *Msg = "ran out of registers during register allocation";
|
|
|
|
// Probably caused by an inline asm.
|
|
|
|
MachineInstr *MI;
|
2011-05-07 05:58:30 +08:00
|
|
|
for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg);
|
2011-07-02 15:17:37 +08:00
|
|
|
(MI = I.skipInstruction());)
|
|
|
|
if (MI->isInlineAsm())
|
|
|
|
break;
|
|
|
|
if (MI)
|
|
|
|
MI->emitError(Msg);
|
|
|
|
else
|
|
|
|
report_fatal_error(Msg);
|
|
|
|
// Keep going after reporting the error.
|
|
|
|
VRM->assignVirt2Phys(VirtReg->reg,
|
|
|
|
RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
|
|
|
|
continue;
|
2011-05-07 05:58:30 +08:00
|
|
|
}
|
|
|
|
|
2011-02-18 08:32:47 +08:00
|
|
|
if (AvailablePhysReg)
|
2011-02-23 07:01:52 +08:00
|
|
|
assign(*VirtReg, AvailablePhysReg);
|
2011-02-18 08:32:47 +08:00
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
|
|
|
|
I != E; ++I) {
|
2011-02-23 07:01:52 +08:00
|
|
|
LiveInterval *SplitVirtReg = *I;
|
2011-03-23 12:32:51 +08:00
|
|
|
assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
|
|
|
|
if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
|
|
|
|
DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
|
|
|
|
LIS->removeInterval(SplitVirtReg->reg);
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-01 07:18:47 +08:00
|
|
|
DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
|
2010-11-09 02:02:08 +08:00
|
|
|
"expect split value in virtual register");
|
2011-02-23 07:01:52 +08:00
|
|
|
enqueue(SplitVirtReg);
|
2011-02-18 06:53:48 +08:00
|
|
|
++NumNewQueued;
|
2010-10-27 02:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08: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 07:10:48 +08:00
|
|
|
for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI)
|
2010-12-01 07:18:47 +08:00
|
|
|
if (query(VirtReg, *AliasI).checkInterference())
|
|
|
|
return *AliasI;
|
2010-11-09 02:02:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08: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-12 01:46:29 +08:00
|
|
|
DEBUG(dbgs() << "extracting from " <<
|
2010-12-01 07:18:47 +08:00
|
|
|
TRI->getName(PhysReg) << " " << SpilledVReg << '\n');
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-11-11 03:18:47 +08: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 09:14:03 +08:00
|
|
|
unassign(SpilledVReg, PhysReg);
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-11-11 03:18:47 +08:00
|
|
|
// Spill the extracted interval.
|
2011-03-10 09:51:42 +08:00
|
|
|
LiveRangeEdit LRE(SpilledVReg, SplitVRegs, 0, &PendingSpills);
|
|
|
|
spiller().spill(LRE);
|
2010-11-11 03:18:47 +08:00
|
|
|
}
|
2010-11-12 01:46:29 +08: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-11 03:18:47 +08:00
|
|
|
}
|
|
|
|
|
2010-12-01 07:18:47 +08: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-11 03:18:47 +08:00
|
|
|
bool
|
2010-12-01 07:18:47 +08:00
|
|
|
RegAllocBase::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs) {
|
2010-11-11 03:18:47 +08:00
|
|
|
// Record each interference and determine if all are spillable before mutating
|
|
|
|
// either the union or live intervals.
|
2010-12-15 07:10:48 +08:00
|
|
|
unsigned NumInterferences = 0;
|
2010-11-12 01:46:29 +08:00
|
|
|
// Collect interferences assigned to any alias of the physical register.
|
2010-12-15 07:10:48 +08:00
|
|
|
for (const unsigned *asI = TRI->getOverlaps(PhysReg); *asI; ++asI) {
|
2010-12-01 07:18:47 +08:00
|
|
|
LiveIntervalUnion::Query &QAlias = query(VirtReg, *asI);
|
|
|
|
NumInterferences += QAlias.collectInterferingVRegs();
|
2010-11-12 01:46:29 +08:00
|
|
|
if (QAlias.seenUnspillableVReg()) {
|
2010-11-11 03:18:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-09 02:02:08 +08:00
|
|
|
}
|
2010-12-01 07:18:47 +08: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 07:10:48 +08:00
|
|
|
for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI)
|
2010-12-01 07:18:47 +08:00
|
|
|
spillReg(VirtReg, *AliasI, SplitVRegs);
|
2010-11-11 03:18:47 +08:00
|
|
|
return true;
|
2010-10-23 07:09:15 +08:00
|
|
|
}
|
|
|
|
|
2010-12-08 09:06:06 +08:00
|
|
|
// Add newly allocated physical registers to the MBB live in sets.
|
|
|
|
void RegAllocBase::addMBBLiveIns(MachineFunction *MF) {
|
2010-12-11 08:19:56 +08:00
|
|
|
NamedRegionTimer T("MBB Live Ins", TimerGroupName, TimePassesIsEnabled);
|
2011-04-12 04:01:41 +08:00
|
|
|
SlotIndexes *Indexes = LIS->getSlotIndexes();
|
|
|
|
if (MF->size() <= 1)
|
|
|
|
return;
|
2010-12-08 09:06:06 +08:00
|
|
|
|
2011-04-12 04:01:41 +08:00
|
|
|
LiveIntervalUnion::SegmentIter SI;
|
2010-12-08 09:06:06 +08:00
|
|
|
for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) {
|
|
|
|
LiveIntervalUnion &LiveUnion = PhysReg2LiveUnion[PhysReg];
|
|
|
|
if (LiveUnion.empty())
|
|
|
|
continue;
|
2011-07-27 07:12:08 +08:00
|
|
|
DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " live-in:");
|
2011-04-12 04:01:41 +08:00
|
|
|
MachineFunction::iterator MBB = llvm::next(MF->begin());
|
|
|
|
MachineFunction::iterator MFE = MF->end();
|
|
|
|
SlotIndex Start, Stop;
|
|
|
|
tie(Start, Stop) = Indexes->getMBBRange(MBB);
|
|
|
|
SI.setMap(LiveUnion.getMap());
|
|
|
|
SI.find(Start);
|
|
|
|
while (SI.valid()) {
|
|
|
|
if (SI.start() <= Start) {
|
|
|
|
if (!MBB->isLiveIn(PhysReg))
|
|
|
|
MBB->addLiveIn(PhysReg);
|
2011-07-27 07:12:08 +08:00
|
|
|
DEBUG(dbgs() << "\tBB#" << MBB->getNumber() << ':'
|
|
|
|
<< PrintReg(SI.value()->reg, TRI));
|
2011-04-12 04:01:41 +08:00
|
|
|
} else if (SI.start() > Stop)
|
2011-04-13 02:11:28 +08:00
|
|
|
MBB = Indexes->getMBBFromIndex(SI.start().getPrevIndex());
|
2011-04-12 04:01:41 +08:00
|
|
|
if (++MBB == MFE)
|
|
|
|
break;
|
|
|
|
tie(Start, Stop) = Indexes->getMBBRange(MBB);
|
|
|
|
SI.advanceTo(Start);
|
2010-12-08 09:06:06 +08:00
|
|
|
}
|
2011-07-27 07:12:08 +08:00
|
|
|
DEBUG(dbgs() << '\n');
|
2010-12-08 09:06:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RABasic Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Driver for the register assignment and splitting heuristics.
|
|
|
|
// Manages iteration over the LiveIntervalUnions.
|
2010-11-20 10:43:55 +08:00
|
|
|
//
|
2010-12-01 07:18:47 +08:00
|
|
|
// This is a minimal implementation of register assignment and splitting that
|
|
|
|
// spills whenever we run out of registers.
|
2010-10-23 07:09:15 +08: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 07:18:47 +08:00
|
|
|
// minimal, there is no value in caching them outside the scope of
|
|
|
|
// selectOrSplit().
|
|
|
|
unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitVRegs) {
|
2010-11-11 03:18:47 +08:00
|
|
|
// Populate a list of physical register spill candidates.
|
2010-12-01 07:18:47 +08:00
|
|
|
SmallVector<unsigned, 8> PhysRegSpillCands;
|
2010-11-09 02:02:08 +08:00
|
|
|
|
2010-11-20 10:43:55 +08:00
|
|
|
// Check for an available register in this class.
|
2011-06-04 04:34:53 +08:00
|
|
|
ArrayRef<unsigned> Order =
|
|
|
|
RegClassInfo.getOrder(MRI->getRegClass(VirtReg.reg));
|
|
|
|
for (ArrayRef<unsigned>::iterator I = Order.begin(), E = Order.end(); I != E;
|
|
|
|
++I) {
|
2010-12-01 07:18:47 +08:00
|
|
|
unsigned PhysReg = *I;
|
|
|
|
|
|
|
|
// Check interference and as a side effect, intialize queries for this
|
|
|
|
// VirtReg and its aliases.
|
|
|
|
unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg);
|
2010-11-09 02:02:08 +08:00
|
|
|
if (interfReg == 0) {
|
2010-11-11 03:18:47 +08:00
|
|
|
// Found an available register.
|
2010-12-01 07:18:47 +08:00
|
|
|
return PhysReg;
|
2010-10-23 07:09:15 +08:00
|
|
|
}
|
2011-08-12 05:00:42 +08:00
|
|
|
Queries[interfReg].collectInterferingVRegs(1);
|
2010-11-11 03:18:47 +08:00
|
|
|
LiveInterval *interferingVirtReg =
|
2011-08-12 05:00:42 +08:00
|
|
|
Queries[interfReg].interferingVRegs().front();
|
2010-11-11 03:18:47 +08:00
|
|
|
|
2010-12-10 02:15:21 +08:00
|
|
|
// The current VirtReg must either be spillable, or one of its interferences
|
2010-12-01 07:18:47 +08:00
|
|
|
// must have less spill weight.
|
|
|
|
if (interferingVirtReg->weight < VirtReg.weight ) {
|
|
|
|
PhysRegSpillCands.push_back(PhysReg);
|
2010-11-09 02:02:08 +08:00
|
|
|
}
|
2010-10-23 07:09:15 +08:00
|
|
|
}
|
2010-11-11 03:18:47 +08:00
|
|
|
// Try to spill another interfering reg with less spill weight.
|
2010-12-01 07:18:47 +08:00
|
|
|
for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
|
|
|
|
PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
|
2010-11-11 03:18:47 +08:00
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue;
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-12-08 02:51:27 +08:00
|
|
|
assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 &&
|
|
|
|
"Interference after spill.");
|
2010-11-11 03:18:47 +08:00
|
|
|
// Tell the caller to allocate to this newly freed physical register.
|
2010-12-01 07:18:47 +08:00
|
|
|
return *PhysRegI;
|
2010-11-09 02:02:08 +08:00
|
|
|
}
|
2011-05-07 05:58:30 +08:00
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
// No other spill candidates were found, so spill the current VirtReg.
|
|
|
|
DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
|
2011-05-07 05:58:30 +08:00
|
|
|
if (!VirtReg.isSpillable())
|
|
|
|
return ~0u;
|
2011-03-10 09:51:42 +08:00
|
|
|
LiveRangeEdit LRE(VirtReg, SplitVRegs);
|
|
|
|
spiller().spill(LRE);
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-11-11 03:18:47 +08:00
|
|
|
// The live virtual register requesting allocation was spilled, so tell
|
|
|
|
// the caller not to allocate anything during this round.
|
|
|
|
return 0;
|
2010-11-09 02:02:08 +08:00
|
|
|
}
|
2010-10-23 07:09:15 +08:00
|
|
|
|
|
|
|
bool RABasic::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
|
|
|
|
<< "********** Function: "
|
|
|
|
<< ((Value*)mf.getFunction())->getName() << '\n');
|
|
|
|
|
2010-12-01 07:18:47 +08:00
|
|
|
MF = &mf;
|
|
|
|
DEBUG(RMF = &getAnalysis<RenderMachineFunction>());
|
2010-11-12 01:46:29 +08:00
|
|
|
|
2010-12-11 07:49:00 +08:00
|
|
|
RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
|
2011-04-01 07:02:17 +08:00
|
|
|
SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-10-27 02:34:01 +08:00
|
|
|
allocatePhysRegs();
|
2010-10-23 07:09:15 +08:00
|
|
|
|
2010-12-08 09:06:06 +08:00
|
|
|
addMBBLiveIns(MF);
|
2010-11-20 10:57:05 +08:00
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
// Diagnostic output before rewriting
|
2010-12-01 07:18:47 +08:00
|
|
|
DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
|
2010-10-23 07:09:15 +08:00
|
|
|
|
|
|
|
// optional HTML output
|
2010-12-01 07:18:47 +08:00
|
|
|
DEBUG(RMF->renderMachineFunction("After basic register allocation.", VRM));
|
2010-10-23 07:09:15 +08:00
|
|
|
|
2010-11-10 05:04:34 +08: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 07:16:35 +08:00
|
|
|
if (VerifyEnabled) {
|
2010-11-10 05:04:34 +08:00
|
|
|
// Verify accuracy of LiveIntervals. The standard machine code verifier
|
|
|
|
// ensures that each LiveIntervals covers all uses of the virtual reg.
|
|
|
|
|
2010-12-01 07:18:47 +08: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 08:06:56 +08:00
|
|
|
MF->verify(this, "In RABasic::verify");
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-11-10 05:04:34 +08:00
|
|
|
// Verify that LiveIntervals are partitioned into unions and disjoint within
|
|
|
|
// the unions.
|
|
|
|
verify();
|
|
|
|
}
|
|
|
|
#endif // !NDEBUG
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
// Run rewriter
|
2011-02-19 06:03:18 +08:00
|
|
|
VRM->rewrite(LIS->getSlotIndexes());
|
2010-10-27 02:34:01 +08:00
|
|
|
|
2011-04-06 05:40:37 +08:00
|
|
|
// Write out new DBG_VALUE instructions.
|
|
|
|
getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
|
|
|
|
|
2010-10-27 02:34:01 +08:00
|
|
|
// The pass output is in VirtRegMap. Release all the transient data.
|
|
|
|
releaseMemory();
|
2010-11-20 10:43:55 +08:00
|
|
|
|
2010-10-23 07:09:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-20 10:43:55 +08:00
|
|
|
FunctionPass* llvm::createBasicRegisterAllocator()
|
2010-10-23 07:09:15 +08:00
|
|
|
{
|
|
|
|
return new RABasic();
|
|
|
|
}
|