LatencyPriorityQueue.cpp 5.4 KB

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