41 #define DEBUG_TYPE "regalloc"
43 STATISTIC(NumSpillSlots,
"Number of spill slots allocated");
44 STATISTIC(NumIdCopies,
"Number of identity moves eliminated after rewriting");
55 MRI = &mf.getRegInfo();
56 TII = mf.getSubtarget().getInstrInfo();
61 Virt2StackSlotMap.clear();
62 Virt2SplitMap.clear();
70 Virt2PhysMap.resize(NumRegs);
71 Virt2StackSlotMap.resize(NumRegs);
72 Virt2SplitMap.resize(NumRegs);
79 "attempt to assign physical register to already mapped "
82 "Attempt to map virtReg to a reserved physReg");
83 Virt2PhysMap[virtReg] = physReg;
100 return getPhys(VirtReg) == Hint;
115 "attempt to assign stack slot to already spilled register");
117 return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
123 "attempt to assign stack slot to already spilled register");
126 "illegal fixed frame index");
127 Virt2StackSlotMap[virtReg] = SS;
131 OS <<
"********** REGISTER MAP **********\n";
135 OS <<
'[' <<
PrintReg(Reg, TRI) <<
" -> "
136 <<
PrintReg(Virt2PhysMap[Reg], TRI) <<
"] "
144 OS <<
'[' <<
PrintReg(Reg, TRI) <<
" -> fi#" << Virt2StackSlotMap[
Reg]
151 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178 void addMBBLiveIns();
180 void addLiveInsForSubRanges(
const LiveInterval &LI,
unsigned PhysReg)
const;
183 bool subRegLiveThrough(
const MachineInstr &
MI,
unsigned SuperPhysReg)
const;
202 "Virtual Register Rewriter",
false,
false)
211 char VirtRegRewriter::
ID = 0;
213 void VirtRegRewriter::getAnalysisUsage(
AnalysisUsage &AU)
const {
214 AU.setPreservesCFG();
227 TM = &MF->getTarget();
228 TRI = MF->getSubtarget().getRegisterInfo();
229 TII = MF->getSubtarget().getInstrInfo();
230 MRI = &MF->getRegInfo();
231 Indexes = &getAnalysis<SlotIndexes>();
232 LIS = &getAnalysis<LiveIntervals>();
233 VRM = &getAnalysis<VirtRegMap>();
234 DEBUG(
dbgs() <<
"********** REWRITE VIRTUAL REGISTERS **********\n"
235 <<
"********** Function: "
236 << MF->getName() <<
'\n');
240 LIS->addKillFlags(VRM);
249 getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
254 MRI->clearVirtRegs();
258 void VirtRegRewriter::addLiveInsForSubRanges(
const LiveInterval &LI,
259 unsigned PhysReg)
const {
268 for (
const LiveInterval::SubRange &SR : LI.
subranges()) {
269 SubRanges.
push_back(std::make_pair(&SR, SR.begin()));
270 if (!First.
isValid() || SR.segments.front().start < First)
271 First = SR.segments.front().start;
272 if (!Last.
isValid() || SR.segments.back().end > Last)
273 Last = SR.segments.back().end;
279 MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) {
284 for (
auto &RangeIterPair : SubRanges) {
285 const LiveInterval::SubRange *SR = RangeIterPair.first;
287 while (SRI != SR->
end() && SRI->end <= MBBBegin)
289 if (SRI == SR->
end())
291 if (SRI->start <= MBBBegin)
303 void VirtRegRewriter::addMBBLiveIns() {
304 for (
unsigned Idx = 0, IdxE =
MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
306 if (
MRI->reg_nodbg_empty(VirtReg))
309 if (LI.
empty() || LIS->intervalIsInOneMBB(LI))
313 unsigned PhysReg = VRM->getPhys(VirtReg);
317 addLiveInsForSubRanges(LI, PhysReg);
323 for (
const auto &Seg : LI) {
324 I = Indexes->advanceMBBIndex(I, Seg.start);
325 for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++
I) {
336 MBB.sortUniqueLiveIns();
341 bool VirtRegRewriter::readsUndefSubreg(
const MachineOperand &MO)
const {
349 SlotIndex BaseIndex = LIS->getInstructionIndex(MI);
353 "Reads of completely dead register should be marked undef already");
356 LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx);
358 for (
const LiveInterval::SubRange &SR : LI.
subranges()) {
365 void VirtRegRewriter::handleIdentityCopy(
MachineInstr &MI)
const {
368 DEBUG(
dbgs() <<
"Identity copy: " << MI);
384 Indexes->removeSingleMachineInstrFromMaps(MI);
393 void VirtRegRewriter::expandCopyBundle(
MachineInstr &MI)
const {
402 I !=
E && I->isBundledWithSucc(); ++
I) {
408 I->isBundledWithPred(); ) {
414 Indexes->insertMachineInstrInMaps(MI);
422 bool VirtRegRewriter::subRegLiveThrough(
const MachineInstr &MI,
423 unsigned SuperPhysReg)
const {
424 SlotIndex MIIndex = LIS->getInstructionIndex(MI);
439 if (UnitRange.
liveAt(AfterMIDefs) && UnitRange.
liveAt(BeforeMIUses))
445 void VirtRegRewriter::rewrite() {
446 bool NoSubRegLiveness = !
MRI->subRegLivenessEnabled();
452 MBBI != MBBE; ++MBBI) {
455 MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) {
469 unsigned VirtReg = MO.
getReg();
470 unsigned PhysReg = VRM->getPhys(VirtReg);
472 "Instruction uses unmapped VirtReg");
473 assert(!
MRI->isReserved(PhysReg) &&
"Reserved register assignment");
478 if (NoSubRegLiveness) {
483 (MO.
isDef() && subRegLiveThrough(*MI, PhysReg)))
495 if (readsUndefSubreg(MO))
500 }
else if (!MO.
isDead()) {
515 PhysReg = TRI->getSubReg(PhysReg, SubReg);
516 assert(PhysReg &&
"Invalid SubReg for physical register");
526 while (!SuperKills.
empty())
529 while (!SuperDeads.
empty())
532 while (!SuperDefs.
empty())
537 expandCopyBundle(*MI);
540 handleIdentityCopy(*MI);
bool hasPhys(unsigned virtReg) const
returns true if the specified virtual register is mapped to a physical register
void push_back(const T &Elt)
mop_iterator operands_end()
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
STATISTIC(NumFunctions,"Total number of functions")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
SlotIndex getBoundaryIndex() const
Returns the boundary index for associated with this index.
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
A Module instance is used to store all the information related to an LLVM module. ...
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
unsigned getSimpleHint(unsigned VReg) const
getSimpleHint - Return the preferred register allocation hint, or 0 if a standard simple hint (Type =...
bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
unsigned getSpillAlignment(const TargetRegisterClass &RC) const
Return the minimum required alignment for a spill slot for a register of this class.
LiveInterval - This class represents the liveness of a register, or stack slot.
void setIsUndef(bool Val=true)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
A live range for subregisters.
This class represents the liveness of a register, stack slot, etc.
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
iterator_range< subrange_iterator > subranges()
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
SmallVectorImpl< IdxMBBPair >::const_iterator MBBIndexIterator
Iterator over the idx2MBBMap (sorted pairs of slot index of basic block begin and basic block) ...
LLVM_NODISCARD bool empty() const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
bool isBundledWithSucc() const
Return true if this instruction is part of a bundle, and it is not the last instruction in the bundle...
void unbundleFromPred()
Break bundle above this instruction.
const HexagonRegisterInfo & getRegisterInfo() const
HexagonInstrInfo specifics.
int getObjectIndexBegin() const
Return the minimum frame object index.
Instructions::reverse_iterator reverse_instr_iterator
const char * getRegClassName(const TargetRegisterClass *Class) const
Returns the name of the register class.
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
const MachineBasicBlock * getParent() const
TargetInstrInfo - Interface to description of machine instruction set.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
bool isValid() const
Returns true if this is a valid index.
unsigned const MachineRegisterInfo * MRI
constexpr bool none() const
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const MachineOperand & getOperand(unsigned i) const
bool hasPreferredPhys(unsigned VirtReg)
returns true if VirtReg is assigned to its preferred physreg.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
void setIsInternalRead(bool Val=true)
bool isBundledWithPred() const
Return true if this instruction is part of a bundle, and it is not the first instruction in the bundl...
bool hasKnownPreference(unsigned VirtReg)
returns true if VirtReg has a known preferred register.
unsigned getSubReg() const
int CreateSpillStackObject(uint64_t Size, unsigned Alignment)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool liveAt(SlotIndex index) const
void eraseFromBundle()
Unlink 'this' form its basic block and delete it.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
Iterator for intrusive lists based on ilist_node.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
void assignVirt2Phys(unsigned virtReg, MCPhysReg physReg)
creates a mapping for the specified virtual register to the specified physical register ...
const uint32_t * getRegMask() const
getRegMask - Returns a bit mask of registers preserved by this RegMask operand.
Segments::const_iterator const_iterator
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Promote Memory to Register
bool isIdentityCopy() const
Return true is the instruction is an identity copy.
LLVM_NODISCARD T pop_back_val()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
reverse_instr_iterator instr_rend()
void addRegisterDefined(unsigned Reg, const TargetRegisterInfo *RegInfo=nullptr)
We have determined MI defines a register.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
Virtual Register Rewriter
Representation of each machine instruction.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
reverse_self_iterator getReverseIterator()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void setSubReg(unsigned subReg)
INITIALIZE_PASS_BEGIN(VirtRegRewriter,"virtregrewriter","Virtual Register Rewriter", false, false) INITIALIZE_PASS_END(VirtRegRewriter
Instructions::iterator instr_iterator
std::vector< uint8_t > Unit
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
mop_iterator operands_begin()
MachineRegisterInfo & getRegInfo() const
char & VirtRegRewriterID
VirtRegRewriter pass.
This class implements an extremely fast bulk output stream that can only output to a stream...
int assignVirt2StackSlot(unsigned virtReg)
create a mapping for the specifed virtual register to the next available stack slot ...
Primary interface to the complete machine description for the target machine.
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
unsigned getPhys(unsigned virtReg) const
returns the physical register mapped to the specified virtual register
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register. ...
std::pair< unsigned, unsigned > getRegAllocationHint(unsigned VReg) const
getRegAllocationHint - Return the register allocation hint for the specified virtual register...
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
bool hasSubRanges() const
Returns true if subregister liveness information is available.
SlotIndex - An opaque wrapper around machine indexes.
Properties which a MachineFunction may have at a given point in time.