1 //===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/ADT/Statistic.h"
31 Statistic<> NumNoops ("scheduler", "Number of noops inserted");
32 Statistic<> NumStalls("scheduler", "Number of pipeline stalls");
34 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or a
35 /// group of nodes flagged together.
37 SDNode *Node; // Representative node.
38 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
39 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.
43 int NumPredsLeft; // # of preds not scheduled.
44 int NumSuccsLeft; // # of succs not scheduled.
45 int NumChainPredsLeft; // # of chain preds not scheduled.
46 int NumChainSuccsLeft; // # of chain succs not scheduled.
47 int SethiUllman; // Sethi Ullman number.
48 bool isTwoAddress; // Is a two-address instruction.
49 bool isDefNUseOperand; // Is a def&use operand.
50 unsigned Latency; // Node latency.
51 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
52 unsigned Slot; // Cycle node is scheduled at.
56 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
57 NumChainPredsLeft(0), NumChainSuccsLeft(0),
59 isTwoAddress(false), isDefNUseOperand(false),
60 Latency(0), CycleBound(0), Slot(0), Next(NULL) {}
62 void dump(const SelectionDAG *G, bool All=true) const;
65 void SUnit::dump(const SelectionDAG *G, bool All) const {
69 if (FlaggedNodes.size() != 0) {
70 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
72 FlaggedNodes[i]->dump(G);
78 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";
83 std::cerr << " SethiUllman : " << SethiUllman << "\n";
85 if (Preds.size() != 0) {
86 std::cerr << " Predecessors:\n";
87 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
88 E = Preds.end(); I != E; ++I) {
93 if (ChainPreds.size() != 0) {
94 std::cerr << " Chained Preds:\n";
95 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
96 E = ChainPreds.end(); I != E; ++I) {
101 if (Succs.size() != 0) {
102 std::cerr << " Successors:\n";
103 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
104 E = Succs.end(); I != E; ++I) {
106 (*I)->dump(G, false);
109 if (ChainSuccs.size() != 0) {
110 std::cerr << " Chained succs:\n";
111 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
112 E = ChainSuccs.end(); I != E; ++I) {
114 (*I)->dump(G, false);
120 /// Sorting functions for the Available queue.
121 struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
122 bool operator()(const SUnit* left, const SUnit* right) const {
123 int LBonus = (int)left ->isDefNUseOperand;
124 int RBonus = (int)right->isDefNUseOperand;
126 // Special tie breaker: if two nodes share a operand, the one that
127 // use it as a def&use operand is preferred.
128 if (left->isTwoAddress && !right->isTwoAddress) {
129 SDNode *DUNode = left->Node->getOperand(0).Val;
130 if (DUNode->isOperand(right->Node))
133 if (!left->isTwoAddress && right->isTwoAddress) {
134 SDNode *DUNode = right->Node->getOperand(0).Val;
135 if (DUNode->isOperand(left->Node))
139 // Priority1 is just the number of live range genned.
140 int LPriority1 = left ->NumPredsLeft - LBonus;
141 int RPriority1 = right->NumPredsLeft - RBonus;
142 int LPriority2 = left ->SethiUllman + LBonus;
143 int RPriority2 = right->SethiUllman + RBonus;
145 if (LPriority1 > RPriority1)
147 else if (LPriority1 == RPriority1)
148 if (LPriority2 < RPriority2)
150 else if (LPriority2 == RPriority2)
151 if (left->CycleBound > right->CycleBound)
159 /// ScheduleDAGList - List scheduler.
160 class ScheduleDAGList : public ScheduleDAG {
162 // SDNode to SUnit mapping (many to one).
163 std::map<SDNode*, SUnit*> SUnitMap;
164 // The schedule. Null SUnit*'s represent noop instructions.
165 std::vector<SUnit*> Sequence;
166 // Current scheduling cycle.
168 // First and last SUnit created.
169 SUnit *HeadSUnit, *TailSUnit;
171 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
175 /// HazardRec - The hazard recognizer to use.
176 HazardRecognizer &HazardRec;
178 typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort>
182 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
183 const TargetMachine &tm, bool isbottomup,
184 HazardRecognizer &HR)
185 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
186 CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL), isBottomUp(isbottomup),
191 SUnit *SU = HeadSUnit;
193 SUnit *NextSU = SU->Next;
204 SUnit *NewSUnit(SDNode *N);
205 void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false);
206 void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false);
207 void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU);
208 void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU);
209 int CalcNodePriority(SUnit *SU);
210 void CalculatePriorities();
211 void ListScheduleTopDown();
212 void ListScheduleBottomUp();
213 void BuildSchedUnits();
218 HazardRecognizer::~HazardRecognizer() {}
221 /// NewSUnit - Creates a new SUnit and return a ptr to it.
222 SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
223 SUnit *CurrSUnit = new SUnit(N);
225 if (HeadSUnit == NULL)
226 HeadSUnit = CurrSUnit;
227 if (TailSUnit != NULL)
228 TailSUnit->Next = CurrSUnit;
229 TailSUnit = CurrSUnit;
234 /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
235 /// the Available queue is the count reaches zero. Also update its cycle bound.
236 void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available,
237 SUnit *PredSU, bool isChain) {
238 // FIXME: the distance between two nodes is not always == the predecessor's
239 // latency. For example, the reader can very well read the register written
240 // by the predecessor later than the issue cycle. It also depends on the
241 // interrupt model (drain vs. freeze).
242 PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency);
245 PredSU->NumSuccsLeft--;
247 PredSU->NumChainSuccsLeft--;
250 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
251 std::cerr << "*** List scheduling failed! ***\n";
253 std::cerr << " has been released too many times!\n";
258 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
259 // EntryToken has to go last! Special case it here.
260 if (PredSU->Node->getOpcode() != ISD::EntryToken)
261 Available.push(PredSU);
265 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
266 /// the Available queue is the count reaches zero. Also update its cycle bound.
267 void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available,
268 SUnit *SuccSU, bool isChain) {
269 // FIXME: the distance between two nodes is not always == the predecessor's
270 // latency. For example, the reader can very well read the register written
271 // by the predecessor later than the issue cycle. It also depends on the
272 // interrupt model (drain vs. freeze).
273 SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurrCycle + SuccSU->Latency);
276 SuccSU->NumPredsLeft--;
278 SuccSU->NumChainPredsLeft--;
281 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
282 std::cerr << "*** List scheduling failed! ***\n";
284 std::cerr << " has been released too many times!\n";
289 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
290 Available.push(SuccSU);
293 /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
294 /// count of its predecessors. If a predecessor pending count is zero, add it to
295 /// the Available queue.
296 void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available,
298 DEBUG(std::cerr << "*** Scheduling: ");
299 DEBUG(SU->dump(&DAG, false));
301 Sequence.push_back(SU);
302 SU->Slot = CurrCycle;
304 // Bottom up: release predecessors
305 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
306 E1 = SU->Preds.end(); I1 != E1; ++I1) {
307 ReleasePred(Available, *I1);
310 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
311 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
312 ReleasePred(Available, *I2, true);
317 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
318 /// count of its successors. If a successor pending count is zero, add it to
319 /// the Available queue.
320 void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available,
322 DEBUG(std::cerr << "*** Scheduling: ");
323 DEBUG(SU->dump(&DAG, false));
325 Sequence.push_back(SU);
326 SU->Slot = CurrCycle;
328 // Bottom up: release successors.
329 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
330 E1 = SU->Succs.end(); I1 != E1; ++I1) {
331 ReleaseSucc(Available, *I1);
334 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
335 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
336 ReleaseSucc(Available, *I2, true);
341 /// isReady - True if node's lower cycle bound is less or equal to the current
342 /// scheduling cycle. Always true if all nodes have uniform latency 1.
343 static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
344 return SU->CycleBound <= CurrCycle;
347 /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
349 void ScheduleDAGList::ListScheduleBottomUp() {
351 AvailableQueueTy Available;
353 // Add root to Available queue.
354 Available.push(SUnitMap[DAG.getRoot().Val]);
356 // While Available queue is not empty, grab the node with the highest
357 // priority. If it is not ready put it back. Schedule the node.
358 std::vector<SUnit*> NotReady;
359 while (!Available.empty()) {
360 SUnit *CurrNode = Available.top();
363 while (!isReady(CurrNode, CurrCycle)) {
364 NotReady.push_back(CurrNode);
365 CurrNode = Available.top();
369 // Add the nodes that aren't ready back onto the available list.
370 while (!NotReady.empty()) {
371 Available.push(NotReady.back());
375 ScheduleNodeBottomUp(Available, CurrNode);
378 // Add entry node last
379 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
380 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
381 Entry->Slot = CurrCycle;
382 Sequence.push_back(Entry);
385 // Reverse the order if it is bottom up.
386 std::reverse(Sequence.begin(), Sequence.end());
390 // Verify that all SUnits were scheduled.
391 bool AnyNotSched = false;
392 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
393 if (SU->NumSuccsLeft != 0 || SU->NumChainSuccsLeft != 0) {
395 std::cerr << "*** List scheduling failed! ***\n";
397 std::cerr << "has not been scheduled!\n";
401 assert(!AnyNotSched);
405 /// ListScheduleTopDown - The main loop of list scheduling for top-down
407 void ScheduleDAGList::ListScheduleTopDown() {
409 AvailableQueueTy Available;
411 HazardRec.StartBasicBlock();
413 // Emit the entry node first.
414 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
415 ScheduleNodeTopDown(Available, Entry);
416 HazardRec.EmitInstruction(Entry->Node);
418 // All leaves to Available queue.
419 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
420 // It is available if it has no predecessors.
421 if ((SU->Preds.size() + SU->ChainPreds.size()) == 0 && SU != Entry)
425 // While Available queue is not empty, grab the node with the highest
426 // priority. If it is not ready put it back. Schedule the node.
427 std::vector<SUnit*> NotReady;
428 while (!Available.empty()) {
429 SUnit *FoundNode = 0;
431 bool HasNoopHazards = false;
433 SUnit *CurrNode = Available.top();
435 HazardRecognizer::HazardType HT =
436 HazardRec.getHazardType(CurrNode->Node);
437 if (HT == HazardRecognizer::NoHazard) {
438 FoundNode = CurrNode;
442 // Remember if this is a noop hazard.
443 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
445 NotReady.push_back(CurrNode);
446 } while (!Available.empty());
448 // Add the nodes that aren't ready back onto the available list.
449 while (!NotReady.empty()) {
450 Available.push(NotReady.back());
454 // If we found a node to schedule, do it now.
456 ScheduleNodeTopDown(Available, FoundNode);
457 HazardRec.EmitInstruction(FoundNode->Node);
458 } else if (!HasNoopHazards) {
459 // Otherwise, we have a pipeline stall, but no other problem, just advance
460 // the current cycle and try again.
461 DEBUG(std::cerr << "*** Advancing cycle, no work to do");
462 HazardRec.AdvanceCycle();
465 // Otherwise, we have no instructions to issue and we have instructions
466 // that will fault if we don't do this right. This is the case for
467 // processors without pipeline interlocks and other cases.
468 DEBUG(std::cerr << "*** Emitting noop");
469 HazardRec.EmitNoop();
470 Sequence.push_back(0); // NULL SUnit* -> noop
476 // Verify that all SUnits were scheduled.
477 bool AnyNotSched = false;
478 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
479 if (SU->NumPredsLeft != 0 || SU->NumChainPredsLeft != 0) {
481 std::cerr << "*** List scheduling failed! ***\n";
483 std::cerr << "has not been scheduled!\n";
487 assert(!AnyNotSched);
492 /// CalcNodePriority - Priority is the Sethi Ullman number.
493 /// Smaller number is the higher priority.
494 int ScheduleDAGList::CalcNodePriority(SUnit *SU) {
495 if (SU->SethiUllman != INT_MIN)
496 return SU->SethiUllman;
498 if (SU->Preds.size() == 0) {
502 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
503 E = SU->Preds.end(); I != E; ++I) {
505 int PredSethiUllman = CalcNodePriority(PredSU);
506 if (PredSethiUllman > SU->SethiUllman) {
507 SU->SethiUllman = PredSethiUllman;
509 } else if (PredSethiUllman == SU->SethiUllman)
513 if (SU->Node->getOpcode() != ISD::TokenFactor)
514 SU->SethiUllman += Extra;
516 SU->SethiUllman = (Extra == 1) ? 0 : Extra-1;
519 return SU->SethiUllman;
522 /// CalculatePriorities - Calculate priorities of all scheduling units.
523 void ScheduleDAGList::CalculatePriorities() {
524 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
525 // FIXME: assumes uniform latency for now.
527 (void)CalcNodePriority(SU);
528 DEBUG(SU->dump(&DAG));
529 DEBUG(std::cerr << "\n");
533 void ScheduleDAGList::BuildSchedUnits() {
534 // Pass 1: create the SUnit's.
535 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
536 NodeInfo *NI = &Info[i];
537 SDNode *N = NI->Node;
538 if (isPassiveNode(N))
542 if (NI->isInGroup()) {
543 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
544 continue; // node of the NodeGroup
547 // Find the flagged nodes.
548 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
549 SDNode *Flag = FlagOp.Val;
550 unsigned ResNo = FlagOp.ResNo;
551 while (Flag->getValueType(ResNo) == MVT::Flag) {
552 NodeInfo *FNI = getNI(Flag);
553 assert(FNI->Group == NI->Group);
554 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
557 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
559 ResNo = FlagOp.ResNo;
567 // Pass 2: add the preds, succs, etc.
568 for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) {
569 SDNode *N = SU->Node;
570 NodeInfo *NI = getNI(N);
572 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
573 SU->isTwoAddress = true;
575 if (NI->isInGroup()) {
576 // Find all predecessors (of the group).
577 NodeGroupOpIterator NGOI(NI);
578 while (!NGOI.isEnd()) {
579 SDOperand Op = NGOI.next();
580 SDNode *OpN = Op.Val;
581 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
582 NodeInfo *OpNI = getNI(OpN);
583 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
584 assert(VT != MVT::Flag);
585 SUnit *OpSU = SUnitMap[OpN];
586 if (VT == MVT::Other) {
587 if (SU->ChainPreds.insert(OpSU).second)
588 SU->NumChainPredsLeft++;
589 if (OpSU->ChainSuccs.insert(SU).second)
590 OpSU->NumChainSuccsLeft++;
592 if (SU->Preds.insert(OpSU).second)
594 if (OpSU->Succs.insert(SU).second)
595 OpSU->NumSuccsLeft++;
600 // Find node predecessors.
601 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
602 SDOperand Op = N->getOperand(j);
603 SDNode *OpN = Op.Val;
604 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
605 if (!isPassiveNode(OpN)) {
606 assert(VT != MVT::Flag);
607 SUnit *OpSU = SUnitMap[OpN];
608 if (VT == MVT::Other) {
609 if (SU->ChainPreds.insert(OpSU).second)
610 SU->NumChainPredsLeft++;
611 if (OpSU->ChainSuccs.insert(SU).second)
612 OpSU->NumChainSuccsLeft++;
614 if (SU->Preds.insert(OpSU).second)
616 if (OpSU->Succs.insert(SU).second)
617 OpSU->NumSuccsLeft++;
618 if (j == 0 && SU->isTwoAddress)
619 OpSU->isDefNUseOperand = true;
627 /// EmitSchedule - Emit the machine code in scheduled order.
628 void ScheduleDAGList::EmitSchedule() {
629 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
630 if (SUnit *SU = Sequence[i]) {
631 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
632 SDNode *N = SU->FlaggedNodes[j];
635 EmitNode(getNI(SU->Node));
637 // Null SUnit* is a noop.
643 /// dump - dump the schedule.
644 void ScheduleDAGList::dump() const {
645 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
646 if (SUnit *SU = Sequence[i])
647 SU->dump(&DAG, false);
649 std::cerr << "**** NOOP ****\n";
653 /// Schedule - Schedule the DAG using list scheduling.
654 /// FIXME: Right now it only supports the burr (bottom up register reducing)
656 void ScheduleDAGList::Schedule() {
657 DEBUG(std::cerr << "********** List Scheduling **********\n");
659 // Build scheduling units.
662 // Calculate node priorities.
663 CalculatePriorities();
665 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
667 ListScheduleBottomUp();
669 ListScheduleTopDown();
671 DEBUG(std::cerr << "*** Final schedule ***\n");
673 DEBUG(std::cerr << "\n");
675 // Emit in scheduled order
679 llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
680 MachineBasicBlock *BB) {
682 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true, HR);
685 /// createTDListDAGScheduler - This creates a top-down list scheduler with the
686 /// specified hazard recognizer.
687 ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
688 MachineBasicBlock *BB,
689 HazardRecognizer &HR) {
690 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, HR);