Indentation.
[llvm/avr.git] / lib / CodeGen / SelectionDAG / ScheduleDAGList.cpp
blobc91ab660dc1027c3c3fa3ed1ca9bf2ad0a6d5913
1 //===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===//
2 //
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 //
10 // This implements a top-down list scheduler, using standard algorithms.
11 // The basic approach uses a priority queue of available nodes to schedule.
12 // One at a time, nodes are taken from the priority queue (thus in priority
13 // order), checked for legality to schedule, and emitted if legal.
15 // Nodes may not be legal to schedule either due to structural hazards (e.g.
16 // pipeline or resource constraints) or because an input to the instruction has
17 // not completed execution.
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "pre-RA-sched"
22 #include "ScheduleDAGSDNodes.h"
23 #include "llvm/CodeGen/LatencyPriorityQueue.h"
24 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
25 #include "llvm/CodeGen/SchedulerRegistry.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetInstrInfo.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/ADT/PriorityQueue.h"
34 #include "llvm/ADT/Statistic.h"
35 #include <climits>
36 using namespace llvm;
38 STATISTIC(NumNoops , "Number of noops inserted");
39 STATISTIC(NumStalls, "Number of pipeline stalls");
41 static RegisterScheduler
42 tdListDAGScheduler("list-td", "Top-down list scheduler",
43 createTDListDAGScheduler);
45 namespace {
46 //===----------------------------------------------------------------------===//
47 /// ScheduleDAGList - The actual list scheduler implementation. This supports
48 /// top-down scheduling.
49 ///
50 class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAGSDNodes {
51 private:
52 /// AvailableQueue - The priority queue to use for the available SUnits.
53 ///
54 SchedulingPriorityQueue *AvailableQueue;
56 /// PendingQueue - This contains all of the instructions whose operands have
57 /// been issued, but their results are not ready yet (due to the latency of
58 /// the operation). Once the operands become available, the instruction is
59 /// added to the AvailableQueue.
60 std::vector<SUnit*> PendingQueue;
62 /// HazardRec - The hazard recognizer to use.
63 ScheduleHazardRecognizer *HazardRec;
65 public:
66 ScheduleDAGList(MachineFunction &mf,
67 SchedulingPriorityQueue *availqueue,
68 ScheduleHazardRecognizer *HR)
69 : ScheduleDAGSDNodes(mf),
70 AvailableQueue(availqueue), HazardRec(HR) {
73 ~ScheduleDAGList() {
74 delete HazardRec;
75 delete AvailableQueue;
78 void Schedule();
80 private:
81 void ReleaseSucc(SUnit *SU, const SDep &D);
82 void ReleaseSuccessors(SUnit *SU);
83 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
84 void ListScheduleTopDown();
86 } // end anonymous namespace
88 /// Schedule - Schedule the DAG using list scheduling.
89 void ScheduleDAGList::Schedule() {
90 DOUT << "********** List Scheduling **********\n";
92 // Build the scheduling graph.
93 BuildSchedGraph();
95 AvailableQueue->initNodes(SUnits);
97 ListScheduleTopDown();
99 AvailableQueue->releaseState();
102 //===----------------------------------------------------------------------===//
103 // Top-Down Scheduling
104 //===----------------------------------------------------------------------===//
106 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
107 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
108 void ScheduleDAGList::ReleaseSucc(SUnit *SU, const SDep &D) {
109 SUnit *SuccSU = D.getSUnit();
110 --SuccSU->NumPredsLeft;
112 #ifndef NDEBUG
113 if (SuccSU->NumPredsLeft < 0) {
114 cerr << "*** Scheduling failed! ***\n";
115 SuccSU->dump(this);
116 cerr << " has been released too many times!\n";
117 llvm_unreachable(0);
119 #endif
121 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
123 // If all the node's predecessors are scheduled, this node is ready
124 // to be scheduled. Ignore the special ExitSU node.
125 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
126 PendingQueue.push_back(SuccSU);
129 void ScheduleDAGList::ReleaseSuccessors(SUnit *SU) {
130 // Top down: release successors.
131 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
132 I != E; ++I) {
133 assert(!I->isAssignedRegDep() &&
134 "The list-td scheduler doesn't yet support physreg dependencies!");
136 ReleaseSucc(SU, *I);
140 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
141 /// count of its successors. If a successor pending count is zero, add it to
142 /// the Available queue.
143 void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
144 DOUT << "*** Scheduling [" << CurCycle << "]: ";
145 DEBUG(SU->dump(this));
147 Sequence.push_back(SU);
148 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
149 SU->setDepthToAtLeast(CurCycle);
151 ReleaseSuccessors(SU);
152 SU->isScheduled = true;
153 AvailableQueue->ScheduledNode(SU);
156 /// ListScheduleTopDown - The main loop of list scheduling for top-down
157 /// schedulers.
158 void ScheduleDAGList::ListScheduleTopDown() {
159 unsigned CurCycle = 0;
161 // Release any successors of the special Entry node.
162 ReleaseSuccessors(&EntrySU);
164 // All leaves to Available queue.
165 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
166 // It is available if it has no predecessors.
167 if (SUnits[i].Preds.empty()) {
168 AvailableQueue->push(&SUnits[i]);
169 SUnits[i].isAvailable = true;
173 // While Available queue is not empty, grab the node with the highest
174 // priority. If it is not ready put it back. Schedule the node.
175 std::vector<SUnit*> NotReady;
176 Sequence.reserve(SUnits.size());
177 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
178 // Check to see if any of the pending instructions are ready to issue. If
179 // so, add them to the available queue.
180 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
181 if (PendingQueue[i]->getDepth() == CurCycle) {
182 AvailableQueue->push(PendingQueue[i]);
183 PendingQueue[i]->isAvailable = true;
184 PendingQueue[i] = PendingQueue.back();
185 PendingQueue.pop_back();
186 --i; --e;
187 } else {
188 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
192 // If there are no instructions available, don't try to issue anything, and
193 // don't advance the hazard recognizer.
194 if (AvailableQueue->empty()) {
195 ++CurCycle;
196 continue;
199 SUnit *FoundSUnit = 0;
201 bool HasNoopHazards = false;
202 while (!AvailableQueue->empty()) {
203 SUnit *CurSUnit = AvailableQueue->pop();
205 ScheduleHazardRecognizer::HazardType HT =
206 HazardRec->getHazardType(CurSUnit);
207 if (HT == ScheduleHazardRecognizer::NoHazard) {
208 FoundSUnit = CurSUnit;
209 break;
212 // Remember if this is a noop hazard.
213 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
215 NotReady.push_back(CurSUnit);
218 // Add the nodes that aren't ready back onto the available list.
219 if (!NotReady.empty()) {
220 AvailableQueue->push_all(NotReady);
221 NotReady.clear();
224 // If we found a node to schedule, do it now.
225 if (FoundSUnit) {
226 ScheduleNodeTopDown(FoundSUnit, CurCycle);
227 HazardRec->EmitInstruction(FoundSUnit);
229 // If this is a pseudo-op node, we don't want to increment the current
230 // cycle.
231 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
232 ++CurCycle;
233 } else if (!HasNoopHazards) {
234 // Otherwise, we have a pipeline stall, but no other problem, just advance
235 // the current cycle and try again.
236 DOUT << "*** Advancing cycle, no work to do\n";
237 HazardRec->AdvanceCycle();
238 ++NumStalls;
239 ++CurCycle;
240 } else {
241 // Otherwise, we have no instructions to issue and we have instructions
242 // that will fault if we don't do this right. This is the case for
243 // processors without pipeline interlocks and other cases.
244 DOUT << "*** Emitting noop\n";
245 HazardRec->EmitNoop();
246 Sequence.push_back(0); // NULL here means noop
247 ++NumNoops;
248 ++CurCycle;
252 #ifndef NDEBUG
253 VerifySchedule(/*isBottomUp=*/false);
254 #endif
257 //===----------------------------------------------------------------------===//
258 // Public Constructor Functions
259 //===----------------------------------------------------------------------===//
261 /// createTDListDAGScheduler - This creates a top-down list scheduler with a
262 /// new hazard recognizer. This scheduler takes ownership of the hazard
263 /// recognizer and deletes it when done.
264 ScheduleDAGSDNodes *
265 llvm::createTDListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
266 return new ScheduleDAGList(*IS->MF,
267 new LatencyPriorityQueue(),
268 IS->CreateTargetHazardRecognizer());