blob: e63484e987d41a9b52e92c31f517c794ff635ad7 [file] [log] [blame]
Evan Chengab495562006-01-25 09:14:32 +00001//===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===//
Evan Cheng31272342006-01-23 08:26:10 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng31272342006-01-23 08:26:10 +00007//
8//===----------------------------------------------------------------------===//
9//
Evan Chengd38c22b2006-05-11 23:55:42 +000010// 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.
Chris Lattner01aa7522006-03-06 17:58:04 +000014//
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.
Evan Cheng31272342006-01-23 08:26:10 +000018//
19//===----------------------------------------------------------------------===//
20
Dale Johannesen2182f062007-07-13 17:13:54 +000021#define DEBUG_TYPE "pre-RA-sched"
Dan Gohman483377c2009-02-06 17:22:58 +000022#include "ScheduleDAGSDNodes.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000023#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohman7e105f02009-01-15 22:18:12 +000024#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000025#include "llvm/CodeGen/SchedulerRegistry.h"
Jim Laskey03593f72006-08-01 18:29:48 +000026#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000028#include "llvm/Target/TargetData.h"
Evan Cheng31272342006-01-23 08:26:10 +000029#include "llvm/Target/TargetInstrInfo.h"
Evan Chengab495562006-01-25 09:14:32 +000030#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Dan Gohman0d8a61e2008-06-23 23:40:09 +000032#include "llvm/ADT/PriorityQueue.h"
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000033#include "llvm/ADT/Statistic.h"
Evan Chengab495562006-01-25 09:14:32 +000034#include <climits>
Evan Cheng31272342006-01-23 08:26:10 +000035using namespace llvm;
36
Chris Lattneraee775a2006-12-19 22:41:21 +000037STATISTIC(NumNoops , "Number of noops inserted");
38STATISTIC(NumStalls, "Number of pipeline stalls");
Evan Chengab495562006-01-25 09:14:32 +000039
Jim Laskey95eda5b2006-08-01 14:21:23 +000040static RegisterScheduler
Dan Gohman9c4b7d52008-10-14 20:25:08 +000041 tdListDAGScheduler("list-td", "Top-down list scheduler",
Jim Laskey95eda5b2006-08-01 14:21:23 +000042 createTDListDAGScheduler);
43
Chris Lattneraf5e26c2006-03-08 04:37:58 +000044namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +000045//===----------------------------------------------------------------------===//
46/// ScheduleDAGList - The actual list scheduler implementation. This supports
Evan Chengd38c22b2006-05-11 23:55:42 +000047/// top-down scheduling.
Chris Lattner9e95acc2006-03-09 06:37:29 +000048///
Dan Gohman60cb69e2008-11-19 23:18:57 +000049class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAGSDNodes {
Evan Cheng31272342006-01-23 08:26:10 +000050private:
Chris Lattner356183d2006-03-11 22:44:37 +000051 /// AvailableQueue - The priority queue to use for the available SUnits.
52 ///
53 SchedulingPriorityQueue *AvailableQueue;
Chris Lattner9df64752006-03-09 06:35:14 +000054
Chris Lattner572003c2006-03-12 00:38:57 +000055 /// 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
Dan Gohmanfe1748d2008-11-18 02:50:01 +000057 /// the operation). Once the operands become available, the instruction is
Dan Gohmana687fd82008-11-17 19:45:19 +000058 /// added to the AvailableQueue.
59 std::vector<SUnit*> PendingQueue;
Evan Cheng9add8802006-05-04 19:16:39 +000060
Chris Lattnere50c0922006-03-05 22:45:01 +000061 /// HazardRec - The hazard recognizer to use.
Dan Gohman7e105f02009-01-15 22:18:12 +000062 ScheduleHazardRecognizer *HazardRec;
Evan Cheng9add8802006-05-04 19:16:39 +000063
Evan Cheng31272342006-01-23 08:26:10 +000064public:
Dan Gohman619ef482009-01-15 19:20:50 +000065 ScheduleDAGList(MachineFunction &mf,
Chris Lattner356183d2006-03-11 22:44:37 +000066 SchedulingPriorityQueue *availqueue,
Dan Gohman7e105f02009-01-15 22:18:12 +000067 ScheduleHazardRecognizer *HR)
Dan Gohman619ef482009-01-15 19:20:50 +000068 : ScheduleDAGSDNodes(mf),
Chris Lattner356183d2006-03-11 22:44:37 +000069 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +000070 }
Evan Chengab495562006-01-25 09:14:32 +000071
72 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +000073 delete HazardRec;
Chris Lattner356183d2006-03-11 22:44:37 +000074 delete AvailableQueue;
Evan Chengab495562006-01-25 09:14:32 +000075 }
Evan Cheng31272342006-01-23 08:26:10 +000076
77 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +000078
Evan Chengab495562006-01-25 09:14:32 +000079private:
Dan Gohman2d170892008-12-09 22:54:47 +000080 void ReleaseSucc(SUnit *SU, const SDep &D);
Dan Gohmanb9543432009-02-10 23:27:53 +000081 void ReleaseSuccessors(SUnit *SU);
Chris Lattner063086b2006-03-11 22:34:41 +000082 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner399bee22006-03-09 06:48:37 +000083 void ListScheduleTopDown();
Evan Chengab495562006-01-25 09:14:32 +000084};
Chris Lattneraf5e26c2006-03-08 04:37:58 +000085} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +000086
Chris Lattner9995a0c2006-03-11 22:28:35 +000087/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner9995a0c2006-03-11 22:28:35 +000088void ScheduleDAGList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +000089 DOUT << "********** List Scheduling **********\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +000090
Dan Gohman04543e72008-12-23 18:36:58 +000091 // Build the scheduling graph.
92 BuildSchedGraph();
Evan Cheng7d693892006-05-09 07:13:34 +000093
Dan Gohman46520a22008-06-21 19:18:17 +000094 AvailableQueue->initNodes(SUnits);
Chris Lattner9995a0c2006-03-11 22:28:35 +000095
Evan Chengd38c22b2006-05-11 23:55:42 +000096 ListScheduleTopDown();
Chris Lattner9995a0c2006-03-11 22:28:35 +000097
Chris Lattner356183d2006-03-11 22:44:37 +000098 AvailableQueue->releaseState();
Chris Lattner9995a0c2006-03-11 22:28:35 +000099}
100
101//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000102// Top-Down Scheduling
103//===----------------------------------------------------------------------===//
104
105/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman5ebdb982008-11-18 00:38:59 +0000106/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000107void ScheduleDAGList::ReleaseSucc(SUnit *SU, const SDep &D) {
108 SUnit *SuccSU = D.getSUnit();
Dan Gohman5ebdb982008-11-18 00:38:59 +0000109 --SuccSU->NumPredsLeft;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000110
Dan Gohman5ebdb982008-11-18 00:38:59 +0000111#ifndef NDEBUG
112 if (SuccSU->NumPredsLeft < 0) {
113 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000114 SuccSU->dump(this);
Dan Gohman5ebdb982008-11-18 00:38:59 +0000115 cerr << " has been released too many times!\n";
116 assert(0);
117 }
118#endif
119
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000120 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
Chris Lattner9995a0c2006-03-11 22:28:35 +0000121
Dan Gohmanb9543432009-02-10 23:27:53 +0000122 // 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)
Dan Gohmana687fd82008-11-17 19:45:19 +0000125 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000126}
127
128void 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!");
134
135 ReleaseSucc(SU, *I);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000136 }
137}
138
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.
Chris Lattner356183d2006-03-11 22:44:37 +0000142void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000143 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000144 DEBUG(SU->dump(this));
Chris Lattner9995a0c2006-03-11 22:28:35 +0000145
146 Sequence.push_back(SU);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000147 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
148 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000149
Dan Gohmanb9543432009-02-10 23:27:53 +0000150 ReleaseSuccessors(SU);
Dan Gohman92a36d72008-11-17 21:31:02 +0000151 SU->isScheduled = true;
152 AvailableQueue->ScheduledNode(SU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000153}
154
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000155/// ListScheduleTopDown - The main loop of list scheduling for top-down
156/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000157void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000158 unsigned CurCycle = 0;
Chris Lattner572003c2006-03-12 00:38:57 +0000159
Dan Gohmanb9543432009-02-10 23:27:53 +0000160 // Release any successors of the special Entry node.
161 ReleaseSuccessors(&EntrySU);
162
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000163 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000164 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000165 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000166 if (SUnits[i].Preds.empty()) {
Chris Lattner356183d2006-03-11 22:44:37 +0000167 AvailableQueue->push(&SUnits[i]);
Dan Gohman17c226b2008-11-17 16:37:30 +0000168 SUnits[i].isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000169 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000170 }
171
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;
Dan Gohmane6e13482008-06-21 15:52:51 +0000175 Sequence.reserve(SUnits.size());
Chris Lattner572003c2006-03-12 00:38:57 +0000176 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.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000179 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000180 if (PendingQueue[i]->getDepth() == CurCycle) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000181 AvailableQueue->push(PendingQueue[i]);
182 PendingQueue[i]->isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000183 PendingQueue[i] = PendingQueue.back();
184 PendingQueue.pop_back();
185 --i; --e;
186 } else {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000187 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
Chris Lattner572003c2006-03-12 00:38:57 +0000188 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000189 }
Chris Lattner572003c2006-03-12 00:38:57 +0000190
Chris Lattnera767dbf2006-03-12 09:01:41 +0000191 // 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;
196 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000197
Chris Lattnera767dbf2006-03-12 09:01:41 +0000198 SUnit *FoundSUnit = 0;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000199
Chris Lattnere50c0922006-03-05 22:45:01 +0000200 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000201 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000202 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000203
Dan Gohman7e105f02009-01-15 22:18:12 +0000204 ScheduleHazardRecognizer::HazardType HT =
205 HazardRec->getHazardType(CurSUnit);
206 if (HT == ScheduleHazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000207 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000208 break;
209 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000210
Chris Lattnere50c0922006-03-05 22:45:01 +0000211 // Remember if this is a noop hazard.
Dan Gohman7e105f02009-01-15 22:18:12 +0000212 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
Chris Lattnere50c0922006-03-05 22:45:01 +0000213
Chris Lattnera767dbf2006-03-12 09:01:41 +0000214 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000215 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000216
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000217 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000218 if (!NotReady.empty()) {
219 AvailableQueue->push_all(NotReady);
220 NotReady.clear();
221 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000222
223 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000224 if (FoundSUnit) {
225 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman7e105f02009-01-15 22:18:12 +0000226 HazardRec->EmitInstruction(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000227
228 // If this is a pseudo-op node, we don't want to increment the current
229 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000230 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
231 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000232 } else if (!HasNoopHazards) {
233 // Otherwise, we have a pipeline stall, but no other problem, just advance
234 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000235 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000236 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000237 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000238 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000239 } 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.
Bill Wendling22e978a2006-12-07 20:04:42 +0000243 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000244 HazardRec->EmitNoop();
Dan Gohmanceac7c32009-01-16 01:33:36 +0000245 Sequence.push_back(0); // NULL here means noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000246 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000247 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000248 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000249 }
250
251#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000252 VerifySchedule(/*isBottomUp=*/false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000253#endif
254}
255
Chris Lattner9df64752006-03-09 06:35:14 +0000256//===----------------------------------------------------------------------===//
Chris Lattner9df64752006-03-09 06:35:14 +0000257// Public Constructor Functions
258//===----------------------------------------------------------------------===//
259
Jim Laskey95eda5b2006-08-01 14:21:23 +0000260/// 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.
Dan Gohmandfaf6462009-02-11 04:27:20 +0000263ScheduleDAGSDNodes *
264llvm::createTDListDAGScheduler(SelectionDAGISel *IS, bool Fast) {
Dan Gohman619ef482009-01-15 19:20:50 +0000265 return new ScheduleDAGList(*IS->MF,
Chris Lattner6398c132006-03-09 07:38:27 +0000266 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000267 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000268}