blob: 0eb009ddac2943c1a5730a23ea92c7457ce97bcb [file] [log] [blame]
Dan Gohmanade9f182008-11-15 00:23:40 +00001//===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LatencyPriorityQueue class, which is a
11// SchedulingPriorityQueue that schedules using latency information to
12// reduce the length of the critical path through the basic block.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "scheduler"
Dan Gohman343f0c02008-11-19 23:18:57 +000017#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohmanade9f182008-11-15 00:23:40 +000018#include "llvm/Support/Debug.h"
Andrew Trick2da8bc82010-12-24 05:03:26 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohmanade9f182008-11-15 00:23:40 +000020using namespace llvm;
21
22bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
Dan Gohman8749b612008-12-16 03:35:01 +000023 // The isScheduleHigh flag allows nodes with wraparound dependencies that
24 // cannot easily be modeled as edges with latencies to be scheduled as
25 // soon as possible in a top-down schedule.
26 if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
27 return false;
28 if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
29 return true;
30
Dan Gohmanade9f182008-11-15 00:23:40 +000031 unsigned LHSNum = LHS->NodeNum;
32 unsigned RHSNum = RHS->NodeNum;
33
34 // The most important heuristic is scheduling the critical path.
35 unsigned LHSLatency = PQ->getLatency(LHSNum);
36 unsigned RHSLatency = PQ->getLatency(RHSNum);
37 if (LHSLatency < RHSLatency) return true;
38 if (LHSLatency > RHSLatency) return false;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000039
Dan Gohmanade9f182008-11-15 00:23:40 +000040 // After that, if two nodes have identical latencies, look to see if one will
41 // unblock more other nodes than the other.
42 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
43 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
44 if (LHSBlocked < RHSBlocked) return true;
45 if (LHSBlocked > RHSBlocked) return false;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000046
Dan Gohmanade9f182008-11-15 00:23:40 +000047 // Finally, just to provide a stable ordering, use the node number as a
48 // deciding factor.
49 return LHSNum < RHSNum;
50}
51
52
Dan Gohmanade9f182008-11-15 00:23:40 +000053/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
54/// of SU, return it, otherwise return null.
55SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
56 SUnit *OnlyAvailablePred = 0;
57 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
58 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +000059 SUnit &Pred = *I->getSUnit();
Dan Gohmanade9f182008-11-15 00:23:40 +000060 if (!Pred.isScheduled) {
61 // We found an available, but not scheduled, predecessor. If it's the
62 // only one we have found, keep track of it... otherwise give up.
63 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
64 return 0;
65 OnlyAvailablePred = &Pred;
66 }
67 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +000068
Dan Gohmanade9f182008-11-15 00:23:40 +000069 return OnlyAvailablePred;
70}
71
Dan Gohmana4e4ffd2010-05-26 01:10:55 +000072void LatencyPriorityQueue::push(SUnit *SU) {
Dan Gohmanade9f182008-11-15 00:23:40 +000073 // Look at all of the successors of this node. Count the number of nodes that
74 // this node is the sole unscheduled node for.
75 unsigned NumNodesBlocking = 0;
76 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +000077 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +000078 if (getSingleUnscheduledPred(I->getSUnit()) == SU)
Dan Gohmanade9f182008-11-15 00:23:40 +000079 ++NumNodesBlocking;
David Goodwin4de099d2009-11-03 20:57:50 +000080 }
Dan Gohmanade9f182008-11-15 00:23:40 +000081 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000082
Dan Gohman93d34332010-05-26 18:52:00 +000083 Queue.push_back(SU);
Dan Gohmanade9f182008-11-15 00:23:40 +000084}
85
86
87// ScheduledNode - As nodes are scheduled, we look to see if there are any
88// successor nodes that have a single unscheduled predecessor. If so, that
89// single predecessor has a higher priority, since scheduling it will make
90// the node available.
91void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
92 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +000093 I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +000094 AdjustPriorityOfUnscheduledPreds(I->getSUnit());
David Goodwin4de099d2009-11-03 20:57:50 +000095 }
Dan Gohmanade9f182008-11-15 00:23:40 +000096}
97
98/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
99/// scheduled. If SU is not itself available, then there is at least one
100/// predecessor node that has not been scheduled yet. If SU has exactly ONE
101/// unscheduled predecessor, we want to increase its priority: it getting
102/// scheduled will make this node available, so it is better than some other
103/// node of the same priority that will not make a node available.
104void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Dan Gohman6560c002008-11-17 16:37:30 +0000105 if (SU->isAvailable) return; // All preds scheduled.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000106
Dan Gohmanade9f182008-11-15 00:23:40 +0000107 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
108 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000109
Dan Gohmanade9f182008-11-15 00:23:40 +0000110 // Okay, we found a single predecessor that is available, but not scheduled.
111 // Since it is available, it must be in the priority queue. First remove it.
112 remove(OnlyAvailablePred);
113
114 // Reinsert the node into the priority queue, which recomputes its
115 // NumNodesSolelyBlocking value.
116 push(OnlyAvailablePred);
117}
Dan Gohman93d34332010-05-26 18:52:00 +0000118
119SUnit *LatencyPriorityQueue::pop() {
120 if (empty()) return NULL;
121 std::vector<SUnit *>::iterator Best = Queue.begin();
Oscar Fuentes10e02a02010-05-30 13:14:21 +0000122 for (std::vector<SUnit *>::iterator I = llvm::next(Queue.begin()),
Dan Gohman93d34332010-05-26 18:52:00 +0000123 E = Queue.end(); I != E; ++I)
124 if (Picker(*Best, *I))
125 Best = I;
126 SUnit *V = *Best;
127 if (Best != prior(Queue.end()))
128 std::swap(*Best, Queue.back());
129 Queue.pop_back();
130 return V;
131}
132
133void LatencyPriorityQueue::remove(SUnit *SU) {
134 assert(!Queue.empty() && "Queue is empty!");
135 std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(), SU);
136 if (I != prior(Queue.end()))
137 std::swap(*I, Queue.back());
138 Queue.pop_back();
139}
Andrew Trick2da8bc82010-12-24 05:03:26 +0000140
141#ifdef NDEBUG
142void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {}
143#else
144void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {
145 LatencyPriorityQueue q = *this;
146 while (!q.empty()) {
147 SUnit *su = q.pop();
148 dbgs() << "Height " << su->getHeight() << ": ";
149 su->dump(DAG);
150 }
151}
152#endif