We're not going to spend 100% of time in interrupts, do we? :)
[llvm/msp430.git] / lib / CodeGen / SelectionDAG / ScheduleDAGList.cpp
blobc4325349990d3b989a5ac9face71d887e622bbc5
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/ADT/PriorityQueue.h"
33 #include "llvm/ADT/Statistic.h"
34 #include <climits>
35 using namespace llvm;
37 STATISTIC(NumNoops , "Number of noops inserted");
38 STATISTIC(NumStalls, "Number of pipeline stalls");
40 static RegisterScheduler
41 tdListDAGScheduler("list-td", "Top-down list scheduler",
42 createTDListDAGScheduler);
44 namespace {
45 //===----------------------------------------------------------------------===//
46 /// ScheduleDAGList - The actual list scheduler implementation. This supports
47 /// top-down scheduling.
48 ///
49 class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAGSDNodes {
50 private:
51 /// AvailableQueue - The priority queue to use for the available SUnits.
52 ///
53 SchedulingPriorityQueue *AvailableQueue;
55 /// PendingQueue - This contains all of the instructions whose operands have
56 /// been issued, but their results are not ready yet (due to the latency of
57 /// the operation). Once the operands become available, the instruction is
58 /// added to the AvailableQueue.
59 std::vector<SUnit*> PendingQueue;
61 /// HazardRec - The hazard recognizer to use.
62 ScheduleHazardRecognizer *HazardRec;
64 public:
65 ScheduleDAGList(MachineFunction &mf,
66 SchedulingPriorityQueue *availqueue,
67 ScheduleHazardRecognizer *HR)
68 : ScheduleDAGSDNodes(mf),
69 AvailableQueue(availqueue), HazardRec(HR) {
72 ~ScheduleDAGList() {
73 delete HazardRec;
74 delete AvailableQueue;
77 void Schedule();
79 private:
80 void ReleaseSucc(SUnit *SU, const SDep &D);
81 void ReleaseSuccessors(SUnit *SU);
82 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
83 void ListScheduleTopDown();
85 } // end anonymous namespace
87 /// Schedule - Schedule the DAG using list scheduling.
88 void ScheduleDAGList::Schedule() {
89 DOUT << "********** List Scheduling **********\n";
91 // Build the scheduling graph.
92 BuildSchedGraph();
94 AvailableQueue->initNodes(SUnits);
96 ListScheduleTopDown();
98 AvailableQueue->releaseState();
101 //===----------------------------------------------------------------------===//
102 // Top-Down Scheduling
103 //===----------------------------------------------------------------------===//
105 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
106 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
107 void ScheduleDAGList::ReleaseSucc(SUnit *SU, const SDep &D) {
108 SUnit *SuccSU = D.getSUnit();
109 --SuccSU->NumPredsLeft;
111 #ifndef NDEBUG
112 if (SuccSU->NumPredsLeft < 0) {
113 cerr << "*** Scheduling failed! ***\n";
114 SuccSU->dump(this);
115 cerr << " has been released too many times!\n";
116 assert(0);
118 #endif
120 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
122 // If all the node's predecessors are scheduled, this node is ready
123 // to be scheduled. Ignore the special ExitSU node.
124 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
125 PendingQueue.push_back(SuccSU);
128 void ScheduleDAGList::ReleaseSuccessors(SUnit *SU) {
129 // Top down: release successors.
130 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
131 I != E; ++I) {
132 assert(!I->isAssignedRegDep() &&
133 "The list-td scheduler doesn't yet support physreg dependencies!");
135 ReleaseSucc(SU, *I);
139 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
140 /// count of its successors. If a successor pending count is zero, add it to
141 /// the Available queue.
142 void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
143 DOUT << "*** Scheduling [" << CurCycle << "]: ";
144 DEBUG(SU->dump(this));
146 Sequence.push_back(SU);
147 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
148 SU->setDepthToAtLeast(CurCycle);
150 ReleaseSuccessors(SU);
151 SU->isScheduled = true;
152 AvailableQueue->ScheduledNode(SU);
155 /// ListScheduleTopDown - The main loop of list scheduling for top-down
156 /// schedulers.
157 void ScheduleDAGList::ListScheduleTopDown() {
158 unsigned CurCycle = 0;
160 // Release any successors of the special Entry node.
161 ReleaseSuccessors(&EntrySU);
163 // All leaves to Available queue.
164 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
165 // It is available if it has no predecessors.
166 if (SUnits[i].Preds.empty()) {
167 AvailableQueue->push(&SUnits[i]);
168 SUnits[i].isAvailable = true;
172 // While Available queue is not empty, grab the node with the highest
173 // priority. If it is not ready put it back. Schedule the node.
174 std::vector<SUnit*> NotReady;
175 Sequence.reserve(SUnits.size());
176 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
177 // Check to see if any of the pending instructions are ready to issue. If
178 // so, add them to the available queue.
179 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
180 if (PendingQueue[i]->getDepth() == CurCycle) {
181 AvailableQueue->push(PendingQueue[i]);
182 PendingQueue[i]->isAvailable = true;
183 PendingQueue[i] = PendingQueue.back();
184 PendingQueue.pop_back();
185 --i; --e;
186 } else {
187 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
191 // If there are no instructions available, don't try to issue anything, and
192 // don't advance the hazard recognizer.
193 if (AvailableQueue->empty()) {
194 ++CurCycle;
195 continue;
198 SUnit *FoundSUnit = 0;
200 bool HasNoopHazards = false;
201 while (!AvailableQueue->empty()) {
202 SUnit *CurSUnit = AvailableQueue->pop();
204 ScheduleHazardRecognizer::HazardType HT =
205 HazardRec->getHazardType(CurSUnit);
206 if (HT == ScheduleHazardRecognizer::NoHazard) {
207 FoundSUnit = CurSUnit;
208 break;
211 // Remember if this is a noop hazard.
212 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
214 NotReady.push_back(CurSUnit);
217 // Add the nodes that aren't ready back onto the available list.
218 if (!NotReady.empty()) {
219 AvailableQueue->push_all(NotReady);
220 NotReady.clear();
223 // If we found a node to schedule, do it now.
224 if (FoundSUnit) {
225 ScheduleNodeTopDown(FoundSUnit, CurCycle);
226 HazardRec->EmitInstruction(FoundSUnit);
228 // If this is a pseudo-op node, we don't want to increment the current
229 // cycle.
230 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
231 ++CurCycle;
232 } else if (!HasNoopHazards) {
233 // Otherwise, we have a pipeline stall, but no other problem, just advance
234 // the current cycle and try again.
235 DOUT << "*** Advancing cycle, no work to do\n";
236 HazardRec->AdvanceCycle();
237 ++NumStalls;
238 ++CurCycle;
239 } else {
240 // Otherwise, we have no instructions to issue and we have instructions
241 // that will fault if we don't do this right. This is the case for
242 // processors without pipeline interlocks and other cases.
243 DOUT << "*** Emitting noop\n";
244 HazardRec->EmitNoop();
245 Sequence.push_back(0); // NULL here means noop
246 ++NumNoops;
247 ++CurCycle;
251 #ifndef NDEBUG
252 VerifySchedule(/*isBottomUp=*/false);
253 #endif
256 //===----------------------------------------------------------------------===//
257 // Public Constructor Functions
258 //===----------------------------------------------------------------------===//
260 /// createTDListDAGScheduler - This creates a top-down list scheduler with a
261 /// new hazard recognizer. This scheduler takes ownership of the hazard
262 /// recognizer and deletes it when done.
263 ScheduleDAGSDNodes *
264 llvm::createTDListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
265 return new ScheduleDAGList(*IS->MF,
266 new LatencyPriorityQueue(),
267 IS->CreateTargetHazardRecognizer());