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"
|
|
|
|
#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"
|
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"
|
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);
|
2008-03-07 20:12:54 +00:00
|
|
|
if (!MO.isRegister())
|
2008-03-06 23:22:43 +00:00
|
|
|
continue;
|
|
|
|
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-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) {
|
|
|
|
assert(MO.isRegister() && MO.isDef() && "Not a register def!");
|
|
|
|
return RedefinesSuperRegPart(MI, MO.getReg(), TRI);
|
|
|
|
}
|
|
|
|
|
2008-03-03 22:12:25 +00:00
|
|
|
/// setUsed - Set the register and its sub-registers as being used.
|
2008-04-10 23:47:53 +00:00
|
|
|
void RegScavenger::setUsed(unsigned Reg, bool ImpDef) {
|
2008-03-03 22:12:25 +00:00
|
|
|
RegsAvailable.reset(Reg);
|
2008-04-10 23:47:53 +00:00
|
|
|
ImplicitDefed[Reg] = ImpDef;
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-03-05 00:59:57 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
2008-04-10 23:47:53 +00:00
|
|
|
unsigned SubReg = *SubRegs; ++SubRegs) {
|
2008-03-03 22:12:25 +00:00
|
|
|
RegsAvailable.reset(SubReg);
|
2008-04-10 23:47:53 +00:00
|
|
|
ImplicitDefed[SubReg] = ImpDef;
|
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-04-10 23:47:53 +00:00
|
|
|
ImplicitDefed.reset(Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
|
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)
|
2008-04-10 23:47:53 +00:00
|
|
|
if (!RedefinesSuperRegPart(MI, Reg, TRI)) {
|
2008-03-06 23:22:43 +00:00
|
|
|
RegsAvailable.set(SubReg);
|
2008-04-10 23:47:53 +00:00
|
|
|
ImplicitDefed.reset(SubReg);
|
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 02:19:39 +00:00
|
|
|
void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
|
2007-02-27 22:58:43 +00:00
|
|
|
const 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?");
|
|
|
|
|
|
|
|
if (!MBB) {
|
2008-03-05 00:59:57 +00:00
|
|
|
NumPhysRegs = TRI->getNumRegs();
|
2007-03-26 22:23:54 +00:00
|
|
|
RegsAvailable.resize(NumPhysRegs);
|
2008-04-10 23:47:53 +00:00
|
|
|
ImplicitDefed.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();
|
2007-02-27 22:58:43 +00:00
|
|
|
if (CSRegs != NULL)
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
CalleeSavedRegs.set(CSRegs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
MBB = mbb;
|
2007-03-06 21:58:15 +00:00
|
|
|
ScavengedReg = 0;
|
|
|
|
ScavengedRC = NULL;
|
2007-02-27 22:58:43 +00:00
|
|
|
|
|
|
|
// All registers started out unused.
|
2007-03-26 22:23:54 +00:00
|
|
|
RegsAvailable.set();
|
2007-02-23 01:01:19 +00:00
|
|
|
|
2007-03-01 02:19:39 +00:00
|
|
|
// Reserved registers are always used.
|
2007-03-26 22:23:54 +00:00
|
|
|
RegsAvailable ^= ReservedRegs;
|
2007-02-23 01:01:19 +00:00
|
|
|
|
2007-02-23 08:41:19 +00:00
|
|
|
// Live-in registers are in use.
|
|
|
|
if (!MBB->livein_empty())
|
|
|
|
for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
|
|
|
|
E = MBB->livein_end(); I != E; ++I)
|
|
|
|
setUsed(*I);
|
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() {
|
|
|
|
if (!ScavengedReg)
|
|
|
|
return;
|
|
|
|
|
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-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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-01-07 07:27:27 +00:00
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
2007-03-06 10:01:25 +00:00
|
|
|
|
|
|
|
// Reaching a terminator instruction. Restore a scavenged register (which
|
|
|
|
// must be life out.
|
2008-01-07 07:27:27 +00:00
|
|
|
if (TID.isTerminator())
|
2007-03-06 10:01:25 +00:00
|
|
|
restoreScavengedReg();
|
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
// Process uses first.
|
|
|
|
BitVector ChangedRegs(NumPhysRegs);
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2007-09-14 20:33:02 +00:00
|
|
|
if (!MO.isRegister() || !MO.isUse())
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2008-03-03 22:12:25 +00:00
|
|
|
if (Reg == 0) continue;
|
|
|
|
|
2007-03-06 10:01:25 +00:00
|
|
|
if (!isUsed(Reg)) {
|
|
|
|
// Register has been scavenged. Restore it!
|
|
|
|
if (Reg != ScavengedReg)
|
2007-07-05 07:05:38 +00:00
|
|
|
assert(false && "Using an undefined register!");
|
2007-03-06 10:01:25 +00:00
|
|
|
else
|
|
|
|
restoreScavengedReg();
|
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
|
|
|
|
if (MO.isKill() && !isReserved(Reg)) {
|
2007-02-23 01:01:19 +00:00
|
|
|
ChangedRegs.set(Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-03-06 23:22:43 +00:00
|
|
|
// Mark sub-registers as changed if they aren't defined in the same
|
|
|
|
// instruction.
|
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)
|
2008-03-07 23:45:15 +00:00
|
|
|
ChangedRegs.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.
|
|
|
|
setUnused(ChangedRegs);
|
|
|
|
|
|
|
|
// Process defs.
|
2008-04-10 23:47:53 +00:00
|
|
|
bool IsImpDef = MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF;
|
2007-02-23 01:01:19 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2007-09-14 20:33:02 +00:00
|
|
|
if (!MO.isRegister() || !MO.isDef())
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2007-02-25 09:47:31 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
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
|
|
|
|
2007-02-23 01:01:19 +00:00
|
|
|
// Skip two-address destination operand.
|
2008-01-07 07:27:27 +00:00
|
|
|
if (TID.findTiedToSrcOperand(i) != -1) {
|
2007-07-05 07:05:38 +00:00
|
|
|
assert(isUsed(Reg) && "Using an undefined register!");
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
2007-02-25 09:47:31 +00:00
|
|
|
}
|
2008-03-03 22:12:25 +00:00
|
|
|
|
2008-03-06 23:22:43 +00:00
|
|
|
// Skip is this is merely redefining part of a super-register.
|
|
|
|
if (RedefinesSuperRegPart(MI, MO, TRI))
|
|
|
|
continue;
|
|
|
|
|
2008-04-10 23:47:53 +00:00
|
|
|
// Implicit def is allowed to "re-define" any register. Similarly,
|
|
|
|
// implicitly defined registers can be clobbered.
|
2008-04-05 01:27:09 +00:00
|
|
|
assert((isReserved(Reg) || isUnused(Reg) ||
|
2008-04-10 23:47:53 +00:00
|
|
|
IsImpDef || isImplicitlyDefined(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!");
|
2008-04-10 23:47:53 +00:00
|
|
|
setUsed(Reg, IsImpDef);
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegScavenger::backward() {
|
2007-02-27 22:58:43 +00:00
|
|
|
assert(Tracking && "Not tracking states!");
|
2007-02-27 01:58:48 +00:00
|
|
|
assert(MBBI != MBB->begin() && "Already at start of basic block!");
|
|
|
|
// Move ptr backward.
|
|
|
|
MBBI = prior(MBBI);
|
|
|
|
|
|
|
|
MachineInstr *MI = MBBI;
|
2007-02-23 01:01:19 +00:00
|
|
|
// Process defs first.
|
2008-01-07 07:27:27 +00:00
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
2007-02-23 01:01:19 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2007-09-14 20:33:02 +00:00
|
|
|
if (!MO.isRegister() || !MO.isDef())
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
|
|
|
// Skip two-address destination operand.
|
2008-01-07 07:27:27 +00:00
|
|
|
if (TID.findTiedToSrcOperand(i) != -1)
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
assert(isUsed(Reg));
|
|
|
|
if (!isReserved(Reg))
|
2008-03-06 23:22:43 +00:00
|
|
|
setUnused(Reg, MI);
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process uses.
|
|
|
|
BitVector ChangedRegs(NumPhysRegs);
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2007-09-14 20:33:02 +00:00
|
|
|
if (!MO.isRegister() || !MO.isUse())
|
2007-02-23 01:01:19 +00:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0)
|
|
|
|
continue;
|
|
|
|
assert(isUnused(Reg) || isReserved(Reg));
|
|
|
|
ChangedRegs.set(Reg);
|
2008-03-03 22:12:25 +00:00
|
|
|
|
|
|
|
// Set the 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)
|
|
|
|
ChangedRegs.set(SubReg);
|
2007-02-23 01:01:19 +00:00
|
|
|
}
|
|
|
|
setUsed(ChangedRegs);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/// calcDistanceToUse - Calculate the distance to the first use of the
|
|
|
|
/// specified register.
|
|
|
|
static unsigned calcDistanceToUse(MachineBasicBlock *MBB,
|
2008-03-05 00:59:57 +00:00
|
|
|
MachineBasicBlock::iterator I, unsigned Reg,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2007-03-06 10:01:25 +00:00
|
|
|
unsigned Dist = 0;
|
|
|
|
I = next(I);
|
|
|
|
while (I != MBB->end()) {
|
|
|
|
Dist++;
|
2008-03-05 00:59:57 +00:00
|
|
|
if (I->readsRegister(Reg, TRI))
|
2007-03-06 10:01:25 +00:00
|
|
|
return Dist;
|
|
|
|
I = next(I);
|
|
|
|
}
|
|
|
|
return Dist + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
Candidates ^= ReservedRegs; // Do not include reserved registers.
|
|
|
|
|
|
|
|
// Exclude all the registers being used by the instruction.
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = I->getOperand(i);
|
2007-09-14 20:33:02 +00:00
|
|
|
if (MO.isRegister())
|
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;
|
|
|
|
int Reg = Candidates.find_first();
|
|
|
|
while (Reg != -1) {
|
2008-03-05 00:59:57 +00:00
|
|
|
unsigned Dist = calcDistanceToUse(MBB, I, Reg, TRI);
|
2007-03-06 10:01:25 +00:00
|
|
|
if (Dist >= MaxDist) {
|
|
|
|
MaxDist = Dist;
|
|
|
|
SReg = Reg;
|
|
|
|
}
|
|
|
|
Reg = Candidates.find_next(Reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ScavengedReg != 0) {
|
|
|
|
// First restore previously scavenged register.
|
2008-01-01 21:11:32 +00:00
|
|
|
TII->loadRegFromStackSlot(*MBB, I, ScavengedReg,
|
2008-02-16 01:09:25 +00:00
|
|
|
ScavengingFrameIndex, ScavengedRC);
|
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);
|
2007-03-06 10:01:25 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2007-03-06 10:01:25 +00:00
|
|
|
ScavengedReg = SReg;
|
|
|
|
ScavengedRC = RC;
|
|
|
|
|
|
|
|
return SReg;
|
|
|
|
}
|