39#define DEBUG_TYPE "post-RA-sched"
43 : MF(MFi),
MRI(MF.getRegInfo()),
TII(MF.getSubtarget().getInstrInfo()),
44 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
45 Classes(
TRI->getNumRegs(), nullptr), KillIndices(
TRI->getNumRegs(), 0),
46 DefIndices(
TRI->getNumRegs(), 0), KeepRegs(
TRI->getNumRegs(),
false) {}
51 const unsigned BBSize = BB->
size();
52 for (
unsigned i = 1, e = TRI->
getNumRegs(); i != e; ++i) {
58 DefIndices[i] = BBSize;
68 for (
const auto &LI : Succ->liveins()) {
72 KillIndices[Reg] = BBSize;
73 DefIndices[Reg] = ~0u;
85 if (!IsReturnBlock && !Pristine.
test(Reg))
90 KillIndices[Reg] = BBSize;
91 DefIndices[Reg] = ~0u;
102 unsigned InsertPosIndex) {
110 if (
MI.isDebugInstr() ||
MI.isKill())
112 assert(Count < InsertPosIndex &&
"Instruction index out of expected range!");
114 for (
unsigned Reg = 1; Reg != TRI->
getNumRegs(); ++Reg) {
115 if (KillIndices[Reg] != ~0u) {
120 KillIndices[Reg] = Count;
121 }
else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
130 DefIndices[Reg] = InsertPosIndex;
134 PrescanInstruction(
MI);
135 ScanInstruction(
MI, Count);
141 const SDep *Next =
nullptr;
142 unsigned NextDepth = 0;
145 const SUnit *PredSU =
P.getSUnit();
146 unsigned PredLatency =
P.getLatency();
147 unsigned PredTotalLatency = PredSU->
getDepth() + PredLatency;
150 if (NextDepth < PredTotalLatency ||
151 (NextDepth == PredTotalLatency &&
P.getKind() ==
SDep::Anti)) {
152 NextDepth = PredTotalLatency;
159void CriticalAntiDepBreaker::PrescanInstruction(
MachineInstr &
MI) {
181 for (
unsigned i = 0, e =
MI.getNumOperands(); i != e; ++i) {
183 if (!MO.
isReg())
continue;
185 if (Reg == 0)
continue;
188 if (i <
MI.getDesc().getNumOperands())
193 if (!Classes[Reg] && NewRC)
194 Classes[
Reg] = NewRC;
195 else if (!NewRC || Classes[Reg] != NewRC)
203 unsigned AliasReg = *AI;
204 if (Classes[AliasReg]) {
212 RegRefs.insert(std::make_pair(Reg, &MO));
214 if (MO.
isUse() && Special) {
215 if (!KeepRegs.
test(Reg)) {
222 for (
unsigned I = 0, E =
MI.getNumOperands();
I != E; ++
I) {
224 if (!MO.
isReg())
continue;
238 if (
MI.isRegTiedToUseOperand(
I) &&
244 KeepRegs.
set(SuperReg);
250void CriticalAntiDepBreaker::ScanInstruction(
MachineInstr &
MI,
unsigned Count) {
254 assert(!
MI.isKill() &&
"Attempting to scan a kill instruction");
259 for (
unsigned i = 0, e =
MI.getNumOperands(); i != e; ++i) {
263 auto ClobbersPhysRegAndSubRegs = [&](
unsigned PhysReg) {
265 [&](
MCPhysReg SR) { return MO.clobbersPhysReg(SR); });
268 for (
unsigned i = 1, e = TRI->
getNumRegs(); i != e; ++i) {
269 if (ClobbersPhysRegAndSubRegs(i)) {
270 DefIndices[i] = Count;
271 KillIndices[i] = ~0
u;
273 Classes[i] =
nullptr;
279 if (!MO.
isReg())
continue;
281 if (Reg == 0)
continue;
282 if (!MO.
isDef())
continue;
285 if (
MI.isRegTiedToUseOperand(i))
295 DefIndices[SubregReg] = Count;
296 KillIndices[SubregReg] = ~0
u;
297 Classes[SubregReg] =
nullptr;
298 RegRefs.erase(SubregReg);
300 KeepRegs.
reset(SubregReg);
307 for (
unsigned i = 0, e =
MI.getNumOperands(); i !=
e; ++i) {
309 if (!MO.
isReg())
continue;
311 if (Reg == 0)
continue;
312 if (!MO.
isUse())
continue;
315 if (i <
MI.getDesc().getNumOperands())
320 if (!Classes[Reg] && NewRC)
321 Classes[
Reg] = NewRC;
322 else if (!NewRC || Classes[Reg] != NewRC)
325 RegRefs.insert(std::make_pair(Reg, &MO));
330 unsigned AliasReg = *AI;
331 if (KillIndices[AliasReg] == ~0u) {
332 KillIndices[AliasReg] = Count;
333 DefIndices[AliasReg] = ~0
u;
351CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
352 RegRefIter RegRefEnd,
354 for (RegRefIter
I = RegRefBegin;
I != RegRefEnd; ++
I ) {
366 if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
369 if (!CheckOper.isReg() || !CheckOper.isDef() ||
370 CheckOper.getReg() != NewReg)
375 if (RefOper->
isDef())
380 if (CheckOper.isEarlyClobber())
385 if (
MI->isInlineAsm())
392unsigned CriticalAntiDepBreaker::
393findSuitableFreeRegister(RegRefIter RegRefBegin,
394 RegRefIter RegRefEnd,
400 for (
unsigned NewReg : Order) {
402 if (NewReg == AntiDepReg)
continue;
406 if (NewReg == LastNewReg)
continue;
410 if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg))
continue;
413 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
414 &&
"Kill and Def maps aren't consistent for AntiDepReg!");
415 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
416 &&
"Kill and Def maps aren't consistent for NewReg!");
417 if (KillIndices[NewReg] != ~0u ||
419 KillIndices[AntiDepReg] > DefIndices[NewReg])
422 bool Forbidden =
false;
423 for (
unsigned R : Forbid)
424 if (
TRI->regsOverlap(NewReg, R)) {
428 if (Forbidden)
continue;
440 unsigned InsertPosIndex,
444 if (SUnits.empty())
return 0;
453 const SUnit *Max =
nullptr;
454 for (
const SUnit &SU : SUnits) {
455 MISUnitMap[SU.getInstr()] = &SU;
456 if (!Max || SU.getDepth() + SU.Latency > Max->getDepth() + Max->Latency)
459 assert(Max &&
"Failed to find bottom of the critical path");
464 << (Max->getDepth() + Max->Latency) <<
"\n");
466 for (
unsigned Reg = 1; Reg <
TRI->getNumRegs(); ++Reg) {
467 if (KillIndices[Reg] == ~0u)
476 const SUnit *CriticalPathSU = Max;
520 std::vector<unsigned> LastNewReg(
TRI->getNumRegs(), 0);
526 unsigned Count = InsertPosIndex - 1;
536 if (
MI.isDebugInstr() ||
MI.isKill())
552 unsigned AntiDepReg = 0;
553 if (&
MI == CriticalPathMI) {
555 const SUnit *NextSU = Edge->getSUnit();
559 AntiDepReg = Edge->getReg();
560 assert(AntiDepReg != 0 &&
"Anti-dependence on reg0?");
564 else if (KeepRegs.
test(AntiDepReg))
578 if (
P.getSUnit() == NextSU
579 ? (
P.getKind() !=
SDep::Anti ||
P.getReg() != AntiDepReg)
581 P.getReg() == AntiDepReg)) {
587 CriticalPathSU = NextSU;
588 CriticalPathMI = CriticalPathSU->
getInstr();
591 CriticalPathSU =
nullptr;
592 CriticalPathMI =
nullptr;
596 PrescanInstruction(
MI);
607 else if (AntiDepReg) {
613 if (!MO.
isReg())
continue;
615 if (Reg == 0)
continue;
616 if (MO.
isUse() &&
TRI->regsOverlap(AntiDepReg, Reg)) {
620 if (MO.
isDef() && Reg != AntiDepReg)
629 assert((AntiDepReg == 0 || RC !=
nullptr) &&
630 "Register should be live if it's causing an anti-dependence!");
638 if (AntiDepReg != 0) {
639 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
640 std::multimap<unsigned, MachineOperand *>::iterator>
641 Range = RegRefs.equal_range(AntiDepReg);
642 if (
unsigned NewReg = findSuitableFreeRegister(
Range.first,
Range.second,
644 LastNewReg[AntiDepReg],
648 << RegRefs.count(AntiDepReg) <<
" references"
653 for (std::multimap<unsigned, MachineOperand *>::iterator
654 Q =
Range.first, QE =
Range.second; Q != QE; ++Q) {
655 Q->second->setReg(NewReg);
659 const SUnit *SU = MISUnitMap[Q->second->getParent()];
668 Classes[NewReg] = Classes[AntiDepReg];
669 DefIndices[NewReg] = DefIndices[AntiDepReg];
670 KillIndices[NewReg] = KillIndices[AntiDepReg];
671 assert(((KillIndices[NewReg] == ~0u) !=
672 (DefIndices[NewReg] == ~0u)) &&
673 "Kill and Def maps aren't consistent for NewReg!");
675 Classes[AntiDepReg] =
nullptr;
676 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
677 KillIndices[AntiDepReg] = ~0u;
678 assert(((KillIndices[AntiDepReg] == ~0u) !=
679 (DefIndices[AntiDepReg] == ~0u)) &&
680 "Kill and Def maps aren't consistent for AntiDepReg!");
682 RegRefs.erase(AntiDepReg);
683 LastNewReg[AntiDepReg] = NewReg;
688 ScanInstruction(
MI, Count);
unsigned const MachineRegisterInfo * MRI
static const SUnit * CriticalPathStep(const SUnit *SU)
CriticalPathStep - Return the next SUnit after SU on the bottom-up critical path.
This file defines the DenseMap class.
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This class works in conjunction with the post-RA scheduler to rename registers to break register anti...
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...
std::vector< std::pair< MachineInstr *, MachineInstr * > > DbgValueVector
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool test(unsigned Idx) const
~CriticalAntiDepBreaker() override
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...
void FinishBlock() override
Finish anti-dep breaking for a basic block.
void Observe(MachineInstr &MI, unsigned Count, unsigned InsertPosIndex) override
Update liveness information to account for the current instruction, which will not be scheduled.
void StartBlock(MachineBasicBlock *BB) override
Initialize anti-dep breaking for a new basic block.
CriticalAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI)
MCRegAliasIterator enumerates all registers aliasing Reg.
iterator_range< MCSuperRegIterator > superregs(MCRegister Reg) const
Return an iterator range over all super-registers of Reg, excluding Reg.
iterator_range< MCSubRegIterator > subregs_inclusive(MCRegister Reg) const
Return an iterator range over all sub-registers of Reg, including Reg.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
iterator_range< succ_iterator > successors()
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
bool isAllocatable(MCRegister PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
Wrapper class representing virtual and physical registers.
@ Anti
A register anti-dependence (aka WAR).
@ Data
Regular data dependence (aka true-dependence).
Scheduling unit. This is a node in the scheduling DAG.
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...
SmallVector< SDep, 4 > Preds
All sunit predecessors.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual bool isPredicated(const MachineInstr &MI) const
Returns true if the instruction is already predicated.
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,...
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
AntiDepBreaker * createCriticalAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI)
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.
@ Keep
No function return thunk.