blob: 46064012d9d87ee82123e2e8d309db5a6edc10d7 [file] [log] [blame]
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +00001//===- ScheduleDAG.cpp - Implement the ScheduleDAG class ------------------===//
Dan Gohman60cb69e2008-11-19 23:18:57 +00002//
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//
Matthias Braun9ab40392017-02-21 01:27:33 +000010/// \file Implements the ScheduleDAG class, which is a base class used by
11/// scheduling implementation classes.
Dan Gohman60cb69e2008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
Dan Gohman60cb69e2008-11-19 23:18:57 +000015#include "llvm/CodeGen/ScheduleDAG.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/iterator_range.h"
19#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman7e105f02009-01-15 22:18:12 +000020#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Andrew Trick10ffc2b2010-12-24 05:03:26 +000021#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetRegisterInfo.h"
24#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000025#include "llvm/Config/llvm-config.h"
Andrew Trick3013b6a2011-06-15 17:16:12 +000026#include "llvm/Support/CommandLine.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000027#include "llvm/Support/Compiler.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000028#include "llvm/Support/Debug.h"
Daniel Dunbar8ef07352009-07-24 09:53:24 +000029#include "llvm/Support/raw_ostream.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000030#include <algorithm>
31#include <cassert>
32#include <iterator>
33#include <limits>
34#include <utility>
35#include <vector>
36
Dan Gohman60cb69e2008-11-19 23:18:57 +000037using namespace llvm;
38
Chandler Carruth1b9dde02014-04-22 02:02:50 +000039#define DEBUG_TYPE "pre-RA-sched"
40
Andrew Trick3013b6a2011-06-15 17:16:12 +000041#ifndef NDEBUG
Benjamin Kramer4938edb2011-08-19 01:42:18 +000042static cl::opt<bool> StressSchedOpt(
Andrew Trick3013b6a2011-06-15 17:16:12 +000043 "stress-sched", cl::Hidden, cl::init(false),
44 cl::desc("Stress test instruction scheduling"));
45#endif
46
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000047void SchedulingPriorityQueue::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +000048
Dan Gohman619ef482009-01-15 19:20:50 +000049ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Eric Christopher33726202015-01-27 08:48:42 +000050 : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()),
51 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000052 MRI(mf.getRegInfo()) {
Andrew Trick3013b6a2011-06-15 17:16:12 +000053#ifndef NDEBUG
54 StressSched = StressSchedOpt;
55#endif
Dan Gohman60cb69e2008-11-19 23:18:57 +000056}
57
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000058ScheduleDAG::~ScheduleDAG() = default;
Dan Gohman60cb69e2008-11-19 23:18:57 +000059
Andrew Trick60cf03e2012-03-07 05:21:52 +000060void ScheduleDAG::clearDAG() {
61 SUnits.clear();
62 EntrySU = SUnit();
63 ExitSU = SUnit();
64}
65
Evan Cheng6cc775f2011-06-28 19:10:37 +000066const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Craig Topperc0196b12014-04-14 00:51:57 +000067 if (!Node || !Node->isMachineOpcode()) return nullptr;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000068 return &TII->get(Node->getMachineOpcode());
69}
70
Evandro Menezes14ba3d72017-07-12 15:30:59 +000071LLVM_DUMP_METHOD
72raw_ostream &SDep::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
73 switch (getKind()) {
74 case Data: OS << "Data"; break;
75 case Anti: OS << "Anti"; break;
76 case Output: OS << "Out "; break;
77 case Order: OS << "Ord "; break;
78 }
79
80 switch (getKind()) {
81 case Data:
82 OS << " Latency=" << getLatency();
83 if (TRI && isAssignedRegDep())
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +000084 OS << " Reg=" << printReg(getReg(), TRI);
Evandro Menezes14ba3d72017-07-12 15:30:59 +000085 break;
86 case Anti:
87 case Output:
88 OS << " Latency=" << getLatency();
89 break;
90 case Order:
91 OS << " Latency=" << getLatency();
92 switch(Contents.OrdKind) {
93 case Barrier: OS << " Barrier"; break;
94 case MayAliasMem:
95 case MustAliasMem: OS << " Memory"; break;
96 case Artificial: OS << " Artificial"; break;
97 case Weak: OS << " Weak"; break;
98 case Cluster: OS << " Cluster"; break;
99 }
100 break;
101 }
102
103 return OS;
104}
105
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000106bool SUnit::addPred(const SDep &D, bool Required) {
Alp Tokercb402912014-01-24 17:20:08 +0000107 // If this node already has this dependence, don't add a redundant one.
Matthias Braun9ab40392017-02-21 01:27:33 +0000108 for (SDep &PredDep : Preds) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000109 // Zero-latency weak edges may be added purely for heuristic ordering. Don't
110 // add them if another kind of edge already exists.
Matthias Braun9ab40392017-02-21 01:27:33 +0000111 if (!Required && PredDep.getSUnit() == D.getSUnit())
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000112 return false;
Matthias Braun9ab40392017-02-21 01:27:33 +0000113 if (PredDep.overlaps(D)) {
114 // Extend the latency if needed. Equivalent to
115 // removePred(PredDep) + addPred(D).
116 if (PredDep.getLatency() < D.getLatency()) {
117 SUnit *PredSU = PredDep.getSUnit();
Andrew Trick5b906452012-06-13 02:39:00 +0000118 // Find the corresponding successor in N.
Matthias Braun9ab40392017-02-21 01:27:33 +0000119 SDep ForwardD = PredDep;
Andrew Trick5b906452012-06-13 02:39:00 +0000120 ForwardD.setSUnit(this);
Matthias Braun9ab40392017-02-21 01:27:33 +0000121 for (SDep &SuccDep : PredSU->Succs) {
122 if (SuccDep == ForwardD) {
123 SuccDep.setLatency(D.getLatency());
Andrew Trick5b906452012-06-13 02:39:00 +0000124 break;
125 }
126 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000127 PredDep.setLatency(D.getLatency());
Andrew Trick5b906452012-06-13 02:39:00 +0000128 }
Andrew Trickd0548ae2011-02-04 03:18:17 +0000129 return false;
Andrew Trick5b906452012-06-13 02:39:00 +0000130 }
131 }
Dan Gohmane3a63512008-12-16 01:05:52 +0000132 // Now add a corresponding succ to N.
133 SDep P = D;
134 P.setSUnit(this);
135 SUnit *N = D.getSUnit();
Dan Gohmane3a63512008-12-16 01:05:52 +0000136 // Update the bookkeeping.
137 if (D.getKind() == SDep::Data) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000138 assert(NumPreds < std::numeric_limits<unsigned>::max() &&
139 "NumPreds will overflow!");
140 assert(N->NumSuccs < std::numeric_limits<unsigned>::max() &&
141 "NumSuccs will overflow!");
Dan Gohmane3a63512008-12-16 01:05:52 +0000142 ++NumPreds;
143 ++N->NumSuccs;
144 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000145 if (!N->isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000146 if (D.isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000147 ++WeakPredsLeft;
148 }
149 else {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000150 assert(NumPredsLeft < std::numeric_limits<unsigned>::max() &&
151 "NumPredsLeft will overflow!");
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000152 ++NumPredsLeft;
153 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000154 }
155 if (!isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000156 if (D.isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000157 ++N->WeakSuccsLeft;
158 }
159 else {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000160 assert(N->NumSuccsLeft < std::numeric_limits<unsigned>::max() &&
161 "NumSuccsLeft will overflow!");
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000162 ++N->NumSuccsLeft;
163 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000164 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000165 Preds.push_back(D);
Dan Gohman13141d52009-01-13 19:08:45 +0000166 N->Succs.push_back(P);
Dan Gohman52d4d822009-01-05 22:40:26 +0000167 if (P.getLatency() != 0) {
168 this->setDepthDirty();
169 N->setHeightDirty();
170 }
Andrew Trickd0548ae2011-02-04 03:18:17 +0000171 return true;
Dan Gohmane3a63512008-12-16 01:05:52 +0000172}
173
Dan Gohmane3a63512008-12-16 01:05:52 +0000174void SUnit::removePred(const SDep &D) {
175 // Find the matching predecessor.
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000176 SmallVectorImpl<SDep>::iterator I = llvm::find(Preds, D);
Matthias Braun9ab40392017-02-21 01:27:33 +0000177 if (I == Preds.end())
178 return;
179 // Find the corresponding successor in N.
180 SDep P = D;
181 P.setSUnit(this);
182 SUnit *N = D.getSUnit();
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000183 SmallVectorImpl<SDep>::iterator Succ = llvm::find(N->Succs, P);
Matthias Braun9ab40392017-02-21 01:27:33 +0000184 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
185 N->Succs.erase(Succ);
186 Preds.erase(I);
187 // Update the bookkeeping.
188 if (P.getKind() == SDep::Data) {
189 assert(NumPreds > 0 && "NumPreds will underflow!");
190 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
191 --NumPreds;
192 --N->NumSuccs;
193 }
194 if (!N->isScheduled) {
195 if (D.isWeak())
196 --WeakPredsLeft;
197 else {
198 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
199 --NumPredsLeft;
Dan Gohmane3a63512008-12-16 01:05:52 +0000200 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000201 }
202 if (!isScheduled) {
203 if (D.isWeak())
204 --N->WeakSuccsLeft;
205 else {
206 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
207 --N->NumSuccsLeft;
208 }
209 }
210 if (P.getLatency() != 0) {
211 this->setDepthDirty();
212 N->setHeightDirty();
213 }
Dan Gohmane3a63512008-12-16 01:05:52 +0000214}
215
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000216void SUnit::setDepthDirty() {
Dan Gohmana04542b2008-12-22 21:11:33 +0000217 if (!isDepthCurrent) return;
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000218 SmallVector<SUnit*, 8> WorkList;
219 WorkList.push_back(this);
Dan Gohmana04542b2008-12-22 21:11:33 +0000220 do {
Dan Gohman24fe9a12008-12-20 16:42:33 +0000221 SUnit *SU = WorkList.pop_back_val();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000222 SU->isDepthCurrent = false;
Matthias Braun9ab40392017-02-21 01:27:33 +0000223 for (SDep &SuccDep : SU->Succs) {
224 SUnit *SuccSU = SuccDep.getSUnit();
Dan Gohmana04542b2008-12-22 21:11:33 +0000225 if (SuccSU->isDepthCurrent)
226 WorkList.push_back(SuccSU);
227 }
228 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000229}
230
231void SUnit::setHeightDirty() {
Dan Gohmana04542b2008-12-22 21:11:33 +0000232 if (!isHeightCurrent) return;
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000233 SmallVector<SUnit*, 8> WorkList;
234 WorkList.push_back(this);
Dan Gohmana04542b2008-12-22 21:11:33 +0000235 do {
Dan Gohman24fe9a12008-12-20 16:42:33 +0000236 SUnit *SU = WorkList.pop_back_val();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000237 SU->isHeightCurrent = false;
Matthias Braun9ab40392017-02-21 01:27:33 +0000238 for (SDep &PredDep : SU->Preds) {
239 SUnit *PredSU = PredDep.getSUnit();
Dan Gohmana04542b2008-12-22 21:11:33 +0000240 if (PredSU->isHeightCurrent)
241 WorkList.push_back(PredSU);
242 }
243 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000244}
245
David Goodwin80a03cc2009-11-20 19:32:48 +0000246void SUnit::setDepthToAtLeast(unsigned NewDepth) {
247 if (NewDepth <= getDepth())
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000248 return;
249 setDepthDirty();
250 Depth = NewDepth;
251 isDepthCurrent = true;
252}
253
David Goodwin80a03cc2009-11-20 19:32:48 +0000254void SUnit::setHeightToAtLeast(unsigned NewHeight) {
255 if (NewHeight <= getHeight())
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000256 return;
257 setHeightDirty();
258 Height = NewHeight;
259 isHeightCurrent = true;
260}
261
Matthias Braun9ab40392017-02-21 01:27:33 +0000262/// Calculates the maximal path from the node to the exit.
David Goodwin80a03cc2009-11-20 19:32:48 +0000263void SUnit::ComputeDepth() {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000264 SmallVector<SUnit*, 8> WorkList;
265 WorkList.push_back(this);
Dan Gohman3a572132008-12-23 17:22:32 +0000266 do {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000267 SUnit *Cur = WorkList.back();
268
269 bool Done = true;
270 unsigned MaxPredDepth = 0;
Matthias Braun9ab40392017-02-21 01:27:33 +0000271 for (const SDep &PredDep : Cur->Preds) {
272 SUnit *PredSU = PredDep.getSUnit();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000273 if (PredSU->isDepthCurrent)
274 MaxPredDepth = std::max(MaxPredDepth,
Matthias Braun9ab40392017-02-21 01:27:33 +0000275 PredSU->Depth + PredDep.getLatency());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000276 else {
277 Done = false;
278 WorkList.push_back(PredSU);
279 }
280 }
281
282 if (Done) {
283 WorkList.pop_back();
284 if (MaxPredDepth != Cur->Depth) {
285 Cur->setDepthDirty();
286 Cur->Depth = MaxPredDepth;
287 }
288 Cur->isDepthCurrent = true;
289 }
Dan Gohman3a572132008-12-23 17:22:32 +0000290 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000291}
292
Matthias Braun9ab40392017-02-21 01:27:33 +0000293/// Calculates the maximal path from the node to the entry.
David Goodwin80a03cc2009-11-20 19:32:48 +0000294void SUnit::ComputeHeight() {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000295 SmallVector<SUnit*, 8> WorkList;
296 WorkList.push_back(this);
Dan Gohman3a572132008-12-23 17:22:32 +0000297 do {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000298 SUnit *Cur = WorkList.back();
299
300 bool Done = true;
301 unsigned MaxSuccHeight = 0;
Matthias Braun9ab40392017-02-21 01:27:33 +0000302 for (const SDep &SuccDep : Cur->Succs) {
303 SUnit *SuccSU = SuccDep.getSUnit();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000304 if (SuccSU->isHeightCurrent)
305 MaxSuccHeight = std::max(MaxSuccHeight,
Matthias Braun9ab40392017-02-21 01:27:33 +0000306 SuccSU->Height + SuccDep.getLatency());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000307 else {
308 Done = false;
309 WorkList.push_back(SuccSU);
310 }
311 }
312
313 if (Done) {
314 WorkList.pop_back();
315 if (MaxSuccHeight != Cur->Height) {
316 Cur->setHeightDirty();
317 Cur->Height = MaxSuccHeight;
318 }
319 Cur->isHeightCurrent = true;
320 }
Dan Gohman3a572132008-12-23 17:22:32 +0000321 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000322}
323
Andrew Trickd3b86292013-01-24 02:09:55 +0000324void SUnit::biasCriticalPath() {
325 if (NumPreds < 2)
326 return;
327
328 SUnit::pred_iterator BestI = Preds.begin();
329 unsigned MaxDepth = BestI->getSUnit()->getDepth();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000330 for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
331 ++I) {
Andrew Trickd3b86292013-01-24 02:09:55 +0000332 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
333 BestI = I;
334 }
335 if (BestI != Preds.begin())
336 std::swap(*Preds.begin(), *BestI);
337}
338
Aaron Ballman615eb472017-10-15 14:32:27 +0000339#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000340LLVM_DUMP_METHOD
Evandro Menezes14ba3d72017-07-12 15:30:59 +0000341raw_ostream &SUnit::print(raw_ostream &OS,
342 const SUnit *Entry, const SUnit *Exit) const {
343 if (this == Entry)
Evandro Menezes063259d2017-01-10 01:08:01 +0000344 OS << "EntrySU";
Evandro Menezes14ba3d72017-07-12 15:30:59 +0000345 else if (this == Exit)
346 OS << "ExitSU";
Matthias Braun636a5972016-11-11 22:37:26 +0000347 else
Evandro Menezes063259d2017-01-10 01:08:01 +0000348 OS << "SU(" << NodeNum << ")";
Evandro Menezes14ba3d72017-07-12 15:30:59 +0000349 return OS;
Matthias Braun636a5972016-11-11 22:37:26 +0000350}
351
Evandro Menezes14ba3d72017-07-12 15:30:59 +0000352LLVM_DUMP_METHOD
353raw_ostream &SUnit::print(raw_ostream &OS, const ScheduleDAG *G) const {
354 return print(OS, &G->EntrySU, &G->ExitSU);
355}
356
357LLVM_DUMP_METHOD
358void SUnit::dump(const ScheduleDAG *G) const {
Evandro Menezes063259d2017-01-10 01:08:01 +0000359 print(dbgs(), G);
Matthias Braun636a5972016-11-11 22:37:26 +0000360 dbgs() << ": ";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000361 G->dumpNode(this);
362}
363
Matthias Braun8c209aa2017-01-28 02:02:38 +0000364LLVM_DUMP_METHOD void SUnit::dumpAll(const ScheduleDAG *G) const {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000365 dump(G);
366
David Greene94745d02010-01-05 01:25:41 +0000367 dbgs() << " # preds left : " << NumPredsLeft << "\n";
368 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000369 if (WeakPredsLeft)
370 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
371 if (WeakSuccsLeft)
372 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trickd0548ae2011-02-04 03:18:17 +0000373 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene94745d02010-01-05 01:25:41 +0000374 dbgs() << " Latency : " << Latency << "\n";
Andrew Trick2a8edef2013-03-01 00:19:09 +0000375 dbgs() << " Depth : " << getDepth() << "\n";
376 dbgs() << " Height : " << getHeight() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000377
378 if (Preds.size() != 0) {
David Greene94745d02010-01-05 01:25:41 +0000379 dbgs() << " Predecessors:\n";
Evandro Menezes14ba3d72017-07-12 15:30:59 +0000380 for (const SDep &Dep : Preds) {
381 dbgs() << " ";
382 Dep.getSUnit()->print(dbgs(), G); dbgs() << ": ";
383 Dep.print(dbgs(), G->TRI); dbgs() << '\n';
Dan Gohman60cb69e2008-11-19 23:18:57 +0000384 }
385 }
386 if (Succs.size() != 0) {
David Greene94745d02010-01-05 01:25:41 +0000387 dbgs() << " Successors:\n";
Evandro Menezes14ba3d72017-07-12 15:30:59 +0000388 for (const SDep &Dep : Succs) {
389 dbgs() << " ";
390 Dep.getSUnit()->print(dbgs(), G); dbgs() << ": ";
391 Dep.print(dbgs(), G->TRI); dbgs() << '\n';
Dan Gohman60cb69e2008-11-19 23:18:57 +0000392 }
393 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000394}
Manman Ren742534c2012-09-06 19:06:06 +0000395#endif
Dan Gohman4ce15e12008-11-20 01:26:25 +0000396
397#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000398unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000399 bool AnyNotSched = false;
400 unsigned DeadNodes = 0;
Matthias Braun9ab40392017-02-21 01:27:33 +0000401 for (const SUnit &SUnit : SUnits) {
402 if (!SUnit.isScheduled) {
403 if (SUnit.NumPreds == 0 && SUnit.NumSuccs == 0) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000404 ++DeadNodes;
405 continue;
406 }
407 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000408 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000409 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000410 dbgs() << "has not been scheduled!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000411 AnyNotSched = true;
412 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000413 if (SUnit.isScheduled &&
414 (isBottomUp ? SUnit.getHeight() : SUnit.getDepth()) >
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000415 unsigned(std::numeric_limits<int>::max())) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000416 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000417 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000418 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000419 dbgs() << "has an unexpected "
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000420 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000421 AnyNotSched = true;
422 }
423 if (isBottomUp) {
Matthias Braun9ab40392017-02-21 01:27:33 +0000424 if (SUnit.NumSuccsLeft != 0) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000425 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000426 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000427 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000428 dbgs() << "has successors left!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000429 AnyNotSched = true;
430 }
431 } else {
Matthias Braun9ab40392017-02-21 01:27:33 +0000432 if (SUnit.NumPredsLeft != 0) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000433 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000434 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000435 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000436 dbgs() << "has predecessors left!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000437 AnyNotSched = true;
438 }
439 }
440 }
Dan Gohman4ce15e12008-11-20 01:26:25 +0000441 assert(!AnyNotSched);
Andrew Trick46a58662012-03-07 05:21:36 +0000442 return SUnits.size() - DeadNodes;
Dan Gohman4ce15e12008-11-20 01:26:25 +0000443}
444#endif
Dan Gohmanad2134d2008-11-25 00:52:40 +0000445
Dan Gohmanad2134d2008-11-25 00:52:40 +0000446void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
Matthias Braun9ab40392017-02-21 01:27:33 +0000447 // The idea of the algorithm is taken from
448 // "Online algorithms for managing the topological order of
449 // a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
450 // This is the MNR algorithm, which was first introduced by
451 // A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
452 // "Maintaining a topological order under edge insertions".
453 //
454 // Short description of the algorithm:
455 //
456 // Topological ordering, ord, of a DAG maps each node to a topological
457 // index so that for all edges X->Y it is the case that ord(X) < ord(Y).
458 //
459 // This means that if there is a path from the node X to the node Z,
460 // then ord(X) < ord(Z).
461 //
462 // This property can be used to check for reachability of nodes:
463 // if Z is reachable from X, then an insertion of the edge Z->X would
464 // create a cycle.
465 //
466 // The algorithm first computes a topological ordering for the DAG by
467 // initializing the Index2Node and Node2Index arrays and then tries to keep
468 // the ordering up-to-date after edge insertions by reordering the DAG.
469 //
470 // On insertion of the edge X->Y, the algorithm first marks by calling DFS
471 // the nodes reachable from Y, and then shifts them using Shift to lie
472 // immediately after X in Index2Node.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000473 unsigned DAGSize = SUnits.size();
474 std::vector<SUnit*> WorkList;
475 WorkList.reserve(DAGSize);
476
477 Index2Node.resize(DAGSize);
478 Node2Index.resize(DAGSize);
479
480 // Initialize the data structures.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000481 if (ExitSU)
482 WorkList.push_back(ExitSU);
Matthias Braun9ab40392017-02-21 01:27:33 +0000483 for (SUnit &SU : SUnits) {
484 int NodeNum = SU.NodeNum;
485 unsigned Degree = SU.Succs.size();
Dan Gohmanad2134d2008-11-25 00:52:40 +0000486 // Temporarily use the Node2Index array as scratch space for degree counts.
487 Node2Index[NodeNum] = Degree;
488
489 // Is it a node without dependencies?
490 if (Degree == 0) {
Matthias Braun9ab40392017-02-21 01:27:33 +0000491 assert(SU.Succs.empty() && "SUnit should have no successors");
Dan Gohmanad2134d2008-11-25 00:52:40 +0000492 // Collect leaf nodes.
Matthias Braun9ab40392017-02-21 01:27:33 +0000493 WorkList.push_back(&SU);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000494 }
John Mosby53646552010-06-30 03:40:54 +0000495 }
Dan Gohmanad2134d2008-11-25 00:52:40 +0000496
497 int Id = DAGSize;
498 while (!WorkList.empty()) {
499 SUnit *SU = WorkList.back();
500 WorkList.pop_back();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000501 if (SU->NodeNum < DAGSize)
502 Allocate(SU->NodeNum, --Id);
Matthias Braun9ab40392017-02-21 01:27:33 +0000503 for (const SDep &PredDep : SU->Preds) {
504 SUnit *SU = PredDep.getSUnit();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000505 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohmanad2134d2008-11-25 00:52:40 +0000506 // If all dependencies of the node are processed already,
507 // then the node can be computed now.
508 WorkList.push_back(SU);
509 }
510 }
511
512 Visited.resize(DAGSize);
513
514#ifndef NDEBUG
515 // Check correctness of the ordering
Matthias Braun9ab40392017-02-21 01:27:33 +0000516 for (SUnit &SU : SUnits) {
517 for (const SDep &PD : SU.Preds) {
518 assert(Node2Index[SU.NodeNum] > Node2Index[PD.getSUnit()->NodeNum] &&
Dan Gohmanad2134d2008-11-25 00:52:40 +0000519 "Wrong topological sorting");
520 }
521 }
522#endif
523}
524
Dan Gohmanad2134d2008-11-25 00:52:40 +0000525void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
526 int UpperBound, LowerBound;
527 LowerBound = Node2Index[Y->NodeNum];
528 UpperBound = Node2Index[X->NodeNum];
529 bool HasLoop = false;
530 // Is Ord(X) < Ord(Y) ?
531 if (LowerBound < UpperBound) {
532 // Update the topological order.
533 Visited.reset();
534 DFS(Y, UpperBound, HasLoop);
535 assert(!HasLoop && "Inserted edge creates a loop!");
536 // Recompute topological indexes.
537 Shift(Visited, LowerBound, UpperBound);
538 }
539}
540
Dan Gohmanad2134d2008-11-25 00:52:40 +0000541void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
542 // InitDAGTopologicalSorting();
543}
544
Dan Gohman7d329742008-12-09 16:37:48 +0000545void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattnered69c6e2010-12-20 00:50:16 +0000546 bool &HasLoop) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000547 std::vector<const SUnit*> WorkList;
John Mosby53646552010-06-30 03:40:54 +0000548 WorkList.reserve(SUnits.size());
Dan Gohmanad2134d2008-11-25 00:52:40 +0000549
550 WorkList.push_back(SU);
Dan Gohman3a572132008-12-23 17:22:32 +0000551 do {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000552 SU = WorkList.back();
553 WorkList.pop_back();
554 Visited.set(SU->NodeNum);
Matthias Braun9ab40392017-02-21 01:27:33 +0000555 for (const SDep &SuccDep
556 : make_range(SU->Succs.rbegin(), SU->Succs.rend())) {
557 unsigned s = SuccDep.getSUnit()->NodeNum;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000558 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
559 if (s >= Node2Index.size())
560 continue;
Dan Gohmanad2134d2008-11-25 00:52:40 +0000561 if (Node2Index[s] == UpperBound) {
John Mosby53646552010-06-30 03:40:54 +0000562 HasLoop = true;
Dan Gohmanad2134d2008-11-25 00:52:40 +0000563 return;
564 }
565 // Visit successors if not already and in affected region.
566 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Matthias Braun9ab40392017-02-21 01:27:33 +0000567 WorkList.push_back(SuccDep.getSUnit());
John Mosby53646552010-06-30 03:40:54 +0000568 }
569 }
Dan Gohman3a572132008-12-23 17:22:32 +0000570 } while (!WorkList.empty());
Dan Gohmanad2134d2008-11-25 00:52:40 +0000571}
572
Valery Pykhtin910da132017-03-28 05:12:31 +0000573std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU,
574 const SUnit &TargetSU,
575 bool &Success) {
576 std::vector<const SUnit*> WorkList;
577 int LowerBound = Node2Index[StartSU.NodeNum];
578 int UpperBound = Node2Index[TargetSU.NodeNum];
579 bool Found = false;
580 BitVector VisitedBack;
581 std::vector<int> Nodes;
582
583 if (LowerBound > UpperBound) {
584 Success = false;
585 return Nodes;
586 }
587
588 WorkList.reserve(SUnits.size());
589 Visited.reset();
590
591 // Starting from StartSU, visit all successors up
592 // to UpperBound.
593 WorkList.push_back(&StartSU);
594 do {
595 const SUnit *SU = WorkList.back();
596 WorkList.pop_back();
597 for (int I = SU->Succs.size()-1; I >= 0; --I) {
598 const SUnit *Succ = SU->Succs[I].getSUnit();
599 unsigned s = Succ->NodeNum;
600 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
601 if (Succ->isBoundaryNode())
602 continue;
603 if (Node2Index[s] == UpperBound) {
604 Found = true;
605 continue;
606 }
607 // Visit successors if not already and in affected region.
608 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
609 Visited.set(s);
610 WorkList.push_back(Succ);
611 }
612 }
613 } while (!WorkList.empty());
614
615 if (!Found) {
616 Success = false;
617 return Nodes;
618 }
619
620 WorkList.clear();
621 VisitedBack.resize(SUnits.size());
622 Found = false;
623
624 // Starting from TargetSU, visit all predecessors up
625 // to LowerBound. SUs that are visited by the two
626 // passes are added to Nodes.
627 WorkList.push_back(&TargetSU);
628 do {
629 const SUnit *SU = WorkList.back();
630 WorkList.pop_back();
631 for (int I = SU->Preds.size()-1; I >= 0; --I) {
632 const SUnit *Pred = SU->Preds[I].getSUnit();
633 unsigned s = Pred->NodeNum;
634 // Edges to non-SUnits are allowed but ignored (e.g. EntrySU).
635 if (Pred->isBoundaryNode())
636 continue;
637 if (Node2Index[s] == LowerBound) {
638 Found = true;
639 continue;
640 }
641 if (!VisitedBack.test(s) && Visited.test(s)) {
642 VisitedBack.set(s);
643 WorkList.push_back(Pred);
644 Nodes.push_back(s);
645 }
646 }
647 } while (!WorkList.empty());
648
649 assert(Found && "Error in SUnit Graph!");
650 Success = true;
651 return Nodes;
652}
653
John Mosby53646552010-06-30 03:40:54 +0000654void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohman7d329742008-12-09 16:37:48 +0000655 int UpperBound) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000656 std::vector<int> L;
657 int shift = 0;
658 int i;
659
660 for (i = LowerBound; i <= UpperBound; ++i) {
661 // w is node at topological index i.
662 int w = Index2Node[i];
663 if (Visited.test(w)) {
664 // Unmark.
665 Visited.reset(w);
666 L.push_back(w);
667 shift = shift + 1;
668 } else {
669 Allocate(w, i - shift);
670 }
671 }
672
Matthias Braun9ab40392017-02-21 01:27:33 +0000673 for (unsigned LI : L) {
674 Allocate(LI, i - shift);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000675 i = i + 1;
676 }
677}
678
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000679bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
680 // Is SU reachable from TargetSU via successor edges?
681 if (IsReachable(SU, TargetSU))
Dan Gohmanad2134d2008-11-25 00:52:40 +0000682 return true;
Matthias Braun9ab40392017-02-21 01:27:33 +0000683 for (const SDep &PredDep : TargetSU->Preds)
684 if (PredDep.isAssignedRegDep() &&
685 IsReachable(SU, PredDep.getSUnit()))
Dan Gohmanad2134d2008-11-25 00:52:40 +0000686 return true;
687 return false;
688}
689
Dan Gohman7d329742008-12-09 16:37:48 +0000690bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
691 const SUnit *TargetSU) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000692 // If insertion of the edge SU->TargetSU would create a cycle
693 // then there is a path from TargetSU to SU.
694 int UpperBound, LowerBound;
695 LowerBound = Node2Index[TargetSU->NodeNum];
696 UpperBound = Node2Index[SU->NodeNum];
697 bool HasLoop = false;
698 // Is Ord(TargetSU) < Ord(SU) ?
699 if (LowerBound < UpperBound) {
700 Visited.reset();
John Mosby53646552010-06-30 03:40:54 +0000701 // There may be a path from TargetSU to SU. Check for it.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000702 DFS(TargetSU, UpperBound, HasLoop);
703 }
704 return HasLoop;
705}
706
Dan Gohmanad2134d2008-11-25 00:52:40 +0000707void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
708 Node2Index[n] = index;
709 Index2Node[index] = n;
710}
711
John Mosby53646552010-06-30 03:40:54 +0000712ScheduleDAGTopologicalSort::
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000713ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
714 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohman7e105f02009-01-15 22:18:12 +0000715
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000716ScheduleHazardRecognizer::~ScheduleHazardRecognizer() = default;