1 //===- ScheduleDAGVLIW.cpp - SelectionDAG list scheduler for VLIW -*- C++ -*-=//
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/SchedulerRegistry.h"
23 #include "ScheduleDAGSDNodes.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/CodeGen/LatencyPriorityQueue.h"
26 #include "llvm/CodeGen/ResourcePriorityQueue.h"
27 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Target/TargetInstrInfo.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
38 STATISTIC(NumNoops , "Number of noops inserted");
39 STATISTIC(NumStalls, "Number of pipeline stalls");
41 static RegisterScheduler
42 VLIWScheduler("vliw-td", "VLIW scheduler",
43 createVLIWDAGScheduler);
46 //===----------------------------------------------------------------------===//
47 /// ScheduleDAGVLIW - The actual DFA list scheduler implementation. This
48 /// supports / top-down scheduling.
50 class ScheduleDAGVLIW : public ScheduleDAGSDNodes {
52 /// AvailableQueue - The priority queue to use for the available SUnits.
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 /// AA - AliasAnalysis for making memory reference queries.
69 ScheduleDAGVLIW(MachineFunction &mf,
71 SchedulingPriorityQueue *availqueue)
72 : ScheduleDAGSDNodes(mf), AvailableQueue(availqueue), AA(aa) {
74 const TargetMachine &tm = mf.getTarget();
75 HazardRec = tm.getInstrInfo()->CreateTargetHazardRecognizer(&tm, this);
80 delete AvailableQueue;
86 void releaseSucc(SUnit *SU, const SDep &D);
87 void releaseSuccessors(SUnit *SU);
88 void scheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
89 void listScheduleTopDown();
91 } // end anonymous namespace
93 /// Schedule - Schedule the DAG using list scheduling.
94 void ScheduleDAGVLIW::Schedule() {
96 << "********** List Scheduling BB#" << BB->getNumber()
97 << " '" << BB->getName() << "' **********\n");
99 // Build the scheduling graph.
102 AvailableQueue->initNodes(SUnits);
104 listScheduleTopDown();
106 AvailableQueue->releaseState();
109 //===----------------------------------------------------------------------===//
110 // Top-Down Scheduling
111 //===----------------------------------------------------------------------===//
113 /// releaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
114 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
115 void ScheduleDAGVLIW::releaseSucc(SUnit *SU, const SDep &D) {
116 SUnit *SuccSU = D.getSUnit();
119 if (SuccSU->NumPredsLeft == 0) {
120 dbgs() << "*** Scheduling failed! ***\n";
122 dbgs() << " has been released too many times!\n";
126 assert(!D.isWeak() && "unexpected artificial DAG edge");
128 --SuccSU->NumPredsLeft;
130 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
132 // If all the node's predecessors are scheduled, this node is ready
133 // to be scheduled. Ignore the special ExitSU node.
134 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
135 PendingQueue.push_back(SuccSU);
139 void ScheduleDAGVLIW::releaseSuccessors(SUnit *SU) {
140 // Top down: release successors.
141 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
143 assert(!I->isAssignedRegDep() &&
144 "The list-td scheduler doesn't yet support physreg dependencies!");
150 /// scheduleNodeTopDown - Add the node to the schedule. Decrement the pending
151 /// count of its successors. If a successor pending count is zero, add it to
152 /// the Available queue.
153 void ScheduleDAGVLIW::scheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
154 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
155 DEBUG(SU->dump(this));
157 Sequence.push_back(SU);
158 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
159 SU->setDepthToAtLeast(CurCycle);
161 releaseSuccessors(SU);
162 SU->isScheduled = true;
163 AvailableQueue->scheduledNode(SU);
166 /// listScheduleTopDown - The main loop of list scheduling for top-down
168 void ScheduleDAGVLIW::listScheduleTopDown() {
169 unsigned CurCycle = 0;
171 // Release any successors of the special Entry node.
172 releaseSuccessors(&EntrySU);
174 // All leaves to AvailableQueue.
175 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
176 // It is available if it has no predecessors.
177 if (SUnits[i].Preds.empty()) {
178 AvailableQueue->push(&SUnits[i]);
179 SUnits[i].isAvailable = true;
183 // While AvailableQueue is not empty, grab the node with the highest
184 // priority. If it is not ready put it back. Schedule the node.
185 std::vector<SUnit*> NotReady;
186 Sequence.reserve(SUnits.size());
187 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
188 // Check to see if any of the pending instructions are ready to issue. If
189 // so, add them to the available queue.
190 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
191 if (PendingQueue[i]->getDepth() == CurCycle) {
192 AvailableQueue->push(PendingQueue[i]);
193 PendingQueue[i]->isAvailable = true;
194 PendingQueue[i] = PendingQueue.back();
195 PendingQueue.pop_back();
199 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
203 // If there are no instructions available, don't try to issue anything, and
204 // don't advance the hazard recognizer.
205 if (AvailableQueue->empty()) {
207 AvailableQueue->scheduledNode(0);
212 SUnit *FoundSUnit = 0;
214 bool HasNoopHazards = false;
215 while (!AvailableQueue->empty()) {
216 SUnit *CurSUnit = AvailableQueue->pop();
218 ScheduleHazardRecognizer::HazardType HT =
219 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
220 if (HT == ScheduleHazardRecognizer::NoHazard) {
221 FoundSUnit = CurSUnit;
225 // Remember if this is a noop hazard.
226 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
228 NotReady.push_back(CurSUnit);
231 // Add the nodes that aren't ready back onto the available list.
232 if (!NotReady.empty()) {
233 AvailableQueue->push_all(NotReady);
237 // If we found a node to schedule, do it now.
239 scheduleNodeTopDown(FoundSUnit, CurCycle);
240 HazardRec->EmitInstruction(FoundSUnit);
242 // If this is a pseudo-op node, we don't want to increment the current
244 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
246 } else if (!HasNoopHazards) {
247 // Otherwise, we have a pipeline stall, but no other problem, just advance
248 // the current cycle and try again.
249 DEBUG(dbgs() << "*** Advancing cycle, no work to do\n");
250 HazardRec->AdvanceCycle();
254 // Otherwise, we have no instructions to issue and we have instructions
255 // that will fault if we don't do this right. This is the case for
256 // processors without pipeline interlocks and other cases.
257 DEBUG(dbgs() << "*** Emitting noop\n");
258 HazardRec->EmitNoop();
259 Sequence.push_back(0); // NULL here means noop
266 VerifyScheduledSequence(/*isBottomUp=*/false);
270 //===----------------------------------------------------------------------===//
271 // Public Constructor Functions
272 //===----------------------------------------------------------------------===//
274 /// createVLIWDAGScheduler - This creates a top-down list scheduler.
276 llvm::createVLIWDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
277 return new ScheduleDAGVLIW(*IS->MF, IS->AA, new ResourcePriorityQueue(IS));