blob: dc72ac07325882f623ea5cb0dc17655d31a4c674 [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
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000015#include "llvm/ADT/iterator_range.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000019#include "llvm/CodeGen/ScheduleDAG.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"
Andrew Trick3013b6a2011-06-15 17:16:12 +000022#include "llvm/Support/CommandLine.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000023#include "llvm/Support/Compiler.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000024#include "llvm/Support/Debug.h"
Daniel Dunbar8ef07352009-07-24 09:53:24 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000028#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000029#include <algorithm>
30#include <cassert>
31#include <iterator>
32#include <limits>
33#include <utility>
34#include <vector>
35
Dan Gohman60cb69e2008-11-19 23:18:57 +000036using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "pre-RA-sched"
39
Andrew Trick3013b6a2011-06-15 17:16:12 +000040#ifndef NDEBUG
Benjamin Kramer4938edb2011-08-19 01:42:18 +000041static cl::opt<bool> StressSchedOpt(
Andrew Trick3013b6a2011-06-15 17:16:12 +000042 "stress-sched", cl::Hidden, cl::init(false),
43 cl::desc("Stress test instruction scheduling"));
44#endif
45
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000046void SchedulingPriorityQueue::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +000047
Dan Gohman619ef482009-01-15 19:20:50 +000048ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Eric Christopher33726202015-01-27 08:48:42 +000049 : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()),
50 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000051 MRI(mf.getRegInfo()) {
Andrew Trick3013b6a2011-06-15 17:16:12 +000052#ifndef NDEBUG
53 StressSched = StressSchedOpt;
54#endif
Dan Gohman60cb69e2008-11-19 23:18:57 +000055}
56
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +000057ScheduleDAG::~ScheduleDAG() = default;
Dan Gohman60cb69e2008-11-19 23:18:57 +000058
Andrew Trick60cf03e2012-03-07 05:21:52 +000059void ScheduleDAG::clearDAG() {
60 SUnits.clear();
61 EntrySU = SUnit();
62 ExitSU = SUnit();
63}
64
Evan Cheng6cc775f2011-06-28 19:10:37 +000065const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Craig Topperc0196b12014-04-14 00:51:57 +000066 if (!Node || !Node->isMachineOpcode()) return nullptr;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000067 return &TII->get(Node->getMachineOpcode());
68}
69
Andrew Trickf1ff84c2012-11-12 19:28:57 +000070bool SUnit::addPred(const SDep &D, bool Required) {
Alp Tokercb402912014-01-24 17:20:08 +000071 // If this node already has this dependence, don't add a redundant one.
Matthias Braun9ab40392017-02-21 01:27:33 +000072 for (SDep &PredDep : Preds) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +000073 // Zero-latency weak edges may be added purely for heuristic ordering. Don't
74 // add them if another kind of edge already exists.
Matthias Braun9ab40392017-02-21 01:27:33 +000075 if (!Required && PredDep.getSUnit() == D.getSUnit())
Andrew Trickf1ff84c2012-11-12 19:28:57 +000076 return false;
Matthias Braun9ab40392017-02-21 01:27:33 +000077 if (PredDep.overlaps(D)) {
78 // Extend the latency if needed. Equivalent to
79 // removePred(PredDep) + addPred(D).
80 if (PredDep.getLatency() < D.getLatency()) {
81 SUnit *PredSU = PredDep.getSUnit();
Andrew Trick5b906452012-06-13 02:39:00 +000082 // Find the corresponding successor in N.
Matthias Braun9ab40392017-02-21 01:27:33 +000083 SDep ForwardD = PredDep;
Andrew Trick5b906452012-06-13 02:39:00 +000084 ForwardD.setSUnit(this);
Matthias Braun9ab40392017-02-21 01:27:33 +000085 for (SDep &SuccDep : PredSU->Succs) {
86 if (SuccDep == ForwardD) {
87 SuccDep.setLatency(D.getLatency());
Andrew Trick5b906452012-06-13 02:39:00 +000088 break;
89 }
90 }
Matthias Braun9ab40392017-02-21 01:27:33 +000091 PredDep.setLatency(D.getLatency());
Andrew Trick5b906452012-06-13 02:39:00 +000092 }
Andrew Trickd0548ae2011-02-04 03:18:17 +000093 return false;
Andrew Trick5b906452012-06-13 02:39:00 +000094 }
95 }
Dan Gohmane3a63512008-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 Gohmane3a63512008-12-16 01:05:52 +0000100 // Update the bookkeeping.
101 if (D.getKind() == SDep::Data) {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000102 assert(NumPreds < std::numeric_limits<unsigned>::max() &&
103 "NumPreds will overflow!");
104 assert(N->NumSuccs < std::numeric_limits<unsigned>::max() &&
105 "NumSuccs will overflow!");
Dan Gohmane3a63512008-12-16 01:05:52 +0000106 ++NumPreds;
107 ++N->NumSuccs;
108 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000109 if (!N->isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000110 if (D.isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000111 ++WeakPredsLeft;
112 }
113 else {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000114 assert(NumPredsLeft < std::numeric_limits<unsigned>::max() &&
115 "NumPredsLeft will overflow!");
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000116 ++NumPredsLeft;
117 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000118 }
119 if (!isScheduled) {
Andrew Trick4b1f9e32012-11-13 02:35:06 +0000120 if (D.isWeak()) {
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000121 ++N->WeakSuccsLeft;
122 }
123 else {
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000124 assert(N->NumSuccsLeft < std::numeric_limits<unsigned>::max() &&
125 "NumSuccsLeft will overflow!");
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000126 ++N->NumSuccsLeft;
127 }
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000128 }
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000129 Preds.push_back(D);
Dan Gohman13141d52009-01-13 19:08:45 +0000130 N->Succs.push_back(P);
Dan Gohman52d4d822009-01-05 22:40:26 +0000131 if (P.getLatency() != 0) {
132 this->setDepthDirty();
133 N->setHeightDirty();
134 }
Andrew Trickd0548ae2011-02-04 03:18:17 +0000135 return true;
Dan Gohmane3a63512008-12-16 01:05:52 +0000136}
137
Dan Gohmane3a63512008-12-16 01:05:52 +0000138void SUnit::removePred(const SDep &D) {
139 // Find the matching predecessor.
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000140 SmallVectorImpl<SDep>::iterator I = llvm::find(Preds, D);
Matthias Braun9ab40392017-02-21 01:27:33 +0000141 if (I == Preds.end())
142 return;
143 // Find the corresponding successor in N.
144 SDep P = D;
145 P.setSUnit(this);
146 SUnit *N = D.getSUnit();
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000147 SmallVectorImpl<SDep>::iterator Succ = llvm::find(N->Succs, P);
Matthias Braun9ab40392017-02-21 01:27:33 +0000148 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
149 N->Succs.erase(Succ);
150 Preds.erase(I);
151 // Update the bookkeeping.
152 if (P.getKind() == SDep::Data) {
153 assert(NumPreds > 0 && "NumPreds will underflow!");
154 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
155 --NumPreds;
156 --N->NumSuccs;
157 }
158 if (!N->isScheduled) {
159 if (D.isWeak())
160 --WeakPredsLeft;
161 else {
162 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
163 --NumPredsLeft;
Dan Gohmane3a63512008-12-16 01:05:52 +0000164 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000165 }
166 if (!isScheduled) {
167 if (D.isWeak())
168 --N->WeakSuccsLeft;
169 else {
170 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
171 --N->NumSuccsLeft;
172 }
173 }
174 if (P.getLatency() != 0) {
175 this->setDepthDirty();
176 N->setHeightDirty();
177 }
Dan Gohmane3a63512008-12-16 01:05:52 +0000178}
179
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000180void SUnit::setDepthDirty() {
Dan Gohmana04542b2008-12-22 21:11:33 +0000181 if (!isDepthCurrent) return;
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000182 SmallVector<SUnit*, 8> WorkList;
183 WorkList.push_back(this);
Dan Gohmana04542b2008-12-22 21:11:33 +0000184 do {
Dan Gohman24fe9a12008-12-20 16:42:33 +0000185 SUnit *SU = WorkList.pop_back_val();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000186 SU->isDepthCurrent = false;
Matthias Braun9ab40392017-02-21 01:27:33 +0000187 for (SDep &SuccDep : SU->Succs) {
188 SUnit *SuccSU = SuccDep.getSUnit();
Dan Gohmana04542b2008-12-22 21:11:33 +0000189 if (SuccSU->isDepthCurrent)
190 WorkList.push_back(SuccSU);
191 }
192 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000193}
194
195void SUnit::setHeightDirty() {
Dan Gohmana04542b2008-12-22 21:11:33 +0000196 if (!isHeightCurrent) return;
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000197 SmallVector<SUnit*, 8> WorkList;
198 WorkList.push_back(this);
Dan Gohmana04542b2008-12-22 21:11:33 +0000199 do {
Dan Gohman24fe9a12008-12-20 16:42:33 +0000200 SUnit *SU = WorkList.pop_back_val();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000201 SU->isHeightCurrent = false;
Matthias Braun9ab40392017-02-21 01:27:33 +0000202 for (SDep &PredDep : SU->Preds) {
203 SUnit *PredSU = PredDep.getSUnit();
Dan Gohmana04542b2008-12-22 21:11:33 +0000204 if (PredSU->isHeightCurrent)
205 WorkList.push_back(PredSU);
206 }
207 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000208}
209
David Goodwin80a03cc2009-11-20 19:32:48 +0000210void SUnit::setDepthToAtLeast(unsigned NewDepth) {
211 if (NewDepth <= getDepth())
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000212 return;
213 setDepthDirty();
214 Depth = NewDepth;
215 isDepthCurrent = true;
216}
217
David Goodwin80a03cc2009-11-20 19:32:48 +0000218void SUnit::setHeightToAtLeast(unsigned NewHeight) {
219 if (NewHeight <= getHeight())
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000220 return;
221 setHeightDirty();
222 Height = NewHeight;
223 isHeightCurrent = true;
224}
225
Matthias Braun9ab40392017-02-21 01:27:33 +0000226/// Calculates the maximal path from the node to the exit.
David Goodwin80a03cc2009-11-20 19:32:48 +0000227void SUnit::ComputeDepth() {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000228 SmallVector<SUnit*, 8> WorkList;
229 WorkList.push_back(this);
Dan Gohman3a572132008-12-23 17:22:32 +0000230 do {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000231 SUnit *Cur = WorkList.back();
232
233 bool Done = true;
234 unsigned MaxPredDepth = 0;
Matthias Braun9ab40392017-02-21 01:27:33 +0000235 for (const SDep &PredDep : Cur->Preds) {
236 SUnit *PredSU = PredDep.getSUnit();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000237 if (PredSU->isDepthCurrent)
238 MaxPredDepth = std::max(MaxPredDepth,
Matthias Braun9ab40392017-02-21 01:27:33 +0000239 PredSU->Depth + PredDep.getLatency());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000240 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 Gohman3a572132008-12-23 17:22:32 +0000254 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000255}
256
Matthias Braun9ab40392017-02-21 01:27:33 +0000257/// Calculates the maximal path from the node to the entry.
David Goodwin80a03cc2009-11-20 19:32:48 +0000258void SUnit::ComputeHeight() {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000259 SmallVector<SUnit*, 8> WorkList;
260 WorkList.push_back(this);
Dan Gohman3a572132008-12-23 17:22:32 +0000261 do {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000262 SUnit *Cur = WorkList.back();
263
264 bool Done = true;
265 unsigned MaxSuccHeight = 0;
Matthias Braun9ab40392017-02-21 01:27:33 +0000266 for (const SDep &SuccDep : Cur->Succs) {
267 SUnit *SuccSU = SuccDep.getSUnit();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000268 if (SuccSU->isHeightCurrent)
269 MaxSuccHeight = std::max(MaxSuccHeight,
Matthias Braun9ab40392017-02-21 01:27:33 +0000270 SuccSU->Height + SuccDep.getLatency());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000271 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 Gohman3a572132008-12-23 17:22:32 +0000285 } while (!WorkList.empty());
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000286}
287
Andrew Trickd3b86292013-01-24 02:09:55 +0000288void SUnit::biasCriticalPath() {
289 if (NumPreds < 2)
290 return;
291
292 SUnit::pred_iterator BestI = Preds.begin();
293 unsigned MaxDepth = BestI->getSUnit()->getDepth();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000294 for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
295 ++I) {
Andrew Trickd3b86292013-01-24 02:09:55 +0000296 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
297 BestI = I;
298 }
299 if (BestI != Preds.begin())
300 std::swap(*Preds.begin(), *BestI);
301}
302
Manman Ren19f49ac2012-09-11 22:23:19 +0000303#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000304LLVM_DUMP_METHOD
Evandro Menezes063259d2017-01-10 01:08:01 +0000305void SUnit::print(raw_ostream &OS, const ScheduleDAG *DAG) const {
306 if (this == &DAG->ExitSU)
307 OS << "ExitSU";
308 else if (this == &DAG->EntrySU)
309 OS << "EntrySU";
Matthias Braun636a5972016-11-11 22:37:26 +0000310 else
Evandro Menezes063259d2017-01-10 01:08:01 +0000311 OS << "SU(" << NodeNum << ")";
Matthias Braun636a5972016-11-11 22:37:26 +0000312}
313
Matthias Braun8c209aa2017-01-28 02:02:38 +0000314LLVM_DUMP_METHOD void SUnit::dump(const ScheduleDAG *G) const {
Evandro Menezes063259d2017-01-10 01:08:01 +0000315 print(dbgs(), G);
Matthias Braun636a5972016-11-11 22:37:26 +0000316 dbgs() << ": ";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000317 G->dumpNode(this);
318}
319
Matthias Braun8c209aa2017-01-28 02:02:38 +0000320LLVM_DUMP_METHOD void SUnit::dumpAll(const ScheduleDAG *G) const {
Dan Gohman60cb69e2008-11-19 23:18:57 +0000321 dump(G);
322
David Greene94745d02010-01-05 01:25:41 +0000323 dbgs() << " # preds left : " << NumPredsLeft << "\n";
324 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000325 if (WeakPredsLeft)
326 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
327 if (WeakSuccsLeft)
328 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trickd0548ae2011-02-04 03:18:17 +0000329 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene94745d02010-01-05 01:25:41 +0000330 dbgs() << " Latency : " << Latency << "\n";
Andrew Trick2a8edef2013-03-01 00:19:09 +0000331 dbgs() << " Depth : " << getDepth() << "\n";
332 dbgs() << " Height : " << getHeight() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000333
334 if (Preds.size() != 0) {
David Greene94745d02010-01-05 01:25:41 +0000335 dbgs() << " Predecessors:\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000336 for (const SDep &SuccDep : Preds) {
David Greene94745d02010-01-05 01:25:41 +0000337 dbgs() << " ";
Matthias Braun9ab40392017-02-21 01:27:33 +0000338 switch (SuccDep.getKind()) {
Matthias Braun1acb55e2016-09-23 18:28:31 +0000339 case SDep::Data: dbgs() << "data "; break;
340 case SDep::Anti: dbgs() << "anti "; break;
341 case SDep::Output: dbgs() << "out "; break;
342 case SDep::Order: dbgs() << "ord "; break;
Dan Gohman2d170892008-12-09 22:54:47 +0000343 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000344 SuccDep.getSUnit()->print(dbgs(), G);
345 if (SuccDep.isArtificial())
David Greene94745d02010-01-05 01:25:41 +0000346 dbgs() << " *";
Matthias Braun9ab40392017-02-21 01:27:33 +0000347 dbgs() << ": Latency=" << SuccDep.getLatency();
348 if (SuccDep.isAssignedRegDep())
349 dbgs() << " Reg=" << PrintReg(SuccDep.getReg(), G->TRI);
David Greene94745d02010-01-05 01:25:41 +0000350 dbgs() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000351 }
352 }
353 if (Succs.size() != 0) {
David Greene94745d02010-01-05 01:25:41 +0000354 dbgs() << " Successors:\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000355 for (const SDep &SuccDep : Succs) {
David Greene94745d02010-01-05 01:25:41 +0000356 dbgs() << " ";
Matthias Braun9ab40392017-02-21 01:27:33 +0000357 switch (SuccDep.getKind()) {
Matthias Braun1acb55e2016-09-23 18:28:31 +0000358 case SDep::Data: dbgs() << "data "; break;
359 case SDep::Anti: dbgs() << "anti "; break;
360 case SDep::Output: dbgs() << "out "; break;
361 case SDep::Order: dbgs() << "ord "; break;
Dan Gohman2d170892008-12-09 22:54:47 +0000362 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000363 SuccDep.getSUnit()->print(dbgs(), G);
364 if (SuccDep.isArtificial())
David Greene94745d02010-01-05 01:25:41 +0000365 dbgs() << " *";
Matthias Braun9ab40392017-02-21 01:27:33 +0000366 dbgs() << ": Latency=" << SuccDep.getLatency();
367 if (SuccDep.isAssignedRegDep())
368 dbgs() << " Reg=" << PrintReg(SuccDep.getReg(), G->TRI);
David Greene94745d02010-01-05 01:25:41 +0000369 dbgs() << "\n";
Dan Gohman60cb69e2008-11-19 23:18:57 +0000370 }
371 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000372}
Manman Ren742534c2012-09-06 19:06:06 +0000373#endif
Dan Gohman4ce15e12008-11-20 01:26:25 +0000374
375#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000376unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000377 bool AnyNotSched = false;
378 unsigned DeadNodes = 0;
Matthias Braun9ab40392017-02-21 01:27:33 +0000379 for (const SUnit &SUnit : SUnits) {
380 if (!SUnit.isScheduled) {
381 if (SUnit.NumPreds == 0 && SUnit.NumSuccs == 0) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000382 ++DeadNodes;
383 continue;
384 }
385 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000386 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000387 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000388 dbgs() << "has not been scheduled!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000389 AnyNotSched = true;
390 }
Matthias Braun9ab40392017-02-21 01:27:33 +0000391 if (SUnit.isScheduled &&
392 (isBottomUp ? SUnit.getHeight() : SUnit.getDepth()) >
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000393 unsigned(std::numeric_limits<int>::max())) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000394 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000395 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000396 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000397 dbgs() << "has an unexpected "
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000398 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000399 AnyNotSched = true;
400 }
401 if (isBottomUp) {
Matthias Braun9ab40392017-02-21 01:27:33 +0000402 if (SUnit.NumSuccsLeft != 0) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000403 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000404 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000405 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000406 dbgs() << "has successors left!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000407 AnyNotSched = true;
408 }
409 } else {
Matthias Braun9ab40392017-02-21 01:27:33 +0000410 if (SUnit.NumPredsLeft != 0) {
Dan Gohman4ce15e12008-11-20 01:26:25 +0000411 if (!AnyNotSched)
David Greene94745d02010-01-05 01:25:41 +0000412 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun9ab40392017-02-21 01:27:33 +0000413 SUnit.dump(this);
David Greene94745d02010-01-05 01:25:41 +0000414 dbgs() << "has predecessors left!\n";
Dan Gohman4ce15e12008-11-20 01:26:25 +0000415 AnyNotSched = true;
416 }
417 }
418 }
Dan Gohman4ce15e12008-11-20 01:26:25 +0000419 assert(!AnyNotSched);
Andrew Trick46a58662012-03-07 05:21:36 +0000420 return SUnits.size() - DeadNodes;
Dan Gohman4ce15e12008-11-20 01:26:25 +0000421}
422#endif
Dan Gohmanad2134d2008-11-25 00:52:40 +0000423
Dan Gohmanad2134d2008-11-25 00:52:40 +0000424void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
Matthias Braun9ab40392017-02-21 01:27:33 +0000425 // The idea of the algorithm is taken from
426 // "Online algorithms for managing the topological order of
427 // a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
428 // This is the MNR algorithm, which was first introduced by
429 // A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
430 // "Maintaining a topological order under edge insertions".
431 //
432 // Short description of the algorithm:
433 //
434 // Topological ordering, ord, of a DAG maps each node to a topological
435 // index so that for all edges X->Y it is the case that ord(X) < ord(Y).
436 //
437 // This means that if there is a path from the node X to the node Z,
438 // then ord(X) < ord(Z).
439 //
440 // This property can be used to check for reachability of nodes:
441 // if Z is reachable from X, then an insertion of the edge Z->X would
442 // create a cycle.
443 //
444 // The algorithm first computes a topological ordering for the DAG by
445 // initializing the Index2Node and Node2Index arrays and then tries to keep
446 // the ordering up-to-date after edge insertions by reordering the DAG.
447 //
448 // On insertion of the edge X->Y, the algorithm first marks by calling DFS
449 // the nodes reachable from Y, and then shifts them using Shift to lie
450 // immediately after X in Index2Node.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000451 unsigned DAGSize = SUnits.size();
452 std::vector<SUnit*> WorkList;
453 WorkList.reserve(DAGSize);
454
455 Index2Node.resize(DAGSize);
456 Node2Index.resize(DAGSize);
457
458 // Initialize the data structures.
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000459 if (ExitSU)
460 WorkList.push_back(ExitSU);
Matthias Braun9ab40392017-02-21 01:27:33 +0000461 for (SUnit &SU : SUnits) {
462 int NodeNum = SU.NodeNum;
463 unsigned Degree = SU.Succs.size();
Dan Gohmanad2134d2008-11-25 00:52:40 +0000464 // Temporarily use the Node2Index array as scratch space for degree counts.
465 Node2Index[NodeNum] = Degree;
466
467 // Is it a node without dependencies?
468 if (Degree == 0) {
Matthias Braun9ab40392017-02-21 01:27:33 +0000469 assert(SU.Succs.empty() && "SUnit should have no successors");
Dan Gohmanad2134d2008-11-25 00:52:40 +0000470 // Collect leaf nodes.
Matthias Braun9ab40392017-02-21 01:27:33 +0000471 WorkList.push_back(&SU);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000472 }
John Mosby53646552010-06-30 03:40:54 +0000473 }
Dan Gohmanad2134d2008-11-25 00:52:40 +0000474
475 int Id = DAGSize;
476 while (!WorkList.empty()) {
477 SUnit *SU = WorkList.back();
478 WorkList.pop_back();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000479 if (SU->NodeNum < DAGSize)
480 Allocate(SU->NodeNum, --Id);
Matthias Braun9ab40392017-02-21 01:27:33 +0000481 for (const SDep &PredDep : SU->Preds) {
482 SUnit *SU = PredDep.getSUnit();
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000483 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohmanad2134d2008-11-25 00:52:40 +0000484 // If all dependencies of the node are processed already,
485 // then the node can be computed now.
486 WorkList.push_back(SU);
487 }
488 }
489
490 Visited.resize(DAGSize);
491
492#ifndef NDEBUG
493 // Check correctness of the ordering
Matthias Braun9ab40392017-02-21 01:27:33 +0000494 for (SUnit &SU : SUnits) {
495 for (const SDep &PD : SU.Preds) {
496 assert(Node2Index[SU.NodeNum] > Node2Index[PD.getSUnit()->NodeNum] &&
Dan Gohmanad2134d2008-11-25 00:52:40 +0000497 "Wrong topological sorting");
498 }
499 }
500#endif
501}
502
Dan Gohmanad2134d2008-11-25 00:52:40 +0000503void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
504 int UpperBound, LowerBound;
505 LowerBound = Node2Index[Y->NodeNum];
506 UpperBound = Node2Index[X->NodeNum];
507 bool HasLoop = false;
508 // Is Ord(X) < Ord(Y) ?
509 if (LowerBound < UpperBound) {
510 // Update the topological order.
511 Visited.reset();
512 DFS(Y, UpperBound, HasLoop);
513 assert(!HasLoop && "Inserted edge creates a loop!");
514 // Recompute topological indexes.
515 Shift(Visited, LowerBound, UpperBound);
516 }
517}
518
Dan Gohmanad2134d2008-11-25 00:52:40 +0000519void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
520 // InitDAGTopologicalSorting();
521}
522
Dan Gohman7d329742008-12-09 16:37:48 +0000523void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattnered69c6e2010-12-20 00:50:16 +0000524 bool &HasLoop) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000525 std::vector<const SUnit*> WorkList;
John Mosby53646552010-06-30 03:40:54 +0000526 WorkList.reserve(SUnits.size());
Dan Gohmanad2134d2008-11-25 00:52:40 +0000527
528 WorkList.push_back(SU);
Dan Gohman3a572132008-12-23 17:22:32 +0000529 do {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000530 SU = WorkList.back();
531 WorkList.pop_back();
532 Visited.set(SU->NodeNum);
Matthias Braun9ab40392017-02-21 01:27:33 +0000533 for (const SDep &SuccDep
534 : make_range(SU->Succs.rbegin(), SU->Succs.rend())) {
535 unsigned s = SuccDep.getSUnit()->NodeNum;
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000536 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
537 if (s >= Node2Index.size())
538 continue;
Dan Gohmanad2134d2008-11-25 00:52:40 +0000539 if (Node2Index[s] == UpperBound) {
John Mosby53646552010-06-30 03:40:54 +0000540 HasLoop = true;
Dan Gohmanad2134d2008-11-25 00:52:40 +0000541 return;
542 }
543 // Visit successors if not already and in affected region.
544 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Matthias Braun9ab40392017-02-21 01:27:33 +0000545 WorkList.push_back(SuccDep.getSUnit());
John Mosby53646552010-06-30 03:40:54 +0000546 }
547 }
Dan Gohman3a572132008-12-23 17:22:32 +0000548 } while (!WorkList.empty());
Dan Gohmanad2134d2008-11-25 00:52:40 +0000549}
550
Valery Pykhtin910da132017-03-28 05:12:31 +0000551std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU,
552 const SUnit &TargetSU,
553 bool &Success) {
554 std::vector<const SUnit*> WorkList;
555 int LowerBound = Node2Index[StartSU.NodeNum];
556 int UpperBound = Node2Index[TargetSU.NodeNum];
557 bool Found = false;
558 BitVector VisitedBack;
559 std::vector<int> Nodes;
560
561 if (LowerBound > UpperBound) {
562 Success = false;
563 return Nodes;
564 }
565
566 WorkList.reserve(SUnits.size());
567 Visited.reset();
568
569 // Starting from StartSU, visit all successors up
570 // to UpperBound.
571 WorkList.push_back(&StartSU);
572 do {
573 const SUnit *SU = WorkList.back();
574 WorkList.pop_back();
575 for (int I = SU->Succs.size()-1; I >= 0; --I) {
576 const SUnit *Succ = SU->Succs[I].getSUnit();
577 unsigned s = Succ->NodeNum;
578 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
579 if (Succ->isBoundaryNode())
580 continue;
581 if (Node2Index[s] == UpperBound) {
582 Found = true;
583 continue;
584 }
585 // Visit successors if not already and in affected region.
586 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
587 Visited.set(s);
588 WorkList.push_back(Succ);
589 }
590 }
591 } while (!WorkList.empty());
592
593 if (!Found) {
594 Success = false;
595 return Nodes;
596 }
597
598 WorkList.clear();
599 VisitedBack.resize(SUnits.size());
600 Found = false;
601
602 // Starting from TargetSU, visit all predecessors up
603 // to LowerBound. SUs that are visited by the two
604 // passes are added to Nodes.
605 WorkList.push_back(&TargetSU);
606 do {
607 const SUnit *SU = WorkList.back();
608 WorkList.pop_back();
609 for (int I = SU->Preds.size()-1; I >= 0; --I) {
610 const SUnit *Pred = SU->Preds[I].getSUnit();
611 unsigned s = Pred->NodeNum;
612 // Edges to non-SUnits are allowed but ignored (e.g. EntrySU).
613 if (Pred->isBoundaryNode())
614 continue;
615 if (Node2Index[s] == LowerBound) {
616 Found = true;
617 continue;
618 }
619 if (!VisitedBack.test(s) && Visited.test(s)) {
620 VisitedBack.set(s);
621 WorkList.push_back(Pred);
622 Nodes.push_back(s);
623 }
624 }
625 } while (!WorkList.empty());
626
627 assert(Found && "Error in SUnit Graph!");
628 Success = true;
629 return Nodes;
630}
631
John Mosby53646552010-06-30 03:40:54 +0000632void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohman7d329742008-12-09 16:37:48 +0000633 int UpperBound) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000634 std::vector<int> L;
635 int shift = 0;
636 int i;
637
638 for (i = LowerBound; i <= UpperBound; ++i) {
639 // w is node at topological index i.
640 int w = Index2Node[i];
641 if (Visited.test(w)) {
642 // Unmark.
643 Visited.reset(w);
644 L.push_back(w);
645 shift = shift + 1;
646 } else {
647 Allocate(w, i - shift);
648 }
649 }
650
Matthias Braun9ab40392017-02-21 01:27:33 +0000651 for (unsigned LI : L) {
652 Allocate(LI, i - shift);
Dan Gohmanad2134d2008-11-25 00:52:40 +0000653 i = i + 1;
654 }
655}
656
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000657bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
658 // Is SU reachable from TargetSU via successor edges?
659 if (IsReachable(SU, TargetSU))
Dan Gohmanad2134d2008-11-25 00:52:40 +0000660 return true;
Matthias Braun9ab40392017-02-21 01:27:33 +0000661 for (const SDep &PredDep : TargetSU->Preds)
662 if (PredDep.isAssignedRegDep() &&
663 IsReachable(SU, PredDep.getSUnit()))
Dan Gohmanad2134d2008-11-25 00:52:40 +0000664 return true;
665 return false;
666}
667
Dan Gohman7d329742008-12-09 16:37:48 +0000668bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
669 const SUnit *TargetSU) {
Dan Gohmanad2134d2008-11-25 00:52:40 +0000670 // If insertion of the edge SU->TargetSU would create a cycle
671 // then there is a path from TargetSU to SU.
672 int UpperBound, LowerBound;
673 LowerBound = Node2Index[TargetSU->NodeNum];
674 UpperBound = Node2Index[SU->NodeNum];
675 bool HasLoop = false;
676 // Is Ord(TargetSU) < Ord(SU) ?
677 if (LowerBound < UpperBound) {
678 Visited.reset();
John Mosby53646552010-06-30 03:40:54 +0000679 // There may be a path from TargetSU to SU. Check for it.
Dan Gohmanad2134d2008-11-25 00:52:40 +0000680 DFS(TargetSU, UpperBound, HasLoop);
681 }
682 return HasLoop;
683}
684
Dan Gohmanad2134d2008-11-25 00:52:40 +0000685void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
686 Node2Index[n] = index;
687 Index2Node[index] = n;
688}
689
John Mosby53646552010-06-30 03:40:54 +0000690ScheduleDAGTopologicalSort::
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000691ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
692 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohman7e105f02009-01-15 22:18:12 +0000693
Eugene Zelenkodb56e5a2017-02-22 22:32:51 +0000694ScheduleHazardRecognizer::~ScheduleHazardRecognizer() = default;