blob: 2e2cac41214e4092ce116413474b5b606e10066a [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 Gohman60cb69e2008-11-19 23:18:57 +000022#include "llvm/CodeGen/LatencyPriorityQueue.h"
23#include "llvm/CodeGen/ScheduleDAGSDNodes.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000024#include "llvm/CodeGen/SchedulerRegistry.h"
Jim Laskey03593f72006-08-01 18:29:48 +000025#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000026#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Evan Cheng31272342006-01-23 08:26:10 +000028#include "llvm/Target/TargetInstrInfo.h"
Evan Chengab495562006-01-25 09:14:32 +000029#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Dan Gohman0d8a61e2008-06-23 23:40:09 +000031#include "llvm/ADT/PriorityQueue.h"
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000032#include "llvm/ADT/Statistic.h"
Evan Chengab495562006-01-25 09:14:32 +000033#include <climits>
Evan Cheng31272342006-01-23 08:26:10 +000034using namespace llvm;
35
Chris Lattneraee775a2006-12-19 22:41:21 +000036STATISTIC(NumNoops , "Number of noops inserted");
37STATISTIC(NumStalls, "Number of pipeline stalls");
Evan Chengab495562006-01-25 09:14:32 +000038
Jim Laskey95eda5b2006-08-01 14:21:23 +000039static RegisterScheduler
Dan Gohman9c4b7d52008-10-14 20:25:08 +000040 tdListDAGScheduler("list-td", "Top-down list scheduler",
Jim Laskey95eda5b2006-08-01 14:21:23 +000041 createTDListDAGScheduler);
42
Chris Lattneraf5e26c2006-03-08 04:37:58 +000043namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +000044//===----------------------------------------------------------------------===//
45/// ScheduleDAGList - The actual list scheduler implementation. This supports
Evan Chengd38c22b2006-05-11 23:55:42 +000046/// top-down scheduling.
Chris Lattner9e95acc2006-03-09 06:37:29 +000047///
Dan Gohman60cb69e2008-11-19 23:18:57 +000048class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAGSDNodes {
Evan Cheng31272342006-01-23 08:26:10 +000049private:
Chris Lattner356183d2006-03-11 22:44:37 +000050 /// AvailableQueue - The priority queue to use for the available SUnits.
51 ///
52 SchedulingPriorityQueue *AvailableQueue;
Chris Lattner9df64752006-03-09 06:35:14 +000053
Chris Lattner572003c2006-03-12 00:38:57 +000054 /// PendingQueue - This contains all of the instructions whose operands have
55 /// been issued, but their results are not ready yet (due to the latency of
Dan Gohmanfe1748d2008-11-18 02:50:01 +000056 /// the operation). Once the operands become available, the instruction is
Dan Gohmana687fd82008-11-17 19:45:19 +000057 /// added to the AvailableQueue.
58 std::vector<SUnit*> PendingQueue;
Evan Cheng9add8802006-05-04 19:16:39 +000059
Chris Lattnere50c0922006-03-05 22:45:01 +000060 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +000061 HazardRecognizer *HazardRec;
Evan Cheng9add8802006-05-04 19:16:39 +000062
Evan Cheng31272342006-01-23 08:26:10 +000063public:
Dan Gohman619ef482009-01-15 19:20:50 +000064 ScheduleDAGList(MachineFunction &mf,
Chris Lattner356183d2006-03-11 22:44:37 +000065 SchedulingPriorityQueue *availqueue,
Chris Lattner543832d2006-03-08 04:25:59 +000066 HazardRecognizer *HR)
Dan Gohman619ef482009-01-15 19:20:50 +000067 : ScheduleDAGSDNodes(mf),
Chris Lattner356183d2006-03-11 22:44:37 +000068 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +000069 }
Evan Chengab495562006-01-25 09:14:32 +000070
71 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +000072 delete HazardRec;
Chris Lattner356183d2006-03-11 22:44:37 +000073 delete AvailableQueue;
Evan Chengab495562006-01-25 09:14:32 +000074 }
Evan Cheng31272342006-01-23 08:26:10 +000075
76 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +000077
Evan Chengab495562006-01-25 09:14:32 +000078private:
Dan Gohman2d170892008-12-09 22:54:47 +000079 void ReleaseSucc(SUnit *SU, const SDep &D);
Chris Lattner063086b2006-03-11 22:34:41 +000080 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner399bee22006-03-09 06:48:37 +000081 void ListScheduleTopDown();
Evan Chengab495562006-01-25 09:14:32 +000082};
Chris Lattneraf5e26c2006-03-08 04:37:58 +000083} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +000084
Chris Lattner47639db2006-03-06 00:22:00 +000085HazardRecognizer::~HazardRecognizer() {}
86
Evan Chengc4c339c2006-01-26 00:30:29 +000087
Chris Lattner9995a0c2006-03-11 22:28:35 +000088/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner9995a0c2006-03-11 22:28:35 +000089void ScheduleDAGList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +000090 DOUT << "********** List Scheduling **********\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +000091
Dan Gohman04543e72008-12-23 18:36:58 +000092 // Build the scheduling graph.
93 BuildSchedGraph();
Evan Cheng7d693892006-05-09 07:13:34 +000094
Dan Gohman46520a22008-06-21 19:18:17 +000095 AvailableQueue->initNodes(SUnits);
Chris Lattner9995a0c2006-03-11 22:28:35 +000096
Evan Chengd38c22b2006-05-11 23:55:42 +000097 ListScheduleTopDown();
Chris Lattner9995a0c2006-03-11 22:28:35 +000098
Chris Lattner356183d2006-03-11 22:44:37 +000099 AvailableQueue->releaseState();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000100}
101
102//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000103// Top-Down Scheduling
104//===----------------------------------------------------------------------===//
105
106/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman5ebdb982008-11-18 00:38:59 +0000107/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000108void ScheduleDAGList::ReleaseSucc(SUnit *SU, const SDep &D) {
109 SUnit *SuccSU = D.getSUnit();
Dan Gohman5ebdb982008-11-18 00:38:59 +0000110 --SuccSU->NumPredsLeft;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000111
Dan Gohman5ebdb982008-11-18 00:38:59 +0000112#ifndef NDEBUG
113 if (SuccSU->NumPredsLeft < 0) {
114 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000115 SuccSU->dump(this);
Dan Gohman5ebdb982008-11-18 00:38:59 +0000116 cerr << " has been released too many times!\n";
117 assert(0);
118 }
119#endif
120
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000121 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
Chris Lattner9995a0c2006-03-11 22:28:35 +0000122
Evan Cheng038dcc52007-09-28 19:24:24 +0000123 if (SuccSU->NumPredsLeft == 0) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000124 PendingQueue.push_back(SuccSU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000125 }
126}
127
128/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
129/// count of its successors. If a successor pending count is zero, add it to
130/// the Available queue.
Chris Lattner356183d2006-03-11 22:44:37 +0000131void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000132 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000133 DEBUG(SU->dump(this));
Chris Lattner9995a0c2006-03-11 22:28:35 +0000134
135 Sequence.push_back(SU);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000136 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
137 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000138
Dan Gohman68294c02008-11-15 00:24:23 +0000139 // Top down: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000140 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Dan Gohman14074842009-01-13 20:24:13 +0000141 I != E; ++I) {
142 assert(!I->isAssignedRegDep() &&
143 "The list-td scheduler doesn't yet support physreg dependencies!");
144
Dan Gohman2d170892008-12-09 22:54:47 +0000145 ReleaseSucc(SU, *I);
Dan Gohman14074842009-01-13 20:24:13 +0000146 }
Dan Gohman92a36d72008-11-17 21:31:02 +0000147
148 SU->isScheduled = true;
149 AvailableQueue->ScheduledNode(SU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000150}
151
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000152/// ListScheduleTopDown - The main loop of list scheduling for top-down
153/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000154void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000155 unsigned CurCycle = 0;
Chris Lattner572003c2006-03-12 00:38:57 +0000156
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000157 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000158 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000159 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000160 if (SUnits[i].Preds.empty()) {
Chris Lattner356183d2006-03-11 22:44:37 +0000161 AvailableQueue->push(&SUnits[i]);
Dan Gohman17c226b2008-11-17 16:37:30 +0000162 SUnits[i].isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000163 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000164 }
165
166 // While Available queue is not empty, grab the node with the highest
167 // priority. If it is not ready put it back. Schedule the node.
168 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +0000169 Sequence.reserve(SUnits.size());
Chris Lattner572003c2006-03-12 00:38:57 +0000170 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
171 // Check to see if any of the pending instructions are ready to issue. If
172 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000173 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000174 if (PendingQueue[i]->getDepth() == CurCycle) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000175 AvailableQueue->push(PendingQueue[i]);
176 PendingQueue[i]->isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000177 PendingQueue[i] = PendingQueue.back();
178 PendingQueue.pop_back();
179 --i; --e;
180 } else {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000181 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
Chris Lattner572003c2006-03-12 00:38:57 +0000182 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000183 }
Chris Lattner572003c2006-03-12 00:38:57 +0000184
Chris Lattnera767dbf2006-03-12 09:01:41 +0000185 // If there are no instructions available, don't try to issue anything, and
186 // don't advance the hazard recognizer.
187 if (AvailableQueue->empty()) {
188 ++CurCycle;
189 continue;
190 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000191
Chris Lattnera767dbf2006-03-12 09:01:41 +0000192 SUnit *FoundSUnit = 0;
193 SDNode *FoundNode = 0;
194
Chris Lattnere50c0922006-03-05 22:45:01 +0000195 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000196 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000197 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000198
199 // Get the node represented by this SUnit.
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000200 FoundNode = CurSUnit->getNode();
Chris Lattnera767dbf2006-03-12 09:01:41 +0000201
Chris Lattner0c801bd2006-03-07 05:40:43 +0000202 // If this is a pseudo op, like copyfromreg, look to see if there is a
203 // real target node flagged to it. If so, use the target node.
Dan Gohman072734e2008-11-13 23:24:17 +0000204 while (!FoundNode->isMachineOpcode()) {
205 SDNode *N = FoundNode->getFlaggedNode();
206 if (!N) break;
207 FoundNode = N;
208 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000209
Chris Lattnera767dbf2006-03-12 09:01:41 +0000210 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000211 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000212 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000213 break;
214 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000215
Chris Lattnere50c0922006-03-05 22:45:01 +0000216 // Remember if this is a noop hazard.
217 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
218
Chris Lattnera767dbf2006-03-12 09:01:41 +0000219 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000220 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000221
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000222 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000223 if (!NotReady.empty()) {
224 AvailableQueue->push_all(NotReady);
225 NotReady.clear();
226 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000227
228 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000229 if (FoundSUnit) {
230 ScheduleNodeTopDown(FoundSUnit, CurCycle);
231 HazardRec->EmitInstruction(FoundNode);
Chris Lattner572003c2006-03-12 00:38:57 +0000232
233 // If this is a pseudo-op node, we don't want to increment the current
234 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000235 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
236 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000237 } else if (!HasNoopHazards) {
238 // Otherwise, we have a pipeline stall, but no other problem, just advance
239 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000240 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000241 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000242 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000243 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000244 } else {
245 // Otherwise, we have no instructions to issue and we have instructions
246 // that will fault if we don't do this right. This is the case for
247 // processors without pipeline interlocks and other cases.
Bill Wendling22e978a2006-12-07 20:04:42 +0000248 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000249 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000250 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000251 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000252 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000253 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000254 }
255
256#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000257 VerifySchedule(/*isBottomUp=*/false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000258#endif
259}
260
Chris Lattner9df64752006-03-09 06:35:14 +0000261//===----------------------------------------------------------------------===//
Chris Lattner9df64752006-03-09 06:35:14 +0000262// Public Constructor Functions
263//===----------------------------------------------------------------------===//
264
Jim Laskey95eda5b2006-08-01 14:21:23 +0000265/// createTDListDAGScheduler - This creates a top-down list scheduler with a
266/// new hazard recognizer. This scheduler takes ownership of the hazard
267/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000268ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
Dan Gohman619ef482009-01-15 19:20:50 +0000269 bool Fast) {
270 return new ScheduleDAGList(*IS->MF,
Chris Lattner6398c132006-03-09 07:38:27 +0000271 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000272 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000273}