blob: 6a2a080964613415a54e0cc8e96e14d121730904 [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
Dan Gohman343f0c02008-11-19 23:18:57 +000015#include "llvm/CodeGen/ScheduleDAG.h"
Dan Gohmanfc54c552009-01-15 22:18:12 +000016#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Andrew Trick2da8bc82010-12-24 05:03:26 +000017#include "llvm/CodeGen/SelectionDAGNodes.h"
Andrew Trick4cb971c2011-06-15 17:16:12 +000018#include "llvm/Support/CommandLine.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000019#include "llvm/Support/Debug.h"
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman40362062008-11-20 01:41:34 +000024#include <climits>
Dan Gohman343f0c02008-11-19 23:18:57 +000025using namespace llvm;
26
Stephen Hinesdce4a402014-05-29 02:49:00 -070027#define DEBUG_TYPE "pre-RA-sched"
28
Andrew Trick4cb971c2011-06-15 17:16:12 +000029#ifndef NDEBUG
Benjamin Kramera67f14b2011-08-19 01:42:18 +000030static cl::opt<bool> StressSchedOpt(
Andrew Trick4cb971c2011-06-15 17:16:12 +000031 "stress-sched", cl::Hidden, cl::init(false),
32 cl::desc("Stress test instruction scheduling"));
33#endif
34
David Blaikie2d24e2a2011-12-20 02:50:00 +000035void SchedulingPriorityQueue::anchor() { }
36
Dan Gohman79ce2762009-01-15 19:20:50 +000037ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Dan Gohman47ac0f02009-02-11 04:27:20 +000038 : TM(mf.getTarget()),
Dan Gohman79ce2762009-01-15 19:20:50 +000039 TII(TM.getInstrInfo()),
40 TRI(TM.getRegisterInfo()),
Dan Gohman79ce2762009-01-15 19:20:50 +000041 MF(mf), MRI(mf.getRegInfo()),
Dan Gohman9e64bbb2009-02-10 23:27:53 +000042 EntrySU(), ExitSU() {
Andrew Trick4cb971c2011-06-15 17:16:12 +000043#ifndef NDEBUG
44 StressSched = StressSchedOpt;
45#endif
Dan Gohman343f0c02008-11-19 23:18:57 +000046}
47
48ScheduleDAG::~ScheduleDAG() {}
49
Andrew Trick47c14452012-03-07 05:21:52 +000050/// Clear the DAG state (e.g. between scheduling regions).
51void ScheduleDAG::clearDAG() {
52 SUnits.clear();
53 EntrySU = SUnit();
54 ExitSU = SUnit();
55}
56
Andrew Trick2da8bc82010-12-24 05:03:26 +000057/// getInstrDesc helper to handle SDNodes.
Evan Chenge837dea2011-06-28 19:10:37 +000058const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Stephen Hinesdce4a402014-05-29 02:49:00 -070059 if (!Node || !Node->isMachineOpcode()) return nullptr;
Andrew Trick2da8bc82010-12-24 05:03:26 +000060 return &TII->get(Node->getMachineOpcode());
61}
62
Dan Gohmanc6b680e2008-12-16 01:05:52 +000063/// addPred - This adds the specified edge as a pred of the current node if
64/// not already. It also adds the current node as a successor of the
65/// specified node.
Andrew Trickae692f22012-11-12 19:28:57 +000066bool SUnit::addPred(const SDep &D, bool Required) {
Stephen Hines36b56882014-04-23 16:57:46 -070067 // If this node already has this dependence, don't add a redundant one.
Craig Topperf22fd3f2013-07-03 05:11:49 +000068 for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
69 I != E; ++I) {
Andrew Trickae692f22012-11-12 19:28:57 +000070 // Zero-latency weak edges may be added purely for heuristic ordering. Don't
71 // add them if another kind of edge already exists.
72 if (!Required && I->getSUnit() == D.getSUnit())
73 return false;
Andrew Trick9df55ee2012-06-13 02:39:00 +000074 if (I->overlaps(D)) {
75 // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
76 if (I->getLatency() < D.getLatency()) {
77 SUnit *PredSU = I->getSUnit();
78 // Find the corresponding successor in N.
79 SDep ForwardD = *I;
80 ForwardD.setSUnit(this);
Craig Topperf22fd3f2013-07-03 05:11:49 +000081 for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
Andrew Trick9df55ee2012-06-13 02:39:00 +000082 EE = PredSU->Succs.end(); II != EE; ++II) {
83 if (*II == ForwardD) {
84 II->setLatency(D.getLatency());
85 break;
86 }
87 }
88 I->setLatency(D.getLatency());
89 }
Andrew Trick92e94662011-02-04 03:18:17 +000090 return false;
Andrew Trick9df55ee2012-06-13 02:39:00 +000091 }
92 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +000093 // Now add a corresponding succ to N.
94 SDep P = D;
95 P.setSUnit(this);
96 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000097 // Update the bookkeeping.
98 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +000099 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
100 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000101 ++NumPreds;
102 ++N->NumSuccs;
103 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000104 if (!N->isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000105 if (D.isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000106 ++WeakPredsLeft;
107 }
108 else {
109 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
110 ++NumPredsLeft;
111 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000112 }
113 if (!isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000114 if (D.isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000115 ++N->WeakSuccsLeft;
116 }
117 else {
118 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
119 ++N->NumSuccsLeft;
120 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000121 }
Dan Gohman3f237442008-12-16 03:25:46 +0000122 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000123 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000124 if (P.getLatency() != 0) {
125 this->setDepthDirty();
126 N->setHeightDirty();
127 }
Andrew Trick92e94662011-02-04 03:18:17 +0000128 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000129}
130
131/// removePred - This removes the specified edge as a pred of the current
132/// node if it exists. It also removes the current node as a successor of
133/// the specified node.
134void SUnit::removePred(const SDep &D) {
135 // Find the matching predecessor.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000136 for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
137 I != E; ++I)
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000138 if (*I == D) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000139 // Find the corresponding successor in N.
140 SDep P = D;
141 P.setSUnit(this);
142 SUnit *N = D.getSUnit();
Benjamin Kramer81474e92013-02-16 17:06:32 +0000143 SmallVectorImpl<SDep>::iterator Succ = std::find(N->Succs.begin(),
144 N->Succs.end(), P);
145 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
146 N->Succs.erase(Succ);
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000147 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000148 // Update the bookkeeping.
149 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000150 assert(NumPreds > 0 && "NumPreds will underflow!");
151 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000152 --NumPreds;
153 --N->NumSuccs;
154 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000155 if (!N->isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000156 if (D.isWeak())
Andrew Trickae692f22012-11-12 19:28:57 +0000157 --WeakPredsLeft;
158 else {
159 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
160 --NumPredsLeft;
161 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000162 }
163 if (!isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000164 if (D.isWeak())
Andrew Trickae692f22012-11-12 19:28:57 +0000165 --N->WeakSuccsLeft;
166 else {
167 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
168 --N->NumSuccsLeft;
169 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000170 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000171 if (P.getLatency() != 0) {
172 this->setDepthDirty();
173 N->setHeightDirty();
174 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000175 return;
176 }
177}
178
Dan Gohman3f237442008-12-16 03:25:46 +0000179void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000180 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000181 SmallVector<SUnit*, 8> WorkList;
182 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000183 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000184 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000185 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000186 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000187 E = SU->Succs.end(); I != E; ++I) {
188 SUnit *SuccSU = I->getSUnit();
189 if (SuccSU->isDepthCurrent)
190 WorkList.push_back(SuccSU);
191 }
192 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000193}
194
195void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000196 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000197 SmallVector<SUnit*, 8> WorkList;
198 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000199 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000200 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000201 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000202 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000203 E = SU->Preds.end(); I != E; ++I) {
204 SUnit *PredSU = I->getSUnit();
205 if (PredSU->isHeightCurrent)
206 WorkList.push_back(PredSU);
207 }
208 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000209}
210
211/// setDepthToAtLeast - Update this node's successors to reflect the
212/// fact that this node's depth just increased.
213///
David Goodwin557bbe62009-11-20 19:32:48 +0000214void SUnit::setDepthToAtLeast(unsigned NewDepth) {
215 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000216 return;
217 setDepthDirty();
218 Depth = NewDepth;
219 isDepthCurrent = true;
220}
221
222/// setHeightToAtLeast - Update this node's predecessors to reflect the
223/// fact that this node's height just increased.
224///
David Goodwin557bbe62009-11-20 19:32:48 +0000225void SUnit::setHeightToAtLeast(unsigned NewHeight) {
226 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000227 return;
228 setHeightDirty();
229 Height = NewHeight;
230 isHeightCurrent = true;
231}
232
233/// ComputeDepth - Calculate the maximal path from the node to the exit.
234///
David Goodwin557bbe62009-11-20 19:32:48 +0000235void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000236 SmallVector<SUnit*, 8> WorkList;
237 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000238 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000239 SUnit *Cur = WorkList.back();
240
241 bool Done = true;
242 unsigned MaxPredDepth = 0;
243 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
244 E = Cur->Preds.end(); I != E; ++I) {
245 SUnit *PredSU = I->getSUnit();
246 if (PredSU->isDepthCurrent)
247 MaxPredDepth = std::max(MaxPredDepth,
248 PredSU->Depth + I->getLatency());
249 else {
250 Done = false;
251 WorkList.push_back(PredSU);
252 }
253 }
254
255 if (Done) {
256 WorkList.pop_back();
257 if (MaxPredDepth != Cur->Depth) {
258 Cur->setDepthDirty();
259 Cur->Depth = MaxPredDepth;
260 }
261 Cur->isDepthCurrent = true;
262 }
Dan Gohman1578f842008-12-23 17:22:32 +0000263 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000264}
265
266/// ComputeHeight - Calculate the maximal path from the node to the entry.
267///
David Goodwin557bbe62009-11-20 19:32:48 +0000268void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000269 SmallVector<SUnit*, 8> WorkList;
270 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000271 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000272 SUnit *Cur = WorkList.back();
273
274 bool Done = true;
275 unsigned MaxSuccHeight = 0;
276 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
277 E = Cur->Succs.end(); I != E; ++I) {
278 SUnit *SuccSU = I->getSUnit();
279 if (SuccSU->isHeightCurrent)
280 MaxSuccHeight = std::max(MaxSuccHeight,
281 SuccSU->Height + I->getLatency());
282 else {
283 Done = false;
284 WorkList.push_back(SuccSU);
285 }
286 }
287
288 if (Done) {
289 WorkList.pop_back();
290 if (MaxSuccHeight != Cur->Height) {
291 Cur->setHeightDirty();
292 Cur->Height = MaxSuccHeight;
293 }
294 Cur->isHeightCurrent = true;
295 }
Dan Gohman1578f842008-12-23 17:22:32 +0000296 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000297}
298
Andrew Trick66658dd2013-01-24 02:09:55 +0000299void SUnit::biasCriticalPath() {
300 if (NumPreds < 2)
301 return;
302
303 SUnit::pred_iterator BestI = Preds.begin();
304 unsigned MaxDepth = BestI->getSUnit()->getDepth();
Stephen Hines36b56882014-04-23 16:57:46 -0700305 for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
306 ++I) {
Andrew Trick66658dd2013-01-24 02:09:55 +0000307 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
308 BestI = I;
309 }
310 if (BestI != Preds.begin())
311 std::swap(*Preds.begin(), *BestI);
312}
313
Manman Renb720be62012-09-11 22:23:19 +0000314#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman343f0c02008-11-19 23:18:57 +0000315/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
316/// a group of nodes flagged together.
317void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000318 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000319 G->dumpNode(this);
320}
321
322void SUnit::dumpAll(const ScheduleDAG *G) const {
323 dump(G);
324
David Greene4b134d12010-01-05 01:25:41 +0000325 dbgs() << " # preds left : " << NumPredsLeft << "\n";
326 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickae692f22012-11-12 19:28:57 +0000327 if (WeakPredsLeft)
328 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
329 if (WeakSuccsLeft)
330 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000331 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000332 dbgs() << " Latency : " << Latency << "\n";
Andrew Trickbf32b7f2013-03-01 00:19:09 +0000333 dbgs() << " Depth : " << getDepth() << "\n";
334 dbgs() << " Height : " << getHeight() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000335
336 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000337 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000338 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
339 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000340 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000341 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000342 case SDep::Data: dbgs() << "val "; break;
343 case SDep::Anti: dbgs() << "anti"; break;
344 case SDep::Output: dbgs() << "out "; break;
345 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000346 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000347 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000348 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000349 dbgs() << " *";
350 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000351 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000352 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000353 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000354 }
355 }
356 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000357 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000358 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
359 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000360 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000361 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000362 case SDep::Data: dbgs() << "val "; break;
363 case SDep::Anti: dbgs() << "anti"; break;
364 case SDep::Output: dbgs() << "out "; break;
365 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000366 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000367 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000368 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000369 dbgs() << " *";
370 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick838038d2013-03-01 00:19:14 +0000371 if (I->isAssignedRegDep())
372 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000373 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000374 }
375 }
David Greene4b134d12010-01-05 01:25:41 +0000376 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000377}
Manman Ren77e300e2012-09-06 19:06:06 +0000378#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000379
380#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000381/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
382/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000383///
Andrew Trick4c727202012-03-07 05:21:36 +0000384unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000385 bool AnyNotSched = false;
386 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000387 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
388 if (!SUnits[i].isScheduled) {
389 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
390 ++DeadNodes;
391 continue;
392 }
393 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000394 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000395 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000396 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000397 AnyNotSched = true;
398 }
Dan Gohman3f237442008-12-16 03:25:46 +0000399 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000400 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000401 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000402 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000403 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000404 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000405 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000406 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000407 AnyNotSched = true;
408 }
409 if (isBottomUp) {
410 if (SUnits[i].NumSuccsLeft != 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 successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000415 AnyNotSched = true;
416 }
417 } else {
418 if (SUnits[i].NumPredsLeft != 0) {
419 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000420 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000421 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000422 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000423 AnyNotSched = true;
424 }
425 }
426 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000427 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000428 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000429}
430#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000431
John Mosby9f71f802010-06-30 03:40:54 +0000432/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000433/// ordering from the DAG to be scheduled.
434///
John Mosby9f71f802010-06-30 03:40:54 +0000435/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000436/// "Online algorithms for managing the topological order of
437/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000438/// This is the MNR algorithm, which was first introduced by
439/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000440/// "Maintaining a topological order under edge insertions".
441///
John Mosby9f71f802010-06-30 03:40:54 +0000442/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000443///
444/// Topological ordering, ord, of a DAG maps each node to a topological
445/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
446///
John Mosby9f71f802010-06-30 03:40:54 +0000447/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000448/// then ord(X) < ord(Z).
449///
450/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000451/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000452/// create a cycle.
453///
454/// The algorithm first computes a topological ordering for the DAG by
455/// initializing the Index2Node and Node2Index arrays and then tries to keep
456/// the ordering up-to-date after edge insertions by reordering the DAG.
457///
458/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
459/// the nodes reachable from Y, and then shifts them using Shift to lie
460/// immediately after X in Index2Node.
461void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
462 unsigned DAGSize = SUnits.size();
463 std::vector<SUnit*> WorkList;
464 WorkList.reserve(DAGSize);
465
466 Index2Node.resize(DAGSize);
467 Node2Index.resize(DAGSize);
468
469 // Initialize the data structures.
Andrew Trickae692f22012-11-12 19:28:57 +0000470 if (ExitSU)
471 WorkList.push_back(ExitSU);
Dan Gohman21d90032008-11-25 00:52:40 +0000472 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
473 SUnit *SU = &SUnits[i];
474 int NodeNum = SU->NodeNum;
475 unsigned Degree = SU->Succs.size();
476 // Temporarily use the Node2Index array as scratch space for degree counts.
477 Node2Index[NodeNum] = Degree;
478
479 // Is it a node without dependencies?
480 if (Degree == 0) {
481 assert(SU->Succs.empty() && "SUnit should have no successors");
482 // Collect leaf nodes.
483 WorkList.push_back(SU);
484 }
John Mosby9f71f802010-06-30 03:40:54 +0000485 }
Dan Gohman21d90032008-11-25 00:52:40 +0000486
487 int Id = DAGSize;
488 while (!WorkList.empty()) {
489 SUnit *SU = WorkList.back();
490 WorkList.pop_back();
Andrew Trickae692f22012-11-12 19:28:57 +0000491 if (SU->NodeNum < DAGSize)
492 Allocate(SU->NodeNum, --Id);
Dan Gohman21d90032008-11-25 00:52:40 +0000493 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
494 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000495 SUnit *SU = I->getSUnit();
Andrew Trickae692f22012-11-12 19:28:57 +0000496 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohman21d90032008-11-25 00:52:40 +0000497 // If all dependencies of the node are processed already,
498 // then the node can be computed now.
499 WorkList.push_back(SU);
500 }
501 }
502
503 Visited.resize(DAGSize);
504
505#ifndef NDEBUG
506 // Check correctness of the ordering
507 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
508 SUnit *SU = &SUnits[i];
509 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
510 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000511 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000512 "Wrong topological sorting");
513 }
514 }
515#endif
516}
517
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000518/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000519/// to be added from SUnit X to SUnit Y.
520void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
521 int UpperBound, LowerBound;
522 LowerBound = Node2Index[Y->NodeNum];
523 UpperBound = Node2Index[X->NodeNum];
524 bool HasLoop = false;
525 // Is Ord(X) < Ord(Y) ?
526 if (LowerBound < UpperBound) {
527 // Update the topological order.
528 Visited.reset();
529 DFS(Y, UpperBound, HasLoop);
530 assert(!HasLoop && "Inserted edge creates a loop!");
531 // Recompute topological indexes.
532 Shift(Visited, LowerBound, UpperBound);
533 }
534}
535
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000536/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000537/// an edge to be removed from the specified node N from the predecessors
538/// of the current node M.
539void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
540 // InitDAGTopologicalSorting();
541}
542
543/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
544/// all nodes affected by the edge insertion. These nodes will later get new
545/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000546void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000547 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000548 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000549 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000550
551 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000552 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000553 SU = WorkList.back();
554 WorkList.pop_back();
555 Visited.set(SU->NodeNum);
556 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Andrew Trickae692f22012-11-12 19:28:57 +0000557 unsigned s = SU->Succs[I].getSUnit()->NodeNum;
558 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
559 if (s >= Node2Index.size())
560 continue;
Dan Gohman21d90032008-11-25 00:52:40 +0000561 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000562 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000563 return;
564 }
565 // Visit successors if not already and in affected region.
566 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000567 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000568 }
569 }
Dan Gohman1578f842008-12-23 17:22:32 +0000570 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000571}
572
John Mosby9f71f802010-06-30 03:40:54 +0000573/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000574/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000575void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000576 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000577 std::vector<int> L;
578 int shift = 0;
579 int i;
580
581 for (i = LowerBound; i <= UpperBound; ++i) {
582 // w is node at topological index i.
583 int w = Index2Node[i];
584 if (Visited.test(w)) {
585 // Unmark.
586 Visited.reset(w);
587 L.push_back(w);
588 shift = shift + 1;
589 } else {
590 Allocate(w, i - shift);
591 }
592 }
593
594 for (unsigned j = 0; j < L.size(); ++j) {
595 Allocate(L[j], i - shift);
596 i = i + 1;
597 }
598}
599
600
Andrew Trickae692f22012-11-12 19:28:57 +0000601/// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
602/// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
603bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
604 // Is SU reachable from TargetSU via successor edges?
605 if (IsReachable(SU, TargetSU))
Dan Gohman21d90032008-11-25 00:52:40 +0000606 return true;
Andrew Trickae692f22012-11-12 19:28:57 +0000607 for (SUnit::pred_iterator
608 I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000609 if (I->isAssignedRegDep() &&
Andrew Trickae692f22012-11-12 19:28:57 +0000610 IsReachable(SU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000611 return true;
612 return false;
613}
614
615/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000616bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
617 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000618 // If insertion of the edge SU->TargetSU would create a cycle
619 // then there is a path from TargetSU to SU.
620 int UpperBound, LowerBound;
621 LowerBound = Node2Index[TargetSU->NodeNum];
622 UpperBound = Node2Index[SU->NodeNum];
623 bool HasLoop = false;
624 // Is Ord(TargetSU) < Ord(SU) ?
625 if (LowerBound < UpperBound) {
626 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000627 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000628 DFS(TargetSU, UpperBound, HasLoop);
629 }
630 return HasLoop;
631}
632
633/// Allocate - assign the topological index to the node n.
634void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
635 Node2Index[n] = index;
636 Index2Node[index] = n;
637}
638
John Mosby9f71f802010-06-30 03:40:54 +0000639ScheduleDAGTopologicalSort::
Andrew Trickae692f22012-11-12 19:28:57 +0000640ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
641 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000642
643ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}