1*0b57cec5SDimitry Andric //===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements the LatencyPriorityQueue class, which is a
10*0b57cec5SDimitry Andric // SchedulingPriorityQueue that schedules using latency information to
11*0b57cec5SDimitry Andric // reduce the length of the critical path through the basic block.
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric
15*0b57cec5SDimitry Andric #include "llvm/CodeGen/LatencyPriorityQueue.h"
16*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
19*0b57cec5SDimitry Andric using namespace llvm;
20*0b57cec5SDimitry Andric
21*0b57cec5SDimitry Andric #define DEBUG_TYPE "scheduler"
22*0b57cec5SDimitry Andric
operator ()(const SUnit * LHS,const SUnit * RHS) const23*0b57cec5SDimitry Andric bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
24*0b57cec5SDimitry Andric // The isScheduleHigh flag allows nodes with wraparound dependencies that
25*0b57cec5SDimitry Andric // cannot easily be modeled as edges with latencies to be scheduled as
26*0b57cec5SDimitry Andric // soon as possible in a top-down schedule.
27*0b57cec5SDimitry Andric if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
28*0b57cec5SDimitry Andric return false;
29*0b57cec5SDimitry Andric if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
30*0b57cec5SDimitry Andric return true;
31*0b57cec5SDimitry Andric
32*0b57cec5SDimitry Andric unsigned LHSNum = LHS->NodeNum;
33*0b57cec5SDimitry Andric unsigned RHSNum = RHS->NodeNum;
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric // The most important heuristic is scheduling the critical path.
36*0b57cec5SDimitry Andric unsigned LHSLatency = PQ->getLatency(LHSNum);
37*0b57cec5SDimitry Andric unsigned RHSLatency = PQ->getLatency(RHSNum);
38*0b57cec5SDimitry Andric if (LHSLatency < RHSLatency) return true;
39*0b57cec5SDimitry Andric if (LHSLatency > RHSLatency) return false;
40*0b57cec5SDimitry Andric
41*0b57cec5SDimitry Andric // After that, if two nodes have identical latencies, look to see if one will
42*0b57cec5SDimitry Andric // unblock more other nodes than the other.
43*0b57cec5SDimitry Andric unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
44*0b57cec5SDimitry Andric unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
45*0b57cec5SDimitry Andric if (LHSBlocked < RHSBlocked) return true;
46*0b57cec5SDimitry Andric if (LHSBlocked > RHSBlocked) return false;
47*0b57cec5SDimitry Andric
48*0b57cec5SDimitry Andric // Finally, just to provide a stable ordering, use the node number as a
49*0b57cec5SDimitry Andric // deciding factor.
50*0b57cec5SDimitry Andric return RHSNum < LHSNum;
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric
53*0b57cec5SDimitry Andric
54*0b57cec5SDimitry Andric /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
55*0b57cec5SDimitry Andric /// of SU, return it, otherwise return null.
getSingleUnscheduledPred(SUnit * SU)56*0b57cec5SDimitry Andric SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
57*0b57cec5SDimitry Andric SUnit *OnlyAvailablePred = nullptr;
58*0b57cec5SDimitry Andric for (const SDep &P : SU->Preds) {
59*0b57cec5SDimitry Andric SUnit &Pred = *P.getSUnit();
60*0b57cec5SDimitry Andric if (!Pred.isScheduled) {
61*0b57cec5SDimitry Andric // We found an available, but not scheduled, predecessor. If it's the
62*0b57cec5SDimitry Andric // only one we have found, keep track of it... otherwise give up.
63*0b57cec5SDimitry Andric if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
64*0b57cec5SDimitry Andric return nullptr;
65*0b57cec5SDimitry Andric OnlyAvailablePred = &Pred;
66*0b57cec5SDimitry Andric }
67*0b57cec5SDimitry Andric }
68*0b57cec5SDimitry Andric
69*0b57cec5SDimitry Andric return OnlyAvailablePred;
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric
push(SUnit * SU)72*0b57cec5SDimitry Andric void LatencyPriorityQueue::push(SUnit *SU) {
73*0b57cec5SDimitry Andric // Look at all of the successors of this node. Count the number of nodes that
74*0b57cec5SDimitry Andric // this node is the sole unscheduled node for.
75*0b57cec5SDimitry Andric unsigned NumNodesBlocking = 0;
76*0b57cec5SDimitry Andric for (const SDep &Succ : SU->Succs)
77*0b57cec5SDimitry Andric if (getSingleUnscheduledPred(Succ.getSUnit()) == SU)
78*0b57cec5SDimitry Andric ++NumNodesBlocking;
79*0b57cec5SDimitry Andric NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
80*0b57cec5SDimitry Andric
81*0b57cec5SDimitry Andric Queue.push_back(SU);
82*0b57cec5SDimitry Andric }
83*0b57cec5SDimitry Andric
84*0b57cec5SDimitry Andric
85*0b57cec5SDimitry Andric // scheduledNode - As nodes are scheduled, we look to see if there are any
86*0b57cec5SDimitry Andric // successor nodes that have a single unscheduled predecessor. If so, that
87*0b57cec5SDimitry Andric // single predecessor has a higher priority, since scheduling it will make
88*0b57cec5SDimitry Andric // the node available.
scheduledNode(SUnit * SU)89*0b57cec5SDimitry Andric void LatencyPriorityQueue::scheduledNode(SUnit *SU) {
90*0b57cec5SDimitry Andric for (const SDep &Succ : SU->Succs)
91*0b57cec5SDimitry Andric AdjustPriorityOfUnscheduledPreds(Succ.getSUnit());
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric
94*0b57cec5SDimitry Andric /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
95*0b57cec5SDimitry Andric /// scheduled. If SU is not itself available, then there is at least one
96*0b57cec5SDimitry Andric /// predecessor node that has not been scheduled yet. If SU has exactly ONE
97*0b57cec5SDimitry Andric /// unscheduled predecessor, we want to increase its priority: it getting
98*0b57cec5SDimitry Andric /// scheduled will make this node available, so it is better than some other
99*0b57cec5SDimitry Andric /// node of the same priority that will not make a node available.
AdjustPriorityOfUnscheduledPreds(SUnit * SU)100*0b57cec5SDimitry Andric void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
101*0b57cec5SDimitry Andric if (SU->isAvailable) return; // All preds scheduled.
102*0b57cec5SDimitry Andric
103*0b57cec5SDimitry Andric SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
104*0b57cec5SDimitry Andric if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return;
105*0b57cec5SDimitry Andric
106*0b57cec5SDimitry Andric // Okay, we found a single predecessor that is available, but not scheduled.
107*0b57cec5SDimitry Andric // Since it is available, it must be in the priority queue. First remove it.
108*0b57cec5SDimitry Andric remove(OnlyAvailablePred);
109*0b57cec5SDimitry Andric
110*0b57cec5SDimitry Andric // Reinsert the node into the priority queue, which recomputes its
111*0b57cec5SDimitry Andric // NumNodesSolelyBlocking value.
112*0b57cec5SDimitry Andric push(OnlyAvailablePred);
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric
pop()115*0b57cec5SDimitry Andric SUnit *LatencyPriorityQueue::pop() {
116*0b57cec5SDimitry Andric if (empty()) return nullptr;
117*0b57cec5SDimitry Andric std::vector<SUnit *>::iterator Best = Queue.begin();
118*0b57cec5SDimitry Andric for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
119*0b57cec5SDimitry Andric E = Queue.end(); I != E; ++I)
120*0b57cec5SDimitry Andric if (Picker(*Best, *I))
121*0b57cec5SDimitry Andric Best = I;
122*0b57cec5SDimitry Andric SUnit *V = *Best;
123*0b57cec5SDimitry Andric if (Best != std::prev(Queue.end()))
124*0b57cec5SDimitry Andric std::swap(*Best, Queue.back());
125*0b57cec5SDimitry Andric Queue.pop_back();
126*0b57cec5SDimitry Andric return V;
127*0b57cec5SDimitry Andric }
128*0b57cec5SDimitry Andric
remove(SUnit * SU)129*0b57cec5SDimitry Andric void LatencyPriorityQueue::remove(SUnit *SU) {
130*0b57cec5SDimitry Andric assert(!Queue.empty() && "Queue is empty!");
131*0b57cec5SDimitry Andric std::vector<SUnit *>::iterator I = find(Queue, SU);
132*0b57cec5SDimitry Andric assert(I != Queue.end() && "Queue doesn't contain the SU being removed!");
133*0b57cec5SDimitry Andric if (I != std::prev(Queue.end()))
134*0b57cec5SDimitry Andric std::swap(*I, Queue.back());
135*0b57cec5SDimitry Andric Queue.pop_back();
136*0b57cec5SDimitry Andric }
137*0b57cec5SDimitry Andric
138*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(ScheduleDAG * DAG) const139*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {
140*0b57cec5SDimitry Andric dbgs() << "Latency Priority Queue\n";
141*0b57cec5SDimitry Andric dbgs() << " Number of Queue Entries: " << Queue.size() << "\n";
142*0b57cec5SDimitry Andric for (const SUnit *SU : Queue) {
143*0b57cec5SDimitry Andric dbgs() << " ";
144*0b57cec5SDimitry Andric DAG->dumpNode(*SU);
145*0b57cec5SDimitry Andric }
146*0b57cec5SDimitry Andric }
147*0b57cec5SDimitry Andric #endif
148*0b57cec5SDimitry Andric