53 #define DEBUG_TYPE "regalloc" 55 STATISTIC(NumStores,
"Number of stores added");
56 STATISTIC(NumLoads ,
"Number of loads added");
57 STATISTIC(NumCopies,
"Number of copies coalesced");
88 unsigned short LastOpNum = 0;
91 explicit LiveReg(
unsigned v) : VirtReg(v) {}
93 unsigned getSparseSetIndex()
const {
102 LiveRegMap LiveVirtRegs;
127 std::vector<unsigned> PhysRegState;
137 UsedInInstrSet UsedInInstr;
140 void markRegUsedInInstr(
MCPhysReg PhysReg) {
142 UsedInInstr.
insert(*Units);
146 bool isRegUsedInInstr(
MCPhysReg PhysReg)
const {
148 if (UsedInInstr.count(*Units))
155 bool isBulkSpilling =
false;
160 spillImpossible = ~0u
164 StringRef getPassName()
const override {
return "Fast Register Allocator"; }
189 void addKillFlag(
const LiveReg &LRI);
190 void killVirtReg(LiveRegMap::iterator LRI);
191 void killVirtReg(
unsigned VirtReg);
198 unsigned calcSpillCost(
MCPhysReg PhysReg)
const;
199 void assignVirtToPhysReg(LiveReg &,
MCPhysReg PhysReg);
201 LiveRegMap::iterator findLiveVirtReg(
unsigned VirtReg) {
205 LiveRegMap::const_iterator findLiveVirtReg(
unsigned VirtReg)
const {
209 LiveRegMap::iterator assignVirtToPhysReg(
unsigned VirtReg,
MCPhysReg PhysReg);
210 LiveRegMap::iterator allocVirtReg(
MachineInstr &
MI, LiveRegMap::iterator,
212 LiveRegMap::iterator defineVirtReg(
MachineInstr &
MI,
unsigned OpNum,
213 unsigned VirtReg,
unsigned Hint);
214 LiveRegMap::iterator reloadVirtReg(
MachineInstr &
MI,
unsigned OpNum,
215 unsigned VirtReg,
unsigned Hint);
231 int RegAllocFast::getStackSpaceFor(
unsigned VirtReg,
234 int SS = StackSlotForVirtReg[VirtReg];
245 StackSlotForVirtReg[VirtReg] = FrameIdx;
251 bool RegAllocFast::isLastUseOfLocalReg(
const MachineOperand &MO)
const {
254 if (StackSlotForVirtReg[MO.
getReg()] != -1)
265 void RegAllocFast::addKillFlag(
const LiveReg &LR) {
266 if (!LR.LastUse)
return;
268 if (MO.
isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
269 if (MO.
getReg() == LR.PhysReg)
284 void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) {
286 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
287 "Broken RegState mapping");
288 PhysRegState[LRI->PhysReg] = regFree;
291 LiveVirtRegs.erase(LRI);
295 void RegAllocFast::killVirtReg(
unsigned VirtReg) {
297 "killVirtReg needs a virtual register");
298 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
299 if (LRI != LiveVirtRegs.end())
308 "Spilling a physical register is illegal!");
309 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
310 assert(LRI != LiveVirtRegs.end() &&
"Spilling unmapped virtual register");
311 spillVirtReg(MI, LRI);
316 LiveRegMap::iterator LRI) {
318 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg &&
"Broken RegState mapping");
328 int FI = getStackSpaceFor(LRI->VirtReg, RC);
337 LiveDbgValueMap[LRI->VirtReg];
349 LRIDbgValues.clear();
351 LR.LastUse =
nullptr;
358 if (LiveVirtRegs.empty())
return;
359 isBulkSpilling =
true;
362 for (LiveRegMap::iterator
I = LiveVirtRegs.begin(),
E = LiveVirtRegs.end();
365 LiveVirtRegs.clear();
366 isBulkSpilling =
false;
377 unsigned PhysReg = MO.
getReg();
379 "Bad usePhysReg operand");
381 markRegUsedInInstr(PhysReg);
382 switch (PhysRegState[PhysReg]) {
386 PhysRegState[PhysReg] = regFree;
400 switch (PhysRegState[Alias]) {
415 "Instruction is not using a subregister of a reserved register");
420 PhysRegState[Alias] = regFree;
425 PhysRegState[Alias] = regDisabled;
433 PhysRegState[PhysReg] = regFree;
442 markRegUsedInInstr(PhysReg);
443 switch (
unsigned VirtReg = PhysRegState[PhysReg]) {
447 spillVirtReg(MI, VirtReg);
451 PhysRegState[PhysReg] = NewState;
456 PhysRegState[PhysReg] = NewState;
459 switch (
unsigned VirtReg = PhysRegState[Alias]) {
463 spillVirtReg(MI, VirtReg);
467 PhysRegState[Alias] = regDisabled;
479 unsigned RegAllocFast::calcSpillCost(
MCPhysReg PhysReg)
const {
480 if (isRegUsedInInstr(PhysReg)) {
482 <<
" is already used in instr.\n");
483 return spillImpossible;
485 switch (
unsigned VirtReg = PhysRegState[PhysReg]) {
492 <<
printReg(PhysReg, TRI) <<
" is reserved already.\n");
493 return spillImpossible;
495 LiveRegMap::const_iterator
I = findLiveVirtReg(VirtReg);
496 assert(I != LiveVirtRegs.end() &&
"Missing VirtReg entry");
497 return I->Dirty ? spillDirty : spillClean;
506 switch (
unsigned VirtReg = PhysRegState[Alias]) {
513 return spillImpossible;
515 LiveRegMap::const_iterator
I = findLiveVirtReg(VirtReg);
516 assert(I != LiveVirtRegs.end() &&
"Missing VirtReg entry");
517 Cost += I->Dirty ? spillDirty : spillClean;
528 void RegAllocFast::assignVirtToPhysReg(LiveReg &LR,
MCPhysReg PhysReg) {
531 PhysRegState[PhysReg] = LR.VirtReg;
532 assert(!LR.PhysReg &&
"Already assigned a physreg");
533 LR.PhysReg = PhysReg;
537 RegAllocFast::assignVirtToPhysReg(
unsigned VirtReg,
MCPhysReg PhysReg) {
538 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
539 assert(LRI != LiveVirtRegs.end() &&
"VirtReg disappeared");
540 assignVirtToPhysReg(*LRI, PhysReg);
546 LiveRegMap::iterator LRI,
unsigned Hint) {
547 const unsigned VirtReg = LRI->VirtReg;
550 "Can only allocate virtual registers");
557 unsigned Cost = calcSpillCost(Hint);
558 if (Cost < spillDirty) {
560 definePhysReg(MI, Hint, regFree);
563 return assignVirtToPhysReg(VirtReg, Hint);
570 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
571 assignVirtToPhysReg(*LRI, PhysReg);
579 unsigned BestReg = 0;
580 unsigned BestCost = spillImpossible;
582 unsigned Cost = calcSpillCost(PhysReg);
588 assignVirtToPhysReg(*LRI, PhysReg);
592 BestReg = PhysReg, BestCost = Cost;
596 definePhysReg(MI, BestReg, regFree);
599 return assignVirtToPhysReg(VirtReg, BestReg);
604 MI.
emitError(
"inline assembly requires more registers than available");
606 MI.
emitError(
"ran out of registers during register allocation");
607 definePhysReg(MI, *AO.begin(), regFree);
608 return assignVirtToPhysReg(VirtReg, *AO.begin());
617 "Not a virtual register");
618 LiveRegMap::iterator LRI;
620 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
630 LRI = allocVirtReg(MI, LRI, Hint);
631 }
else if (LRI->LastUse) {
634 if (LRI->LastUse != &MI || LRI->LastUse->
getOperand(LRI->LastOpNum).
isUse())
637 assert(LRI->PhysReg &&
"Register not assigned");
639 LRI->LastOpNum = OpNum;
641 markRegUsedInInstr(LRI->PhysReg);
651 "Not a virtual register");
652 LiveRegMap::iterator LRI;
654 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
657 LRI = allocVirtReg(MI, LRI, Hint);
659 int FrameIndex = getStackSpaceFor(VirtReg, RC);
661 <<
printReg(LRI->PhysReg, TRI) <<
"\n");
664 }
else if (LRI->Dirty) {
665 if (isLastUseOfLocalReg(MO)) {
689 assert(LRI->PhysReg &&
"Register not assigned");
691 LRI->LastOpNum = OpNum;
692 markRegUsedInInstr(LRI->PhysReg);
699 bool RegAllocFast::setPhysReg(
MachineInstr &MI,
unsigned OpNum,
731 void RegAllocFast::handleThroughOperands(
MachineInstr &MI,
736 if (!MO.
isReg())
continue;
742 if (ThroughRegs.
insert(Reg).second)
754 markRegUsedInInstr(Reg);
756 if (ThroughRegs.
count(PhysRegState[*AI]))
757 definePhysReg(MI, *AI, regFree);
765 if (!MO.
isReg())
continue;
769 if (!MO.
isTied())
continue;
773 LiveRegMap::iterator LRI = reloadVirtReg(MI,
I, Reg, 0);
775 setPhysReg(MI,
I, PhysReg);
782 LiveRegMap::iterator LRI = reloadVirtReg(MI,
I, Reg, 0);
790 if (!MO.
isReg())
continue;
796 LiveRegMap::iterator LRI = defineVirtReg(MI,
I, Reg, 0);
798 if (setPhysReg(MI,
I, PhysReg))
809 <<
" as used in instr\n");
810 markRegUsedInInstr(Reg);
814 for (
unsigned PartialDef : PartialDefs)
815 markRegUsedInInstr(PartialDef);
819 void RegAllocFast::dumpState() {
821 if (PhysRegState[
Reg] == regDisabled)
continue;
823 switch(PhysRegState[
Reg]) {
831 LiveRegMap::iterator
I = findLiveVirtReg(PhysRegState[Reg]);
832 assert(I != LiveVirtRegs.end() &&
"Missing VirtReg entry");
835 assert(I->PhysReg == Reg &&
"Bad inverse map");
842 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
843 e = LiveVirtRegs.end(); i != e; ++i) {
848 assert(PhysRegState[i->PhysReg] == i->VirtReg &&
"Bad inverse map");
857 PhysRegState.assign(TRI->
getNumRegs(), regDisabled);
858 assert(LiveVirtRegs.empty() &&
"Mapping not cleared from last block?");
865 definePhysReg(MII, LI.PhysReg, regReserved);
890 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
891 if (LRI != LiveVirtRegs.end())
892 setPhysReg(*DebugMI, 0, LRI->PhysReg);
894 int SS = StackSlotForVirtReg[
Reg];
899 <<
"\t" << *DebugMI);
904 LLVM_DEBUG(
dbgs() <<
"Unable to allocate vreg used by DBG_VALUE");
910 LiveDbgValueMap[
Reg].push_back(DebugMI);
918 unsigned CopySrcReg = 0;
919 unsigned CopyDstReg = 0;
920 unsigned CopySrcSub = 0;
921 unsigned CopyDstSub = 0;
935 unsigned VirtOpEnd = 0;
936 bool hasTiedOps =
false;
937 bool hasEarlyClobbers =
false;
938 bool hasPartialRedefs =
false;
939 bool hasPhysDefs =
false;
947 if (!MO.
isReg())
continue;
953 hasTiedOps = hasTiedOps ||
957 hasEarlyClobbers =
true;
959 hasPartialRedefs =
true;
967 definePhysReg(MI, Reg,
969 hasEarlyClobbers =
true;
983 if (MI.
isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
984 (hasTiedOps && (hasPhysDefs || MCID.
getNumDefs() > 1))) {
985 handleThroughOperands(MI, VirtDead);
990 hasEarlyClobbers =
true;
995 for (
unsigned I = 0;
I != VirtOpEnd; ++
I) {
997 if (!MO.
isReg())
continue;
1001 LiveRegMap::iterator LRI = reloadVirtReg(MI,
I, Reg, CopyDstReg);
1003 CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
1004 if (setPhysReg(MI,
I, PhysReg))
1011 UsedInInstr.clear();
1012 if (hasEarlyClobbers) {
1014 if (!MO.
isReg())
continue;
1019 markRegUsedInInstr(Reg);
1032 LLVM_DEBUG(
dbgs() <<
" Spilling remaining registers before call.\n");
1038 for (
unsigned I = 0;
I != DefOpEnd; ++
I) {
1046 definePhysReg(MI, Reg, MO.
isDead() ? regFree : regReserved);
1049 LiveRegMap::iterator LRI = defineVirtReg(MI,
I, Reg, CopySrcReg);
1051 if (setPhysReg(MI,
I, PhysReg)) {
1055 CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1062 for (
unsigned VirtReg : VirtDead)
1063 killVirtReg(VirtReg);
1066 if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1075 LLVM_DEBUG(
dbgs() <<
"Spilling live registers at end of block.\n");
1076 spillAll(MBB.getFirstTerminator());
1082 NumCopies += Coalesced.size();
1089 LLVM_DEBUG(
dbgs() <<
"********** FAST REGISTER ALLOCATION **********\n" 1090 <<
"********** Function: " << MF.
getName() <<
'\n');
1096 MRI->freezeReservedRegs(MF);
1098 UsedInInstr.clear();
1103 unsigned NumVirtRegs = MRI->getNumVirtRegs();
1104 StackSlotForVirtReg.
resize(NumVirtRegs);
1105 LiveVirtRegs.setUniverse(NumVirtRegs);
1109 allocateBasicBlock(MBB);
1113 MRI->clearVirtRegs();
1115 StackSlotForVirtReg.
clear();
1116 LiveDbgValueMap.
clear();
1121 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.
void push_back(const T &Elt)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool isDebugLabel() const
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.
Compute iterated dominance frontiers using a linear time algorithm.
std::pair< iterator, bool > insert(const ValueT &Val)
insert - Attempts to insert a new element.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
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
Access to explicit operands of the instruction.
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
typename DenseT::iterator iterator
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.
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.
INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false, false) int RegAllocFast
This allocates space for the specified virtual register to be held on the stack.
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
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
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.