afce34879c2c0f1bbef2d5d17217f63c68f966ea
[oota-llvm.git] / lib / CodeGen / SelectionDAG / ScheduleDAGList.cpp
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.
14 //
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.
18 //
19 //===----------------------------------------------------------------------===//
20
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;
37
38 STATISTIC(NumNoops , "Number of noops inserted");
39 STATISTIC(NumStalls, "Number of pipeline stalls");
40
41 static RegisterScheduler
42   tdListDAGScheduler("list-td", "Top-down list scheduler",
43                      createTDListDAGScheduler);
44    
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;
55   
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;
61
62   /// HazardRec - The hazard recognizer to use.
63   ScheduleHazardRecognizer *HazardRec;
64
65 public:
66   ScheduleDAGList(MachineFunction &mf,
67                   SchedulingPriorityQueue *availqueue,
68                   ScheduleHazardRecognizer *HR)
69     : ScheduleDAGSDNodes(mf),
70       AvailableQueue(availqueue), HazardRec(HR) {
71     }
72
73   ~ScheduleDAGList() {
74     delete HazardRec;
75     delete AvailableQueue;
76   }
77
78   void Schedule();
79
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();
85 };
86 }  // end anonymous namespace
87
88 /// Schedule - Schedule the DAG using list scheduling.
89 void ScheduleDAGList::Schedule() {
90   DOUT << "********** List Scheduling **********\n";
91   
92   // Build the scheduling graph.
93   BuildSchedGraph();
94
95   AvailableQueue->initNodes(SUnits);
96   
97   ListScheduleTopDown();
98   
99   AvailableQueue->releaseState();
100 }
101
102 //===----------------------------------------------------------------------===//
103 //  Top-Down Scheduling
104 //===----------------------------------------------------------------------===//
105
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;
111   
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();
118   }
119 #endif
120   
121   SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
122   
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);
127 }
128
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!");
135
136     ReleaseSucc(SU, *I);
137   }
138 }
139
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));
146   
147   Sequence.push_back(SU);
148   assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
149   SU->setDepthToAtLeast(CurCycle);
150
151   ReleaseSuccessors(SU);
152   SU->isScheduled = true;
153   AvailableQueue->ScheduledNode(SU);
154 }
155
156 /// ListScheduleTopDown - The main loop of list scheduling for top-down
157 /// schedulers.
158 void ScheduleDAGList::ListScheduleTopDown() {
159   unsigned CurCycle = 0;
160
161   // Release any successors of the special Entry node.
162   ReleaseSuccessors(&EntrySU);
163
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;
170     }
171   }
172   
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?");
189       }
190     }
191     
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;
197     }
198
199     SUnit *FoundSUnit = 0;
200     
201     bool HasNoopHazards = false;
202     while (!AvailableQueue->empty()) {
203       SUnit *CurSUnit = AvailableQueue->pop();
204       
205       ScheduleHazardRecognizer::HazardType HT =
206         HazardRec->getHazardType(CurSUnit);
207       if (HT == ScheduleHazardRecognizer::NoHazard) {
208         FoundSUnit = CurSUnit;
209         break;
210       }
211     
212       // Remember if this is a noop hazard.
213       HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
214       
215       NotReady.push_back(CurSUnit);
216     }
217     
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();
222     }
223
224     // If we found a node to schedule, do it now.
225     if (FoundSUnit) {
226       ScheduleNodeTopDown(FoundSUnit, CurCycle);
227       HazardRec->EmitInstruction(FoundSUnit);
228
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;
249     }
250   }
251
252 #ifndef NDEBUG
253   VerifySchedule(/*isBottomUp=*/false);
254 #endif
255 }
256
257 //===----------------------------------------------------------------------===//
258 //                         Public Constructor Functions
259 //===----------------------------------------------------------------------===//
260
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());
269 }