2003-11-20 03:32:25 +00:00
|
|
|
//===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2003-11-20 03:32:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a linear scan register allocator.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-02-24 08:58:30 +00:00
|
|
|
|
2003-11-20 03:32:25 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2005-08-23 22:27:31 +00:00
|
|
|
#include "PhysRegTracker.h"
|
|
|
|
#include "VirtRegMap.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/Function.h"
|
2008-06-04 09:18:41 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2007-12-11 02:09:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2006-08-02 12:30:23 +00:00
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2007-09-06 16:18:45 +00:00
|
|
|
#include "llvm/CodeGen/RegisterCoalescer.h"
|
2008-02-10 18:45:23 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2007-11-03 07:20:12 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2005-08-23 22:27:31 +00:00
|
|
|
#include "llvm/ADT/EquivalenceClasses.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2005-08-23 22:27:31 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-02-15 10:24:21 +00:00
|
|
|
#include <algorithm>
|
2004-05-30 07:24:39 +00:00
|
|
|
#include <set>
|
2004-07-22 08:14:44 +00:00
|
|
|
#include <queue>
|
2005-12-28 04:55:42 +00:00
|
|
|
#include <memory>
|
2006-12-02 02:22:01 +00:00
|
|
|
#include <cmath>
|
2003-11-20 03:32:25 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 22:41:21 +00:00
|
|
|
STATISTIC(NumIters , "Number of iterations performed");
|
|
|
|
STATISTIC(NumBacktracks, "Number of times we had to backtrack");
|
2007-11-03 07:20:12 +00:00
|
|
|
STATISTIC(NumCoalesce, "Number of copies coalesced");
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
NewHeuristic("new-spilling-heuristic",
|
|
|
|
cl::desc("Use new spilling heuristic"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2006-12-19 22:41:21 +00:00
|
|
|
static RegisterRegAlloc
|
|
|
|
linearscanRegAlloc("linearscan", " linear scan register allocator",
|
|
|
|
createLinearScanRegisterAllocator);
|
2006-08-01 14:21:23 +00:00
|
|
|
|
2006-12-19 22:41:21 +00:00
|
|
|
namespace {
|
2007-05-08 19:02:46 +00:00
|
|
|
struct VISIBILITY_HIDDEN RALinScan : public MachineFunctionPass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2007-05-08 19:02:46 +00:00
|
|
|
RALinScan() : MachineFunctionPass((intptr_t)&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
|
|
|
|
typedef std::vector<IntervalPtr> IntervalPtrs;
|
|
|
|
private:
|
2005-08-23 22:27:31 +00:00
|
|
|
/// RelatedRegClasses - This structure is built the first time a function is
|
|
|
|
/// compiled, and keeps track of which register classes have registers that
|
|
|
|
/// belong to multiple classes or have aliases that are in other classes.
|
|
|
|
EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
|
|
|
|
std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
MachineFunction* mf_;
|
2008-06-20 21:45:16 +00:00
|
|
|
MachineRegisterInfo* mri_;
|
2004-08-04 09:46:26 +00:00
|
|
|
const TargetMachine* tm_;
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo* tri_;
|
2007-11-03 07:20:12 +00:00
|
|
|
const TargetInstrInfo* tii_;
|
2007-12-31 04:13:23 +00:00
|
|
|
MachineRegisterInfo *reginfo_;
|
2007-11-03 07:20:12 +00:00
|
|
|
BitVector allocatableRegs_;
|
2004-08-04 09:46:26 +00:00
|
|
|
LiveIntervals* li_;
|
2008-06-04 09:18:41 +00:00
|
|
|
LiveStacks* ls_;
|
2007-12-11 02:09:15 +00:00
|
|
|
const MachineLoopInfo *loopInfo;
|
2004-11-18 02:42:27 +00:00
|
|
|
|
|
|
|
/// handled_ - Intervals are added to the handled_ set in the order of their
|
|
|
|
/// start value. This is uses for backtracking.
|
|
|
|
std::vector<LiveInterval*> handled_;
|
|
|
|
|
|
|
|
/// fixed_ - Intervals that correspond to machine registers.
|
|
|
|
///
|
|
|
|
IntervalPtrs fixed_;
|
|
|
|
|
|
|
|
/// active_ - Intervals that are currently being processed, and which have a
|
|
|
|
/// live range active for the current point.
|
|
|
|
IntervalPtrs active_;
|
|
|
|
|
|
|
|
/// inactive_ - Intervals that are currently being processed, but which have
|
|
|
|
/// a hold at the current point.
|
|
|
|
IntervalPtrs inactive_;
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
typedef std::priority_queue<LiveInterval*,
|
2004-11-18 02:42:27 +00:00
|
|
|
std::vector<LiveInterval*>,
|
2004-08-04 09:46:26 +00:00
|
|
|
greater_ptr<LiveInterval> > IntervalHeap;
|
|
|
|
IntervalHeap unhandled_;
|
|
|
|
std::auto_ptr<PhysRegTracker> prt_;
|
|
|
|
std::auto_ptr<VirtRegMap> vrm_;
|
|
|
|
std::auto_ptr<Spiller> spiller_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual const char* getPassName() const {
|
|
|
|
return "Linear Scan Register Allocator";
|
|
|
|
}
|
2003-11-20 03:32:25 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<LiveIntervals>();
|
2007-09-06 16:18:45 +00:00
|
|
|
// Make sure PassManager knows which analyses to make available
|
|
|
|
// to coalescing and which analyses coalescing invalidates.
|
|
|
|
AU.addRequiredTransitive<RegisterCoalescer>();
|
2008-06-04 09:18:41 +00:00
|
|
|
AU.addRequired<LiveStacks>();
|
|
|
|
AU.addPreserved<LiveStacks>();
|
2007-12-11 02:09:15 +00:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2008-01-04 20:54:55 +00:00
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2004-08-04 09:46:26 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// linearScan - the linear scan algorithm
|
|
|
|
void linearScan();
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// initIntervalSets - initialize the interval sets.
|
|
|
|
///
|
2004-08-04 09:46:26 +00:00
|
|
|
void initIntervalSets();
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// processActiveIntervals - expire old intervals and move non-overlapping
|
|
|
|
/// ones to the inactive list.
|
|
|
|
void processActiveIntervals(unsigned CurPoint);
|
2003-11-20 03:32:25 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// processInactiveIntervals - expire old intervals and move overlapping
|
|
|
|
/// ones to the active list.
|
|
|
|
void processInactiveIntervals(unsigned CurPoint);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
|
|
|
/// assignRegOrStackSlotAtInterval - assign a register if one
|
|
|
|
/// is available, or spill.
|
|
|
|
void assignRegOrStackSlotAtInterval(LiveInterval* cur);
|
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
/// findIntervalsToSpill - Determine the intervals to spill for the
|
|
|
|
/// specified interval. It's passed the physical registers whose spill
|
|
|
|
/// weight is the lowest among all the registers whose live intervals
|
|
|
|
/// conflict with the interval.
|
|
|
|
void findIntervalsToSpill(LiveInterval *cur,
|
|
|
|
std::vector<std::pair<unsigned,float> > &Candidates,
|
|
|
|
unsigned NumCands,
|
|
|
|
SmallVector<LiveInterval*, 8> &SpillIntervals);
|
|
|
|
|
2007-11-03 07:20:12 +00:00
|
|
|
/// attemptTrivialCoalescing - If a simple interval is defined by a copy,
|
|
|
|
/// try allocate the definition the same register as the source register
|
|
|
|
/// if the register is not defined during live time of the interval. This
|
|
|
|
/// eliminate a copy. This is used to coalesce copies which were not
|
|
|
|
/// coalesced away before allocation either due to dest and src being in
|
|
|
|
/// different register classes or because the coalescer was overly
|
|
|
|
/// conservative.
|
|
|
|
unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg);
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
///
|
|
|
|
/// register handling helpers
|
|
|
|
///
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// getFreePhysReg - return a free physical register for this virtual
|
|
|
|
/// register interval if we have one, otherwise return 0.
|
2004-08-04 09:46:26 +00:00
|
|
|
unsigned getFreePhysReg(LiveInterval* cur);
|
|
|
|
|
|
|
|
/// assignVirt2StackSlot - assigns this virtual register to a
|
|
|
|
/// stack slot. returns the stack slot
|
|
|
|
int assignVirt2StackSlot(unsigned virtReg);
|
|
|
|
|
2005-08-23 22:27:31 +00:00
|
|
|
void ComputeRelatedRegClasses();
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
template <typename ItTy>
|
|
|
|
void printIntervals(const char* const str, ItTy i, ItTy e) const {
|
2006-11-17 00:50:36 +00:00
|
|
|
if (str) DOUT << str << " intervals:\n";
|
2004-08-04 09:46:26 +00:00
|
|
|
for (; i != e; ++i) {
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t" << *i->first << " -> ";
|
2004-11-18 02:42:27 +00:00
|
|
|
unsigned reg = i->first->reg;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(reg)) {
|
2004-08-04 09:46:26 +00:00
|
|
|
reg = vrm_->getPhys(reg);
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << tri_->getName(reg) << '\n';
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2007-05-08 19:02:46 +00:00
|
|
|
char RALinScan::ID = 0;
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 09:18:41 +00:00
|
|
|
static RegisterPass<RALinScan>
|
|
|
|
X("linearscan-regalloc", "Linear Scan Register Allocator");
|
|
|
|
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::ComputeRelatedRegClasses() {
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo &TRI = *tri_;
|
2005-08-23 22:27:31 +00:00
|
|
|
|
|
|
|
// First pass, add all reg classes to the union, and determine at least one
|
|
|
|
// reg class that each register is in.
|
|
|
|
bool HasAliases = false;
|
2008-02-10 18:45:23 +00:00
|
|
|
for (TargetRegisterInfo::regclass_iterator RCI = TRI.regclass_begin(),
|
|
|
|
E = TRI.regclass_end(); RCI != E; ++RCI) {
|
2005-08-23 22:27:31 +00:00
|
|
|
RelatedRegClasses.insert(*RCI);
|
|
|
|
for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
|
|
|
|
I != E; ++I) {
|
2008-02-10 18:45:23 +00:00
|
|
|
HasAliases = HasAliases || *TRI.getAliasSet(*I) != 0;
|
2005-08-23 22:27:31 +00:00
|
|
|
|
|
|
|
const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
|
|
|
|
if (PRC) {
|
|
|
|
// Already processed this register. Just make sure we know that
|
|
|
|
// multiple register classes share a register.
|
|
|
|
RelatedRegClasses.unionSets(PRC, *RCI);
|
|
|
|
} else {
|
|
|
|
PRC = *RCI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass, now that we know conservatively what register classes each reg
|
|
|
|
// belongs to, add info about aliases. We don't need to do this for targets
|
|
|
|
// without register aliases.
|
|
|
|
if (HasAliases)
|
|
|
|
for (std::map<unsigned, const TargetRegisterClass*>::iterator
|
|
|
|
I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
|
|
|
|
I != E; ++I)
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AS = TRI.getAliasSet(I->first); *AS; ++AS)
|
2005-08-23 22:27:31 +00:00
|
|
|
RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
|
|
|
|
}
|
|
|
|
|
2007-11-03 07:20:12 +00:00
|
|
|
/// attemptTrivialCoalescing - If a simple interval is defined by a copy,
|
|
|
|
/// try allocate the definition the same register as the source register
|
|
|
|
/// if the register is not defined during live time of the interval. This
|
|
|
|
/// eliminate a copy. This is used to coalesce copies which were not
|
|
|
|
/// coalesced away before allocation either due to dest and src being in
|
|
|
|
/// different register classes or because the coalescer was overly
|
|
|
|
/// conservative.
|
|
|
|
unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
|
2007-11-04 08:32:21 +00:00
|
|
|
if ((cur.preference && cur.preference == Reg) || !cur.containsOneValue())
|
2007-11-03 07:20:12 +00:00
|
|
|
return Reg;
|
|
|
|
|
|
|
|
VNInfo *vni = cur.getValNumInfo(0);
|
|
|
|
if (!vni->def || vni->def == ~1U || vni->def == ~0U)
|
|
|
|
return Reg;
|
|
|
|
MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
|
|
|
|
unsigned SrcReg, DstReg;
|
|
|
|
if (!CopyMI || !tii_->isMoveInstr(*CopyMI, SrcReg, DstReg))
|
|
|
|
return Reg;
|
2008-02-20 12:07:57 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
2007-11-03 07:20:12 +00:00
|
|
|
if (!vrm_->isAssignedReg(SrcReg))
|
|
|
|
return Reg;
|
|
|
|
else
|
|
|
|
SrcReg = vrm_->getPhys(SrcReg);
|
2008-02-20 12:07:57 +00:00
|
|
|
}
|
2007-11-03 07:20:12 +00:00
|
|
|
if (Reg == SrcReg)
|
|
|
|
return Reg;
|
|
|
|
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RC = reginfo_->getRegClass(cur.reg);
|
2007-11-03 07:20:12 +00:00
|
|
|
if (!RC->contains(SrcReg))
|
|
|
|
return Reg;
|
|
|
|
|
|
|
|
// Try to coalesce.
|
|
|
|
if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) {
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << "Coalescing: " << cur << " -> " << tri_->getName(SrcReg)
|
2008-02-26 21:11:01 +00:00
|
|
|
<< '\n';
|
2007-11-03 07:20:12 +00:00
|
|
|
vrm_->clearVirt(cur.reg);
|
|
|
|
vrm_->assignVirt2Phys(cur.reg, SrcReg);
|
|
|
|
++NumCoalesce;
|
|
|
|
return SrcReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Reg;
|
|
|
|
}
|
|
|
|
|
2007-05-08 19:02:46 +00:00
|
|
|
bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
|
2004-08-04 09:46:26 +00:00
|
|
|
mf_ = &fn;
|
2008-06-20 21:45:16 +00:00
|
|
|
mri_ = &fn.getRegInfo();
|
2004-08-04 09:46:26 +00:00
|
|
|
tm_ = &fn.getTarget();
|
2008-02-10 18:45:23 +00:00
|
|
|
tri_ = tm_->getRegisterInfo();
|
2007-11-03 07:20:12 +00:00
|
|
|
tii_ = tm_->getInstrInfo();
|
2007-12-31 04:13:23 +00:00
|
|
|
reginfo_ = &mf_->getRegInfo();
|
2008-02-10 18:45:23 +00:00
|
|
|
allocatableRegs_ = tri_->getAllocatableSet(fn);
|
2004-08-04 09:46:26 +00:00
|
|
|
li_ = &getAnalysis<LiveIntervals>();
|
2008-06-04 09:18:41 +00:00
|
|
|
ls_ = &getAnalysis<LiveStacks>();
|
2007-12-11 02:09:15 +00:00
|
|
|
loopInfo = &getAnalysis<MachineLoopInfo>();
|
2004-11-18 04:33:31 +00:00
|
|
|
|
2007-09-06 16:18:45 +00:00
|
|
|
// We don't run the coalescer here because we have no reason to
|
|
|
|
// interact with it. If the coalescer requires interaction, it
|
|
|
|
// won't do anything. If it doesn't require interaction, we assume
|
|
|
|
// it was run as a separate pass.
|
|
|
|
|
2005-08-23 22:27:31 +00:00
|
|
|
// If this is the first function compiled, compute the related reg classes.
|
|
|
|
if (RelatedRegClasses.empty())
|
|
|
|
ComputeRelatedRegClasses();
|
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
if (!prt_.get()) prt_.reset(new PhysRegTracker(*tri_));
|
2004-08-04 09:46:26 +00:00
|
|
|
vrm_.reset(new VirtRegMap(*mf_));
|
|
|
|
if (!spiller_.get()) spiller_.reset(createSpiller());
|
2003-12-21 05:43:40 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
initIntervalSets();
|
2003-11-20 03:32:25 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
linearScan();
|
2004-02-24 08:58:30 +00:00
|
|
|
|
2005-01-23 22:45:13 +00:00
|
|
|
// Rewrite spill code and update the PhysRegsUsed set.
|
2004-08-04 09:46:26 +00:00
|
|
|
spiller_->runOnMachineFunction(*mf_, *vrm_);
|
2004-09-30 02:02:33 +00:00
|
|
|
vrm_.reset(); // Free the VirtRegMap
|
2004-11-18 02:42:27 +00:00
|
|
|
|
2008-06-23 23:51:16 +00:00
|
|
|
assert(unhandled_.empty() && "Unhandled live intervals remain!");
|
2004-11-18 02:42:27 +00:00
|
|
|
fixed_.clear();
|
|
|
|
active_.clear();
|
|
|
|
inactive_.clear();
|
|
|
|
handled_.clear();
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
return true;
|
2004-02-24 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 06:01:45 +00:00
|
|
|
/// initIntervalSets - initialize the interval sets.
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::initIntervalSets()
|
2004-11-18 06:01:45 +00:00
|
|
|
{
|
|
|
|
assert(unhandled_.empty() && fixed_.empty() &&
|
|
|
|
active_.empty() && inactive_.empty() &&
|
|
|
|
"interval sets should be empty on initialization");
|
|
|
|
|
|
|
|
for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(i->second.reg)) {
|
2007-12-31 04:13:23 +00:00
|
|
|
reginfo_->setPhysRegUsed(i->second.reg);
|
2004-11-18 06:01:45 +00:00
|
|
|
fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
|
2005-01-23 22:45:13 +00:00
|
|
|
} else
|
2004-11-18 06:01:45 +00:00
|
|
|
unhandled_.push(&i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::linearScan()
|
2004-02-24 08:58:30 +00:00
|
|
|
{
|
2004-08-04 09:46:26 +00:00
|
|
|
// linear scan algorithm
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "********** LINEAR SCAN **********\n";
|
|
|
|
DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
|
2004-08-04 09:46:26 +00:00
|
|
|
|
|
|
|
DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
|
|
|
|
|
|
|
|
while (!unhandled_.empty()) {
|
|
|
|
// pick the interval with the earliest start point
|
|
|
|
LiveInterval* cur = unhandled_.top();
|
|
|
|
unhandled_.pop();
|
2007-10-16 21:09:14 +00:00
|
|
|
++NumIters;
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\n*** CURRENT ***: " << *cur << '\n';
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2008-04-03 16:40:27 +00:00
|
|
|
if (!cur->empty()) {
|
|
|
|
processActiveIntervals(cur->beginNumber());
|
|
|
|
processInactiveIntervals(cur->beginNumber());
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2008-04-03 16:40:27 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(cur->reg) &&
|
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
}
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2004-11-18 06:01:45 +00:00
|
|
|
// Allocating a virtual register. try to find a free
|
|
|
|
// physical register or spill an interval (possibly this one) in order to
|
|
|
|
// assign it one.
|
|
|
|
assignRegOrStackSlotAtInterval(cur);
|
2004-07-22 08:14:44 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
DEBUG(printIntervals("active", active_.begin(), active_.end()));
|
|
|
|
DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// expire any remaining active intervals
|
2007-10-16 21:09:14 +00:00
|
|
|
while (!active_.empty()) {
|
|
|
|
IntervalPtr &IP = active_.back();
|
|
|
|
unsigned reg = IP.first->reg;
|
|
|
|
DOUT << "\tinterval " << *IP.first << " expired\n";
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(reg) &&
|
2004-11-18 06:01:45 +00:00
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
reg = vrm_->getPhys(reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
prt_->delRegUse(reg);
|
2007-10-16 21:09:14 +00:00
|
|
|
active_.pop_back();
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// expire any remaining inactive intervals
|
2007-10-16 21:09:14 +00:00
|
|
|
DEBUG(for (IntervalPtrs::reverse_iterator
|
2007-11-15 00:40:48 +00:00
|
|
|
i = inactive_.rbegin(); i != inactive_.rend(); ++i)
|
2007-10-16 21:09:14 +00:00
|
|
|
DOUT << "\tinterval " << *i->first << " expired\n");
|
|
|
|
inactive_.clear();
|
2004-08-04 09:46:26 +00:00
|
|
|
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44198 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-17 00:40:40 +00:00
|
|
|
// Add live-ins to every BB except for entry. Also perform trivial coalescing.
|
2007-10-17 02:12:22 +00:00
|
|
|
MachineFunction::iterator EntryMBB = mf_->begin();
|
2007-10-17 06:53:44 +00:00
|
|
|
SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
|
2007-10-17 02:12:22 +00:00
|
|
|
for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
|
2007-11-03 07:20:12 +00:00
|
|
|
LiveInterval &cur = i->second;
|
2007-10-17 02:12:22 +00:00
|
|
|
unsigned Reg = 0;
|
2008-02-10 18:45:23 +00:00
|
|
|
bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg);
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44198 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-17 00:40:40 +00:00
|
|
|
if (isPhys)
|
2007-10-17 02:12:22 +00:00
|
|
|
Reg = i->second.reg;
|
|
|
|
else if (vrm_->isAssignedReg(cur.reg))
|
2007-11-03 07:20:12 +00:00
|
|
|
Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
|
2007-10-17 02:12:22 +00:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44198 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-17 00:40:40 +00:00
|
|
|
// Ignore splited live intervals.
|
|
|
|
if (!isPhys && vrm_->getPreSplitReg(cur.reg))
|
|
|
|
continue;
|
2007-10-17 02:12:22 +00:00
|
|
|
for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const LiveRange &LR = *I;
|
|
|
|
if (li_->findLiveInMBBs(LR, LiveInMBBs)) {
|
|
|
|
for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
|
|
|
|
if (LiveInMBBs[i] != EntryMBB)
|
|
|
|
LiveInMBBs[i]->addLiveIn(Reg);
|
2007-10-17 06:53:44 +00:00
|
|
|
LiveInMBBs.clear();
|
2007-02-16 09:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << *vrm_;
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// processActiveIntervals - expire old intervals and move non-overlapping ones
|
|
|
|
/// to the inactive list.
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::processActiveIntervals(unsigned CurPoint)
|
2003-11-20 03:32:25 +00:00
|
|
|
{
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\tprocessing active intervals:\n";
|
2004-11-18 01:29:39 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
for (unsigned i = 0, e = active_.size(); i != e; ++i) {
|
|
|
|
LiveInterval *Interval = active_[i].first;
|
|
|
|
LiveInterval::iterator IntervalPos = active_[i].second;
|
|
|
|
unsigned reg = Interval->reg;
|
|
|
|
|
|
|
|
IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
|
2004-09-01 22:52:29 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
if (IntervalPos == Interval->end()) { // Remove expired intervals.
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\tinterval " << *Interval << " expired\n";
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(reg) &&
|
2004-11-18 06:01:45 +00:00
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
reg = vrm_->getPhys(reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
prt_->delRegUse(reg);
|
2004-11-18 02:42:27 +00:00
|
|
|
|
|
|
|
// Pop off the end of the list.
|
|
|
|
active_[i] = active_.back();
|
|
|
|
active_.pop_back();
|
|
|
|
--i; --e;
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
} else if (IntervalPos->start > CurPoint) {
|
|
|
|
// Move inactive intervals to inactive list.
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\tinterval " << *Interval << " inactive\n";
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(reg) &&
|
2004-11-18 06:01:45 +00:00
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
reg = vrm_->getPhys(reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
prt_->delRegUse(reg);
|
2004-11-18 02:42:27 +00:00
|
|
|
// add to inactive.
|
|
|
|
inactive_.push_back(std::make_pair(Interval, IntervalPos));
|
|
|
|
|
|
|
|
// Pop off the end of the list.
|
|
|
|
active_[i] = active_.back();
|
|
|
|
active_.pop_back();
|
|
|
|
--i; --e;
|
|
|
|
} else {
|
|
|
|
// Otherwise, just update the iterator position.
|
|
|
|
active_[i].second = IntervalPos;
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// processInactiveIntervals - expire old intervals and move overlapping
|
|
|
|
/// ones to the active list.
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::processInactiveIntervals(unsigned CurPoint)
|
2003-11-20 03:32:25 +00:00
|
|
|
{
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\tprocessing inactive intervals:\n";
|
2004-11-18 04:13:02 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
|
|
|
|
LiveInterval *Interval = inactive_[i].first;
|
|
|
|
LiveInterval::iterator IntervalPos = inactive_[i].second;
|
|
|
|
unsigned reg = Interval->reg;
|
|
|
|
|
|
|
|
IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
if (IntervalPos == Interval->end()) { // remove expired intervals.
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\tinterval " << *Interval << " expired\n";
|
2004-11-18 02:42:27 +00:00
|
|
|
|
|
|
|
// Pop off the end of the list.
|
|
|
|
inactive_[i] = inactive_.back();
|
|
|
|
inactive_.pop_back();
|
|
|
|
--i; --e;
|
|
|
|
} else if (IntervalPos->start <= CurPoint) {
|
|
|
|
// move re-activated intervals in active list
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\tinterval " << *Interval << " active\n";
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(reg) &&
|
2004-11-18 06:01:45 +00:00
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
reg = vrm_->getPhys(reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
prt_->addRegUse(reg);
|
|
|
|
// add to active
|
2004-11-18 02:42:27 +00:00
|
|
|
active_.push_back(std::make_pair(Interval, IntervalPos));
|
|
|
|
|
|
|
|
// Pop off the end of the list.
|
|
|
|
inactive_[i] = inactive_.back();
|
|
|
|
inactive_.pop_back();
|
|
|
|
--i; --e;
|
|
|
|
} else {
|
|
|
|
// Otherwise, just update the iterator position.
|
|
|
|
inactive_[i].second = IntervalPos;
|
2003-12-21 05:43:40 +00:00
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
2003-12-21 05:43:40 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// updateSpillWeights - updates the spill weights of the specifed physical
|
|
|
|
/// register and its weight.
|
2005-04-21 22:36:52 +00:00
|
|
|
static void updateSpillWeights(std::vector<float> &Weights,
|
2004-11-18 06:01:45 +00:00
|
|
|
unsigned reg, float weight,
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo *TRI) {
|
2004-11-18 06:01:45 +00:00
|
|
|
Weights[reg] += weight;
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned* as = TRI->getAliasSet(reg); *as; ++as)
|
2004-11-18 06:01:45 +00:00
|
|
|
Weights[*as] += weight;
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
2007-05-08 19:02:46 +00:00
|
|
|
static
|
|
|
|
RALinScan::IntervalPtrs::iterator
|
|
|
|
FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
|
|
|
|
for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
|
|
|
|
I != E; ++I)
|
2004-11-18 02:42:27 +00:00
|
|
|
if (I->first == LI) return I;
|
|
|
|
return IP.end();
|
|
|
|
}
|
|
|
|
|
2007-05-08 19:02:46 +00:00
|
|
|
static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){
|
2004-11-18 03:49:30 +00:00
|
|
|
for (unsigned i = 0, e = V.size(); i != e; ++i) {
|
2007-05-08 19:02:46 +00:00
|
|
|
RALinScan::IntervalPtr &IP = V[i];
|
2004-11-18 03:49:30 +00:00
|
|
|
LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
|
|
|
|
IP.second, Point);
|
|
|
|
if (I != IP.first->begin()) --I;
|
|
|
|
IP.second = I;
|
|
|
|
}
|
|
|
|
}
|
2004-11-18 02:42:27 +00:00
|
|
|
|
2008-06-04 09:18:41 +00:00
|
|
|
/// addStackInterval - Create a LiveInterval for stack if the specified live
|
|
|
|
/// interval has been spilled.
|
|
|
|
static void addStackInterval(LiveInterval *cur, LiveStacks *ls_,
|
2008-06-06 07:54:39 +00:00
|
|
|
LiveIntervals *li_, float &Weight,
|
|
|
|
VirtRegMap &vrm_) {
|
2008-06-04 09:18:41 +00:00
|
|
|
int SS = vrm_.getStackSlot(cur->reg);
|
|
|
|
if (SS == VirtRegMap::NO_STACK_SLOT)
|
|
|
|
return;
|
|
|
|
LiveInterval &SI = ls_->getOrCreateInterval(SS);
|
2008-06-06 07:54:39 +00:00
|
|
|
SI.weight += Weight;
|
|
|
|
|
2008-06-04 09:18:41 +00:00
|
|
|
VNInfo *VNI;
|
|
|
|
if (SI.getNumValNums())
|
|
|
|
VNI = SI.getValNumInfo(0);
|
|
|
|
else
|
|
|
|
VNI = SI.getNextValue(~0U, 0, ls_->getVNInfoAllocator());
|
|
|
|
|
|
|
|
LiveInterval &RI = li_->getInterval(cur->reg);
|
|
|
|
// FIXME: This may be overly conservative.
|
|
|
|
SI.MergeRangesInAsValue(RI, VNI);
|
|
|
|
}
|
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
/// getConflictWeight - Return the number of conflicts between cur
|
|
|
|
/// live interval and defs and uses of Reg weighted by loop depthes.
|
|
|
|
static float getConflictWeight(LiveInterval *cur, unsigned Reg,
|
|
|
|
LiveIntervals *li_,
|
|
|
|
MachineRegisterInfo *mri_,
|
|
|
|
const MachineLoopInfo *loopInfo) {
|
|
|
|
float Conflicts = 0;
|
|
|
|
for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg),
|
|
|
|
E = mri_->reg_end(); I != E; ++I) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
if (cur->liveAt(li_->getInstructionIndex(MI))) {
|
|
|
|
unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent());
|
|
|
|
Conflicts += powf(10.0f, (float)loopDepth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Conflicts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// findIntervalsToSpill - Determine the intervals to spill for the
|
|
|
|
/// specified interval. It's passed the physical registers whose spill
|
|
|
|
/// weight is the lowest among all the registers whose live intervals
|
|
|
|
/// conflict with the interval.
|
|
|
|
void RALinScan::findIntervalsToSpill(LiveInterval *cur,
|
|
|
|
std::vector<std::pair<unsigned,float> > &Candidates,
|
|
|
|
unsigned NumCands,
|
|
|
|
SmallVector<LiveInterval*, 8> &SpillIntervals) {
|
|
|
|
// We have figured out the *best* register to spill. But there are other
|
|
|
|
// registers that are pretty good as well (spill weight within 3%). Spill
|
|
|
|
// the one that has fewest defs and uses that conflict with cur.
|
|
|
|
float Conflicts[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
SmallVector<LiveInterval*, 8> SLIs[3];
|
|
|
|
|
|
|
|
DOUT << "\tConsidering " << NumCands << " candidates: ";
|
|
|
|
DEBUG(for (unsigned i = 0; i != NumCands; ++i)
|
|
|
|
DOUT << tri_->getName(Candidates[i].first) << " ";
|
|
|
|
DOUT << "\n";);
|
|
|
|
|
|
|
|
// Calculate the number of conflicts of each candidate.
|
|
|
|
for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
|
|
|
|
unsigned Reg = i->first->reg;
|
|
|
|
unsigned PhysReg = vrm_->getPhys(Reg);
|
|
|
|
if (!cur->overlapsFrom(*i->first, i->second))
|
|
|
|
continue;
|
|
|
|
for (unsigned j = 0; j < NumCands; ++j) {
|
|
|
|
unsigned Candidate = Candidates[j].first;
|
|
|
|
if (tri_->regsOverlap(PhysReg, Candidate)) {
|
|
|
|
if (NumCands > 1)
|
|
|
|
Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
|
|
|
|
SLIs[j].push_back(i->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
|
|
|
|
unsigned Reg = i->first->reg;
|
|
|
|
unsigned PhysReg = vrm_->getPhys(Reg);
|
|
|
|
if (!cur->overlapsFrom(*i->first, i->second-1))
|
|
|
|
continue;
|
|
|
|
for (unsigned j = 0; j < NumCands; ++j) {
|
|
|
|
unsigned Candidate = Candidates[j].first;
|
|
|
|
if (tri_->regsOverlap(PhysReg, Candidate)) {
|
|
|
|
if (NumCands > 1)
|
|
|
|
Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
|
|
|
|
SLIs[j].push_back(i->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Which is the best candidate?
|
|
|
|
unsigned BestCandidate = 0;
|
|
|
|
float MinConflicts = Conflicts[0];
|
|
|
|
for (unsigned i = 1; i != NumCands; ++i) {
|
|
|
|
if (Conflicts[i] < MinConflicts) {
|
|
|
|
BestCandidate = i;
|
|
|
|
MinConflicts = Conflicts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(),
|
|
|
|
std::back_inserter(SpillIntervals));
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct WeightCompare {
|
|
|
|
typedef std::pair<unsigned, float> RegWeightPair;
|
|
|
|
bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const {
|
|
|
|
return LHS.second < RHS.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool weightsAreClose(float w1, float w2) {
|
|
|
|
if (!NewHeuristic)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
float diff = w1 - w2;
|
|
|
|
if (diff <= 0.02f) // Within 0.02f
|
|
|
|
return true;
|
|
|
|
return (diff / w2) <= 0.05f; // Within 5%.
|
|
|
|
}
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
|
|
|
|
/// spill.
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur)
|
2003-11-20 03:32:25 +00:00
|
|
|
{
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\tallocating current interval: ";
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2008-04-03 16:40:27 +00:00
|
|
|
// This is an implicitly defined live interval, just assign any register.
|
|
|
|
const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
|
|
|
|
if (cur->empty()) {
|
|
|
|
unsigned physReg = cur->preference;
|
|
|
|
if (!physReg)
|
|
|
|
physReg = *RC->allocation_order_begin(*mf_);
|
|
|
|
DOUT << tri_->getName(physReg) << '\n';
|
|
|
|
// Note the register is not really in use.
|
|
|
|
vrm_->assignVirt2Phys(cur->reg, physReg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
PhysRegTracker backupPrt = *prt_;
|
|
|
|
|
2005-08-22 20:20:42 +00:00
|
|
|
std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
|
2004-11-18 04:13:02 +00:00
|
|
|
unsigned StartPosition = cur->beginNumber();
|
2005-08-23 22:27:31 +00:00
|
|
|
const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
|
2007-11-03 07:20:12 +00:00
|
|
|
|
|
|
|
// If this live interval is defined by a move instruction and its source is
|
|
|
|
// assigned a physical register that is compatible with the target register
|
|
|
|
// class, then we should try to assign it the same register.
|
|
|
|
// This can happen when the move is from a larger register class to a smaller
|
|
|
|
// one, e.g. X86::mov32to32_. These move instructions are not coalescable.
|
|
|
|
if (!cur->preference && cur->containsOneValue()) {
|
|
|
|
VNInfo *vni = cur->getValNumInfo(0);
|
|
|
|
if (vni->def && vni->def != ~1U && vni->def != ~0U) {
|
|
|
|
MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
|
|
|
|
unsigned SrcReg, DstReg;
|
2008-04-11 17:55:47 +00:00
|
|
|
if (CopyMI && tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) {
|
2007-11-03 07:20:12 +00:00
|
|
|
unsigned Reg = 0;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
|
2007-11-03 07:20:12 +00:00
|
|
|
Reg = SrcReg;
|
|
|
|
else if (vrm_->isAssignedReg(SrcReg))
|
|
|
|
Reg = vrm_->getPhys(SrcReg);
|
|
|
|
if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
|
|
|
|
cur->preference = Reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
// for every interval in inactive we overlap with, mark the
|
2005-08-22 20:20:42 +00:00
|
|
|
// register as not free and update spill weights.
|
2004-08-04 09:46:26 +00:00
|
|
|
for (IntervalPtrs::const_iterator i = inactive_.begin(),
|
|
|
|
e = inactive_.end(); i != e; ++i) {
|
2005-08-23 22:27:31 +00:00
|
|
|
unsigned Reg = i->first->reg;
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
|
2005-08-23 22:27:31 +00:00
|
|
|
"Can only allocate virtual registers!");
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RegRC = reginfo_->getRegClass(Reg);
|
2005-08-23 22:27:31 +00:00
|
|
|
// If this is not in a related reg class to the register we're allocating,
|
|
|
|
// don't check it.
|
|
|
|
if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
|
|
|
|
cur->overlapsFrom(*i->first, i->second-1)) {
|
|
|
|
Reg = vrm_->getPhys(Reg);
|
|
|
|
prt_->addRegUse(Reg);
|
|
|
|
SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
2005-08-22 20:59:30 +00:00
|
|
|
|
|
|
|
// Speculatively check to see if we can get a register right now. If not,
|
|
|
|
// we know we won't be able to by adding more constraints. If so, we can
|
|
|
|
// check to see if it is valid. Doing an exhaustive search of the fixed_ list
|
|
|
|
// is very bad (it contains all callee clobbered registers for any functions
|
|
|
|
// with a call), so we want to avoid doing that if possible.
|
|
|
|
unsigned physReg = getFreePhysReg(cur);
|
2008-03-11 07:19:34 +00:00
|
|
|
unsigned BestPhysReg = physReg;
|
2005-08-22 20:59:30 +00:00
|
|
|
if (physReg) {
|
|
|
|
// We got a register. However, if it's in the fixed_ list, we might
|
2005-08-30 21:03:36 +00:00
|
|
|
// conflict with it. Check to see if we conflict with it or any of its
|
|
|
|
// aliases.
|
2007-11-03 07:20:12 +00:00
|
|
|
SmallSet<unsigned, 8> RegAliases;
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS)
|
2005-08-30 21:03:36 +00:00
|
|
|
RegAliases.insert(*AS);
|
|
|
|
|
2005-08-22 20:59:30 +00:00
|
|
|
bool ConflictsWithFixed = false;
|
|
|
|
for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
|
2006-10-24 14:35:25 +00:00
|
|
|
IntervalPtr &IP = fixed_[i];
|
|
|
|
if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
|
2005-08-22 20:59:30 +00:00
|
|
|
// Okay, this reg is on the fixed list. Check to see if we actually
|
|
|
|
// conflict.
|
|
|
|
LiveInterval *I = IP.first;
|
|
|
|
if (I->endNumber() > StartPosition) {
|
|
|
|
LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
|
|
|
|
IP.second = II;
|
|
|
|
if (II != I->begin() && II->start > StartPosition)
|
|
|
|
--II;
|
2005-08-30 21:03:36 +00:00
|
|
|
if (cur->overlapsFrom(*I, II)) {
|
2005-08-22 20:59:30 +00:00
|
|
|
ConflictsWithFixed = true;
|
2005-08-30 21:03:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-08-22 20:59:30 +00:00
|
|
|
}
|
2004-11-18 04:33:31 +00:00
|
|
|
}
|
2004-02-06 18:08:18 +00:00
|
|
|
}
|
2005-08-22 20:59:30 +00:00
|
|
|
|
|
|
|
// Okay, the register picked by our speculative getFreePhysReg call turned
|
|
|
|
// out to be in use. Actually add all of the conflicting fixed registers to
|
|
|
|
// prt so we can do an accurate query.
|
|
|
|
if (ConflictsWithFixed) {
|
2005-08-23 22:27:31 +00:00
|
|
|
// For every interval in fixed we overlap with, mark the register as not
|
|
|
|
// free and update spill weights.
|
2005-08-22 20:59:30 +00:00
|
|
|
for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
|
|
|
|
IntervalPtr &IP = fixed_[i];
|
|
|
|
LiveInterval *I = IP.first;
|
2005-08-23 22:27:31 +00:00
|
|
|
|
|
|
|
const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
|
|
|
|
if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
|
|
|
|
I->endNumber() > StartPosition) {
|
2005-08-22 20:59:30 +00:00
|
|
|
LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
|
|
|
|
IP.second = II;
|
|
|
|
if (II != I->begin() && II->start > StartPosition)
|
|
|
|
--II;
|
|
|
|
if (cur->overlapsFrom(*I, II)) {
|
|
|
|
unsigned reg = I->reg;
|
|
|
|
prt_->addRegUse(reg);
|
|
|
|
SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2005-08-22 20:59:30 +00:00
|
|
|
// Using the newly updated prt_ object, which includes conflicts in the
|
|
|
|
// future, see if there are any registers available.
|
|
|
|
physReg = getFreePhysReg(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-22 20:20:42 +00:00
|
|
|
// Restore the physical register tracker, removing information about the
|
|
|
|
// future.
|
2004-08-04 09:46:26 +00:00
|
|
|
*prt_ = backupPrt;
|
2005-08-22 20:20:42 +00:00
|
|
|
|
2004-08-04 09:46:26 +00: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.
|
|
|
|
if (physReg) {
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << tri_->getName(physReg) << '\n';
|
2004-08-04 09:46:26 +00:00
|
|
|
vrm_->assignVirt2Phys(cur->reg, physReg);
|
|
|
|
prt_->addRegUse(physReg);
|
2004-11-18 02:42:27 +00:00
|
|
|
active_.push_back(std::make_pair(cur, cur->begin()));
|
2004-08-04 09:46:26 +00:00
|
|
|
handled_.push_back(cur);
|
|
|
|
return;
|
|
|
|
}
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "no free registers\n";
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2005-08-22 20:20:42 +00:00
|
|
|
// Compile the spill weights into an array that is better for scanning.
|
2008-06-20 21:45:16 +00:00
|
|
|
std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f);
|
2005-08-22 20:20:42 +00:00
|
|
|
for (std::vector<std::pair<unsigned, float> >::iterator
|
|
|
|
I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
|
2008-02-10 18:45:23 +00:00
|
|
|
updateSpillWeights(SpillWeights, I->first, I->second, tri_);
|
2005-08-22 20:20:42 +00:00
|
|
|
|
|
|
|
// for each interval in active, update spill weights.
|
|
|
|
for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
unsigned reg = i->first->reg;
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(reg) &&
|
2005-08-22 20:20:42 +00:00
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
reg = vrm_->getPhys(reg);
|
2008-02-10 18:45:23 +00:00
|
|
|
updateSpillWeights(SpillWeights, reg, i->first->weight, tri_);
|
2005-08-22 20:20:42 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2006-03-25 23:00:56 +00:00
|
|
|
// Find a register to spill.
|
2006-11-07 12:25:45 +00:00
|
|
|
float minWeight = HUGE_VALF;
|
2008-06-20 21:45:16 +00:00
|
|
|
unsigned minReg = 0; /*cur->preference*/; // Try the preferred register first.
|
|
|
|
|
|
|
|
bool Found = false;
|
|
|
|
std::vector<std::pair<unsigned,float> > RegsWeights;
|
2007-04-17 20:32:26 +00:00
|
|
|
if (!minReg || SpillWeights[minReg] == HUGE_VALF)
|
|
|
|
for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
|
|
|
|
e = RC->allocation_order_end(*mf_); i != e; ++i) {
|
|
|
|
unsigned reg = *i;
|
2008-06-20 21:45:16 +00:00
|
|
|
float regWeight = SpillWeights[reg];
|
|
|
|
if (minWeight > regWeight)
|
|
|
|
Found = true;
|
|
|
|
RegsWeights.push_back(std::make_pair(reg, regWeight));
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
2006-03-25 23:00:56 +00:00
|
|
|
|
|
|
|
// If we didn't find a register that is spillable, try aliases?
|
2008-06-20 21:45:16 +00:00
|
|
|
if (!Found) {
|
2006-05-12 19:07:46 +00:00
|
|
|
for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
|
|
|
|
e = RC->allocation_order_end(*mf_); i != e; ++i) {
|
|
|
|
unsigned reg = *i;
|
|
|
|
// No need to worry about if the alias register size < regsize of RC.
|
|
|
|
// We are going to spill all registers that alias it anyway.
|
2008-06-20 21:45:16 +00:00
|
|
|
for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as)
|
|
|
|
RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as]));
|
2006-05-12 19:07:46 +00:00
|
|
|
}
|
2008-06-20 21:45:16 +00:00
|
|
|
}
|
2006-05-12 19:07:46 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
// Sort all potential spill candidates by weight.
|
|
|
|
std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare());
|
|
|
|
minReg = RegsWeights[0].first;
|
|
|
|
minWeight = RegsWeights[0].second;
|
|
|
|
if (minWeight == HUGE_VALF) {
|
2006-05-12 19:07:46 +00:00
|
|
|
// All registers must have inf weight. Just grab one!
|
2008-06-20 21:45:16 +00:00
|
|
|
minReg = BestPhysReg ? BestPhysReg : *RC->allocation_order_begin(*mf_);
|
|
|
|
if (cur->weight == HUGE_VALF || cur->getSize() == 1)
|
|
|
|
// Spill a physical register around defs and uses.
|
|
|
|
li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_);
|
2006-05-12 19:07:46 +00:00
|
|
|
}
|
2008-06-20 21:45:16 +00:00
|
|
|
|
|
|
|
// Find up to 3 registers to consider as spill candidates.
|
|
|
|
unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1;
|
|
|
|
while (LastCandidate > 1) {
|
|
|
|
if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight))
|
|
|
|
break;
|
|
|
|
--LastCandidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOUT << "\t\tregister(s) with min weight(s): ";
|
|
|
|
DEBUG(for (unsigned i = 0; i != LastCandidate; ++i)
|
|
|
|
DOUT << tri_->getName(RegsWeights[i].first)
|
|
|
|
<< " (" << RegsWeights[i].second << ")\n");
|
2004-08-04 09:46:26 +00:00
|
|
|
|
|
|
|
// if the current has the minimum weight, we need to spill it and
|
|
|
|
// add any added intervals back to unhandled, and restart
|
|
|
|
// linearscan.
|
2006-11-07 12:25:45 +00:00
|
|
|
if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\t\tspilling(c): " << *cur << '\n';
|
2008-06-06 07:54:39 +00:00
|
|
|
float SSWeight;
|
2004-08-04 09:46:26 +00:00
|
|
|
std::vector<LiveInterval*> added =
|
2008-06-06 07:54:39 +00:00
|
|
|
li_->addIntervalsForSpills(*cur, loopInfo, *vrm_, SSWeight);
|
|
|
|
addStackInterval(cur, ls_, li_, SSWeight, *vrm_);
|
2004-08-04 09:46:26 +00: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.
|
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i)
|
|
|
|
unhandled_.push(added[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-18 03:49:30 +00:00
|
|
|
++NumBacktracks;
|
|
|
|
|
2004-08-04 09:46:26 +00: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
|
|
|
|
unhandled_.push(cur);
|
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
|
2004-08-04 09:46:26 +00:00
|
|
|
"did not choose a register to spill?");
|
2004-11-18 03:49:30 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
// 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
|
|
|
|
SmallVector<LiveInterval*, 8> spillIs;
|
|
|
|
|
|
|
|
// Determine which intervals have to be spilled.
|
|
|
|
findIntervalsToSpill(cur, RegsWeights, LastCandidate, spillIs);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
// Set of spilled vregs (used later to rollback properly)
|
|
|
|
SmallSet<unsigned, 8> spilled;
|
|
|
|
|
|
|
|
// The earliest start of a Spilled interval indicates up to where
|
2004-08-04 09:46:26 +00:00
|
|
|
// in handled we need to roll back
|
2004-11-18 01:29:39 +00:00
|
|
|
unsigned earliestStart = cur->beginNumber();
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
// Spill live intervals of virtual regs mapped to the physical register we
|
2004-11-18 03:49:30 +00:00
|
|
|
// want to clear (and its aliases). We only spill those that overlap with the
|
|
|
|
// current interval as the rest do not affect its allocation. we also keep
|
|
|
|
// track of the earliest start of all spilled live intervals since this will
|
|
|
|
// mark our rollback point.
|
2008-06-20 21:45:16 +00:00
|
|
|
std::vector<LiveInterval*> added;
|
|
|
|
while (!spillIs.empty()) {
|
|
|
|
LiveInterval *sli = spillIs.back();
|
|
|
|
spillIs.pop_back();
|
|
|
|
DOUT << "\t\t\tspilling(a): " << *sli << '\n';
|
|
|
|
earliestStart = std::min(earliestStart, sli->beginNumber());
|
|
|
|
float SSWeight;
|
|
|
|
std::vector<LiveInterval*> newIs =
|
|
|
|
li_->addIntervalsForSpills(*sli, loopInfo, *vrm_, SSWeight);
|
|
|
|
addStackInterval(sli, ls_, li_, SSWeight, *vrm_);
|
|
|
|
std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
|
|
|
|
spilled.insert(sli->reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\trolling back to: " << earliestStart << '\n';
|
2004-11-18 02:42:27 +00:00
|
|
|
|
|
|
|
// Scan handled in reverse order up to the earliest start of a
|
2004-08-04 09:46:26 +00:00
|
|
|
// spilled live interval and undo each one, restoring the state of
|
2004-11-18 02:42:27 +00:00
|
|
|
// unhandled.
|
2004-08-04 09:46:26 +00:00
|
|
|
while (!handled_.empty()) {
|
|
|
|
LiveInterval* i = handled_.back();
|
2004-11-18 02:42:27 +00:00
|
|
|
// If this interval starts before t we are done.
|
2004-11-18 01:29:39 +00:00
|
|
|
if (i->beginNumber() < earliestStart)
|
2004-08-04 09:46:26 +00:00
|
|
|
break;
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\t\tundo changes for: " << *i << '\n';
|
2004-08-04 09:46:26 +00:00
|
|
|
handled_.pop_back();
|
2004-11-18 02:42:27 +00:00
|
|
|
|
|
|
|
// When undoing a live interval allocation we must know if it is active or
|
|
|
|
// inactive to properly update the PhysRegTracker and the VirtRegMap.
|
2004-08-04 09:46:26 +00:00
|
|
|
IntervalPtrs::iterator it;
|
2004-11-18 02:42:27 +00:00
|
|
|
if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
|
2004-08-04 09:46:26 +00:00
|
|
|
active_.erase(it);
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
|
2006-02-23 06:44:17 +00:00
|
|
|
if (!spilled.count(i->reg))
|
2004-08-04 09:46:26 +00:00
|
|
|
unhandled_.push(i);
|
2006-02-23 06:44:17 +00:00
|
|
|
prt_->delRegUse(vrm_->getPhys(i->reg));
|
|
|
|
vrm_->clearVirt(i->reg);
|
2004-11-18 02:42:27 +00:00
|
|
|
} else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
|
2004-08-04 09:46:26 +00:00
|
|
|
inactive_.erase(it);
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
|
2006-02-23 06:44:17 +00:00
|
|
|
if (!spilled.count(i->reg))
|
2004-08-04 09:46:26 +00:00
|
|
|
unhandled_.push(i);
|
2006-02-23 06:44:17 +00:00
|
|
|
vrm_->clearVirt(i->reg);
|
2004-11-18 06:01:45 +00:00
|
|
|
} else {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(i->reg) &&
|
2004-11-18 06:01:45 +00:00
|
|
|
"Can only allocate virtual registers!");
|
|
|
|
vrm_->clearVirt(i->reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
unhandled_.push(i);
|
2004-02-15 10:24:21 +00:00
|
|
|
}
|
2007-11-04 08:32:21 +00:00
|
|
|
|
|
|
|
// It interval has a preference, it must be defined by a copy. Clear the
|
|
|
|
// preference now since the source interval allocation may have been undone
|
|
|
|
// as well.
|
|
|
|
i->preference = 0;
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
2004-11-18 03:49:30 +00:00
|
|
|
// Rewind the iterators in the active, inactive, and fixed lists back to the
|
|
|
|
// point we reverted to.
|
|
|
|
RevertVectorIteratorsTo(active_, earliestStart);
|
|
|
|
RevertVectorIteratorsTo(inactive_, earliestStart);
|
|
|
|
RevertVectorIteratorsTo(fixed_, earliestStart);
|
|
|
|
|
2004-08-04 09:46:26 +00: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)
|
2004-11-18 02:42:27 +00:00
|
|
|
for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
|
|
|
|
LiveInterval *HI = handled_[i];
|
|
|
|
if (!HI->expiredAt(earliestStart) &&
|
|
|
|
HI->expiredAt(cur->beginNumber())) {
|
2006-11-17 00:50:36 +00:00
|
|
|
DOUT << "\t\t\tundo changes for: " << *HI << '\n';
|
2004-11-18 02:42:27 +00:00
|
|
|
active_.push_back(std::make_pair(HI, HI->begin()));
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg));
|
2006-02-23 06:44:17 +00:00
|
|
|
prt_->addRegUse(vrm_->getPhys(HI->reg));
|
2004-02-06 18:08:18 +00:00
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
2004-05-30 07:24:39 +00:00
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
// merge added with unhandled
|
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i)
|
|
|
|
unhandled_.push(added[i]);
|
2004-02-15 10:24:21 +00:00
|
|
|
}
|
2003-12-21 05:43:40 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// getFreePhysReg - return a free physical register for this virtual register
|
|
|
|
/// interval if we have one, otherwise return 0.
|
2007-05-08 19:02:46 +00:00
|
|
|
unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
|
2008-02-26 22:08:41 +00:00
|
|
|
SmallVector<unsigned, 256> inactiveCounts;
|
2005-08-22 16:55:22 +00:00
|
|
|
unsigned MaxInactiveCount = 0;
|
|
|
|
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
|
2005-08-23 22:27:31 +00:00
|
|
|
const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
|
|
|
|
|
2004-09-02 21:23:32 +00:00
|
|
|
for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
|
|
|
|
i != e; ++i) {
|
2004-11-18 02:42:27 +00:00
|
|
|
unsigned reg = i->first->reg;
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(reg) &&
|
2004-11-18 06:01:45 +00:00
|
|
|
"Can only allocate virtual registers!");
|
2005-08-23 22:27:31 +00:00
|
|
|
|
|
|
|
// If this is not in a related reg class to the register we're allocating,
|
|
|
|
// don't check it.
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RegRC = reginfo_->getRegClass(reg);
|
2005-08-23 22:27:31 +00:00
|
|
|
if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
|
|
|
|
reg = vrm_->getPhys(reg);
|
2008-02-26 22:08:41 +00:00
|
|
|
if (inactiveCounts.size() <= reg)
|
|
|
|
inactiveCounts.resize(reg+1);
|
2005-08-23 22:27:31 +00:00
|
|
|
++inactiveCounts[reg];
|
|
|
|
MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
|
|
|
|
}
|
2004-09-02 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
2005-08-22 16:55:22 +00:00
|
|
|
unsigned FreeReg = 0;
|
|
|
|
unsigned FreeRegInactiveCount = 0;
|
2007-04-17 20:32:26 +00:00
|
|
|
|
|
|
|
// If copy coalescer has assigned a "preferred" register, check if it's
|
|
|
|
// available first.
|
2008-02-20 12:07:57 +00:00
|
|
|
if (cur->preference) {
|
2007-04-17 20:32:26 +00:00
|
|
|
if (prt_->isRegAvail(cur->preference)) {
|
|
|
|
DOUT << "\t\tassigned the preferred register: "
|
2008-02-26 21:47:57 +00:00
|
|
|
<< tri_->getName(cur->preference) << "\n";
|
2007-04-17 20:32:26 +00:00
|
|
|
return cur->preference;
|
|
|
|
} else
|
|
|
|
DOUT << "\t\tunable to assign the preferred register: "
|
2008-02-26 21:47:57 +00:00
|
|
|
<< tri_->getName(cur->preference) << "\n";
|
2008-02-20 12:07:57 +00:00
|
|
|
}
|
2007-04-17 20:32:26 +00:00
|
|
|
|
2005-08-22 16:55:22 +00:00
|
|
|
// Scan for the first available register.
|
2007-04-25 07:18:20 +00:00
|
|
|
TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_);
|
|
|
|
TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_);
|
2008-03-24 23:28:21 +00:00
|
|
|
assert(I != E && "No allocatable register in this register class!");
|
2005-08-22 16:55:22 +00:00
|
|
|
for (; I != E; ++I)
|
|
|
|
if (prt_->isRegAvail(*I)) {
|
|
|
|
FreeReg = *I;
|
2008-02-26 22:08:41 +00:00
|
|
|
if (FreeReg < inactiveCounts.size())
|
|
|
|
FreeRegInactiveCount = inactiveCounts[FreeReg];
|
|
|
|
else
|
|
|
|
FreeRegInactiveCount = 0;
|
2005-08-22 16:55:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-26 22:08:41 +00:00
|
|
|
|
2005-08-22 16:55:22 +00:00
|
|
|
// If there are no free regs, or if this reg has the max inactive count,
|
|
|
|
// return this register.
|
|
|
|
if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
|
|
|
|
|
|
|
|
// Continue scanning the registers, looking for the one with the highest
|
|
|
|
// inactive count. Alkis found that this reduced register pressure very
|
|
|
|
// slightly on X86 (in rev 1.94 of this file), though this should probably be
|
|
|
|
// reevaluated now.
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
unsigned Reg = *I;
|
2008-02-26 22:08:41 +00:00
|
|
|
if (prt_->isRegAvail(Reg) && Reg < inactiveCounts.size() &&
|
|
|
|
FreeRegInactiveCount < inactiveCounts[Reg]) {
|
2005-08-22 16:55:22 +00:00
|
|
|
FreeReg = Reg;
|
|
|
|
FreeRegInactiveCount = inactiveCounts[Reg];
|
|
|
|
if (FreeRegInactiveCount == MaxInactiveCount)
|
|
|
|
break; // We found the one with the max inactive count.
|
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
2005-08-22 16:55:22 +00:00
|
|
|
|
|
|
|
return FreeReg;
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass* llvm::createLinearScanRegisterAllocator() {
|
2007-05-08 19:02:46 +00:00
|
|
|
return new RALinScan();
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|