freebsd-dev/lib/CodeGen/RegisterScavenging.cpp

442 lines
14 KiB
C++
Raw Normal View History

2009-06-02 17:52:33 +00:00
//===-- RegisterScavenging.cpp - Machine register scavenging --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the machine register scavenger. It can provide
// 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.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "reg-scavenging"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
2009-10-14 17:57:32 +00:00
#include "llvm/CodeGen/MachineFrameInfo.h"
2009-06-02 17:52:33 +00:00
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/Debug.h"
2009-10-14 17:57:32 +00:00
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
2009-06-02 17:52:33 +00:00
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegisterInfo.h"
2009-06-02 17:52:33 +00:00
using namespace llvm;
/// setUsed - Set the register and its sub-registers as being used.
2009-07-04 13:58:26 +00:00
void RegScavenger::setUsed(unsigned Reg) {
for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
SubRegs.isValid(); ++SubRegs)
RegsAvailable.reset(*SubRegs);
2009-06-02 17:52:33 +00:00
}
2009-10-14 17:57:32 +00:00
bool RegScavenger::isAliasUsed(unsigned Reg) const {
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
if (isUsed(*AI, *AI == Reg))
2009-10-14 17:57:32 +00:00
return true;
return false;
}
2009-06-02 17:52:33 +00:00
2009-10-14 17:57:32 +00:00
void RegScavenger::initRegState() {
for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(),
IE = Scavenged.end(); I != IE; ++I) {
I->Reg = 0;
I->Restore = NULL;
}
2009-10-14 17:57:32 +00:00
// All registers started out unused.
RegsAvailable.set();
if (!MBB)
return;
// Live-in registers are in use.
2010-05-04 16:11:02 +00:00
for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
2009-10-14 17:57:32 +00:00
E = MBB->livein_end(); I != E; ++I)
setUsed(*I);
// 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-06-02 17:52:33 +00:00
}
void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
MachineFunction &MF = *mbb->getParent();
const TargetMachine &TM = MF.getTarget();
TII = TM.getInstrInfo();
TRI = TM.getRegisterInfo();
MRI = &MF.getRegInfo();
assert((NumPhysRegs == 0 || NumPhysRegs == TRI->getNumRegs()) &&
"Target changed?");
// 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-10-14 17:57:32 +00:00
// Self-initialize.
2009-06-02 17:52:33 +00:00
if (!MBB) {
NumPhysRegs = TRI->getNumRegs();
RegsAvailable.resize(NumPhysRegs);
KillRegs.resize(NumPhysRegs);
DefRegs.resize(NumPhysRegs);
2009-06-02 17:52:33 +00:00
// Create callee-saved registers bitvector.
CalleeSavedRegs.resize(NumPhysRegs);
const uint16_t *CSRegs = TRI->getCalleeSavedRegs(&MF);
2009-06-02 17:52:33 +00:00
if (CSRegs != NULL)
for (unsigned i = 0; CSRegs[i]; ++i)
CalleeSavedRegs.set(CSRegs[i]);
}
2009-11-18 14:58:34 +00:00
MBB = mbb;
initRegState();
2009-06-02 17:52:33 +00:00
Tracking = false;
}
2009-10-14 17:57:32 +00:00
void RegScavenger::addRegWithSubRegs(BitVector &BV, unsigned Reg) {
for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
SubRegs.isValid(); ++SubRegs)
BV.set(*SubRegs);
2009-06-02 17:52:33 +00:00
}
void RegScavenger::determineKillsAndDefs() {
assert(Tracking && "Must be tracking to determine kills and defs");
2009-06-02 17:52:33 +00:00
MachineInstr *MI = MBBI;
assert(!MI->isDebugValue() && "Debug values have no kills or defs");
2010-05-04 16:11:02 +00:00
2009-10-14 17:57:32 +00:00
// Find out which registers are early clobbered, killed, defined, and marked
// def-dead in this instruction.
2010-07-13 17:19:57 +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);
KillRegs.reset();
DefRegs.reset();
2009-06-02 17:52:33 +00:00
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
if (MO.isRegMask())
(isPred ? DefRegs : KillRegs).setBitsNotInMask(MO.getRegMask());
if (!MO.isReg())
2009-06-02 17:52:33 +00:00
continue;
unsigned Reg = MO.getReg();
if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) || isReserved(Reg))
2009-07-04 13:58:26 +00:00
continue;
2009-06-02 17:52:33 +00:00
2009-10-14 17:57:32 +00:00
if (MO.isUse()) {
// Ignore undef uses.
if (MO.isUndef())
continue;
if (!isPred && MO.isKill())
2009-10-14 17:57:32 +00:00
addRegWithSubRegs(KillRegs, Reg);
} else {
assert(MO.isDef());
2010-07-13 17:19:57 +00:00
if (!isPred && MO.isDead())
addRegWithSubRegs(KillRegs, Reg);
2009-10-14 17:57:32 +00:00
else
addRegWithSubRegs(DefRegs, Reg);
2009-06-02 17:52:33 +00:00
}
}
}
void RegScavenger::unprocess() {
assert(Tracking && "Cannot unprocess because we're not tracking");
MachineInstr *MI = MBBI;
if (!MI->isDebugValue()) {
determineKillsAndDefs();
// Commit the changes.
setUsed(KillRegs);
setUnused(DefRegs);
}
if (MBBI == MBB->begin()) {
MBBI = MachineBasicBlock::iterator(NULL);
Tracking = false;
} else
--MBBI;
}
void RegScavenger::forward() {
// Move ptr forward.
if (!Tracking) {
MBBI = MBB->begin();
Tracking = true;
} else {
assert(MBBI != MBB->end() && "Already past the end of the basic block!");
MBBI = llvm::next(MBBI);
}
assert(MBBI != MBB->end() && "Already at the end of the basic block!");
MachineInstr *MI = MBBI;
for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(),
IE = Scavenged.end(); I != IE; ++I) {
if (I->Restore != MI)
continue;
I->Reg = 0;
I->Restore = NULL;
}
if (MI->isDebugValue())
return;
determineKillsAndDefs();
2009-06-02 17:52:33 +00:00
2009-10-14 17:57:32 +00:00
// Verify uses and defs.
#ifndef NDEBUG
2009-06-02 17:52:33 +00:00
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
if (!MO.isReg())
2009-06-02 17:52:33 +00:00
continue;
unsigned Reg = MO.getReg();
if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) || isReserved(Reg))
2009-10-14 17:57:32 +00:00
continue;
if (MO.isUse()) {
if (MO.isUndef())
continue;
2009-11-04 14:58:56 +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;
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
if (isUsed(*SubRegs)) {
2009-11-04 14:58:56 +00:00
SubUsed = true;
break;
}
if (!SubUsed) {
MBB->getParent()->verify(NULL, "In Register Scavenger");
llvm_unreachable("Using an undefined register!");
}
(void)SubUsed;
2009-11-04 14:58:56 +00:00
}
2009-10-14 17:57:32 +00:00
} else {
assert(MO.isDef());
#if 0
// FIXME: Enable this once we've figured out how to correctly transfer
// implicit kills during codegen passes like the coalescer.
assert((KillRegs.test(Reg) || isUnused(Reg) ||
isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
"Re-defining a live register!");
#endif
}
2009-06-02 17:52:33 +00:00
}
#endif // NDEBUG
2009-06-02 17:52:33 +00:00
2009-10-14 17:57:32 +00:00
// Commit the changes.
setUnused(KillRegs);
setUsed(DefRegs);
2009-06-02 17:52:33 +00:00
}
void RegScavenger::getRegsUsed(BitVector &used, bool includeReserved) {
used = RegsAvailable;
used.flip();
2009-06-02 17:52:33 +00:00
if (includeReserved)
used |= MRI->getReservedRegs();
2009-06-02 17:52:33 +00:00
else
used.reset(MRI->getReservedRegs());
2009-06-02 17:52:33 +00:00
}
2009-10-14 17:57:32 +00:00
unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
I != E; ++I)
if (!isAliasUsed(*I)) {
DEBUG(dbgs() << "Scavenger found unused reg: " << TRI->getName(*I) <<
"\n");
2009-10-14 17:57:32 +00:00
return *I;
}
2009-10-14 17:57:32 +00:00
return 0;
2009-06-02 17:52:33 +00:00
}
2010-07-13 17:19:57 +00:00
/// getRegsAvailable - Return all available registers in the register class
/// in Mask.
BitVector RegScavenger::getRegsAvailable(const TargetRegisterClass *RC) {
BitVector Mask(TRI->getNumRegs());
2010-07-13 17:19:57 +00:00
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
I != E; ++I)
if (!isAliasUsed(*I))
Mask.set(*I);
return Mask;
2010-07-13 17:19:57 +00:00
}
2009-10-14 17:57:32 +00:00
/// findSurvivorReg - Return the candidate register that is unused for the
2010-07-13 17:19:57 +00:00
/// longest after StargMII. UseMI is set to the instruction where the search
2009-10-14 17:57:32 +00:00
/// stopped.
///
/// No more than InstrLimit instructions are inspected.
///
2009-11-04 14:58:56 +00:00
unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI,
2009-10-14 17:57:32 +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-11-04 14:58:56 +00:00
assert(StartMI != ME && "MI already at terminator");
MachineBasicBlock::iterator RestorePointMI = StartMI;
MachineBasicBlock::iterator MI = StartMI;
2009-10-14 17:57:32 +00:00
2009-11-04 14:58:56 +00:00
bool inVirtLiveRange = false;
2009-10-14 17:57:32 +00:00
for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
2010-07-13 17:19:57 +00:00
if (MI->isDebugValue()) {
++InstrLimit; // Don't count debug instructions
continue;
}
2009-11-04 14:58:56 +00:00
bool isVirtKillInsn = false;
bool isVirtDefInsn = false;
2009-10-14 17:57:32 +00:00
// Remove any candidates touched by instruction.
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i);
if (MO.isRegMask())
Candidates.clearBitsNotInMask(MO.getRegMask());
2009-11-04 14:58:56 +00:00
if (!MO.isReg() || MO.isUndef() || !MO.getReg())
2009-10-14 17:57:32 +00:00
continue;
2009-11-04 14:58:56 +00:00
if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
if (MO.isDef())
isVirtDefInsn = true;
else if (MO.isKill())
isVirtKillInsn = true;
continue;
}
for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
Candidates.reset(*AI);
2009-10-14 17:57:32 +00:00
}
2009-11-04 14:58:56 +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-06-02 17:52:33 +00:00
2009-10-14 17:57:32 +00:00
// Was our survivor untouched by this instruction?
if (Candidates.test(Survivor))
2009-06-02 17:52:33 +00:00
continue;
2009-10-14 17:57:32 +00:00
// All candidates gone?
if (Candidates.none())
break;
Survivor = Candidates.find_first();
2009-06-02 17:52:33 +00:00
}
2009-11-04 14:58:56 +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-10-14 17:57:32 +00:00
// We ran out of candidates, so stop the search.
2009-11-04 14:58:56 +00:00
UseMI = RestorePointMI;
2009-10-14 17:57:32 +00:00
return Survivor;
2009-06-02 17:52:33 +00:00
}
static unsigned getFrameIndexOperandNum(MachineInstr *MI) {
unsigned i = 0;
while (!MI->getOperand(i).isFI()) {
++i;
assert(i < MI->getNumOperands() &&
"Instr doesn't have FrameIndex operand!");
}
return i;
}
2009-06-02 17:52:33 +00:00
unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
MachineBasicBlock::iterator I,
int SPAdj) {
// Consider all allocatable registers in the register class initially
BitVector Candidates =
TRI->getAllocatableSet(*I->getParent()->getParent(), RC);
2009-06-02 17:52:33 +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);
if (MO.isReg() && MO.getReg() != 0 && !(MO.isUse() && MO.isUndef()) &&
2009-10-14 17:57:32 +00:00
!TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2009-06-02 17:52:33 +00:00
Candidates.reset(MO.getReg());
}
2010-07-13 17:19:57 +00:00
// Try to find a register that's unused if there is one, as then we won't
// 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);
Available &= Candidates;
if (Available.any())
Candidates = Available;
2010-07-13 17:19:57 +00:00
2009-06-02 17:52:33 +00:00
// Find the register whose use is furthest away.
2009-10-14 17:57:32 +00:00
MachineBasicBlock::iterator UseMI;
unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
2009-06-02 17:52:33 +00:00
2010-07-13 17:19:57 +00:00
// If we found an unused register there is no reason to spill it.
if (!isAliasUsed(SReg)) {
DEBUG(dbgs() << "Scavenged register: " << TRI->getName(SReg) << "\n");
2009-10-14 17:57:32 +00:00
return SReg;
}
2009-06-02 17:52:33 +00:00
// Find an available scavenging slot.
unsigned SI;
for (SI = 0; SI < Scavenged.size(); ++SI)
if (Scavenged[SI].Reg == 0)
break;
if (SI == Scavenged.size()) {
// We need to scavenge a register but have no spill slot, the target
// must know how to do it (if not, we'll assert below).
Scavenged.push_back(ScavengedInfo());
}
2009-06-02 17:52:33 +00:00
2009-10-14 17:57:32 +00:00
// Avoid infinite regress
Scavenged[SI].Reg = SReg;
2009-10-14 17:57:32 +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-23 14:19:52 +00:00
if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) {
2009-10-14 17:57:32 +00:00
// Spill the scavenged register before I.
assert(Scavenged[SI].FrameIndex >= 0 &&
2009-10-14 17:57:32 +00:00
"Cannot scavenge register without an emergency spill slot!");
TII->storeRegToStackSlot(*MBB, I, SReg, true, Scavenged[SI].FrameIndex,
RC, TRI);
2009-10-14 17:57:32 +00:00
MachineBasicBlock::iterator II = prior(I);
unsigned FIOperandNum = getFrameIndexOperandNum(II);
TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this);
2009-10-14 17:57:32 +00:00
// Restore the scavenged register before its use (or first terminator).
TII->loadRegFromStackSlot(*MBB, UseMI, SReg, Scavenged[SI].FrameIndex,
RC, TRI);
2009-10-23 14:19:52 +00:00
II = prior(UseMI);
FIOperandNum = getFrameIndexOperandNum(II);
TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this);
2009-10-23 14:19:52 +00:00
}
2009-10-14 17:57:32 +00:00
Scavenged[SI].Restore = prior(UseMI);
2009-10-14 17:57:32 +00:00
// Doing this here leads to infinite regress.
// Scavenged[SI].Reg = SReg;
2009-06-02 17:52:33 +00:00
DEBUG(dbgs() << "Scavenged register (with spill): " << TRI->getName(SReg) <<
"\n");
2009-06-02 17:52:33 +00:00
return SReg;
}