blob: de8f5199ebffe2744f3b90d27889cec0016a370e [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 Trick2da8bc82010-12-24 05:03:26 +000049/// getInstrDesc helper to handle SDNodes.
Evan Chenge837dea2011-06-28 19:10:37 +000050const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Andrew Trick24312232010-12-24 06:46:50 +000051 if (!Node || !Node->isMachineOpcode()) return NULL;
Andrew Trick2da8bc82010-12-24 05:03:26 +000052 return &TII->get(Node->getMachineOpcode());
53}
54
Dan Gohman343f0c02008-11-19 23:18:57 +000055/// dump - dump the schedule.
56void ScheduleDAG::dumpSchedule() const {
57 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
58 if (SUnit *SU = Sequence[i])
59 SU->dump(this);
60 else
David Greene4b134d12010-01-05 01:25:41 +000061 dbgs() << "**** NOOP ****\n";
Dan Gohman343f0c02008-11-19 23:18:57 +000062 }
63}
64
65
66/// Run - perform scheduling.
67///
Dan Gohman47ac0f02009-02-11 04:27:20 +000068void ScheduleDAG::Run(MachineBasicBlock *bb,
69 MachineBasicBlock::iterator insertPos) {
70 BB = bb;
71 InsertPos = insertPos;
Dan Gohmanf7119392009-01-16 22:10:20 +000072
Dan Gohman79ce2762009-01-15 19:20:50 +000073 SUnits.clear();
74 Sequence.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +000075 EntrySU = SUnit();
76 ExitSU = SUnit();
Dan Gohman79ce2762009-01-15 19:20:50 +000077
Dan Gohman343f0c02008-11-19 23:18:57 +000078 Schedule();
Dan Gohman47ac0f02009-02-11 04:27:20 +000079
Bill Wendling960bb852009-08-22 20:41:06 +000080 DEBUG({
David Greene4b134d12010-01-05 01:25:41 +000081 dbgs() << "*** Final schedule ***\n";
Bill Wendling960bb852009-08-22 20:41:06 +000082 dumpSchedule();
David Greene4b134d12010-01-05 01:25:41 +000083 dbgs() << '\n';
Bill Wendling960bb852009-08-22 20:41:06 +000084 });
Dan Gohman343f0c02008-11-19 23:18:57 +000085}
86
Dan Gohmanc6b680e2008-12-16 01:05:52 +000087/// addPred - This adds the specified edge as a pred of the current node if
88/// not already. It also adds the current node as a successor of the
89/// specified node.
Andrew Trick92e94662011-02-04 03:18:17 +000090bool SUnit::addPred(const SDep &D) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +000091 // If this node already has this depenence, don't add a redundant one.
Dan Gohman5cffa6f2009-02-11 00:12:28 +000092 for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end();
93 I != E; ++I)
94 if (*I == D)
Andrew Trick92e94662011-02-04 03:18:17 +000095 return false;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000096 // Now add a corresponding succ to N.
97 SDep P = D;
98 P.setSUnit(this);
99 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000100 // Update the bookkeeping.
101 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000102 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
103 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000104 ++NumPreds;
105 ++N->NumSuccs;
106 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000107 if (!N->isScheduled) {
108 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000109 ++NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000110 }
111 if (!isScheduled) {
112 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000113 ++N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000114 }
Dan Gohman3f237442008-12-16 03:25:46 +0000115 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000116 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000117 if (P.getLatency() != 0) {
118 this->setDepthDirty();
119 N->setHeightDirty();
120 }
Andrew Trick92e94662011-02-04 03:18:17 +0000121 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000122}
123
124/// removePred - This removes the specified edge as a pred of the current
125/// node if it exists. It also removes the current node as a successor of
126/// the specified node.
127void SUnit::removePred(const SDep &D) {
128 // Find the matching predecessor.
129 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
130 I != E; ++I)
131 if (*I == D) {
132 bool FoundSucc = false;
133 // Find the corresponding successor in N.
134 SDep P = D;
135 P.setSUnit(this);
136 SUnit *N = D.getSUnit();
137 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
138 EE = N->Succs.end(); II != EE; ++II)
139 if (*II == P) {
140 FoundSucc = true;
141 N->Succs.erase(II);
142 break;
143 }
144 assert(FoundSucc && "Mismatching preds / succs lists!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000145 (void)FoundSucc;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000146 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000147 // Update the bookkeeping.
148 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000149 assert(NumPreds > 0 && "NumPreds will underflow!");
150 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000151 --NumPreds;
152 --N->NumSuccs;
153 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000154 if (!N->isScheduled) {
155 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000156 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000157 }
158 if (!isScheduled) {
159 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000160 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000161 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000162 if (P.getLatency() != 0) {
163 this->setDepthDirty();
164 N->setHeightDirty();
165 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000166 return;
167 }
168}
169
Dan Gohman3f237442008-12-16 03:25:46 +0000170void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000171 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000172 SmallVector<SUnit*, 8> WorkList;
173 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000174 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000175 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000176 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000177 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000178 E = SU->Succs.end(); I != E; ++I) {
179 SUnit *SuccSU = I->getSUnit();
180 if (SuccSU->isDepthCurrent)
181 WorkList.push_back(SuccSU);
182 }
183 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000184}
185
186void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000187 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000188 SmallVector<SUnit*, 8> WorkList;
189 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000190 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000191 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000192 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000193 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000194 E = SU->Preds.end(); I != E; ++I) {
195 SUnit *PredSU = I->getSUnit();
196 if (PredSU->isHeightCurrent)
197 WorkList.push_back(PredSU);
198 }
199 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000200}
201
202/// setDepthToAtLeast - Update this node's successors to reflect the
203/// fact that this node's depth just increased.
204///
David Goodwin557bbe62009-11-20 19:32:48 +0000205void SUnit::setDepthToAtLeast(unsigned NewDepth) {
206 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000207 return;
208 setDepthDirty();
209 Depth = NewDepth;
210 isDepthCurrent = true;
211}
212
213/// setHeightToAtLeast - Update this node's predecessors to reflect the
214/// fact that this node's height just increased.
215///
David Goodwin557bbe62009-11-20 19:32:48 +0000216void SUnit::setHeightToAtLeast(unsigned NewHeight) {
217 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000218 return;
219 setHeightDirty();
220 Height = NewHeight;
221 isHeightCurrent = true;
222}
223
224/// ComputeDepth - Calculate the maximal path from the node to the exit.
225///
David Goodwin557bbe62009-11-20 19:32:48 +0000226void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000227 SmallVector<SUnit*, 8> WorkList;
228 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000229 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000230 SUnit *Cur = WorkList.back();
231
232 bool Done = true;
233 unsigned MaxPredDepth = 0;
234 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
235 E = Cur->Preds.end(); I != E; ++I) {
236 SUnit *PredSU = I->getSUnit();
237 if (PredSU->isDepthCurrent)
238 MaxPredDepth = std::max(MaxPredDepth,
239 PredSU->Depth + I->getLatency());
240 else {
241 Done = false;
242 WorkList.push_back(PredSU);
243 }
244 }
245
246 if (Done) {
247 WorkList.pop_back();
248 if (MaxPredDepth != Cur->Depth) {
249 Cur->setDepthDirty();
250 Cur->Depth = MaxPredDepth;
251 }
252 Cur->isDepthCurrent = true;
253 }
Dan Gohman1578f842008-12-23 17:22:32 +0000254 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000255}
256
257/// ComputeHeight - Calculate the maximal path from the node to the entry.
258///
David Goodwin557bbe62009-11-20 19:32:48 +0000259void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000260 SmallVector<SUnit*, 8> WorkList;
261 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000262 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000263 SUnit *Cur = WorkList.back();
264
265 bool Done = true;
266 unsigned MaxSuccHeight = 0;
267 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
268 E = Cur->Succs.end(); I != E; ++I) {
269 SUnit *SuccSU = I->getSUnit();
270 if (SuccSU->isHeightCurrent)
271 MaxSuccHeight = std::max(MaxSuccHeight,
272 SuccSU->Height + I->getLatency());
273 else {
274 Done = false;
275 WorkList.push_back(SuccSU);
276 }
277 }
278
279 if (Done) {
280 WorkList.pop_back();
281 if (MaxSuccHeight != Cur->Height) {
282 Cur->setHeightDirty();
283 Cur->Height = MaxSuccHeight;
284 }
285 Cur->isHeightCurrent = true;
286 }
Dan Gohman1578f842008-12-23 17:22:32 +0000287 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000288}
289
Dan Gohman343f0c02008-11-19 23:18:57 +0000290/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
291/// a group of nodes flagged together.
292void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000293 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000294 G->dumpNode(this);
295}
296
297void SUnit::dumpAll(const ScheduleDAG *G) const {
298 dump(G);
299
David Greene4b134d12010-01-05 01:25:41 +0000300 dbgs() << " # preds left : " << NumPredsLeft << "\n";
301 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000302 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000303 dbgs() << " Latency : " << Latency << "\n";
304 dbgs() << " Depth : " << Depth << "\n";
305 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000306
307 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000308 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000309 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
310 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000311 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000312 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000313 case SDep::Data: dbgs() << "val "; break;
314 case SDep::Anti: dbgs() << "anti"; break;
315 case SDep::Output: dbgs() << "out "; break;
316 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000317 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000318 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000319 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000320 dbgs() << " *";
321 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000322 if (I->isAssignedRegDep())
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000323 dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
David Greene4b134d12010-01-05 01:25:41 +0000324 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000325 }
326 }
327 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000328 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000329 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
330 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000331 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000332 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000333 case SDep::Data: dbgs() << "val "; break;
334 case SDep::Anti: dbgs() << "anti"; break;
335 case SDep::Output: dbgs() << "out "; break;
336 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000337 }
Jakob Stoklund Olesen0b923d92012-02-17 21:44:51 +0000338 dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000339 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000340 dbgs() << " *";
341 dbgs() << ": Latency=" << I->getLatency();
342 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000343 }
344 }
David Greene4b134d12010-01-05 01:25:41 +0000345 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000346}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000347
348#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000349/// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
350/// their state is consistent. Return the number of scheduled nodes.
Dan Gohmana1e6d362008-11-20 01:26:25 +0000351///
Andrew Trick4c727202012-03-07 05:21:36 +0000352unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000353 bool AnyNotSched = false;
354 unsigned DeadNodes = 0;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000355 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
356 if (!SUnits[i].isScheduled) {
357 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
358 ++DeadNodes;
359 continue;
360 }
361 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000362 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000363 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000364 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000365 AnyNotSched = true;
366 }
Dan Gohman3f237442008-12-16 03:25:46 +0000367 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000368 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000369 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000370 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000371 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000372 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000373 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000374 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000375 AnyNotSched = true;
376 }
377 if (isBottomUp) {
378 if (SUnits[i].NumSuccsLeft != 0) {
379 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000380 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000381 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000382 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000383 AnyNotSched = true;
384 }
385 } else {
386 if (SUnits[i].NumPredsLeft != 0) {
387 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000388 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000389 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000390 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000391 AnyNotSched = true;
392 }
393 }
394 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000395 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000396 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000397}
398#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000399
John Mosby9f71f802010-06-30 03:40:54 +0000400/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000401/// ordering from the DAG to be scheduled.
402///
John Mosby9f71f802010-06-30 03:40:54 +0000403/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000404/// "Online algorithms for managing the topological order of
405/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000406/// This is the MNR algorithm, which was first introduced by
407/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000408/// "Maintaining a topological order under edge insertions".
409///
John Mosby9f71f802010-06-30 03:40:54 +0000410/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000411///
412/// Topological ordering, ord, of a DAG maps each node to a topological
413/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
414///
John Mosby9f71f802010-06-30 03:40:54 +0000415/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000416/// then ord(X) < ord(Z).
417///
418/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000419/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000420/// create a cycle.
421///
422/// The algorithm first computes a topological ordering for the DAG by
423/// initializing the Index2Node and Node2Index arrays and then tries to keep
424/// the ordering up-to-date after edge insertions by reordering the DAG.
425///
426/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
427/// the nodes reachable from Y, and then shifts them using Shift to lie
428/// immediately after X in Index2Node.
429void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
430 unsigned DAGSize = SUnits.size();
431 std::vector<SUnit*> WorkList;
432 WorkList.reserve(DAGSize);
433
434 Index2Node.resize(DAGSize);
435 Node2Index.resize(DAGSize);
436
437 // Initialize the data structures.
438 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
439 SUnit *SU = &SUnits[i];
440 int NodeNum = SU->NodeNum;
441 unsigned Degree = SU->Succs.size();
442 // Temporarily use the Node2Index array as scratch space for degree counts.
443 Node2Index[NodeNum] = Degree;
444
445 // Is it a node without dependencies?
446 if (Degree == 0) {
447 assert(SU->Succs.empty() && "SUnit should have no successors");
448 // Collect leaf nodes.
449 WorkList.push_back(SU);
450 }
John Mosby9f71f802010-06-30 03:40:54 +0000451 }
Dan Gohman21d90032008-11-25 00:52:40 +0000452
453 int Id = DAGSize;
454 while (!WorkList.empty()) {
455 SUnit *SU = WorkList.back();
456 WorkList.pop_back();
457 Allocate(SU->NodeNum, --Id);
458 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
459 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000460 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000461 if (!--Node2Index[SU->NodeNum])
462 // If all dependencies of the node are processed already,
463 // then the node can be computed now.
464 WorkList.push_back(SU);
465 }
466 }
467
468 Visited.resize(DAGSize);
469
470#ifndef NDEBUG
471 // Check correctness of the ordering
472 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
473 SUnit *SU = &SUnits[i];
474 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
475 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000476 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000477 "Wrong topological sorting");
478 }
479 }
480#endif
481}
482
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000483/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000484/// to be added from SUnit X to SUnit Y.
485void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
486 int UpperBound, LowerBound;
487 LowerBound = Node2Index[Y->NodeNum];
488 UpperBound = Node2Index[X->NodeNum];
489 bool HasLoop = false;
490 // Is Ord(X) < Ord(Y) ?
491 if (LowerBound < UpperBound) {
492 // Update the topological order.
493 Visited.reset();
494 DFS(Y, UpperBound, HasLoop);
495 assert(!HasLoop && "Inserted edge creates a loop!");
496 // Recompute topological indexes.
497 Shift(Visited, LowerBound, UpperBound);
498 }
499}
500
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000501/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000502/// an edge to be removed from the specified node N from the predecessors
503/// of the current node M.
504void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
505 // InitDAGTopologicalSorting();
506}
507
508/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
509/// all nodes affected by the edge insertion. These nodes will later get new
510/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000511void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000512 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000513 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000514 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000515
516 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000517 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000518 SU = WorkList.back();
519 WorkList.pop_back();
520 Visited.set(SU->NodeNum);
521 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000522 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000523 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000524 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000525 return;
526 }
527 // Visit successors if not already and in affected region.
528 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000529 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000530 }
531 }
Dan Gohman1578f842008-12-23 17:22:32 +0000532 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000533}
534
John Mosby9f71f802010-06-30 03:40:54 +0000535/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000536/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000537void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000538 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000539 std::vector<int> L;
540 int shift = 0;
541 int i;
542
543 for (i = LowerBound; i <= UpperBound; ++i) {
544 // w is node at topological index i.
545 int w = Index2Node[i];
546 if (Visited.test(w)) {
547 // Unmark.
548 Visited.reset(w);
549 L.push_back(w);
550 shift = shift + 1;
551 } else {
552 Allocate(w, i - shift);
553 }
554 }
555
556 for (unsigned j = 0; j < L.size(); ++j) {
557 Allocate(L[j], i - shift);
558 i = i + 1;
559 }
560}
561
562
563/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
564/// create a cycle.
565bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
566 if (IsReachable(TargetSU, SU))
567 return true;
568 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
569 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000570 if (I->isAssignedRegDep() &&
571 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000572 return true;
573 return false;
574}
575
576/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000577bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
578 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000579 // If insertion of the edge SU->TargetSU would create a cycle
580 // then there is a path from TargetSU to SU.
581 int UpperBound, LowerBound;
582 LowerBound = Node2Index[TargetSU->NodeNum];
583 UpperBound = Node2Index[SU->NodeNum];
584 bool HasLoop = false;
585 // Is Ord(TargetSU) < Ord(SU) ?
586 if (LowerBound < UpperBound) {
587 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000588 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000589 DFS(TargetSU, UpperBound, HasLoop);
590 }
591 return HasLoop;
592}
593
594/// Allocate - assign the topological index to the node n.
595void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
596 Node2Index[n] = index;
597 Index2Node[index] = n;
598}
599
John Mosby9f71f802010-06-30 03:40:54 +0000600ScheduleDAGTopologicalSort::
601ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000602
603ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}