blob: 8a1dade30b3ba0f1f57ece6dcb6b94681bce297b [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"
Evan Cheng31272342006-01-23 08:26:10 +000022#include "llvm/CodeGen/ScheduleDAG.h"
Jim Laskey29e635d2006-08-02 12:30:23 +000023#include "llvm/CodeGen/SchedulerRegistry.h"
Jim Laskey03593f72006-08-01 18:29:48 +000024#include "llvm/CodeGen/SelectionDAGISel.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Evan Cheng31272342006-01-23 08:26:10 +000027#include "llvm/Target/TargetMachine.h"
28#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
40 tdListDAGScheduler("list-td", " Top-down list scheduler",
41 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///
Chris Lattnere097e6f2006-06-28 22:17:39 +000048class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG {
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
56 /// the operation). Once the operands becomes available, the instruction is
57 /// added to the AvailableQueue. This keeps track of each SUnit and the
58 /// number of cycles left to execute before the operation is available.
59 std::vector<std::pair<unsigned, 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:
65 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)
Evan Chengd38c22b2006-05-11 23:55:42 +000069 : ScheduleDAG(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:
Chris Lattner572003c2006-03-12 00:38:57 +000081 void ReleaseSucc(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
Bill Wendling22e978a2006-12-07 20:04:42 +0000103 DOUT << "*** Final schedule ***\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000104 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000105 DOUT << "\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000106
107 // Emit in scheduled order
108 EmitSchedule();
109}
110
111//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000112// Top-Down Scheduling
113//===----------------------------------------------------------------------===//
114
115/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Chris Lattner572003c2006-03-12 00:38:57 +0000116/// the PendingQueue if the count reaches zero.
117void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000118 SuccSU->NumPredsLeft--;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000119
Evan Cheng038dcc52007-09-28 19:24:24 +0000120 assert(SuccSU->NumPredsLeft >= 0 &&
Chris Lattner572003c2006-03-12 00:38:57 +0000121 "List scheduling internal error");
Chris Lattner9995a0c2006-03-11 22:28:35 +0000122
Evan Cheng038dcc52007-09-28 19:24:24 +0000123 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner572003c2006-03-12 00:38:57 +0000124 // Compute how many cycles it will be before this actually becomes
125 // available. This is the max of the start time of all predecessors plus
126 // their latencies.
127 unsigned AvailableCycle = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000128 for (SUnit::pred_iterator I = SuccSU->Preds.begin(),
Chris Lattner572003c2006-03-12 00:38:57 +0000129 E = SuccSU->Preds.end(); I != E; ++I) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000130 // If this is a token edge, we don't need to wait for the latency of the
131 // preceeding instruction (e.g. a long-latency load) unless there is also
132 // some other data dependence.
Evan Cheng0effc3a2007-09-19 01:38:40 +0000133 SUnit &Pred = *I->Dep;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000134 unsigned PredDoneCycle = Pred.Cycle;
Evan Cheng0effc3a2007-09-19 01:38:40 +0000135 if (!I->isCtrl)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000136 PredDoneCycle += Pred.Latency;
137 else if (Pred.Latency)
Chris Lattnera767dbf2006-03-12 09:01:41 +0000138 PredDoneCycle += 1;
Chris Lattner86a9b602006-03-12 03:52:09 +0000139
140 AvailableCycle = std::max(AvailableCycle, PredDoneCycle);
Chris Lattner572003c2006-03-12 00:38:57 +0000141 }
142
143 PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU));
Chris Lattner9995a0c2006-03-11 22:28:35 +0000144 }
145}
146
147/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
148/// count of its successors. If a successor pending count is zero, add it to
149/// the Available queue.
Chris Lattner356183d2006-03-11 22:44:37 +0000150void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000151 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000152 DEBUG(SU->dump(&DAG));
153
154 Sequence.push_back(SU);
Chris Lattner356183d2006-03-11 22:44:37 +0000155 SU->Cycle = CurCycle;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000156
157 // Bottom up: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000158 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
159 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000160 ReleaseSucc(I->Dep, I->isCtrl);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000161}
162
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000163/// ListScheduleTopDown - The main loop of list scheduling for top-down
164/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000165void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000166 unsigned CurCycle = 0;
Chris Lattner572003c2006-03-12 00:38:57 +0000167
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000168 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000169 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000170 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000171 if (SUnits[i].Preds.empty()) {
Chris Lattner356183d2006-03-11 22:44:37 +0000172 AvailableQueue->push(&SUnits[i]);
Chris Lattner572003c2006-03-12 00:38:57 +0000173 SUnits[i].isAvailable = SUnits[i].isPending = true;
174 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000175 }
176
177 // While Available queue is not empty, grab the node with the highest
178 // priority. If it is not ready put it back. Schedule the node.
179 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +0000180 Sequence.reserve(SUnits.size());
Chris Lattner572003c2006-03-12 00:38:57 +0000181 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
182 // Check to see if any of the pending instructions are ready to issue. If
183 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000184 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Chris Lattner572003c2006-03-12 00:38:57 +0000185 if (PendingQueue[i].first == CurCycle) {
186 AvailableQueue->push(PendingQueue[i].second);
187 PendingQueue[i].second->isAvailable = true;
188 PendingQueue[i] = PendingQueue.back();
189 PendingQueue.pop_back();
190 --i; --e;
191 } else {
192 assert(PendingQueue[i].first > CurCycle && "Negative latency?");
193 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000194 }
Chris Lattner572003c2006-03-12 00:38:57 +0000195
Chris Lattnera767dbf2006-03-12 09:01:41 +0000196 // If there are no instructions available, don't try to issue anything, and
197 // don't advance the hazard recognizer.
198 if (AvailableQueue->empty()) {
199 ++CurCycle;
200 continue;
201 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000202
Chris Lattnera767dbf2006-03-12 09:01:41 +0000203 SUnit *FoundSUnit = 0;
204 SDNode *FoundNode = 0;
205
Chris Lattnere50c0922006-03-05 22:45:01 +0000206 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000207 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000208 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000209
210 // Get the node represented by this SUnit.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000211 FoundNode = CurSUnit->Node;
212
Chris Lattner0c801bd2006-03-07 05:40:43 +0000213 // If this is a pseudo op, like copyfromreg, look to see if there is a
214 // real target node flagged to it. If so, use the target node.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000215 for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size();
216 FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
217 FoundNode = CurSUnit->FlaggedNodes[i];
Chris Lattner0c801bd2006-03-07 05:40:43 +0000218
Chris Lattnera767dbf2006-03-12 09:01:41 +0000219 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000220 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000221 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000222 break;
223 }
224
225 // Remember if this is a noop hazard.
226 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
227
Chris Lattnera767dbf2006-03-12 09:01:41 +0000228 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000229 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000230
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000231 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000232 if (!NotReady.empty()) {
233 AvailableQueue->push_all(NotReady);
234 NotReady.clear();
235 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000236
237 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000238 if (FoundSUnit) {
239 ScheduleNodeTopDown(FoundSUnit, CurCycle);
240 HazardRec->EmitInstruction(FoundNode);
241 FoundSUnit->isScheduled = true;
242 AvailableQueue->ScheduledNode(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000243
244 // If this is a pseudo-op node, we don't want to increment the current
245 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000246 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
247 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000248 } else if (!HasNoopHazards) {
249 // Otherwise, we have a pipeline stall, but no other problem, just advance
250 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000251 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000252 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000253 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000254 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000255 } else {
256 // Otherwise, we have no instructions to issue and we have instructions
257 // that will fault if we don't do this right. This is the case for
258 // processors without pipeline interlocks and other cases.
Bill Wendling22e978a2006-12-07 20:04:42 +0000259 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000260 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000261 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000262 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000263 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000264 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000265 }
266
267#ifndef NDEBUG
268 // Verify that all SUnits were scheduled.
269 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000270 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000271 if (SUnits[i].NumPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000272 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000273 cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000274 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000275 cerr << "has not been scheduled!\n";
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000276 AnyNotSched = true;
277 }
278 }
279 assert(!AnyNotSched);
280#endif
281}
282
Chris Lattner9df64752006-03-09 06:35:14 +0000283//===----------------------------------------------------------------------===//
Chris Lattner6398c132006-03-09 07:38:27 +0000284// LatencyPriorityQueue Implementation
285//===----------------------------------------------------------------------===//
286//
287// This is a SchedulingPriorityQueue that schedules using latency information to
288// reduce the length of the critical path through the basic block.
289//
290namespace {
291 class LatencyPriorityQueue;
292
293 /// Sorting functions for the Available queue.
294 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
295 LatencyPriorityQueue *PQ;
296 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
297 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
298
299 bool operator()(const SUnit* left, const SUnit* right) const;
300 };
301} // end anonymous namespace
302
303namespace {
304 class LatencyPriorityQueue : public SchedulingPriorityQueue {
305 // SUnits - The SUnits for the current graph.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000306 std::vector<SUnit> *SUnits;
Chris Lattner6398c132006-03-09 07:38:27 +0000307
308 // Latencies - The latency (max of latency from this node to the bb exit)
309 // for each node.
310 std::vector<int> Latencies;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000311
312 /// NumNodesSolelyBlocking - This vector contains, for every node in the
313 /// Queue, the number of nodes that the node is the sole unscheduled
314 /// predecessor for. This is used as a tie-breaker heuristic for better
315 /// mobility.
316 std::vector<unsigned> NumNodesSolelyBlocking;
317
Dan Gohman0d8a61e2008-06-23 23:40:09 +0000318 PriorityQueue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
Chris Lattner6398c132006-03-09 07:38:27 +0000319public:
320 LatencyPriorityQueue() : Queue(latency_sort(this)) {
321 }
322
Dan Gohman46520a22008-06-21 19:18:17 +0000323 void initNodes(std::vector<SUnit> &sunits) {
Chris Lattner6398c132006-03-09 07:38:27 +0000324 SUnits = &sunits;
325 // Calculate node priorities.
326 CalculatePriorities();
327 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000328
329 void addNode(const SUnit *SU) {
330 Latencies.resize(SUnits->size(), -1);
331 NumNodesSolelyBlocking.resize(SUnits->size(), 0);
332 CalcLatency(*SU);
333 }
334
335 void updateNode(const SUnit *SU) {
336 Latencies[SU->NodeNum] = -1;
337 CalcLatency(*SU);
338 }
339
Chris Lattner6398c132006-03-09 07:38:27 +0000340 void releaseState() {
341 SUnits = 0;
342 Latencies.clear();
343 }
344
345 unsigned getLatency(unsigned NodeNum) const {
346 assert(NodeNum < Latencies.size());
347 return Latencies[NodeNum];
348 }
349
Chris Lattner349e9dd2006-03-10 05:51:05 +0000350 unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {
351 assert(NodeNum < NumNodesSolelyBlocking.size());
352 return NumNodesSolelyBlocking[NodeNum];
353 }
354
Evan Cheng5924bf72007-09-25 01:54:36 +0000355 unsigned size() const { return Queue.size(); }
356
Chris Lattner6398c132006-03-09 07:38:27 +0000357 bool empty() const { return Queue.empty(); }
358
Chris Lattner349e9dd2006-03-10 05:51:05 +0000359 virtual void push(SUnit *U) {
360 push_impl(U);
Chris Lattner6398c132006-03-09 07:38:27 +0000361 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000362 void push_impl(SUnit *U);
363
Chris Lattner25e25562006-03-10 04:32:49 +0000364 void push_all(const std::vector<SUnit *> &Nodes) {
365 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000366 push_impl(Nodes[i]);
Chris Lattner25e25562006-03-10 04:32:49 +0000367 }
368
Chris Lattner6398c132006-03-09 07:38:27 +0000369 SUnit *pop() {
Evan Cheng61e9f0d2006-05-30 18:04:34 +0000370 if (empty()) return NULL;
Chris Lattner6398c132006-03-09 07:38:27 +0000371 SUnit *V = Queue.top();
372 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000373 return V;
374 }
Evan Cheng7d693892006-05-09 07:13:34 +0000375
Evan Cheng5924bf72007-09-25 01:54:36 +0000376 void remove(SUnit *SU) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000377 assert(!Queue.empty() && "Not in queue!");
Dan Gohman0d8a61e2008-06-23 23:40:09 +0000378 Queue.erase_one(SU);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000379 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000380
381 // ScheduledNode - As nodes are scheduled, we look to see if there are any
382 // successor nodes that have a single unscheduled predecessor. If so, that
383 // single predecessor has a higher priority, since scheduling it will make
384 // the node available.
385 void ScheduledNode(SUnit *Node);
386
387private:
388 void CalculatePriorities();
389 int CalcLatency(const SUnit &SU);
390 void AdjustPriorityOfUnscheduledPreds(SUnit *SU);
391 SUnit *getSingleUnscheduledPred(SUnit *SU);
Chris Lattner6398c132006-03-09 07:38:27 +0000392 };
393}
394
395bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
396 unsigned LHSNum = LHS->NodeNum;
397 unsigned RHSNum = RHS->NodeNum;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000398
399 // The most important heuristic is scheduling the critical path.
400 unsigned LHSLatency = PQ->getLatency(LHSNum);
401 unsigned RHSLatency = PQ->getLatency(RHSNum);
402 if (LHSLatency < RHSLatency) return true;
403 if (LHSLatency > RHSLatency) return false;
Chris Lattner6398c132006-03-09 07:38:27 +0000404
Chris Lattner349e9dd2006-03-10 05:51:05 +0000405 // After that, if two nodes have identical latencies, look to see if one will
406 // unblock more other nodes than the other.
407 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
408 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
409 if (LHSBlocked < RHSBlocked) return true;
410 if (LHSBlocked > RHSBlocked) return false;
411
412 // Finally, just to provide a stable ordering, use the node number as a
413 // deciding factor.
414 return LHSNum < RHSNum;
Chris Lattner6398c132006-03-09 07:38:27 +0000415}
416
417
418/// CalcNodePriority - Calculate the maximal path from the node to the exit.
419///
420int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
421 int &Latency = Latencies[SU.NodeNum];
422 if (Latency != -1)
423 return Latency;
Chris Lattner6398c132006-03-09 07:38:27 +0000424
Evan Cheng04c44712007-10-15 21:33:22 +0000425 std::vector<const SUnit*> WorkList;
426 WorkList.push_back(&SU);
427 while (!WorkList.empty()) {
428 const SUnit *Cur = WorkList.back();
429 bool AllDone = true;
430 int MaxSuccLatency = 0;
431 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),E = Cur->Succs.end();
432 I != E; ++I) {
433 int SuccLatency = Latencies[I->Dep->NodeNum];
434 if (SuccLatency == -1) {
435 AllDone = false;
436 WorkList.push_back(I->Dep);
437 } else {
438 MaxSuccLatency = std::max(MaxSuccLatency, SuccLatency);
439 }
440 }
441 if (AllDone) {
442 Latencies[Cur->NodeNum] = MaxSuccLatency + Cur->Latency;
443 WorkList.pop_back();
444 }
445 }
446
447 return Latency;
Chris Lattner6398c132006-03-09 07:38:27 +0000448}
449
450/// CalculatePriorities - Calculate priorities of all scheduling units.
451void LatencyPriorityQueue::CalculatePriorities() {
452 Latencies.assign(SUnits->size(), -1);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000453 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
Evan Cheng04c44712007-10-15 21:33:22 +0000454
455 // For each node, calculate the maximal path from the node to the exit.
456 std::vector<std::pair<const SUnit*, unsigned> > WorkList;
457 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
458 const SUnit *SU = &(*SUnits)[i];
Dan Gohman70de4cb2008-01-29 13:02:09 +0000459 if (SU->Succs.empty())
Evan Cheng04c44712007-10-15 21:33:22 +0000460 WorkList.push_back(std::make_pair(SU, 0U));
461 }
462
463 while (!WorkList.empty()) {
464 const SUnit *SU = WorkList.back().first;
465 unsigned SuccLat = WorkList.back().second;
466 WorkList.pop_back();
467 int &Latency = Latencies[SU->NodeNum];
468 if (Latency == -1 || (SU->Latency + SuccLat) > (unsigned)Latency) {
469 Latency = SU->Latency + SuccLat;
470 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
471 I != E; ++I)
472 WorkList.push_back(std::make_pair(I->Dep, Latency));
473 }
474 }
Chris Lattner6398c132006-03-09 07:38:27 +0000475}
476
Chris Lattner349e9dd2006-03-10 05:51:05 +0000477/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
478/// of SU, return it, otherwise return null.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000479SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000480 SUnit *OnlyAvailablePred = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000481 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
482 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000483 SUnit &Pred = *I->Dep;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000484 if (!Pred.isScheduled) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000485 // We found an available, but not scheduled, predecessor. If it's the
486 // only one we have found, keep track of it... otherwise give up.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000487 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000488 return 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000489 OnlyAvailablePred = &Pred;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000490 }
Chris Lattnerd86418a2006-08-17 00:09:56 +0000491 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000492
493 return OnlyAvailablePred;
494}
495
496void LatencyPriorityQueue::push_impl(SUnit *SU) {
497 // Look at all of the successors of this node. Count the number of nodes that
498 // this node is the sole unscheduled node for.
499 unsigned NumNodesBlocking = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000500 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
501 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000502 if (getSingleUnscheduledPred(I->Dep) == SU)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000503 ++NumNodesBlocking;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000504 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000505
506 Queue.push(SU);
507}
508
509
510// ScheduledNode - As nodes are scheduled, we look to see if there are any
511// successor nodes that have a single unscheduled predecessor. If so, that
512// single predecessor has a higher priority, since scheduling it will make
513// the node available.
514void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
Chris Lattnerd86418a2006-08-17 00:09:56 +0000515 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
516 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000517 AdjustPriorityOfUnscheduledPreds(I->Dep);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000518}
519
520/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
521/// scheduled. If SU is not itself available, then there is at least one
522/// predecessor node that has not been scheduled yet. If SU has exactly ONE
523/// unscheduled predecessor, we want to increase its priority: it getting
524/// scheduled will make this node available, so it is better than some other
525/// node of the same priority that will not make a node available.
526void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Chris Lattner572003c2006-03-12 00:38:57 +0000527 if (SU->isPending) return; // All preds scheduled.
Chris Lattner349e9dd2006-03-10 05:51:05 +0000528
529 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
530 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
531
532 // Okay, we found a single predecessor that is available, but not scheduled.
533 // Since it is available, it must be in the priority queue. First remove it.
Evan Cheng5924bf72007-09-25 01:54:36 +0000534 remove(OnlyAvailablePred);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000535
536 // Reinsert the node into the priority queue, which recomputes its
537 // NumNodesSolelyBlocking value.
538 push(OnlyAvailablePred);
539}
540
Chris Lattner9df64752006-03-09 06:35:14 +0000541
542//===----------------------------------------------------------------------===//
543// Public Constructor Functions
544//===----------------------------------------------------------------------===//
545
Jim Laskey95eda5b2006-08-01 14:21:23 +0000546/// createTDListDAGScheduler - This creates a top-down list scheduler with a
547/// new hazard recognizer. This scheduler takes ownership of the hazard
548/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000549ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
550 SelectionDAG *DAG,
Evan Cheng2c977312008-07-01 18:05:03 +0000551 MachineBasicBlock *BB, bool Fast) {
Jim Laskey95eda5b2006-08-01 14:21:23 +0000552 return new ScheduleDAGList(*DAG, BB, DAG->getTarget(),
Chris Lattner6398c132006-03-09 07:38:27 +0000553 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000554 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000555}