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 bottom-up and top-down list schedulers, using standard
11 // algorithms. The basic approach uses a priority queue of available nodes to
12 // schedule. One at a time, nodes are taken from the priority queue (thus in
13 // priority order), checked for legality to schedule, and emitted if legal.
15 // Nodes may not be legal to schedule either due to structural hazards (e.g.
16 // pipeline or resource constraints) or because an input to the instruction has
17 // not completed execution.
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "sched"
22 #include "llvm/CodeGen/ScheduleDAG.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/CommandLine.h"
36 // FIXME: UseLatencies is temporary.
37 cl::opt<bool> UseLatencies("use-sched-latencies");
38 Statistic<> NumNoops ("scheduler", "Number of noops inserted");
39 Statistic<> NumStalls("scheduler", "Number of pipeline stalls");
41 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
42 /// a group of nodes flagged together.
44 SDNode *Node; // Representative node.
45 std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node.
46 std::set<SUnit*> Preds; // All real predecessors.
47 std::set<SUnit*> ChainPreds; // All chain predecessors.
48 std::set<SUnit*> Succs; // All real successors.
49 std::set<SUnit*> ChainSuccs; // All chain successors.
50 short NumPredsLeft; // # of preds not scheduled.
51 short NumSuccsLeft; // # of succs not scheduled.
52 short NumChainPredsLeft; // # of chain preds not scheduled.
53 short NumChainSuccsLeft; // # of chain succs not scheduled.
54 bool isTwoAddress : 1; // Is a two-address instruction.
55 bool isDefNUseOperand : 1; // Is a def&use operand.
56 unsigned short Latency; // Node latency.
57 unsigned CycleBound; // Upper/lower cycle to be scheduled at.
58 unsigned NodeNum; // Entry # of node in the node vector.
60 SUnit(SDNode *node, unsigned nodenum)
61 : Node(node), NumPredsLeft(0), NumSuccsLeft(0),
62 NumChainPredsLeft(0), NumChainSuccsLeft(0),
63 isTwoAddress(false), isDefNUseOperand(false),
64 Latency(0), CycleBound(0), NodeNum(nodenum) {}
66 void dump(const SelectionDAG *G) const;
67 void dumpAll(const SelectionDAG *G) const;
71 void SUnit::dump(const SelectionDAG *G) const {
75 if (FlaggedNodes.size() != 0) {
76 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
78 FlaggedNodes[i]->dump(G);
84 void SUnit::dumpAll(const SelectionDAG *G) const {
87 std::cerr << " # preds left : " << NumPredsLeft << "\n";
88 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
89 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
90 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
91 std::cerr << " Latency : " << Latency << "\n";
93 if (Preds.size() != 0) {
94 std::cerr << " Predecessors:\n";
95 for (std::set<SUnit*>::const_iterator I = Preds.begin(),
96 E = Preds.end(); I != E; ++I) {
101 if (ChainPreds.size() != 0) {
102 std::cerr << " Chained Preds:\n";
103 for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(),
104 E = ChainPreds.end(); I != E; ++I) {
109 if (Succs.size() != 0) {
110 std::cerr << " Successors:\n";
111 for (std::set<SUnit*>::const_iterator I = Succs.begin(),
112 E = Succs.end(); I != E; ++I) {
117 if (ChainSuccs.size() != 0) {
118 std::cerr << " Chained succs:\n";
119 for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(),
120 E = ChainSuccs.end(); I != E; ++I) {
128 //===----------------------------------------------------------------------===//
129 /// SchedulingPriorityQueue - This interface is used to plug different
130 /// priorities computation algorithms into the list scheduler. It implements the
131 /// interface of a standard priority queue, where nodes are inserted in
132 /// arbitrary order and returned in priority order. The computation of the
133 /// priority and the representation of the queue are totally up to the
134 /// implementation to decide.
137 class SchedulingPriorityQueue {
139 virtual ~SchedulingPriorityQueue() {}
141 virtual void initNodes(const std::vector<SUnit> &SUnits) = 0;
142 virtual void releaseState() = 0;
144 virtual bool empty() const = 0;
145 virtual void push(SUnit *U) = 0;
146 virtual SUnit *pop() = 0;
153 //===----------------------------------------------------------------------===//
154 /// ScheduleDAGList - The actual list scheduler implementation. This supports
155 /// both top-down and bottom-up scheduling.
157 class ScheduleDAGList : public ScheduleDAG {
159 // SDNode to SUnit mapping (many to one).
160 std::map<SDNode*, SUnit*> SUnitMap;
161 // The schedule. Null SUnit*'s represent noop instructions.
162 std::vector<SUnit*> Sequence;
163 // Current scheduling cycle.
166 // The scheduling units.
167 std::vector<SUnit> SUnits;
169 /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
173 /// PriorityQueue - The priority queue to use.
174 SchedulingPriorityQueue *PriorityQueue;
176 /// HazardRec - The hazard recognizer to use.
177 HazardRecognizer *HazardRec;
180 ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb,
181 const TargetMachine &tm, bool isbottomup,
182 SchedulingPriorityQueue *priorityqueue,
183 HazardRecognizer *HR)
184 : ScheduleDAG(listSchedulingBURR, dag, bb, tm),
185 CurrCycle(0), isBottomUp(isbottomup),
186 PriorityQueue(priorityqueue), HazardRec(HR) {
191 delete PriorityQueue;
196 void dumpSchedule() const;
199 SUnit *NewSUnit(SDNode *N);
200 void ReleasePred(SUnit *PredSU, bool isChain = false);
201 void ReleaseSucc(SUnit *SuccSU, bool isChain = false);
202 void ScheduleNodeBottomUp(SUnit *SU);
203 void ScheduleNodeTopDown(SUnit *SU);
204 void ListScheduleTopDown();
205 void ListScheduleBottomUp();
206 void BuildSchedUnits();
209 } // end anonymous namespace
211 HazardRecognizer::~HazardRecognizer() {}
214 /// NewSUnit - Creates a new SUnit and return a ptr to it.
215 SUnit *ScheduleDAGList::NewSUnit(SDNode *N) {
216 SUnits.push_back(SUnit(N, SUnits.size()));
217 return &SUnits.back();
220 /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
221 /// the Available queue is the count reaches zero. Also update its cycle bound.
222 void ScheduleDAGList::ReleasePred(SUnit *PredSU, bool isChain) {
223 // FIXME: the distance between two nodes is not always == the predecessor's
224 // latency. For example, the reader can very well read the register written
225 // by the predecessor later than the issue cycle. It also depends on the
226 // interrupt model (drain vs. freeze).
227 PredSU->CycleBound = std::max(PredSU->CycleBound,CurrCycle + PredSU->Latency);
230 PredSU->NumSuccsLeft--;
232 PredSU->NumChainSuccsLeft--;
235 if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
236 std::cerr << "*** List scheduling failed! ***\n";
238 std::cerr << " has been released too many times!\n";
243 if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
244 // EntryToken has to go last! Special case it here.
245 if (PredSU->Node->getOpcode() != ISD::EntryToken)
246 PriorityQueue->push(PredSU);
250 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
251 /// the Available queue is the count reaches zero. Also update its cycle bound.
252 void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) {
253 // FIXME: the distance between two nodes is not always == the predecessor's
254 // latency. For example, the reader can very well read the register written
255 // by the predecessor later than the issue cycle. It also depends on the
256 // interrupt model (drain vs. freeze).
257 SuccSU->CycleBound = std::max(SuccSU->CycleBound,CurrCycle + SuccSU->Latency);
260 SuccSU->NumPredsLeft--;
262 SuccSU->NumChainPredsLeft--;
265 if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
266 std::cerr << "*** List scheduling failed! ***\n";
268 std::cerr << " has been released too many times!\n";
273 if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0)
274 PriorityQueue->push(SuccSU);
277 /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
278 /// count of its predecessors. If a predecessor pending count is zero, add it to
279 /// the Available queue.
280 void ScheduleDAGList::ScheduleNodeBottomUp(SUnit *SU) {
281 DEBUG(std::cerr << "*** Scheduling: ");
282 DEBUG(SU->dump(&DAG));
284 Sequence.push_back(SU);
286 // Bottom up: release predecessors
287 for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(),
288 E1 = SU->Preds.end(); I1 != E1; ++I1) {
292 for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(),
293 E2 = SU->ChainPreds.end(); I2 != E2; ++I2)
294 ReleasePred(*I2, true);
299 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
300 /// count of its successors. If a successor pending count is zero, add it to
301 /// the Available queue.
302 void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU) {
303 DEBUG(std::cerr << "*** Scheduling: ");
304 DEBUG(SU->dump(&DAG));
306 Sequence.push_back(SU);
308 // Bottom up: release successors.
309 for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(),
310 E1 = SU->Succs.end(); I1 != E1; ++I1) {
314 for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(),
315 E2 = SU->ChainSuccs.end(); I2 != E2; ++I2)
316 ReleaseSucc(*I2, true);
321 /// isReady - True if node's lower cycle bound is less or equal to the current
322 /// scheduling cycle. Always true if all nodes have uniform latency 1.
323 static inline bool isReady(SUnit *SU, unsigned CurrCycle) {
324 return SU->CycleBound <= CurrCycle;
327 /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
329 void ScheduleDAGList::ListScheduleBottomUp() {
330 // Add root to Available queue.
331 PriorityQueue->push(SUnitMap[DAG.getRoot().Val]);
333 // While Available queue is not empty, grab the node with the highest
334 // priority. If it is not ready put it back. Schedule the node.
335 std::vector<SUnit*> NotReady;
336 while (!PriorityQueue->empty()) {
337 SUnit *CurrNode = PriorityQueue->pop();
339 while (!isReady(CurrNode, CurrCycle)) {
340 NotReady.push_back(CurrNode);
341 CurrNode = PriorityQueue->pop();
344 // Add the nodes that aren't ready back onto the available list.
345 while (!NotReady.empty()) {
346 PriorityQueue->push(NotReady.back());
350 ScheduleNodeBottomUp(CurrNode);
353 // Add entry node last
354 if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
355 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
356 Sequence.push_back(Entry);
359 // Reverse the order if it is bottom up.
360 std::reverse(Sequence.begin(), Sequence.end());
364 // Verify that all SUnits were scheduled.
365 bool AnyNotSched = false;
366 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
367 if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
369 std::cerr << "*** List scheduling failed! ***\n";
370 SUnits[i].dump(&DAG);
371 std::cerr << "has not been scheduled!\n";
375 assert(!AnyNotSched);
379 /// ListScheduleTopDown - The main loop of list scheduling for top-down
381 void ScheduleDAGList::ListScheduleTopDown() {
382 // Emit the entry node first.
383 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
384 ScheduleNodeTopDown(Entry);
385 HazardRec->EmitInstruction(Entry->Node);
387 // All leaves to Available queue.
388 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
389 // It is available if it has no predecessors.
390 if ((SUnits[i].Preds.size() + SUnits[i].ChainPreds.size()) == 0 &&
392 PriorityQueue->push(&SUnits[i]);
395 // While Available queue is not empty, grab the node with the highest
396 // priority. If it is not ready put it back. Schedule the node.
397 std::vector<SUnit*> NotReady;
398 while (!PriorityQueue->empty()) {
399 SUnit *FoundNode = 0;
401 bool HasNoopHazards = false;
403 SUnit *CurNode = PriorityQueue->pop();
405 // Get the node represented by this SUnit.
406 SDNode *N = CurNode->Node;
407 // If this is a pseudo op, like copyfromreg, look to see if there is a
408 // real target node flagged to it. If so, use the target node.
409 for (unsigned i = 0, e = CurNode->FlaggedNodes.size();
410 N->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i)
411 N = CurNode->FlaggedNodes[i];
413 HazardRecognizer::HazardType HT = HazardRec->getHazardType(N);
414 if (HT == HazardRecognizer::NoHazard) {
419 // Remember if this is a noop hazard.
420 HasNoopHazards |= HT == HazardRecognizer::NoopHazard;
422 NotReady.push_back(CurNode);
423 } while (!PriorityQueue->empty());
425 // Add the nodes that aren't ready back onto the available list.
426 while (!NotReady.empty()) {
427 PriorityQueue->push(NotReady.back());
431 // If we found a node to schedule, do it now.
433 ScheduleNodeTopDown(FoundNode);
434 HazardRec->EmitInstruction(FoundNode->Node);
435 } else if (!HasNoopHazards) {
436 // Otherwise, we have a pipeline stall, but no other problem, just advance
437 // the current cycle and try again.
438 DEBUG(std::cerr << "*** Advancing cycle, no work to do\n");
439 HazardRec->AdvanceCycle();
442 // Otherwise, we have no instructions to issue and we have instructions
443 // that will fault if we don't do this right. This is the case for
444 // processors without pipeline interlocks and other cases.
445 DEBUG(std::cerr << "*** Emitting noop\n");
446 HazardRec->EmitNoop();
447 Sequence.push_back(0); // NULL SUnit* -> noop
453 // Verify that all SUnits were scheduled.
454 bool AnyNotSched = false;
455 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
456 if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) {
458 std::cerr << "*** List scheduling failed! ***\n";
459 SUnits[i].dump(&DAG);
460 std::cerr << "has not been scheduled!\n";
464 assert(!AnyNotSched);
469 void ScheduleDAGList::BuildSchedUnits() {
470 // Reserve entries in the vector for each of the SUnits we are creating. This
471 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
473 SUnits.reserve(NodeCount);
475 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
477 // Pass 1: create the SUnit's.
478 for (unsigned i = 0, NC = NodeCount; i < NC; i++) {
479 NodeInfo *NI = &Info[i];
480 SDNode *N = NI->Node;
481 if (isPassiveNode(N))
485 if (NI->isInGroup()) {
486 if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom
487 continue; // node of the NodeGroup
490 // Find the flagged nodes.
491 SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1);
492 SDNode *Flag = FlagOp.Val;
493 unsigned ResNo = FlagOp.ResNo;
494 while (Flag->getValueType(ResNo) == MVT::Flag) {
495 NodeInfo *FNI = getNI(Flag);
496 assert(FNI->Group == NI->Group);
497 SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag);
500 FlagOp = Flag->getOperand(Flag->getNumOperands() - 1);
502 ResNo = FlagOp.ResNo;
509 // Compute the latency for the node. We use the sum of the latencies for
510 // all nodes flagged together into this SUnit.
511 if (InstrItins.isEmpty() || !UseLatencies) {
512 // No latency information.
516 if (N->isTargetOpcode()) {
517 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
518 InstrStage *S = InstrItins.begin(SchedClass);
519 InstrStage *E = InstrItins.end(SchedClass);
521 SU->Latency += S->Cycles;
523 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
524 SDNode *FNode = SU->FlaggedNodes[i];
525 if (FNode->isTargetOpcode()) {
526 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
527 InstrStage *S = InstrItins.begin(SchedClass);
528 InstrStage *E = InstrItins.end(SchedClass);
530 SU->Latency += S->Cycles;
536 // Pass 2: add the preds, succs, etc.
537 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
538 SUnit *SU = &SUnits[i];
539 SDNode *N = SU->Node;
540 NodeInfo *NI = getNI(N);
542 if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode()))
543 SU->isTwoAddress = true;
545 if (NI->isInGroup()) {
546 // Find all predecessors (of the group).
547 NodeGroupOpIterator NGOI(NI);
548 while (!NGOI.isEnd()) {
549 SDOperand Op = NGOI.next();
550 SDNode *OpN = Op.Val;
551 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
552 NodeInfo *OpNI = getNI(OpN);
553 if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) {
554 assert(VT != MVT::Flag);
555 SUnit *OpSU = SUnitMap[OpN];
556 if (VT == MVT::Other) {
557 if (SU->ChainPreds.insert(OpSU).second)
558 SU->NumChainPredsLeft++;
559 if (OpSU->ChainSuccs.insert(SU).second)
560 OpSU->NumChainSuccsLeft++;
562 if (SU->Preds.insert(OpSU).second)
564 if (OpSU->Succs.insert(SU).second)
565 OpSU->NumSuccsLeft++;
570 // Find node predecessors.
571 for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) {
572 SDOperand Op = N->getOperand(j);
573 SDNode *OpN = Op.Val;
574 MVT::ValueType VT = OpN->getValueType(Op.ResNo);
575 if (!isPassiveNode(OpN)) {
576 assert(VT != MVT::Flag);
577 SUnit *OpSU = SUnitMap[OpN];
578 if (VT == MVT::Other) {
579 if (SU->ChainPreds.insert(OpSU).second)
580 SU->NumChainPredsLeft++;
581 if (OpSU->ChainSuccs.insert(SU).second)
582 OpSU->NumChainSuccsLeft++;
584 if (SU->Preds.insert(OpSU).second)
586 if (OpSU->Succs.insert(SU).second)
587 OpSU->NumSuccsLeft++;
588 if (j == 0 && SU->isTwoAddress)
589 OpSU->isDefNUseOperand = true;
595 DEBUG(SU->dumpAll(&DAG));
599 /// EmitSchedule - Emit the machine code in scheduled order.
600 void ScheduleDAGList::EmitSchedule() {
601 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
602 if (SUnit *SU = Sequence[i]) {
603 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) {
604 SDNode *N = SU->FlaggedNodes[j];
607 EmitNode(getNI(SU->Node));
609 // Null SUnit* is a noop.
615 /// dump - dump the schedule.
616 void ScheduleDAGList::dumpSchedule() const {
617 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
618 if (SUnit *SU = Sequence[i])
621 std::cerr << "**** NOOP ****\n";
625 /// Schedule - Schedule the DAG using list scheduling.
626 /// FIXME: Right now it only supports the burr (bottom up register reducing)
628 void ScheduleDAGList::Schedule() {
629 DEBUG(std::cerr << "********** List Scheduling **********\n");
631 // Build scheduling units.
634 PriorityQueue->initNodes(SUnits);
636 // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
638 ListScheduleBottomUp();
640 ListScheduleTopDown();
642 PriorityQueue->releaseState();
644 DEBUG(std::cerr << "*** Final schedule ***\n");
645 DEBUG(dumpSchedule());
646 DEBUG(std::cerr << "\n");
648 // Emit in scheduled order
652 //===----------------------------------------------------------------------===//
653 // RegReductionPriorityQueue Implementation
654 //===----------------------------------------------------------------------===//
656 // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
657 // to reduce register pressure.
660 class RegReductionPriorityQueue;
662 /// Sorting functions for the Available queue.
663 struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
664 RegReductionPriorityQueue *SPQ;
665 ls_rr_sort(RegReductionPriorityQueue *spq) : SPQ(spq) {}
666 ls_rr_sort(const ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
668 bool operator()(const SUnit* left, const SUnit* right) const;
670 } // end anonymous namespace
673 class RegReductionPriorityQueue : public SchedulingPriorityQueue {
674 // SUnits - The SUnits for the current graph.
675 const std::vector<SUnit> *SUnits;
677 // SethiUllmanNumbers - The SethiUllman number for each node.
678 std::vector<int> SethiUllmanNumbers;
680 std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Queue;
682 RegReductionPriorityQueue() : Queue(ls_rr_sort(this)) {
685 void initNodes(const std::vector<SUnit> &sunits) {
687 // Calculate node priorities.
688 CalculatePriorities();
690 void releaseState() {
692 SethiUllmanNumbers.clear();
695 unsigned getSethiUllmanNumber(unsigned NodeNum) const {
696 assert(NodeNum < SethiUllmanNumbers.size());
697 return SethiUllmanNumbers[NodeNum];
700 bool empty() const { return Queue.empty(); }
702 void push(SUnit *U) {
706 SUnit *V = Queue.top();
711 void CalculatePriorities();
712 int CalcNodePriority(const SUnit *SU);
716 bool ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
717 unsigned LeftNum = left->NodeNum;
718 unsigned RightNum = right->NodeNum;
720 int LBonus = (int)left ->isDefNUseOperand;
721 int RBonus = (int)right->isDefNUseOperand;
723 // Special tie breaker: if two nodes share a operand, the one that
724 // use it as a def&use operand is preferred.
725 if (left->isTwoAddress && !right->isTwoAddress) {
726 SDNode *DUNode = left->Node->getOperand(0).Val;
727 if (DUNode->isOperand(right->Node))
730 if (!left->isTwoAddress && right->isTwoAddress) {
731 SDNode *DUNode = right->Node->getOperand(0).Val;
732 if (DUNode->isOperand(left->Node))
736 // Priority1 is just the number of live range genned.
737 int LPriority1 = left ->NumPredsLeft - LBonus;
738 int RPriority1 = right->NumPredsLeft - RBonus;
739 int LPriority2 = SPQ->getSethiUllmanNumber(LeftNum) + LBonus;
740 int RPriority2 = SPQ->getSethiUllmanNumber(RightNum) + RBonus;
742 if (LPriority1 > RPriority1)
744 else if (LPriority1 == RPriority1)
745 if (LPriority2 < RPriority2)
747 else if (LPriority2 == RPriority2)
748 if (left->CycleBound > right->CycleBound)
755 /// CalcNodePriority - Priority is the Sethi Ullman number.
756 /// Smaller number is the higher priority.
757 int RegReductionPriorityQueue::CalcNodePriority(const SUnit *SU) {
758 int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
759 if (SethiUllmanNumber != INT_MIN)
760 return SethiUllmanNumber;
762 if (SU->Preds.size() == 0) {
763 SethiUllmanNumber = 1;
766 for (std::set<SUnit*>::iterator I = SU->Preds.begin(),
767 E = SU->Preds.end(); I != E; ++I) {
769 int PredSethiUllman = CalcNodePriority(PredSU);
770 if (PredSethiUllman > SethiUllmanNumber) {
771 SethiUllmanNumber = PredSethiUllman;
773 } else if (PredSethiUllman == SethiUllmanNumber)
777 if (SU->Node->getOpcode() != ISD::TokenFactor)
778 SethiUllmanNumber += Extra;
780 SethiUllmanNumber = (Extra == 1) ? 0 : Extra-1;
783 return SethiUllmanNumber;
786 /// CalculatePriorities - Calculate priorities of all scheduling units.
787 void RegReductionPriorityQueue::CalculatePriorities() {
788 SethiUllmanNumbers.assign(SUnits->size(), INT_MIN);
790 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
791 CalcNodePriority(&(*SUnits)[i]);
794 //===----------------------------------------------------------------------===//
795 // LatencyPriorityQueue Implementation
796 //===----------------------------------------------------------------------===//
798 // This is a SchedulingPriorityQueue that schedules using latency information to
799 // reduce the length of the critical path through the basic block.
802 class LatencyPriorityQueue;
804 /// Sorting functions for the Available queue.
805 struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
806 LatencyPriorityQueue *PQ;
807 latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
808 latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {}
810 bool operator()(const SUnit* left, const SUnit* right) const;
812 } // end anonymous namespace
815 class LatencyPriorityQueue : public SchedulingPriorityQueue {
816 // SUnits - The SUnits for the current graph.
817 const std::vector<SUnit> *SUnits;
819 // Latencies - The latency (max of latency from this node to the bb exit)
821 std::vector<int> Latencies;
823 std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
825 LatencyPriorityQueue() : Queue(latency_sort(this)) {
828 void initNodes(const std::vector<SUnit> &sunits) {
830 // Calculate node priorities.
831 CalculatePriorities();
833 void releaseState() {
838 unsigned getLatency(unsigned NodeNum) const {
839 assert(NodeNum < Latencies.size());
840 return Latencies[NodeNum];
843 bool empty() const { return Queue.empty(); }
845 void push(SUnit *U) {
849 SUnit *V = Queue.top();
852 std::cerr << "Got node. Latency: " << getLatency(V->NodeNum)
857 void CalculatePriorities();
858 int CalcLatency(const SUnit &SU);
862 bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
863 unsigned LHSNum = LHS->NodeNum;
864 unsigned RHSNum = RHS->NodeNum;
866 return PQ->getLatency(LHSNum) < PQ->getLatency(RHSNum);
870 /// CalcNodePriority - Calculate the maximal path from the node to the exit.
872 int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
873 int &Latency = Latencies[SU.NodeNum];
877 int MaxSuccLatency = 0;
878 for (std::set<SUnit*>::iterator I = SU.Succs.begin(),
879 E = SU.Succs.end(); I != E; ++I)
880 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I));
882 for (std::set<SUnit*>::iterator I = SU.ChainSuccs.begin(),
883 E = SU.ChainSuccs.end(); I != E; ++I)
884 MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I));
886 return Latency = MaxSuccLatency + SU.Latency;
889 /// CalculatePriorities - Calculate priorities of all scheduling units.
890 void LatencyPriorityQueue::CalculatePriorities() {
891 Latencies.assign(SUnits->size(), -1);
893 for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
894 CalcLatency((*SUnits)[i]);
898 //===----------------------------------------------------------------------===//
899 // Public Constructor Functions
900 //===----------------------------------------------------------------------===//
902 llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
903 MachineBasicBlock *BB) {
904 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true,
905 new RegReductionPriorityQueue(),
906 new HazardRecognizer());
909 /// createTDListDAGScheduler - This creates a top-down list scheduler with the
910 /// specified hazard recognizer.
911 ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG,
912 MachineBasicBlock *BB,
913 HazardRecognizer *HR) {
914 return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false,
915 new LatencyPriorityQueue(),