blob: d4acf5fa5fecbb63b0f7039704b9484044cce650 [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 Gohman5ebdb982008-11-18 00:38:59 +000081 void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain);
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
94 // Build scheduling units.
95 BuildSchedUnits();
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.
110void ScheduleDAGList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) {
111 --SuccSU->NumPredsLeft;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000112
Dan Gohman5ebdb982008-11-18 00:38:59 +0000113#ifndef NDEBUG
114 if (SuccSU->NumPredsLeft < 0) {
115 cerr << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000116 SuccSU->dump(this);
Dan Gohman5ebdb982008-11-18 00:38:59 +0000117 cerr << " has been released too many times!\n";
118 assert(0);
119 }
120#endif
121
Dan Gohman71b632f2008-11-18 21:14:44 +0000122 // Compute the cycle when this SUnit actually becomes available. This
123 // is the max of the start time of all predecessors plus their latencies.
Dan Gohman5ebdb982008-11-18 00:38:59 +0000124 // If this is a token edge, we don't need to wait for the latency of the
125 // preceeding instruction (e.g. a long-latency load) unless there is also
126 // some other data dependence.
127 unsigned PredDoneCycle = SU->Cycle;
128 if (!isChain)
129 PredDoneCycle += SU->Latency;
130 else if (SU->Latency)
131 PredDoneCycle += 1;
132 SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000133
Evan Cheng038dcc52007-09-28 19:24:24 +0000134 if (SuccSU->NumPredsLeft == 0) {
Dan Gohmana687fd82008-11-17 19:45:19 +0000135 PendingQueue.push_back(SuccSU);
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);
Chris Lattner356183d2006-03-11 22:44:37 +0000147 SU->Cycle = CurCycle;
Dan Gohman92a36d72008-11-17 21:31:02 +0000148
Dan Gohman68294c02008-11-15 00:24:23 +0000149 // Top down: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000150 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
151 I != E; ++I)
Dan Gohman5ebdb982008-11-18 00:38:59 +0000152 ReleaseSucc(SU, I->Dep, I->isCtrl);
Dan Gohman92a36d72008-11-17 21:31:02 +0000153
154 SU->isScheduled = true;
155 AvailableQueue->ScheduledNode(SU);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000156}
157
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000158/// ListScheduleTopDown - The main loop of list scheduling for top-down
159/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000160void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000161 unsigned CurCycle = 0;
Chris Lattner572003c2006-03-12 00:38:57 +0000162
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 Gohmana687fd82008-11-17 19:45:19 +0000180 if (PendingQueue[i]->CycleBound == CurCycle) {
181 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 Gohmana687fd82008-11-17 19:45:19 +0000187 assert(PendingQueue[i]->CycleBound > 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;
199 SDNode *FoundNode = 0;
200
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();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000204
205 // Get the node represented by this SUnit.
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000206 FoundNode = CurSUnit->getNode();
Chris Lattnera767dbf2006-03-12 09:01:41 +0000207
Chris Lattner0c801bd2006-03-07 05:40:43 +0000208 // If this is a pseudo op, like copyfromreg, look to see if there is a
209 // real target node flagged to it. If so, use the target node.
Dan Gohman072734e2008-11-13 23:24:17 +0000210 while (!FoundNode->isMachineOpcode()) {
211 SDNode *N = FoundNode->getFlaggedNode();
212 if (!N) break;
213 FoundNode = N;
214 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000215
Chris Lattnera767dbf2006-03-12 09:01:41 +0000216 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000217 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000218 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000219 break;
220 }
Dan Gohman60cb69e2008-11-19 23:18:57 +0000221
Chris Lattnere50c0922006-03-05 22:45:01 +0000222 // Remember if this is a noop hazard.
223 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
224
Chris Lattnera767dbf2006-03-12 09:01:41 +0000225 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000226 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000227
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000228 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000229 if (!NotReady.empty()) {
230 AvailableQueue->push_all(NotReady);
231 NotReady.clear();
232 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000233
234 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000235 if (FoundSUnit) {
236 ScheduleNodeTopDown(FoundSUnit, CurCycle);
237 HazardRec->EmitInstruction(FoundNode);
Chris Lattner572003c2006-03-12 00:38:57 +0000238
239 // If this is a pseudo-op node, we don't want to increment the current
240 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000241 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
242 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000243 } else if (!HasNoopHazards) {
244 // Otherwise, we have a pipeline stall, but no other problem, just advance
245 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000246 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000247 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000248 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000249 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000250 } else {
251 // Otherwise, we have no instructions to issue and we have instructions
252 // that will fault if we don't do this right. This is the case for
253 // processors without pipeline interlocks and other cases.
Bill Wendling22e978a2006-12-07 20:04:42 +0000254 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000255 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000256 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000257 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000258 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000259 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000260 }
261
262#ifndef NDEBUG
Dan Gohman4ce15e12008-11-20 01:26:25 +0000263 VerifySchedule(/*isBottomUp=*/false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000264#endif
265}
266
Chris Lattner9df64752006-03-09 06:35:14 +0000267//===----------------------------------------------------------------------===//
Chris Lattner9df64752006-03-09 06:35:14 +0000268// Public Constructor Functions
269//===----------------------------------------------------------------------===//
270
Jim Laskey95eda5b2006-08-01 14:21:23 +0000271/// createTDListDAGScheduler - This creates a top-down list scheduler with a
272/// new hazard recognizer. This scheduler takes ownership of the hazard
273/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000274ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
275 SelectionDAG *DAG,
Dan Gohman5499e892008-11-11 17:50:47 +0000276 const TargetMachine *TM,
Evan Cheng2c977312008-07-01 18:05:03 +0000277 MachineBasicBlock *BB, bool Fast) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000278 return new ScheduleDAGList(DAG, BB, *TM,
Chris Lattner6398c132006-03-09 07:38:27 +0000279 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000280 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000281}