blob: 86ef898932a7dddc51b4b4239063ef15d914b578 [file] [log] [blame]
Dan Gohmand2760c02008-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
Dan Gohman60cb69e2008-11-19 23:18:57 +000016#include "llvm/CodeGen/LatencyPriorityQueue.h"
Dan Gohmand2760c02008-11-15 00:23:40 +000017#include "llvm/Support/Debug.h"
Andrew Trick10ffc2b2010-12-24 05:03:26 +000018#include "llvm/Support/raw_ostream.h"
Dan Gohmand2760c02008-11-15 00:23:40 +000019using namespace llvm;
20
Chandler Carruth1b9dde02014-04-22 02:02:50 +000021#define DEBUG_TYPE "scheduler"
22
Dan Gohmand2760c02008-11-15 00:23:40 +000023bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
Dan Gohmanb9a01212008-12-16 03:35:01 +000024 // The isScheduleHigh flag allows nodes with wraparound dependencies that
25 // cannot easily be modeled as edges with latencies to be scheduled as
26 // soon as possible in a top-down schedule.
27 if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
28 return false;
29 if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
30 return true;
31
Dan Gohmand2760c02008-11-15 00:23:40 +000032 unsigned LHSNum = LHS->NodeNum;
33 unsigned RHSNum = RHS->NodeNum;
34
35 // The most important heuristic is scheduling the critical path.
36 unsigned LHSLatency = PQ->getLatency(LHSNum);
37 unsigned RHSLatency = PQ->getLatency(RHSNum);
38 if (LHSLatency < RHSLatency) return true;
39 if (LHSLatency > RHSLatency) return false;
Andrew Trickc416ba62010-12-24 04:28:06 +000040
Dan Gohmand2760c02008-11-15 00:23:40 +000041 // After that, if two nodes have identical latencies, look to see if one will
42 // unblock more other nodes than the other.
43 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
44 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
45 if (LHSBlocked < RHSBlocked) return true;
46 if (LHSBlocked > RHSBlocked) return false;
Andrew Trickc416ba62010-12-24 04:28:06 +000047
Dan Gohmand2760c02008-11-15 00:23:40 +000048 // Finally, just to provide a stable ordering, use the node number as a
49 // deciding factor.
Andrew Trick46cc9a42012-02-22 06:08:11 +000050 return RHSNum < LHSNum;
Dan Gohmand2760c02008-11-15 00:23:40 +000051}
52
53
Dan Gohmand2760c02008-11-15 00:23:40 +000054/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
55/// of SU, return it, otherwise return null.
56SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +000057 SUnit *OnlyAvailablePred = nullptr;
Dan Gohmand2760c02008-11-15 00:23:40 +000058 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
59 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +000060 SUnit &Pred = *I->getSUnit();
Dan Gohmand2760c02008-11-15 00:23:40 +000061 if (!Pred.isScheduled) {
62 // We found an available, but not scheduled, predecessor. If it's the
63 // only one we have found, keep track of it... otherwise give up.
64 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Craig Topperc0196b12014-04-14 00:51:57 +000065 return nullptr;
Dan Gohmand2760c02008-11-15 00:23:40 +000066 OnlyAvailablePred = &Pred;
67 }
68 }
Andrew Trickc416ba62010-12-24 04:28:06 +000069
Dan Gohmand2760c02008-11-15 00:23:40 +000070 return OnlyAvailablePred;
71}
72
Dan Gohman7c005762010-05-26 01:10:55 +000073void LatencyPriorityQueue::push(SUnit *SU) {
Dan Gohmand2760c02008-11-15 00:23:40 +000074 // Look at all of the successors of this node. Count the number of nodes that
75 // this node is the sole unscheduled node for.
76 unsigned NumNodesBlocking = 0;
77 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +000078 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +000079 if (getSingleUnscheduledPred(I->getSUnit()) == SU)
Dan Gohmand2760c02008-11-15 00:23:40 +000080 ++NumNodesBlocking;
David Goodwin8501dbbe2009-11-03 20:57:50 +000081 }
Dan Gohmand2760c02008-11-15 00:23:40 +000082 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Andrew Trickc416ba62010-12-24 04:28:06 +000083
Dan Gohman52c27382010-05-26 18:52:00 +000084 Queue.push_back(SU);
Dan Gohmand2760c02008-11-15 00:23:40 +000085}
86
87
Andrew Trick52226d42012-03-07 23:00:49 +000088// scheduledNode - As nodes are scheduled, we look to see if there are any
Dan Gohmand2760c02008-11-15 00:23:40 +000089// successor nodes that have a single unscheduled predecessor. If so, that
90// single predecessor has a higher priority, since scheduling it will make
91// the node available.
Andrew Trick52226d42012-03-07 23:00:49 +000092void LatencyPriorityQueue::scheduledNode(SUnit *SU) {
Dan Gohmand2760c02008-11-15 00:23:40 +000093 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +000094 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +000095 AdjustPriorityOfUnscheduledPreds(I->getSUnit());
David Goodwin8501dbbe2009-11-03 20:57:50 +000096 }
Dan Gohmand2760c02008-11-15 00:23:40 +000097}
98
99/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
100/// scheduled. If SU is not itself available, then there is at least one
101/// predecessor node that has not been scheduled yet. If SU has exactly ONE
102/// unscheduled predecessor, we want to increase its priority: it getting
103/// scheduled will make this node available, so it is better than some other
104/// node of the same priority that will not make a node available.
105void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Dan Gohman17c226b2008-11-17 16:37:30 +0000106 if (SU->isAvailable) return; // All preds scheduled.
Andrew Trickc416ba62010-12-24 04:28:06 +0000107
Dan Gohmand2760c02008-11-15 00:23:40 +0000108 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
Craig Topperc0196b12014-04-14 00:51:57 +0000109 if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return;
Andrew Trickc416ba62010-12-24 04:28:06 +0000110
Dan Gohmand2760c02008-11-15 00:23:40 +0000111 // Okay, we found a single predecessor that is available, but not scheduled.
112 // Since it is available, it must be in the priority queue. First remove it.
113 remove(OnlyAvailablePred);
114
115 // Reinsert the node into the priority queue, which recomputes its
116 // NumNodesSolelyBlocking value.
117 push(OnlyAvailablePred);
118}
Dan Gohman52c27382010-05-26 18:52:00 +0000119
120SUnit *LatencyPriorityQueue::pop() {
Craig Topperc0196b12014-04-14 00:51:57 +0000121 if (empty()) return nullptr;
Dan Gohman52c27382010-05-26 18:52:00 +0000122 std::vector<SUnit *>::iterator Best = Queue.begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000123 for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +0000124 E = Queue.end(); I != E; ++I)
125 if (Picker(*Best, *I))
126 Best = I;
127 SUnit *V = *Best;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000128 if (Best != std::prev(Queue.end()))
Dan Gohman52c27382010-05-26 18:52:00 +0000129 std::swap(*Best, Queue.back());
130 Queue.pop_back();
131 return V;
132}
133
134void LatencyPriorityQueue::remove(SUnit *SU) {
135 assert(!Queue.empty() && "Queue is empty!");
David Majnemer0d955d02016-08-11 22:21:41 +0000136 std::vector<SUnit *>::iterator I = find(Queue, SU);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000137 if (I != std::prev(Queue.end()))
Dan Gohman52c27382010-05-26 18:52:00 +0000138 std::swap(*I, Queue.back());
139 Queue.pop_back();
140}