blob: 6224036663734c9dfcde882d9f097d4339eb0b67 [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 }
Andrew Trickae692f22012-11-12 19:28:57 +0000103 // SD scheduler relies on artificial edges to enforce physreg
104 // antidependence, so it doesn't treat them as weak edges.
105 bool isWeak = D.isWeak() && N->isInstr();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000106 if (!N->isScheduled) {
Andrew Trickae692f22012-11-12 19:28:57 +0000107 if (isWeak) {
108 ++WeakPredsLeft;
109 }
110 else {
111 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
112 ++NumPredsLeft;
113 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000114 }
115 if (!isScheduled) {
Andrew Trickae692f22012-11-12 19:28:57 +0000116 if (isWeak) {
117 ++N->WeakSuccsLeft;
118 }
119 else {
120 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
121 ++N->NumSuccsLeft;
122 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000123 }
Dan Gohman3f237442008-12-16 03:25:46 +0000124 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000125 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000126 if (P.getLatency() != 0) {
127 this->setDepthDirty();
128 N->setHeightDirty();
129 }
Andrew Trick92e94662011-02-04 03:18:17 +0000130 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000131}
132
133/// removePred - This removes the specified edge as a pred of the current
134/// node if it exists. It also removes the current node as a successor of
135/// the specified node.
136void SUnit::removePred(const SDep &D) {
137 // Find the matching predecessor.
138 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
139 I != E; ++I)
140 if (*I == D) {
141 bool FoundSucc = false;
142 // Find the corresponding successor in N.
143 SDep P = D;
144 P.setSUnit(this);
145 SUnit *N = D.getSUnit();
146 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
147 EE = N->Succs.end(); II != EE; ++II)
148 if (*II == P) {
149 FoundSucc = true;
150 N->Succs.erase(II);
151 break;
152 }
153 assert(FoundSucc && "Mismatching preds / succs lists!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000154 (void)FoundSucc;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000155 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000156 // Update the bookkeeping.
157 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000158 assert(NumPreds > 0 && "NumPreds will underflow!");
159 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000160 --NumPreds;
161 --N->NumSuccs;
162 }
Andrew Trickae692f22012-11-12 19:28:57 +0000163 bool isWeak = D.isWeak() && N->isInstr();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000164 if (!N->isScheduled) {
Andrew Trickae692f22012-11-12 19:28:57 +0000165 if (isWeak)
166 --WeakPredsLeft;
167 else {
168 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
169 --NumPredsLeft;
170 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000171 }
172 if (!isScheduled) {
Andrew Trickae692f22012-11-12 19:28:57 +0000173 if (isWeak)
174 --N->WeakSuccsLeft;
175 else {
176 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
177 --N->NumSuccsLeft;
178 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000179 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000180 if (P.getLatency() != 0) {
181 this->setDepthDirty();
182 N->setHeightDirty();
183 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000184 return;
185 }
186}
187
Dan Gohman3f237442008-12-16 03:25:46 +0000188void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000189 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000190 SmallVector<SUnit*, 8> WorkList;
191 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000192 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000193 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000194 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000195 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000196 E = SU->Succs.end(); I != E; ++I) {
197 SUnit *SuccSU = I->getSUnit();
198 if (SuccSU->isDepthCurrent)
199 WorkList.push_back(SuccSU);
200 }
201 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000202}
203
204void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000205 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000206 SmallVector<SUnit*, 8> WorkList;
207 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000208 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000209 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000210 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000211 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000212 E = SU->Preds.end(); I != E; ++I) {
213 SUnit *PredSU = I->getSUnit();
214 if (PredSU->isHeightCurrent)
215 WorkList.push_back(PredSU);
216 }
217 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000218}
219
220/// setDepthToAtLeast - Update this node's successors to reflect the
221/// fact that this node's depth just increased.
222///
David Goodwin557bbe62009-11-20 19:32:48 +0000223void SUnit::setDepthToAtLeast(unsigned NewDepth) {
224 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000225 return;
226 setDepthDirty();
227 Depth = NewDepth;
228 isDepthCurrent = true;
229}
230
231/// setHeightToAtLeast - Update this node's predecessors to reflect the
232/// fact that this node's height just increased.
233///
David Goodwin557bbe62009-11-20 19:32:48 +0000234void SUnit::setHeightToAtLeast(unsigned NewHeight) {
235 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000236 return;
237 setHeightDirty();
238 Height = NewHeight;
239 isHeightCurrent = true;
240}
241
242/// ComputeDepth - Calculate the maximal path from the node to the exit.
243///
David Goodwin557bbe62009-11-20 19:32:48 +0000244void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000245 SmallVector<SUnit*, 8> WorkList;
246 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000247 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000248 SUnit *Cur = WorkList.back();
249
250 bool Done = true;
251 unsigned MaxPredDepth = 0;
252 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
253 E = Cur->Preds.end(); I != E; ++I) {
254 SUnit *PredSU = I->getSUnit();
255 if (PredSU->isDepthCurrent)
256 MaxPredDepth = std::max(MaxPredDepth,
257 PredSU->Depth + I->getLatency());
258 else {
259 Done = false;
260 WorkList.push_back(PredSU);
261 }
262 }
263
264 if (Done) {
265 WorkList.pop_back();
266 if (MaxPredDepth != Cur->Depth) {
267 Cur->setDepthDirty();
268 Cur->Depth = MaxPredDepth;
269 }
270 Cur->isDepthCurrent = true;
271 }
Dan Gohman1578f842008-12-23 17:22:32 +0000272 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000273}
274
275/// ComputeHeight - Calculate the maximal path from the node to the entry.
276///
David Goodwin557bbe62009-11-20 19:32:48 +0000277void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000278 SmallVector<SUnit*, 8> WorkList;
279 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000280 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000281 SUnit *Cur = WorkList.back();
282
283 bool Done = true;
284 unsigned MaxSuccHeight = 0;
285 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
286 E = Cur->Succs.end(); I != E; ++I) {
287 SUnit *SuccSU = I->getSUnit();
288 if (SuccSU->isHeightCurrent)
289 MaxSuccHeight = std::max(MaxSuccHeight,
290 SuccSU->Height + I->getLatency());
291 else {
292 Done = false;
293 WorkList.push_back(SuccSU);
294 }
295 }
296
297 if (Done) {
298 WorkList.pop_back();
299 if (MaxSuccHeight != Cur->Height) {
300 Cur->setHeightDirty();
301 Cur->Height = MaxSuccHeight;
302 }
303 Cur->isHeightCurrent = true;
304 }
Dan Gohman1578f842008-12-23 17:22:32 +0000305 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000306}
307
Manman Renb720be62012-09-11 22:23:19 +0000308#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman343f0c02008-11-19 23:18:57 +0000309/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
310/// a group of nodes flagged together.
311void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000312 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000313 G->dumpNode(this);
314}
315
316void SUnit::dumpAll(const ScheduleDAG *G) const {
317 dump(G);
318
David Greene4b134d12010-01-05 01:25:41 +0000319 dbgs() << " # preds left : " << NumPredsLeft << "\n";
320 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickae692f22012-11-12 19:28:57 +0000321 if (WeakPredsLeft)
322 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
323 if (WeakSuccsLeft)
324 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000325 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000326 dbgs() << " Latency : " << Latency << "\n";
327 dbgs() << " Depth : " << Depth << "\n";
328 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000329
330 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000331 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000332 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
333 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000334 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000335 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000336 case SDep::Data: dbgs() << "val "; break;
337 case SDep::Anti: dbgs() << "anti"; break;
338 case SDep::Output: dbgs() << "out "; break;
339 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000340 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000341 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000342 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000343 dbgs() << " *";
344 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000345 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000346 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000347 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000348 }
349 }
350 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000351 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000352 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
353 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000354 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000355 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000356 case SDep::Data: dbgs() << "val "; break;
357 case SDep::Anti: dbgs() << "anti"; break;
358 case SDep::Output: dbgs() << "out "; break;
359 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000360 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000361 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000362 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000363 dbgs() << " *";
364 dbgs() << ": Latency=" << I->getLatency();
365 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000366 }
367 }
David Greene4b134d12010-01-05 01:25:41 +0000368 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000369}
Manman Ren77e300e2012-09-06 19:06:06 +0000370#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000371
372#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000373/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
374/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000375///
Andrew Trick4c727202012-03-07 05:21:36 +0000376unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000377 bool AnyNotSched = false;
378 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000379 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
380 if (!SUnits[i].isScheduled) {
381 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
382 ++DeadNodes;
383 continue;
384 }
385 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000386 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000387 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000388 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000389 AnyNotSched = true;
390 }
Dan Gohman3f237442008-12-16 03:25:46 +0000391 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000392 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000393 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000394 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000395 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000396 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000397 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000398 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000399 AnyNotSched = true;
400 }
401 if (isBottomUp) {
402 if (SUnits[i].NumSuccsLeft != 0) {
403 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000404 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000405 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000406 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000407 AnyNotSched = true;
408 }
409 } else {
410 if (SUnits[i].NumPredsLeft != 0) {
411 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000412 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000413 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000414 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000415 AnyNotSched = true;
416 }
417 }
418 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000419 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000420 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000421}
422#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000423
John Mosby9f71f802010-06-30 03:40:54 +0000424/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000425/// ordering from the DAG to be scheduled.
426///
John Mosby9f71f802010-06-30 03:40:54 +0000427/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000428/// "Online algorithms for managing the topological order of
429/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000430/// This is the MNR algorithm, which was first introduced by
431/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000432/// "Maintaining a topological order under edge insertions".
433///
John Mosby9f71f802010-06-30 03:40:54 +0000434/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000435///
436/// Topological ordering, ord, of a DAG maps each node to a topological
437/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
438///
John Mosby9f71f802010-06-30 03:40:54 +0000439/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000440/// then ord(X) < ord(Z).
441///
442/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000443/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000444/// create a cycle.
445///
446/// The algorithm first computes a topological ordering for the DAG by
447/// initializing the Index2Node and Node2Index arrays and then tries to keep
448/// the ordering up-to-date after edge insertions by reordering the DAG.
449///
450/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
451/// the nodes reachable from Y, and then shifts them using Shift to lie
452/// immediately after X in Index2Node.
453void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
454 unsigned DAGSize = SUnits.size();
455 std::vector<SUnit*> WorkList;
456 WorkList.reserve(DAGSize);
457
458 Index2Node.resize(DAGSize);
459 Node2Index.resize(DAGSize);
460
461 // Initialize the data structures.
Andrew Trickae692f22012-11-12 19:28:57 +0000462 if (ExitSU)
463 WorkList.push_back(ExitSU);
Dan Gohman21d90032008-11-25 00:52:40 +0000464 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
465 SUnit *SU = &SUnits[i];
466 int NodeNum = SU->NodeNum;
467 unsigned Degree = SU->Succs.size();
468 // Temporarily use the Node2Index array as scratch space for degree counts.
469 Node2Index[NodeNum] = Degree;
470
471 // Is it a node without dependencies?
472 if (Degree == 0) {
473 assert(SU->Succs.empty() && "SUnit should have no successors");
474 // Collect leaf nodes.
475 WorkList.push_back(SU);
476 }
John Mosby9f71f802010-06-30 03:40:54 +0000477 }
Dan Gohman21d90032008-11-25 00:52:40 +0000478
479 int Id = DAGSize;
480 while (!WorkList.empty()) {
481 SUnit *SU = WorkList.back();
482 WorkList.pop_back();
Andrew Trickae692f22012-11-12 19:28:57 +0000483 if (SU->NodeNum < DAGSize)
484 Allocate(SU->NodeNum, --Id);
Dan Gohman21d90032008-11-25 00:52:40 +0000485 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
486 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000487 SUnit *SU = I->getSUnit();
Andrew Trickae692f22012-11-12 19:28:57 +0000488 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohman21d90032008-11-25 00:52:40 +0000489 // If all dependencies of the node are processed already,
490 // then the node can be computed now.
491 WorkList.push_back(SU);
492 }
493 }
494
495 Visited.resize(DAGSize);
496
497#ifndef NDEBUG
498 // Check correctness of the ordering
499 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
500 SUnit *SU = &SUnits[i];
501 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
502 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000503 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000504 "Wrong topological sorting");
505 }
506 }
507#endif
508}
509
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000510/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000511/// to be added from SUnit X to SUnit Y.
512void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
513 int UpperBound, LowerBound;
514 LowerBound = Node2Index[Y->NodeNum];
515 UpperBound = Node2Index[X->NodeNum];
516 bool HasLoop = false;
517 // Is Ord(X) < Ord(Y) ?
518 if (LowerBound < UpperBound) {
519 // Update the topological order.
520 Visited.reset();
521 DFS(Y, UpperBound, HasLoop);
522 assert(!HasLoop && "Inserted edge creates a loop!");
523 // Recompute topological indexes.
524 Shift(Visited, LowerBound, UpperBound);
525 }
526}
527
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000528/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000529/// an edge to be removed from the specified node N from the predecessors
530/// of the current node M.
531void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
532 // InitDAGTopologicalSorting();
533}
534
535/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
536/// all nodes affected by the edge insertion. These nodes will later get new
537/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000538void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000539 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000540 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000541 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000542
543 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000544 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000545 SU = WorkList.back();
546 WorkList.pop_back();
547 Visited.set(SU->NodeNum);
548 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Andrew Trickae692f22012-11-12 19:28:57 +0000549 unsigned s = SU->Succs[I].getSUnit()->NodeNum;
550 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
551 if (s >= Node2Index.size())
552 continue;
Dan Gohman21d90032008-11-25 00:52:40 +0000553 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000554 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000555 return;
556 }
557 // Visit successors if not already and in affected region.
558 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000559 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000560 }
561 }
Dan Gohman1578f842008-12-23 17:22:32 +0000562 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000563}
564
John Mosby9f71f802010-06-30 03:40:54 +0000565/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000566/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000567void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000568 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000569 std::vector<int> L;
570 int shift = 0;
571 int i;
572
573 for (i = LowerBound; i <= UpperBound; ++i) {
574 // w is node at topological index i.
575 int w = Index2Node[i];
576 if (Visited.test(w)) {
577 // Unmark.
578 Visited.reset(w);
579 L.push_back(w);
580 shift = shift + 1;
581 } else {
582 Allocate(w, i - shift);
583 }
584 }
585
586 for (unsigned j = 0; j < L.size(); ++j) {
587 Allocate(L[j], i - shift);
588 i = i + 1;
589 }
590}
591
592
Andrew Trickae692f22012-11-12 19:28:57 +0000593/// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
594/// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
595bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
596 // Is SU reachable from TargetSU via successor edges?
597 if (IsReachable(SU, TargetSU))
Dan Gohman21d90032008-11-25 00:52:40 +0000598 return true;
Andrew Trickae692f22012-11-12 19:28:57 +0000599 for (SUnit::pred_iterator
600 I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000601 if (I->isAssignedRegDep() &&
Andrew Trickae692f22012-11-12 19:28:57 +0000602 IsReachable(SU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000603 return true;
604 return false;
605}
606
607/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000608bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
609 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000610 // If insertion of the edge SU->TargetSU would create a cycle
611 // then there is a path from TargetSU to SU.
612 int UpperBound, LowerBound;
613 LowerBound = Node2Index[TargetSU->NodeNum];
614 UpperBound = Node2Index[SU->NodeNum];
615 bool HasLoop = false;
616 // Is Ord(TargetSU) < Ord(SU) ?
617 if (LowerBound < UpperBound) {
618 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000619 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000620 DFS(TargetSU, UpperBound, HasLoop);
621 }
622 return HasLoop;
623}
624
625/// Allocate - assign the topological index to the node n.
626void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
627 Node2Index[n] = index;
628 Index2Node[index] = n;
629}
630
John Mosby9f71f802010-06-30 03:40:54 +0000631ScheduleDAGTopologicalSort::
Andrew Trickae692f22012-11-12 19:28:57 +0000632ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
633 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000634
635ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}