30 #define DEBUG_TYPE "post-RA-sched"
35 cl::desc(
"Debug control for aggressive anti-dep breaker"),
39 cl::desc(
"Debug control for aggressive anti-dep breaker"),
44 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
45 GroupNodeIndices(TargetRegs, 0),
46 KillIndices(TargetRegs, 0),
47 DefIndices(TargetRegs, 0)
49 const unsigned BBSize = BB->
size();
50 for (
unsigned i = 0; i < NumTargetRegs; ++i) {
53 GroupNodeIndices[i] = i;
56 DefIndices[i] = BBSize;
61 unsigned Node = GroupNodeIndices[
Reg];
62 while (GroupNodes[Node] != Node)
63 Node = GroupNodes[Node];
70 std::vector<unsigned> &
Regs,
71 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
73 for (
unsigned Reg = 0;
Reg != NumTargetRegs; ++
Reg) {
81 assert(GroupNodes[0] == 0 &&
"GroupNode 0 not parent!");
82 assert(GroupNodeIndices[0] == 0 &&
"Reg 0 not in Group 0!");
89 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
90 unsigned Other = (Parent == Group1) ? Group2 : Group1;
91 GroupNodes.at(Other) = Parent;
100 unsigned idx = GroupNodes.size();
101 GroupNodes.push_back(idx);
102 GroupNodeIndices[
Reg] = idx;
110 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
117 TII(MF.getSubtarget().getInstrInfo()),
118 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
122 for (
unsigned i = 0, e = CriticalPathRCs.
size(); i < e; ++i) {
124 if (CriticalPathSet.
none())
125 CriticalPathSet = CPSet;
127 CriticalPathSet |= CPSet;
130 DEBUG(
dbgs() <<
"AntiDep Critical-Path Registers:");
151 for (
const auto &LI : (*SI)->liveins()) {
156 DefIndices[
Reg] = ~0u;
168 if (!IsReturnBlock && !Pristine.
test(Reg))
171 unsigned AliasReg = *AI;
173 KillIndices[AliasReg] = BB->
size();
174 DefIndices[AliasReg] = ~0u;
185 unsigned InsertPosIndex) {
186 assert(Count < InsertPosIndex &&
"Instruction index out of expected range!");
188 std::set<unsigned> PassthruRegs;
189 GetPassthruRegs(MI, PassthruRegs);
190 PrescanInstruction(MI, Count, PassthruRegs);
191 ScanInstruction(MI, Count);
210 }
else if ((DefIndices[
Reg] < InsertPosIndex)
211 && (DefIndices[
Reg] >= Count)) {
212 DefIndices[
Reg] = Count;
236 void AggressiveAntiDepBreaker::GetPassthruRegs(
240 if (!MO.
isReg())
continue;
242 IsImplicitDefUse(MI, MO)) {
243 const unsigned Reg = MO.
getReg();
246 PassthruRegs.insert(*SubRegs);
259 Edges.push_back(&*
P);
267 const SDep *Next =
nullptr;
268 unsigned NextDepth = 0;
273 const SUnit *PredSU =
P->getSUnit();
274 unsigned PredLatency =
P->getLatency();
275 unsigned PredTotalLatency = PredSU->
getDepth() + PredLatency;
278 if (NextDepth < PredTotalLatency ||
279 (NextDepth == PredTotalLatency &&
P->getKind() ==
SDep::Anti)) {
280 NextDepth = PredTotalLatency;
286 return (Next) ? Next->
getSUnit() :
nullptr;
289 void AggressiveAntiDepBreaker::HandleLastUse(
unsigned Reg,
unsigned KillIdx,
292 const char *footer) {
295 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
304 DEBUG(
if (!header && footer)
dbgs() << footer);
308 if (!State->
IsLive(Reg)) {
309 KillIndices[
Reg] = KillIdx;
310 DefIndices[
Reg] = ~0u;
314 dbgs() << header << TRI->
getName(Reg); header =
nullptr; });
321 unsigned SubregReg = *SubRegs;
322 if (!State->
IsLive(SubregReg)) {
323 KillIndices[SubregReg] = KillIdx;
324 DefIndices[SubregReg] = ~0u;
325 RegRefs.erase(SubregReg);
328 dbgs() << header << TRI->
getName(Reg); header =
nullptr; });
335 DEBUG(
if (!header && footer)
dbgs() << footer);
338 void AggressiveAntiDepBreaker::PrescanInstruction(
339 MachineInstr &MI,
unsigned Count, std::set<unsigned> &PassthruRegs) {
341 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
352 unsigned Reg = MO.
getReg();
353 if (Reg == 0)
continue;
355 HandleLastUse(Reg, Count + 1,
"",
"\tDead Def: ",
"\n");
362 unsigned Reg = MO.
getReg();
363 if (Reg == 0)
continue;
381 unsigned AliasReg = *AI;
382 if (State->
IsLive(AliasReg)) {
385 TRI->
getName(AliasReg) <<
")");
394 RegRefs.insert(std::make_pair(Reg, RR));
404 unsigned Reg = MO.
getReg();
405 if (Reg == 0)
continue;
407 if (MI.
isKill() || (PassthruRegs.count(Reg) != 0))
418 if (TRI->isSuperRegister(Reg, *AI) && State->
IsLive(*AI))
421 DefIndices[*AI] = Count;
426 void AggressiveAntiDepBreaker::ScanInstruction(
MachineInstr &MI,
429 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
457 unsigned Reg = MO.
getReg();
458 if (Reg == 0)
continue;
460 DEBUG(
dbgs() <<
" " << TRI->getName(Reg) <<
"=g" <<
466 HandleLastUse(Reg, Count,
"(last-use)");
478 RegRefs.insert(std::make_pair(Reg, RR));
488 unsigned FirstReg = 0;
491 if (!MO.
isReg())
continue;
492 unsigned Reg = MO.
getReg();
493 if (Reg == 0)
continue;
496 DEBUG(
dbgs() <<
"=" << TRI->getName(Reg));
499 DEBUG(
dbgs() <<
" " << TRI->getName(Reg));
508 BitVector AggressiveAntiDepBreaker::GetRenameRegisters(
unsigned Reg) {
519 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
527 DEBUG(
dbgs() <<
" " << TRI->getRegClassName(RC));
533 bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
534 unsigned AntiDepGroupIndex,
535 RenameOrderType& RenameOrder,
536 std::map<unsigned, unsigned> &RenameMap) {
539 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
545 std::vector<unsigned>
Regs;
547 assert(Regs.size() > 0 &&
"Empty register group!");
548 if (Regs.size() == 0)
554 DEBUG(
dbgs() <<
"\tRename Candidates for Group g" << AntiDepGroupIndex
556 std::map<unsigned, BitVector> RenameRegisterMap;
557 unsigned SuperReg = 0;
558 for (
unsigned i = 0, e = Regs.size(); i != e; ++i) {
559 unsigned Reg = Regs[i];
560 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
564 if (RegRefs.count(Reg) > 0) {
565 DEBUG(
dbgs() <<
"\t\t" << TRI->getName(Reg) <<
":");
569 BV = GetRenameRegisters(Reg);
573 for (
unsigned r : BV.set_bits())
574 dbgs() <<
" " << TRI->getName(r);
581 for (
unsigned i = 0, e = Regs.size(); i != e; ++i) {
582 unsigned Reg = Regs[i];
583 if (Reg == SuperReg)
continue;
584 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
595 static int renamecnt = 0;
599 dbgs() <<
"*** Performing rename " << TRI->getName(SuperReg) <<
613 TRI->getMinimalPhysRegClass(SuperReg,
MVT::Other);
617 DEBUG(
dbgs() <<
"\tEmpty Super Regclass!!\n");
623 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.
size()));
625 unsigned OrigR = RenameOrder[SuperRC];
626 unsigned EndR = ((OrigR == Order.
size()) ? 0 : OrigR);
629 if (R == 0) R = Order.
size();
631 const unsigned NewSuperReg = Order[R];
635 if (NewSuperReg == SuperReg)
continue;
637 DEBUG(
dbgs() <<
" [" << TRI->getName(NewSuperReg) <<
':');
643 for (
unsigned i = 0, e = Regs.size(); i != e; ++i) {
644 unsigned Reg = Regs[i];
646 if (Reg == SuperReg) {
647 NewReg = NewSuperReg;
649 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
650 if (NewSubRegIdx != 0)
651 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
654 DEBUG(
dbgs() <<
" " << TRI->getName(NewReg));
657 if (!RenameRegisterMap[Reg].test(NewReg)) {
666 if (State->
IsLive(NewReg) || (KillIndices[
Reg] > DefIndices[NewReg])) {
672 unsigned AliasReg = *AI;
673 if (State->
IsLive(AliasReg) ||
674 (KillIndices[
Reg] > DefIndices[AliasReg])) {
675 DEBUG(
dbgs() <<
"(alias " << TRI->getName(AliasReg) <<
" live)");
686 for (
const auto &Q :
make_range(RegRefs.equal_range(Reg))) {
701 for (
const auto &Q :
make_range(RegRefs.equal_range(Reg))) {
702 if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
713 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
718 RenameOrder.erase(SuperRC);
719 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
737 const std::vector<SUnit>& SUnits,
740 unsigned InsertPosIndex,
745 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
750 if (SUnits.empty())
return 0;
753 RenameOrderType RenameOrder;
756 std::map<MachineInstr *, const SUnit *> MISUnitMap;
757 for (
unsigned i = 0, e = SUnits.size(); i != e; ++i) {
758 const SUnit *SU = &SUnits[i];
759 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->
getInstr(),
766 const SUnit *CriticalPathSU =
nullptr;
768 if (CriticalPathSet.
any()) {
769 for (
unsigned i = 0, e = SUnits.size(); i != e; ++i) {
770 const SUnit *SU = &SUnits[i];
771 if (!CriticalPathSU ||
778 CriticalPathMI = CriticalPathSU->
getInstr();
782 DEBUG(
dbgs() <<
"\n===== Aggressive anti-dependency breaking\n");
784 for (
unsigned Reg = 0; Reg < TRI->getNumRegs(); ++
Reg) {
786 DEBUG(
dbgs() <<
" " << TRI->getName(Reg));
797 unsigned Count = InsertPosIndex - 1;
808 std::set<unsigned> PassthruRegs;
809 GetPassthruRegs(MI, PassthruRegs);
812 PrescanInstruction(MI, Count, PassthruRegs);
816 std::vector<const SDep *> Edges;
817 const SUnit *PathSU = MISUnitMap[&
MI];
823 if (&MI == CriticalPathMI) {
825 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->
getInstr() :
nullptr;
826 }
else if (CriticalPathSet.
any()) {
827 ExcludeRegs = &CriticalPathSet;
834 for (
unsigned i = 0, e = Edges.size(); i != e; ++i) {
835 const SDep *Edge = Edges[i];
841 unsigned AntiDepReg = Edge->
getReg();
842 DEBUG(
dbgs() <<
"\tAntidep reg: " << TRI->getName(AntiDepReg));
843 assert(AntiDepReg != 0 &&
"Anti-dependence on reg0?");
849 }
else if (ExcludeRegs && ExcludeRegs->
test(AntiDepReg)) {
852 DEBUG(
dbgs() <<
" (not critical-path)\n");
854 }
else if (PassthruRegs.count(AntiDepReg) != 0) {
863 assert(AntiDepOp &&
"Can't find index for defined register operand");
879 PE = PathSU->
Preds.end();
P != PE; ++
P) {
880 if (
P->getSUnit() == NextSU ?
881 (
P->getKind() !=
SDep::Anti ||
P->getReg() != AntiDepReg) :
882 (
P->getKind() ==
SDep::Data &&
P->getReg() == AntiDepReg)) {
888 PE = PathSU->
Preds.end();
P != PE; ++
P) {
889 if ((
P->getSUnit() == NextSU) && (
P->getKind() !=
SDep::Anti) &&
894 }
else if ((
P->getSUnit() != NextSU) &&
896 (
P->getReg() == AntiDepReg)) {
897 DEBUG(
dbgs() <<
" (other dependency)\n");
903 if (AntiDepReg == 0)
continue;
920 if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R))
926 if (AntiDepReg == 0)
continue;
930 if (AntiDepReg == 0)
continue;
933 const unsigned GroupIndex = State->
GetGroup(AntiDepReg);
934 if (GroupIndex == 0) {
942 std::map<unsigned, unsigned> RenameMap;
943 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
944 DEBUG(
dbgs() <<
"\tBreaking anti-dependence edge on "
945 << TRI->getName(AntiDepReg) <<
":");
948 for (std::map<unsigned, unsigned>::iterator
949 S = RenameMap.begin(),
E = RenameMap.end(); S !=
E; ++S) {
950 unsigned CurrReg = S->first;
951 unsigned NewReg = S->second;
953 DEBUG(
dbgs() <<
" " << TRI->getName(CurrReg) <<
"->" <<
954 TRI->getName(NewReg) <<
"(" <<
955 RegRefs.count(CurrReg) <<
" refs)");
959 for (
const auto &Q :
make_range(RegRefs.equal_range(CurrReg))) {
960 Q.second.Operand->setReg(NewReg);
964 const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
974 RegRefs.erase(NewReg);
975 DefIndices[NewReg] = DefIndices[CurrReg];
976 KillIndices[NewReg] = KillIndices[CurrReg];
979 RegRefs.erase(CurrReg);
980 DefIndices[CurrReg] = KillIndices[CurrReg];
981 KillIndices[CurrReg] = ~0u;
982 assert(((KillIndices[CurrReg] == ~0u) !=
983 (DefIndices[CurrReg] == ~0u)) &&
984 "Kill and Def maps aren't consistent for AntiDepReg!");
993 ScanInstruction(MI, Count);
Information about a register reference within a liverange.
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.
MachineOperand * findRegisterDefOperand(unsigned Reg, bool isDead=false, const TargetRegisterInfo *TRI=nullptr)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
bool none() const
none - Returns true if none of the bits are set.
iterator_range< const_set_bits_iterator > set_bits() const
AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB)
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
MachineOperand * findRegisterUseOperand(unsigned Reg, bool isKill=false, const TargetRegisterInfo *TRI=nullptr)
Wrapper for findRegisterUseOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
bool any() const
any - Returns true if any bit is set.
Kind
These are the different kinds of scheduling dependencies.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
unsigned LeaveGroup(unsigned Reg)
A register anti-dependence (aka WAR).
void StartBlock(MachineBasicBlock *BB) override
Initialize anti-dep breaking for a new basic block.
AggressiveAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI, TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
unsigned GetGroup(unsigned Reg)
const HexagonInstrInfo * TII
Kind getKind() const
Returns an enum value representing the kind of the dependence.
This class works in conjunction with the post-RA scheduler to rename registers to break register anti...
SmallVectorImpl< SDep >::const_iterator const_pred_iterator
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Regular data dependence (aka true-dependence).
~AggressiveAntiDepBreaker() override
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...
A register output-dependence (aka WAW).
static void AntiDepEdges(const SUnit *SU, std::vector< const SDep * > &Edges)
AntiDepEdges - Return in Edges the anti- and output- dependencies in SU that we want to consider for ...
bool hasExtraSrcRegAllocReq(QueryType Type=AnyInBundle) const
Returns true if this instruction source operands have special register allocation requirements that a...
void GetGroupRegs(unsigned Group, std::vector< unsigned > &Regs, std::multimap< unsigned, AggressiveAntiDepState::RegisterReference > *RegRefs)
unsigned UnionGroups(unsigned Reg1, unsigned Reg2)
BitVector getAllocatableSet(const MachineFunction &MF, const TargetRegisterClass *RC=nullptr) const
Returns a bitset indexed by register number indicating if a register is allocatable or not...
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...
const MachineBasicBlock * getParent() const
bool isDebugValue() const
bool isEarlyClobber() const
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
initializer< Ty > init(const Ty &Val)
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...
unsigned const MachineRegisterInfo * MRI
unsigned short Latency
Node latency.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineInstrBuilder & UseMI
int findRegisterDefOperandIdx(unsigned Reg, bool isDead=false, bool Overlap=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a def of the specified register or -1 if it is not found...
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
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...
void FinishBlock() override
Finish anti-dep breaking for a basic block.
MCRegAliasIterator enumerates all registers aliasing Reg.
static const unsigned End
static const SUnit * CriticalPathStep(const SUnit *SU)
CriticalPathStep - Return the next SUnit after SU on the bottom-up critical path. ...
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
bool empty() const
empty - Check if the array is empty.
succ_iterator succ_begin()
void Observe(MachineInstr &MI, unsigned Count, unsigned InsertPosIndex) override
Update liveness information to account for the current instruction, which will not be scheduled...
MCSubRegIterator enumerates all sub-registers of Reg.
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction. ...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
MachineOperand class - Representation of each machine instruction operand.
bool test(unsigned Idx) const
std::vector< unsigned > & GetDefIndices()
Return the define indices.
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.
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...
bool isSuperRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a super-register of RegA.
Representation of each machine instruction.
std::multimap< unsigned, RegisterReference > & GetRegRefs()
Return the RegRefs map.
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
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static cl::opt< int > DebugDiv("agg-antidep-debugdiv", cl::desc("Debug control for aggressive anti-dep breaker"), cl::init(0), cl::Hidden)
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...
SmallVector< SDep, 4 > Succs
All sunit successors.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
std::vector< std::pair< MachineInstr *, MachineInstr * > > DbgValueVector
const char * getName(unsigned RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
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...
Contains all the state necessary for anti-dep breaking.
static cl::opt< int > DebugMod("agg-antidep-debugmod", cl::desc("Debug control for aggressive anti-dep breaker"), cl::init(0), cl::Hidden)
unsigned getReg() const
Returns the register associated with this edge.
bool IsLive(unsigned Reg)
Return true if Reg is live.
std::vector< unsigned > & GetKillIndices()
Return the kill indices.
std::vector< MachineBasicBlock * >::iterator succ_iterator
Scheduling unit. This is a node in the scheduling DAG.