38 #define DEBUG_TYPE "pre-RA-sched"
43 cl::desc(
"Stress test instruction scheduling"));
46 void SchedulingPriorityQueue::anchor() {}
49 :
TM(mf.getTarget()),
TII(mf.getSubtarget().getInstrInfo()),
50 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
51 MRI(mf.getRegInfo()) {
73 case Data: OS <<
"Data";
break;
74 case Anti: OS <<
"Anti";
break;
75 case Output: OS <<
"Out ";
break;
76 case Order: OS <<
"Ord ";
break;
91 switch(Contents.OrdKind) {
92 case Barrier: OS <<
" Barrier";
break;
96 case Weak: OS <<
" Weak";
break;
97 case Cluster: OS <<
" Cluster";
break;
110 if (!Required && PredDep.getSUnit() == D.
getSUnit())
112 if (PredDep.overlaps(D)) {
116 SUnit *PredSU = PredDep.getSUnit();
118 SDep ForwardD = PredDep;
121 if (SuccDep == ForwardD) {
138 "NumPreds will overflow!");
140 "NumSuccs will overflow!");
150 "NumPredsLeft will overflow!");
160 "NumSuccsLeft will overflow!");
165 N->
Succs.push_back(P);
176 if (I ==
Preds.end())
183 assert(Succ != N->
Succs.end() &&
"Mismatching preds / succs lists!");
184 N->
Succs.erase(Succ);
209 if (
P.getLatency() != 0) {
210 this->setDepthDirty();
215 void SUnit::setDepthDirty() {
216 if (!isDepthCurrent)
return;
221 SU->isDepthCurrent =
false;
224 if (SuccSU->isDepthCurrent)
227 }
while (!WorkList.
empty());
231 if (!isHeightCurrent)
return;
236 SU->isHeightCurrent =
false;
239 if (PredSU->isHeightCurrent)
242 }
while (!WorkList.
empty());
250 isDepthCurrent =
true;
258 isHeightCurrent =
true;
262 void SUnit::ComputeDepth() {
269 unsigned MaxPredDepth = 0;
272 if (PredSU->isDepthCurrent)
273 MaxPredDepth =
std::max(MaxPredDepth,
283 if (MaxPredDepth != Cur->Depth) {
285 Cur->Depth = MaxPredDepth;
287 Cur->isDepthCurrent =
true;
289 }
while (!WorkList.
empty());
293 void SUnit::ComputeHeight() {
300 unsigned MaxSuccHeight = 0;
303 if (SuccSU->isHeightCurrent)
304 MaxSuccHeight =
std::max(MaxSuccHeight,
314 if (MaxSuccHeight != Cur->Height) {
316 Cur->Height = MaxSuccHeight;
318 Cur->isHeightCurrent =
true;
320 }
while (!WorkList.
empty());
328 unsigned MaxDepth = BestI->getSUnit()->getDepth();
334 if (BestI !=
Preds.begin())
338 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
341 const SUnit *Entry,
const SUnit *Exit)
const {
344 else if (
this == Exit)
377 if (
Preds.size() != 0) {
378 dbgs() <<
" Predecessors:\n";
381 Dep.getSUnit()->print(
dbgs(), G);
dbgs() <<
": ";
385 if (
Succs.size() != 0) {
386 dbgs() <<
" Successors:\n";
389 Dep.getSUnit()->print(
dbgs(), G);
dbgs() <<
": ";
398 bool AnyNotSched =
false;
399 unsigned DeadNodes = 0;
407 dbgs() <<
"*** Scheduling failed! ***\n";
409 dbgs() <<
"has not been scheduled!\n";
416 dbgs() <<
"*** Scheduling failed! ***\n";
418 dbgs() <<
"has an unexpected "
419 << (isBottomUp ?
"Height" :
"Depth") <<
" value!\n";
425 dbgs() <<
"*** Scheduling failed! ***\n";
427 dbgs() <<
"has successors left!\n";
433 dbgs() <<
"*** Scheduling failed! ***\n";
435 dbgs() <<
"has predecessors left!\n";
441 return SUnits.size() - DeadNodes;
472 unsigned DAGSize = SUnits.size();
473 std::vector<SUnit*> WorkList;
476 Index2Node.resize(DAGSize);
477 Node2Index.resize(DAGSize);
481 WorkList.push_back(ExitSU);
482 for (
SUnit &SU : SUnits) {
483 int NodeNum = SU.NodeNum;
484 unsigned Degree = SU.Succs.size();
486 Node2Index[NodeNum] = Degree;
490 assert(SU.Succs.empty() &&
"SUnit should have no successors");
492 WorkList.push_back(&SU);
497 while (!WorkList.empty()) {
498 SUnit *SU = WorkList.back();
507 WorkList.push_back(SU);
515 for (
SUnit &SU : SUnits) {
516 for (
const SDep &
PD : SU.Preds) {
518 "Wrong topological sorting");
525 int UpperBound, LowerBound;
526 LowerBound = Node2Index[Y->
NodeNum];
527 UpperBound = Node2Index[X->
NodeNum];
528 bool HasLoop =
false;
530 if (LowerBound < UpperBound) {
533 DFS(Y, UpperBound, HasLoop);
534 assert(!HasLoop &&
"Inserted edge creates a loop!");
536 Shift(Visited, LowerBound, UpperBound);
544 void ScheduleDAGTopologicalSort::DFS(
const SUnit *SU,
int UpperBound,
546 std::vector<const SUnit*> WorkList;
547 WorkList.
reserve(SUnits.size());
549 WorkList.push_back(SU);
551 SU = WorkList.back();
554 for (
const SDep &SuccDep
558 if (s >= Node2Index.size())
560 if (Node2Index[s] == UpperBound) {
565 if (!Visited.
test(s) && Node2Index[s] < UpperBound) {
566 WorkList.push_back(SuccDep.
getSUnit());
569 }
while (!WorkList.empty());
573 const SUnit &TargetSU,
575 std::vector<const SUnit*> WorkList;
576 int LowerBound = Node2Index[StartSU.
NodeNum];
577 int UpperBound = Node2Index[TargetSU.
NodeNum];
580 std::vector<int> Nodes;
582 if (LowerBound > UpperBound) {
587 WorkList.reserve(SUnits.size());
592 WorkList.push_back(&StartSU);
594 const SUnit *SU = WorkList.back();
596 for (
int I = SU->
Succs.size()-1;
I >= 0; --
I) {
602 if (Node2Index[s] == UpperBound) {
607 if (!Visited.
test(s) && Node2Index[s] < UpperBound) {
609 WorkList.push_back(Succ);
612 }
while (!WorkList.empty());
620 VisitedBack.
resize(SUnits.size());
626 WorkList.push_back(&TargetSU);
628 const SUnit *SU = WorkList.back();
630 for (
int I = SU->
Preds.size()-1;
I >= 0; --
I) {
636 if (Node2Index[s] == LowerBound) {
640 if (!VisitedBack.
test(s) && Visited.
test(s)) {
642 WorkList.push_back(Pred);
646 }
while (!WorkList.empty());
648 assert(Found &&
"Error in SUnit Graph!");
653 void ScheduleDAGTopologicalSort::Shift(
BitVector& Visited,
int LowerBound,
659 for (i = LowerBound; i <= UpperBound; ++i) {
661 int w = Index2Node[i];
662 if (Visited.
test(w)) {
668 Allocate(w, i - shift);
672 for (
unsigned LI : L) {
673 Allocate(LI, i - shift);
682 for (
const SDep &PredDep : TargetSU->
Preds)
690 const SUnit *TargetSU) {
693 int UpperBound, LowerBound;
694 LowerBound = Node2Index[TargetSU->
NodeNum];
695 UpperBound = Node2Index[SU->
NodeNum];
696 bool HasLoop =
false;
698 if (LowerBound < UpperBound) {
701 DFS(TargetSU, UpperBound, HasLoop);
706 void ScheduleDAGTopologicalSort::Allocate(
int n,
int index) {
707 Node2Index[n] = index;
708 Index2Node[index] = n;
713 : SUnits(sunits), ExitSU(exitsu) {}
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Weak DAG edge linking a chain of clustered instrs.
void push_back(const T &Elt)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
unsigned NumPreds
of SDep::Data preds.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Describe properties that are true of each instruction in the target description file.
void dumpAll(const ScheduleDAG *G) const
virtual ~ScheduleHazardRecognizer()
void removePred(const SDep &D)
Removes the specified edge as a pred of the current node if it exists.
void reserve(size_type N)
bool isAssignedRegDep() const
Tests if this is a Data dependence that is associated with a register.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
A register anti-dependence (aka WAR).
bool isScheduled
True once scheduled.
unsigned getHeight() const
Returns the height of this node, which is the length of the maximum path down to any node which has n...
SmallVectorImpl< SDep >::iterator pred_iterator
unsigned NumSuccs
of SDep::Data sucss.
unsigned NumSuccsLeft
of succs not scheduled.
bool isWeak() const
Tests if this a weak dependence.
const HexagonInstrInfo * TII
Kind getKind() const
Returns an enum value representing the kind of the dependence.
raw_ostream & print(raw_ostream &O, const TargetRegisterInfo *TRI=nullptr) const
Regular data dependence (aka true-dependence).
void InitDAGTopologicalSorting()
Creates the initial topological ordering from the DAG to be scheduled.
LLVM_NODISCARD bool empty() const
A register output-dependence (aka WAW).
std::vector< int > GetSubGraph(const SUnit &StartSU, const SUnit &TargetSU, bool &Success)
Returns an array of SUs that are both in the successor subtree of StartSU and in the predecessor subt...
static cl::opt< bool > StressSchedOpt("stress-sched", cl::Hidden, cl::init(false), cl::desc("Stress test instruction scheduling"))
unsigned WeakSuccsLeft
of weak succs not scheduled.
void dump(const ScheduleDAG *G) const
unsigned NumPredsLeft
of preds not scheduled.
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
void setDepthToAtLeast(unsigned NewDepth)
If NewDepth is greater than this node's depth value, sets it to be the new depth value.
void setHeightToAtLeast(unsigned NewHeight)
If NewDepth is greater than this node's depth value, set it to be the new height value.
initializer< Ty > init(const Ty &Val)
void setDepthDirty()
Sets a flag in this node to indicate that its stored Depth value will require recomputation the next ...
bool WillCreateCycle(SUnit *TargetSU, SUnit *SU)
Returns true if addPred(TargetSU, SU) creates a cycle.
unsigned const MachineRegisterInfo * MRI
void clearDAG()
Clears the DAG state (between regions).
unsigned short Latency
Node latency.
unsigned getLatency() const
Returns the latency value for this edge, which roughly means the minimum number of cycles that must e...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
raw_ostream & print(raw_ostream &O, const SUnit *N=nullptr, const SUnit *X=nullptr) const
void RemovePred(SUnit *M, SUnit *N)
Updates the topological ordering to accommodate an an edge to be removed from the specified node N fr...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Any other ordering dependency.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
unsigned WeakPredsLeft
of weak preds not scheduled.
An unknown scheduling barrier.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool test(unsigned Idx) const
LLVM_NODISCARD T pop_back_val()
Nonvolatile load/Store instructions that may alias.
Represents one node in the SelectionDAG.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
bool IsReachable(const SUnit *SU, const SUnit *TargetSU)
Checks if SU is reachable from TargetSU.
void biasCriticalPath()
Orders this node's predecessor edges such that the critical path edge occurs first.
ScheduleDAG(MachineFunction &mf)
typename SuperClass::iterator iterator
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
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 setLatency(unsigned Lat)
Sets the latency for this edge.
SUnit EntrySU
Special node for the region entry.
unsigned VerifyScheduledDAG(bool isBottomUp)
Verifies that all SUnits were scheduled and that their state is consistent.
SUnit ExitSU
Special node for the region exit.
const TargetRegisterInfo * TRI
Target processor register info.
unsigned short NumRegDefsLeft
of reg defs with no scheduled use.
const TargetInstrInfo * TII
Target instruction information.
unsigned NodeNum
Entry # of node in the node vector.
void setHeightDirty()
Sets a flag in this node to indicate that its stored Height value will require recomputation the next...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual void dumpNode(const SUnit *SU) const =0
bool addPred(const SDep &D, bool Required=true)
Adds the specified edge as a pred of the current node if not already.
SmallVector< SDep, 4 > Succs
All sunit successors.
Arbitrary strong DAG edge (no real dependence).
This class implements an extremely fast bulk output stream that can only output to a stream...
void AddPred(SUnit *Y, SUnit *X)
Updates the topological ordering to accommodate an edge to be added from SUnit X to SUnit Y...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
std::vector< SUnit > SUnits
The scheduling units.
unsigned getReg() const
Returns the register associated with this edge.
ScheduleDAGTopologicalSort(std::vector< SUnit > &SUnits, SUnit *ExitSU)
Scheduling unit. This is a node in the scheduling DAG.
Nonvolatile load/Store instructions that must alias.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.