blob: f3284939368989807f626cddc2d18515106c2e04 [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
29cl::opt<bool> StressSchedOpt(
30 "stress-sched", cl::Hidden, cl::init(false),
31 cl::desc("Stress test instruction scheduling"));
32#endif
33
Dan Gohman79ce2762009-01-15 19:20:50 +000034ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Dan Gohman47ac0f02009-02-11 04:27:20 +000035 : TM(mf.getTarget()),
Dan Gohman79ce2762009-01-15 19:20:50 +000036 TII(TM.getInstrInfo()),
37 TRI(TM.getRegisterInfo()),
Dan Gohman79ce2762009-01-15 19:20:50 +000038 MF(mf), MRI(mf.getRegInfo()),
Dan Gohman9e64bbb2009-02-10 23:27:53 +000039 EntrySU(), ExitSU() {
Andrew Trick4cb971c2011-06-15 17:16:12 +000040#ifndef NDEBUG
41 StressSched = StressSchedOpt;
42#endif
Dan Gohman343f0c02008-11-19 23:18:57 +000043}
44
45ScheduleDAG::~ScheduleDAG() {}
46
Andrew Trick2da8bc82010-12-24 05:03:26 +000047/// getInstrDesc helper to handle SDNodes.
48const TargetInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Andrew Trick24312232010-12-24 06:46:50 +000049 if (!Node || !Node->isMachineOpcode()) return NULL;
Andrew Trick2da8bc82010-12-24 05:03:26 +000050 return &TII->get(Node->getMachineOpcode());
51}
52
Dan Gohman343f0c02008-11-19 23:18:57 +000053/// dump - dump the schedule.
54void ScheduleDAG::dumpSchedule() const {
55 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
56 if (SUnit *SU = Sequence[i])
57 SU->dump(this);
58 else
David Greene4b134d12010-01-05 01:25:41 +000059 dbgs() << "**** NOOP ****\n";
Dan Gohman343f0c02008-11-19 23:18:57 +000060 }
61}
62
63
64/// Run - perform scheduling.
65///
Dan Gohman47ac0f02009-02-11 04:27:20 +000066void ScheduleDAG::Run(MachineBasicBlock *bb,
67 MachineBasicBlock::iterator insertPos) {
68 BB = bb;
69 InsertPos = insertPos;
Dan Gohmanf7119392009-01-16 22:10:20 +000070
Dan Gohman79ce2762009-01-15 19:20:50 +000071 SUnits.clear();
72 Sequence.clear();
Dan Gohman9e64bbb2009-02-10 23:27:53 +000073 EntrySU = SUnit();
74 ExitSU = SUnit();
Dan Gohman79ce2762009-01-15 19:20:50 +000075
Dan Gohman343f0c02008-11-19 23:18:57 +000076 Schedule();
Dan Gohman47ac0f02009-02-11 04:27:20 +000077
Bill Wendling960bb852009-08-22 20:41:06 +000078 DEBUG({
David Greene4b134d12010-01-05 01:25:41 +000079 dbgs() << "*** Final schedule ***\n";
Bill Wendling960bb852009-08-22 20:41:06 +000080 dumpSchedule();
David Greene4b134d12010-01-05 01:25:41 +000081 dbgs() << '\n';
Bill Wendling960bb852009-08-22 20:41:06 +000082 });
Dan Gohman343f0c02008-11-19 23:18:57 +000083}
84
Dan Gohmanc6b680e2008-12-16 01:05:52 +000085/// addPred - This adds the specified edge as a pred of the current node if
86/// not already. It also adds the current node as a successor of the
87/// specified node.
Andrew Trick92e94662011-02-04 03:18:17 +000088bool SUnit::addPred(const SDep &D) {
Dan Gohmanc6b680e2008-12-16 01:05:52 +000089 // If this node already has this depenence, don't add a redundant one.
Dan Gohman5cffa6f2009-02-11 00:12:28 +000090 for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end();
91 I != E; ++I)
92 if (*I == D)
Andrew Trick92e94662011-02-04 03:18:17 +000093 return false;
Dan Gohmanc6b680e2008-12-16 01:05:52 +000094 // Now add a corresponding succ to N.
95 SDep P = D;
96 P.setSUnit(this);
97 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +000098 // Update the bookkeeping.
99 if (D.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000100 assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
101 assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000102 ++NumPreds;
103 ++N->NumSuccs;
104 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000105 if (!N->isScheduled) {
106 assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000107 ++NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000108 }
109 if (!isScheduled) {
110 assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000111 ++N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000112 }
Dan Gohman3f237442008-12-16 03:25:46 +0000113 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000114 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000115 if (P.getLatency() != 0) {
116 this->setDepthDirty();
117 N->setHeightDirty();
118 }
Andrew Trick92e94662011-02-04 03:18:17 +0000119 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000120}
121
122/// removePred - This removes the specified edge as a pred of the current
123/// node if it exists. It also removes the current node as a successor of
124/// the specified node.
125void SUnit::removePred(const SDep &D) {
126 // Find the matching predecessor.
127 for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end();
128 I != E; ++I)
129 if (*I == D) {
130 bool FoundSucc = false;
131 // Find the corresponding successor in N.
132 SDep P = D;
133 P.setSUnit(this);
134 SUnit *N = D.getSUnit();
135 for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(),
136 EE = N->Succs.end(); II != EE; ++II)
137 if (*II == P) {
138 FoundSucc = true;
139 N->Succs.erase(II);
140 break;
141 }
142 assert(FoundSucc && "Mismatching preds / succs lists!");
143 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000144 // Update the bookkeeping.
145 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000146 assert(NumPreds > 0 && "NumPreds will underflow!");
147 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000148 --NumPreds;
149 --N->NumSuccs;
150 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000151 if (!N->isScheduled) {
152 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000153 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000154 }
155 if (!isScheduled) {
156 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000157 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000158 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000159 if (P.getLatency() != 0) {
160 this->setDepthDirty();
161 N->setHeightDirty();
162 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000163 return;
164 }
165}
166
Dan Gohman3f237442008-12-16 03:25:46 +0000167void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000168 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000169 SmallVector<SUnit*, 8> WorkList;
170 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000171 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000172 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000173 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000174 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000175 E = SU->Succs.end(); I != E; ++I) {
176 SUnit *SuccSU = I->getSUnit();
177 if (SuccSU->isDepthCurrent)
178 WorkList.push_back(SuccSU);
179 }
180 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000181}
182
183void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000184 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000185 SmallVector<SUnit*, 8> WorkList;
186 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000187 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000188 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000189 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000190 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000191 E = SU->Preds.end(); I != E; ++I) {
192 SUnit *PredSU = I->getSUnit();
193 if (PredSU->isHeightCurrent)
194 WorkList.push_back(PredSU);
195 }
196 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000197}
198
199/// setDepthToAtLeast - Update this node's successors to reflect the
200/// fact that this node's depth just increased.
201///
David Goodwin557bbe62009-11-20 19:32:48 +0000202void SUnit::setDepthToAtLeast(unsigned NewDepth) {
203 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000204 return;
205 setDepthDirty();
206 Depth = NewDepth;
207 isDepthCurrent = true;
208}
209
210/// setHeightToAtLeast - Update this node's predecessors to reflect the
211/// fact that this node's height just increased.
212///
David Goodwin557bbe62009-11-20 19:32:48 +0000213void SUnit::setHeightToAtLeast(unsigned NewHeight) {
214 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000215 return;
216 setHeightDirty();
217 Height = NewHeight;
218 isHeightCurrent = true;
219}
220
221/// ComputeDepth - Calculate the maximal path from the node to the exit.
222///
David Goodwin557bbe62009-11-20 19:32:48 +0000223void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000224 SmallVector<SUnit*, 8> WorkList;
225 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000226 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000227 SUnit *Cur = WorkList.back();
228
229 bool Done = true;
230 unsigned MaxPredDepth = 0;
231 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
232 E = Cur->Preds.end(); I != E; ++I) {
233 SUnit *PredSU = I->getSUnit();
234 if (PredSU->isDepthCurrent)
235 MaxPredDepth = std::max(MaxPredDepth,
236 PredSU->Depth + I->getLatency());
237 else {
238 Done = false;
239 WorkList.push_back(PredSU);
240 }
241 }
242
243 if (Done) {
244 WorkList.pop_back();
245 if (MaxPredDepth != Cur->Depth) {
246 Cur->setDepthDirty();
247 Cur->Depth = MaxPredDepth;
248 }
249 Cur->isDepthCurrent = true;
250 }
Dan Gohman1578f842008-12-23 17:22:32 +0000251 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000252}
253
254/// ComputeHeight - Calculate the maximal path from the node to the entry.
255///
David Goodwin557bbe62009-11-20 19:32:48 +0000256void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000257 SmallVector<SUnit*, 8> WorkList;
258 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000259 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000260 SUnit *Cur = WorkList.back();
261
262 bool Done = true;
263 unsigned MaxSuccHeight = 0;
264 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
265 E = Cur->Succs.end(); I != E; ++I) {
266 SUnit *SuccSU = I->getSUnit();
267 if (SuccSU->isHeightCurrent)
268 MaxSuccHeight = std::max(MaxSuccHeight,
269 SuccSU->Height + I->getLatency());
270 else {
271 Done = false;
272 WorkList.push_back(SuccSU);
273 }
274 }
275
276 if (Done) {
277 WorkList.pop_back();
278 if (MaxSuccHeight != Cur->Height) {
279 Cur->setHeightDirty();
280 Cur->Height = MaxSuccHeight;
281 }
282 Cur->isHeightCurrent = true;
283 }
Dan Gohman1578f842008-12-23 17:22:32 +0000284 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000285}
286
Dan Gohman343f0c02008-11-19 23:18:57 +0000287/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
288/// a group of nodes flagged together.
289void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000290 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000291 G->dumpNode(this);
292}
293
294void SUnit::dumpAll(const ScheduleDAG *G) const {
295 dump(G);
296
David Greene4b134d12010-01-05 01:25:41 +0000297 dbgs() << " # preds left : " << NumPredsLeft << "\n";
298 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000299 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000300 dbgs() << " Latency : " << Latency << "\n";
301 dbgs() << " Depth : " << Depth << "\n";
302 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000303
304 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000305 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000306 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
307 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000308 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000309 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000310 case SDep::Data: dbgs() << "val "; break;
311 case SDep::Anti: dbgs() << "anti"; break;
312 case SDep::Output: dbgs() << "out "; break;
313 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000314 }
David Greene4b134d12010-01-05 01:25:41 +0000315 dbgs() << "#";
316 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000317 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000318 dbgs() << " *";
319 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000320 if (I->isAssignedRegDep())
321 dbgs() << " Reg=" << G->TRI->getName(I->getReg());
David Greene4b134d12010-01-05 01:25:41 +0000322 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000323 }
324 }
325 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000326 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000327 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
328 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000329 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000330 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000331 case SDep::Data: dbgs() << "val "; break;
332 case SDep::Anti: dbgs() << "anti"; break;
333 case SDep::Output: dbgs() << "out "; break;
334 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000335 }
David Greene4b134d12010-01-05 01:25:41 +0000336 dbgs() << "#";
337 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000338 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000339 dbgs() << " *";
340 dbgs() << ": Latency=" << I->getLatency();
341 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000342 }
343 }
David Greene4b134d12010-01-05 01:25:41 +0000344 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000345}
Dan Gohmana1e6d362008-11-20 01:26:25 +0000346
347#ifndef NDEBUG
348/// VerifySchedule - Verify that all SUnits were scheduled and that
349/// their state is consistent.
350///
351void ScheduleDAG::VerifySchedule(bool isBottomUp) {
352 bool AnyNotSched = false;
353 unsigned DeadNodes = 0;
354 unsigned Noops = 0;
355 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 }
395 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
396 if (!Sequence[i])
397 ++Noops;
398 assert(!AnyNotSched);
399 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
400 "The number of nodes scheduled doesn't match the expected number!");
401}
402#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000403
John Mosby9f71f802010-06-30 03:40:54 +0000404/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000405/// ordering from the DAG to be scheduled.
406///
John Mosby9f71f802010-06-30 03:40:54 +0000407/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000408/// "Online algorithms for managing the topological order of
409/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000410/// This is the MNR algorithm, which was first introduced by
411/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000412/// "Maintaining a topological order under edge insertions".
413///
John Mosby9f71f802010-06-30 03:40:54 +0000414/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000415///
416/// Topological ordering, ord, of a DAG maps each node to a topological
417/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
418///
John Mosby9f71f802010-06-30 03:40:54 +0000419/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000420/// then ord(X) < ord(Z).
421///
422/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000423/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000424/// create a cycle.
425///
426/// The algorithm first computes a topological ordering for the DAG by
427/// initializing the Index2Node and Node2Index arrays and then tries to keep
428/// the ordering up-to-date after edge insertions by reordering the DAG.
429///
430/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
431/// the nodes reachable from Y, and then shifts them using Shift to lie
432/// immediately after X in Index2Node.
433void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
434 unsigned DAGSize = SUnits.size();
435 std::vector<SUnit*> WorkList;
436 WorkList.reserve(DAGSize);
437
438 Index2Node.resize(DAGSize);
439 Node2Index.resize(DAGSize);
440
441 // Initialize the data structures.
442 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
443 SUnit *SU = &SUnits[i];
444 int NodeNum = SU->NodeNum;
445 unsigned Degree = SU->Succs.size();
446 // Temporarily use the Node2Index array as scratch space for degree counts.
447 Node2Index[NodeNum] = Degree;
448
449 // Is it a node without dependencies?
450 if (Degree == 0) {
451 assert(SU->Succs.empty() && "SUnit should have no successors");
452 // Collect leaf nodes.
453 WorkList.push_back(SU);
454 }
John Mosby9f71f802010-06-30 03:40:54 +0000455 }
Dan Gohman21d90032008-11-25 00:52:40 +0000456
457 int Id = DAGSize;
458 while (!WorkList.empty()) {
459 SUnit *SU = WorkList.back();
460 WorkList.pop_back();
461 Allocate(SU->NodeNum, --Id);
462 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
463 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000464 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000465 if (!--Node2Index[SU->NodeNum])
466 // If all dependencies of the node are processed already,
467 // then the node can be computed now.
468 WorkList.push_back(SU);
469 }
470 }
471
472 Visited.resize(DAGSize);
473
474#ifndef NDEBUG
475 // Check correctness of the ordering
476 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
477 SUnit *SU = &SUnits[i];
478 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
479 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000480 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000481 "Wrong topological sorting");
482 }
483 }
484#endif
485}
486
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000487/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000488/// to be added from SUnit X to SUnit Y.
489void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
490 int UpperBound, LowerBound;
491 LowerBound = Node2Index[Y->NodeNum];
492 UpperBound = Node2Index[X->NodeNum];
493 bool HasLoop = false;
494 // Is Ord(X) < Ord(Y) ?
495 if (LowerBound < UpperBound) {
496 // Update the topological order.
497 Visited.reset();
498 DFS(Y, UpperBound, HasLoop);
499 assert(!HasLoop && "Inserted edge creates a loop!");
500 // Recompute topological indexes.
501 Shift(Visited, LowerBound, UpperBound);
502 }
503}
504
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000505/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000506/// an edge to be removed from the specified node N from the predecessors
507/// of the current node M.
508void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
509 // InitDAGTopologicalSorting();
510}
511
512/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
513/// all nodes affected by the edge insertion. These nodes will later get new
514/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000515void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000516 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000517 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000518 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000519
520 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000521 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000522 SU = WorkList.back();
523 WorkList.pop_back();
524 Visited.set(SU->NodeNum);
525 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000526 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000527 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000528 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000529 return;
530 }
531 // Visit successors if not already and in affected region.
532 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000533 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000534 }
535 }
Dan Gohman1578f842008-12-23 17:22:32 +0000536 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000537}
538
John Mosby9f71f802010-06-30 03:40:54 +0000539/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000540/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000541void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000542 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000543 std::vector<int> L;
544 int shift = 0;
545 int i;
546
547 for (i = LowerBound; i <= UpperBound; ++i) {
548 // w is node at topological index i.
549 int w = Index2Node[i];
550 if (Visited.test(w)) {
551 // Unmark.
552 Visited.reset(w);
553 L.push_back(w);
554 shift = shift + 1;
555 } else {
556 Allocate(w, i - shift);
557 }
558 }
559
560 for (unsigned j = 0; j < L.size(); ++j) {
561 Allocate(L[j], i - shift);
562 i = i + 1;
563 }
564}
565
566
567/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
568/// create a cycle.
569bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
570 if (IsReachable(TargetSU, SU))
571 return true;
572 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
573 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000574 if (I->isAssignedRegDep() &&
575 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000576 return true;
577 return false;
578}
579
580/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000581bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
582 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000583 // If insertion of the edge SU->TargetSU would create a cycle
584 // then there is a path from TargetSU to SU.
585 int UpperBound, LowerBound;
586 LowerBound = Node2Index[TargetSU->NodeNum];
587 UpperBound = Node2Index[SU->NodeNum];
588 bool HasLoop = false;
589 // Is Ord(TargetSU) < Ord(SU) ?
590 if (LowerBound < UpperBound) {
591 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000592 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000593 DFS(TargetSU, UpperBound, HasLoop);
594 }
595 return HasLoop;
596}
597
598/// Allocate - assign the topological index to the node n.
599void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
600 Node2Index[n] = index;
601 Index2Node[index] = n;
602}
603
John Mosby9f71f802010-06-30 03:40:54 +0000604ScheduleDAGTopologicalSort::
605ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000606
607ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}