42 #define DEBUG_TYPE "post-RA-sched" 47 TII(MF.getSubtarget().getInstrInfo()),
48 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
49 Classes(
TRI->getNumRegs(), nullptr), KillIndices(
TRI->getNumRegs(), 0),
50 DefIndices(
TRI->getNumRegs(), 0), KeepRegs(
TRI->getNumRegs(),
false) {}
55 const unsigned BBSize = BB->
size();
56 for (
unsigned i = 0,
e = TRI->
getNumRegs(); i !=
e; ++i) {
62 DefIndices[i] = BBSize;
73 for (
const auto &LI : (*SI)->liveins()) {
77 KillIndices[
Reg] = BBSize;
78 DefIndices[
Reg] = ~0u;
90 if (!IsReturnBlock && !Pristine.
test(Reg))
95 KillIndices[
Reg] = BBSize;
96 DefIndices[
Reg] = ~0u;
107 unsigned InsertPosIndex) {
117 assert(Count < InsertPosIndex &&
"Instruction index out of expected range!");
120 if (KillIndices[
Reg] != ~0u) {
125 KillIndices[
Reg] = Count;
126 }
else if (DefIndices[
Reg] < InsertPosIndex && DefIndices[
Reg] >= Count) {
135 DefIndices[
Reg] = InsertPosIndex;
139 PrescanInstruction(MI);
140 ScanInstruction(MI, Count);
146 const SDep *Next =
nullptr;
147 unsigned NextDepth = 0;
151 const SUnit *PredSU =
P->getSUnit();
152 unsigned PredLatency =
P->getLatency();
153 unsigned PredTotalLatency = PredSU->
getDepth() + PredLatency;
156 if (NextDepth < PredTotalLatency ||
157 (NextDepth == PredTotalLatency &&
P->getKind() ==
SDep::Anti)) {
158 NextDepth = PredTotalLatency;
165 void CriticalAntiDepBreaker::PrescanInstruction(
MachineInstr &
MI) {
189 if (!MO.
isReg())
continue;
191 if (Reg == 0)
continue;
199 if (!Classes[Reg] && NewRC)
200 Classes[
Reg] = NewRC;
201 else if (!NewRC || Classes[Reg] != NewRC)
209 unsigned AliasReg = *AI;
210 if (Classes[AliasReg]) {
217 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
218 RegRefs.insert(std::make_pair(Reg, &MO));
233 SubRegs.
isValid(); ++SubRegs) {
234 KeepRegs.
set(*SubRegs);
237 SuperRegs.
isValid(); ++SuperRegs) {
238 KeepRegs.
set(*SuperRegs);
242 if (MO.
isUse() && Special) {
243 if (!KeepRegs.
test(Reg)) {
246 KeepRegs.
set(*SubRegs);
252 void CriticalAntiDepBreaker::ScanInstruction(
MachineInstr &MI,
unsigned Count) {
256 assert(!MI.
isKill() &&
"Attempting to scan a kill instruction");
265 auto ClobbersPhysRegAndSubRegs = [&](
unsigned PhysReg) {
273 for (
unsigned i = 0,
e = TRI->
getNumRegs(); i !=
e; ++i) {
274 if (ClobbersPhysRegAndSubRegs(i)) {
275 DefIndices[i] = Count;
276 KillIndices[i] = ~0u;
278 Classes[i] =
nullptr;
284 if (!MO.
isReg())
continue;
286 if (Reg == 0)
continue;
287 if (!MO.
isDef())
continue;
295 bool Keep = KeepRegs.
test(Reg);
300 unsigned SubregReg = *SRI;
301 DefIndices[SubregReg] = Count;
302 KillIndices[SubregReg] = ~0u;
303 Classes[SubregReg] =
nullptr;
304 RegRefs.erase(SubregReg);
306 KeepRegs.
reset(SubregReg);
310 Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
315 if (!MO.
isReg())
continue;
317 if (Reg == 0)
continue;
318 if (!MO.
isUse())
continue;
326 if (!Classes[Reg] && NewRC)
327 Classes[
Reg] = NewRC;
328 else if (!NewRC || Classes[Reg] != NewRC)
331 RegRefs.insert(std::make_pair(Reg, &MO));
336 unsigned AliasReg = *AI;
337 if (KillIndices[AliasReg] == ~0u) {
338 KillIndices[AliasReg] = Count;
339 DefIndices[AliasReg] = ~0u;
357 CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
358 RegRefIter RegRefEnd,
360 for (RegRefIter
I = RegRefBegin;
I != RegRefEnd; ++
I ) {
377 if (!CheckOper.
isReg() || !CheckOper.
isDef() ||
378 CheckOper.
getReg() != NewReg)
383 if (RefOper->
isDef())
400 unsigned CriticalAntiDepBreaker::
401 findSuitableFreeRegister(RegRefIter RegRefBegin,
402 RegRefIter RegRefEnd,
408 for (
unsigned i = 0; i != Order.
size(); ++i) {
409 unsigned NewReg = Order[i];
411 if (NewReg == AntiDepReg)
continue;
415 if (NewReg == LastNewReg)
continue;
419 if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg))
continue;
422 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
423 &&
"Kill and Def maps aren't consistent for AntiDepReg!");
424 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
425 &&
"Kill and Def maps aren't consistent for NewReg!");
426 if (KillIndices[NewReg] != ~0u ||
427 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
428 KillIndices[AntiDepReg] > DefIndices[NewReg])
431 bool Forbidden =
false;
433 ite = Forbid.
end(); it != ite; ++it)
438 if (Forbidden)
continue;
450 unsigned InsertPosIndex,
454 if (SUnits.empty())
return 0;
463 const SUnit *Max =
nullptr;
464 for (
unsigned i = 0,
e = SUnits.size(); i !=
e; ++i) {
465 const SUnit *SU = &SUnits[i];
470 assert(Max &&
"Failed to find bottom of the critical path");
478 if (KillIndices[
Reg] == ~0u)
487 const SUnit *CriticalPathSU = Max;
531 std::vector<unsigned> LastNewReg(TRI->
getNumRegs(), 0);
537 unsigned Count = InsertPosIndex - 1;
563 unsigned AntiDepReg = 0;
564 if (&MI == CriticalPathMI) {
566 const SUnit *NextSU = Edge->getSUnit();
570 AntiDepReg = Edge->getReg();
571 assert(AntiDepReg != 0 &&
"Anti-dependence on reg0?");
575 else if (KeepRegs.
test(AntiDepReg))
589 PE = CriticalPathSU->
Preds.end();
P != PE; ++
P)
590 if (
P->getSUnit() == NextSU ?
591 (
P->getKind() !=
SDep::Anti ||
P->getReg() != AntiDepReg) :
592 (
P->getKind() ==
SDep::Data &&
P->getReg() == AntiDepReg)) {
598 CriticalPathSU = NextSU;
599 CriticalPathMI = CriticalPathSU->
getInstr();
602 CriticalPathSU =
nullptr;
603 CriticalPathMI =
nullptr;
607 PrescanInstruction(MI);
618 else if (AntiDepReg) {
625 if (!MO.
isReg())
continue;
627 if (Reg == 0)
continue;
632 if (MO.
isDef() && Reg != AntiDepReg)
641 assert((AntiDepReg == 0 || RC !=
nullptr) &&
642 "Register should be live if it's causing an anti-dependence!");
643 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
650 if (AntiDepReg != 0) {
651 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
652 std::multimap<unsigned, MachineOperand *>::iterator>
653 Range = RegRefs.equal_range(AntiDepReg);
654 if (
unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
656 LastNewReg[AntiDepReg],
659 <<
printReg(AntiDepReg, TRI) <<
" with " 660 << RegRefs.count(AntiDepReg) <<
" references" 661 <<
" using " <<
printReg(NewReg, TRI) <<
"!\n");
665 for (std::multimap<unsigned, MachineOperand *>::iterator
666 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
667 Q->second->setReg(NewReg);
671 const SUnit *SU = MISUnitMap[Q->second->getParent()];
680 Classes[NewReg] = Classes[AntiDepReg];
681 DefIndices[NewReg] = DefIndices[AntiDepReg];
682 KillIndices[NewReg] = KillIndices[AntiDepReg];
683 assert(((KillIndices[NewReg] == ~0u) !=
684 (DefIndices[NewReg] == ~0u)) &&
685 "Kill and Def maps aren't consistent for NewReg!");
687 Classes[AntiDepReg] =
nullptr;
688 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
689 KillIndices[AntiDepReg] = ~0u;
690 assert(((KillIndices[AntiDepReg] == ~0u) !=
691 (DefIndices[AntiDepReg] == ~0u)) &&
692 "Kill and Def maps aren't consistent for AntiDepReg!");
694 RegRefs.erase(AntiDepReg);
695 LastNewReg[AntiDepReg] = NewReg;
700 ScanInstruction(MI, Count);
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.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isCall(QueryType Type=AnyInBundle) const
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
This class represents lattice values for constants.
bool hasExtraDefRegAllocReq(QueryType Type=AnyInBundle) const
Returns true if this instruction def operands have special register allocation requirements that are ...
void FinishBlock() override
Finish anti-dep breaking for a basic block.
void push_back(const T &Elt)
unsigned getDepth() const
Returns the depth of this node, which is the length of the maximum path up to any node which has no p...
~CriticalAntiDepBreaker() override
bool test(unsigned Idx) const
unsigned const TargetRegisterInfo * TRI
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
A register anti-dependence (aka WAR).
bool isEarlyClobber() const
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction. ...
MCSuperRegIterator enumerates all super-registers of Reg.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const HexagonInstrInfo * TII
This class works in conjunction with the post-RA scheduler to rename registers to break register anti...
unsigned getNumOperands() const
Retuns the total number of operands.
virtual const TargetRegisterClass * getRegClass(const MCInstrDesc &MCID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
Given a machine instruction descriptor, returns the register class constraint for OpNum...
SmallVectorImpl< SDep >::const_iterator const_pred_iterator
Regular data dependence (aka true-dependence).
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool regsOverlap(Register regA, Register regB) const
Returns true if the two registers are equal or alias each other.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
std::vector< std::pair< MachineInstr *, MachineInstr * > > DbgValueVector
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
unsigned const MachineRegisterInfo * MRI
unsigned short Latency
Node latency.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MCRegAliasIterator enumerates all registers aliasing Reg.
void StartBlock(MachineBasicBlock *BB) override
Initialize anti-dep breaking for a new basic block.
CriticalAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI)
succ_iterator succ_begin()
unsigned BreakAntiDependencies(const std::vector< SUnit > &SUnits, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned InsertPosIndex, DbgValueVector &DbgValues) override
Identifiy anti-dependencies along the critical path of the ScheduleDAG and break them by renaming reg...
MCSubRegIterator enumerates all sub-registers of Reg.
virtual bool isPredicated(const MachineInstr &MI) const
Returns true if the instruction is already predicated.
bool isDebugInstr() const
MachineOperand class - Representation of each machine instruction operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
void Observe(MachineInstr &MI, unsigned Count, unsigned InsertPosIndex) override
Update liveness information to account for the current instruction, which will not be scheduled...
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool hasExtraSrcRegAllocReq(QueryType Type=AnyInBundle) const
Returns true if this instruction source operands have special register allocation requirements that a...
static const SDep * CriticalPathStep(const SUnit *SU)
CriticalPathStep - Return the next SUnit after SU on the bottom-up critical path. ...
bool isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx=nullptr) const
Given the index of a register def operand, check if the register def is tied to a source operand...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI, unsigned OldReg, unsigned NewReg)
Update all DBG_VALUE instructions that may be affected by the dependency breaker's update of ParentMI...
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
std::vector< MachineBasicBlock * >::iterator succ_iterator
Scheduling unit. This is a node in the scheduling DAG.
Wrapper class representing virtual and physical registers.