blob: d21ef34c6457610534bfbfecbb8ec8350479bcbf [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>
Evan Cheng31272342006-01-23 08:26:10 +000034#include <queue>
35using namespace llvm;
36
Chris Lattneraee775a2006-12-19 22:41:21 +000037STATISTIC(NumNoops , "Number of noops inserted");
38STATISTIC(NumStalls, "Number of pipeline stalls");
Evan Chengab495562006-01-25 09:14:32 +000039
Jim Laskey95eda5b2006-08-01 14:21:23 +000040static RegisterScheduler
41 tdListDAGScheduler("list-td", " Top-down list scheduler",
42 createTDListDAGScheduler);
43
Chris Lattneraf5e26c2006-03-08 04:37:58 +000044namespace {
Chris Lattner9e95acc2006-03-09 06:37:29 +000045//===----------------------------------------------------------------------===//
46/// ScheduleDAGList - The actual list scheduler implementation. This supports
Evan Chengd38c22b2006-05-11 23:55:42 +000047/// top-down scheduling.
Chris Lattner9e95acc2006-03-09 06:37:29 +000048///
Chris Lattnere097e6f2006-06-28 22:17:39 +000049class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG {
Evan Cheng31272342006-01-23 08:26:10 +000050private:
Chris Lattner356183d2006-03-11 22:44:37 +000051 /// AvailableQueue - The priority queue to use for the available SUnits.
52 ///
53 SchedulingPriorityQueue *AvailableQueue;
Chris Lattner9df64752006-03-09 06:35:14 +000054
Chris Lattner572003c2006-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 Cheng9add8802006-05-04 19:16:39 +000061
Chris Lattnere50c0922006-03-05 22:45:01 +000062 /// HazardRec - The hazard recognizer to use.
Chris Lattner543832d2006-03-08 04:25:59 +000063 HazardRecognizer *HazardRec;
Evan Cheng9add8802006-05-04 19:16:39 +000064
Evan Cheng31272342006-01-23 08:26:10 +000065public:
66 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Evan Chengd38c22b2006-05-11 23:55:42 +000067 const TargetMachine &tm,
Chris Lattner356183d2006-03-11 22:44:37 +000068 SchedulingPriorityQueue *availqueue,
Chris Lattner543832d2006-03-08 04:25:59 +000069 HazardRecognizer *HR)
Evan Chengd38c22b2006-05-11 23:55:42 +000070 : ScheduleDAG(dag, bb, tm),
Chris Lattner356183d2006-03-11 22:44:37 +000071 AvailableQueue(availqueue), HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +000072 }
Evan Chengab495562006-01-25 09:14:32 +000073
74 ~ScheduleDAGList() {
Chris Lattner543832d2006-03-08 04:25:59 +000075 delete HazardRec;
Chris Lattner356183d2006-03-11 22:44:37 +000076 delete AvailableQueue;
Evan Chengab495562006-01-25 09:14:32 +000077 }
Evan Cheng31272342006-01-23 08:26:10 +000078
79 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +000080
Evan Chengab495562006-01-25 09:14:32 +000081private:
Chris Lattner572003c2006-03-12 00:38:57 +000082 void ReleaseSucc(SUnit *SuccSU, bool isChain);
Chris Lattner063086b2006-03-11 22:34:41 +000083 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
Chris Lattner399bee22006-03-09 06:48:37 +000084 void ListScheduleTopDown();
Evan Chengab495562006-01-25 09:14:32 +000085};
Chris Lattneraf5e26c2006-03-08 04:37:58 +000086} // end anonymous namespace
Evan Chengab495562006-01-25 09:14:32 +000087
Chris Lattner47639db2006-03-06 00:22:00 +000088HazardRecognizer::~HazardRecognizer() {}
89
Evan Chengc4c339c2006-01-26 00:30:29 +000090
Chris Lattner9995a0c2006-03-11 22:28:35 +000091/// Schedule - Schedule the DAG using list scheduling.
Chris Lattner9995a0c2006-03-11 22:28:35 +000092void ScheduleDAGList::Schedule() {
Bill Wendling22e978a2006-12-07 20:04:42 +000093 DOUT << "********** List Scheduling **********\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +000094
95 // Build scheduling units.
96 BuildSchedUnits();
Evan Cheng7d693892006-05-09 07:13:34 +000097
Evan Chengfd2c5dd2006-11-04 09:44:31 +000098 AvailableQueue->initNodes(SUnitMap, SUnits);
Chris Lattner9995a0c2006-03-11 22:28:35 +000099
Evan Chengd38c22b2006-05-11 23:55:42 +0000100 ListScheduleTopDown();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000101
Chris Lattner356183d2006-03-11 22:44:37 +0000102 AvailableQueue->releaseState();
Chris Lattner9995a0c2006-03-11 22:28:35 +0000103
Bill Wendling22e978a2006-12-07 20:04:42 +0000104 DOUT << "*** Final schedule ***\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000105 DEBUG(dumpSchedule());
Bill Wendling22e978a2006-12-07 20:04:42 +0000106 DOUT << "\n";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000107
108 // Emit in scheduled order
109 EmitSchedule();
110}
111
112//===----------------------------------------------------------------------===//
Chris Lattner9995a0c2006-03-11 22:28:35 +0000113// Top-Down Scheduling
114//===----------------------------------------------------------------------===//
115
116/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
Chris Lattner572003c2006-03-12 00:38:57 +0000117/// the PendingQueue if the count reaches zero.
118void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
Chris Lattner9995a0c2006-03-11 22:28:35 +0000119 if (!isChain)
120 SuccSU->NumPredsLeft--;
121 else
122 SuccSU->NumChainPredsLeft--;
123
Chris Lattner572003c2006-03-12 00:38:57 +0000124 assert(SuccSU->NumPredsLeft >= 0 && SuccSU->NumChainPredsLeft >= 0 &&
125 "List scheduling internal error");
Chris Lattner9995a0c2006-03-11 22:28:35 +0000126
127 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) {
Chris Lattner572003c2006-03-12 00:38:57 +0000128 // Compute how many cycles it will be before this actually becomes
129 // available. This is the max of the start time of all predecessors plus
130 // their latencies.
131 unsigned AvailableCycle = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000132 for (SUnit::pred_iterator I = SuccSU->Preds.begin(),
Chris Lattner572003c2006-03-12 00:38:57 +0000133 E = SuccSU->Preds.end(); I != E; ++I) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000134 // If this is a token edge, we don't need to wait for the latency of the
135 // preceeding instruction (e.g. a long-latency load) unless there is also
136 // some other data dependence.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000137 SUnit &Pred = *I->first;
138 unsigned PredDoneCycle = Pred.Cycle;
Chris Lattner86a9b602006-03-12 03:52:09 +0000139 if (!I->second)
Chris Lattnerd86418a2006-08-17 00:09:56 +0000140 PredDoneCycle += Pred.Latency;
141 else if (Pred.Latency)
Chris Lattnera767dbf2006-03-12 09:01:41 +0000142 PredDoneCycle += 1;
Chris Lattner86a9b602006-03-12 03:52:09 +0000143
144 AvailableCycle = std::max(AvailableCycle, PredDoneCycle);
Chris Lattner572003c2006-03-12 00:38:57 +0000145 }
146
147 PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU));
Chris Lattner9995a0c2006-03-11 22:28:35 +0000148 }
149}
150
151/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
152/// count of its successors. If a successor pending count is zero, add it to
153/// the Available queue.
Chris Lattner356183d2006-03-11 22:44:37 +0000154void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000155 DOUT << "*** Scheduling [" << CurCycle << "]: ";
Chris Lattner9995a0c2006-03-11 22:28:35 +0000156 DEBUG(SU->dump(&DAG));
157
158 Sequence.push_back(SU);
Chris Lattner356183d2006-03-11 22:44:37 +0000159 SU->Cycle = CurCycle;
Chris Lattner9995a0c2006-03-11 22:28:35 +0000160
161 // Bottom up: release successors.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000162 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
163 I != E; ++I)
Chris Lattner572003c2006-03-12 00:38:57 +0000164 ReleaseSucc(I->first, I->second);
Chris Lattner9995a0c2006-03-11 22:28:35 +0000165}
166
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000167/// ListScheduleTopDown - The main loop of list scheduling for top-down
168/// schedulers.
Chris Lattner399bee22006-03-09 06:48:37 +0000169void ScheduleDAGList::ListScheduleTopDown() {
Chris Lattner572003c2006-03-12 00:38:57 +0000170 unsigned CurCycle = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000171 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner572003c2006-03-12 00:38:57 +0000172
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000173 // All leaves to Available queue.
Chris Lattner42e20262006-03-08 04:54:34 +0000174 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000175 // It is available if it has no predecessors.
Chris Lattner572003c2006-03-12 00:38:57 +0000176 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Chris Lattner356183d2006-03-11 22:44:37 +0000177 AvailableQueue->push(&SUnits[i]);
Chris Lattner572003c2006-03-12 00:38:57 +0000178 SUnits[i].isAvailable = SUnits[i].isPending = true;
179 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000180 }
181
Chris Lattner572003c2006-03-12 00:38:57 +0000182 // Emit the entry node first.
183 ScheduleNodeTopDown(Entry, CurCycle);
184 HazardRec->EmitInstruction(Entry->Node);
185
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000186 // While Available queue is not empty, grab the node with the highest
187 // priority. If it is not ready put it back. Schedule the node.
188 std::vector<SUnit*> NotReady;
Chris Lattner572003c2006-03-12 00:38:57 +0000189 while (!AvailableQueue->empty() || !PendingQueue.empty()) {
190 // Check to see if any of the pending instructions are ready to issue. If
191 // so, add them to the available queue.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000192 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Chris Lattner572003c2006-03-12 00:38:57 +0000193 if (PendingQueue[i].first == CurCycle) {
194 AvailableQueue->push(PendingQueue[i].second);
195 PendingQueue[i].second->isAvailable = true;
196 PendingQueue[i] = PendingQueue.back();
197 PendingQueue.pop_back();
198 --i; --e;
199 } else {
200 assert(PendingQueue[i].first > CurCycle && "Negative latency?");
201 }
Chris Lattnera767dbf2006-03-12 09:01:41 +0000202 }
Chris Lattner572003c2006-03-12 00:38:57 +0000203
Chris Lattnera767dbf2006-03-12 09:01:41 +0000204 // If there are no instructions available, don't try to issue anything, and
205 // don't advance the hazard recognizer.
206 if (AvailableQueue->empty()) {
207 ++CurCycle;
208 continue;
209 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000210
Chris Lattnera767dbf2006-03-12 09:01:41 +0000211 SUnit *FoundSUnit = 0;
212 SDNode *FoundNode = 0;
213
Chris Lattnere50c0922006-03-05 22:45:01 +0000214 bool HasNoopHazards = false;
Chris Lattner572003c2006-03-12 00:38:57 +0000215 while (!AvailableQueue->empty()) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000216 SUnit *CurSUnit = AvailableQueue->pop();
Chris Lattner0c801bd2006-03-07 05:40:43 +0000217
218 // Get the node represented by this SUnit.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000219 FoundNode = CurSUnit->Node;
220
Chris Lattner0c801bd2006-03-07 05:40:43 +0000221 // If this is a pseudo op, like copyfromreg, look to see if there is a
222 // real target node flagged to it. If so, use the target node.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000223 for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size();
224 FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
225 FoundNode = CurSUnit->FlaggedNodes[i];
Chris Lattner0c801bd2006-03-07 05:40:43 +0000226
Chris Lattnera767dbf2006-03-12 09:01:41 +0000227 HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode);
Chris Lattnere50c0922006-03-05 22:45:01 +0000228 if (HT == HazardRecognizer::NoHazard) {
Chris Lattnera767dbf2006-03-12 09:01:41 +0000229 FoundSUnit = CurSUnit;
Chris Lattnere50c0922006-03-05 22:45:01 +0000230 break;
231 }
232
233 // Remember if this is a noop hazard.
234 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
235
Chris Lattnera767dbf2006-03-12 09:01:41 +0000236 NotReady.push_back(CurSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000237 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000238
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000239 // Add the nodes that aren't ready back onto the available list.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000240 if (!NotReady.empty()) {
241 AvailableQueue->push_all(NotReady);
242 NotReady.clear();
243 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000244
245 // If we found a node to schedule, do it now.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000246 if (FoundSUnit) {
247 ScheduleNodeTopDown(FoundSUnit, CurCycle);
248 HazardRec->EmitInstruction(FoundNode);
249 FoundSUnit->isScheduled = true;
250 AvailableQueue->ScheduledNode(FoundSUnit);
Chris Lattner572003c2006-03-12 00:38:57 +0000251
252 // If this is a pseudo-op node, we don't want to increment the current
253 // cycle.
Chris Lattnera767dbf2006-03-12 09:01:41 +0000254 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
255 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000256 } else if (!HasNoopHazards) {
257 // Otherwise, we have a pipeline stall, but no other problem, just advance
258 // the current cycle and try again.
Bill Wendling22e978a2006-12-07 20:04:42 +0000259 DOUT << "*** Advancing cycle, no work to do\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000260 HazardRec->AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000261 ++NumStalls;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000262 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000263 } else {
264 // Otherwise, we have no instructions to issue and we have instructions
265 // that will fault if we don't do this right. This is the case for
266 // processors without pipeline interlocks and other cases.
Bill Wendling22e978a2006-12-07 20:04:42 +0000267 DOUT << "*** Emitting noop\n";
Chris Lattner543832d2006-03-08 04:25:59 +0000268 HazardRec->EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000269 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000270 ++NumNoops;
Chris Lattnera767dbf2006-03-12 09:01:41 +0000271 ++CurCycle;
Chris Lattnere50c0922006-03-05 22:45:01 +0000272 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000273 }
274
275#ifndef NDEBUG
276 // Verify that all SUnits were scheduled.
277 bool AnyNotSched = false;
Chris Lattner42e20262006-03-08 04:54:34 +0000278 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
279 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000280 if (!AnyNotSched)
Bill Wendling22e978a2006-12-07 20:04:42 +0000281 cerr << "*** List scheduling failed! ***\n";
Chris Lattner42e20262006-03-08 04:54:34 +0000282 SUnits[i].dump(&DAG);
Bill Wendling22e978a2006-12-07 20:04:42 +0000283 cerr << "has not been scheduled!\n";
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000284 AnyNotSched = true;
285 }
286 }
287 assert(!AnyNotSched);
288#endif
289}
290
Chris Lattner9df64752006-03-09 06:35:14 +0000291//===----------------------------------------------------------------------===//
Chris Lattner6398c132006-03-09 07:38:27 +0000292// LatencyPriorityQueue Implementation
293//===----------------------------------------------------------------------===//
294//
295// This is a SchedulingPriorityQueue that schedules using latency information to
296// reduce the length of the critical path through the basic block.
297//
298namespace {
299 class LatencyPriorityQueue;
300
301 /// Sorting functions for the Available queue.
302 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
303 LatencyPriorityQueue *PQ;
304 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
305 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
306
307 bool operator()(const SUnit* left, const SUnit* right) const;
308 };
309} // end anonymous namespace
310
311namespace {
312 class LatencyPriorityQueue : public SchedulingPriorityQueue {
313 // SUnits - The SUnits for the current graph.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000314 std::vector<SUnit> *SUnits;
Chris Lattner6398c132006-03-09 07:38:27 +0000315
316 // Latencies - The latency (max of latency from this node to the bb exit)
317 // for each node.
318 std::vector<int> Latencies;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000319
320 /// NumNodesSolelyBlocking - This vector contains, for every node in the
321 /// Queue, the number of nodes that the node is the sole unscheduled
322 /// predecessor for. This is used as a tie-breaker heuristic for better
323 /// mobility.
324 std::vector<unsigned> NumNodesSolelyBlocking;
325
Chris Lattner6398c132006-03-09 07:38:27 +0000326 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
327public:
328 LatencyPriorityQueue() : Queue(latency_sort(this)) {
329 }
330
Evan Chengfd2c5dd2006-11-04 09:44:31 +0000331 void initNodes(std::map<SDNode*, SUnit*> &sumap,
332 std::vector<SUnit> &sunits) {
Chris Lattner6398c132006-03-09 07:38:27 +0000333 SUnits = &sunits;
334 // Calculate node priorities.
335 CalculatePriorities();
336 }
337 void releaseState() {
338 SUnits = 0;
339 Latencies.clear();
340 }
341
342 unsigned getLatency(unsigned NodeNum) const {
343 assert(NodeNum < Latencies.size());
344 return Latencies[NodeNum];
345 }
346
Chris Lattner349e9dd2006-03-10 05:51:05 +0000347 unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {
348 assert(NodeNum < NumNodesSolelyBlocking.size());
349 return NumNodesSolelyBlocking[NodeNum];
350 }
351
Chris Lattner6398c132006-03-09 07:38:27 +0000352 bool empty() const { return Queue.empty(); }
353
Chris Lattner349e9dd2006-03-10 05:51:05 +0000354 virtual void push(SUnit *U) {
355 push_impl(U);
Chris Lattner6398c132006-03-09 07:38:27 +0000356 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000357 void push_impl(SUnit *U);
358
Chris Lattner25e25562006-03-10 04:32:49 +0000359 void push_all(const std::vector<SUnit *> &Nodes) {
360 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000361 push_impl(Nodes[i]);
Chris Lattner25e25562006-03-10 04:32:49 +0000362 }
363
Chris Lattner6398c132006-03-09 07:38:27 +0000364 SUnit *pop() {
Evan Cheng61e9f0d2006-05-30 18:04:34 +0000365 if (empty()) return NULL;
Chris Lattner6398c132006-03-09 07:38:27 +0000366 SUnit *V = Queue.top();
367 Queue.pop();
Chris Lattner6398c132006-03-09 07:38:27 +0000368 return V;
369 }
Evan Cheng7d693892006-05-09 07:13:34 +0000370
Evan Chengd38c22b2006-05-11 23:55:42 +0000371 // ScheduledNode - As nodes are scheduled, we look to see if there are any
372 // successor nodes that have a single unscheduled predecessor. If so, that
373 // single predecessor has a higher priority, since scheduling it will make
374 // the node available.
375 void ScheduledNode(SUnit *Node);
376
377private:
378 void CalculatePriorities();
379 int CalcLatency(const SUnit &SU);
380 void AdjustPriorityOfUnscheduledPreds(SUnit *SU);
Chris Lattnerd86418a2006-08-17 00:09:56 +0000381 SUnit *getSingleUnscheduledPred(SUnit *SU);
Evan Chengd38c22b2006-05-11 23:55:42 +0000382
Chris Lattner349e9dd2006-03-10 05:51:05 +0000383 /// RemoveFromPriorityQueue - This is a really inefficient way to remove a
384 /// node from a priority queue. We should roll our own heap to make this
385 /// better or something.
386 void RemoveFromPriorityQueue(SUnit *SU) {
387 std::vector<SUnit*> Temp;
388
389 assert(!Queue.empty() && "Not in queue!");
390 while (Queue.top() != SU) {
391 Temp.push_back(Queue.top());
392 Queue.pop();
393 assert(!Queue.empty() && "Not in queue!");
394 }
395
396 // Remove the node from the PQ.
397 Queue.pop();
398
399 // Add all the other nodes back.
400 for (unsigned i = 0, e = Temp.size(); i != e; ++i)
401 Queue.push(Temp[i]);
402 }
Chris Lattner6398c132006-03-09 07:38:27 +0000403 };
404}
405
406bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
407 unsigned LHSNum = LHS->NodeNum;
408 unsigned RHSNum = RHS->NodeNum;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000409
410 // The most important heuristic is scheduling the critical path.
411 unsigned LHSLatency = PQ->getLatency(LHSNum);
412 unsigned RHSLatency = PQ->getLatency(RHSNum);
413 if (LHSLatency < RHSLatency) return true;
414 if (LHSLatency > RHSLatency) return false;
Chris Lattner6398c132006-03-09 07:38:27 +0000415
Chris Lattner349e9dd2006-03-10 05:51:05 +0000416 // After that, if two nodes have identical latencies, look to see if one will
417 // unblock more other nodes than the other.
418 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
419 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
420 if (LHSBlocked < RHSBlocked) return true;
421 if (LHSBlocked > RHSBlocked) return false;
422
423 // Finally, just to provide a stable ordering, use the node number as a
424 // deciding factor.
425 return LHSNum < RHSNum;
Chris Lattner6398c132006-03-09 07:38:27 +0000426}
427
428
429/// CalcNodePriority - Calculate the maximal path from the node to the exit.
430///
431int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
432 int &Latency = Latencies[SU.NodeNum];
433 if (Latency != -1)
434 return Latency;
435
436 int MaxSuccLatency = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000437 for (SUnit::const_succ_iterator I = SU.Succs.begin(), E = SU.Succs.end();
438 I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000439 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(*I->first));
Chris Lattner6398c132006-03-09 07:38:27 +0000440
441 return Latency = MaxSuccLatency + SU.Latency;
442}
443
444/// CalculatePriorities - Calculate priorities of all scheduling units.
445void LatencyPriorityQueue::CalculatePriorities() {
446 Latencies.assign(SUnits->size(), -1);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000447 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
Chris Lattner6398c132006-03-09 07:38:27 +0000448
449 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
450 CalcLatency((*SUnits)[i]);
451}
452
Chris Lattner349e9dd2006-03-10 05:51:05 +0000453/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
454/// of SU, return it, otherwise return null.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000455SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000456 SUnit *OnlyAvailablePred = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000457 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
458 I != E; ++I) {
459 SUnit &Pred = *I->first;
460 if (!Pred.isScheduled) {
Chris Lattner349e9dd2006-03-10 05:51:05 +0000461 // We found an available, but not scheduled, predecessor. If it's the
462 // only one we have found, keep track of it... otherwise give up.
Chris Lattnerd86418a2006-08-17 00:09:56 +0000463 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000464 return 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000465 OnlyAvailablePred = &Pred;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000466 }
Chris Lattnerd86418a2006-08-17 00:09:56 +0000467 }
Chris Lattner349e9dd2006-03-10 05:51:05 +0000468
469 return OnlyAvailablePred;
470}
471
472void LatencyPriorityQueue::push_impl(SUnit *SU) {
473 // Look at all of the successors of this node. Count the number of nodes that
474 // this node is the sole unscheduled node for.
475 unsigned NumNodesBlocking = 0;
Chris Lattnerd86418a2006-08-17 00:09:56 +0000476 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
477 I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000478 if (getSingleUnscheduledPred(I->first) == SU)
Chris Lattner349e9dd2006-03-10 05:51:05 +0000479 ++NumNodesBlocking;
Chris Lattner578d8fc2006-03-11 22:24:20 +0000480 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Chris Lattner349e9dd2006-03-10 05:51:05 +0000481
482 Queue.push(SU);
483}
484
485
486// ScheduledNode - As nodes are scheduled, we look to see if there are any
487// successor nodes that have a single unscheduled predecessor. If so, that
488// single predecessor has a higher priority, since scheduling it will make
489// the node available.
490void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
Chris Lattnerd86418a2006-08-17 00:09:56 +0000491 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
492 I != E; ++I)
Chris Lattner578d8fc2006-03-11 22:24:20 +0000493 AdjustPriorityOfUnscheduledPreds(I->first);
Chris Lattner349e9dd2006-03-10 05:51:05 +0000494}
495
496/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
497/// scheduled. If SU is not itself available, then there is at least one
498/// predecessor node that has not been scheduled yet. If SU has exactly ONE
499/// unscheduled predecessor, we want to increase its priority: it getting
500/// scheduled will make this node available, so it is better than some other
501/// node of the same priority that will not make a node available.
502void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Chris Lattner572003c2006-03-12 00:38:57 +0000503 if (SU->isPending) return; // All preds scheduled.
Chris Lattner349e9dd2006-03-10 05:51:05 +0000504
505 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
506 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
507
508 // Okay, we found a single predecessor that is available, but not scheduled.
509 // Since it is available, it must be in the priority queue. First remove it.
510 RemoveFromPriorityQueue(OnlyAvailablePred);
511
512 // Reinsert the node into the priority queue, which recomputes its
513 // NumNodesSolelyBlocking value.
514 push(OnlyAvailablePred);
515}
516
Chris Lattner9df64752006-03-09 06:35:14 +0000517
518//===----------------------------------------------------------------------===//
519// Public Constructor Functions
520//===----------------------------------------------------------------------===//
521
Jim Laskey95eda5b2006-08-01 14:21:23 +0000522/// createTDListDAGScheduler - This creates a top-down list scheduler with a
523/// new hazard recognizer. This scheduler takes ownership of the hazard
524/// recognizer and deletes it when done.
Jim Laskey03593f72006-08-01 18:29:48 +0000525ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS,
526 SelectionDAG *DAG,
Jim Laskey95eda5b2006-08-01 14:21:23 +0000527 MachineBasicBlock *BB) {
528 return new ScheduleDAGList(*DAG, BB, DAG->getTarget(),
Chris Lattner6398c132006-03-09 07:38:27 +0000529 new LatencyPriorityQueue(),
Jim Laskey03593f72006-08-01 18:29:48 +0000530 IS->CreateTargetHazardRecognizer());
Evan Cheng31272342006-01-23 08:26:10 +0000531}