blob: dc702e559b379f0bdf4638a0350dd1ae29a4a496 [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"
22#include <climits>
23#include <iostream>
24#include <memory>
Evan Cheng31272342006-01-23 08:26:10 +000025#include <queue>
26using namespace llvm;
27
Evan Chengab495562006-01-25 09:14:32 +000028namespace {
Evan Cheng31272342006-01-23 08:26:10 +000029
Evan Chengab495562006-01-25 09:14:32 +000030/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or a
31/// group of nodes flagged together.
32struct SUnit {
33 SDNode *Node; // Representative node.
34 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
Evan Chengc4c339c2006-01-26 00:30:29 +000035 std::vector<SUnit*> Preds; // All real predecessors.
36 std::vector<SUnit*> ChainPreds; // All chain predecessors.
37 std::vector<SUnit*> Succs; // All real successors.
38 std::vector<SUnit*> ChainSuccs; // All chain successors.
Evan Chengab495562006-01-25 09:14:32 +000039 int NumPredsLeft; // # of preds not scheduled.
40 int NumSuccsLeft; // # of succs not scheduled.
41 int Priority1; // Scheduling priority 1.
42 int Priority2; // Scheduling priority 2.
43 unsigned Latency; // Node latency.
44 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
45 unsigned Slot; // Cycle node is scheduled at.
Evan Chengc4c339c2006-01-26 00:30:29 +000046 SUnit *Next;
Evan Chengab495562006-01-25 09:14:32 +000047
48 SUnit(SDNode *node)
49 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
50 Priority1(INT_MIN), Priority2(INT_MIN), Latency(0),
Evan Chengc4c339c2006-01-26 00:30:29 +000051 CycleBound(0), Slot(0), Next(NULL) {}
Evan Chengab495562006-01-25 09:14:32 +000052
53 void dump(const SelectionDAG *G, bool All=true) const;
54};
55
56void SUnit::dump(const SelectionDAG *G, bool All) const {
Evan Chengc4c339c2006-01-26 00:30:29 +000057 std::cerr << "SU: ";
Evan Chengab495562006-01-25 09:14:32 +000058 Node->dump(G);
59 std::cerr << "\n";
Evan Chengab495562006-01-25 09:14:32 +000060 if (FlaggedNodes.size() != 0) {
Evan Chengab495562006-01-25 09:14:32 +000061 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +000062 std::cerr << " ";
Evan Chengab495562006-01-25 09:14:32 +000063 FlaggedNodes[i]->dump(G);
64 std::cerr << "\n";
65 }
66 }
67
68 if (All) {
Evan Chengc4c339c2006-01-26 00:30:29 +000069 std::cerr << "# preds left : " << NumPredsLeft << "\n";
70 std::cerr << "# succs left : " << NumSuccsLeft << "\n";
71 std::cerr << "Latency : " << Latency << "\n";
72 std::cerr << "Priority : " << Priority1 << " , " << Priority2 << "\n";
73
Evan Chengab495562006-01-25 09:14:32 +000074 if (Preds.size() != 0) {
75 std::cerr << "Predecessors :\n";
76 for (unsigned i = 0, e = Preds.size(); i != e; i++) {
77 std::cerr << " ";
Evan Chengc4c339c2006-01-26 00:30:29 +000078 Preds[i]->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000079 }
80 }
81 if (ChainPreds.size() != 0) {
82 std::cerr << "Chained Preds :\n";
83 for (unsigned i = 0, e = ChainPreds.size(); i != e; i++) {
84 std::cerr << " ";
Evan Chengc4c339c2006-01-26 00:30:29 +000085 ChainPreds[i]->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000086 }
87 }
88 if (Succs.size() != 0) {
89 std::cerr << "Successors :\n";
90 for (unsigned i = 0, e = Succs.size(); i != e; i++) {
91 std::cerr << " ";
Evan Chengc4c339c2006-01-26 00:30:29 +000092 Succs[i]->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +000093 }
94 }
95 if (ChainSuccs.size() != 0) {
96 std::cerr << "Chained succs :\n";
97 for (unsigned i = 0, e = ChainSuccs.size(); i != e; i++) {
98 std::cerr << " ";
Evan Chengc4c339c2006-01-26 00:30:29 +000099 ChainSuccs[i]->dump(G, false);
Evan Chengab495562006-01-25 09:14:32 +0000100 }
101 }
102 }
103}
104
105/// Sorting functions for the Available queue.
106struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
107 bool operator()(const SUnit* left, const SUnit* right) const {
108 if (left->Priority1 > right->Priority1) {
109 return true;
110 } else if (left->Priority1 == right->Priority1) {
111 unsigned lf = left->FlaggedNodes.size();
112 unsigned rf = right->FlaggedNodes.size();
113 if (lf > rf)
114 return true;
115 else if (lf == rf) {
116 if (left->Priority2 > right->Priority2)
117 return true;
118 else if (left->Priority2 == right->Priority2) {
119 if (left->CycleBound > right->CycleBound)
120 return true;
121 else
122 return left->Node->getNodeDepth() < right->Node->getNodeDepth();
123 }
124 }
125 }
126
127 return false;
Evan Cheng31272342006-01-23 08:26:10 +0000128 }
129};
130
131/// ScheduleDAGList - List scheduler.
Evan Cheng31272342006-01-23 08:26:10 +0000132class ScheduleDAGList : public ScheduleDAG {
133private:
Evan Chengab495562006-01-25 09:14:32 +0000134 // SDNode to SUnit mapping (many to one).
135 std::map<SDNode*, SUnit*> SUnitMap;
136 // Available queue.
137 std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Available;
138 // The schedule.
139 std::vector<SUnit*> Sequence;
140 // Current scheduling cycle.
141 unsigned CurrCycle;
Evan Chengc4c339c2006-01-26 00:30:29 +0000142 // First and last SUnit created.
143 SUnit *HeadSUnit, *TailSUnit;
Evan Cheng31272342006-01-23 08:26:10 +0000144
Evan Cheng31272342006-01-23 08:26:10 +0000145public:
146 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
Evan Chengab495562006-01-25 09:14:32 +0000147 const TargetMachine &tm)
Evan Chengc4c339c2006-01-26 00:30:29 +0000148 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
149 CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL) {};
Evan Chengab495562006-01-25 09:14:32 +0000150
151 ~ScheduleDAGList() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000152 SUnit *SU = HeadSUnit;
153 while (SU) {
154 SUnit *NextSU = SU->Next;
155 delete SU;
156 SU = NextSU;
Evan Chengab495562006-01-25 09:14:32 +0000157 }
158 }
Evan Cheng31272342006-01-23 08:26:10 +0000159
160 void Schedule();
Evan Cheng31272342006-01-23 08:26:10 +0000161
Evan Chengab495562006-01-25 09:14:32 +0000162 void dump() const;
163
164private:
Evan Chengc4c339c2006-01-26 00:30:29 +0000165 SUnit *NewSUnit(SDNode *N);
Evan Chengab495562006-01-25 09:14:32 +0000166 void ReleasePred(SUnit *PredSU);
167 void ScheduleNode(SUnit *SU);
168 int CalcNodePriority(SUnit *SU);
169 void CalculatePriorities();
170 void ListSchedule();
171 void BuildSchedUnits();
172 void EmitSchedule();
173};
174} // end namespace
175
Evan Chengc4c339c2006-01-26 00:30:29 +0000176
177/// NewSUnit - Creates a new SUnit and return a ptr to it.
178SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
179 SUnit *CurrSUnit = new SUnit(N);
180
181 if (HeadSUnit == NULL)
182 HeadSUnit = CurrSUnit;
183 if (TailSUnit != NULL)
184 TailSUnit->Next = CurrSUnit;
185 TailSUnit = CurrSUnit;
186
187 return CurrSUnit;
188}
189
190/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
191/// the Available queue is the count reaches zero. Also update its cycle bound.
Evan Chengab495562006-01-25 09:14:32 +0000192void ScheduleDAGList::ReleasePred(SUnit *PredSU) {
193 SDNode *PredNode = PredSU->Node;
194
195 PredSU->NumSuccsLeft--;
196 if (PredSU->NumSuccsLeft == 0) {
197 // EntryToken has to go last!
198 if (PredNode->getOpcode() != ISD::EntryToken)
199 Available.push(PredSU);
200 } else if (PredSU->NumSuccsLeft < 0) {
201#ifndef NDEBUG
202 std::cerr << "*** List scheduling failed! ***\n";
203 PredSU->dump(&DAG);
204 std::cerr << " has been released too many times!\n";
205 assert(0);
206#endif
207 }
208
209 // FIXME: the distance between two nodes is not always == the predecessor's
210 // latency. For example, the reader can very well read the register written
211 // by the predecessor later than the issue cycle. It also depends on the
212 // interrupt model (drain vs. freeze).
213 PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency);
214}
215
216/// ScheduleNode - Add the node to the schedule. Decrement the pending count of
217/// its predecessors. If a predecessor pending count is zero, add it to the
218/// Available queue.
219void ScheduleDAGList::ScheduleNode(SUnit *SU) {
220 Sequence.push_back(SU);
221 SU->Slot = CurrCycle;
222
223 // Bottom up: release predecessors
224 for (unsigned i = 0, e = SU->Preds.size(); i != e; i++)
Evan Chengc4c339c2006-01-26 00:30:29 +0000225 ReleasePred(SU->Preds[i]);
Evan Chengab495562006-01-25 09:14:32 +0000226 for (unsigned i = 0, e = SU->ChainPreds.size(); i != e; i++)
Evan Chengc4c339c2006-01-26 00:30:29 +0000227 ReleasePred(SU->ChainPreds[i]);
Evan Chengab495562006-01-25 09:14:32 +0000228
229 CurrCycle++;
230}
231
232/// isReady - True if node's lower cycle bound is less or equal to the current
233/// scheduling cycle. Always true if all nodes have uniform latency 1.
234static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
235 return SU->CycleBound <= CurrCycle;
236}
237
238/// ListSchedule - The main loop of list scheduling.
239void ScheduleDAGList::ListSchedule() {
240 // Add root to Available queue
241 SUnit *Root = SUnitMap[DAG.getRoot().Val];
242 Available.push(Root);
243
244 // While Available queue is not empty, grab the node with the highest
245 // priority. If it is not ready put it back. Schedule the node.
246 std::vector<SUnit*> NotReady;
247 while (!Available.empty()) {
248 SUnit *CurrNode = Available.top();
249 Available.pop();
250
251 NotReady.clear();
252 while (!isReady(CurrNode, CurrCycle)) {
253 NotReady.push_back(CurrNode);
254 CurrNode = Available.top();
255 Available.pop();
256 }
257 for (unsigned i = 0, e = NotReady.size(); i != e; ++i)
258 Available.push(NotReady[i]);
259
260 DEBUG(std::cerr << "\n*** Scheduling: ");
261 DEBUG(CurrNode->dump(&DAG, false));
262 DEBUG(std::cerr << "\n");
263 ScheduleNode(CurrNode);
264 }
265
266 // Add entry node last
267 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
268 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
269 Entry->Slot = CurrCycle;
270 Sequence.push_back(Entry);
271 }
272
273#ifndef NDEBUG
Evan Chengc4c339c2006-01-26 00:30:29 +0000274 bool AnyNotSched = false;
275 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000276 if (SU->NumSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000277 if (!AnyNotSched)
278 std::cerr << "*** List scheduling failed! ***\n";
Evan Chengab495562006-01-25 09:14:32 +0000279 SU->dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000280 std::cerr << "has not been scheduled!\n";
281 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000282 }
Evan Chengab495562006-01-25 09:14:32 +0000283 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000284 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000285#endif
Evan Chengab495562006-01-25 09:14:32 +0000286
287
288 // Reverse the order if it is bottom up.
289 std::reverse(Sequence.begin(), Sequence.end());
290
291 DEBUG(std::cerr << "*** Final schedule ***\n");
292 DEBUG(dump());
Evan Chengc4c339c2006-01-26 00:30:29 +0000293 DEBUG(std::cerr << "\n");
Evan Chengab495562006-01-25 09:14:32 +0000294}
295
296/// CalcNodePriority - Priority 1 is just the number of live range genned - number
297/// of live range killed. Priority 2 is the Sethi Ullman number. It returns
298/// priority 2 since it is calculated recursively.
299/// Smaller number is the higher priority in both cases.
300int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
301 if (SU->Priority2 != INT_MIN)
302 return SU->Priority2;
303
304 SU->Priority1 = SU->Preds.size() - SU->Succs.size();
305
306 if (SU->Preds.size() == 0) {
307 SU->Priority2 = 1;
308 } else {
309 int Extra = 0;
310 for (unsigned i = 0, e = SU->Preds.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000311 SUnit *PredSU = SU->Preds[i];
Evan Chengab495562006-01-25 09:14:32 +0000312 int PredPriority = CalcNodePriority(PredSU);
313 if (PredPriority > SU->Priority2) {
314 SU->Priority2 = PredPriority;
315 Extra = 0;
316 } else if (PredPriority == SU->Priority2)
317 Extra++;
318 }
319
320 if (SU->Node->getOpcode() != ISD::TokenFactor)
321 SU->Priority2 += Extra;
322 else
323 SU->Priority2 = (Extra == 1) ? 0 : Extra-1;
324 }
325
326 return SU->Priority2;
327}
328
329/// CalculatePriorities - Calculate priorities of all scheduling units.
330void ScheduleDAGList::CalculatePriorities() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000331 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000332 // FIXME: assumes uniform latency for now.
333 SU->Latency = 1;
334 (void)CalcNodePriority(SU);
Evan Chengc4c339c2006-01-26 00:30:29 +0000335 DEBUG(SU->dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000336 DEBUG(std::cerr << "\n");
337 }
338}
339
Evan Chengab495562006-01-25 09:14:32 +0000340void ScheduleDAGList::BuildSchedUnits() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000341 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000342 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000343 NodeInfo *NI = &Info[i];
344 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000345 if (isPassiveNode(N))
346 continue;
Evan Chengab495562006-01-25 09:14:32 +0000347
Evan Chengc4c339c2006-01-26 00:30:29 +0000348 SUnit *SU;
349 if (NI->isInGroup()) {
350 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
351 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000352
Evan Chengc4c339c2006-01-26 00:30:29 +0000353 SU = NewSUnit(N);
354 // Find the flagged nodes.
355 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
356 SDNode *Flag = FlagOp.Val;
357 unsigned ResNo = FlagOp.ResNo;
358 while (Flag->getValueType(ResNo) == MVT::Flag) {
359 NodeInfo *FNI = getNI(Flag);
360 assert(FNI->Group == NI->Group);
361 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
362 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000363
Evan Chengc4c339c2006-01-26 00:30:29 +0000364 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
365 Flag = FlagOp.Val;
366 ResNo = FlagOp.ResNo;
367 }
368 } else {
369 SU = NewSUnit(N);
370 }
371 SUnitMap[N] = SU;
372 }
Evan Chengab495562006-01-25 09:14:32 +0000373
Evan Chengc4c339c2006-01-26 00:30:29 +0000374 // Pass 2: add the preds, succs, etc.
375 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
376 SDNode *N = SU->Node;
377 NodeInfo *NI = getNI(N);
378
379 if (NI->isInGroup()) {
380 // Find all predecessors (of the group).
381 NodeGroupOpIterator NGOI(NI);
382 while (!NGOI.isEnd()) {
383 SDOperand Op = NGOI.next();
384 SDNode *OpN = Op.Val;
385 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
386 NodeInfo *OpNI = getNI(OpN);
387 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
388 assert(VT != MVT::Flag);
389 SUnit *OpSU = SUnitMap[OpN];
390 if (VT == MVT::Other) {
391 SU ->ChainPreds.push_back(OpSU);
392 OpSU->ChainSuccs.push_back(SU);
393 } else {
394 SU ->Preds.push_back(OpSU);
395 OpSU->Succs.push_back(SU);
Evan Chengab495562006-01-25 09:14:32 +0000396 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000397 SU->NumPredsLeft++;
398 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000399 }
400 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000401 } else {
402 // Find node predecessors.
403 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
404 SDOperand Op = N->getOperand(j);
405 SDNode *OpN = Op.Val;
406 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
407 if (!isPassiveNode(OpN)) {
408 assert(VT != MVT::Flag);
409 SUnit *OpSU = SUnitMap[OpN];
410 if (VT == MVT::Other) {
411 SU ->ChainPreds.push_back(OpSU);
412 OpSU->ChainSuccs.push_back(SU);
413 } else {
414 SU ->Preds.push_back(OpSU);
415 OpSU->Succs.push_back(SU);
416 }
417 SU->NumPredsLeft++;
418 OpSU->NumSuccsLeft++;
419 }
420 }
Evan Chengab495562006-01-25 09:14:32 +0000421 }
422 }
Evan Chengab495562006-01-25 09:14:32 +0000423}
424
425/// EmitSchedule - Emit the machine code in scheduled order.
426void ScheduleDAGList::EmitSchedule() {
427 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
428 SDNode *N;
429 SUnit *SU = Sequence[i];
430 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
431 N = SU->FlaggedNodes[j];
432 EmitNode(getNI(N));
433 }
434 EmitNode(getNI(SU->Node));
435 }
436}
437
438/// dump - dump the schedule.
439void ScheduleDAGList::dump() const {
440 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
441 SUnit *SU = Sequence[i];
442 SU->dump(&DAG, false);
443 }
444}
445
446/// Schedule - Schedule the DAG using list scheduling.
447/// FIXME: Right now it only supports the burr (bottom up register reducing)
448/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000449void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000450 DEBUG(std::cerr << "********** List Scheduling **********\n");
451
452 // Build scheduling units.
453 BuildSchedUnits();
454
455 // Calculate node prirorities.
456 CalculatePriorities();
457
458 // Execute the actual scheduling loop.
459 ListSchedule();
460
461 // Emit in scheduled order
462 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000463}
464
Evan Chengab495562006-01-25 09:14:32 +0000465llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
466 MachineBasicBlock *BB) {
467 return new ScheduleDAGList(DAG, BB, DAG.getTarget());
Evan Cheng31272342006-01-23 08:26:10 +0000468}