blob: 067407b1eb84e5f27bf13eb1cb69066df6b20c22 [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
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///
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}
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
Chris Lattner572003c2006-03-12 00:38:57 +0000109/// the PendingQueue if the count reaches zero.
110void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000111 SuccSU->NumPredsLeft--;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000112
Evan Cheng038dcc52007-09-28 19:24:24 +0000113 assert(SuccSU->NumPredsLeft >= 0 &&
Chris Lattner572003c2006-03-12 00:38:57 +0000114 "List scheduling internal error");
Chris Lattner9995a0c2006-03-11 22:28:35 +0000115
Evan Cheng038dcc52007-09-28 19:24:24 +0000116 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner572003c2006-03-12 00:38:57 +0000117 // Compute how many cycles it will be before this actually becomes
118 // available. This is the max of the start time of all predecessors plus
119 // their latencies.
120 unsigned AvailableCycle = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000121 for (SUnit::pred_iterator I = SuccSU->Preds.begin(),
Chris Lattner572003c2006-03-12 00:38:57 +0000122 E = SuccSU->Preds.end(); I != E; ++I) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000123 // If this is a token edge, we don't need to wait for the latency of the
124 // preceeding instruction (e.g. a long-latency load) unless there is also
125 // some other data dependence.
Evan Cheng0effc3a2007-09-19 01:38:40 +0000126 SUnit &Pred = *I->Dep;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000127 unsigned PredDoneCycle = Pred.Cycle;
Evan Cheng0effc3a2007-09-19 01:38:40 +0000128 if (!I->isCtrl)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000129 PredDoneCycle += Pred.Latency;
130 else if (Pred.Latency)
Chris Lattnera767dbf2006-03-12 09:01:41 +0000131 PredDoneCycle += 1;
Chris Lattner86a9b602006-03-12 03:52:09 +0000132
133 AvailableCycle = std::max(AvailableCycle, PredDoneCycle);
Chris Lattner572003c2006-03-12 00:38:57 +0000134 }
135
136 PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU));
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) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000144 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000145 DEBUG(SU->dump(&DAG));
146
147 Sequence.push_back(SU);
Chris Lattner356183d2006-03-11 22:44:37 +0000148 SU->Cycle = CurCycle;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000149
150 // Bottom up: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000151 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
152 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000153 ReleaseSucc(I->Dep, I->isCtrl);
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
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000161 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000162 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000163 // It is available if it has no predecessors.
Dan Gohman4370f262008-04-15 01:22:18 +0000164 if (SUnits[i].Preds.empty()) {
Chris Lattner356183d2006-03-11 22:44:37 +0000165 AvailableQueue->push(&SUnits[i]);
Chris Lattner572003c2006-03-12 00:38:57 +0000166 SUnits[i].isAvailable = SUnits[i].isPending = true;
167 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000168 }
169
170 // While Available queue is not empty, grab the node with the highest
171 // priority. If it is not ready put it back. Schedule the node.
172 std::vector<SUnit*> NotReady;
Dan Gohmane6e13482008-06-21 15:52:51 +0000173 Sequence.reserve(SUnits.size());
Chris Lattner572003c2006-03-12 00:38:57 +0000174 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
175 // Check to see if any of the pending instructions are ready to issue. If
176 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000177 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Chris Lattner572003c2006-03-12 00:38:57 +0000178 if (PendingQueue[i].first == CurCycle) {
179 AvailableQueue->push(PendingQueue[i].second);
180 PendingQueue[i].second->isAvailable = true;
181 PendingQueue[i] = PendingQueue.back();
182 PendingQueue.pop_back();
183 --i; --e;
184 } else {
185 assert(PendingQueue[i].first > CurCycle && "Negative latency?");
186 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000187 }
Chris Lattner572003c2006-03-12 00:38:57 +0000188
Chris Lattnera767dbf2006-03-12 09:01:41 +0000189 // If there are no instructions available, don't try to issue anything, and
190 // don't advance the hazard recognizer.
191 if (AvailableQueue->empty()) {
192 ++CurCycle;
193 continue;
194 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000195
Chris Lattnera767dbf2006-03-12 09:01:41 +0000196 SUnit *FoundSUnit = 0;
197 SDNode *FoundNode = 0;
198
Chris Lattnere50c0922006-03-05 22:45:01 +0000199 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000200 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000201 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000202
203 // Get the node represented by this SUnit.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000204 FoundNode = CurSUnit->Node;
205
Chris Lattner0c801bd2006-03-07 05:40:43 +0000206 // If this is a pseudo op, like copyfromreg, look to see if there is a
207 // real target node flagged to it. If so, use the target node.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000208 for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size();
Dan Gohman17059682008-07-17 19:10:17 +0000209 !FoundNode->isMachineOpcode() && i != e; ++i)
Chris Lattnera767dbf2006-03-12 09:01:41 +0000210 FoundNode = CurSUnit->FlaggedNodes[i];
Chris Lattner0c801bd2006-03-07 05:40:43 +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 }
217
218 // 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);
234 FoundSUnit->isScheduled = true;
235 AvailableQueue->ScheduledNode(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000236
237 // If this is a pseudo-op node, we don't want to increment the current
238 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000239 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
240 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000241 } else if (!HasNoopHazards) {
242 // Otherwise, we have a pipeline stall, but no other problem, just advance
243 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000244 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000245 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000246 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000247 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000248 } else {
249 // Otherwise, we have no instructions to issue and we have instructions
250 // that will fault if we don't do this right. This is the case for
251 // processors without pipeline interlocks and other cases.
Bill Wendling22e978a2006-12-07 20:04:42 +0000252 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000253 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000254 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000255 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000256 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000257 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000258 }
259
260#ifndef NDEBUG
261 // Verify that all SUnits were scheduled.
262 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000263 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Evan Cheng038dcc52007-09-28 19:24:24 +0000264 if (SUnits[i].NumPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000265 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000266 cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000267 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000268 cerr << "has not been scheduled!\n";
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000269 AnyNotSched = true;
270 }
271 }
272 assert(!AnyNotSched);
273#endif
274}
275
Chris Lattner9df64752006-03-09 06:35:14 +0000276//===----------------------------------------------------------------------===//
Chris Lattner6398c132006-03-09 07:38:27 +0000277// LatencyPriorityQueue Implementation
278//===----------------------------------------------------------------------===//
279//
280// This is a SchedulingPriorityQueue that schedules using latency information to
281// reduce the length of the critical path through the basic block.
282//
283namespace {
284 class LatencyPriorityQueue;
285
286 /// Sorting functions for the Available queue.
287 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
288 LatencyPriorityQueue *PQ;
289 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
290 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
291
292 bool operator()(const SUnit* left, const SUnit* right) const;
293 };
294} // end anonymous namespace
295
296namespace {
297 class LatencyPriorityQueue : public SchedulingPriorityQueue {
298 // SUnits - The SUnits for the current graph.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000299 std::vector<SUnit> *SUnits;
Chris Lattner6398c132006-03-09 07:38:27 +0000300
301 // Latencies - The latency (max of latency from this node to the bb exit)
302 // for each node.
303 std::vector<int> Latencies;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000304
305 /// NumNodesSolelyBlocking - This vector contains, for every node in the
306 /// Queue, the number of nodes that the node is the sole unscheduled
307 /// predecessor for. This is used as a tie-breaker heuristic for better
308 /// mobility.
309 std::vector<unsigned> NumNodesSolelyBlocking;
310
Dan Gohman0d8a61e2008-06-23 23:40:09 +0000311 PriorityQueue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
Chris Lattner6398c132006-03-09 07:38:27 +0000312public:
313 LatencyPriorityQueue() : Queue(latency_sort(this)) {
314 }
315
Dan Gohman46520a22008-06-21 19:18:17 +0000316 void initNodes(std::vector<SUnit> &sunits) {
Chris Lattner6398c132006-03-09 07:38:27 +0000317 SUnits = &sunits;
318 // Calculate node priorities.
319 CalculatePriorities();
320 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000321
322 void addNode(const SUnit *SU) {
323 Latencies.resize(SUnits->size(), -1);
324 NumNodesSolelyBlocking.resize(SUnits->size(), 0);
325 CalcLatency(*SU);
326 }
327
328 void updateNode(const SUnit *SU) {
329 Latencies[SU->NodeNum] = -1;
330 CalcLatency(*SU);
331 }
332
Chris Lattner6398c132006-03-09 07:38:27 +0000333 void releaseState() {
334 SUnits = 0;
335 Latencies.clear();
336 }
337
338 unsigned getLatency(unsigned NodeNum) const {
339 assert(NodeNum < Latencies.size());
340 return Latencies[NodeNum];
341 }
342
Chris Lattner349e9dd2006-03-10 05:51:05 +0000343 unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {
344 assert(NodeNum < NumNodesSolelyBlocking.size());
345 return NumNodesSolelyBlocking[NodeNum];
346 }
347
Evan Cheng5924bf72007-09-25 01:54:36 +0000348 unsigned size() const { return Queue.size(); }
349
Chris Lattner6398c132006-03-09 07:38:27 +0000350 bool empty() const { return Queue.empty(); }
351
Chris Lattner349e9dd2006-03-10 05:51:05 +0000352 virtual void push(SUnit *U) {
353 push_impl(U);
Chris Lattner6398c132006-03-09 07:38:27 +0000354 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000355 void push_impl(SUnit *U);
356
Chris Lattner25e25562006-03-10 04:32:49 +0000357 void push_all(const std::vector<SUnit *> &Nodes) {
358 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000359 push_impl(Nodes[i]);
Chris Lattner25e25562006-03-10 04:32:49 +0000360 }
361
Chris Lattner6398c132006-03-09 07:38:27 +0000362 SUnit *pop() {
Evan Cheng61e9f0d2006-05-30 18:04:34 +0000363 if (empty()) return NULL;
Chris Lattner6398c132006-03-09 07:38:27 +0000364 SUnit *V = Queue.top();
365 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000366 return V;
367 }
Evan Cheng7d693892006-05-09 07:13:34 +0000368
Evan Cheng5924bf72007-09-25 01:54:36 +0000369 void remove(SUnit *SU) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000370 assert(!Queue.empty() && "Not in queue!");
Dan Gohman0d8a61e2008-06-23 23:40:09 +0000371 Queue.erase_one(SU);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000372 }
Evan Cheng5924bf72007-09-25 01:54:36 +0000373
374 // ScheduledNode - As nodes are scheduled, we look to see if there are any
375 // successor nodes that have a single unscheduled predecessor. If so, that
376 // single predecessor has a higher priority, since scheduling it will make
377 // the node available.
378 void ScheduledNode(SUnit *Node);
379
380private:
381 void CalculatePriorities();
382 int CalcLatency(const SUnit &SU);
383 void AdjustPriorityOfUnscheduledPreds(SUnit *SU);
384 SUnit *getSingleUnscheduledPred(SUnit *SU);
Chris Lattner6398c132006-03-09 07:38:27 +0000385 };
386}
387
388bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
389 unsigned LHSNum = LHS->NodeNum;
390 unsigned RHSNum = RHS->NodeNum;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000391
392 // The most important heuristic is scheduling the critical path.
393 unsigned LHSLatency = PQ->getLatency(LHSNum);
394 unsigned RHSLatency = PQ->getLatency(RHSNum);
395 if (LHSLatency < RHSLatency) return true;
396 if (LHSLatency > RHSLatency) return false;
Chris Lattner6398c132006-03-09 07:38:27 +0000397
Chris Lattner349e9dd2006-03-10 05:51:05 +0000398 // After that, if two nodes have identical latencies, look to see if one will
399 // unblock more other nodes than the other.
400 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
401 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
402 if (LHSBlocked < RHSBlocked) return true;
403 if (LHSBlocked > RHSBlocked) return false;
404
405 // Finally, just to provide a stable ordering, use the node number as a
406 // deciding factor.
407 return LHSNum < RHSNum;
Chris Lattner6398c132006-03-09 07:38:27 +0000408}
409
410
411/// CalcNodePriority - Calculate the maximal path from the node to the exit.
412///
413int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
414 int &Latency = Latencies[SU.NodeNum];
415 if (Latency != -1)
416 return Latency;
Chris Lattner6398c132006-03-09 07:38:27 +0000417
Evan Cheng04c44712007-10-15 21:33:22 +0000418 std::vector<const SUnit*> WorkList;
419 WorkList.push_back(&SU);
420 while (!WorkList.empty()) {
421 const SUnit *Cur = WorkList.back();
422 bool AllDone = true;
423 int MaxSuccLatency = 0;
424 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),E = Cur->Succs.end();
425 I != E; ++I) {
426 int SuccLatency = Latencies[I->Dep->NodeNum];
427 if (SuccLatency == -1) {
428 AllDone = false;
429 WorkList.push_back(I->Dep);
430 } else {
431 MaxSuccLatency = std::max(MaxSuccLatency, SuccLatency);
432 }
433 }
434 if (AllDone) {
435 Latencies[Cur->NodeNum] = MaxSuccLatency + Cur->Latency;
436 WorkList.pop_back();
437 }
438 }
439
440 return Latency;
Chris Lattner6398c132006-03-09 07:38:27 +0000441}
442
443/// CalculatePriorities - Calculate priorities of all scheduling units.
444void LatencyPriorityQueue::CalculatePriorities() {
445 Latencies.assign(SUnits->size(), -1);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000446 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
Evan Cheng04c44712007-10-15 21:33:22 +0000447
448 // For each node, calculate the maximal path from the node to the exit.
449 std::vector<std::pair<const SUnit*, unsigned> > WorkList;
450 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
451 const SUnit *SU = &(*SUnits)[i];
Dan Gohman70de4cb2008-01-29 13:02:09 +0000452 if (SU->Succs.empty())
Evan Cheng04c44712007-10-15 21:33:22 +0000453 WorkList.push_back(std::make_pair(SU, 0U));
454 }
455
456 while (!WorkList.empty()) {
457 const SUnit *SU = WorkList.back().first;
458 unsigned SuccLat = WorkList.back().second;
459 WorkList.pop_back();
460 int &Latency = Latencies[SU->NodeNum];
461 if (Latency == -1 || (SU->Latency + SuccLat) > (unsigned)Latency) {
462 Latency = SU->Latency + SuccLat;
463 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
464 I != E; ++I)
465 WorkList.push_back(std::make_pair(I->Dep, Latency));
466 }
467 }
Chris Lattner6398c132006-03-09 07:38:27 +0000468}
469
Chris Lattner349e9dd2006-03-10 05:51:05 +0000470/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
471/// of SU, return it, otherwise return null.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000472SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000473 SUnit *OnlyAvailablePred = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000474 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
475 I != E; ++I) {
Evan Cheng0effc3a2007-09-19 01:38:40 +0000476 SUnit &Pred = *I->Dep;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000477 if (!Pred.isScheduled) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000478 // We found an available, but not scheduled, predecessor. If it's the
479 // only one we have found, keep track of it... otherwise give up.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000480 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000481 return 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000482 OnlyAvailablePred = &Pred;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000483 }
Chris Lattnerd86418a2006-08-17 00:09:56 +0000484 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000485
486 return OnlyAvailablePred;
487}
488
489void LatencyPriorityQueue::push_impl(SUnit *SU) {
490 // Look at all of the successors of this node. Count the number of nodes that
491 // this node is the sole unscheduled node for.
492 unsigned NumNodesBlocking = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000493 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
494 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000495 if (getSingleUnscheduledPred(I->Dep) == SU)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000496 ++NumNodesBlocking;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000497 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000498
499 Queue.push(SU);
500}
501
502
503// ScheduledNode - As nodes are scheduled, we look to see if there are any
504// successor nodes that have a single unscheduled predecessor. If so, that
505// single predecessor has a higher priority, since scheduling it will make
506// the node available.
507void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
Chris Lattnerd86418a2006-08-17 00:09:56 +0000508 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
509 I != E; ++I)
Evan Cheng0effc3a2007-09-19 01:38:40 +0000510 AdjustPriorityOfUnscheduledPreds(I->Dep);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000511}
512
513/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
514/// scheduled. If SU is not itself available, then there is at least one
515/// predecessor node that has not been scheduled yet. If SU has exactly ONE
516/// unscheduled predecessor, we want to increase its priority: it getting
517/// scheduled will make this node available, so it is better than some other
518/// node of the same priority that will not make a node available.
519void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Chris Lattner572003c2006-03-12 00:38:57 +0000520 if (SU->isPending) return; // All preds scheduled.
Chris Lattner349e9dd2006-03-10 05:51:05 +0000521
522 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
523 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
524
525 // Okay, we found a single predecessor that is available, but not scheduled.
526 // Since it is available, it must be in the priority queue. First remove it.
Evan Cheng5924bf72007-09-25 01:54:36 +0000527 remove(OnlyAvailablePred);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000528
529 // Reinsert the node into the priority queue, which recomputes its
530 // NumNodesSolelyBlocking value.
531 push(OnlyAvailablePred);
532}
533
Chris Lattner9df64752006-03-09 06:35:14 +0000534
535//===----------------------------------------------------------------------===//
536// Public Constructor Functions
537//===----------------------------------------------------------------------===//
538
Jim Laskey95eda5b2006-08-01 14:21:23 +0000539/// createTDListDAGScheduler - This creates a top-down list scheduler with a
540/// new hazard recognizer. This scheduler takes ownership of the hazard
541/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000542ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
543 SelectionDAG *DAG,
Evan Cheng2c977312008-07-01 18:05:03 +0000544 MachineBasicBlock *BB, bool Fast) {
Jim Laskey95eda5b2006-08-01 14:21:23 +0000545 return new ScheduleDAGList(*DAG, BB, DAG->getTarget(),
Chris Lattner6398c132006-03-09 07:38:27 +0000546 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000547 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000548}