ScheduleDAGVLIW.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //===- ScheduleDAGVLIW.cpp - SelectionDAG list scheduler for VLIW -*- C++ -*-=//
  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 implements a top-down list scheduler, using standard algorithms.
  10. // The basic approach uses a priority queue of available nodes to schedule.
  11. // One at a time, nodes are taken from the priority queue (thus in priority
  12. // order), checked for legality to schedule, and emitted if legal.
  13. //
  14. // Nodes may not be legal to schedule either due to structural hazards (e.g.
  15. // pipeline or resource constraints) or because an input to the instruction has
  16. // not completed execution.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "ScheduleDAGSDNodes.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include "llvm/CodeGen/ResourcePriorityQueue.h"
  22. #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
  23. #include "llvm/CodeGen/SchedulerRegistry.h"
  24. #include "llvm/CodeGen/SelectionDAGISel.h"
  25. #include "llvm/CodeGen/TargetInstrInfo.h"
  26. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "pre-RA-sched"
  32. STATISTIC(NumNoops , "Number of noops inserted");
  33. STATISTIC(NumStalls, "Number of pipeline stalls");
  34. static RegisterScheduler
  35. VLIWScheduler("vliw-td", "VLIW scheduler",
  36. createVLIWDAGScheduler);
  37. namespace {
  38. //===----------------------------------------------------------------------===//
  39. /// ScheduleDAGVLIW - The actual DFA list scheduler implementation. This
  40. /// supports / top-down scheduling.
  41. ///
  42. class ScheduleDAGVLIW : public ScheduleDAGSDNodes {
  43. private:
  44. /// AvailableQueue - The priority queue to use for the available SUnits.
  45. ///
  46. SchedulingPriorityQueue *AvailableQueue;
  47. /// PendingQueue - This contains all of the instructions whose operands have
  48. /// been issued, but their results are not ready yet (due to the latency of
  49. /// the operation). Once the operands become available, the instruction is
  50. /// added to the AvailableQueue.
  51. std::vector<SUnit*> PendingQueue;
  52. /// HazardRec - The hazard recognizer to use.
  53. ScheduleHazardRecognizer *HazardRec;
  54. /// AA - AAResults for making memory reference queries.
  55. AAResults *AA;
  56. public:
  57. ScheduleDAGVLIW(MachineFunction &mf, AAResults *aa,
  58. SchedulingPriorityQueue *availqueue)
  59. : ScheduleDAGSDNodes(mf), AvailableQueue(availqueue), AA(aa) {
  60. const TargetSubtargetInfo &STI = mf.getSubtarget();
  61. HazardRec = STI.getInstrInfo()->CreateTargetHazardRecognizer(&STI, this);
  62. }
  63. ~ScheduleDAGVLIW() override {
  64. delete HazardRec;
  65. delete AvailableQueue;
  66. }
  67. void Schedule() override;
  68. private:
  69. void releaseSucc(SUnit *SU, const SDep &D);
  70. void releaseSuccessors(SUnit *SU);
  71. void scheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
  72. void listScheduleTopDown();
  73. };
  74. } // end anonymous namespace
  75. /// Schedule - Schedule the DAG using list scheduling.
  76. void ScheduleDAGVLIW::Schedule() {
  77. LLVM_DEBUG(dbgs() << "********** List Scheduling " << printMBBReference(*BB)
  78. << " '" << BB->getName() << "' **********\n");
  79. // Build the scheduling graph.
  80. BuildSchedGraph(AA);
  81. AvailableQueue->initNodes(SUnits);
  82. listScheduleTopDown();
  83. AvailableQueue->releaseState();
  84. }
  85. //===----------------------------------------------------------------------===//
  86. // Top-Down Scheduling
  87. //===----------------------------------------------------------------------===//
  88. /// releaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
  89. /// the PendingQueue if the count reaches zero. Also update its cycle bound.
  90. void ScheduleDAGVLIW::releaseSucc(SUnit *SU, const SDep &D) {
  91. SUnit *SuccSU = D.getSUnit();
  92. #ifndef NDEBUG
  93. if (SuccSU->NumPredsLeft == 0) {
  94. dbgs() << "*** Scheduling failed! ***\n";
  95. dumpNode(*SuccSU);
  96. dbgs() << " has been released too many times!\n";
  97. llvm_unreachable(nullptr);
  98. }
  99. #endif
  100. assert(!D.isWeak() && "unexpected artificial DAG edge");
  101. --SuccSU->NumPredsLeft;
  102. SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
  103. // If all the node's predecessors are scheduled, this node is ready
  104. // to be scheduled. Ignore the special ExitSU node.
  105. if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
  106. PendingQueue.push_back(SuccSU);
  107. }
  108. }
  109. void ScheduleDAGVLIW::releaseSuccessors(SUnit *SU) {
  110. // Top down: release successors.
  111. for (SDep &Succ : SU->Succs) {
  112. assert(!Succ.isAssignedRegDep() &&
  113. "The list-td scheduler doesn't yet support physreg dependencies!");
  114. releaseSucc(SU, Succ);
  115. }
  116. }
  117. /// scheduleNodeTopDown - Add the node to the schedule. Decrement the pending
  118. /// count of its successors. If a successor pending count is zero, add it to
  119. /// the Available queue.
  120. void ScheduleDAGVLIW::scheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
  121. LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
  122. LLVM_DEBUG(dumpNode(*SU));
  123. Sequence.push_back(SU);
  124. assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
  125. SU->setDepthToAtLeast(CurCycle);
  126. releaseSuccessors(SU);
  127. SU->isScheduled = true;
  128. AvailableQueue->scheduledNode(SU);
  129. }
  130. /// listScheduleTopDown - The main loop of list scheduling for top-down
  131. /// schedulers.
  132. void ScheduleDAGVLIW::listScheduleTopDown() {
  133. unsigned CurCycle = 0;
  134. // Release any successors of the special Entry node.
  135. releaseSuccessors(&EntrySU);
  136. // All leaves to AvailableQueue.
  137. for (SUnit &SU : SUnits) {
  138. // It is available if it has no predecessors.
  139. if (SU.Preds.empty()) {
  140. AvailableQueue->push(&SU);
  141. SU.isAvailable = true;
  142. }
  143. }
  144. // While AvailableQueue is not empty, grab the node with the highest
  145. // priority. If it is not ready put it back. Schedule the node.
  146. std::vector<SUnit*> NotReady;
  147. Sequence.reserve(SUnits.size());
  148. while (!AvailableQueue->empty() || !PendingQueue.empty()) {
  149. // Check to see if any of the pending instructions are ready to issue. If
  150. // so, add them to the available queue.
  151. for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
  152. if (PendingQueue[i]->getDepth() == CurCycle) {
  153. AvailableQueue->push(PendingQueue[i]);
  154. PendingQueue[i]->isAvailable = true;
  155. PendingQueue[i] = PendingQueue.back();
  156. PendingQueue.pop_back();
  157. --i; --e;
  158. }
  159. else {
  160. assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
  161. }
  162. }
  163. // If there are no instructions available, don't try to issue anything, and
  164. // don't advance the hazard recognizer.
  165. if (AvailableQueue->empty()) {
  166. // Reset DFA state.
  167. AvailableQueue->scheduledNode(nullptr);
  168. ++CurCycle;
  169. continue;
  170. }
  171. SUnit *FoundSUnit = nullptr;
  172. bool HasNoopHazards = false;
  173. while (!AvailableQueue->empty()) {
  174. SUnit *CurSUnit = AvailableQueue->pop();
  175. ScheduleHazardRecognizer::HazardType HT =
  176. HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
  177. if (HT == ScheduleHazardRecognizer::NoHazard) {
  178. FoundSUnit = CurSUnit;
  179. break;
  180. }
  181. // Remember if this is a noop hazard.
  182. HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
  183. NotReady.push_back(CurSUnit);
  184. }
  185. // Add the nodes that aren't ready back onto the available list.
  186. if (!NotReady.empty()) {
  187. AvailableQueue->push_all(NotReady);
  188. NotReady.clear();
  189. }
  190. // If we found a node to schedule, do it now.
  191. if (FoundSUnit) {
  192. scheduleNodeTopDown(FoundSUnit, CurCycle);
  193. HazardRec->EmitInstruction(FoundSUnit);
  194. // If this is a pseudo-op node, we don't want to increment the current
  195. // cycle.
  196. if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
  197. ++CurCycle;
  198. } else if (!HasNoopHazards) {
  199. // Otherwise, we have a pipeline stall, but no other problem, just advance
  200. // the current cycle and try again.
  201. LLVM_DEBUG(dbgs() << "*** Advancing cycle, no work to do\n");
  202. HazardRec->AdvanceCycle();
  203. ++NumStalls;
  204. ++CurCycle;
  205. } else {
  206. // Otherwise, we have no instructions to issue and we have instructions
  207. // that will fault if we don't do this right. This is the case for
  208. // processors without pipeline interlocks and other cases.
  209. LLVM_DEBUG(dbgs() << "*** Emitting noop\n");
  210. HazardRec->EmitNoop();
  211. Sequence.push_back(nullptr); // NULL here means noop
  212. ++NumNoops;
  213. ++CurCycle;
  214. }
  215. }
  216. #ifndef NDEBUG
  217. VerifyScheduledSequence(/*isBottomUp=*/false);
  218. #endif
  219. }
  220. //===----------------------------------------------------------------------===//
  221. // Public Constructor Functions
  222. //===----------------------------------------------------------------------===//
  223. /// createVLIWDAGScheduler - This creates a top-down list scheduler.
  224. ScheduleDAGSDNodes *
  225. llvm::createVLIWDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
  226. return new ScheduleDAGVLIW(*IS->MF, IS->AA, new ResourcePriorityQueue(IS));
  227. }