blob: 1f0c3283ceb115a439474e180c382d33eff50c85 [file] [log] [blame]
Dan Gohman60cb69e2008-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
Dan Gohman60cb69e2008-11-19 23:18:57 +000015#include "llvm/CodeGen/ScheduleDAG.h"
Dan Gohman7e105f02009-01-15 22:18:12 +000016#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Andrew Trick10ffc2b2010-12-24 05:03:26 +000017#include "llvm/CodeGen/SelectionDAGNodes.h"
Andrew Trick3013b6a2011-06-15 17:16:12 +000018#include "llvm/Support/CommandLine.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000019#include "llvm/Support/Debug.h"
Daniel Dunbar8ef07352009-07-24 09:53:24 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000024#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohman866b0342008-11-20 01:41:34 +000025#include <climits>
Dan Gohman60cb69e2008-11-19 23:18:57 +000026using namespace llvm;
27
Chandler Carruth1b9dde02014-04-22 02:02:50 +000028#define DEBUG_TYPE "pre-RA-sched"
29
Andrew Trick3013b6a2011-06-15 17:16:12 +000030#ifndef NDEBUG
Benjamin Kramer4938edb2011-08-19 01:42:18 +000031static cl::opt<bool> StressSchedOpt(
Andrew Trick3013b6a2011-06-15 17:16:12 +000032 "stress-sched", cl::Hidden, cl::init(false),
33 cl::desc("Stress test instruction scheduling"));
34#endif
35
David Blaikiea379b1812011-12-20 02:50:00 +000036void SchedulingPriorityQueue::anchor() { }
37
Dan Gohman619ef482009-01-15 19:20:50 +000038ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Eric Christopher33726202015-01-27 08:48:42 +000039 : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()),
40 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
Eric Christopherd9134482014-08-04 21:25:23 +000041 MRI(mf.getRegInfo()), EntrySU(), ExitSU() {
Andrew Trick3013b6a2011-06-15 17:16:12 +000042#ifndef NDEBUG
43 StressSched = StressSchedOpt;
44#endif
Dan Gohman60cb69e2008-11-19 23:18:57 +000045}
46
47ScheduleDAG::~ScheduleDAG() {}
48
Andrew Trick60cf03e2012-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 Trick10ffc2b2010-12-24 05:03:26 +000056/// getInstrDesc helper to handle SDNodes.
Evan Cheng6cc775f2011-06-28 19:10:37 +000057const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Craig Topperc0196b12014-04-14 00:51:57 +000058 if (!Node || !Node->isMachineOpcode()) return nullptr;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000059 return &TII->get(Node->getMachineOpcode());
60}
61
Dan Gohmane3a63512008-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 Trickf1ff84c2012-11-12 19:28:57 +000065bool SUnit::addPred(const SDep &D, bool Required) {
Alp Tokercb402912014-01-24 17:20:08 +000066 // If this node already has this dependence, don't add a redundant one.
Craig Toppere1c1d362013-07-03 05:11:49 +000067 for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
68 I != E; ++I) {
Andrew Trickf1ff84c2012-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 Trick5b906452012-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 Toppere1c1d362013-07-03 05:11:49 +000080 for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
Andrew Trick5b906452012-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 Trickd0548ae2011-02-04 03:18:17 +000089 return false;
Andrew Trick5b906452012-06-13 02:39:00 +000090 }
91 }
Dan Gohmane3a63512008-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 Gohmane3a63512008-12-16 01:05:52 +000096 // Update the bookkeeping.
97 if (D.getKind() == SDep::Data) {
Reid Kleckner8ff5c192009-09-30 20:15:38 +000098 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
99 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmane3a63512008-12-16 01:05:52 +0000100 ++NumPreds;
101 ++N->NumSuccs;
102 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000103 if (!N->isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000104 if (D.isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000105 ++WeakPredsLeft;
106 }
107 else {
108 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
109 ++NumPredsLeft;
110 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000111 }
112 if (!isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000113 if (D.isWeak()) {
Andrew Trickf1ff84c2012-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 Kleckner8ff5c192009-09-30 20:15:38 +0000120 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000121 Preds.push_back(D);
Dan Gohman13141d52009-01-13 19:08:45 +0000122 N->Succs.push_back(P);
Dan Gohman52d4d822009-01-05 22:40:26 +0000123 if (P.getLatency() != 0) {
124 this->setDepthDirty();
125 N->setHeightDirty();
126 }
Andrew Trickd0548ae2011-02-04 03:18:17 +0000127 return true;
Dan Gohmane3a63512008-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 Toppere1c1d362013-07-03 05:11:49 +0000135 for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
136 I != E; ++I)
Dan Gohmane3a63512008-12-16 01:05:52 +0000137 if (*I == D) {
Dan Gohmane3a63512008-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();
David Majnemer42531262016-08-12 03:55:06 +0000142 SmallVectorImpl<SDep>::iterator Succ = find(N->Succs, P);
Benjamin Kramer981de9a2013-02-16 17:06:32 +0000143 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
144 N->Succs.erase(Succ);
Dan Gohmane3a63512008-12-16 01:05:52 +0000145 Preds.erase(I);
Dan Gohman13141d52009-01-13 19:08:45 +0000146 // Update the bookkeeping.
147 if (P.getKind() == SDep::Data) {
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000148 assert(NumPreds > 0 && "NumPreds will underflow!");
149 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmane3a63512008-12-16 01:05:52 +0000150 --NumPreds;
151 --N->NumSuccs;
152 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000153 if (!N->isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000154 if (D.isWeak())
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000155 --WeakPredsLeft;
156 else {
157 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
158 --NumPredsLeft;
159 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000160 }
161 if (!isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000162 if (D.isWeak())
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000163 --N->WeakSuccsLeft;
164 else {
165 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
166 --N->NumSuccsLeft;
167 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000168 }
Dan Gohman52d4d822009-01-05 22:40:26 +0000169 if (P.getLatency() != 0) {
170 this->setDepthDirty();
171 N->setHeightDirty();
172 }
Dan Gohmane3a63512008-12-16 01:05:52 +0000173 return;
174 }
175}
176
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000177void SUnit::setDepthDirty() {
Dan Gohmana04542b2008-12-22 21:11:33 +0000178 if (!isDepthCurrent) return;
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000179 SmallVector<SUnit*, 8> WorkList;
180 WorkList.push_back(this);
Dan Gohmana04542b2008-12-22 21:11:33 +0000181 do {
Dan Gohman24fe9a12008-12-20 16:42:33 +0000182 SUnit *SU = WorkList.pop_back_val();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000183 SU->isDepthCurrent = false;
Dan Gohmanbb92a1b2008-12-20 16:34:57 +0000184 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohmana04542b2008-12-22 21:11:33 +0000185 E = SU->Succs.end(); I != E; ++I) {
186 SUnit *SuccSU = I->getSUnit();
187 if (SuccSU->isDepthCurrent)
188 WorkList.push_back(SuccSU);
189 }
190 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000191}
192
193void SUnit::setHeightDirty() {
Dan Gohmana04542b2008-12-22 21:11:33 +0000194 if (!isHeightCurrent) return;
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000195 SmallVector<SUnit*, 8> WorkList;
196 WorkList.push_back(this);
Dan Gohmana04542b2008-12-22 21:11:33 +0000197 do {
Dan Gohman24fe9a12008-12-20 16:42:33 +0000198 SUnit *SU = WorkList.pop_back_val();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000199 SU->isHeightCurrent = false;
Dan Gohmanbb92a1b2008-12-20 16:34:57 +0000200 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohmana04542b2008-12-22 21:11:33 +0000201 E = SU->Preds.end(); I != E; ++I) {
202 SUnit *PredSU = I->getSUnit();
203 if (PredSU->isHeightCurrent)
204 WorkList.push_back(PredSU);
205 }
206 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000207}
208
209/// setDepthToAtLeast - Update this node's successors to reflect the
210/// fact that this node's depth just increased.
211///
David Goodwin80a03cc2009-11-20 19:32:48 +0000212void SUnit::setDepthToAtLeast(unsigned NewDepth) {
213 if (NewDepth <= getDepth())
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000214 return;
215 setDepthDirty();
216 Depth = NewDepth;
217 isDepthCurrent = true;
218}
219
220/// setHeightToAtLeast - Update this node's predecessors to reflect the
221/// fact that this node's height just increased.
222///
David Goodwin80a03cc2009-11-20 19:32:48 +0000223void SUnit::setHeightToAtLeast(unsigned NewHeight) {
224 if (NewHeight <= getHeight())
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000225 return;
226 setHeightDirty();
227 Height = NewHeight;
228 isHeightCurrent = true;
229}
230
231/// ComputeDepth - Calculate the maximal path from the node to the exit.
232///
David Goodwin80a03cc2009-11-20 19:32:48 +0000233void SUnit::ComputeDepth() {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000234 SmallVector<SUnit*, 8> WorkList;
235 WorkList.push_back(this);
Dan Gohman3a572132008-12-23 17:22:32 +0000236 do {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000237 SUnit *Cur = WorkList.back();
238
239 bool Done = true;
240 unsigned MaxPredDepth = 0;
241 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
242 E = Cur->Preds.end(); I != E; ++I) {
243 SUnit *PredSU = I->getSUnit();
244 if (PredSU->isDepthCurrent)
245 MaxPredDepth = std::max(MaxPredDepth,
246 PredSU->Depth + I->getLatency());
247 else {
248 Done = false;
249 WorkList.push_back(PredSU);
250 }
251 }
252
253 if (Done) {
254 WorkList.pop_back();
255 if (MaxPredDepth != Cur->Depth) {
256 Cur->setDepthDirty();
257 Cur->Depth = MaxPredDepth;
258 }
259 Cur->isDepthCurrent = true;
260 }
Dan Gohman3a572132008-12-23 17:22:32 +0000261 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000262}
263
264/// ComputeHeight - Calculate the maximal path from the node to the entry.
265///
David Goodwin80a03cc2009-11-20 19:32:48 +0000266void SUnit::ComputeHeight() {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000267 SmallVector<SUnit*, 8> WorkList;
268 WorkList.push_back(this);
Dan Gohman3a572132008-12-23 17:22:32 +0000269 do {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000270 SUnit *Cur = WorkList.back();
271
272 bool Done = true;
273 unsigned MaxSuccHeight = 0;
274 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
275 E = Cur->Succs.end(); I != E; ++I) {
276 SUnit *SuccSU = I->getSUnit();
277 if (SuccSU->isHeightCurrent)
278 MaxSuccHeight = std::max(MaxSuccHeight,
279 SuccSU->Height + I->getLatency());
280 else {
281 Done = false;
282 WorkList.push_back(SuccSU);
283 }
284 }
285
286 if (Done) {
287 WorkList.pop_back();
288 if (MaxSuccHeight != Cur->Height) {
289 Cur->setHeightDirty();
290 Cur->Height = MaxSuccHeight;
291 }
292 Cur->isHeightCurrent = true;
293 }
Dan Gohman3a572132008-12-23 17:22:32 +0000294 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000295}
296
Andrew Trickd3b86292013-01-24 02:09:55 +0000297void SUnit::biasCriticalPath() {
298 if (NumPreds < 2)
299 return;
300
301 SUnit::pred_iterator BestI = Preds.begin();
302 unsigned MaxDepth = BestI->getSUnit()->getDepth();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000303 for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
304 ++I) {
Andrew Trickd3b86292013-01-24 02:09:55 +0000305 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
306 BestI = I;
307 }
308 if (BestI != Preds.begin())
309 std::swap(*Preds.begin(), *BestI);
310}
311
Manman Ren19f49ac2012-09-11 22:23:19 +0000312#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun636a5972016-11-11 22:37:26 +0000313static void dumpSUIdentifier(const ScheduleDAG &DAG, const SUnit &SU) {
314 if (&SU == &DAG.ExitSU)
315 dbgs() << "ExitSU";
316 else if (&SU == &DAG.EntrySU)
317 dbgs() << "EntrySU";
318 else
319 dbgs() << "SU(" << SU.NodeNum << ")";
320}
321
Dan Gohman60cb69e2008-11-19 23:18:57 +0000322/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
323/// a group of nodes flagged together.
324void SUnit::dump(const ScheduleDAG *G) const {
Matthias Braun636a5972016-11-11 22:37:26 +0000325 dumpSUIdentifier(*G, *this);
326 dbgs() << ": ";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000327 G->dumpNode(this);
328}
329
330void SUnit::dumpAll(const ScheduleDAG *G) const {
331 dump(G);
332
David Greene94745d02010-01-05 01:25:41 +0000333 dbgs() << " # preds left : " << NumPredsLeft << "\n";
334 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000335 if (WeakPredsLeft)
336 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
337 if (WeakSuccsLeft)
338 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trickd0548ae2011-02-04 03:18:17 +0000339 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene94745d02010-01-05 01:25:41 +0000340 dbgs() << " Latency : " << Latency << "\n";
Andrew Trick2a8edef2013-03-01 00:19:09 +0000341 dbgs() << " Depth : " << getDepth() << "\n";
342 dbgs() << " Height : " << getHeight() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000343
344 if (Preds.size() != 0) {
David Greene94745d02010-01-05 01:25:41 +0000345 dbgs() << " Predecessors:\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000346 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
347 I != E; ++I) {
David Greene94745d02010-01-05 01:25:41 +0000348 dbgs() << " ";
Dan Gohman2d170892008-12-09 22:54:47 +0000349 switch (I->getKind()) {
Matthias Braun1acb55e2016-09-23 18:28:31 +0000350 case SDep::Data: dbgs() << "data "; break;
351 case SDep::Anti: dbgs() << "anti "; break;
352 case SDep::Output: dbgs() << "out "; break;
353 case SDep::Order: dbgs() << "ord "; break;
Dan Gohman2d170892008-12-09 22:54:47 +0000354 }
Matthias Braun636a5972016-11-11 22:37:26 +0000355 dumpSUIdentifier(*G, *I->getSUnit());
Dan Gohman2d170892008-12-09 22:54:47 +0000356 if (I->isArtificial())
David Greene94745d02010-01-05 01:25:41 +0000357 dbgs() << " *";
358 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick3013b6a2011-06-15 17:16:12 +0000359 if (I->isAssignedRegDep())
Jakob Stoklund Olesena2755ea2012-02-17 21:44:51 +0000360 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene94745d02010-01-05 01:25:41 +0000361 dbgs() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000362 }
363 }
364 if (Succs.size() != 0) {
David Greene94745d02010-01-05 01:25:41 +0000365 dbgs() << " Successors:\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000366 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
367 I != E; ++I) {
David Greene94745d02010-01-05 01:25:41 +0000368 dbgs() << " ";
Dan Gohman2d170892008-12-09 22:54:47 +0000369 switch (I->getKind()) {
Matthias Braun1acb55e2016-09-23 18:28:31 +0000370 case SDep::Data: dbgs() << "data "; break;
371 case SDep::Anti: dbgs() << "anti "; break;
372 case SDep::Output: dbgs() << "out "; break;
373 case SDep::Order: dbgs() << "ord "; break;
Dan Gohman2d170892008-12-09 22:54:47 +0000374 }
Matthias Braun636a5972016-11-11 22:37:26 +0000375 dumpSUIdentifier(*G, *I->getSUnit());
Dan Gohman2d170892008-12-09 22:54:47 +0000376 if (I->isArtificial())
David Greene94745d02010-01-05 01:25:41 +0000377 dbgs() << " *";
378 dbgs() << ": Latency=" << I->getLatency();
Andrew Trickf9669be2013-03-01 00:19:14 +0000379 if (I->isAssignedRegDep())
380 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene94745d02010-01-05 01:25:41 +0000381 dbgs() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000382 }
383 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000384}
Manman Ren742534c2012-09-06 19:06:06 +0000385#endif
Dan Gohman4ce15e12008-11-20 01:26:25 +0000386
387#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000388/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
389/// their state is consistent. Return the number of scheduled nodes.
Dan Gohman4ce15e12008-11-20 01:26:25 +0000390///
Andrew Trick46a58662012-03-07 05:21:36 +0000391unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000392 bool AnyNotSched = false;
393 unsigned DeadNodes = 0;
Dan Gohman4ce15e12008-11-20 01:26:25 +0000394 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
395 if (!SUnits[i].isScheduled) {
396 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
397 ++DeadNodes;
398 continue;
399 }
400 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000401 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000402 SUnits[i].dump(this);
David Greene94745d02010-01-05 01:25:41 +0000403 dbgs() << "has not been scheduled!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000404 AnyNotSched = true;
405 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000406 if (SUnits[i].isScheduled &&
David Goodwin8501dbbe2009-11-03 20:57:50 +0000407 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000408 unsigned(INT_MAX)) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000409 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000410 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000411 SUnits[i].dump(this);
David Greene94745d02010-01-05 01:25:41 +0000412 dbgs() << "has an unexpected "
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000413 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000414 AnyNotSched = true;
415 }
416 if (isBottomUp) {
417 if (SUnits[i].NumSuccsLeft != 0) {
418 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000419 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000420 SUnits[i].dump(this);
David Greene94745d02010-01-05 01:25:41 +0000421 dbgs() << "has successors left!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000422 AnyNotSched = true;
423 }
424 } else {
425 if (SUnits[i].NumPredsLeft != 0) {
426 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000427 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000428 SUnits[i].dump(this);
David Greene94745d02010-01-05 01:25:41 +0000429 dbgs() << "has predecessors left!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000430 AnyNotSched = true;
431 }
432 }
433 }
Dan Gohman4ce15e12008-11-20 01:26:25 +0000434 assert(!AnyNotSched);
Andrew Trick46a58662012-03-07 05:21:36 +0000435 return SUnits.size() - DeadNodes;
Dan Gohman4ce15e12008-11-20 01:26:25 +0000436}
437#endif
Dan Gohmanad2134d2008-11-25 00:52:40 +0000438
John Mosby53646552010-06-30 03:40:54 +0000439/// InitDAGTopologicalSorting - create the initial topological
Dan Gohmanad2134d2008-11-25 00:52:40 +0000440/// ordering from the DAG to be scheduled.
441///
John Mosby53646552010-06-30 03:40:54 +0000442/// The idea of the algorithm is taken from
Dan Gohmanad2134d2008-11-25 00:52:40 +0000443/// "Online algorithms for managing the topological order of
444/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby53646552010-06-30 03:40:54 +0000445/// This is the MNR algorithm, which was first introduced by
446/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohmanad2134d2008-11-25 00:52:40 +0000447/// "Maintaining a topological order under edge insertions".
448///
John Mosby53646552010-06-30 03:40:54 +0000449/// Short description of the algorithm:
Dan Gohmanad2134d2008-11-25 00:52:40 +0000450///
451/// Topological ordering, ord, of a DAG maps each node to a topological
452/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
453///
John Mosby53646552010-06-30 03:40:54 +0000454/// This means that if there is a path from the node X to the node Z,
Dan Gohmanad2134d2008-11-25 00:52:40 +0000455/// then ord(X) < ord(Z).
456///
457/// This property can be used to check for reachability of nodes:
John Mosby53646552010-06-30 03:40:54 +0000458/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohmanad2134d2008-11-25 00:52:40 +0000459/// create a cycle.
460///
461/// The algorithm first computes a topological ordering for the DAG by
462/// initializing the Index2Node and Node2Index arrays and then tries to keep
463/// the ordering up-to-date after edge insertions by reordering the DAG.
464///
465/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
466/// the nodes reachable from Y, and then shifts them using Shift to lie
467/// immediately after X in Index2Node.
468void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
469 unsigned DAGSize = SUnits.size();
470 std::vector<SUnit*> WorkList;
471 WorkList.reserve(DAGSize);
472
473 Index2Node.resize(DAGSize);
474 Node2Index.resize(DAGSize);
475
476 // Initialize the data structures.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000477 if (ExitSU)
478 WorkList.push_back(ExitSU);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000479 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
480 SUnit *SU = &SUnits[i];
481 int NodeNum = SU->NodeNum;
482 unsigned Degree = SU->Succs.size();
483 // Temporarily use the Node2Index array as scratch space for degree counts.
484 Node2Index[NodeNum] = Degree;
485
486 // Is it a node without dependencies?
487 if (Degree == 0) {
488 assert(SU->Succs.empty() && "SUnit should have no successors");
489 // Collect leaf nodes.
490 WorkList.push_back(SU);
491 }
John Mosby53646552010-06-30 03:40:54 +0000492 }
Dan Gohmanad2134d2008-11-25 00:52:40 +0000493
494 int Id = DAGSize;
495 while (!WorkList.empty()) {
496 SUnit *SU = WorkList.back();
497 WorkList.pop_back();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000498 if (SU->NodeNum < DAGSize)
499 Allocate(SU->NodeNum, --Id);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000500 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
501 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000502 SUnit *SU = I->getSUnit();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000503 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohmanad2134d2008-11-25 00:52:40 +0000504 // If all dependencies of the node are processed already,
505 // then the node can be computed now.
506 WorkList.push_back(SU);
507 }
508 }
509
510 Visited.resize(DAGSize);
511
512#ifndef NDEBUG
513 // Check correctness of the ordering
514 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
515 SUnit *SU = &SUnits[i];
516 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
517 I != E; ++I) {
John Mosby53646552010-06-30 03:40:54 +0000518 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohmanad2134d2008-11-25 00:52:40 +0000519 "Wrong topological sorting");
520 }
521 }
522#endif
523}
524
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000525/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohmanad2134d2008-11-25 00:52:40 +0000526/// to be added from SUnit X to SUnit Y.
527void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
528 int UpperBound, LowerBound;
529 LowerBound = Node2Index[Y->NodeNum];
530 UpperBound = Node2Index[X->NodeNum];
531 bool HasLoop = false;
532 // Is Ord(X) < Ord(Y) ?
533 if (LowerBound < UpperBound) {
534 // Update the topological order.
535 Visited.reset();
536 DFS(Y, UpperBound, HasLoop);
537 assert(!HasLoop && "Inserted edge creates a loop!");
538 // Recompute topological indexes.
539 Shift(Visited, LowerBound, UpperBound);
540 }
541}
542
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000543/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohmanad2134d2008-11-25 00:52:40 +0000544/// an edge to be removed from the specified node N from the predecessors
545/// of the current node M.
546void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
547 // InitDAGTopologicalSorting();
548}
549
550/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
551/// all nodes affected by the edge insertion. These nodes will later get new
552/// topological indexes by means of the Shift method.
Dan Gohman7d329742008-12-09 16:37:48 +0000553void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattnered69c6e2010-12-20 00:50:16 +0000554 bool &HasLoop) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000555 std::vector<const SUnit*> WorkList;
John Mosby53646552010-06-30 03:40:54 +0000556 WorkList.reserve(SUnits.size());
Dan Gohmanad2134d2008-11-25 00:52:40 +0000557
558 WorkList.push_back(SU);
Dan Gohman3a572132008-12-23 17:22:32 +0000559 do {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000560 SU = WorkList.back();
561 WorkList.pop_back();
562 Visited.set(SU->NodeNum);
563 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000564 unsigned s = SU->Succs[I].getSUnit()->NodeNum;
565 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
566 if (s >= Node2Index.size())
567 continue;
Dan Gohmanad2134d2008-11-25 00:52:40 +0000568 if (Node2Index[s] == UpperBound) {
John Mosby53646552010-06-30 03:40:54 +0000569 HasLoop = true;
Dan Gohmanad2134d2008-11-25 00:52:40 +0000570 return;
571 }
572 // Visit successors if not already and in affected region.
573 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman2d170892008-12-09 22:54:47 +0000574 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby53646552010-06-30 03:40:54 +0000575 }
576 }
Dan Gohman3a572132008-12-23 17:22:32 +0000577 } while (!WorkList.empty());
Dan Gohmanad2134d2008-11-25 00:52:40 +0000578}
579
John Mosby53646552010-06-30 03:40:54 +0000580/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohmanad2134d2008-11-25 00:52:40 +0000581/// preserved.
John Mosby53646552010-06-30 03:40:54 +0000582void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohman7d329742008-12-09 16:37:48 +0000583 int UpperBound) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000584 std::vector<int> L;
585 int shift = 0;
586 int i;
587
588 for (i = LowerBound; i <= UpperBound; ++i) {
589 // w is node at topological index i.
590 int w = Index2Node[i];
591 if (Visited.test(w)) {
592 // Unmark.
593 Visited.reset(w);
594 L.push_back(w);
595 shift = shift + 1;
596 } else {
597 Allocate(w, i - shift);
598 }
599 }
600
601 for (unsigned j = 0; j < L.size(); ++j) {
602 Allocate(L[j], i - shift);
603 i = i + 1;
604 }
605}
606
607
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000608/// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
609/// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
610bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
611 // Is SU reachable from TargetSU via successor edges?
612 if (IsReachable(SU, TargetSU))
Dan Gohmanad2134d2008-11-25 00:52:40 +0000613 return true;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000614 for (SUnit::pred_iterator
615 I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
Dan Gohman2d170892008-12-09 22:54:47 +0000616 if (I->isAssignedRegDep() &&
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000617 IsReachable(SU, I->getSUnit()))
Dan Gohmanad2134d2008-11-25 00:52:40 +0000618 return true;
619 return false;
620}
621
622/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohman7d329742008-12-09 16:37:48 +0000623bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
624 const SUnit *TargetSU) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000625 // If insertion of the edge SU->TargetSU would create a cycle
626 // then there is a path from TargetSU to SU.
627 int UpperBound, LowerBound;
628 LowerBound = Node2Index[TargetSU->NodeNum];
629 UpperBound = Node2Index[SU->NodeNum];
630 bool HasLoop = false;
631 // Is Ord(TargetSU) < Ord(SU) ?
632 if (LowerBound < UpperBound) {
633 Visited.reset();
John Mosby53646552010-06-30 03:40:54 +0000634 // There may be a path from TargetSU to SU. Check for it.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000635 DFS(TargetSU, UpperBound, HasLoop);
636 }
637 return HasLoop;
638}
639
640/// Allocate - assign the topological index to the node n.
641void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
642 Node2Index[n] = index;
643 Index2Node[index] = n;
644}
645
John Mosby53646552010-06-30 03:40:54 +0000646ScheduleDAGTopologicalSort::
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000647ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
648 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohman7e105f02009-01-15 22:18:12 +0000649
650ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}