ScheduleDAGVLIW.cpp 9.2 KB

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