1 //===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===//
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 //===----------------------------------------------------------------------===//
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 "llvm/CodeGen/ScheduleDAG.h"
23 #include "llvm/CodeGen/SchedulerRegistry.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/ADT/PriorityQueue.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "LatencyPriorityQueue.h"
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);
45 //===----------------------------------------------------------------------===//
46 /// ScheduleDAGList - The actual list scheduler implementation. This supports
47 /// top-down scheduling.
49 class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG {
51 /// AvailableQueue - The priority queue to use for the available SUnits.
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 HazardRecognizer *HazardRec;
65 ScheduleDAGList(SelectionDAG *dag, MachineBasicBlock *bb,
66 const TargetMachine &tm,
67 SchedulingPriorityQueue *availqueue,
69 : ScheduleDAG(dag, bb, tm),
70 AvailableQueue(availqueue), HazardRec(HR) {
75 delete AvailableQueue;
81 void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain);
82 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
83 void ListScheduleTopDown();
85 } // end anonymous namespace
87 HazardRecognizer::~HazardRecognizer() {}
90 /// Schedule - Schedule the DAG using list scheduling.
91 void ScheduleDAGList::Schedule() {
92 DOUT << "********** List Scheduling **********\n";
94 // Build scheduling units.
97 AvailableQueue->initNodes(SUnits);
99 ListScheduleTopDown();
101 AvailableQueue->releaseState();
104 //===----------------------------------------------------------------------===//
105 // Top-Down Scheduling
106 //===----------------------------------------------------------------------===//
108 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
109 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
110 void ScheduleDAGList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
111 --SuccSU->NumPredsLeft;
114 if (SuccSU->NumPredsLeft < 0) {
115 cerr << "*** Scheduling failed! ***\n";
117 cerr << " has been released too many times!\n";
122 // Compute the cycle when this SUnit actually becomes available. This
123 // is the max of the start time of all predecessors plus their latencies.
124 // If this is a token edge, we don't need to wait for the latency of the
125 // preceeding instruction (e.g. a long-latency load) unless there is also
126 // some other data dependence.
127 unsigned PredDoneCycle = SU->Cycle;
129 PredDoneCycle += SU->Latency;
130 else if (SU->Latency)
132 SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle);
134 if (SuccSU->NumPredsLeft == 0) {
135 PendingQueue.push_back(SuccSU);
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 SU->Cycle = CurCycle;
149 // Top down: release successors.
150 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
152 ReleaseSucc(SU, I->Dep, I->isCtrl);
154 SU->isScheduled = true;
155 AvailableQueue->ScheduledNode(SU);
158 /// ListScheduleTopDown - The main loop of list scheduling for top-down
160 void ScheduleDAGList::ListScheduleTopDown() {
161 unsigned CurCycle = 0;
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]->CycleBound == CurCycle) {
181 AvailableQueue->push(PendingQueue[i]);
182 PendingQueue[i]->isAvailable = true;
183 PendingQueue[i] = PendingQueue.back();
184 PendingQueue.pop_back();
187 assert(PendingQueue[i]->CycleBound > 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()) {
198 SUnit *FoundSUnit = 0;
199 SDNode *FoundNode = 0;
201 bool HasNoopHazards = false;
202 while (!AvailableQueue->empty()) {
203 SUnit *CurSUnit = AvailableQueue->pop();
205 // Get the node represented by this SUnit.
206 FoundNode = CurSUnit->getNode();
208 // If this is a pseudo op, like copyfromreg, look to see if there is a
209 // real target node flagged to it. If so, use the target node.
210 while (!FoundNode->isMachineOpcode()) {
211 SDNode *N = FoundNode->getFlaggedNode();
216 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
217 if (HT == HazardRecognizer::NoHazard) {
218 FoundSUnit = CurSUnit;
222 // Remember if this is a noop hazard.
223 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
225 NotReady.push_back(CurSUnit);
228 // Add the nodes that aren't ready back onto the available list.
229 if (!NotReady.empty()) {
230 AvailableQueue->push_all(NotReady);
234 // If we found a node to schedule, do it now.
236 ScheduleNodeTopDown(FoundSUnit, CurCycle);
237 HazardRec->EmitInstruction(FoundNode);
239 // If this is a pseudo-op node, we don't want to increment the current
241 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
243 } else if (!HasNoopHazards) {
244 // Otherwise, we have a pipeline stall, but no other problem, just advance
245 // the current cycle and try again.
246 DOUT << "*** Advancing cycle, no work to do\n";
247 HazardRec->AdvanceCycle();
251 // Otherwise, we have no instructions to issue and we have instructions
252 // that will fault if we don't do this right. This is the case for
253 // processors without pipeline interlocks and other cases.
254 DOUT << "*** Emitting noop\n";
255 HazardRec->EmitNoop();
256 Sequence.push_back(0); // NULL SUnit* -> noop
263 // Verify that all SUnits were scheduled.
264 bool AnyNotSched = false;
265 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
266 if (SUnits[i].NumPredsLeft != 0) {
268 cerr << "*** List scheduling failed! ***\n";
269 SUnits[i].dump(this);
270 cerr << "has not been scheduled!\n";
274 assert(!AnyNotSched);
278 //===----------------------------------------------------------------------===//
279 // Public Constructor Functions
280 //===----------------------------------------------------------------------===//
282 /// createTDListDAGScheduler - This creates a top-down list scheduler with a
283 /// new hazard recognizer. This scheduler takes ownership of the hazard
284 /// recognizer and deletes it when done.
285 ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
287 const TargetMachine *TM,
288 MachineBasicBlock *BB, bool Fast) {
289 return new ScheduleDAGList(DAG, BB, *TM,
290 new LatencyPriorityQueue(),
291 IS->CreateTargetHazardRecognizer());