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.
|
|
|
|
//
|
2010-02-10 16:03:48 +00:00
|
|
|
// It also contains implementations of the Spiller interface, which, given a
|
2004-09-30 01:54:45 +00:00
|
|
|
// 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"
|
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"
|
2011-02-18 22:03:18 +00:00
|
|
|
#include "llvm/CodeGen/SlotIndexes.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"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-10-26 15:35:58 +00:00
|
|
|
#include <algorithm>
|
2004-02-23 23:08:11 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-09-15 18:31:13 +00:00
|
|
|
STATISTIC(NumSpillSlots, "Number of spill slots allocated");
|
|
|
|
STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting");
|
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;
|
|
|
|
|
2010-10-07 22:25:06 +00:00
|
|
|
INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false)
|
2009-03-13 05:55:11 +00:00
|
|
|
|
|
|
|
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;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2009-03-13 05:55:11 +00:00
|
|
|
Virt2PhysMap.clear();
|
|
|
|
Virt2StackSlotMap.clear();
|
|
|
|
Virt2SplitMap.clear();
|
2009-05-04 18:40:41 +00:00
|
|
|
|
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() {
|
2011-01-09 21:58:20 +00:00
|
|
|
unsigned NumRegs = MF->getRegInfo().getNumVirtRegs();
|
|
|
|
Virt2PhysMap.resize(NumRegs);
|
|
|
|
Virt2StackSlotMap.resize(NumRegs);
|
|
|
|
Virt2SplitMap.resize(NumRegs);
|
2004-02-23 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 00:41:01 +00:00
|
|
|
unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) {
|
|
|
|
int SS = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
|
|
|
|
RC->getAlignment());
|
2011-09-15 18:31:13 +00:00
|
|
|
++NumSpillSlots;
|
2010-11-16 00:41:01 +00:00
|
|
|
return SS;
|
|
|
|
}
|
|
|
|
|
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;
|
2011-01-10 02:58:51 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(physReg) && hasPhys(physReg))
|
2009-06-15 08:28:29 +00:00
|
|
|
physReg = getPhys(physReg);
|
|
|
|
if (Hint.first == 0)
|
2011-01-10 02:58:51 +00:00
|
|
|
return (TargetRegisterInfo::isPhysicalRegister(physReg))
|
2009-06-15 08:28:29 +00:00
|
|
|
? 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);
|
2010-11-16 00:41:01 +00:00
|
|
|
return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
|
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
|
|
|
}
|
|
|
|
|
2011-02-18 22:03:18 +00:00
|
|
|
void VirtRegMap::rewrite(SlotIndexes *Indexes) {
|
|
|
|
DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
|
|
|
|
<< "********** Function: "
|
|
|
|
<< MF->getFunction()->getName() << '\n');
|
2011-03-23 04:32:49 +00:00
|
|
|
DEBUG(dump());
|
2011-04-27 17:42:31 +00:00
|
|
|
SmallVector<unsigned, 8> SuperDeads;
|
|
|
|
SmallVector<unsigned, 8> SuperDefs;
|
2011-02-18 22:03:18 +00:00
|
|
|
SmallVector<unsigned, 8> SuperKills;
|
|
|
|
|
|
|
|
for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
|
|
|
|
MBBI != MBBE; ++MBBI) {
|
|
|
|
DEBUG(MBBI->print(dbgs(), Indexes));
|
|
|
|
for (MachineBasicBlock::iterator MII = MBBI->begin(), MIE = MBBI->end();
|
|
|
|
MII != MIE;) {
|
|
|
|
MachineInstr *MI = MII;
|
|
|
|
++MII;
|
|
|
|
|
|
|
|
for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
|
|
|
|
MOE = MI->operands_end(); MOI != MOE; ++MOI) {
|
|
|
|
MachineOperand &MO = *MOI;
|
|
|
|
if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
|
|
|
continue;
|
|
|
|
unsigned VirtReg = MO.getReg();
|
|
|
|
unsigned PhysReg = getPhys(VirtReg);
|
|
|
|
assert(PhysReg != NO_PHYS_REG && "Instruction uses unmapped VirtReg");
|
|
|
|
|
|
|
|
// Preserve semantics of sub-register operands.
|
|
|
|
if (MO.getSubReg()) {
|
|
|
|
// A virtual register kill refers to the whole register, so we may
|
2011-10-05 00:01:48 +00:00
|
|
|
// have to add <imp-use,kill> operands for the super-register. A
|
|
|
|
// partial redef always kills and redefines the super-register.
|
|
|
|
if (MO.readsReg() && (MO.isDef() || MO.isKill()))
|
|
|
|
SuperKills.push_back(PhysReg);
|
|
|
|
|
|
|
|
if (MO.isDef()) {
|
|
|
|
// The <def,undef> flag only makes sense for sub-register defs, and
|
|
|
|
// we are substituting a full physreg. An <imp-use,kill> operand
|
|
|
|
// from the SuperKills list will represent the partial read of the
|
|
|
|
// super-register.
|
|
|
|
MO.setIsUndef(false);
|
|
|
|
|
|
|
|
// Also add implicit defs for the super-register.
|
|
|
|
if (MO.isDead())
|
|
|
|
SuperDeads.push_back(PhysReg);
|
|
|
|
else
|
|
|
|
SuperDefs.push_back(PhysReg);
|
|
|
|
}
|
2011-02-18 22:03:18 +00:00
|
|
|
|
|
|
|
// PhysReg operands cannot have subregister indexes.
|
|
|
|
PhysReg = TRI->getSubReg(PhysReg, MO.getSubReg());
|
|
|
|
assert(PhysReg && "Invalid SubReg for physical register");
|
|
|
|
MO.setSubReg(0);
|
|
|
|
}
|
|
|
|
// Rewrite. Note we could have used MachineOperand::substPhysReg(), but
|
|
|
|
// we need the inlining here.
|
|
|
|
MO.setReg(PhysReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any missing super-register kills after rewriting the whole
|
|
|
|
// instruction.
|
|
|
|
while (!SuperKills.empty())
|
|
|
|
MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true);
|
|
|
|
|
2011-04-27 17:42:31 +00:00
|
|
|
while (!SuperDeads.empty())
|
|
|
|
MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true);
|
|
|
|
|
|
|
|
while (!SuperDefs.empty())
|
|
|
|
MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI);
|
|
|
|
|
2011-02-18 22:03:18 +00:00
|
|
|
DEBUG(dbgs() << "> " << *MI);
|
|
|
|
|
|
|
|
// Finally, remove any identity copies.
|
|
|
|
if (MI->isIdentityCopy()) {
|
2011-05-06 17:59:57 +00:00
|
|
|
++NumIdCopies;
|
2011-03-31 17:55:25 +00:00
|
|
|
if (MI->getNumOperands() == 2) {
|
|
|
|
DEBUG(dbgs() << "Deleting identity copy.\n");
|
|
|
|
if (Indexes)
|
|
|
|
Indexes->removeMachineInstrFromMaps(MI);
|
|
|
|
// It's safe to erase MI because MII has already been incremented.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
// Transform identity copy to a KILL to deal with subregisters.
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
|
|
|
DEBUG(dbgs() << "Identity copy: " << *MI);
|
|
|
|
}
|
2011-02-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell MRI about physical registers in use.
|
|
|
|
for (unsigned Reg = 1, RegE = TRI->getNumRegs(); Reg != RegE; ++Reg)
|
|
|
|
if (!MRI->reg_nodbg_empty(Reg))
|
|
|
|
MRI->setPhysRegUsed(Reg);
|
|
|
|
}
|
|
|
|
|
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();
|
2010-02-26 21:09:24 +00:00
|
|
|
const MachineRegisterInfo &MRI = MF->getRegInfo();
|
2004-09-30 01:54:45 +00:00
|
|
|
|
2004-09-30 02:15:18 +00:00
|
|
|
OS << "********** REGISTER MAP **********\n";
|
2011-01-08 23:11:07 +00:00
|
|
|
for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) {
|
2011-01-09 03:05:53 +00:00
|
|
|
OS << '[' << PrintReg(Reg, TRI) << " -> "
|
|
|
|
<< PrintReg(Virt2PhysMap[Reg], TRI) << "] "
|
|
|
|
<< MRI.getRegClass(Reg)->getName() << "\n";
|
2011-01-08 23:11:07 +00:00
|
|
|
}
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-08 23:11:07 +00:00
|
|
|
for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) {
|
2011-01-09 03:05:53 +00:00
|
|
|
OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg]
|
2011-01-08 23:11:07 +00:00
|
|
|
<< "] " << MRI.getRegClass(Reg)->getName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
2004-09-30 02:15:18 +00:00
|
|
|
OS << '\n';
|
2004-03-01 20:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 02:09:07 +00:00
|
|
|
void VirtRegMap::dump() const {
|
2010-01-05 01:25:45 +00:00
|
|
|
print(dbgs());
|
2009-03-14 01:53:05 +00:00
|
|
|
}
|