blob: f9f33a98a9d1c392f3b145eb6a69362932dbfd58 [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"
Nico Weber432a3882018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Dan Gohmand2760c02008-11-15 00:23:40 +000018#include "llvm/Support/Debug.h"
Andrew Trick10ffc2b2010-12-24 05:03:26 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohmand2760c02008-11-15 00:23:40 +000020using namespace llvm;
21
Chandler Carruth1b9dde02014-04-22 02:02:50 +000022#define DEBUG_TYPE "scheduler"
23
Dan Gohmand2760c02008-11-15 00:23:40 +000024bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
Dan Gohmanb9a01212008-12-16 03:35:01 +000025 // The isScheduleHigh flag allows nodes with wraparound dependencies that
26 // cannot easily be modeled as edges with latencies to be scheduled as
27 // soon as possible in a top-down schedule.
28 if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
29 return false;
30 if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
31 return true;
32
Dan Gohmand2760c02008-11-15 00:23:40 +000033 unsigned LHSNum = LHS->NodeNum;
34 unsigned RHSNum = RHS->NodeNum;
35
36 // The most important heuristic is scheduling the critical path.
37 unsigned LHSLatency = PQ->getLatency(LHSNum);
38 unsigned RHSLatency = PQ->getLatency(RHSNum);
39 if (LHSLatency < RHSLatency) return true;
40 if (LHSLatency > RHSLatency) return false;
Andrew Trickc416ba62010-12-24 04:28:06 +000041
Dan Gohmand2760c02008-11-15 00:23:40 +000042 // After that, if two nodes have identical latencies, look to see if one will
43 // unblock more other nodes than the other.
44 unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
45 unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
46 if (LHSBlocked < RHSBlocked) return true;
47 if (LHSBlocked > RHSBlocked) return false;
Andrew Trickc416ba62010-12-24 04:28:06 +000048
Dan Gohmand2760c02008-11-15 00:23:40 +000049 // Finally, just to provide a stable ordering, use the node number as a
50 // deciding factor.
Andrew Trick46cc9a42012-02-22 06:08:11 +000051 return RHSNum < LHSNum;
Dan Gohmand2760c02008-11-15 00:23:40 +000052}
53
54
Dan Gohmand2760c02008-11-15 00:23:40 +000055/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
56/// of SU, return it, otherwise return null.
57SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +000058 SUnit *OnlyAvailablePred = nullptr;
Dan Gohmand2760c02008-11-15 00:23:40 +000059 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
60 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +000061 SUnit &Pred = *I->getSUnit();
Dan Gohmand2760c02008-11-15 00:23:40 +000062 if (!Pred.isScheduled) {
63 // We found an available, but not scheduled, predecessor. If it's the
64 // only one we have found, keep track of it... otherwise give up.
65 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Craig Topperc0196b12014-04-14 00:51:57 +000066 return nullptr;
Dan Gohmand2760c02008-11-15 00:23:40 +000067 OnlyAvailablePred = &Pred;
68 }
69 }
Andrew Trickc416ba62010-12-24 04:28:06 +000070
Dan Gohmand2760c02008-11-15 00:23:40 +000071 return OnlyAvailablePred;
72}
73
Dan Gohman7c005762010-05-26 01:10:55 +000074void LatencyPriorityQueue::push(SUnit *SU) {
Dan Gohmand2760c02008-11-15 00:23:40 +000075 // Look at all of the successors of this node. Count the number of nodes that
76 // this node is the sole unscheduled node for.
77 unsigned NumNodesBlocking = 0;
78 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +000079 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +000080 if (getSingleUnscheduledPred(I->getSUnit()) == SU)
Dan Gohmand2760c02008-11-15 00:23:40 +000081 ++NumNodesBlocking;
David Goodwin8501dbbe2009-11-03 20:57:50 +000082 }
Dan Gohmand2760c02008-11-15 00:23:40 +000083 NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Andrew Trickc416ba62010-12-24 04:28:06 +000084
Dan Gohman52c27382010-05-26 18:52:00 +000085 Queue.push_back(SU);
Dan Gohmand2760c02008-11-15 00:23:40 +000086}
87
88
Andrew Trick52226d42012-03-07 23:00:49 +000089// scheduledNode - As nodes are scheduled, we look to see if there are any
Dan Gohmand2760c02008-11-15 00:23:40 +000090// successor nodes that have a single unscheduled predecessor. If so, that
91// single predecessor has a higher priority, since scheduling it will make
92// the node available.
Andrew Trick52226d42012-03-07 23:00:49 +000093void LatencyPriorityQueue::scheduledNode(SUnit *SU) {
Dan Gohmand2760c02008-11-15 00:23:40 +000094 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin8501dbbe2009-11-03 20:57:50 +000095 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +000096 AdjustPriorityOfUnscheduledPreds(I->getSUnit());
David Goodwin8501dbbe2009-11-03 20:57:50 +000097 }
Dan Gohmand2760c02008-11-15 00:23:40 +000098}
99
100/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
101/// scheduled. If SU is not itself available, then there is at least one
102/// predecessor node that has not been scheduled yet. If SU has exactly ONE
103/// unscheduled predecessor, we want to increase its priority: it getting
104/// scheduled will make this node available, so it is better than some other
105/// node of the same priority that will not make a node available.
106void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
Dan Gohman17c226b2008-11-17 16:37:30 +0000107 if (SU->isAvailable) return; // All preds scheduled.
Andrew Trickc416ba62010-12-24 04:28:06 +0000108
Dan Gohmand2760c02008-11-15 00:23:40 +0000109 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
Craig Topperc0196b12014-04-14 00:51:57 +0000110 if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return;
Andrew Trickc416ba62010-12-24 04:28:06 +0000111
Dan Gohmand2760c02008-11-15 00:23:40 +0000112 // Okay, we found a single predecessor that is available, but not scheduled.
113 // Since it is available, it must be in the priority queue. First remove it.
114 remove(OnlyAvailablePred);
115
116 // Reinsert the node into the priority queue, which recomputes its
117 // NumNodesSolelyBlocking value.
118 push(OnlyAvailablePred);
119}
Dan Gohman52c27382010-05-26 18:52:00 +0000120
121SUnit *LatencyPriorityQueue::pop() {
Craig Topperc0196b12014-04-14 00:51:57 +0000122 if (empty()) return nullptr;
Dan Gohman52c27382010-05-26 18:52:00 +0000123 std::vector<SUnit *>::iterator Best = Queue.begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000124 for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
Dan Gohman52c27382010-05-26 18:52:00 +0000125 E = Queue.end(); I != E; ++I)
126 if (Picker(*Best, *I))
127 Best = I;
128 SUnit *V = *Best;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000129 if (Best != std::prev(Queue.end()))
Dan Gohman52c27382010-05-26 18:52:00 +0000130 std::swap(*Best, Queue.back());
131 Queue.pop_back();
132 return V;
133}
134
135void LatencyPriorityQueue::remove(SUnit *SU) {
136 assert(!Queue.empty() && "Queue is empty!");
David Majnemer0d955d02016-08-11 22:21:41 +0000137 std::vector<SUnit *>::iterator I = find(Queue, SU);
Benjamin Kramerbd20e972017-11-16 10:18:07 +0000138 assert(I != Queue.end() && "Queue doesn't contain the SU being removed!");
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000139 if (I != std::prev(Queue.end()))
Dan Gohman52c27382010-05-26 18:52:00 +0000140 std::swap(*I, Queue.back());
141 Queue.pop_back();
142}
Amara Emerson6aacbf42018-04-20 00:42:46 +0000143
144#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
145LLVM_DUMP_METHOD void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {
146 dbgs() << "Latency Priority Queue\n";
147 dbgs() << " Number of Queue Entries: " << Queue.size() << "\n";
Matthias Braun726e12c2018-09-19 00:23:35 +0000148 for (const SUnit *SU : Queue) {
Amara Emerson6aacbf42018-04-20 00:42:46 +0000149 dbgs() << " ";
Matthias Braun726e12c2018-09-19 00:23:35 +0000150 DAG->dumpNode(*SU);
Amara Emerson6aacbf42018-04-20 00:42:46 +0000151 }
152}
153#endif