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/Target/TargetMachine.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 ScheduleDAG::ScheduleDAG(MachineFunction
&mf
)
28 TII(TM
.getInstrInfo()),
29 TRI(TM
.getRegisterInfo()),
30 MF(mf
), MRI(mf
.getRegInfo()),
34 ScheduleDAG::~ScheduleDAG() {}
36 /// dump - dump the schedule.
37 void ScheduleDAG::dumpSchedule() const {
38 for (unsigned i
= 0, e
= Sequence
.size(); i
!= e
; i
++) {
39 if (SUnit
*SU
= Sequence
[i
])
42 dbgs() << "**** NOOP ****\n";
47 /// Run - perform scheduling.
49 void ScheduleDAG::Run(MachineBasicBlock
*bb
,
50 MachineBasicBlock::iterator insertPos
) {
52 InsertPos
= insertPos
;
62 dbgs() << "*** Final schedule ***\n";
68 /// addPred - This adds the specified edge as a pred of the current node if
69 /// not already. It also adds the current node as a successor of the
71 void SUnit::addPred(const SDep
&D
) {
72 // If this node already has this depenence, don't add a redundant one.
73 for (SmallVector
<SDep
, 4>::const_iterator I
= Preds
.begin(), E
= Preds
.end();
77 // Now add a corresponding succ to N.
80 SUnit
*N
= D
.getSUnit();
81 // Update the bookkeeping.
82 if (D
.getKind() == SDep::Data
) {
83 assert(NumPreds
< UINT_MAX
&& "NumPreds will overflow!");
84 assert(N
->NumSuccs
< UINT_MAX
&& "NumSuccs will overflow!");
88 if (!N
->isScheduled
) {
89 assert(NumPredsLeft
< UINT_MAX
&& "NumPredsLeft will overflow!");
93 assert(N
->NumSuccsLeft
< UINT_MAX
&& "NumSuccsLeft will overflow!");
97 N
->Succs
.push_back(P
);
98 if (P
.getLatency() != 0) {
99 this->setDepthDirty();
104 /// removePred - This removes the specified edge as a pred of the current
105 /// node if it exists. It also removes the current node as a successor of
106 /// the specified node.
107 void SUnit::removePred(const SDep
&D
) {
108 // Find the matching predecessor.
109 for (SmallVector
<SDep
, 4>::iterator I
= Preds
.begin(), E
= Preds
.end();
112 bool FoundSucc
= false;
113 // Find the corresponding successor in N.
116 SUnit
*N
= D
.getSUnit();
117 for (SmallVector
<SDep
, 4>::iterator II
= N
->Succs
.begin(),
118 EE
= N
->Succs
.end(); II
!= EE
; ++II
)
124 assert(FoundSucc
&& "Mismatching preds / succs lists!");
126 // Update the bookkeeping.
127 if (P
.getKind() == SDep::Data
) {
128 assert(NumPreds
> 0 && "NumPreds will underflow!");
129 assert(N
->NumSuccs
> 0 && "NumSuccs will underflow!");
133 if (!N
->isScheduled
) {
134 assert(NumPredsLeft
> 0 && "NumPredsLeft will underflow!");
138 assert(N
->NumSuccsLeft
> 0 && "NumSuccsLeft will underflow!");
141 if (P
.getLatency() != 0) {
142 this->setDepthDirty();
149 void SUnit::setDepthDirty() {
150 if (!isDepthCurrent
) return;
151 SmallVector
<SUnit
*, 8> WorkList
;
152 WorkList
.push_back(this);
154 SUnit
*SU
= WorkList
.pop_back_val();
155 SU
->isDepthCurrent
= false;
156 for (SUnit::const_succ_iterator I
= SU
->Succs
.begin(),
157 E
= SU
->Succs
.end(); I
!= E
; ++I
) {
158 SUnit
*SuccSU
= I
->getSUnit();
159 if (SuccSU
->isDepthCurrent
)
160 WorkList
.push_back(SuccSU
);
162 } while (!WorkList
.empty());
165 void SUnit::setHeightDirty() {
166 if (!isHeightCurrent
) return;
167 SmallVector
<SUnit
*, 8> WorkList
;
168 WorkList
.push_back(this);
170 SUnit
*SU
= WorkList
.pop_back_val();
171 SU
->isHeightCurrent
= false;
172 for (SUnit::const_pred_iterator I
= SU
->Preds
.begin(),
173 E
= SU
->Preds
.end(); I
!= E
; ++I
) {
174 SUnit
*PredSU
= I
->getSUnit();
175 if (PredSU
->isHeightCurrent
)
176 WorkList
.push_back(PredSU
);
178 } while (!WorkList
.empty());
181 /// setDepthToAtLeast - Update this node's successors to reflect the
182 /// fact that this node's depth just increased.
184 void SUnit::setDepthToAtLeast(unsigned NewDepth
) {
185 if (NewDepth
<= getDepth())
189 isDepthCurrent
= true;
192 /// setHeightToAtLeast - Update this node's predecessors to reflect the
193 /// fact that this node's height just increased.
195 void SUnit::setHeightToAtLeast(unsigned NewHeight
) {
196 if (NewHeight
<= getHeight())
200 isHeightCurrent
= true;
203 /// ComputeDepth - Calculate the maximal path from the node to the exit.
205 void SUnit::ComputeDepth() {
206 SmallVector
<SUnit
*, 8> WorkList
;
207 WorkList
.push_back(this);
209 SUnit
*Cur
= WorkList
.back();
212 unsigned MaxPredDepth
= 0;
213 for (SUnit::const_pred_iterator I
= Cur
->Preds
.begin(),
214 E
= Cur
->Preds
.end(); I
!= E
; ++I
) {
215 SUnit
*PredSU
= I
->getSUnit();
216 if (PredSU
->isDepthCurrent
)
217 MaxPredDepth
= std::max(MaxPredDepth
,
218 PredSU
->Depth
+ I
->getLatency());
221 WorkList
.push_back(PredSU
);
227 if (MaxPredDepth
!= Cur
->Depth
) {
228 Cur
->setDepthDirty();
229 Cur
->Depth
= MaxPredDepth
;
231 Cur
->isDepthCurrent
= true;
233 } while (!WorkList
.empty());
236 /// ComputeHeight - Calculate the maximal path from the node to the entry.
238 void SUnit::ComputeHeight() {
239 SmallVector
<SUnit
*, 8> WorkList
;
240 WorkList
.push_back(this);
242 SUnit
*Cur
= WorkList
.back();
245 unsigned MaxSuccHeight
= 0;
246 for (SUnit::const_succ_iterator I
= Cur
->Succs
.begin(),
247 E
= Cur
->Succs
.end(); I
!= E
; ++I
) {
248 SUnit
*SuccSU
= I
->getSUnit();
249 if (SuccSU
->isHeightCurrent
)
250 MaxSuccHeight
= std::max(MaxSuccHeight
,
251 SuccSU
->Height
+ I
->getLatency());
254 WorkList
.push_back(SuccSU
);
260 if (MaxSuccHeight
!= Cur
->Height
) {
261 Cur
->setHeightDirty();
262 Cur
->Height
= MaxSuccHeight
;
264 Cur
->isHeightCurrent
= true;
266 } while (!WorkList
.empty());
269 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
270 /// a group of nodes flagged together.
271 void SUnit::dump(const ScheduleDAG
*G
) const {
272 dbgs() << "SU(" << NodeNum
<< "): ";
276 void SUnit::dumpAll(const ScheduleDAG
*G
) const {
279 dbgs() << " # preds left : " << NumPredsLeft
<< "\n";
280 dbgs() << " # succs left : " << NumSuccsLeft
<< "\n";
281 dbgs() << " Latency : " << Latency
<< "\n";
282 dbgs() << " Depth : " << Depth
<< "\n";
283 dbgs() << " Height : " << Height
<< "\n";
285 if (Preds
.size() != 0) {
286 dbgs() << " Predecessors:\n";
287 for (SUnit::const_succ_iterator I
= Preds
.begin(), E
= Preds
.end();
290 switch (I
->getKind()) {
291 case SDep::Data
: dbgs() << "val "; break;
292 case SDep::Anti
: dbgs() << "anti"; break;
293 case SDep::Output
: dbgs() << "out "; break;
294 case SDep::Order
: dbgs() << "ch "; break;
297 dbgs() << I
->getSUnit() << " - SU(" << I
->getSUnit()->NodeNum
<< ")";
298 if (I
->isArtificial())
300 dbgs() << ": Latency=" << I
->getLatency();
304 if (Succs
.size() != 0) {
305 dbgs() << " Successors:\n";
306 for (SUnit::const_succ_iterator I
= Succs
.begin(), E
= Succs
.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();
327 /// VerifySchedule - Verify that all SUnits were scheduled and that
328 /// their state is consistent.
330 void ScheduleDAG::VerifySchedule(bool isBottomUp
) {
331 bool AnyNotSched
= false;
332 unsigned DeadNodes
= 0;
334 for (unsigned i
= 0, e
= SUnits
.size(); i
!= e
; ++i
) {
335 if (!SUnits
[i
].isScheduled
) {
336 if (SUnits
[i
].NumPreds
== 0 && SUnits
[i
].NumSuccs
== 0) {
341 dbgs() << "*** Scheduling failed! ***\n";
342 SUnits
[i
].dump(this);
343 dbgs() << "has not been scheduled!\n";
346 if (SUnits
[i
].isScheduled
&&
347 (isBottomUp
? SUnits
[i
].getHeight() : SUnits
[i
].getDepth()) >
350 dbgs() << "*** Scheduling failed! ***\n";
351 SUnits
[i
].dump(this);
352 dbgs() << "has an unexpected "
353 << (isBottomUp
? "Height" : "Depth") << " value!\n";
357 if (SUnits
[i
].NumSuccsLeft
!= 0) {
359 dbgs() << "*** Scheduling failed! ***\n";
360 SUnits
[i
].dump(this);
361 dbgs() << "has successors left!\n";
365 if (SUnits
[i
].NumPredsLeft
!= 0) {
367 dbgs() << "*** Scheduling failed! ***\n";
368 SUnits
[i
].dump(this);
369 dbgs() << "has predecessors left!\n";
374 for (unsigned i
= 0, e
= Sequence
.size(); i
!= e
; ++i
)
377 assert(!AnyNotSched
);
378 assert(Sequence
.size() + DeadNodes
- Noops
== SUnits
.size() &&
379 "The number of nodes scheduled doesn't match the expected number!");
383 /// InitDAGTopologicalSorting - create the initial topological
384 /// ordering from the DAG to be scheduled.
386 /// The idea of the algorithm is taken from
387 /// "Online algorithms for managing the topological order of
388 /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
389 /// This is the MNR algorithm, which was first introduced by
390 /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
391 /// "Maintaining a topological order under edge insertions".
393 /// Short description of the algorithm:
395 /// Topological ordering, ord, of a DAG maps each node to a topological
396 /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
398 /// This means that if there is a path from the node X to the node Z,
399 /// then ord(X) < ord(Z).
401 /// This property can be used to check for reachability of nodes:
402 /// if Z is reachable from X, then an insertion of the edge Z->X would
405 /// The algorithm first computes a topological ordering for the DAG by
406 /// initializing the Index2Node and Node2Index arrays and then tries to keep
407 /// the ordering up-to-date after edge insertions by reordering the DAG.
409 /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
410 /// the nodes reachable from Y, and then shifts them using Shift to lie
411 /// immediately after X in Index2Node.
412 void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
413 unsigned DAGSize
= SUnits
.size();
414 std::vector
<SUnit
*> WorkList
;
415 WorkList
.reserve(DAGSize
);
417 Index2Node
.resize(DAGSize
);
418 Node2Index
.resize(DAGSize
);
420 // Initialize the data structures.
421 for (unsigned i
= 0, e
= DAGSize
; i
!= e
; ++i
) {
422 SUnit
*SU
= &SUnits
[i
];
423 int NodeNum
= SU
->NodeNum
;
424 unsigned Degree
= SU
->Succs
.size();
425 // Temporarily use the Node2Index array as scratch space for degree counts.
426 Node2Index
[NodeNum
] = Degree
;
428 // Is it a node without dependencies?
430 assert(SU
->Succs
.empty() && "SUnit should have no successors");
431 // Collect leaf nodes.
432 WorkList
.push_back(SU
);
437 while (!WorkList
.empty()) {
438 SUnit
*SU
= WorkList
.back();
440 Allocate(SU
->NodeNum
, --Id
);
441 for (SUnit::const_pred_iterator I
= SU
->Preds
.begin(), E
= SU
->Preds
.end();
443 SUnit
*SU
= I
->getSUnit();
444 if (!--Node2Index
[SU
->NodeNum
])
445 // If all dependencies of the node are processed already,
446 // then the node can be computed now.
447 WorkList
.push_back(SU
);
451 Visited
.resize(DAGSize
);
454 // Check correctness of the ordering
455 for (unsigned i
= 0, e
= DAGSize
; i
!= e
; ++i
) {
456 SUnit
*SU
= &SUnits
[i
];
457 for (SUnit::const_pred_iterator I
= SU
->Preds
.begin(), E
= SU
->Preds
.end();
459 assert(Node2Index
[SU
->NodeNum
] > Node2Index
[I
->getSUnit()->NodeNum
] &&
460 "Wrong topological sorting");
466 /// AddPred - Updates the topological ordering to accomodate an edge
467 /// to be added from SUnit X to SUnit Y.
468 void ScheduleDAGTopologicalSort::AddPred(SUnit
*Y
, SUnit
*X
) {
469 int UpperBound
, LowerBound
;
470 LowerBound
= Node2Index
[Y
->NodeNum
];
471 UpperBound
= Node2Index
[X
->NodeNum
];
472 bool HasLoop
= false;
473 // Is Ord(X) < Ord(Y) ?
474 if (LowerBound
< UpperBound
) {
475 // Update the topological order.
477 DFS(Y
, UpperBound
, HasLoop
);
478 assert(!HasLoop
&& "Inserted edge creates a loop!");
479 // Recompute topological indexes.
480 Shift(Visited
, LowerBound
, UpperBound
);
484 /// RemovePred - Updates the topological ordering to accomodate an
485 /// an edge to be removed from the specified node N from the predecessors
486 /// of the current node M.
487 void ScheduleDAGTopologicalSort::RemovePred(SUnit
*M
, SUnit
*N
) {
488 // InitDAGTopologicalSorting();
491 /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
492 /// all nodes affected by the edge insertion. These nodes will later get new
493 /// topological indexes by means of the Shift method.
494 void ScheduleDAGTopologicalSort::DFS(const SUnit
*SU
, int UpperBound
,
496 std::vector
<const SUnit
*> WorkList
;
497 WorkList
.reserve(SUnits
.size());
499 WorkList
.push_back(SU
);
501 SU
= WorkList
.back();
503 Visited
.set(SU
->NodeNum
);
504 for (int I
= SU
->Succs
.size()-1; I
>= 0; --I
) {
505 int s
= SU
->Succs
[I
].getSUnit()->NodeNum
;
506 if (Node2Index
[s
] == UpperBound
) {
510 // Visit successors if not already and in affected region.
511 if (!Visited
.test(s
) && Node2Index
[s
] < UpperBound
) {
512 WorkList
.push_back(SU
->Succs
[I
].getSUnit());
515 } while (!WorkList
.empty());
518 /// Shift - Renumber the nodes so that the topological ordering is
520 void ScheduleDAGTopologicalSort::Shift(BitVector
& Visited
, int LowerBound
,
526 for (i
= LowerBound
; i
<= UpperBound
; ++i
) {
527 // w is node at topological index i.
528 int w
= Index2Node
[i
];
529 if (Visited
.test(w
)) {
535 Allocate(w
, i
- shift
);
539 for (unsigned j
= 0; j
< L
.size(); ++j
) {
540 Allocate(L
[j
], i
- shift
);
546 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
548 bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit
*SU
, SUnit
*TargetSU
) {
549 if (IsReachable(TargetSU
, SU
))
551 for (SUnit::pred_iterator I
= SU
->Preds
.begin(), E
= SU
->Preds
.end();
553 if (I
->isAssignedRegDep() &&
554 IsReachable(TargetSU
, I
->getSUnit()))
559 /// IsReachable - Checks if SU is reachable from TargetSU.
560 bool ScheduleDAGTopologicalSort::IsReachable(const SUnit
*SU
,
561 const SUnit
*TargetSU
) {
562 // If insertion of the edge SU->TargetSU would create a cycle
563 // then there is a path from TargetSU to SU.
564 int UpperBound
, LowerBound
;
565 LowerBound
= Node2Index
[TargetSU
->NodeNum
];
566 UpperBound
= Node2Index
[SU
->NodeNum
];
567 bool HasLoop
= false;
568 // Is Ord(TargetSU) < Ord(SU) ?
569 if (LowerBound
< UpperBound
) {
571 // There may be a path from TargetSU to SU. Check for it.
572 DFS(TargetSU
, UpperBound
, HasLoop
);
577 /// Allocate - assign the topological index to the node n.
578 void ScheduleDAGTopologicalSort::Allocate(int n
, int index
) {
579 Node2Index
[n
] = index
;
580 Index2Node
[index
] = n
;
583 ScheduleDAGTopologicalSort::
584 ScheduleDAGTopologicalSort(std::vector
<SUnit
> &sunits
) : SUnits(sunits
) {}
586 ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}