1 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the ScheduleDAG class, which is a base class used by
11 // scheduling implementation classes.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "pre-RA-sched"
16 #include "llvm/CodeGen/ScheduleDAG.h"
17 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
18 #include "llvm/CodeGen/SelectionDAGNodes.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
29 cl::opt<bool> StressSchedOpt(
30 "stress-sched", cl::Hidden, cl::init(false),
31 cl::desc("Stress test instruction scheduling"));
34 ScheduleDAG::ScheduleDAG(MachineFunction &mf)
36 TII(TM.getInstrInfo()),
37 TRI(TM.getRegisterInfo()),
38 MF(mf), MRI(mf.getRegInfo()),
41 StressSched = StressSchedOpt;
45 ScheduleDAG::~ScheduleDAG() {}
47 /// getInstrDesc helper to handle SDNodes.
48 const TargetInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
49 if (!Node || !Node->isMachineOpcode()) return NULL;
50 return &TII->get(Node->getMachineOpcode());
53 /// dump - dump the schedule.
54 void ScheduleDAG::dumpSchedule() const {
55 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
56 if (SUnit *SU = Sequence[i])
59 dbgs() << "**** NOOP ****\n";
64 /// Run - perform scheduling.
66 void ScheduleDAG::Run(MachineBasicBlock *bb,
67 MachineBasicBlock::iterator insertPos) {
69 InsertPos = insertPos;
79 dbgs() << "*** Final schedule ***\n";
85 /// addPred - This adds the specified edge as a pred of the current node if
86 /// not already. It also adds the current node as a successor of the
88 bool SUnit::addPred(const SDep &D) {
89 // If this node already has this depenence, don't add a redundant one.
90 for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end();
94 // Now add a corresponding succ to N.
97 SUnit *N = D.getSUnit();
98 // Update the bookkeeping.
99 if (D.getKind() == SDep::Data) {
100 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
101 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
105 if (!N->isScheduled) {
106 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
110 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
114 N->Succs.push_back(P);
115 if (P.getLatency() != 0) {
116 this->setDepthDirty();
122 /// removePred - This removes the specified edge as a pred of the current
123 /// node if it exists. It also removes the current node as a successor of
124 /// the specified node.
125 void SUnit::removePred(const SDep &D) {
126 // Find the matching predecessor.
127 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
130 bool FoundSucc = false;
131 // Find the corresponding successor in N.
134 SUnit *N = D.getSUnit();
135 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
136 EE = N->Succs.end(); II != EE; ++II)
142 assert(FoundSucc && "Mismatching preds / succs lists!");
144 // Update the bookkeeping.
145 if (P.getKind() == SDep::Data) {
146 assert(NumPreds > 0 && "NumPreds will underflow!");
147 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
151 if (!N->isScheduled) {
152 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
156 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
159 if (P.getLatency() != 0) {
160 this->setDepthDirty();
167 void SUnit::setDepthDirty() {
168 if (!isDepthCurrent) return;
169 SmallVector<SUnit*, 8> WorkList;
170 WorkList.push_back(this);
172 SUnit *SU = WorkList.pop_back_val();
173 SU->isDepthCurrent = false;
174 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
175 E = SU->Succs.end(); I != E; ++I) {
176 SUnit *SuccSU = I->getSUnit();
177 if (SuccSU->isDepthCurrent)
178 WorkList.push_back(SuccSU);
180 } while (!WorkList.empty());
183 void SUnit::setHeightDirty() {
184 if (!isHeightCurrent) return;
185 SmallVector<SUnit*, 8> WorkList;
186 WorkList.push_back(this);
188 SUnit *SU = WorkList.pop_back_val();
189 SU->isHeightCurrent = false;
190 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
191 E = SU->Preds.end(); I != E; ++I) {
192 SUnit *PredSU = I->getSUnit();
193 if (PredSU->isHeightCurrent)
194 WorkList.push_back(PredSU);
196 } while (!WorkList.empty());
199 /// setDepthToAtLeast - Update this node's successors to reflect the
200 /// fact that this node's depth just increased.
202 void SUnit::setDepthToAtLeast(unsigned NewDepth) {
203 if (NewDepth <= getDepth())
207 isDepthCurrent = true;
210 /// setHeightToAtLeast - Update this node's predecessors to reflect the
211 /// fact that this node's height just increased.
213 void SUnit::setHeightToAtLeast(unsigned NewHeight) {
214 if (NewHeight <= getHeight())
218 isHeightCurrent = true;
221 /// ComputeDepth - Calculate the maximal path from the node to the exit.
223 void SUnit::ComputeDepth() {
224 SmallVector<SUnit*, 8> WorkList;
225 WorkList.push_back(this);
227 SUnit *Cur = WorkList.back();
230 unsigned MaxPredDepth = 0;
231 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
232 E = Cur->Preds.end(); I != E; ++I) {
233 SUnit *PredSU = I->getSUnit();
234 if (PredSU->isDepthCurrent)
235 MaxPredDepth = std::max(MaxPredDepth,
236 PredSU->Depth + I->getLatency());
239 WorkList.push_back(PredSU);
245 if (MaxPredDepth != Cur->Depth) {
246 Cur->setDepthDirty();
247 Cur->Depth = MaxPredDepth;
249 Cur->isDepthCurrent = true;
251 } while (!WorkList.empty());
254 /// ComputeHeight - Calculate the maximal path from the node to the entry.
256 void SUnit::ComputeHeight() {
257 SmallVector<SUnit*, 8> WorkList;
258 WorkList.push_back(this);
260 SUnit *Cur = WorkList.back();
263 unsigned MaxSuccHeight = 0;
264 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
265 E = Cur->Succs.end(); I != E; ++I) {
266 SUnit *SuccSU = I->getSUnit();
267 if (SuccSU->isHeightCurrent)
268 MaxSuccHeight = std::max(MaxSuccHeight,
269 SuccSU->Height + I->getLatency());
272 WorkList.push_back(SuccSU);
278 if (MaxSuccHeight != Cur->Height) {
279 Cur->setHeightDirty();
280 Cur->Height = MaxSuccHeight;
282 Cur->isHeightCurrent = true;
284 } while (!WorkList.empty());
287 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
288 /// a group of nodes flagged together.
289 void SUnit::dump(const ScheduleDAG *G) const {
290 dbgs() << "SU(" << NodeNum << "): ";
294 void SUnit::dumpAll(const ScheduleDAG *G) const {
297 dbgs() << " # preds left : " << NumPredsLeft << "\n";
298 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
299 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
300 dbgs() << " Latency : " << Latency << "\n";
301 dbgs() << " Depth : " << Depth << "\n";
302 dbgs() << " Height : " << Height << "\n";
304 if (Preds.size() != 0) {
305 dbgs() << " Predecessors:\n";
306 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
309 switch (I->getKind()) {
310 case SDep::Data: dbgs() << "val "; break;
311 case SDep::Anti: dbgs() << "anti"; break;
312 case SDep::Output: dbgs() << "out "; break;
313 case SDep::Order: dbgs() << "ch "; break;
316 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
317 if (I->isArtificial())
319 dbgs() << ": Latency=" << I->getLatency();
320 if (I->isAssignedRegDep())
321 dbgs() << " Reg=" << G->TRI->getName(I->getReg());
325 if (Succs.size() != 0) {
326 dbgs() << " Successors:\n";
327 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
330 switch (I->getKind()) {
331 case SDep::Data: dbgs() << "val "; break;
332 case SDep::Anti: dbgs() << "anti"; break;
333 case SDep::Output: dbgs() << "out "; break;
334 case SDep::Order: dbgs() << "ch "; break;
337 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
338 if (I->isArtificial())
340 dbgs() << ": Latency=" << I->getLatency();
348 /// VerifySchedule - Verify that all SUnits were scheduled and that
349 /// their state is consistent.
351 void ScheduleDAG::VerifySchedule(bool isBottomUp) {
352 bool AnyNotSched = false;
353 unsigned DeadNodes = 0;
355 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
356 if (!SUnits[i].isScheduled) {
357 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
362 dbgs() << "*** Scheduling failed! ***\n";
363 SUnits[i].dump(this);
364 dbgs() << "has not been scheduled!\n";
367 if (SUnits[i].isScheduled &&
368 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
371 dbgs() << "*** Scheduling failed! ***\n";
372 SUnits[i].dump(this);
373 dbgs() << "has an unexpected "
374 << (isBottomUp ? "Height" : "Depth") << " value!\n";
378 if (SUnits[i].NumSuccsLeft != 0) {
380 dbgs() << "*** Scheduling failed! ***\n";
381 SUnits[i].dump(this);
382 dbgs() << "has successors left!\n";
386 if (SUnits[i].NumPredsLeft != 0) {
388 dbgs() << "*** Scheduling failed! ***\n";
389 SUnits[i].dump(this);
390 dbgs() << "has predecessors left!\n";
395 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
398 assert(!AnyNotSched);
399 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
400 "The number of nodes scheduled doesn't match the expected number!");
404 /// InitDAGTopologicalSorting - create the initial topological
405 /// ordering from the DAG to be scheduled.
407 /// The idea of the algorithm is taken from
408 /// "Online algorithms for managing the topological order of
409 /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
410 /// This is the MNR algorithm, which was first introduced by
411 /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
412 /// "Maintaining a topological order under edge insertions".
414 /// Short description of the algorithm:
416 /// Topological ordering, ord, of a DAG maps each node to a topological
417 /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
419 /// This means that if there is a path from the node X to the node Z,
420 /// then ord(X) < ord(Z).
422 /// This property can be used to check for reachability of nodes:
423 /// if Z is reachable from X, then an insertion of the edge Z->X would
426 /// The algorithm first computes a topological ordering for the DAG by
427 /// initializing the Index2Node and Node2Index arrays and then tries to keep
428 /// the ordering up-to-date after edge insertions by reordering the DAG.
430 /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
431 /// the nodes reachable from Y, and then shifts them using Shift to lie
432 /// immediately after X in Index2Node.
433 void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
434 unsigned DAGSize = SUnits.size();
435 std::vector<SUnit*> WorkList;
436 WorkList.reserve(DAGSize);
438 Index2Node.resize(DAGSize);
439 Node2Index.resize(DAGSize);
441 // Initialize the data structures.
442 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
443 SUnit *SU = &SUnits[i];
444 int NodeNum = SU->NodeNum;
445 unsigned Degree = SU->Succs.size();
446 // Temporarily use the Node2Index array as scratch space for degree counts.
447 Node2Index[NodeNum] = Degree;
449 // Is it a node without dependencies?
451 assert(SU->Succs.empty() && "SUnit should have no successors");
452 // Collect leaf nodes.
453 WorkList.push_back(SU);
458 while (!WorkList.empty()) {
459 SUnit *SU = WorkList.back();
461 Allocate(SU->NodeNum, --Id);
462 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
464 SUnit *SU = I->getSUnit();
465 if (!--Node2Index[SU->NodeNum])
466 // If all dependencies of the node are processed already,
467 // then the node can be computed now.
468 WorkList.push_back(SU);
472 Visited.resize(DAGSize);
475 // Check correctness of the ordering
476 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
477 SUnit *SU = &SUnits[i];
478 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
480 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
481 "Wrong topological sorting");
487 /// AddPred - Updates the topological ordering to accommodate an edge
488 /// to be added from SUnit X to SUnit Y.
489 void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
490 int UpperBound, LowerBound;
491 LowerBound = Node2Index[Y->NodeNum];
492 UpperBound = Node2Index[X->NodeNum];
493 bool HasLoop = false;
494 // Is Ord(X) < Ord(Y) ?
495 if (LowerBound < UpperBound) {
496 // Update the topological order.
498 DFS(Y, UpperBound, HasLoop);
499 assert(!HasLoop && "Inserted edge creates a loop!");
500 // Recompute topological indexes.
501 Shift(Visited, LowerBound, UpperBound);
505 /// RemovePred - Updates the topological ordering to accommodate an
506 /// an edge to be removed from the specified node N from the predecessors
507 /// of the current node M.
508 void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
509 // InitDAGTopologicalSorting();
512 /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
513 /// all nodes affected by the edge insertion. These nodes will later get new
514 /// topological indexes by means of the Shift method.
515 void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
517 std::vector<const SUnit*> WorkList;
518 WorkList.reserve(SUnits.size());
520 WorkList.push_back(SU);
522 SU = WorkList.back();
524 Visited.set(SU->NodeNum);
525 for (int I = SU->Succs.size()-1; I >= 0; --I) {
526 int s = SU->Succs[I].getSUnit()->NodeNum;
527 if (Node2Index[s] == UpperBound) {
531 // Visit successors if not already and in affected region.
532 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
533 WorkList.push_back(SU->Succs[I].getSUnit());
536 } while (!WorkList.empty());
539 /// Shift - Renumber the nodes so that the topological ordering is
541 void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
547 for (i = LowerBound; i <= UpperBound; ++i) {
548 // w is node at topological index i.
549 int w = Index2Node[i];
550 if (Visited.test(w)) {
556 Allocate(w, i - shift);
560 for (unsigned j = 0; j < L.size(); ++j) {
561 Allocate(L[j], i - shift);
567 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
569 bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
570 if (IsReachable(TargetSU, SU))
572 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
574 if (I->isAssignedRegDep() &&
575 IsReachable(TargetSU, I->getSUnit()))
580 /// IsReachable - Checks if SU is reachable from TargetSU.
581 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
582 const SUnit *TargetSU) {
583 // If insertion of the edge SU->TargetSU would create a cycle
584 // then there is a path from TargetSU to SU.
585 int UpperBound, LowerBound;
586 LowerBound = Node2Index[TargetSU->NodeNum];
587 UpperBound = Node2Index[SU->NodeNum];
588 bool HasLoop = false;
589 // Is Ord(TargetSU) < Ord(SU) ?
590 if (LowerBound < UpperBound) {
592 // There may be a path from TargetSU to SU. Check for it.
593 DFS(TargetSU, UpperBound, HasLoop);
598 /// Allocate - assign the topological index to the node n.
599 void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
600 Node2Index[n] = index;
601 Index2Node[index] = n;
604 ScheduleDAGTopologicalSort::
605 ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
607 ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}