blob: 02e398f7efc834ba688ff5c96f46006c266a7bbf [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"
Andrew Trick2da8bc82010-12-24 05:03:26 +000018#include "llvm/CodeGen/SelectionDAGNodes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetRegisterInfo.h"
22#include "llvm/Support/Debug.h"
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000023#include "llvm/Support/raw_ostream.h"
Dan Gohman40362062008-11-20 01:41:34 +000024#include <climits>
Dan Gohman343f0c02008-11-19 23:18:57 +000025using namespace llvm;
26
Dan Gohman79ce2762009-01-15 19:20:50 +000027ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Dan Gohman47ac0f02009-02-11 04:27:20 +000028 : TM(mf.getTarget()),
Dan Gohman79ce2762009-01-15 19:20:50 +000029 TII(TM.getInstrInfo()),
30 TRI(TM.getRegisterInfo()),
Dan Gohman79ce2762009-01-15 19:20:50 +000031 MF(mf), MRI(mf.getRegInfo()),
Dan Gohman9e64bbb2009-02-10 23:27:53 +000032 EntrySU(), ExitSU() {
Dan Gohman343f0c02008-11-19 23:18:57 +000033}
34
35ScheduleDAG::~ScheduleDAG() {}
36
Andrew Trick2da8bc82010-12-24 05:03:26 +000037/// getInstrDesc helper to handle SDNodes.
38const TargetInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
39 if (!Node->isMachineOpcode()) return NULL;
40 return &TII->get(Node->getMachineOpcode());
41}
42
Dan Gohman343f0c02008-11-19 23:18:57 +000043/// dump - dump the schedule.
44void ScheduleDAG::dumpSchedule() const {
45 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
46 if (SUnit *SU = Sequence[i])
47 SU->dump(this);
48 else
David Greene4b134d12010-01-05 01:25:41 +000049 dbgs() << "**** NOOP ****\n";
Dan Gohman343f0c02008-11-19 23:18:57 +000050 }
51}
52
53
54/// Run - perform scheduling.
55///
Dan Gohman47ac0f02009-02-11 04:27:20 +000056void ScheduleDAG::Run(MachineBasicBlock *bb,
57 MachineBasicBlock::iterator insertPos) {
58 BB = bb;
59 InsertPos = insertPos;
Dan Gohmanf7119392009-01-16 22:10:20 +000060
Dan Gohman79ce2762009-01-15 19:20:50 +000061 SUnits.clear();
62 Sequence.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +000063 EntrySU = SUnit();
64 ExitSU = SUnit();
Dan Gohman79ce2762009-01-15 19:20:50 +000065
Dan Gohman343f0c02008-11-19 23:18:57 +000066 Schedule();
Dan Gohman47ac0f02009-02-11 04:27:20 +000067
Bill Wendling960bb852009-08-22 20:41:06 +000068 DEBUG({
David Greene4b134d12010-01-05 01:25:41 +000069 dbgs() << "*** Final schedule ***\n";
Bill Wendling960bb852009-08-22 20:41:06 +000070 dumpSchedule();
David Greene4b134d12010-01-05 01:25:41 +000071 dbgs() << '\n';
Bill Wendling960bb852009-08-22 20:41:06 +000072 });
Dan Gohman343f0c02008-11-19 23:18:57 +000073}
74
Dan Gohmanc6b680e2008-12-16 01:05:52 +000075/// addPred - This adds the specified edge as a pred of the current node if
76/// not already. It also adds the current node as a successor of the
77/// specified node.
78void SUnit::addPred(const SDep &D) {
79 // If this node already has this depenence, don't add a redundant one.
Dan Gohman5cffa6f2009-02-11 00:12:28 +000080 for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end();
81 I != E; ++I)
82 if (*I == D)
Dan Gohmanc6b680e2008-12-16 01:05:52 +000083 return;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000084 // Now add a corresponding succ to N.
85 SDep P = D;
86 P.setSUnit(this);
87 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000088 // Update the bookkeeping.
89 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +000090 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
91 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000092 ++NumPreds;
93 ++N->NumSuccs;
94 }
Reid Klecknerc277ab02009-09-30 20:15:38 +000095 if (!N->isScheduled) {
96 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000097 ++NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +000098 }
99 if (!isScheduled) {
100 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000101 ++N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000102 }
Dan Gohman3f237442008-12-16 03:25:46 +0000103 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000104 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000105 if (P.getLatency() != 0) {
106 this->setDepthDirty();
107 N->setHeightDirty();
108 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000109}
110
111/// removePred - This removes the specified edge as a pred of the current
112/// node if it exists. It also removes the current node as a successor of
113/// the specified node.
114void SUnit::removePred(const SDep &D) {
115 // Find the matching predecessor.
116 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
117 I != E; ++I)
118 if (*I == D) {
119 bool FoundSucc = false;
120 // Find the corresponding successor in N.
121 SDep P = D;
122 P.setSUnit(this);
123 SUnit *N = D.getSUnit();
124 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
125 EE = N->Succs.end(); II != EE; ++II)
126 if (*II == P) {
127 FoundSucc = true;
128 N->Succs.erase(II);
129 break;
130 }
131 assert(FoundSucc && "Mismatching preds / succs lists!");
132 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000133 // Update the bookkeeping.
134 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000135 assert(NumPreds > 0 && "NumPreds will underflow!");
136 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000137 --NumPreds;
138 --N->NumSuccs;
139 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000140 if (!N->isScheduled) {
141 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000142 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000143 }
144 if (!isScheduled) {
145 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000146 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000147 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000148 if (P.getLatency() != 0) {
149 this->setDepthDirty();
150 N->setHeightDirty();
151 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000152 return;
153 }
154}
155
Dan Gohman3f237442008-12-16 03:25:46 +0000156void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000157 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000158 SmallVector<SUnit*, 8> WorkList;
159 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000160 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000161 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000162 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000163 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000164 E = SU->Succs.end(); I != E; ++I) {
165 SUnit *SuccSU = I->getSUnit();
166 if (SuccSU->isDepthCurrent)
167 WorkList.push_back(SuccSU);
168 }
169 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000170}
171
172void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000173 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000174 SmallVector<SUnit*, 8> WorkList;
175 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000176 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000177 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000178 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000179 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000180 E = SU->Preds.end(); I != E; ++I) {
181 SUnit *PredSU = I->getSUnit();
182 if (PredSU->isHeightCurrent)
183 WorkList.push_back(PredSU);
184 }
185 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000186}
187
188/// setDepthToAtLeast - Update this node's successors to reflect the
189/// fact that this node's depth just increased.
190///
David Goodwin557bbe62009-11-20 19:32:48 +0000191void SUnit::setDepthToAtLeast(unsigned NewDepth) {
192 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000193 return;
194 setDepthDirty();
195 Depth = NewDepth;
196 isDepthCurrent = true;
197}
198
199/// setHeightToAtLeast - Update this node's predecessors to reflect the
200/// fact that this node's height just increased.
201///
David Goodwin557bbe62009-11-20 19:32:48 +0000202void SUnit::setHeightToAtLeast(unsigned NewHeight) {
203 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000204 return;
205 setHeightDirty();
206 Height = NewHeight;
207 isHeightCurrent = true;
208}
209
210/// ComputeDepth - Calculate the maximal path from the node to the exit.
211///
David Goodwin557bbe62009-11-20 19:32:48 +0000212void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000213 SmallVector<SUnit*, 8> WorkList;
214 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000215 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000216 SUnit *Cur = WorkList.back();
217
218 bool Done = true;
219 unsigned MaxPredDepth = 0;
220 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
221 E = Cur->Preds.end(); I != E; ++I) {
222 SUnit *PredSU = I->getSUnit();
223 if (PredSU->isDepthCurrent)
224 MaxPredDepth = std::max(MaxPredDepth,
225 PredSU->Depth + I->getLatency());
226 else {
227 Done = false;
228 WorkList.push_back(PredSU);
229 }
230 }
231
232 if (Done) {
233 WorkList.pop_back();
234 if (MaxPredDepth != Cur->Depth) {
235 Cur->setDepthDirty();
236 Cur->Depth = MaxPredDepth;
237 }
238 Cur->isDepthCurrent = true;
239 }
Dan Gohman1578f842008-12-23 17:22:32 +0000240 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000241}
242
243/// ComputeHeight - Calculate the maximal path from the node to the entry.
244///
David Goodwin557bbe62009-11-20 19:32:48 +0000245void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000246 SmallVector<SUnit*, 8> WorkList;
247 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000248 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000249 SUnit *Cur = WorkList.back();
250
251 bool Done = true;
252 unsigned MaxSuccHeight = 0;
253 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
254 E = Cur->Succs.end(); I != E; ++I) {
255 SUnit *SuccSU = I->getSUnit();
256 if (SuccSU->isHeightCurrent)
257 MaxSuccHeight = std::max(MaxSuccHeight,
258 SuccSU->Height + I->getLatency());
259 else {
260 Done = false;
261 WorkList.push_back(SuccSU);
262 }
263 }
264
265 if (Done) {
266 WorkList.pop_back();
267 if (MaxSuccHeight != Cur->Height) {
268 Cur->setHeightDirty();
269 Cur->Height = MaxSuccHeight;
270 }
271 Cur->isHeightCurrent = true;
272 }
Dan Gohman1578f842008-12-23 17:22:32 +0000273 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000274}
275
Dan Gohman343f0c02008-11-19 23:18:57 +0000276/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
277/// a group of nodes flagged together.
278void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000279 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000280 G->dumpNode(this);
281}
282
283void SUnit::dumpAll(const ScheduleDAG *G) const {
284 dump(G);
285
David Greene4b134d12010-01-05 01:25:41 +0000286 dbgs() << " # preds left : " << NumPredsLeft << "\n";
287 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
288 dbgs() << " Latency : " << Latency << "\n";
289 dbgs() << " Depth : " << Depth << "\n";
290 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000291
292 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000293 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000294 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
295 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000296 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000297 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000298 case SDep::Data: dbgs() << "val "; break;
299 case SDep::Anti: dbgs() << "anti"; break;
300 case SDep::Output: dbgs() << "out "; break;
301 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000302 }
David Greene4b134d12010-01-05 01:25:41 +0000303 dbgs() << "#";
304 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000305 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000306 dbgs() << " *";
307 dbgs() << ": Latency=" << I->getLatency();
308 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000309 }
310 }
311 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000312 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000313 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
314 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000315 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000316 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000317 case SDep::Data: dbgs() << "val "; break;
318 case SDep::Anti: dbgs() << "anti"; break;
319 case SDep::Output: dbgs() << "out "; break;
320 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000321 }
David Greene4b134d12010-01-05 01:25:41 +0000322 dbgs() << "#";
323 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000324 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000325 dbgs() << " *";
326 dbgs() << ": Latency=" << I->getLatency();
327 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000328 }
329 }
David Greene4b134d12010-01-05 01:25:41 +0000330 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000331}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000332
333#ifndef NDEBUG
334/// VerifySchedule - Verify that all SUnits were scheduled and that
335/// their state is consistent.
336///
337void ScheduleDAG::VerifySchedule(bool isBottomUp) {
338 bool AnyNotSched = false;
339 unsigned DeadNodes = 0;
340 unsigned Noops = 0;
341 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
342 if (!SUnits[i].isScheduled) {
343 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
344 ++DeadNodes;
345 continue;
346 }
347 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000348 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000349 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000350 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000351 AnyNotSched = true;
352 }
Dan Gohman3f237442008-12-16 03:25:46 +0000353 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000354 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000355 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000356 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000357 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000358 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000359 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000360 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000361 AnyNotSched = true;
362 }
363 if (isBottomUp) {
364 if (SUnits[i].NumSuccsLeft != 0) {
365 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000366 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000367 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000368 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000369 AnyNotSched = true;
370 }
371 } else {
372 if (SUnits[i].NumPredsLeft != 0) {
373 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000374 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000375 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000376 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000377 AnyNotSched = true;
378 }
379 }
380 }
381 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
382 if (!Sequence[i])
383 ++Noops;
384 assert(!AnyNotSched);
385 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
386 "The number of nodes scheduled doesn't match the expected number!");
387}
388#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000389
John Mosby9f71f802010-06-30 03:40:54 +0000390/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000391/// ordering from the DAG to be scheduled.
392///
John Mosby9f71f802010-06-30 03:40:54 +0000393/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000394/// "Online algorithms for managing the topological order of
395/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000396/// This is the MNR algorithm, which was first introduced by
397/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000398/// "Maintaining a topological order under edge insertions".
399///
John Mosby9f71f802010-06-30 03:40:54 +0000400/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000401///
402/// Topological ordering, ord, of a DAG maps each node to a topological
403/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
404///
John Mosby9f71f802010-06-30 03:40:54 +0000405/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000406/// then ord(X) < ord(Z).
407///
408/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000409/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000410/// create a cycle.
411///
412/// The algorithm first computes a topological ordering for the DAG by
413/// initializing the Index2Node and Node2Index arrays and then tries to keep
414/// the ordering up-to-date after edge insertions by reordering the DAG.
415///
416/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
417/// the nodes reachable from Y, and then shifts them using Shift to lie
418/// immediately after X in Index2Node.
419void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
420 unsigned DAGSize = SUnits.size();
421 std::vector<SUnit*> WorkList;
422 WorkList.reserve(DAGSize);
423
424 Index2Node.resize(DAGSize);
425 Node2Index.resize(DAGSize);
426
427 // Initialize the data structures.
428 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
429 SUnit *SU = &SUnits[i];
430 int NodeNum = SU->NodeNum;
431 unsigned Degree = SU->Succs.size();
432 // Temporarily use the Node2Index array as scratch space for degree counts.
433 Node2Index[NodeNum] = Degree;
434
435 // Is it a node without dependencies?
436 if (Degree == 0) {
437 assert(SU->Succs.empty() && "SUnit should have no successors");
438 // Collect leaf nodes.
439 WorkList.push_back(SU);
440 }
John Mosby9f71f802010-06-30 03:40:54 +0000441 }
Dan Gohman21d90032008-11-25 00:52:40 +0000442
443 int Id = DAGSize;
444 while (!WorkList.empty()) {
445 SUnit *SU = WorkList.back();
446 WorkList.pop_back();
447 Allocate(SU->NodeNum, --Id);
448 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
449 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000450 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000451 if (!--Node2Index[SU->NodeNum])
452 // If all dependencies of the node are processed already,
453 // then the node can be computed now.
454 WorkList.push_back(SU);
455 }
456 }
457
458 Visited.resize(DAGSize);
459
460#ifndef NDEBUG
461 // Check correctness of the ordering
462 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
463 SUnit *SU = &SUnits[i];
464 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
465 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000466 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000467 "Wrong topological sorting");
468 }
469 }
470#endif
471}
472
473/// AddPred - Updates the topological ordering to accomodate an edge
474/// to be added from SUnit X to SUnit Y.
475void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
476 int UpperBound, LowerBound;
477 LowerBound = Node2Index[Y->NodeNum];
478 UpperBound = Node2Index[X->NodeNum];
479 bool HasLoop = false;
480 // Is Ord(X) < Ord(Y) ?
481 if (LowerBound < UpperBound) {
482 // Update the topological order.
483 Visited.reset();
484 DFS(Y, UpperBound, HasLoop);
485 assert(!HasLoop && "Inserted edge creates a loop!");
486 // Recompute topological indexes.
487 Shift(Visited, LowerBound, UpperBound);
488 }
489}
490
491/// RemovePred - Updates the topological ordering to accomodate an
492/// an edge to be removed from the specified node N from the predecessors
493/// of the current node M.
494void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
495 // InitDAGTopologicalSorting();
496}
497
498/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
499/// all nodes affected by the edge insertion. These nodes will later get new
500/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000501void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000502 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000503 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000504 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000505
506 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000507 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000508 SU = WorkList.back();
509 WorkList.pop_back();
510 Visited.set(SU->NodeNum);
511 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000512 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000513 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000514 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000515 return;
516 }
517 // Visit successors if not already and in affected region.
518 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000519 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000520 }
521 }
Dan Gohman1578f842008-12-23 17:22:32 +0000522 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000523}
524
John Mosby9f71f802010-06-30 03:40:54 +0000525/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000526/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000527void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000528 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000529 std::vector<int> L;
530 int shift = 0;
531 int i;
532
533 for (i = LowerBound; i <= UpperBound; ++i) {
534 // w is node at topological index i.
535 int w = Index2Node[i];
536 if (Visited.test(w)) {
537 // Unmark.
538 Visited.reset(w);
539 L.push_back(w);
540 shift = shift + 1;
541 } else {
542 Allocate(w, i - shift);
543 }
544 }
545
546 for (unsigned j = 0; j < L.size(); ++j) {
547 Allocate(L[j], i - shift);
548 i = i + 1;
549 }
550}
551
552
553/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
554/// create a cycle.
555bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
556 if (IsReachable(TargetSU, SU))
557 return true;
558 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
559 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000560 if (I->isAssignedRegDep() &&
561 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000562 return true;
563 return false;
564}
565
566/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000567bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
568 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000569 // If insertion of the edge SU->TargetSU would create a cycle
570 // then there is a path from TargetSU to SU.
571 int UpperBound, LowerBound;
572 LowerBound = Node2Index[TargetSU->NodeNum];
573 UpperBound = Node2Index[SU->NodeNum];
574 bool HasLoop = false;
575 // Is Ord(TargetSU) < Ord(SU) ?
576 if (LowerBound < UpperBound) {
577 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000578 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000579 DFS(TargetSU, UpperBound, HasLoop);
580 }
581 return HasLoop;
582}
583
584/// Allocate - assign the topological index to the node n.
585void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
586 Node2Index[n] = index;
587 Index2Node[index] = n;
588}
589
John Mosby9f71f802010-06-30 03:40:54 +0000590ScheduleDAGTopologicalSort::
591ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000592
593ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}