blob: 9a65071001709970097d3f04f8077bb38f59f3dd [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 Trick92e94662011-02-04 03:18:17 +000065bool SUnit::addPred(const SDep &D) {
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) {
69 if (I->overlaps(D)) {
70 // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
71 if (I->getLatency() < D.getLatency()) {
72 SUnit *PredSU = I->getSUnit();
73 // Find the corresponding successor in N.
74 SDep ForwardD = *I;
75 ForwardD.setSUnit(this);
76 for (SmallVector<SDep, 4>::iterator II = PredSU->Succs.begin(),
77 EE = PredSU->Succs.end(); II != EE; ++II) {
78 if (*II == ForwardD) {
79 II->setLatency(D.getLatency());
80 break;
81 }
82 }
83 I->setLatency(D.getLatency());
84 }
Andrew Trick92e94662011-02-04 03:18:17 +000085 return false;
Andrew Trick9df55ee2012-06-13 02:39:00 +000086 }
87 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +000088 // Now add a corresponding succ to N.
89 SDep P = D;
90 P.setSUnit(this);
91 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000092 // Update the bookkeeping.
93 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +000094 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
95 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000096 ++NumPreds;
97 ++N->NumSuccs;
98 }
Reid Klecknerc277ab02009-09-30 20:15:38 +000099 if (!N->isScheduled) {
100 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000101 ++NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000102 }
103 if (!isScheduled) {
104 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000105 ++N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000106 }
Dan Gohman3f237442008-12-16 03:25:46 +0000107 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000108 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000109 if (P.getLatency() != 0) {
110 this->setDepthDirty();
111 N->setHeightDirty();
112 }
Andrew Trick92e94662011-02-04 03:18:17 +0000113 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000114}
115
116/// removePred - This removes the specified edge as a pred of the current
117/// node if it exists. It also removes the current node as a successor of
118/// the specified node.
119void SUnit::removePred(const SDep &D) {
120 // Find the matching predecessor.
121 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
122 I != E; ++I)
123 if (*I == D) {
124 bool FoundSucc = false;
125 // Find the corresponding successor in N.
126 SDep P = D;
127 P.setSUnit(this);
128 SUnit *N = D.getSUnit();
129 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
130 EE = N->Succs.end(); II != EE; ++II)
131 if (*II == P) {
132 FoundSucc = true;
133 N->Succs.erase(II);
134 break;
135 }
136 assert(FoundSucc && "Mismatching preds / succs lists!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000137 (void)FoundSucc;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000138 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000139 // Update the bookkeeping.
140 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000141 assert(NumPreds > 0 && "NumPreds will underflow!");
142 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000143 --NumPreds;
144 --N->NumSuccs;
145 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000146 if (!N->isScheduled) {
147 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000148 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000149 }
150 if (!isScheduled) {
151 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000152 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000153 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000154 if (P.getLatency() != 0) {
155 this->setDepthDirty();
156 N->setHeightDirty();
157 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000158 return;
159 }
160}
161
Dan Gohman3f237442008-12-16 03:25:46 +0000162void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000163 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000164 SmallVector<SUnit*, 8> WorkList;
165 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000166 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000167 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000168 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000169 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000170 E = SU->Succs.end(); I != E; ++I) {
171 SUnit *SuccSU = I->getSUnit();
172 if (SuccSU->isDepthCurrent)
173 WorkList.push_back(SuccSU);
174 }
175 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000176}
177
178void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000179 if (!isHeightCurrent) 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->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000185 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000186 E = SU->Preds.end(); I != E; ++I) {
187 SUnit *PredSU = I->getSUnit();
188 if (PredSU->isHeightCurrent)
189 WorkList.push_back(PredSU);
190 }
191 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000192}
193
194/// setDepthToAtLeast - Update this node's successors to reflect the
195/// fact that this node's depth just increased.
196///
David Goodwin557bbe62009-11-20 19:32:48 +0000197void SUnit::setDepthToAtLeast(unsigned NewDepth) {
198 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000199 return;
200 setDepthDirty();
201 Depth = NewDepth;
202 isDepthCurrent = true;
203}
204
205/// setHeightToAtLeast - Update this node's predecessors to reflect the
206/// fact that this node's height just increased.
207///
David Goodwin557bbe62009-11-20 19:32:48 +0000208void SUnit::setHeightToAtLeast(unsigned NewHeight) {
209 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000210 return;
211 setHeightDirty();
212 Height = NewHeight;
213 isHeightCurrent = true;
214}
215
216/// ComputeDepth - Calculate the maximal path from the node to the exit.
217///
David Goodwin557bbe62009-11-20 19:32:48 +0000218void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000219 SmallVector<SUnit*, 8> WorkList;
220 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000221 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000222 SUnit *Cur = WorkList.back();
223
224 bool Done = true;
225 unsigned MaxPredDepth = 0;
226 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
227 E = Cur->Preds.end(); I != E; ++I) {
228 SUnit *PredSU = I->getSUnit();
229 if (PredSU->isDepthCurrent)
230 MaxPredDepth = std::max(MaxPredDepth,
231 PredSU->Depth + I->getLatency());
232 else {
233 Done = false;
234 WorkList.push_back(PredSU);
235 }
236 }
237
238 if (Done) {
239 WorkList.pop_back();
240 if (MaxPredDepth != Cur->Depth) {
241 Cur->setDepthDirty();
242 Cur->Depth = MaxPredDepth;
243 }
244 Cur->isDepthCurrent = true;
245 }
Dan Gohman1578f842008-12-23 17:22:32 +0000246 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000247}
248
249/// ComputeHeight - Calculate the maximal path from the node to the entry.
250///
David Goodwin557bbe62009-11-20 19:32:48 +0000251void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000252 SmallVector<SUnit*, 8> WorkList;
253 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000254 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000255 SUnit *Cur = WorkList.back();
256
257 bool Done = true;
258 unsigned MaxSuccHeight = 0;
259 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
260 E = Cur->Succs.end(); I != E; ++I) {
261 SUnit *SuccSU = I->getSUnit();
262 if (SuccSU->isHeightCurrent)
263 MaxSuccHeight = std::max(MaxSuccHeight,
264 SuccSU->Height + I->getLatency());
265 else {
266 Done = false;
267 WorkList.push_back(SuccSU);
268 }
269 }
270
271 if (Done) {
272 WorkList.pop_back();
273 if (MaxSuccHeight != Cur->Height) {
274 Cur->setHeightDirty();
275 Cur->Height = MaxSuccHeight;
276 }
277 Cur->isHeightCurrent = true;
278 }
Dan Gohman1578f842008-12-23 17:22:32 +0000279 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000280}
281
Manman Renb720be62012-09-11 22:23:19 +0000282#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman343f0c02008-11-19 23:18:57 +0000283/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
284/// a group of nodes flagged together.
285void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000286 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000287 G->dumpNode(this);
288}
289
290void SUnit::dumpAll(const ScheduleDAG *G) const {
291 dump(G);
292
David Greene4b134d12010-01-05 01:25:41 +0000293 dbgs() << " # preds left : " << NumPredsLeft << "\n";
294 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000295 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000296 dbgs() << " Latency : " << Latency << "\n";
297 dbgs() << " Depth : " << Depth << "\n";
298 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000299
300 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000301 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000302 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
303 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000304 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000305 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000306 case SDep::Data: dbgs() << "val "; break;
307 case SDep::Anti: dbgs() << "anti"; break;
308 case SDep::Output: dbgs() << "out "; break;
309 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000310 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000311 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000312 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000313 dbgs() << " *";
314 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000315 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000316 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000317 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000318 }
319 }
320 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000321 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000322 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
323 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000324 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000325 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000326 case SDep::Data: dbgs() << "val "; break;
327 case SDep::Anti: dbgs() << "anti"; break;
328 case SDep::Output: dbgs() << "out "; break;
329 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000330 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000331 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000332 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000333 dbgs() << " *";
334 dbgs() << ": Latency=" << I->getLatency();
335 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000336 }
337 }
David Greene4b134d12010-01-05 01:25:41 +0000338 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000339}
Manman Ren77e300e2012-09-06 19:06:06 +0000340#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000341
342#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000343/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
344/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000345///
Andrew Trick4c727202012-03-07 05:21:36 +0000346unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000347 bool AnyNotSched = false;
348 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000349 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
350 if (!SUnits[i].isScheduled) {
351 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
352 ++DeadNodes;
353 continue;
354 }
355 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000356 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000357 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000358 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000359 AnyNotSched = true;
360 }
Dan Gohman3f237442008-12-16 03:25:46 +0000361 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000362 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000363 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000364 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000365 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000366 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000367 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000368 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000369 AnyNotSched = true;
370 }
371 if (isBottomUp) {
372 if (SUnits[i].NumSuccsLeft != 0) {
373 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000374 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000375 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000376 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000377 AnyNotSched = true;
378 }
379 } else {
380 if (SUnits[i].NumPredsLeft != 0) {
381 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000382 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000383 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000384 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000385 AnyNotSched = true;
386 }
387 }
388 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000389 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000390 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000391}
392#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000393
John Mosby9f71f802010-06-30 03:40:54 +0000394/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000395/// ordering from the DAG to be scheduled.
396///
John Mosby9f71f802010-06-30 03:40:54 +0000397/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000398/// "Online algorithms for managing the topological order of
399/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000400/// This is the MNR algorithm, which was first introduced by
401/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000402/// "Maintaining a topological order under edge insertions".
403///
John Mosby9f71f802010-06-30 03:40:54 +0000404/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000405///
406/// Topological ordering, ord, of a DAG maps each node to a topological
407/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
408///
John Mosby9f71f802010-06-30 03:40:54 +0000409/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000410/// then ord(X) < ord(Z).
411///
412/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000413/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000414/// create a cycle.
415///
416/// The algorithm first computes a topological ordering for the DAG by
417/// initializing the Index2Node and Node2Index arrays and then tries to keep
418/// the ordering up-to-date after edge insertions by reordering the DAG.
419///
420/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
421/// the nodes reachable from Y, and then shifts them using Shift to lie
422/// immediately after X in Index2Node.
423void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
424 unsigned DAGSize = SUnits.size();
425 std::vector<SUnit*> WorkList;
426 WorkList.reserve(DAGSize);
427
428 Index2Node.resize(DAGSize);
429 Node2Index.resize(DAGSize);
430
431 // Initialize the data structures.
432 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
433 SUnit *SU = &SUnits[i];
434 int NodeNum = SU->NodeNum;
435 unsigned Degree = SU->Succs.size();
436 // Temporarily use the Node2Index array as scratch space for degree counts.
437 Node2Index[NodeNum] = Degree;
438
439 // Is it a node without dependencies?
440 if (Degree == 0) {
441 assert(SU->Succs.empty() && "SUnit should have no successors");
442 // Collect leaf nodes.
443 WorkList.push_back(SU);
444 }
John Mosby9f71f802010-06-30 03:40:54 +0000445 }
Dan Gohman21d90032008-11-25 00:52:40 +0000446
447 int Id = DAGSize;
448 while (!WorkList.empty()) {
449 SUnit *SU = WorkList.back();
450 WorkList.pop_back();
451 Allocate(SU->NodeNum, --Id);
452 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
453 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000454 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000455 if (!--Node2Index[SU->NodeNum])
456 // If all dependencies of the node are processed already,
457 // then the node can be computed now.
458 WorkList.push_back(SU);
459 }
460 }
461
462 Visited.resize(DAGSize);
463
464#ifndef NDEBUG
465 // Check correctness of the ordering
466 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
467 SUnit *SU = &SUnits[i];
468 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
469 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000470 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000471 "Wrong topological sorting");
472 }
473 }
474#endif
475}
476
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000477/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000478/// to be added from SUnit X to SUnit Y.
479void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
480 int UpperBound, LowerBound;
481 LowerBound = Node2Index[Y->NodeNum];
482 UpperBound = Node2Index[X->NodeNum];
483 bool HasLoop = false;
484 // Is Ord(X) < Ord(Y) ?
485 if (LowerBound < UpperBound) {
486 // Update the topological order.
487 Visited.reset();
488 DFS(Y, UpperBound, HasLoop);
489 assert(!HasLoop && "Inserted edge creates a loop!");
490 // Recompute topological indexes.
491 Shift(Visited, LowerBound, UpperBound);
492 }
493}
494
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000495/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000496/// an edge to be removed from the specified node N from the predecessors
497/// of the current node M.
498void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
499 // InitDAGTopologicalSorting();
500}
501
502/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
503/// all nodes affected by the edge insertion. These nodes will later get new
504/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000505void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000506 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000507 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000508 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000509
510 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000511 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000512 SU = WorkList.back();
513 WorkList.pop_back();
514 Visited.set(SU->NodeNum);
515 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000516 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000517 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000518 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000519 return;
520 }
521 // Visit successors if not already and in affected region.
522 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000523 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000524 }
525 }
Dan Gohman1578f842008-12-23 17:22:32 +0000526 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000527}
528
John Mosby9f71f802010-06-30 03:40:54 +0000529/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000530/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000531void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000532 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000533 std::vector<int> L;
534 int shift = 0;
535 int i;
536
537 for (i = LowerBound; i <= UpperBound; ++i) {
538 // w is node at topological index i.
539 int w = Index2Node[i];
540 if (Visited.test(w)) {
541 // Unmark.
542 Visited.reset(w);
543 L.push_back(w);
544 shift = shift + 1;
545 } else {
546 Allocate(w, i - shift);
547 }
548 }
549
550 for (unsigned j = 0; j < L.size(); ++j) {
551 Allocate(L[j], i - shift);
552 i = i + 1;
553 }
554}
555
556
557/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
558/// create a cycle.
559bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
560 if (IsReachable(TargetSU, SU))
561 return true;
562 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
563 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000564 if (I->isAssignedRegDep() &&
565 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000566 return true;
567 return false;
568}
569
570/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000571bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
572 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000573 // If insertion of the edge SU->TargetSU would create a cycle
574 // then there is a path from TargetSU to SU.
575 int UpperBound, LowerBound;
576 LowerBound = Node2Index[TargetSU->NodeNum];
577 UpperBound = Node2Index[SU->NodeNum];
578 bool HasLoop = false;
579 // Is Ord(TargetSU) < Ord(SU) ?
580 if (LowerBound < UpperBound) {
581 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000582 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000583 DFS(TargetSU, UpperBound, HasLoop);
584 }
585 return HasLoop;
586}
587
588/// Allocate - assign the topological index to the node n.
589void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
590 Node2Index[n] = index;
591 Index2Node[index] = n;
592}
593
John Mosby9f71f802010-06-30 03:40:54 +0000594ScheduleDAGTopologicalSort::
595ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000596
597ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}