blob: 6f0767aa1000349dadb30c92eb80ad7a5a186de1 [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/TargetMachine.h"
29#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.
Chris Lattner543832d2006-03-08 04:25:59 +000062 HazardRecognizer *HazardRec;
Evan Cheng9add8802006-05-04 19:16:39 +000063
Evan Cheng31272342006-01-23 08:26:10 +000064public:
Dan Gohman5a390b92008-11-13 21:21:28 +000065 ScheduleDAGList(SelectionDAG *dag, MachineBasicBlock *bb,
Evan Chengd38c22b2006-05-11 23:55:42 +000066 const TargetMachine &tm,
Chris Lattner356183d2006-03-11 22:44:37 +000067 SchedulingPriorityQueue *availqueue,
Chris Lattner543832d2006-03-08 04:25:59 +000068 HazardRecognizer *HR)
Dan Gohman60cb69e2008-11-19 23:18:57 +000069 : ScheduleDAGSDNodes(dag, bb, tm),
Chris Lattner356183d2006-03-11 22:44:37 +000070 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +000071 }
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);
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 Lattner47639db2006-03-06 00:22:00 +000087HazardRecognizer::~HazardRecognizer() {}
88
Evan Chengc4c339c2006-01-26 00:30:29 +000089
Chris Lattner9995a0c2006-03-11 22:28:35 +000090/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner9995a0c2006-03-11 22:28:35 +000091void ScheduleDAGList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +000092 DOUT << "********** List Scheduling **********\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +000093
Dan Gohman04543e72008-12-23 18:36:58 +000094 // Build the scheduling graph.
95 BuildSchedGraph();
Evan Cheng7d693892006-05-09 07:13:34 +000096
Dan Gohman46520a22008-06-21 19:18:17 +000097 AvailableQueue->initNodes(SUnits);
Chris Lattner9995a0c2006-03-11 22:28:35 +000098
Evan Chengd38c22b2006-05-11 23:55:42 +000099 ListScheduleTopDown();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000100
Chris Lattner356183d2006-03-11 22:44:37 +0000101 AvailableQueue->releaseState();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000102}
103
104//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000105// Top-Down Scheduling
106//===----------------------------------------------------------------------===//
107
108/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Dan Gohman5ebdb982008-11-18 00:38:59 +0000109/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000110void ScheduleDAGList::ReleaseSucc(SUnit *SU, const SDep &D) {
111 SUnit *SuccSU = D.getSUnit();
Dan Gohman5ebdb982008-11-18 00:38:59 +0000112 --SuccSU->NumPredsLeft;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000113
Dan Gohman5ebdb982008-11-18 00:38:59 +0000114#ifndef NDEBUG
115 if (SuccSU->NumPredsLeft < 0) {
116 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000117 SuccSU->dump(this);
Dan Gohman5ebdb982008-11-18 00:38:59 +0000118 cerr << " has been released too many times!\n";
119 assert(0);
120 }
121#endif
122
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000123 SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
Chris Lattner9995a0c2006-03-11 22:28:35 +0000124
Evan Cheng038dcc52007-09-28 19:24:24 +0000125 if (SuccSU->NumPredsLeft == 0) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000126 PendingQueue.push_back(SuccSU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000127 }
128}
129
130/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
131/// count of its successors. If a successor pending count is zero, add it to
132/// the Available queue.
Chris Lattner356183d2006-03-11 22:44:37 +0000133void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000134 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Dan Gohman22d07b12008-11-18 02:06:40 +0000135 DEBUG(SU->dump(this));
Chris Lattner9995a0c2006-03-11 22:28:35 +0000136
137 Sequence.push_back(SU);
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000138 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
139 SU->setDepthToAtLeast(CurCycle);
Dan Gohman92a36d72008-11-17 21:31:02 +0000140
Dan Gohman68294c02008-11-15 00:24:23 +0000141 // Top down: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000142 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
Dan Gohman14074842009-01-13 20:24:13 +0000143 I != E; ++I) {
144 assert(!I->isAssignedRegDep() &&
145 "The list-td scheduler doesn't yet support physreg dependencies!");
146
Dan Gohman2d170892008-12-09 22:54:47 +0000147 ReleaseSucc(SU, *I);
Dan Gohman14074842009-01-13 20:24:13 +0000148 }
Dan Gohman92a36d72008-11-17 21:31:02 +0000149
150 SU->isScheduled = true;
151 AvailableQueue->ScheduledNode(SU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000152}
153
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000154/// ListScheduleTopDown - The main loop of list scheduling for top-down
155/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000156void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000157 unsigned CurCycle = 0;
Chris Lattner572003c2006-03-12 00:38:57 +0000158
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000159 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000160 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000161 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000162 if (SUnits[i].Preds.empty()) {
Chris Lattner356183d2006-03-11 22:44:37 +0000163 AvailableQueue->push(&SUnits[i]);
Dan Gohman17c226b2008-11-17 16:37:30 +0000164 SUnits[i].isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000165 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000166 }
167
168 // While Available queue is not empty, grab the node with the highest
169 // priority. If it is not ready put it back. Schedule the node.
170 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +0000171 Sequence.reserve(SUnits.size());
Chris Lattner572003c2006-03-12 00:38:57 +0000172 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
173 // Check to see if any of the pending instructions are ready to issue. If
174 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000175 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000176 if (PendingQueue[i]->getDepth() == CurCycle) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000177 AvailableQueue->push(PendingQueue[i]);
178 PendingQueue[i]->isAvailable = true;
Chris Lattner572003c2006-03-12 00:38:57 +0000179 PendingQueue[i] = PendingQueue.back();
180 PendingQueue.pop_back();
181 --i; --e;
182 } else {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000183 assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
Chris Lattner572003c2006-03-12 00:38:57 +0000184 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000185 }
Chris Lattner572003c2006-03-12 00:38:57 +0000186
Chris Lattnera767dbf2006-03-12 09:01:41 +0000187 // If there are no instructions available, don't try to issue anything, and
188 // don't advance the hazard recognizer.
189 if (AvailableQueue->empty()) {
190 ++CurCycle;
191 continue;
192 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000193
Chris Lattnera767dbf2006-03-12 09:01:41 +0000194 SUnit *FoundSUnit = 0;
195 SDNode *FoundNode = 0;
196
Chris Lattnere50c0922006-03-05 22:45:01 +0000197 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000198 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000199 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000200
201 // Get the node represented by this SUnit.
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000202 FoundNode = CurSUnit->getNode();
Chris Lattnera767dbf2006-03-12 09:01:41 +0000203
Chris Lattner0c801bd2006-03-07 05:40:43 +0000204 // If this is a pseudo op, like copyfromreg, look to see if there is a
205 // real target node flagged to it. If so, use the target node.
Dan Gohman072734e2008-11-13 23:24:17 +0000206 while (!FoundNode->isMachineOpcode()) {
207 SDNode *N = FoundNode->getFlaggedNode();
208 if (!N) break;
209 FoundNode = N;
210 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000211
Chris Lattnera767dbf2006-03-12 09:01:41 +0000212 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000213 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000214 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000215 break;
216 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000217
Chris Lattnere50c0922006-03-05 22:45:01 +0000218 // Remember if this is a noop hazard.
219 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
220
Chris Lattnera767dbf2006-03-12 09:01:41 +0000221 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000222 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000223
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000224 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000225 if (!NotReady.empty()) {
226 AvailableQueue->push_all(NotReady);
227 NotReady.clear();
228 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000229
230 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000231 if (FoundSUnit) {
232 ScheduleNodeTopDown(FoundSUnit, CurCycle);
233 HazardRec->EmitInstruction(FoundNode);
Chris Lattner572003c2006-03-12 00:38:57 +0000234
235 // If this is a pseudo-op node, we don't want to increment the current
236 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000237 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
238 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000239 } else if (!HasNoopHazards) {
240 // Otherwise, we have a pipeline stall, but no other problem, just advance
241 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000242 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000243 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000244 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000245 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000246 } else {
247 // Otherwise, we have no instructions to issue and we have instructions
248 // that will fault if we don't do this right. This is the case for
249 // processors without pipeline interlocks and other cases.
Bill Wendling22e978a2006-12-07 20:04:42 +0000250 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000251 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000252 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000253 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000254 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000255 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000256 }
257
258#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000259 VerifySchedule(/*isBottomUp=*/false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000260#endif
261}
262
Chris Lattner9df64752006-03-09 06:35:14 +0000263//===----------------------------------------------------------------------===//
Chris Lattner9df64752006-03-09 06:35:14 +0000264// Public Constructor Functions
265//===----------------------------------------------------------------------===//
266
Jim Laskey95eda5b2006-08-01 14:21:23 +0000267/// createTDListDAGScheduler - This creates a top-down list scheduler with a
268/// new hazard recognizer. This scheduler takes ownership of the hazard
269/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000270ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
271 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +0000272 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +0000273 MachineBasicBlock *BB, bool Fast) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000274 return new ScheduleDAGList(DAG, BB, *TM,
Chris Lattner6398c132006-03-09 07:38:27 +0000275 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000276 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000277}