28 #define DEBUG_TYPE "post-RA-sched"
33 TII(MF.getSubtarget().getInstrInfo()),
34 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
35 Classes(TRI->getNumRegs(), nullptr), KillIndices(TRI->getNumRegs(), 0),
36 DefIndices(TRI->getNumRegs(), 0), KeepRegs(TRI->getNumRegs(),
false) {}
42 const unsigned BBSize = BB->
size();
43 for (
unsigned i = 0, e = TRI->
getNumRegs(); i != e; ++i) {
49 DefIndices[i] = BBSize;
60 for (
const auto &LI : (*SI)->liveins()) {
64 KillIndices[
Reg] = BBSize;
65 DefIndices[
Reg] = ~0u;
77 if (!IsReturnBlock && !Pristine.
test(Reg))
82 KillIndices[
Reg] = BBSize;
83 DefIndices[
Reg] = ~0u;
94 unsigned InsertPosIndex) {
104 assert(Count < InsertPosIndex &&
"Instruction index out of expected range!");
107 if (KillIndices[
Reg] != ~0u) {
112 KillIndices[
Reg] = Count;
113 }
else if (DefIndices[
Reg] < InsertPosIndex && DefIndices[
Reg] >= Count) {
122 DefIndices[
Reg] = InsertPosIndex;
126 PrescanInstruction(MI);
127 ScanInstruction(MI, Count);
133 const SDep *Next =
nullptr;
134 unsigned NextDepth = 0;
138 const SUnit *PredSU =
P->getSUnit();
139 unsigned PredLatency =
P->getLatency();
140 unsigned PredTotalLatency = PredSU->
getDepth() + PredLatency;
143 if (NextDepth < PredTotalLatency ||
144 (NextDepth == PredTotalLatency &&
P->getKind() ==
SDep::Anti)) {
145 NextDepth = PredTotalLatency;
152 void CriticalAntiDepBreaker::PrescanInstruction(
MachineInstr &
MI) {
176 if (!MO.
isReg())
continue;
178 if (Reg == 0)
continue;
186 if (!Classes[Reg] && NewRC)
187 Classes[
Reg] = NewRC;
188 else if (!NewRC || Classes[Reg] != NewRC)
196 unsigned AliasReg = *AI;
197 if (Classes[AliasReg]) {
204 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
205 RegRefs.insert(std::make_pair(Reg, &MO));
220 SubRegs.
isValid(); ++SubRegs) {
221 KeepRegs.
set(*SubRegs);
224 SuperRegs.
isValid(); ++SuperRegs) {
225 KeepRegs.
set(*SuperRegs);
229 if (MO.
isUse() && Special) {
230 if (!KeepRegs.
test(Reg)) {
233 KeepRegs.
set(*SubRegs);
239 void CriticalAntiDepBreaker::ScanInstruction(
MachineInstr &MI,
unsigned Count) {
243 assert(!MI.
isKill() &&
"Attempting to scan a kill instruction");
252 for (
unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
254 DefIndices[i] = Count;
255 KillIndices[i] = ~0u;
257 Classes[i] =
nullptr;
261 if (!MO.
isReg())
continue;
262 unsigned Reg = MO.
getReg();
263 if (Reg == 0)
continue;
264 if (!MO.
isDef())
continue;
272 bool Keep = KeepRegs.
test(Reg);
277 unsigned SubregReg = *SRI;
278 DefIndices[SubregReg] = Count;
279 KillIndices[SubregReg] = ~0u;
280 Classes[SubregReg] =
nullptr;
281 RegRefs.erase(SubregReg);
283 KeepRegs.
reset(SubregReg);
287 Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
292 if (!MO.
isReg())
continue;
293 unsigned Reg = MO.
getReg();
294 if (Reg == 0)
continue;
295 if (!MO.
isUse())
continue;
303 if (!Classes[Reg] && NewRC)
304 Classes[
Reg] = NewRC;
305 else if (!NewRC || Classes[Reg] != NewRC)
308 RegRefs.insert(std::make_pair(Reg, &MO));
313 unsigned AliasReg = *AI;
314 if (KillIndices[AliasReg] == ~0u) {
315 KillIndices[AliasReg] = Count;
316 DefIndices[AliasReg] = ~0u;
334 CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
335 RegRefIter RegRefEnd,
338 for (RegRefIter
I = RegRefBegin;
I != RegRefEnd; ++
I ) {
355 if (!CheckOper.
isReg() || !CheckOper.
isDef() ||
356 CheckOper.
getReg() != NewReg)
361 if (RefOper->
isDef())
378 unsigned CriticalAntiDepBreaker::
379 findSuitableFreeRegister(RegRefIter RegRefBegin,
380 RegRefIter RegRefEnd,
387 for (
unsigned i = 0; i != Order.
size(); ++i) {
388 unsigned NewReg = Order[i];
390 if (NewReg == AntiDepReg)
continue;
394 if (NewReg == LastNewReg)
continue;
398 if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg))
continue;
401 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
402 &&
"Kill and Def maps aren't consistent for AntiDepReg!");
403 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
404 &&
"Kill and Def maps aren't consistent for NewReg!");
405 if (KillIndices[NewReg] != ~0u ||
406 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
407 KillIndices[AntiDepReg] > DefIndices[NewReg])
410 bool Forbidden =
false;
412 ite = Forbid.
end(); it != ite; ++it)
413 if (TRI->regsOverlap(NewReg, *it)) {
417 if (Forbidden)
continue;
429 unsigned InsertPosIndex,
433 if (SUnits.empty())
return 0;
442 const SUnit *Max =
nullptr;
443 for (
unsigned i = 0, e = SUnits.size(); i != e; ++i) {
444 const SUnit *SU = &SUnits[i];
452 DEBUG(
dbgs() <<
"Critical path has total latency "
455 for (
unsigned Reg = 0; Reg < TRI->getNumRegs(); ++
Reg) {
456 if (KillIndices[Reg] == ~0u)
457 DEBUG(
dbgs() <<
" " << TRI->getName(Reg));
465 const SUnit *CriticalPathSU = Max;
509 std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
515 unsigned Count = InsertPosIndex - 1;
541 unsigned AntiDepReg = 0;
542 if (&MI == CriticalPathMI) {
544 const SUnit *NextSU = Edge->getSUnit();
548 AntiDepReg = Edge->getReg();
549 assert(AntiDepReg != 0 &&
"Anti-dependence on reg0?");
553 else if (KeepRegs.
test(AntiDepReg))
567 PE = CriticalPathSU->
Preds.end();
P != PE; ++
P)
568 if (
P->getSUnit() == NextSU ?
569 (
P->getKind() !=
SDep::Anti ||
P->getReg() != AntiDepReg) :
570 (
P->getKind() ==
SDep::Data &&
P->getReg() == AntiDepReg)) {
576 CriticalPathSU = NextSU;
577 CriticalPathMI = CriticalPathSU->
getInstr();
580 CriticalPathSU =
nullptr;
581 CriticalPathMI =
nullptr;
585 PrescanInstruction(MI);
596 else if (AntiDepReg) {
603 if (!MO.
isReg())
continue;
604 unsigned Reg = MO.
getReg();
605 if (Reg == 0)
continue;
606 if (MO.
isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
610 if (MO.
isDef() && Reg != AntiDepReg)
619 assert((AntiDepReg == 0 || RC !=
nullptr) &&
620 "Register should be live if it's causing an anti-dependence!");
621 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
628 if (AntiDepReg != 0) {
629 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
630 std::multimap<unsigned, MachineOperand *>::iterator>
631 Range = RegRefs.equal_range(AntiDepReg);
632 if (
unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
634 LastNewReg[AntiDepReg],
636 DEBUG(
dbgs() <<
"Breaking anti-dependence edge on "
637 << TRI->getName(AntiDepReg)
638 <<
" with " << RegRefs.count(AntiDepReg) <<
" references"
639 <<
" using " << TRI->getName(NewReg) <<
"!\n");
643 for (std::multimap<unsigned, MachineOperand *>::iterator
644 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
645 Q->second->setReg(NewReg);
649 const SUnit *SU = MISUnitMap[Q->second->getParent()];
658 Classes[NewReg] = Classes[AntiDepReg];
659 DefIndices[NewReg] = DefIndices[AntiDepReg];
660 KillIndices[NewReg] = KillIndices[AntiDepReg];
661 assert(((KillIndices[NewReg] == ~0u) !=
662 (DefIndices[NewReg] == ~0u)) &&
663 "Kill and Def maps aren't consistent for NewReg!");
665 Classes[AntiDepReg] =
nullptr;
666 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
667 KillIndices[AntiDepReg] = ~0u;
668 assert(((KillIndices[AntiDepReg] == ~0u) !=
669 (DefIndices[AntiDepReg] == ~0u)) &&
670 "Kill and Def maps aren't consistent for AntiDepReg!");
672 RegRefs.erase(AntiDepReg);
673 LastNewReg[AntiDepReg] = NewReg;
678 ScanInstruction(MI, Count);
void push_back(const T &Elt)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
void FinishBlock() override
Finish anti-dep breaking for a basic block.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
~CriticalAntiDepBreaker() override
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
A register anti-dependence (aka WAR).
MCSuperRegIterator enumerates all super-registers of Reg.
const HexagonInstrInfo * TII
This class works in conjunction with the post-RA scheduler to rename registers to break register anti...
SmallVectorImpl< SDep >::const_iterator const_pred_iterator
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Regular data dependence (aka true-dependence).
Reg
All possible values of the reg field in the ModR/M byte.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
bool hasExtraSrcRegAllocReq(QueryType Type=AnyInBundle) const
Returns true if this instruction source operands have special register allocation requirements that a...
size_t size() const
size - Get the array size.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
bool isDebugValue() const
bool isEarlyClobber() const
Function Alias Analysis false
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
unsigned const MachineRegisterInfo * MRI
unsigned short Latency
Node latency.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const MachineOperand & getOperand(unsigned i) const
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...
MCRegAliasIterator enumerates all registers aliasing Reg.
static const unsigned End
void StartBlock(MachineBasicBlock *BB) override
Initialize anti-dep breaking for a new basic block.
CriticalAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &)
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.
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction. ...
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
MachineOperand class - Representation of each machine instruction operand.
bool test(unsigned Idx) const
bool hasExtraDefRegAllocReq(QueryType Type=AnyInBundle) const
Returns true if this instruction def operands have special register allocation requirements that are ...
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
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.
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...
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
virtual bool isPredicated(const MachineInstr &MI) const
Returns true if the instruction is already predicated.
bool isCall(QueryType Type=AnyInBundle) const
static const SDep * CriticalPathStep(const SUnit *SU)
CriticalPathStep - Return the next SUnit after SU on the bottom-up critical path. ...
unsigned getReg() const
getReg - Returns the register number.
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...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
std::vector< std::pair< MachineInstr *, MachineInstr * > > DbgValueVector
const TargetRegisterClass * getRegClass(const MCInstrDesc &TID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
Given a machine instruction descriptor, returns the register class constraint for OpNum...
std::vector< MachineBasicBlock * >::iterator succ_iterator
Scheduling unit. This is a node in the scheduling DAG.