blob: a0858d99905a2ed4c18f6af11bbaab9540cc0b61 [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//
5// This file was developed by Evan Cheng and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
21#define DEBUG_TYPE "sched"
22#include "llvm/CodeGen/ScheduleDAG.h"
Evan Cheng9add8802006-05-04 19:16:39 +000023#include "llvm/CodeGen/SSARegMap.h"
24#include "llvm/Target/MRegisterInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Evan Cheng31272342006-01-23 08:26:10 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetInstrInfo.h"
Evan Chengab495562006-01-25 09:14:32 +000028#include "llvm/Support/Debug.h"
Chris Lattnere097e6f2006-06-28 22:17:39 +000029#include "llvm/Support/Visibility.h"
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000030#include "llvm/ADT/Statistic.h"
Evan Chengab495562006-01-25 09:14:32 +000031#include <climits>
32#include <iostream>
Evan Cheng31272342006-01-23 08:26:10 +000033#include <queue>
34using namespace llvm;
35
Evan Chengab495562006-01-25 09:14:32 +000036namespace {
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000037 Statistic<> NumNoops ("scheduler", "Number of noops inserted");
38 Statistic<> NumStalls("scheduler", "Number of pipeline stalls");
Chris Lattneraf5e26c2006-03-08 04:37:58 +000039}
Evan Chengab495562006-01-25 09:14:32 +000040
Chris Lattneraf5e26c2006-03-08 04:37:58 +000041namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +000042//===----------------------------------------------------------------------===//
43/// ScheduleDAGList - The actual list scheduler implementation. This supports
Evan Chengd38c22b2006-05-11 23:55:42 +000044/// top-down scheduling.
Chris Lattner9e95acc2006-03-09 06:37:29 +000045///
Chris Lattnere097e6f2006-06-28 22:17:39 +000046class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG {
Evan Cheng31272342006-01-23 08:26:10 +000047private:
Chris Lattner356183d2006-03-11 22:44:37 +000048 /// AvailableQueue - The priority queue to use for the available SUnits.
49 ///
50 SchedulingPriorityQueue *AvailableQueue;
Chris Lattner9df64752006-03-09 06:35:14 +000051
Chris Lattner572003c2006-03-12 00:38:57 +000052 /// PendingQueue - This contains all of the instructions whose operands have
53 /// been issued, but their results are not ready yet (due to the latency of
54 /// the operation). Once the operands becomes available, the instruction is
55 /// added to the AvailableQueue. This keeps track of each SUnit and the
56 /// number of cycles left to execute before the operation is available.
57 std::vector<std::pair<unsigned, SUnit*> > PendingQueue;
Evan Cheng9add8802006-05-04 19:16:39 +000058
Chris Lattnere50c0922006-03-05 22:45:01 +000059 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +000060 HazardRecognizer *HazardRec;
Evan Cheng9add8802006-05-04 19:16:39 +000061
Evan Cheng31272342006-01-23 08:26:10 +000062public:
63 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Evan Chengd38c22b2006-05-11 23:55:42 +000064 const TargetMachine &tm,
Chris Lattner356183d2006-03-11 22:44:37 +000065 SchedulingPriorityQueue *availqueue,
Chris Lattner543832d2006-03-08 04:25:59 +000066 HazardRecognizer *HR)
Evan Chengd38c22b2006-05-11 23:55:42 +000067 : ScheduleDAG(dag, bb, tm),
Chris Lattner356183d2006-03-11 22:44:37 +000068 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +000069 }
Evan Chengab495562006-01-25 09:14:32 +000070
71 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +000072 delete HazardRec;
Chris Lattner356183d2006-03-11 22:44:37 +000073 delete AvailableQueue;
Evan Chengab495562006-01-25 09:14:32 +000074 }
Evan Cheng31272342006-01-23 08:26:10 +000075
76 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +000077
Evan Chengab495562006-01-25 09:14:32 +000078private:
Chris Lattner572003c2006-03-12 00:38:57 +000079 void ReleaseSucc(SUnit *SuccSU, bool isChain);
Chris Lattner063086b2006-03-11 22:34:41 +000080 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner399bee22006-03-09 06:48:37 +000081 void ListScheduleTopDown();
Evan Chengab495562006-01-25 09:14:32 +000082};
Chris Lattneraf5e26c2006-03-08 04:37:58 +000083} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +000084
Chris Lattner47639db2006-03-06 00:22:00 +000085HazardRecognizer::~HazardRecognizer() {}
86
Evan Chengc4c339c2006-01-26 00:30:29 +000087
Chris Lattner9995a0c2006-03-11 22:28:35 +000088/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner9995a0c2006-03-11 22:28:35 +000089void ScheduleDAGList::Schedule() {
90 DEBUG(std::cerr << "********** List Scheduling **********\n");
91
92 // Build scheduling units.
93 BuildSchedUnits();
Evan Cheng7d693892006-05-09 07:13:34 +000094
Chris Lattner356183d2006-03-11 22:44:37 +000095 AvailableQueue->initNodes(SUnits);
Chris Lattner9995a0c2006-03-11 22:28:35 +000096
Evan Chengd38c22b2006-05-11 23:55:42 +000097 ListScheduleTopDown();
Chris Lattner9995a0c2006-03-11 22:28:35 +000098
Chris Lattner356183d2006-03-11 22:44:37 +000099 AvailableQueue->releaseState();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000100
101 DEBUG(std::cerr << "*** Final schedule ***\n");
102 DEBUG(dumpSchedule());
103 DEBUG(std::cerr << "\n");
104
105 // Emit in scheduled order
106 EmitSchedule();
107}
108
109//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000110// Top-Down Scheduling
111//===----------------------------------------------------------------------===//
112
113/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Chris Lattner572003c2006-03-12 00:38:57 +0000114/// the PendingQueue if the count reaches zero.
115void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Chris Lattner9995a0c2006-03-11 22:28:35 +0000116 if (!isChain)
117 SuccSU->NumPredsLeft--;
118 else
119 SuccSU->NumChainPredsLeft--;
120
Chris Lattner572003c2006-03-12 00:38:57 +0000121 assert(SuccSU->NumPredsLeft >= 0 && SuccSU->NumChainPredsLeft >= 0 &&
122 "List scheduling internal error");
Chris Lattner9995a0c2006-03-11 22:28:35 +0000123
124 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) {
Chris Lattner572003c2006-03-12 00:38:57 +0000125 // Compute how many cycles it will be before this actually becomes
126 // available. This is the max of the start time of all predecessors plus
127 // their latencies.
128 unsigned AvailableCycle = 0;
129 for (std::set<std::pair<SUnit*, bool> >::iterator I = SuccSU->Preds.begin(),
130 E = SuccSU->Preds.end(); I != E; ++I) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000131 // If this is a token edge, we don't need to wait for the latency of the
132 // preceeding instruction (e.g. a long-latency load) unless there is also
133 // some other data dependence.
Chris Lattner86a9b602006-03-12 03:52:09 +0000134 unsigned PredDoneCycle = I->first->Cycle;
135 if (!I->second)
136 PredDoneCycle += I->first->Latency;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000137 else if (I->first->Latency)
138 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) {
Chris Lattner572003c2006-03-12 00:38:57 +0000151 DEBUG(std::cerr << "*** 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.
158 for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
Chris Lattner356183d2006-03-11 22:44:37 +0000159 E = SU->Succs.end(); I != E; ++I)
Chris Lattner572003c2006-03-12 00:38:57 +0000160 ReleaseSucc(I->first, I->second);
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 Lattner98ecb8e2006-03-05 21:10:33 +0000167 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner572003c2006-03-12 00:38:57 +0000168
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000169 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000170 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000171 // It is available if it has no predecessors.
Chris Lattner572003c2006-03-12 00:38:57 +0000172 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Chris Lattner356183d2006-03-11 22:44:37 +0000173 AvailableQueue->push(&SUnits[i]);
Chris Lattner572003c2006-03-12 00:38:57 +0000174 SUnits[i].isAvailable = SUnits[i].isPending = true;
175 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000176 }
177
Chris Lattner572003c2006-03-12 00:38:57 +0000178 // Emit the entry node first.
179 ScheduleNodeTopDown(Entry, CurCycle);
180 HazardRec->EmitInstruction(Entry->Node);
181
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000182 // While Available queue is not empty, grab the node with the highest
183 // priority. If it is not ready put it back. Schedule the node.
184 std::vector<SUnit*> NotReady;
Chris Lattner572003c2006-03-12 00:38:57 +0000185 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
186 // Check to see if any of the pending instructions are ready to issue. If
187 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000188 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Chris Lattner572003c2006-03-12 00:38:57 +0000189 if (PendingQueue[i].first == CurCycle) {
190 AvailableQueue->push(PendingQueue[i].second);
191 PendingQueue[i].second->isAvailable = true;
192 PendingQueue[i] = PendingQueue.back();
193 PendingQueue.pop_back();
194 --i; --e;
195 } else {
196 assert(PendingQueue[i].first > CurCycle && "Negative latency?");
197 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000198 }
Chris Lattner572003c2006-03-12 00:38:57 +0000199
Chris Lattnera767dbf2006-03-12 09:01:41 +0000200 // If there are no instructions available, don't try to issue anything, and
201 // don't advance the hazard recognizer.
202 if (AvailableQueue->empty()) {
203 ++CurCycle;
204 continue;
205 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000206
Chris Lattnera767dbf2006-03-12 09:01:41 +0000207 SUnit *FoundSUnit = 0;
208 SDNode *FoundNode = 0;
209
Chris Lattnere50c0922006-03-05 22:45:01 +0000210 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000211 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000212 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000213
214 // Get the node represented by this SUnit.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000215 FoundNode = CurSUnit->Node;
216
Chris Lattner0c801bd2006-03-07 05:40:43 +0000217 // If this is a pseudo op, like copyfromreg, look to see if there is a
218 // real target node flagged to it. If so, use the target node.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000219 for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size();
220 FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
221 FoundNode = CurSUnit->FlaggedNodes[i];
Chris Lattner0c801bd2006-03-07 05:40:43 +0000222
Chris Lattnera767dbf2006-03-12 09:01:41 +0000223 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000224 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000225 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000226 break;
227 }
228
229 // Remember if this is a noop hazard.
230 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
231
Chris Lattnera767dbf2006-03-12 09:01:41 +0000232 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000233 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000234
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000235 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000236 if (!NotReady.empty()) {
237 AvailableQueue->push_all(NotReady);
238 NotReady.clear();
239 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000240
241 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000242 if (FoundSUnit) {
243 ScheduleNodeTopDown(FoundSUnit, CurCycle);
244 HazardRec->EmitInstruction(FoundNode);
245 FoundSUnit->isScheduled = true;
246 AvailableQueue->ScheduledNode(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000247
248 // If this is a pseudo-op node, we don't want to increment the current
249 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000250 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
251 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000252 } else if (!HasNoopHazards) {
253 // Otherwise, we have a pipeline stall, but no other problem, just advance
254 // the current cycle and try again.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000255 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000256 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000257 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000258 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000259 } else {
260 // Otherwise, we have no instructions to issue and we have instructions
261 // that will fault if we don't do this right. This is the case for
262 // processors without pipeline interlocks and other cases.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000263 DEBUG(std::cerr << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000264 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000265 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000266 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000267 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000268 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000269 }
270
271#ifndef NDEBUG
272 // Verify that all SUnits were scheduled.
273 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000274 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
275 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000276 if (!AnyNotSched)
277 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000278 SUnits[i].dump(&DAG);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000279 std::cerr << "has not been scheduled!\n";
280 AnyNotSched = true;
281 }
282 }
283 assert(!AnyNotSched);
284#endif
285}
286
Chris Lattner9df64752006-03-09 06:35:14 +0000287//===----------------------------------------------------------------------===//
Chris Lattner6398c132006-03-09 07:38:27 +0000288// LatencyPriorityQueue Implementation
289//===----------------------------------------------------------------------===//
290//
291// This is a SchedulingPriorityQueue that schedules using latency information to
292// reduce the length of the critical path through the basic block.
293//
294namespace {
295 class LatencyPriorityQueue;
296
297 /// Sorting functions for the Available queue.
298 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
299 LatencyPriorityQueue *PQ;
300 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
301 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
302
303 bool operator()(const SUnit* left, const SUnit* right) const;
304 };
305} // end anonymous namespace
306
307namespace {
308 class LatencyPriorityQueue : public SchedulingPriorityQueue {
309 // SUnits - The SUnits for the current graph.
310 const std::vector<SUnit> *SUnits;
311
312 // Latencies - The latency (max of latency from this node to the bb exit)
313 // for each node.
314 std::vector<int> Latencies;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000315
316 /// NumNodesSolelyBlocking - This vector contains, for every node in the
317 /// Queue, the number of nodes that the node is the sole unscheduled
318 /// predecessor for. This is used as a tie-breaker heuristic for better
319 /// mobility.
320 std::vector<unsigned> NumNodesSolelyBlocking;
321
Chris Lattner6398c132006-03-09 07:38:27 +0000322 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
323public:
324 LatencyPriorityQueue() : Queue(latency_sort(this)) {
325 }
326
327 void initNodes(const std::vector<SUnit> &sunits) {
328 SUnits = &sunits;
329 // Calculate node priorities.
330 CalculatePriorities();
331 }
332 void releaseState() {
333 SUnits = 0;
334 Latencies.clear();
335 }
336
337 unsigned getLatency(unsigned NodeNum) const {
338 assert(NodeNum < Latencies.size());
339 return Latencies[NodeNum];
340 }
341
Chris Lattner349e9dd2006-03-10 05:51:05 +0000342 unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {
343 assert(NodeNum < NumNodesSolelyBlocking.size());
344 return NumNodesSolelyBlocking[NodeNum];
345 }
346
Chris Lattner6398c132006-03-09 07:38:27 +0000347 bool empty() const { return Queue.empty(); }
348
Chris Lattner349e9dd2006-03-10 05:51:05 +0000349 virtual void push(SUnit *U) {
350 push_impl(U);
Chris Lattner6398c132006-03-09 07:38:27 +0000351 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000352 void push_impl(SUnit *U);
353
Chris Lattner25e25562006-03-10 04:32:49 +0000354 void push_all(const std::vector<SUnit *> &Nodes) {
355 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000356 push_impl(Nodes[i]);
Chris Lattner25e25562006-03-10 04:32:49 +0000357 }
358
Chris Lattner6398c132006-03-09 07:38:27 +0000359 SUnit *pop() {
Evan Cheng61e9f0d2006-05-30 18:04:34 +0000360 if (empty()) return NULL;
Chris Lattner6398c132006-03-09 07:38:27 +0000361 SUnit *V = Queue.top();
362 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000363 return V;
364 }
Evan Cheng7d693892006-05-09 07:13:34 +0000365
Evan Chengd38c22b2006-05-11 23:55:42 +0000366 // ScheduledNode - As nodes are scheduled, we look to see if there are any
367 // successor nodes that have a single unscheduled predecessor. If so, that
368 // single predecessor has a higher priority, since scheduling it will make
369 // the node available.
370 void ScheduledNode(SUnit *Node);
371
372private:
373 void CalculatePriorities();
374 int CalcLatency(const SUnit &SU);
375 void AdjustPriorityOfUnscheduledPreds(SUnit *SU);
376
Chris Lattner349e9dd2006-03-10 05:51:05 +0000377 /// RemoveFromPriorityQueue - This is a really inefficient way to remove a
378 /// node from a priority queue. We should roll our own heap to make this
379 /// better or something.
380 void RemoveFromPriorityQueue(SUnit *SU) {
381 std::vector<SUnit*> Temp;
382
383 assert(!Queue.empty() && "Not in queue!");
384 while (Queue.top() != SU) {
385 Temp.push_back(Queue.top());
386 Queue.pop();
387 assert(!Queue.empty() && "Not in queue!");
388 }
389
390 // Remove the node from the PQ.
391 Queue.pop();
392
393 // Add all the other nodes back.
394 for (unsigned i = 0, e = Temp.size(); i != e; ++i)
395 Queue.push(Temp[i]);
396 }
Chris Lattner6398c132006-03-09 07:38:27 +0000397 };
398}
399
400bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
401 unsigned LHSNum = LHS->NodeNum;
402 unsigned RHSNum = RHS->NodeNum;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000403
404 // The most important heuristic is scheduling the critical path.
405 unsigned LHSLatency = PQ->getLatency(LHSNum);
406 unsigned RHSLatency = PQ->getLatency(RHSNum);
407 if (LHSLatency < RHSLatency) return true;
408 if (LHSLatency > RHSLatency) return false;
Chris Lattner6398c132006-03-09 07:38:27 +0000409
Chris Lattner349e9dd2006-03-10 05:51:05 +0000410 // After that, if two nodes have identical latencies, look to see if one will
411 // unblock more other nodes than the other.
412 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
413 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
414 if (LHSBlocked < RHSBlocked) return true;
415 if (LHSBlocked > RHSBlocked) return false;
416
417 // Finally, just to provide a stable ordering, use the node number as a
418 // deciding factor.
419 return LHSNum < RHSNum;
Chris Lattner6398c132006-03-09 07:38:27 +0000420}
421
422
423/// CalcNodePriority - Calculate the maximal path from the node to the exit.
424///
425int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
426 int &Latency = Latencies[SU.NodeNum];
427 if (Latency != -1)
428 return Latency;
429
430 int MaxSuccLatency = 0;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000431 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU.Succs.begin(),
Chris Lattner6398c132006-03-09 07:38:27 +0000432 E = SU.Succs.end(); I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000433 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(*I->first));
Chris Lattner6398c132006-03-09 07:38:27 +0000434
435 return Latency = MaxSuccLatency + SU.Latency;
436}
437
438/// CalculatePriorities - Calculate priorities of all scheduling units.
439void LatencyPriorityQueue::CalculatePriorities() {
440 Latencies.assign(SUnits->size(), -1);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000441 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
Chris Lattner6398c132006-03-09 07:38:27 +0000442
443 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
444 CalcLatency((*SUnits)[i]);
445}
446
Chris Lattner349e9dd2006-03-10 05:51:05 +0000447/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
448/// of SU, return it, otherwise return null.
449static SUnit *getSingleUnscheduledPred(SUnit *SU) {
450 SUnit *OnlyAvailablePred = 0;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000451 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU->Preds.begin(),
Chris Lattner349e9dd2006-03-10 05:51:05 +0000452 E = SU->Preds.end(); I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000453 if (!I->first->isScheduled) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000454 // We found an available, but not scheduled, predecessor. If it's the
455 // only one we have found, keep track of it... otherwise give up.
Chris Lattner578d8fc2006-03-11 22:24:20 +0000456 if (OnlyAvailablePred && OnlyAvailablePred != I->first)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000457 return 0;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000458 OnlyAvailablePred = I->first;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000459 }
460
461 return OnlyAvailablePred;
462}
463
464void LatencyPriorityQueue::push_impl(SUnit *SU) {
465 // Look at all of the successors of this node. Count the number of nodes that
466 // this node is the sole unscheduled node for.
467 unsigned NumNodesBlocking = 0;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000468 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU->Succs.begin(),
Chris Lattner349e9dd2006-03-10 05:51:05 +0000469 E = SU->Succs.end(); I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000470 if (getSingleUnscheduledPred(I->first) == SU)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000471 ++NumNodesBlocking;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000472 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000473
474 Queue.push(SU);
475}
476
477
478// ScheduledNode - As nodes are scheduled, we look to see if there are any
479// successor nodes that have a single unscheduled predecessor. If so, that
480// single predecessor has a higher priority, since scheduling it will make
481// the node available.
482void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
Chris Lattner578d8fc2006-03-11 22:24:20 +0000483 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU->Succs.begin(),
Chris Lattner349e9dd2006-03-10 05:51:05 +0000484 E = SU->Succs.end(); I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000485 AdjustPriorityOfUnscheduledPreds(I->first);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000486}
487
488/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
489/// scheduled. If SU is not itself available, then there is at least one
490/// predecessor node that has not been scheduled yet. If SU has exactly ONE
491/// unscheduled predecessor, we want to increase its priority: it getting
492/// scheduled will make this node available, so it is better than some other
493/// node of the same priority that will not make a node available.
494void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Chris Lattner572003c2006-03-12 00:38:57 +0000495 if (SU->isPending) return; // All preds scheduled.
Chris Lattner349e9dd2006-03-10 05:51:05 +0000496
497 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
498 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
499
500 // Okay, we found a single predecessor that is available, but not scheduled.
501 // Since it is available, it must be in the priority queue. First remove it.
502 RemoveFromPriorityQueue(OnlyAvailablePred);
503
504 // Reinsert the node into the priority queue, which recomputes its
505 // NumNodesSolelyBlocking value.
506 push(OnlyAvailablePred);
507}
508
Chris Lattner9df64752006-03-09 06:35:14 +0000509
510//===----------------------------------------------------------------------===//
511// Public Constructor Functions
512//===----------------------------------------------------------------------===//
513
Chris Lattner47639db2006-03-06 00:22:00 +0000514/// createTDListDAGScheduler - This creates a top-down list scheduler with the
515/// specified hazard recognizer.
516ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
517 MachineBasicBlock *BB,
Chris Lattner543832d2006-03-08 04:25:59 +0000518 HazardRecognizer *HR) {
Evan Chengd38c22b2006-05-11 23:55:42 +0000519 return new ScheduleDAGList(DAG, BB, DAG.getTarget(),
Chris Lattner6398c132006-03-09 07:38:27 +0000520 new LatencyPriorityQueue(),
Chris Lattner9df64752006-03-09 06:35:14 +0000521 HR);
Evan Cheng31272342006-01-23 08:26:10 +0000522}