2004-02-23 23:08:11 +00:00
|
|
|
//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2004-02-23 23:08:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-09-30 01:54:45 +00: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 08:58:30 +00:00
|
|
|
// code as necessary.
|
2004-02-23 23:08:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-11 22:31:21 +00:00
|
|
|
#define DEBUG_TYPE "virtregmap"
|
2004-02-23 23:08:11 +00:00
|
|
|
#include "VirtRegMap.h"
|
2004-02-24 08:58:30 +00:00
|
|
|
#include "llvm/Function.h"
|
2009-05-03 18:32:42 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2004-02-23 23:08:11 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2004-09-30 01:54:45 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2008-04-11 17:53:36 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2004-02-23 23:08:11 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-02-24 08:58:30 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-05-04 18:40:41 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-02-11 08:24:21 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-24 10:36:58 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-02-21 02:22:03 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2008-06-04 09:16:33 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-02-11 08:24:21 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-01-23 00:59:48 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2004-10-26 15:35:58 +00:00
|
|
|
#include <algorithm>
|
2004-02-23 23:08:11 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-06-13 23:58:02 +00:00
|
|
|
STATISTIC(NumSpills , "Number of register spills");
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VirtRegMap implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-13 05:55:11 +00:00
|
|
|
char VirtRegMap::ID = 0;
|
|
|
|
|
|
|
|
static RegisterPass<VirtRegMap>
|
|
|
|
X("virtregmap", "Virtual Register Map");
|
|
|
|
|
|
|
|
bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
|
2009-06-14 20:22:55 +00:00
|
|
|
MRI = &mf.getRegInfo();
|
2009-03-13 05:55:11 +00:00
|
|
|
TII = mf.getTarget().getInstrInfo();
|
2009-05-04 18:40:41 +00:00
|
|
|
TRI = mf.getTarget().getRegisterInfo();
|
2009-03-13 05:55:11 +00: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 03:04:06 +00:00
|
|
|
SpillSlotToUsesMap.resize(8);
|
2009-03-13 05:55:11 +00:00
|
|
|
ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
|
2008-04-11 17:53:36 +00:00
|
|
|
TargetRegisterInfo::FirstVirtualRegister);
|
2009-05-04 18:40:41 +00: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 02:12:02 +00:00
|
|
|
grow();
|
2009-03-13 05:55:11 +00:00
|
|
|
|
|
|
|
return false;
|
2006-09-05 02:12:02 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
void VirtRegMap::grow() {
|
2009-03-13 05:55:11 +00:00
|
|
|
unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
|
2007-08-13 23:45:17 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44198 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-17 00:40:40 +00:00
|
|
|
Virt2SplitMap.grow(LastVirtReg);
|
2007-12-05 09:51:10 +00:00
|
|
|
Virt2SplitKillMap.grow(LastVirtReg);
|
2007-08-13 23:45:17 +00:00
|
|
|
ReMatMap.grow(LastVirtReg);
|
2008-04-11 17:53:36 +00:00
|
|
|
ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
|
2004-02-23 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2009-06-14 20:22:55 +00:00
|
|
|
unsigned VirtRegMap::getRegAllocPref(unsigned virtReg) {
|
2009-06-15 08:28:29 +00: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-14 20:22:55 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 02:15:18 +00:00
|
|
|
assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
|
2004-09-30 01:54:45 +00:00
|
|
|
"attempt to assign stack slot to already spilled register");
|
2009-03-13 05:55:11 +00:00
|
|
|
const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
|
|
|
|
int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
2009-10-17 09:20:14 +00:00
|
|
|
RC->getAlignment(), /*isSS*/true);
|
2008-02-27 03:04:06 +00: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 01:54:45 +00:00
|
|
|
++NumSpills;
|
2008-02-27 03:04:06 +00:00
|
|
|
return SS;
|
2004-02-23 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 03:04:06 +00:00
|
|
|
void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 02:15:18 +00:00
|
|
|
assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
|
2004-09-30 01:54:45 +00:00
|
|
|
"attempt to assign stack slot to already spilled register");
|
2008-02-27 03:04:06 +00:00
|
|
|
assert((SS >= 0 ||
|
2009-03-13 05:55:11 +00:00
|
|
|
(SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
|
2007-04-04 07:40:01 +00:00
|
|
|
"illegal fixed frame index");
|
2008-02-27 03:04:06 +00:00
|
|
|
Virt2StackSlotMap[virtReg] = SS;
|
2004-05-29 20:38:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 08:13:50 +00:00
|
|
|
int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2007-08-13 23:45:17 +00:00
|
|
|
assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
|
2007-03-20 08:13:50 +00:00
|
|
|
"attempt to assign re-mat id to already spilled register");
|
2007-08-13 23:45:17 +00:00
|
|
|
Virt2ReMatIdMap[virtReg] = ReMatId;
|
2007-03-20 08:13:50 +00:00
|
|
|
return ReMatId++;
|
|
|
|
}
|
|
|
|
|
2007-08-13 23:45:17 +00:00
|
|
|
void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2007-08-13 23:45:17 +00:00
|
|
|
assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
|
|
|
|
"attempt to assign re-mat id to already spilled register");
|
|
|
|
Virt2ReMatIdMap[virtReg] = id;
|
|
|
|
}
|
|
|
|
|
2008-03-11 07:19:34 +00: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 05:55:11 +00:00
|
|
|
int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
2009-10-17 09:20:14 +00:00
|
|
|
RC->getAlignment(), /*isSS*/true);
|
2008-03-11 07:19:34 +00:00
|
|
|
if (LowSpillSlot == NO_STACK_SLOT)
|
|
|
|
LowSpillSlot = SS;
|
|
|
|
if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
|
|
|
|
HighSpillSlot = SS;
|
2008-10-06 18:00:07 +00:00
|
|
|
EmergencySpillSlots[RC] = SS;
|
2008-03-11 07:19:34 +00:00
|
|
|
return SS;
|
|
|
|
}
|
|
|
|
|
2008-02-27 03:04:06 +00:00
|
|
|
void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
|
2009-03-13 05:55:11 +00:00
|
|
|
if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
|
2008-05-22 21:12:21 +00: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 01:29:08 +00:00
|
|
|
assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
|
2008-05-22 21:12:21 +00:00
|
|
|
&& "Invalid spill slot");
|
|
|
|
SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
|
|
|
|
}
|
2008-02-27 03:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-01 23:15:36 +00:00
|
|
|
void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
|
2007-12-02 08:30:39 +00:00
|
|
|
MachineInstr *NewMI, ModRef MRInfo) {
|
2004-10-01 23:15:36 +00:00
|
|
|
// Move previous memory references folded to new instruction.
|
|
|
|
MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
|
2005-04-21 22:36:52 +00:00
|
|
|
for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
|
2004-10-01 23:15:36 +00:00
|
|
|
E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
|
|
|
|
MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
|
2004-09-30 16:35:08 +00:00
|
|
|
MI2VirtMap.erase(I++);
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
2004-09-30 16:35:08 +00:00
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
// add new memory reference
|
2004-10-01 23:15:36 +00:00
|
|
|
MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
2004-03-01 20:05:10 +00:00
|
|
|
|
2007-10-13 02:50:24 +00: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 03:04:06 +00:00
|
|
|
void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (!MO.isFI())
|
2008-02-27 03:04:06 +00:00
|
|
|
continue;
|
|
|
|
int FI = MO.getIndex();
|
2009-03-13 05:55:11 +00:00
|
|
|
if (MF->getFrameInfo()->isFixedObjectIndex(FI))
|
2008-02-27 03:04:06 +00:00
|
|
|
continue;
|
2008-05-22 21:12:21 +00:00
|
|
|
// This stack reference was produced by instruction selection and
|
2009-03-31 08:41:31 +00:00
|
|
|
// is not a spill
|
2008-05-22 21:12:21 +00:00
|
|
|
if (FI < LowSpillSlot)
|
|
|
|
continue;
|
2008-05-23 01:29:08 +00:00
|
|
|
assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
|
2008-05-22 21:12:21 +00:00
|
|
|
&& "Invalid spill slot");
|
2008-02-27 03:04:06 +00:00
|
|
|
SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
|
|
|
|
}
|
|
|
|
MI2VirtMap.erase(MI);
|
|
|
|
SpillPt2VirtMap.erase(MI);
|
|
|
|
RestorePt2VirtMap.erase(MI);
|
2008-03-11 07:19:34 +00:00
|
|
|
EmergencySpillMap.erase(MI);
|
2008-02-27 03:04:06 +00:00
|
|
|
}
|
|
|
|
|
2009-05-03 18:32:42 +00:00
|
|
|
/// FindUnusedRegisters - Gather a list of allocatable registers that
|
|
|
|
/// have not been allocated to any virtual register.
|
2009-06-14 20:22:55 +00:00
|
|
|
bool VirtRegMap::FindUnusedRegisters(LiveIntervals* LIs) {
|
2009-05-03 18:32:42 +00: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 10:36:58 +00:00
|
|
|
void VirtRegMap::print(raw_ostream &OS, const Module* M) const {
|
2009-03-13 05:55:11 +00:00
|
|
|
const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
|
2004-09-30 01:54:45 +00:00
|
|
|
|
2004-09-30 02:15:18 +00:00
|
|
|
OS << "********** REGISTER MAP **********\n";
|
2008-02-10 18:45:23 +00:00
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
2009-03-13 05:55:11 +00:00
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
|
2004-09-30 02:15:18 +00:00
|
|
|
if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
|
2008-02-26 21:47:57 +00:00
|
|
|
OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
|
2008-02-26 21:11:01 +00:00
|
|
|
<< "]\n";
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
2009-03-13 05:55:11 +00:00
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
|
2004-09-30 02:15:18 +00:00
|
|
|
if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
|
|
|
|
OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
|
|
|
|
OS << '\n';
|
2004-03-01 20:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 02:09:07 +00:00
|
|
|
void VirtRegMap::dump() const {
|
2009-08-23 08:43:55 +00:00
|
|
|
print(errs());
|
2009-03-14 01:53:05 +00:00
|
|
|
}
|