blob: 6c135b3d69d66ec37be8b974b2eba8ec0722d178 [file] [log] [blame]
Eugene Zelenko096e40d2017-02-22 22:32:51 +00001//===- ScheduleDAG.cpp - Implement the ScheduleDAG class ------------------===//
Dan Gohman343f0c02008-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 Braun1cd3a6f2017-02-21 01:27:33 +000010/// \file Implements the ScheduleDAG class, which is a base class used by
11/// scheduling implementation classes.
Dan Gohman343f0c02008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
Dan Gohman343f0c02008-11-19 23:18:57 +000015#include "llvm/CodeGen/ScheduleDAG.h"
Chandler Carruthe3e43d92017-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 Gohmanfc54c552009-01-15 22:18:12 +000020#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Andrew Trick2da8bc82010-12-24 05:03:26 +000021#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie48319232017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetRegisterInfo.h"
24#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber0f38c602018-04-30 14:59:11 +000025#include "llvm/Config/llvm-config.h"
Andrew Trick4cb971c2011-06-15 17:16:12 +000026#include "llvm/Support/CommandLine.h"
Eugene Zelenko096e40d2017-02-22 22:32:51 +000027#include "llvm/Support/Compiler.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000028#include "llvm/Support/Debug.h"
Daniel Dunbar3f0e8302009-07-24 09:53:24 +000029#include "llvm/Support/raw_ostream.h"
Eugene Zelenko096e40d2017-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 Gohman343f0c02008-11-19 23:18:57 +000037using namespace llvm;
38
Chandler Carruth8677f2f2014-04-22 02:02:50 +000039#define DEBUG_TYPE "pre-RA-sched"
40
Andrew Trick4cb971c2011-06-15 17:16:12 +000041#ifndef NDEBUG
Benjamin Kramera67f14b2011-08-19 01:42:18 +000042static cl::opt<bool> StressSchedOpt(
Andrew Trick4cb971c2011-06-15 17:16:12 +000043 "stress-sched", cl::Hidden, cl::init(false),
44 cl::desc("Stress test instruction scheduling"));
45#endif
46
Eugene Zelenko096e40d2017-02-22 22:32:51 +000047void SchedulingPriorityQueue::anchor() {}
David Blaikie2d24e2a2011-12-20 02:50:00 +000048
Dan Gohman79ce2762009-01-15 19:20:50 +000049ScheduleDAG::ScheduleDAG(MachineFunction &mf)
Eric Christopherad5a8572015-01-27 08:48:42 +000050 : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()),
51 TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
Eugene Zelenko096e40d2017-02-22 22:32:51 +000052 MRI(mf.getRegInfo()) {
Andrew Trick4cb971c2011-06-15 17:16:12 +000053#ifndef NDEBUG
54 StressSched = StressSchedOpt;
55#endif
Dan Gohman343f0c02008-11-19 23:18:57 +000056}
57
Eugene Zelenko096e40d2017-02-22 22:32:51 +000058ScheduleDAG::~ScheduleDAG() = default;
Dan Gohman343f0c02008-11-19 23:18:57 +000059
Andrew Trick47c14452012-03-07 05:21:52 +000060void ScheduleDAG::clearDAG() {
61 SUnits.clear();
62 EntrySU = SUnit();
63 ExitSU = SUnit();
64}
65
Evan Chenge837dea2011-06-28 19:10:37 +000066const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
Craig Topper4ba84432014-04-14 00:51:57 +000067 if (!Node || !Node->isMachineOpcode()) return nullptr;
Andrew Trick2da8bc82010-12-24 05:03:26 +000068 return &TII->get(Node->getMachineOpcode());
69}
70
Matthias Braunb064c242018-09-19 00:23:35 +000071LLVM_DUMP_METHOD void SDep::dump(const TargetRegisterInfo *TRI) const {
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000072 switch (getKind()) {
Matthias Braunb064c242018-09-19 00:23:35 +000073 case Data: dbgs() << "Data"; break;
74 case Anti: dbgs() << "Anti"; break;
75 case Output: dbgs() << "Out "; break;
76 case Order: dbgs() << "Ord "; break;
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000077 }
78
79 switch (getKind()) {
80 case Data:
Matthias Braunb064c242018-09-19 00:23:35 +000081 dbgs() << " Latency=" << getLatency();
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000082 if (TRI && isAssignedRegDep())
Matthias Braunb064c242018-09-19 00:23:35 +000083 dbgs() << " Reg=" << printReg(getReg(), TRI);
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000084 break;
85 case Anti:
86 case Output:
Matthias Braunb064c242018-09-19 00:23:35 +000087 dbgs() << " Latency=" << getLatency();
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000088 break;
89 case Order:
Matthias Braunb064c242018-09-19 00:23:35 +000090 dbgs() << " Latency=" << getLatency();
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000091 switch(Contents.OrdKind) {
Matthias Braunb064c242018-09-19 00:23:35 +000092 case Barrier: dbgs() << " Barrier"; break;
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000093 case MayAliasMem:
Matthias Braunb064c242018-09-19 00:23:35 +000094 case MustAliasMem: dbgs() << " Memory"; break;
95 case Artificial: dbgs() << " Artificial"; break;
96 case Weak: dbgs() << " Weak"; break;
97 case Cluster: dbgs() << " Cluster"; break;
Evandro Menezes0dedcfc2017-07-12 15:30:59 +000098 }
99 break;
100 }
Evandro Menezes0dedcfc2017-07-12 15:30:59 +0000101}
102
Andrew Trickae692f22012-11-12 19:28:57 +0000103bool SUnit::addPred(const SDep &D, bool Required) {
Alp Tokerae43cab62014-01-24 17:20:08 +0000104 // If this node already has this dependence, don't add a redundant one.
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000105 for (SDep &PredDep : Preds) {
Andrew Trickae692f22012-11-12 19:28:57 +0000106 // Zero-latency weak edges may be added purely for heuristic ordering. Don't
107 // add them if another kind of edge already exists.
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000108 if (!Required && PredDep.getSUnit() == D.getSUnit())
Andrew Trickae692f22012-11-12 19:28:57 +0000109 return false;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000110 if (PredDep.overlaps(D)) {
111 // Extend the latency if needed. Equivalent to
112 // removePred(PredDep) + addPred(D).
113 if (PredDep.getLatency() < D.getLatency()) {
114 SUnit *PredSU = PredDep.getSUnit();
Andrew Trick9df55ee2012-06-13 02:39:00 +0000115 // Find the corresponding successor in N.
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000116 SDep ForwardD = PredDep;
Andrew Trick9df55ee2012-06-13 02:39:00 +0000117 ForwardD.setSUnit(this);
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000118 for (SDep &SuccDep : PredSU->Succs) {
119 if (SuccDep == ForwardD) {
120 SuccDep.setLatency(D.getLatency());
Andrew Trick9df55ee2012-06-13 02:39:00 +0000121 break;
122 }
123 }
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000124 PredDep.setLatency(D.getLatency());
Andrew Trick9df55ee2012-06-13 02:39:00 +0000125 }
Andrew Trick92e94662011-02-04 03:18:17 +0000126 return false;
Andrew Trick9df55ee2012-06-13 02:39:00 +0000127 }
128 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000129 // Now add a corresponding succ to N.
130 SDep P = D;
131 P.setSUnit(this);
132 SUnit *N = D.getSUnit();
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000133 // Update the bookkeeping.
134 if (D.getKind() == SDep::Data) {
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000135 assert(NumPreds < std::numeric_limits<unsigned>::max() &&
136 "NumPreds will overflow!");
137 assert(N->NumSuccs < std::numeric_limits<unsigned>::max() &&
138 "NumSuccs will overflow!");
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000139 ++NumPreds;
140 ++N->NumSuccs;
141 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000142 if (!N->isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000143 if (D.isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000144 ++WeakPredsLeft;
145 }
146 else {
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000147 assert(NumPredsLeft < std::numeric_limits<unsigned>::max() &&
148 "NumPredsLeft will overflow!");
Andrew Trickae692f22012-11-12 19:28:57 +0000149 ++NumPredsLeft;
150 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000151 }
152 if (!isScheduled) {
Andrew Trickcf6b6132012-11-13 02:35:06 +0000153 if (D.isWeak()) {
Andrew Trickae692f22012-11-12 19:28:57 +0000154 ++N->WeakSuccsLeft;
155 }
156 else {
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000157 assert(N->NumSuccsLeft < std::numeric_limits<unsigned>::max() &&
158 "NumSuccsLeft will overflow!");
Andrew Trickae692f22012-11-12 19:28:57 +0000159 ++N->NumSuccsLeft;
160 }
Reid Klecknerc277ab02009-09-30 20:15:38 +0000161 }
Dan Gohman3f237442008-12-16 03:25:46 +0000162 Preds.push_back(D);
Dan Gohmana1f50e22009-01-13 19:08:45 +0000163 N->Succs.push_back(P);
Dan Gohmana80c8592009-01-05 22:40:26 +0000164 if (P.getLatency() != 0) {
165 this->setDepthDirty();
166 N->setHeightDirty();
167 }
Andrew Trick92e94662011-02-04 03:18:17 +0000168 return true;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000169}
170
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000171void SUnit::removePred(const SDep &D) {
172 // Find the matching predecessor.
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000173 SmallVectorImpl<SDep>::iterator I = llvm::find(Preds, D);
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000174 if (I == Preds.end())
175 return;
176 // Find the corresponding successor in N.
177 SDep P = D;
178 P.setSUnit(this);
179 SUnit *N = D.getSUnit();
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000180 SmallVectorImpl<SDep>::iterator Succ = llvm::find(N->Succs, P);
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000181 assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
182 N->Succs.erase(Succ);
183 Preds.erase(I);
184 // Update the bookkeeping.
185 if (P.getKind() == SDep::Data) {
186 assert(NumPreds > 0 && "NumPreds will underflow!");
187 assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
188 --NumPreds;
189 --N->NumSuccs;
190 }
191 if (!N->isScheduled) {
192 if (D.isWeak())
193 --WeakPredsLeft;
194 else {
195 assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
196 --NumPredsLeft;
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000197 }
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000198 }
199 if (!isScheduled) {
200 if (D.isWeak())
201 --N->WeakSuccsLeft;
202 else {
203 assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
204 --N->NumSuccsLeft;
205 }
206 }
207 if (P.getLatency() != 0) {
208 this->setDepthDirty();
209 N->setHeightDirty();
210 }
Dan Gohmanc6b680e2008-12-16 01:05:52 +0000211}
212
Dan Gohman3f237442008-12-16 03:25:46 +0000213void SUnit::setDepthDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000214 if (!isDepthCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000215 SmallVector<SUnit*, 8> WorkList;
216 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000217 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000218 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000219 SU->isDepthCurrent = false;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000220 for (SDep &SuccDep : SU->Succs) {
221 SUnit *SuccSU = SuccDep.getSUnit();
Dan Gohman8044e9b2008-12-22 21:11:33 +0000222 if (SuccSU->isDepthCurrent)
223 WorkList.push_back(SuccSU);
224 }
225 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000226}
227
228void SUnit::setHeightDirty() {
Dan Gohman8044e9b2008-12-22 21:11:33 +0000229 if (!isHeightCurrent) return;
Dan Gohman3f237442008-12-16 03:25:46 +0000230 SmallVector<SUnit*, 8> WorkList;
231 WorkList.push_back(this);
Dan Gohman8044e9b2008-12-22 21:11:33 +0000232 do {
Dan Gohmane19c6362008-12-20 16:42:33 +0000233 SUnit *SU = WorkList.pop_back_val();
Dan Gohman3f237442008-12-16 03:25:46 +0000234 SU->isHeightCurrent = false;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000235 for (SDep &PredDep : SU->Preds) {
236 SUnit *PredSU = PredDep.getSUnit();
Dan Gohman8044e9b2008-12-22 21:11:33 +0000237 if (PredSU->isHeightCurrent)
238 WorkList.push_back(PredSU);
239 }
240 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000241}
242
David Goodwin557bbe62009-11-20 19:32:48 +0000243void SUnit::setDepthToAtLeast(unsigned NewDepth) {
244 if (NewDepth <= getDepth())
Dan Gohman3f237442008-12-16 03:25:46 +0000245 return;
246 setDepthDirty();
247 Depth = NewDepth;
248 isDepthCurrent = true;
249}
250
David Goodwin557bbe62009-11-20 19:32:48 +0000251void SUnit::setHeightToAtLeast(unsigned NewHeight) {
252 if (NewHeight <= getHeight())
Dan Gohman3f237442008-12-16 03:25:46 +0000253 return;
254 setHeightDirty();
255 Height = NewHeight;
256 isHeightCurrent = true;
257}
258
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000259/// Calculates the maximal path from the node to the exit.
David Goodwin557bbe62009-11-20 19:32:48 +0000260void SUnit::ComputeDepth() {
Dan Gohman3f237442008-12-16 03:25:46 +0000261 SmallVector<SUnit*, 8> WorkList;
262 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000263 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000264 SUnit *Cur = WorkList.back();
265
266 bool Done = true;
267 unsigned MaxPredDepth = 0;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000268 for (const SDep &PredDep : Cur->Preds) {
269 SUnit *PredSU = PredDep.getSUnit();
Dan Gohman3f237442008-12-16 03:25:46 +0000270 if (PredSU->isDepthCurrent)
271 MaxPredDepth = std::max(MaxPredDepth,
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000272 PredSU->Depth + PredDep.getLatency());
Dan Gohman3f237442008-12-16 03:25:46 +0000273 else {
274 Done = false;
275 WorkList.push_back(PredSU);
276 }
277 }
278
279 if (Done) {
280 WorkList.pop_back();
281 if (MaxPredDepth != Cur->Depth) {
282 Cur->setDepthDirty();
283 Cur->Depth = MaxPredDepth;
284 }
285 Cur->isDepthCurrent = true;
286 }
Dan Gohman1578f842008-12-23 17:22:32 +0000287 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000288}
289
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000290/// Calculates the maximal path from the node to the entry.
David Goodwin557bbe62009-11-20 19:32:48 +0000291void SUnit::ComputeHeight() {
Dan Gohman3f237442008-12-16 03:25:46 +0000292 SmallVector<SUnit*, 8> WorkList;
293 WorkList.push_back(this);
Dan Gohman1578f842008-12-23 17:22:32 +0000294 do {
Dan Gohman3f237442008-12-16 03:25:46 +0000295 SUnit *Cur = WorkList.back();
296
297 bool Done = true;
298 unsigned MaxSuccHeight = 0;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000299 for (const SDep &SuccDep : Cur->Succs) {
300 SUnit *SuccSU = SuccDep.getSUnit();
Dan Gohman3f237442008-12-16 03:25:46 +0000301 if (SuccSU->isHeightCurrent)
302 MaxSuccHeight = std::max(MaxSuccHeight,
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000303 SuccSU->Height + SuccDep.getLatency());
Dan Gohman3f237442008-12-16 03:25:46 +0000304 else {
305 Done = false;
306 WorkList.push_back(SuccSU);
307 }
308 }
309
310 if (Done) {
311 WorkList.pop_back();
312 if (MaxSuccHeight != Cur->Height) {
313 Cur->setHeightDirty();
314 Cur->Height = MaxSuccHeight;
315 }
316 Cur->isHeightCurrent = true;
317 }
Dan Gohman1578f842008-12-23 17:22:32 +0000318 } while (!WorkList.empty());
Dan Gohman3f237442008-12-16 03:25:46 +0000319}
320
Andrew Trick66658dd2013-01-24 02:09:55 +0000321void SUnit::biasCriticalPath() {
322 if (NumPreds < 2)
323 return;
324
325 SUnit::pred_iterator BestI = Preds.begin();
326 unsigned MaxDepth = BestI->getSUnit()->getDepth();
Benjamin Kramerd628f192014-03-02 12:27:27 +0000327 for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
328 ++I) {
Andrew Trick66658dd2013-01-24 02:09:55 +0000329 if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
330 BestI = I;
331 }
332 if (BestI != Preds.begin())
333 std::swap(*Preds.begin(), *BestI);
334}
335
Aaron Ballman1d03d382017-10-15 14:32:27 +0000336#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braunb064c242018-09-19 00:23:35 +0000337LLVM_DUMP_METHOD void SUnit::dumpAttributes() const {
David Greene4b134d12010-01-05 01:25:41 +0000338 dbgs() << " # preds left : " << NumPredsLeft << "\n";
339 dbgs() << " # succs left : " << NumSuccsLeft << "\n";
Andrew Trickae692f22012-11-12 19:28:57 +0000340 if (WeakPredsLeft)
341 dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
342 if (WeakSuccsLeft)
343 dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
Andrew Trick92e94662011-02-04 03:18:17 +0000344 dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
David Greene4b134d12010-01-05 01:25:41 +0000345 dbgs() << " Latency : " << Latency << "\n";
Andrew Trickbf32b7f2013-03-01 00:19:09 +0000346 dbgs() << " Depth : " << getDepth() << "\n";
347 dbgs() << " Height : " << getHeight() << "\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000348}
Dan Gohman343f0c02008-11-19 23:18:57 +0000349
Matthias Braunb064c242018-09-19 00:23:35 +0000350LLVM_DUMP_METHOD void ScheduleDAG::dumpNodeName(const SUnit &SU) const {
351 if (&SU == &EntrySU)
352 dbgs() << "EntrySU";
353 else if (&SU == &ExitSU)
354 dbgs() << "ExitSU";
355 else
356 dbgs() << "SU(" << SU.NodeNum << ")";
357}
358
359LLVM_DUMP_METHOD void ScheduleDAG::dumpNodeAll(const SUnit &SU) const {
360 dumpNode(SU);
361 SU.dumpAttributes();
362 if (SU.Preds.size() > 0) {
David Greene4b134d12010-01-05 01:25:41 +0000363 dbgs() << " Predecessors:\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000364 for (const SDep &Dep : SU.Preds) {
Evandro Menezes0dedcfc2017-07-12 15:30:59 +0000365 dbgs() << " ";
Matthias Braunb064c242018-09-19 00:23:35 +0000366 dumpNodeName(*Dep.getSUnit());
367 dbgs() << ": ";
368 Dep.dump(TRI);
369 dbgs() << '\n';
Dan Gohman343f0c02008-11-19 23:18:57 +0000370 }
371 }
Matthias Braunb064c242018-09-19 00:23:35 +0000372 if (SU.Succs.size() > 0) {
David Greene4b134d12010-01-05 01:25:41 +0000373 dbgs() << " Successors:\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000374 for (const SDep &Dep : SU.Succs) {
Evandro Menezes0dedcfc2017-07-12 15:30:59 +0000375 dbgs() << " ";
Matthias Braunb064c242018-09-19 00:23:35 +0000376 dumpNodeName(*Dep.getSUnit());
377 dbgs() << ": ";
378 Dep.dump(TRI);
379 dbgs() << '\n';
Dan Gohman343f0c02008-11-19 23:18:57 +0000380 }
381 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000382}
Manman Ren77e300e2012-09-06 19:06:06 +0000383#endif
Dan Gohmana1e6d362008-11-20 01:26:25 +0000384
385#ifndef NDEBUG
Andrew Trick4c727202012-03-07 05:21:36 +0000386unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000387 bool AnyNotSched = false;
388 unsigned DeadNodes = 0;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000389 for (const SUnit &SUnit : SUnits) {
390 if (!SUnit.isScheduled) {
391 if (SUnit.NumPreds == 0 && SUnit.NumSuccs == 0) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000392 ++DeadNodes;
393 continue;
394 }
395 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000396 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000397 dumpNode(SUnit);
David Greene4b134d12010-01-05 01:25:41 +0000398 dbgs() << "has not been scheduled!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000399 AnyNotSched = true;
400 }
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000401 if (SUnit.isScheduled &&
402 (isBottomUp ? SUnit.getHeight() : SUnit.getDepth()) >
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000403 unsigned(std::numeric_limits<int>::max())) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000404 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000405 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000406 dumpNode(SUnit);
David Greene4b134d12010-01-05 01:25:41 +0000407 dbgs() << "has an unexpected "
Dan Gohman3f237442008-12-16 03:25:46 +0000408 << (isBottomUp ? "Height" : "Depth") << " value!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000409 AnyNotSched = true;
410 }
411 if (isBottomUp) {
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000412 if (SUnit.NumSuccsLeft != 0) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000413 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000414 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000415 dumpNode(SUnit);
David Greene4b134d12010-01-05 01:25:41 +0000416 dbgs() << "has successors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000417 AnyNotSched = true;
418 }
419 } else {
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000420 if (SUnit.NumPredsLeft != 0) {
Dan Gohmana1e6d362008-11-20 01:26:25 +0000421 if (!AnyNotSched)
David Greene4b134d12010-01-05 01:25:41 +0000422 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braunb064c242018-09-19 00:23:35 +0000423 dumpNode(SUnit);
David Greene4b134d12010-01-05 01:25:41 +0000424 dbgs() << "has predecessors left!\n";
Dan Gohmana1e6d362008-11-20 01:26:25 +0000425 AnyNotSched = true;
426 }
427 }
428 }
Dan Gohmana1e6d362008-11-20 01:26:25 +0000429 assert(!AnyNotSched);
Andrew Trick4c727202012-03-07 05:21:36 +0000430 return SUnits.size() - DeadNodes;
Dan Gohmana1e6d362008-11-20 01:26:25 +0000431}
432#endif
Dan Gohman21d90032008-11-25 00:52:40 +0000433
Dan Gohman21d90032008-11-25 00:52:40 +0000434void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000435 // The idea of the algorithm is taken from
436 // "Online algorithms for managing the topological order of
437 // a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
438 // This is the MNR algorithm, which was first introduced by
439 // A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
440 // "Maintaining a topological order under edge insertions".
441 //
442 // Short description of the algorithm:
443 //
444 // Topological ordering, ord, of a DAG maps each node to a topological
445 // index so that for all edges X->Y it is the case that ord(X) < ord(Y).
446 //
447 // This means that if there is a path from the node X to the node Z,
448 // then ord(X) < ord(Z).
449 //
450 // This property can be used to check for reachability of nodes:
451 // if Z is reachable from X, then an insertion of the edge Z->X would
452 // create a cycle.
453 //
454 // The algorithm first computes a topological ordering for the DAG by
455 // initializing the Index2Node and Node2Index arrays and then tries to keep
456 // the ordering up-to-date after edge insertions by reordering the DAG.
457 //
458 // On insertion of the edge X->Y, the algorithm first marks by calling DFS
459 // the nodes reachable from Y, and then shifts them using Shift to lie
460 // immediately after X in Index2Node.
Dan Gohman21d90032008-11-25 00:52:40 +0000461 unsigned DAGSize = SUnits.size();
462 std::vector<SUnit*> WorkList;
463 WorkList.reserve(DAGSize);
464
465 Index2Node.resize(DAGSize);
466 Node2Index.resize(DAGSize);
467
468 // Initialize the data structures.
Andrew Trickae692f22012-11-12 19:28:57 +0000469 if (ExitSU)
470 WorkList.push_back(ExitSU);
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000471 for (SUnit &SU : SUnits) {
472 int NodeNum = SU.NodeNum;
473 unsigned Degree = SU.Succs.size();
Dan Gohman21d90032008-11-25 00:52:40 +0000474 // Temporarily use the Node2Index array as scratch space for degree counts.
475 Node2Index[NodeNum] = Degree;
476
477 // Is it a node without dependencies?
478 if (Degree == 0) {
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000479 assert(SU.Succs.empty() && "SUnit should have no successors");
Dan Gohman21d90032008-11-25 00:52:40 +0000480 // Collect leaf nodes.
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000481 WorkList.push_back(&SU);
Dan Gohman21d90032008-11-25 00:52:40 +0000482 }
John Mosby9f71f802010-06-30 03:40:54 +0000483 }
Dan Gohman21d90032008-11-25 00:52:40 +0000484
485 int Id = DAGSize;
486 while (!WorkList.empty()) {
487 SUnit *SU = WorkList.back();
488 WorkList.pop_back();
Andrew Trickae692f22012-11-12 19:28:57 +0000489 if (SU->NodeNum < DAGSize)
490 Allocate(SU->NodeNum, --Id);
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000491 for (const SDep &PredDep : SU->Preds) {
492 SUnit *SU = PredDep.getSUnit();
Andrew Trickae692f22012-11-12 19:28:57 +0000493 if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
Dan Gohman21d90032008-11-25 00:52:40 +0000494 // If all dependencies of the node are processed already,
495 // then the node can be computed now.
496 WorkList.push_back(SU);
497 }
498 }
499
500 Visited.resize(DAGSize);
501
502#ifndef NDEBUG
503 // Check correctness of the ordering
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000504 for (SUnit &SU : SUnits) {
505 for (const SDep &PD : SU.Preds) {
506 assert(Node2Index[SU.NodeNum] > Node2Index[PD.getSUnit()->NodeNum] &&
Dan Gohman21d90032008-11-25 00:52:40 +0000507 "Wrong topological sorting");
508 }
509 }
510#endif
511}
512
Dan Gohman21d90032008-11-25 00:52:40 +0000513void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
514 int UpperBound, LowerBound;
515 LowerBound = Node2Index[Y->NodeNum];
516 UpperBound = Node2Index[X->NodeNum];
517 bool HasLoop = false;
518 // Is Ord(X) < Ord(Y) ?
519 if (LowerBound < UpperBound) {
520 // Update the topological order.
521 Visited.reset();
522 DFS(Y, UpperBound, HasLoop);
523 assert(!HasLoop && "Inserted edge creates a loop!");
524 // Recompute topological indexes.
525 Shift(Visited, LowerBound, UpperBound);
526 }
527}
528
Dan Gohman21d90032008-11-25 00:52:40 +0000529void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
530 // InitDAGTopologicalSorting();
531}
532
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000533void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
Chris Lattner50782932010-12-20 00:50:16 +0000534 bool &HasLoop) {
Dan Gohman21d90032008-11-25 00:52:40 +0000535 std::vector<const SUnit*> WorkList;
John Mosby9f71f802010-06-30 03:40:54 +0000536 WorkList.reserve(SUnits.size());
Dan Gohman21d90032008-11-25 00:52:40 +0000537
538 WorkList.push_back(SU);
Dan Gohman1578f842008-12-23 17:22:32 +0000539 do {
Dan Gohman21d90032008-11-25 00:52:40 +0000540 SU = WorkList.back();
541 WorkList.pop_back();
542 Visited.set(SU->NodeNum);
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000543 for (const SDep &SuccDep
544 : make_range(SU->Succs.rbegin(), SU->Succs.rend())) {
545 unsigned s = SuccDep.getSUnit()->NodeNum;
Andrew Trickae692f22012-11-12 19:28:57 +0000546 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
547 if (s >= Node2Index.size())
548 continue;
Dan Gohman21d90032008-11-25 00:52:40 +0000549 if (Node2Index[s] == UpperBound) {
John Mosby9f71f802010-06-30 03:40:54 +0000550 HasLoop = true;
Dan Gohman21d90032008-11-25 00:52:40 +0000551 return;
552 }
553 // Visit successors if not already and in affected region.
554 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000555 WorkList.push_back(SuccDep.getSUnit());
John Mosby9f71f802010-06-30 03:40:54 +0000556 }
557 }
Dan Gohman1578f842008-12-23 17:22:32 +0000558 } while (!WorkList.empty());
Dan Gohman21d90032008-11-25 00:52:40 +0000559}
560
Valery Pykhtin217a5692017-03-28 05:12:31 +0000561std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU,
562 const SUnit &TargetSU,
563 bool &Success) {
564 std::vector<const SUnit*> WorkList;
565 int LowerBound = Node2Index[StartSU.NodeNum];
566 int UpperBound = Node2Index[TargetSU.NodeNum];
567 bool Found = false;
568 BitVector VisitedBack;
569 std::vector<int> Nodes;
570
571 if (LowerBound > UpperBound) {
572 Success = false;
573 return Nodes;
574 }
575
576 WorkList.reserve(SUnits.size());
577 Visited.reset();
578
579 // Starting from StartSU, visit all successors up
580 // to UpperBound.
581 WorkList.push_back(&StartSU);
582 do {
583 const SUnit *SU = WorkList.back();
584 WorkList.pop_back();
585 for (int I = SU->Succs.size()-1; I >= 0; --I) {
586 const SUnit *Succ = SU->Succs[I].getSUnit();
587 unsigned s = Succ->NodeNum;
588 // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
589 if (Succ->isBoundaryNode())
590 continue;
591 if (Node2Index[s] == UpperBound) {
592 Found = true;
593 continue;
594 }
595 // Visit successors if not already and in affected region.
596 if (!Visited.test(s) && Node2Index[s] < UpperBound) {
597 Visited.set(s);
598 WorkList.push_back(Succ);
599 }
600 }
601 } while (!WorkList.empty());
602
603 if (!Found) {
604 Success = false;
605 return Nodes;
606 }
607
608 WorkList.clear();
609 VisitedBack.resize(SUnits.size());
610 Found = false;
611
612 // Starting from TargetSU, visit all predecessors up
613 // to LowerBound. SUs that are visited by the two
614 // passes are added to Nodes.
615 WorkList.push_back(&TargetSU);
616 do {
617 const SUnit *SU = WorkList.back();
618 WorkList.pop_back();
619 for (int I = SU->Preds.size()-1; I >= 0; --I) {
620 const SUnit *Pred = SU->Preds[I].getSUnit();
621 unsigned s = Pred->NodeNum;
622 // Edges to non-SUnits are allowed but ignored (e.g. EntrySU).
623 if (Pred->isBoundaryNode())
624 continue;
625 if (Node2Index[s] == LowerBound) {
626 Found = true;
627 continue;
628 }
629 if (!VisitedBack.test(s) && Visited.test(s)) {
630 VisitedBack.set(s);
631 WorkList.push_back(Pred);
632 Nodes.push_back(s);
633 }
634 }
635 } while (!WorkList.empty());
636
637 assert(Found && "Error in SUnit Graph!");
638 Success = true;
639 return Nodes;
640}
641
John Mosby9f71f802010-06-30 03:40:54 +0000642void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000643 int UpperBound) {
Dan Gohman21d90032008-11-25 00:52:40 +0000644 std::vector<int> L;
645 int shift = 0;
646 int i;
647
648 for (i = LowerBound; i <= UpperBound; ++i) {
649 // w is node at topological index i.
650 int w = Index2Node[i];
651 if (Visited.test(w)) {
652 // Unmark.
653 Visited.reset(w);
654 L.push_back(w);
655 shift = shift + 1;
656 } else {
657 Allocate(w, i - shift);
658 }
659 }
660
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000661 for (unsigned LI : L) {
662 Allocate(LI, i - shift);
Dan Gohman21d90032008-11-25 00:52:40 +0000663 i = i + 1;
664 }
665}
666
Andrew Trickae692f22012-11-12 19:28:57 +0000667bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
668 // Is SU reachable from TargetSU via successor edges?
669 if (IsReachable(SU, TargetSU))
Dan Gohman21d90032008-11-25 00:52:40 +0000670 return true;
Matthias Braun1cd3a6f2017-02-21 01:27:33 +0000671 for (const SDep &PredDep : TargetSU->Preds)
672 if (PredDep.isAssignedRegDep() &&
673 IsReachable(SU, PredDep.getSUnit()))
Dan Gohman21d90032008-11-25 00:52:40 +0000674 return true;
675 return false;
676}
677
Dan Gohmane3a49cd2008-12-09 16:37:48 +0000678bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
679 const SUnit *TargetSU) {
Dan Gohman21d90032008-11-25 00:52:40 +0000680 // If insertion of the edge SU->TargetSU would create a cycle
681 // then there is a path from TargetSU to SU.
682 int UpperBound, LowerBound;
683 LowerBound = Node2Index[TargetSU->NodeNum];
684 UpperBound = Node2Index[SU->NodeNum];
685 bool HasLoop = false;
686 // Is Ord(TargetSU) < Ord(SU) ?
687 if (LowerBound < UpperBound) {
688 Visited.reset();
John Mosby9f71f802010-06-30 03:40:54 +0000689 // There may be a path from TargetSU to SU. Check for it.
Dan Gohman21d90032008-11-25 00:52:40 +0000690 DFS(TargetSU, UpperBound, HasLoop);
691 }
692 return HasLoop;
693}
694
Dan Gohman21d90032008-11-25 00:52:40 +0000695void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
696 Node2Index[n] = index;
697 Index2Node[index] = n;
698}
699
John Mosby9f71f802010-06-30 03:40:54 +0000700ScheduleDAGTopologicalSort::
Andrew Trickae692f22012-11-12 19:28:57 +0000701ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
702 : SUnits(sunits), ExitSU(exitsu) {}
Dan Gohmanfc54c552009-01-15 22:18:12 +0000703
Eugene Zelenko096e40d2017-02-22 22:32:51 +0000704ScheduleHazardRecognizer::~ScheduleHazardRecognizer() = default;