blob: 718f591902fcd87bf0092221aec14b22364ea3aa [file] [log] [blame]
Dan Gohman343f0c02008-11-19 23:18:57 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements the ScheduleDAG class, which is a base class used by
11// scheduling implementation classes.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "pre-RA-sched"
16#include "llvm/CodeGen/ScheduleDAG.h"
Dan Gohmanfc54c552009-01-15 22:18:12 +000017#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetRegisterInfo.h"
21#include "llvm/Support/Debug.h"
Dan Gohman40362062008-11-20 01:41:34 +000022#include <climits>
Dan Gohman343f0c02008-11-19 23:18:57 +000023using namespace llvm;
24
Dan Gohman79ce2762009-01-15 19:20:50 +000025ScheduleDAG::ScheduleDAG(MachineFunction &mf)
26 : DAG(0), BB(0), TM(mf.getTarget()),
27 TII(TM.getInstrInfo()),
28 TRI(TM.getRegisterInfo()),
29 TLI(TM.getTargetLowering()),
30 MF(mf), MRI(mf.getRegInfo()),
31 ConstPool(MF.getConstantPool()) {
Dan Gohman343f0c02008-11-19 23:18:57 +000032}
33
34ScheduleDAG::~ScheduleDAG() {}
35
Dan Gohman343f0c02008-11-19 23:18:57 +000036/// dump - dump the schedule.
37void ScheduleDAG::dumpSchedule() const {
38 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
39 if (SUnit *SU = Sequence[i])
40 SU->dump(this);
41 else
42 cerr << "**** NOOP ****\n";
43 }
44}
45
46
47/// Run - perform scheduling.
48///
Dan Gohmanf7119392009-01-16 22:10:20 +000049void ScheduleDAG::Run(SelectionDAG *dag, MachineBasicBlock *bb,
50 MachineBasicBlock::iterator begin,
51 MachineBasicBlock::iterator end) {
52 assert((!dag || begin == end) &&
53 "An instruction range was given for SelectionDAG scheduling!");
54
Dan Gohman79ce2762009-01-15 19:20:50 +000055 SUnits.clear();
56 Sequence.clear();
57 DAG = dag;
58 BB = bb;
Dan Gohmanf7119392009-01-16 22:10:20 +000059 Begin = begin;
60 End = end;
Dan Gohman79ce2762009-01-15 19:20:50 +000061
Dan Gohman343f0c02008-11-19 23:18:57 +000062 Schedule();
63
64 DOUT << "*** Final schedule ***\n";
65 DEBUG(dumpSchedule());
66 DOUT << "\n";
67}
68
Dan Gohmanc6b680e2008-12-16 01:05:52 +000069/// addPred - This adds the specified edge as a pred of the current node if
70/// not already. It also adds the current node as a successor of the
71/// specified node.
72void SUnit::addPred(const SDep &D) {
73 // If this node already has this depenence, don't add a redundant one.
74 for (unsigned i = 0, e = (unsigned)Preds.size(); i != e; ++i)
75 if (Preds[i] == D)
76 return;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000077 // Now add a corresponding succ to N.
78 SDep P = D;
79 P.setSUnit(this);
80 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000081 // Update the bookkeeping.
82 if (D.getKind() == SDep::Data) {
83 ++NumPreds;
84 ++N->NumSuccs;
85 }
86 if (!N->isScheduled)
87 ++NumPredsLeft;
88 if (!isScheduled)
89 ++N->NumSuccsLeft;
Dan Gohman3f237442008-12-16 03:25:46 +000090 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +000091 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +000092 if (P.getLatency() != 0) {
93 this->setDepthDirty();
94 N->setHeightDirty();
95 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +000096}
97
98/// removePred - This removes the specified edge as a pred of the current
99/// node if it exists. It also removes the current node as a successor of
100/// the specified node.
101void SUnit::removePred(const SDep &D) {
102 // Find the matching predecessor.
103 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
104 I != E; ++I)
105 if (*I == D) {
106 bool FoundSucc = false;
107 // Find the corresponding successor in N.
108 SDep P = D;
109 P.setSUnit(this);
110 SUnit *N = D.getSUnit();
111 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
112 EE = N->Succs.end(); II != EE; ++II)
113 if (*II == P) {
114 FoundSucc = true;
115 N->Succs.erase(II);
116 break;
117 }
118 assert(FoundSucc && "Mismatching preds / succs lists!");
119 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000120 // Update the bookkeeping.
121 if (P.getKind() == SDep::Data) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000122 --NumPreds;
123 --N->NumSuccs;
124 }
125 if (!N->isScheduled)
126 --NumPredsLeft;
127 if (!isScheduled)
128 --N->NumSuccsLeft;
Dan Gohmana80c8592009-01-05 22:40:26 +0000129 if (P.getLatency() != 0) {
130 this->setDepthDirty();
131 N->setHeightDirty();
132 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000133 return;
134 }
135}
136
Dan Gohman3f237442008-12-16 03:25:46 +0000137void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000138 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000139 SmallVector<SUnit*, 8> WorkList;
140 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000141 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000142 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000143 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000144 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000145 E = SU->Succs.end(); I != E; ++I) {
146 SUnit *SuccSU = I->getSUnit();
147 if (SuccSU->isDepthCurrent)
148 WorkList.push_back(SuccSU);
149 }
150 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000151}
152
153void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000154 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000155 SmallVector<SUnit*, 8> WorkList;
156 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000157 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000158 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000159 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000160 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000161 E = SU->Preds.end(); I != E; ++I) {
162 SUnit *PredSU = I->getSUnit();
163 if (PredSU->isHeightCurrent)
164 WorkList.push_back(PredSU);
165 }
166 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000167}
168
169/// setDepthToAtLeast - Update this node's successors to reflect the
170/// fact that this node's depth just increased.
171///
172void SUnit::setDepthToAtLeast(unsigned NewDepth) {
Dan Gohmanfccf6dd2008-12-17 04:25:52 +0000173 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000174 return;
175 setDepthDirty();
176 Depth = NewDepth;
177 isDepthCurrent = true;
178}
179
180/// setHeightToAtLeast - Update this node's predecessors to reflect the
181/// fact that this node's height just increased.
182///
183void SUnit::setHeightToAtLeast(unsigned NewHeight) {
Dan Gohmanfccf6dd2008-12-17 04:25:52 +0000184 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000185 return;
186 setHeightDirty();
187 Height = NewHeight;
188 isHeightCurrent = true;
189}
190
191/// ComputeDepth - Calculate the maximal path from the node to the exit.
192///
193void SUnit::ComputeDepth() {
194 SmallVector<SUnit*, 8> WorkList;
195 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000196 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000197 SUnit *Cur = WorkList.back();
198
199 bool Done = true;
200 unsigned MaxPredDepth = 0;
201 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
202 E = Cur->Preds.end(); I != E; ++I) {
203 SUnit *PredSU = I->getSUnit();
204 if (PredSU->isDepthCurrent)
205 MaxPredDepth = std::max(MaxPredDepth,
206 PredSU->Depth + I->getLatency());
207 else {
208 Done = false;
209 WorkList.push_back(PredSU);
210 }
211 }
212
213 if (Done) {
214 WorkList.pop_back();
215 if (MaxPredDepth != Cur->Depth) {
216 Cur->setDepthDirty();
217 Cur->Depth = MaxPredDepth;
218 }
219 Cur->isDepthCurrent = true;
220 }
Dan Gohman1578f842008-12-23 17:22:32 +0000221 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000222}
223
224/// ComputeHeight - Calculate the maximal path from the node to the entry.
225///
226void SUnit::ComputeHeight() {
227 SmallVector<SUnit*, 8> WorkList;
228 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000229 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000230 SUnit *Cur = WorkList.back();
231
232 bool Done = true;
233 unsigned MaxSuccHeight = 0;
234 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
235 E = Cur->Succs.end(); I != E; ++I) {
236 SUnit *SuccSU = I->getSUnit();
237 if (SuccSU->isHeightCurrent)
238 MaxSuccHeight = std::max(MaxSuccHeight,
239 SuccSU->Height + I->getLatency());
240 else {
241 Done = false;
242 WorkList.push_back(SuccSU);
243 }
244 }
245
246 if (Done) {
247 WorkList.pop_back();
248 if (MaxSuccHeight != Cur->Height) {
249 Cur->setHeightDirty();
250 Cur->Height = MaxSuccHeight;
251 }
252 Cur->isHeightCurrent = true;
253 }
Dan Gohman1578f842008-12-23 17:22:32 +0000254 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000255}
256
Dan Gohman343f0c02008-11-19 23:18:57 +0000257/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
258/// a group of nodes flagged together.
259void SUnit::dump(const ScheduleDAG *G) const {
260 cerr << "SU(" << NodeNum << "): ";
261 G->dumpNode(this);
262}
263
264void SUnit::dumpAll(const ScheduleDAG *G) const {
265 dump(G);
266
267 cerr << " # preds left : " << NumPredsLeft << "\n";
268 cerr << " # succs left : " << NumSuccsLeft << "\n";
269 cerr << " Latency : " << Latency << "\n";
270 cerr << " Depth : " << Depth << "\n";
271 cerr << " Height : " << Height << "\n";
272
273 if (Preds.size() != 0) {
274 cerr << " Predecessors:\n";
275 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
276 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000277 cerr << " ";
278 switch (I->getKind()) {
279 case SDep::Data: cerr << "val "; break;
280 case SDep::Anti: cerr << "anti"; break;
281 case SDep::Output: cerr << "out "; break;
282 case SDep::Order: cerr << "ch "; break;
283 }
284 cerr << "#";
285 cerr << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
286 if (I->isArtificial())
Dan Gohman343f0c02008-11-19 23:18:57 +0000287 cerr << " *";
288 cerr << "\n";
289 }
290 }
291 if (Succs.size() != 0) {
292 cerr << " Successors:\n";
293 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
294 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000295 cerr << " ";
296 switch (I->getKind()) {
297 case SDep::Data: cerr << "val "; break;
298 case SDep::Anti: cerr << "anti"; break;
299 case SDep::Output: cerr << "out "; break;
300 case SDep::Order: cerr << "ch "; break;
301 }
302 cerr << "#";
303 cerr << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
304 if (I->isArtificial())
Dan Gohman343f0c02008-11-19 23:18:57 +0000305 cerr << " *";
306 cerr << "\n";
307 }
308 }
309 cerr << "\n";
310}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000311
312#ifndef NDEBUG
313/// VerifySchedule - Verify that all SUnits were scheduled and that
314/// their state is consistent.
315///
316void ScheduleDAG::VerifySchedule(bool isBottomUp) {
317 bool AnyNotSched = false;
318 unsigned DeadNodes = 0;
319 unsigned Noops = 0;
320 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
321 if (!SUnits[i].isScheduled) {
322 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
323 ++DeadNodes;
324 continue;
325 }
326 if (!AnyNotSched)
327 cerr << "*** Scheduling failed! ***\n";
328 SUnits[i].dump(this);
329 cerr << "has not been scheduled!\n";
330 AnyNotSched = true;
331 }
Dan Gohman3f237442008-12-16 03:25:46 +0000332 if (SUnits[i].isScheduled &&
333 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getHeight()) >
334 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000335 if (!AnyNotSched)
336 cerr << "*** Scheduling failed! ***\n";
337 SUnits[i].dump(this);
Dan Gohman3f237442008-12-16 03:25:46 +0000338 cerr << "has an unexpected "
339 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000340 AnyNotSched = true;
341 }
342 if (isBottomUp) {
343 if (SUnits[i].NumSuccsLeft != 0) {
344 if (!AnyNotSched)
345 cerr << "*** Scheduling failed! ***\n";
346 SUnits[i].dump(this);
347 cerr << "has successors left!\n";
348 AnyNotSched = true;
349 }
350 } else {
351 if (SUnits[i].NumPredsLeft != 0) {
352 if (!AnyNotSched)
353 cerr << "*** Scheduling failed! ***\n";
354 SUnits[i].dump(this);
355 cerr << "has predecessors left!\n";
356 AnyNotSched = true;
357 }
358 }
359 }
360 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
361 if (!Sequence[i])
362 ++Noops;
363 assert(!AnyNotSched);
364 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
365 "The number of nodes scheduled doesn't match the expected number!");
366}
367#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000368
369/// InitDAGTopologicalSorting - create the initial topological
370/// ordering from the DAG to be scheduled.
371///
372/// The idea of the algorithm is taken from
373/// "Online algorithms for managing the topological order of
374/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
375/// This is the MNR algorithm, which was first introduced by
376/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
377/// "Maintaining a topological order under edge insertions".
378///
379/// Short description of the algorithm:
380///
381/// Topological ordering, ord, of a DAG maps each node to a topological
382/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
383///
384/// This means that if there is a path from the node X to the node Z,
385/// then ord(X) < ord(Z).
386///
387/// This property can be used to check for reachability of nodes:
388/// if Z is reachable from X, then an insertion of the edge Z->X would
389/// create a cycle.
390///
391/// The algorithm first computes a topological ordering for the DAG by
392/// initializing the Index2Node and Node2Index arrays and then tries to keep
393/// the ordering up-to-date after edge insertions by reordering the DAG.
394///
395/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
396/// the nodes reachable from Y, and then shifts them using Shift to lie
397/// immediately after X in Index2Node.
398void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
399 unsigned DAGSize = SUnits.size();
400 std::vector<SUnit*> WorkList;
401 WorkList.reserve(DAGSize);
402
403 Index2Node.resize(DAGSize);
404 Node2Index.resize(DAGSize);
405
406 // Initialize the data structures.
407 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
408 SUnit *SU = &SUnits[i];
409 int NodeNum = SU->NodeNum;
410 unsigned Degree = SU->Succs.size();
411 // Temporarily use the Node2Index array as scratch space for degree counts.
412 Node2Index[NodeNum] = Degree;
413
414 // Is it a node without dependencies?
415 if (Degree == 0) {
416 assert(SU->Succs.empty() && "SUnit should have no successors");
417 // Collect leaf nodes.
418 WorkList.push_back(SU);
419 }
420 }
421
422 int Id = DAGSize;
423 while (!WorkList.empty()) {
424 SUnit *SU = WorkList.back();
425 WorkList.pop_back();
426 Allocate(SU->NodeNum, --Id);
427 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
428 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000429 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000430 if (!--Node2Index[SU->NodeNum])
431 // If all dependencies of the node are processed already,
432 // then the node can be computed now.
433 WorkList.push_back(SU);
434 }
435 }
436
437 Visited.resize(DAGSize);
438
439#ifndef NDEBUG
440 // Check correctness of the ordering
441 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
442 SUnit *SU = &SUnits[i];
443 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
444 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000445 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000446 "Wrong topological sorting");
447 }
448 }
449#endif
450}
451
452/// AddPred - Updates the topological ordering to accomodate an edge
453/// to be added from SUnit X to SUnit Y.
454void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
455 int UpperBound, LowerBound;
456 LowerBound = Node2Index[Y->NodeNum];
457 UpperBound = Node2Index[X->NodeNum];
458 bool HasLoop = false;
459 // Is Ord(X) < Ord(Y) ?
460 if (LowerBound < UpperBound) {
461 // Update the topological order.
462 Visited.reset();
463 DFS(Y, UpperBound, HasLoop);
464 assert(!HasLoop && "Inserted edge creates a loop!");
465 // Recompute topological indexes.
466 Shift(Visited, LowerBound, UpperBound);
467 }
468}
469
470/// RemovePred - Updates the topological ordering to accomodate an
471/// an edge to be removed from the specified node N from the predecessors
472/// of the current node M.
473void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
474 // InitDAGTopologicalSorting();
475}
476
477/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
478/// all nodes affected by the edge insertion. These nodes will later get new
479/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000480void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
481 bool& HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000482 std::vector<const SUnit*> WorkList;
483 WorkList.reserve(SUnits.size());
484
485 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000486 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000487 SU = WorkList.back();
488 WorkList.pop_back();
489 Visited.set(SU->NodeNum);
490 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000491 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000492 if (Node2Index[s] == UpperBound) {
493 HasLoop = true;
494 return;
495 }
496 // Visit successors if not already and in affected region.
497 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000498 WorkList.push_back(SU->Succs[I].getSUnit());
Dan Gohman21d90032008-11-25 00:52:40 +0000499 }
500 }
Dan Gohman1578f842008-12-23 17:22:32 +0000501 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000502}
503
504/// Shift - Renumber the nodes so that the topological ordering is
505/// preserved.
506void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000507 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000508 std::vector<int> L;
509 int shift = 0;
510 int i;
511
512 for (i = LowerBound; i <= UpperBound; ++i) {
513 // w is node at topological index i.
514 int w = Index2Node[i];
515 if (Visited.test(w)) {
516 // Unmark.
517 Visited.reset(w);
518 L.push_back(w);
519 shift = shift + 1;
520 } else {
521 Allocate(w, i - shift);
522 }
523 }
524
525 for (unsigned j = 0; j < L.size(); ++j) {
526 Allocate(L[j], i - shift);
527 i = i + 1;
528 }
529}
530
531
532/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
533/// create a cycle.
534bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
535 if (IsReachable(TargetSU, SU))
536 return true;
537 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
538 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000539 if (I->isAssignedRegDep() &&
540 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000541 return true;
542 return false;
543}
544
545/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000546bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
547 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000548 // If insertion of the edge SU->TargetSU would create a cycle
549 // then there is a path from TargetSU to SU.
550 int UpperBound, LowerBound;
551 LowerBound = Node2Index[TargetSU->NodeNum];
552 UpperBound = Node2Index[SU->NodeNum];
553 bool HasLoop = false;
554 // Is Ord(TargetSU) < Ord(SU) ?
555 if (LowerBound < UpperBound) {
556 Visited.reset();
557 // There may be a path from TargetSU to SU. Check for it.
558 DFS(TargetSU, UpperBound, HasLoop);
559 }
560 return HasLoop;
561}
562
563/// Allocate - assign the topological index to the node n.
564void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
565 Node2Index[n] = index;
566 Index2Node[index] = n;
567}
568
569ScheduleDAGTopologicalSort::ScheduleDAGTopologicalSort(
570 std::vector<SUnit> &sunits)
571 : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000572
573ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}