LLVM 24.0.0git
ScheduleDAG.cpp
Go to the documentation of this file.
1//===- ScheduleDAG.cpp - Implement the ScheduleDAG class ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file Implements the ScheduleDAG class, which is a base class used by
10/// scheduling implementation classes.
11//
12//===----------------------------------------------------------------------===//
13
15#include "CodeGenOptions.h"
16#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/Statistic.h"
25#include "llvm/Config/llvm-config.h"
27#include "llvm/Support/Debug.h"
29#include <algorithm>
30#include <cassert>
31#include <iterator>
32#include <limits>
33#include <utility>
34#include <vector>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "pre-RA-sched"
39
40STATISTIC(NumNewPredsAdded, "Number of times a single predecessor was added");
41STATISTIC(NumTopoInits,
42 "Number of times the topological order has been recomputed");
43
44void SchedulingPriorityQueue::anchor() {}
45
47 : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()),
48 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
49 MRI(mf.getRegInfo()) {
50#ifndef NDEBUG
51 StressSched = getCodeGenOptions(mf).stress_sched;
52#endif
53}
54
56
58 SUnits.clear();
59 EntrySU = SUnit();
60 ExitSU = SUnit();
61}
62
63const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
64 if (!Node || !Node->isMachineOpcode()) return nullptr;
65 return &TII->get(Node->getMachineOpcode());
66}
67
69 switch (getKind()) {
70 case Data: dbgs() << "Data"; break;
71 case Anti: dbgs() << "Anti"; break;
72 case Output: dbgs() << "Out "; break;
73 case Order: dbgs() << "Ord "; break;
74 }
75
76 switch (getKind()) {
77 case Data:
78 dbgs() << " Latency=" << getLatency();
79 if (TRI && isAssignedRegDep())
80 dbgs() << " Reg=" << printReg(getReg(), TRI);
81 break;
82 case Anti:
83 case Output:
84 dbgs() << " Latency=" << getLatency();
85 break;
86 case Order:
87 dbgs() << " Latency=" << getLatency();
88 switch(Contents.OrdKind) {
89 case Barrier: dbgs() << " Barrier"; break;
90 case MayAliasMem:
91 case MustAliasMem: dbgs() << " Memory"; break;
92 case Artificial: dbgs() << " Artificial"; break;
93 case Weak: dbgs() << " Weak"; break;
94 case Cluster: dbgs() << " Cluster"; break;
95 }
96 break;
97 }
98}
99
100bool SUnit::addPred(const SDep &D, bool Required) {
101 // If this node already has this dependence, don't add a redundant one.
102 for (SDep &PredDep : Preds) {
103 // Zero-latency weak edges may be added purely for heuristic ordering. Don't
104 // add them if another kind of edge already exists.
105 if (!Required && PredDep.getSUnit() == D.getSUnit())
106 return false;
107 if (PredDep.overlaps(D)) {
108 // Extend the latency if needed. Equivalent to
109 // removePred(PredDep) + addPred(D).
110 if (PredDep.getLatency() < D.getLatency()) {
111 SUnit *PredSU = PredDep.getSUnit();
112 // Find the corresponding successor in N.
113 SDep ForwardD = PredDep;
114 ForwardD.setSUnit(this);
115 for (SDep &SuccDep : PredSU->Succs) {
116 if (SuccDep == ForwardD) {
117 SuccDep.setLatency(D.getLatency());
118 break;
119 }
120 }
121 PredDep.setLatency(D.getLatency());
122 // Changing latency, dirty the involved SUnits.
123 this->setDepthDirty();
125 }
126 return false;
127 }
128 }
129 // Now add a corresponding succ to N.
130 SDep P = D;
131 P.setSUnit(this);
132 SUnit *N = D.getSUnit();
133 // Update the bookkeeping.
134 if (D.getKind() == SDep::Data) {
135 assert(NumPreds < std::numeric_limits<unsigned>::max() &&
136 "NumPreds will overflow!");
137 assert(N->NumSuccs < std::numeric_limits<unsigned>::max() &&
138 "NumSuccs will overflow!");
139 ++NumPreds;
140 ++N->NumSuccs;
141 }
142 if (!N->isScheduled) {
143 if (D.isWeak()) {
145 }
146 else {
147 assert(NumPredsLeft < std::numeric_limits<unsigned>::max() &&
148 "NumPredsLeft will overflow!");
149 ++NumPredsLeft;
150 }
151 }
152 if (!isScheduled) {
153 if (D.isWeak()) {
154 ++N->WeakSuccsLeft;
155 }
156 else {
157 assert(N->NumSuccsLeft < std::numeric_limits<unsigned>::max() &&
158 "NumSuccsLeft will overflow!");
159 ++N->NumSuccsLeft;
160 }
161 }
162 Preds.push_back(D);
163 N->Succs.push_back(P);
164 this->setDepthDirty();
165 N->setHeightDirty();
166 return true;
167}
168
170 // Find the matching predecessor.
172 if (I == Preds.end())
173 return;
174 // Find the corresponding successor in N.
175 SDep P = D;
176 P.setSUnit(this);
177 SUnit *N = D.getSUnit();
179 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
180 // Update the bookkeeping.
181 if (P.getKind() == SDep::Data) {
182 assert(NumPreds > 0 && "NumPreds will underflow!");
183 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
184 --NumPreds;
185 --N->NumSuccs;
186 }
187 if (!N->isScheduled) {
188 if (D.isWeak()) {
189 assert(WeakPredsLeft > 0 && "WeakPredsLeft will underflow!");
191 } else {
192 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
193 --NumPredsLeft;
194 }
195 }
196 if (!isScheduled) {
197 if (D.isWeak()) {
198 assert(N->WeakSuccsLeft > 0 && "WeakSuccsLeft will underflow!");
199 --N->WeakSuccsLeft;
200 } else {
201 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
202 --N->NumSuccsLeft;
203 }
204 }
205 N->Succs.erase(Succ);
206 Preds.erase(I);
207 this->setDepthDirty();
208 N->setHeightDirty();
209}
210
212 if (!isDepthCurrent) return;
213 SmallVector<SUnit*, 8> WorkList;
214 WorkList.push_back(this);
215 do {
216 SUnit *SU = WorkList.pop_back_val();
217 SU->isDepthCurrent = false;
218 for (SDep &SuccDep : SU->Succs) {
219 SUnit *SuccSU = SuccDep.getSUnit();
220 if (SuccSU->isDepthCurrent)
221 WorkList.push_back(SuccSU);
222 }
223 } while (!WorkList.empty());
224}
225
227 if (!isHeightCurrent) return;
228 SmallVector<SUnit*, 8> WorkList;
229 WorkList.push_back(this);
230 do {
231 SUnit *SU = WorkList.pop_back_val();
232 SU->isHeightCurrent = false;
233 for (SDep &PredDep : SU->Preds) {
234 SUnit *PredSU = PredDep.getSUnit();
235 if (PredSU->isHeightCurrent)
236 WorkList.push_back(PredSU);
237 }
238 } while (!WorkList.empty());
239}
240
241void SUnit::setDepthToAtLeast(unsigned NewDepth) {
242 if (NewDepth <= getDepth())
243 return;
245 Depth = NewDepth;
246 isDepthCurrent = true;
247}
248
249void SUnit::setHeightToAtLeast(unsigned NewHeight) {
250 if (NewHeight <= getHeight())
251 return;
253 Height = NewHeight;
254 isHeightCurrent = true;
255}
256
257/// Calculates the maximal path from the node to the entry.
258void SUnit::ComputeDepth() {
259 // Iterative post-order DFS along Preds. Pushing one pred at a time and
260 // finalizing on pop. A node on the stack cannot reappear as a pred of any
261 // descendant.
263 WorkList.push_back(this);
264 do {
265 SUnit *Cur = WorkList.back();
266 bool Descended = false;
267 for (const SDep &PredDep : Cur->Preds) {
268 SUnit *PredSU = PredDep.getSUnit();
269 if (!PredSU->isDepthCurrent) {
270 WorkList.push_back(PredSU);
271 Descended = true;
272 break;
273 }
274 }
275 if (Descended)
276 continue;
277 WorkList.pop_back();
278 unsigned MaxPredDepth = 0;
279 for (const SDep &PredDep : Cur->Preds)
280 MaxPredDepth = std::max(MaxPredDepth,
281 PredDep.getSUnit()->Depth + PredDep.getLatency());
282 Cur->Depth = MaxPredDepth;
283 Cur->isDepthCurrent = true;
284 } while (!WorkList.empty());
285}
286
287/// Calculates the maximal path from the node to the exit.
288void SUnit::ComputeHeight() {
289 // See ComputeDepth; this is the mirror image walking Succs.
291 WorkList.push_back(this);
292 do {
293 SUnit *Cur = WorkList.back();
294 bool Descended = false;
295 for (const SDep &SuccDep : Cur->Succs) {
296 SUnit *SuccSU = SuccDep.getSUnit();
297 if (!SuccSU->isHeightCurrent) {
298 WorkList.push_back(SuccSU);
299 Descended = true;
300 break;
301 }
302 }
303 if (Descended)
304 continue;
305 WorkList.pop_back();
306 unsigned MaxSuccHeight = 0;
307 for (const SDep &SuccDep : Cur->Succs)
308 MaxSuccHeight = std::max(MaxSuccHeight, SuccDep.getSUnit()->Height +
309 SuccDep.getLatency());
310 Cur->Height = MaxSuccHeight;
311 Cur->isHeightCurrent = true;
312 } while (!WorkList.empty());
313}
314
316 if (NumPreds < 2)
317 return;
318
319 SUnit::pred_iterator BestI = Preds.begin();
320 unsigned MaxDepth = BestI->getSUnit()->getDepth();
321 for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
322 ++I) {
323 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth) {
324 MaxDepth = I->getSUnit()->getDepth();
325 BestI = I;
326 }
327 }
328 if (BestI != Preds.begin())
329 std::swap(*Preds.begin(), *BestI);
330}
331
332#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
334 assert(!SU.isBoundaryNode() &&
335 "use ScheduleDAG::dumpNodeName for boundary nodes");
336 return OS << "SU(" << SU.NodeNum << ")";
337}
338#endif
339
340#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
342 dbgs() << " # preds left : " << NumPredsLeft << "\n";
343 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
344 if (WeakPredsLeft)
345 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
346 if (WeakSuccsLeft)
347 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
348 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
349 dbgs() << " Latency : " << Latency << "\n";
350 dbgs() << " Depth : " << getDepth() << "\n";
351 dbgs() << " Height : " << getHeight() << "\n";
352}
353
355 if (&SU == &EntrySU)
356 dbgs() << "EntrySU";
357 else if (&SU == &ExitSU)
358 dbgs() << "ExitSU";
359 else
360 dbgs() << SU;
361}
362
364 dumpNode(SU);
365 SU.dumpAttributes();
366 if (SU.isClustered())
367 dbgs() << " Parent Cluster Index: " << SU.ParentClusterIdx << '\n';
368
369 if (SU.Preds.size() > 0) {
370 dbgs() << " Predecessors:\n";
371 for (const SDep &Dep : SU.Preds) {
372 dbgs() << " ";
373 dumpNodeName(*Dep.getSUnit());
374 dbgs() << ": ";
375 Dep.dump(TRI);
376 dbgs() << '\n';
377 }
378 }
379 if (SU.Succs.size() > 0) {
380 dbgs() << " Successors:\n";
381 for (const SDep &Dep : SU.Succs) {
382 dbgs() << " ";
383 dumpNodeName(*Dep.getSUnit());
384 dbgs() << ": ";
385 Dep.dump(TRI);
386 dbgs() << '\n';
387 }
388 }
389}
390#endif
391
392#ifndef NDEBUG
393unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
394 bool AnyNotSched = false;
395 unsigned DeadNodes = 0;
396 for (const SUnit &SUnit : SUnits) {
397 if (!SUnit.isScheduled) {
398 if (SUnit.NumPreds == 0 && SUnit.NumSuccs == 0) {
399 ++DeadNodes;
400 continue;
401 }
402 if (!AnyNotSched)
403 dbgs() << "*** Scheduling failed! ***\n";
405 dbgs() << "has not been scheduled!\n";
406 AnyNotSched = true;
407 }
408 if (SUnit.isScheduled &&
409 (isBottomUp ? SUnit.getHeight() : SUnit.getDepth()) >
410 unsigned(std::numeric_limits<int>::max())) {
411 if (!AnyNotSched)
412 dbgs() << "*** Scheduling failed! ***\n";
414 dbgs() << "has an unexpected "
415 << (isBottomUp ? "Height" : "Depth") << " value!\n";
416 AnyNotSched = true;
417 }
418 if (isBottomUp) {
419 if (SUnit.NumSuccsLeft != 0) {
420 if (!AnyNotSched)
421 dbgs() << "*** Scheduling failed! ***\n";
423 dbgs() << "has successors left!\n";
424 AnyNotSched = true;
425 }
426 } else {
427 if (SUnit.NumPredsLeft != 0) {
428 if (!AnyNotSched)
429 dbgs() << "*** Scheduling failed! ***\n";
431 dbgs() << "has predecessors left!\n";
432 AnyNotSched = true;
433 }
434 }
435 }
436 assert(!AnyNotSched);
437 return SUnits.size() - DeadNodes;
438}
439#endif
440
442 // The idea of the algorithm is taken from
443 // "Online algorithms for managing the topological order of
444 // a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
445 // This is the MNR algorithm, which was first introduced by
446 // A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
447 // "Maintaining a topological order under edge insertions".
448 //
449 // Short description of the algorithm:
450 //
451 // Topological ordering, ord, of a DAG maps each node to a topological
452 // index so that for all edges X->Y it is the case that ord(X) < ord(Y).
453 //
454 // This means that if there is a path from the node X to the node Z,
455 // then ord(X) < ord(Z).
456 //
457 // This property can be used to check for reachability of nodes:
458 // if Z is reachable from X, then an insertion of the edge Z->X would
459 // create a cycle.
460 //
461 // The algorithm first computes a topological ordering for the DAG by
462 // initializing the Index2Node and Node2Index arrays and then tries to keep
463 // the ordering up-to-date after edge insertions by reordering the DAG.
464 //
465 // On insertion of the edge X->Y, the algorithm first marks by calling DFS
466 // the nodes reachable from Y, and then shifts them using Shift to lie
467 // immediately after X in Index2Node.
468
469 // Cancel pending updates, mark as valid.
470 Dirty = false;
471 Updates.clear();
472 Reachable.clear();
473
474 unsigned DAGSize = SUnits.size();
475
476 Index2Node.resize(DAGSize);
477 Node2Index.resize(DAGSize);
478
479 WorkList.reserve(DAGSize);
480 WorkList.clear();
481
482 // Initialize the data structures.
483 if (ExitSU)
484 WorkList.push_back(ExitSU);
485 for (SUnit &SU : SUnits) {
486 int NodeNum = SU.NodeNum;
487 unsigned Degree = SU.Succs.size();
488 // Temporarily use the Node2Index array as scratch space for degree counts.
489 Node2Index[NodeNum] = Degree;
490
491 // Is it a node without dependencies?
492 if (Degree == 0) {
493 assert(SU.Succs.empty() && "SUnit should have no successors");
494 // Collect leaf nodes.
495 WorkList.push_back(&SU);
496 }
497 }
498
499 int Id = DAGSize;
500 while (!WorkList.empty()) {
501 const SUnit *SU = WorkList.back();
502 WorkList.pop_back();
503 if (SU->NodeNum < DAGSize)
504 Allocate(SU->NodeNum, --Id);
505 for (const SDep &PredDep : SU->Preds) {
506 SUnit *SU = PredDep.getSUnit();
507 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
508 // If all dependencies of the node are processed already,
509 // then the node can be computed now.
510 WorkList.push_back(SU);
511 }
512 }
513
514 Visited.resize(DAGSize);
515 NumTopoInits++;
516
517#ifndef NDEBUG
518 // Check correctness of the ordering
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");
523 }
524 }
525#endif
526}
527
528void ScheduleDAGTopologicalSort::FixOrder() {
529 // Recompute from scratch after new nodes have been added.
530 if (Dirty) {
532 return;
533 }
534
535 // Otherwise apply updates one-by-one.
536 for (auto &U : Updates)
537 AddPred(U.first, U.second);
538 Updates.clear();
539}
540
542 // Recomputing the order from scratch is likely more efficient than applying
543 // updates one-by-one for too many updates. The current cut-off is arbitrarily
544 // chosen.
545 Dirty = Dirty || Updates.size() > 10;
546
547 if (Dirty)
548 return;
549
550 Updates.emplace_back(Y, X);
551}
552
554 int UpperBound, LowerBound;
555 LowerBound = Node2Index[Y->NodeNum];
556 UpperBound = Node2Index[X->NodeNum];
557 bool HasLoop = false;
558 // Is Ord(X) < Ord(Y) ?
559 if (LowerBound < UpperBound) {
560 // Update the topological order.
561 Visited.reset();
562 DFS(Y, UpperBound, HasLoop);
563 assert(!HasLoop && "Inserted edge creates a loop!");
564 // Recompute topological indexes.
565 Shift(Visited, LowerBound, UpperBound);
566 }
567
568 NumNewPredsAdded++;
569 Reachable.clear();
570}
571
573 // InitDAGTopologicalSorting();
574}
575
576void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
577 bool &HasLoop) {
578 WorkList.clear();
579
580 WorkList.push_back(SU);
581 do {
582 SU = WorkList.back();
583 WorkList.pop_back();
584 Visited.set(SU->NodeNum);
585 for (const SDep &SuccDep : llvm::reverse(SU->Succs)) {
586 unsigned s = SuccDep.getSUnit()->NodeNum;
587 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
588 if (s >= Node2Index.size())
589 continue;
590 if (Node2Index[s] == UpperBound) {
591 HasLoop = true;
592 return;
593 }
594 // Visit successors if not already and in affected region.
595 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
596 WorkList.push_back(SuccDep.getSUnit());
597 }
598 }
599 } while (!WorkList.empty());
600}
601
602std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU,
603 const SUnit &TargetSU,
604 bool &Success) {
605 int LowerBound = Node2Index[StartSU.NodeNum];
606 int UpperBound = Node2Index[TargetSU.NodeNum];
607 bool Found = false;
608 BitVector VisitedBack;
609 std::vector<int> Nodes;
610
611 if (LowerBound > UpperBound) {
612 Success = false;
613 return Nodes;
614 }
615
616 WorkList.clear();
617 Visited.reset();
618
619 // Starting from StartSU, visit all successors up
620 // to UpperBound.
621 WorkList.push_back(&StartSU);
622 do {
623 const SUnit *SU = WorkList.back();
624 WorkList.pop_back();
625 for (const SDep &SD : llvm::reverse(SU->Succs)) {
626 const SUnit *Succ = SD.getSUnit();
627 unsigned s = Succ->NodeNum;
628 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
629 if (Succ->isBoundaryNode())
630 continue;
631 if (Node2Index[s] == UpperBound) {
632 Found = true;
633 continue;
634 }
635 // Visit successors if not already and in affected region.
636 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
637 Visited.set(s);
638 WorkList.push_back(Succ);
639 }
640 }
641 } while (!WorkList.empty());
642
643 if (!Found) {
644 Success = false;
645 return Nodes;
646 }
647
648 WorkList.clear();
649 VisitedBack.resize(SUnits.size());
650 Found = false;
651
652 // Starting from TargetSU, visit all predecessors up
653 // to LowerBound. SUs that are visited by the two
654 // passes are added to Nodes.
655 WorkList.push_back(&TargetSU);
656 do {
657 const SUnit *SU = WorkList.back();
658 WorkList.pop_back();
659 for (const SDep &SD : llvm::reverse(SU->Preds)) {
660 const SUnit *Pred = SD.getSUnit();
661 unsigned s = Pred->NodeNum;
662 // Edges to non-SUnits are allowed but ignored (e.g. EntrySU).
663 if (Pred->isBoundaryNode())
664 continue;
665 if (Node2Index[s] == LowerBound) {
666 Found = true;
667 continue;
668 }
669 if (!VisitedBack.test(s) && Visited.test(s)) {
670 VisitedBack.set(s);
671 WorkList.push_back(Pred);
672 Nodes.push_back(s);
673 }
674 }
675 } while (!WorkList.empty());
676
677 assert(Found && "Error in SUnit Graph!");
678 Success = true;
679 return Nodes;
680}
681
682void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
683 int UpperBound) {
684 std::vector<int> L;
685 int shift = 0;
686 int i;
687
688 for (i = LowerBound; i <= UpperBound; ++i) {
689 // w is node at topological index i.
690 int w = Index2Node[i];
691 if (Visited.test(w)) {
692 // Unmark.
693 Visited.reset(w);
694 L.push_back(w);
695 shift = shift + 1;
696 } else {
697 Allocate(w, i - shift);
698 }
699 }
700
701 for (unsigned LI : L) {
702 Allocate(LI, i - shift);
703 i = i + 1;
704 }
705}
706
708 FixOrder();
709 // Is SU reachable from TargetSU via successor edges?
710 if (IsReachable(SU, TargetSU))
711 return true;
712 for (const SDep &PredDep : TargetSU->Preds)
713 if (PredDep.isAssignedRegDep() &&
714 IsReachable(SU, PredDep.getSUnit()))
715 return true;
716 return false;
717}
718
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());
725}
726
728 const SUnit *TargetSU) {
729 assert(TargetSU != nullptr && "Invalid target SUnit");
730 assert(SU != nullptr && "Invalid SUnit");
731 FixOrder();
732 // If insertion of the edge SU->TargetSU would create a cycle
733 // then there is a path from TargetSU to SU.
734 int UpperBound, LowerBound;
735 LowerBound = Node2Index[TargetSU->NodeNum];
736 UpperBound = Node2Index[SU->NodeNum];
737 bool HasLoop = false;
738 // Is Ord(TargetSU) < Ord(SU) ?
739 if (LowerBound < UpperBound) {
740 if (auto It = Reachable.find({TargetSU->NodeNum, SU->NodeNum});
741 It != Reachable.end()) {
742 return It->second;
743 }
744 Visited.reset();
745 // There may be a path from TargetSU to SU. Check for it.
746 DFS(TargetSU, UpperBound, HasLoop);
747 // If there's no loop, cache the result. We only cache negative results,
748 // as positive results are not safe to cache; users call SU.removePred()
749 // without notifying us.
750 if (!HasLoop)
751 Reachable[{TargetSU->NodeNum, SU->NodeNum}] = false;
752 }
753 return HasLoop;
754}
755
756void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
757 Node2Index[n] = index;
758 Index2Node[index] = n;
759}
760
762ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
763 : SUnits(sunits), ExitSU(exitsu) {}
764
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
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.
Definition Compiler.h:702
const HexagonInstrInfo * TII
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
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)
Definition Statistic.h:171
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.
Definition BitVector.h:483
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:410
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:356
void clear()
Removes all bits from the bitvector.
Definition BitVector.h:350
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:367
void push_back(bool Val)
Definition BitVector.h:506
Describe properties that are true of each instruction in the target description file.
Represents one node in the SelectionDAG.
Scheduling dependency.
Definition ScheduleDAG.h:54
SUnit * getSUnit() const
Kind getKind() const
Returns an enum value representing the kind of the dependence.
@ Output
A register output-dependence (aka WAW).
Definition ScheduleDAG.h:60
@ Order
Any other ordering dependency.
Definition ScheduleDAG.h:61
@ Anti
A register anti-dependence (aka WAR).
Definition ScheduleDAG.h:59
@ Data
Regular data dependence (aka true-dependence).
Definition ScheduleDAG.h:58
void setLatency(unsigned Lat)
Sets the latency for this edge.
@ Cluster
Weak DAG edge linking a chain of clustered instrs.
Definition ScheduleDAG.h:79
@ Barrier
An unknown scheduling barrier.
Definition ScheduleDAG.h:74
@ Artificial
Arbitrary strong DAG edge (no real dependence).
Definition ScheduleDAG.h:77
@ MayAliasMem
Nonvolatile load/Store instructions that may alias.
Definition ScheduleDAG.h:75
@ Weak
Arbitrary weak DAG edge.
Definition ScheduleDAG.h:78
@ MustAliasMem
Nonvolatile load/Store instructions that must alias.
Definition ScheduleDAG.h:76
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.
void setSUnit(SUnit *SU)
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 NumSuccs
unsigned NumPreds
unsigned NodeNum
Entry # of node in the node vector.
unsigned NumSuccsLeft
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.
unsigned NumPredsLeft
bool isClustered() const
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.
unsigned WeakPredsLeft
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.
unsigned WeakSuccsLeft
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.
virtual ~ScheduleDAG()
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.
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.
Definition raw_ostream.h:53
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.
Definition STLExtras.h:1781
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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.
Definition BitVector.h:881
#define N