1 //===----------------------- GCNMinRegStrategy.cpp - ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/ScheduleDAG.h"
18 #define DEBUG_TYPE "machine-scheduler"
21 class GCNMinRegScheduler {
22 struct Candidate : ilist_node<Candidate> {
26 Candidate(const SUnit *SU_, int Priority_ = 0)
27 : SU(SU_), Priority(Priority_) {}
30 SpecificBumpPtrAllocator<Candidate> Alloc;
31 typedef simple_ilist<Candidate> Queue;
32 Queue RQ; // Ready queue
34 std::vector<unsigned> NumPreds;
36 bool isScheduled(const SUnit *SU) const {
37 assert(!SU->isBoundaryNode());
38 return NumPreds[SU->NodeNum] == std::numeric_limits<unsigned>::max();
41 void setIsScheduled(const SUnit *SU) {
42 assert(!SU->isBoundaryNode());
43 NumPreds[SU->NodeNum] = std::numeric_limits<unsigned>::max();
46 unsigned getNumPreds(const SUnit *SU) const {
47 assert(!SU->isBoundaryNode());
48 assert(NumPreds[SU->NodeNum] != std::numeric_limits<unsigned>::max());
49 return NumPreds[SU->NodeNum];
52 unsigned decNumPreds(const SUnit *SU) {
53 assert(!SU->isBoundaryNode());
54 assert(NumPreds[SU->NodeNum] != std::numeric_limits<unsigned>::max());
55 return --NumPreds[SU->NodeNum];
58 void initNumPreds(const decltype(ScheduleDAG::SUnits) &SUnits);
60 int getReadySuccessors(const SUnit *SU) const;
61 int getNotReadySuccessors(const SUnit *SU) const;
63 template <typename Calc>
64 unsigned findMax(unsigned Num, Calc C);
66 Candidate* pickCandidate();
68 void bumpPredsPriority(const SUnit *SchedSU, int Priority);
69 void releaseSuccessors(const SUnit* SU, int Priority);
72 std::vector<const SUnit*> schedule(ArrayRef<const SUnit*> TopRoots,
73 const ScheduleDAG &DAG);
77 void GCNMinRegScheduler::initNumPreds(const decltype(ScheduleDAG::SUnits) &SUnits) {
78 NumPreds.resize(SUnits.size());
79 for (unsigned I = 0; I < SUnits.size(); ++I)
80 NumPreds[I] = SUnits[I].NumPredsLeft;
83 int GCNMinRegScheduler::getReadySuccessors(const SUnit *SU) const {
84 unsigned NumSchedSuccs = 0;
85 for (auto SDep : SU->Succs) {
86 bool wouldBeScheduled = true;
87 for (auto PDep : SDep.getSUnit()->Preds) {
88 auto PSU = PDep.getSUnit();
89 assert(!PSU->isBoundaryNode());
90 if (PSU != SU && !isScheduled(PSU)) {
91 wouldBeScheduled = false;
95 NumSchedSuccs += wouldBeScheduled ? 1 : 0;
100 int GCNMinRegScheduler::getNotReadySuccessors(const SUnit *SU) const {
101 return SU->Succs.size() - getReadySuccessors(SU);
104 template <typename Calc>
105 unsigned GCNMinRegScheduler::findMax(unsigned Num, Calc C) {
106 assert(!RQ.empty() && Num <= RQ.size());
107 typedef decltype(C(*RQ.begin())) T;
108 T Max = std::numeric_limits<T>::min();
110 for (auto I = RQ.begin(); Num; --Num) {
128 GCNMinRegScheduler::Candidate* GCNMinRegScheduler::pickCandidate() {
130 unsigned Num = RQ.size();
133 DEBUG(dbgs() << "\nSelecting max priority candidates among " << Num << '\n');
134 Num = findMax(Num, [=](const Candidate &C) { return C.Priority; });
137 DEBUG(dbgs() << "\nSelecting min non-ready producing candidate among "
139 Num = findMax(Num, [=](const Candidate &C) {
141 int Res = getNotReadySuccessors(SU);
142 DEBUG(dbgs() << "SU(" << SU->NodeNum << ") would left non-ready "
143 << Res << " successors, metric = " << -Res << '\n');
148 DEBUG(dbgs() << "\nSelecting most producing candidate among "
150 Num = findMax(Num, [=](const Candidate &C) {
152 auto Res = getReadySuccessors(SU);
153 DEBUG(dbgs() << "SU(" << SU->NodeNum << ") would make ready "
154 << Res << " successors, metric = " << Res << '\n');
159 Num = Num ? Num : RQ.size();
160 DEBUG(dbgs() << "\nCan't find best candidate, selecting in program order among "
162 Num = findMax(Num, [=](const Candidate &C) { return -(int64_t)C.SU->NodeNum; });
169 void GCNMinRegScheduler::bumpPredsPriority(const SUnit *SchedSU, int Priority) {
170 SmallPtrSet<const SUnit*, 32> Set;
171 for (const auto &S : SchedSU->Succs) {
172 if (S.getSUnit()->isBoundaryNode() || isScheduled(S.getSUnit()) ||
173 S.getKind() != SDep::Data)
175 for (const auto &P : S.getSUnit()->Preds) {
176 auto PSU = P.getSUnit();
177 assert(!PSU->isBoundaryNode());
178 if (PSU != SchedSU && !isScheduled(PSU)) {
183 SmallVector<const SUnit*, 32> Worklist(Set.begin(), Set.end());
184 while (!Worklist.empty()) {
185 auto SU = Worklist.pop_back_val();
186 assert(!SU->isBoundaryNode());
187 for (const auto &P : SU->Preds) {
188 if (!P.getSUnit()->isBoundaryNode() && !isScheduled(P.getSUnit()) &&
189 Set.insert(P.getSUnit()).second)
190 Worklist.push_back(P.getSUnit());
193 DEBUG(dbgs() << "Make the predecessors of SU(" << SchedSU->NodeNum
194 << ")'s non-ready successors of " << Priority
195 << " priority in ready queue: ");
196 const auto SetEnd = Set.end();
198 if (Set.find(C.SU) != SetEnd) {
199 C.Priority = Priority;
200 DEBUG(dbgs() << " SU(" << C.SU->NodeNum << ')');
203 DEBUG(dbgs() << '\n');
206 void GCNMinRegScheduler::releaseSuccessors(const SUnit* SU, int Priority) {
207 for (const auto &S : SU->Succs) {
208 auto SuccSU = S.getSUnit();
211 assert(SuccSU->isBoundaryNode() || getNumPreds(SuccSU) > 0);
212 if (!SuccSU->isBoundaryNode() && decNumPreds(SuccSU) == 0)
213 RQ.push_front(*new (Alloc.Allocate()) Candidate(SuccSU, Priority));
217 std::vector<const SUnit*>
218 GCNMinRegScheduler::schedule(ArrayRef<const SUnit*> TopRoots,
219 const ScheduleDAG &DAG) {
220 const auto &SUnits = DAG.SUnits;
221 std::vector<const SUnit*> Schedule;
222 Schedule.reserve(SUnits.size());
224 initNumPreds(SUnits);
228 for (auto SU : TopRoots) {
229 RQ.push_back(*new (Alloc.Allocate()) Candidate(SU, StepNo));
231 releaseSuccessors(&DAG.EntrySU, StepNo);
233 while (!RQ.empty()) {
235 dbgs() << "\n=== Picking candidate, Step = " << StepNo << "\n"
238 dbgs() << ' ' << C.SU->NodeNum << "(P" << C.Priority << ')';
242 auto C = pickCandidate();
246 DEBUG(dbgs() << "Selected "; SU->dump(&DAG));
248 releaseSuccessors(SU, StepNo);
249 Schedule.push_back(SU);
252 if (getReadySuccessors(SU) == 0)
253 bumpPredsPriority(SU, StepNo);
257 assert(SUnits.size() == Schedule.size());
263 std::vector<const SUnit*> makeMinRegSchedule(ArrayRef<const SUnit*> TopRoots,
264 const ScheduleDAG &DAG) {
265 GCNMinRegScheduler S;
266 return S.schedule(TopRoots, DAG);