10b57cec5SDimitry Andric //===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the LatencyPriorityQueue class, which is a
100b57cec5SDimitry Andric // SchedulingPriorityQueue that schedules using latency information to
110b57cec5SDimitry Andric // reduce the length of the critical path through the basic block.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #include "llvm/CodeGen/LatencyPriorityQueue.h"
160b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
170b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
180b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric #define DEBUG_TYPE "scheduler"
220b57cec5SDimitry Andric
operator ()(const SUnit * LHS,const SUnit * RHS) const230b57cec5SDimitry Andric bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const {
240b57cec5SDimitry Andric // The isScheduleHigh flag allows nodes with wraparound dependencies that
250b57cec5SDimitry Andric // cannot easily be modeled as edges with latencies to be scheduled as
260b57cec5SDimitry Andric // soon as possible in a top-down schedule.
270b57cec5SDimitry Andric if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
280b57cec5SDimitry Andric return false;
290b57cec5SDimitry Andric if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
300b57cec5SDimitry Andric return true;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric unsigned LHSNum = LHS->NodeNum;
330b57cec5SDimitry Andric unsigned RHSNum = RHS->NodeNum;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric // The most important heuristic is scheduling the critical path.
360b57cec5SDimitry Andric unsigned LHSLatency = PQ->getLatency(LHSNum);
370b57cec5SDimitry Andric unsigned RHSLatency = PQ->getLatency(RHSNum);
380b57cec5SDimitry Andric if (LHSLatency < RHSLatency) return true;
390b57cec5SDimitry Andric if (LHSLatency > RHSLatency) return false;
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric // After that, if two nodes have identical latencies, look to see if one will
420b57cec5SDimitry Andric // unblock more other nodes than the other.
430b57cec5SDimitry Andric unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum);
440b57cec5SDimitry Andric unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum);
450b57cec5SDimitry Andric if (LHSBlocked < RHSBlocked) return true;
460b57cec5SDimitry Andric if (LHSBlocked > RHSBlocked) return false;
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric // Finally, just to provide a stable ordering, use the node number as a
490b57cec5SDimitry Andric // deciding factor.
500b57cec5SDimitry Andric return RHSNum < LHSNum;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
550b57cec5SDimitry Andric /// of SU, return it, otherwise return null.
getSingleUnscheduledPred(SUnit * SU)560b57cec5SDimitry Andric SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
570b57cec5SDimitry Andric SUnit *OnlyAvailablePred = nullptr;
58fe6060f1SDimitry Andric for (const SDep &P : SU->Preds) {
59fe6060f1SDimitry Andric SUnit &Pred = *P.getSUnit();
600b57cec5SDimitry Andric if (!Pred.isScheduled) {
610b57cec5SDimitry Andric // We found an available, but not scheduled, predecessor. If it's the
620b57cec5SDimitry Andric // only one we have found, keep track of it... otherwise give up.
630b57cec5SDimitry Andric if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
640b57cec5SDimitry Andric return nullptr;
650b57cec5SDimitry Andric OnlyAvailablePred = &Pred;
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric return OnlyAvailablePred;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric
push(SUnit * SU)720b57cec5SDimitry Andric void LatencyPriorityQueue::push(SUnit *SU) {
730b57cec5SDimitry Andric // Look at all of the successors of this node. Count the number of nodes that
740b57cec5SDimitry Andric // this node is the sole unscheduled node for.
750b57cec5SDimitry Andric unsigned NumNodesBlocking = 0;
76*4824e7fdSDimitry Andric for (const SDep &Succ : SU->Succs)
77*4824e7fdSDimitry Andric if (getSingleUnscheduledPred(Succ.getSUnit()) == SU)
780b57cec5SDimitry Andric ++NumNodesBlocking;
790b57cec5SDimitry Andric NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric Queue.push_back(SU);
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric // scheduledNode - As nodes are scheduled, we look to see if there are any
860b57cec5SDimitry Andric // successor nodes that have a single unscheduled predecessor. If so, that
870b57cec5SDimitry Andric // single predecessor has a higher priority, since scheduling it will make
880b57cec5SDimitry Andric // the node available.
scheduledNode(SUnit * SU)890b57cec5SDimitry Andric void LatencyPriorityQueue::scheduledNode(SUnit *SU) {
90fe6060f1SDimitry Andric for (const SDep &Succ : SU->Succs)
91fe6060f1SDimitry Andric AdjustPriorityOfUnscheduledPreds(Succ.getSUnit());
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
950b57cec5SDimitry Andric /// scheduled. If SU is not itself available, then there is at least one
960b57cec5SDimitry Andric /// predecessor node that has not been scheduled yet. If SU has exactly ONE
970b57cec5SDimitry Andric /// unscheduled predecessor, we want to increase its priority: it getting
980b57cec5SDimitry Andric /// scheduled will make this node available, so it is better than some other
990b57cec5SDimitry Andric /// node of the same priority that will not make a node available.
AdjustPriorityOfUnscheduledPreds(SUnit * SU)1000b57cec5SDimitry Andric void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
1010b57cec5SDimitry Andric if (SU->isAvailable) return; // All preds scheduled.
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
1040b57cec5SDimitry Andric if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return;
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // Okay, we found a single predecessor that is available, but not scheduled.
1070b57cec5SDimitry Andric // Since it is available, it must be in the priority queue. First remove it.
1080b57cec5SDimitry Andric remove(OnlyAvailablePred);
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric // Reinsert the node into the priority queue, which recomputes its
1110b57cec5SDimitry Andric // NumNodesSolelyBlocking value.
1120b57cec5SDimitry Andric push(OnlyAvailablePred);
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
pop()1150b57cec5SDimitry Andric SUnit *LatencyPriorityQueue::pop() {
1160b57cec5SDimitry Andric if (empty()) return nullptr;
1170b57cec5SDimitry Andric std::vector<SUnit *>::iterator Best = Queue.begin();
1180b57cec5SDimitry Andric for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
1190b57cec5SDimitry Andric E = Queue.end(); I != E; ++I)
1200b57cec5SDimitry Andric if (Picker(*Best, *I))
1210b57cec5SDimitry Andric Best = I;
1220b57cec5SDimitry Andric SUnit *V = *Best;
1230b57cec5SDimitry Andric if (Best != std::prev(Queue.end()))
1240b57cec5SDimitry Andric std::swap(*Best, Queue.back());
1250b57cec5SDimitry Andric Queue.pop_back();
1260b57cec5SDimitry Andric return V;
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
remove(SUnit * SU)1290b57cec5SDimitry Andric void LatencyPriorityQueue::remove(SUnit *SU) {
1300b57cec5SDimitry Andric assert(!Queue.empty() && "Queue is empty!");
1310b57cec5SDimitry Andric std::vector<SUnit *>::iterator I = find(Queue, SU);
1320b57cec5SDimitry Andric assert(I != Queue.end() && "Queue doesn't contain the SU being removed!");
1330b57cec5SDimitry Andric if (I != std::prev(Queue.end()))
1340b57cec5SDimitry Andric std::swap(*I, Queue.back());
1350b57cec5SDimitry Andric Queue.pop_back();
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(ScheduleDAG * DAG) const1390b57cec5SDimitry Andric LLVM_DUMP_METHOD void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const {
1400b57cec5SDimitry Andric dbgs() << "Latency Priority Queue\n";
1410b57cec5SDimitry Andric dbgs() << " Number of Queue Entries: " << Queue.size() << "\n";
1420b57cec5SDimitry Andric for (const SUnit *SU : Queue) {
1430b57cec5SDimitry Andric dbgs() << " ";
1440b57cec5SDimitry Andric DAG->dumpNode(*SU);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric #endif
148