Another nice speedup for the register allocator. This time, we replace

the Virt2PhysRegMap std::map with an std::vector.  This speeds up the
register allocator another (almost) 40%, from .72->.45s in a release build
of LLC on 253.perlbmk.

llvm-svn: 11219
This commit is contained in:
Chris Lattner 2004-02-09 02:12:04 +00:00
parent 56f9b190e1
commit 80cbed4f61
1 changed files with 55 additions and 37 deletions

View File

@ -44,9 +44,26 @@ namespace {
std::map<unsigned, int> StackSlotForVirtReg; std::map<unsigned, int> StackSlotForVirtReg;
// Virt2PhysRegMap - This map contains entries for each virtual register // Virt2PhysRegMap - This map contains entries for each virtual register
// that is currently available in a physical register. // that is currently available in a physical register. This is "logically"
// a map from virtual register numbers to physical register numbers.
// Instead of using a map, however, which is slow, we use a vector. The
// index is the VREG number - FirstVirtualRegister. If the entry is zero,
// then it is logically "not in the map".
// //
std::map<unsigned, unsigned> Virt2PhysRegMap; std::vector<unsigned> Virt2PhysRegMap;
unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
assert(VirtReg >= MRegisterInfo::FirstVirtualRegister &&"Illegal VREG #");
assert(VirtReg-MRegisterInfo::FirstVirtualRegister <Virt2PhysRegMap.size()
&& "VirtReg not in map!");
return Virt2PhysRegMap[VirtReg-MRegisterInfo::FirstVirtualRegister];
}
unsigned &getOrInsertVirt2PhysRegMapSlot(unsigned VirtReg) {
assert(VirtReg >= MRegisterInfo::FirstVirtualRegister &&"Illegal VREG #");
if (VirtReg-MRegisterInfo::FirstVirtualRegister >= Virt2PhysRegMap.size())
Virt2PhysRegMap.resize(VirtReg-MRegisterInfo::FirstVirtualRegister+1);
return Virt2PhysRegMap[VirtReg-MRegisterInfo::FirstVirtualRegister];
}
// PhysRegsUsed - This array is effectively a map, containing entries for // PhysRegsUsed - This array is effectively a map, containing entries for
// each physical register that currently has a value (ie, it is in // each physical register that currently has a value (ie, it is in
@ -263,7 +280,8 @@ void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC); RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
++NumSpilled; // Update statistics ++NumSpilled; // Update statistics
} }
Virt2PhysRegMap.erase(VirtReg); // VirtReg no longer available
getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
DEBUG(std::cerr << "\n"); DEBUG(std::cerr << "\n");
removePhysReg(PhysReg); removePhysReg(PhysReg);
@ -301,7 +319,7 @@ void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
// Update information to note the fact that this register was just used, and // Update information to note the fact that this register was just used, and
// it holds VirtReg. // it holds VirtReg.
PhysRegsUsed[PhysReg] = VirtReg; PhysRegsUsed[PhysReg] = VirtReg;
Virt2PhysRegMap[VirtReg] = PhysReg; getOrInsertVirt2PhysRegMapSlot(VirtReg) = PhysReg;
PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
} }
@ -371,7 +389,7 @@ void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
// Update our internal state to indicate that PhysReg is available and Reg // Update our internal state to indicate that PhysReg is available and Reg
// isn't. // isn't.
Virt2PhysRegMap.erase(VirtReg); getVirt2PhysRegMapSlot[VirtReg] = 0;
removePhysReg(PhysReg); // Free the physreg removePhysReg(PhysReg); // Free the physreg
// Move reference over to new register... // Move reference over to new register...
@ -453,10 +471,9 @@ unsigned RA::getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
unsigned RA::reloadVirtReg(MachineBasicBlock &MBB, unsigned RA::reloadVirtReg(MachineBasicBlock &MBB,
MachineBasicBlock::iterator &I, MachineBasicBlock::iterator &I,
unsigned VirtReg) { unsigned VirtReg) {
std::map<unsigned, unsigned>::iterator It = Virt2PhysRegMap.find(VirtReg); if (unsigned PR = getOrInsertVirt2PhysRegMapSlot(VirtReg)) {
if (It != Virt2PhysRegMap.end()) { MarkPhysRegRecentlyUsed(PR);
MarkPhysRegRecentlyUsed(It->second); return PR; // Already have this value available!
return It->second; // Already have this value available!
} }
unsigned PhysReg = getReg(MBB, I, VirtReg); unsigned PhysReg = getReg(MBB, I, VirtReg);
@ -522,11 +539,10 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
unsigned VirtReg = KI->second; unsigned VirtReg = KI->second;
unsigned PhysReg = VirtReg; unsigned PhysReg = VirtReg;
if (MRegisterInfo::isVirtualRegister(VirtReg)) { if (MRegisterInfo::isVirtualRegister(VirtReg)) {
std::map<unsigned, unsigned>::iterator I = unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
Virt2PhysRegMap.find(VirtReg); PhysReg = PhysRegSlot;
assert(I != Virt2PhysRegMap.end()); assert(PhysReg != 0);
PhysReg = I->second; PhysRegSlot = 0;
Virt2PhysRegMap.erase(I);
} }
if (PhysReg) { if (PhysReg) {
@ -579,14 +595,8 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
unsigned DestPhysReg; unsigned DestPhysReg;
// If DestVirtReg already has a value, use it. // If DestVirtReg already has a value, use it.
std::map<unsigned, unsigned>::iterator DestI = if (!(DestPhysReg = getOrInsertVirt2PhysRegMapSlot(DestVirtReg)))
Virt2PhysRegMap.find(DestVirtReg);
if (DestI != Virt2PhysRegMap.end()) {
DestPhysReg = DestI->second;
}
else {
DestPhysReg = getReg(MBB, I, DestVirtReg); DestPhysReg = getReg(MBB, I, DestVirtReg);
}
markVirtRegModified(DestVirtReg); markVirtRegModified(DestVirtReg);
MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
} }
@ -600,11 +610,10 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
unsigned VirtReg = KI->second; unsigned VirtReg = KI->second;
unsigned PhysReg = VirtReg; unsigned PhysReg = VirtReg;
if (MRegisterInfo::isVirtualRegister(VirtReg)) { if (MRegisterInfo::isVirtualRegister(VirtReg)) {
std::map<unsigned, unsigned>::iterator I = unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
Virt2PhysRegMap.find(VirtReg); PhysReg = PhysRegSlot;
assert(I != Virt2PhysRegMap.end()); assert(PhysReg != 0);
PhysReg = I->second; PhysRegSlot = 0;
Virt2PhysRegMap.erase(I);
} }
if (PhysReg) { if (PhysReg) {
@ -631,12 +640,15 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
else else
removePhysReg(i); removePhysReg(i);
for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(), #ifndef NDEBUG
E = Virt2PhysRegMap.end(); I != E; ++I) bool AllOk = true;
std::cerr << "Register still mapped: " << I->first << " -> " for (unsigned i = 0, e = Virt2PhysRegMap.size(); i != e; ++i)
<< I->second << "\n"; if (unsigned PR = Virt2PhysRegMap[i]) {
std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
assert(Virt2PhysRegMap.empty() && "Virtual registers still in phys regs?"); AllOk = false;
}
assert(AllOk && "Virtual registers still in phys regs?");
#endif
// Clear any physical register which appear live at the end of the basic // Clear any physical register which appear live at the end of the basic
// block, but which do not hold any virtual registers. e.g., the stack // block, but which do not hold any virtual registers. e.g., the stack
@ -655,6 +667,11 @@ bool RA::runOnMachineFunction(MachineFunction &Fn) {
memset(PhysRegsUsed, -1, RegInfo->getNumRegs()*sizeof(unsigned)); memset(PhysRegsUsed, -1, RegInfo->getNumRegs()*sizeof(unsigned));
// Reserve some space for a moderate number of registers. If we know what the
// max virtual register number was we could use that instead and save some
// runtime overhead...
Virt2PhysRegMap.resize(1024);
if (!DisableKill) if (!DisableKill)
LV = &getAnalysis<LiveVariables>(); LV = &getAnalysis<LiveVariables>();
@ -665,6 +682,7 @@ bool RA::runOnMachineFunction(MachineFunction &Fn) {
StackSlotForVirtReg.clear(); StackSlotForVirtReg.clear();
VirtRegModified.clear(); VirtRegModified.clear();
Virt2PhysRegMap.clear();
return true; return true;
} }