25 #include "llvm/Config/llvm-config.h" 39 #define DEBUG_TYPE "pre-RA-sched" 41 STATISTIC(NumNewPredsAdded,
"Number of times a single predecessor was added");
43 "Number of times the topological order has been recomputed");
48 cl::desc(
"Stress test instruction scheduling"));
51 void SchedulingPriorityQueue::anchor() {}
54 :
TM(mf.getTarget()),
TII(mf.getSubtarget().getInstrInfo()),
55 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
56 MRI(mf.getRegInfo()) {
78 case Anti:
dbgs() <<
"Anti";
break;
79 case Output:
dbgs() <<
"Out ";
break;
80 case Order:
dbgs() <<
"Ord ";
break;
86 if (TRI && isAssignedRegDep())
95 switch(Contents.OrdKind) {
98 case MustAliasMem:
dbgs() <<
" Memory";
break;
99 case Artificial:
dbgs() <<
" Artificial";
break;
100 case Weak:
dbgs() <<
" Weak";
break;
101 case Cluster:
dbgs() <<
" Cluster";
break;
109 for (
SDep &PredDep : Preds) {
112 if (!Required && PredDep.getSUnit() == D.
getSUnit())
114 if (PredDep.overlaps(D)) {
118 SUnit *PredSU = PredDep.getSUnit();
120 SDep ForwardD = PredDep;
123 if (SuccDep == ForwardD) {
140 "NumPreds will overflow!");
142 "NumSuccs will overflow!");
152 "NumPredsLeft will overflow!");
162 "NumSuccsLeft will overflow!");
167 N->
Succs.push_back(P);
169 this->setDepthDirty();
178 if (I == Preds.
end())
185 assert(Succ != N->
Succs.end() &&
"Mismatching preds / succs lists!");
186 N->
Succs.erase(Succ);
190 assert(NumPreds > 0 &&
"NumPreds will underflow!");
199 assert(NumPredsLeft > 0 &&
"NumPredsLeft will underflow!");
211 if (
P.getLatency() != 0) {
212 this->setDepthDirty();
218 if (!isDepthCurrent)
return;
223 SU->isDepthCurrent =
false;
226 if (SuccSU->isDepthCurrent)
229 }
while (!WorkList.
empty());
233 if (!isHeightCurrent)
return;
238 SU->isHeightCurrent =
false;
241 if (PredSU->isHeightCurrent)
244 }
while (!WorkList.
empty());
252 isDepthCurrent =
true;
256 if (NewHeight <= getHeight())
260 isHeightCurrent =
true;
264 void SUnit::ComputeDepth() {
271 unsigned MaxPredDepth = 0;
274 if (PredSU->isDepthCurrent)
275 MaxPredDepth =
std::max(MaxPredDepth,
285 if (MaxPredDepth != Cur->Depth) {
287 Cur->Depth = MaxPredDepth;
289 Cur->isDepthCurrent =
true;
291 }
while (!WorkList.
empty());
295 void SUnit::ComputeHeight() {
302 unsigned MaxSuccHeight = 0;
305 if (SuccSU->isHeightCurrent)
306 MaxSuccHeight =
std::max(MaxSuccHeight,
316 if (MaxSuccHeight != Cur->Height) {
318 Cur->Height = MaxSuccHeight;
320 Cur->isHeightCurrent =
true;
322 }
while (!WorkList.
empty());
330 unsigned MaxDepth = BestI->getSUnit()->getDepth();
336 if (BestI != Preds.begin())
340 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 342 dbgs() <<
" # preds left : " << NumPredsLeft <<
"\n";
343 dbgs() <<
" # succs left : " << NumSuccsLeft <<
"\n";
345 dbgs() <<
" # weak preds left : " << WeakPredsLeft <<
"\n";
347 dbgs() <<
" # weak succs left : " << WeakSuccsLeft <<
"\n";
348 dbgs() <<
" # rdefs left : " << NumRegDefsLeft <<
"\n";
351 dbgs() <<
" Height : " << getHeight() <<
"\n";
366 if (SU.
Preds.size() > 0) {
367 dbgs() <<
" Predecessors:\n";
376 if (SU.
Succs.size() > 0) {
377 dbgs() <<
" Successors:\n";
391 bool AnyNotSched =
false;
392 unsigned DeadNodes = 0;
400 dbgs() <<
"*** Scheduling failed! ***\n";
402 dbgs() <<
"has not been scheduled!\n";
409 dbgs() <<
"*** Scheduling failed! ***\n";
411 dbgs() <<
"has an unexpected " 412 << (isBottomUp ?
"Height" :
"Depth") <<
" value!\n";
418 dbgs() <<
"*** Scheduling failed! ***\n";
420 dbgs() <<
"has successors left!\n";
426 dbgs() <<
"*** Scheduling failed! ***\n";
428 dbgs() <<
"has predecessors left!\n";
434 return SUnits.size() - DeadNodes;
470 unsigned DAGSize =
SUnits.size();
471 std::vector<SUnit*> WorkList;
472 WorkList.reserve(DAGSize);
474 Index2Node.resize(DAGSize);
475 Node2Index.resize(DAGSize);
479 WorkList.push_back(
ExitSU);
481 int NodeNum = SU.NodeNum;
482 unsigned Degree = SU.Succs.size();
484 Node2Index[NodeNum] = Degree;
488 assert(SU.Succs.empty() &&
"SUnit should have no successors");
490 WorkList.push_back(&SU);
495 while (!WorkList.empty()) {
496 SUnit *SU = WorkList.back();
505 WorkList.push_back(SU);
509 Visited.resize(DAGSize);
514 for (
SUnit &SU : SUnits) {
515 for (
const SDep &
PD : SU.Preds) {
517 "Wrong topological sorting");
523 void ScheduleDAGTopologicalSort::FixOrder() {
526 InitDAGTopologicalSorting();
531 for (
auto &U : Updates)
532 AddPred(U.first, U.second);
540 Dirty = Dirty || Updates.size() > 10;
545 Updates.emplace_back(Y, X);
549 int UpperBound, LowerBound;
550 LowerBound = Node2Index[Y->
NodeNum];
551 UpperBound = Node2Index[X->
NodeNum];
552 bool HasLoop =
false;
554 if (LowerBound < UpperBound) {
557 DFS(Y, UpperBound, HasLoop);
558 assert(!HasLoop &&
"Inserted edge creates a loop!");
560 Shift(Visited, LowerBound, UpperBound);
570 void ScheduleDAGTopologicalSort::DFS(
const SUnit *SU,
int UpperBound,
572 std::vector<const SUnit*> WorkList;
573 WorkList.reserve(
SUnits.size());
575 WorkList.push_back(SU);
577 SU = WorkList.back();
580 for (
const SDep &SuccDep
582 unsigned s = SuccDep.getSUnit()->
NodeNum;
584 if (s >= Node2Index.size())
586 if (Node2Index[s] == UpperBound) {
591 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
592 WorkList.push_back(SuccDep.getSUnit());
595 }
while (!WorkList.empty());
599 const SUnit &TargetSU,
601 std::vector<const SUnit*> WorkList;
602 int LowerBound = Node2Index[StartSU.
NodeNum];
603 int UpperBound = Node2Index[TargetSU.
NodeNum];
606 std::vector<int> Nodes;
608 if (LowerBound > UpperBound) {
613 WorkList.reserve(
SUnits.size());
618 WorkList.push_back(&StartSU);
620 const SUnit *SU = WorkList.back();
622 for (
int I = SU->
Succs.size()-1;
I >= 0; --
I) {
628 if (Node2Index[s] == UpperBound) {
633 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
635 WorkList.push_back(Succ);
638 }
while (!WorkList.empty());
652 WorkList.push_back(&TargetSU);
654 const SUnit *SU = WorkList.back();
656 for (
int I = SU->
Preds.size()-1;
I >= 0; --
I) {
662 if (Node2Index[s] == LowerBound) {
666 if (!VisitedBack.
test(s) && Visited.test(s)) {
672 }
while (!WorkList.empty());
674 assert(Found &&
"Error in SUnit Graph!");
679 void ScheduleDAGTopologicalSort::Shift(
BitVector& Visited,
int LowerBound,
685 for (i = LowerBound; i <= UpperBound; ++i) {
687 int w = Index2Node[i];
688 if (Visited.
test(w)) {
694 Allocate(w, i - shift);
698 for (
unsigned LI : L) {
699 Allocate(LI, i - shift);
707 if (IsReachable(SU, TargetSU))
709 for (
const SDep &PredDep : TargetSU->
Preds)
711 IsReachable(SU, PredDep.
getSUnit()))
717 const SUnit *TargetSU) {
721 int UpperBound, LowerBound;
722 LowerBound = Node2Index[TargetSU->
NodeNum];
723 UpperBound = Node2Index[SU->
NodeNum];
724 bool HasLoop =
false;
726 if (LowerBound < UpperBound) {
729 DFS(TargetSU, UpperBound, HasLoop);
734 void ScheduleDAGTopologicalSort::Allocate(
int n,
int index) {
735 Node2Index[n] = index;
736 Index2Node[index] = n;
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
unsigned NumPreds
of SDep::Data preds.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
void push_back(const T &Elt)
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.
virtual void dumpNode(const SUnit &SU) const =0
virtual ~ScheduleHazardRecognizer()
bool test(unsigned Idx) const
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void removePred(const SDep &D)
Removes the specified edge as a pred of the current node if it exists.
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.
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")
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
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 dumpNodeAll(const SUnit &SU) const
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 NewHeight is greater than this node's height 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).
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void RemovePred(SUnit *M, SUnit *N)
Updates the topological ordering to accommodate an an edge to be removed from the specified node N fr...
void dumpNodeName(const SUnit &SU) const
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...
void dump(const TargetRegisterInfo *TRI=nullptr) const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
void AddPredQueued(SUnit *Y, SUnit *X)
Queues an update to the topological ordering to accommodate an edge to be added from SUnit X to SUnit...
static unsigned getDepth(const SmallVectorImpl< const MachineBasicBlock *> &Stack, const MachineBasicBlock *MBB)
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.
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())
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.
void AddPred(SUnit *Y, SUnit *X)
Updates the topological ordering to accommodate an edge to be added from SUnit X to SUnit Y...
std::vector< SUnit > SUnits
The scheduling units.
void dumpAttributes() const
ScheduleDAGTopologicalSort(std::vector< SUnit > &SUnits, SUnit *ExitSU)
Scheduling unit. This is a node in the scheduling DAG.