Replace RegScavenger::DistanceMap with a simpler local algorithm.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79195 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jakob Stoklund Olesen 2009-08-16 17:41:39 +00:00
parent e689ce626c
commit 66a39699fb
2 changed files with 52 additions and 67 deletions

View File

@ -150,6 +150,12 @@ private:
/// Add Reg and its aliases to BV. /// Add Reg and its aliases to BV.
void addRegWithAliases(BitVector &BV, unsigned Reg); void addRegWithAliases(BitVector &BV, unsigned Reg);
unsigned findSurvivorReg(MachineBasicBlock::iterator MI,
BitVector &Candidates,
unsigned InstrLimit,
MachineBasicBlock::iterator &UseMI);
}; };
} // End llvm namespace } // End llvm namespace

View File

@ -280,41 +280,47 @@ unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RegClass,
return (Reg == -1) ? 0 : Reg; return (Reg == -1) ? 0 : Reg;
} }
/// DistanceMap - Keep track the distance of an MI from the current position. /// findSurvivorReg - Return the candidate register that is unused for the
typedef DenseMap<MachineInstr*, unsigned> DistanceMap; /// longest after MBBI. UseMI is set to the instruction where the search
/// stopped.
///
/// No more than InstrLimit instructions are inspected.
///
unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator MI,
BitVector &Candidates,
unsigned InstrLimit,
MachineBasicBlock::iterator &UseMI) {
int Survivor = Candidates.find_first();
assert(Survivor > 0 && "No candidates for scavenging");
/// Build a distance map for instructions from I to E. MachineBasicBlock::iterator ME = MBB->getFirstTerminator();
static void buildDistanceMap(DistanceMap &DM, assert(MI != ME && "MI already at terminator");
MachineBasicBlock::iterator I,
MachineBasicBlock::iterator E) {
DM.clear();
for (unsigned d = 0; I != E; ++I, ++d)
DM.insert(DistanceMap::value_type(I, d));
}
/// findFirstUse - Calculate the distance to the first use of the for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
/// specified register in the range covered by DM. // Remove any candidates touched by instruction.
static MachineInstr *findFirstUse(const MachineBasicBlock *MBB, for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const DistanceMap &DM, const MachineOperand &MO = MI->getOperand(i);
unsigned Reg, if (!MO.isReg() || MO.isUndef() || !MO.getReg())
unsigned &Dist) { continue;
const MachineRegisterInfo *MRI = &MBB->getParent()->getRegInfo(); Candidates.reset(MO.getReg());
MachineInstr *UseMI = 0; for (const unsigned *R = TRI->getAliasSet(MO.getReg()); *R; R++)
Dist = ~0U; Candidates.reset(*R);
for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(Reg),
RE = MRI->reg_end(); RI != RE; ++RI) {
MachineInstr *UDMI = &*RI;
if (UDMI->getParent() != MBB)
continue;
DistanceMap::const_iterator DI = DM.find(UDMI);
if (DI == DM.end())
continue;
if (DI->second < Dist) {
Dist = DI->second;
UseMI = UDMI;
} }
// Was our survivor untouched by this instruction?
if (Candidates.test(Survivor))
continue;
// All candidates gone?
if (Candidates.none())
break;
Survivor = Candidates.find_first();
} }
return UseMI;
// We ran out of candidates, so stop the search.
UseMI = MI;
return Survivor;
} }
unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC, unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
@ -336,40 +342,15 @@ unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
Candidates.reset(MO.getReg()); Candidates.reset(MO.getReg());
} }
// Prepare to call findFirstUse() a number of times.
DistanceMap DM;
buildDistanceMap(DM, I, MBB->end());
// Find the register whose use is furthest away. // Find the register whose use is furthest away.
unsigned SReg = 0; MachineBasicBlock::iterator UseMI;
unsigned MaxDist = 0; unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
MachineInstr *MaxUseMI = 0;
int Reg = Candidates.find_first();
while (Reg != -1) {
unsigned Dist;
MachineInstr *UseMI = findFirstUse(MBB, DM, Reg, Dist);
for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
unsigned AsDist;
MachineInstr *AsUseMI = findFirstUse(MBB, DM, *AS, AsDist);
if (AsDist < Dist) {
Dist = AsDist;
UseMI = AsUseMI;
}
}
// If we found an unused register there is no reason to spill it. We have // 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 // probably found a callee-saved register that has been saved in the
// prologue, but happens to be unused at this point. // prologue, but happens to be unused at this point.
if (!isAliasUsed(Reg)) if (!isAliasUsed(SReg))
return Reg; return SReg;
if (Dist >= MaxDist) {
MaxDist = Dist;
MaxUseMI = UseMI;
SReg = Reg;
}
Reg = Candidates.find_next(Reg);
}
assert(ScavengedReg == 0 && assert(ScavengedReg == 0 &&
"Scavenger slot is live, unable to scavenge another register!"); "Scavenger slot is live, unable to scavenge another register!");
@ -383,10 +364,8 @@ unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
TRI->eliminateFrameIndex(II, SPAdj, this); TRI->eliminateFrameIndex(II, SPAdj, this);
// Restore the scavenged register before its use (or first terminator). // Restore the scavenged register before its use (or first terminator).
II = MaxUseMI TII->loadRegFromStackSlot(*MBB, UseMI, SReg, ScavengingFrameIndex, RC);
? MachineBasicBlock::iterator(MaxUseMI) : MBB->getFirstTerminator(); ScavengeRestore = prior(UseMI);
TII->loadRegFromStackSlot(*MBB, II, SReg, ScavengingFrameIndex, RC);
ScavengeRestore = prior(II);
// Doing this here leads to infinite regress. // Doing this here leads to infinite regress.
// ScavengedReg = SReg; // ScavengedReg = SReg;
ScavengedRC = RC; ScavengedRC = RC;