blob: 8a6d1f23af142809f7c8c4c97da70d3e8e3cee07 [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"
19using namespace llvm;
20
21bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
22 unsigned LHSNum = LHS->NodeNum;
23 unsigned RHSNum = RHS->NodeNum;
24
25 // The most important heuristic is scheduling the critical path.
26 unsigned LHSLatency = PQ->getLatency(LHSNum);
27 unsigned RHSLatency = PQ->getLatency(RHSNum);
28 if (LHSLatency < RHSLatency) return true;
29 if (LHSLatency > RHSLatency) return false;
30
31 // After that, if two nodes have identical latencies, look to see if one will
32 // unblock more other nodes than the other.
33 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
34 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
35 if (LHSBlocked < RHSBlocked) return true;
36 if (LHSBlocked > RHSBlocked) return false;
37
38 // Finally, just to provide a stable ordering, use the node number as a
39 // deciding factor.
40 return LHSNum < RHSNum;
41}
42
43
44/// CalcNodePriority - Calculate the maximal path from the node to the exit.
45///
46int LatencyPriorityQueue::CalcLatency(const SUnit &SU) {
47 int &Latency = Latencies[SU.NodeNum];
48 if (Latency != -1)
49 return Latency;
50
51 std::vector<const SUnit*> WorkList;
52 WorkList.push_back(&SU);
53 while (!WorkList.empty()) {
54 const SUnit *Cur = WorkList.back();
Dan Gohmanaeac8f92008-12-09 00:26:46 +000055 unsigned CurLatency = Cur->Latency;
Dan Gohmanade9f182008-11-15 00:23:40 +000056 bool AllDone = true;
Dan Gohmanaeac8f92008-12-09 00:26:46 +000057 unsigned MaxSuccLatency = 0;
Dan Gohmanade9f182008-11-15 00:23:40 +000058 for (SUnit::const_succ_iterator I = Cur->Succs.begin(),E = Cur->Succs.end();
59 I != E; ++I) {
60 int SuccLatency = Latencies[I->Dep->NodeNum];
61 if (SuccLatency == -1) {
62 AllDone = false;
63 WorkList.push_back(I->Dep);
64 } else {
Dan Gohmanaeac8f92008-12-09 00:26:46 +000065 // This assumes that there's no delay for reusing registers.
66 unsigned NewLatency =
67 SuccLatency + ((I->isCtrl && I->Reg != 0) ? 1 : CurLatency);
68 MaxSuccLatency = std::max(MaxSuccLatency, NewLatency);
Dan Gohmanade9f182008-11-15 00:23:40 +000069 }
70 }
71 if (AllDone) {
Dan Gohmanaeac8f92008-12-09 00:26:46 +000072 Latencies[Cur->NodeNum] = MaxSuccLatency;
Dan Gohmanade9f182008-11-15 00:23:40 +000073 WorkList.pop_back();
74 }
75 }
76
77 return Latency;
78}
79
80/// CalculatePriorities - Calculate priorities of all scheduling units.
81void LatencyPriorityQueue::CalculatePriorities() {
82 Latencies.assign(SUnits->size(), -1);
83 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
84
85 // For each node, calculate the maximal path from the node to the exit.
86 std::vector<std::pair<const SUnit*, unsigned> > WorkList;
87 for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
88 const SUnit *SU = &(*SUnits)[i];
89 if (SU->Succs.empty())
90 WorkList.push_back(std::make_pair(SU, 0U));
91 }
92
93 while (!WorkList.empty()) {
94 const SUnit *SU = WorkList.back().first;
95 unsigned SuccLat = WorkList.back().second;
96 WorkList.pop_back();
97 int &Latency = Latencies[SU->NodeNum];
98 if (Latency == -1 || (SU->Latency + SuccLat) > (unsigned)Latency) {
99 Latency = SU->Latency + SuccLat;
100 for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end();
101 I != E; ++I)
102 WorkList.push_back(std::make_pair(I->Dep, Latency));
103 }
104 }
105}
106
107/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
108/// of SU, return it, otherwise return null.
109SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
110 SUnit *OnlyAvailablePred = 0;
111 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
112 I != E; ++I) {
113 SUnit &Pred = *I->Dep;
114 if (!Pred.isScheduled) {
115 // We found an available, but not scheduled, predecessor. If it's the
116 // only one we have found, keep track of it... otherwise give up.
117 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
118 return 0;
119 OnlyAvailablePred = &Pred;
120 }
121 }
122
123 return OnlyAvailablePred;
124}
125
126void LatencyPriorityQueue::push_impl(SUnit *SU) {
127 // Look at all of the successors of this node. Count the number of nodes that
128 // this node is the sole unscheduled node for.
129 unsigned NumNodesBlocking = 0;
130 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
131 I != E; ++I)
132 if (getSingleUnscheduledPred(I->Dep) == SU)
133 ++NumNodesBlocking;
134 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
135
136 Queue.push(SU);
137}
138
139
140// ScheduledNode - As nodes are scheduled, we look to see if there are any
141// successor nodes that have a single unscheduled predecessor. If so, that
142// single predecessor has a higher priority, since scheduling it will make
143// the node available.
144void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
145 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
146 I != E; ++I)
147 AdjustPriorityOfUnscheduledPreds(I->Dep);
148}
149
150/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
151/// scheduled. If SU is not itself available, then there is at least one
152/// predecessor node that has not been scheduled yet. If SU has exactly ONE
153/// unscheduled predecessor, we want to increase its priority: it getting
154/// scheduled will make this node available, so it is better than some other
155/// node of the same priority that will not make a node available.
156void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Dan Gohman6560c002008-11-17 16:37:30 +0000157 if (SU->isAvailable) return; // All preds scheduled.
Dan Gohmanade9f182008-11-15 00:23:40 +0000158
159 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
160 if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return;
161
162 // Okay, we found a single predecessor that is available, but not scheduled.
163 // Since it is available, it must be in the priority queue. First remove it.
164 remove(OnlyAvailablePred);
165
166 // Reinsert the node into the priority queue, which recomputes its
167 // NumNodesSolelyBlocking value.
168 push(OnlyAvailablePred);
169}