53 #define DEBUG_TYPE "regalloc" 55 STATISTIC(NumStores,
"Number of stores added");
56 STATISTIC(NumLoads ,
"Number of loads added");
57 STATISTIC(NumCoalesced,
"Number of copies coalesced");
88 unsigned short LastOpNum = 0;
91 explicit LiveReg(
unsigned VirtReg) : VirtReg(VirtReg) {}
93 unsigned getSparseSetIndex()
const {
101 LiveRegMap LiveVirtRegs;
126 std::vector<unsigned> PhysRegState;
134 RegUnitSet UsedInInstr;
136 void setPhysRegState(
MCPhysReg PhysReg,
unsigned NewState);
139 void markRegUsedInInstr(
MCPhysReg PhysReg) {
141 UsedInInstr.insert(*Units);
145 bool isRegUsedInInstr(
MCPhysReg PhysReg)
const {
147 if (UsedInInstr.count(*Units))
155 spillImpossible = ~0u
159 StringRef getPassName()
const override {
return "Fast Register Allocator"; }
186 void addKillFlag(
const LiveReg &LRI);
187 void killVirtReg(LiveReg &LR);
188 void killVirtReg(
unsigned VirtReg);
195 unsigned calcSpillCost(
MCPhysReg PhysReg)
const;
196 void assignVirtToPhysReg(LiveReg &,
MCPhysReg PhysReg);
198 LiveRegMap::iterator findLiveVirtReg(
unsigned VirtReg) {
202 LiveRegMap::const_iterator findLiveVirtReg(
unsigned VirtReg)
const {
209 LiveReg &reloadVirtReg(
MachineInstr &
MI,
unsigned OpNum,
unsigned VirtReg,
214 int getStackSpaceFor(
unsigned VirtReg);
230 void RegAllocFast::setPhysRegState(
MCPhysReg PhysReg,
unsigned NewState) {
231 PhysRegState[PhysReg] = NewState;
236 int RegAllocFast::getStackSpaceFor(
unsigned VirtReg) {
238 int SS = StackSlotForVirtReg[VirtReg];
250 StackSlotForVirtReg[VirtReg] = FrameIdx;
259 <<
" in " <<
printReg(AssignedReg, TRI));
260 int FI = getStackSpaceFor(VirtReg);
275 LLVM_DEBUG(
dbgs() <<
"Inserting debug info due to spill:\n" << *NewDV);
280 LRIDbgValues.clear();
288 int FI = getStackSpaceFor(VirtReg);
296 bool RegAllocFast::isLastUseOfLocalReg(
const MachineOperand &MO)
const {
299 if (StackSlotForVirtReg[MO.
getReg()] != -1)
310 void RegAllocFast::addKillFlag(
const LiveReg &LR) {
311 if (!LR.LastUse)
return;
313 if (MO.
isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
314 if (MO.
getReg() == LR.PhysReg)
329 void RegAllocFast::killVirtReg(LiveReg &LR) {
331 assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
332 "Broken RegState mapping");
333 setPhysRegState(LR.PhysReg, regFree);
338 void RegAllocFast::killVirtReg(
unsigned VirtReg) {
340 "killVirtReg needs a virtual register");
341 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
342 if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
351 "Spilling a physical register is illegal!");
352 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
353 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
354 "Spilling unmapped virtual register");
355 spillVirtReg(MI, *LRI);
360 assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
"Broken RegState mapping");
368 spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
371 LR.LastUse =
nullptr;
378 if (LiveVirtRegs.empty())
382 for (LiveReg &LR : LiveVirtRegs) {
385 spillVirtReg(MI, LR);
387 LiveVirtRegs.clear();
398 unsigned PhysReg = MO.
getReg();
400 "Bad usePhysReg operand");
402 markRegUsedInInstr(PhysReg);
403 switch (PhysRegState[PhysReg]) {
407 PhysRegState[PhysReg] = regFree;
421 switch (PhysRegState[Alias]) {
436 "Instruction is not using a subregister of a reserved register");
441 setPhysRegState(Alias, regFree);
446 setPhysRegState(Alias, regDisabled);
454 setPhysRegState(PhysReg, regFree);
463 markRegUsedInInstr(PhysReg);
464 switch (
unsigned VirtReg = PhysRegState[PhysReg]) {
468 spillVirtReg(MI, VirtReg);
472 setPhysRegState(PhysReg, NewState);
477 setPhysRegState(PhysReg, NewState);
480 switch (
unsigned VirtReg = PhysRegState[Alias]) {
484 spillVirtReg(MI, VirtReg);
488 setPhysRegState(Alias, regDisabled);
500 unsigned RegAllocFast::calcSpillCost(
MCPhysReg PhysReg)
const {
501 if (isRegUsedInInstr(PhysReg)) {
503 <<
" is already used in instr.\n");
504 return spillImpossible;
506 switch (
unsigned VirtReg = PhysRegState[PhysReg]) {
513 <<
printReg(PhysReg, TRI) <<
" is reserved already.\n");
514 return spillImpossible;
516 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
517 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
518 "Missing VirtReg entry");
519 return LRI->Dirty ? spillDirty : spillClean;
528 switch (
unsigned VirtReg = PhysRegState[Alias]) {
535 return spillImpossible;
537 LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
538 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
539 "Missing VirtReg entry");
540 Cost += LRI->Dirty ? spillDirty : spillClean;
551 void RegAllocFast::assignVirtToPhysReg(LiveReg &LR,
MCPhysReg PhysReg) {
552 unsigned VirtReg = LR.VirtReg;
555 assert(LR.PhysReg == 0 &&
"Already assigned a physreg");
556 assert(PhysReg != 0 &&
"Trying to assign no register");
557 LR.PhysReg = PhysReg;
558 setPhysRegState(PhysReg, VirtReg);
562 void RegAllocFast::allocVirtReg(
MachineInstr &MI, LiveReg &LR,
unsigned Hint) {
563 const unsigned VirtReg = LR.VirtReg;
566 "Can only allocate virtual registers");
576 unsigned Cost = calcSpillCost(Hint);
577 if (Cost < spillDirty) {
579 definePhysReg(MI, Hint, regFree);
580 assignVirtToPhysReg(LR, Hint);
587 for (
MCPhysReg PhysReg : AllocationOrder) {
588 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
589 assignVirtToPhysReg(LR, PhysReg);
595 unsigned BestCost = spillImpossible;
596 for (
MCPhysReg PhysReg : AllocationOrder) {
598 unsigned Cost = calcSpillCost(PhysReg);
599 LLVM_DEBUG(
dbgs() <<
"Cost: " << Cost <<
" BestCost: " << BestCost <<
'\n');
602 assignVirtToPhysReg(LR, PhysReg);
605 if (Cost < BestCost) {
615 MI.
emitError(
"inline assembly requires more registers than available");
617 MI.
emitError(
"ran out of registers during register allocation");
618 definePhysReg(MI, *AllocationOrder.begin(), regFree);
619 assignVirtToPhysReg(LR, *AllocationOrder.begin());
623 definePhysReg(MI, BestReg, regFree);
624 assignVirtToPhysReg(LR, BestReg);
629 unsigned VirtReg,
unsigned Hint) {
631 "Not a virtual register");
632 LiveRegMap::iterator LRI;
634 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
644 allocVirtReg(MI, *LRI, Hint);
645 }
else if (LRI->LastUse) {
648 if (LRI->LastUse != &MI || LRI->LastUse->
getOperand(LRI->LastOpNum).
isUse())
651 assert(LRI->PhysReg &&
"Register not assigned");
653 LRI->LastOpNum = OpNum;
655 markRegUsedInInstr(LRI->PhysReg);
660 RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(
MachineInstr &MI,
665 "Not a virtual register");
666 LiveRegMap::iterator LRI;
668 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
671 allocVirtReg(MI, *LRI, Hint);
672 reload(MI, VirtReg, LRI->PhysReg);
673 }
else if (LRI->Dirty) {
674 if (isLastUseOfLocalReg(MO)) {
698 assert(LRI->PhysReg &&
"Register not assigned");
700 LRI->LastOpNum = OpNum;
701 markRegUsedInInstr(LRI->PhysReg);
739 void RegAllocFast::handleThroughOperands(
MachineInstr &MI,
744 if (!MO.
isReg())
continue;
750 if (ThroughRegs.
insert(Reg).second)
762 markRegUsedInInstr(Reg);
764 if (ThroughRegs.
count(PhysRegState[*AI]))
765 definePhysReg(MI, *AI, regFree);
773 if (!MO.
isReg())
continue;
777 if (!MO.
isTied())
continue;
781 LiveReg &LR = reloadVirtReg(MI,
I, Reg, 0);
783 setPhysReg(MI, MO, PhysReg);
790 LiveReg &LR = reloadVirtReg(MI,
I, Reg, 0);
798 if (!MO.
isReg())
continue;
804 MCPhysReg PhysReg = defineVirtReg(MI,
I, Reg, 0);
816 <<
" as used in instr\n");
817 markRegUsedInInstr(Reg);
821 for (
unsigned PartialDef : PartialDefs)
822 markRegUsedInInstr(PartialDef);
826 void RegAllocFast::dumpState() {
828 if (PhysRegState[
Reg] == regDisabled)
continue;
830 switch(PhysRegState[
Reg]) {
838 LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
839 assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
840 "Missing VirtReg entry");
843 assert(LRI->PhysReg == Reg &&
"Bad inverse map");
850 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
851 e = LiveVirtRegs.end(); i != e; ++i) {
858 assert(PhysRegState[i->PhysReg] == i->VirtReg &&
"Bad inverse map");
863 void RegAllocFast::allocateInstruction(
MachineInstr &MI) {
867 unsigned CopySrcReg = 0;
868 unsigned CopyDstReg = 0;
869 unsigned CopySrcSub = 0;
870 unsigned CopyDstSub = 0;
884 unsigned VirtOpEnd = 0;
885 bool hasTiedOps =
false;
886 bool hasEarlyClobbers =
false;
887 bool hasPartialRedefs =
false;
888 bool hasPhysDefs =
false;
896 if (!MO.
isReg())
continue;
902 hasTiedOps = hasTiedOps ||
906 hasEarlyClobbers =
true;
908 hasPartialRedefs =
true;
916 definePhysReg(MI, Reg,
918 hasEarlyClobbers =
true;
932 if (MI.
isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
933 (hasTiedOps && (hasPhysDefs || MCID.
getNumDefs() > 1))) {
934 handleThroughOperands(MI, VirtDead);
939 hasEarlyClobbers =
true;
944 for (
unsigned I = 0;
I != VirtOpEnd; ++
I) {
946 if (!MO.
isReg())
continue;
950 LiveReg &LR = reloadVirtReg(MI,
I, Reg, CopyDstReg);
952 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
953 if (setPhysReg(MI, MO, PhysReg))
961 if (hasEarlyClobbers) {
963 if (!MO.
isReg())
continue;
968 markRegUsedInInstr(Reg);
981 LLVM_DEBUG(
dbgs() <<
" Spilling remaining registers before call.\n");
987 for (
unsigned I = 0;
I != DefOpEnd; ++
I) {
995 definePhysReg(MI, Reg, MO.
isDead() ? regFree : regReserved);
998 MCPhysReg PhysReg = defineVirtReg(MI,
I, Reg, CopySrcReg);
1003 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1010 for (
unsigned VirtReg : VirtDead)
1011 killVirtReg(VirtReg);
1015 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1021 void RegAllocFast::handleDebugValue(
MachineInstr &MI) {
1034 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1035 if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1036 setPhysReg(MI, MO, LRI->PhysReg);
1038 int SS = StackSlotForVirtReg[
Reg];
1042 LLVM_DEBUG(
dbgs() <<
"Modifying debug info due to spill:" <<
"\t" << MI);
1047 LLVM_DEBUG(
dbgs() <<
"Unable to allocate vreg used by DBG_VALUE");
1053 LiveDbgValueMap[
Reg].push_back(&MI);
1060 PhysRegState.assign(TRI->
getNumRegs(), regDisabled);
1061 assert(LiveVirtRegs.empty() &&
"Mapping not cleared from last block?");
1068 definePhysReg(MII, LI.PhysReg, regReserved);
1076 dbgs() <<
"\n>> " << MI <<
"Regs:";
1083 handleDebugValue(MI);
1087 allocateInstruction(MI);
1091 LLVM_DEBUG(
dbgs() <<
"Spilling live registers at end of block.\n");
1092 spillAll(MBB.getFirstTerminator());
1098 NumCoalesced += Coalesced.size();
1104 LLVM_DEBUG(
dbgs() <<
"********** FAST REGISTER ALLOCATION **********\n" 1105 <<
"********** Function: " << MF.
getName() <<
'\n');
1111 MRI->freezeReservedRegs(MF);
1113 UsedInInstr.clear();
1118 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1119 StackSlotForVirtReg.
resize(NumVirtRegs);
1120 LiveVirtRegs.setUniverse(NumVirtRegs);
1124 allocateBasicBlock(MBB);
1128 MRI->clearVirtRegs();
1130 StackSlotForVirtReg.
clear();
1131 LiveDbgValueMap.
clear();
1136 return new RegAllocFast();
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool contains(unsigned Reg) const
Return true if the specified register is included in this register class.
bool isCall(QueryType Type=AnyInBundle) const
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
void emitError(StringRef Msg) const
Emit an error referring to the source location of this instruction.
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
This class represents lattice values for constants.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
void push_back(const T &Elt)
Describe properties that are true of each instruction in the target description file.
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned getSubReg() const
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const
iterator_range< mop_iterator > operands()
bool isCopyLike() const
Return true if the instruction behaves like a copy.
void setIsRenamable(bool Val=true)
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
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 isEarlyClobber() const
const char * getRegClassName(const TargetRegisterClass *Class) const
Returns the name of the register class.
unsigned getSpillAlignment(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class...
MachineInstr * buildDbgValueForSpill(MachineBasicBlock &BB, MachineBasicBlock::iterator I, const MachineInstr &Orig, int FrameIndex)
Clone a DBG_VALUE whose value has been spilled to FrameIndex.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
RegisterRegAlloc class - Track the registration of register allocators.
virtual const TargetInstrInfo * getInstrInfo() const
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
TargetInstrInfo - Interface to description of machine instruction set.
bool isSuperRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a super-register of RegA.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
MachineInstrBundleIterator< MachineInstr > iterator
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool readsVirtualRegister(unsigned Reg) const
Return true if the MachineInstr reads the specified virtual register.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineInstrBuilder & UseMI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
unsigned getSubReg(unsigned Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo...
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
void resize(typename StorageT::size_type s)
FunctionPass class - This class is used to implement most global optimizations.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
void runOnMachineFunction(const MachineFunction &MF)
runOnFunction - Prepare to answer questions about MF.
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...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setIsKill(bool Val=true)
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specific constraint if it is set.
unsigned findTiedOperandIdx(unsigned OpIdx) const
Given the index of a tied register operand, find the operand it is tied to.
bool isDebugValue() const
MachineOperand class - Representation of each machine instruction operand.
static RegisterRegAlloc fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator)
void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex)
Update a DBG_VALUE whose value has been spilled to FrameIndex.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void addRegisterDefined(unsigned Reg, const TargetRegisterInfo *RegInfo=nullptr)
We have determined MI defines a register.
const uint32_t * getRegMask() const
getRegMask - Returns a bit mask of registers preserved by this RegMask operand.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
virtual void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const
Store the specified register of the given register class to the specified stack frame index...
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
TargetSubtargetInfo - Generic base class for all target subtargets.
Representation of each machine instruction.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
SparseSet - Fast set implmentation for objects that can be identified by small unsigned keys...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false, false) void RegAllocFast
Pair of physical register and lane mask.
void setSubReg(unsigned subReg)
void addPhysRegsUsedFromRegMask(const uint32_t *RegMask)
addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
bool hasOneNonDBGUse(unsigned RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug instruction using the specified regis...
iterator_range< livein_iterator > liveins() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static reg_nodbg_iterator reg_nodbg_end()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunctionPass * createFastRegisterAllocator()
FastRegisterAllocation Pass - This pass register allocates as fast as possible.
use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const
reg_begin/reg_end - Provide iteration support to walk over all definitions and uses of a register wit...
virtual void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const
Load the specified register of the given register class from the specified stack frame index...
Properties which a MachineFunction may have at a given point in time.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.