blob: 6b27db263b25296ce2bf66f83ef2b11c81fad68b [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) {
Reid Klecknerc277ab02009-09-30 20:15:38 +000085 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
86 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000087 ++NumPreds;
88 ++N->NumSuccs;
89 }
Reid Klecknerc277ab02009-09-30 20:15:38 +000090 if (!N->isScheduled) {
91 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000092 ++NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +000093 }
94 if (!isScheduled) {
95 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000096 ++N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +000097 }
Dan Gohman3f237442008-12-16 03:25:46 +000098 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +000099 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000100 if (P.getLatency() != 0) {
101 this->setDepthDirty();
102 N->setHeightDirty();
103 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000104}
105
106/// removePred - This removes the specified edge as a pred of the current
107/// node if it exists. It also removes the current node as a successor of
108/// the specified node.
109void SUnit::removePred(const SDep &D) {
110 // Find the matching predecessor.
111 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
112 I != E; ++I)
113 if (*I == D) {
114 bool FoundSucc = false;
115 // Find the corresponding successor in N.
116 SDep P = D;
117 P.setSUnit(this);
118 SUnit *N = D.getSUnit();
119 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
120 EE = N->Succs.end(); II != EE; ++II)
121 if (*II == P) {
122 FoundSucc = true;
123 N->Succs.erase(II);
124 break;
125 }
126 assert(FoundSucc && "Mismatching preds / succs lists!");
127 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000128 // Update the bookkeeping.
129 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000130 assert(NumPreds > 0 && "NumPreds will underflow!");
131 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000132 --NumPreds;
133 --N->NumSuccs;
134 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000135 if (!N->isScheduled) {
136 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000137 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000138 }
139 if (!isScheduled) {
140 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000141 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000142 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000143 if (P.getLatency() != 0) {
144 this->setDepthDirty();
145 N->setHeightDirty();
146 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000147 return;
148 }
149}
150
Dan Gohman3f237442008-12-16 03:25:46 +0000151void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000152 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000153 SmallVector<SUnit*, 8> WorkList;
154 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000155 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000156 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000157 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000158 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000159 E = SU->Succs.end(); I != E; ++I) {
160 SUnit *SuccSU = I->getSUnit();
161 if (SuccSU->isDepthCurrent)
162 WorkList.push_back(SuccSU);
163 }
164 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000165}
166
167void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000168 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000169 SmallVector<SUnit*, 8> WorkList;
170 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000171 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000172 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000173 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000174 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000175 E = SU->Preds.end(); I != E; ++I) {
176 SUnit *PredSU = I->getSUnit();
177 if (PredSU->isHeightCurrent)
178 WorkList.push_back(PredSU);
179 }
180 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000181}
182
183/// setDepthToAtLeast - Update this node's successors to reflect the
184/// fact that this node's depth just increased.
185///
David Goodwin4de099d2009-11-03 20:57:50 +0000186void SUnit::setDepthToAtLeast(unsigned NewDepth, bool IgnoreAntiDep) {
187 if (NewDepth <= getDepth(IgnoreAntiDep))
Dan Gohman3f237442008-12-16 03:25:46 +0000188 return;
189 setDepthDirty();
190 Depth = NewDepth;
191 isDepthCurrent = true;
192}
193
194/// setHeightToAtLeast - Update this node's predecessors to reflect the
195/// fact that this node's height just increased.
196///
David Goodwin4de099d2009-11-03 20:57:50 +0000197void SUnit::setHeightToAtLeast(unsigned NewHeight, bool IgnoreAntiDep) {
198 if (NewHeight <= getHeight(IgnoreAntiDep))
Dan Gohman3f237442008-12-16 03:25:46 +0000199 return;
200 setHeightDirty();
201 Height = NewHeight;
202 isHeightCurrent = true;
203}
204
205/// ComputeDepth - Calculate the maximal path from the node to the exit.
206///
David Goodwin4de099d2009-11-03 20:57:50 +0000207void SUnit::ComputeDepth(bool IgnoreAntiDep) {
Dan Gohman3f237442008-12-16 03:25:46 +0000208 SmallVector<SUnit*, 8> WorkList;
209 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000210 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000211 SUnit *Cur = WorkList.back();
212
213 bool Done = true;
214 unsigned MaxPredDepth = 0;
215 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
216 E = Cur->Preds.end(); I != E; ++I) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000217 if (IgnoreAntiDep &&
218 ((I->getKind() == SDep::Anti) || (I->getKind() == SDep::Output)))
219 continue;
220
Dan Gohman3f237442008-12-16 03:25:46 +0000221 SUnit *PredSU = I->getSUnit();
222 if (PredSU->isDepthCurrent)
223 MaxPredDepth = std::max(MaxPredDepth,
224 PredSU->Depth + I->getLatency());
225 else {
226 Done = false;
227 WorkList.push_back(PredSU);
228 }
229 }
230
231 if (Done) {
232 WorkList.pop_back();
233 if (MaxPredDepth != Cur->Depth) {
234 Cur->setDepthDirty();
235 Cur->Depth = MaxPredDepth;
236 }
237 Cur->isDepthCurrent = true;
238 }
Dan Gohman1578f842008-12-23 17:22:32 +0000239 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000240}
241
242/// ComputeHeight - Calculate the maximal path from the node to the entry.
243///
David Goodwin4de099d2009-11-03 20:57:50 +0000244void SUnit::ComputeHeight(bool IgnoreAntiDep) {
Dan Gohman3f237442008-12-16 03:25:46 +0000245 SmallVector<SUnit*, 8> WorkList;
246 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000247 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000248 SUnit *Cur = WorkList.back();
249
250 bool Done = true;
251 unsigned MaxSuccHeight = 0;
252 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
253 E = Cur->Succs.end(); I != E; ++I) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000254 if (IgnoreAntiDep &&
255 ((I->getKind() == SDep::Anti) || (I->getKind() == SDep::Output)))
256 continue;
257
Dan Gohman3f237442008-12-16 03:25:46 +0000258 SUnit *SuccSU = I->getSUnit();
259 if (SuccSU->isHeightCurrent)
260 MaxSuccHeight = std::max(MaxSuccHeight,
261 SuccSU->Height + I->getLatency());
262 else {
263 Done = false;
264 WorkList.push_back(SuccSU);
265 }
266 }
267
268 if (Done) {
269 WorkList.pop_back();
270 if (MaxSuccHeight != Cur->Height) {
271 Cur->setHeightDirty();
272 Cur->Height = MaxSuccHeight;
273 }
274 Cur->isHeightCurrent = true;
275 }
Dan Gohman1578f842008-12-23 17:22:32 +0000276 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000277}
278
Dan Gohman343f0c02008-11-19 23:18:57 +0000279/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
280/// a group of nodes flagged together.
281void SUnit::dump(const ScheduleDAG *G) const {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000282 errs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000283 G->dumpNode(this);
284}
285
286void SUnit::dumpAll(const ScheduleDAG *G) const {
287 dump(G);
288
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000289 errs() << " # preds left : " << NumPredsLeft << "\n";
290 errs() << " # succs left : " << NumSuccsLeft << "\n";
291 errs() << " Latency : " << Latency << "\n";
292 errs() << " Depth : " << Depth << "\n";
293 errs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000294
295 if (Preds.size() != 0) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000296 errs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000297 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
298 I != E; ++I) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000299 errs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000300 switch (I->getKind()) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000301 case SDep::Data: errs() << "val "; break;
302 case SDep::Anti: errs() << "anti"; break;
303 case SDep::Output: errs() << "out "; break;
304 case SDep::Order: errs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000305 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000306 errs() << "#";
307 errs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000308 if (I->isArtificial())
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000309 errs() << " *";
David Goodwinc93d8372009-08-11 17:35:23 +0000310 errs() << ": Latency=" << I->getLatency();
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000311 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000312 }
313 }
314 if (Succs.size() != 0) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000315 errs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000316 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
317 I != E; ++I) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000318 errs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000319 switch (I->getKind()) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000320 case SDep::Data: errs() << "val "; break;
321 case SDep::Anti: errs() << "anti"; break;
322 case SDep::Output: errs() << "out "; break;
323 case SDep::Order: errs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000324 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000325 errs() << "#";
326 errs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000327 if (I->isArtificial())
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000328 errs() << " *";
David Goodwinc93d8372009-08-11 17:35:23 +0000329 errs() << ": Latency=" << I->getLatency();
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000330 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000331 }
332 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000333 errs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000334}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000335
336#ifndef NDEBUG
337/// VerifySchedule - Verify that all SUnits were scheduled and that
338/// their state is consistent.
339///
340void ScheduleDAG::VerifySchedule(bool isBottomUp) {
341 bool AnyNotSched = false;
342 unsigned DeadNodes = 0;
343 unsigned Noops = 0;
344 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
345 if (!SUnits[i].isScheduled) {
346 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
347 ++DeadNodes;
348 continue;
349 }
350 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000351 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000352 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000353 errs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000354 AnyNotSched = true;
355 }
Dan Gohman3f237442008-12-16 03:25:46 +0000356 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000357 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000358 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000359 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000360 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000361 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000362 errs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000363 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000364 AnyNotSched = true;
365 }
366 if (isBottomUp) {
367 if (SUnits[i].NumSuccsLeft != 0) {
368 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000369 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000370 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000371 errs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000372 AnyNotSched = true;
373 }
374 } else {
375 if (SUnits[i].NumPredsLeft != 0) {
376 if (!AnyNotSched)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000377 errs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000378 SUnits[i].dump(this);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000379 errs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000380 AnyNotSched = true;
381 }
382 }
383 }
384 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
385 if (!Sequence[i])
386 ++Noops;
387 assert(!AnyNotSched);
388 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
389 "The number of nodes scheduled doesn't match the expected number!");
390}
391#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000392
393/// InitDAGTopologicalSorting - create the initial topological
394/// ordering from the DAG to be scheduled.
395///
396/// The idea of the algorithm is taken from
397/// "Online algorithms for managing the topological order of
398/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
399/// This is the MNR algorithm, which was first introduced by
400/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
401/// "Maintaining a topological order under edge insertions".
402///
403/// Short description of the algorithm:
404///
405/// Topological ordering, ord, of a DAG maps each node to a topological
406/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
407///
408/// This means that if there is a path from the node X to the node Z,
409/// then ord(X) < ord(Z).
410///
411/// This property can be used to check for reachability of nodes:
412/// if Z is reachable from X, then an insertion of the edge Z->X would
413/// create a cycle.
414///
415/// The algorithm first computes a topological ordering for the DAG by
416/// initializing the Index2Node and Node2Index arrays and then tries to keep
417/// the ordering up-to-date after edge insertions by reordering the DAG.
418///
419/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
420/// the nodes reachable from Y, and then shifts them using Shift to lie
421/// immediately after X in Index2Node.
422void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
423 unsigned DAGSize = SUnits.size();
424 std::vector<SUnit*> WorkList;
425 WorkList.reserve(DAGSize);
426
427 Index2Node.resize(DAGSize);
428 Node2Index.resize(DAGSize);
429
430 // Initialize the data structures.
431 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
432 SUnit *SU = &SUnits[i];
433 int NodeNum = SU->NodeNum;
434 unsigned Degree = SU->Succs.size();
435 // Temporarily use the Node2Index array as scratch space for degree counts.
436 Node2Index[NodeNum] = Degree;
437
438 // Is it a node without dependencies?
439 if (Degree == 0) {
440 assert(SU->Succs.empty() && "SUnit should have no successors");
441 // Collect leaf nodes.
442 WorkList.push_back(SU);
443 }
444 }
445
446 int Id = DAGSize;
447 while (!WorkList.empty()) {
448 SUnit *SU = WorkList.back();
449 WorkList.pop_back();
450 Allocate(SU->NodeNum, --Id);
451 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
452 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000453 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000454 if (!--Node2Index[SU->NodeNum])
455 // If all dependencies of the node are processed already,
456 // then the node can be computed now.
457 WorkList.push_back(SU);
458 }
459 }
460
461 Visited.resize(DAGSize);
462
463#ifndef NDEBUG
464 // Check correctness of the ordering
465 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
466 SUnit *SU = &SUnits[i];
467 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
468 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000469 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000470 "Wrong topological sorting");
471 }
472 }
473#endif
474}
475
476/// AddPred - Updates the topological ordering to accomodate an edge
477/// to be added from SUnit X to SUnit Y.
478void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
479 int UpperBound, LowerBound;
480 LowerBound = Node2Index[Y->NodeNum];
481 UpperBound = Node2Index[X->NodeNum];
482 bool HasLoop = false;
483 // Is Ord(X) < Ord(Y) ?
484 if (LowerBound < UpperBound) {
485 // Update the topological order.
486 Visited.reset();
487 DFS(Y, UpperBound, HasLoop);
488 assert(!HasLoop && "Inserted edge creates a loop!");
489 // Recompute topological indexes.
490 Shift(Visited, LowerBound, UpperBound);
491 }
492}
493
494/// RemovePred - Updates the topological ordering to accomodate an
495/// an edge to be removed from the specified node N from the predecessors
496/// of the current node M.
497void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
498 // InitDAGTopologicalSorting();
499}
500
501/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
502/// all nodes affected by the edge insertion. These nodes will later get new
503/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000504void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
505 bool& HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000506 std::vector<const SUnit*> WorkList;
507 WorkList.reserve(SUnits.size());
508
509 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000510 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000511 SU = WorkList.back();
512 WorkList.pop_back();
513 Visited.set(SU->NodeNum);
514 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000515 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000516 if (Node2Index[s] == UpperBound) {
517 HasLoop = true;
518 return;
519 }
520 // Visit successors if not already and in affected region.
521 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000522 WorkList.push_back(SU->Succs[I].getSUnit());
Dan Gohman21d90032008-11-25 00:52:40 +0000523 }
524 }
Dan Gohman1578f842008-12-23 17:22:32 +0000525 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000526}
527
528/// Shift - Renumber the nodes so that the topological ordering is
529/// preserved.
530void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000531 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000532 std::vector<int> L;
533 int shift = 0;
534 int i;
535
536 for (i = LowerBound; i <= UpperBound; ++i) {
537 // w is node at topological index i.
538 int w = Index2Node[i];
539 if (Visited.test(w)) {
540 // Unmark.
541 Visited.reset(w);
542 L.push_back(w);
543 shift = shift + 1;
544 } else {
545 Allocate(w, i - shift);
546 }
547 }
548
549 for (unsigned j = 0; j < L.size(); ++j) {
550 Allocate(L[j], i - shift);
551 i = i + 1;
552 }
553}
554
555
556/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
557/// create a cycle.
558bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
559 if (IsReachable(TargetSU, SU))
560 return true;
561 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
562 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000563 if (I->isAssignedRegDep() &&
564 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000565 return true;
566 return false;
567}
568
569/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000570bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
571 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000572 // If insertion of the edge SU->TargetSU would create a cycle
573 // then there is a path from TargetSU to SU.
574 int UpperBound, LowerBound;
575 LowerBound = Node2Index[TargetSU->NodeNum];
576 UpperBound = Node2Index[SU->NodeNum];
577 bool HasLoop = false;
578 // Is Ord(TargetSU) < Ord(SU) ?
579 if (LowerBound < UpperBound) {
580 Visited.reset();
581 // There may be a path from TargetSU to SU. Check for it.
582 DFS(TargetSU, UpperBound, HasLoop);
583 }
584 return HasLoop;
585}
586
587/// Allocate - assign the topological index to the node n.
588void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
589 Node2Index[n] = index;
590 Index2Node[index] = n;
591}
592
593ScheduleDAGTopologicalSort::ScheduleDAGTopologicalSort(
594 std::vector<SUnit> &sunits)
595 : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000596
597ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}