Dan Gohman | ade9f18 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 1 | //===---- 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 | |
| 16 | #define DEBUG_TYPE "scheduler" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame^] | 17 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
Dan Gohman | ade9f18 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { |
| 22 | unsigned LHSNum = LHS->NodeNum; |
| 23 | unsigned RHSNum = RHS->NodeNum; |
| 24 | |
| 25 | // The most important heuristic is scheduling the critical path. |
| 26 | unsigned LHSLatency = PQ->getLatency(LHSNum); |
| 27 | unsigned RHSLatency = PQ->getLatency(RHSNum); |
| 28 | if (LHSLatency < RHSLatency) return true; |
| 29 | if (LHSLatency > RHSLatency) return false; |
| 30 | |
| 31 | // After that, if two nodes have identical latencies, look to see if one will |
| 32 | // unblock more other nodes than the other. |
| 33 | unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum); |
| 34 | unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum); |
| 35 | if (LHSBlocked < RHSBlocked) return true; |
| 36 | if (LHSBlocked > RHSBlocked) return false; |
| 37 | |
| 38 | // Finally, just to provide a stable ordering, use the node number as a |
| 39 | // deciding factor. |
| 40 | return LHSNum < RHSNum; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /// CalcNodePriority - Calculate the maximal path from the node to the exit. |
| 45 | /// |
| 46 | int LatencyPriorityQueue::CalcLatency(const SUnit &SU) { |
| 47 | int &Latency = Latencies[SU.NodeNum]; |
| 48 | if (Latency != -1) |
| 49 | return Latency; |
| 50 | |
| 51 | std::vector<const SUnit*> WorkList; |
| 52 | WorkList.push_back(&SU); |
| 53 | while (!WorkList.empty()) { |
| 54 | const SUnit *Cur = WorkList.back(); |
| 55 | bool AllDone = true; |
| 56 | int MaxSuccLatency = 0; |
| 57 | for (SUnit::const_succ_iterator I = Cur->Succs.begin(),E = Cur->Succs.end(); |
| 58 | I != E; ++I) { |
| 59 | int SuccLatency = Latencies[I->Dep->NodeNum]; |
| 60 | if (SuccLatency == -1) { |
| 61 | AllDone = false; |
| 62 | WorkList.push_back(I->Dep); |
| 63 | } else { |
| 64 | MaxSuccLatency = std::max(MaxSuccLatency, SuccLatency); |
| 65 | } |
| 66 | } |
| 67 | if (AllDone) { |
| 68 | Latencies[Cur->NodeNum] = MaxSuccLatency + Cur->Latency; |
| 69 | WorkList.pop_back(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return Latency; |
| 74 | } |
| 75 | |
| 76 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 77 | void LatencyPriorityQueue::CalculatePriorities() { |
| 78 | Latencies.assign(SUnits->size(), -1); |
| 79 | NumNodesSolelyBlocking.assign(SUnits->size(), 0); |
| 80 | |
| 81 | // For each node, calculate the maximal path from the node to the exit. |
| 82 | std::vector<std::pair<const SUnit*, unsigned> > WorkList; |
| 83 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { |
| 84 | const SUnit *SU = &(*SUnits)[i]; |
| 85 | if (SU->Succs.empty()) |
| 86 | WorkList.push_back(std::make_pair(SU, 0U)); |
| 87 | } |
| 88 | |
| 89 | while (!WorkList.empty()) { |
| 90 | const SUnit *SU = WorkList.back().first; |
| 91 | unsigned SuccLat = WorkList.back().second; |
| 92 | WorkList.pop_back(); |
| 93 | int &Latency = Latencies[SU->NodeNum]; |
| 94 | if (Latency == -1 || (SU->Latency + SuccLat) > (unsigned)Latency) { |
| 95 | Latency = SU->Latency + SuccLat; |
| 96 | for (SUnit::const_pred_iterator I = SU->Preds.begin(),E = SU->Preds.end(); |
| 97 | I != E; ++I) |
| 98 | WorkList.push_back(std::make_pair(I->Dep, Latency)); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor |
| 104 | /// of SU, return it, otherwise return null. |
| 105 | SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) { |
| 106 | SUnit *OnlyAvailablePred = 0; |
| 107 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 108 | I != E; ++I) { |
| 109 | SUnit &Pred = *I->Dep; |
| 110 | if (!Pred.isScheduled) { |
| 111 | // We found an available, but not scheduled, predecessor. If it's the |
| 112 | // only one we have found, keep track of it... otherwise give up. |
| 113 | if (OnlyAvailablePred && OnlyAvailablePred != &Pred) |
| 114 | return 0; |
| 115 | OnlyAvailablePred = &Pred; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return OnlyAvailablePred; |
| 120 | } |
| 121 | |
| 122 | void LatencyPriorityQueue::push_impl(SUnit *SU) { |
| 123 | // Look at all of the successors of this node. Count the number of nodes that |
| 124 | // this node is the sole unscheduled node for. |
| 125 | unsigned NumNodesBlocking = 0; |
| 126 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 127 | I != E; ++I) |
| 128 | if (getSingleUnscheduledPred(I->Dep) == SU) |
| 129 | ++NumNodesBlocking; |
| 130 | NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; |
| 131 | |
| 132 | Queue.push(SU); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 137 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 138 | // single predecessor has a higher priority, since scheduling it will make |
| 139 | // the node available. |
| 140 | void LatencyPriorityQueue::ScheduledNode(SUnit *SU) { |
| 141 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 142 | I != E; ++I) |
| 143 | AdjustPriorityOfUnscheduledPreds(I->Dep); |
| 144 | } |
| 145 | |
| 146 | /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just |
| 147 | /// scheduled. If SU is not itself available, then there is at least one |
| 148 | /// predecessor node that has not been scheduled yet. If SU has exactly ONE |
| 149 | /// unscheduled predecessor, we want to increase its priority: it getting |
| 150 | /// scheduled will make this node available, so it is better than some other |
| 151 | /// node of the same priority that will not make a node available. |
| 152 | void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) { |
Dan Gohman | 6560c00 | 2008-11-17 16:37:30 +0000 | [diff] [blame] | 153 | if (SU->isAvailable) return; // All preds scheduled. |
Dan Gohman | ade9f18 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 154 | |
| 155 | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); |
| 156 | if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return; |
| 157 | |
| 158 | // Okay, we found a single predecessor that is available, but not scheduled. |
| 159 | // Since it is available, it must be in the priority queue. First remove it. |
| 160 | remove(OnlyAvailablePred); |
| 161 | |
| 162 | // Reinsert the node into the priority queue, which recomputes its |
| 163 | // NumNodesSolelyBlocking value. |
| 164 | push(OnlyAvailablePred); |
| 165 | } |