xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SelectionDAG/ScheduleDAGVLIW.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- ScheduleDAGVLIW.cpp - SelectionDAG list scheduler for VLIW -*- C++ -*-=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements a top-down list scheduler, using standard algorithms.
100b57cec5SDimitry Andric // The basic approach uses a priority queue of available nodes to schedule.
110b57cec5SDimitry Andric // One at a time, nodes are taken from the priority queue (thus in priority
120b57cec5SDimitry Andric // order), checked for legality to schedule, and emitted if legal.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric // Nodes may not be legal to schedule either due to structural hazards (e.g.
150b57cec5SDimitry Andric // pipeline or resource constraints) or because an input to the instruction has
160b57cec5SDimitry Andric // not completed execution.
170b57cec5SDimitry Andric //
180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #include "ScheduleDAGSDNodes.h"
210b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/ResourcePriorityQueue.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/SchedulerRegistry.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGISel.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
280b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
290b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
300b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric using namespace llvm;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric #define DEBUG_TYPE "pre-RA-sched"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric STATISTIC(NumNoops , "Number of noops inserted");
360b57cec5SDimitry Andric STATISTIC(NumStalls, "Number of pipeline stalls");
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric static RegisterScheduler
390b57cec5SDimitry Andric   VLIWScheduler("vliw-td", "VLIW scheduler",
400b57cec5SDimitry Andric                 createVLIWDAGScheduler);
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric namespace {
430b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
440b57cec5SDimitry Andric /// ScheduleDAGVLIW - The actual DFA list scheduler implementation.  This
450b57cec5SDimitry Andric /// supports / top-down scheduling.
460b57cec5SDimitry Andric ///
470b57cec5SDimitry Andric class ScheduleDAGVLIW : public ScheduleDAGSDNodes {
480b57cec5SDimitry Andric private:
490b57cec5SDimitry Andric   /// AvailableQueue - The priority queue to use for the available SUnits.
500b57cec5SDimitry Andric   ///
510b57cec5SDimitry Andric   SchedulingPriorityQueue *AvailableQueue;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   /// PendingQueue - This contains all of the instructions whose operands have
540b57cec5SDimitry Andric   /// been issued, but their results are not ready yet (due to the latency of
550b57cec5SDimitry Andric   /// the operation).  Once the operands become available, the instruction is
560b57cec5SDimitry Andric   /// added to the AvailableQueue.
570b57cec5SDimitry Andric   std::vector<SUnit*> PendingQueue;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   /// HazardRec - The hazard recognizer to use.
600b57cec5SDimitry Andric   ScheduleHazardRecognizer *HazardRec;
610b57cec5SDimitry Andric 
628bcb0991SDimitry Andric   /// AA - AAResults for making memory reference queries.
638bcb0991SDimitry Andric   AAResults *AA;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric public:
668bcb0991SDimitry Andric   ScheduleDAGVLIW(MachineFunction &mf, AAResults *aa,
670b57cec5SDimitry Andric                   SchedulingPriorityQueue *availqueue)
680b57cec5SDimitry Andric       : ScheduleDAGSDNodes(mf), AvailableQueue(availqueue), AA(aa) {
690b57cec5SDimitry Andric     const TargetSubtargetInfo &STI = mf.getSubtarget();
700b57cec5SDimitry Andric     HazardRec = STI.getInstrInfo()->CreateTargetHazardRecognizer(&STI, this);
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   ~ScheduleDAGVLIW() override {
740b57cec5SDimitry Andric     delete HazardRec;
750b57cec5SDimitry Andric     delete AvailableQueue;
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   void Schedule() override;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric private:
810b57cec5SDimitry Andric   void releaseSucc(SUnit *SU, const SDep &D);
820b57cec5SDimitry Andric   void releaseSuccessors(SUnit *SU);
830b57cec5SDimitry Andric   void scheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
840b57cec5SDimitry Andric   void listScheduleTopDown();
850b57cec5SDimitry Andric };
860b57cec5SDimitry Andric }  // end anonymous namespace
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric /// Schedule - Schedule the DAG using list scheduling.
890b57cec5SDimitry Andric void ScheduleDAGVLIW::Schedule() {
900b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** List Scheduling " << printMBBReference(*BB)
910b57cec5SDimitry Andric                     << " '" << BB->getName() << "' **********\n");
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   // Build the scheduling graph.
940b57cec5SDimitry Andric   BuildSchedGraph(AA);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   AvailableQueue->initNodes(SUnits);
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   listScheduleTopDown();
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   AvailableQueue->releaseState();
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1040b57cec5SDimitry Andric //  Top-Down Scheduling
1050b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric /// releaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
1080b57cec5SDimitry Andric /// the PendingQueue if the count reaches zero. Also update its cycle bound.
1090b57cec5SDimitry Andric void ScheduleDAGVLIW::releaseSucc(SUnit *SU, const SDep &D) {
1100b57cec5SDimitry Andric   SUnit *SuccSU = D.getSUnit();
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric #ifndef NDEBUG
1130b57cec5SDimitry Andric   if (SuccSU->NumPredsLeft == 0) {
1140b57cec5SDimitry Andric     dbgs() << "*** Scheduling failed! ***\n";
1150b57cec5SDimitry Andric     dumpNode(*SuccSU);
1160b57cec5SDimitry Andric     dbgs() << " has been released too many times!\n";
1170b57cec5SDimitry Andric     llvm_unreachable(nullptr);
1180b57cec5SDimitry Andric   }
1190b57cec5SDimitry Andric #endif
1200b57cec5SDimitry Andric   assert(!D.isWeak() && "unexpected artificial DAG edge");
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   --SuccSU->NumPredsLeft;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   SuccSU->setDepthToAtLeast(SU->getDepth() + D.getLatency());
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   // If all the node's predecessors are scheduled, this node is ready
1270b57cec5SDimitry Andric   // to be scheduled. Ignore the special ExitSU node.
1280b57cec5SDimitry Andric   if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) {
1290b57cec5SDimitry Andric     PendingQueue.push_back(SuccSU);
1300b57cec5SDimitry Andric   }
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric void ScheduleDAGVLIW::releaseSuccessors(SUnit *SU) {
1340b57cec5SDimitry Andric   // Top down: release successors.
135fe6060f1SDimitry Andric   for (SDep &Succ : SU->Succs) {
136fe6060f1SDimitry Andric     assert(!Succ.isAssignedRegDep() &&
1370b57cec5SDimitry Andric            "The list-td scheduler doesn't yet support physreg dependencies!");
1380b57cec5SDimitry Andric 
139fe6060f1SDimitry Andric     releaseSucc(SU, Succ);
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric /// scheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1440b57cec5SDimitry Andric /// count of its successors. If a successor pending count is zero, add it to
1450b57cec5SDimitry Andric /// the Available queue.
1460b57cec5SDimitry Andric void ScheduleDAGVLIW::scheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
1470b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
1480b57cec5SDimitry Andric   LLVM_DEBUG(dumpNode(*SU));
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   Sequence.push_back(SU);
1510b57cec5SDimitry Andric   assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
1520b57cec5SDimitry Andric   SU->setDepthToAtLeast(CurCycle);
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   releaseSuccessors(SU);
1550b57cec5SDimitry Andric   SU->isScheduled = true;
1560b57cec5SDimitry Andric   AvailableQueue->scheduledNode(SU);
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric /// listScheduleTopDown - The main loop of list scheduling for top-down
1600b57cec5SDimitry Andric /// schedulers.
1610b57cec5SDimitry Andric void ScheduleDAGVLIW::listScheduleTopDown() {
1620b57cec5SDimitry Andric   unsigned CurCycle = 0;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   // Release any successors of the special Entry node.
1650b57cec5SDimitry Andric   releaseSuccessors(&EntrySU);
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   // All leaves to AvailableQueue.
1680eae32dcSDimitry Andric   for (SUnit &SU : SUnits) {
1690b57cec5SDimitry Andric     // It is available if it has no predecessors.
1700eae32dcSDimitry Andric     if (SU.Preds.empty()) {
1710eae32dcSDimitry Andric       AvailableQueue->push(&SU);
1720eae32dcSDimitry Andric       SU.isAvailable = true;
1730b57cec5SDimitry Andric     }
1740b57cec5SDimitry Andric   }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   // While AvailableQueue is not empty, grab the node with the highest
1770b57cec5SDimitry Andric   // priority. If it is not ready put it back.  Schedule the node.
1780b57cec5SDimitry Andric   std::vector<SUnit*> NotReady;
1790b57cec5SDimitry Andric   Sequence.reserve(SUnits.size());
1800b57cec5SDimitry Andric   while (!AvailableQueue->empty() || !PendingQueue.empty()) {
1810b57cec5SDimitry Andric     // Check to see if any of the pending instructions are ready to issue.  If
1820b57cec5SDimitry Andric     // so, add them to the available queue.
1830b57cec5SDimitry Andric     for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
1840b57cec5SDimitry Andric       if (PendingQueue[i]->getDepth() == CurCycle) {
1850b57cec5SDimitry Andric         AvailableQueue->push(PendingQueue[i]);
1860b57cec5SDimitry Andric         PendingQueue[i]->isAvailable = true;
1870b57cec5SDimitry Andric         PendingQueue[i] = PendingQueue.back();
1880b57cec5SDimitry Andric         PendingQueue.pop_back();
1890b57cec5SDimitry Andric         --i; --e;
1900b57cec5SDimitry Andric       }
1910b57cec5SDimitry Andric       else {
1920b57cec5SDimitry Andric         assert(PendingQueue[i]->getDepth() > CurCycle && "Negative latency?");
1930b57cec5SDimitry Andric       }
1940b57cec5SDimitry Andric     }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric     // If there are no instructions available, don't try to issue anything, and
1970b57cec5SDimitry Andric     // don't advance the hazard recognizer.
1980b57cec5SDimitry Andric     if (AvailableQueue->empty()) {
1990b57cec5SDimitry Andric       // Reset DFA state.
2000b57cec5SDimitry Andric       AvailableQueue->scheduledNode(nullptr);
2010b57cec5SDimitry Andric       ++CurCycle;
2020b57cec5SDimitry Andric       continue;
2030b57cec5SDimitry Andric     }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric     SUnit *FoundSUnit = nullptr;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric     bool HasNoopHazards = false;
2080b57cec5SDimitry Andric     while (!AvailableQueue->empty()) {
2090b57cec5SDimitry Andric       SUnit *CurSUnit = AvailableQueue->pop();
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric       ScheduleHazardRecognizer::HazardType HT =
2120b57cec5SDimitry Andric         HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
2130b57cec5SDimitry Andric       if (HT == ScheduleHazardRecognizer::NoHazard) {
2140b57cec5SDimitry Andric         FoundSUnit = CurSUnit;
2150b57cec5SDimitry Andric         break;
2160b57cec5SDimitry Andric       }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric       // Remember if this is a noop hazard.
2190b57cec5SDimitry Andric       HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric       NotReady.push_back(CurSUnit);
2220b57cec5SDimitry Andric     }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric     // Add the nodes that aren't ready back onto the available list.
2250b57cec5SDimitry Andric     if (!NotReady.empty()) {
2260b57cec5SDimitry Andric       AvailableQueue->push_all(NotReady);
2270b57cec5SDimitry Andric       NotReady.clear();
2280b57cec5SDimitry Andric     }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric     // If we found a node to schedule, do it now.
2310b57cec5SDimitry Andric     if (FoundSUnit) {
2320b57cec5SDimitry Andric       scheduleNodeTopDown(FoundSUnit, CurCycle);
2330b57cec5SDimitry Andric       HazardRec->EmitInstruction(FoundSUnit);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric       // If this is a pseudo-op node, we don't want to increment the current
2360b57cec5SDimitry Andric       // cycle.
2370b57cec5SDimitry Andric       if (FoundSUnit->Latency)  // Don't increment CurCycle for pseudo-ops!
2380b57cec5SDimitry Andric         ++CurCycle;
2390b57cec5SDimitry Andric     } else if (!HasNoopHazards) {
2400b57cec5SDimitry Andric       // Otherwise, we have a pipeline stall, but no other problem, just advance
2410b57cec5SDimitry Andric       // the current cycle and try again.
2420b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "*** Advancing cycle, no work to do\n");
2430b57cec5SDimitry Andric       HazardRec->AdvanceCycle();
2440b57cec5SDimitry Andric       ++NumStalls;
2450b57cec5SDimitry Andric       ++CurCycle;
2460b57cec5SDimitry Andric     } else {
2470b57cec5SDimitry Andric       // Otherwise, we have no instructions to issue and we have instructions
2480b57cec5SDimitry Andric       // that will fault if we don't do this right.  This is the case for
2490b57cec5SDimitry Andric       // processors without pipeline interlocks and other cases.
2500b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "*** Emitting noop\n");
2510b57cec5SDimitry Andric       HazardRec->EmitNoop();
2520b57cec5SDimitry Andric       Sequence.push_back(nullptr);   // NULL here means noop
2530b57cec5SDimitry Andric       ++NumNoops;
2540b57cec5SDimitry Andric       ++CurCycle;
2550b57cec5SDimitry Andric     }
2560b57cec5SDimitry Andric   }
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric #ifndef NDEBUG
2590b57cec5SDimitry Andric   VerifyScheduledSequence(/*isBottomUp=*/false);
2600b57cec5SDimitry Andric #endif
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2640b57cec5SDimitry Andric //                         Public Constructor Functions
2650b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric /// createVLIWDAGScheduler - This creates a top-down list scheduler.
268*5f757f3fSDimitry Andric ScheduleDAGSDNodes *llvm::createVLIWDAGScheduler(SelectionDAGISel *IS,
269*5f757f3fSDimitry Andric                                                  CodeGenOptLevel) {
2700b57cec5SDimitry Andric   return new ScheduleDAGVLIW(*IS->MF, IS->AA, new ResourcePriorityQueue(IS));
2710b57cec5SDimitry Andric }
272