blob: 75e379073578dc54de5322409a4e6e2ca25b4b18 [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.
Craig Topperf22fd3f2013-07-03 05:11:49 +000067 for (SmallVectorImpl<SDep>::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);
Craig Topperf22fd3f2013-07-03 05:11:49 +000080 for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
Andrew Trick9df55ee2012-06-13 02:39:00 +000081 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.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000135 for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
136 I != E; ++I)
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000137 if (*I == D) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000138 // Find the corresponding successor in N.
139 SDep P = D;
140 P.setSUnit(this);
141 SUnit *N = D.getSUnit();
Benjamin Kramer81474e92013-02-16 17:06:32 +0000142 SmallVectorImpl<SDep>::iterator Succ = std::find(N->Succs.begin(),
143 N->Succs.end(), P);
144 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
145 N->Succs.erase(Succ);
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000146 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000147 // Update the bookkeeping.
148 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000149 assert(NumPreds > 0 && "NumPreds will underflow!");
150 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000151 --NumPreds;
152 --N->NumSuccs;
153 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000154 if (!N->isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000155 if (D.isWeak())
Andrew Trickae692f22012-11-12 19:28:57 +0000156 --WeakPredsLeft;
157 else {
158 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
159 --NumPredsLeft;
160 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000161 }
162 if (!isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000163 if (D.isWeak())
Andrew Trickae692f22012-11-12 19:28:57 +0000164 --N->WeakSuccsLeft;
165 else {
166 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
167 --N->NumSuccsLeft;
168 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000169 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000170 if (P.getLatency() != 0) {
171 this->setDepthDirty();
172 N->setHeightDirty();
173 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000174 return;
175 }
176}
177
Dan Gohman3f237442008-12-16 03:25:46 +0000178void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000179 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000180 SmallVector<SUnit*, 8> WorkList;
181 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000182 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000183 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000184 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000185 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000186 E = SU->Succs.end(); I != E; ++I) {
187 SUnit *SuccSU = I->getSUnit();
188 if (SuccSU->isDepthCurrent)
189 WorkList.push_back(SuccSU);
190 }
191 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000192}
193
194void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000195 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000196 SmallVector<SUnit*, 8> WorkList;
197 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000198 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000199 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000200 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000201 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000202 E = SU->Preds.end(); I != E; ++I) {
203 SUnit *PredSU = I->getSUnit();
204 if (PredSU->isHeightCurrent)
205 WorkList.push_back(PredSU);
206 }
207 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000208}
209
210/// setDepthToAtLeast - Update this node's successors to reflect the
211/// fact that this node's depth just increased.
212///
David Goodwin557bbe62009-11-20 19:32:48 +0000213void SUnit::setDepthToAtLeast(unsigned NewDepth) {
214 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000215 return;
216 setDepthDirty();
217 Depth = NewDepth;
218 isDepthCurrent = true;
219}
220
221/// setHeightToAtLeast - Update this node's predecessors to reflect the
222/// fact that this node's height just increased.
223///
David Goodwin557bbe62009-11-20 19:32:48 +0000224void SUnit::setHeightToAtLeast(unsigned NewHeight) {
225 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000226 return;
227 setHeightDirty();
228 Height = NewHeight;
229 isHeightCurrent = true;
230}
231
232/// ComputeDepth - Calculate the maximal path from the node to the exit.
233///
David Goodwin557bbe62009-11-20 19:32:48 +0000234void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000235 SmallVector<SUnit*, 8> WorkList;
236 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000237 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000238 SUnit *Cur = WorkList.back();
239
240 bool Done = true;
241 unsigned MaxPredDepth = 0;
242 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
243 E = Cur->Preds.end(); I != E; ++I) {
244 SUnit *PredSU = I->getSUnit();
245 if (PredSU->isDepthCurrent)
246 MaxPredDepth = std::max(MaxPredDepth,
247 PredSU->Depth + I->getLatency());
248 else {
249 Done = false;
250 WorkList.push_back(PredSU);
251 }
252 }
253
254 if (Done) {
255 WorkList.pop_back();
256 if (MaxPredDepth != Cur->Depth) {
257 Cur->setDepthDirty();
258 Cur->Depth = MaxPredDepth;
259 }
260 Cur->isDepthCurrent = true;
261 }
Dan Gohman1578f842008-12-23 17:22:32 +0000262 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000263}
264
265/// ComputeHeight - Calculate the maximal path from the node to the entry.
266///
David Goodwin557bbe62009-11-20 19:32:48 +0000267void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000268 SmallVector<SUnit*, 8> WorkList;
269 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000270 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000271 SUnit *Cur = WorkList.back();
272
273 bool Done = true;
274 unsigned MaxSuccHeight = 0;
275 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
276 E = Cur->Succs.end(); I != E; ++I) {
277 SUnit *SuccSU = I->getSUnit();
278 if (SuccSU->isHeightCurrent)
279 MaxSuccHeight = std::max(MaxSuccHeight,
280 SuccSU->Height + I->getLatency());
281 else {
282 Done = false;
283 WorkList.push_back(SuccSU);
284 }
285 }
286
287 if (Done) {
288 WorkList.pop_back();
289 if (MaxSuccHeight != Cur->Height) {
290 Cur->setHeightDirty();
291 Cur->Height = MaxSuccHeight;
292 }
293 Cur->isHeightCurrent = true;
294 }
Dan Gohman1578f842008-12-23 17:22:32 +0000295 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000296}
297
Andrew Trick66658dd2013-01-24 02:09:55 +0000298void SUnit::biasCriticalPath() {
299 if (NumPreds < 2)
300 return;
301
302 SUnit::pred_iterator BestI = Preds.begin();
303 unsigned MaxDepth = BestI->getSUnit()->getDepth();
304 for (SUnit::pred_iterator
305 I = llvm::next(BestI), E = Preds.end(); I != E; ++I) {
306 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
307 BestI = I;
308 }
309 if (BestI != Preds.begin())
310 std::swap(*Preds.begin(), *BestI);
311}
312
Manman Renb720be62012-09-11 22:23:19 +0000313#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman343f0c02008-11-19 23:18:57 +0000314/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
315/// a group of nodes flagged together.
316void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000317 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000318 G->dumpNode(this);
319}
320
321void SUnit::dumpAll(const ScheduleDAG *G) const {
322 dump(G);
323
David Greene4b134d12010-01-05 01:25:41 +0000324 dbgs() << " # preds left : " << NumPredsLeft << "\n";
325 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickae692f22012-11-12 19:28:57 +0000326 if (WeakPredsLeft)
327 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
328 if (WeakSuccsLeft)
329 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000330 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000331 dbgs() << " Latency : " << Latency << "\n";
Andrew Trickbf32b7f2013-03-01 00:19:09 +0000332 dbgs() << " Depth : " << getDepth() << "\n";
333 dbgs() << " Height : " << getHeight() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000334
335 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000336 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000337 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
338 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000339 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000340 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000341 case SDep::Data: dbgs() << "val "; break;
342 case SDep::Anti: dbgs() << "anti"; break;
343 case SDep::Output: dbgs() << "out "; break;
344 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000345 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000346 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000347 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000348 dbgs() << " *";
349 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000350 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000351 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000352 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000353 }
354 }
355 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000356 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000357 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
358 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000359 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000360 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000361 case SDep::Data: dbgs() << "val "; break;
362 case SDep::Anti: dbgs() << "anti"; break;
363 case SDep::Output: dbgs() << "out "; break;
364 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000365 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000366 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000367 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000368 dbgs() << " *";
369 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick838038d2013-03-01 00:19:14 +0000370 if (I->isAssignedRegDep())
371 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000372 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000373 }
374 }
David Greene4b134d12010-01-05 01:25:41 +0000375 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000376}
Manman Ren77e300e2012-09-06 19:06:06 +0000377#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000378
379#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000380/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
381/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000382///
Andrew Trick4c727202012-03-07 05:21:36 +0000383unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000384 bool AnyNotSched = false;
385 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000386 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
387 if (!SUnits[i].isScheduled) {
388 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
389 ++DeadNodes;
390 continue;
391 }
392 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000393 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000394 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000395 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000396 AnyNotSched = true;
397 }
Dan Gohman3f237442008-12-16 03:25:46 +0000398 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000399 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000400 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000401 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000402 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000403 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000404 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000405 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000406 AnyNotSched = true;
407 }
408 if (isBottomUp) {
409 if (SUnits[i].NumSuccsLeft != 0) {
410 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000411 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000412 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000413 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000414 AnyNotSched = true;
415 }
416 } else {
417 if (SUnits[i].NumPredsLeft != 0) {
418 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000419 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000420 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000421 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000422 AnyNotSched = true;
423 }
424 }
425 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000426 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000427 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000428}
429#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000430
John Mosby9f71f802010-06-30 03:40:54 +0000431/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000432/// ordering from the DAG to be scheduled.
433///
John Mosby9f71f802010-06-30 03:40:54 +0000434/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000435/// "Online algorithms for managing the topological order of
436/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000437/// This is the MNR algorithm, which was first introduced by
438/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000439/// "Maintaining a topological order under edge insertions".
440///
John Mosby9f71f802010-06-30 03:40:54 +0000441/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000442///
443/// Topological ordering, ord, of a DAG maps each node to a topological
444/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
445///
John Mosby9f71f802010-06-30 03:40:54 +0000446/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000447/// then ord(X) < ord(Z).
448///
449/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000450/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000451/// create a cycle.
452///
453/// The algorithm first computes a topological ordering for the DAG by
454/// initializing the Index2Node and Node2Index arrays and then tries to keep
455/// the ordering up-to-date after edge insertions by reordering the DAG.
456///
457/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
458/// the nodes reachable from Y, and then shifts them using Shift to lie
459/// immediately after X in Index2Node.
460void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
461 unsigned DAGSize = SUnits.size();
462 std::vector<SUnit*> WorkList;
463 WorkList.reserve(DAGSize);
464
465 Index2Node.resize(DAGSize);
466 Node2Index.resize(DAGSize);
467
468 // Initialize the data structures.
Andrew Trickae692f22012-11-12 19:28:57 +0000469 if (ExitSU)
470 WorkList.push_back(ExitSU);
Dan Gohman21d90032008-11-25 00:52:40 +0000471 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
472 SUnit *SU = &SUnits[i];
473 int NodeNum = SU->NodeNum;
474 unsigned Degree = SU->Succs.size();
475 // Temporarily use the Node2Index array as scratch space for degree counts.
476 Node2Index[NodeNum] = Degree;
477
478 // Is it a node without dependencies?
479 if (Degree == 0) {
480 assert(SU->Succs.empty() && "SUnit should have no successors");
481 // Collect leaf nodes.
482 WorkList.push_back(SU);
483 }
John Mosby9f71f802010-06-30 03:40:54 +0000484 }
Dan Gohman21d90032008-11-25 00:52:40 +0000485
486 int Id = DAGSize;
487 while (!WorkList.empty()) {
488 SUnit *SU = WorkList.back();
489 WorkList.pop_back();
Andrew Trickae692f22012-11-12 19:28:57 +0000490 if (SU->NodeNum < DAGSize)
491 Allocate(SU->NodeNum, --Id);
Dan Gohman21d90032008-11-25 00:52:40 +0000492 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
493 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000494 SUnit *SU = I->getSUnit();
Andrew Trickae692f22012-11-12 19:28:57 +0000495 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohman21d90032008-11-25 00:52:40 +0000496 // If all dependencies of the node are processed already,
497 // then the node can be computed now.
498 WorkList.push_back(SU);
499 }
500 }
501
502 Visited.resize(DAGSize);
503
504#ifndef NDEBUG
505 // Check correctness of the ordering
506 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
507 SUnit *SU = &SUnits[i];
508 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
509 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000510 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000511 "Wrong topological sorting");
512 }
513 }
514#endif
515}
516
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000517/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000518/// to be added from SUnit X to SUnit Y.
519void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
520 int UpperBound, LowerBound;
521 LowerBound = Node2Index[Y->NodeNum];
522 UpperBound = Node2Index[X->NodeNum];
523 bool HasLoop = false;
524 // Is Ord(X) < Ord(Y) ?
525 if (LowerBound < UpperBound) {
526 // Update the topological order.
527 Visited.reset();
528 DFS(Y, UpperBound, HasLoop);
529 assert(!HasLoop && "Inserted edge creates a loop!");
530 // Recompute topological indexes.
531 Shift(Visited, LowerBound, UpperBound);
532 }
533}
534
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000535/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000536/// an edge to be removed from the specified node N from the predecessors
537/// of the current node M.
538void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
539 // InitDAGTopologicalSorting();
540}
541
542/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
543/// all nodes affected by the edge insertion. These nodes will later get new
544/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000545void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000546 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000547 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000548 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000549
550 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000551 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000552 SU = WorkList.back();
553 WorkList.pop_back();
554 Visited.set(SU->NodeNum);
555 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Andrew Trickae692f22012-11-12 19:28:57 +0000556 unsigned s = SU->Succs[I].getSUnit()->NodeNum;
557 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
558 if (s >= Node2Index.size())
559 continue;
Dan Gohman21d90032008-11-25 00:52:40 +0000560 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000561 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000562 return;
563 }
564 // Visit successors if not already and in affected region.
565 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000566 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000567 }
568 }
Dan Gohman1578f842008-12-23 17:22:32 +0000569 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000570}
571
John Mosby9f71f802010-06-30 03:40:54 +0000572/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000573/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000574void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000575 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000576 std::vector<int> L;
577 int shift = 0;
578 int i;
579
580 for (i = LowerBound; i <= UpperBound; ++i) {
581 // w is node at topological index i.
582 int w = Index2Node[i];
583 if (Visited.test(w)) {
584 // Unmark.
585 Visited.reset(w);
586 L.push_back(w);
587 shift = shift + 1;
588 } else {
589 Allocate(w, i - shift);
590 }
591 }
592
593 for (unsigned j = 0; j < L.size(); ++j) {
594 Allocate(L[j], i - shift);
595 i = i + 1;
596 }
597}
598
599
Andrew Trickae692f22012-11-12 19:28:57 +0000600/// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
601/// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
602bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
603 // Is SU reachable from TargetSU via successor edges?
604 if (IsReachable(SU, TargetSU))
Dan Gohman21d90032008-11-25 00:52:40 +0000605 return true;
Andrew Trickae692f22012-11-12 19:28:57 +0000606 for (SUnit::pred_iterator
607 I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000608 if (I->isAssignedRegDep() &&
Andrew Trickae692f22012-11-12 19:28:57 +0000609 IsReachable(SU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000610 return true;
611 return false;
612}
613
614/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000615bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
616 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000617 // If insertion of the edge SU->TargetSU would create a cycle
618 // then there is a path from TargetSU to SU.
619 int UpperBound, LowerBound;
620 LowerBound = Node2Index[TargetSU->NodeNum];
621 UpperBound = Node2Index[SU->NodeNum];
622 bool HasLoop = false;
623 // Is Ord(TargetSU) < Ord(SU) ?
624 if (LowerBound < UpperBound) {
625 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000626 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000627 DFS(TargetSU, UpperBound, HasLoop);
628 }
629 return HasLoop;
630}
631
632/// Allocate - assign the topological index to the node n.
633void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
634 Node2Index[n] = index;
635 Index2Node[index] = n;
636}
637
John Mosby9f71f802010-06-30 03:40:54 +0000638ScheduleDAGTopologicalSort::
Andrew Trickae692f22012-11-12 19:28:57 +0000639ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
640 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000641
642ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}