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"
|
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/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2008-04-05 01:27:09 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-02-10 18:45:23 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-02-23 01:01:19 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-04-05 01:27:09 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-01-05 17:59:02 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-02-27 01:58:48 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-02-23 01:01:19 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-06 23:22:43 +00:00
|
|
|
/// RedefinesSuperRegPart - Return true if the specified register is redefining
|
|
|
|
/// part of a super-register.
|
|
|
|
static bool RedefinesSuperRegPart(const MachineInstr *MI, unsigned SubReg,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2008-03-07 20:12:54 +00:00
|
|
|
bool SeenSuperUse = false;
|
|
|
|
bool SeenSuperDef = false;
|
2008-03-06 23:22:43 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2009-06-30 08:49:04 +00:00
|
|
|
if (!MO.isReg() || MO.isUndef())
|
2008-03-06 23:22:43 +00:00
|
|
|
continue;
|
2008-05-27 11:50:51 +00:00
|
|
|
if (TRI->isSuperRegister(SubReg, MO.getReg())) {
|
2008-03-07 20:12:54 +00:00
|
|
|
if (MO.isUse())
|
|
|
|
SeenSuperUse = true;
|
|
|
|
else if (MO.isImplicit())
|
|
|
|
SeenSuperDef = true;
|
2008-05-27 11:50:51 +00:00
|
|
|
}
|
2008-03-06 23:22:43 +00:00
|
|
|
}
|
|
|
|
|
2008-03-07 20:12:54 +00:00
|
|
|
return SeenSuperDef && SeenSuperUse;
|
2008-03-06 23:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool RedefinesSuperRegPart(const MachineInstr *MI,
|
|
|
|
const MachineOperand &MO,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2008-10-03 15:45:36 +00:00
|
|
|
assert(MO.isReg() && MO.isDef() && "Not a register def!");
|
2008-03-06 23:22:43 +00:00
|
|
|
return RedefinesSuperRegPart(MI, MO.getReg(), TRI);
|
|
|
|
}
|
|
|
|
|
2009-08-02 18:28:41 +00:00
|
|
|
bool RegScavenger::isSuperRegUsed(unsigned Reg) const {
|
|
|
|
for (const unsigned *SuperRegs = TRI->getSuperRegisters(Reg);
|
|
|
|
unsigned SuperReg = *SuperRegs; ++SuperRegs)
|
|
|
|
if (isUsed(SuperReg))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2008-03-05 00:59:57 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
2009-07-01 08:19:36 +00:00
|
|
|
unsigned SubReg = *SubRegs; ++SubRegs)
|
2008-03-03 22:12:25 +00:00
|
|
|
RegsAvailable.reset(SubReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setUnused - Set the register and its sub-registers as being unused.
|
2008-03-06 23:22:43 +00:00
|
|
|
void RegScavenger::setUnused(unsigned Reg, const MachineInstr *MI) {
|
2008-03-03 22:12:25 +00:00
|
|
|
RegsAvailable.set(Reg);
|
|
|
|
|
2008-03-05 00:59:57 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
unsigned SubReg = *SubRegs; ++SubRegs)
|
2009-07-01 08:19:36 +00:00
|
|
|
if (!RedefinesSuperRegPart(MI, Reg, TRI))
|
2008-03-06 23:22:43 +00:00
|
|
|
RegsAvailable.set(SubReg);
|
2008-03-03 22:12:25 +00:00
|
|
|
}
|
|
|
|
|
2009-08-06 16:32:47 +00:00
|
|
|
void RegScavenger::initRegState() {
|
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
|
|
|
ScavengeRestore = NULL;
|
|
|
|
CurrDist = 0;
|
|
|
|
DistanceMap.clear();
|
|
|
|
|
|
|
|
// All registers started out unused.
|
|
|
|
RegsAvailable.set();
|
|
|
|
|
|
|
|
// Reserved registers are always used.
|
|
|
|
RegsAvailable ^= ReservedRegs;
|
|
|
|
|
|
|
|
// Live-in registers are in use.
|
2009-08-07 22:39:43 +00:00
|
|
|
if (!MBB || MBB->livein_empty())
|
|
|
|
return;
|
|
|
|
for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
|
|
|
|
E = MBB->livein_end(); I != E; ++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?");
|
|
|
|
|
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);
|
2007-02-27 22:58:43 +00:00
|
|
|
|
2007-03-01 02:19:39 +00:00
|
|
|
// Create reserved registers bitvector.
|
2008-03-05 00:59:57 +00:00
|
|
|
ReservedRegs = TRI->getReservedRegs(MF);
|
2007-03-01 02:19:39 +00:00
|
|
|
|
2007-02-27 22:58:43 +00:00
|
|
|
// Create callee-saved registers bitvector.
|
|
|
|
CalleeSavedRegs.resize(NumPhysRegs);
|
2008-03-05 00:59:57 +00:00
|
|
|
const unsigned *CSRegs = TRI->getCalleeSavedRegs();
|
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-08-07 22:39:43 +00:00
|
|
|
// RS used within emit{Pro,Epi}logue()
|
2009-08-06 16:32:47 +00:00
|
|
|
if (mbb != MBB) {
|
|
|
|
MBB = mbb;
|
|
|
|
initRegState();
|
|
|
|
}
|
2007-03-01 02:19:39 +00:00
|
|
|
|
|
|
|
Tracking = false;
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
|
2007-03-06 10:01:25 +00:00
|
|
|
void RegScavenger::restoreScavengedReg() {
|
2008-01-01 21:11:32 +00:00
|
|
|
TII->loadRegFromStackSlot(*MBB, MBBI, ScavengedReg,
|
2008-04-05 01:27:09 +00:00
|
|
|
ScavengingFrameIndex, ScavengedRC);
|
2007-03-06 10:01:25 +00:00
|
|
|
MachineBasicBlock::iterator II = prior(MBBI);
|
2008-03-05 00:59:57 +00:00
|
|
|
TRI->eliminateFrameIndex(II, 0, this);
|
2007-03-06 10:01:25 +00:00
|
|
|
setUsed(ScavengedReg);
|
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-23 21:55:04 +00:00
|
|
|
#ifndef NDEBUG
|
2008-04-05 01:27:09 +00:00
|
|
|
/// isLiveInButUnusedBefore - Return true if register is livein the MBB not
|
|
|
|
/// not used before it reaches the MI that defines register.
|
|
|
|
static bool isLiveInButUnusedBefore(unsigned Reg, MachineInstr *MI,
|
|
|
|
MachineBasicBlock *MBB,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
MachineRegisterInfo* MRI) {
|
|
|
|
// First check if register is livein.
|
|
|
|
bool isLiveIn = false;
|
|
|
|
for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
|
|
|
|
E = MBB->livein_end(); I != E; ++I)
|
|
|
|
if (Reg == *I || TRI->isSuperRegister(Reg, *I)) {
|
|
|
|
isLiveIn = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!isLiveIn)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Is there any use of it before the specified MI?
|
|
|
|
SmallPtrSet<MachineInstr*, 4> UsesInMBB;
|
|
|
|
for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
|
|
|
|
UE = MRI->use_end(); UI != UE; ++UI) {
|
2009-07-22 21:51:42 +00:00
|
|
|
MachineOperand &UseMO = UI.getOperand();
|
|
|
|
if (UseMO.isReg() && UseMO.isUndef())
|
|
|
|
continue;
|
2008-04-05 01:27:09 +00:00
|
|
|
MachineInstr *UseMI = &*UI;
|
|
|
|
if (UseMI->getParent() == MBB)
|
|
|
|
UsesInMBB.insert(UseMI);
|
|
|
|
}
|
|
|
|
if (UsesInMBB.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(), E = MI; I != E; ++I)
|
|
|
|
if (UsesInMBB.count(&*I))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-23 21:55:04 +00:00
|
|
|
#endif
|
2008-04-05 01:27:09 +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 {
|
|
|
|
assert(MBBI != MBB->end() && "Already at the end of the basic block!");
|
2007-02-27 01:58:48 +00:00
|
|
|
MBBI = next(MBBI);
|
2007-02-27 22:58:43 +00:00
|
|
|
}
|
2007-02-27 01:58:48 +00:00
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
MachineInstr *MI = MBBI;
|
2008-11-20 02:32:35 +00:00
|
|
|
DistanceMap.insert(std::make_pair(MI, CurrDist++));
|
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
|
|
|
|
2008-11-18 22:28:38 +00:00
|
|
|
// Separate register operands into 3 classes: uses, defs, earlyclobbers.
|
2008-11-18 22:56:19 +00:00
|
|
|
SmallVector<std::pair<const MachineOperand*,unsigned>, 4> UseMOs;
|
|
|
|
SmallVector<std::pair<const MachineOperand*,unsigned>, 4> DefMOs;
|
|
|
|
SmallVector<std::pair<const MachineOperand*,unsigned>, 4> EarlyClobberMOs;
|
2007-02-23 01:01:19 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2009-06-30 08:49:04 +00:00
|
|
|
if (!MO.isReg() || MO.getReg() == 0 || MO.isUndef())
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
2008-11-18 22:28:38 +00:00
|
|
|
if (MO.isUse())
|
2008-11-18 22:56:19 +00:00
|
|
|
UseMOs.push_back(std::make_pair(&MO,i));
|
2008-11-18 22:28:38 +00:00
|
|
|
else if (MO.isEarlyClobber())
|
2008-11-18 22:56:19 +00:00
|
|
|
EarlyClobberMOs.push_back(std::make_pair(&MO,i));
|
2009-08-07 22:39:43 +00:00
|
|
|
else {
|
|
|
|
assert(MO.isDef());
|
2008-11-18 22:56:19 +00:00
|
|
|
DefMOs.push_back(std::make_pair(&MO,i));
|
2009-08-07 22:39:43 +00:00
|
|
|
}
|
2008-11-18 22:28:38 +00:00
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-11-18 22:28:38 +00:00
|
|
|
// Process uses first.
|
2009-06-12 21:34:26 +00:00
|
|
|
BitVector KillRegs(NumPhysRegs);
|
2008-11-18 22:28:38 +00:00
|
|
|
for (unsigned i = 0, e = UseMOs.size(); i != e; ++i) {
|
2008-11-18 22:56:19 +00:00
|
|
|
const MachineOperand MO = *UseMOs[i].first;
|
2009-08-04 21:30:30 +00:00
|
|
|
unsigned Idx = UseMOs[i].second;
|
2007-02-23 01:01:19 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2009-08-07 22:39:43 +00:00
|
|
|
assert(isUsed(Reg) && "Using an undefined register!");
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2009-08-04 21:30:30 +00:00
|
|
|
// Two-address operands implicitly kill.
|
|
|
|
if ((MO.isKill() || MI->isRegTiedToDefOperand(Idx)) && !isReserved(Reg)) {
|
2009-06-12 21:34:26 +00:00
|
|
|
KillRegs.set(Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-11-18 22:28:38 +00:00
|
|
|
// Mark sub-registers as used.
|
2008-03-05 00:59:57 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
unsigned SubReg = *SubRegs; ++SubRegs)
|
2009-06-12 21:34:26 +00:00
|
|
|
KillRegs.set(SubReg);
|
2008-03-03 22:12:25 +00:00
|
|
|
}
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
// Change states of all registers after all the uses are processed to guard
|
|
|
|
// against multiple uses.
|
2009-06-12 21:34:26 +00:00
|
|
|
setUnused(KillRegs);
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-11-18 22:28:38 +00:00
|
|
|
// Process early clobber defs then process defs. We can have a early clobber
|
|
|
|
// that is dead, it should not conflict with a def that happens one "slot"
|
|
|
|
// (see InstrSlots in LiveIntervalAnalysis.h) later.
|
|
|
|
unsigned NumECs = EarlyClobberMOs.size();
|
|
|
|
unsigned NumDefs = DefMOs.size();
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-11-18 22:28:38 +00:00
|
|
|
for (unsigned i = 0, e = NumECs + NumDefs; i != e; ++i) {
|
|
|
|
const MachineOperand &MO = (i < NumECs)
|
2008-11-18 22:56:19 +00:00
|
|
|
? *EarlyClobberMOs[i].first : *DefMOs[i-NumECs].first;
|
2007-02-25 09:47:31 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2009-07-01 01:59:31 +00:00
|
|
|
if (MO.isUndef())
|
|
|
|
continue;
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2007-03-02 10:43:16 +00:00
|
|
|
// If it's dead upon def, then it is now free.
|
|
|
|
if (MO.isDead()) {
|
2008-03-06 23:22:43 +00:00
|
|
|
setUnused(Reg, MI);
|
2007-03-02 10:43:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-12-02 19:27:20 +00:00
|
|
|
// Skip if this is merely redefining part of a super-register.
|
2008-03-06 23:22:43 +00:00
|
|
|
if (RedefinesSuperRegPart(MI, MO, TRI))
|
|
|
|
continue;
|
|
|
|
|
2009-08-04 21:29:11 +00:00
|
|
|
assert((isReserved(Reg) || isUnused(Reg) || isSuperRegUsed(Reg) ||
|
2008-04-05 01:27:09 +00:00
|
|
|
isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
|
2007-07-05 07:05:38 +00:00
|
|
|
"Re-defining a live register!");
|
2009-07-01 08:19:36 +00:00
|
|
|
setUsed(Reg);
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-20 21:35:06 +00:00
|
|
|
void RegScavenger::getRegsUsed(BitVector &used, bool includeReserved) {
|
|
|
|
if (includeReserved)
|
2007-03-26 22:23:54 +00:00
|
|
|
used = ~RegsAvailable;
|
2007-03-20 21:35:06 +00:00
|
|
|
else
|
2007-03-26 22:23:54 +00:00
|
|
|
used = ~RegsAvailable & ~ReservedRegs;
|
2007-03-20 21:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 01:01:19 +00: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);
|
|
|
|
}
|
|
|
|
|
2007-03-01 08:56:24 +00:00
|
|
|
unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RegClass,
|
|
|
|
const BitVector &Candidates) const {
|
|
|
|
// Mask off the registers which are not in the TargetRegisterClass.
|
2007-03-26 22:23:54 +00:00
|
|
|
BitVector RegsAvailableCopy(NumPhysRegs, false);
|
|
|
|
CreateRegClassMask(RegClass, RegsAvailableCopy);
|
|
|
|
RegsAvailableCopy &= RegsAvailable;
|
2007-03-01 08:56:24 +00:00
|
|
|
|
|
|
|
// Restrict the search to candidates.
|
2007-03-26 22:23:54 +00:00
|
|
|
RegsAvailableCopy &= Candidates;
|
2007-03-01 08:56:24 +00:00
|
|
|
|
|
|
|
// Returns the first unused (bit is set) register, or 0 is none is found.
|
2007-03-26 22:23:54 +00:00
|
|
|
int Reg = RegsAvailableCopy.find_first();
|
2007-03-01 08:56:24 +00:00
|
|
|
return (Reg == -1) ? 0 : Reg;
|
|
|
|
}
|
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RegClass,
|
|
|
|
bool ExCalleeSaved) const {
|
|
|
|
// Mask off the registers which are not in the TargetRegisterClass.
|
2007-03-26 22:23:54 +00:00
|
|
|
BitVector RegsAvailableCopy(NumPhysRegs, false);
|
|
|
|
CreateRegClassMask(RegClass, RegsAvailableCopy);
|
|
|
|
RegsAvailableCopy &= RegsAvailable;
|
2007-02-23 01:01:19 +00:00
|
|
|
|
|
|
|
// If looking for a non-callee-saved register, mask off all the callee-saved
|
|
|
|
// registers.
|
|
|
|
if (ExCalleeSaved)
|
2007-03-26 22:23:54 +00:00
|
|
|
RegsAvailableCopy &= ~CalleeSavedRegs;
|
2007-02-23 01:01:19 +00:00
|
|
|
|
|
|
|
// Returns the first unused (bit is set) register, or 0 is none is found.
|
2007-03-26 22:23:54 +00:00
|
|
|
int Reg = RegsAvailableCopy.find_first();
|
2007-02-23 01:01:19 +00:00
|
|
|
return (Reg == -1) ? 0 : Reg;
|
|
|
|
}
|
2007-03-06 10:01:25 +00:00
|
|
|
|
2008-11-20 02:32:35 +00:00
|
|
|
/// findFirstUse - Calculate the distance to the first use of the
|
2007-03-06 10:01:25 +00:00
|
|
|
/// specified register.
|
2008-11-20 02:32:35 +00:00
|
|
|
MachineInstr*
|
|
|
|
RegScavenger::findFirstUse(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator I, unsigned Reg,
|
|
|
|
unsigned &Dist) {
|
|
|
|
MachineInstr *UseMI = 0;
|
|
|
|
Dist = ~0U;
|
|
|
|
for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg),
|
|
|
|
RE = MRI->reg_end(); RI != RE; ++RI) {
|
|
|
|
MachineInstr *UDMI = &*RI;
|
|
|
|
if (UDMI->getParent() != MBB)
|
|
|
|
continue;
|
|
|
|
DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
|
|
|
|
if (DI == DistanceMap.end()) {
|
|
|
|
// If it's not in map, it's below current MI, let's initialize the
|
|
|
|
// map.
|
|
|
|
I = next(I);
|
|
|
|
unsigned Dist = CurrDist + 1;
|
|
|
|
while (I != MBB->end()) {
|
|
|
|
DistanceMap.insert(std::make_pair(I, Dist++));
|
|
|
|
I = next(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DI = DistanceMap.find(UDMI);
|
|
|
|
if (DI->second > CurrDist && DI->second < Dist) {
|
|
|
|
Dist = DI->second;
|
|
|
|
UseMI = UDMI;
|
|
|
|
}
|
2007-03-06 10:01:25 +00:00
|
|
|
}
|
2008-11-20 02:32:35 +00:00
|
|
|
return UseMI;
|
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) {
|
2007-03-06 10:01:25 +00:00
|
|
|
assert(ScavengingFrameIndex >= 0 &&
|
|
|
|
"Cannot scavenge a register without an emergency spill slot!");
|
|
|
|
|
|
|
|
// Mask off the registers which are not in the TargetRegisterClass.
|
|
|
|
BitVector Candidates(NumPhysRegs, false);
|
|
|
|
CreateRegClassMask(RC, Candidates);
|
2009-08-06 16:32:47 +00:00
|
|
|
// Do not include reserved registers.
|
|
|
|
Candidates ^= ReservedRegs & Candidates;
|
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);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg())
|
2007-03-06 10:01:25 +00:00
|
|
|
Candidates.reset(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2008-02-16 01:09:25 +00:00
|
|
|
// Find the register whose use is furthest away.
|
2007-03-06 10:01:25 +00:00
|
|
|
unsigned SReg = 0;
|
|
|
|
unsigned MaxDist = 0;
|
2008-11-20 02:32:35 +00:00
|
|
|
MachineInstr *MaxUseMI = 0;
|
2007-03-06 10:01:25 +00:00
|
|
|
int Reg = Candidates.find_first();
|
|
|
|
while (Reg != -1) {
|
2008-11-20 02:32:35 +00:00
|
|
|
unsigned Dist;
|
|
|
|
MachineInstr *UseMI = findFirstUse(MBB, I, Reg, Dist);
|
|
|
|
for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
|
|
|
|
unsigned AsDist;
|
|
|
|
MachineInstr *AsUseMI = findFirstUse(MBB, I, *AS, AsDist);
|
|
|
|
if (AsDist < Dist) {
|
|
|
|
Dist = AsDist;
|
|
|
|
UseMI = AsUseMI;
|
|
|
|
}
|
|
|
|
}
|
2007-03-06 10:01:25 +00:00
|
|
|
if (Dist >= MaxDist) {
|
|
|
|
MaxDist = Dist;
|
2008-11-20 02:32:35 +00:00
|
|
|
MaxUseMI = UseMI;
|
2007-03-06 10:01:25 +00:00
|
|
|
SReg = Reg;
|
|
|
|
}
|
|
|
|
Reg = Candidates.find_next(Reg);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Make sure SReg is marked as used. It could be considered available
|
|
|
|
// if it is one of the callee saved registers, but hasn't been spilled.
|
2009-08-02 20:29:41 +00:00
|
|
|
if (!isUsed(SReg)) {
|
|
|
|
MBB->addLiveIn(SReg);
|
|
|
|
setUsed(SReg);
|
|
|
|
}
|
|
|
|
|
2008-11-20 02:32:35 +00:00
|
|
|
// Spill the scavenged register before I.
|
2008-01-01 21:11:32 +00:00
|
|
|
TII->storeRegToStackSlot(*MBB, I, SReg, true, ScavengingFrameIndex, RC);
|
2007-03-06 10:01:25 +00:00
|
|
|
MachineBasicBlock::iterator II = prior(I);
|
2008-03-05 00:59:57 +00:00
|
|
|
TRI->eliminateFrameIndex(II, SPAdj, this);
|
2008-11-20 02:32:35 +00:00
|
|
|
|
|
|
|
// Restore the scavenged register before its use (or first terminator).
|
|
|
|
II = MaxUseMI
|
|
|
|
? MachineBasicBlock::iterator(MaxUseMI) : MBB->getFirstTerminator();
|
|
|
|
TII->loadRegFromStackSlot(*MBB, II, SReg, ScavengingFrameIndex, RC);
|
|
|
|
ScavengeRestore = prior(II);
|
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;
|
|
|
|
|
|
|
|
return SReg;
|
|
|
|
}
|