25#include "llvm/Config/llvm-config.h"
38#define DEBUG_TYPE "pre-RA-sched"
40STATISTIC(NumNewPredsAdded,
"Number of times a single predecessor was added");
42 "Number of times the topological order has been recomputed");
44void SchedulingPriorityQueue::anchor() {}
47 :
TM(mf.getTarget()),
TII(mf.getSubtarget().getInstrInfo()),
48 TRI(mf.getSubtarget().getRegisterInfo()),
MF(mf),
49 MRI(mf.getRegInfo()) {
64 if (!
Node || !
Node->isMachineOpcode())
return nullptr;
65 return &
TII->get(
Node->getMachineOpcode());
88 switch(Contents.OrdKind) {
105 if (!Required && PredDep.getSUnit() ==
D.getSUnit())
107 if (PredDep.overlaps(
D)) {
110 if (PredDep.getLatency() <
D.getLatency()) {
111 SUnit *PredSU = PredDep.getSUnit();
113 SDep ForwardD = PredDep;
116 if (SuccDep == ForwardD) {
121 PredDep.setLatency(
D.getLatency());
136 "NumPreds will overflow!");
137 assert(
N->NumSuccs < std::numeric_limits<unsigned>::max() &&
138 "NumSuccs will overflow!");
142 if (!
N->isScheduled) {
148 "NumPredsLeft will overflow!");
157 assert(
N->NumSuccsLeft < std::numeric_limits<unsigned>::max() &&
158 "NumSuccsLeft will overflow!");
163 N->Succs.push_back(
P);
179 assert(Succ !=
N->Succs.end() &&
"Mismatching preds / succs lists!");
183 assert(
N->NumSuccs > 0 &&
"NumSuccs will underflow!");
187 if (!
N->isScheduled) {
198 assert(
N->WeakSuccsLeft > 0 &&
"WeakSuccsLeft will underflow!");
201 assert(
N->NumSuccsLeft > 0 &&
"NumSuccsLeft will underflow!");
205 N->Succs.erase(Succ);
212 if (!isDepthCurrent)
return;
217 SU->isDepthCurrent =
false;
220 if (SuccSU->isDepthCurrent)
223 }
while (!WorkList.
empty());
227 if (!isHeightCurrent)
return;
232 SU->isHeightCurrent =
false;
235 if (PredSU->isHeightCurrent)
238 }
while (!WorkList.
empty());
246 isDepthCurrent =
true;
254 isHeightCurrent =
true;
258void SUnit::ComputeDepth() {
266 bool Descended =
false;
269 if (!PredSU->isDepthCurrent) {
278 unsigned MaxPredDepth = 0;
279 for (
const SDep &PredDep : Cur->
Preds)
280 MaxPredDepth = std::max(MaxPredDepth,
282 Cur->Depth = MaxPredDepth;
283 Cur->isDepthCurrent =
true;
284 }
while (!WorkList.
empty());
288void SUnit::ComputeHeight() {
294 bool Descended =
false;
295 for (
const SDep &SuccDep : Cur->
Succs) {
297 if (!SuccSU->isHeightCurrent) {
306 unsigned MaxSuccHeight = 0;
307 for (
const SDep &SuccDep : Cur->
Succs)
308 MaxSuccHeight = std::max(MaxSuccHeight, SuccDep.
getSUnit()->Height +
310 Cur->Height = MaxSuccHeight;
311 Cur->isHeightCurrent =
true;
312 }
while (!WorkList.
empty());
320 unsigned MaxDepth = BestI->getSUnit()->getDepth();
323 if (
I->getKind() ==
SDep::Data &&
I->getSUnit()->getDepth() > MaxDepth) {
324 MaxDepth =
I->getSUnit()->getDepth();
328 if (BestI !=
Preds.begin())
332#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
335 "use ScheduleDAG::dumpNodeName for boundary nodes");
336 return OS <<
"SU(" << SU.
NodeNum <<
")";
340#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
369 if (SU.
Preds.size() > 0) {
370 dbgs() <<
" Predecessors:\n";
379 if (SU.
Succs.size() > 0) {
380 dbgs() <<
" Successors:\n";
394 bool AnyNotSched =
false;
395 unsigned DeadNodes = 0;
403 dbgs() <<
"*** Scheduling failed! ***\n";
405 dbgs() <<
"has not been scheduled!\n";
410 unsigned(std::numeric_limits<int>::max())) {
412 dbgs() <<
"*** Scheduling failed! ***\n";
414 dbgs() <<
"has an unexpected "
415 << (isBottomUp ?
"Height" :
"Depth") <<
" value!\n";
421 dbgs() <<
"*** Scheduling failed! ***\n";
423 dbgs() <<
"has successors left!\n";
429 dbgs() <<
"*** Scheduling failed! ***\n";
431 dbgs() <<
"has predecessors left!\n";
437 return SUnits.size() - DeadNodes;
474 unsigned DAGSize = SUnits.size();
476 Index2Node.resize(DAGSize);
477 Node2Index.resize(DAGSize);
479 WorkList.reserve(DAGSize);
484 WorkList.push_back(ExitSU);
485 for (
SUnit &SU : SUnits) {
486 int NodeNum = SU.NodeNum;
487 unsigned Degree = SU.Succs.size();
489 Node2Index[NodeNum] = Degree;
493 assert(SU.Succs.empty() &&
"SUnit should have no successors");
495 WorkList.push_back(&SU);
500 while (!WorkList.empty()) {
501 const SUnit *SU = WorkList.back();
510 WorkList.push_back(SU);
514 Visited.resize(DAGSize);
519 for (
SUnit &SU : SUnits) {
520 for (
const SDep &PD : SU.Preds) {
521 assert(Node2Index[SU.NodeNum] > Node2Index[PD.getSUnit()->NodeNum] &&
522 "Wrong topological sorting");
528void ScheduleDAGTopologicalSort::FixOrder() {
536 for (
auto &U : Updates)
545 Dirty = Dirty || Updates.size() > 10;
550 Updates.emplace_back(
Y,
X);
554 int UpperBound, LowerBound;
555 LowerBound = Node2Index[
Y->NodeNum];
556 UpperBound = Node2Index[
X->NodeNum];
557 bool HasLoop =
false;
559 if (LowerBound < UpperBound) {
562 DFS(
Y, UpperBound, HasLoop);
563 assert(!HasLoop &&
"Inserted edge creates a loop!");
565 Shift(Visited, LowerBound, UpperBound);
576void ScheduleDAGTopologicalSort::DFS(
const SUnit *SU,
int UpperBound,
582 SU = WorkList.
back();
588 if (s >= Node2Index.size())
590 if (Node2Index[s] == UpperBound) {
595 if (!Visited.
test(s) && Node2Index[s] < UpperBound) {
599 }
while (!WorkList.empty());
603 const SUnit &TargetSU,
605 int LowerBound = Node2Index[StartSU.
NodeNum];
606 int UpperBound = Node2Index[TargetSU.
NodeNum];
609 std::vector<int> Nodes;
611 if (LowerBound > UpperBound) {
621 WorkList.push_back(&StartSU);
623 const SUnit *SU = WorkList.back();
626 const SUnit *Succ = SD.getSUnit();
631 if (Node2Index[s] == UpperBound) {
636 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
638 WorkList.push_back(Succ);
641 }
while (!WorkList.empty());
649 VisitedBack.
resize(SUnits.size());
655 WorkList.push_back(&TargetSU);
657 const SUnit *SU = WorkList.back();
660 const SUnit *Pred = SD.getSUnit();
661 unsigned s = Pred->NodeNum;
663 if (Pred->isBoundaryNode())
665 if (Node2Index[s] == LowerBound) {
669 if (!VisitedBack.
test(s) && Visited.test(s)) {
675 }
while (!WorkList.empty());
677 assert(Found &&
"Error in SUnit Graph!");
682void ScheduleDAGTopologicalSort::Shift(
BitVector& Visited,
int LowerBound,
688 for (i = LowerBound; i <= UpperBound; ++i) {
690 int w = Index2Node[i];
691 if (Visited.
test(w)) {
697 Allocate(w, i - shift);
701 for (
unsigned LI : L) {
702 Allocate(LI, i - shift);
712 for (
const SDep &PredDep : TargetSU->
Preds)
720 assert(SU->
NodeNum == Index2Node.size() &&
"Node cannot be added at the end");
721 assert(SU->
NumPreds == 0 &&
"Can only add SU's with no predecessors");
722 Node2Index.push_back(Index2Node.size());
723 Index2Node.push_back(SU->
NodeNum);
724 Visited.resize(Node2Index.size());
728 const SUnit *TargetSU) {
729 assert(TargetSU !=
nullptr &&
"Invalid target SUnit");
730 assert(SU !=
nullptr &&
"Invalid SUnit");
734 int UpperBound, LowerBound;
735 LowerBound = Node2Index[TargetSU->
NodeNum];
736 UpperBound = Node2Index[SU->
NodeNum];
737 bool HasLoop =
false;
739 if (LowerBound < UpperBound) {
740 if (
auto It = Reachable.find({TargetSU->NodeNum, SU->NodeNum});
741 It != Reachable.end()) {
746 DFS(TargetSU, UpperBound, HasLoop);
756void ScheduleDAGTopologicalSort::Allocate(
int n,
int index) {
757 Node2Index[n] = index;
758 Index2Node[index] = n;
763 : SUnits(sunits), ExitSU(exitsu) {}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
bool test(unsigned Idx) const
Returns true if bit Idx is set.
BitVector & reset()
Reset all bits in the bitvector.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
void clear()
Removes all bits from the bitvector.
BitVector & set()
Set all bits in the bitvector.
Describe properties that are true of each instruction in the target description file.
Represents one node in the SelectionDAG.
Kind getKind() const
Returns an enum value representing the kind of the dependence.
@ Output
A register output-dependence (aka WAW).
@ Order
Any other ordering dependency.
@ Anti
A register anti-dependence (aka WAR).
@ Data
Regular data dependence (aka true-dependence).
void setLatency(unsigned Lat)
Sets the latency for this edge.
@ Cluster
Weak DAG edge linking a chain of clustered instrs.
@ Barrier
An unknown scheduling barrier.
@ Artificial
Arbitrary strong DAG edge (no real dependence).
@ MayAliasMem
Nonvolatile load/Store instructions that may alias.
@ Weak
Arbitrary weak DAG edge.
@ MustAliasMem
Nonvolatile load/Store instructions that must alias.
unsigned getLatency() const
Returns the latency value for this edge, which roughly means the minimum number of cycles that must e...
bool isAssignedRegDep() const
Tests if this is a Data dependence that is associated with a register.
Register getReg() const
Returns the register associated with this edge.
LLVM_ABI void dump(const TargetRegisterInfo *TRI=nullptr) const
Scheduling unit. This is a node in the scheduling DAG.
LLVM_ABI void setHeightToAtLeast(unsigned NewHeight)
If NewHeight is greater than this node's height value, set it to be the new height value.
unsigned NodeNum
Entry # of node in the node vector.
LLVM_ABI void biasCriticalPath()
Orders this node's predecessor edges such that the critical path edge occurs first.
unsigned getHeight() const
Returns the height of this node, which is the length of the maximum path down to any node which has n...
LLVM_ABI void setHeightDirty()
Sets a flag in this node to indicate that its stored Height value will require recomputation the next...
LLVM_ABI void removePred(const SDep &D)
Removes the specified edge as a pred of the current node if it exists.
unsigned short Latency
Node latency.
SmallVectorImpl< SDep >::iterator pred_iterator
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
unsigned short NumRegDefsLeft
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 isScheduled
True once scheduled.
unsigned ParentClusterIdx
The parent cluster id.
LLVM_ABI void dumpAttributes() const
SmallVector< SDep, 4 > Succs
All sunit successors.
SUnit(SDNode *node, unsigned nodenum)
Constructs an SUnit for pre-regalloc scheduling to represent an SDNode and any nodes flagged to it.
LLVM_ABI void setDepthDirty()
Sets a flag in this node to indicate that its stored Depth value will require recomputation the next ...
SmallVector< SDep, 4 > Preds
All sunit predecessors.
LLVM_ABI void setDepthToAtLeast(unsigned NewDepth)
If NewDepth is greater than this node's depth value, sets it to be the new depth value.
LLVM_ABI bool addPred(const SDep &D, bool Required=true)
Adds the specified edge as a pred of the current node if not already.
LLVM_ABI void RemovePred(SUnit *M, SUnit *N)
Updates the topological ordering to accommodate an edge to be removed from the specified node N from ...
LLVM_ABI bool WillCreateCycle(SUnit *TargetSU, SUnit *SU)
Returns true if addPred(TargetSU, SU) creates a cycle.
LLVM_ABI void AddSUnitWithoutPredecessors(const SUnit *SU)
Add a SUnit without predecessors to the end of the topological order.
LLVM_ABI ScheduleDAGTopologicalSort(std::vector< SUnit > &SUnits, SUnit *ExitSU)
LLVM_ABI 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...
LLVM_ABI void InitDAGTopologicalSorting()
Creates the initial topological ordering from the DAG to be scheduled.
LLVM_ABI void AddPred(SUnit *Y, SUnit *X)
Updates the topological ordering to accommodate an edge to be added from SUnit X to SUnit Y.
LLVM_ABI bool IsReachable(const SUnit *SU, const SUnit *TargetSU)
Checks if SU is reachable from TargetSU.
LLVM_ABI 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...
MachineRegisterInfo & MRI
Virtual/real register map.
void clearDAG()
Clears the DAG state (between regions).
const TargetInstrInfo * TII
Target instruction information.
std::vector< SUnit > SUnits
The scheduling units.
const TargetRegisterInfo * TRI
Target processor register info.
SUnit EntrySU
Special node for the region entry.
MachineFunction & MF
Machine function.
ScheduleDAG(const ScheduleDAG &)=delete
void dumpNodeAll(const SUnit &SU) const
const TargetMachine & TM
Target processor.
unsigned VerifyScheduledDAG(bool isBottomUp)
Verifies that all SUnits were scheduled and that their state is consistent.
virtual void dumpNode(const SUnit &SU) const =0
void dumpNodeName(const SUnit &SU) const
SUnit ExitSU
Special node for the region exit.
virtual ~ScheduleHazardRecognizer()
typename SuperClass::iterator iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Success
The lock was released successfully.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
LLVM_ABI 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.
const CodeGenOptions & getCodeGenOptions(const LLVMContext &C)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.