Remove now-unused constructors.
[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/ADT/PriorityQueue.h"
33 #include "llvm/ADT/Statistic.h"
34 #include <climits>
35 using namespace llvm;
36
37 STATISTIC(NumNoops , "Number of noops inserted");
38 STATISTIC(NumStalls, "Number of pipeline stalls");
39
40 static RegisterScheduler
41   tdListDAGScheduler("list-td", "Top-down list scheduler",
42                      createTDListDAGScheduler);
43    
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;
54   
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;
60
61   /// HazardRec - The hazard recognizer to use.
62   ScheduleHazardRecognizer *HazardRec;
63
64 public:
65   ScheduleDAGList(MachineFunction &mf,
66                   SchedulingPriorityQueue *availqueue,
67                   ScheduleHazardRecognizer *HR)
68     : ScheduleDAGSDNodes(mf),
69       AvailableQueue(availqueue), HazardRec(HR) {
70     }
71
72   ~ScheduleDAGList() {
73     delete HazardRec;
74     delete AvailableQueue;
75   }
76
77   void Schedule();
78
79 private:
80   void ReleaseSucc(SUnit *SU, const SDep &D);
81   void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
82   void ListScheduleTopDown();
83 };
84 }  // end anonymous namespace
85
86 /// Schedule - Schedule the DAG using list scheduling.
87 void ScheduleDAGList::Schedule() {
88   DOUT << "********** List Scheduling **********\n";
89   
90   // Build the scheduling graph.
91   BuildSchedGraph();
92
93   AvailableQueue->initNodes(SUnits);
94   
95   ListScheduleTopDown();
96   
97   AvailableQueue->releaseState();
98 }
99
100 //===----------------------------------------------------------------------===//
101 //  Top-Down Scheduling
102 //===----------------------------------------------------------------------===//
103
104 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
105 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
106 void ScheduleDAGList::ReleaseSucc(SUnit *SU, const SDep &D) {
107   SUnit *SuccSU = D.getSUnit();
108   --SuccSU->NumPredsLeft;
109   
110 #ifndef NDEBUG
111   if (SuccSU->NumPredsLeft < 0) {
112     cerr << "*** Scheduling failed! ***\n";
113     SuccSU->dump(this);
114     cerr << " has been released too many times!\n";
115     assert(0);
116   }
117 #endif
118   
119   SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
120   
121   if (SuccSU->NumPredsLeft == 0) {
122     PendingQueue.push_back(SuccSU);
123   }
124 }
125
126 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
127 /// count of its successors. If a successor pending count is zero, add it to
128 /// the Available queue.
129 void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
130   DOUT << "*** Scheduling [" << CurCycle << "]: ";
131   DEBUG(SU->dump(this));
132   
133   Sequence.push_back(SU);
134   assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
135   SU->setDepthToAtLeast(CurCycle);
136
137   // Top down: release successors.
138   for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
139        I != E; ++I) {
140     assert(!I->isAssignedRegDep() &&
141            "The list-td scheduler doesn't yet support physreg dependencies!");
142
143     ReleaseSucc(SU, *I);
144   }
145
146   SU->isScheduled = true;
147   AvailableQueue->ScheduledNode(SU);
148 }
149
150 /// ListScheduleTopDown - The main loop of list scheduling for top-down
151 /// schedulers.
152 void ScheduleDAGList::ListScheduleTopDown() {
153   unsigned CurCycle = 0;
154
155   // All leaves to Available queue.
156   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
157     // It is available if it has no predecessors.
158     if (SUnits[i].Preds.empty()) {
159       AvailableQueue->push(&SUnits[i]);
160       SUnits[i].isAvailable = true;
161     }
162   }
163   
164   // While Available queue is not empty, grab the node with the highest
165   // priority. If it is not ready put it back.  Schedule the node.
166   std::vector<SUnit*> NotReady;
167   Sequence.reserve(SUnits.size());
168   while (!AvailableQueue->empty() || !PendingQueue.empty()) {
169     // Check to see if any of the pending instructions are ready to issue.  If
170     // so, add them to the available queue.
171     for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
172       if (PendingQueue[i]->getDepth() == CurCycle) {
173         AvailableQueue->push(PendingQueue[i]);
174         PendingQueue[i]->isAvailable = true;
175         PendingQueue[i] = PendingQueue.back();
176         PendingQueue.pop_back();
177         --i; --e;
178       } else {
179         assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
180       }
181     }
182     
183     // If there are no instructions available, don't try to issue anything, and
184     // don't advance the hazard recognizer.
185     if (AvailableQueue->empty()) {
186       ++CurCycle;
187       continue;
188     }
189
190     SUnit *FoundSUnit = 0;
191     
192     bool HasNoopHazards = false;
193     while (!AvailableQueue->empty()) {
194       SUnit *CurSUnit = AvailableQueue->pop();
195       
196       ScheduleHazardRecognizer::HazardType HT =
197         HazardRec->getHazardType(CurSUnit);
198       if (HT == ScheduleHazardRecognizer::NoHazard) {
199         FoundSUnit = CurSUnit;
200         break;
201       }
202     
203       // Remember if this is a noop hazard.
204       HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
205       
206       NotReady.push_back(CurSUnit);
207     }
208     
209     // Add the nodes that aren't ready back onto the available list.
210     if (!NotReady.empty()) {
211       AvailableQueue->push_all(NotReady);
212       NotReady.clear();
213     }
214
215     // If we found a node to schedule, do it now.
216     if (FoundSUnit) {
217       ScheduleNodeTopDown(FoundSUnit, CurCycle);
218       HazardRec->EmitInstruction(FoundSUnit);
219
220       // If this is a pseudo-op node, we don't want to increment the current
221       // cycle.
222       if (FoundSUnit->Latency)  // Don't increment CurCycle for pseudo-ops!
223         ++CurCycle;        
224     } else if (!HasNoopHazards) {
225       // Otherwise, we have a pipeline stall, but no other problem, just advance
226       // the current cycle and try again.
227       DOUT << "*** Advancing cycle, no work to do\n";
228       HazardRec->AdvanceCycle();
229       ++NumStalls;
230       ++CurCycle;
231     } else {
232       // Otherwise, we have no instructions to issue and we have instructions
233       // that will fault if we don't do this right.  This is the case for
234       // processors without pipeline interlocks and other cases.
235       DOUT << "*** Emitting noop\n";
236       HazardRec->EmitNoop();
237       Sequence.push_back(0);   // NULL here means noop
238       ++NumNoops;
239       ++CurCycle;
240     }
241   }
242
243 #ifndef NDEBUG
244   VerifySchedule(/*isBottomUp=*/false);
245 #endif
246 }
247
248 //===----------------------------------------------------------------------===//
249 //                         Public Constructor Functions
250 //===----------------------------------------------------------------------===//
251
252 /// createTDListDAGScheduler - This creates a top-down list scheduler with a
253 /// new hazard recognizer. This scheduler takes ownership of the hazard
254 /// recognizer and deletes it when done.
255 ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
256                                             bool Fast) {
257   return new ScheduleDAGList(*IS->MF,
258                              new LatencyPriorityQueue(),
259                              IS->CreateTargetHazardRecognizer());
260 }