| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 1 | //===---- LatencyPriorityQueue.cpp - A latency-oriented priority queue ----===// | 
|  | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
|  | 9 | // This file implements the LatencyPriorityQueue class, which is a | 
|  | 10 | // SchedulingPriorityQueue that schedules using latency information to | 
|  | 11 | // reduce the length of the critical path through the basic block. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/LatencyPriorityQueue.h" | 
| Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 16 | #include "llvm/Config/llvm-config.h" | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 19 | using namespace llvm; | 
|  | 20 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "scheduler" | 
|  | 22 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 23 | bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { | 
| Dan Gohman | b9a0121 | 2008-12-16 03:35:01 +0000 | [diff] [blame] | 24 | // 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 Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 32 | 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 Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 40 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 41 | // 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 Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 47 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 48 | // Finally, just to provide a stable ordering, use the node number as a | 
|  | 49 | // deciding factor. | 
| Andrew Trick | 46cc9a4 | 2012-02-22 06:08:11 +0000 | [diff] [blame] | 50 | return RHSNum < LHSNum; | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 54 | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor | 
|  | 55 | /// of SU, return it, otherwise return null. | 
|  | 56 | SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) { | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 57 | SUnit *OnlyAvailablePred = nullptr; | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 58 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 59 | I != E; ++I) { | 
| Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 60 | SUnit &Pred = *I->getSUnit(); | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 61 | 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 Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 65 | return nullptr; | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 66 | OnlyAvailablePred = &Pred; | 
|  | 67 | } | 
|  | 68 | } | 
| Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 69 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 70 | return OnlyAvailablePred; | 
|  | 71 | } | 
|  | 72 |  | 
| Dan Gohman | 7c00576 | 2010-05-26 01:10:55 +0000 | [diff] [blame] | 73 | void LatencyPriorityQueue::push(SUnit *SU) { | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 74 | // 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 Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 78 | I != E; ++I) { | 
| Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 79 | if (getSingleUnscheduledPred(I->getSUnit()) == SU) | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 80 | ++NumNodesBlocking; | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 81 | } | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 82 | NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; | 
| Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 83 |  | 
| Dan Gohman | 52c2738 | 2010-05-26 18:52:00 +0000 | [diff] [blame] | 84 | Queue.push_back(SU); | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 87 |  | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 88 | // scheduledNode - As nodes are scheduled, we look to see if there are any | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 89 | // 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 Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 92 | void LatencyPriorityQueue::scheduledNode(SUnit *SU) { | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 93 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 94 | I != E; ++I) { | 
| Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 95 | AdjustPriorityOfUnscheduledPreds(I->getSUnit()); | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 96 | } | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 97 | } | 
|  | 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. | 
|  | 105 | void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) { | 
| Dan Gohman | 17c226b | 2008-11-17 16:37:30 +0000 | [diff] [blame] | 106 | if (SU->isAvailable) return;  // All preds scheduled. | 
| Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 107 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 108 | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 109 | if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return; | 
| Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 110 |  | 
| Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 111 | // 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 Gohman | 52c2738 | 2010-05-26 18:52:00 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | SUnit *LatencyPriorityQueue::pop() { | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 121 | if (empty()) return nullptr; | 
| Dan Gohman | 52c2738 | 2010-05-26 18:52:00 +0000 | [diff] [blame] | 122 | std::vector<SUnit *>::iterator Best = Queue.begin(); | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 123 | for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()), | 
| Dan Gohman | 52c2738 | 2010-05-26 18:52:00 +0000 | [diff] [blame] | 124 | E = Queue.end(); I != E; ++I) | 
|  | 125 | if (Picker(*Best, *I)) | 
|  | 126 | Best = I; | 
|  | 127 | SUnit *V = *Best; | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 128 | if (Best != std::prev(Queue.end())) | 
| Dan Gohman | 52c2738 | 2010-05-26 18:52:00 +0000 | [diff] [blame] | 129 | std::swap(*Best, Queue.back()); | 
|  | 130 | Queue.pop_back(); | 
|  | 131 | return V; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | void LatencyPriorityQueue::remove(SUnit *SU) { | 
|  | 135 | assert(!Queue.empty() && "Queue is empty!"); | 
| David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 136 | std::vector<SUnit *>::iterator I = find(Queue, SU); | 
| Benjamin Kramer | bd20e97 | 2017-11-16 10:18:07 +0000 | [diff] [blame] | 137 | assert(I != Queue.end() && "Queue doesn't contain the SU being removed!"); | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 138 | if (I != std::prev(Queue.end())) | 
| Dan Gohman | 52c2738 | 2010-05-26 18:52:00 +0000 | [diff] [blame] | 139 | std::swap(*I, Queue.back()); | 
|  | 140 | Queue.pop_back(); | 
|  | 141 | } | 
| Amara Emerson | 6aacbf4 | 2018-04-20 00:42:46 +0000 | [diff] [blame] | 142 |  | 
|  | 143 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | 
|  | 144 | LLVM_DUMP_METHOD void LatencyPriorityQueue::dump(ScheduleDAG *DAG) const { | 
|  | 145 | dbgs() << "Latency Priority Queue\n"; | 
|  | 146 | dbgs() << "  Number of Queue Entries: " << Queue.size() << "\n"; | 
| Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 147 | for (const SUnit *SU : Queue) { | 
| Amara Emerson | 6aacbf4 | 2018-04-20 00:42:46 +0000 | [diff] [blame] | 148 | dbgs() << "    "; | 
| Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 149 | DAG->dumpNode(*SU); | 
| Amara Emerson | 6aacbf4 | 2018-04-20 00:42:46 +0000 | [diff] [blame] | 150 | } | 
|  | 151 | } | 
|  | 152 | #endif |