blob: 3388889c9e917d0318c293fa7c23d1f76bd6e7c0 [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 {
Andrew Trick24312232010-12-24 06:46:50 +000039 if (!Node || !Node->isMachineOpcode()) return NULL;
Andrew Trick2da8bc82010-12-24 05:03:26 +000040 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.
Andrew Trick92e94662011-02-04 03:18:17 +000078bool SUnit::addPred(const SDep &D) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +000079 // 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)
Andrew Trick92e94662011-02-04 03:18:17 +000083 return false;
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 }
Andrew Trick92e94662011-02-04 03:18:17 +0000109 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000110}
111
112/// removePred - This removes the specified edge as a pred of the current
113/// node if it exists. It also removes the current node as a successor of
114/// the specified node.
115void SUnit::removePred(const SDep &D) {
116 // Find the matching predecessor.
117 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
118 I != E; ++I)
119 if (*I == D) {
120 bool FoundSucc = false;
121 // Find the corresponding successor in N.
122 SDep P = D;
123 P.setSUnit(this);
124 SUnit *N = D.getSUnit();
125 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
126 EE = N->Succs.end(); II != EE; ++II)
127 if (*II == P) {
128 FoundSucc = true;
129 N->Succs.erase(II);
130 break;
131 }
132 assert(FoundSucc && "Mismatching preds / succs lists!");
133 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000134 // Update the bookkeeping.
135 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000136 assert(NumPreds > 0 && "NumPreds will underflow!");
137 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000138 --NumPreds;
139 --N->NumSuccs;
140 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000141 if (!N->isScheduled) {
142 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000143 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000144 }
145 if (!isScheduled) {
146 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000147 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000148 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000149 if (P.getLatency() != 0) {
150 this->setDepthDirty();
151 N->setHeightDirty();
152 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000153 return;
154 }
155}
156
Dan Gohman3f237442008-12-16 03:25:46 +0000157void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000158 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000159 SmallVector<SUnit*, 8> WorkList;
160 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000161 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000162 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000163 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000164 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000165 E = SU->Succs.end(); I != E; ++I) {
166 SUnit *SuccSU = I->getSUnit();
167 if (SuccSU->isDepthCurrent)
168 WorkList.push_back(SuccSU);
169 }
170 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000171}
172
173void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000174 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000175 SmallVector<SUnit*, 8> WorkList;
176 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000177 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000178 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000179 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000180 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000181 E = SU->Preds.end(); I != E; ++I) {
182 SUnit *PredSU = I->getSUnit();
183 if (PredSU->isHeightCurrent)
184 WorkList.push_back(PredSU);
185 }
186 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000187}
188
189/// setDepthToAtLeast - Update this node's successors to reflect the
190/// fact that this node's depth just increased.
191///
David Goodwin557bbe62009-11-20 19:32:48 +0000192void SUnit::setDepthToAtLeast(unsigned NewDepth) {
193 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000194 return;
195 setDepthDirty();
196 Depth = NewDepth;
197 isDepthCurrent = true;
198}
199
200/// setHeightToAtLeast - Update this node's predecessors to reflect the
201/// fact that this node's height just increased.
202///
David Goodwin557bbe62009-11-20 19:32:48 +0000203void SUnit::setHeightToAtLeast(unsigned NewHeight) {
204 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000205 return;
206 setHeightDirty();
207 Height = NewHeight;
208 isHeightCurrent = true;
209}
210
211/// ComputeDepth - Calculate the maximal path from the node to the exit.
212///
David Goodwin557bbe62009-11-20 19:32:48 +0000213void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000214 SmallVector<SUnit*, 8> WorkList;
215 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000216 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000217 SUnit *Cur = WorkList.back();
218
219 bool Done = true;
220 unsigned MaxPredDepth = 0;
221 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
222 E = Cur->Preds.end(); I != E; ++I) {
223 SUnit *PredSU = I->getSUnit();
224 if (PredSU->isDepthCurrent)
225 MaxPredDepth = std::max(MaxPredDepth,
226 PredSU->Depth + I->getLatency());
227 else {
228 Done = false;
229 WorkList.push_back(PredSU);
230 }
231 }
232
233 if (Done) {
234 WorkList.pop_back();
235 if (MaxPredDepth != Cur->Depth) {
236 Cur->setDepthDirty();
237 Cur->Depth = MaxPredDepth;
238 }
239 Cur->isDepthCurrent = true;
240 }
Dan Gohman1578f842008-12-23 17:22:32 +0000241 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000242}
243
244/// ComputeHeight - Calculate the maximal path from the node to the entry.
245///
David Goodwin557bbe62009-11-20 19:32:48 +0000246void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000247 SmallVector<SUnit*, 8> WorkList;
248 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000249 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000250 SUnit *Cur = WorkList.back();
251
252 bool Done = true;
253 unsigned MaxSuccHeight = 0;
254 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
255 E = Cur->Succs.end(); I != E; ++I) {
256 SUnit *SuccSU = I->getSUnit();
257 if (SuccSU->isHeightCurrent)
258 MaxSuccHeight = std::max(MaxSuccHeight,
259 SuccSU->Height + I->getLatency());
260 else {
261 Done = false;
262 WorkList.push_back(SuccSU);
263 }
264 }
265
266 if (Done) {
267 WorkList.pop_back();
268 if (MaxSuccHeight != Cur->Height) {
269 Cur->setHeightDirty();
270 Cur->Height = MaxSuccHeight;
271 }
272 Cur->isHeightCurrent = true;
273 }
Dan Gohman1578f842008-12-23 17:22:32 +0000274 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000275}
276
Dan Gohman343f0c02008-11-19 23:18:57 +0000277/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
278/// a group of nodes flagged together.
279void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000280 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000281 G->dumpNode(this);
282}
283
284void SUnit::dumpAll(const ScheduleDAG *G) const {
285 dump(G);
286
David Greene4b134d12010-01-05 01:25:41 +0000287 dbgs() << " # preds left : " << NumPredsLeft << "\n";
288 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000289 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000290 dbgs() << " Latency : " << Latency << "\n";
291 dbgs() << " Depth : " << Depth << "\n";
292 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000293
294 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000295 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000296 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
297 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000298 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000299 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000300 case SDep::Data: dbgs() << "val "; break;
301 case SDep::Anti: dbgs() << "anti"; break;
302 case SDep::Output: dbgs() << "out "; break;
303 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000304 }
David Greene4b134d12010-01-05 01:25:41 +0000305 dbgs() << "#";
306 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000307 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000308 dbgs() << " *";
309 dbgs() << ": Latency=" << I->getLatency();
310 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000311 }
312 }
313 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000314 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000315 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
316 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000317 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000318 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000319 case SDep::Data: dbgs() << "val "; break;
320 case SDep::Anti: dbgs() << "anti"; break;
321 case SDep::Output: dbgs() << "out "; break;
322 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000323 }
David Greene4b134d12010-01-05 01:25:41 +0000324 dbgs() << "#";
325 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000326 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000327 dbgs() << " *";
328 dbgs() << ": Latency=" << I->getLatency();
329 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000330 }
331 }
David Greene4b134d12010-01-05 01:25:41 +0000332 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000333}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000334
335#ifndef NDEBUG
336/// VerifySchedule - Verify that all SUnits were scheduled and that
337/// their state is consistent.
338///
339void ScheduleDAG::VerifySchedule(bool isBottomUp) {
340 bool AnyNotSched = false;
341 unsigned DeadNodes = 0;
342 unsigned Noops = 0;
343 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
344 if (!SUnits[i].isScheduled) {
345 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
346 ++DeadNodes;
347 continue;
348 }
349 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000350 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000351 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000352 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000353 AnyNotSched = true;
354 }
Dan Gohman3f237442008-12-16 03:25:46 +0000355 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000356 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000357 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000358 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000359 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000360 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000361 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000362 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000363 AnyNotSched = true;
364 }
365 if (isBottomUp) {
366 if (SUnits[i].NumSuccsLeft != 0) {
367 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000368 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000369 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000370 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000371 AnyNotSched = true;
372 }
373 } else {
374 if (SUnits[i].NumPredsLeft != 0) {
375 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000376 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000377 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000378 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000379 AnyNotSched = true;
380 }
381 }
382 }
383 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
384 if (!Sequence[i])
385 ++Noops;
386 assert(!AnyNotSched);
387 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
388 "The number of nodes scheduled doesn't match the expected number!");
389}
390#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000391
John Mosby9f71f802010-06-30 03:40:54 +0000392/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000393/// ordering from the DAG to be scheduled.
394///
John Mosby9f71f802010-06-30 03:40:54 +0000395/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000396/// "Online algorithms for managing the topological order of
397/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000398/// This is the MNR algorithm, which was first introduced by
399/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000400/// "Maintaining a topological order under edge insertions".
401///
John Mosby9f71f802010-06-30 03:40:54 +0000402/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000403///
404/// Topological ordering, ord, of a DAG maps each node to a topological
405/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
406///
John Mosby9f71f802010-06-30 03:40:54 +0000407/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000408/// then ord(X) < ord(Z).
409///
410/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000411/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000412/// create a cycle.
413///
414/// The algorithm first computes a topological ordering for the DAG by
415/// initializing the Index2Node and Node2Index arrays and then tries to keep
416/// the ordering up-to-date after edge insertions by reordering the DAG.
417///
418/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
419/// the nodes reachable from Y, and then shifts them using Shift to lie
420/// immediately after X in Index2Node.
421void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
422 unsigned DAGSize = SUnits.size();
423 std::vector<SUnit*> WorkList;
424 WorkList.reserve(DAGSize);
425
426 Index2Node.resize(DAGSize);
427 Node2Index.resize(DAGSize);
428
429 // Initialize the data structures.
430 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
431 SUnit *SU = &SUnits[i];
432 int NodeNum = SU->NodeNum;
433 unsigned Degree = SU->Succs.size();
434 // Temporarily use the Node2Index array as scratch space for degree counts.
435 Node2Index[NodeNum] = Degree;
436
437 // Is it a node without dependencies?
438 if (Degree == 0) {
439 assert(SU->Succs.empty() && "SUnit should have no successors");
440 // Collect leaf nodes.
441 WorkList.push_back(SU);
442 }
John Mosby9f71f802010-06-30 03:40:54 +0000443 }
Dan Gohman21d90032008-11-25 00:52:40 +0000444
445 int Id = DAGSize;
446 while (!WorkList.empty()) {
447 SUnit *SU = WorkList.back();
448 WorkList.pop_back();
449 Allocate(SU->NodeNum, --Id);
450 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
451 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000452 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000453 if (!--Node2Index[SU->NodeNum])
454 // If all dependencies of the node are processed already,
455 // then the node can be computed now.
456 WorkList.push_back(SU);
457 }
458 }
459
460 Visited.resize(DAGSize);
461
462#ifndef NDEBUG
463 // Check correctness of the ordering
464 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
465 SUnit *SU = &SUnits[i];
466 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
467 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000468 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000469 "Wrong topological sorting");
470 }
471 }
472#endif
473}
474
475/// AddPred - Updates the topological ordering to accomodate an edge
476/// to be added from SUnit X to SUnit Y.
477void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
478 int UpperBound, LowerBound;
479 LowerBound = Node2Index[Y->NodeNum];
480 UpperBound = Node2Index[X->NodeNum];
481 bool HasLoop = false;
482 // Is Ord(X) < Ord(Y) ?
483 if (LowerBound < UpperBound) {
484 // Update the topological order.
485 Visited.reset();
486 DFS(Y, UpperBound, HasLoop);
487 assert(!HasLoop && "Inserted edge creates a loop!");
488 // Recompute topological indexes.
489 Shift(Visited, LowerBound, UpperBound);
490 }
491}
492
493/// RemovePred - Updates the topological ordering to accomodate an
494/// an edge to be removed from the specified node N from the predecessors
495/// of the current node M.
496void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
497 // InitDAGTopologicalSorting();
498}
499
500/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
501/// all nodes affected by the edge insertion. These nodes will later get new
502/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000503void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000504 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000505 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000506 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000507
508 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000509 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000510 SU = WorkList.back();
511 WorkList.pop_back();
512 Visited.set(SU->NodeNum);
513 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000514 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000515 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000516 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000517 return;
518 }
519 // Visit successors if not already and in affected region.
520 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000521 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000522 }
523 }
Dan Gohman1578f842008-12-23 17:22:32 +0000524 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000525}
526
John Mosby9f71f802010-06-30 03:40:54 +0000527/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000528/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000529void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000530 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000531 std::vector<int> L;
532 int shift = 0;
533 int i;
534
535 for (i = LowerBound; i <= UpperBound; ++i) {
536 // w is node at topological index i.
537 int w = Index2Node[i];
538 if (Visited.test(w)) {
539 // Unmark.
540 Visited.reset(w);
541 L.push_back(w);
542 shift = shift + 1;
543 } else {
544 Allocate(w, i - shift);
545 }
546 }
547
548 for (unsigned j = 0; j < L.size(); ++j) {
549 Allocate(L[j], i - shift);
550 i = i + 1;
551 }
552}
553
554
555/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
556/// create a cycle.
557bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
558 if (IsReachable(TargetSU, SU))
559 return true;
560 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
561 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000562 if (I->isAssignedRegDep() &&
563 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000564 return true;
565 return false;
566}
567
568/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000569bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
570 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000571 // If insertion of the edge SU->TargetSU would create a cycle
572 // then there is a path from TargetSU to SU.
573 int UpperBound, LowerBound;
574 LowerBound = Node2Index[TargetSU->NodeNum];
575 UpperBound = Node2Index[SU->NodeNum];
576 bool HasLoop = false;
577 // Is Ord(TargetSU) < Ord(SU) ?
578 if (LowerBound < UpperBound) {
579 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000580 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000581 DFS(TargetSU, UpperBound, HasLoop);
582 }
583 return HasLoop;
584}
585
586/// Allocate - assign the topological index to the node n.
587void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
588 Node2Index[n] = index;
589 Index2Node[index] = n;
590}
591
John Mosby9f71f802010-06-30 03:40:54 +0000592ScheduleDAGTopologicalSort::
593ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000594
595ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}