2007-02-23 01:01:19 +00:00
|
|
|
//===-- RegisterScavenging.cpp - Machine register scavenging --------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2007-02-23 01:01:19 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the machine register scavenger. It can provide
|
2008-03-06 23:22:43 +00: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 01:01:19 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "reg-scavenging"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2009-08-06 16:32:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-02-23 01:01:19 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2008-04-05 01:27:09 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2010-09-02 00:51:37 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-09-02 00:51:37 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-02-23 01:01:19 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-02-23 01:01:19 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-03 22:12:25 +00:00
|
|
|
/// setUsed - Set the register and its sub-registers as being used.
|
2009-07-01 08:19:36 +00:00
|
|
|
void RegScavenger::setUsed(unsigned Reg) {
|
2008-03-03 22:12:25 +00:00
|
|
|
RegsAvailable.reset(Reg);
|
|
|
|
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
|
|
|
|
RegsAvailable.reset(*SubRegs);
|
2008-03-03 22:12:25 +00:00
|
|
|
}
|
|
|
|
|
2009-08-11 06:25:12 +00:00
|
|
|
bool RegScavenger::isAliasUsed(unsigned Reg) const {
|
2012-06-01 22:38:17 +00:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
2012-11-15 18:13:20 +00:00
|
|
|
if (isUsed(*AI, *AI == Reg))
|
2009-08-11 06:25:12 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-06 16:32:47 +00:00
|
|
|
void RegScavenger::initRegState() {
|
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
|
|
|
ScavengeRestore = NULL;
|
|
|
|
|
|
|
|
// All registers started out unused.
|
|
|
|
RegsAvailable.set();
|
|
|
|
|
2009-08-13 16:20:04 +00:00
|
|
|
if (!MBB)
|
2009-08-07 22:39:43 +00:00
|
|
|
return;
|
2009-08-13 16:20:04 +00:00
|
|
|
|
|
|
|
// Live-in registers are in use.
|
2010-04-13 16:57:55 +00:00
|
|
|
for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
|
2009-08-07 22:39:43 +00:00
|
|
|
E = MBB->livein_end(); I != E; ++I)
|
|
|
|
setUsed(*I);
|
2009-08-13 16:20:04 +00: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 16:32:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 02:19:39 +00:00
|
|
|
void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
|
2008-07-07 20:06:06 +00:00
|
|
|
MachineFunction &MF = *mbb->getParent();
|
2007-02-23 01:01:19 +00:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
2007-03-06 10:01:25 +00:00
|
|
|
TII = TM.getInstrInfo();
|
2008-03-05 00:59:57 +00:00
|
|
|
TRI = TM.getRegisterInfo();
|
2008-04-05 01:27:09 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
2007-02-23 01:01:19 +00:00
|
|
|
|
2008-03-05 00:59:57 +00:00
|
|
|
assert((NumPhysRegs == 0 || NumPhysRegs == TRI->getNumRegs()) &&
|
2007-02-27 22:58:43 +00:00
|
|
|
"Target changed?");
|
|
|
|
|
2012-03-27 15:13:58 +00:00
|
|
|
// It is not possible to use the register scavenger after late optimization
|
|
|
|
// passes that don't preserve accurate liveness information.
|
|
|
|
assert(MRI->tracksLiveness() &&
|
|
|
|
"Cannot use register scavenger with inaccurate liveness");
|
|
|
|
|
2009-08-06 16:32:47 +00:00
|
|
|
// Self-initialize.
|
2007-02-27 22:58:43 +00:00
|
|
|
if (!MBB) {
|
2008-03-05 00:59:57 +00:00
|
|
|
NumPhysRegs = TRI->getNumRegs();
|
2007-03-26 22:23:54 +00:00
|
|
|
RegsAvailable.resize(NumPhysRegs);
|
2012-01-29 01:29:28 +00:00
|
|
|
KillRegs.resize(NumPhysRegs);
|
|
|
|
DefRegs.resize(NumPhysRegs);
|
2007-02-27 22:58:43 +00:00
|
|
|
|
|
|
|
// Create callee-saved registers bitvector.
|
|
|
|
CalleeSavedRegs.resize(NumPhysRegs);
|
2012-03-04 03:33:22 +00:00
|
|
|
const uint16_t *CSRegs = TRI->getCalleeSavedRegs(&MF);
|
2009-08-07 22:39:43 +00:00
|
|
|
if (CSRegs != NULL)
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
CalleeSavedRegs.set(CSRegs[i]);
|
2007-02-27 22:58:43 +00:00
|
|
|
}
|
|
|
|
|
2009-11-12 07:49:10 +00:00
|
|
|
MBB = mbb;
|
|
|
|
initRegState();
|
2007-03-01 02:19:39 +00:00
|
|
|
|
|
|
|
Tracking = false;
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 13:18:47 +00:00
|
|
|
void RegScavenger::addRegWithSubRegs(BitVector &BV, unsigned Reg) {
|
|
|
|
BV.set(Reg);
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
|
|
|
|
BV.set(*SubRegs);
|
2009-08-08 13:18:47 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
void RegScavenger::forward() {
|
2007-02-27 01:58:48 +00:00
|
|
|
// Move ptr forward.
|
2007-02-27 22:58:43 +00:00
|
|
|
if (!Tracking) {
|
|
|
|
MBBI = MBB->begin();
|
|
|
|
Tracking = true;
|
|
|
|
} else {
|
2011-04-05 20:44:15 +00:00
|
|
|
assert(MBBI != MBB->end() && "Already past the end of the basic block!");
|
2009-12-03 00:50:42 +00:00
|
|
|
MBBI = llvm::next(MBBI);
|
2007-02-27 22:58:43 +00:00
|
|
|
}
|
2011-04-05 20:44:15 +00:00
|
|
|
assert(MBBI != MBB->end() && "Already at the end of the basic block!");
|
2007-02-27 01:58:48 +00:00
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
MachineInstr *MI = MBBI;
|
2007-03-06 10:01:25 +00:00
|
|
|
|
2008-11-20 02:32:35 +00:00
|
|
|
if (MI == ScavengeRestore) {
|
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
|
|
|
ScavengeRestore = NULL;
|
|
|
|
}
|
2007-03-06 10:01:25 +00:00
|
|
|
|
2010-04-15 20:28:39 +00:00
|
|
|
if (MI->isDebugValue())
|
|
|
|
return;
|
|
|
|
|
2009-08-08 13:18:47 +00:00
|
|
|
// Find out which registers are early clobbered, killed, defined, and marked
|
|
|
|
// def-dead in this instruction.
|
2010-06-16 07:35:02 +00:00
|
|
|
// FIXME: The scavenger is not predication aware. If the instruction is
|
|
|
|
// predicated, conservatively assume "kill" markers do not actually kill the
|
|
|
|
// register. Similarly ignores "dead" markers.
|
|
|
|
bool isPred = TII->isPredicated(MI);
|
2012-01-29 01:29:28 +00:00
|
|
|
KillRegs.reset();
|
|
|
|
DefRegs.reset();
|
2007-02-23 01:01:19 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2012-02-22 22:50:14 +00:00
|
|
|
if (MO.isRegMask())
|
|
|
|
(isPred ? DefRegs : KillRegs).setBitsNotInMask(MO.getRegMask());
|
2011-05-02 20:06:28 +00:00
|
|
|
if (!MO.isReg())
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
2011-07-30 00:57:25 +00:00
|
|
|
if (!Reg || isReserved(Reg))
|
2009-08-08 13:18:47 +00:00
|
|
|
continue;
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2009-08-08 13:18:47 +00:00
|
|
|
if (MO.isUse()) {
|
2011-05-02 20:06:28 +00:00
|
|
|
// Ignore undef uses.
|
|
|
|
if (MO.isUndef())
|
|
|
|
continue;
|
2012-01-29 01:29:28 +00:00
|
|
|
if (!isPred && MO.isKill())
|
2009-08-08 13:18:47 +00:00
|
|
|
addRegWithSubRegs(KillRegs, Reg);
|
|
|
|
} else {
|
|
|
|
assert(MO.isDef());
|
2010-06-16 07:35:02 +00:00
|
|
|
if (!isPred && MO.isDead())
|
2012-01-29 01:29:28 +00:00
|
|
|
addRegWithSubRegs(KillRegs, Reg);
|
2009-08-08 13:18:47 +00:00
|
|
|
else
|
|
|
|
addRegWithSubRegs(DefRegs, Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
}
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2009-08-08 13:18:47 +00:00
|
|
|
// Verify uses and defs.
|
2012-01-29 01:29:28 +00:00
|
|
|
#ifndef NDEBUG
|
2009-08-08 13:18:47 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2011-05-02 20:36:53 +00:00
|
|
|
if (!MO.isReg())
|
2009-07-01 01:59:31 +00:00
|
|
|
continue;
|
2009-08-08 13:18:47 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-07-30 00:57:25 +00:00
|
|
|
if (!Reg || isReserved(Reg))
|
2007-03-02 10:43:16 +00:00
|
|
|
continue;
|
2009-08-08 13:18:47 +00:00
|
|
|
if (MO.isUse()) {
|
2011-05-02 20:36:53 +00:00
|
|
|
if (MO.isUndef())
|
|
|
|
continue;
|
2009-10-26 04:56:07 +00: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;
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
|
|
|
|
if (isUsed(*SubRegs)) {
|
2009-10-26 04:56:07 +00:00
|
|
|
SubUsed = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-16 20:38:31 +00:00
|
|
|
if (!SubUsed) {
|
|
|
|
MBB->getParent()->verify(NULL, "In Register Scavenger");
|
|
|
|
llvm_unreachable("Using an undefined register!");
|
|
|
|
}
|
2011-08-12 14:54:45 +00:00
|
|
|
(void)SubUsed;
|
2009-10-26 04:56:07 +00:00
|
|
|
}
|
2009-08-08 13:18:47 +00:00
|
|
|
} 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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82677 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-24 02:27:09 +00: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>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78466 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-08 13:19:10 +00:00
|
|
|
assert((KillRegs.test(Reg) || isUnused(Reg) ||
|
2009-08-08 13:18:47 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82677 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-24 02:27:09 +00:00
|
|
|
#endif
|
2007-03-02 10:43:16 +00:00
|
|
|
}
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
2012-01-29 01:29:28 +00:00
|
|
|
#endif // NDEBUG
|
2009-08-08 13:18:47 +00:00
|
|
|
|
|
|
|
// Commit the changes.
|
|
|
|
setUnused(KillRegs);
|
|
|
|
setUsed(DefRegs);
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 21:35:06 +00:00
|
|
|
void RegScavenger::getRegsUsed(BitVector &used, bool includeReserved) {
|
2012-01-29 01:29:25 +00:00
|
|
|
used = RegsAvailable;
|
|
|
|
used.flip();
|
2012-02-23 01:13:32 +00:00
|
|
|
if (includeReserved)
|
2012-10-15 21:57:41 +00:00
|
|
|
used |= MRI->getReservedRegs();
|
2012-02-23 01:13:32 +00:00
|
|
|
else
|
2012-10-15 21:57:41 +00:00
|
|
|
used.reset(MRI->getReservedRegs());
|
2007-03-20 21:35:06 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 21:14:54 +00:00
|
|
|
unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
|
|
|
|
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
|
|
|
|
I != E; ++I)
|
2010-09-02 00:51:37 +00:00
|
|
|
if (!isAliasUsed(*I)) {
|
|
|
|
DEBUG(dbgs() << "Scavenger found unused reg: " << TRI->getName(*I) <<
|
|
|
|
"\n");
|
2009-08-18 21:14:54 +00:00
|
|
|
return *I;
|
2010-09-02 00:51:37 +00:00
|
|
|
}
|
2009-08-18 21:14:54 +00:00
|
|
|
return 0;
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
2007-03-06 10:01:25 +00:00
|
|
|
|
2010-07-08 00:38:54 +00:00
|
|
|
/// getRegsAvailable - Return all available registers in the register class
|
|
|
|
/// in Mask.
|
2011-03-05 00:20:19 +00:00
|
|
|
BitVector RegScavenger::getRegsAvailable(const TargetRegisterClass *RC) {
|
|
|
|
BitVector Mask(TRI->getNumRegs());
|
2010-07-08 00:38:54 +00:00
|
|
|
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (!isAliasUsed(*I))
|
|
|
|
Mask.set(*I);
|
2011-03-05 00:20:19 +00:00
|
|
|
return Mask;
|
2010-07-08 00:38:54 +00:00
|
|
|
}
|
|
|
|
|
2009-08-16 17:41:39 +00:00
|
|
|
/// findSurvivorReg - Return the candidate register that is unused for the
|
2010-07-08 00:38:54 +00:00
|
|
|
/// longest after StargMII. UseMI is set to the instruction where the search
|
2009-08-16 17:41:39 +00:00
|
|
|
/// stopped.
|
|
|
|
///
|
|
|
|
/// No more than InstrLimit instructions are inspected.
|
|
|
|
///
|
2009-10-25 00:45:07 +00:00
|
|
|
unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI,
|
2009-08-16 17:41:39 +00: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 00:45:07 +00:00
|
|
|
assert(StartMI != ME && "MI already at terminator");
|
|
|
|
MachineBasicBlock::iterator RestorePointMI = StartMI;
|
|
|
|
MachineBasicBlock::iterator MI = StartMI;
|
2009-08-16 17:41:39 +00:00
|
|
|
|
2009-10-25 00:45:07 +00:00
|
|
|
bool inVirtLiveRange = false;
|
2009-08-16 17:41:39 +00:00
|
|
|
for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
|
2010-06-04 20:18:30 +00:00
|
|
|
if (MI->isDebugValue()) {
|
|
|
|
++InstrLimit; // Don't count debug instructions
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-25 00:45:07 +00:00
|
|
|
bool isVirtKillInsn = false;
|
|
|
|
bool isVirtDefInsn = false;
|
2009-08-16 17:41:39 +00:00
|
|
|
// Remove any candidates touched by instruction.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2012-02-22 22:50:14 +00:00
|
|
|
if (MO.isRegMask())
|
|
|
|
Candidates.clearBitsNotInMask(MO.getRegMask());
|
2009-10-25 00:45:07 +00:00
|
|
|
if (!MO.isReg() || MO.isUndef() || !MO.getReg())
|
2009-08-16 17:41:39 +00:00
|
|
|
continue;
|
2009-10-25 00:45:07 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
|
|
|
|
if (MO.isDef())
|
|
|
|
isVirtDefInsn = true;
|
|
|
|
else if (MO.isKill())
|
|
|
|
isVirtKillInsn = true;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-01 22:38:17 +00:00
|
|
|
for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
|
|
|
|
Candidates.reset(*AI);
|
2009-08-16 17:41:39 +00:00
|
|
|
}
|
2009-10-25 00:45:07 +00: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 06:25:12 +00:00
|
|
|
|
2009-08-16 17:41:39 +00:00
|
|
|
// Was our survivor untouched by this instruction?
|
|
|
|
if (Candidates.test(Survivor))
|
2009-08-11 06:25:12 +00:00
|
|
|
continue;
|
2009-08-16 17:41:39 +00:00
|
|
|
|
|
|
|
// All candidates gone?
|
|
|
|
if (Candidates.none())
|
|
|
|
break;
|
|
|
|
|
|
|
|
Survivor = Candidates.find_first();
|
2007-03-06 10:01:25 +00:00
|
|
|
}
|
2009-10-25 00:45:07 +00: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 17:41:39 +00:00
|
|
|
|
|
|
|
// We ran out of candidates, so stop the search.
|
2009-10-25 00:45:07 +00:00
|
|
|
UseMI = RestorePointMI;
|
2009-08-16 17:41:39 +00:00
|
|
|
return Survivor;
|
2007-03-06 10:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
|
2007-05-01 09:01:42 +00:00
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
int SPAdj) {
|
2010-09-02 18:29:04 +00:00
|
|
|
// Consider all allocatable registers in the register class initially
|
|
|
|
BitVector Candidates =
|
|
|
|
TRI->getAllocatableSet(*I->getParent()->getParent(), RC);
|
2007-03-06 10:01:25 +00: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 01:47:59 +00:00
|
|
|
if (MO.isReg() && MO.getReg() != 0 &&
|
|
|
|
!TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
2007-03-06 10:01:25 +00:00
|
|
|
Candidates.reset(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2010-07-08 16:49:26 +00:00
|
|
|
// Try to find a register that's unused if there is one, as then we won't
|
2011-03-05 00:20:19 +00:00
|
|
|
// have to spill. Search explicitly rather than masking out based on
|
|
|
|
// RegsAvailable, as RegsAvailable does not take aliases into account.
|
|
|
|
// That's what getRegsAvailable() is for.
|
|
|
|
BitVector Available = getRegsAvailable(RC);
|
2012-01-29 01:29:25 +00:00
|
|
|
Available &= Candidates;
|
|
|
|
if (Available.any())
|
|
|
|
Candidates = Available;
|
2010-07-08 16:49:26 +00:00
|
|
|
|
2008-02-16 01:09:25 +00:00
|
|
|
// Find the register whose use is furthest away.
|
2009-08-16 17:41:39 +00:00
|
|
|
MachineBasicBlock::iterator UseMI;
|
|
|
|
unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
|
2009-08-11 06:25:12 +00:00
|
|
|
|
2010-07-08 16:49:26 +00:00
|
|
|
// If we found an unused register there is no reason to spill it.
|
2010-09-02 00:51:37 +00:00
|
|
|
if (!isAliasUsed(SReg)) {
|
|
|
|
DEBUG(dbgs() << "Scavenged register: " << TRI->getName(SReg) << "\n");
|
2009-08-16 17:41:39 +00:00
|
|
|
return SReg;
|
2010-09-02 00:51:37 +00:00
|
|
|
}
|
2007-03-06 10:01:25 +00:00
|
|
|
|
2009-08-02 20:29:41 +00:00
|
|
|
assert(ScavengedReg == 0 &&
|
2009-07-12 20:07:01 +00:00
|
|
|
"Scavenger slot is live, unable to scavenge another register!");
|
2007-03-06 10:01:25 +00:00
|
|
|
|
2009-08-06 16:32:47 +00:00
|
|
|
// Avoid infinite regress
|
|
|
|
ScavengedReg = SReg;
|
|
|
|
|
2009-10-05 22:30:23 +00:00
|
|
|
// If the target knows how to save/restore the register, let it do so;
|
|
|
|
// otherwise, use the emergency stack spill slot.
|
2009-10-19 22:27:30 +00:00
|
|
|
if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) {
|
2009-10-05 22:30:23 +00:00
|
|
|
// Spill the scavenged register before I.
|
|
|
|
assert(ScavengingFrameIndex >= 0 &&
|
2009-10-06 15:03:44 +00:00
|
|
|
"Cannot scavenge register without an emergency spill slot!");
|
2010-05-06 19:06:44 +00:00
|
|
|
TII->storeRegToStackSlot(*MBB, I, SReg, true, ScavengingFrameIndex, RC,TRI);
|
2009-10-05 22:30:23 +00:00
|
|
|
MachineBasicBlock::iterator II = prior(I);
|
2010-08-26 23:32:16 +00:00
|
|
|
TRI->eliminateFrameIndex(II, SPAdj, this);
|
2009-10-05 22:30:23 +00:00
|
|
|
|
|
|
|
// Restore the scavenged register before its use (or first terminator).
|
2010-05-06 19:06:44 +00:00
|
|
|
TII->loadRegFromStackSlot(*MBB, UseMI, SReg, ScavengingFrameIndex, RC, TRI);
|
2009-10-21 22:59:56 +00:00
|
|
|
II = prior(UseMI);
|
2010-08-26 23:32:16 +00:00
|
|
|
TRI->eliminateFrameIndex(II, SPAdj, this);
|
2009-10-19 22:27:30 +00:00
|
|
|
}
|
2008-11-20 02:32:35 +00:00
|
|
|
|
2009-08-16 17:41:39 +00:00
|
|
|
ScavengeRestore = prior(UseMI);
|
2009-10-05 22:30:23 +00:00
|
|
|
|
2009-08-07 22:39:43 +00:00
|
|
|
// Doing this here leads to infinite regress.
|
2009-08-06 16:32:47 +00:00
|
|
|
// ScavengedReg = SReg;
|
2007-03-06 10:01:25 +00:00
|
|
|
ScavengedRC = RC;
|
|
|
|
|
2010-09-02 00:51:37 +00:00
|
|
|
DEBUG(dbgs() << "Scavenged register (with spill): " << TRI->getName(SReg) <<
|
|
|
|
"\n");
|
|
|
|
|
2007-03-06 10:01:25 +00:00
|
|
|
return SReg;
|
|
|
|
}
|