2012-01-11 22:28:30 +00:00
|
|
|
//===-- RegAllocBasic.cpp - Basic Register Allocator ----------------------===//
|
2010-10-22 23:09:15 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-06-20 22:52:24 +00:00
|
|
|
#include "AllocationOrder.h"
|
2011-04-05 21:40:37 +00:00
|
|
|
#include "LiveDebugVariables.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "RegAllocBase.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "Spiller.h"
|
2010-11-11 17:46:29 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "llvm/CodeGen/CalcSpillWeights.h"
|
2010-11-08 18:02:08 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2012-04-02 22:44:18 +00:00
|
|
|
#include "llvm/CodeGen/LiveRangeEdit.h"
|
2012-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/LiveRegMatrix.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2013-06-17 19:00:36 +00:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2012-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/PassAnalysisSupport.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-10-22 23:09:15 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2010-11-08 18:02:08 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2010-12-07 23:18:47 +00:00
|
|
|
#include <cstdlib>
|
2011-02-22 23:01:52 +00:00
|
|
|
#include <queue>
|
2010-10-26 18:34:01 +00:00
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
|
|
|
|
createBasicRegisterAllocator);
|
|
|
|
|
2011-02-22 23:01:52 +00:00
|
|
|
namespace {
|
|
|
|
struct CompSpillWeight {
|
|
|
|
bool operator()(LiveInterval *A, LiveInterval *B) const {
|
|
|
|
return A->weight < B->weight;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-11-25 16:42:51 +00:00
|
|
|
namespace {
|
2010-10-22 23:09:15 +00: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-11-30 23:18:47 +00:00
|
|
|
MachineFunction *MF;
|
2010-10-22 23:09:15 +00:00
|
|
|
|
|
|
|
// state
|
2013-04-12 10:56:28 +00:00
|
|
|
OwningPtr<Spiller> SpillerInstance;
|
2011-02-22 23:01:52 +00:00
|
|
|
std::priority_queue<LiveInterval*, std::vector<LiveInterval*>,
|
|
|
|
CompSpillWeight> Queue;
|
2012-02-08 18:54:35 +00:00
|
|
|
|
|
|
|
// Scratch space. Allocated here to avoid repeated malloc calls in
|
|
|
|
// selectOrSplit().
|
|
|
|
BitVector UsableRegs;
|
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
public:
|
|
|
|
RABasic();
|
|
|
|
|
|
|
|
/// Return the pass name.
|
|
|
|
virtual const char* getPassName() const {
|
|
|
|
return "Basic Register Allocator";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RABasic analysis usage.
|
2010-11-30 23:18:47 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
2010-10-22 23:09:15 +00:00
|
|
|
|
|
|
|
virtual void releaseMemory();
|
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
virtual Spiller &spiller() { return *SpillerInstance; }
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2010-12-08 22:22:41 +00:00
|
|
|
virtual float getPriority(LiveInterval *LI) { return LI->weight; }
|
|
|
|
|
2011-02-22 23:01:52 +00: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-11-30 23:18:47 +00:00
|
|
|
virtual unsigned selectOrSplit(LiveInterval &VirtReg,
|
2013-08-14 23:50:04 +00:00
|
|
|
SmallVectorImpl<unsigned> &SplitVRegs);
|
2010-10-22 23:09:15 +00:00
|
|
|
|
|
|
|
/// Perform register allocation.
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &mf);
|
|
|
|
|
2012-01-11 22:52:14 +00:00
|
|
|
// Helper for spilling all live virtual registers currently unified under preg
|
|
|
|
// that interfere with the most recently queried lvr. Return true if spilling
|
|
|
|
// was successful, and append any new spilled/split intervals to splitLVRs.
|
|
|
|
bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
|
2013-08-14 23:50:04 +00:00
|
|
|
SmallVectorImpl<unsigned> &SplitVRegs);
|
2012-01-11 22:52:14 +00:00
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
static char ID;
|
|
|
|
};
|
|
|
|
|
|
|
|
char RABasic::ID = 0;
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
RABasic::RABasic(): MachineFunctionPass(ID) {
|
2011-04-05 21:40:37 +00:00
|
|
|
initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
|
2010-10-22 23:09:15 +00:00
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
|
2011-06-26 22:34:10 +00:00
|
|
|
initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
|
2012-01-17 06:55:03 +00:00
|
|
|
initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
|
2013-11-08 18:19:19 +00:00
|
|
|
initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
|
2010-10-22 23:09:15 +00:00
|
|
|
initializeLiveStacksPass(*PassRegistry::getPassRegistry());
|
2010-11-03 20:39:26 +00:00
|
|
|
initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
|
2010-10-22 23:09:15 +00:00
|
|
|
initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
|
2012-06-20 22:52:24 +00:00
|
|
|
initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
|
|
|
AU.addRequired<LiveIntervals>();
|
2012-06-08 23:44:45 +00:00
|
|
|
AU.addPreserved<LiveIntervals>();
|
2010-11-30 23:18:47 +00:00
|
|
|
AU.addPreserved<SlotIndexes>();
|
2011-04-05 21:40:37 +00:00
|
|
|
AU.addRequired<LiveDebugVariables>();
|
|
|
|
AU.addPreserved<LiveDebugVariables>();
|
2013-11-08 18:19:19 +00:00
|
|
|
AU.addRequired<CalculateSpillWeights>();
|
2010-11-30 23:18:47 +00:00
|
|
|
AU.addRequired<LiveStacks>();
|
|
|
|
AU.addPreserved<LiveStacks>();
|
2013-06-17 19:00:36 +00:00
|
|
|
AU.addRequired<MachineBlockFrequencyInfo>();
|
|
|
|
AU.addPreserved<MachineBlockFrequencyInfo>();
|
2010-11-30 23:18:47 +00:00
|
|
|
AU.addRequiredID(MachineDominatorsID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addRequired<VirtRegMap>();
|
|
|
|
AU.addPreserved<VirtRegMap>();
|
2012-06-20 22:52:24 +00:00
|
|
|
AU.addRequired<LiveRegMatrix>();
|
|
|
|
AU.addPreserved<LiveRegMatrix>();
|
2010-11-30 23:18:47 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RABasic::releaseMemory() {
|
2010-11-30 23:18:47 +00:00
|
|
|
SpillerInstance.reset(0);
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-11 22:52:14 +00: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.
|
|
|
|
bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg,
|
2013-08-14 23:50:04 +00:00
|
|
|
SmallVectorImpl<unsigned> &SplitVRegs) {
|
2012-01-11 22:52:14 +00:00
|
|
|
// Record each interference and determine if all are spillable before mutating
|
|
|
|
// either the union or live intervals.
|
2012-06-20 22:52:24 +00:00
|
|
|
SmallVector<LiveInterval*, 8> Intfs;
|
|
|
|
|
2012-01-11 22:52:14 +00:00
|
|
|
// Collect interferences assigned to any alias of the physical register.
|
2012-06-20 22:52:24 +00:00
|
|
|
for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
|
|
|
|
LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
|
|
|
|
Q.collectInterferingVRegs();
|
|
|
|
if (Q.seenUnspillableVReg())
|
2012-01-11 22:52:14 +00:00
|
|
|
return false;
|
2012-06-20 22:52:24 +00:00
|
|
|
for (unsigned i = Q.interferingVRegs().size(); i; --i) {
|
|
|
|
LiveInterval *Intf = Q.interferingVRegs()[i - 1];
|
|
|
|
if (!Intf->isSpillable() || Intf->weight > VirtReg.weight)
|
|
|
|
return false;
|
|
|
|
Intfs.push_back(Intf);
|
2012-01-11 22:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) <<
|
|
|
|
" interferences with " << VirtReg << "\n");
|
2012-06-20 22:52:24 +00:00
|
|
|
assert(!Intfs.empty() && "expected interference");
|
2012-01-11 22:52:14 +00:00
|
|
|
|
|
|
|
// Spill each interfering vreg allocated to PhysReg or an alias.
|
2012-06-20 22:52:24 +00:00
|
|
|
for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
|
|
|
|
LiveInterval &Spill = *Intfs[i];
|
|
|
|
|
|
|
|
// Skip duplicates.
|
|
|
|
if (!VRM->hasPhys(Spill.reg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Deallocate the interfering vreg by removing it from the union.
|
|
|
|
// A LiveInterval instance may not be in a union during modification!
|
|
|
|
Matrix->unassign(Spill);
|
|
|
|
|
|
|
|
// Spill the extracted interval.
|
|
|
|
LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM);
|
|
|
|
spiller().spill(LRE);
|
|
|
|
}
|
2012-01-11 22:52:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-22 23:09:15 +00:00
|
|
|
// Driver for the register assignment and splitting heuristics.
|
|
|
|
// Manages iteration over the LiveIntervalUnions.
|
2010-11-20 02:43:55 +00:00
|
|
|
//
|
2010-11-30 23:18:47 +00:00
|
|
|
// This is a minimal implementation of register assignment and splitting that
|
|
|
|
// spills whenever we run out of registers.
|
2010-10-22 23:09:15 +00: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-11-30 23:18:47 +00:00
|
|
|
// minimal, there is no value in caching them outside the scope of
|
|
|
|
// selectOrSplit().
|
|
|
|
unsigned RABasic::selectOrSplit(LiveInterval &VirtReg,
|
2013-08-14 23:50:04 +00:00
|
|
|
SmallVectorImpl<unsigned> &SplitVRegs) {
|
2010-11-10 19:18:47 +00:00
|
|
|
// Populate a list of physical register spill candidates.
|
2010-11-30 23:18:47 +00:00
|
|
|
SmallVector<unsigned, 8> PhysRegSpillCands;
|
2010-11-08 18:02:08 +00:00
|
|
|
|
2010-11-20 02:43:55 +00:00
|
|
|
// Check for an available register in this class.
|
2012-06-20 22:52:24 +00:00
|
|
|
AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
|
|
|
|
while (unsigned PhysReg = Order.next()) {
|
|
|
|
// Check for interference in PhysReg
|
|
|
|
switch (Matrix->checkInterference(VirtReg, PhysReg)) {
|
|
|
|
case LiveRegMatrix::IK_Free:
|
|
|
|
// PhysReg is available, allocate it.
|
2010-11-30 23:18:47 +00:00
|
|
|
return PhysReg;
|
2010-11-10 19:18:47 +00:00
|
|
|
|
2012-06-20 22:52:24 +00:00
|
|
|
case LiveRegMatrix::IK_VirtReg:
|
|
|
|
// Only virtual registers in the way, we may be able to spill them.
|
2010-11-30 23:18:47 +00:00
|
|
|
PhysRegSpillCands.push_back(PhysReg);
|
2012-06-20 22:52:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// RegMask or RegUnit interference.
|
|
|
|
continue;
|
2010-11-08 18:02:08 +00:00
|
|
|
}
|
2010-10-22 23:09:15 +00:00
|
|
|
}
|
2012-06-20 22:52:24 +00:00
|
|
|
|
2010-11-10 19:18:47 +00:00
|
|
|
// Try to spill another interfering reg with less spill weight.
|
2010-11-30 23:18:47 +00:00
|
|
|
for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(),
|
2012-06-20 22:52:24 +00:00
|
|
|
PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) {
|
|
|
|
if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs))
|
|
|
|
continue;
|
2010-11-20 02:43:55 +00:00
|
|
|
|
2012-06-20 22:52:24 +00:00
|
|
|
assert(!Matrix->checkInterference(VirtReg, *PhysRegI) &&
|
2010-12-07 18:51:27 +00:00
|
|
|
"Interference after spill.");
|
2010-11-10 19:18:47 +00:00
|
|
|
// Tell the caller to allocate to this newly freed physical register.
|
2010-11-30 23:18:47 +00:00
|
|
|
return *PhysRegI;
|
2010-11-08 18:02:08 +00:00
|
|
|
}
|
2011-05-06 21:58:30 +00:00
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
// No other spill candidates were found, so spill the current VirtReg.
|
|
|
|
DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
|
2011-05-06 21:58:30 +00:00
|
|
|
if (!VirtReg.isSpillable())
|
|
|
|
return ~0u;
|
2012-05-19 05:25:46 +00:00
|
|
|
LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM);
|
2011-03-10 01:51:42 +00:00
|
|
|
spiller().spill(LRE);
|
2010-11-20 02:43:55 +00:00
|
|
|
|
2010-11-10 19:18:47 +00: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 18:02:08 +00:00
|
|
|
}
|
2010-10-22 23:09:15 +00:00
|
|
|
|
|
|
|
bool RABasic::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
|
|
|
|
<< "********** Function: "
|
2012-08-22 17:18:53 +00:00
|
|
|
<< mf.getName() << '\n');
|
2010-10-22 23:09:15 +00:00
|
|
|
|
2010-11-30 23:18:47 +00:00
|
|
|
MF = &mf;
|
2012-06-20 22:52:29 +00:00
|
|
|
RegAllocBase::init(getAnalysis<VirtRegMap>(),
|
|
|
|
getAnalysis<LiveIntervals>(),
|
|
|
|
getAnalysis<LiveRegMatrix>());
|
2011-03-31 23:02:17 +00:00
|
|
|
SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
|
2010-11-20 02:43:55 +00:00
|
|
|
|
2010-10-26 18:34:01 +00:00
|
|
|
allocatePhysRegs();
|
2010-10-22 23:09:15 +00:00
|
|
|
|
|
|
|
// Diagnostic output before rewriting
|
2010-11-30 23:18:47 +00:00
|
|
|
DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
|
2010-10-22 23:09:15 +00:00
|
|
|
|
2010-10-26 18:34:01 +00:00
|
|
|
releaseMemory();
|
2010-10-22 23:09:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-20 02:43:55 +00:00
|
|
|
FunctionPass* llvm::createBasicRegisterAllocator()
|
2010-10-22 23:09:15 +00:00
|
|
|
{
|
|
|
|
return new RABasic();
|
|
|
|
}
|