blob: 2d08cbabb5dfecaa7170da392b6d29c15b480458 [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"
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"
Evan Cheng9add8802006-05-04 19:16:39 +000025#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/MRegisterInfo.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"
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000032#include "llvm/ADT/Statistic.h"
Evan Chengab495562006-01-25 09:14:32 +000033#include <climits>
34#include <iostream>
Evan Cheng31272342006-01-23 08:26:10 +000035#include <queue>
36using namespace llvm;
37
Evan Chengab495562006-01-25 09:14:32 +000038namespace {
Chris Lattner700b8732006-12-06 17:46:33 +000039 static Statistic NumNoops ("scheduler", "Number of noops inserted");
40 static Statistic NumStalls("scheduler", "Number of pipeline stalls");
Chris Lattneraf5e26c2006-03-08 04:37:58 +000041}
Evan Chengab495562006-01-25 09:14:32 +000042
Jim Laskey95eda5b2006-08-01 14:21:23 +000043static RegisterScheduler
44 tdListDAGScheduler("list-td", " Top-down list scheduler",
45 createTDListDAGScheduler);
46
Chris Lattneraf5e26c2006-03-08 04:37:58 +000047namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +000048//===----------------------------------------------------------------------===//
49/// ScheduleDAGList - The actual list scheduler implementation. This supports
Evan Chengd38c22b2006-05-11 23:55:42 +000050/// top-down scheduling.
Chris Lattner9e95acc2006-03-09 06:37:29 +000051///
Chris Lattnere097e6f2006-06-28 22:17:39 +000052class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG {
Evan Cheng31272342006-01-23 08:26:10 +000053private:
Chris Lattner356183d2006-03-11 22:44:37 +000054 /// AvailableQueue - The priority queue to use for the available SUnits.
55 ///
56 SchedulingPriorityQueue *AvailableQueue;
Chris Lattner9df64752006-03-09 06:35:14 +000057
Chris Lattner572003c2006-03-12 00:38:57 +000058 /// PendingQueue - This contains all of the instructions whose operands have
59 /// been issued, but their results are not ready yet (due to the latency of
60 /// the operation). Once the operands becomes available, the instruction is
61 /// added to the AvailableQueue. This keeps track of each SUnit and the
62 /// number of cycles left to execute before the operation is available.
63 std::vector<std::pair<unsigned, SUnit*> > PendingQueue;
Evan Cheng9add8802006-05-04 19:16:39 +000064
Chris Lattnere50c0922006-03-05 22:45:01 +000065 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +000066 HazardRecognizer *HazardRec;
Evan Cheng9add8802006-05-04 19:16:39 +000067
Evan Cheng31272342006-01-23 08:26:10 +000068public:
69 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Evan Chengd38c22b2006-05-11 23:55:42 +000070 const TargetMachine &tm,
Chris Lattner356183d2006-03-11 22:44:37 +000071 SchedulingPriorityQueue *availqueue,
Chris Lattner543832d2006-03-08 04:25:59 +000072 HazardRecognizer *HR)
Evan Chengd38c22b2006-05-11 23:55:42 +000073 : ScheduleDAG(dag, bb, tm),
Chris Lattner356183d2006-03-11 22:44:37 +000074 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +000075 }
Evan Chengab495562006-01-25 09:14:32 +000076
77 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +000078 delete HazardRec;
Chris Lattner356183d2006-03-11 22:44:37 +000079 delete AvailableQueue;
Evan Chengab495562006-01-25 09:14:32 +000080 }
Evan Cheng31272342006-01-23 08:26:10 +000081
82 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +000083
Evan Chengab495562006-01-25 09:14:32 +000084private:
Chris Lattner572003c2006-03-12 00:38:57 +000085 void ReleaseSucc(SUnit *SuccSU, bool isChain);
Chris Lattner063086b2006-03-11 22:34:41 +000086 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner399bee22006-03-09 06:48:37 +000087 void ListScheduleTopDown();
Evan Chengab495562006-01-25 09:14:32 +000088};
Chris Lattneraf5e26c2006-03-08 04:37:58 +000089} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +000090
Chris Lattner47639db2006-03-06 00:22:00 +000091HazardRecognizer::~HazardRecognizer() {}
92
Evan Chengc4c339c2006-01-26 00:30:29 +000093
Chris Lattner9995a0c2006-03-11 22:28:35 +000094/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner9995a0c2006-03-11 22:28:35 +000095void ScheduleDAGList::Schedule() {
96 DEBUG(std::cerr << "********** List Scheduling **********\n");
97
98 // Build scheduling units.
99 BuildSchedUnits();
Evan Cheng7d693892006-05-09 07:13:34 +0000100
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000101 AvailableQueue->initNodes(SUnitMap, SUnits);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000102
Evan Chengd38c22b2006-05-11 23:55:42 +0000103 ListScheduleTopDown();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000104
Chris Lattner356183d2006-03-11 22:44:37 +0000105 AvailableQueue->releaseState();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000106
107 DEBUG(std::cerr << "*** Final schedule ***\n");
108 DEBUG(dumpSchedule());
109 DEBUG(std::cerr << "\n");
110
111 // Emit in scheduled order
112 EmitSchedule();
113}
114
115//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000116// Top-Down Scheduling
117//===----------------------------------------------------------------------===//
118
119/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Chris Lattner572003c2006-03-12 00:38:57 +0000120/// the PendingQueue if the count reaches zero.
121void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Chris Lattner9995a0c2006-03-11 22:28:35 +0000122 if (!isChain)
123 SuccSU->NumPredsLeft--;
124 else
125 SuccSU->NumChainPredsLeft--;
126
Chris Lattner572003c2006-03-12 00:38:57 +0000127 assert(SuccSU->NumPredsLeft >= 0 && SuccSU->NumChainPredsLeft >= 0 &&
128 "List scheduling internal error");
Chris Lattner9995a0c2006-03-11 22:28:35 +0000129
130 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) {
Chris Lattner572003c2006-03-12 00:38:57 +0000131 // Compute how many cycles it will be before this actually becomes
132 // available. This is the max of the start time of all predecessors plus
133 // their latencies.
134 unsigned AvailableCycle = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000135 for (SUnit::pred_iterator I = SuccSU->Preds.begin(),
Chris Lattner572003c2006-03-12 00:38:57 +0000136 E = SuccSU->Preds.end(); I != E; ++I) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000137 // If this is a token edge, we don't need to wait for the latency of the
138 // preceeding instruction (e.g. a long-latency load) unless there is also
139 // some other data dependence.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000140 SUnit &Pred = *I->first;
141 unsigned PredDoneCycle = Pred.Cycle;
Chris Lattner86a9b602006-03-12 03:52:09 +0000142 if (!I->second)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000143 PredDoneCycle += Pred.Latency;
144 else if (Pred.Latency)
Chris Lattnera767dbf2006-03-12 09:01:41 +0000145 PredDoneCycle += 1;
Chris Lattner86a9b602006-03-12 03:52:09 +0000146
147 AvailableCycle = std::max(AvailableCycle, PredDoneCycle);
Chris Lattner572003c2006-03-12 00:38:57 +0000148 }
149
150 PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU));
Chris Lattner9995a0c2006-03-11 22:28:35 +0000151 }
152}
153
154/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
155/// count of its successors. If a successor pending count is zero, add it to
156/// the Available queue.
Chris Lattner356183d2006-03-11 22:44:37 +0000157void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Chris Lattner572003c2006-03-12 00:38:57 +0000158 DEBUG(std::cerr << "*** Scheduling [" << CurCycle << "]: ");
Chris Lattner9995a0c2006-03-11 22:28:35 +0000159 DEBUG(SU->dump(&DAG));
160
161 Sequence.push_back(SU);
Chris Lattner356183d2006-03-11 22:44:37 +0000162 SU->Cycle = CurCycle;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000163
164 // Bottom up: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000165 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
166 I != E; ++I)
Chris Lattner572003c2006-03-12 00:38:57 +0000167 ReleaseSucc(I->first, I->second);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000168}
169
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000170/// ListScheduleTopDown - The main loop of list scheduling for top-down
171/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000172void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000173 unsigned CurCycle = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000174 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner572003c2006-03-12 00:38:57 +0000175
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000176 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000177 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000178 // It is available if it has no predecessors.
Chris Lattner572003c2006-03-12 00:38:57 +0000179 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Chris Lattner356183d2006-03-11 22:44:37 +0000180 AvailableQueue->push(&SUnits[i]);
Chris Lattner572003c2006-03-12 00:38:57 +0000181 SUnits[i].isAvailable = SUnits[i].isPending = true;
182 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000183 }
184
Chris Lattner572003c2006-03-12 00:38:57 +0000185 // Emit the entry node first.
186 ScheduleNodeTopDown(Entry, CurCycle);
187 HazardRec->EmitInstruction(Entry->Node);
188
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000189 // While Available queue is not empty, grab the node with the highest
190 // priority. If it is not ready put it back. Schedule the node.
191 std::vector<SUnit*> NotReady;
Chris Lattner572003c2006-03-12 00:38:57 +0000192 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
193 // Check to see if any of the pending instructions are ready to issue. If
194 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000195 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Chris Lattner572003c2006-03-12 00:38:57 +0000196 if (PendingQueue[i].first == CurCycle) {
197 AvailableQueue->push(PendingQueue[i].second);
198 PendingQueue[i].second->isAvailable = true;
199 PendingQueue[i] = PendingQueue.back();
200 PendingQueue.pop_back();
201 --i; --e;
202 } else {
203 assert(PendingQueue[i].first > CurCycle && "Negative latency?");
204 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000205 }
Chris Lattner572003c2006-03-12 00:38:57 +0000206
Chris Lattnera767dbf2006-03-12 09:01:41 +0000207 // If there are no instructions available, don't try to issue anything, and
208 // don't advance the hazard recognizer.
209 if (AvailableQueue->empty()) {
210 ++CurCycle;
211 continue;
212 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000213
Chris Lattnera767dbf2006-03-12 09:01:41 +0000214 SUnit *FoundSUnit = 0;
215 SDNode *FoundNode = 0;
216
Chris Lattnere50c0922006-03-05 22:45:01 +0000217 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000218 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000219 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000220
221 // Get the node represented by this SUnit.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000222 FoundNode = CurSUnit->Node;
223
Chris Lattner0c801bd2006-03-07 05:40:43 +0000224 // If this is a pseudo op, like copyfromreg, look to see if there is a
225 // real target node flagged to it. If so, use the target node.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000226 for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size();
227 FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
228 FoundNode = CurSUnit->FlaggedNodes[i];
Chris Lattner0c801bd2006-03-07 05:40:43 +0000229
Chris Lattnera767dbf2006-03-12 09:01:41 +0000230 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000231 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000232 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000233 break;
234 }
235
236 // Remember if this is a noop hazard.
237 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
238
Chris Lattnera767dbf2006-03-12 09:01:41 +0000239 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000240 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000241
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000242 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000243 if (!NotReady.empty()) {
244 AvailableQueue->push_all(NotReady);
245 NotReady.clear();
246 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000247
248 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000249 if (FoundSUnit) {
250 ScheduleNodeTopDown(FoundSUnit, CurCycle);
251 HazardRec->EmitInstruction(FoundNode);
252 FoundSUnit->isScheduled = true;
253 AvailableQueue->ScheduledNode(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000254
255 // If this is a pseudo-op node, we don't want to increment the current
256 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000257 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
258 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000259 } else if (!HasNoopHazards) {
260 // Otherwise, we have a pipeline stall, but no other problem, just advance
261 // the current cycle and try again.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000262 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000263 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000264 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000265 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000266 } else {
267 // Otherwise, we have no instructions to issue and we have instructions
268 // that will fault if we don't do this right. This is the case for
269 // processors without pipeline interlocks and other cases.
Chris Lattner0c801bd2006-03-07 05:40:43 +0000270 DEBUG(std::cerr << "*** Emitting noop\n");
Chris Lattner543832d2006-03-08 04:25:59 +0000271 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000272 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000273 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000274 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000275 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000276 }
277
278#ifndef NDEBUG
279 // Verify that all SUnits were scheduled.
280 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000281 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
282 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000283 if (!AnyNotSched)
284 std::cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000285 SUnits[i].dump(&DAG);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000286 std::cerr << "has not been scheduled!\n";
287 AnyNotSched = true;
288 }
289 }
290 assert(!AnyNotSched);
291#endif
292}
293
Chris Lattner9df64752006-03-09 06:35:14 +0000294//===----------------------------------------------------------------------===//
Chris Lattner6398c132006-03-09 07:38:27 +0000295// LatencyPriorityQueue Implementation
296//===----------------------------------------------------------------------===//
297//
298// This is a SchedulingPriorityQueue that schedules using latency information to
299// reduce the length of the critical path through the basic block.
300//
301namespace {
302 class LatencyPriorityQueue;
303
304 /// Sorting functions for the Available queue.
305 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
306 LatencyPriorityQueue *PQ;
307 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
308 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
309
310 bool operator()(const SUnit* left, const SUnit* right) const;
311 };
312} // end anonymous namespace
313
314namespace {
315 class LatencyPriorityQueue : public SchedulingPriorityQueue {
316 // SUnits - The SUnits for the current graph.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000317 std::vector<SUnit> *SUnits;
Chris Lattner6398c132006-03-09 07:38:27 +0000318
319 // Latencies - The latency (max of latency from this node to the bb exit)
320 // for each node.
321 std::vector<int> Latencies;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000322
323 /// NumNodesSolelyBlocking - This vector contains, for every node in the
324 /// Queue, the number of nodes that the node is the sole unscheduled
325 /// predecessor for. This is used as a tie-breaker heuristic for better
326 /// mobility.
327 std::vector<unsigned> NumNodesSolelyBlocking;
328
Chris Lattner6398c132006-03-09 07:38:27 +0000329 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
330public:
331 LatencyPriorityQueue() : Queue(latency_sort(this)) {
332 }
333
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000334 void initNodes(std::map<SDNode*, SUnit*> &sumap,
335 std::vector<SUnit> &sunits) {
Chris Lattner6398c132006-03-09 07:38:27 +0000336 SUnits = &sunits;
337 // Calculate node priorities.
338 CalculatePriorities();
339 }
340 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
Chris Lattner6398c132006-03-09 07:38:27 +0000355 bool empty() const { return Queue.empty(); }
356
Chris Lattner349e9dd2006-03-10 05:51:05 +0000357 virtual void push(SUnit *U) {
358 push_impl(U);
Chris Lattner6398c132006-03-09 07:38:27 +0000359 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000360 void push_impl(SUnit *U);
361
Chris Lattner25e25562006-03-10 04:32:49 +0000362 void push_all(const std::vector<SUnit *> &Nodes) {
363 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000364 push_impl(Nodes[i]);
Chris Lattner25e25562006-03-10 04:32:49 +0000365 }
366
Chris Lattner6398c132006-03-09 07:38:27 +0000367 SUnit *pop() {
Evan Cheng61e9f0d2006-05-30 18:04:34 +0000368 if (empty()) return NULL;
Chris Lattner6398c132006-03-09 07:38:27 +0000369 SUnit *V = Queue.top();
370 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000371 return V;
372 }
Evan Cheng7d693892006-05-09 07:13:34 +0000373
Evan Chengd38c22b2006-05-11 23:55:42 +0000374 // 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);
Chris Lattnerd86418a2006-08-17 00:09:56 +0000384 SUnit *getSingleUnscheduledPred(SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000385
Chris Lattner349e9dd2006-03-10 05:51:05 +0000386 /// RemoveFromPriorityQueue - This is a really inefficient way to remove a
387 /// node from a priority queue. We should roll our own heap to make this
388 /// better or something.
389 void RemoveFromPriorityQueue(SUnit *SU) {
390 std::vector<SUnit*> Temp;
391
392 assert(!Queue.empty() && "Not in queue!");
393 while (Queue.top() != SU) {
394 Temp.push_back(Queue.top());
395 Queue.pop();
396 assert(!Queue.empty() && "Not in queue!");
397 }
398
399 // Remove the node from the PQ.
400 Queue.pop();
401
402 // Add all the other nodes back.
403 for (unsigned i = 0, e = Temp.size(); i != e; ++i)
404 Queue.push(Temp[i]);
405 }
Chris Lattner6398c132006-03-09 07:38:27 +0000406 };
407}
408
409bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
410 unsigned LHSNum = LHS->NodeNum;
411 unsigned RHSNum = RHS->NodeNum;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000412
413 // The most important heuristic is scheduling the critical path.
414 unsigned LHSLatency = PQ->getLatency(LHSNum);
415 unsigned RHSLatency = PQ->getLatency(RHSNum);
416 if (LHSLatency < RHSLatency) return true;
417 if (LHSLatency > RHSLatency) return false;
Chris Lattner6398c132006-03-09 07:38:27 +0000418
Chris Lattner349e9dd2006-03-10 05:51:05 +0000419 // After that, if two nodes have identical latencies, look to see if one will
420 // unblock more other nodes than the other.
421 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
422 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
423 if (LHSBlocked < RHSBlocked) return true;
424 if (LHSBlocked > RHSBlocked) return false;
425
426 // Finally, just to provide a stable ordering, use the node number as a
427 // deciding factor.
428 return LHSNum < RHSNum;
Chris Lattner6398c132006-03-09 07:38:27 +0000429}
430
431
432/// CalcNodePriority - Calculate the maximal path from the node to the exit.
433///
434int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
435 int &Latency = Latencies[SU.NodeNum];
436 if (Latency != -1)
437 return Latency;
438
439 int MaxSuccLatency = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000440 for (SUnit::const_succ_iterator I = SU.Succs.begin(), E = SU.Succs.end();
441 I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000442 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(*I->first));
Chris Lattner6398c132006-03-09 07:38:27 +0000443
444 return Latency = MaxSuccLatency + SU.Latency;
445}
446
447/// CalculatePriorities - Calculate priorities of all scheduling units.
448void LatencyPriorityQueue::CalculatePriorities() {
449 Latencies.assign(SUnits->size(), -1);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000450 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
Chris Lattner6398c132006-03-09 07:38:27 +0000451
452 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
453 CalcLatency((*SUnits)[i]);
454}
455
Chris Lattner349e9dd2006-03-10 05:51:05 +0000456/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
457/// of SU, return it, otherwise return null.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000458SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000459 SUnit *OnlyAvailablePred = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000460 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
461 I != E; ++I) {
462 SUnit &Pred = *I->first;
463 if (!Pred.isScheduled) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000464 // We found an available, but not scheduled, predecessor. If it's the
465 // only one we have found, keep track of it... otherwise give up.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000466 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000467 return 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000468 OnlyAvailablePred = &Pred;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000469 }
Chris Lattnerd86418a2006-08-17 00:09:56 +0000470 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000471
472 return OnlyAvailablePred;
473}
474
475void LatencyPriorityQueue::push_impl(SUnit *SU) {
476 // Look at all of the successors of this node. Count the number of nodes that
477 // this node is the sole unscheduled node for.
478 unsigned NumNodesBlocking = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000479 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
480 I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000481 if (getSingleUnscheduledPred(I->first) == SU)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000482 ++NumNodesBlocking;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000483 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000484
485 Queue.push(SU);
486}
487
488
489// ScheduledNode - As nodes are scheduled, we look to see if there are any
490// successor nodes that have a single unscheduled predecessor. If so, that
491// single predecessor has a higher priority, since scheduling it will make
492// the node available.
493void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
Chris Lattnerd86418a2006-08-17 00:09:56 +0000494 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
495 I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000496 AdjustPriorityOfUnscheduledPreds(I->first);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000497}
498
499/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
500/// scheduled. If SU is not itself available, then there is at least one
501/// predecessor node that has not been scheduled yet. If SU has exactly ONE
502/// unscheduled predecessor, we want to increase its priority: it getting
503/// scheduled will make this node available, so it is better than some other
504/// node of the same priority that will not make a node available.
505void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Chris Lattner572003c2006-03-12 00:38:57 +0000506 if (SU->isPending) return; // All preds scheduled.
Chris Lattner349e9dd2006-03-10 05:51:05 +0000507
508 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
509 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
510
511 // Okay, we found a single predecessor that is available, but not scheduled.
512 // Since it is available, it must be in the priority queue. First remove it.
513 RemoveFromPriorityQueue(OnlyAvailablePred);
514
515 // Reinsert the node into the priority queue, which recomputes its
516 // NumNodesSolelyBlocking value.
517 push(OnlyAvailablePred);
518}
519
Chris Lattner9df64752006-03-09 06:35:14 +0000520
521//===----------------------------------------------------------------------===//
522// Public Constructor Functions
523//===----------------------------------------------------------------------===//
524
Jim Laskey95eda5b2006-08-01 14:21:23 +0000525/// createTDListDAGScheduler - This creates a top-down list scheduler with a
526/// new hazard recognizer. This scheduler takes ownership of the hazard
527/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000528ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
529 SelectionDAG *DAG,
Jim Laskey95eda5b2006-08-01 14:21:23 +0000530 MachineBasicBlock *BB) {
531 return new ScheduleDAGList(*DAG, BB, DAG->getTarget(),
Chris Lattner6398c132006-03-09 07:38:27 +0000532 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000533 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000534}