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