2003-11-20 11:32:25 +08:00
|
|
|
//===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a linear scan register allocator.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-02-24 16:58:30 +08:00
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/SSARegMap.h"
|
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "Support/Debug.h"
|
2004-07-04 15:59:06 +08:00
|
|
|
#include "Support/Statistic.h"
|
2004-05-30 15:46:27 +08:00
|
|
|
#include "Support/STLExtras.h"
|
2004-07-24 01:56:30 +08:00
|
|
|
#include "LiveIntervalAnalysis.h"
|
2004-02-23 08:53:31 +08:00
|
|
|
#include "PhysRegTracker.h"
|
2004-02-24 07:08:11 +08:00
|
|
|
#include "VirtRegMap.h"
|
2004-02-15 18:24:21 +08:00
|
|
|
#include <algorithm>
|
2004-05-08 11:50:03 +08:00
|
|
|
#include <cmath>
|
2004-05-30 15:24:39 +08:00
|
|
|
#include <set>
|
2004-07-22 16:14:44 +08:00
|
|
|
#include <queue>
|
2004-02-24 16:58:30 +08:00
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2004-07-04 15:59:06 +08:00
|
|
|
|
|
|
|
Statistic<double> efficiency
|
|
|
|
("regalloc", "Ratio of intervals processed over total intervals");
|
|
|
|
|
2004-07-05 01:23:35 +08:00
|
|
|
static unsigned numIterations = 0;
|
|
|
|
static unsigned numIntervals = 0;
|
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
class RA : public MachineFunctionPass {
|
|
|
|
private:
|
|
|
|
MachineFunction* mf_;
|
|
|
|
const TargetMachine* tm_;
|
|
|
|
const MRegisterInfo* mri_;
|
2004-01-23 07:08:45 +08:00
|
|
|
LiveIntervals* li_;
|
2004-07-22 16:14:44 +08:00
|
|
|
typedef std::vector<LiveInterval*> IntervalPtrs;
|
|
|
|
IntervalPtrs handled_, fixed_, active_, inactive_;
|
|
|
|
typedef std::priority_queue<LiveInterval*,
|
|
|
|
IntervalPtrs,
|
|
|
|
greater_ptr<LiveInterval> > IntervalHeap;
|
|
|
|
IntervalHeap unhandled_;
|
2004-02-23 14:10:13 +08:00
|
|
|
std::auto_ptr<PhysRegTracker> prt_;
|
2004-02-24 07:08:11 +08:00
|
|
|
std::auto_ptr<VirtRegMap> vrm_;
|
2004-03-02 07:18:15 +08:00
|
|
|
std::auto_ptr<Spiller> spiller_;
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
typedef std::vector<float> SpillWeights;
|
|
|
|
SpillWeights spillWeights_;
|
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
public:
|
|
|
|
virtual const char* getPassName() const {
|
|
|
|
return "Linear Scan Register Allocator";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<LiveVariables>();
|
|
|
|
AU.addRequired<LiveIntervals>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
|
|
|
|
2004-02-02 04:13:26 +08:00
|
|
|
void releaseMemory();
|
|
|
|
|
|
|
|
private:
|
2004-02-24 16:58:30 +08:00
|
|
|
/// linearScan - the linear scan algorithm
|
|
|
|
void linearScan();
|
|
|
|
|
2004-01-07 17:20:58 +08:00
|
|
|
/// initIntervalSets - initializa the four interval sets:
|
|
|
|
/// unhandled, fixed, active and inactive
|
2004-07-24 11:32:06 +08:00
|
|
|
void initIntervalSets();
|
2004-01-07 17:20:58 +08:00
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
/// processActiveIntervals - expire old intervals and move
|
|
|
|
/// non-overlapping ones to the incative list
|
2004-07-22 16:14:44 +08:00
|
|
|
void processActiveIntervals(LiveInterval* cur);
|
2003-11-20 11:32:25 +08:00
|
|
|
|
|
|
|
/// processInactiveIntervals - expire old intervals and move
|
|
|
|
/// overlapping ones to the active list
|
2004-07-22 16:14:44 +08:00
|
|
|
void processInactiveIntervals(LiveInterval* cur);
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
/// updateSpillWeights - updates the spill weights of the
|
|
|
|
/// specifed physical register and its weight
|
|
|
|
void updateSpillWeights(unsigned reg, SpillWeights::value_type weight);
|
|
|
|
|
|
|
|
/// assignRegOrStackSlotAtInterval - assign a register if one
|
|
|
|
/// is available, or spill.
|
2004-07-22 16:14:44 +08:00
|
|
|
void assignRegOrStackSlotAtInterval(LiveInterval* cur);
|
2003-11-20 11:32:25 +08:00
|
|
|
|
|
|
|
///
|
|
|
|
/// register handling helpers
|
|
|
|
///
|
|
|
|
|
2003-12-21 13:43:40 +08:00
|
|
|
/// getFreePhysReg - return a free physical register for this
|
|
|
|
/// virtual register interval if we have one, otherwise return
|
|
|
|
/// 0
|
2004-07-22 16:14:44 +08:00
|
|
|
unsigned getFreePhysReg(LiveInterval* cur);
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
/// assignVirt2StackSlot - assigns this virtual register to a
|
2004-02-15 18:24:21 +08:00
|
|
|
/// stack slot. returns the stack slot
|
|
|
|
int assignVirt2StackSlot(unsigned virtReg);
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2004-07-22 16:14:44 +08:00
|
|
|
template <typename ItTy>
|
|
|
|
void printIntervals(const char* const str, ItTy i, ItTy e) const {
|
2003-11-20 11:32:25 +08:00
|
|
|
if (str) std::cerr << str << " intervals:\n";
|
|
|
|
for (; i != e; ++i) {
|
2004-02-20 14:15:40 +08:00
|
|
|
std::cerr << "\t" << **i << " -> ";
|
2004-01-17 04:29:42 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
2004-02-01 09:27:01 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(reg)) {
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
2004-01-17 04:33:13 +08:00
|
|
|
std::cerr << mri_->getName(reg) << '\n';
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2004-02-02 04:13:26 +08:00
|
|
|
void RA::releaseMemory()
|
|
|
|
{
|
2004-07-22 16:14:44 +08:00
|
|
|
while (!unhandled_.empty()) unhandled_.pop();
|
2004-03-17 08:48:59 +08:00
|
|
|
fixed_.clear();
|
2004-02-02 04:13:26 +08:00
|
|
|
active_.clear();
|
|
|
|
inactive_.clear();
|
2004-02-15 18:24:21 +08:00
|
|
|
handled_.clear();
|
2004-02-02 04:13:26 +08:00
|
|
|
}
|
|
|
|
|
2003-11-20 11:32:25 +08:00
|
|
|
bool RA::runOnMachineFunction(MachineFunction &fn) {
|
|
|
|
mf_ = &fn;
|
|
|
|
tm_ = &fn.getTarget();
|
|
|
|
mri_ = tm_->getRegisterInfo();
|
2004-01-23 07:08:45 +08:00
|
|
|
li_ = &getAnalysis<LiveIntervals>();
|
2004-02-23 14:10:13 +08:00
|
|
|
if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
|
2004-02-24 07:08:11 +08:00
|
|
|
vrm_.reset(new VirtRegMap(*mf_));
|
2004-03-02 07:18:15 +08:00
|
|
|
if (!spiller_.get()) spiller_.reset(createSpiller());
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2004-07-24 11:32:06 +08:00
|
|
|
initIntervalSets();
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2004-02-24 16:58:30 +08:00
|
|
|
linearScan();
|
|
|
|
|
2004-03-02 07:18:15 +08:00
|
|
|
spiller_->runOnMachineFunction(*mf_, *vrm_);
|
2004-02-24 16:58:30 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RA::linearScan()
|
|
|
|
{
|
2004-01-07 17:20:58 +08:00
|
|
|
// linear scan algorithm
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
|
|
|
|
DEBUG(std::cerr << "********** Function: "
|
|
|
|
<< mf_->getFunction()->getName() << '\n');
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2004-07-22 16:14:44 +08:00
|
|
|
// DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
|
|
|
|
DEBUG(printIntervals("active", active_.begin(), active_.end()));
|
|
|
|
DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
|
2003-12-24 02:00:33 +08:00
|
|
|
|
2004-03-17 08:48:59 +08:00
|
|
|
while (!unhandled_.empty()) {
|
2004-01-07 17:20:58 +08:00
|
|
|
// pick the interval with the earliest start point
|
2004-07-22 16:14:44 +08:00
|
|
|
LiveInterval* cur = unhandled_.top();
|
|
|
|
unhandled_.pop();
|
2004-07-05 01:23:35 +08:00
|
|
|
++numIterations;
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
|
2003-12-24 02:00:33 +08:00
|
|
|
|
2004-01-07 17:20:58 +08:00
|
|
|
processActiveIntervals(cur);
|
|
|
|
processInactiveIntervals(cur);
|
2004-01-14 04:42:08 +08:00
|
|
|
|
2004-01-07 17:20:58 +08:00
|
|
|
// if this register is fixed we are done
|
2004-02-01 09:27:01 +08:00
|
|
|
if (MRegisterInfo::isPhysicalRegister(cur->reg)) {
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->addRegUse(cur->reg);
|
2004-01-07 17:20:58 +08:00
|
|
|
active_.push_back(cur);
|
2004-02-15 18:24:21 +08:00
|
|
|
handled_.push_back(cur);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
// otherwise we are allocating a virtual register. try to find
|
|
|
|
// a free physical register or spill an interval in order to
|
|
|
|
// assign it one (we could spill the current though).
|
|
|
|
else {
|
2004-02-07 02:08:18 +08:00
|
|
|
assignRegOrStackSlotAtInterval(cur);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
2004-01-07 17:20:58 +08:00
|
|
|
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(printIntervals("active", active_.begin(), active_.end()));
|
|
|
|
DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
|
|
|
|
}
|
2004-07-24 11:32:06 +08:00
|
|
|
numIntervals += li_->getNumIntervals();
|
2004-07-05 01:23:35 +08:00
|
|
|
efficiency = double(numIterations) / double(numIntervals);
|
2004-01-07 17:20:58 +08:00
|
|
|
|
2003-12-13 13:50:19 +08:00
|
|
|
// expire any remaining active intervals
|
2004-07-22 16:14:44 +08:00
|
|
|
for (IntervalPtrs::reverse_iterator
|
|
|
|
i = active_.rbegin(); i != active_.rend(); ) {
|
2003-12-13 13:50:19 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\tinterval " << **i << " expired\n");
|
2004-02-24 07:08:11 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(reg))
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->delRegUse(reg);
|
2004-07-22 16:14:44 +08:00
|
|
|
i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// expire any remaining inactive intervals
|
|
|
|
for (IntervalPtrs::reverse_iterator
|
|
|
|
i = inactive_.rbegin(); i != inactive_.rend(); ) {
|
|
|
|
DEBUG(std::cerr << "\tinterval " << **i << " expired\n");
|
|
|
|
i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
|
2003-12-13 13:50:19 +08:00
|
|
|
}
|
2003-12-14 21:24:17 +08:00
|
|
|
|
2004-02-24 07:08:11 +08:00
|
|
|
DEBUG(std::cerr << *vrm_);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
|
2004-07-24 11:32:06 +08:00
|
|
|
void RA::initIntervalSets()
|
2004-01-07 17:20:58 +08:00
|
|
|
{
|
|
|
|
assert(unhandled_.empty() && fixed_.empty() &&
|
|
|
|
active_.empty() && inactive_.empty() &&
|
|
|
|
"interval sets should be empty on initialization");
|
|
|
|
|
2004-07-24 11:32:06 +08:00
|
|
|
for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i){
|
2004-07-24 19:44:15 +08:00
|
|
|
unhandled_.push(&i->second);
|
|
|
|
if (MRegisterInfo::isPhysicalRegister(i->second.reg))
|
|
|
|
fixed_.push_back(&i->second);
|
2004-01-07 17:20:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RA::processActiveIntervals(IntervalPtrs::value_type cur)
|
2003-11-20 11:32:25 +08:00
|
|
|
{
|
|
|
|
DEBUG(std::cerr << "\tprocessing active intervals:\n");
|
2004-07-22 16:14:44 +08:00
|
|
|
for (IntervalPtrs::reverse_iterator
|
|
|
|
i = active_.rbegin(); i != active_.rend();) {
|
2003-11-20 11:32:25 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
2004-01-17 04:17:05 +08:00
|
|
|
// remove expired intervals
|
|
|
|
if ((*i)->expiredAt(cur->start())) {
|
2003-11-20 11:32:25 +08:00
|
|
|
DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
|
2004-02-24 07:08:11 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(reg))
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->delRegUse(reg);
|
2003-12-21 13:43:40 +08:00
|
|
|
// remove from active
|
2004-07-22 16:14:44 +08:00
|
|
|
i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
|
2003-12-21 13:43:40 +08:00
|
|
|
}
|
|
|
|
// move inactive intervals to inactive list
|
|
|
|
else if (!(*i)->liveAt(cur->start())) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\tinterval " << **i << " inactive\n");
|
2004-02-24 07:08:11 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(reg))
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->delRegUse(reg);
|
2003-12-21 13:43:40 +08:00
|
|
|
// add to inactive
|
|
|
|
inactive_.push_back(*i);
|
|
|
|
// remove from active
|
2004-07-22 16:14:44 +08:00
|
|
|
i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-07 17:20:58 +08:00
|
|
|
void RA::processInactiveIntervals(IntervalPtrs::value_type cur)
|
2003-11-20 11:32:25 +08:00
|
|
|
{
|
2003-12-21 13:43:40 +08:00
|
|
|
DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
|
2004-07-22 16:14:44 +08:00
|
|
|
for (IntervalPtrs::reverse_iterator
|
|
|
|
i = inactive_.rbegin(); i != inactive_.rend();) {
|
2003-12-21 13:43:40 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
|
|
|
|
2004-01-17 04:17:05 +08:00
|
|
|
// remove expired intervals
|
|
|
|
if ((*i)->expiredAt(cur->start())) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n");
|
2003-12-21 13:43:40 +08:00
|
|
|
// remove from inactive
|
2004-07-22 16:14:44 +08:00
|
|
|
i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
|
2003-12-21 13:43:40 +08:00
|
|
|
}
|
|
|
|
// move re-activated intervals in active list
|
|
|
|
else if ((*i)->liveAt(cur->start())) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\tinterval " << **i << " active\n");
|
2004-02-24 07:08:11 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(reg))
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->addRegUse(reg);
|
2003-12-21 13:43:40 +08:00
|
|
|
// add to active
|
|
|
|
active_.push_back(*i);
|
|
|
|
// remove from inactive
|
2004-07-22 16:14:44 +08:00
|
|
|
i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
|
2003-12-21 13:43:40 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
void RA::updateSpillWeights(unsigned reg, SpillWeights::value_type weight)
|
|
|
|
{
|
|
|
|
spillWeights_[reg] += weight;
|
|
|
|
for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
|
|
|
|
spillWeights_[*as] += weight;
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
|
2004-07-22 16:14:44 +08:00
|
|
|
void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
|
2003-11-20 11:32:25 +08:00
|
|
|
{
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\tallocating current interval: ");
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2004-02-23 14:10:13 +08:00
|
|
|
PhysRegTracker backupPrt = *prt_;
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
spillWeights_.assign(mri_->getNumRegs(), 0.0);
|
|
|
|
|
|
|
|
// for each interval in active update spill weights
|
2004-01-07 17:20:58 +08:00
|
|
|
for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
|
|
|
|
i != e; ++i) {
|
2003-12-21 13:43:40 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
2004-02-07 02:08:18 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(reg))
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2004-02-07 02:08:18 +08:00
|
|
|
updateSpillWeights(reg, (*i)->weight);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
// for every interval in inactive we overlap with, mark the
|
|
|
|
// register as not free and update spill weights
|
2004-01-07 17:20:58 +08:00
|
|
|
for (IntervalPtrs::const_iterator i = inactive_.begin(),
|
|
|
|
e = inactive_.end(); i != e; ++i) {
|
2004-02-07 02:08:18 +08:00
|
|
|
if (cur->overlaps(**i)) {
|
|
|
|
unsigned reg = (*i)->reg;
|
|
|
|
if (MRegisterInfo::isVirtualRegister(reg))
|
2004-02-24 07:47:10 +08:00
|
|
|
reg = vrm_->getPhys(reg);
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->addRegUse(reg);
|
2004-02-07 02:08:18 +08:00
|
|
|
updateSpillWeights(reg, (*i)->weight);
|
|
|
|
}
|
|
|
|
}
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
// for every interval in fixed we overlap with,
|
|
|
|
// mark the register as not free and update spill weights
|
|
|
|
for (IntervalPtrs::const_iterator i = fixed_.begin(),
|
|
|
|
e = fixed_.end(); i != e; ++i) {
|
|
|
|
if (cur->overlaps(**i)) {
|
|
|
|
unsigned reg = (*i)->reg;
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->addRegUse(reg);
|
2004-02-07 02:08:18 +08:00
|
|
|
updateSpillWeights(reg, (*i)->weight);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
}
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2004-02-07 02:08:18 +08:00
|
|
|
unsigned physReg = getFreePhysReg(cur);
|
2004-02-15 18:24:21 +08:00
|
|
|
// restore the physical register tracker
|
2004-02-23 14:10:13 +08:00
|
|
|
*prt_ = backupPrt;
|
2004-02-15 18:24:21 +08:00
|
|
|
// if we find a free register, we are done: assign this virtual to
|
|
|
|
// the free physical register and add this interval to the active
|
|
|
|
// list.
|
2004-02-07 02:08:18 +08:00
|
|
|
if (physReg) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << mri_->getName(physReg) << '\n');
|
2004-02-24 07:08:11 +08:00
|
|
|
vrm_->assignVirt2Phys(cur->reg, physReg);
|
|
|
|
prt_->addRegUse(physReg);
|
2004-02-07 02:08:18 +08:00
|
|
|
active_.push_back(cur);
|
2004-02-15 18:24:21 +08:00
|
|
|
handled_.push_back(cur);
|
2004-02-07 02:08:18 +08:00
|
|
|
return;
|
2003-12-24 02:00:33 +08:00
|
|
|
}
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "no free registers\n");
|
2003-12-24 02:00:33 +08:00
|
|
|
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
|
2004-02-07 02:08:18 +08:00
|
|
|
|
2004-05-07 00:25:59 +08:00
|
|
|
float minWeight = HUGE_VAL;
|
2003-12-21 13:43:40 +08:00
|
|
|
unsigned minReg = 0;
|
|
|
|
const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
|
|
|
|
for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
|
|
|
|
i != rc->allocation_order_end(*mf_); ++i) {
|
|
|
|
unsigned reg = *i;
|
2004-02-15 18:24:21 +08:00
|
|
|
if (minWeight > spillWeights_[reg]) {
|
2004-02-07 02:08:18 +08:00
|
|
|
minWeight = spillWeights_[reg];
|
2003-12-21 13:43:40 +08:00
|
|
|
minReg = reg;
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
}
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\tregister with min weight: "
|
2004-01-23 03:24:43 +08:00
|
|
|
<< mri_->getName(minReg) << " (" << minWeight << ")\n");
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2004-05-30 15:24:39 +08:00
|
|
|
// if the current has the minimum weight, we need to spill it and
|
|
|
|
// add any added intervals back to unhandled, and restart
|
|
|
|
// linearscan.
|
2004-02-20 14:15:40 +08:00
|
|
|
if (cur->weight <= minWeight) {
|
|
|
|
DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
|
2004-02-24 07:08:11 +08:00
|
|
|
int slot = vrm_->assignVirt2StackSlot(cur->reg);
|
2004-06-21 21:10:56 +08:00
|
|
|
std::vector<LiveInterval*> added =
|
2004-05-30 15:24:39 +08:00
|
|
|
li_->addIntervalsForSpills(*cur, *vrm_, slot);
|
2004-07-19 12:47:36 +08:00
|
|
|
if (added.empty())
|
|
|
|
return; // Early exit if all spills were folded.
|
|
|
|
|
|
|
|
// Merge added with unhandled. Note that we know that
|
|
|
|
// addIntervalsForSpills returns intervals sorted by their starting
|
|
|
|
// point.
|
2004-07-22 16:14:44 +08:00
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i)
|
|
|
|
unhandled_.push(added[i]);
|
2004-02-07 02:08:18 +08:00
|
|
|
return;
|
2003-12-21 13:43:40 +08:00
|
|
|
}
|
2004-02-07 02:08:18 +08:00
|
|
|
|
2004-02-20 14:15:40 +08:00
|
|
|
// push the current interval back to unhandled since we are going
|
|
|
|
// to re-run at least this iteration. Since we didn't modify it it
|
|
|
|
// should go back right in the front of the list
|
2004-07-22 16:14:44 +08:00
|
|
|
unhandled_.push(cur);
|
2004-02-20 14:15:40 +08:00
|
|
|
|
2004-02-15 18:24:21 +08:00
|
|
|
// otherwise we spill all intervals aliasing the register with
|
|
|
|
// minimum weight, rollback to the interval with the earliest
|
|
|
|
// start point and let the linear scan algorithm run again
|
2004-06-21 21:10:56 +08:00
|
|
|
std::vector<LiveInterval*> added;
|
2004-02-23 08:50:15 +08:00
|
|
|
assert(MRegisterInfo::isPhysicalRegister(minReg) &&
|
|
|
|
"did not choose a register to spill?");
|
2004-02-07 02:08:18 +08:00
|
|
|
std::vector<bool> toSpill(mri_->getNumRegs(), false);
|
|
|
|
toSpill[minReg] = true;
|
|
|
|
for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
|
|
|
|
toSpill[*as] = true;
|
2004-02-15 18:24:21 +08:00
|
|
|
unsigned earliestStart = cur->start();
|
2004-02-07 02:08:18 +08:00
|
|
|
|
2004-05-30 15:24:39 +08:00
|
|
|
std::set<unsigned> spilled;
|
|
|
|
|
2004-07-22 16:14:44 +08:00
|
|
|
for (IntervalPtrs::iterator
|
|
|
|
i = active_.begin(); i != active_.end(); ++i) {
|
2004-02-07 02:08:18 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
|
|
|
if (MRegisterInfo::isVirtualRegister(reg) &&
|
2004-02-24 07:47:10 +08:00
|
|
|
toSpill[vrm_->getPhys(reg)] &&
|
2004-02-07 02:08:18 +08:00
|
|
|
cur->overlaps(**i)) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\t\tspilling(a): " << **i << '\n');
|
|
|
|
earliestStart = std::min(earliestStart, (*i)->start());
|
2004-02-24 07:08:11 +08:00
|
|
|
int slot = vrm_->assignVirt2StackSlot((*i)->reg);
|
2004-06-21 21:10:56 +08:00
|
|
|
std::vector<LiveInterval*> newIs =
|
2004-05-30 15:24:39 +08:00
|
|
|
li_->addIntervalsForSpills(**i, *vrm_, slot);
|
|
|
|
std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
|
|
|
|
spilled.insert(reg);
|
2004-02-07 02:08:18 +08:00
|
|
|
}
|
|
|
|
}
|
2004-07-22 16:14:44 +08:00
|
|
|
for (IntervalPtrs::iterator
|
|
|
|
i = inactive_.begin(); i != inactive_.end(); ++i) {
|
2004-02-07 02:08:18 +08:00
|
|
|
unsigned reg = (*i)->reg;
|
|
|
|
if (MRegisterInfo::isVirtualRegister(reg) &&
|
2004-02-24 07:47:10 +08:00
|
|
|
toSpill[vrm_->getPhys(reg)] &&
|
2004-02-07 02:08:18 +08:00
|
|
|
cur->overlaps(**i)) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\t\tspilling(i): " << **i << '\n');
|
|
|
|
earliestStart = std::min(earliestStart, (*i)->start());
|
2004-02-24 07:08:11 +08:00
|
|
|
int slot = vrm_->assignVirt2StackSlot((*i)->reg);
|
2004-06-21 21:10:56 +08:00
|
|
|
std::vector<LiveInterval*> newIs =
|
2004-05-30 15:24:39 +08:00
|
|
|
li_->addIntervalsForSpills(**i, *vrm_, slot);
|
|
|
|
std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
|
|
|
|
spilled.insert(reg);
|
2004-02-15 18:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
|
2004-02-15 18:24:21 +08:00
|
|
|
// scan handled in reverse order and undo each one, restoring the
|
2004-03-17 08:48:59 +08:00
|
|
|
// state of unhandled
|
2004-02-15 18:24:21 +08:00
|
|
|
while (!handled_.empty()) {
|
2004-07-22 16:14:44 +08:00
|
|
|
LiveInterval* i = handled_.back();
|
2004-02-15 18:24:21 +08:00
|
|
|
// if this interval starts before t we are done
|
2004-05-30 15:24:39 +08:00
|
|
|
if (i->start() < earliestStart)
|
2004-02-15 18:24:21 +08:00
|
|
|
break;
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
|
2004-02-15 18:24:21 +08:00
|
|
|
handled_.pop_back();
|
|
|
|
IntervalPtrs::iterator it;
|
|
|
|
if ((it = find(active_.begin(), active_.end(), i)) != active_.end()) {
|
|
|
|
active_.erase(it);
|
|
|
|
if (MRegisterInfo::isPhysicalRegister(i->reg)) {
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->delRegUse(i->reg);
|
2004-07-22 16:14:44 +08:00
|
|
|
unhandled_.push(i);
|
2004-02-15 18:24:21 +08:00
|
|
|
}
|
|
|
|
else {
|
2004-05-30 15:24:39 +08:00
|
|
|
if (!spilled.count(i->reg))
|
2004-07-22 16:14:44 +08:00
|
|
|
unhandled_.push(i);
|
2004-02-24 07:47:10 +08:00
|
|
|
prt_->delRegUse(vrm_->getPhys(i->reg));
|
2004-02-27 14:11:15 +08:00
|
|
|
vrm_->clearVirt(i->reg);
|
2004-02-15 18:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((it = find(inactive_.begin(), inactive_.end(), i)) != inactive_.end()) {
|
|
|
|
inactive_.erase(it);
|
|
|
|
if (MRegisterInfo::isPhysicalRegister(i->reg))
|
2004-07-22 16:14:44 +08:00
|
|
|
unhandled_.push(i);
|
2004-02-15 18:24:21 +08:00
|
|
|
else {
|
2004-05-30 15:24:39 +08:00
|
|
|
if (!spilled.count(i->reg))
|
2004-07-22 16:14:44 +08:00
|
|
|
unhandled_.push(i);
|
2004-05-30 15:24:39 +08:00
|
|
|
vrm_->clearVirt(i->reg);
|
2004-02-15 18:24:21 +08:00
|
|
|
}
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
2004-02-07 02:08:18 +08:00
|
|
|
else {
|
2004-03-17 08:48:59 +08:00
|
|
|
if (MRegisterInfo::isVirtualRegister(i->reg))
|
2004-02-27 14:11:15 +08:00
|
|
|
vrm_->clearVirt(i->reg);
|
2004-07-22 16:14:44 +08:00
|
|
|
unhandled_.push(i);
|
2004-02-15 18:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan the rest and undo each interval that expired after t and
|
|
|
|
// insert it in active (the next iteration of the algorithm will
|
|
|
|
// put it in inactive if required)
|
|
|
|
IntervalPtrs::iterator i = handled_.begin(), e = handled_.end();
|
|
|
|
for (; i != e; ++i) {
|
|
|
|
if (!(*i)->expiredAt(earliestStart) && (*i)->expiredAt(cur->start())) {
|
2004-02-20 14:15:40 +08:00
|
|
|
DEBUG(std::cerr << "\t\t\tundo changes for: " << **i << '\n');
|
2004-02-15 18:24:21 +08:00
|
|
|
active_.push_back(*i);
|
|
|
|
if (MRegisterInfo::isPhysicalRegister((*i)->reg))
|
2004-02-23 14:10:13 +08:00
|
|
|
prt_->addRegUse((*i)->reg);
|
2004-02-24 07:08:11 +08:00
|
|
|
else
|
2004-02-24 07:47:10 +08:00
|
|
|
prt_->addRegUse(vrm_->getPhys((*i)->reg));
|
2004-02-07 02:08:18 +08:00
|
|
|
}
|
|
|
|
}
|
2004-05-30 15:24:39 +08:00
|
|
|
|
2004-06-21 21:10:56 +08:00
|
|
|
std::sort(added.begin(), added.end(), less_ptr<LiveInterval>());
|
2004-05-30 15:24:39 +08:00
|
|
|
// merge added with unhandled
|
2004-07-22 16:14:44 +08:00
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i)
|
|
|
|
unhandled_.push(added[i]);
|
2004-02-15 18:24:21 +08:00
|
|
|
}
|
2003-12-21 13:43:40 +08:00
|
|
|
|
2004-07-22 16:14:44 +08:00
|
|
|
unsigned RA::getFreePhysReg(LiveInterval* cur)
|
2003-11-20 11:32:25 +08:00
|
|
|
{
|
2003-12-21 13:43:40 +08:00
|
|
|
const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
|
2003-12-29 01:58:18 +08:00
|
|
|
|
2003-12-21 13:43:40 +08:00
|
|
|
for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_);
|
|
|
|
i != rc->allocation_order_end(*mf_); ++i) {
|
|
|
|
unsigned reg = *i;
|
2004-02-23 14:10:13 +08:00
|
|
|
if (prt_->isRegAvail(reg))
|
2003-12-21 13:43:40 +08:00
|
|
|
return reg;
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass* llvm::createLinearScanRegisterAllocator() {
|
|
|
|
return new RA();
|
|
|
|
}
|