xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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 "SIMachineFunctionInfo.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #define DEBUG_TYPE "machine-scheduler"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric GCNMaxOccupancySchedStrategy::GCNMaxOccupancySchedStrategy(
220b57cec5SDimitry Andric     const MachineSchedContext *C) :
23*fe6060f1SDimitry Andric     GenericScheduler(C), TargetOccupancy(0), HasClusteredNodes(false),
24*fe6060f1SDimitry Andric     HasExcessPressure(false), MF(nullptr) { }
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) {
270b57cec5SDimitry Andric   GenericScheduler::initialize(DAG);
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   MF = &DAG->MF;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   // FIXME: This is also necessary, because some passes that run after
360b57cec5SDimitry Andric   // scheduling and before regalloc increase register pressure.
370b57cec5SDimitry Andric   const int ErrorMargin = 3;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   SGPRExcessLimit = Context->RegClassInfo
400b57cec5SDimitry Andric     ->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass) - ErrorMargin;
410b57cec5SDimitry Andric   VGPRExcessLimit = Context->RegClassInfo
420b57cec5SDimitry Andric     ->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass) - ErrorMargin;
430b57cec5SDimitry Andric   if (TargetOccupancy) {
440b57cec5SDimitry Andric     SGPRCriticalLimit = ST.getMaxNumSGPRs(TargetOccupancy, true);
450b57cec5SDimitry Andric     VGPRCriticalLimit = ST.getMaxNumVGPRs(TargetOccupancy);
460b57cec5SDimitry Andric   } else {
470b57cec5SDimitry Andric     SGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
485ffd83dbSDimitry Andric         AMDGPU::RegisterPressureSets::SReg_32);
490b57cec5SDimitry Andric     VGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
505ffd83dbSDimitry Andric         AMDGPU::RegisterPressureSets::VGPR_32);
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   SGPRCriticalLimit -= ErrorMargin;
540b57cec5SDimitry Andric   VGPRCriticalLimit -= ErrorMargin;
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU,
580b57cec5SDimitry Andric                                      bool AtTop, const RegPressureTracker &RPTracker,
590b57cec5SDimitry Andric                                      const SIRegisterInfo *SRI,
600b57cec5SDimitry Andric                                      unsigned SGPRPressure,
610b57cec5SDimitry Andric                                      unsigned VGPRPressure) {
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   Cand.SU = SU;
640b57cec5SDimitry Andric   Cand.AtTop = AtTop;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   // getDownwardPressure() and getUpwardPressure() make temporary changes to
670b57cec5SDimitry Andric   // the tracker, so we need to pass those function a non-const copy.
680b57cec5SDimitry Andric   RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
690b57cec5SDimitry Andric 
708bcb0991SDimitry Andric   Pressure.clear();
718bcb0991SDimitry Andric   MaxPressure.clear();
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   if (AtTop)
740b57cec5SDimitry Andric     TempTracker.getDownwardPressure(SU->getInstr(), Pressure, MaxPressure);
750b57cec5SDimitry Andric   else {
760b57cec5SDimitry Andric     // FIXME: I think for bottom up scheduling, the register pressure is cached
770b57cec5SDimitry Andric     // and can be retrieved by DAG->getPressureDif(SU).
780b57cec5SDimitry Andric     TempTracker.getUpwardPressure(SU->getInstr(), Pressure, MaxPressure);
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
815ffd83dbSDimitry Andric   unsigned NewSGPRPressure = Pressure[AMDGPU::RegisterPressureSets::SReg_32];
825ffd83dbSDimitry Andric   unsigned NewVGPRPressure = Pressure[AMDGPU::RegisterPressureSets::VGPR_32];
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // If two instructions increase the pressure of different register sets
850b57cec5SDimitry Andric   // by the same amount, the generic scheduler will prefer to schedule the
860b57cec5SDimitry Andric   // instruction that increases the set with the least amount of registers,
870b57cec5SDimitry Andric   // which in our case would be SGPRs.  This is rarely what we want, so
880b57cec5SDimitry Andric   // when we report excess/critical register pressure, we do it either
890b57cec5SDimitry Andric   // only for VGPRs or only for SGPRs.
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   // FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs.
920b57cec5SDimitry Andric   const unsigned MaxVGPRPressureInc = 16;
930b57cec5SDimitry Andric   bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit;
940b57cec5SDimitry Andric   bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // FIXME: We have to enter REG-EXCESS before we reach the actual threshold
980b57cec5SDimitry Andric   // to increase the likelihood we don't go over the limits.  We should improve
990b57cec5SDimitry Andric   // the analysis to look through dependencies to find the path with the least
1000b57cec5SDimitry Andric   // register pressure.
1010b57cec5SDimitry Andric 
1028bcb0991SDimitry Andric   // We only need to update the RPDelta for instructions that increase register
1038bcb0991SDimitry Andric   // pressure. Instructions that decrease or keep reg pressure the same will be
1048bcb0991SDimitry Andric   // marked as RegExcess in tryCandidate() when they are compared with
1058bcb0991SDimitry Andric   // instructions that increase the register pressure.
1060b57cec5SDimitry Andric   if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) {
107*fe6060f1SDimitry Andric     HasExcessPressure = true;
1085ffd83dbSDimitry Andric     Cand.RPDelta.Excess = PressureChange(AMDGPU::RegisterPressureSets::VGPR_32);
1090b57cec5SDimitry Andric     Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit);
1100b57cec5SDimitry Andric   }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) {
113*fe6060f1SDimitry Andric     HasExcessPressure = true;
1145ffd83dbSDimitry Andric     Cand.RPDelta.Excess = PressureChange(AMDGPU::RegisterPressureSets::SReg_32);
1150b57cec5SDimitry Andric     Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit);
1160b57cec5SDimitry Andric   }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   // Register pressure is considered 'CRITICAL' if it is approaching a value
1190b57cec5SDimitry Andric   // that would reduce the wave occupancy for the execution unit.  When
1200b57cec5SDimitry Andric   // register pressure is 'CRITICAL', increading SGPR and VGPR pressure both
1210b57cec5SDimitry Andric   // has the same cost, so we don't need to prefer one over the other.
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit;
1240b57cec5SDimitry Andric   int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   if (SGPRDelta >= 0 || VGPRDelta >= 0) {
127*fe6060f1SDimitry Andric     HasExcessPressure = true;
1280b57cec5SDimitry Andric     if (SGPRDelta > VGPRDelta) {
1295ffd83dbSDimitry Andric       Cand.RPDelta.CriticalMax =
1305ffd83dbSDimitry Andric         PressureChange(AMDGPU::RegisterPressureSets::SReg_32);
1310b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta);
1320b57cec5SDimitry Andric     } else {
1335ffd83dbSDimitry Andric       Cand.RPDelta.CriticalMax =
1345ffd83dbSDimitry Andric         PressureChange(AMDGPU::RegisterPressureSets::VGPR_32);
1350b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta);
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric // This function is mostly cut and pasted from
1410b57cec5SDimitry Andric // GenericScheduler::pickNodeFromQueue()
1420b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone,
1430b57cec5SDimitry Andric                                          const CandPolicy &ZonePolicy,
1440b57cec5SDimitry Andric                                          const RegPressureTracker &RPTracker,
1450b57cec5SDimitry Andric                                          SchedCandidate &Cand) {
1460b57cec5SDimitry Andric   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
1470b57cec5SDimitry Andric   ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos();
1485ffd83dbSDimitry Andric   unsigned SGPRPressure = Pressure[AMDGPU::RegisterPressureSets::SReg_32];
1495ffd83dbSDimitry Andric   unsigned VGPRPressure = Pressure[AMDGPU::RegisterPressureSets::VGPR_32];
1500b57cec5SDimitry Andric   ReadyQueue &Q = Zone.Available;
1510b57cec5SDimitry Andric   for (SUnit *SU : Q) {
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     SchedCandidate TryCand(ZonePolicy);
1540b57cec5SDimitry Andric     initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI,
1550b57cec5SDimitry Andric                   SGPRPressure, VGPRPressure);
1560b57cec5SDimitry Andric     // Pass SchedBoundary only when comparing nodes from the same boundary.
1570b57cec5SDimitry Andric     SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
1580b57cec5SDimitry Andric     GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg);
1590b57cec5SDimitry Andric     if (TryCand.Reason != NoCand) {
1600b57cec5SDimitry Andric       // Initialize resource delta if needed in case future heuristics query it.
1610b57cec5SDimitry Andric       if (TryCand.ResDelta == SchedResourceDelta())
1620b57cec5SDimitry Andric         TryCand.initResourceDelta(Zone.DAG, SchedModel);
1630b57cec5SDimitry Andric       Cand.setBest(TryCand);
1648bcb0991SDimitry Andric       LLVM_DEBUG(traceCandidate(Cand));
1650b57cec5SDimitry Andric     }
1660b57cec5SDimitry Andric   }
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric // This function is mostly cut and pasted from
1700b57cec5SDimitry Andric // GenericScheduler::pickNodeBidirectional()
1710b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) {
1720b57cec5SDimitry Andric   // Schedule as far as possible in the direction of no choice. This is most
1730b57cec5SDimitry Andric   // efficient, but also provides the best heuristics for CriticalPSets.
1740b57cec5SDimitry Andric   if (SUnit *SU = Bot.pickOnlyChoice()) {
1750b57cec5SDimitry Andric     IsTopNode = false;
1760b57cec5SDimitry Andric     return SU;
1770b57cec5SDimitry Andric   }
1780b57cec5SDimitry Andric   if (SUnit *SU = Top.pickOnlyChoice()) {
1790b57cec5SDimitry Andric     IsTopNode = true;
1800b57cec5SDimitry Andric     return SU;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric   // Set the bottom-up policy based on the state of the current bottom zone and
1830b57cec5SDimitry Andric   // the instructions outside the zone, including the top zone.
1840b57cec5SDimitry Andric   CandPolicy BotPolicy;
1850b57cec5SDimitry Andric   setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
1860b57cec5SDimitry Andric   // Set the top-down policy based on the state of the current top zone and
1870b57cec5SDimitry Andric   // the instructions outside the zone, including the bottom zone.
1880b57cec5SDimitry Andric   CandPolicy TopPolicy;
1890b57cec5SDimitry Andric   setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   // See if BotCand is still valid (because we previously scheduled from Top).
1920b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking from Bot:\n");
1930b57cec5SDimitry Andric   if (!BotCand.isValid() || BotCand.SU->isScheduled ||
1940b57cec5SDimitry Andric       BotCand.Policy != BotPolicy) {
1950b57cec5SDimitry Andric     BotCand.reset(CandPolicy());
1960b57cec5SDimitry Andric     pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
1970b57cec5SDimitry Andric     assert(BotCand.Reason != NoCand && "failed to find the first candidate");
1980b57cec5SDimitry Andric   } else {
1990b57cec5SDimitry Andric     LLVM_DEBUG(traceCandidate(BotCand));
2008bcb0991SDimitry Andric #ifndef NDEBUG
2018bcb0991SDimitry Andric     if (VerifyScheduling) {
2028bcb0991SDimitry Andric       SchedCandidate TCand;
2038bcb0991SDimitry Andric       TCand.reset(CandPolicy());
2048bcb0991SDimitry Andric       pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), TCand);
2058bcb0991SDimitry Andric       assert(TCand.SU == BotCand.SU &&
2068bcb0991SDimitry Andric              "Last pick result should correspond to re-picking right now");
2078bcb0991SDimitry Andric     }
2088bcb0991SDimitry Andric #endif
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   // Check if the top Q has a better candidate.
2120b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking from Top:\n");
2130b57cec5SDimitry Andric   if (!TopCand.isValid() || TopCand.SU->isScheduled ||
2140b57cec5SDimitry Andric       TopCand.Policy != TopPolicy) {
2150b57cec5SDimitry Andric     TopCand.reset(CandPolicy());
2160b57cec5SDimitry Andric     pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
2170b57cec5SDimitry Andric     assert(TopCand.Reason != NoCand && "failed to find the first candidate");
2180b57cec5SDimitry Andric   } else {
2190b57cec5SDimitry Andric     LLVM_DEBUG(traceCandidate(TopCand));
2208bcb0991SDimitry Andric #ifndef NDEBUG
2218bcb0991SDimitry Andric     if (VerifyScheduling) {
2228bcb0991SDimitry Andric       SchedCandidate TCand;
2238bcb0991SDimitry Andric       TCand.reset(CandPolicy());
2248bcb0991SDimitry Andric       pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TCand);
2258bcb0991SDimitry Andric       assert(TCand.SU == TopCand.SU &&
2268bcb0991SDimitry Andric            "Last pick result should correspond to re-picking right now");
2278bcb0991SDimitry Andric     }
2288bcb0991SDimitry Andric #endif
2290b57cec5SDimitry Andric   }
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   // Pick best from BotCand and TopCand.
2320b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Top Cand: "; traceCandidate(TopCand);
2330b57cec5SDimitry Andric              dbgs() << "Bot Cand: "; traceCandidate(BotCand););
2345ffd83dbSDimitry Andric   SchedCandidate Cand = BotCand;
2350b57cec5SDimitry Andric   TopCand.Reason = NoCand;
2360b57cec5SDimitry Andric   GenericScheduler::tryCandidate(Cand, TopCand, nullptr);
2370b57cec5SDimitry Andric   if (TopCand.Reason != NoCand) {
2380b57cec5SDimitry Andric     Cand.setBest(TopCand);
2390b57cec5SDimitry Andric   }
2400b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking: "; traceCandidate(Cand););
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   IsTopNode = Cand.AtTop;
2430b57cec5SDimitry Andric   return Cand.SU;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric // This function is mostly cut and pasted from
2470b57cec5SDimitry Andric // GenericScheduler::pickNode()
2480b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) {
2490b57cec5SDimitry Andric   if (DAG->top() == DAG->bottom()) {
2500b57cec5SDimitry Andric     assert(Top.Available.empty() && Top.Pending.empty() &&
2510b57cec5SDimitry Andric            Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
2520b57cec5SDimitry Andric     return nullptr;
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric   SUnit *SU;
2550b57cec5SDimitry Andric   do {
2560b57cec5SDimitry Andric     if (RegionPolicy.OnlyTopDown) {
2570b57cec5SDimitry Andric       SU = Top.pickOnlyChoice();
2580b57cec5SDimitry Andric       if (!SU) {
2590b57cec5SDimitry Andric         CandPolicy NoPolicy;
2600b57cec5SDimitry Andric         TopCand.reset(NoPolicy);
2610b57cec5SDimitry Andric         pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
2620b57cec5SDimitry Andric         assert(TopCand.Reason != NoCand && "failed to find a candidate");
2630b57cec5SDimitry Andric         SU = TopCand.SU;
2640b57cec5SDimitry Andric       }
2650b57cec5SDimitry Andric       IsTopNode = true;
2660b57cec5SDimitry Andric     } else if (RegionPolicy.OnlyBottomUp) {
2670b57cec5SDimitry Andric       SU = Bot.pickOnlyChoice();
2680b57cec5SDimitry Andric       if (!SU) {
2690b57cec5SDimitry Andric         CandPolicy NoPolicy;
2700b57cec5SDimitry Andric         BotCand.reset(NoPolicy);
2710b57cec5SDimitry Andric         pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
2720b57cec5SDimitry Andric         assert(BotCand.Reason != NoCand && "failed to find a candidate");
2730b57cec5SDimitry Andric         SU = BotCand.SU;
2740b57cec5SDimitry Andric       }
2750b57cec5SDimitry Andric       IsTopNode = false;
2760b57cec5SDimitry Andric     } else {
2770b57cec5SDimitry Andric       SU = pickNodeBidirectional(IsTopNode);
2780b57cec5SDimitry Andric     }
2790b57cec5SDimitry Andric   } while (SU->isScheduled);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   if (SU->isTopReady())
2820b57cec5SDimitry Andric     Top.removeReady(SU);
2830b57cec5SDimitry Andric   if (SU->isBottomReady())
2840b57cec5SDimitry Andric     Bot.removeReady(SU);
2850b57cec5SDimitry Andric 
286*fe6060f1SDimitry Andric   if (!HasClusteredNodes && SU->getInstr()->mayLoadOrStore()) {
287*fe6060f1SDimitry Andric     for (SDep &Dep : SU->Preds) {
288*fe6060f1SDimitry Andric       if (Dep.isCluster()) {
289*fe6060f1SDimitry Andric         HasClusteredNodes = true;
290*fe6060f1SDimitry Andric         break;
291*fe6060f1SDimitry Andric       }
292*fe6060f1SDimitry Andric     }
293*fe6060f1SDimitry Andric   }
294*fe6060f1SDimitry Andric 
2950b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") "
2960b57cec5SDimitry Andric                     << *SU->getInstr());
2970b57cec5SDimitry Andric   return SU;
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C,
3010b57cec5SDimitry Andric                         std::unique_ptr<MachineSchedStrategy> S) :
3020b57cec5SDimitry Andric   ScheduleDAGMILive(C, std::move(S)),
3030b57cec5SDimitry Andric   ST(MF.getSubtarget<GCNSubtarget>()),
3040b57cec5SDimitry Andric   MFI(*MF.getInfo<SIMachineFunctionInfo>()),
3050b57cec5SDimitry Andric   StartingOccupancy(MFI.getOccupancy()),
3065ffd83dbSDimitry Andric   MinOccupancy(StartingOccupancy), Stage(Collect), RegionIdx(0) {
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n");
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric void GCNScheduleDAGMILive::schedule() {
3125ffd83dbSDimitry Andric   if (Stage == Collect) {
3130b57cec5SDimitry Andric     // Just record regions at the first pass.
3140b57cec5SDimitry Andric     Regions.push_back(std::make_pair(RegionBegin, RegionEnd));
3150b57cec5SDimitry Andric     return;
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   std::vector<MachineInstr*> Unsched;
3190b57cec5SDimitry Andric   Unsched.reserve(NumRegionInstrs);
3200b57cec5SDimitry Andric   for (auto &I : *this) {
3210b57cec5SDimitry Andric     Unsched.push_back(&I);
3220b57cec5SDimitry Andric   }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   GCNRegPressure PressureBefore;
3250b57cec5SDimitry Andric   if (LIS) {
3260b57cec5SDimitry Andric     PressureBefore = Pressure[RegionIdx];
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Pressure before scheduling:\nRegion live-ins:";
3290b57cec5SDimitry Andric                GCNRPTracker::printLiveRegs(dbgs(), LiveIns[RegionIdx], MRI);
3300b57cec5SDimitry Andric                dbgs() << "Region live-in pressure:  ";
3310b57cec5SDimitry Andric                llvm::getRegPressure(MRI, LiveIns[RegionIdx]).print(dbgs());
3320b57cec5SDimitry Andric                dbgs() << "Region register pressure: ";
3330b57cec5SDimitry Andric                PressureBefore.print(dbgs()));
3340b57cec5SDimitry Andric   }
3350b57cec5SDimitry Andric 
336*fe6060f1SDimitry Andric   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
337*fe6060f1SDimitry Andric   // Set HasClusteredNodes to true for late stages where we have already
338*fe6060f1SDimitry Andric   // collected it. That way pickNode() will not scan SDep's when not needed.
339*fe6060f1SDimitry Andric   S.HasClusteredNodes = Stage > InitialSchedule;
340*fe6060f1SDimitry Andric   S.HasExcessPressure = false;
3410b57cec5SDimitry Andric   ScheduleDAGMILive::schedule();
3420b57cec5SDimitry Andric   Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
3435ffd83dbSDimitry Andric   RescheduleRegions[RegionIdx] = false;
344*fe6060f1SDimitry Andric   if (Stage == InitialSchedule && S.HasClusteredNodes)
345*fe6060f1SDimitry Andric     RegionsWithClusters[RegionIdx] = true;
346*fe6060f1SDimitry Andric   if (S.HasExcessPressure)
347*fe6060f1SDimitry Andric     RegionsWithHighRP[RegionIdx] = true;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   if (!LIS)
3500b57cec5SDimitry Andric     return;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   // Check the results of scheduling.
3530b57cec5SDimitry Andric   auto PressureAfter = getRealRegPressure();
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Pressure after scheduling: ";
3560b57cec5SDimitry Andric              PressureAfter.print(dbgs()));
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit &&
359*fe6060f1SDimitry Andric       PressureAfter.getVGPRNum(ST.hasGFX90AInsts()) <= S.VGPRCriticalLimit) {
3600b57cec5SDimitry Andric     Pressure[RegionIdx] = PressureAfter;
3610b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Pressure in desired limits, done.\n");
3620b57cec5SDimitry Andric     return;
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric   unsigned Occ = MFI.getOccupancy();
3650b57cec5SDimitry Andric   unsigned WavesAfter = std::min(Occ, PressureAfter.getOccupancy(ST));
3660b57cec5SDimitry Andric   unsigned WavesBefore = std::min(Occ, PressureBefore.getOccupancy(ST));
3670b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore
3680b57cec5SDimitry Andric                     << ", after " << WavesAfter << ".\n");
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   // We could not keep current target occupancy because of the just scheduled
3710b57cec5SDimitry Andric   // region. Record new occupancy for next scheduling cycle.
3720b57cec5SDimitry Andric   unsigned NewOccupancy = std::max(WavesAfter, WavesBefore);
3730b57cec5SDimitry Andric   // Allow memory bound functions to drop to 4 waves if not limited by an
3740b57cec5SDimitry Andric   // attribute.
3750b57cec5SDimitry Andric   if (WavesAfter < WavesBefore && WavesAfter < MinOccupancy &&
3760b57cec5SDimitry Andric       WavesAfter >= MFI.getMinAllowedOccupancy()) {
3770b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Function is memory bound, allow occupancy drop up to "
3780b57cec5SDimitry Andric                       << MFI.getMinAllowedOccupancy() << " waves\n");
3790b57cec5SDimitry Andric     NewOccupancy = WavesAfter;
3800b57cec5SDimitry Andric   }
3810b57cec5SDimitry Andric   if (NewOccupancy < MinOccupancy) {
3820b57cec5SDimitry Andric     MinOccupancy = NewOccupancy;
3830b57cec5SDimitry Andric     MFI.limitOccupancy(MinOccupancy);
3840b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Occupancy lowered for the function to "
3850b57cec5SDimitry Andric                       << MinOccupancy << ".\n");
3860b57cec5SDimitry Andric   }
3870b57cec5SDimitry Andric 
3885ffd83dbSDimitry Andric   unsigned MaxVGPRs = ST.getMaxNumVGPRs(MF);
3895ffd83dbSDimitry Andric   unsigned MaxSGPRs = ST.getMaxNumSGPRs(MF);
390*fe6060f1SDimitry Andric   if (PressureAfter.getVGPRNum(false) > MaxVGPRs ||
391*fe6060f1SDimitry Andric       PressureAfter.getAGPRNum() > MaxVGPRs ||
392*fe6060f1SDimitry Andric       PressureAfter.getSGPRNum() > MaxSGPRs) {
3935ffd83dbSDimitry Andric     RescheduleRegions[RegionIdx] = true;
394*fe6060f1SDimitry Andric     RegionsWithHighRP[RegionIdx] = true;
395*fe6060f1SDimitry Andric   }
3965ffd83dbSDimitry Andric 
3970b57cec5SDimitry Andric   if (WavesAfter >= MinOccupancy) {
3985ffd83dbSDimitry Andric     if (Stage == UnclusteredReschedule &&
3995ffd83dbSDimitry Andric         !PressureAfter.less(ST, PressureBefore)) {
4005ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "Unclustered reschedule did not help.\n");
4015ffd83dbSDimitry Andric     } else if (WavesAfter > MFI.getMinWavesPerEU() ||
402480093f4SDimitry Andric         PressureAfter.less(ST, PressureBefore) ||
4035ffd83dbSDimitry Andric         !RescheduleRegions[RegionIdx]) {
4040b57cec5SDimitry Andric       Pressure[RegionIdx] = PressureAfter;
405*fe6060f1SDimitry Andric       if (!RegionsWithClusters[RegionIdx] &&
406*fe6060f1SDimitry Andric           (Stage + 1) == UnclusteredReschedule)
407*fe6060f1SDimitry Andric         RescheduleRegions[RegionIdx] = false;
4080b57cec5SDimitry Andric       return;
4095ffd83dbSDimitry Andric     } else {
410480093f4SDimitry Andric       LLVM_DEBUG(dbgs() << "New pressure will result in more spilling.\n");
411480093f4SDimitry Andric     }
4125ffd83dbSDimitry Andric   }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Attempting to revert scheduling.\n");
415*fe6060f1SDimitry Andric   RescheduleRegions[RegionIdx] = RegionsWithClusters[RegionIdx] ||
416*fe6060f1SDimitry Andric                                  (Stage + 1) != UnclusteredReschedule;
4170b57cec5SDimitry Andric   RegionEnd = RegionBegin;
4180b57cec5SDimitry Andric   for (MachineInstr *MI : Unsched) {
4190b57cec5SDimitry Andric     if (MI->isDebugInstr())
4200b57cec5SDimitry Andric       continue;
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric     if (MI->getIterator() != RegionEnd) {
4230b57cec5SDimitry Andric       BB->remove(MI);
4240b57cec5SDimitry Andric       BB->insert(RegionEnd, MI);
4250b57cec5SDimitry Andric       if (!MI->isDebugInstr())
4260b57cec5SDimitry Andric         LIS->handleMove(*MI, true);
4270b57cec5SDimitry Andric     }
4280b57cec5SDimitry Andric     // Reset read-undef flags and update them later.
4290b57cec5SDimitry Andric     for (auto &Op : MI->operands())
4300b57cec5SDimitry Andric       if (Op.isReg() && Op.isDef())
4310b57cec5SDimitry Andric         Op.setIsUndef(false);
4320b57cec5SDimitry Andric     RegisterOperands RegOpers;
4330b57cec5SDimitry Andric     RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
4340b57cec5SDimitry Andric     if (!MI->isDebugInstr()) {
4350b57cec5SDimitry Andric       if (ShouldTrackLaneMasks) {
4360b57cec5SDimitry Andric         // Adjust liveness and add missing dead+read-undef flags.
4370b57cec5SDimitry Andric         SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
4380b57cec5SDimitry Andric         RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
4390b57cec5SDimitry Andric       } else {
4400b57cec5SDimitry Andric         // Adjust for missing dead-def flags.
4410b57cec5SDimitry Andric         RegOpers.detectDeadDefs(*MI, *LIS);
4420b57cec5SDimitry Andric       }
4430b57cec5SDimitry Andric     }
4440b57cec5SDimitry Andric     RegionEnd = MI->getIterator();
4450b57cec5SDimitry Andric     ++RegionEnd;
4460b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Scheduling " << *MI);
4470b57cec5SDimitry Andric   }
4480b57cec5SDimitry Andric   RegionBegin = Unsched.front()->getIterator();
4490b57cec5SDimitry Andric   Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric   placeDebugValues();
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const {
4550b57cec5SDimitry Andric   GCNDownwardRPTracker RPTracker(*LIS);
4560b57cec5SDimitry Andric   RPTracker.advance(begin(), end(), &LiveIns[RegionIdx]);
4570b57cec5SDimitry Andric   return RPTracker.moveMaxPressure();
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric void GCNScheduleDAGMILive::computeBlockPressure(const MachineBasicBlock *MBB) {
4610b57cec5SDimitry Andric   GCNDownwardRPTracker RPTracker(*LIS);
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   // If the block has the only successor then live-ins of that successor are
4640b57cec5SDimitry Andric   // live-outs of the current block. We can reuse calculated live set if the
4650b57cec5SDimitry Andric   // successor will be sent to scheduling past current block.
4660b57cec5SDimitry Andric   const MachineBasicBlock *OnlySucc = nullptr;
4670b57cec5SDimitry Andric   if (MBB->succ_size() == 1 && !(*MBB->succ_begin())->empty()) {
4680b57cec5SDimitry Andric     SlotIndexes *Ind = LIS->getSlotIndexes();
4690b57cec5SDimitry Andric     if (Ind->getMBBStartIdx(MBB) < Ind->getMBBStartIdx(*MBB->succ_begin()))
4700b57cec5SDimitry Andric       OnlySucc = *MBB->succ_begin();
4710b57cec5SDimitry Andric   }
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   // Scheduler sends regions from the end of the block upwards.
4740b57cec5SDimitry Andric   size_t CurRegion = RegionIdx;
4750b57cec5SDimitry Andric   for (size_t E = Regions.size(); CurRegion != E; ++CurRegion)
4760b57cec5SDimitry Andric     if (Regions[CurRegion].first->getParent() != MBB)
4770b57cec5SDimitry Andric       break;
4780b57cec5SDimitry Andric   --CurRegion;
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   auto I = MBB->begin();
4810b57cec5SDimitry Andric   auto LiveInIt = MBBLiveIns.find(MBB);
4820b57cec5SDimitry Andric   if (LiveInIt != MBBLiveIns.end()) {
4830b57cec5SDimitry Andric     auto LiveIn = std::move(LiveInIt->second);
4840b57cec5SDimitry Andric     RPTracker.reset(*MBB->begin(), &LiveIn);
4850b57cec5SDimitry Andric     MBBLiveIns.erase(LiveInIt);
4860b57cec5SDimitry Andric   } else {
4870b57cec5SDimitry Andric     auto &Rgn = Regions[CurRegion];
4880b57cec5SDimitry Andric     I = Rgn.first;
4890b57cec5SDimitry Andric     auto *NonDbgMI = &*skipDebugInstructionsForward(Rgn.first, Rgn.second);
4900b57cec5SDimitry Andric     auto LRS = BBLiveInMap.lookup(NonDbgMI);
491*fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS
4920b57cec5SDimitry Andric     assert(isEqual(getLiveRegsBefore(*NonDbgMI, *LIS), LRS));
493*fe6060f1SDimitry Andric #endif
4940b57cec5SDimitry Andric     RPTracker.reset(*I, &LRS);
4950b57cec5SDimitry Andric   }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   for ( ; ; ) {
4980b57cec5SDimitry Andric     I = RPTracker.getNext();
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric     if (Regions[CurRegion].first == I) {
5010b57cec5SDimitry Andric       LiveIns[CurRegion] = RPTracker.getLiveRegs();
5020b57cec5SDimitry Andric       RPTracker.clearMaxPressure();
5030b57cec5SDimitry Andric     }
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric     if (Regions[CurRegion].second == I) {
5060b57cec5SDimitry Andric       Pressure[CurRegion] = RPTracker.moveMaxPressure();
5070b57cec5SDimitry Andric       if (CurRegion-- == RegionIdx)
5080b57cec5SDimitry Andric         break;
5090b57cec5SDimitry Andric     }
5100b57cec5SDimitry Andric     RPTracker.advanceToNext();
5110b57cec5SDimitry Andric     RPTracker.advanceBeforeNext();
5120b57cec5SDimitry Andric   }
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   if (OnlySucc) {
5150b57cec5SDimitry Andric     if (I != MBB->end()) {
5160b57cec5SDimitry Andric       RPTracker.advanceToNext();
5170b57cec5SDimitry Andric       RPTracker.advance(MBB->end());
5180b57cec5SDimitry Andric     }
5190b57cec5SDimitry Andric     RPTracker.reset(*OnlySucc->begin(), &RPTracker.getLiveRegs());
5200b57cec5SDimitry Andric     RPTracker.advanceBeforeNext();
5210b57cec5SDimitry Andric     MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs();
5220b57cec5SDimitry Andric   }
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet>
5260b57cec5SDimitry Andric GCNScheduleDAGMILive::getBBLiveInMap() const {
5270b57cec5SDimitry Andric   assert(!Regions.empty());
5280b57cec5SDimitry Andric   std::vector<MachineInstr *> BBStarters;
5290b57cec5SDimitry Andric   BBStarters.reserve(Regions.size());
5300b57cec5SDimitry Andric   auto I = Regions.rbegin(), E = Regions.rend();
5310b57cec5SDimitry Andric   auto *BB = I->first->getParent();
5320b57cec5SDimitry Andric   do {
5330b57cec5SDimitry Andric     auto *MI = &*skipDebugInstructionsForward(I->first, I->second);
5340b57cec5SDimitry Andric     BBStarters.push_back(MI);
5350b57cec5SDimitry Andric     do {
5360b57cec5SDimitry Andric       ++I;
5370b57cec5SDimitry Andric     } while (I != E && I->first->getParent() == BB);
5380b57cec5SDimitry Andric   } while (I != E);
5390b57cec5SDimitry Andric   return getLiveRegMap(BBStarters, false /*After*/, *LIS);
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric void GCNScheduleDAGMILive::finalizeSchedule() {
5430b57cec5SDimitry Andric   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
5440b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n");
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   LiveIns.resize(Regions.size());
5470b57cec5SDimitry Andric   Pressure.resize(Regions.size());
5485ffd83dbSDimitry Andric   RescheduleRegions.resize(Regions.size());
549*fe6060f1SDimitry Andric   RegionsWithClusters.resize(Regions.size());
550*fe6060f1SDimitry Andric   RegionsWithHighRP.resize(Regions.size());
5515ffd83dbSDimitry Andric   RescheduleRegions.set();
552*fe6060f1SDimitry Andric   RegionsWithClusters.reset();
553*fe6060f1SDimitry Andric   RegionsWithHighRP.reset();
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric   if (!Regions.empty())
5560b57cec5SDimitry Andric     BBLiveInMap = getBBLiveInMap();
5570b57cec5SDimitry Andric 
5585ffd83dbSDimitry Andric   std::vector<std::unique_ptr<ScheduleDAGMutation>> SavedMutations;
5595ffd83dbSDimitry Andric 
5600b57cec5SDimitry Andric   do {
5610b57cec5SDimitry Andric     Stage++;
5620b57cec5SDimitry Andric     RegionIdx = 0;
5630b57cec5SDimitry Andric     MachineBasicBlock *MBB = nullptr;
5640b57cec5SDimitry Andric 
5655ffd83dbSDimitry Andric     if (Stage > InitialSchedule) {
5665ffd83dbSDimitry Andric       if (!LIS)
5675ffd83dbSDimitry Andric         break;
5685ffd83dbSDimitry Andric 
5690b57cec5SDimitry Andric       // Retry function scheduling if we found resulting occupancy and it is
5700b57cec5SDimitry Andric       // lower than used for first pass scheduling. This will give more freedom
5710b57cec5SDimitry Andric       // to schedule low register pressure blocks.
5720b57cec5SDimitry Andric       // Code is partially copied from MachineSchedulerBase::scheduleRegions().
5730b57cec5SDimitry Andric 
5745ffd83dbSDimitry Andric       if (Stage == UnclusteredReschedule) {
5755ffd83dbSDimitry Andric         if (RescheduleRegions.none())
5765ffd83dbSDimitry Andric           continue;
5775ffd83dbSDimitry Andric         LLVM_DEBUG(dbgs() <<
5785ffd83dbSDimitry Andric           "Retrying function scheduling without clustering.\n");
5795ffd83dbSDimitry Andric       }
5805ffd83dbSDimitry Andric 
5815ffd83dbSDimitry Andric       if (Stage == ClusteredLowOccupancyReschedule) {
5825ffd83dbSDimitry Andric         if (StartingOccupancy <= MinOccupancy)
5830b57cec5SDimitry Andric           break;
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric         LLVM_DEBUG(
5860b57cec5SDimitry Andric             dbgs()
5870b57cec5SDimitry Andric             << "Retrying function scheduling with lowest recorded occupancy "
5880b57cec5SDimitry Andric             << MinOccupancy << ".\n");
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric         S.setTargetOccupancy(MinOccupancy);
5910b57cec5SDimitry Andric       }
5925ffd83dbSDimitry Andric     }
5935ffd83dbSDimitry Andric 
5945ffd83dbSDimitry Andric     if (Stage == UnclusteredReschedule)
5955ffd83dbSDimitry Andric       SavedMutations.swap(Mutations);
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric     for (auto Region : Regions) {
598*fe6060f1SDimitry Andric       if ((Stage == UnclusteredReschedule && !RescheduleRegions[RegionIdx]) ||
599*fe6060f1SDimitry Andric           (Stage == ClusteredLowOccupancyReschedule &&
600*fe6060f1SDimitry Andric            !RegionsWithClusters[RegionIdx] && !RegionsWithHighRP[RegionIdx])) {
601*fe6060f1SDimitry Andric 
602e8d8bef9SDimitry Andric         ++RegionIdx;
6035ffd83dbSDimitry Andric         continue;
604e8d8bef9SDimitry Andric       }
6055ffd83dbSDimitry Andric 
6060b57cec5SDimitry Andric       RegionBegin = Region.first;
6070b57cec5SDimitry Andric       RegionEnd = Region.second;
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric       if (RegionBegin->getParent() != MBB) {
6100b57cec5SDimitry Andric         if (MBB) finishBlock();
6110b57cec5SDimitry Andric         MBB = RegionBegin->getParent();
6120b57cec5SDimitry Andric         startBlock(MBB);
6135ffd83dbSDimitry Andric         if (Stage == InitialSchedule)
6140b57cec5SDimitry Andric           computeBlockPressure(MBB);
6150b57cec5SDimitry Andric       }
6160b57cec5SDimitry Andric 
6170b57cec5SDimitry Andric       unsigned NumRegionInstrs = std::distance(begin(), end());
6180b57cec5SDimitry Andric       enterRegion(MBB, begin(), end(), NumRegionInstrs);
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric       // Skip empty scheduling regions (0 or 1 schedulable instructions).
6210b57cec5SDimitry Andric       if (begin() == end() || begin() == std::prev(end())) {
6220b57cec5SDimitry Andric         exitRegion();
6230b57cec5SDimitry Andric         continue;
6240b57cec5SDimitry Andric       }
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "********** MI Scheduling **********\n");
6270b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << MF.getName() << ":" << printMBBReference(*MBB) << " "
6280b57cec5SDimitry Andric                         << MBB->getName() << "\n  From: " << *begin()
6290b57cec5SDimitry Andric                         << "    To: ";
6300b57cec5SDimitry Andric                  if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
6310b57cec5SDimitry Andric                  else dbgs() << "End";
6320b57cec5SDimitry Andric                  dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric       schedule();
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric       exitRegion();
6370b57cec5SDimitry Andric       ++RegionIdx;
6380b57cec5SDimitry Andric     }
6390b57cec5SDimitry Andric     finishBlock();
6400b57cec5SDimitry Andric 
6415ffd83dbSDimitry Andric     if (Stage == UnclusteredReschedule)
6425ffd83dbSDimitry Andric       SavedMutations.swap(Mutations);
6435ffd83dbSDimitry Andric   } while (Stage != LastStage);
6440b57cec5SDimitry Andric }
645