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"
|
2010-12-03 21:47:10 +00:00
|
|
|
#include "LiveDebugVariables.h"
|
2011-03-10 01:51:42 +00:00
|
|
|
#include "LiveRangeEdit.h"
|
2005-08-23 22:27:31 +00:00
|
|
|
#include "VirtRegMap.h"
|
2009-05-06 02:36:21 +00:00
|
|
|
#include "VirtRegRewriter.h"
|
2009-05-18 19:03:16 +00:00
|
|
|
#include "Spiller.h"
|
2010-11-10 23:55:56 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2003-11-20 03:32:25 +00:00
|
|
|
#include "llvm/Function.h"
|
2009-12-14 06:49:42 +00:00
|
|
|
#include "llvm/CodeGen/CalcSpillWeights.h"
|
2008-06-04 09:18:41 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.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"
|
2008-10-07 20:22:28 +00:00
|
|
|
#include "llvm/Target/TargetOptions.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"
|
2009-01-05 17:59:02 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2009-08-22 20:30:53 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.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>
|
2009-06-02 16:53:25 +00:00
|
|
|
|
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");
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
STATISTIC(NumDowngrade, "Number of registers downgraded");
|
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);
|
|
|
|
|
2008-10-23 20:43:13 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
PreSplitIntervals("pre-alloc-split",
|
|
|
|
cl::desc("Pre-register allocation live interval splitting"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2009-12-10 17:48:32 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
TrivCoalesceEnds("trivial-coalesce-ends",
|
|
|
|
cl::desc("Attempt trivial coalescing of interval ends"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2006-12-19 22:41:21 +00:00
|
|
|
static RegisterRegAlloc
|
2008-10-14 20:25:08 +00:00
|
|
|
linearscanRegAlloc("linearscan", "linear scan register allocator",
|
2006-12-19 22:41:21 +00:00
|
|
|
createLinearScanRegisterAllocator);
|
2006-08-01 14:21:23 +00:00
|
|
|
|
2006-12-19 22:41:21 +00:00
|
|
|
namespace {
|
2009-11-19 15:55:49 +00:00
|
|
|
// When we allocate a register, add it to a fixed-size queue of
|
|
|
|
// registers to skip in subsequent allocations. This trades a small
|
|
|
|
// amount of register pressure and increased spills for flexibility in
|
|
|
|
// the post-pass scheduler.
|
|
|
|
//
|
|
|
|
// Note that in a the number of registers used for reloading spills
|
|
|
|
// will be one greater than the value of this option.
|
|
|
|
//
|
|
|
|
// One big limitation of this is that it doesn't differentiate between
|
|
|
|
// different register classes. So on x86-64, if there is xmm register
|
|
|
|
// pressure, it can caused fewer GPRs to be held in the queue.
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
NumRecentlyUsedRegs("linearscan-skip-count",
|
2010-07-02 23:17:38 +00:00
|
|
|
cl::desc("Number of registers for linearscan to remember"
|
|
|
|
"to skip."),
|
2009-11-19 15:55:49 +00:00
|
|
|
cl::init(0),
|
|
|
|
cl::Hidden);
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2009-10-25 06:33:48 +00:00
|
|
|
struct RALinScan : public MachineFunctionPass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2010-08-06 18:33:48 +00:00
|
|
|
RALinScan() : MachineFunctionPass(ID) {
|
2010-12-03 21:47:10 +00:00
|
|
|
initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeRegisterCoalescerAnalysisGroup(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializePreAllocSplittingPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeLiveStacksPass(*PassRegistry::getPassRegistry());
|
2010-10-28 20:34:50 +00:00
|
|
|
initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
|
|
|
|
initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
|
|
|
|
|
2009-11-19 15:55:49 +00:00
|
|
|
// Initialize the queue to record recently-used registers.
|
|
|
|
if (NumRecentlyUsedRegs > 0)
|
|
|
|
RecentRegs.resize(NumRecentlyUsedRegs, 0);
|
2009-11-20 21:13:27 +00:00
|
|
|
RecentNext = RecentRegs.begin();
|
2009-11-19 15:55:49 +00:00
|
|
|
}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
|
2008-08-15 18:49:41 +00:00
|
|
|
typedef SmallVector<IntervalPtr, 32> IntervalPtrs;
|
2004-11-18 02:42:27 +00:00
|
|
|
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;
|
2008-08-13 23:36:23 +00:00
|
|
|
DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
|
2005-08-23 22:27:31 +00:00
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// NextReloadMap - For each register in the map, it maps to the another
|
|
|
|
// register which is defined by a reload from the same stack slot and
|
|
|
|
// both reloads are in the same basic block.
|
|
|
|
DenseMap<unsigned, unsigned> NextReloadMap;
|
|
|
|
|
|
|
|
// DowngradedRegs - A set of registers which are being "downgraded", i.e.
|
|
|
|
// un-favored for allocation.
|
|
|
|
SmallSet<unsigned, 8> DowngradedRegs;
|
|
|
|
|
|
|
|
// DowngradeMap - A map from virtual registers to physical registers being
|
|
|
|
// downgraded for the virtual registers.
|
|
|
|
DenseMap<unsigned, unsigned> DowngradeMap;
|
|
|
|
|
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_;
|
|
|
|
BitVector allocatableRegs_;
|
2010-09-01 21:04:27 +00:00
|
|
|
BitVector reservedRegs_;
|
2004-08-04 09:46:26 +00:00
|
|
|
LiveIntervals* li_;
|
2010-07-19 18:41:20 +00:00
|
|
|
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*,
|
2008-08-15 18:49:41 +00:00
|
|
|
SmallVector<LiveInterval*, 64>,
|
2004-08-04 09:46:26 +00:00
|
|
|
greater_ptr<LiveInterval> > IntervalHeap;
|
|
|
|
IntervalHeap unhandled_;
|
2009-05-01 01:03:49 +00:00
|
|
|
|
|
|
|
/// regUse_ - Tracks register usage.
|
|
|
|
SmallVector<unsigned, 32> regUse_;
|
|
|
|
SmallVector<unsigned, 32> regUseBackUp_;
|
|
|
|
|
|
|
|
/// vrm_ - Tracks register assignments.
|
2009-03-13 05:55:11 +00:00
|
|
|
VirtRegMap* vrm_;
|
2009-05-01 01:03:49 +00:00
|
|
|
|
2009-05-06 02:36:21 +00:00
|
|
|
std::auto_ptr<VirtRegRewriter> rewriter_;
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2009-05-18 19:03:16 +00:00
|
|
|
std::auto_ptr<Spiller> spiller_;
|
|
|
|
|
2009-11-19 15:55:49 +00:00
|
|
|
// The queue of recently-used registers.
|
2009-11-20 21:13:27 +00:00
|
|
|
SmallVector<unsigned, 4> RecentRegs;
|
|
|
|
SmallVector<unsigned, 4>::iterator RecentNext;
|
2009-11-19 15:55:49 +00:00
|
|
|
|
|
|
|
// Record that we just picked this register.
|
|
|
|
void recordRecentlyUsed(unsigned reg) {
|
|
|
|
assert(reg != 0 && "Recently used register is NOREG!");
|
|
|
|
if (!RecentRegs.empty()) {
|
2009-11-20 21:13:27 +00:00
|
|
|
*RecentNext++ = reg;
|
|
|
|
if (RecentNext == RecentRegs.end())
|
|
|
|
RecentNext = RecentRegs.begin();
|
2009-11-19 15:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
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 {
|
2009-07-31 23:37:33 +00:00
|
|
|
AU.setPreservesCFG();
|
2010-11-10 23:55:56 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2004-08-04 09:46:26 +00:00
|
|
|
AU.addRequired<LiveIntervals>();
|
2009-11-03 23:52:08 +00:00
|
|
|
AU.addPreserved<SlotIndexes>();
|
2008-10-07 20:22:28 +00:00
|
|
|
if (StrongPHIElim)
|
|
|
|
AU.addRequiredID(StrongPHIEliminationID);
|
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>();
|
2009-12-14 06:49:42 +00:00
|
|
|
AU.addRequired<CalculateSpillWeights>();
|
2008-10-23 20:43:13 +00:00
|
|
|
if (PreSplitIntervals)
|
|
|
|
AU.addRequiredID(PreAllocSplittingID);
|
2010-10-26 00:11:33 +00:00
|
|
|
AU.addRequiredID(LiveStacksID);
|
|
|
|
AU.addPreservedID(LiveStacksID);
|
2007-12-11 02:09:15 +00:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2008-01-04 20:54:55 +00:00
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
2009-03-13 05:55:11 +00:00
|
|
|
AU.addRequired<VirtRegMap>();
|
|
|
|
AU.addPreserved<VirtRegMap>();
|
2010-12-03 21:47:10 +00:00
|
|
|
AU.addRequired<LiveDebugVariables>();
|
|
|
|
AU.addPreserved<LiveDebugVariables>();
|
2010-10-28 20:34:50 +00:00
|
|
|
AU.addRequiredID(MachineDominatorsID);
|
2008-01-04 20:54:55 +00:00
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2004-08-04 09:46:26 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
|
|
|
|
2009-11-19 15:55:49 +00:00
|
|
|
// Determine if we skip this register due to its being recently used.
|
|
|
|
bool isRecentlyUsed(unsigned reg) const {
|
|
|
|
return std::find(RecentRegs.begin(), RecentRegs.end(), reg) !=
|
|
|
|
RecentRegs.end();
|
|
|
|
}
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
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.
|
2009-11-03 23:52:08 +00:00
|
|
|
void processActiveIntervals(SlotIndex 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.
|
2009-11-03 23:52:08 +00:00
|
|
|
void processInactiveIntervals(SlotIndex CurPoint);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
/// hasNextReloadInterval - Return the next liveinterval that's being
|
|
|
|
/// defined by a reload from the same SS as the specified one.
|
|
|
|
LiveInterval *hasNextReloadInterval(LiveInterval *cur);
|
|
|
|
|
|
|
|
/// DowngradeRegister - Downgrade a register for allocation.
|
|
|
|
void DowngradeRegister(LiveInterval *li, unsigned Reg);
|
|
|
|
|
|
|
|
/// UpgradeRegister - Upgrade a register for allocation.
|
|
|
|
void UpgradeRegister(unsigned Reg);
|
|
|
|
|
2004-08-04 09:46:26 +00:00
|
|
|
/// assignRegOrStackSlotAtInterval - assign a register if one
|
|
|
|
/// is available, or spill.
|
|
|
|
void assignRegOrStackSlotAtInterval(LiveInterval* cur);
|
|
|
|
|
2009-03-23 22:57:19 +00:00
|
|
|
void updateSpillWeights(std::vector<float> &Weights,
|
|
|
|
unsigned reg, float weight,
|
|
|
|
const TargetRegisterClass *RC);
|
|
|
|
|
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,
|
2010-07-27 18:36:27 +00:00
|
|
|
/// try to allocate the definition to the same register as the source,
|
|
|
|
/// if the register is not defined during the life time of the interval.
|
|
|
|
/// This eliminates a copy, and is used to coalesce copies which were not
|
2007-11-03 07:20:12 +00:00
|
|
|
/// 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
|
|
|
///
|
2009-05-01 01:03:49 +00:00
|
|
|
/// Register usage / availability tracking helpers.
|
|
|
|
///
|
|
|
|
|
|
|
|
void initRegUses() {
|
|
|
|
regUse_.resize(tri_->getNumRegs(), 0);
|
|
|
|
regUseBackUp_.resize(tri_->getNumRegs(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalizeRegUses() {
|
2009-05-03 18:32:42 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Verify all the registers are "freed".
|
|
|
|
bool Error = false;
|
|
|
|
for (unsigned i = 0, e = tri_->getNumRegs(); i != e; ++i) {
|
|
|
|
if (regUse_[i] != 0) {
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << tri_->getName(i) << " is still in use!\n";
|
2009-05-03 18:32:42 +00:00
|
|
|
Error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Error)
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable(0);
|
2009-05-03 18:32:42 +00:00
|
|
|
#endif
|
2009-05-01 01:03:49 +00:00
|
|
|
regUse_.clear();
|
|
|
|
regUseBackUp_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void addRegUse(unsigned physReg) {
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
|
|
|
|
"should be physical register!");
|
|
|
|
++regUse_[physReg];
|
|
|
|
for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as)
|
|
|
|
++regUse_[*as];
|
|
|
|
}
|
|
|
|
|
|
|
|
void delRegUse(unsigned physReg) {
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
|
|
|
|
"should be physical register!");
|
|
|
|
assert(regUse_[physReg] != 0);
|
|
|
|
--regUse_[physReg];
|
|
|
|
for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) {
|
|
|
|
assert(regUse_[*as] != 0);
|
|
|
|
--regUse_[*as];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isRegAvail(unsigned physReg) const {
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
|
|
|
|
"should be physical register!");
|
|
|
|
return regUse_[physReg] == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void backUpRegUses() {
|
|
|
|
regUseBackUp_ = regUse_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void restoreRegUses() {
|
|
|
|
regUse_ = regUseBackUp_;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Register handling helpers.
|
2004-08-04 09:46:26 +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.
|
2004-08-04 09:46:26 +00:00
|
|
|
unsigned getFreePhysReg(LiveInterval* cur);
|
2009-06-15 08:28:29 +00:00
|
|
|
unsigned getFreePhysReg(LiveInterval* cur,
|
|
|
|
const TargetRegisterClass *RC,
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
unsigned MaxInactiveCount,
|
|
|
|
SmallVector<unsigned, 256> &inactiveCounts,
|
|
|
|
bool SkipDGRegs);
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2010-09-01 21:34:41 +00:00
|
|
|
/// getFirstNonReservedPhysReg - return the first non-reserved physical
|
|
|
|
/// register in the register class.
|
|
|
|
unsigned getFirstNonReservedPhysReg(const TargetRegisterClass *RC) {
|
|
|
|
TargetRegisterClass::iterator aoe = RC->allocation_order_end(*mf_);
|
|
|
|
TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_);
|
|
|
|
while (i != aoe && reservedRegs_.test(*i))
|
|
|
|
++i;
|
|
|
|
assert(i != aoe && "All registers reserved?!");
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
|
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 {
|
2009-08-22 20:30:53 +00:00
|
|
|
DEBUG({
|
|
|
|
if (str)
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << str << " intervals:\n";
|
2009-08-22 20:30:53 +00:00
|
|
|
|
|
|
|
for (; i != e; ++i) {
|
2011-03-25 06:04:26 +00:00
|
|
|
dbgs() << '\t' << *i->first << " -> ";
|
2009-08-22 20:30:53 +00:00
|
|
|
|
|
|
|
unsigned reg = i->first->reg;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(reg))
|
|
|
|
reg = vrm_->getPhys(reg);
|
|
|
|
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << tri_->getName(reg) << '\n';
|
2009-08-22 20:30:53 +00:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(RALinScan, "linearscan-regalloc",
|
2011-03-25 06:04:26 +00:00
|
|
|
"Linear Scan Register Allocator", false, false)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(PreAllocSplitting)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveStacks)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(RegisterCoalescer)
|
2010-11-10 23:55:56 +00:00
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_END(RALinScan, "linearscan-regalloc",
|
2011-03-25 06:04:26 +00:00
|
|
|
"Linear Scan Register Allocator", false, false)
|
2008-06-04 09:18:41 +00:00
|
|
|
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALinScan::ComputeRelatedRegClasses() {
|
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;
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +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) {
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
HasAliases = HasAliases || *tri_->getAliasSet(*I) != 0;
|
2010-09-01 21:48:06 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2005-08-23 22:27:31 +00:00
|
|
|
// 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)
|
2008-08-13 23:36:23 +00:00
|
|
|
for (DenseMap<unsigned, const TargetRegisterClass*>::iterator
|
2005-08-23 22:27:31 +00:00
|
|
|
I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
|
|
|
|
I != E; ++I)
|
2011-01-27 07:26:15 +00:00
|
|
|
for (const unsigned *AS = tri_->getAliasSet(I->first); *AS; ++AS) {
|
|
|
|
const TargetRegisterClass *AliasClass =
|
|
|
|
OneClassForEachPhysReg.lookup(*AS);
|
|
|
|
if (AliasClass)
|
|
|
|
RelatedRegClasses.unionSets(I->second, AliasClass);
|
|
|
|
}
|
2005-08-23 22:27:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-10 17:48:32 +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. If the interval is
|
|
|
|
/// killed by a copy, try to use the destination register. This eliminates 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.
|
2007-11-03 07:20:12 +00:00
|
|
|
unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
|
2009-06-14 20:22:55 +00:00
|
|
|
unsigned Preference = vrm_->getRegAllocPref(cur.reg);
|
|
|
|
if ((Preference && Preference == Reg) || !cur.containsOneValue())
|
2007-11-03 07:20:12 +00:00
|
|
|
return Reg;
|
|
|
|
|
2009-12-10 17:48:32 +00:00
|
|
|
// We cannot handle complicated live ranges. Simple linear stuff only.
|
|
|
|
if (cur.ranges.size() != 1)
|
2007-11-03 07:20:12 +00:00
|
|
|
return Reg;
|
2009-12-10 17:48:32 +00:00
|
|
|
|
|
|
|
const LiveRange &range = cur.ranges.front();
|
|
|
|
|
|
|
|
VNInfo *vni = range.valno;
|
2011-03-03 05:18:19 +00:00
|
|
|
if (vni->isUnused() || !vni->def.isValid())
|
2009-12-05 07:30:23 +00:00
|
|
|
return Reg;
|
2009-12-10 17:48:32 +00:00
|
|
|
|
|
|
|
unsigned CandReg;
|
|
|
|
{
|
|
|
|
MachineInstr *CopyMI;
|
2010-09-25 12:04:16 +00:00
|
|
|
if ((CopyMI = li_->getInstructionFromIndex(vni->def)) && CopyMI->isCopy())
|
2009-12-10 17:48:32 +00:00
|
|
|
// Defined by a copy, try to extend SrcReg forward
|
2010-07-16 04:45:42 +00:00
|
|
|
CandReg = CopyMI->getOperand(1).getReg();
|
2009-12-10 17:48:32 +00:00
|
|
|
else if (TrivCoalesceEnds &&
|
2010-07-16 04:45:42 +00:00
|
|
|
(CopyMI = li_->getInstructionFromIndex(range.end.getBaseIndex())) &&
|
|
|
|
CopyMI->isCopy() && cur.reg == CopyMI->getOperand(1).getReg())
|
2009-12-10 17:48:32 +00:00
|
|
|
// Only used by a copy, try to extend DstReg backwards
|
2010-07-16 04:45:42 +00:00
|
|
|
CandReg = CopyMI->getOperand(0).getReg();
|
2009-12-10 17:48:32 +00:00
|
|
|
else
|
2007-11-03 07:20:12 +00:00
|
|
|
return Reg;
|
2010-11-19 05:45:24 +00:00
|
|
|
|
|
|
|
// If the target of the copy is a sub-register then don't coalesce.
|
|
|
|
if(CopyMI->getOperand(0).getSubReg())
|
|
|
|
return Reg;
|
2008-02-20 12:07:57 +00:00
|
|
|
}
|
2009-12-10 17:48:32 +00:00
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(CandReg)) {
|
|
|
|
if (!vrm_->isAssignedReg(CandReg))
|
|
|
|
return Reg;
|
|
|
|
CandReg = vrm_->getPhys(CandReg);
|
|
|
|
}
|
|
|
|
if (Reg == CandReg)
|
2007-11-03 07:20:12 +00:00
|
|
|
return Reg;
|
|
|
|
|
2008-09-18 22:38:47 +00:00
|
|
|
const TargetRegisterClass *RC = mri_->getRegClass(cur.reg);
|
2009-12-10 17:48:32 +00:00
|
|
|
if (!RC->contains(CandReg))
|
2007-11-03 07:20:12 +00:00
|
|
|
return Reg;
|
|
|
|
|
2009-12-10 17:48:32 +00:00
|
|
|
if (li_->conflictsWithPhysReg(cur, *vrm_, CandReg))
|
|
|
|
return Reg;
|
2009-12-05 07:30:23 +00:00
|
|
|
|
2009-12-10 17:48:32 +00:00
|
|
|
// Try to coalesce.
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "Coalescing: " << cur << " -> " << tri_->getName(CandReg)
|
2009-12-10 17:48:32 +00:00
|
|
|
<< '\n');
|
|
|
|
vrm_->clearVirt(cur.reg);
|
|
|
|
vrm_->assignVirt2Phys(cur.reg, CandReg);
|
2009-12-04 00:16:04 +00:00
|
|
|
|
2009-12-10 17:48:32 +00:00
|
|
|
++NumCoalesce;
|
|
|
|
return CandReg;
|
2007-11-03 07:20:12 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2008-02-10 18:45:23 +00:00
|
|
|
allocatableRegs_ = tri_->getAllocatableSet(fn);
|
2010-09-01 21:04:27 +00:00
|
|
|
reservedRegs_ = tri_->getReservedRegs(fn);
|
2004-08-04 09:46:26 +00:00
|
|
|
li_ = &getAnalysis<LiveIntervals>();
|
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();
|
2009-05-01 01:03:49 +00:00
|
|
|
|
|
|
|
// Also resize register usage trackers.
|
|
|
|
initRegUses();
|
|
|
|
|
2009-03-13 05:55:11 +00:00
|
|
|
vrm_ = &getAnalysis<VirtRegMap>();
|
2009-05-06 02:36:21 +00:00
|
|
|
if (!rewriter_.get()) rewriter_.reset(createVirtRegRewriter());
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2010-07-20 23:50:15 +00:00
|
|
|
spiller_.reset(createSpiller(*this, *mf_, *vrm_));
|
2010-09-01 21:48:06 +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.
|
2009-05-06 02:36:21 +00:00
|
|
|
rewriter_->runOnMachineFunction(*mf_, *vrm_, li_);
|
2004-11-18 02:42:27 +00:00
|
|
|
|
2010-12-03 21:47:10 +00:00
|
|
|
// Write out new DBG_VALUE instructions.
|
|
|
|
getAnalysis<LiveDebugVariables>().emitDebugValues(vrm_);
|
|
|
|
|
2008-06-23 23:51:16 +00:00
|
|
|
assert(unhandled_.empty() && "Unhandled live intervals remain!");
|
2009-05-01 01:03:49 +00:00
|
|
|
|
|
|
|
finalizeRegUses();
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
fixed_.clear();
|
|
|
|
active_.clear();
|
|
|
|
inactive_.clear();
|
|
|
|
handled_.clear();
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
NextReloadMap.clear();
|
|
|
|
DowngradedRegs.clear();
|
|
|
|
DowngradeMap.clear();
|
2009-06-02 16:53:25 +00:00
|
|
|
spiller_.reset(0);
|
2004-11-18 02:42:27 +00:00
|
|
|
|
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");
|
|
|
|
|
2008-08-15 18:49:41 +00:00
|
|
|
handled_.reserve(li_->getNumIntervals());
|
|
|
|
|
2004-11-18 06:01:45 +00:00
|
|
|
for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
|
2008-08-13 21:49:13 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) {
|
2009-11-03 23:52:08 +00:00
|
|
|
if (!i->second->empty()) {
|
|
|
|
mri_->setPhysRegUsed(i->second->reg);
|
|
|
|
fixed_.push_back(std::make_pair(i->second, i->second->begin()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i->second->empty()) {
|
|
|
|
assignRegOrStackSlotAtInterval(i->second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
unhandled_.push(i->second);
|
|
|
|
}
|
2004-11-18 06:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-22 20:30:53 +00:00
|
|
|
void RALinScan::linearScan() {
|
2004-08-04 09:46:26 +00:00
|
|
|
// linear scan algorithm
|
2009-08-22 20:30:53 +00:00
|
|
|
DEBUG({
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << "********** LINEAR SCAN **********\n"
|
2010-09-01 21:48:06 +00:00
|
|
|
<< "********** Function: "
|
2009-08-22 20:30:53 +00:00
|
|
|
<< mf_->getFunction()->getName() << '\n';
|
|
|
|
printIntervals("fixed", fixed_.begin(), fixed_.end());
|
|
|
|
});
|
2004-08-04 09:46:26 +00:00
|
|
|
|
|
|
|
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;
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\n*** CURRENT ***: " << *cur << '\n');
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
assert(!cur->empty() && "Empty interval in unhandled set.");
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
processActiveIntervals(cur->beginIndex());
|
|
|
|
processInactiveIntervals(cur->beginIndex());
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-08-22 20:30:53 +00:00
|
|
|
DEBUG({
|
|
|
|
printIntervals("active", active_.begin(), active_.end());
|
|
|
|
printIntervals("inactive", inactive_.begin(), inactive_.end());
|
|
|
|
});
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 01:03:49 +00:00
|
|
|
// 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;
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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);
|
2009-05-01 01:03:49 +00:00
|
|
|
delRegUse(reg);
|
2007-10-16 21:09:14 +00:00
|
|
|
active_.pop_back();
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 01:03:49 +00:00
|
|
|
// Expire any remaining inactive intervals
|
2009-08-22 20:30:53 +00:00
|
|
|
DEBUG({
|
|
|
|
for (IntervalPtrs::reverse_iterator
|
|
|
|
i = inactive_.rbegin(); i != inactive_.rend(); ++i)
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << "\tinterval " << *i->first << " expired\n";
|
2009-08-22 20:30:53 +00:00
|
|
|
});
|
2007-10-16 21:09:14 +00:00
|
|
|
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) {
|
2008-08-13 21:49:13 +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)
|
2008-08-13 21:49:13 +00:00
|
|
|
Reg = cur.reg;
|
2007-10-17 02:12:22 +00:00
|
|
|
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;
|
2009-06-04 20:28:22 +00:00
|
|
|
|
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;
|
2008-10-29 05:06:14 +00:00
|
|
|
if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) {
|
2007-10-17 02:12:22 +00:00
|
|
|
for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
|
2009-06-04 20:53:36 +00:00
|
|
|
if (LiveInMBBs[i] != EntryMBB) {
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
|
|
|
|
"Adding a virtual register to livein set?");
|
2007-10-17 02:12:22 +00:00
|
|
|
LiveInMBBs[i]->addLiveIn(Reg);
|
2009-06-04 20:53:36 +00:00
|
|
|
}
|
2007-10-17 06:53:44 +00:00
|
|
|
LiveInMBBs.clear();
|
2007-02-16 09:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << *vrm_);
|
2009-05-03 18:32:42 +00:00
|
|
|
|
|
|
|
// Look for physical registers that end up not being allocated even though
|
|
|
|
// register allocator had to spill other registers in its register class.
|
2009-06-14 20:22:55 +00:00
|
|
|
if (!vrm_->FindUnusedRegisters(li_))
|
2009-05-03 18:32:42 +00:00
|
|
|
return;
|
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.
|
2009-11-03 23:52:08 +00:00
|
|
|
void RALinScan::processActiveIntervals(SlotIndex CurPoint)
|
2003-11-20 03:32:25 +00:00
|
|
|
{
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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.
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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);
|
2009-05-01 01:03:49 +00:00
|
|
|
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.
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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);
|
2009-05-01 01:03:49 +00:00
|
|
|
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.
|
2009-11-03 23:52:08 +00:00
|
|
|
void RALinScan::processInactiveIntervals(SlotIndex CurPoint)
|
2003-11-20 03:32:25 +00:00
|
|
|
{
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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.
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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);
|
2009-05-01 01:03:49 +00:00
|
|
|
addRegUse(reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
// 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.
|
2009-03-23 22:57:19 +00:00
|
|
|
void RALinScan::updateSpillWeights(std::vector<float> &Weights,
|
|
|
|
unsigned reg, float weight,
|
|
|
|
const TargetRegisterClass *RC) {
|
|
|
|
SmallSet<unsigned, 4> Processed;
|
|
|
|
SmallSet<unsigned, 4> SuperAdded;
|
|
|
|
SmallVector<unsigned, 4> Supers;
|
2004-11-18 06:01:45 +00:00
|
|
|
Weights[reg] += weight;
|
2009-03-23 22:57:19 +00:00
|
|
|
Processed.insert(reg);
|
|
|
|
for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) {
|
2004-11-18 06:01:45 +00:00
|
|
|
Weights[*as] += weight;
|
2009-03-23 22:57:19 +00:00
|
|
|
Processed.insert(*as);
|
|
|
|
if (tri_->isSubRegister(*as, reg) &&
|
|
|
|
SuperAdded.insert(*as) &&
|
|
|
|
RC->contains(*as)) {
|
|
|
|
Supers.push_back(*as);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the alias is a super-register, and the super-register is in the
|
|
|
|
// register class we are trying to allocate. Then add the weight to all
|
|
|
|
// sub-registers of the super-register even if they are not aliases.
|
|
|
|
// e.g. allocating for GR32, bh is not used, updating bl spill weight.
|
|
|
|
// bl should get the same spill weight otherwise it will be choosen
|
|
|
|
// as a spill candidate since spilling bh doesn't make ebx available.
|
|
|
|
for (unsigned i = 0, e = Supers.size(); i != e; ++i) {
|
2009-05-03 18:32:42 +00:00
|
|
|
for (const unsigned *sr = tri_->getSubRegisters(Supers[i]); *sr; ++sr)
|
|
|
|
if (!Processed.count(*sr))
|
|
|
|
Weights[*sr] += weight;
|
2009-03-23 22:57:19 +00:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2010-09-01 21:48:06 +00:00
|
|
|
static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V,
|
|
|
|
SlotIndex 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-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.
|
2009-05-03 18:32:42 +00:00
|
|
|
static
|
|
|
|
float getConflictWeight(LiveInterval *cur, unsigned Reg, LiveIntervals *li_,
|
|
|
|
MachineRegisterInfo *mri_,
|
2010-07-19 18:41:20 +00:00
|
|
|
MachineLoopInfo *loopInfo) {
|
2008-06-20 21:45:16 +00:00
|
|
|
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());
|
2010-05-15 17:10:24 +00:00
|
|
|
Conflicts += std::pow(10.0f, (float)loopDepth);
|
2008-06-20 21:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
|
2009-08-22 20:30:53 +00:00
|
|
|
DEBUG({
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << "\tConsidering " << NumCands << " candidates: ";
|
2009-08-22 20:30:53 +00:00
|
|
|
for (unsigned i = 0; i != NumCands; ++i)
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << tri_->getName(Candidates[i].first) << " ";
|
|
|
|
dbgs() << "\n";
|
2009-08-22 20:30:53 +00:00
|
|
|
});
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
// 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 {
|
2009-11-19 15:55:49 +00:00
|
|
|
private:
|
|
|
|
const RALinScan &Allocator;
|
|
|
|
|
|
|
|
public:
|
2009-12-19 07:05:23 +00:00
|
|
|
WeightCompare(const RALinScan &Alloc) : Allocator(Alloc) {}
|
2009-11-19 15:55:49 +00:00
|
|
|
|
2008-06-20 21:45:16 +00:00
|
|
|
typedef std::pair<unsigned, float> RegWeightPair;
|
|
|
|
bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const {
|
2009-11-19 15:55:49 +00:00
|
|
|
return LHS.second < RHS.second && !Allocator.isRecentlyUsed(LHS.first);
|
2008-06-20 21:45:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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%.
|
|
|
|
}
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
LiveInterval *RALinScan::hasNextReloadInterval(LiveInterval *cur) {
|
|
|
|
DenseMap<unsigned, unsigned>::iterator I = NextReloadMap.find(cur->reg);
|
|
|
|
if (I == NextReloadMap.end())
|
|
|
|
return 0;
|
|
|
|
return &li_->getInterval(I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RALinScan::DowngradeRegister(LiveInterval *li, unsigned Reg) {
|
2011-01-06 01:33:22 +00:00
|
|
|
for (const unsigned *AS = tri_->getOverlaps(Reg); *AS; ++AS) {
|
|
|
|
bool isNew = DowngradedRegs.insert(*AS);
|
|
|
|
(void)isNew; // Silence compiler warning.
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
assert(isNew && "Multiple reloads holding the same register?");
|
|
|
|
DowngradeMap.insert(std::make_pair(li->reg, *AS));
|
|
|
|
}
|
|
|
|
++NumDowngrade;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RALinScan::UpgradeRegister(unsigned Reg) {
|
|
|
|
if (Reg) {
|
|
|
|
DowngradedRegs.erase(Reg);
|
|
|
|
for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS)
|
|
|
|
DowngradedRegs.erase(*AS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct LISorter {
|
|
|
|
bool operator()(LiveInterval* A, LiveInterval* B) {
|
2009-09-04 20:41:11 +00:00
|
|
|
return A->beginIndex() < B->beginIndex();
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2004-11-18 02:42:27 +00:00
|
|
|
/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
|
|
|
|
/// spill.
|
2009-08-22 20:30:53 +00:00
|
|
|
void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) {
|
2010-11-16 19:55:12 +00:00
|
|
|
const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
|
|
|
|
DEBUG(dbgs() << "\tallocating current interval from "
|
|
|
|
<< RC->getName() << ": ");
|
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.
|
|
|
|
if (cur->empty()) {
|
2009-06-14 20:22:55 +00:00
|
|
|
unsigned physReg = vrm_->getRegAllocPref(cur->reg);
|
2010-09-01 21:34:41 +00:00
|
|
|
if (!physReg)
|
|
|
|
physReg = getFirstNonReservedPhysReg(RC);
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << tri_->getName(physReg) << '\n');
|
2008-04-03 16:40:27 +00:00
|
|
|
// Note the register is not really in use.
|
|
|
|
vrm_->assignVirt2Phys(cur->reg, physReg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-01 01:03:49 +00:00
|
|
|
backUpRegUses();
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2005-08-22 20:20:42 +00:00
|
|
|
std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
|
2009-11-03 23:52:08 +00:00
|
|
|
SlotIndex StartPosition = cur->beginIndex();
|
2005-08-23 22:27:31 +00:00
|
|
|
const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
|
2007-11-03 07:20:12 +00:00
|
|
|
|
2009-01-20 00:16:18 +00:00
|
|
|
// If start of 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.
|
2007-11-03 07:20:12 +00:00
|
|
|
// 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.
|
2009-06-14 20:22:55 +00:00
|
|
|
if (!vrm_->getRegAllocPref(cur->reg) && cur->hasAtLeastOneValue()) {
|
2009-01-20 00:16:18 +00:00
|
|
|
VNInfo *vni = cur->begin()->valno;
|
2011-03-03 05:18:19 +00:00
|
|
|
if (!vni->isUnused() && vni->def.isValid()) {
|
2007-11-03 07:20:12 +00:00
|
|
|
MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
|
2010-07-16 04:45:42 +00:00
|
|
|
if (CopyMI && CopyMI->isCopy()) {
|
|
|
|
unsigned DstSubReg = CopyMI->getOperand(0).getSubReg();
|
|
|
|
unsigned SrcReg = CopyMI->getOperand(1).getReg();
|
|
|
|
unsigned SrcSubReg = CopyMI->getOperand(1).getSubReg();
|
2010-07-08 16:40:22 +00:00
|
|
|
unsigned Reg = 0;
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
|
|
|
|
Reg = SrcReg;
|
|
|
|
else if (vrm_->isAssignedReg(SrcReg))
|
|
|
|
Reg = vrm_->getPhys(SrcReg);
|
|
|
|
if (Reg) {
|
|
|
|
if (SrcSubReg)
|
|
|
|
Reg = tri_->getSubReg(Reg, SrcSubReg);
|
|
|
|
if (DstSubReg)
|
|
|
|
Reg = tri_->getMatchingSuperReg(Reg, DstSubReg, RC);
|
2009-04-29 00:42:27 +00:00
|
|
|
if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
|
2009-06-15 08:28:29 +00:00
|
|
|
mri_->setRegAllocationHint(cur->reg, 0, Reg);
|
2009-04-29 00:42:27 +00:00
|
|
|
}
|
2007-11-03 07:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-01 01:03:49 +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!");
|
2008-09-18 22:38:47 +00:00
|
|
|
const TargetRegisterClass *RegRC = mri_->getRegClass(Reg);
|
2010-09-01 21:48:06 +00:00
|
|
|
// If this is not in a related reg class to the register we're allocating,
|
2005-08-23 22:27:31 +00:00
|
|
|
// don't check it.
|
|
|
|
if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
|
|
|
|
cur->overlapsFrom(*i->first, i->second-1)) {
|
|
|
|
Reg = vrm_->getPhys(Reg);
|
2009-05-01 01:03:49 +00:00
|
|
|
addRegUse(Reg);
|
2005-08-23 22:27:31 +00:00
|
|
|
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
|
|
|
}
|
2010-09-01 21:48:06 +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);
|
2010-09-01 21:48:06 +00:00
|
|
|
|
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;
|
2009-09-04 20:41:11 +00:00
|
|
|
if (I->endIndex() > 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;
|
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
|
|
|
}
|
2010-09-01 21:48:06 +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
|
2009-05-01 01:03:49 +00:00
|
|
|
// regUse_ so we can do an accurate query.
|
2005-08-22 20:59:30 +00:00
|
|
|
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];
|
2010-09-01 21:48:06 +00:00
|
|
|
if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
|
2009-09-04 20:41:11 +00:00
|
|
|
I->endIndex() > 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;
|
2009-05-01 01:03:49 +00:00
|
|
|
addRegUse(reg);
|
2005-08-22 20:59:30 +00:00
|
|
|
SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
|
2009-05-01 01:03:49 +00:00
|
|
|
// Using the newly updated regUse_ object, which includes conflicts in the
|
2005-08-22 20:59:30 +00:00
|
|
|
// future, see if there are any registers available.
|
|
|
|
physReg = getFreePhysReg(cur);
|
|
|
|
}
|
|
|
|
}
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2005-08-22 20:20:42 +00:00
|
|
|
// Restore the physical register tracker, removing information about the
|
|
|
|
// future.
|
2009-05-01 01:03:49 +00:00
|
|
|
restoreRegUses();
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2009-05-01 01:03:49 +00:00
|
|
|
// If we find a free register, we are done: assign this virtual to
|
2004-08-04 09:46:26 +00:00
|
|
|
// the free physical register and add this interval to the active
|
|
|
|
// list.
|
|
|
|
if (physReg) {
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << tri_->getName(physReg) << '\n');
|
2011-03-25 01:48:18 +00:00
|
|
|
assert(RC->contains(physReg) && "Invalid candidate");
|
2004-08-04 09:46:26 +00:00
|
|
|
vrm_->assignVirt2Phys(cur->reg, physReg);
|
2009-05-01 01:03:49 +00:00
|
|
|
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);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
|
|
|
|
// "Upgrade" the physical register since it has been allocated.
|
|
|
|
UpgradeRegister(physReg);
|
|
|
|
if (LiveInterval *NextReloadLI = hasNextReloadInterval(cur)) {
|
|
|
|
// "Downgrade" physReg to try to keep physReg from being allocated until
|
2010-09-01 21:48:06 +00:00
|
|
|
// the next reload from the same SS is allocated.
|
2009-06-15 08:28:29 +00:00
|
|
|
mri_->setRegAllocationHint(NextReloadLI->reg, 0, physReg);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
DowngradeRegister(cur, physReg);
|
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "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)
|
2009-03-23 22:57:19 +00:00
|
|
|
updateSpillWeights(SpillWeights, I->first, I->second, RC);
|
2010-09-01 21:48:06 +00:00
|
|
|
|
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);
|
2009-03-23 22:57:19 +00:00
|
|
|
updateSpillWeights(SpillWeights, reg, i->first->weight, RC);
|
2005-08-22 20:20:42 +00:00
|
|
|
}
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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;
|
2009-06-14 20:22:55 +00:00
|
|
|
unsigned minReg = 0;
|
2008-06-20 21:45:16 +00:00
|
|
|
|
|
|
|
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];
|
2010-09-01 22:48:34 +00:00
|
|
|
// Don't even consider reserved regs.
|
|
|
|
if (reservedRegs_.test(reg))
|
|
|
|
continue;
|
2010-09-01 21:04:27 +00:00
|
|
|
// Skip recently allocated registers and reserved registers.
|
2010-09-01 22:48:34 +00:00
|
|
|
if (minWeight > regWeight && !isRecentlyUsed(reg))
|
2008-06-20 21:45:16 +00:00
|
|
|
Found = true;
|
|
|
|
RegsWeights.push_back(std::make_pair(reg, regWeight));
|
2003-11-20 03:32:25 +00:00
|
|
|
}
|
2010-09-01 21:48:06 +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;
|
2010-09-01 21:04:27 +00:00
|
|
|
if (reservedRegs_.test(reg))
|
|
|
|
continue;
|
2006-05-12 19:07:46 +00:00
|
|
|
// 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.
|
2009-11-19 15:55:49 +00:00
|
|
|
std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare(*this));
|
2008-06-20 21:45:16 +00:00
|
|
|
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!
|
2010-09-01 21:34:41 +00:00
|
|
|
minReg = BestPhysReg ? BestPhysReg : getFirstNonReservedPhysReg(RC);
|
2008-07-22 22:46:49 +00:00
|
|
|
if (cur->weight == HUGE_VALF ||
|
2008-09-20 01:28:05 +00:00
|
|
|
li_->getApproximateInstructionCount(*cur) == 0) {
|
2008-06-20 21:45:16 +00:00
|
|
|
// Spill a physical register around defs and uses.
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
if (li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_)) {
|
2009-04-29 07:16:34 +00:00
|
|
|
// spillPhysRegAroundRegDefsUses may have invalidated iterator stored
|
|
|
|
// in fixed_. Reset them.
|
|
|
|
for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
|
|
|
|
IntervalPtr &IP = fixed_[i];
|
|
|
|
LiveInterval *I = IP.first;
|
|
|
|
if (I->reg == minReg || tri_->isSubRegister(minReg, I->reg))
|
|
|
|
IP.second = I->advanceTo(I->begin(), StartPosition);
|
|
|
|
}
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
DowngradedRegs.clear();
|
2009-03-23 18:24:37 +00:00
|
|
|
assignRegOrStackSlotAtInterval(cur);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
} else {
|
2009-11-03 23:52:08 +00:00
|
|
|
assert(false && "Ran out of registers during register allocation!");
|
2010-04-07 22:58:41 +00:00
|
|
|
report_fatal_error("Ran out of registers during register allocation!");
|
2009-03-23 18:24:37 +00:00
|
|
|
}
|
2008-09-20 01:28:05 +00:00
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-22 20:30:53 +00:00
|
|
|
DEBUG({
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << "\t\tregister(s) with min weight(s): ";
|
2009-08-22 20:30:53 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != LastCandidate; ++i)
|
2010-01-05 01:25:20 +00:00
|
|
|
dbgs() << tri_->getName(RegsWeights[i].first)
|
2009-08-22 20:30:53 +00:00
|
|
|
<< " (" << RegsWeights[i].second << ")\n";
|
|
|
|
});
|
2004-08-04 09:46:26 +00:00
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// If the current has the minimum weight, we need to spill it and
|
2004-08-04 09:46:26 +00:00
|
|
|
// 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) {
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\t\t\tspilling(c): " << *cur << '\n');
|
2011-03-10 01:21:58 +00:00
|
|
|
SmallVector<LiveInterval*, 8> added;
|
2011-03-10 01:51:42 +00:00
|
|
|
LiveRangeEdit LRE(*cur, added);
|
|
|
|
spiller_->spill(LRE);
|
2009-05-18 19:03:16 +00:00
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
std::sort(added.begin(), added.end(), LISorter());
|
2004-08-04 09:46:26 +00:00
|
|
|
if (added.empty())
|
|
|
|
return; // Early exit if all spills were folded.
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// Merge added with unhandled. Note that we have already sorted
|
|
|
|
// intervals returned by addIntervalsForSpills by their starting
|
2004-08-04 09:46:26 +00:00
|
|
|
// point.
|
2009-04-20 17:23:48 +00:00
|
|
|
// This also update the NextReloadMap. That is, it adds mapping from a
|
|
|
|
// register defined by a reload from SS to the next reload from SS in the
|
|
|
|
// same basic block.
|
|
|
|
MachineBasicBlock *LastReloadMBB = 0;
|
|
|
|
LiveInterval *LastReload = 0;
|
|
|
|
int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
|
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i) {
|
|
|
|
LiveInterval *ReloadLi = added[i];
|
|
|
|
if (ReloadLi->weight == HUGE_VALF &&
|
|
|
|
li_->getApproximateInstructionCount(*ReloadLi) == 0) {
|
2009-11-03 23:52:08 +00:00
|
|
|
SlotIndex ReloadIdx = ReloadLi->beginIndex();
|
2009-04-20 17:23:48 +00:00
|
|
|
MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
|
|
|
|
int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
|
|
|
|
if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
|
|
|
|
// Last reload of same SS is in the same MBB. We want to try to
|
|
|
|
// allocate both reloads the same register and make sure the reg
|
|
|
|
// isn't clobbered in between if at all possible.
|
2009-09-04 20:41:11 +00:00
|
|
|
assert(LastReload->beginIndex() < ReloadIdx);
|
2009-04-20 17:23:48 +00:00
|
|
|
NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
|
|
|
|
}
|
|
|
|
LastReloadMBB = ReloadMBB;
|
|
|
|
LastReload = ReloadLi;
|
|
|
|
LastReloadSS = ReloadSS;
|
|
|
|
}
|
|
|
|
unhandled_.push(ReloadLi);
|
|
|
|
}
|
2004-08-04 09:46:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-18 03:49:30 +00:00
|
|
|
++NumBacktracks;
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// Push the current interval back to unhandled since we are going
|
2004-08-04 09:46:26 +00:00
|
|
|
// 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
|
2010-09-01 21:48:06 +00:00
|
|
|
assert(!spillIs.empty() && "No spill intervals?");
|
2009-12-09 05:39:12 +00:00
|
|
|
SlotIndex earliestStart = spillIs[0]->beginIndex();
|
2010-08-13 22:56:53 +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.
|
2010-08-13 22:56:53 +00:00
|
|
|
SmallVector<LiveInterval*, 8> added;
|
2008-06-20 21:45:16 +00:00
|
|
|
while (!spillIs.empty()) {
|
|
|
|
LiveInterval *sli = spillIs.back();
|
|
|
|
spillIs.pop_back();
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\t\t\tspilling(a): " << *sli << '\n');
|
2009-12-09 05:39:12 +00:00
|
|
|
if (sli->beginIndex() < earliestStart)
|
|
|
|
earliestStart = sli->beginIndex();
|
2011-03-10 01:51:42 +00:00
|
|
|
LiveRangeEdit LRE(*sli, added, 0, &spillIs);
|
|
|
|
spiller_->spill(LRE);
|
2008-06-20 21:45:16 +00:00
|
|
|
spilled.insert(sli->reg);
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 22:56:53 +00:00
|
|
|
// Include any added intervals in earliestStart.
|
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i) {
|
|
|
|
SlotIndex SI = added[i]->beginIndex();
|
|
|
|
if (SI < earliestStart)
|
|
|
|
earliestStart = SI;
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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.
|
2009-12-09 05:39:12 +00:00
|
|
|
if (!i->empty() && i->beginIndex() < earliestStart)
|
2004-08-04 09:46:26 +00:00
|
|
|
break;
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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
|
2009-05-01 01:03:49 +00:00
|
|
|
// inactive to properly update regUse_ 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);
|
2009-05-01 01:03:49 +00:00
|
|
|
delRegUse(vrm_->getPhys(i->reg));
|
2006-02-23 06:44:17 +00:00
|
|
|
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
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
DenseMap<unsigned, unsigned>::iterator ii = DowngradeMap.find(i->reg);
|
|
|
|
if (ii == DowngradeMap.end())
|
|
|
|
// 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.
|
2009-06-15 08:28:29 +00:00
|
|
|
mri_->setRegAllocationHint(i->reg, 0, 0);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
else {
|
|
|
|
UpgradeRegister(ii->second);
|
|
|
|
}
|
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);
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// Scan the rest and undo each interval that expired after t and
|
2004-08-04 09:46:26 +00:00
|
|
|
// 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) &&
|
2009-09-04 20:41:11 +00:00
|
|
|
HI->expiredAt(cur->beginIndex())) {
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "\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));
|
2009-05-01 01:03:49 +00:00
|
|
|
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
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// Merge added with unhandled.
|
|
|
|
// This also update the NextReloadMap. That is, it adds mapping from a
|
|
|
|
// register defined by a reload from SS to the next reload from SS in the
|
|
|
|
// same basic block.
|
|
|
|
MachineBasicBlock *LastReloadMBB = 0;
|
|
|
|
LiveInterval *LastReload = 0;
|
|
|
|
int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
|
|
|
|
std::sort(added.begin(), added.end(), LISorter());
|
|
|
|
for (unsigned i = 0, e = added.size(); i != e; ++i) {
|
|
|
|
LiveInterval *ReloadLi = added[i];
|
|
|
|
if (ReloadLi->weight == HUGE_VALF &&
|
|
|
|
li_->getApproximateInstructionCount(*ReloadLi) == 0) {
|
2009-11-03 23:52:08 +00:00
|
|
|
SlotIndex ReloadIdx = ReloadLi->beginIndex();
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
|
|
|
|
int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
|
|
|
|
if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
|
|
|
|
// Last reload of same SS is in the same MBB. We want to try to
|
|
|
|
// allocate both reloads the same register and make sure the reg
|
|
|
|
// isn't clobbered in between if at all possible.
|
2009-09-04 20:41:11 +00:00
|
|
|
assert(LastReload->beginIndex() < ReloadIdx);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
|
|
|
|
}
|
|
|
|
LastReloadMBB = ReloadMBB;
|
|
|
|
LastReload = ReloadLi;
|
|
|
|
LastReloadSS = ReloadSS;
|
|
|
|
}
|
|
|
|
unhandled_.push(ReloadLi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 08:28:29 +00:00
|
|
|
unsigned RALinScan::getFreePhysReg(LiveInterval* cur,
|
|
|
|
const TargetRegisterClass *RC,
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
unsigned MaxInactiveCount,
|
|
|
|
SmallVector<unsigned, 256> &inactiveCounts,
|
|
|
|
bool SkipDGRegs) {
|
|
|
|
unsigned FreeReg = 0;
|
|
|
|
unsigned FreeRegInactiveCount = 0;
|
|
|
|
|
2009-06-18 02:04:01 +00:00
|
|
|
std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(cur->reg);
|
|
|
|
// Resolve second part of the hint (if possible) given the current allocation.
|
|
|
|
unsigned physReg = Hint.second;
|
2011-01-10 02:58:51 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(physReg) && vrm_->hasPhys(physReg))
|
2009-06-18 02:04:01 +00:00
|
|
|
physReg = vrm_->getPhys(physReg);
|
|
|
|
|
2009-06-15 08:28:29 +00:00
|
|
|
TargetRegisterClass::iterator I, E;
|
2009-06-18 02:04:01 +00:00
|
|
|
tie(I, E) = tri_->getAllocationOrder(RC, Hint.first, physReg, *mf_);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
assert(I != E && "No allocatable register in this register class!");
|
|
|
|
|
|
|
|
// Scan for the first available register.
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
unsigned Reg = *I;
|
|
|
|
// Ignore "downgraded" registers.
|
|
|
|
if (SkipDGRegs && DowngradedRegs.count(Reg))
|
|
|
|
continue;
|
2010-09-01 21:04:27 +00:00
|
|
|
// Skip reserved registers.
|
|
|
|
if (reservedRegs_.test(Reg))
|
|
|
|
continue;
|
2009-11-19 15:55:49 +00:00
|
|
|
// Skip recently allocated registers.
|
|
|
|
if (isRegAvail(Reg) && !isRecentlyUsed(Reg)) {
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
FreeReg = Reg;
|
|
|
|
if (FreeReg < inactiveCounts.size())
|
|
|
|
FreeRegInactiveCount = inactiveCounts[FreeReg];
|
|
|
|
else
|
|
|
|
FreeRegInactiveCount = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no free regs, or if this reg has the max inactive count,
|
|
|
|
// return this register.
|
2009-11-19 15:55:49 +00:00
|
|
|
if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) {
|
|
|
|
// Remember what register we picked so we can skip it next time.
|
|
|
|
if (FreeReg != 0) recordRecentlyUsed(FreeReg);
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
return FreeReg;
|
2009-11-19 15:55:49 +00:00
|
|
|
}
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
// 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;
|
|
|
|
// Ignore "downgraded" registers.
|
|
|
|
if (SkipDGRegs && DowngradedRegs.count(Reg))
|
|
|
|
continue;
|
2010-09-01 21:04:27 +00:00
|
|
|
// Skip reserved registers.
|
|
|
|
if (reservedRegs_.test(Reg))
|
|
|
|
continue;
|
2009-05-01 01:03:49 +00:00
|
|
|
if (isRegAvail(Reg) && Reg < inactiveCounts.size() &&
|
2009-11-19 19:09:39 +00:00
|
|
|
FreeRegInactiveCount < inactiveCounts[Reg] && !isRecentlyUsed(Reg)) {
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
FreeReg = Reg;
|
|
|
|
FreeRegInactiveCount = inactiveCounts[Reg];
|
|
|
|
if (FreeRegInactiveCount == MaxInactiveCount)
|
|
|
|
break; // We found the one with the max inactive count.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 15:55:49 +00:00
|
|
|
// Remember what register we picked so we can skip it next time.
|
|
|
|
recordRecentlyUsed(FreeReg);
|
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
return FreeReg;
|
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;
|
2010-09-01 21:48:06 +00:00
|
|
|
|
2008-09-18 22:38:47 +00:00
|
|
|
const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
|
2005-08-23 22:27:31 +00:00
|
|
|
const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
|
2010-09-01 21:48:06 +00:00
|
|
|
|
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
|
|
|
|
2010-09-01 21:48:06 +00:00
|
|
|
// If this is not in a related reg class to the register we're allocating,
|
2005-08-23 22:27:31 +00:00
|
|
|
// don't check it.
|
2008-09-18 22:38:47 +00:00
|
|
|
const TargetRegisterClass *RegRC = mri_->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
|
|
|
}
|
|
|
|
|
2007-04-17 20:32:26 +00:00
|
|
|
// If copy coalescer has assigned a "preferred" register, check if it's
|
2008-09-24 01:07:17 +00:00
|
|
|
// available first.
|
2009-06-14 20:22:55 +00:00
|
|
|
unsigned Preference = vrm_->getRegAllocPref(cur->reg);
|
|
|
|
if (Preference) {
|
2010-01-05 01:25:20 +00:00
|
|
|
DEBUG(dbgs() << "(preferred: " << tri_->getName(Preference) << ") ");
|
2010-09-01 21:48:06 +00:00
|
|
|
if (isRegAvail(Preference) &&
|
2009-06-14 20:22:55 +00:00
|
|
|
RC->contains(Preference))
|
|
|
|
return Preference;
|
2008-02-20 12:07:57 +00:00
|
|
|
}
|
2007-04-17 20:32:26 +00:00
|
|
|
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
if (!DowngradedRegs.empty()) {
|
2009-06-15 08:28:29 +00:00
|
|
|
unsigned FreeReg = getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts,
|
Added a linearscan register allocation optimization. When the register allocator spill an interval with multiple uses in the same basic block, it creates a different virtual register for each of the reloads. e.g.
%reg1498<def> = MOV32rm %reg1024, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg1024, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg1024, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
=>
%reg1498<def> = MOV32rm %reg2036, 1, %reg0, 12, %reg0, Mem:LD(4,4) [sunkaddr39 + 0]
%reg1506<def> = MOV32rm %reg2037, 1, %reg0, 8, %reg0, Mem:LD(4,4) [sunkaddr42 + 0]
%reg1486<def> = MOV32rr %reg1506
%reg1486<def> = XOR32rr %reg1486, %reg1498, %EFLAGS<imp-def,dead>
%reg1510<def> = MOV32rm %reg2038, 1, %reg0, 4, %reg0, Mem:LD(4,4) [sunkaddr45 + 0]
From linearscan's point of view, each of reg2036, 2037, and 2038 are separate registers, each is "killed" after a single use. The reloaded register is available and it's often clobbered right away. e.g. In thise case reg1498 is allocated EAX while reg2036 is allocated RAX. This means we end up with multiple reloads from the same stack slot in the same basic block.
Now linearscan recognize there are other reloads from same SS in the same BB. So it'll "downgrade" RAX (and its aliases) after reg2036 is allocated until the next reload (reg2037) is done. This greatly increase the likihood reloads from SS are reused.
This speeds up sha1 from OpenSSL by 5.8%. It is also an across the board win for SPEC2000 and 2006.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69585 91177308-0d34-0410-b5e6-96231b3b80d8
2009-04-20 08:01:12 +00:00
|
|
|
true);
|
|
|
|
if (FreeReg)
|
|
|
|
return FreeReg;
|
2004-08-04 09:46:26 +00:00
|
|
|
}
|
2009-06-15 08:28:29 +00:00
|
|
|
return getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, false);
|
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
|
|
|
}
|