2004-02-24 07:08:11 +08:00
|
|
|
//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-02-24 07:08:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-09-30 09:54:45 +08:00
|
|
|
// This file implements the VirtRegMap class.
|
|
|
|
//
|
|
|
|
// It also contains implementations of the the Spiller interface, which, given a
|
|
|
|
// virtual register map and a machine function, eliminates all virtual
|
|
|
|
// references by replacing them with physical register references - adding spill
|
2004-02-24 16:58:30 +08:00
|
|
|
// code as necessary.
|
2004-02-24 07:08:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-12 06:31:21 +08:00
|
|
|
#define DEBUG_TYPE "virtregmap"
|
2004-02-24 07:08:11 +08:00
|
|
|
#include "VirtRegMap.h"
|
2004-02-24 16:58:30 +08:00
|
|
|
#include "llvm/Function.h"
|
2009-05-04 02:32:42 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2004-02-24 07:08:11 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2004-09-30 09:54:45 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2008-04-12 01:53:36 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2004-02-24 07:08:11 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-02-24 16:58:30 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-05-04 11:30:11 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-08-27 20:54:02 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-02-11 16:24:21 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-24 18:36:58 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-02-21 10:22:03 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2008-06-04 17:16:33 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-02-11 16:24:21 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-01-23 08:59:48 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2004-10-26 23:35:58 +08:00
|
|
|
#include <algorithm>
|
2004-02-24 07:08:11 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-06-14 07:58:02 +08:00
|
|
|
STATISTIC(NumSpills , "Number of register spills");
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2004-09-30 09:54:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VirtRegMap implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-13 13:55:11 +08:00
|
|
|
char VirtRegMap::ID = 0;
|
|
|
|
|
|
|
|
static RegisterPass<VirtRegMap>
|
|
|
|
X("virtregmap", "Virtual Register Map");
|
|
|
|
|
|
|
|
bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
|
2009-06-15 04:22:55 +08:00
|
|
|
MRI = &mf.getRegInfo();
|
2009-03-13 13:55:11 +08:00
|
|
|
TII = mf.getTarget().getInstrInfo();
|
2009-05-04 11:30:11 +08:00
|
|
|
TRI = mf.getTarget().getRegisterInfo();
|
2009-03-13 13:55:11 +08:00
|
|
|
MF = &mf;
|
|
|
|
|
|
|
|
ReMatId = MAX_STACK_SLOT+1;
|
|
|
|
LowSpillSlot = HighSpillSlot = NO_STACK_SLOT;
|
|
|
|
|
|
|
|
Virt2PhysMap.clear();
|
|
|
|
Virt2StackSlotMap.clear();
|
|
|
|
Virt2ReMatIdMap.clear();
|
|
|
|
Virt2SplitMap.clear();
|
|
|
|
Virt2SplitKillMap.clear();
|
|
|
|
ReMatMap.clear();
|
|
|
|
ImplicitDefed.clear();
|
|
|
|
SpillSlotToUsesMap.clear();
|
|
|
|
MI2VirtMap.clear();
|
|
|
|
SpillPt2VirtMap.clear();
|
|
|
|
RestorePt2VirtMap.clear();
|
|
|
|
EmergencySpillMap.clear();
|
|
|
|
EmergencySpillSlots.clear();
|
|
|
|
|
2008-02-27 11:04:06 +08:00
|
|
|
SpillSlotToUsesMap.resize(8);
|
2009-03-13 13:55:11 +08:00
|
|
|
ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
|
2008-04-12 01:53:36 +08:00
|
|
|
TargetRegisterInfo::FirstVirtualRegister);
|
2009-05-04 11:30:11 +08:00
|
|
|
|
|
|
|
allocatableRCRegs.clear();
|
|
|
|
for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
|
|
|
|
E = TRI->regclass_end(); I != E; ++I)
|
|
|
|
allocatableRCRegs.insert(std::make_pair(*I,
|
|
|
|
TRI->getAllocatableSet(mf, *I)));
|
|
|
|
|
2006-09-05 10:12:02 +08:00
|
|
|
grow();
|
2009-03-13 13:55:11 +08:00
|
|
|
|
|
|
|
return false;
|
2006-09-05 10:12:02 +08:00
|
|
|
}
|
|
|
|
|
2004-09-30 09:54:45 +08:00
|
|
|
void VirtRegMap::grow() {
|
2009-03-13 13:55:11 +08:00
|
|
|
unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
|
2007-08-14 07:45:17 +08:00
|
|
|
Virt2PhysMap.grow(LastVirtReg);
|
|
|
|
Virt2StackSlotMap.grow(LastVirtReg);
|
|
|
|
Virt2ReMatIdMap.grow(LastVirtReg);
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 08:40:40 +08:00
|
|
|
Virt2SplitMap.grow(LastVirtReg);
|
2007-12-05 17:51:10 +08:00
|
|
|
Virt2SplitKillMap.grow(LastVirtReg);
|
2007-08-14 07:45:17 +08:00
|
|
|
ReMatMap.grow(LastVirtReg);
|
2008-04-12 01:53:36 +08:00
|
|
|
ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
|
2004-02-24 07:08:11 +08:00
|
|
|
}
|
|
|
|
|
2009-06-15 04:22:55 +08:00
|
|
|
unsigned VirtRegMap::getRegAllocPref(unsigned virtReg) {
|
2009-06-15 16:28:29 +08:00
|
|
|
std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(virtReg);
|
|
|
|
unsigned physReg = Hint.second;
|
|
|
|
if (physReg &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(physReg) && hasPhys(physReg))
|
|
|
|
physReg = getPhys(physReg);
|
|
|
|
if (Hint.first == 0)
|
|
|
|
return (physReg && TargetRegisterInfo::isPhysicalRegister(physReg))
|
|
|
|
? physReg : 0;
|
|
|
|
return TRI->ResolveRegAllocHint(Hint.first, physReg, *MF);
|
2009-06-15 04:22:55 +08:00
|
|
|
}
|
|
|
|
|
2004-09-30 09:54:45 +08:00
|
|
|
int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 10:15:18 +08:00
|
|
|
assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
|
2004-09-30 09:54:45 +08:00
|
|
|
"attempt to assign stack slot to already spilled register");
|
2009-03-13 13:55:11 +08:00
|
|
|
const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
|
|
|
|
int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
2009-10-17 17:20:14 +08:00
|
|
|
RC->getAlignment(), /*isSS*/true);
|
2008-02-27 11:04:06 +08:00
|
|
|
if (LowSpillSlot == NO_STACK_SLOT)
|
|
|
|
LowSpillSlot = SS;
|
|
|
|
if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
|
|
|
|
HighSpillSlot = SS;
|
|
|
|
unsigned Idx = SS-LowSpillSlot;
|
|
|
|
while (Idx >= SpillSlotToUsesMap.size())
|
|
|
|
SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
|
|
|
|
Virt2StackSlotMap[virtReg] = SS;
|
2004-09-30 09:54:45 +08:00
|
|
|
++NumSpills;
|
2008-02-27 11:04:06 +08:00
|
|
|
return SS;
|
2004-02-24 07:08:11 +08:00
|
|
|
}
|
|
|
|
|
2008-02-27 11:04:06 +08:00
|
|
|
void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 10:15:18 +08:00
|
|
|
assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
|
2004-09-30 09:54:45 +08:00
|
|
|
"attempt to assign stack slot to already spilled register");
|
2008-02-27 11:04:06 +08:00
|
|
|
assert((SS >= 0 ||
|
2009-03-13 13:55:11 +08:00
|
|
|
(SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
|
2007-04-04 15:40:01 +08:00
|
|
|
"illegal fixed frame index");
|
2008-02-27 11:04:06 +08:00
|
|
|
Virt2StackSlotMap[virtReg] = SS;
|
2004-05-30 04:38:05 +08:00
|
|
|
}
|
|
|
|
|
2007-03-20 16:13:50 +08:00
|
|
|
int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2007-08-14 07:45:17 +08:00
|
|
|
assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
|
2007-03-20 16:13:50 +08:00
|
|
|
"attempt to assign re-mat id to already spilled register");
|
2007-08-14 07:45:17 +08:00
|
|
|
Virt2ReMatIdMap[virtReg] = ReMatId;
|
2007-03-20 16:13:50 +08:00
|
|
|
return ReMatId++;
|
|
|
|
}
|
|
|
|
|
2007-08-14 07:45:17 +08:00
|
|
|
void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2007-08-14 07:45:17 +08:00
|
|
|
assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
|
|
|
|
"attempt to assign re-mat id to already spilled register");
|
|
|
|
Virt2ReMatIdMap[virtReg] = id;
|
|
|
|
}
|
|
|
|
|
2008-03-11 15:19:34 +08:00
|
|
|
int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
|
|
|
|
std::map<const TargetRegisterClass*, int>::iterator I =
|
|
|
|
EmergencySpillSlots.find(RC);
|
|
|
|
if (I != EmergencySpillSlots.end())
|
|
|
|
return I->second;
|
2009-03-13 13:55:11 +08:00
|
|
|
int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
2009-10-17 17:20:14 +08:00
|
|
|
RC->getAlignment(), /*isSS*/true);
|
2008-03-11 15:19:34 +08:00
|
|
|
if (LowSpillSlot == NO_STACK_SLOT)
|
|
|
|
LowSpillSlot = SS;
|
|
|
|
if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
|
|
|
|
HighSpillSlot = SS;
|
2008-10-07 02:00:07 +08:00
|
|
|
EmergencySpillSlots[RC] = SS;
|
2008-03-11 15:19:34 +08:00
|
|
|
return SS;
|
|
|
|
}
|
|
|
|
|
2008-02-27 11:04:06 +08:00
|
|
|
void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
|
2009-03-13 13:55:11 +08:00
|
|
|
if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
|
2008-05-23 05:12:21 +08:00
|
|
|
// If FI < LowSpillSlot, this stack reference was produced by
|
|
|
|
// instruction selection and is not a spill
|
|
|
|
if (FI >= LowSpillSlot) {
|
|
|
|
assert(FI >= 0 && "Spill slot index should not be negative!");
|
2008-05-23 09:29:08 +08:00
|
|
|
assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
|
2008-05-23 05:12:21 +08:00
|
|
|
&& "Invalid spill slot");
|
|
|
|
SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
|
|
|
|
}
|
2008-02-27 11:04:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-02 07:15:36 +08:00
|
|
|
void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
|
2007-12-02 16:30:39 +08:00
|
|
|
MachineInstr *NewMI, ModRef MRInfo) {
|
2004-10-02 07:15:36 +08:00
|
|
|
// Move previous memory references folded to new instruction.
|
|
|
|
MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
|
2005-04-22 06:36:52 +08:00
|
|
|
for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
|
2004-10-02 07:15:36 +08:00
|
|
|
E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
|
|
|
|
MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
|
2004-10-01 00:35:08 +08:00
|
|
|
MI2VirtMap.erase(I++);
|
2004-09-30 09:54:45 +08:00
|
|
|
}
|
2004-10-01 00:35:08 +08:00
|
|
|
|
2004-09-30 09:54:45 +08:00
|
|
|
// add new memory reference
|
2004-10-02 07:15:36 +08:00
|
|
|
MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
|
2004-09-30 09:54:45 +08:00
|
|
|
}
|
2004-03-02 04:05:10 +08:00
|
|
|
|
2007-10-13 10:50:24 +08:00
|
|
|
void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
|
|
|
|
MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
|
|
|
|
MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
|
|
|
|
}
|
|
|
|
|
2008-02-27 11:04:06 +08:00
|
|
|
void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 23:45:36 +08:00
|
|
|
if (!MO.isFI())
|
2008-02-27 11:04:06 +08:00
|
|
|
continue;
|
|
|
|
int FI = MO.getIndex();
|
2009-03-13 13:55:11 +08:00
|
|
|
if (MF->getFrameInfo()->isFixedObjectIndex(FI))
|
2008-02-27 11:04:06 +08:00
|
|
|
continue;
|
2008-05-23 05:12:21 +08:00
|
|
|
// This stack reference was produced by instruction selection and
|
2009-03-31 16:41:31 +08:00
|
|
|
// is not a spill
|
2008-05-23 05:12:21 +08:00
|
|
|
if (FI < LowSpillSlot)
|
|
|
|
continue;
|
2008-05-23 09:29:08 +08:00
|
|
|
assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
|
2008-05-23 05:12:21 +08:00
|
|
|
&& "Invalid spill slot");
|
2008-02-27 11:04:06 +08:00
|
|
|
SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
|
|
|
|
}
|
|
|
|
MI2VirtMap.erase(MI);
|
|
|
|
SpillPt2VirtMap.erase(MI);
|
|
|
|
RestorePt2VirtMap.erase(MI);
|
2008-03-11 15:19:34 +08:00
|
|
|
EmergencySpillMap.erase(MI);
|
2008-02-27 11:04:06 +08:00
|
|
|
}
|
|
|
|
|
2009-05-04 02:32:42 +08:00
|
|
|
/// FindUnusedRegisters - Gather a list of allocatable registers that
|
|
|
|
/// have not been allocated to any virtual register.
|
2009-06-15 04:22:55 +08:00
|
|
|
bool VirtRegMap::FindUnusedRegisters(LiveIntervals* LIs) {
|
2009-05-04 02:32:42 +08:00
|
|
|
unsigned NumRegs = TRI->getNumRegs();
|
|
|
|
UnusedRegs.reset();
|
|
|
|
UnusedRegs.resize(NumRegs);
|
|
|
|
|
|
|
|
BitVector Used(NumRegs);
|
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
|
|
|
|
if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
|
|
|
|
Used.set(Virt2PhysMap[i]);
|
|
|
|
|
|
|
|
BitVector Allocatable = TRI->getAllocatableSet(*MF);
|
|
|
|
bool AnyUnused = false;
|
|
|
|
for (unsigned Reg = 1; Reg < NumRegs; ++Reg) {
|
|
|
|
if (Allocatable[Reg] && !Used[Reg] && !LIs->hasInterval(Reg)) {
|
|
|
|
bool ReallyUnused = true;
|
|
|
|
for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
|
|
|
|
if (Used[*AS] || LIs->hasInterval(*AS)) {
|
|
|
|
ReallyUnused = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ReallyUnused) {
|
|
|
|
AnyUnused = true;
|
|
|
|
UnusedRegs.set(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AnyUnused;
|
|
|
|
}
|
|
|
|
|
2009-07-24 18:36:58 +08:00
|
|
|
void VirtRegMap::print(raw_ostream &OS, const Module* M) const {
|
2009-03-13 13:55:11 +08:00
|
|
|
const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
|
2004-09-30 09:54:45 +08:00
|
|
|
|
2004-09-30 10:15:18 +08:00
|
|
|
OS << "********** REGISTER MAP **********\n";
|
2008-02-11 02:45:23 +08:00
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
2009-03-13 13:55:11 +08:00
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
|
2004-09-30 10:15:18 +08:00
|
|
|
if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
|
2008-02-27 05:47:57 +08:00
|
|
|
OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
|
2008-02-27 05:11:01 +08:00
|
|
|
<< "]\n";
|
2004-09-30 09:54:45 +08:00
|
|
|
}
|
|
|
|
|
2008-02-11 02:45:23 +08:00
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
2009-03-13 13:55:11 +08:00
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
|
2004-09-30 10:15:18 +08:00
|
|
|
if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
|
|
|
|
OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
|
|
|
|
OS << '\n';
|
2004-03-02 04:05:10 +08:00
|
|
|
}
|
|
|
|
|
2006-11-17 10:09:07 +08:00
|
|
|
void VirtRegMap::dump() const {
|
2009-08-23 16:43:55 +08:00
|
|
|
print(errs());
|
2009-03-14 09:53:05 +08:00
|
|
|
}
|