ScheduleDAGVLIW.cpp 9.3 KB

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