blob: 430283d5eff9c1d47068538507168309e2f24b08 [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"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000032#include "llvm/Support/raw_ostream.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);
Andrew Trickc416ba62010-12-24 04:28:06 +000043
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///
Nick Lewycky02d5f772009-10-25 06:33:48 +000049class 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;
Andrew Trickc416ba62010-12-24 04:28:06 +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,
Andrew Trick10ffc2b2010-12-24 05:03:26 +000066 SchedulingPriorityQueue *availqueue)
67 : ScheduleDAGSDNodes(mf), AvailableQueue(availqueue) {
68
69 const TargetMachine &tm = mf.getTarget();
70 HazardRec = tm.getInstrInfo()->CreateTargetHazardRecognizer(&tm, this);
71 }
Evan Chengab495562006-01-25 09:14:32 +000072
73 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +000074 delete HazardRec;
Chris Lattner356183d2006-03-11 22:44:37 +000075 delete AvailableQueue;
Evan Chengab495562006-01-25 09:14:32 +000076 }
Evan Cheng31272342006-01-23 08:26:10 +000077
78 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +000079
Evan Chengab495562006-01-25 09:14:32 +000080private:
Dan Gohman2d170892008-12-09 22:54:47 +000081 void ReleaseSucc(SUnit *SU, const SDep &D);
Dan Gohmanb9543432009-02-10 23:27:53 +000082 void ReleaseSuccessors(SUnit *SU);
Chris Lattner063086b2006-03-11 22:34:41 +000083 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner399bee22006-03-09 06:48:37 +000084 void ListScheduleTopDown();
Evan Chengab495562006-01-25 09:14:32 +000085};
Chris Lattneraf5e26c2006-03-08 04:37:58 +000086} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +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() {
David Greene63145842010-01-05 01:24:43 +000090 DEBUG(dbgs() << "********** List Scheduling **********\n");
Andrew Trickc416ba62010-12-24 04:28:06 +000091
Dan Gohman04543e72008-12-23 18:36:58 +000092 // Build the scheduling graph.
Dan Gohman918ec532009-10-09 23:33:48 +000093 BuildSchedGraph(NULL);
Evan Cheng7d693892006-05-09 07:13:34 +000094
Dan Gohman46520a22008-06-21 19:18:17 +000095 AvailableQueue->initNodes(SUnits);
Andrew Trickc416ba62010-12-24 04:28:06 +000096
Evan Chengd38c22b2006-05-11 23:55:42 +000097 ListScheduleTopDown();
Andrew Trickc416ba62010-12-24 04:28:06 +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();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000110
Dan Gohman5ebdb982008-11-18 00:38:59 +0000111#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000112 if (SuccSU->NumPredsLeft == 0) {
David Greene63145842010-01-05 01:24:43 +0000113 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000114 SuccSU->dump(this);
David Greene63145842010-01-05 01:24:43 +0000115 dbgs() << " has been released too many times!\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000116 llvm_unreachable(0);
Dan Gohman5ebdb982008-11-18 00:38:59 +0000117 }
118#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000119 --SuccSU->NumPredsLeft;
120
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000121 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
Andrew Trickc416ba62010-12-24 04:28:06 +0000122
Dan Gohmanb9543432009-02-10 23:27:53 +0000123 // 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)
Dan Gohmana687fd82008-11-17 19:45:19 +0000126 PendingQueue.push_back(SuccSU);
Dan Gohmanb9543432009-02-10 23:27:53 +0000127}
128
129void 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);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000137 }
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.
Chris Lattner356183d2006-03-11 22:44:37 +0000143void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greene63145842010-01-05 01:24:43 +0000144 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman22d07b12008-11-18 02:06:40 +0000145 DEBUG(SU->dump(this));
Andrew Trickc416ba62010-12-24 04:28:06 +0000146
Chris Lattner9995a0c2006-03-11 22:28:35 +0000147 Sequence.push_back(SU);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000148 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
149 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000150
Dan Gohmanb9543432009-02-10 23:27:53 +0000151 ReleaseSuccessors(SU);
Dan Gohman92a36d72008-11-17 21:31:02 +0000152 SU->isScheduled = true;
153 AvailableQueue->ScheduledNode(SU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000154}
155
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000156/// ListScheduleTopDown - The main loop of list scheduling for top-down
157/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000158void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000159 unsigned CurCycle = 0;
Chris Lattner572003c2006-03-12 00:38:57 +0000160
Dan Gohmanb9543432009-02-10 23:27:53 +0000161 // Release any successors of the special Entry node.
162 ReleaseSuccessors(&EntrySU);
163
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000164 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000165 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000166 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000167 if (SUnits[i].Preds.empty()) {
Chris Lattner356183d2006-03-11 22:44:37 +0000168 AvailableQueue->push(&SUnits[i]);
Dan Gohman17c226b2008-11-17 16:37:30 +0000169 SUnits[i].isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000170 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000171 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000172
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000173 // 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;
Dan Gohmane6e13482008-06-21 15:52:51 +0000176 Sequence.reserve(SUnits.size());
Chris Lattner572003c2006-03-12 00:38:57 +0000177 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.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000180 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000181 if (PendingQueue[i]->getDepth() == CurCycle) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000182 AvailableQueue->push(PendingQueue[i]);
183 PendingQueue[i]->isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000184 PendingQueue[i] = PendingQueue.back();
185 PendingQueue.pop_back();
186 --i; --e;
187 } else {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000188 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
Chris Lattner572003c2006-03-12 00:38:57 +0000189 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000190 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000191
Chris Lattnera767dbf2006-03-12 09:01:41 +0000192 // 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 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000198
Chris Lattnera767dbf2006-03-12 09:01:41 +0000199 SUnit *FoundSUnit = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000200
Chris Lattnere50c0922006-03-05 22:45:01 +0000201 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000202 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000203 SUnit *CurSUnit = AvailableQueue->pop();
Andrew Trickc416ba62010-12-24 04:28:06 +0000204
Dan Gohman7e105f02009-01-15 22:18:12 +0000205 ScheduleHazardRecognizer::HazardType HT =
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000206 HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
Dan Gohman7e105f02009-01-15 22:18:12 +0000207 if (HT == ScheduleHazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000208 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000209 break;
210 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000211
Chris Lattnere50c0922006-03-05 22:45:01 +0000212 // Remember if this is a noop hazard.
Dan Gohman7e105f02009-01-15 22:18:12 +0000213 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
Andrew Trickc416ba62010-12-24 04:28:06 +0000214
Chris Lattnera767dbf2006-03-12 09:01:41 +0000215 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000216 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000217
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000218 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000219 if (!NotReady.empty()) {
220 AvailableQueue->push_all(NotReady);
221 NotReady.clear();
222 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000223
224 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000225 if (FoundSUnit) {
226 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman7e105f02009-01-15 22:18:12 +0000227 HazardRec->EmitInstruction(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000228
229 // If this is a pseudo-op node, we don't want to increment the current
230 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000231 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
Andrew Trickc416ba62010-12-24 04:28:06 +0000232 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000233 } else if (!HasNoopHazards) {
234 // Otherwise, we have a pipeline stall, but no other problem, just advance
235 // the current cycle and try again.
David Greene63145842010-01-05 01:24:43 +0000236 DEBUG(dbgs() << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000237 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000238 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000239 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000240 } 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.
David Greene63145842010-01-05 01:24:43 +0000244 DEBUG(dbgs() << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000245 HazardRec->EmitNoop();
Dan Gohmanceac7c32009-01-16 01:33:36 +0000246 Sequence.push_back(0); // NULL here means noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000247 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000248 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000249 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000250 }
251
252#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000253 VerifySchedule(/*isBottomUp=*/false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000254#endif
255}
256
Chris Lattner9df64752006-03-09 06:35:14 +0000257//===----------------------------------------------------------------------===//
Chris Lattner9df64752006-03-09 06:35:14 +0000258// Public Constructor Functions
259//===----------------------------------------------------------------------===//
260
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000261/// createTDListDAGScheduler - This creates a top-down list scheduler.
Dan Gohmandfaf6462009-02-11 04:27:20 +0000262ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +0000263llvm::createTDListDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000264 return new ScheduleDAGList(*IS->MF, new LatencyPriorityQueue());
Evan Cheng31272342006-01-23 08:26:10 +0000265}