blob: ff5c236e3797cecbc04e180efc8bccb64f9fd610 [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"
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000022#include "llvm/Support/raw_ostream.h"
Dan Gohman40362062008-11-20 01:41:34 +000023#include <climits>
Dan Gohman343f0c02008-11-19 23:18:57 +000024using namespace llvm;
25
Dan Gohman79ce2762009-01-15 19:20:50 +000026ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Dan Gohman47ac0f02009-02-11 04:27:20 +000027 : TM(mf.getTarget()),
Dan Gohman79ce2762009-01-15 19:20:50 +000028 TII(TM.getInstrInfo()),
29 TRI(TM.getRegisterInfo()),
30 TLI(TM.getTargetLowering()),
31 MF(mf), MRI(mf.getRegInfo()),
Dan Gohman9e64bbb2009-02-10 23:27:53 +000032 ConstPool(MF.getConstantPool()),
33 EntrySU(), ExitSU() {
Dan Gohman343f0c02008-11-19 23:18:57 +000034}
35
36ScheduleDAG::~ScheduleDAG() {}
37
Dan Gohman343f0c02008-11-19 23:18:57 +000038/// dump - dump the schedule.
39void ScheduleDAG::dumpSchedule() const {
40 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
41 if (SUnit *SU = Sequence[i])
42 SU->dump(this);
43 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000044 errs() << "**** NOOP ****\n";
Dan Gohman343f0c02008-11-19 23:18:57 +000045 }
46}
47
48
49/// Run - perform scheduling.
50///
Dan Gohman47ac0f02009-02-11 04:27:20 +000051void ScheduleDAG::Run(MachineBasicBlock *bb,
52 MachineBasicBlock::iterator insertPos) {
53 BB = bb;
54 InsertPos = insertPos;
Dan Gohmanf7119392009-01-16 22:10:20 +000055
Dan Gohman79ce2762009-01-15 19:20:50 +000056 SUnits.clear();
57 Sequence.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +000058 EntrySU = SUnit();
59 ExitSU = SUnit();
Dan Gohman79ce2762009-01-15 19:20:50 +000060
Dan Gohman343f0c02008-11-19 23:18:57 +000061 Schedule();
Dan Gohman47ac0f02009-02-11 04:27:20 +000062
Bill Wendling960bb852009-08-22 20:41:06 +000063 DEBUG({
64 errs() << "*** Final schedule ***\n";
65 dumpSchedule();
66 errs() << '\n';
67 });
Dan Gohman343f0c02008-11-19 23:18:57 +000068}
69
Dan Gohmanc6b680e2008-12-16 01:05:52 +000070/// addPred - This adds the specified edge as a pred of the current node if
71/// not already. It also adds the current node as a successor of the
72/// specified node.
73void SUnit::addPred(const SDep &D) {
74 // If this node already has this depenence, don't add a redundant one.
Dan Gohman5cffa6f2009-02-11 00:12:28 +000075 for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end();
76 I != E; ++I)
77 if (*I == D)
Dan Gohmanc6b680e2008-12-16 01:05:52 +000078 return;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000079 // Now add a corresponding succ to N.
80 SDep P = D;
81 P.setSUnit(this);
82 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000083 // Update the bookkeeping.
84 if (D.getKind() == SDep::Data) {
85 ++NumPreds;
86 ++N->NumSuccs;
87 }
88 if (!N->isScheduled)
89 ++NumPredsLeft;
90 if (!isScheduled)
91 ++N->NumSuccsLeft;
Dan Gohman3f237442008-12-16 03:25:46 +000092 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +000093 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +000094 if (P.getLatency() != 0) {
95 this->setDepthDirty();
96 N->setHeightDirty();
97 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +000098}
99
100/// removePred - This removes the specified edge as a pred of the current
101/// node if it exists. It also removes the current node as a successor of
102/// the specified node.
103void SUnit::removePred(const SDep &D) {
104 // Find the matching predecessor.
105 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
106 I != E; ++I)
107 if (*I == D) {
108 bool FoundSucc = false;
109 // Find the corresponding successor in N.
110 SDep P = D;
111 P.setSUnit(this);
112 SUnit *N = D.getSUnit();
113 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
114 EE = N->Succs.end(); II != EE; ++II)
115 if (*II == P) {
116 FoundSucc = true;
117 N->Succs.erase(II);
118 break;
119 }
120 assert(FoundSucc && "Mismatching preds / succs lists!");
121 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000122 // Update the bookkeeping.
123 if (P.getKind() == SDep::Data) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000124 --NumPreds;
125 --N->NumSuccs;
126 }
127 if (!N->isScheduled)
128 --NumPredsLeft;
129 if (!isScheduled)
130 --N->NumSuccsLeft;
Dan Gohmana80c8592009-01-05 22:40:26 +0000131 if (P.getLatency() != 0) {
132 this->setDepthDirty();
133 N->setHeightDirty();
134 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000135 return;
136 }
137}
138
Dan Gohman3f237442008-12-16 03:25:46 +0000139void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000140 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000141 SmallVector<SUnit*, 8> WorkList;
142 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000143 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000144 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000145 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000146 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000147 E = SU->Succs.end(); I != E; ++I) {
148 SUnit *SuccSU = I->getSUnit();
149 if (SuccSU->isDepthCurrent)
150 WorkList.push_back(SuccSU);
151 }
152 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000153}
154
155void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000156 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000157 SmallVector<SUnit*, 8> WorkList;
158 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000159 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000160 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000161 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000162 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000163 E = SU->Preds.end(); I != E; ++I) {
164 SUnit *PredSU = I->getSUnit();
165 if (PredSU->isHeightCurrent)
166 WorkList.push_back(PredSU);
167 }
168 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000169}
170
171/// setDepthToAtLeast - Update this node's successors to reflect the
172/// fact that this node's depth just increased.
173///
174void SUnit::setDepthToAtLeast(unsigned NewDepth) {
Dan Gohmanfccf6dd2008-12-17 04:25:52 +0000175 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000176 return;
177 setDepthDirty();
178 Depth = NewDepth;
179 isDepthCurrent = true;
180}
181
182/// setHeightToAtLeast - Update this node's predecessors to reflect the
183/// fact that this node's height just increased.
184///
185void SUnit::setHeightToAtLeast(unsigned NewHeight) {
Dan Gohmanfccf6dd2008-12-17 04:25:52 +0000186 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000187 return;
188 setHeightDirty();
189 Height = NewHeight;
190 isHeightCurrent = true;
191}
192
193/// ComputeDepth - Calculate the maximal path from the node to the exit.
194///
195void SUnit::ComputeDepth() {
196 SmallVector<SUnit*, 8> WorkList;
197 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000198 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000199 SUnit *Cur = WorkList.back();
200
201 bool Done = true;
202 unsigned MaxPredDepth = 0;
203 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
204 E = Cur->Preds.end(); I != E; ++I) {
205 SUnit *PredSU = I->getSUnit();
206 if (PredSU->isDepthCurrent)
207 MaxPredDepth = std::max(MaxPredDepth,
208 PredSU->Depth + I->getLatency());
209 else {
210 Done = false;
211 WorkList.push_back(PredSU);
212 }
213 }
214
215 if (Done) {
216 WorkList.pop_back();
217 if (MaxPredDepth != Cur->Depth) {
218 Cur->setDepthDirty();
219 Cur->Depth = MaxPredDepth;
220 }
221 Cur->isDepthCurrent = true;
222 }
Dan Gohman1578f842008-12-23 17:22:32 +0000223 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000224}
225
226/// ComputeHeight - Calculate the maximal path from the node to the entry.
227///
228void SUnit::ComputeHeight() {
229 SmallVector<SUnit*, 8> WorkList;
230 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000231 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000232 SUnit *Cur = WorkList.back();
233
234 bool Done = true;
235 unsigned MaxSuccHeight = 0;
236 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
237 E = Cur->Succs.end(); I != E; ++I) {
238 SUnit *SuccSU = I->getSUnit();
239 if (SuccSU->isHeightCurrent)
240 MaxSuccHeight = std::max(MaxSuccHeight,
241 SuccSU->Height + I->getLatency());
242 else {
243 Done = false;
244 WorkList.push_back(SuccSU);
245 }
246 }
247
248 if (Done) {
249 WorkList.pop_back();
250 if (MaxSuccHeight != Cur->Height) {
251 Cur->setHeightDirty();
252 Cur->Height = MaxSuccHeight;
253 }
254 Cur->isHeightCurrent = true;
255 }
Dan Gohman1578f842008-12-23 17:22:32 +0000256 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000257}
258
Dan Gohman343f0c02008-11-19 23:18:57 +0000259/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
260/// a group of nodes flagged together.
261void SUnit::dump(const ScheduleDAG *G) const {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000262 errs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000263 G->dumpNode(this);
264}
265
266void SUnit::dumpAll(const ScheduleDAG *G) const {
267 dump(G);
268
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000269 errs() << " # preds left : " << NumPredsLeft << "\n";
270 errs() << " # succs left : " << NumSuccsLeft << "\n";
271 errs() << " Latency : " << Latency << "\n";
272 errs() << " Depth : " << Depth << "\n";
273 errs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000274
275 if (Preds.size() != 0) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000276 errs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000277 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
278 I != E; ++I) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000279 errs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000280 switch (I->getKind()) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000281 case SDep::Data: errs() << "val "; break;
282 case SDep::Anti: errs() << "anti"; break;
283 case SDep::Output: errs() << "out "; break;
284 case SDep::Order: errs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000285 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000286 errs() << "#";
287 errs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000288 if (I->isArtificial())
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000289 errs() << " *";
David Goodwinc93d8372009-08-11 17:35:23 +0000290 errs() << ": Latency=" << I->getLatency();
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000291 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000292 }
293 }
294 if (Succs.size() != 0) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000295 errs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000296 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
297 I != E; ++I) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000298 errs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000299 switch (I->getKind()) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000300 case SDep::Data: errs() << "val "; break;
301 case SDep::Anti: errs() << "anti"; break;
302 case SDep::Output: errs() << "out "; break;
303 case SDep::Order: errs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000304 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000305 errs() << "#";
306 errs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000307 if (I->isArtificial())
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000308 errs() << " *";
David Goodwinc93d8372009-08-11 17:35:23 +0000309 errs() << ": Latency=" << I->getLatency();
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000310 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000311 }
312 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000313 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000314}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000315
316#ifndef NDEBUG
317/// VerifySchedule - Verify that all SUnits were scheduled and that
318/// their state is consistent.
319///
320void ScheduleDAG::VerifySchedule(bool isBottomUp) {
321 bool AnyNotSched = false;
322 unsigned DeadNodes = 0;
323 unsigned Noops = 0;
324 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
325 if (!SUnits[i].isScheduled) {
326 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
327 ++DeadNodes;
328 continue;
329 }
330 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000331 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000332 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000333 errs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000334 AnyNotSched = true;
335 }
Dan Gohman3f237442008-12-16 03:25:46 +0000336 if (SUnits[i].isScheduled &&
337 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getHeight()) >
338 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000339 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000340 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000341 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000342 errs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000343 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000344 AnyNotSched = true;
345 }
346 if (isBottomUp) {
347 if (SUnits[i].NumSuccsLeft != 0) {
348 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000349 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000350 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000351 errs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000352 AnyNotSched = true;
353 }
354 } else {
355 if (SUnits[i].NumPredsLeft != 0) {
356 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000357 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000358 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000359 errs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000360 AnyNotSched = true;
361 }
362 }
363 }
364 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
365 if (!Sequence[i])
366 ++Noops;
367 assert(!AnyNotSched);
368 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
369 "The number of nodes scheduled doesn't match the expected number!");
370}
371#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000372
373/// InitDAGTopologicalSorting - create the initial topological
374/// ordering from the DAG to be scheduled.
375///
376/// The idea of the algorithm is taken from
377/// "Online algorithms for managing the topological order of
378/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
379/// This is the MNR algorithm, which was first introduced by
380/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
381/// "Maintaining a topological order under edge insertions".
382///
383/// Short description of the algorithm:
384///
385/// Topological ordering, ord, of a DAG maps each node to a topological
386/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
387///
388/// This means that if there is a path from the node X to the node Z,
389/// then ord(X) < ord(Z).
390///
391/// This property can be used to check for reachability of nodes:
392/// if Z is reachable from X, then an insertion of the edge Z->X would
393/// create a cycle.
394///
395/// The algorithm first computes a topological ordering for the DAG by
396/// initializing the Index2Node and Node2Index arrays and then tries to keep
397/// the ordering up-to-date after edge insertions by reordering the DAG.
398///
399/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
400/// the nodes reachable from Y, and then shifts them using Shift to lie
401/// immediately after X in Index2Node.
402void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
403 unsigned DAGSize = SUnits.size();
404 std::vector<SUnit*> WorkList;
405 WorkList.reserve(DAGSize);
406
407 Index2Node.resize(DAGSize);
408 Node2Index.resize(DAGSize);
409
410 // Initialize the data structures.
411 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
412 SUnit *SU = &SUnits[i];
413 int NodeNum = SU->NodeNum;
414 unsigned Degree = SU->Succs.size();
415 // Temporarily use the Node2Index array as scratch space for degree counts.
416 Node2Index[NodeNum] = Degree;
417
418 // Is it a node without dependencies?
419 if (Degree == 0) {
420 assert(SU->Succs.empty() && "SUnit should have no successors");
421 // Collect leaf nodes.
422 WorkList.push_back(SU);
423 }
424 }
425
426 int Id = DAGSize;
427 while (!WorkList.empty()) {
428 SUnit *SU = WorkList.back();
429 WorkList.pop_back();
430 Allocate(SU->NodeNum, --Id);
431 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
432 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000433 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000434 if (!--Node2Index[SU->NodeNum])
435 // If all dependencies of the node are processed already,
436 // then the node can be computed now.
437 WorkList.push_back(SU);
438 }
439 }
440
441 Visited.resize(DAGSize);
442
443#ifndef NDEBUG
444 // Check correctness of the ordering
445 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
446 SUnit *SU = &SUnits[i];
447 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
448 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000449 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000450 "Wrong topological sorting");
451 }
452 }
453#endif
454}
455
456/// AddPred - Updates the topological ordering to accomodate an edge
457/// to be added from SUnit X to SUnit Y.
458void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
459 int UpperBound, LowerBound;
460 LowerBound = Node2Index[Y->NodeNum];
461 UpperBound = Node2Index[X->NodeNum];
462 bool HasLoop = false;
463 // Is Ord(X) < Ord(Y) ?
464 if (LowerBound < UpperBound) {
465 // Update the topological order.
466 Visited.reset();
467 DFS(Y, UpperBound, HasLoop);
468 assert(!HasLoop && "Inserted edge creates a loop!");
469 // Recompute topological indexes.
470 Shift(Visited, LowerBound, UpperBound);
471 }
472}
473
474/// RemovePred - Updates the topological ordering to accomodate an
475/// an edge to be removed from the specified node N from the predecessors
476/// of the current node M.
477void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
478 // InitDAGTopologicalSorting();
479}
480
481/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
482/// all nodes affected by the edge insertion. These nodes will later get new
483/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000484void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
485 bool& HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000486 std::vector<const SUnit*> WorkList;
487 WorkList.reserve(SUnits.size());
488
489 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000490 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000491 SU = WorkList.back();
492 WorkList.pop_back();
493 Visited.set(SU->NodeNum);
494 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000495 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000496 if (Node2Index[s] == UpperBound) {
497 HasLoop = true;
498 return;
499 }
500 // Visit successors if not already and in affected region.
501 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000502 WorkList.push_back(SU->Succs[I].getSUnit());
Dan Gohman21d90032008-11-25 00:52:40 +0000503 }
504 }
Dan Gohman1578f842008-12-23 17:22:32 +0000505 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000506}
507
508/// Shift - Renumber the nodes so that the topological ordering is
509/// preserved.
510void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000511 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000512 std::vector<int> L;
513 int shift = 0;
514 int i;
515
516 for (i = LowerBound; i <= UpperBound; ++i) {
517 // w is node at topological index i.
518 int w = Index2Node[i];
519 if (Visited.test(w)) {
520 // Unmark.
521 Visited.reset(w);
522 L.push_back(w);
523 shift = shift + 1;
524 } else {
525 Allocate(w, i - shift);
526 }
527 }
528
529 for (unsigned j = 0; j < L.size(); ++j) {
530 Allocate(L[j], i - shift);
531 i = i + 1;
532 }
533}
534
535
536/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
537/// create a cycle.
538bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
539 if (IsReachable(TargetSU, SU))
540 return true;
541 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
542 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000543 if (I->isAssignedRegDep() &&
544 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000545 return true;
546 return false;
547}
548
549/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000550bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
551 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000552 // If insertion of the edge SU->TargetSU would create a cycle
553 // then there is a path from TargetSU to SU.
554 int UpperBound, LowerBound;
555 LowerBound = Node2Index[TargetSU->NodeNum];
556 UpperBound = Node2Index[SU->NodeNum];
557 bool HasLoop = false;
558 // Is Ord(TargetSU) < Ord(SU) ?
559 if (LowerBound < UpperBound) {
560 Visited.reset();
561 // There may be a path from TargetSU to SU. Check for it.
562 DFS(TargetSU, UpperBound, HasLoop);
563 }
564 return HasLoop;
565}
566
567/// Allocate - assign the topological index to the node n.
568void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
569 Node2Index[n] = index;
570 Index2Node[index] = n;
571}
572
573ScheduleDAGTopologicalSort::ScheduleDAGTopologicalSort(
574 std::vector<SUnit> &sunits)
575 : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000576
577ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}