blob: ab06b55b49fdda9404458ed8f02abb3948edfb1e [file] [log] [blame]
Andrew Trickd06df962012-02-01 22:13:57 +00001//===- ScheduleDAGVLIW.cpp - SelectionDAG list scheduler for VLIW -*- C++ -*-=//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Trickd06df962012-02-01 22:13:57 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This implements a top-down list scheduler, using standard algorithms.
10// The basic approach uses a priority queue of available nodes to schedule.
11// One at a time, nodes are taken from the priority queue (thus in priority
12// order), checked for legality to schedule, and emitted if legal.
13//
14// Nodes may not be legal to schedule either due to structural hazards (e.g.
15// pipeline or resource constraints) or because an input to the instruction has
16// not completed execution.
17//
18//===----------------------------------------------------------------------===//
19
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "ScheduleDAGSDNodes.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/CodeGen/LatencyPriorityQueue.h"
23#include "llvm/CodeGen/ResourcePriorityQueue.h"
24#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/CodeGen/SchedulerRegistry.h"
Andrew Trickd06df962012-02-01 22:13:57 +000026#include "llvm/CodeGen/SelectionDAGISel.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000027#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000028#include "llvm/CodeGen/TargetRegisterInfo.h"
29#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DataLayout.h"
Andrew Trickd06df962012-02-01 22:13:57 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
Andrew Trickd06df962012-02-01 22:13:57 +000034#include <climits>
35using namespace llvm;
36
Chandler Carruth1b9dde02014-04-22 02:02:50 +000037#define DEBUG_TYPE "pre-RA-sched"
38
Andrew Trickd06df962012-02-01 22:13:57 +000039STATISTIC(NumNoops , "Number of noops inserted");
40STATISTIC(NumStalls, "Number of pipeline stalls");
41
42static RegisterScheduler
43 VLIWScheduler("vliw-td", "VLIW scheduler",
44 createVLIWDAGScheduler);
45
46namespace {
47//===----------------------------------------------------------------------===//
48/// ScheduleDAGVLIW - The actual DFA list scheduler implementation. This
49/// supports / top-down scheduling.
50///
51class ScheduleDAGVLIW : public ScheduleDAGSDNodes {
52private:
53 /// AvailableQueue - The priority queue to use for the available SUnits.
54 ///
55 SchedulingPriorityQueue *AvailableQueue;
56
57 /// PendingQueue - This contains all of the instructions whose operands have
58 /// been issued, but their results are not ready yet (due to the latency of
59 /// the operation). Once the operands become available, the instruction is
60 /// added to the AvailableQueue.
61 std::vector<SUnit*> PendingQueue;
62
63 /// HazardRec - The hazard recognizer to use.
64 ScheduleHazardRecognizer *HazardRec;
65
66 /// AA - AliasAnalysis for making memory reference queries.
67 AliasAnalysis *AA;
68
69public:
70 ScheduleDAGVLIW(MachineFunction &mf,
71 AliasAnalysis *aa,
72 SchedulingPriorityQueue *availqueue)
73 : ScheduleDAGSDNodes(mf), AvailableQueue(availqueue), AA(aa) {
Eric Christopheredba30c2014-10-09 06:28:06 +000074 const TargetSubtargetInfo &STI = mf.getSubtarget();
75 HazardRec = STI.getInstrInfo()->CreateTargetHazardRecognizer(&STI, this);
Andrew Trickd06df962012-02-01 22:13:57 +000076 }
77
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000078 ~ScheduleDAGVLIW() override {
Andrew Trickd06df962012-02-01 22:13:57 +000079 delete HazardRec;
80 delete AvailableQueue;
81 }
82
Craig Topper7b883b32014-03-08 06:31:39 +000083 void Schedule() override;
Andrew Trickd06df962012-02-01 22:13:57 +000084
85private:
86 void releaseSucc(SUnit *SU, const SDep &D);
87 void releaseSuccessors(SUnit *SU);
88 void scheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
89 void listScheduleTopDown();
90};
91} // end anonymous namespace
92
93/// Schedule - Schedule the DAG using list scheduling.
94void ScheduleDAGVLIW::Schedule() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000095 LLVM_DEBUG(dbgs() << "********** List Scheduling " << printMBBReference(*BB)
96 << " '" << BB->getName() << "' **********\n");
Andrew Trickd06df962012-02-01 22:13:57 +000097
98 // Build the scheduling graph.
99 BuildSchedGraph(AA);
100
101 AvailableQueue->initNodes(SUnits);
102
103 listScheduleTopDown();
104
105 AvailableQueue->releaseState();
106}
107
108//===----------------------------------------------------------------------===//
109// Top-Down Scheduling
110//===----------------------------------------------------------------------===//
111
112/// releaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
113/// the PendingQueue if the count reaches zero. Also update its cycle bound.
114void ScheduleDAGVLIW::releaseSucc(SUnit *SU, const SDep &D) {
115 SUnit *SuccSU = D.getSUnit();
116
117#ifndef NDEBUG
118 if (SuccSU->NumPredsLeft == 0) {
119 dbgs() << "*** Scheduling failed! ***\n";
Matthias Braun726e12c2018-09-19 00:23:35 +0000120 dumpNode(*SuccSU);
Andrew Trickd06df962012-02-01 22:13:57 +0000121 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000122 llvm_unreachable(nullptr);
Andrew Trickd06df962012-02-01 22:13:57 +0000123 }
124#endif
Andrew Trickf1ff84c2012-11-12 19:28:57 +0000125 assert(!D.isWeak() && "unexpected artificial DAG edge");
126
Andrew Trickd06df962012-02-01 22:13:57 +0000127 --SuccSU->NumPredsLeft;
128
129 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
130
131 // If all the node's predecessors are scheduled, this node is ready
132 // to be scheduled. Ignore the special ExitSU node.
133 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
134 PendingQueue.push_back(SuccSU);
135 }
136}
137
138void ScheduleDAGVLIW::releaseSuccessors(SUnit *SU) {
139 // Top down: release successors.
140 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
141 I != E; ++I) {
142 assert(!I->isAssignedRegDep() &&
143 "The list-td scheduler doesn't yet support physreg dependencies!");
144
145 releaseSucc(SU, *I);
146 }
147}
148
149/// scheduleNodeTopDown - Add the node to the schedule. Decrement the pending
150/// count of its successors. If a successor pending count is zero, add it to
151/// the Available queue.
152void ScheduleDAGVLIW::scheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000153 LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Matthias Braun726e12c2018-09-19 00:23:35 +0000154 LLVM_DEBUG(dumpNode(*SU));
Andrew Trickd06df962012-02-01 22:13:57 +0000155
156 Sequence.push_back(SU);
157 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
158 SU->setDepthToAtLeast(CurCycle);
159
160 releaseSuccessors(SU);
161 SU->isScheduled = true;
Andrew Trick52226d42012-03-07 23:00:49 +0000162 AvailableQueue->scheduledNode(SU);
Andrew Trickd06df962012-02-01 22:13:57 +0000163}
164
165/// listScheduleTopDown - The main loop of list scheduling for top-down
166/// schedulers.
167void ScheduleDAGVLIW::listScheduleTopDown() {
168 unsigned CurCycle = 0;
169
170 // Release any successors of the special Entry node.
171 releaseSuccessors(&EntrySU);
172
173 // All leaves to AvailableQueue.
174 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
175 // It is available if it has no predecessors.
176 if (SUnits[i].Preds.empty()) {
177 AvailableQueue->push(&SUnits[i]);
178 SUnits[i].isAvailable = true;
179 }
180 }
181
182 // While AvailableQueue is not empty, grab the node with the highest
183 // priority. If it is not ready put it back. Schedule the node.
184 std::vector<SUnit*> NotReady;
185 Sequence.reserve(SUnits.size());
186 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
187 // Check to see if any of the pending instructions are ready to issue. If
188 // so, add them to the available queue.
189 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
190 if (PendingQueue[i]->getDepth() == CurCycle) {
191 AvailableQueue->push(PendingQueue[i]);
192 PendingQueue[i]->isAvailable = true;
193 PendingQueue[i] = PendingQueue.back();
194 PendingQueue.pop_back();
195 --i; --e;
196 }
197 else {
198 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
199 }
200 }
201
202 // If there are no instructions available, don't try to issue anything, and
203 // don't advance the hazard recognizer.
204 if (AvailableQueue->empty()) {
205 // Reset DFA state.
Craig Topperc0196b12014-04-14 00:51:57 +0000206 AvailableQueue->scheduledNode(nullptr);
Andrew Trickd06df962012-02-01 22:13:57 +0000207 ++CurCycle;
208 continue;
209 }
210
Craig Topperc0196b12014-04-14 00:51:57 +0000211 SUnit *FoundSUnit = nullptr;
Andrew Trickd06df962012-02-01 22:13:57 +0000212
213 bool HasNoopHazards = false;
214 while (!AvailableQueue->empty()) {
215 SUnit *CurSUnit = AvailableQueue->pop();
216
217 ScheduleHazardRecognizer::HazardType HT =
218 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
219 if (HT == ScheduleHazardRecognizer::NoHazard) {
220 FoundSUnit = CurSUnit;
221 break;
222 }
223
224 // Remember if this is a noop hazard.
225 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
226
227 NotReady.push_back(CurSUnit);
228 }
229
230 // Add the nodes that aren't ready back onto the available list.
231 if (!NotReady.empty()) {
232 AvailableQueue->push_all(NotReady);
233 NotReady.clear();
234 }
235
236 // If we found a node to schedule, do it now.
237 if (FoundSUnit) {
238 scheduleNodeTopDown(FoundSUnit, CurCycle);
239 HazardRec->EmitInstruction(FoundSUnit);
240
241 // If this is a pseudo-op node, we don't want to increment the current
242 // cycle.
243 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
244 ++CurCycle;
245 } else if (!HasNoopHazards) {
246 // Otherwise, we have a pipeline stall, but no other problem, just advance
247 // the current cycle and try again.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000248 LLVM_DEBUG(dbgs() << "*** Advancing cycle, no work to do\n");
Andrew Trickd06df962012-02-01 22:13:57 +0000249 HazardRec->AdvanceCycle();
250 ++NumStalls;
251 ++CurCycle;
252 } else {
253 // Otherwise, we have no instructions to issue and we have instructions
254 // that will fault if we don't do this right. This is the case for
255 // processors without pipeline interlocks and other cases.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000256 LLVM_DEBUG(dbgs() << "*** Emitting noop\n");
Andrew Trickd06df962012-02-01 22:13:57 +0000257 HazardRec->EmitNoop();
Craig Topperc0196b12014-04-14 00:51:57 +0000258 Sequence.push_back(nullptr); // NULL here means noop
Andrew Trickd06df962012-02-01 22:13:57 +0000259 ++NumNoops;
260 ++CurCycle;
261 }
262 }
263
264#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000265 VerifyScheduledSequence(/*isBottomUp=*/false);
Andrew Trickd06df962012-02-01 22:13:57 +0000266#endif
267}
268
269//===----------------------------------------------------------------------===//
270// Public Constructor Functions
271//===----------------------------------------------------------------------===//
272
273/// createVLIWDAGScheduler - This creates a top-down list scheduler.
274ScheduleDAGSDNodes *
275llvm::createVLIWDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
276 return new ScheduleDAGVLIW(*IS->MF, IS->AA, new ResourcePriorityQueue(IS));
277}