blob: 6d2ada715c564ba07d18bbe41cfb1149c00d7dc1 [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
Chris Lattner0bd74552006-02-02 00:38:08 +0000260 DEBUG(std::cerr << "*** Scheduling: ");
Evan Chengab495562006-01-25 09:14:32 +0000261 DEBUG(CurrNode->dump(&DAG, false));
Evan Chengab495562006-01-25 09:14:32 +0000262 ScheduleNode(CurrNode);
263 }
264
265 // Add entry node last
266 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
267 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
268 Entry->Slot = CurrCycle;
269 Sequence.push_back(Entry);
270 }
271
272#ifndef NDEBUG
Evan Chengc4c339c2006-01-26 00:30:29 +0000273 bool AnyNotSched = false;
274 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000275 if (SU->NumSuccsLeft != 0) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000276 if (!AnyNotSched)
277 std::cerr << "*** List scheduling failed! ***\n";
Evan Chengab495562006-01-25 09:14:32 +0000278 SU->dump(&DAG);
Evan Chengc4c339c2006-01-26 00:30:29 +0000279 std::cerr << "has not been scheduled!\n";
280 AnyNotSched = true;
Evan Chengab495562006-01-25 09:14:32 +0000281 }
Evan Chengab495562006-01-25 09:14:32 +0000282 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000283 assert(!AnyNotSched);
Reid Spencer5edde662006-01-25 21:49:13 +0000284#endif
Evan Chengab495562006-01-25 09:14:32 +0000285
286
287 // Reverse the order if it is bottom up.
288 std::reverse(Sequence.begin(), Sequence.end());
289
290 DEBUG(std::cerr << "*** Final schedule ***\n");
291 DEBUG(dump());
Evan Chengc4c339c2006-01-26 00:30:29 +0000292 DEBUG(std::cerr << "\n");
Evan Chengab495562006-01-25 09:14:32 +0000293}
294
295/// CalcNodePriority - Priority 1 is just the number of live range genned - number
296/// of live range killed. Priority 2 is the Sethi Ullman number. It returns
297/// priority 2 since it is calculated recursively.
298/// Smaller number is the higher priority in both cases.
299int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
300 if (SU->Priority2 != INT_MIN)
301 return SU->Priority2;
302
303 SU->Priority1 = SU->Preds.size() - SU->Succs.size();
304
305 if (SU->Preds.size() == 0) {
306 SU->Priority2 = 1;
307 } else {
308 int Extra = 0;
309 for (unsigned i = 0, e = SU->Preds.size(); i != e; i++) {
Evan Chengc4c339c2006-01-26 00:30:29 +0000310 SUnit *PredSU = SU->Preds[i];
Evan Chengab495562006-01-25 09:14:32 +0000311 int PredPriority = CalcNodePriority(PredSU);
312 if (PredPriority > SU->Priority2) {
313 SU->Priority2 = PredPriority;
314 Extra = 0;
315 } else if (PredPriority == SU->Priority2)
316 Extra++;
317 }
318
319 if (SU->Node->getOpcode() != ISD::TokenFactor)
320 SU->Priority2 += Extra;
321 else
322 SU->Priority2 = (Extra == 1) ? 0 : Extra-1;
323 }
324
325 return SU->Priority2;
326}
327
328/// CalculatePriorities - Calculate priorities of all scheduling units.
329void ScheduleDAGList::CalculatePriorities() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000330 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
Evan Chengab495562006-01-25 09:14:32 +0000331 // FIXME: assumes uniform latency for now.
332 SU->Latency = 1;
333 (void)CalcNodePriority(SU);
Evan Chengc4c339c2006-01-26 00:30:29 +0000334 DEBUG(SU->dump(&DAG));
Evan Chengab495562006-01-25 09:14:32 +0000335 DEBUG(std::cerr << "\n");
336 }
337}
338
Evan Chengab495562006-01-25 09:14:32 +0000339void ScheduleDAGList::BuildSchedUnits() {
Evan Chengc4c339c2006-01-26 00:30:29 +0000340 // Pass 1: create the SUnit's.
Jeff Cohenfb206162006-01-25 17:17:49 +0000341 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
Evan Chengab495562006-01-25 09:14:32 +0000342 NodeInfo *NI = &Info[i];
343 SDNode *N = NI->Node;
Evan Chengc4c339c2006-01-26 00:30:29 +0000344 if (isPassiveNode(N))
345 continue;
Evan Chengab495562006-01-25 09:14:32 +0000346
Evan Chengc4c339c2006-01-26 00:30:29 +0000347 SUnit *SU;
348 if (NI->isInGroup()) {
349 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
350 continue; // node of the NodeGroup
Evan Chengab495562006-01-25 09:14:32 +0000351
Evan Chengc4c339c2006-01-26 00:30:29 +0000352 SU = NewSUnit(N);
353 // Find the flagged nodes.
354 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
355 SDNode *Flag = FlagOp.Val;
356 unsigned ResNo = FlagOp.ResNo;
357 while (Flag->getValueType(ResNo) == MVT::Flag) {
358 NodeInfo *FNI = getNI(Flag);
359 assert(FNI->Group == NI->Group);
360 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
361 SUnitMap[Flag] = SU;
Evan Chengab495562006-01-25 09:14:32 +0000362
Evan Chengc4c339c2006-01-26 00:30:29 +0000363 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
364 Flag = FlagOp.Val;
365 ResNo = FlagOp.ResNo;
366 }
367 } else {
368 SU = NewSUnit(N);
369 }
370 SUnitMap[N] = SU;
371 }
Evan Chengab495562006-01-25 09:14:32 +0000372
Evan Chengc4c339c2006-01-26 00:30:29 +0000373 // Pass 2: add the preds, succs, etc.
374 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
375 SDNode *N = SU->Node;
376 NodeInfo *NI = getNI(N);
377
378 if (NI->isInGroup()) {
379 // Find all predecessors (of the group).
380 NodeGroupOpIterator NGOI(NI);
381 while (!NGOI.isEnd()) {
382 SDOperand Op = NGOI.next();
383 SDNode *OpN = Op.Val;
384 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
385 NodeInfo *OpNI = getNI(OpN);
386 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
387 assert(VT != MVT::Flag);
388 SUnit *OpSU = SUnitMap[OpN];
389 if (VT == MVT::Other) {
390 SU ->ChainPreds.push_back(OpSU);
391 OpSU->ChainSuccs.push_back(SU);
392 } else {
393 SU ->Preds.push_back(OpSU);
394 OpSU->Succs.push_back(SU);
Evan Chengab495562006-01-25 09:14:32 +0000395 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000396 SU->NumPredsLeft++;
397 OpSU->NumSuccsLeft++;
Evan Chengab495562006-01-25 09:14:32 +0000398 }
399 }
Evan Chengc4c339c2006-01-26 00:30:29 +0000400 } else {
401 // Find node predecessors.
402 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
403 SDOperand Op = N->getOperand(j);
404 SDNode *OpN = Op.Val;
405 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
406 if (!isPassiveNode(OpN)) {
407 assert(VT != MVT::Flag);
408 SUnit *OpSU = SUnitMap[OpN];
409 if (VT == MVT::Other) {
410 SU ->ChainPreds.push_back(OpSU);
411 OpSU->ChainSuccs.push_back(SU);
412 } else {
413 SU ->Preds.push_back(OpSU);
414 OpSU->Succs.push_back(SU);
415 }
416 SU->NumPredsLeft++;
417 OpSU->NumSuccsLeft++;
418 }
419 }
Evan Chengab495562006-01-25 09:14:32 +0000420 }
421 }
Evan Chengab495562006-01-25 09:14:32 +0000422}
423
424/// EmitSchedule - Emit the machine code in scheduled order.
425void ScheduleDAGList::EmitSchedule() {
426 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
427 SDNode *N;
428 SUnit *SU = Sequence[i];
429 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
430 N = SU->FlaggedNodes[j];
431 EmitNode(getNI(N));
432 }
433 EmitNode(getNI(SU->Node));
434 }
435}
436
437/// dump - dump the schedule.
438void ScheduleDAGList::dump() const {
439 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
440 SUnit *SU = Sequence[i];
441 SU->dump(&DAG, false);
442 }
443}
444
445/// Schedule - Schedule the DAG using list scheduling.
446/// FIXME: Right now it only supports the burr (bottom up register reducing)
447/// heuristic.
Evan Cheng31272342006-01-23 08:26:10 +0000448void ScheduleDAGList::Schedule() {
Evan Chengab495562006-01-25 09:14:32 +0000449 DEBUG(std::cerr << "********** List Scheduling **********\n");
450
451 // Build scheduling units.
452 BuildSchedUnits();
453
454 // Calculate node prirorities.
455 CalculatePriorities();
456
457 // Execute the actual scheduling loop.
458 ListSchedule();
459
460 // Emit in scheduled order
461 EmitSchedule();
Evan Cheng31272342006-01-23 08:26:10 +0000462}
463
Evan Chengab495562006-01-25 09:14:32 +0000464llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
465 MachineBasicBlock *BB) {
466 return new ScheduleDAGList(DAG, BB, DAG.getTarget());
Evan Cheng31272342006-01-23 08:26:10 +0000467}