ScheduleDAGVLIW.cpp 9.3 KB

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