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