blob: dfaf373b727b9935cd4a4f0f77d0714d931d332e [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 Chengc5c06582006-03-06 06:08:54 +000047 int SethiUllman; // Sethi Ullman number.
Evan Cheng5e9a6952006-03-03 06:23:43 +000048 bool isTwoAddress; // Is a two-address instruction.
Evan Cheng4e3904f2006-03-02 21:38:29 +000049 bool isDefNUseOperand; // Is a def&use operand.
Evan Chengab495562006-01-25 09:14:32 +000050 unsigned Latency; // Node latency.
51 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
52 unsigned Slot; // Cycle node is scheduled at.
Evan Chengc4c339c2006-01-26 00:30:29 +000053 SUnit *Next;
Evan Chengab495562006-01-25 09:14:32 +000054
55 SUnit(SDNode *node)
56 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
Evan Cheng4e3904f2006-03-02 21:38:29 +000057 NumChainPredsLeft(0), NumChainSuccsLeft(0),
Evan Chengc5c06582006-03-06 06:08:54 +000058 SethiUllman(INT_MIN),
Evan Cheng5e9a6952006-03-03 06:23:43 +000059 isTwoAddress(false), isDefNUseOperand(false),
Evan Cheng4e3904f2006-03-02 21:38:29 +000060 Latency(0), CycleBound(0), Slot(0), Next(NULL) {}
Evan Chengab495562006-01-25 09:14:32 +000061
62 void dump(const SelectionDAG *G, bool All=true) const;
63};
64
65void SUnit::dump(const SelectionDAG *G, bool All) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000066 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000067 Node->dump(G);
68 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000069 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000070 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000071 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000072 FlaggedNodes[i]->dump(G);
73 std::cerr << "\n";
74 }
75 }
76
77 if (All) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000078 std::cerr << " # preds left : " << NumPredsLeft << "\n";
79 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
80 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
81 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
82 std::cerr << " Latency : " << Latency << "\n";
Evan Chengc5c06582006-03-06 06:08:54 +000083 std::cerr << " SethiUllman : " << SethiUllman << "\n";
Evan Chengc4c339c2006-01-26 00:30:29 +000084
Evan Chengab495562006-01-25 09:14:32 +000085 if (Preds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000086 std::cerr << " Predecessors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000087 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000088 E = Preds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000089 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000090 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000091 }
92 }
93 if (ChainPreds.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +000094 std::cerr << " Chained Preds:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +000095 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +000096 E = ChainPreds.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +000097 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +000098 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000099 }
100 }
101 if (Succs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000102 std::cerr << " Successors:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000103 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000104 E = Succs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000105 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000106 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000107 }
108 }
109 if (ChainSuccs.size() != 0) {
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000110 std::cerr << " Chained succs:\n";
Jeff Cohen55c11732006-03-03 03:25:07 +0000111 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
Evan Cheng4e3904f2006-03-02 21:38:29 +0000112 E = ChainSuccs.end(); I != E; ++I) {
Evan Chengab495562006-01-25 09:14:32 +0000113 std::cerr << " ";
Evan Cheng4e3904f2006-03-02 21:38:29 +0000114 (*I)->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000115 }
116 }
117 }
118}
119
120/// Sorting functions for the Available queue.
121struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
122 bool operator()(const SUnit* left, const SUnit* right) const {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000123 bool LFloater = (left ->Preds.size() == 0);
124 bool RFloater = (right->Preds.size() == 0);
125 int LBonus = (int)left ->isDefNUseOperand;
126 int RBonus = (int)right->isDefNUseOperand;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000127
128 // Special tie breaker: if two nodes share a operand, the one that
129 // use it as a def&use operand is preferred.
130 if (left->isTwoAddress && !right->isTwoAddress) {
131 SDNode *DUNode = left->Node->getOperand(0).Val;
132 if (DUNode->isOperand(right->Node))
133 LBonus++;
134 }
135 if (!left->isTwoAddress && right->isTwoAddress) {
136 SDNode *DUNode = right->Node->getOperand(0).Val;
137 if (DUNode->isOperand(left->Node))
138 RBonus++;
139 }
140
Evan Chengc5c06582006-03-06 06:08:54 +0000141 // Priority1 is just the number of live range genned.
142 int LPriority1 = left ->NumPredsLeft - LBonus;
143 int RPriority1 = right->NumPredsLeft - RBonus;
144 int LPriority2 = left ->SethiUllman + LBonus;
145 int RPriority2 = right->SethiUllman + RBonus;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000146
147 // Favor floaters (i.e. node with no non-passive predecessors):
148 // e.g. MOV32ri.
149 if (!LFloater && RFloater)
Evan Chengab495562006-01-25 09:14:32 +0000150 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000151 else if (LFloater == RFloater)
152 if (LPriority1 > RPriority1)
Evan Chengab495562006-01-25 09:14:32 +0000153 return true;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000154 else if (LPriority1 == RPriority1)
155 if (LPriority2 < RPriority2)
Evan Chengab495562006-01-25 09:14:32 +0000156 return true;
Evan Chengc5c06582006-03-06 06:08:54 +0000157 else if (LPriority2 == RPriority2)
Evan Chengab495562006-01-25 09:14:32 +0000158 if (left->CycleBound > right->CycleBound)
159 return true;
Evan Chengab495562006-01-25 09:14:32 +0000160
161 return false;
Evan Cheng31272342006-01-23 08:26:10 +0000162 }
163};
164
Chris Lattnere50c0922006-03-05 22:45:01 +0000165
Evan Cheng31272342006-01-23 08:26:10 +0000166/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000167class ScheduleDAGList : public ScheduleDAG {
168private:
Evan Chengab495562006-01-25 09:14:32 +0000169 // SDNode to SUnit mapping (many to one).
170 std::map<SDNode*, SUnit*> SUnitMap;
Chris Lattner00b52ea2006-03-05 23:59:20 +0000171 // The schedule. Null SUnit*'s represent noop instructions.
Evan Chengab495562006-01-25 09:14:32 +0000172 std::vector<SUnit*> Sequence;
173 // Current scheduling cycle.
174 unsigned CurrCycle;
Evan Chengc4c339c2006-01-26 00:30:29 +0000175 // First and last SUnit created.
176 SUnit *HeadSUnit, *TailSUnit;
Evan Cheng31272342006-01-23 08:26:10 +0000177
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000178 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
179 /// it is top-down.
180 bool isBottomUp;
181
Chris Lattnere50c0922006-03-05 22:45:01 +0000182 /// HazardRec - The hazard recognizer to use.
Chris Lattner47639db2006-03-06 00:22:00 +0000183 HazardRecognizer &HazardRec;
Chris Lattnere50c0922006-03-05 22:45:01 +0000184
Chris Lattner7a36d972006-03-05 20:21:55 +0000185 typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort>
186 AvailableQueueTy;
187
Evan Cheng31272342006-01-23 08:26:10 +0000188public:
189 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Chris Lattnere50c0922006-03-05 22:45:01 +0000190 const TargetMachine &tm, bool isbottomup,
Chris Lattner47639db2006-03-06 00:22:00 +0000191 HazardRecognizer &HR)
Evan Chengc4c339c2006-01-26 00:30:29 +0000192 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
Chris Lattner47639db2006-03-06 00:22:00 +0000193 CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL), isBottomUp(isbottomup),
194 HazardRec(HR) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000195 }
Evan Chengab495562006-01-25 09:14:32 +0000196
197 ~ScheduleDAGList() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000198 SUnit *SU = HeadSUnit;
199 while (SU) {
200 SUnit *NextSU = SU->Next;
201 delete SU;
202 SU = NextSU;
Evan Chengab495562006-01-25 09:14:32 +0000203 }
204 }
Evan Cheng31272342006-01-23 08:26:10 +0000205
206 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000207
Evan Chengab495562006-01-25 09:14:32 +0000208 void dump() const;
209
210private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000211 SUnit *NewSUnit(SDNode *N);
Chris Lattner7a36d972006-03-05 20:21:55 +0000212 void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000213 void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false);
214 void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU);
215 void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU);
Evan Chengab495562006-01-25 09:14:32 +0000216 int CalcNodePriority(SUnit *SU);
217 void CalculatePriorities();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000218 void ListScheduleTopDown();
219 void ListScheduleBottomUp();
Evan Chengab495562006-01-25 09:14:32 +0000220 void BuildSchedUnits();
221 void EmitSchedule();
222};
223} // end namespace
224
Chris Lattner47639db2006-03-06 00:22:00 +0000225HazardRecognizer::~HazardRecognizer() {}
226
Evan Chengc4c339c2006-01-26 00:30:29 +0000227
228/// NewSUnit - Creates a new SUnit and return a ptr to it.
229SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
230 SUnit *CurrSUnit = new SUnit(N);
231
232 if (HeadSUnit == NULL)
233 HeadSUnit = CurrSUnit;
234 if (TailSUnit != NULL)
235 TailSUnit->Next = CurrSUnit;
236 TailSUnit = CurrSUnit;
237
238 return CurrSUnit;
239}
240
241/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
242/// the Available queue is the count reaches zero. Also update its cycle bound.
Chris Lattner7a36d972006-03-05 20:21:55 +0000243void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available,
244 SUnit *PredSU, bool isChain) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000245 // FIXME: the distance between two nodes is not always == the predecessor's
246 // latency. For example, the reader can very well read the register written
247 // by the predecessor later than the issue cycle. It also depends on the
248 // interrupt model (drain vs. freeze).
249 PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency);
250
Evan Chengc5c06582006-03-06 06:08:54 +0000251 if (!isChain)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000252 PredSU->NumSuccsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000253 else
Evan Cheng4e3904f2006-03-02 21:38:29 +0000254 PredSU->NumChainSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000255
Evan Chengab495562006-01-25 09:14:32 +0000256#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000257 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
Evan Chengab495562006-01-25 09:14:32 +0000258 std::cerr << "*** List scheduling failed! ***\n";
259 PredSU->dump(&DAG);
260 std::cerr << " has been released too many times!\n";
261 assert(0);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000262 }
Evan Chengab495562006-01-25 09:14:32 +0000263#endif
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000264
265 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
266 // EntryToken has to go last! Special case it here.
267 if (PredSU->Node->getOpcode() != ISD::EntryToken)
268 Available.push(PredSU);
Evan Chengab495562006-01-25 09:14:32 +0000269 }
Evan Chengab495562006-01-25 09:14:32 +0000270}
271
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000272/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
273/// the Available queue is the count reaches zero. Also update its cycle bound.
274void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available,
275 SUnit *SuccSU, bool isChain) {
276 // FIXME: the distance between two nodes is not always == the predecessor's
277 // latency. For example, the reader can very well read the register written
278 // by the predecessor later than the issue cycle. It also depends on the
279 // interrupt model (drain vs. freeze).
280 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurrCycle + SuccSU->Latency);
281
Evan Chengc5c06582006-03-06 06:08:54 +0000282 if (!isChain)
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000283 SuccSU->NumPredsLeft--;
Evan Chengc5c06582006-03-06 06:08:54 +0000284 else
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000285 SuccSU->NumChainPredsLeft--;
286
287#ifndef NDEBUG
288 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
289 std::cerr << "*** List scheduling failed! ***\n";
290 SuccSU->dump(&DAG);
291 std::cerr << " has been released too many times!\n";
292 abort();
293 }
294#endif
295
296 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
297 Available.push(SuccSU);
298}
299
300/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
301/// count of its predecessors. If a predecessor pending count is zero, add it to
302/// the Available queue.
303void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available,
304 SUnit *SU) {
Evan Cheng5e9a6952006-03-03 06:23:43 +0000305 DEBUG(std::cerr << "*** Scheduling: ");
306 DEBUG(SU->dump(&DAG, false));
307
Evan Chengab495562006-01-25 09:14:32 +0000308 Sequence.push_back(SU);
309 SU->Slot = CurrCycle;
310
311 // Bottom up: release predecessors
Evan Cheng4e3904f2006-03-02 21:38:29 +0000312 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
313 E1 = SU->Preds.end(); I1 != E1; ++I1) {
Chris Lattner7a36d972006-03-05 20:21:55 +0000314 ReleasePred(Available, *I1);
Evan Cheng4e3904f2006-03-02 21:38:29 +0000315 SU->NumPredsLeft--;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000316 }
317 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
318 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
Chris Lattner7a36d972006-03-05 20:21:55 +0000319 ReleasePred(Available, *I2, true);
Evan Chengab495562006-01-25 09:14:32 +0000320
321 CurrCycle++;
322}
323
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000324/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
325/// count of its successors. If a successor pending count is zero, add it to
326/// the Available queue.
327void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available,
328 SUnit *SU) {
329 DEBUG(std::cerr << "*** Scheduling: ");
330 DEBUG(SU->dump(&DAG, false));
331
332 Sequence.push_back(SU);
333 SU->Slot = CurrCycle;
334
335 // Bottom up: release successors.
336 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
337 E1 = SU->Succs.end(); I1 != E1; ++I1) {
338 ReleaseSucc(Available, *I1);
339 SU->NumSuccsLeft--;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000340 }
341 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
342 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
343 ReleaseSucc(Available, *I2, true);
344
345 CurrCycle++;
346}
347
Evan Chengab495562006-01-25 09:14:32 +0000348/// isReady - True if node's lower cycle bound is less or equal to the current
349/// scheduling cycle. Always true if all nodes have uniform latency 1.
350static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
351 return SU->CycleBound <= CurrCycle;
352}
353
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000354/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
355/// schedulers.
356void ScheduleDAGList::ListScheduleBottomUp() {
Chris Lattner7a36d972006-03-05 20:21:55 +0000357 // Available queue.
358 AvailableQueueTy Available;
359
360 // Add root to Available queue.
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000361 Available.push(SUnitMap[DAG.getRoot().Val]);
Evan Chengab495562006-01-25 09:14:32 +0000362
363 // While Available queue is not empty, grab the node with the highest
364 // priority. If it is not ready put it back. Schedule the node.
365 std::vector<SUnit*> NotReady;
366 while (!Available.empty()) {
367 SUnit *CurrNode = Available.top();
368 Available.pop();
369
Evan Chengab495562006-01-25 09:14:32 +0000370 while (!isReady(CurrNode, CurrCycle)) {
371 NotReady.push_back(CurrNode);
372 CurrNode = Available.top();
373 Available.pop();
374 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000375
376 // Add the nodes that aren't ready back onto the available list.
377 while (!NotReady.empty()) {
378 Available.push(NotReady.back());
379 NotReady.pop_back();
380 }
Evan Chengab495562006-01-25 09:14:32 +0000381
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000382 ScheduleNodeBottomUp(Available, CurrNode);
Evan Chengab495562006-01-25 09:14:32 +0000383 }
384
385 // Add entry node last
386 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
387 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
388 Entry->Slot = CurrCycle;
389 Sequence.push_back(Entry);
390 }
391
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000392 // Reverse the order if it is bottom up.
393 std::reverse(Sequence.begin(), Sequence.end());
394
395
Evan Chengab495562006-01-25 09:14:32 +0000396#ifndef NDEBUG
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000397 // Verify that all SUnits were scheduled.
Evan Chengc4c339c2006-01-26 00:30:29 +0000398 bool AnyNotSched = false;
399 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000400 if (SU->NumSuccsLeft != 0 || SU->NumChainSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000401 if (!AnyNotSched)
402 std::cerr << "*** List scheduling failed! ***\n";
Evan Chengab495562006-01-25 09:14:32 +0000403 SU->dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000404 std::cerr << "has not been scheduled!\n";
405 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000406 }
Evan Chengab495562006-01-25 09:14:32 +0000407 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000408 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000409#endif
Evan Chengab495562006-01-25 09:14:32 +0000410}
411
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000412/// ListScheduleTopDown - The main loop of list scheduling for top-down
413/// schedulers.
414void ScheduleDAGList::ListScheduleTopDown() {
415 // Available queue.
416 AvailableQueueTy Available;
Chris Lattner47639db2006-03-06 00:22:00 +0000417
418 HazardRec.StartBasicBlock();
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000419
420 // Emit the entry node first.
421 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
422 ScheduleNodeTopDown(Available, Entry);
Chris Lattner47639db2006-03-06 00:22:00 +0000423 HazardRec.EmitInstruction(Entry->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000424
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000425 // All leaves to Available queue.
426 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
427 // It is available if it has no predecessors.
428 if ((SU->Preds.size() + SU->ChainPreds.size()) == 0 && SU != Entry)
429 Available.push(SU);
430 }
431
432 // While Available queue is not empty, grab the node with the highest
433 // priority. If it is not ready put it back. Schedule the node.
434 std::vector<SUnit*> NotReady;
435 while (!Available.empty()) {
Chris Lattnere50c0922006-03-05 22:45:01 +0000436 SUnit *FoundNode = 0;
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000437
Chris Lattnere50c0922006-03-05 22:45:01 +0000438 bool HasNoopHazards = false;
439 do {
440 SUnit *CurrNode = Available.top();
441 Available.pop();
442 HazardRecognizer::HazardType HT =
Chris Lattner47639db2006-03-06 00:22:00 +0000443 HazardRec.getHazardType(CurrNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000444 if (HT == HazardRecognizer::NoHazard) {
445 FoundNode = CurrNode;
446 break;
447 }
448
449 // Remember if this is a noop hazard.
450 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
451
452 NotReady.push_back(CurrNode);
453 } while (!Available.empty());
454
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000455 // Add the nodes that aren't ready back onto the available list.
456 while (!NotReady.empty()) {
457 Available.push(NotReady.back());
458 NotReady.pop_back();
459 }
Chris Lattnere50c0922006-03-05 22:45:01 +0000460
461 // If we found a node to schedule, do it now.
462 if (FoundNode) {
463 ScheduleNodeTopDown(Available, FoundNode);
Chris Lattner47639db2006-03-06 00:22:00 +0000464 HazardRec.EmitInstruction(FoundNode->Node);
Chris Lattnere50c0922006-03-05 22:45:01 +0000465 } else if (!HasNoopHazards) {
466 // Otherwise, we have a pipeline stall, but no other problem, just advance
467 // the current cycle and try again.
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000468 DEBUG(std::cerr << "*** Advancing cycle, no work to do");
Chris Lattner47639db2006-03-06 00:22:00 +0000469 HazardRec.AdvanceCycle();
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000470 ++NumStalls;
Chris Lattnere50c0922006-03-05 22:45:01 +0000471 } else {
472 // Otherwise, we have no instructions to issue and we have instructions
473 // that will fault if we don't do this right. This is the case for
474 // processors without pipeline interlocks and other cases.
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000475 DEBUG(std::cerr << "*** Emitting noop");
Chris Lattner47639db2006-03-06 00:22:00 +0000476 HazardRec.EmitNoop();
Chris Lattner00b52ea2006-03-05 23:59:20 +0000477 Sequence.push_back(0); // NULL SUnit* -> noop
Chris Lattnerfa5e1c92006-03-05 23:13:56 +0000478 ++NumNoops;
Chris Lattnere50c0922006-03-05 22:45:01 +0000479 }
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000480 }
481
482#ifndef NDEBUG
483 // Verify that all SUnits were scheduled.
484 bool AnyNotSched = false;
485 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
486 if (SU->NumPredsLeft != 0 || SU->NumChainPredsLeft != 0) {
487 if (!AnyNotSched)
488 std::cerr << "*** List scheduling failed! ***\n";
489 SU->dump(&DAG);
490 std::cerr << "has not been scheduled!\n";
491 AnyNotSched = true;
492 }
493 }
494 assert(!AnyNotSched);
495#endif
496}
497
498
Evan Chengc5c06582006-03-06 06:08:54 +0000499/// CalcNodePriority - Priority is the Sethi Ullman number.
500/// Smaller number is the higher priority.
Evan Chengab495562006-01-25 09:14:32 +0000501int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
Evan Chengc5c06582006-03-06 06:08:54 +0000502 if (SU->SethiUllman != INT_MIN)
503 return SU->SethiUllman;
Evan Chengab495562006-01-25 09:14:32 +0000504
505 if (SU->Preds.size() == 0) {
Evan Chengc5c06582006-03-06 06:08:54 +0000506 SU->SethiUllman = 1;
Evan Chengab495562006-01-25 09:14:32 +0000507 } else {
508 int Extra = 0;
Evan Cheng4e3904f2006-03-02 21:38:29 +0000509 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
510 E = SU->Preds.end(); I != E; ++I) {
511 SUnit *PredSU = *I;
Evan Chengc5c06582006-03-06 06:08:54 +0000512 int PredSethiUllman = CalcNodePriority(PredSU);
513 if (PredSethiUllman > SU->SethiUllman) {
514 SU->SethiUllman = PredSethiUllman;
Evan Chengab495562006-01-25 09:14:32 +0000515 Extra = 0;
Evan Chengc5c06582006-03-06 06:08:54 +0000516 } else if (PredSethiUllman == SU->SethiUllman)
Evan Chengab495562006-01-25 09:14:32 +0000517 Extra++;
518 }
519
520 if (SU->Node->getOpcode() != ISD::TokenFactor)
Evan Chengc5c06582006-03-06 06:08:54 +0000521 SU->SethiUllman += Extra;
Evan Chengab495562006-01-25 09:14:32 +0000522 else
Evan Chengc5c06582006-03-06 06:08:54 +0000523 SU->SethiUllman = (Extra == 1) ? 0 : Extra-1;
Evan Chengab495562006-01-25 09:14:32 +0000524 }
525
Evan Chengc5c06582006-03-06 06:08:54 +0000526 return SU->SethiUllman;
Evan Chengab495562006-01-25 09:14:32 +0000527}
528
529/// CalculatePriorities - Calculate priorities of all scheduling units.
530void ScheduleDAGList::CalculatePriorities() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000531 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000532 // FIXME: assumes uniform latency for now.
533 SU->Latency = 1;
534 (void)CalcNodePriority(SU);
Evan Chengc4c339c2006-01-26 00:30:29 +0000535 DEBUG(SU->dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000536 DEBUG(std::cerr << "\n");
537 }
538}
539
Evan Chengab495562006-01-25 09:14:32 +0000540void ScheduleDAGList::BuildSchedUnits() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000541 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000542 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000543 NodeInfo *NI = &Info[i];
544 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000545 if (isPassiveNode(N))
546 continue;
Evan Chengab495562006-01-25 09:14:32 +0000547
Evan Chengc4c339c2006-01-26 00:30:29 +0000548 SUnit *SU;
549 if (NI->isInGroup()) {
550 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
551 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000552
Evan Chengc4c339c2006-01-26 00:30:29 +0000553 SU = NewSUnit(N);
554 // Find the flagged nodes.
555 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
556 SDNode *Flag = FlagOp.Val;
557 unsigned ResNo = FlagOp.ResNo;
558 while (Flag->getValueType(ResNo) == MVT::Flag) {
559 NodeInfo *FNI = getNI(Flag);
560 assert(FNI->Group == NI->Group);
561 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
562 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000563
Evan Chengc4c339c2006-01-26 00:30:29 +0000564 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
565 Flag = FlagOp.Val;
566 ResNo = FlagOp.ResNo;
567 }
568 } else {
569 SU = NewSUnit(N);
570 }
571 SUnitMap[N] = SU;
572 }
Evan Chengab495562006-01-25 09:14:32 +0000573
Evan Chengc4c339c2006-01-26 00:30:29 +0000574 // Pass 2: add the preds, succs, etc.
575 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
576 SDNode *N = SU->Node;
577 NodeInfo *NI = getNI(N);
Evan Cheng5e9a6952006-03-03 06:23:43 +0000578
579 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
580 SU->isTwoAddress = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000581
582 if (NI->isInGroup()) {
583 // Find all predecessors (of the group).
584 NodeGroupOpIterator NGOI(NI);
585 while (!NGOI.isEnd()) {
586 SDOperand Op = NGOI.next();
587 SDNode *OpN = Op.Val;
588 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
589 NodeInfo *OpNI = getNI(OpN);
590 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
591 assert(VT != MVT::Flag);
592 SUnit *OpSU = SUnitMap[OpN];
593 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000594 if (SU->ChainPreds.insert(OpSU).second)
595 SU->NumChainPredsLeft++;
596 if (OpSU->ChainSuccs.insert(SU).second)
597 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000598 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000599 if (SU->Preds.insert(OpSU).second)
600 SU->NumPredsLeft++;
601 if (OpSU->Succs.insert(SU).second)
602 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000603 }
Evan Chengab495562006-01-25 09:14:32 +0000604 }
605 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000606 } else {
607 // Find node predecessors.
608 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
609 SDOperand Op = N->getOperand(j);
610 SDNode *OpN = Op.Val;
611 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
612 if (!isPassiveNode(OpN)) {
613 assert(VT != MVT::Flag);
614 SUnit *OpSU = SUnitMap[OpN];
615 if (VT == MVT::Other) {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000616 if (SU->ChainPreds.insert(OpSU).second)
617 SU->NumChainPredsLeft++;
618 if (OpSU->ChainSuccs.insert(SU).second)
619 OpSU->NumChainSuccsLeft++;
Evan Chengc4c339c2006-01-26 00:30:29 +0000620 } else {
Evan Cheng4e3904f2006-03-02 21:38:29 +0000621 if (SU->Preds.insert(OpSU).second)
622 SU->NumPredsLeft++;
623 if (OpSU->Succs.insert(SU).second)
624 OpSU->NumSuccsLeft++;
Evan Cheng5e9a6952006-03-03 06:23:43 +0000625 if (j == 0 && SU->isTwoAddress)
Evan Cheng4e3904f2006-03-02 21:38:29 +0000626 OpSU->isDefNUseOperand = true;
Evan Chengc4c339c2006-01-26 00:30:29 +0000627 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000628 }
629 }
Evan Chengab495562006-01-25 09:14:32 +0000630 }
631 }
Evan Chengab495562006-01-25 09:14:32 +0000632}
633
634/// EmitSchedule - Emit the machine code in scheduled order.
635void ScheduleDAGList::EmitSchedule() {
636 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000637 if (SUnit *SU = Sequence[i]) {
638 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
639 SDNode *N = SU->FlaggedNodes[j];
640 EmitNode(getNI(N));
641 }
642 EmitNode(getNI(SU->Node));
643 } else {
644 // Null SUnit* is a noop.
645 EmitNoop();
Evan Chengab495562006-01-25 09:14:32 +0000646 }
Evan Chengab495562006-01-25 09:14:32 +0000647 }
648}
649
650/// dump - dump the schedule.
651void ScheduleDAGList::dump() const {
652 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Chris Lattner2d945ba2006-03-05 23:51:47 +0000653 if (SUnit *SU = Sequence[i])
654 SU->dump(&DAG, false);
655 else
656 std::cerr << "**** NOOP ****\n";
Evan Chengab495562006-01-25 09:14:32 +0000657 }
658}
659
660/// Schedule - Schedule the DAG using list scheduling.
661/// FIXME: Right now it only supports the burr (bottom up register reducing)
662/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000663void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000664 DEBUG(std::cerr << "********** List Scheduling **********\n");
665
666 // Build scheduling units.
667 BuildSchedUnits();
668
Chris Lattner47639db2006-03-06 00:22:00 +0000669 // Calculate node priorities.
Evan Chengab495562006-01-25 09:14:32 +0000670 CalculatePriorities();
671
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000672 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
673 if (isBottomUp)
674 ListScheduleBottomUp();
675 else
676 ListScheduleTopDown();
677
678 DEBUG(std::cerr << "*** Final schedule ***\n");
679 DEBUG(dump());
680 DEBUG(std::cerr << "\n");
681
Evan Chengab495562006-01-25 09:14:32 +0000682 // Emit in scheduled order
683 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000684}
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000685
Evan Chengab495562006-01-25 09:14:32 +0000686llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
687 MachineBasicBlock *BB) {
Chris Lattner47639db2006-03-06 00:22:00 +0000688 HazardRecognizer HR;
689 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true, HR);
Chris Lattner98ecb8e2006-03-05 21:10:33 +0000690}
691
Chris Lattner47639db2006-03-06 00:22:00 +0000692/// createTDListDAGScheduler - This creates a top-down list scheduler with the
693/// specified hazard recognizer.
694ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
695 MachineBasicBlock *BB,
696 HazardRecognizer &HR) {
697 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, HR);
Evan Cheng31272342006-01-23 08:26:10 +0000698}