xref: /src/contrib/llvm-project/llvm/lib/CodeGen/LatencyPriorityQueue.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
1009b1c42SEd Schouten //===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the LatencyPriorityQueue class, which is a
10009b1c42SEd Schouten // SchedulingPriorityQueue that schedules using latency information to
11009b1c42SEd Schouten // reduce the length of the critical path through the basic block.
12009b1c42SEd Schouten //
13009b1c42SEd Schouten //===----------------------------------------------------------------------===//
14009b1c42SEd Schouten 
15009b1c42SEd Schouten #include "llvm/CodeGen/LatencyPriorityQueue.h"
16eb11fae6SDimitry Andric #include "llvm/Config/llvm-config.h"
17009b1c42SEd Schouten #include "llvm/Support/Debug.h"
18cf099d11SDimitry Andric #include "llvm/Support/raw_ostream.h"
19009b1c42SEd Schouten using namespace llvm;
20009b1c42SEd Schouten 
215ca98fd9SDimitry Andric #define DEBUG_TYPE "scheduler"
225ca98fd9SDimitry Andric 
operator ()(const SUnit * LHS,const SUnit * RHS) const23009b1c42SEd Schouten bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
24009b1c42SEd Schouten   // The isScheduleHigh flag allows nodes with wraparound dependencies that
25009b1c42SEd Schouten   // cannot easily be modeled as edges with latencies to be scheduled as
26009b1c42SEd Schouten   // soon as possible in a top-down schedule.
27009b1c42SEd Schouten   if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
28009b1c42SEd Schouten     return false;
29009b1c42SEd Schouten   if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
30009b1c42SEd Schouten     return true;
31009b1c42SEd Schouten 
32009b1c42SEd Schouten   unsigned LHSNum = LHS->NodeNum;
33009b1c42SEd Schouten   unsigned RHSNum = RHS->NodeNum;
34009b1c42SEd Schouten 
35009b1c42SEd Schouten   // The most important heuristic is scheduling the critical path.
36009b1c42SEd Schouten   unsigned LHSLatency = PQ->getLatency(LHSNum);
37009b1c42SEd Schouten   unsigned RHSLatency = PQ->getLatency(RHSNum);
38009b1c42SEd Schouten   if (LHSLatency < RHSLatency) return true;
39009b1c42SEd Schouten   if (LHSLatency > RHSLatency) return false;
40009b1c42SEd Schouten 
41009b1c42SEd Schouten   // After that, if two nodes have identical latencies, look to see if one will
42009b1c42SEd Schouten   // unblock more other nodes than the other.
43009b1c42SEd Schouten   unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
44009b1c42SEd Schouten   unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
45009b1c42SEd Schouten   if (LHSBlocked < RHSBlocked) return true;
46009b1c42SEd Schouten   if (LHSBlocked > RHSBlocked) return false;
47009b1c42SEd Schouten 
48009b1c42SEd Schouten   // Finally, just to provide a stable ordering, use the node number as a
49009b1c42SEd Schouten   // deciding factor.
5063faed5bSDimitry Andric   return RHSNum < LHSNum;
51009b1c42SEd Schouten }
52009b1c42SEd Schouten 
53009b1c42SEd Schouten 
54009b1c42SEd Schouten /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
55009b1c42SEd Schouten /// of SU, return it, otherwise return null.
getSingleUnscheduledPred(SUnit * SU)56009b1c42SEd Schouten SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
575ca98fd9SDimitry Andric   SUnit *OnlyAvailablePred = nullptr;
58344a3780SDimitry Andric   for (const SDep &P : SU->Preds) {
59344a3780SDimitry Andric     SUnit &Pred = *P.getSUnit();
60009b1c42SEd Schouten     if (!Pred.isScheduled) {
61009b1c42SEd Schouten       // We found an available, but not scheduled, predecessor.  If it's the
62009b1c42SEd Schouten       // only one we have found, keep track of it... otherwise give up.
63009b1c42SEd Schouten       if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
645ca98fd9SDimitry Andric         return nullptr;
65009b1c42SEd Schouten       OnlyAvailablePred = &Pred;
66009b1c42SEd Schouten     }
67009b1c42SEd Schouten   }
68009b1c42SEd Schouten 
69009b1c42SEd Schouten   return OnlyAvailablePred;
70009b1c42SEd Schouten }
71009b1c42SEd Schouten 
push(SUnit * SU)72abdf259dSRoman Divacky void LatencyPriorityQueue::push(SUnit *SU) {
73009b1c42SEd Schouten   // Look at all of the successors of this node.  Count the number of nodes that
74009b1c42SEd Schouten   // this node is the sole unscheduled node for.
75009b1c42SEd Schouten   unsigned NumNodesBlocking = 0;
76f65dcba8SDimitry Andric   for (const SDep &Succ : SU->Succs)
77f65dcba8SDimitry Andric     if (getSingleUnscheduledPred(Succ.getSUnit()) == SU)
78009b1c42SEd Schouten       ++NumNodesBlocking;
79009b1c42SEd Schouten   NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
80009b1c42SEd Schouten 
81abdf259dSRoman Divacky   Queue.push_back(SU);
82009b1c42SEd Schouten }
83009b1c42SEd Schouten 
84009b1c42SEd Schouten 
8563faed5bSDimitry Andric // scheduledNode - As nodes are scheduled, we look to see if there are any
86009b1c42SEd Schouten // successor nodes that have a single unscheduled predecessor.  If so, that
87009b1c42SEd Schouten // single predecessor has a higher priority, since scheduling it will make
88009b1c42SEd Schouten // the node available.
scheduledNode(SUnit * SU)8963faed5bSDimitry Andric void LatencyPriorityQueue::scheduledNode(SUnit *SU) {
90344a3780SDimitry Andric   for (const SDep &Succ : SU->Succs)
91344a3780SDimitry Andric     AdjustPriorityOfUnscheduledPreds(Succ.getSUnit());
9236bf506aSRoman Divacky }
93009b1c42SEd Schouten 
94009b1c42SEd Schouten /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
95009b1c42SEd Schouten /// scheduled.  If SU is not itself available, then there is at least one
96009b1c42SEd Schouten /// predecessor node that has not been scheduled yet.  If SU has exactly ONE
97009b1c42SEd Schouten /// unscheduled predecessor, we want to increase its priority: it getting
98009b1c42SEd Schouten /// scheduled will make this node available, so it is better than some other
99009b1c42SEd Schouten /// node of the same priority that will not make a node available.
AdjustPriorityOfUnscheduledPreds(SUnit * SU)100009b1c42SEd Schouten void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
101009b1c42SEd Schouten   if (SU->isAvailable) return;  // All preds scheduled.
102009b1c42SEd Schouten 
103009b1c42SEd Schouten   SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
1045ca98fd9SDimitry Andric   if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return;
105009b1c42SEd Schouten 
106009b1c42SEd Schouten   // Okay, we found a single predecessor that is available, but not scheduled.
107009b1c42SEd Schouten   // Since it is available, it must be in the priority queue.  First remove it.
108009b1c42SEd Schouten   remove(OnlyAvailablePred);
109009b1c42SEd Schouten 
110009b1c42SEd Schouten   // Reinsert the node into the priority queue, which recomputes its
111009b1c42SEd Schouten   // NumNodesSolelyBlocking value.
112009b1c42SEd Schouten   push(OnlyAvailablePred);
113009b1c42SEd Schouten }
114abdf259dSRoman Divacky 
pop()115abdf259dSRoman Divacky SUnit *LatencyPriorityQueue::pop() {
1165ca98fd9SDimitry Andric   if (empty()) return nullptr;
117abdf259dSRoman Divacky   std::vector<SUnit *>::iterator Best = Queue.begin();
1185ca98fd9SDimitry Andric   for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
119abdf259dSRoman Divacky        E = Queue.end(); I != E; ++I)
120abdf259dSRoman Divacky     if (Picker(*Best, *I))
121abdf259dSRoman Divacky       Best = I;
122abdf259dSRoman Divacky   SUnit *V = *Best;
1235ca98fd9SDimitry Andric   if (Best != std::prev(Queue.end()))
124abdf259dSRoman Divacky     std::swap(*Best, Queue.back());
125abdf259dSRoman Divacky   Queue.pop_back();
126abdf259dSRoman Divacky   return V;
127abdf259dSRoman Divacky }
128abdf259dSRoman Divacky 
remove(SUnit * SU)129abdf259dSRoman Divacky void LatencyPriorityQueue::remove(SUnit *SU) {
130abdf259dSRoman Divacky   assert(!Queue.empty() && "Queue is empty!");
131b915e9e0SDimitry Andric   std::vector<SUnit *>::iterator I = find(Queue, SU);
132044eb2f6SDimitry Andric   assert(I != Queue.end() && "Queue doesn't contain the SU being removed!");
1335ca98fd9SDimitry Andric   if (I != std::prev(Queue.end()))
134abdf259dSRoman Divacky     std::swap(*I, Queue.back());
135abdf259dSRoman Divacky   Queue.pop_back();
136abdf259dSRoman Divacky }
137eb11fae6SDimitry Andric 
138eb11fae6SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(ScheduleDAG * DAG) const139eb11fae6SDimitry Andric LLVM_DUMP_METHOD void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {
140eb11fae6SDimitry Andric   dbgs() << "Latency Priority Queue\n";
141eb11fae6SDimitry Andric   dbgs() << "  Number of Queue Entries: " << Queue.size() << "\n";
142d8e91e46SDimitry Andric   for (const SUnit *SU : Queue) {
143eb11fae6SDimitry Andric     dbgs() << "    ";
144d8e91e46SDimitry Andric     DAG->dumpNode(*SU);
145eb11fae6SDimitry Andric   }
146eb11fae6SDimitry Andric }
147eb11fae6SDimitry Andric #endif
148