blob: 1e9b5c89f1724f9ebab3aed9b6dd4c6305332c3b [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
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.
Evan Chenge837dea2011-06-28 19:10:37 +000048const MCInstrDesc *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!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000143 (void)FoundSucc;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000144 Preds.erase(I);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000145 // Update the bookkeeping.
146 if (P.getKind() == SDep::Data) {
Reid Klecknerc277ab02009-09-30 20:15:38 +0000147 assert(NumPreds > 0 && "NumPreds will underflow!");
148 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000149 --NumPreds;
150 --N->NumSuccs;
151 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000152 if (!N->isScheduled) {
153 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000154 --NumPredsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000155 }
156 if (!isScheduled) {
157 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000158 --N->NumSuccsLeft;
Reid Klecknerc277ab02009-09-30 20:15:38 +0000159 }
Dan Gohmana80c8592009-01-05 22:40:26 +0000160 if (P.getLatency() != 0) {
161 this->setDepthDirty();
162 N->setHeightDirty();
163 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000164 return;
165 }
166}
167
Dan Gohman3f237442008-12-16 03:25:46 +0000168void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000169 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000170 SmallVector<SUnit*, 8> WorkList;
171 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000172 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000173 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000174 SU->isDepthCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000175 for (SUnit::const_succ_iterator I = SU->Succs.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000176 E = SU->Succs.end(); I != E; ++I) {
177 SUnit *SuccSU = I->getSUnit();
178 if (SuccSU->isDepthCurrent)
179 WorkList.push_back(SuccSU);
180 }
181 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000182}
183
184void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000185 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000186 SmallVector<SUnit*, 8> WorkList;
187 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000188 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000189 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000190 SU->isHeightCurrent = false;
Dan Gohmanf89e6e62008-12-20 16:34:57 +0000191 for (SUnit::const_pred_iterator I = SU->Preds.begin(),
Dan Gohman8044e9b2008-12-22 21:11:33 +0000192 E = SU->Preds.end(); I != E; ++I) {
193 SUnit *PredSU = I->getSUnit();
194 if (PredSU->isHeightCurrent)
195 WorkList.push_back(PredSU);
196 }
197 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000198}
199
200/// setDepthToAtLeast - Update this node's successors to reflect the
201/// fact that this node's depth just increased.
202///
David Goodwin557bbe62009-11-20 19:32:48 +0000203void SUnit::setDepthToAtLeast(unsigned NewDepth) {
204 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000205 return;
206 setDepthDirty();
207 Depth = NewDepth;
208 isDepthCurrent = true;
209}
210
211/// setHeightToAtLeast - Update this node's predecessors to reflect the
212/// fact that this node's height just increased.
213///
David Goodwin557bbe62009-11-20 19:32:48 +0000214void SUnit::setHeightToAtLeast(unsigned NewHeight) {
215 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000216 return;
217 setHeightDirty();
218 Height = NewHeight;
219 isHeightCurrent = true;
220}
221
222/// ComputeDepth - Calculate the maximal path from the node to the exit.
223///
David Goodwin557bbe62009-11-20 19:32:48 +0000224void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000225 SmallVector<SUnit*, 8> WorkList;
226 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000227 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000228 SUnit *Cur = WorkList.back();
229
230 bool Done = true;
231 unsigned MaxPredDepth = 0;
232 for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
233 E = Cur->Preds.end(); I != E; ++I) {
234 SUnit *PredSU = I->getSUnit();
235 if (PredSU->isDepthCurrent)
236 MaxPredDepth = std::max(MaxPredDepth,
237 PredSU->Depth + I->getLatency());
238 else {
239 Done = false;
240 WorkList.push_back(PredSU);
241 }
242 }
243
244 if (Done) {
245 WorkList.pop_back();
246 if (MaxPredDepth != Cur->Depth) {
247 Cur->setDepthDirty();
248 Cur->Depth = MaxPredDepth;
249 }
250 Cur->isDepthCurrent = true;
251 }
Dan Gohman1578f842008-12-23 17:22:32 +0000252 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000253}
254
255/// ComputeHeight - Calculate the maximal path from the node to the entry.
256///
David Goodwin557bbe62009-11-20 19:32:48 +0000257void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000258 SmallVector<SUnit*, 8> WorkList;
259 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000260 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000261 SUnit *Cur = WorkList.back();
262
263 bool Done = true;
264 unsigned MaxSuccHeight = 0;
265 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
266 E = Cur->Succs.end(); I != E; ++I) {
267 SUnit *SuccSU = I->getSUnit();
268 if (SuccSU->isHeightCurrent)
269 MaxSuccHeight = std::max(MaxSuccHeight,
270 SuccSU->Height + I->getLatency());
271 else {
272 Done = false;
273 WorkList.push_back(SuccSU);
274 }
275 }
276
277 if (Done) {
278 WorkList.pop_back();
279 if (MaxSuccHeight != Cur->Height) {
280 Cur->setHeightDirty();
281 Cur->Height = MaxSuccHeight;
282 }
283 Cur->isHeightCurrent = true;
284 }
Dan Gohman1578f842008-12-23 17:22:32 +0000285 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000286}
287
Dan Gohman343f0c02008-11-19 23:18:57 +0000288/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
289/// a group of nodes flagged together.
290void SUnit::dump(const ScheduleDAG *G) const {
David Greene4b134d12010-01-05 01:25:41 +0000291 dbgs() << "SU(" << NodeNum << "): ";
Dan Gohman343f0c02008-11-19 23:18:57 +0000292 G->dumpNode(this);
293}
294
295void SUnit::dumpAll(const ScheduleDAG *G) const {
296 dump(G);
297
David Greene4b134d12010-01-05 01:25:41 +0000298 dbgs() << " # preds left : " << NumPredsLeft << "\n";
299 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000300 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000301 dbgs() << " Latency : " << Latency << "\n";
302 dbgs() << " Depth : " << Depth << "\n";
303 dbgs() << " Height : " << Height << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000304
305 if (Preds.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000306 dbgs() << " Predecessors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000307 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
308 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000309 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000310 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000311 case SDep::Data: dbgs() << "val "; break;
312 case SDep::Anti: dbgs() << "anti"; break;
313 case SDep::Output: dbgs() << "out "; break;
314 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000315 }
David Greene4b134d12010-01-05 01:25:41 +0000316 dbgs() << "#";
317 dbgs() << I->getSUnit() << " - SU(" << I->getSUnit()->NodeNum << ")";
Dan Gohman54e4c362008-12-09 22:54:47 +0000318 if (I->isArtificial())
David Greene4b134d12010-01-05 01:25:41 +0000319 dbgs() << " *";
320 dbgs() << ": Latency=" << I->getLatency();
Andrew Trick4cb971c2011-06-15 17:16:12 +0000321 if (I->isAssignedRegDep())
322 dbgs() << " Reg=" << G->TRI->getName(I->getReg());
David Greene4b134d12010-01-05 01:25:41 +0000323 dbgs() << "\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000324 }
325 }
326 if (Succs.size() != 0) {
David Greene4b134d12010-01-05 01:25:41 +0000327 dbgs() << " Successors:\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000328 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
329 I != E; ++I) {
David Greene4b134d12010-01-05 01:25:41 +0000330 dbgs() << " ";
Dan Gohman54e4c362008-12-09 22:54:47 +0000331 switch (I->getKind()) {
David Greene4b134d12010-01-05 01:25:41 +0000332 case SDep::Data: dbgs() << "val "; break;
333 case SDep::Anti: dbgs() << "anti"; break;
334 case SDep::Output: dbgs() << "out "; break;
335 case SDep::Order: dbgs() << "ch "; break;
Dan Gohman54e4c362008-12-09 22:54:47 +0000336 }
David Greene4b134d12010-01-05 01:25:41 +0000337 dbgs() << "#";
338 dbgs() << I->getSUnit() << " - 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
349/// VerifySchedule - Verify that all SUnits were scheduled and that
350/// their state is consistent.
351///
352void ScheduleDAG::VerifySchedule(bool isBottomUp) {
353 bool AnyNotSched = false;
354 unsigned DeadNodes = 0;
355 unsigned Noops = 0;
356 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
357 if (!SUnits[i].isScheduled) {
358 if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
359 ++DeadNodes;
360 continue;
361 }
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 not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000366 AnyNotSched = true;
367 }
Dan Gohman3f237442008-12-16 03:25:46 +0000368 if (SUnits[i].isScheduled &&
David Goodwin4de099d2009-11-03 20:57:50 +0000369 (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
Dan Gohman3f237442008-12-16 03:25:46 +0000370 unsigned(INT_MAX)) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000371 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000372 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000373 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000374 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000375 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000376 AnyNotSched = true;
377 }
378 if (isBottomUp) {
379 if (SUnits[i].NumSuccsLeft != 0) {
380 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000381 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000382 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000383 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000384 AnyNotSched = true;
385 }
386 } else {
387 if (SUnits[i].NumPredsLeft != 0) {
388 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000389 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000390 SUnits[i].dump(this);
David Greene4b134d12010-01-05 01:25:41 +0000391 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000392 AnyNotSched = true;
393 }
394 }
395 }
396 for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
397 if (!Sequence[i])
398 ++Noops;
399 assert(!AnyNotSched);
400 assert(Sequence.size() + DeadNodes - Noops == SUnits.size() &&
401 "The number of nodes scheduled doesn't match the expected number!");
402}
403#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000404
John Mosby9f71f802010-06-30 03:40:54 +0000405/// InitDAGTopologicalSorting - create the initial topological
Dan Gohman21d90032008-11-25 00:52:40 +0000406/// ordering from the DAG to be scheduled.
407///
John Mosby9f71f802010-06-30 03:40:54 +0000408/// The idea of the algorithm is taken from
Dan Gohman21d90032008-11-25 00:52:40 +0000409/// "Online algorithms for managing the topological order of
410/// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
John Mosby9f71f802010-06-30 03:40:54 +0000411/// This is the MNR algorithm, which was first introduced by
412/// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
Dan Gohman21d90032008-11-25 00:52:40 +0000413/// "Maintaining a topological order under edge insertions".
414///
John Mosby9f71f802010-06-30 03:40:54 +0000415/// Short description of the algorithm:
Dan Gohman21d90032008-11-25 00:52:40 +0000416///
417/// Topological ordering, ord, of a DAG maps each node to a topological
418/// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
419///
John Mosby9f71f802010-06-30 03:40:54 +0000420/// This means that if there is a path from the node X to the node Z,
Dan Gohman21d90032008-11-25 00:52:40 +0000421/// then ord(X) < ord(Z).
422///
423/// This property can be used to check for reachability of nodes:
John Mosby9f71f802010-06-30 03:40:54 +0000424/// if Z is reachable from X, then an insertion of the edge Z->X would
Dan Gohman21d90032008-11-25 00:52:40 +0000425/// create a cycle.
426///
427/// The algorithm first computes a topological ordering for the DAG by
428/// initializing the Index2Node and Node2Index arrays and then tries to keep
429/// the ordering up-to-date after edge insertions by reordering the DAG.
430///
431/// On insertion of the edge X->Y, the algorithm first marks by calling DFS
432/// the nodes reachable from Y, and then shifts them using Shift to lie
433/// immediately after X in Index2Node.
434void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
435 unsigned DAGSize = SUnits.size();
436 std::vector<SUnit*> WorkList;
437 WorkList.reserve(DAGSize);
438
439 Index2Node.resize(DAGSize);
440 Node2Index.resize(DAGSize);
441
442 // Initialize the data structures.
443 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
444 SUnit *SU = &SUnits[i];
445 int NodeNum = SU->NodeNum;
446 unsigned Degree = SU->Succs.size();
447 // Temporarily use the Node2Index array as scratch space for degree counts.
448 Node2Index[NodeNum] = Degree;
449
450 // Is it a node without dependencies?
451 if (Degree == 0) {
452 assert(SU->Succs.empty() && "SUnit should have no successors");
453 // Collect leaf nodes.
454 WorkList.push_back(SU);
455 }
John Mosby9f71f802010-06-30 03:40:54 +0000456 }
Dan Gohman21d90032008-11-25 00:52:40 +0000457
458 int Id = DAGSize;
459 while (!WorkList.empty()) {
460 SUnit *SU = WorkList.back();
461 WorkList.pop_back();
462 Allocate(SU->NodeNum, --Id);
463 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
464 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000465 SUnit *SU = I->getSUnit();
Dan Gohman21d90032008-11-25 00:52:40 +0000466 if (!--Node2Index[SU->NodeNum])
467 // If all dependencies of the node are processed already,
468 // then the node can be computed now.
469 WorkList.push_back(SU);
470 }
471 }
472
473 Visited.resize(DAGSize);
474
475#ifndef NDEBUG
476 // Check correctness of the ordering
477 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
478 SUnit *SU = &SUnits[i];
479 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
480 I != E; ++I) {
John Mosby9f71f802010-06-30 03:40:54 +0000481 assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000482 "Wrong topological sorting");
483 }
484 }
485#endif
486}
487
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000488/// AddPred - Updates the topological ordering to accommodate an edge
Dan Gohman21d90032008-11-25 00:52:40 +0000489/// to be added from SUnit X to SUnit Y.
490void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
491 int UpperBound, LowerBound;
492 LowerBound = Node2Index[Y->NodeNum];
493 UpperBound = Node2Index[X->NodeNum];
494 bool HasLoop = false;
495 // Is Ord(X) < Ord(Y) ?
496 if (LowerBound < UpperBound) {
497 // Update the topological order.
498 Visited.reset();
499 DFS(Y, UpperBound, HasLoop);
500 assert(!HasLoop && "Inserted edge creates a loop!");
501 // Recompute topological indexes.
502 Shift(Visited, LowerBound, UpperBound);
503 }
504}
505
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000506/// RemovePred - Updates the topological ordering to accommodate an
Dan Gohman21d90032008-11-25 00:52:40 +0000507/// an edge to be removed from the specified node N from the predecessors
508/// of the current node M.
509void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
510 // InitDAGTopologicalSorting();
511}
512
513/// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
514/// all nodes affected by the edge insertion. These nodes will later get new
515/// topological indexes by means of the Shift method.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000516void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000517 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000518 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000519 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000520
521 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000522 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000523 SU = WorkList.back();
524 WorkList.pop_back();
525 Visited.set(SU->NodeNum);
526 for (int I = SU->Succs.size()-1; I >= 0; --I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000527 int s = SU->Succs[I].getSUnit()->NodeNum;
Dan Gohman21d90032008-11-25 00:52:40 +0000528 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000529 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000530 return;
531 }
532 // Visit successors if not already and in affected region.
533 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000534 WorkList.push_back(SU->Succs[I].getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000535 }
536 }
Dan Gohman1578f842008-12-23 17:22:32 +0000537 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000538}
539
John Mosby9f71f802010-06-30 03:40:54 +0000540/// Shift - Renumber the nodes so that the topological ordering is
Dan Gohman21d90032008-11-25 00:52:40 +0000541/// preserved.
John Mosby9f71f802010-06-30 03:40:54 +0000542void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000543 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000544 std::vector<int> L;
545 int shift = 0;
546 int i;
547
548 for (i = LowerBound; i <= UpperBound; ++i) {
549 // w is node at topological index i.
550 int w = Index2Node[i];
551 if (Visited.test(w)) {
552 // Unmark.
553 Visited.reset(w);
554 L.push_back(w);
555 shift = shift + 1;
556 } else {
557 Allocate(w, i - shift);
558 }
559 }
560
561 for (unsigned j = 0; j < L.size(); ++j) {
562 Allocate(L[j], i - shift);
563 i = i + 1;
564 }
565}
566
567
568/// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will
569/// create a cycle.
570bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) {
571 if (IsReachable(TargetSU, SU))
572 return true;
573 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
574 I != E; ++I)
Dan Gohman54e4c362008-12-09 22:54:47 +0000575 if (I->isAssignedRegDep() &&
576 IsReachable(TargetSU, I->getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000577 return true;
578 return false;
579}
580
581/// IsReachable - Checks if SU is reachable from TargetSU.
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000582bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
583 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000584 // If insertion of the edge SU->TargetSU would create a cycle
585 // then there is a path from TargetSU to SU.
586 int UpperBound, LowerBound;
587 LowerBound = Node2Index[TargetSU->NodeNum];
588 UpperBound = Node2Index[SU->NodeNum];
589 bool HasLoop = false;
590 // Is Ord(TargetSU) < Ord(SU) ?
591 if (LowerBound < UpperBound) {
592 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000593 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000594 DFS(TargetSU, UpperBound, HasLoop);
595 }
596 return HasLoop;
597}
598
599/// Allocate - assign the topological index to the node n.
600void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
601 Node2Index[n] = index;
602 Index2Node[index] = n;
603}
604
John Mosby9f71f802010-06-30 03:40:54 +0000605ScheduleDAGTopologicalSort::
606ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000607
608ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}