blob: 70ad94957133e99ad5f62fd3658eb6f57f72d267 [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"
Andrew Trick4cb971c2011-06-15 17:16:12 +000019#include "llvm/Support/CommandLine.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000020#include "llvm/Support/Debug.h"
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000021#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman40362062008-11-20 01:41:34 +000025#include <climits>
Dan Gohman343f0c02008-11-19 23:18:57 +000026using namespace llvm;
27
Andrew Trick4cb971c2011-06-15 17:16:12 +000028#ifndef NDEBUG
Benjamin Kramera67f14b2011-08-19 01:42:18 +000029static cl::opt<bool> StressSchedOpt(
Andrew Trick4cb971c2011-06-15 17:16:12 +000030 "stress-sched", cl::Hidden, cl::init(false),
31 cl::desc("Stress test instruction scheduling"));
32#endif
33
David Blaikie2d24e2a2011-12-20 02:50:00 +000034void SchedulingPriorityQueue::anchor() { }
35
Dan Gohman79ce2762009-01-15 19:20:50 +000036ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Dan Gohman47ac0f02009-02-11 04:27:20 +000037 : TM(mf.getTarget()),
Dan Gohman79ce2762009-01-15 19:20:50 +000038 TII(TM.getInstrInfo()),
39 TRI(TM.getRegisterInfo()),
Dan Gohman79ce2762009-01-15 19:20:50 +000040 MF(mf), MRI(mf.getRegInfo()),
Dan Gohman9e64bbb2009-02-10 23:27:53 +000041 EntrySU(), ExitSU() {
Andrew Trick4cb971c2011-06-15 17:16:12 +000042#ifndef NDEBUG
43 StressSched = StressSchedOpt;
44#endif
Dan Gohman343f0c02008-11-19 23:18:57 +000045}
46
47ScheduleDAG::~ScheduleDAG() {}
48
Andrew Trick47c14452012-03-07 05:21:52 +000049/// Clear the DAG state (e.g. between scheduling regions).
50void ScheduleDAG::clearDAG() {
51 SUnits.clear();
52 EntrySU = SUnit();
53 ExitSU = SUnit();
54}
55
Andrew Trick2da8bc82010-12-24 05:03:26 +000056/// getInstrDesc helper to handle SDNodes.
Evan Chenge837dea2011-06-28 19:10:37 +000057const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Andrew Trick24312232010-12-24 06:46:50 +000058 if (!Node || !Node->isMachineOpcode()) return NULL;
Andrew Trick2da8bc82010-12-24 05:03:26 +000059 return &TII->get(Node->getMachineOpcode());
60}
61
Dan Gohmanc6b680e2008-12-16 01:05:52 +000062/// addPred - This adds the specified edge as a pred of the current node if
63/// not already. It also adds the current node as a successor of the
64/// specified node.
Andrew Trickae692f22012-11-12 19:28:57 +000065bool SUnit::addPred(const SDep &D, bool Required) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +000066 // If this node already has this depenence, don't add a redundant one.
Andrew Trick9df55ee2012-06-13 02:39:00 +000067 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
68 I != E; ++I) {
Andrew Trickae692f22012-11-12 19:28:57 +000069 // Zero-latency weak edges may be added purely for heuristic ordering. Don't
70 // add them if another kind of edge already exists.
71 if (!Required && I->getSUnit() == D.getSUnit())
72 return false;
Andrew Trick9df55ee2012-06-13 02:39:00 +000073 if (I->overlaps(D)) {
74 // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
75 if (I->getLatency() < D.getLatency()) {
76 SUnit *PredSU = I->getSUnit();
77 // Find the corresponding successor in N.
78 SDep ForwardD = *I;
79 ForwardD.setSUnit(this);
80 for (SmallVector<SDep, 4>::iterator II = PredSU->Succs.begin(),
81 EE = PredSU->Succs.end(); II != EE; ++II) {
82 if (*II == ForwardD) {
83 II->setLatency(D.getLatency());
84 break;
85 }
86 }
87 I->setLatency(D.getLatency());
88 }
Andrew Trick92e94662011-02-04 03:18:17 +000089 return false;
Andrew Trick9df55ee2012-06-13 02:39:00 +000090 }
91 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +000092 // Now add a corresponding succ to N.
93 SDep P = D;
94 P.setSUnit(this);
95 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000096 // Update the bookkeeping.
97 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +000098 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
99 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000100 ++NumPreds;
101 ++N->NumSuccs;
102 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000103 if (!N->isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000104 if (D.isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000105 ++WeakPredsLeft;
106 }
107 else {
108 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
109 ++NumPredsLeft;
110 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000111 }
112 if (!isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000113 if (D.isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000114 ++N->WeakSuccsLeft;
115 }
116 else {
117 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
118 ++N->NumSuccsLeft;
119 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000120 }
Dan Gohman3f237442008-12-16 03:25:46 +0000121 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000122 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000123 if (P.getLatency() != 0) {
124 this->setDepthDirty();
125 N->setHeightDirty();
126 }
Andrew Trick92e94662011-02-04 03:18:17 +0000127 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000128}
129
130/// removePred - This removes the specified edge as a pred of the current
131/// node if it exists. It also removes the current node as a successor of
132/// the specified node.
133void SUnit::removePred(const SDep &D) {
134 // Find the matching predecessor.
135 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
136 I != E; ++I)
137 if (*I == D) {
138 bool FoundSucc = false;
139 // Find the corresponding successor in N.
140 SDep P = D;
141 P.setSUnit(this);
142 SUnit *N = D.getSUnit();
143 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
144 EE = N->Succs.end(); II != EE; ++II)
145 if (*II == P) {
146 FoundSucc = true;
147 N->Succs.erase(II);
148 break;
149 }
150 assert(FoundSucc && "Mismatching preds / succs lists!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000151 (void)FoundSucc;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000152 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000153 // Update the bookkeeping.
154 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000155 assert(NumPreds > 0 && "NumPreds will underflow!");
156 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000157 --NumPreds;
158 --N->NumSuccs;
159 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000160 if (!N->isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000161 if (D.isWeak())
Andrew Trickae692f22012-11-12 19:28:57 +0000162 --WeakPredsLeft;
163 else {
164 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
165 --NumPredsLeft;
166 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000167 }
168 if (!isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000169 if (D.isWeak())
Andrew Trickae692f22012-11-12 19:28:57 +0000170 --N->WeakSuccsLeft;
171 else {
172 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
173 --N->NumSuccsLeft;
174 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000175 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000176 if (P.getLatency() != 0) {
177 this->setDepthDirty();
178 N->setHeightDirty();
179 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000180 return;
181 }
182}
183
Dan Gohman3f237442008-12-16 03:25:46 +0000184void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000185 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000186 SmallVector<SUnit*, 8> WorkList;
187 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000188 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000189 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000190 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000191 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000192 E = SU->Succs.end(); I != E; ++I) {
193 SUnit *SuccSU = I->getSUnit();
194 if (SuccSU->isDepthCurrent)
195 WorkList.push_back(SuccSU);
196 }
197 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000198}
199
200void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000201 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000202 SmallVector<SUnit*, 8> WorkList;
203 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000204 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000205 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000206 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000207 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000208 E = SU->Preds.end(); I != E; ++I) {
209 SUnit *PredSU = I->getSUnit();
210 if (PredSU->isHeightCurrent)
211 WorkList.push_back(PredSU);
212 }
213 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000214}
215
216/// setDepthToAtLeast - Update this node's successors to reflect the
217/// fact that this node's depth just increased.
218///
David Goodwin557bbe62009-11-20 19:32:48 +0000219void SUnit::setDepthToAtLeast(unsigned NewDepth) {
220 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000221 return;
222 setDepthDirty();
223 Depth = NewDepth;
224 isDepthCurrent = true;
225}
226
227/// setHeightToAtLeast - Update this node's predecessors to reflect the
228/// fact that this node's height just increased.
229///
David Goodwin557bbe62009-11-20 19:32:48 +0000230void SUnit::setHeightToAtLeast(unsigned NewHeight) {
231 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000232 return;
233 setHeightDirty();
234 Height = NewHeight;
235 isHeightCurrent = true;
236}
237
238/// ComputeDepth - Calculate the maximal path from the node to the exit.
239///
David Goodwin557bbe62009-11-20 19:32:48 +0000240void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000241 SmallVector<SUnit*, 8> WorkList;
242 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000243 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000244 SUnit *Cur = WorkList.back();
245
246 bool Done = true;
247 unsigned MaxPredDepth = 0;
248 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
249 E = Cur->Preds.end(); I != E; ++I) {
250 SUnit *PredSU = I->getSUnit();
251 if (PredSU->isDepthCurrent)
252 MaxPredDepth = std::max(MaxPredDepth,
253 PredSU->Depth + I->getLatency());
254 else {
255 Done = false;
256 WorkList.push_back(PredSU);
257 }
258 }
259
260 if (Done) {
261 WorkList.pop_back();
262 if (MaxPredDepth != Cur->Depth) {
263 Cur->setDepthDirty();
264 Cur->Depth = MaxPredDepth;
265 }
266 Cur->isDepthCurrent = true;
267 }
Dan Gohman1578f842008-12-23 17:22:32 +0000268 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000269}
270
271/// ComputeHeight - Calculate the maximal path from the node to the entry.
272///
David Goodwin557bbe62009-11-20 19:32:48 +0000273void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000274 SmallVector<SUnit*, 8> WorkList;
275 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000276 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000277 SUnit *Cur = WorkList.back();
278
279 bool Done = true;
280 unsigned MaxSuccHeight = 0;
281 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
282 E = Cur->Succs.end(); I != E; ++I) {
283 SUnit *SuccSU = I->getSUnit();
284 if (SuccSU->isHeightCurrent)
285 MaxSuccHeight = std::max(MaxSuccHeight,
286 SuccSU->Height + I->getLatency());
287 else {
288 Done = false;
289 WorkList.push_back(SuccSU);
290 }
291 }
292
293 if (Done) {
294 WorkList.pop_back();
295 if (MaxSuccHeight != Cur->Height) {
296 Cur->setHeightDirty();
297 Cur->Height = MaxSuccHeight;
298 }
299 Cur->isHeightCurrent = true;
300 }
Dan Gohman1578f842008-12-23 17:22:32 +0000301 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000302}
303
Andrew Trick66658dd2013-01-24 02:09:55 +0000304void SUnit::biasCriticalPath() {
305 if (NumPreds < 2)
306 return;
307
308 SUnit::pred_iterator BestI = Preds.begin();
309 unsigned MaxDepth = BestI->getSUnit()->getDepth();
310 for (SUnit::pred_iterator
311 I = llvm::next(BestI), E = Preds.end(); I != E; ++I) {
312 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
313 BestI = I;
314 }
315 if (BestI != Preds.begin())
316 std::swap(*Preds.begin(), *BestI);
317}
318
Manman Renb720be62012-09-11 22:23:19 +0000319#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman343f0c02008-11-19 23:18:57 +0000320/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
321/// a group of nodes flagged together.
322void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000323 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000324 G->dumpNode(this);
325}
326
327void SUnit::dumpAll(const ScheduleDAG *G) const {
328 dump(G);
329
David Greene4b134d12010-01-05 01:25:41 +0000330 dbgs() << " # preds left : " << NumPredsLeft << "\n";
331 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickae692f22012-11-12 19:28:57 +0000332 if (WeakPredsLeft)
333 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
334 if (WeakSuccsLeft)
335 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000336 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000337 dbgs() << " Latency : " << Latency << "\n";
338 dbgs() << " Depth : " << Depth << "\n";
339 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000340
341 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000342 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000343 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
344 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000345 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000346 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000347 case SDep::Data: dbgs() << "val "; break;
348 case SDep::Anti: dbgs() << "anti"; break;
349 case SDep::Output: dbgs() << "out "; break;
350 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000351 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000352 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000353 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000354 dbgs() << " *";
355 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000356 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000357 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000358 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000359 }
360 }
361 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000362 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000363 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
364 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000365 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000366 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000367 case SDep::Data: dbgs() << "val "; break;
368 case SDep::Anti: dbgs() << "anti"; break;
369 case SDep::Output: dbgs() << "out "; break;
370 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000371 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000372 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000373 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000374 dbgs() << " *";
375 dbgs() << ": Latency=" << I->getLatency();
376 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000377 }
378 }
David Greene4b134d12010-01-05 01:25:41 +0000379 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000380}
Manman Ren77e300e2012-09-06 19:06:06 +0000381#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000382
383#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000384/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
385/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000386///
Andrew Trick4c727202012-03-07 05:21:36 +0000387unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000388 bool AnyNotSched = false;
389 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000390 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
391 if (!SUnits[i].isScheduled) {
392 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
393 ++DeadNodes;
394 continue;
395 }
396 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000397 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000398 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000399 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000400 AnyNotSched = true;
401 }
Dan Gohman3f237442008-12-16 03:25:46 +0000402 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000403 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000404 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000405 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000406 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000407 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000408 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000409 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000410 AnyNotSched = true;
411 }
412 if (isBottomUp) {
413 if (SUnits[i].NumSuccsLeft != 0) {
414 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000415 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000416 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000417 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000418 AnyNotSched = true;
419 }
420 } else {
421 if (SUnits[i].NumPredsLeft != 0) {
422 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000423 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000424 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000425 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000426 AnyNotSched = true;
427 }
428 }
429 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000430 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000431 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000432}
433#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000434
John Mosby9f71f802010-06-30 03:40:54 +0000435/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000436/// ordering from the DAG to be scheduled.
437///
John Mosby9f71f802010-06-30 03:40:54 +0000438/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000439/// "Online algorithms for managing the topological order of
440/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000441/// This is the MNR algorithm, which was first introduced by
442/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000443/// "Maintaining a topological order under edge insertions".
444///
John Mosby9f71f802010-06-30 03:40:54 +0000445/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000446///
447/// Topological ordering, ord, of a DAG maps each node to a topological
448/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
449///
John Mosby9f71f802010-06-30 03:40:54 +0000450/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000451/// then ord(X) < ord(Z).
452///
453/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000454/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000455/// create a cycle.
456///
457/// The algorithm first computes a topological ordering for the DAG by
458/// initializing the Index2Node and Node2Index arrays and then tries to keep
459/// the ordering up-to-date after edge insertions by reordering the DAG.
460///
461/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
462/// the nodes reachable from Y, and then shifts them using Shift to lie
463/// immediately after X in Index2Node.
464void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
465 unsigned DAGSize = SUnits.size();
466 std::vector<SUnit*> WorkList;
467 WorkList.reserve(DAGSize);
468
469 Index2Node.resize(DAGSize);
470 Node2Index.resize(DAGSize);
471
472 // Initialize the data structures.
Andrew Trickae692f22012-11-12 19:28:57 +0000473 if (ExitSU)
474 WorkList.push_back(ExitSU);
Dan Gohman21d90032008-11-25 00:52:40 +0000475 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
476 SUnit *SU = &SUnits[i];
477 int NodeNum = SU->NodeNum;
478 unsigned Degree = SU->Succs.size();
479 // Temporarily use the Node2Index array as scratch space for degree counts.
480 Node2Index[NodeNum] = Degree;
481
482 // Is it a node without dependencies?
483 if (Degree == 0) {
484 assert(SU->Succs.empty() && "SUnit should have no successors");
485 // Collect leaf nodes.
486 WorkList.push_back(SU);
487 }
John Mosby9f71f802010-06-30 03:40:54 +0000488 }
Dan Gohman21d90032008-11-25 00:52:40 +0000489
490 int Id = DAGSize;
491 while (!WorkList.empty()) {
492 SUnit *SU = WorkList.back();
493 WorkList.pop_back();
Andrew Trickae692f22012-11-12 19:28:57 +0000494 if (SU->NodeNum < DAGSize)
495 Allocate(SU->NodeNum, --Id);
Dan Gohman21d90032008-11-25 00:52:40 +0000496 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
497 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000498 SUnit *SU = I->getSUnit();
Andrew Trickae692f22012-11-12 19:28:57 +0000499 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohman21d90032008-11-25 00:52:40 +0000500 // If all dependencies of the node are processed already,
501 // then the node can be computed now.
502 WorkList.push_back(SU);
503 }
504 }
505
506 Visited.resize(DAGSize);
507
508#ifndef NDEBUG
509 // Check correctness of the ordering
510 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
511 SUnit *SU = &SUnits[i];
512 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
513 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000514 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000515 "Wrong topological sorting");
516 }
517 }
518#endif
519}
520
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000521/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000522/// to be added from SUnit X to SUnit Y.
523void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
524 int UpperBound, LowerBound;
525 LowerBound = Node2Index[Y->NodeNum];
526 UpperBound = Node2Index[X->NodeNum];
527 bool HasLoop = false;
528 // Is Ord(X) < Ord(Y) ?
529 if (LowerBound < UpperBound) {
530 // Update the topological order.
531 Visited.reset();
532 DFS(Y, UpperBound, HasLoop);
533 assert(!HasLoop && "Inserted edge creates a loop!");
534 // Recompute topological indexes.
535 Shift(Visited, LowerBound, UpperBound);
536 }
537}
538
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000539/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000540/// an edge to be removed from the specified node N from the predecessors
541/// of the current node M.
542void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
543 // InitDAGTopologicalSorting();
544}
545
546/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
547/// all nodes affected by the edge insertion. These nodes will later get new
548/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000549void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000550 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000551 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000552 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000553
554 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000555 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000556 SU = WorkList.back();
557 WorkList.pop_back();
558 Visited.set(SU->NodeNum);
559 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Andrew Trickae692f22012-11-12 19:28:57 +0000560 unsigned s = SU->Succs[I].getSUnit()->NodeNum;
561 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
562 if (s >= Node2Index.size())
563 continue;
Dan Gohman21d90032008-11-25 00:52:40 +0000564 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000565 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000566 return;
567 }
568 // Visit successors if not already and in affected region.
569 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000570 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000571 }
572 }
Dan Gohman1578f842008-12-23 17:22:32 +0000573 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000574}
575
John Mosby9f71f802010-06-30 03:40:54 +0000576/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000577/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000578void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000579 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000580 std::vector<int> L;
581 int shift = 0;
582 int i;
583
584 for (i = LowerBound; i <= UpperBound; ++i) {
585 // w is node at topological index i.
586 int w = Index2Node[i];
587 if (Visited.test(w)) {
588 // Unmark.
589 Visited.reset(w);
590 L.push_back(w);
591 shift = shift + 1;
592 } else {
593 Allocate(w, i - shift);
594 }
595 }
596
597 for (unsigned j = 0; j < L.size(); ++j) {
598 Allocate(L[j], i - shift);
599 i = i + 1;
600 }
601}
602
603
Andrew Trickae692f22012-11-12 19:28:57 +0000604/// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
605/// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
606bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
607 // Is SU reachable from TargetSU via successor edges?
608 if (IsReachable(SU, TargetSU))
Dan Gohman21d90032008-11-25 00:52:40 +0000609 return true;
Andrew Trickae692f22012-11-12 19:28:57 +0000610 for (SUnit::pred_iterator
611 I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000612 if (I->isAssignedRegDep() &&
Andrew Trickae692f22012-11-12 19:28:57 +0000613 IsReachable(SU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000614 return true;
615 return false;
616}
617
618/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000619bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
620 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000621 // If insertion of the edge SU->TargetSU would create a cycle
622 // then there is a path from TargetSU to SU.
623 int UpperBound, LowerBound;
624 LowerBound = Node2Index[TargetSU->NodeNum];
625 UpperBound = Node2Index[SU->NodeNum];
626 bool HasLoop = false;
627 // Is Ord(TargetSU) < Ord(SU) ?
628 if (LowerBound < UpperBound) {
629 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000630 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000631 DFS(TargetSU, UpperBound, HasLoop);
632 }
633 return HasLoop;
634}
635
636/// Allocate - assign the topological index to the node n.
637void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
638 Node2Index[n] = index;
639 Index2Node[index] = n;
640}
641
John Mosby9f71f802010-06-30 03:40:54 +0000642ScheduleDAGTopologicalSort::
Andrew Trickae692f22012-11-12 19:28:57 +0000643ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
644 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000645
646ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}