blob: cf24e46e554d7cbf25e9cb1bd4c82b45bacda1c4 [file] [log] [blame]
Evan Chengcd1419a2006-01-25 09:14:32 +00001//===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===//
Evan Chengf0f9c902006-01-23 08:26:10 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Chengf0f9c902006-01-23 08:26:10 +00007//
8//===----------------------------------------------------------------------===//
9//
Evan Chenge165a782006-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 Lattner6af7ef82006-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 Chengf0f9c902006-01-23 08:26:10 +000018//
19//===----------------------------------------------------------------------===//
20
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000021#define DEBUG_TYPE "pre-RA-sched"
Evan Chengf0f9c902006-01-23 08:26:10 +000022#include "llvm/CodeGen/ScheduleDAG.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000023#include "llvm/CodeGen/SchedulerRegistry.h"
Jim Laskey9ff542f2006-08-01 18:29:48 +000024#include "llvm/CodeGen/SelectionDAGISel.h"
Evan Cheng14a6db82006-05-04 19:16:39 +000025#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Evan Chengf0f9c902006-01-23 08:26:10 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetInstrInfo.h"
Evan Chengcd1419a2006-01-25 09:14:32 +000030#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Chris Lattner6cc3f0a2006-03-05 23:13:56 +000032#include "llvm/ADT/Statistic.h"
Evan Chengcd1419a2006-01-25 09:14:32 +000033#include <climits>
Evan Chengf0f9c902006-01-23 08:26:10 +000034#include <queue>
35using namespace llvm;
36
Chris Lattnercd3245a2006-12-19 22:41:21 +000037STATISTIC(NumNoops , "Number of noops inserted");
38STATISTIC(NumStalls, "Number of pipeline stalls");
Evan Chengcd1419a2006-01-25 09:14:32 +000039
Jim Laskey13ec7022006-08-01 14:21:23 +000040static RegisterScheduler
41 tdListDAGScheduler("list-td", " Top-down list scheduler",
42 createTDListDAGScheduler);
43
Chris Lattner5874f822006-03-08 04:37:58 +000044namespace {
Chris Lattnere87c5c82006-03-09 06:37:29 +000045//===----------------------------------------------------------------------===//
46/// ScheduleDAGList - The actual list scheduler implementation. This supports
Evan Chenge165a782006-05-11 23:55:42 +000047/// top-down scheduling.
Chris Lattnere87c5c82006-03-09 06:37:29 +000048///
Chris Lattnerf8c68f62006-06-28 22:17:39 +000049class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG {
Evan Chengf0f9c902006-01-23 08:26:10 +000050private:
Chris Lattner84690312006-03-11 22:44:37 +000051 /// AvailableQueue - The priority queue to use for the available SUnits.
52 ///
53 SchedulingPriorityQueue *AvailableQueue;
Chris Lattnere32178d2006-03-09 06:35:14 +000054
Chris Lattner53fbf2a2006-03-12 00:38:57 +000055 /// PendingQueue - This contains all of the instructions whose operands have
56 /// been issued, but their results are not ready yet (due to the latency of
57 /// the operation). Once the operands becomes available, the instruction is
58 /// added to the AvailableQueue. This keeps track of each SUnit and the
59 /// number of cycles left to execute before the operation is available.
60 std::vector<std::pair<unsigned, SUnit*> > PendingQueue;
Evan Cheng14a6db82006-05-04 19:16:39 +000061
Chris Lattnerad0f78a2006-03-05 22:45:01 +000062 /// HazardRec - The hazard recognizer to use.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000063 HazardRecognizer *HazardRec;
Evan Cheng14a6db82006-05-04 19:16:39 +000064
Evan Chengf0f9c902006-01-23 08:26:10 +000065public:
66 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Evan Chenge165a782006-05-11 23:55:42 +000067 const TargetMachine &tm,
Chris Lattner84690312006-03-11 22:44:37 +000068 SchedulingPriorityQueue *availqueue,
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000069 HazardRecognizer *HR)
Evan Chenge165a782006-05-11 23:55:42 +000070 : ScheduleDAG(dag, bb, tm),
Chris Lattner84690312006-03-11 22:44:37 +000071 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnerad0f78a2006-03-05 22:45:01 +000072 }
Evan Chengcd1419a2006-01-25 09:14:32 +000073
74 ~ScheduleDAGList() {
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000075 delete HazardRec;
Chris Lattner84690312006-03-11 22:44:37 +000076 delete AvailableQueue;
Evan Chengcd1419a2006-01-25 09:14:32 +000077 }
Evan Chengf0f9c902006-01-23 08:26:10 +000078
79 void Schedule();
Evan Chengf0f9c902006-01-23 08:26:10 +000080
Evan Chengcd1419a2006-01-25 09:14:32 +000081private:
Chris Lattner53fbf2a2006-03-12 00:38:57 +000082 void ReleaseSucc(SUnit *SuccSU, bool isChain);
Chris Lattnerc1c078c2006-03-11 22:34:41 +000083 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner0324ba82006-03-09 06:48:37 +000084 void ListScheduleTopDown();
Evan Chengcd1419a2006-01-25 09:14:32 +000085};
Chris Lattner5874f822006-03-08 04:37:58 +000086} // end anonymous namespace
Evan Chengcd1419a2006-01-25 09:14:32 +000087
Chris Lattner03fc53c2006-03-06 00:22:00 +000088HazardRecognizer::~HazardRecognizer() {}
89
Evan Chengcdf38382006-01-26 00:30:29 +000090
Chris Lattner7d82b002006-03-11 22:28:35 +000091/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner7d82b002006-03-11 22:28:35 +000092void ScheduleDAGList::Schedule() {
Bill Wendling832171c2006-12-07 20:04:42 +000093 DOUT << "********** List Scheduling **********\n";
Chris Lattner7d82b002006-03-11 22:28:35 +000094
95 // Build scheduling units.
96 BuildSchedUnits();
Evan Chengb63b0672006-05-09 07:13:34 +000097
Evan Cheng95f6ede2006-11-04 09:44:31 +000098 AvailableQueue->initNodes(SUnitMap, SUnits);
Chris Lattner7d82b002006-03-11 22:28:35 +000099
Evan Chenge165a782006-05-11 23:55:42 +0000100 ListScheduleTopDown();
Chris Lattner7d82b002006-03-11 22:28:35 +0000101
Chris Lattner84690312006-03-11 22:44:37 +0000102 AvailableQueue->releaseState();
Chris Lattner7d82b002006-03-11 22:28:35 +0000103
Bill Wendling832171c2006-12-07 20:04:42 +0000104 DOUT << "*** Final schedule ***\n";
Chris Lattner7d82b002006-03-11 22:28:35 +0000105 DEBUG(dumpSchedule());
Bill Wendling832171c2006-12-07 20:04:42 +0000106 DOUT << "\n";
Chris Lattner7d82b002006-03-11 22:28:35 +0000107
108 // Emit in scheduled order
109 EmitSchedule();
110}
111
112//===----------------------------------------------------------------------===//
Chris Lattner7d82b002006-03-11 22:28:35 +0000113// Top-Down Scheduling
114//===----------------------------------------------------------------------===//
115
116/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000117/// the PendingQueue if the count reaches zero.
118void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Evan Cheng74d2fd82007-09-28 19:24:24 +0000119 SuccSU->NumPredsLeft--;
Chris Lattner7d82b002006-03-11 22:28:35 +0000120
Evan Cheng74d2fd82007-09-28 19:24:24 +0000121 assert(SuccSU->NumPredsLeft >= 0 &&
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000122 "List scheduling internal error");
Chris Lattner7d82b002006-03-11 22:28:35 +0000123
Evan Cheng74d2fd82007-09-28 19:24:24 +0000124 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner53fbf2a2006-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;
Chris Lattner228a18e2006-08-17 00:09:56 +0000129 for (SUnit::pred_iterator I = SuccSU->Preds.begin(),
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000130 E = SuccSU->Preds.end(); I != E; ++I) {
Chris Lattnerfc3549e2006-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.
Evan Cheng713a98d2007-09-19 01:38:40 +0000134 SUnit &Pred = *I->Dep;
Chris Lattner228a18e2006-08-17 00:09:56 +0000135 unsigned PredDoneCycle = Pred.Cycle;
Evan Cheng713a98d2007-09-19 01:38:40 +0000136 if (!I->isCtrl)
Chris Lattner228a18e2006-08-17 00:09:56 +0000137 PredDoneCycle += Pred.Latency;
138 else if (Pred.Latency)
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000139 PredDoneCycle += 1;
Chris Lattnerb2215032006-03-12 03:52:09 +0000140
141 AvailableCycle = std::max(AvailableCycle, PredDoneCycle);
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000142 }
143
144 PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU));
Chris Lattner7d82b002006-03-11 22:28:35 +0000145 }
146}
147
148/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
149/// count of its successors. If a successor pending count is zero, add it to
150/// the Available queue.
Chris Lattner84690312006-03-11 22:44:37 +0000151void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling832171c2006-12-07 20:04:42 +0000152 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Chris Lattner7d82b002006-03-11 22:28:35 +0000153 DEBUG(SU->dump(&DAG));
154
155 Sequence.push_back(SU);
Chris Lattner84690312006-03-11 22:44:37 +0000156 SU->Cycle = CurCycle;
Chris Lattner7d82b002006-03-11 22:28:35 +0000157
158 // Bottom up: release successors.
Chris Lattner228a18e2006-08-17 00:09:56 +0000159 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
160 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000161 ReleaseSucc(I->Dep, I->isCtrl);
Chris Lattner7d82b002006-03-11 22:28:35 +0000162}
163
Chris Lattnera5de4842006-03-05 21:10:33 +0000164/// ListScheduleTopDown - The main loop of list scheduling for top-down
165/// schedulers.
Chris Lattner0324ba82006-03-09 06:48:37 +0000166void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000167 unsigned CurCycle = 0;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000168 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front();
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000169
Chris Lattnera5de4842006-03-05 21:10:33 +0000170 // All leaves to Available queue.
Chris Lattnerc45a59b2006-03-08 04:54:34 +0000171 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattnera5de4842006-03-05 21:10:33 +0000172 // It is available if it has no predecessors.
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000173 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Chris Lattner84690312006-03-11 22:44:37 +0000174 AvailableQueue->push(&SUnits[i]);
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000175 SUnits[i].isAvailable = SUnits[i].isPending = true;
176 }
Chris Lattnera5de4842006-03-05 21:10:33 +0000177 }
178
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000179 // Emit the entry node first.
180 ScheduleNodeTopDown(Entry, CurCycle);
181 HazardRec->EmitInstruction(Entry->Node);
182
Chris Lattnera5de4842006-03-05 21:10:33 +0000183 // While Available queue is not empty, grab the node with the highest
184 // priority. If it is not ready put it back. Schedule the node.
185 std::vector<SUnit*> NotReady;
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000186 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
187 // Check to see if any of the pending instructions are ready to issue. If
188 // so, add them to the available queue.
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000189 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000190 if (PendingQueue[i].first == CurCycle) {
191 AvailableQueue->push(PendingQueue[i].second);
192 PendingQueue[i].second->isAvailable = true;
193 PendingQueue[i] = PendingQueue.back();
194 PendingQueue.pop_back();
195 --i; --e;
196 } else {
197 assert(PendingQueue[i].first > CurCycle && "Negative latency?");
198 }
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000199 }
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000200
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000201 // If there are no instructions available, don't try to issue anything, and
202 // don't advance the hazard recognizer.
203 if (AvailableQueue->empty()) {
204 ++CurCycle;
205 continue;
206 }
Chris Lattnera5de4842006-03-05 21:10:33 +0000207
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000208 SUnit *FoundSUnit = 0;
209 SDNode *FoundNode = 0;
210
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000211 bool HasNoopHazards = false;
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000212 while (!AvailableQueue->empty()) {
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000213 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattnerb2d63582006-03-07 05:40:43 +0000214
215 // Get the node represented by this SUnit.
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000216 FoundNode = CurSUnit->Node;
217
Chris Lattnerb2d63582006-03-07 05:40:43 +0000218 // If this is a pseudo op, like copyfromreg, look to see if there is a
219 // real target node flagged to it. If so, use the target node.
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000220 for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size();
221 FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
222 FoundNode = CurSUnit->FlaggedNodes[i];
Chris Lattnerb2d63582006-03-07 05:40:43 +0000223
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000224 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000225 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000226 FoundSUnit = CurSUnit;
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000227 break;
228 }
229
230 // Remember if this is a noop hazard.
231 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
232
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000233 NotReady.push_back(CurSUnit);
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000234 }
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000235
Chris Lattnera5de4842006-03-05 21:10:33 +0000236 // Add the nodes that aren't ready back onto the available list.
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000237 if (!NotReady.empty()) {
238 AvailableQueue->push_all(NotReady);
239 NotReady.clear();
240 }
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000241
242 // If we found a node to schedule, do it now.
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000243 if (FoundSUnit) {
244 ScheduleNodeTopDown(FoundSUnit, CurCycle);
245 HazardRec->EmitInstruction(FoundNode);
246 FoundSUnit->isScheduled = true;
247 AvailableQueue->ScheduledNode(FoundSUnit);
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000248
249 // If this is a pseudo-op node, we don't want to increment the current
250 // cycle.
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000251 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
252 ++CurCycle;
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000253 } else if (!HasNoopHazards) {
254 // Otherwise, we have a pipeline stall, but no other problem, just advance
255 // the current cycle and try again.
Bill Wendling832171c2006-12-07 20:04:42 +0000256 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000257 HazardRec->AdvanceCycle();
Chris Lattner6cc3f0a2006-03-05 23:13:56 +0000258 ++NumStalls;
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000259 ++CurCycle;
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000260 } else {
261 // Otherwise, we have no instructions to issue and we have instructions
262 // that will fault if we don't do this right. This is the case for
263 // processors without pipeline interlocks and other cases.
Bill Wendling832171c2006-12-07 20:04:42 +0000264 DOUT << "*** Emitting noop\n";
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000265 HazardRec->EmitNoop();
Chris Lattner67727302006-03-05 23:59:20 +0000266 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattner6cc3f0a2006-03-05 23:13:56 +0000267 ++NumNoops;
Chris Lattnerfc3549e2006-03-12 09:01:41 +0000268 ++CurCycle;
Chris Lattnerad0f78a2006-03-05 22:45:01 +0000269 }
Chris Lattnera5de4842006-03-05 21:10:33 +0000270 }
271
272#ifndef NDEBUG
273 // Verify that all SUnits were scheduled.
274 bool AnyNotSched = false;
Chris Lattnerc45a59b2006-03-08 04:54:34 +0000275 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Evan Cheng74d2fd82007-09-28 19:24:24 +0000276 if (SUnits[i].NumPredsLeft != 0) {
Chris Lattnera5de4842006-03-05 21:10:33 +0000277 if (!AnyNotSched)
Bill Wendling832171c2006-12-07 20:04:42 +0000278 cerr << "*** List scheduling failed! ***\n";
Chris Lattnerc45a59b2006-03-08 04:54:34 +0000279 SUnits[i].dump(&DAG);
Bill Wendling832171c2006-12-07 20:04:42 +0000280 cerr << "has not been scheduled!\n";
Chris Lattnera5de4842006-03-05 21:10:33 +0000281 AnyNotSched = true;
282 }
283 }
284 assert(!AnyNotSched);
285#endif
286}
287
Chris Lattnere32178d2006-03-09 06:35:14 +0000288//===----------------------------------------------------------------------===//
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000289// LatencyPriorityQueue Implementation
290//===----------------------------------------------------------------------===//
291//
292// This is a SchedulingPriorityQueue that schedules using latency information to
293// reduce the length of the critical path through the basic block.
294//
295namespace {
296 class LatencyPriorityQueue;
297
298 /// Sorting functions for the Available queue.
299 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
300 LatencyPriorityQueue *PQ;
301 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
302 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
303
304 bool operator()(const SUnit* left, const SUnit* right) const;
305 };
306} // end anonymous namespace
307
308namespace {
309 class LatencyPriorityQueue : public SchedulingPriorityQueue {
310 // SUnits - The SUnits for the current graph.
Chris Lattner228a18e2006-08-17 00:09:56 +0000311 std::vector<SUnit> *SUnits;
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000312
313 // Latencies - The latency (max of latency from this node to the bb exit)
314 // for each node.
315 std::vector<int> Latencies;
Chris Lattnerda4ff692006-03-10 05:51:05 +0000316
317 /// NumNodesSolelyBlocking - This vector contains, for every node in the
318 /// Queue, the number of nodes that the node is the sole unscheduled
319 /// predecessor for. This is used as a tie-breaker heuristic for better
320 /// mobility.
321 std::vector<unsigned> NumNodesSolelyBlocking;
322
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000323 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
324public:
325 LatencyPriorityQueue() : Queue(latency_sort(this)) {
326 }
327
Evan Chenga6fb1b62007-09-25 01:54:36 +0000328 void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap,
Evan Cheng95f6ede2006-11-04 09:44:31 +0000329 std::vector<SUnit> &sunits) {
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000330 SUnits = &sunits;
331 // Calculate node priorities.
332 CalculatePriorities();
333 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000334
335 void addNode(const SUnit *SU) {
336 Latencies.resize(SUnits->size(), -1);
337 NumNodesSolelyBlocking.resize(SUnits->size(), 0);
338 CalcLatency(*SU);
339 }
340
341 void updateNode(const SUnit *SU) {
342 Latencies[SU->NodeNum] = -1;
343 CalcLatency(*SU);
344 }
345
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000346 void releaseState() {
347 SUnits = 0;
348 Latencies.clear();
349 }
350
351 unsigned getLatency(unsigned NodeNum) const {
352 assert(NodeNum < Latencies.size());
353 return Latencies[NodeNum];
354 }
355
Chris Lattnerda4ff692006-03-10 05:51:05 +0000356 unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {
357 assert(NodeNum < NumNodesSolelyBlocking.size());
358 return NumNodesSolelyBlocking[NodeNum];
359 }
360
Evan Chenga6fb1b62007-09-25 01:54:36 +0000361 unsigned size() const { return Queue.size(); }
362
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000363 bool empty() const { return Queue.empty(); }
364
Chris Lattnerda4ff692006-03-10 05:51:05 +0000365 virtual void push(SUnit *U) {
366 push_impl(U);
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000367 }
Chris Lattnerda4ff692006-03-10 05:51:05 +0000368 void push_impl(SUnit *U);
369
Chris Lattnerf83a47d2006-03-10 04:32:49 +0000370 void push_all(const std::vector<SUnit *> &Nodes) {
371 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerda4ff692006-03-10 05:51:05 +0000372 push_impl(Nodes[i]);
Chris Lattnerf83a47d2006-03-10 04:32:49 +0000373 }
374
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000375 SUnit *pop() {
Evan Cheng19564e32006-05-30 18:04:34 +0000376 if (empty()) return NULL;
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000377 SUnit *V = Queue.top();
378 Queue.pop();
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000379 return V;
380 }
Evan Chengb63b0672006-05-09 07:13:34 +0000381
Evan Chenga6fb1b62007-09-25 01:54:36 +0000382 /// remove - This is a really inefficient way to remove a node from a
383 /// priority queue. We should roll our own heap to make this better or
384 /// something.
385 void remove(SUnit *SU) {
Chris Lattnerda4ff692006-03-10 05:51:05 +0000386 std::vector<SUnit*> Temp;
387
388 assert(!Queue.empty() && "Not in queue!");
389 while (Queue.top() != SU) {
390 Temp.push_back(Queue.top());
391 Queue.pop();
392 assert(!Queue.empty() && "Not in queue!");
393 }
394
395 // Remove the node from the PQ.
396 Queue.pop();
397
398 // Add all the other nodes back.
399 for (unsigned i = 0, e = Temp.size(); i != e; ++i)
400 Queue.push(Temp[i]);
401 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000402
403 // ScheduledNode - As nodes are scheduled, we look to see if there are any
404 // successor nodes that have a single unscheduled predecessor. If so, that
405 // single predecessor has a higher priority, since scheduling it will make
406 // the node available.
407 void ScheduledNode(SUnit *Node);
408
409private:
410 void CalculatePriorities();
411 int CalcLatency(const SUnit &SU);
412 void AdjustPriorityOfUnscheduledPreds(SUnit *SU);
413 SUnit *getSingleUnscheduledPred(SUnit *SU);
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000414 };
415}
416
417bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
418 unsigned LHSNum = LHS->NodeNum;
419 unsigned RHSNum = RHS->NodeNum;
Chris Lattnerda4ff692006-03-10 05:51:05 +0000420
421 // The most important heuristic is scheduling the critical path.
422 unsigned LHSLatency = PQ->getLatency(LHSNum);
423 unsigned RHSLatency = PQ->getLatency(RHSNum);
424 if (LHSLatency < RHSLatency) return true;
425 if (LHSLatency > RHSLatency) return false;
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000426
Chris Lattnerda4ff692006-03-10 05:51:05 +0000427 // After that, if two nodes have identical latencies, look to see if one will
428 // unblock more other nodes than the other.
429 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
430 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
431 if (LHSBlocked < RHSBlocked) return true;
432 if (LHSBlocked > RHSBlocked) return false;
433
434 // Finally, just to provide a stable ordering, use the node number as a
435 // deciding factor.
436 return LHSNum < RHSNum;
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000437}
438
439
440/// CalcNodePriority - Calculate the maximal path from the node to the exit.
441///
442int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
443 int &Latency = Latencies[SU.NodeNum];
444 if (Latency != -1)
445 return Latency;
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000446
Evan Cheng4859e272007-10-15 21:33:22 +0000447 std::vector<const SUnit*> WorkList;
448 WorkList.push_back(&SU);
449 while (!WorkList.empty()) {
450 const SUnit *Cur = WorkList.back();
451 bool AllDone = true;
452 int MaxSuccLatency = 0;
453 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),E = Cur->Succs.end();
454 I != E; ++I) {
455 int SuccLatency = Latencies[I->Dep->NodeNum];
456 if (SuccLatency == -1) {
457 AllDone = false;
458 WorkList.push_back(I->Dep);
459 } else {
460 MaxSuccLatency = std::max(MaxSuccLatency, SuccLatency);
461 }
462 }
463 if (AllDone) {
464 Latencies[Cur->NodeNum] = MaxSuccLatency + Cur->Latency;
465 WorkList.pop_back();
466 }
467 }
468
469 return Latency;
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000470}
471
472/// CalculatePriorities - Calculate priorities of all scheduling units.
473void LatencyPriorityQueue::CalculatePriorities() {
474 Latencies.assign(SUnits->size(), -1);
Chris Lattnerda4ff692006-03-10 05:51:05 +0000475 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
Evan Cheng4859e272007-10-15 21:33:22 +0000476
477 // For each node, calculate the maximal path from the node to the exit.
478 std::vector<std::pair<const SUnit*, unsigned> > WorkList;
479 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
480 const SUnit *SU = &(*SUnits)[i];
481 if (SU->Succs.size() == 0)
482 WorkList.push_back(std::make_pair(SU, 0U));
483 }
484
485 while (!WorkList.empty()) {
486 const SUnit *SU = WorkList.back().first;
487 unsigned SuccLat = WorkList.back().second;
488 WorkList.pop_back();
489 int &Latency = Latencies[SU->NodeNum];
490 if (Latency == -1 || (SU->Latency + SuccLat) > (unsigned)Latency) {
491 Latency = SU->Latency + SuccLat;
492 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
493 I != E; ++I)
494 WorkList.push_back(std::make_pair(I->Dep, Latency));
495 }
496 }
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000497}
498
Chris Lattnerda4ff692006-03-10 05:51:05 +0000499/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
500/// of SU, return it, otherwise return null.
Chris Lattner228a18e2006-08-17 00:09:56 +0000501SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Chris Lattnerda4ff692006-03-10 05:51:05 +0000502 SUnit *OnlyAvailablePred = 0;
Chris Lattner228a18e2006-08-17 00:09:56 +0000503 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
504 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000505 SUnit &Pred = *I->Dep;
Chris Lattner228a18e2006-08-17 00:09:56 +0000506 if (!Pred.isScheduled) {
Chris Lattnerda4ff692006-03-10 05:51:05 +0000507 // We found an available, but not scheduled, predecessor. If it's the
508 // only one we have found, keep track of it... otherwise give up.
Chris Lattner228a18e2006-08-17 00:09:56 +0000509 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Chris Lattnerda4ff692006-03-10 05:51:05 +0000510 return 0;
Chris Lattner228a18e2006-08-17 00:09:56 +0000511 OnlyAvailablePred = &Pred;
Chris Lattnerda4ff692006-03-10 05:51:05 +0000512 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000513 }
Chris Lattnerda4ff692006-03-10 05:51:05 +0000514
515 return OnlyAvailablePred;
516}
517
518void LatencyPriorityQueue::push_impl(SUnit *SU) {
519 // Look at all of the successors of this node. Count the number of nodes that
520 // this node is the sole unscheduled node for.
521 unsigned NumNodesBlocking = 0;
Chris Lattner228a18e2006-08-17 00:09:56 +0000522 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
523 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000524 if (getSingleUnscheduledPred(I->Dep) == SU)
Chris Lattnerda4ff692006-03-10 05:51:05 +0000525 ++NumNodesBlocking;
Chris Lattner309cf8a2006-03-11 22:24:20 +0000526 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Chris Lattnerda4ff692006-03-10 05:51:05 +0000527
528 Queue.push(SU);
529}
530
531
532// ScheduledNode - As nodes are scheduled, we look to see if there are any
533// successor nodes that have a single unscheduled predecessor. If so, that
534// single predecessor has a higher priority, since scheduling it will make
535// the node available.
536void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
Chris Lattner228a18e2006-08-17 00:09:56 +0000537 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
538 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000539 AdjustPriorityOfUnscheduledPreds(I->Dep);
Chris Lattnerda4ff692006-03-10 05:51:05 +0000540}
541
542/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
543/// scheduled. If SU is not itself available, then there is at least one
544/// predecessor node that has not been scheduled yet. If SU has exactly ONE
545/// unscheduled predecessor, we want to increase its priority: it getting
546/// scheduled will make this node available, so it is better than some other
547/// node of the same priority that will not make a node available.
548void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Chris Lattner53fbf2a2006-03-12 00:38:57 +0000549 if (SU->isPending) return; // All preds scheduled.
Chris Lattnerda4ff692006-03-10 05:51:05 +0000550
551 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
552 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
553
554 // Okay, we found a single predecessor that is available, but not scheduled.
555 // Since it is available, it must be in the priority queue. First remove it.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000556 remove(OnlyAvailablePred);
Chris Lattnerda4ff692006-03-10 05:51:05 +0000557
558 // Reinsert the node into the priority queue, which recomputes its
559 // NumNodesSolelyBlocking value.
560 push(OnlyAvailablePred);
561}
562
Chris Lattnere32178d2006-03-09 06:35:14 +0000563
564//===----------------------------------------------------------------------===//
565// Public Constructor Functions
566//===----------------------------------------------------------------------===//
567
Jim Laskey13ec7022006-08-01 14:21:23 +0000568/// createTDListDAGScheduler - This creates a top-down list scheduler with a
569/// new hazard recognizer. This scheduler takes ownership of the hazard
570/// recognizer and deletes it when done.
Jim Laskey9ff542f2006-08-01 18:29:48 +0000571ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
572 SelectionDAG *DAG,
Jim Laskey13ec7022006-08-01 14:21:23 +0000573 MachineBasicBlock *BB) {
574 return new ScheduleDAGList(*DAG, BB, DAG->getTarget(),
Chris Lattner6a4b70b2006-03-09 07:38:27 +0000575 new LatencyPriorityQueue(),
Jim Laskey9ff542f2006-08-01 18:29:48 +0000576 IS->CreateTargetHazardRecognizer());
Evan Chengf0f9c902006-01-23 08:26:10 +0000577}