2007-02-23 02:01:19 +01:00
|
|
|
//===-- RegisterScavenging.cpp - Machine register scavenging --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-02-23 02:01:19 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the machine register scavenger. It can provide
|
2008-03-07 00:22:43 +01:00
|
|
|
// information, such as unused registers, at any point in a machine basic block.
|
|
|
|
// It also provides a mechanism to make registers available by evicting them to
|
|
|
|
// spill slots.
|
2007-02-23 02:01:19 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "reg-scavenging"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2009-08-06 18:32:47 +02:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-02-23 02:01:19 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2008-04-05 03:27:09 +02:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-07-11 15:10:19 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-02-10 19:45:23 +01:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-02-23 02:01:19 +01:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-08-11 08:25:12 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2008-04-05 03:27:09 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-01-05 18:59:02 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-02-27 02:58:48 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-02-23 02:01:19 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-03 23:12:25 +01:00
|
|
|
/// setUsed - Set the register and its sub-registers as being used.
|
2009-07-01 10:19:36 +02:00
|
|
|
void RegScavenger::setUsed(unsigned Reg) {
|
2008-03-03 23:12:25 +01:00
|
|
|
RegsAvailable.reset(Reg);
|
|
|
|
|
2008-03-05 01:59:57 +01:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
2009-07-01 10:19:36 +02:00
|
|
|
unsigned SubReg = *SubRegs; ++SubRegs)
|
2008-03-03 23:12:25 +01:00
|
|
|
RegsAvailable.reset(SubReg);
|
|
|
|
}
|
|
|
|
|
2009-08-11 08:25:12 +02:00
|
|
|
bool RegScavenger::isAliasUsed(unsigned Reg) const {
|
|
|
|
if (isUsed(Reg))
|
|
|
|
return true;
|
|
|
|
for (const unsigned *R = TRI->getAliasSet(Reg); *R; ++R)
|
|
|
|
if (isUsed(*R))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-06 18:32:47 +02:00
|
|
|
void RegScavenger::initRegState() {
|
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
|
|
|
ScavengeRestore = NULL;
|
|
|
|
|
|
|
|
// All registers started out unused.
|
|
|
|
RegsAvailable.set();
|
|
|
|
|
|
|
|
// Reserved registers are always used.
|
|
|
|
RegsAvailable ^= ReservedRegs;
|
|
|
|
|
2009-08-13 18:20:04 +02:00
|
|
|
if (!MBB)
|
2009-08-08 00:39:43 +02:00
|
|
|
return;
|
2009-08-13 18:20:04 +02:00
|
|
|
|
|
|
|
// Live-in registers are in use.
|
2009-08-08 00:39:43 +02:00
|
|
|
for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
|
|
|
|
E = MBB->livein_end(); I != E; ++I)
|
|
|
|
setUsed(*I);
|
2009-08-13 18:20:04 +02:00
|
|
|
|
|
|
|
// Pristine CSRs are also unavailable.
|
|
|
|
BitVector PR = MBB->getParent()->getFrameInfo()->getPristineRegs(MBB);
|
|
|
|
for (int I = PR.find_first(); I>0; I = PR.find_next(I))
|
|
|
|
setUsed(I);
|
2009-08-06 18:32:47 +02:00
|
|
|
}
|
|
|
|
|
2007-03-01 03:19:39 +01:00
|
|
|
void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
|
2008-07-07 22:06:06 +02:00
|
|
|
MachineFunction &MF = *mbb->getParent();
|
2007-02-23 02:01:19 +01:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2007-03-06 11:01:25 +01:00
|
|
|
TII = TM.getInstrInfo();
|
2008-03-05 01:59:57 +01:00
|
|
|
TRI = TM.getRegisterInfo();
|
2008-04-05 03:27:09 +02:00
|
|
|
MRI = &MF.getRegInfo();
|
2007-02-23 02:01:19 +01:00
|
|
|
|
2008-03-05 01:59:57 +01:00
|
|
|
assert((NumPhysRegs == 0 || NumPhysRegs == TRI->getNumRegs()) &&
|
2007-02-27 23:58:43 +01:00
|
|
|
"Target changed?");
|
|
|
|
|
2009-08-06 18:32:47 +02:00
|
|
|
// Self-initialize.
|
2007-02-27 23:58:43 +01:00
|
|
|
if (!MBB) {
|
2008-03-05 01:59:57 +01:00
|
|
|
NumPhysRegs = TRI->getNumRegs();
|
2007-03-27 00:23:54 +02:00
|
|
|
RegsAvailable.resize(NumPhysRegs);
|
2007-02-27 23:58:43 +01:00
|
|
|
|
2007-03-01 03:19:39 +01:00
|
|
|
// Create reserved registers bitvector.
|
2008-03-05 01:59:57 +01:00
|
|
|
ReservedRegs = TRI->getReservedRegs(MF);
|
2007-03-01 03:19:39 +01:00
|
|
|
|
2007-02-27 23:58:43 +01:00
|
|
|
// Create callee-saved registers bitvector.
|
|
|
|
CalleeSavedRegs.resize(NumPhysRegs);
|
2008-03-05 01:59:57 +01:00
|
|
|
const unsigned *CSRegs = TRI->getCalleeSavedRegs();
|
2009-08-08 00:39:43 +02:00
|
|
|
if (CSRegs != NULL)
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
CalleeSavedRegs.set(CSRegs[i]);
|
2007-02-27 23:58:43 +01:00
|
|
|
}
|
|
|
|
|
2009-11-12 08:49:10 +01:00
|
|
|
MBB = mbb;
|
|
|
|
initRegState();
|
2007-03-01 03:19:39 +01:00
|
|
|
|
|
|
|
Tracking = false;
|
2007-02-23 02:01:19 +01:00
|
|
|
}
|
|
|
|
|
2009-08-08 15:18:47 +02:00
|
|
|
void RegScavenger::addRegWithSubRegs(BitVector &BV, unsigned Reg) {
|
|
|
|
BV.set(Reg);
|
|
|
|
for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
|
|
|
|
BV.set(*R);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegScavenger::addRegWithAliases(BitVector &BV, unsigned Reg) {
|
|
|
|
BV.set(Reg);
|
|
|
|
for (const unsigned *R = TRI->getAliasSet(Reg); *R; R++)
|
|
|
|
BV.set(*R);
|
|
|
|
}
|
|
|
|
|
2007-02-23 02:01:19 +01:00
|
|
|
void RegScavenger::forward() {
|
2007-02-27 02:58:48 +01:00
|
|
|
// Move ptr forward.
|
2007-02-27 23:58:43 +01:00
|
|
|
if (!Tracking) {
|
|
|
|
MBBI = MBB->begin();
|
|
|
|
Tracking = true;
|
|
|
|
} else {
|
|
|
|
assert(MBBI != MBB->end() && "Already at the end of the basic block!");
|
2009-12-03 01:50:42 +01:00
|
|
|
MBBI = llvm::next(MBBI);
|
2007-02-27 23:58:43 +01:00
|
|
|
}
|
2007-02-27 02:58:48 +01:00
|
|
|
|
2007-02-23 02:01:19 +01:00
|
|
|
MachineInstr *MI = MBBI;
|
2007-03-06 11:01:25 +01:00
|
|
|
|
2008-11-20 03:32:35 +01:00
|
|
|
if (MI == ScavengeRestore) {
|
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
|
|
|
ScavengeRestore = NULL;
|
|
|
|
}
|
2007-03-06 11:01:25 +01:00
|
|
|
|
2009-08-08 15:18:47 +02:00
|
|
|
// Find out which registers are early clobbered, killed, defined, and marked
|
|
|
|
// def-dead in this instruction.
|
|
|
|
BitVector EarlyClobberRegs(NumPhysRegs);
|
|
|
|
BitVector KillRegs(NumPhysRegs);
|
|
|
|
BitVector DefRegs(NumPhysRegs);
|
|
|
|
BitVector DeadRegs(NumPhysRegs);
|
2007-02-23 02:01:19 +01:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2009-08-08 15:18:47 +02:00
|
|
|
if (!MO.isReg() || MO.isUndef())
|
2007-02-23 02:01:19 +01:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
2009-08-08 15:18:47 +02:00
|
|
|
if (!Reg || isReserved(Reg))
|
|
|
|
continue;
|
2008-03-03 23:12:25 +01:00
|
|
|
|
2009-08-08 15:18:47 +02:00
|
|
|
if (MO.isUse()) {
|
|
|
|
// Two-address operands implicitly kill.
|
|
|
|
if (MO.isKill() || MI->isRegTiedToDefOperand(i))
|
|
|
|
addRegWithSubRegs(KillRegs, Reg);
|
|
|
|
} else {
|
|
|
|
assert(MO.isDef());
|
|
|
|
if (MO.isDead())
|
|
|
|
addRegWithSubRegs(DeadRegs, Reg);
|
|
|
|
else
|
|
|
|
addRegWithSubRegs(DefRegs, Reg);
|
|
|
|
if (MO.isEarlyClobber())
|
|
|
|
addRegWithAliases(EarlyClobberRegs, Reg);
|
2008-03-03 23:12:25 +01:00
|
|
|
}
|
2007-02-23 02:01:19 +01:00
|
|
|
}
|
2008-03-03 23:12:25 +01:00
|
|
|
|
2009-08-08 15:18:47 +02:00
|
|
|
// Verify uses and defs.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || MO.isUndef())
|
2009-07-01 03:59:31 +02:00
|
|
|
continue;
|
2009-08-08 15:18:47 +02:00
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg || isReserved(Reg))
|
2007-03-02 11:43:16 +01:00
|
|
|
continue;
|
2009-08-08 15:18:47 +02:00
|
|
|
if (MO.isUse()) {
|
2009-10-26 05:56:07 +01:00
|
|
|
if (!isUsed(Reg)) {
|
|
|
|
// Check if it's partial live: e.g.
|
|
|
|
// D0 = insert_subreg D0<undef>, S0
|
|
|
|
// ... D0
|
|
|
|
// The problem is the insert_subreg could be eliminated. The use of
|
|
|
|
// D0 is using a partially undef value. This is not *incorrect* since
|
|
|
|
// S1 is can be freely clobbered.
|
|
|
|
// Ideally we would like a way to model this, but leaving the
|
|
|
|
// insert_subreg around causes both correctness and performance issues.
|
|
|
|
bool SubUsed = false;
|
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
|
|
|
unsigned SubReg = *SubRegs; ++SubRegs)
|
|
|
|
if (isUsed(SubReg)) {
|
|
|
|
SubUsed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(SubUsed && "Using an undefined register!");
|
|
|
|
}
|
2009-08-15 20:16:58 +02:00
|
|
|
assert((!EarlyClobberRegs.test(Reg) || MI->isRegTiedToDefOperand(i)) &&
|
2009-08-08 15:18:47 +02:00
|
|
|
"Using an early clobbered register!");
|
|
|
|
} else {
|
|
|
|
assert(MO.isDef());
|
Fix PR5024 with a big hammer: disable the double-def assertion in the scavenger.
LiveVariables add implicit kills to correctly track partial register kills. This works well enough and is fairly accurate. But coalescer can make it impossible to maintain these markers. e.g.
BL <ga:sss1>, %R0<kill,undef>, %S0<kill>, %R0<imp-def>, %R1<imp-def,dead>, %R2<imp-def,dead>, %R3<imp-def,dead>, %R12<imp-def,dead>, %LR<imp-def,dead>, %D0<imp-def>, ...
...
%reg1031<def> = FLDS <cp#1>, 0, 14, %reg0, Mem:LD4[ConstantPool]
...
%S0<def> = FCPYS %reg1031<kill>, 14, %reg0, %D0<imp-use,kill>
When reg1031 and S0 are coalesced, the copy (FCPYS) will be eliminated the the implicit-kill of D0 is lost. In this case it's possible to move the marker to the FLDS. But in many cases, this is not possible. Suppose
%reg1031<def> = FOO <cp#1>, %D0<imp-def>
...
%S0<def> = FCPYS %reg1031<kill>, 14, %reg0, %D0<imp-use,kill>
When FCPYS goes away, the definition of S0 is the "FOO" instruction. However, transferring the D0 implicit-kill to FOO doesn't work since it is the def of D0 itself. We need to fix this in another time by introducing a "kill" pseudo instruction to track liveness.
Disabling the assertion is not ideal, but machine verifier is doing that job now. It's important to know double-def is not a miscomputation since it means a register should be free but it's not tracked as free. It's a performance issue instead.
llvm-svn: 82677
2009-09-24 04:27:09 +02:00
|
|
|
#if 0
|
|
|
|
// FIXME: Enable this once we've figured out how to correctly transfer
|
|
|
|
// implicit kills during codegen passes like the coalescer.
|
Remove RegisterScavenger::isSuperRegUsed(). This completely reverses the mistaken commit r77904.
Now there is no special treatment of instructions that redefine part of a
super-register. Instead, the super-register is marked with <imp-use,kill> and
<imp-def>. For instance, from LowerSubregs on ARM:
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1<undef>, %D1<kill>, 5
subreg: %D2<def> = FCPYD %D1<kill>, 14, %reg0, %Q1<imp-def>
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1, %D0<kill>, 6
subreg: %D3<def> = FCPYD %D0<kill>, 14, %reg0, %Q1<imp-use,kill>, %Q1<imp-def>
llvm-svn: 78466
2009-08-08 15:19:10 +02:00
|
|
|
assert((KillRegs.test(Reg) || isUnused(Reg) ||
|
2009-08-08 15:18:47 +02:00
|
|
|
isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
|
|
|
|
"Re-defining a live register!");
|
Fix PR5024 with a big hammer: disable the double-def assertion in the scavenger.
LiveVariables add implicit kills to correctly track partial register kills. This works well enough and is fairly accurate. But coalescer can make it impossible to maintain these markers. e.g.
BL <ga:sss1>, %R0<kill,undef>, %S0<kill>, %R0<imp-def>, %R1<imp-def,dead>, %R2<imp-def,dead>, %R3<imp-def,dead>, %R12<imp-def,dead>, %LR<imp-def,dead>, %D0<imp-def>, ...
...
%reg1031<def> = FLDS <cp#1>, 0, 14, %reg0, Mem:LD4[ConstantPool]
...
%S0<def> = FCPYS %reg1031<kill>, 14, %reg0, %D0<imp-use,kill>
When reg1031 and S0 are coalesced, the copy (FCPYS) will be eliminated the the implicit-kill of D0 is lost. In this case it's possible to move the marker to the FLDS. But in many cases, this is not possible. Suppose
%reg1031<def> = FOO <cp#1>, %D0<imp-def>
...
%S0<def> = FCPYS %reg1031<kill>, 14, %reg0, %D0<imp-use,kill>
When FCPYS goes away, the definition of S0 is the "FOO" instruction. However, transferring the D0 implicit-kill to FOO doesn't work since it is the def of D0 itself. We need to fix this in another time by introducing a "kill" pseudo instruction to track liveness.
Disabling the assertion is not ideal, but machine verifier is doing that job now. It's important to know double-def is not a miscomputation since it means a register should be free but it's not tracked as free. It's a performance issue instead.
llvm-svn: 82677
2009-09-24 04:27:09 +02:00
|
|
|
#endif
|
2007-03-02 11:43:16 +01:00
|
|
|
}
|
2007-02-23 02:01:19 +01:00
|
|
|
}
|
2009-08-08 15:18:47 +02:00
|
|
|
|
|
|
|
// Commit the changes.
|
|
|
|
setUnused(KillRegs);
|
|
|
|
setUnused(DeadRegs);
|
|
|
|
setUsed(DefRegs);
|
2007-02-23 02:01:19 +01:00
|
|
|
}
|
|
|
|
|
2007-03-20 22:35:06 +01:00
|
|
|
void RegScavenger::getRegsUsed(BitVector &used, bool includeReserved) {
|
|
|
|
if (includeReserved)
|
2007-03-27 00:23:54 +02:00
|
|
|
used = ~RegsAvailable;
|
2007-03-20 22:35:06 +01:00
|
|
|
else
|
2007-03-27 00:23:54 +02:00
|
|
|
used = ~RegsAvailable & ~ReservedRegs;
|
2007-03-20 22:35:06 +01:00
|
|
|
}
|
|
|
|
|
2007-02-23 02:01:19 +01:00
|
|
|
/// CreateRegClassMask - Set the bits that represent the registers in the
|
|
|
|
/// TargetRegisterClass.
|
|
|
|
static void CreateRegClassMask(const TargetRegisterClass *RC, BitVector &Mask) {
|
|
|
|
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I != E;
|
|
|
|
++I)
|
|
|
|
Mask.set(*I);
|
|
|
|
}
|
|
|
|
|
2009-08-18 23:14:54 +02:00
|
|
|
unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
|
|
|
|
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (!isAliasUsed(*I))
|
|
|
|
return *I;
|
|
|
|
return 0;
|
2007-02-23 02:01:19 +01:00
|
|
|
}
|
2007-03-06 11:01:25 +01:00
|
|
|
|
2009-08-16 19:41:39 +02:00
|
|
|
/// findSurvivorReg - Return the candidate register that is unused for the
|
|
|
|
/// longest after MBBI. UseMI is set to the instruction where the search
|
|
|
|
/// stopped.
|
|
|
|
///
|
|
|
|
/// No more than InstrLimit instructions are inspected.
|
|
|
|
///
|
2009-10-25 02:45:07 +02:00
|
|
|
unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI,
|
2009-08-16 19:41:39 +02:00
|
|
|
BitVector &Candidates,
|
|
|
|
unsigned InstrLimit,
|
|
|
|
MachineBasicBlock::iterator &UseMI) {
|
|
|
|
int Survivor = Candidates.find_first();
|
|
|
|
assert(Survivor > 0 && "No candidates for scavenging");
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator ME = MBB->getFirstTerminator();
|
2009-10-25 02:45:07 +02:00
|
|
|
assert(StartMI != ME && "MI already at terminator");
|
|
|
|
MachineBasicBlock::iterator RestorePointMI = StartMI;
|
|
|
|
MachineBasicBlock::iterator MI = StartMI;
|
2009-08-16 19:41:39 +02:00
|
|
|
|
2009-10-25 02:45:07 +02:00
|
|
|
bool inVirtLiveRange = false;
|
2009-08-16 19:41:39 +02:00
|
|
|
for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
|
2009-10-25 02:45:07 +02:00
|
|
|
bool isVirtKillInsn = false;
|
|
|
|
bool isVirtDefInsn = false;
|
2009-08-16 19:41:39 +02:00
|
|
|
// Remove any candidates touched by instruction.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2009-10-25 02:45:07 +02:00
|
|
|
if (!MO.isReg() || MO.isUndef() || !MO.getReg())
|
2009-08-16 19:41:39 +02:00
|
|
|
continue;
|
2009-10-25 02:45:07 +02:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
|
|
|
|
if (MO.isDef())
|
|
|
|
isVirtDefInsn = true;
|
|
|
|
else if (MO.isKill())
|
|
|
|
isVirtKillInsn = true;
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-16 19:41:39 +02:00
|
|
|
Candidates.reset(MO.getReg());
|
|
|
|
for (const unsigned *R = TRI->getAliasSet(MO.getReg()); *R; R++)
|
|
|
|
Candidates.reset(*R);
|
|
|
|
}
|
2009-10-25 02:45:07 +02:00
|
|
|
// If we're not in a virtual reg's live range, this is a valid
|
|
|
|
// restore point.
|
|
|
|
if (!inVirtLiveRange) RestorePointMI = MI;
|
|
|
|
|
|
|
|
// Update whether we're in the live range of a virtual register
|
|
|
|
if (isVirtKillInsn) inVirtLiveRange = false;
|
|
|
|
if (isVirtDefInsn) inVirtLiveRange = true;
|
2009-08-11 08:25:12 +02:00
|
|
|
|
2009-08-16 19:41:39 +02:00
|
|
|
// Was our survivor untouched by this instruction?
|
|
|
|
if (Candidates.test(Survivor))
|
2009-08-11 08:25:12 +02:00
|
|
|
continue;
|
2009-08-16 19:41:39 +02:00
|
|
|
|
|
|
|
// All candidates gone?
|
|
|
|
if (Candidates.none())
|
|
|
|
break;
|
|
|
|
|
|
|
|
Survivor = Candidates.find_first();
|
2007-03-06 11:01:25 +01:00
|
|
|
}
|
2009-10-25 02:45:07 +02:00
|
|
|
// If we ran off the end, that's where we want to restore.
|
|
|
|
if (MI == ME) RestorePointMI = ME;
|
|
|
|
assert (RestorePointMI != StartMI &&
|
|
|
|
"No available scavenger restore location!");
|
2009-08-16 19:41:39 +02:00
|
|
|
|
|
|
|
// We ran out of candidates, so stop the search.
|
2009-10-25 02:45:07 +02:00
|
|
|
UseMI = RestorePointMI;
|
2009-08-16 19:41:39 +02:00
|
|
|
return Survivor;
|
2007-03-06 11:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
|
2007-05-01 11:01:42 +02:00
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
int SPAdj) {
|
2007-03-06 11:01:25 +01:00
|
|
|
// Mask off the registers which are not in the TargetRegisterClass.
|
|
|
|
BitVector Candidates(NumPhysRegs, false);
|
|
|
|
CreateRegClassMask(RC, Candidates);
|
2009-08-06 18:32:47 +02:00
|
|
|
// Do not include reserved registers.
|
|
|
|
Candidates ^= ReservedRegs & Candidates;
|
2007-03-06 11:01:25 +01:00
|
|
|
|
|
|
|
// Exclude all the registers being used by the instruction.
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = I->getOperand(i);
|
2009-09-30 03:47:59 +02:00
|
|
|
if (MO.isReg() && MO.getReg() != 0 &&
|
|
|
|
!TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
2007-03-06 11:01:25 +01:00
|
|
|
Candidates.reset(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2008-02-16 02:09:25 +01:00
|
|
|
// Find the register whose use is furthest away.
|
2009-08-16 19:41:39 +02:00
|
|
|
MachineBasicBlock::iterator UseMI;
|
|
|
|
unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
|
2009-08-11 08:25:12 +02:00
|
|
|
|
2009-08-16 19:41:39 +02:00
|
|
|
// If we found an unused register there is no reason to spill it. We have
|
|
|
|
// probably found a callee-saved register that has been saved in the
|
|
|
|
// prologue, but happens to be unused at this point.
|
|
|
|
if (!isAliasUsed(SReg))
|
|
|
|
return SReg;
|
2007-03-06 11:01:25 +01:00
|
|
|
|
2009-08-02 22:29:41 +02:00
|
|
|
assert(ScavengedReg == 0 &&
|
2009-07-12 22:07:01 +02:00
|
|
|
"Scavenger slot is live, unable to scavenge another register!");
|
2007-03-06 11:01:25 +01:00
|
|
|
|
2009-08-06 18:32:47 +02:00
|
|
|
// Avoid infinite regress
|
|
|
|
ScavengedReg = SReg;
|
|
|
|
|
2009-10-06 00:30:23 +02:00
|
|
|
// If the target knows how to save/restore the register, let it do so;
|
|
|
|
// otherwise, use the emergency stack spill slot.
|
2009-10-20 00:27:30 +02:00
|
|
|
if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) {
|
2009-10-06 00:30:23 +02:00
|
|
|
// Spill the scavenged register before I.
|
|
|
|
assert(ScavengingFrameIndex >= 0 &&
|
2009-10-06 17:03:44 +02:00
|
|
|
"Cannot scavenge register without an emergency spill slot!");
|
2009-10-06 00:30:23 +02:00
|
|
|
TII->storeRegToStackSlot(*MBB, I, SReg, true, ScavengingFrameIndex, RC);
|
|
|
|
MachineBasicBlock::iterator II = prior(I);
|
2009-10-07 19:12:56 +02:00
|
|
|
TRI->eliminateFrameIndex(II, SPAdj, NULL, this);
|
2009-10-06 00:30:23 +02:00
|
|
|
|
|
|
|
// Restore the scavenged register before its use (or first terminator).
|
|
|
|
TII->loadRegFromStackSlot(*MBB, UseMI, SReg, ScavengingFrameIndex, RC);
|
2009-10-22 00:59:56 +02:00
|
|
|
II = prior(UseMI);
|
|
|
|
TRI->eliminateFrameIndex(II, SPAdj, NULL, this);
|
2009-10-20 00:27:30 +02:00
|
|
|
}
|
2008-11-20 03:32:35 +01:00
|
|
|
|
2009-08-16 19:41:39 +02:00
|
|
|
ScavengeRestore = prior(UseMI);
|
2009-10-06 00:30:23 +02:00
|
|
|
|
2009-08-08 00:39:43 +02:00
|
|
|
// Doing this here leads to infinite regress.
|
2009-08-06 18:32:47 +02:00
|
|
|
// ScavengedReg = SReg;
|
2007-03-06 11:01:25 +01:00
|
|
|
ScavengedRC = RC;
|
|
|
|
|
|
|
|
return SReg;
|
|
|
|
}
|