LatencyPriorityQueue.cpp 5.6 KB

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