xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- GCNSchedStrategy.cpp - GCN Scheduler Strategy ---------------------===//
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 /// \file
100b57cec5SDimitry Andric /// This contains a MachineSchedStrategy implementation for maximizing wave
110b57cec5SDimitry Andric /// occupancy on GCN hardware.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "GCNSchedStrategy.h"
150b57cec5SDimitry Andric #include "AMDGPUSubtarget.h"
160b57cec5SDimitry Andric #include "SIInstrInfo.h"
170b57cec5SDimitry Andric #include "SIMachineFunctionInfo.h"
180b57cec5SDimitry Andric #include "SIRegisterInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
200b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #define DEBUG_TYPE "machine-scheduler"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric GCNMaxOccupancySchedStrategy::GCNMaxOccupancySchedStrategy(
270b57cec5SDimitry Andric     const MachineSchedContext *C) :
280b57cec5SDimitry Andric     GenericScheduler(C), TargetOccupancy(0), MF(nullptr) { }
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) {
310b57cec5SDimitry Andric   GenericScheduler::initialize(DAG);
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   MF = &DAG->MF;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   // FIXME: This is also necessary, because some passes that run after
400b57cec5SDimitry Andric   // scheduling and before regalloc increase register pressure.
410b57cec5SDimitry Andric   const int ErrorMargin = 3;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   SGPRExcessLimit = Context->RegClassInfo
440b57cec5SDimitry Andric     ->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass) - ErrorMargin;
450b57cec5SDimitry Andric   VGPRExcessLimit = Context->RegClassInfo
460b57cec5SDimitry Andric     ->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass) - ErrorMargin;
470b57cec5SDimitry Andric   if (TargetOccupancy) {
480b57cec5SDimitry Andric     SGPRCriticalLimit = ST.getMaxNumSGPRs(TargetOccupancy, true);
490b57cec5SDimitry Andric     VGPRCriticalLimit = ST.getMaxNumVGPRs(TargetOccupancy);
500b57cec5SDimitry Andric   } else {
510b57cec5SDimitry Andric     SGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
520b57cec5SDimitry Andric                                                     SRI->getSGPRPressureSet());
530b57cec5SDimitry Andric     VGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
540b57cec5SDimitry Andric                                                     SRI->getVGPRPressureSet());
550b57cec5SDimitry Andric   }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   SGPRCriticalLimit -= ErrorMargin;
580b57cec5SDimitry Andric   VGPRCriticalLimit -= ErrorMargin;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU,
620b57cec5SDimitry Andric                                      bool AtTop, const RegPressureTracker &RPTracker,
630b57cec5SDimitry Andric                                      const SIRegisterInfo *SRI,
640b57cec5SDimitry Andric                                      unsigned SGPRPressure,
650b57cec5SDimitry Andric                                      unsigned VGPRPressure) {
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   Cand.SU = SU;
680b57cec5SDimitry Andric   Cand.AtTop = AtTop;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // getDownwardPressure() and getUpwardPressure() make temporary changes to
710b57cec5SDimitry Andric   // the tracker, so we need to pass those function a non-const copy.
720b57cec5SDimitry Andric   RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
730b57cec5SDimitry Andric 
74*8bcb0991SDimitry Andric   Pressure.clear();
75*8bcb0991SDimitry Andric   MaxPressure.clear();
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   if (AtTop)
780b57cec5SDimitry Andric     TempTracker.getDownwardPressure(SU->getInstr(), Pressure, MaxPressure);
790b57cec5SDimitry Andric   else {
800b57cec5SDimitry Andric     // FIXME: I think for bottom up scheduling, the register pressure is cached
810b57cec5SDimitry Andric     // and can be retrieved by DAG->getPressureDif(SU).
820b57cec5SDimitry Andric     TempTracker.getUpwardPressure(SU->getInstr(), Pressure, MaxPressure);
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   unsigned NewSGPRPressure = Pressure[SRI->getSGPRPressureSet()];
860b57cec5SDimitry Andric   unsigned NewVGPRPressure = Pressure[SRI->getVGPRPressureSet()];
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   // If two instructions increase the pressure of different register sets
890b57cec5SDimitry Andric   // by the same amount, the generic scheduler will prefer to schedule the
900b57cec5SDimitry Andric   // instruction that increases the set with the least amount of registers,
910b57cec5SDimitry Andric   // which in our case would be SGPRs.  This is rarely what we want, so
920b57cec5SDimitry Andric   // when we report excess/critical register pressure, we do it either
930b57cec5SDimitry Andric   // only for VGPRs or only for SGPRs.
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   // FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs.
960b57cec5SDimitry Andric   const unsigned MaxVGPRPressureInc = 16;
970b57cec5SDimitry Andric   bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit;
980b57cec5SDimitry Andric   bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   // FIXME: We have to enter REG-EXCESS before we reach the actual threshold
1020b57cec5SDimitry Andric   // to increase the likelihood we don't go over the limits.  We should improve
1030b57cec5SDimitry Andric   // the analysis to look through dependencies to find the path with the least
1040b57cec5SDimitry Andric   // register pressure.
1050b57cec5SDimitry Andric 
106*8bcb0991SDimitry Andric   // We only need to update the RPDelta for instructions that increase register
107*8bcb0991SDimitry Andric   // pressure. Instructions that decrease or keep reg pressure the same will be
108*8bcb0991SDimitry Andric   // marked as RegExcess in tryCandidate() when they are compared with
109*8bcb0991SDimitry Andric   // instructions that increase the register pressure.
1100b57cec5SDimitry Andric   if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) {
1110b57cec5SDimitry Andric     Cand.RPDelta.Excess = PressureChange(SRI->getVGPRPressureSet());
1120b57cec5SDimitry Andric     Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit);
1130b57cec5SDimitry Andric   }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) {
1160b57cec5SDimitry Andric     Cand.RPDelta.Excess = PressureChange(SRI->getSGPRPressureSet());
1170b57cec5SDimitry Andric     Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit);
1180b57cec5SDimitry Andric   }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // Register pressure is considered 'CRITICAL' if it is approaching a value
1210b57cec5SDimitry Andric   // that would reduce the wave occupancy for the execution unit.  When
1220b57cec5SDimitry Andric   // register pressure is 'CRITICAL', increading SGPR and VGPR pressure both
1230b57cec5SDimitry Andric   // has the same cost, so we don't need to prefer one over the other.
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit;
1260b57cec5SDimitry Andric   int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   if (SGPRDelta >= 0 || VGPRDelta >= 0) {
1290b57cec5SDimitry Andric     if (SGPRDelta > VGPRDelta) {
1300b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax = PressureChange(SRI->getSGPRPressureSet());
1310b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta);
1320b57cec5SDimitry Andric     } else {
1330b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax = PressureChange(SRI->getVGPRPressureSet());
1340b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta);
1350b57cec5SDimitry Andric     }
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric // This function is mostly cut and pasted from
1400b57cec5SDimitry Andric // GenericScheduler::pickNodeFromQueue()
1410b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone,
1420b57cec5SDimitry Andric                                          const CandPolicy &ZonePolicy,
1430b57cec5SDimitry Andric                                          const RegPressureTracker &RPTracker,
1440b57cec5SDimitry Andric                                          SchedCandidate &Cand) {
1450b57cec5SDimitry Andric   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
1460b57cec5SDimitry Andric   ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos();
1470b57cec5SDimitry Andric   unsigned SGPRPressure = Pressure[SRI->getSGPRPressureSet()];
1480b57cec5SDimitry Andric   unsigned VGPRPressure = Pressure[SRI->getVGPRPressureSet()];
1490b57cec5SDimitry Andric   ReadyQueue &Q = Zone.Available;
1500b57cec5SDimitry Andric   for (SUnit *SU : Q) {
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric     SchedCandidate TryCand(ZonePolicy);
1530b57cec5SDimitry Andric     initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI,
1540b57cec5SDimitry Andric                   SGPRPressure, VGPRPressure);
1550b57cec5SDimitry Andric     // Pass SchedBoundary only when comparing nodes from the same boundary.
1560b57cec5SDimitry Andric     SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
1570b57cec5SDimitry Andric     GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg);
1580b57cec5SDimitry Andric     if (TryCand.Reason != NoCand) {
1590b57cec5SDimitry Andric       // Initialize resource delta if needed in case future heuristics query it.
1600b57cec5SDimitry Andric       if (TryCand.ResDelta == SchedResourceDelta())
1610b57cec5SDimitry Andric         TryCand.initResourceDelta(Zone.DAG, SchedModel);
1620b57cec5SDimitry Andric       Cand.setBest(TryCand);
163*8bcb0991SDimitry Andric       LLVM_DEBUG(traceCandidate(Cand));
1640b57cec5SDimitry Andric     }
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric // This function is mostly cut and pasted from
1690b57cec5SDimitry Andric // GenericScheduler::pickNodeBidirectional()
1700b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) {
1710b57cec5SDimitry Andric   // Schedule as far as possible in the direction of no choice. This is most
1720b57cec5SDimitry Andric   // efficient, but also provides the best heuristics for CriticalPSets.
1730b57cec5SDimitry Andric   if (SUnit *SU = Bot.pickOnlyChoice()) {
1740b57cec5SDimitry Andric     IsTopNode = false;
1750b57cec5SDimitry Andric     return SU;
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric   if (SUnit *SU = Top.pickOnlyChoice()) {
1780b57cec5SDimitry Andric     IsTopNode = true;
1790b57cec5SDimitry Andric     return SU;
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric   // Set the bottom-up policy based on the state of the current bottom zone and
1820b57cec5SDimitry Andric   // the instructions outside the zone, including the top zone.
1830b57cec5SDimitry Andric   CandPolicy BotPolicy;
1840b57cec5SDimitry Andric   setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
1850b57cec5SDimitry Andric   // Set the top-down policy based on the state of the current top zone and
1860b57cec5SDimitry Andric   // the instructions outside the zone, including the bottom zone.
1870b57cec5SDimitry Andric   CandPolicy TopPolicy;
1880b57cec5SDimitry Andric   setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   // See if BotCand is still valid (because we previously scheduled from Top).
1910b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking from Bot:\n");
1920b57cec5SDimitry Andric   if (!BotCand.isValid() || BotCand.SU->isScheduled ||
1930b57cec5SDimitry Andric       BotCand.Policy != BotPolicy) {
1940b57cec5SDimitry Andric     BotCand.reset(CandPolicy());
1950b57cec5SDimitry Andric     pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
1960b57cec5SDimitry Andric     assert(BotCand.Reason != NoCand && "failed to find the first candidate");
1970b57cec5SDimitry Andric   } else {
1980b57cec5SDimitry Andric     LLVM_DEBUG(traceCandidate(BotCand));
199*8bcb0991SDimitry Andric #ifndef NDEBUG
200*8bcb0991SDimitry Andric     if (VerifyScheduling) {
201*8bcb0991SDimitry Andric       SchedCandidate TCand;
202*8bcb0991SDimitry Andric       TCand.reset(CandPolicy());
203*8bcb0991SDimitry Andric       pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), TCand);
204*8bcb0991SDimitry Andric       assert(TCand.SU == BotCand.SU &&
205*8bcb0991SDimitry Andric              "Last pick result should correspond to re-picking right now");
206*8bcb0991SDimitry Andric     }
207*8bcb0991SDimitry Andric #endif
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   // Check if the top Q has a better candidate.
2110b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking from Top:\n");
2120b57cec5SDimitry Andric   if (!TopCand.isValid() || TopCand.SU->isScheduled ||
2130b57cec5SDimitry Andric       TopCand.Policy != TopPolicy) {
2140b57cec5SDimitry Andric     TopCand.reset(CandPolicy());
2150b57cec5SDimitry Andric     pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
2160b57cec5SDimitry Andric     assert(TopCand.Reason != NoCand && "failed to find the first candidate");
2170b57cec5SDimitry Andric   } else {
2180b57cec5SDimitry Andric     LLVM_DEBUG(traceCandidate(TopCand));
219*8bcb0991SDimitry Andric #ifndef NDEBUG
220*8bcb0991SDimitry Andric     if (VerifyScheduling) {
221*8bcb0991SDimitry Andric       SchedCandidate TCand;
222*8bcb0991SDimitry Andric       TCand.reset(CandPolicy());
223*8bcb0991SDimitry Andric       pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TCand);
224*8bcb0991SDimitry Andric       assert(TCand.SU == TopCand.SU &&
225*8bcb0991SDimitry Andric            "Last pick result should correspond to re-picking right now");
226*8bcb0991SDimitry Andric     }
227*8bcb0991SDimitry Andric #endif
2280b57cec5SDimitry Andric   }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   // Pick best from BotCand and TopCand.
2310b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Top Cand: "; traceCandidate(TopCand);
2320b57cec5SDimitry Andric              dbgs() << "Bot Cand: "; traceCandidate(BotCand););
2330b57cec5SDimitry Andric   SchedCandidate Cand;
2340b57cec5SDimitry Andric   if (TopCand.Reason == BotCand.Reason) {
2350b57cec5SDimitry Andric     Cand = BotCand;
2360b57cec5SDimitry Andric     GenericSchedulerBase::CandReason TopReason = TopCand.Reason;
2370b57cec5SDimitry Andric     TopCand.Reason = NoCand;
2380b57cec5SDimitry Andric     GenericScheduler::tryCandidate(Cand, TopCand, nullptr);
2390b57cec5SDimitry Andric     if (TopCand.Reason != NoCand) {
2400b57cec5SDimitry Andric       Cand.setBest(TopCand);
2410b57cec5SDimitry Andric     } else {
2420b57cec5SDimitry Andric       TopCand.Reason = TopReason;
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric   } else {
2450b57cec5SDimitry Andric     if (TopCand.Reason == RegExcess && TopCand.RPDelta.Excess.getUnitInc() <= 0) {
2460b57cec5SDimitry Andric       Cand = TopCand;
2470b57cec5SDimitry Andric     } else if (BotCand.Reason == RegExcess && BotCand.RPDelta.Excess.getUnitInc() <= 0) {
2480b57cec5SDimitry Andric       Cand = BotCand;
2490b57cec5SDimitry Andric     } else if (TopCand.Reason == RegCritical && TopCand.RPDelta.CriticalMax.getUnitInc() <= 0) {
2500b57cec5SDimitry Andric       Cand = TopCand;
2510b57cec5SDimitry Andric     } else if (BotCand.Reason == RegCritical && BotCand.RPDelta.CriticalMax.getUnitInc() <= 0) {
2520b57cec5SDimitry Andric       Cand = BotCand;
2530b57cec5SDimitry Andric     } else {
2540b57cec5SDimitry Andric       if (BotCand.Reason > TopCand.Reason) {
2550b57cec5SDimitry Andric         Cand = TopCand;
2560b57cec5SDimitry Andric       } else {
2570b57cec5SDimitry Andric         Cand = BotCand;
2580b57cec5SDimitry Andric       }
2590b57cec5SDimitry Andric     }
2600b57cec5SDimitry Andric   }
2610b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking: "; traceCandidate(Cand););
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   IsTopNode = Cand.AtTop;
2640b57cec5SDimitry Andric   return Cand.SU;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric // This function is mostly cut and pasted from
2680b57cec5SDimitry Andric // GenericScheduler::pickNode()
2690b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) {
2700b57cec5SDimitry Andric   if (DAG->top() == DAG->bottom()) {
2710b57cec5SDimitry Andric     assert(Top.Available.empty() && Top.Pending.empty() &&
2720b57cec5SDimitry Andric            Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
2730b57cec5SDimitry Andric     return nullptr;
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric   SUnit *SU;
2760b57cec5SDimitry Andric   do {
2770b57cec5SDimitry Andric     if (RegionPolicy.OnlyTopDown) {
2780b57cec5SDimitry Andric       SU = Top.pickOnlyChoice();
2790b57cec5SDimitry Andric       if (!SU) {
2800b57cec5SDimitry Andric         CandPolicy NoPolicy;
2810b57cec5SDimitry Andric         TopCand.reset(NoPolicy);
2820b57cec5SDimitry Andric         pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
2830b57cec5SDimitry Andric         assert(TopCand.Reason != NoCand && "failed to find a candidate");
2840b57cec5SDimitry Andric         SU = TopCand.SU;
2850b57cec5SDimitry Andric       }
2860b57cec5SDimitry Andric       IsTopNode = true;
2870b57cec5SDimitry Andric     } else if (RegionPolicy.OnlyBottomUp) {
2880b57cec5SDimitry Andric       SU = Bot.pickOnlyChoice();
2890b57cec5SDimitry Andric       if (!SU) {
2900b57cec5SDimitry Andric         CandPolicy NoPolicy;
2910b57cec5SDimitry Andric         BotCand.reset(NoPolicy);
2920b57cec5SDimitry Andric         pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
2930b57cec5SDimitry Andric         assert(BotCand.Reason != NoCand && "failed to find a candidate");
2940b57cec5SDimitry Andric         SU = BotCand.SU;
2950b57cec5SDimitry Andric       }
2960b57cec5SDimitry Andric       IsTopNode = false;
2970b57cec5SDimitry Andric     } else {
2980b57cec5SDimitry Andric       SU = pickNodeBidirectional(IsTopNode);
2990b57cec5SDimitry Andric     }
3000b57cec5SDimitry Andric   } while (SU->isScheduled);
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   if (SU->isTopReady())
3030b57cec5SDimitry Andric     Top.removeReady(SU);
3040b57cec5SDimitry Andric   if (SU->isBottomReady())
3050b57cec5SDimitry Andric     Bot.removeReady(SU);
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") "
3080b57cec5SDimitry Andric                     << *SU->getInstr());
3090b57cec5SDimitry Andric   return SU;
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C,
3130b57cec5SDimitry Andric                         std::unique_ptr<MachineSchedStrategy> S) :
3140b57cec5SDimitry Andric   ScheduleDAGMILive(C, std::move(S)),
3150b57cec5SDimitry Andric   ST(MF.getSubtarget<GCNSubtarget>()),
3160b57cec5SDimitry Andric   MFI(*MF.getInfo<SIMachineFunctionInfo>()),
3170b57cec5SDimitry Andric   StartingOccupancy(MFI.getOccupancy()),
3180b57cec5SDimitry Andric   MinOccupancy(StartingOccupancy), Stage(0), RegionIdx(0) {
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n");
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric void GCNScheduleDAGMILive::schedule() {
3240b57cec5SDimitry Andric   if (Stage == 0) {
3250b57cec5SDimitry Andric     // Just record regions at the first pass.
3260b57cec5SDimitry Andric     Regions.push_back(std::make_pair(RegionBegin, RegionEnd));
3270b57cec5SDimitry Andric     return;
3280b57cec5SDimitry Andric   }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   std::vector<MachineInstr*> Unsched;
3310b57cec5SDimitry Andric   Unsched.reserve(NumRegionInstrs);
3320b57cec5SDimitry Andric   for (auto &I : *this) {
3330b57cec5SDimitry Andric     Unsched.push_back(&I);
3340b57cec5SDimitry Andric   }
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   GCNRegPressure PressureBefore;
3370b57cec5SDimitry Andric   if (LIS) {
3380b57cec5SDimitry Andric     PressureBefore = Pressure[RegionIdx];
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Pressure before scheduling:\nRegion live-ins:";
3410b57cec5SDimitry Andric                GCNRPTracker::printLiveRegs(dbgs(), LiveIns[RegionIdx], MRI);
3420b57cec5SDimitry Andric                dbgs() << "Region live-in pressure:  ";
3430b57cec5SDimitry Andric                llvm::getRegPressure(MRI, LiveIns[RegionIdx]).print(dbgs());
3440b57cec5SDimitry Andric                dbgs() << "Region register pressure: ";
3450b57cec5SDimitry Andric                PressureBefore.print(dbgs()));
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   ScheduleDAGMILive::schedule();
3490b57cec5SDimitry Andric   Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   if (!LIS)
3520b57cec5SDimitry Andric     return;
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   // Check the results of scheduling.
3550b57cec5SDimitry Andric   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
3560b57cec5SDimitry Andric   auto PressureAfter = getRealRegPressure();
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Pressure after scheduling: ";
3590b57cec5SDimitry Andric              PressureAfter.print(dbgs()));
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit &&
3620b57cec5SDimitry Andric       PressureAfter.getVGPRNum() <= S.VGPRCriticalLimit) {
3630b57cec5SDimitry Andric     Pressure[RegionIdx] = PressureAfter;
3640b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Pressure in desired limits, done.\n");
3650b57cec5SDimitry Andric     return;
3660b57cec5SDimitry Andric   }
3670b57cec5SDimitry Andric   unsigned Occ = MFI.getOccupancy();
3680b57cec5SDimitry Andric   unsigned WavesAfter = std::min(Occ, PressureAfter.getOccupancy(ST));
3690b57cec5SDimitry Andric   unsigned WavesBefore = std::min(Occ, PressureBefore.getOccupancy(ST));
3700b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore
3710b57cec5SDimitry Andric                     << ", after " << WavesAfter << ".\n");
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   // We could not keep current target occupancy because of the just scheduled
3740b57cec5SDimitry Andric   // region. Record new occupancy for next scheduling cycle.
3750b57cec5SDimitry Andric   unsigned NewOccupancy = std::max(WavesAfter, WavesBefore);
3760b57cec5SDimitry Andric   // Allow memory bound functions to drop to 4 waves if not limited by an
3770b57cec5SDimitry Andric   // attribute.
3780b57cec5SDimitry Andric   if (WavesAfter < WavesBefore && WavesAfter < MinOccupancy &&
3790b57cec5SDimitry Andric       WavesAfter >= MFI.getMinAllowedOccupancy()) {
3800b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Function is memory bound, allow occupancy drop up to "
3810b57cec5SDimitry Andric                       << MFI.getMinAllowedOccupancy() << " waves\n");
3820b57cec5SDimitry Andric     NewOccupancy = WavesAfter;
3830b57cec5SDimitry Andric   }
3840b57cec5SDimitry Andric   if (NewOccupancy < MinOccupancy) {
3850b57cec5SDimitry Andric     MinOccupancy = NewOccupancy;
3860b57cec5SDimitry Andric     MFI.limitOccupancy(MinOccupancy);
3870b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Occupancy lowered for the function to "
3880b57cec5SDimitry Andric                       << MinOccupancy << ".\n");
3890b57cec5SDimitry Andric   }
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   if (WavesAfter >= MinOccupancy) {
3920b57cec5SDimitry Andric     Pressure[RegionIdx] = PressureAfter;
3930b57cec5SDimitry Andric     return;
3940b57cec5SDimitry Andric   }
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Attempting to revert scheduling.\n");
3970b57cec5SDimitry Andric   RegionEnd = RegionBegin;
3980b57cec5SDimitry Andric   for (MachineInstr *MI : Unsched) {
3990b57cec5SDimitry Andric     if (MI->isDebugInstr())
4000b57cec5SDimitry Andric       continue;
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric     if (MI->getIterator() != RegionEnd) {
4030b57cec5SDimitry Andric       BB->remove(MI);
4040b57cec5SDimitry Andric       BB->insert(RegionEnd, MI);
4050b57cec5SDimitry Andric       if (!MI->isDebugInstr())
4060b57cec5SDimitry Andric         LIS->handleMove(*MI, true);
4070b57cec5SDimitry Andric     }
4080b57cec5SDimitry Andric     // Reset read-undef flags and update them later.
4090b57cec5SDimitry Andric     for (auto &Op : MI->operands())
4100b57cec5SDimitry Andric       if (Op.isReg() && Op.isDef())
4110b57cec5SDimitry Andric         Op.setIsUndef(false);
4120b57cec5SDimitry Andric     RegisterOperands RegOpers;
4130b57cec5SDimitry Andric     RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
4140b57cec5SDimitry Andric     if (!MI->isDebugInstr()) {
4150b57cec5SDimitry Andric       if (ShouldTrackLaneMasks) {
4160b57cec5SDimitry Andric         // Adjust liveness and add missing dead+read-undef flags.
4170b57cec5SDimitry Andric         SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
4180b57cec5SDimitry Andric         RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
4190b57cec5SDimitry Andric       } else {
4200b57cec5SDimitry Andric         // Adjust for missing dead-def flags.
4210b57cec5SDimitry Andric         RegOpers.detectDeadDefs(*MI, *LIS);
4220b57cec5SDimitry Andric       }
4230b57cec5SDimitry Andric     }
4240b57cec5SDimitry Andric     RegionEnd = MI->getIterator();
4250b57cec5SDimitry Andric     ++RegionEnd;
4260b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Scheduling " << *MI);
4270b57cec5SDimitry Andric   }
4280b57cec5SDimitry Andric   RegionBegin = Unsched.front()->getIterator();
4290b57cec5SDimitry Andric   Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   placeDebugValues();
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const {
4350b57cec5SDimitry Andric   GCNDownwardRPTracker RPTracker(*LIS);
4360b57cec5SDimitry Andric   RPTracker.advance(begin(), end(), &LiveIns[RegionIdx]);
4370b57cec5SDimitry Andric   return RPTracker.moveMaxPressure();
4380b57cec5SDimitry Andric }
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric void GCNScheduleDAGMILive::computeBlockPressure(const MachineBasicBlock *MBB) {
4410b57cec5SDimitry Andric   GCNDownwardRPTracker RPTracker(*LIS);
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   // If the block has the only successor then live-ins of that successor are
4440b57cec5SDimitry Andric   // live-outs of the current block. We can reuse calculated live set if the
4450b57cec5SDimitry Andric   // successor will be sent to scheduling past current block.
4460b57cec5SDimitry Andric   const MachineBasicBlock *OnlySucc = nullptr;
4470b57cec5SDimitry Andric   if (MBB->succ_size() == 1 && !(*MBB->succ_begin())->empty()) {
4480b57cec5SDimitry Andric     SlotIndexes *Ind = LIS->getSlotIndexes();
4490b57cec5SDimitry Andric     if (Ind->getMBBStartIdx(MBB) < Ind->getMBBStartIdx(*MBB->succ_begin()))
4500b57cec5SDimitry Andric       OnlySucc = *MBB->succ_begin();
4510b57cec5SDimitry Andric   }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   // Scheduler sends regions from the end of the block upwards.
4540b57cec5SDimitry Andric   size_t CurRegion = RegionIdx;
4550b57cec5SDimitry Andric   for (size_t E = Regions.size(); CurRegion != E; ++CurRegion)
4560b57cec5SDimitry Andric     if (Regions[CurRegion].first->getParent() != MBB)
4570b57cec5SDimitry Andric       break;
4580b57cec5SDimitry Andric   --CurRegion;
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   auto I = MBB->begin();
4610b57cec5SDimitry Andric   auto LiveInIt = MBBLiveIns.find(MBB);
4620b57cec5SDimitry Andric   if (LiveInIt != MBBLiveIns.end()) {
4630b57cec5SDimitry Andric     auto LiveIn = std::move(LiveInIt->second);
4640b57cec5SDimitry Andric     RPTracker.reset(*MBB->begin(), &LiveIn);
4650b57cec5SDimitry Andric     MBBLiveIns.erase(LiveInIt);
4660b57cec5SDimitry Andric   } else {
4670b57cec5SDimitry Andric     auto &Rgn = Regions[CurRegion];
4680b57cec5SDimitry Andric     I = Rgn.first;
4690b57cec5SDimitry Andric     auto *NonDbgMI = &*skipDebugInstructionsForward(Rgn.first, Rgn.second);
4700b57cec5SDimitry Andric     auto LRS = BBLiveInMap.lookup(NonDbgMI);
4710b57cec5SDimitry Andric     assert(isEqual(getLiveRegsBefore(*NonDbgMI, *LIS), LRS));
4720b57cec5SDimitry Andric     RPTracker.reset(*I, &LRS);
4730b57cec5SDimitry Andric   }
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   for ( ; ; ) {
4760b57cec5SDimitry Andric     I = RPTracker.getNext();
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric     if (Regions[CurRegion].first == I) {
4790b57cec5SDimitry Andric       LiveIns[CurRegion] = RPTracker.getLiveRegs();
4800b57cec5SDimitry Andric       RPTracker.clearMaxPressure();
4810b57cec5SDimitry Andric     }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     if (Regions[CurRegion].second == I) {
4840b57cec5SDimitry Andric       Pressure[CurRegion] = RPTracker.moveMaxPressure();
4850b57cec5SDimitry Andric       if (CurRegion-- == RegionIdx)
4860b57cec5SDimitry Andric         break;
4870b57cec5SDimitry Andric     }
4880b57cec5SDimitry Andric     RPTracker.advanceToNext();
4890b57cec5SDimitry Andric     RPTracker.advanceBeforeNext();
4900b57cec5SDimitry Andric   }
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric   if (OnlySucc) {
4930b57cec5SDimitry Andric     if (I != MBB->end()) {
4940b57cec5SDimitry Andric       RPTracker.advanceToNext();
4950b57cec5SDimitry Andric       RPTracker.advance(MBB->end());
4960b57cec5SDimitry Andric     }
4970b57cec5SDimitry Andric     RPTracker.reset(*OnlySucc->begin(), &RPTracker.getLiveRegs());
4980b57cec5SDimitry Andric     RPTracker.advanceBeforeNext();
4990b57cec5SDimitry Andric     MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs();
5000b57cec5SDimitry Andric   }
5010b57cec5SDimitry Andric }
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet>
5040b57cec5SDimitry Andric GCNScheduleDAGMILive::getBBLiveInMap() const {
5050b57cec5SDimitry Andric   assert(!Regions.empty());
5060b57cec5SDimitry Andric   std::vector<MachineInstr *> BBStarters;
5070b57cec5SDimitry Andric   BBStarters.reserve(Regions.size());
5080b57cec5SDimitry Andric   auto I = Regions.rbegin(), E = Regions.rend();
5090b57cec5SDimitry Andric   auto *BB = I->first->getParent();
5100b57cec5SDimitry Andric   do {
5110b57cec5SDimitry Andric     auto *MI = &*skipDebugInstructionsForward(I->first, I->second);
5120b57cec5SDimitry Andric     BBStarters.push_back(MI);
5130b57cec5SDimitry Andric     do {
5140b57cec5SDimitry Andric       ++I;
5150b57cec5SDimitry Andric     } while (I != E && I->first->getParent() == BB);
5160b57cec5SDimitry Andric   } while (I != E);
5170b57cec5SDimitry Andric   return getLiveRegMap(BBStarters, false /*After*/, *LIS);
5180b57cec5SDimitry Andric }
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric void GCNScheduleDAGMILive::finalizeSchedule() {
5210b57cec5SDimitry Andric   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
5220b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n");
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric   LiveIns.resize(Regions.size());
5250b57cec5SDimitry Andric   Pressure.resize(Regions.size());
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   if (!Regions.empty())
5280b57cec5SDimitry Andric     BBLiveInMap = getBBLiveInMap();
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric   do {
5310b57cec5SDimitry Andric     Stage++;
5320b57cec5SDimitry Andric     RegionIdx = 0;
5330b57cec5SDimitry Andric     MachineBasicBlock *MBB = nullptr;
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric     if (Stage > 1) {
5360b57cec5SDimitry Andric       // Retry function scheduling if we found resulting occupancy and it is
5370b57cec5SDimitry Andric       // lower than used for first pass scheduling. This will give more freedom
5380b57cec5SDimitry Andric       // to schedule low register pressure blocks.
5390b57cec5SDimitry Andric       // Code is partially copied from MachineSchedulerBase::scheduleRegions().
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric       if (!LIS || StartingOccupancy <= MinOccupancy)
5420b57cec5SDimitry Andric         break;
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric       LLVM_DEBUG(
5450b57cec5SDimitry Andric           dbgs()
5460b57cec5SDimitry Andric           << "Retrying function scheduling with lowest recorded occupancy "
5470b57cec5SDimitry Andric           << MinOccupancy << ".\n");
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric       S.setTargetOccupancy(MinOccupancy);
5500b57cec5SDimitry Andric     }
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric     for (auto Region : Regions) {
5530b57cec5SDimitry Andric       RegionBegin = Region.first;
5540b57cec5SDimitry Andric       RegionEnd = Region.second;
5550b57cec5SDimitry Andric 
5560b57cec5SDimitry Andric       if (RegionBegin->getParent() != MBB) {
5570b57cec5SDimitry Andric         if (MBB) finishBlock();
5580b57cec5SDimitry Andric         MBB = RegionBegin->getParent();
5590b57cec5SDimitry Andric         startBlock(MBB);
5600b57cec5SDimitry Andric         if (Stage == 1)
5610b57cec5SDimitry Andric           computeBlockPressure(MBB);
5620b57cec5SDimitry Andric       }
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric       unsigned NumRegionInstrs = std::distance(begin(), end());
5650b57cec5SDimitry Andric       enterRegion(MBB, begin(), end(), NumRegionInstrs);
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric       // Skip empty scheduling regions (0 or 1 schedulable instructions).
5680b57cec5SDimitry Andric       if (begin() == end() || begin() == std::prev(end())) {
5690b57cec5SDimitry Andric         exitRegion();
5700b57cec5SDimitry Andric         continue;
5710b57cec5SDimitry Andric       }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "********** MI Scheduling **********\n");
5740b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << MF.getName() << ":" << printMBBReference(*MBB) << " "
5750b57cec5SDimitry Andric                         << MBB->getName() << "\n  From: " << *begin()
5760b57cec5SDimitry Andric                         << "    To: ";
5770b57cec5SDimitry Andric                  if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
5780b57cec5SDimitry Andric                  else dbgs() << "End";
5790b57cec5SDimitry Andric                  dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric       schedule();
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric       exitRegion();
5840b57cec5SDimitry Andric       ++RegionIdx;
5850b57cec5SDimitry Andric     }
5860b57cec5SDimitry Andric     finishBlock();
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric   } while (Stage < 2);
5890b57cec5SDimitry Andric }
590