blob: 0c50db8d34568f074124c374afdc9333f36b85c9 [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"
Andrew Trick4cb971c2011-06-15 17:16:12 +000022#include "llvm/Support/CommandLine.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000023#include "llvm/Support/Debug.h"
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000024#include "llvm/Support/raw_ostream.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
Manman Renb720be62012-09-11 22:23:19 +0000304#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman343f0c02008-11-19 23:18:57 +0000305/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
306/// a group of nodes flagged together.
307void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000308 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000309 G->dumpNode(this);
310}
311
312void SUnit::dumpAll(const ScheduleDAG *G) const {
313 dump(G);
314
David Greene4b134d12010-01-05 01:25:41 +0000315 dbgs() << " # preds left : " << NumPredsLeft << "\n";
316 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickae692f22012-11-12 19:28:57 +0000317 if (WeakPredsLeft)
318 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
319 if (WeakSuccsLeft)
320 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000321 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000322 dbgs() << " Latency : " << Latency << "\n";
323 dbgs() << " Depth : " << Depth << "\n";
324 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000325
326 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000327 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000328 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
329 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000330 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000331 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000332 case SDep::Data: dbgs() << "val "; break;
333 case SDep::Anti: dbgs() << "anti"; break;
334 case SDep::Output: dbgs() << "out "; break;
335 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000336 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000337 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000338 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000339 dbgs() << " *";
340 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000341 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000342 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000343 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000344 }
345 }
346 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000347 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000348 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
349 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000350 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000351 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000352 case SDep::Data: dbgs() << "val "; break;
353 case SDep::Anti: dbgs() << "anti"; break;
354 case SDep::Output: dbgs() << "out "; break;
355 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000356 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000357 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000358 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000359 dbgs() << " *";
360 dbgs() << ": Latency=" << I->getLatency();
361 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000362 }
363 }
David Greene4b134d12010-01-05 01:25:41 +0000364 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000365}
Manman Ren77e300e2012-09-06 19:06:06 +0000366#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000367
368#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000369/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
370/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000371///
Andrew Trick4c727202012-03-07 05:21:36 +0000372unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000373 bool AnyNotSched = false;
374 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000375 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
376 if (!SUnits[i].isScheduled) {
377 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
378 ++DeadNodes;
379 continue;
380 }
381 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000382 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000383 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000384 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000385 AnyNotSched = true;
386 }
Dan Gohman3f237442008-12-16 03:25:46 +0000387 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000388 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000389 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000390 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000391 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000392 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000393 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000394 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000395 AnyNotSched = true;
396 }
397 if (isBottomUp) {
398 if (SUnits[i].NumSuccsLeft != 0) {
399 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000400 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000401 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000402 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000403 AnyNotSched = true;
404 }
405 } else {
406 if (SUnits[i].NumPredsLeft != 0) {
407 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000408 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000409 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000410 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000411 AnyNotSched = true;
412 }
413 }
414 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000415 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000416 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000417}
418#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000419
John Mosby9f71f802010-06-30 03:40:54 +0000420/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000421/// ordering from the DAG to be scheduled.
422///
John Mosby9f71f802010-06-30 03:40:54 +0000423/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000424/// "Online algorithms for managing the topological order of
425/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000426/// This is the MNR algorithm, which was first introduced by
427/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000428/// "Maintaining a topological order under edge insertions".
429///
John Mosby9f71f802010-06-30 03:40:54 +0000430/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000431///
432/// Topological ordering, ord, of a DAG maps each node to a topological
433/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
434///
John Mosby9f71f802010-06-30 03:40:54 +0000435/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000436/// then ord(X) < ord(Z).
437///
438/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000439/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000440/// create a cycle.
441///
442/// The algorithm first computes a topological ordering for the DAG by
443/// initializing the Index2Node and Node2Index arrays and then tries to keep
444/// the ordering up-to-date after edge insertions by reordering the DAG.
445///
446/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
447/// the nodes reachable from Y, and then shifts them using Shift to lie
448/// immediately after X in Index2Node.
449void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
450 unsigned DAGSize = SUnits.size();
451 std::vector<SUnit*> WorkList;
452 WorkList.reserve(DAGSize);
453
454 Index2Node.resize(DAGSize);
455 Node2Index.resize(DAGSize);
456
457 // Initialize the data structures.
Andrew Trickae692f22012-11-12 19:28:57 +0000458 if (ExitSU)
459 WorkList.push_back(ExitSU);
Dan Gohman21d90032008-11-25 00:52:40 +0000460 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
461 SUnit *SU = &SUnits[i];
462 int NodeNum = SU->NodeNum;
463 unsigned Degree = SU->Succs.size();
464 // Temporarily use the Node2Index array as scratch space for degree counts.
465 Node2Index[NodeNum] = Degree;
466
467 // Is it a node without dependencies?
468 if (Degree == 0) {
469 assert(SU->Succs.empty() && "SUnit should have no successors");
470 // Collect leaf nodes.
471 WorkList.push_back(SU);
472 }
John Mosby9f71f802010-06-30 03:40:54 +0000473 }
Dan Gohman21d90032008-11-25 00:52:40 +0000474
475 int Id = DAGSize;
476 while (!WorkList.empty()) {
477 SUnit *SU = WorkList.back();
478 WorkList.pop_back();
Andrew Trickae692f22012-11-12 19:28:57 +0000479 if (SU->NodeNum < DAGSize)
480 Allocate(SU->NodeNum, --Id);
Dan Gohman21d90032008-11-25 00:52:40 +0000481 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
482 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000483 SUnit *SU = I->getSUnit();
Andrew Trickae692f22012-11-12 19:28:57 +0000484 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohman21d90032008-11-25 00:52:40 +0000485 // If all dependencies of the node are processed already,
486 // then the node can be computed now.
487 WorkList.push_back(SU);
488 }
489 }
490
491 Visited.resize(DAGSize);
492
493#ifndef NDEBUG
494 // Check correctness of the ordering
495 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
496 SUnit *SU = &SUnits[i];
497 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
498 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000499 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000500 "Wrong topological sorting");
501 }
502 }
503#endif
504}
505
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000506/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000507/// to be added from SUnit X to SUnit Y.
508void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
509 int UpperBound, LowerBound;
510 LowerBound = Node2Index[Y->NodeNum];
511 UpperBound = Node2Index[X->NodeNum];
512 bool HasLoop = false;
513 // Is Ord(X) < Ord(Y) ?
514 if (LowerBound < UpperBound) {
515 // Update the topological order.
516 Visited.reset();
517 DFS(Y, UpperBound, HasLoop);
518 assert(!HasLoop && "Inserted edge creates a loop!");
519 // Recompute topological indexes.
520 Shift(Visited, LowerBound, UpperBound);
521 }
522}
523
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000524/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000525/// an edge to be removed from the specified node N from the predecessors
526/// of the current node M.
527void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
528 // InitDAGTopologicalSorting();
529}
530
531/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
532/// all nodes affected by the edge insertion. These nodes will later get new
533/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000534void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000535 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000536 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000537 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000538
539 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000540 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000541 SU = WorkList.back();
542 WorkList.pop_back();
543 Visited.set(SU->NodeNum);
544 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Andrew Trickae692f22012-11-12 19:28:57 +0000545 unsigned s = SU->Succs[I].getSUnit()->NodeNum;
546 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
547 if (s >= Node2Index.size())
548 continue;
Dan Gohman21d90032008-11-25 00:52:40 +0000549 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000550 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000551 return;
552 }
553 // Visit successors if not already and in affected region.
554 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000555 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000556 }
557 }
Dan Gohman1578f842008-12-23 17:22:32 +0000558 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000559}
560
John Mosby9f71f802010-06-30 03:40:54 +0000561/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000562/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000563void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000564 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000565 std::vector<int> L;
566 int shift = 0;
567 int i;
568
569 for (i = LowerBound; i <= UpperBound; ++i) {
570 // w is node at topological index i.
571 int w = Index2Node[i];
572 if (Visited.test(w)) {
573 // Unmark.
574 Visited.reset(w);
575 L.push_back(w);
576 shift = shift + 1;
577 } else {
578 Allocate(w, i - shift);
579 }
580 }
581
582 for (unsigned j = 0; j < L.size(); ++j) {
583 Allocate(L[j], i - shift);
584 i = i + 1;
585 }
586}
587
588
Andrew Trickae692f22012-11-12 19:28:57 +0000589/// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
590/// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
591bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
592 // Is SU reachable from TargetSU via successor edges?
593 if (IsReachable(SU, TargetSU))
Dan Gohman21d90032008-11-25 00:52:40 +0000594 return true;
Andrew Trickae692f22012-11-12 19:28:57 +0000595 for (SUnit::pred_iterator
596 I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000597 if (I->isAssignedRegDep() &&
Andrew Trickae692f22012-11-12 19:28:57 +0000598 IsReachable(SU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000599 return true;
600 return false;
601}
602
603/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000604bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
605 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000606 // If insertion of the edge SU->TargetSU would create a cycle
607 // then there is a path from TargetSU to SU.
608 int UpperBound, LowerBound;
609 LowerBound = Node2Index[TargetSU->NodeNum];
610 UpperBound = Node2Index[SU->NodeNum];
611 bool HasLoop = false;
612 // Is Ord(TargetSU) < Ord(SU) ?
613 if (LowerBound < UpperBound) {
614 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000615 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000616 DFS(TargetSU, UpperBound, HasLoop);
617 }
618 return HasLoop;
619}
620
621/// Allocate - assign the topological index to the node n.
622void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
623 Node2Index[n] = index;
624 Index2Node[index] = n;
625}
626
John Mosby9f71f802010-06-30 03:40:54 +0000627ScheduleDAGTopologicalSort::
Andrew Trickae692f22012-11-12 19:28:57 +0000628ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
629 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000630
631ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}