blob: 703e4626d8d3dbfdca29d4babdd7c9387621edbd [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//
10// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "sched"
17#include "llvm/CodeGen/ScheduleDAG.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
Evan Chengab495562006-01-25 09:14:32 +000021#include "llvm/Support/Debug.h"
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000022#include "llvm/ADT/Statistic.h"
Evan Chengab495562006-01-25 09:14:32 +000023#include <climits>
24#include <iostream>
Evan Cheng31272342006-01-23 08:26:10 +000025#include <queue>
Evan Cheng4e3904f2006-03-02 21:38:29 +000026#include <set>
27#include <vector>
Evan Cheng31272342006-01-23 08:26:10 +000028using namespace llvm;
29
Evan Chengab495562006-01-25 09:14:32 +000030namespace {
Chris Lattnerfa5e1c92006-03-05 23:13:56 +000031 Statistic<> NumNoops ("scheduler", "Number of noops inserted");
32 Statistic<> NumStalls("scheduler", "Number of pipeline stalls");
Evan Cheng31272342006-01-23 08:26:10 +000033
Evan Chengab495562006-01-25 09:14:32 +000034/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or a
35/// group of nodes flagged together.
36struct SUnit {
37 SDNode *Node; // Representative node.
38 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
Evan Cheng4e3904f2006-03-02 21:38:29 +000039 std::set<SUnit*> Preds; // All real predecessors.
40 std::set<SUnit*> ChainPreds; // All chain predecessors.
41 std::set<SUnit*> Succs; // All real successors.
42 std::set<SUnit*> ChainSuccs; // All chain successors.
Evan Chengab495562006-01-25 09:14:32 +000043 int NumPredsLeft; // # of preds not scheduled.
44 int NumSuccsLeft; // # of succs not scheduled.
Evan Cheng4e3904f2006-03-02 21:38:29 +000045 int NumChainPredsLeft; // # of chain preds not scheduled.
46 int NumChainSuccsLeft; // # of chain succs not scheduled.
Evan Chengab495562006-01-25 09:14:32 +000047 int Priority1; // Scheduling priority 1.
48 int Priority2; // Scheduling priority 2.
Evan Cheng5e9a6952006-03-03 06:23:43 +000049 bool isTwoAddress; // Is a two-address instruction.
Evan Cheng4e3904f2006-03-02 21:38:29 +000050 bool isDefNUseOperand; // Is a def&use operand.
Evan Chengab495562006-01-25 09:14:32 +000051 unsigned Latency; // Node latency.
52 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
53 unsigned Slot; // Cycle node is scheduled at.
Evan Chengc4c339c2006-01-26 00:30:29 +000054 SUnit *Next;
Evan Chengab495562006-01-25 09:14:32 +000055
56 SUnit(SDNode *node)
57 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
Evan Cheng4e3904f2006-03-02 21:38:29 +000058 NumChainPredsLeft(0), NumChainSuccsLeft(0),
Evan Cheng5e9a6952006-03-03 06:23:43 +000059 Priority1(INT_MIN), Priority2(INT_MIN),
60 isTwoAddress(false), isDefNUseOperand(false),
Evan Cheng4e3904f2006-03-02 21:38:29 +000061 Latency(0), CycleBound(0), Slot(0), Next(NULL) {}
Evan Chengab495562006-01-25 09:14:32 +000062
63 void dump(const SelectionDAG *G, bool All=true) const;
64};
65
66void SUnit::dump(const SelectionDAG *G, bool All) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000067 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000068 Node->dump(G);
69 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000070 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000071 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000072 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000073 FlaggedNodes[i]->dump(G);
74 std::cerr << "\n";
75 }
76 }
77
78 if (All) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000079 std::cerr << " # preds left : " << NumPredsLeft << "\n";
80 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
81 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
82 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
83 std::cerr << " Latency : " << Latency << "\n";
84 std::cerr << " Priority : " << Priority1 << " , "
85 << Priority2 << "\n";
Evan Chengc4c339c2006-01-26 00:30:29 +000086
Evan Chengab495562006-01-25 09:14:32 +000087 if (Preds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000088 std::cerr << " Predecessors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000089 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000090 E = Preds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000091 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000092 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000093 }
94 }
95 if (ChainPreds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000096 std::cerr << " Chained Preds:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000097 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000098 E = ChainPreds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000099 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000100 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000101 }
102 }
103 if (Succs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000104 std::cerr << " Successors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000105 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000106 E = Succs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000107 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000108 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000109 }
110 }
111 if (ChainSuccs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000112 std::cerr << " Chained succs:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000113 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000114 E = ChainSuccs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000115 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000116 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000117 }
118 }
119 }
120}
121
122/// Sorting functions for the Available queue.
123struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
124 bool operator()(const SUnit* left, const SUnit* right) const {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000125 bool LFloater = (left ->Preds.size() == 0);
126 bool RFloater = (right->Preds.size() == 0);
127 int LBonus = (int)left ->isDefNUseOperand;
128 int RBonus = (int)right->isDefNUseOperand;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000129
130 // Special tie breaker: if two nodes share a operand, the one that
131 // use it as a def&use operand is preferred.
132 if (left->isTwoAddress && !right->isTwoAddress) {
133 SDNode *DUNode = left->Node->getOperand(0).Val;
134 if (DUNode->isOperand(right->Node))
135 LBonus++;
136 }
137 if (!left->isTwoAddress && right->isTwoAddress) {
138 SDNode *DUNode = right->Node->getOperand(0).Val;
139 if (DUNode->isOperand(left->Node))
140 RBonus++;
141 }
142
Evan Cheng4e3904f2006-03-02 21:38:29 +0000143 int LPriority1 = left ->Priority1 - LBonus;
144 int RPriority1 = right->Priority1 - RBonus;
145 int LPriority2 = left ->Priority2 + LBonus;
146 int RPriority2 = right->Priority2 + RBonus;
147
148 // Favor floaters (i.e. node with no non-passive predecessors):
149 // e.g. MOV32ri.
150 if (!LFloater && RFloater)
Evan Chengab495562006-01-25 09:14:32 +0000151 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000152 else if (LFloater == RFloater)
153 if (LPriority1 > RPriority1)
Evan Chengab495562006-01-25 09:14:32 +0000154 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000155 else if (LPriority1 == RPriority1)
156 if (LPriority2 < RPriority2)
Evan Chengab495562006-01-25 09:14:32 +0000157 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000158 else if (LPriority1 == RPriority1)
Evan Chengab495562006-01-25 09:14:32 +0000159 if (left->CycleBound > right->CycleBound)
160 return true;
Evan Chengab495562006-01-25 09:14:32 +0000161
162 return false;
Evan Cheng31272342006-01-23 08:26:10 +0000163 }
164};
165
Chris Lattnere50c0922006-03-05 22:45:01 +0000166
Evan Cheng31272342006-01-23 08:26:10 +0000167/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000168class ScheduleDAGList : public ScheduleDAG {
169private:
Evan Chengab495562006-01-25 09:14:32 +0000170 // SDNode to SUnit mapping (many to one).
171 std::map<SDNode*, SUnit*> SUnitMap;
Chris Lattner00b52ea2006-03-05 23:59:20 +0000172 // The schedule. Null SUnit*'s represent noop instructions.
Evan Chengab495562006-01-25 09:14:32 +0000173 std::vector<SUnit*> Sequence;
174 // Current scheduling cycle.
175 unsigned CurrCycle;
Evan Chengc4c339c2006-01-26 00:30:29 +0000176 // First and last SUnit created.
177 SUnit *HeadSUnit, *TailSUnit;
Evan Cheng31272342006-01-23 08:26:10 +0000178
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000179 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
180 /// it is top-down.
181 bool isBottomUp;
182
Chris Lattnere50c0922006-03-05 22:45:01 +0000183 /// HazardRec - The hazard recognizer to use.
Chris Lattner47639db2006-03-06 00:22:00 +0000184 HazardRecognizer &HazardRec;
Chris Lattnere50c0922006-03-05 22:45:01 +0000185
Chris Lattner7a36d972006-03-05 20:21:55 +0000186 typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort>
187 AvailableQueueTy;
188
Evan Cheng31272342006-01-23 08:26:10 +0000189public:
190 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000191 const TargetMachine &tm, bool isbottomup,
Chris Lattner47639db2006-03-06 00:22:00 +0000192 HazardRecognizer &HR)
Evan Chengc4c339c2006-01-26 00:30:29 +0000193 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner47639db2006-03-06 00:22:00 +0000194 CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL), isBottomUp(isbottomup),
195 HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000196 }
Evan Chengab495562006-01-25 09:14:32 +0000197
198 ~ScheduleDAGList() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000199 SUnit *SU = HeadSUnit;
200 while (SU) {
201 SUnit *NextSU = SU->Next;
202 delete SU;
203 SU = NextSU;
Evan Chengab495562006-01-25 09:14:32 +0000204 }
205 }
Evan Cheng31272342006-01-23 08:26:10 +0000206
207 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000208
Evan Chengab495562006-01-25 09:14:32 +0000209 void dump() const;
210
211private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000212 SUnit *NewSUnit(SDNode *N);
Chris Lattner7a36d972006-03-05 20:21:55 +0000213 void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000214 void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false);
215 void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU);
216 void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU);
Evan Chengab495562006-01-25 09:14:32 +0000217 int CalcNodePriority(SUnit *SU);
218 void CalculatePriorities();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000219 void ListScheduleTopDown();
220 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000221 void BuildSchedUnits();
222 void EmitSchedule();
223};
224} // end namespace
225
Chris Lattner47639db2006-03-06 00:22:00 +0000226HazardRecognizer::~HazardRecognizer() {}
227
Evan Chengc4c339c2006-01-26 00:30:29 +0000228
229/// NewSUnit - Creates a new SUnit and return a ptr to it.
230SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
231 SUnit *CurrSUnit = new SUnit(N);
232
233 if (HeadSUnit == NULL)
234 HeadSUnit = CurrSUnit;
235 if (TailSUnit != NULL)
236 TailSUnit->Next = CurrSUnit;
237 TailSUnit = CurrSUnit;
238
239 return CurrSUnit;
240}
241
242/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
243/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner7a36d972006-03-05 20:21:55 +0000244void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available,
245 SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000246 // FIXME: the distance between two nodes is not always == the predecessor's
247 // latency. For example, the reader can very well read the register written
248 // by the predecessor later than the issue cycle. It also depends on the
249 // interrupt model (drain vs. freeze).
250 PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency);
251
252 if (!isChain) {
253 PredSU->NumSuccsLeft--;
254 PredSU->Priority1++;
255 } else
256 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000257
Evan Chengab495562006-01-25 09:14:32 +0000258#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000259 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000260 std::cerr << "*** List scheduling failed! ***\n";
261 PredSU->dump(&DAG);
262 std::cerr << " has been released too many times!\n";
263 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000264 }
Evan Chengab495562006-01-25 09:14:32 +0000265#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000266
267 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
268 // EntryToken has to go last! Special case it here.
269 if (PredSU->Node->getOpcode() != ISD::EntryToken)
270 Available.push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000271 }
Evan Chengab495562006-01-25 09:14:32 +0000272}
273
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000274/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
275/// the Available queue is the count reaches zero. Also update its cycle bound.
276void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available,
277 SUnit *SuccSU, bool isChain) {
278 // FIXME: the distance between two nodes is not always == the predecessor's
279 // latency. For example, the reader can very well read the register written
280 // by the predecessor later than the issue cycle. It also depends on the
281 // interrupt model (drain vs. freeze).
282 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurrCycle + SuccSU->Latency);
283
284 if (!isChain) {
285 SuccSU->NumPredsLeft--;
286 SuccSU->Priority1++; // FIXME: ??
287 } else
288 SuccSU->NumChainPredsLeft--;
289
290#ifndef NDEBUG
291 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
292 std::cerr << "*** List scheduling failed! ***\n";
293 SuccSU->dump(&DAG);
294 std::cerr << " has been released too many times!\n";
295 abort();
296 }
297#endif
298
299 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
300 Available.push(SuccSU);
301}
302
303/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
304/// count of its predecessors. If a predecessor pending count is zero, add it to
305/// the Available queue.
306void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available,
307 SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000308 DEBUG(std::cerr << "*** Scheduling: ");
309 DEBUG(SU->dump(&DAG, false));
310
Evan Chengab495562006-01-25 09:14:32 +0000311 Sequence.push_back(SU);
312 SU->Slot = CurrCycle;
313
314 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000315 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
316 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000317 ReleasePred(Available, *I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000318 SU->NumPredsLeft--;
319 SU->Priority1--;
320 }
321 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
322 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner7a36d972006-03-05 20:21:55 +0000323 ReleasePred(Available, *I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000324
325 CurrCycle++;
326}
327
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000328/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
329/// count of its successors. If a successor pending count is zero, add it to
330/// the Available queue.
331void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available,
332 SUnit *SU) {
333 DEBUG(std::cerr << "*** Scheduling: ");
334 DEBUG(SU->dump(&DAG, false));
335
336 Sequence.push_back(SU);
337 SU->Slot = CurrCycle;
338
339 // Bottom up: release successors.
340 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
341 E1 = SU->Succs.end(); I1 != E1; ++I1) {
342 ReleaseSucc(Available, *I1);
343 SU->NumSuccsLeft--;
344 SU->Priority1--; // FIXME: what is this??
345 }
346 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
347 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
348 ReleaseSucc(Available, *I2, true);
349
350 CurrCycle++;
351}
352
Evan Chengab495562006-01-25 09:14:32 +0000353/// isReady - True if node's lower cycle bound is less or equal to the current
354/// scheduling cycle. Always true if all nodes have uniform latency 1.
355static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
356 return SU->CycleBound <= CurrCycle;
357}
358
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000359/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
360/// schedulers.
361void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000362 // Available queue.
363 AvailableQueueTy Available;
364
365 // Add root to Available queue.
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000366 Available.push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000367
368 // While Available queue is not empty, grab the node with the highest
369 // priority. If it is not ready put it back. Schedule the node.
370 std::vector<SUnit*> NotReady;
371 while (!Available.empty()) {
372 SUnit *CurrNode = Available.top();
373 Available.pop();
374
Evan Chengab495562006-01-25 09:14:32 +0000375 while (!isReady(CurrNode, CurrCycle)) {
376 NotReady.push_back(CurrNode);
377 CurrNode = Available.top();
378 Available.pop();
379 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000380
381 // Add the nodes that aren't ready back onto the available list.
382 while (!NotReady.empty()) {
383 Available.push(NotReady.back());
384 NotReady.pop_back();
385 }
Evan Chengab495562006-01-25 09:14:32 +0000386
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000387 ScheduleNodeBottomUp(Available, CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000388 }
389
390 // Add entry node last
391 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
392 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
393 Entry->Slot = CurrCycle;
394 Sequence.push_back(Entry);
395 }
396
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000397 // Reverse the order if it is bottom up.
398 std::reverse(Sequence.begin(), Sequence.end());
399
400
Evan Chengab495562006-01-25 09:14:32 +0000401#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000402 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000403 bool AnyNotSched = false;
404 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000405 if (SU->NumSuccsLeft != 0 || SU->NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000406 if (!AnyNotSched)
407 std::cerr << "*** List scheduling failed! ***\n";
Evan Chengab495562006-01-25 09:14:32 +0000408 SU->dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000409 std::cerr << "has not been scheduled!\n";
410 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000411 }
Evan Chengab495562006-01-25 09:14:32 +0000412 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000413 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000414#endif
Evan Chengab495562006-01-25 09:14:32 +0000415}
416
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000417/// ListScheduleTopDown - The main loop of list scheduling for top-down
418/// schedulers.
419void ScheduleDAGList::ListScheduleTopDown() {
420 // Available queue.
421 AvailableQueueTy Available;
Chris Lattner47639db2006-03-06 00:22:00 +0000422
423 HazardRec.StartBasicBlock();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000424
425 // Emit the entry node first.
426 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
427 ScheduleNodeTopDown(Available, Entry);
Chris Lattner47639db2006-03-06 00:22:00 +0000428 HazardRec.EmitInstruction(Entry->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000429
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000430 // All leaves to Available queue.
431 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
432 // It is available if it has no predecessors.
433 if ((SU->Preds.size() + SU->ChainPreds.size()) == 0 && SU != Entry)
434 Available.push(SU);
435 }
436
437 // While Available queue is not empty, grab the node with the highest
438 // priority. If it is not ready put it back. Schedule the node.
439 std::vector<SUnit*> NotReady;
440 while (!Available.empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000441 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000442
Chris Lattnere50c0922006-03-05 22:45:01 +0000443 bool HasNoopHazards = false;
444 do {
445 SUnit *CurrNode = Available.top();
446 Available.pop();
447 HazardRecognizer::HazardType HT =
Chris Lattner47639db2006-03-06 00:22:00 +0000448 HazardRec.getHazardType(CurrNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000449 if (HT == HazardRecognizer::NoHazard) {
450 FoundNode = CurrNode;
451 break;
452 }
453
454 // Remember if this is a noop hazard.
455 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
456
457 NotReady.push_back(CurrNode);
458 } while (!Available.empty());
459
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000460 // Add the nodes that aren't ready back onto the available list.
461 while (!NotReady.empty()) {
462 Available.push(NotReady.back());
463 NotReady.pop_back();
464 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000465
466 // If we found a node to schedule, do it now.
467 if (FoundNode) {
468 ScheduleNodeTopDown(Available, FoundNode);
Chris Lattner47639db2006-03-06 00:22:00 +0000469 HazardRec.EmitInstruction(FoundNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000470 } else if (!HasNoopHazards) {
471 // Otherwise, we have a pipeline stall, but no other problem, just advance
472 // the current cycle and try again.
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000473 DEBUG(std::cerr << "*** Advancing cycle, no work to do");
Chris Lattner47639db2006-03-06 00:22:00 +0000474 HazardRec.AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000475 ++NumStalls;
Chris Lattnere50c0922006-03-05 22:45:01 +0000476 } else {
477 // Otherwise, we have no instructions to issue and we have instructions
478 // that will fault if we don't do this right. This is the case for
479 // processors without pipeline interlocks and other cases.
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000480 DEBUG(std::cerr << "*** Emitting noop");
Chris Lattner47639db2006-03-06 00:22:00 +0000481 HazardRec.EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000482 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000483 ++NumNoops;
Chris Lattnere50c0922006-03-05 22:45:01 +0000484 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000485 }
486
487#ifndef NDEBUG
488 // Verify that all SUnits were scheduled.
489 bool AnyNotSched = false;
490 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
491 if (SU->NumPredsLeft != 0 || SU->NumChainPredsLeft != 0) {
492 if (!AnyNotSched)
493 std::cerr << "*** List scheduling failed! ***\n";
494 SU->dump(&DAG);
495 std::cerr << "has not been scheduled!\n";
496 AnyNotSched = true;
497 }
498 }
499 assert(!AnyNotSched);
500#endif
501}
502
503
Evan Cheng4e3904f2006-03-02 21:38:29 +0000504/// CalcNodePriority - Priority1 is just the number of live range genned -
505/// number of live range killed. Priority2 is the Sethi Ullman number. It
506/// returns Priority2 since it is calculated recursively.
507/// Smaller number is the higher priority for Priority2. Reverse is true for
508/// Priority1.
Evan Chengab495562006-01-25 09:14:32 +0000509int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
510 if (SU->Priority2 != INT_MIN)
511 return SU->Priority2;
512
Evan Cheng4e3904f2006-03-02 21:38:29 +0000513 SU->Priority1 = SU->NumPredsLeft - SU->NumSuccsLeft;
Evan Chengab495562006-01-25 09:14:32 +0000514
515 if (SU->Preds.size() == 0) {
516 SU->Priority2 = 1;
517 } else {
518 int Extra = 0;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000519 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
520 E = SU->Preds.end(); I != E; ++I) {
521 SUnit *PredSU = *I;
Evan Chengab495562006-01-25 09:14:32 +0000522 int PredPriority = CalcNodePriority(PredSU);
523 if (PredPriority > SU->Priority2) {
524 SU->Priority2 = PredPriority;
525 Extra = 0;
526 } else if (PredPriority == SU->Priority2)
527 Extra++;
528 }
529
530 if (SU->Node->getOpcode() != ISD::TokenFactor)
531 SU->Priority2 += Extra;
532 else
533 SU->Priority2 = (Extra == 1) ? 0 : Extra-1;
534 }
535
536 return SU->Priority2;
537}
538
539/// CalculatePriorities - Calculate priorities of all scheduling units.
540void ScheduleDAGList::CalculatePriorities() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000541 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000542 // FIXME: assumes uniform latency for now.
543 SU->Latency = 1;
544 (void)CalcNodePriority(SU);
Evan Chengc4c339c2006-01-26 00:30:29 +0000545 DEBUG(SU->dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000546 DEBUG(std::cerr << "\n");
547 }
548}
549
Evan Chengab495562006-01-25 09:14:32 +0000550void ScheduleDAGList::BuildSchedUnits() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000551 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000552 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000553 NodeInfo *NI = &Info[i];
554 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000555 if (isPassiveNode(N))
556 continue;
Evan Chengab495562006-01-25 09:14:32 +0000557
Evan Chengc4c339c2006-01-26 00:30:29 +0000558 SUnit *SU;
559 if (NI->isInGroup()) {
560 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
561 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000562
Evan Chengc4c339c2006-01-26 00:30:29 +0000563 SU = NewSUnit(N);
564 // Find the flagged nodes.
565 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
566 SDNode *Flag = FlagOp.Val;
567 unsigned ResNo = FlagOp.ResNo;
568 while (Flag->getValueType(ResNo) == MVT::Flag) {
569 NodeInfo *FNI = getNI(Flag);
570 assert(FNI->Group == NI->Group);
571 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
572 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000573
Evan Chengc4c339c2006-01-26 00:30:29 +0000574 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
575 Flag = FlagOp.Val;
576 ResNo = FlagOp.ResNo;
577 }
578 } else {
579 SU = NewSUnit(N);
580 }
581 SUnitMap[N] = SU;
582 }
Evan Chengab495562006-01-25 09:14:32 +0000583
Evan Chengc4c339c2006-01-26 00:30:29 +0000584 // Pass 2: add the preds, succs, etc.
585 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
586 SDNode *N = SU->Node;
587 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000588
589 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
590 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000591
592 if (NI->isInGroup()) {
593 // Find all predecessors (of the group).
594 NodeGroupOpIterator NGOI(NI);
595 while (!NGOI.isEnd()) {
596 SDOperand Op = NGOI.next();
597 SDNode *OpN = Op.Val;
598 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
599 NodeInfo *OpNI = getNI(OpN);
600 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
601 assert(VT != MVT::Flag);
602 SUnit *OpSU = SUnitMap[OpN];
603 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000604 if (SU->ChainPreds.insert(OpSU).second)
605 SU->NumChainPredsLeft++;
606 if (OpSU->ChainSuccs.insert(SU).second)
607 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000608 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000609 if (SU->Preds.insert(OpSU).second)
610 SU->NumPredsLeft++;
611 if (OpSU->Succs.insert(SU).second)
612 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000613 }
Evan Chengab495562006-01-25 09:14:32 +0000614 }
615 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000616 } else {
617 // Find node predecessors.
618 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
619 SDOperand Op = N->getOperand(j);
620 SDNode *OpN = Op.Val;
621 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
622 if (!isPassiveNode(OpN)) {
623 assert(VT != MVT::Flag);
624 SUnit *OpSU = SUnitMap[OpN];
625 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000626 if (SU->ChainPreds.insert(OpSU).second)
627 SU->NumChainPredsLeft++;
628 if (OpSU->ChainSuccs.insert(SU).second)
629 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000630 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000631 if (SU->Preds.insert(OpSU).second)
632 SU->NumPredsLeft++;
633 if (OpSU->Succs.insert(SU).second)
634 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000635 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000636 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000637 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000638 }
639 }
Evan Chengab495562006-01-25 09:14:32 +0000640 }
641 }
Evan Chengab495562006-01-25 09:14:32 +0000642}
643
644/// EmitSchedule - Emit the machine code in scheduled order.
645void ScheduleDAGList::EmitSchedule() {
646 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000647 if (SUnit *SU = Sequence[i]) {
648 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
649 SDNode *N = SU->FlaggedNodes[j];
650 EmitNode(getNI(N));
651 }
652 EmitNode(getNI(SU->Node));
653 } else {
654 // Null SUnit* is a noop.
655 EmitNoop();
Evan Chengab495562006-01-25 09:14:32 +0000656 }
Evan Chengab495562006-01-25 09:14:32 +0000657 }
658}
659
660/// dump - dump the schedule.
661void ScheduleDAGList::dump() const {
662 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000663 if (SUnit *SU = Sequence[i])
664 SU->dump(&DAG, false);
665 else
666 std::cerr << "**** NOOP ****\n";
Evan Chengab495562006-01-25 09:14:32 +0000667 }
668}
669
670/// Schedule - Schedule the DAG using list scheduling.
671/// FIXME: Right now it only supports the burr (bottom up register reducing)
672/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000673void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000674 DEBUG(std::cerr << "********** List Scheduling **********\n");
675
676 // Build scheduling units.
677 BuildSchedUnits();
678
Chris Lattner47639db2006-03-06 00:22:00 +0000679 // Calculate node priorities.
Evan Chengab495562006-01-25 09:14:32 +0000680 CalculatePriorities();
681
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000682 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
683 if (isBottomUp)
684 ListScheduleBottomUp();
685 else
686 ListScheduleTopDown();
687
688 DEBUG(std::cerr << "*** Final schedule ***\n");
689 DEBUG(dump());
690 DEBUG(std::cerr << "\n");
691
Evan Chengab495562006-01-25 09:14:32 +0000692 // Emit in scheduled order
693 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000694}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000695
Evan Chengab495562006-01-25 09:14:32 +0000696llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
697 MachineBasicBlock *BB) {
Chris Lattner47639db2006-03-06 00:22:00 +0000698 HazardRecognizer HR;
699 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true, HR);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000700}
701
Chris Lattner47639db2006-03-06 00:22:00 +0000702/// createTDListDAGScheduler - This creates a top-down list scheduler with the
703/// specified hazard recognizer.
704ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
705 MachineBasicBlock *BB,
706 HazardRecognizer &HR) {
707 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, HR);
Evan Cheng31272342006-01-23 08:26:10 +0000708}