blob: 8fd64265fda6bba82a540881593f2a38a179387a [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.
Dan Gohman5cffa6f2009-02-11 00:12:28 +000067 for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end();
68 I != E; ++I)
69 if (*I == D)
Andrew Trick92e94662011-02-04 03:18:17 +000070 return false;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000071 // Now add a corresponding succ to N.
72 SDep P = D;
73 P.setSUnit(this);
74 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000075 // Update the bookkeeping.
76 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +000077 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
78 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000079 ++NumPreds;
80 ++N->NumSuccs;
81 }
Reid Klecknerc277ab02009-09-30 20:15:38 +000082 if (!N->isScheduled) {
83 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000084 ++NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +000085 }
86 if (!isScheduled) {
87 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +000088 ++N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +000089 }
Dan Gohman3f237442008-12-16 03:25:46 +000090 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +000091 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +000092 if (P.getLatency() != 0) {
93 this->setDepthDirty();
94 N->setHeightDirty();
95 }
Andrew Trick92e94662011-02-04 03:18:17 +000096 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000097}
98
99/// removePred - This removes the specified edge as a pred of the current
100/// node if it exists. It also removes the current node as a successor of
101/// the specified node.
102void SUnit::removePred(const SDep &D) {
103 // Find the matching predecessor.
104 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
105 I != E; ++I)
106 if (*I == D) {
107 bool FoundSucc = false;
108 // Find the corresponding successor in N.
109 SDep P = D;
110 P.setSUnit(this);
111 SUnit *N = D.getSUnit();
112 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
113 EE = N->Succs.end(); II != EE; ++II)
114 if (*II == P) {
115 FoundSucc = true;
116 N->Succs.erase(II);
117 break;
118 }
119 assert(FoundSucc && "Mismatching preds / succs lists!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000120 (void)FoundSucc;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000121 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000122 // Update the bookkeeping.
123 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000124 assert(NumPreds > 0 && "NumPreds will underflow!");
125 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000126 --NumPreds;
127 --N->NumSuccs;
128 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000129 if (!N->isScheduled) {
130 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000131 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000132 }
133 if (!isScheduled) {
134 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000135 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000136 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000137 if (P.getLatency() != 0) {
138 this->setDepthDirty();
139 N->setHeightDirty();
140 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000141 return;
142 }
143}
144
Dan Gohman3f237442008-12-16 03:25:46 +0000145void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000146 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000147 SmallVector<SUnit*, 8> WorkList;
148 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000149 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000150 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000151 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000152 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000153 E = SU->Succs.end(); I != E; ++I) {
154 SUnit *SuccSU = I->getSUnit();
155 if (SuccSU->isDepthCurrent)
156 WorkList.push_back(SuccSU);
157 }
158 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000159}
160
161void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000162 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000163 SmallVector<SUnit*, 8> WorkList;
164 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000165 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000166 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000167 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000168 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000169 E = SU->Preds.end(); I != E; ++I) {
170 SUnit *PredSU = I->getSUnit();
171 if (PredSU->isHeightCurrent)
172 WorkList.push_back(PredSU);
173 }
174 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000175}
176
177/// setDepthToAtLeast - Update this node's successors to reflect the
178/// fact that this node's depth just increased.
179///
David Goodwin557bbe62009-11-20 19:32:48 +0000180void SUnit::setDepthToAtLeast(unsigned NewDepth) {
181 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000182 return;
183 setDepthDirty();
184 Depth = NewDepth;
185 isDepthCurrent = true;
186}
187
188/// setHeightToAtLeast - Update this node's predecessors to reflect the
189/// fact that this node's height just increased.
190///
David Goodwin557bbe62009-11-20 19:32:48 +0000191void SUnit::setHeightToAtLeast(unsigned NewHeight) {
192 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000193 return;
194 setHeightDirty();
195 Height = NewHeight;
196 isHeightCurrent = true;
197}
198
199/// ComputeDepth - Calculate the maximal path from the node to the exit.
200///
David Goodwin557bbe62009-11-20 19:32:48 +0000201void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000202 SmallVector<SUnit*, 8> WorkList;
203 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000204 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000205 SUnit *Cur = WorkList.back();
206
207 bool Done = true;
208 unsigned MaxPredDepth = 0;
209 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
210 E = Cur->Preds.end(); I != E; ++I) {
211 SUnit *PredSU = I->getSUnit();
212 if (PredSU->isDepthCurrent)
213 MaxPredDepth = std::max(MaxPredDepth,
214 PredSU->Depth + I->getLatency());
215 else {
216 Done = false;
217 WorkList.push_back(PredSU);
218 }
219 }
220
221 if (Done) {
222 WorkList.pop_back();
223 if (MaxPredDepth != Cur->Depth) {
224 Cur->setDepthDirty();
225 Cur->Depth = MaxPredDepth;
226 }
227 Cur->isDepthCurrent = true;
228 }
Dan Gohman1578f842008-12-23 17:22:32 +0000229 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000230}
231
232/// ComputeHeight - Calculate the maximal path from the node to the entry.
233///
David Goodwin557bbe62009-11-20 19:32:48 +0000234void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000235 SmallVector<SUnit*, 8> WorkList;
236 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000237 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000238 SUnit *Cur = WorkList.back();
239
240 bool Done = true;
241 unsigned MaxSuccHeight = 0;
242 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
243 E = Cur->Succs.end(); I != E; ++I) {
244 SUnit *SuccSU = I->getSUnit();
245 if (SuccSU->isHeightCurrent)
246 MaxSuccHeight = std::max(MaxSuccHeight,
247 SuccSU->Height + I->getLatency());
248 else {
249 Done = false;
250 WorkList.push_back(SuccSU);
251 }
252 }
253
254 if (Done) {
255 WorkList.pop_back();
256 if (MaxSuccHeight != Cur->Height) {
257 Cur->setHeightDirty();
258 Cur->Height = MaxSuccHeight;
259 }
260 Cur->isHeightCurrent = true;
261 }
Dan Gohman1578f842008-12-23 17:22:32 +0000262 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000263}
264
Dan Gohman343f0c02008-11-19 23:18:57 +0000265/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
266/// a group of nodes flagged together.
267void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000268 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000269 G->dumpNode(this);
270}
271
272void SUnit::dumpAll(const ScheduleDAG *G) const {
273 dump(G);
274
David Greene4b134d12010-01-05 01:25:41 +0000275 dbgs() << " # preds left : " << NumPredsLeft << "\n";
276 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000277 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000278 dbgs() << " Latency : " << Latency << "\n";
279 dbgs() << " Depth : " << Depth << "\n";
280 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000281
282 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000283 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000284 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
285 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000286 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000287 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000288 case SDep::Data: dbgs() << "val "; break;
289 case SDep::Anti: dbgs() << "anti"; break;
290 case SDep::Output: dbgs() << "out "; break;
291 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000292 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000293 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000294 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000295 dbgs() << " *";
296 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000297 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000298 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000299 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000300 }
301 }
302 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000303 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000304 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
305 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000306 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000307 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000308 case SDep::Data: dbgs() << "val "; break;
309 case SDep::Anti: dbgs() << "anti"; break;
310 case SDep::Output: dbgs() << "out "; break;
311 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000312 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000313 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000314 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000315 dbgs() << " *";
316 dbgs() << ": Latency=" << I->getLatency();
317 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000318 }
319 }
David Greene4b134d12010-01-05 01:25:41 +0000320 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000321}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000322
323#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000324/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
325/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000326///
Andrew Trick4c727202012-03-07 05:21:36 +0000327unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000328 bool AnyNotSched = false;
329 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000330 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
331 if (!SUnits[i].isScheduled) {
332 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
333 ++DeadNodes;
334 continue;
335 }
336 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000337 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000338 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000339 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000340 AnyNotSched = true;
341 }
Dan Gohman3f237442008-12-16 03:25:46 +0000342 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000343 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000344 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000345 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000346 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000347 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000348 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000349 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000350 AnyNotSched = true;
351 }
352 if (isBottomUp) {
353 if (SUnits[i].NumSuccsLeft != 0) {
354 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000355 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000356 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000357 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000358 AnyNotSched = true;
359 }
360 } else {
361 if (SUnits[i].NumPredsLeft != 0) {
362 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000363 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000364 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000365 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000366 AnyNotSched = true;
367 }
368 }
369 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000370 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000371 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000372}
373#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000374
John Mosby9f71f802010-06-30 03:40:54 +0000375/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000376/// ordering from the DAG to be scheduled.
377///
John Mosby9f71f802010-06-30 03:40:54 +0000378/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000379/// "Online algorithms for managing the topological order of
380/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000381/// This is the MNR algorithm, which was first introduced by
382/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000383/// "Maintaining a topological order under edge insertions".
384///
John Mosby9f71f802010-06-30 03:40:54 +0000385/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000386///
387/// Topological ordering, ord, of a DAG maps each node to a topological
388/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
389///
John Mosby9f71f802010-06-30 03:40:54 +0000390/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000391/// then ord(X) < ord(Z).
392///
393/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000394/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000395/// create a cycle.
396///
397/// The algorithm first computes a topological ordering for the DAG by
398/// initializing the Index2Node and Node2Index arrays and then tries to keep
399/// the ordering up-to-date after edge insertions by reordering the DAG.
400///
401/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
402/// the nodes reachable from Y, and then shifts them using Shift to lie
403/// immediately after X in Index2Node.
404void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
405 unsigned DAGSize = SUnits.size();
406 std::vector<SUnit*> WorkList;
407 WorkList.reserve(DAGSize);
408
409 Index2Node.resize(DAGSize);
410 Node2Index.resize(DAGSize);
411
412 // Initialize the data structures.
413 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
414 SUnit *SU = &SUnits[i];
415 int NodeNum = SU->NodeNum;
416 unsigned Degree = SU->Succs.size();
417 // Temporarily use the Node2Index array as scratch space for degree counts.
418 Node2Index[NodeNum] = Degree;
419
420 // Is it a node without dependencies?
421 if (Degree == 0) {
422 assert(SU->Succs.empty() && "SUnit should have no successors");
423 // Collect leaf nodes.
424 WorkList.push_back(SU);
425 }
John Mosby9f71f802010-06-30 03:40:54 +0000426 }
Dan Gohman21d90032008-11-25 00:52:40 +0000427
428 int Id = DAGSize;
429 while (!WorkList.empty()) {
430 SUnit *SU = WorkList.back();
431 WorkList.pop_back();
432 Allocate(SU->NodeNum, --Id);
433 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
434 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000435 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000436 if (!--Node2Index[SU->NodeNum])
437 // If all dependencies of the node are processed already,
438 // then the node can be computed now.
439 WorkList.push_back(SU);
440 }
441 }
442
443 Visited.resize(DAGSize);
444
445#ifndef NDEBUG
446 // Check correctness of the ordering
447 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
448 SUnit *SU = &SUnits[i];
449 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
450 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000451 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000452 "Wrong topological sorting");
453 }
454 }
455#endif
456}
457
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000458/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000459/// to be added from SUnit X to SUnit Y.
460void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
461 int UpperBound, LowerBound;
462 LowerBound = Node2Index[Y->NodeNum];
463 UpperBound = Node2Index[X->NodeNum];
464 bool HasLoop = false;
465 // Is Ord(X) < Ord(Y) ?
466 if (LowerBound < UpperBound) {
467 // Update the topological order.
468 Visited.reset();
469 DFS(Y, UpperBound, HasLoop);
470 assert(!HasLoop && "Inserted edge creates a loop!");
471 // Recompute topological indexes.
472 Shift(Visited, LowerBound, UpperBound);
473 }
474}
475
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000476/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000477/// an edge to be removed from the specified node N from the predecessors
478/// of the current node M.
479void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
480 // InitDAGTopologicalSorting();
481}
482
483/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
484/// all nodes affected by the edge insertion. These nodes will later get new
485/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000486void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000487 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000488 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000489 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000490
491 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000492 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000493 SU = WorkList.back();
494 WorkList.pop_back();
495 Visited.set(SU->NodeNum);
496 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000497 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000498 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000499 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000500 return;
501 }
502 // Visit successors if not already and in affected region.
503 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000504 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000505 }
506 }
Dan Gohman1578f842008-12-23 17:22:32 +0000507 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000508}
509
John Mosby9f71f802010-06-30 03:40:54 +0000510/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000511/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000512void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000513 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000514 std::vector<int> L;
515 int shift = 0;
516 int i;
517
518 for (i = LowerBound; i <= UpperBound; ++i) {
519 // w is node at topological index i.
520 int w = Index2Node[i];
521 if (Visited.test(w)) {
522 // Unmark.
523 Visited.reset(w);
524 L.push_back(w);
525 shift = shift + 1;
526 } else {
527 Allocate(w, i - shift);
528 }
529 }
530
531 for (unsigned j = 0; j < L.size(); ++j) {
532 Allocate(L[j], i - shift);
533 i = i + 1;
534 }
535}
536
537
538/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
539/// create a cycle.
540bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
541 if (IsReachable(TargetSU, SU))
542 return true;
543 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
544 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000545 if (I->isAssignedRegDep() &&
546 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000547 return true;
548 return false;
549}
550
551/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000552bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
553 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000554 // If insertion of the edge SU->TargetSU would create a cycle
555 // then there is a path from TargetSU to SU.
556 int UpperBound, LowerBound;
557 LowerBound = Node2Index[TargetSU->NodeNum];
558 UpperBound = Node2Index[SU->NodeNum];
559 bool HasLoop = false;
560 // Is Ord(TargetSU) < Ord(SU) ?
561 if (LowerBound < UpperBound) {
562 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000563 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000564 DFS(TargetSU, UpperBound, HasLoop);
565 }
566 return HasLoop;
567}
568
569/// Allocate - assign the topological index to the node n.
570void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
571 Node2Index[n] = index;
572 Index2Node[index] = n;
573}
574
John Mosby9f71f802010-06-30 03:40:54 +0000575ScheduleDAGTopologicalSort::
576ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000577
578ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}