LatencyPriorityQueue.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/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 (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
  50. I != E; ++I) {
  51. SUnit &Pred = *I->getSUnit();
  52. if (!Pred.isScheduled) {
  53. // We found an available, but not scheduled, predecessor. If it's the
  54. // only one we have found, keep track of it... otherwise give up.
  55. if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
  56. return nullptr;
  57. OnlyAvailablePred = &Pred;
  58. }
  59. }
  60. return OnlyAvailablePred;
  61. }
  62. void LatencyPriorityQueue::push(SUnit *SU) {
  63. // Look at all of the successors of this node. Count the number of nodes that
  64. // this node is the sole unscheduled node for.
  65. unsigned NumNodesBlocking = 0;
  66. for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  67. I != E; ++I) {
  68. if (getSingleUnscheduledPred(I->getSUnit()) == SU)
  69. ++NumNodesBlocking;
  70. }
  71. NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
  72. Queue.push_back(SU);
  73. }
  74. // scheduledNode - As nodes are scheduled, we look to see if there are any
  75. // successor nodes that have a single unscheduled predecessor. If so, that
  76. // single predecessor has a higher priority, since scheduling it will make
  77. // the node available.
  78. void LatencyPriorityQueue::scheduledNode(SUnit *SU) {
  79. for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
  80. I != E; ++I) {
  81. AdjustPriorityOfUnscheduledPreds(I->getSUnit());
  82. }
  83. }
  84. /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
  85. /// scheduled. If SU is not itself available, then there is at least one
  86. /// predecessor node that has not been scheduled yet. If SU has exactly ONE
  87. /// unscheduled predecessor, we want to increase its priority: it getting
  88. /// scheduled will make this node available, so it is better than some other
  89. /// node of the same priority that will not make a node available.
  90. void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) {
  91. if (SU->isAvailable) return; // All preds scheduled.
  92. SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
  93. if (!OnlyAvailablePred || !OnlyAvailablePred->isAvailable) return;
  94. // Okay, we found a single predecessor that is available, but not scheduled.
  95. // Since it is available, it must be in the priority queue. First remove it.
  96. remove(OnlyAvailablePred);
  97. // Reinsert the node into the priority queue, which recomputes its
  98. // NumNodesSolelyBlocking value.
  99. push(OnlyAvailablePred);
  100. }
  101. SUnit *LatencyPriorityQueue::pop() {
  102. if (empty()) return nullptr;
  103. std::vector<SUnit *>::iterator Best = Queue.begin();
  104. for (std::vector<SUnit *>::iterator I = std::next(Queue.begin()),
  105. E = Queue.end(); I != E; ++I)
  106. if (Picker(*Best, *I))
  107. Best = I;
  108. SUnit *V = *Best;
  109. if (Best != std::prev(Queue.end()))
  110. std::swap(*Best, Queue.back());
  111. Queue.pop_back();
  112. return V;
  113. }
  114. void LatencyPriorityQueue::remove(SUnit *SU) {
  115. assert(!Queue.empty() && "Queue is empty!");
  116. std::vector<SUnit *>::iterator I = std::find(Queue.begin(), Queue.end(), SU);
  117. if (I != std::prev(Queue.end()))
  118. std::swap(*I, Queue.back());
  119. Queue.pop_back();
  120. }