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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-07 07:39:47 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2004-02-23 23:08:11 +00:00
|
|
|
#include "VirtRegMap.h"
|
2012-06-08 23:44:45 +00:00
|
|
|
#include "LiveDebugVariables.h"
|
2004-02-24 08:58:30 +00:00
|
|
|
#include "llvm/Function.h"
|
2012-06-08 23:44:45 +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"
|
2012-06-08 23:44:45 +00:00
|
|
|
#include "llvm/CodeGen/Passes.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
|
|
|
}
|
|
|
|
|
2012-06-08 23:44:45 +00:00
|
|
|
void VirtRegMap::print(raw_ostream &OS, const Module*) const {
|
|
|
|
OS << "********** REGISTER MAP **********\n";
|
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) {
|
|
|
|
OS << '[' << PrintReg(Reg, TRI) << " -> "
|
|
|
|
<< PrintReg(Virt2PhysMap[Reg], TRI) << "] "
|
|
|
|
<< MRI->getRegClass(Reg)->getName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) {
|
|
|
|
OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg]
|
|
|
|
<< "] " << MRI->getRegClass(Reg)->getName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtRegMap::dump() const {
|
|
|
|
print(dbgs());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VirtRegRewriter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The VirtRegRewriter is the last of the register allocator passes.
|
|
|
|
// It rewrites virtual registers to physical registers as specified in the
|
|
|
|
// VirtRegMap analysis. It also updates live-in information on basic blocks
|
|
|
|
// according to LiveIntervals.
|
|
|
|
//
|
|
|
|
namespace {
|
|
|
|
class VirtRegRewriter : public MachineFunctionPass {
|
|
|
|
MachineFunction *MF;
|
|
|
|
const TargetMachine *TM;
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
MachineRegisterInfo *MRI;
|
|
|
|
SlotIndexes *Indexes;
|
|
|
|
LiveIntervals *LIS;
|
|
|
|
VirtRegMap *VRM;
|
|
|
|
|
|
|
|
void rewrite();
|
|
|
|
void addMBBLiveIns();
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
VirtRegRewriter() : MachineFunctionPass(ID) {}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction&);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char &llvm::VirtRegRewriterID = VirtRegRewriter::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
|
|
|
|
"Virtual Register Rewriter", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
|
|
|
|
INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter",
|
|
|
|
"Virtual Register Rewriter", false, false)
|
|
|
|
|
|
|
|
char VirtRegRewriter::ID = 0;
|
|
|
|
|
|
|
|
void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<LiveIntervals>();
|
|
|
|
AU.addRequired<SlotIndexes>();
|
|
|
|
AU.addPreserved<SlotIndexes>();
|
|
|
|
AU.addRequired<LiveDebugVariables>();
|
|
|
|
AU.addRequired<VirtRegMap>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
|
|
|
|
MF = &fn;
|
|
|
|
TM = &MF->getTarget();
|
|
|
|
TRI = TM->getRegisterInfo();
|
|
|
|
TII = TM->getInstrInfo();
|
|
|
|
MRI = &MF->getRegInfo();
|
|
|
|
Indexes = &getAnalysis<SlotIndexes>();
|
|
|
|
LIS = &getAnalysis<LiveIntervals>();
|
|
|
|
VRM = &getAnalysis<VirtRegMap>();
|
2011-02-18 22:03:18 +00:00
|
|
|
DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
|
|
|
|
<< "********** Function: "
|
|
|
|
<< MF->getFunction()->getName() << '\n');
|
2012-06-08 23:44:45 +00:00
|
|
|
DEBUG(VRM->dump());
|
|
|
|
|
|
|
|
// Add kill flags while we still have virtual registers.
|
|
|
|
LIS->addKillFlags();
|
|
|
|
|
|
|
|
// Rewrite virtual registers.
|
|
|
|
rewrite();
|
|
|
|
|
|
|
|
// Write out new DBG_VALUE instructions.
|
|
|
|
getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
|
|
|
|
|
|
|
|
// All machine operands and other references to virtual registers have been
|
|
|
|
// replaced. Remove the virtual registers and release all the transient data.
|
|
|
|
VRM->clearAllVirt();
|
|
|
|
MRI->clearVirtRegs();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtRegRewriter::rewrite() {
|
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;
|
2012-01-03 22:34:31 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
BitVector Reserved = TRI->getReservedRegs(*MF);
|
|
|
|
#endif
|
2011-02-18 22:03:18 +00:00
|
|
|
|
|
|
|
for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
|
|
|
|
MBBI != MBBE; ++MBBI) {
|
|
|
|
DEBUG(MBBI->print(dbgs(), Indexes));
|
2012-01-19 07:46:36 +00:00
|
|
|
for (MachineBasicBlock::instr_iterator
|
|
|
|
MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) {
|
2011-02-18 22:03:18 +00:00
|
|
|
MachineInstr *MI = MII;
|
|
|
|
++MII;
|
|
|
|
|
|
|
|
for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
|
|
|
|
MOE = MI->operands_end(); MOI != MOE; ++MOI) {
|
|
|
|
MachineOperand &MO = *MOI;
|
2012-02-17 19:07:56 +00:00
|
|
|
|
|
|
|
// Make sure MRI knows about registers clobbered by regmasks.
|
|
|
|
if (MO.isRegMask())
|
|
|
|
MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
|
|
|
|
|
2011-02-18 22:03:18 +00:00
|
|
|
if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
|
|
|
continue;
|
|
|
|
unsigned VirtReg = MO.getReg();
|
2012-06-08 23:44:45 +00:00
|
|
|
unsigned PhysReg = VRM->getPhys(VirtReg);
|
|
|
|
assert(PhysReg != VirtRegMap::NO_PHYS_REG &&
|
|
|
|
"Instruction uses unmapped VirtReg");
|
2012-01-03 22:34:31 +00:00
|
|
|
assert(!Reserved.test(PhysReg) && "Reserved register assignment");
|
2011-02-18 22:03:18 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|