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-11-28 19:13:06 +00:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2012-06-08 23:44:45 +00:00
|
|
|
#include "LiveDebugVariables.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-02-06 09:57:39 +00:00
|
|
|
#include "llvm/ADT/SparseSet.h"
|
2014-03-04 10:07:28 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-06-08 23:44:45 +00:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2012-09-21 20:04:28 +00:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.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"
|
2013-09-25 00:26:17 +00:00
|
|
|
#include "llvm/IR/Function.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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2004-10-26 15:35:58 +00:00
|
|
|
#include <algorithm>
|
2004-02-23 23:08:11 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
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();
|
2014-08-05 02:39:49 +00:00
|
|
|
TII = mf.getSubtarget().getInstrInfo();
|
|
|
|
TRI = mf.getSubtarget().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;
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:30:22 +00:00
|
|
|
bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) {
|
|
|
|
unsigned Hint = MRI->getSimpleHint(VirtReg);
|
|
|
|
if (!Hint)
|
|
|
|
return 0;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Hint))
|
|
|
|
Hint = getPhys(Hint);
|
|
|
|
return getPhys(VirtReg) == Hint;
|
|
|
|
}
|
|
|
|
|
2012-12-03 23:23:50 +00:00
|
|
|
bool VirtRegMap::hasKnownPreference(unsigned VirtReg) {
|
|
|
|
std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg);
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Hint.second))
|
|
|
|
return true;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Hint.second))
|
|
|
|
return hasPhys(Hint.second);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) << "] "
|
2014-11-17 05:50:14 +00:00
|
|
|
<< TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
|
2012-06-08 23:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
2014-11-17 05:50:14 +00:00
|
|
|
<< "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
|
2012-06-08 23:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2012-09-11 22:23:19 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2012-06-08 23:44:45 +00:00
|
|
|
void VirtRegMap::dump() const {
|
|
|
|
print(dbgs());
|
|
|
|
}
|
2012-09-06 19:06:06 +00:00
|
|
|
#endif
|
2012-06-08 23:44:45 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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();
|
2015-06-16 18:22:28 +00:00
|
|
|
bool readsUndefSubreg(const MachineOperand &MO) const;
|
2012-06-08 23:44:45 +00:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
VirtRegRewriter() : MachineFunctionPass(ID) {}
|
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2012-06-08 23:44:45 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction&) override;
|
2012-06-08 23:44:45 +00:00
|
|
|
};
|
|
|
|
} // 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)
|
2012-09-21 20:04:28 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LiveStacks)
|
2012-06-08 23:44:45 +00:00
|
|
|
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>();
|
2012-09-21 20:04:28 +00:00
|
|
|
AU.addRequired<LiveStacks>();
|
|
|
|
AU.addPreserved<LiveStacks>();
|
2012-06-08 23:44:45 +00:00
|
|
|
AU.addRequired<VirtRegMap>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
|
|
|
|
MF = &fn;
|
|
|
|
TM = &MF->getTarget();
|
2014-10-13 21:57:44 +00:00
|
|
|
TRI = MF->getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF->getSubtarget().getInstrInfo();
|
2012-06-08 23:44:45 +00:00
|
|
|
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: "
|
2012-08-22 06:07:19 +00:00
|
|
|
<< MF->getName() << '\n');
|
2012-06-08 23:44:45 +00:00
|
|
|
DEBUG(VRM->dump());
|
|
|
|
|
|
|
|
// Add kill flags while we still have virtual registers.
|
2012-09-06 18:15:18 +00:00
|
|
|
LIS->addKillFlags(VRM);
|
2012-06-08 23:44:45 +00:00
|
|
|
|
2012-06-09 00:14:47 +00:00
|
|
|
// Live-in lists on basic blocks are required for physregs.
|
|
|
|
addMBBLiveIns();
|
|
|
|
|
2012-06-08 23:44:45 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2012-06-09 00:14:47 +00:00
|
|
|
// Compute MBB live-in lists from virtual register live ranges and their
|
|
|
|
// assignments.
|
|
|
|
void VirtRegRewriter::addMBBLiveIns() {
|
|
|
|
SmallVector<MachineBasicBlock*, 16> LiveIn;
|
|
|
|
for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
|
|
|
|
unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx);
|
|
|
|
if (MRI->reg_nodbg_empty(VirtReg))
|
|
|
|
continue;
|
|
|
|
LiveInterval &LI = LIS->getInterval(VirtReg);
|
|
|
|
if (LI.empty() || LIS->intervalIsInOneMBB(LI))
|
|
|
|
continue;
|
|
|
|
// This is a virtual register that is live across basic blocks. Its
|
|
|
|
// assigned PhysReg must be marked as live-in to those blocks.
|
|
|
|
unsigned PhysReg = VRM->getPhys(VirtReg);
|
|
|
|
assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register.");
|
|
|
|
|
2014-12-10 01:13:08 +00:00
|
|
|
if (LI.hasSubRanges()) {
|
2014-12-11 00:59:06 +00:00
|
|
|
for (LiveInterval::SubRange &S : LI.subranges()) {
|
|
|
|
for (const auto &Seg : S.segments) {
|
2014-12-10 23:07:54 +00:00
|
|
|
if (!Indexes->findLiveInMBBs(Seg.start, Seg.end, LiveIn))
|
2014-12-10 01:13:08 +00:00
|
|
|
continue;
|
|
|
|
for (MCSubRegIndexIterator SR(PhysReg, TRI); SR.isValid(); ++SR) {
|
|
|
|
unsigned SubReg = SR.getSubReg();
|
|
|
|
unsigned SubRegIndex = SR.getSubRegIndex();
|
|
|
|
unsigned SubRegLaneMask = TRI->getSubRegIndexLaneMask(SubRegIndex);
|
2014-12-11 00:59:06 +00:00
|
|
|
if ((SubRegLaneMask & S.LaneMask) == 0)
|
2014-12-10 01:13:08 +00:00
|
|
|
continue;
|
|
|
|
for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) {
|
2015-05-22 08:11:26 +00:00
|
|
|
LiveIn[i]->addLiveIn(SubReg);
|
2014-12-10 01:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LiveIn.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Scan the segments of LI.
|
2014-12-10 23:07:54 +00:00
|
|
|
for (const auto &Seg : LI.segments) {
|
|
|
|
if (!Indexes->findLiveInMBBs(Seg.start, Seg.end, LiveIn))
|
2014-12-10 01:13:08 +00:00
|
|
|
continue;
|
|
|
|
for (unsigned i = 0, e = LiveIn.size(); i != e; ++i)
|
2015-05-22 08:11:26 +00:00
|
|
|
LiveIn[i]->addLiveIn(PhysReg);
|
2014-12-10 01:13:08 +00:00
|
|
|
LiveIn.clear();
|
|
|
|
}
|
2012-06-09 00:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-22 08:11:26 +00:00
|
|
|
|
|
|
|
// Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in
|
|
|
|
// each MBB's LiveIns set before calling addLiveIn on them.
|
|
|
|
for (MachineBasicBlock &MBB : *MF)
|
|
|
|
MBB.sortUniqueLiveIns();
|
2012-06-09 00:14:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 18:22:28 +00:00
|
|
|
/// Returns true if the given machine operand \p MO only reads undefined lanes.
|
|
|
|
/// The function only works for use operands with a subregister set.
|
|
|
|
bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const {
|
|
|
|
// Shortcut if the operand is already marked undef.
|
|
|
|
if (MO.isUndef())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
const LiveInterval &LI = LIS->getInterval(Reg);
|
|
|
|
const MachineInstr &MI = *MO.getParent();
|
|
|
|
SlotIndex BaseIndex = LIS->getInstructionIndex(&MI);
|
|
|
|
// This code is only meant to handle reading undefined subregisters which
|
|
|
|
// we couldn't properly detect before.
|
|
|
|
assert(LI.liveAt(BaseIndex) &&
|
|
|
|
"Reads of completely dead register should be marked undef already");
|
|
|
|
unsigned SubRegIdx = MO.getSubReg();
|
|
|
|
unsigned UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx);
|
|
|
|
// See if any of the relevant subregister liveranges is defined at this point.
|
|
|
|
for (const LiveInterval::SubRange &SR : LI.subranges()) {
|
|
|
|
if ((SR.LaneMask & UseMask) != 0 && SR.liveAt(BaseIndex))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-08 23:44:45 +00:00
|
|
|
void VirtRegRewriter::rewrite() {
|
2015-03-19 00:21:58 +00:00
|
|
|
bool NoSubRegLiveness = !MRI->subRegLivenessEnabled();
|
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;
|
2014-02-25 16:57:28 +00:00
|
|
|
|
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-10-15 21:57:41 +00:00
|
|
|
assert(!MRI->isReserved(PhysReg) && "Reserved register assignment");
|
2011-02-18 22:03:18 +00:00
|
|
|
|
|
|
|
// Preserve semantics of sub-register operands.
|
2015-06-16 18:22:28 +00:00
|
|
|
unsigned SubReg = MO.getSubReg();
|
|
|
|
if (SubReg != 0) {
|
|
|
|
if (NoSubRegLiveness) {
|
|
|
|
// A virtual register kill refers to the whole register, so we may
|
|
|
|
// 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()) {
|
|
|
|
// Also add implicit defs for the super-register.
|
2014-12-10 01:13:04 +00:00
|
|
|
if (MO.isDead())
|
|
|
|
SuperDeads.push_back(PhysReg);
|
|
|
|
else
|
|
|
|
SuperDefs.push_back(PhysReg);
|
|
|
|
}
|
2015-06-16 18:22:28 +00:00
|
|
|
} else {
|
|
|
|
if (MO.isUse()) {
|
|
|
|
if (readsUndefSubreg(MO))
|
|
|
|
// We need to add an <undef> flag if the subregister is
|
|
|
|
// completely undefined (and we are not adding super-register
|
|
|
|
// defs).
|
|
|
|
MO.setIsUndef(true);
|
|
|
|
} else if (!MO.isDead()) {
|
|
|
|
assert(MO.isDef());
|
|
|
|
// Things get tricky when we ran out of lane mask bits and
|
|
|
|
// merged multiple lanes into the overflow bit: In this case
|
|
|
|
// our subregister liveness tracking isn't precise and we can't
|
|
|
|
// know what subregister parts are undefined, fall back to the
|
|
|
|
// implicit super-register def then.
|
|
|
|
unsigned LaneMask = TRI->getSubRegIndexLaneMask(SubReg);
|
|
|
|
if (TargetRegisterInfo::isImpreciseLaneMask(LaneMask))
|
|
|
|
SuperDefs.push_back(PhysReg);
|
|
|
|
}
|
2011-10-05 00:01:48 +00:00
|
|
|
}
|
2011-02-18 22:03:18 +00:00
|
|
|
|
2015-06-16 18:22:28 +00:00
|
|
|
// 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.
|
|
|
|
if (MO.isDef())
|
|
|
|
MO.setIsUndef(false);
|
|
|
|
|
2011-02-18 22:03:18 +00:00
|
|
|
// PhysReg operands cannot have subregister indexes.
|
2015-06-16 18:22:28 +00:00
|
|
|
PhysReg = TRI->getSubReg(PhysReg, SubReg);
|
2011-02-18 22:03:18 +00:00
|
|
|
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;
|
2015-05-29 18:19:25 +00:00
|
|
|
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();
|
2011-02-18 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 09:57:39 +00:00
|
|
|
|