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