25 #include "llvm/Config/llvm-config.h" 39 #define DEBUG_TYPE "pre-RA-sched" 44 cl::desc(
"Stress test instruction scheduling"));
47 void SchedulingPriorityQueue::anchor() {}
50 :
TM(mf.getTarget()),
TII(mf.getSubtarget().getInstrInfo()),
51 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
52 MRI(mf.getRegInfo()) {
74 case Data: OS <<
"Data";
break;
75 case Anti: OS <<
"Anti";
break;
76 case Output: OS <<
"Out ";
break;
77 case Order: OS <<
"Ord ";
break;
83 if (TRI && isAssignedRegDep())
92 switch(Contents.OrdKind) {
93 case Barrier: OS <<
" Barrier";
break;
95 case MustAliasMem: OS <<
" Memory";
break;
96 case Artificial: OS <<
" Artificial";
break;
97 case Weak: OS <<
" Weak";
break;
98 case Cluster: OS <<
" Cluster";
break;
108 for (
SDep &PredDep : Preds) {
111 if (!Required && PredDep.getSUnit() == D.
getSUnit())
113 if (PredDep.overlaps(D)) {
117 SUnit *PredSU = PredDep.getSUnit();
119 SDep ForwardD = PredDep;
122 if (SuccDep == ForwardD) {
139 "NumPreds will overflow!");
141 "NumSuccs will overflow!");
151 "NumPredsLeft will overflow!");
161 "NumSuccsLeft will overflow!");
166 N->
Succs.push_back(P);
168 this->setDepthDirty();
177 if (I == Preds.
end())
184 assert(Succ != N->
Succs.end() &&
"Mismatching preds / succs lists!");
185 N->
Succs.erase(Succ);
189 assert(NumPreds > 0 &&
"NumPreds will underflow!");
198 assert(NumPredsLeft > 0 &&
"NumPredsLeft will underflow!");
210 if (
P.getLatency() != 0) {
211 this->setDepthDirty();
217 if (!isDepthCurrent)
return;
222 SU->isDepthCurrent =
false;
225 if (SuccSU->isDepthCurrent)
228 }
while (!WorkList.
empty());
232 if (!isHeightCurrent)
return;
237 SU->isHeightCurrent =
false;
240 if (PredSU->isHeightCurrent)
243 }
while (!WorkList.
empty());
247 if (NewDepth <= getDepth())
251 isDepthCurrent =
true;
255 if (NewHeight <= getHeight())
259 isHeightCurrent =
true;
263 void SUnit::ComputeDepth() {
270 unsigned MaxPredDepth = 0;
273 if (PredSU->isDepthCurrent)
274 MaxPredDepth =
std::max(MaxPredDepth,
284 if (MaxPredDepth != Cur->Depth) {
286 Cur->Depth = MaxPredDepth;
288 Cur->isDepthCurrent =
true;
290 }
while (!WorkList.
empty());
294 void SUnit::ComputeHeight() {
301 unsigned MaxSuccHeight = 0;
304 if (SuccSU->isHeightCurrent)
305 MaxSuccHeight =
std::max(MaxSuccHeight,
315 if (MaxSuccHeight != Cur->Height) {
317 Cur->Height = MaxSuccHeight;
319 Cur->isHeightCurrent =
true;
321 }
while (!WorkList.
empty());
329 unsigned MaxDepth = BestI->getSUnit()->getDepth();
335 if (BestI != Preds.begin())
339 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 342 const SUnit *Entry,
const SUnit *Exit)
const {
345 else if (
this == Exit)
348 OS <<
"SU(" << NodeNum <<
")";
367 dbgs() <<
" # preds left : " << NumPredsLeft <<
"\n";
368 dbgs() <<
" # succs left : " << NumSuccsLeft <<
"\n";
370 dbgs() <<
" # weak preds left : " << WeakPredsLeft <<
"\n";
372 dbgs() <<
" # weak succs left : " << WeakSuccsLeft <<
"\n";
373 dbgs() <<
" # rdefs left : " << NumRegDefsLeft <<
"\n";
375 dbgs() <<
" Depth : " << getDepth() <<
"\n";
376 dbgs() <<
" Height : " << getHeight() <<
"\n";
378 if (Preds.size() != 0) {
379 dbgs() <<
" Predecessors:\n";
380 for (
const SDep &Dep : Preds) {
382 Dep.getSUnit()->print(
dbgs(), G);
dbgs() <<
": ";
386 if (Succs.size() != 0) {
387 dbgs() <<
" Successors:\n";
388 for (
const SDep &Dep : Succs) {
390 Dep.getSUnit()->print(
dbgs(), G);
dbgs() <<
": ";
399 bool AnyNotSched =
false;
400 unsigned DeadNodes = 0;
408 dbgs() <<
"*** Scheduling failed! ***\n";
410 dbgs() <<
"has not been scheduled!\n";
417 dbgs() <<
"*** Scheduling failed! ***\n";
419 dbgs() <<
"has an unexpected " 420 << (isBottomUp ?
"Height" :
"Depth") <<
" value!\n";
426 dbgs() <<
"*** Scheduling failed! ***\n";
428 dbgs() <<
"has successors left!\n";
434 dbgs() <<
"*** Scheduling failed! ***\n";
436 dbgs() <<
"has predecessors left!\n";
442 return SUnits.size() - DeadNodes;
473 unsigned DAGSize =
SUnits.size();
474 std::vector<SUnit*> WorkList;
475 WorkList.reserve(DAGSize);
477 Index2Node.resize(DAGSize);
478 Node2Index.resize(DAGSize);
482 WorkList.push_back(
ExitSU);
484 int NodeNum = SU.NodeNum;
485 unsigned Degree = SU.Succs.size();
487 Node2Index[NodeNum] = Degree;
491 assert(SU.Succs.empty() &&
"SUnit should have no successors");
493 WorkList.push_back(&SU);
498 while (!WorkList.empty()) {
499 SUnit *SU = WorkList.back();
508 WorkList.push_back(SU);
512 Visited.resize(DAGSize);
516 for (
SUnit &SU : SUnits) {
517 for (
const SDep &
PD : SU.Preds) {
519 "Wrong topological sorting");
526 int UpperBound, LowerBound;
527 LowerBound = Node2Index[Y->
NodeNum];
528 UpperBound = Node2Index[X->
NodeNum];
529 bool HasLoop =
false;
531 if (LowerBound < UpperBound) {
534 DFS(Y, UpperBound, HasLoop);
535 assert(!HasLoop &&
"Inserted edge creates a loop!");
537 Shift(Visited, LowerBound, UpperBound);
545 void ScheduleDAGTopologicalSort::DFS(
const SUnit *SU,
int UpperBound,
547 std::vector<const SUnit*> WorkList;
548 WorkList.reserve(
SUnits.size());
550 WorkList.push_back(SU);
552 SU = WorkList.back();
555 for (
const SDep &SuccDep
557 unsigned s = SuccDep.getSUnit()->
NodeNum;
559 if (s >= Node2Index.size())
561 if (Node2Index[s] == UpperBound) {
566 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
567 WorkList.push_back(SuccDep.getSUnit());
570 }
while (!WorkList.empty());
574 const SUnit &TargetSU,
576 std::vector<const SUnit*> WorkList;
577 int LowerBound = Node2Index[StartSU.
NodeNum];
578 int UpperBound = Node2Index[TargetSU.
NodeNum];
581 std::vector<int> Nodes;
583 if (LowerBound > UpperBound) {
588 WorkList.reserve(
SUnits.size());
593 WorkList.push_back(&StartSU);
595 const SUnit *SU = WorkList.back();
597 for (
int I = SU->
Succs.size()-1;
I >= 0; --
I) {
603 if (Node2Index[s] == UpperBound) {
608 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
610 WorkList.push_back(Succ);
613 }
while (!WorkList.empty());
627 WorkList.push_back(&TargetSU);
629 const SUnit *SU = WorkList.back();
631 for (
int I = SU->
Preds.size()-1;
I >= 0; --
I) {
637 if (Node2Index[s] == LowerBound) {
641 if (!VisitedBack.
test(s) && Visited.test(s)) {
643 WorkList.push_back(Pred);
647 }
while (!WorkList.empty());
649 assert(Found &&
"Error in SUnit Graph!");
654 void ScheduleDAGTopologicalSort::Shift(
BitVector& Visited,
int LowerBound,
660 for (i = LowerBound; i <= UpperBound; ++i) {
662 int w = Index2Node[i];
663 if (Visited.
test(w)) {
669 Allocate(w, i - shift);
673 for (
unsigned LI : L) {
674 Allocate(LI, i - shift);
681 if (IsReachable(SU, TargetSU))
683 for (
const SDep &PredDep : TargetSU->
Preds)
685 IsReachable(SU, PredDep.
getSUnit()))
691 const SUnit *TargetSU) {
694 int UpperBound, LowerBound;
695 LowerBound = Node2Index[TargetSU->
NodeNum];
696 UpperBound = Node2Index[SU->
NodeNum];
697 bool HasLoop =
false;
699 if (LowerBound < UpperBound) {
702 DFS(TargetSU, UpperBound, HasLoop);
707 void ScheduleDAGTopologicalSort::Allocate(
int n,
int index) {
708 Node2Index[n] = index;
709 Index2Node[index] = n;
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
void push_back(const T &Elt)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
unsigned NumPreds
of SDep::Data preds.
Compute iterated dominance frontiers using a linear time algorithm.
void dump(const ScheduleDAG *G) const
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...
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.
raw_ostream & print(raw_ostream &O, const SUnit *Entry=nullptr, const SUnit *Exit=nullptr) const
virtual ~ScheduleHazardRecognizer()
bool test(unsigned Idx) const
unsigned const TargetRegisterInfo * TRI
void removePred(const SDep &D)
Removes the specified edge as a pred of the current node if it exists.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
bool isAssignedRegDep() const
Tests if this is a Data dependence that is associated with a register.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
bool isScheduled
True once scheduled.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
raw_ostream & print(raw_ostream &O, const TargetRegisterInfo *TRI=nullptr) const
SmallVectorImpl< SDep >::iterator pred_iterator
unsigned NumSuccs
of SDep::Data sucss.
unsigned getLatency() const
Returns the latency value for this edge, which roughly means the minimum number of cycles that must e...
unsigned NumSuccsLeft
of succs not scheduled.
const HexagonInstrInfo * TII
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
Regular data dependence (aka true-dependence).
void InitDAGTopologicalSorting()
Creates the initial topological ordering from the DAG to be scheduled.
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.
unsigned NumPredsLeft
of preds not scheduled.
static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst)
Gets latency information for Inst, based on DC information.
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
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.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
void clearDAG()
Clears the DAG state (between regions).
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
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...
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
ScheduleDAG(MachineFunction &mf)
typename SuperClass::iterator iterator
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.
unsigned getHeight() const
Returns the height of this node, which is the length of the maximum path down to any node which has n...
SUnit ExitSU
Special node for the region exit.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
const TargetRegisterInfo * TRI
Target processor register info.
LLVM_NODISCARD bool empty() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
const TargetInstrInfo * TII
Target instruction information.
Kind getKind() const
Returns an enum value representing the kind of the dependence.
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.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
SmallVector< SDep, 4 > Succs
All sunit successors.
bool isWeak() const
Tests if this a weak 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...
void dumpAll(const ScheduleDAG *G) const
std::vector< SUnit > SUnits
The scheduling units.
ScheduleDAGTopologicalSort(std::vector< SUnit > &SUnits, SUnit *ExitSU)
Scheduling unit. This is a node in the scheduling DAG.