xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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) :
23fe6060f1SDimitry Andric     GenericScheduler(C), TargetOccupancy(0), HasClusteredNodes(false),
24fe6060f1SDimitry Andric     HasExcessPressure(false), MF(nullptr) { }
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) {
270b57cec5SDimitry Andric   GenericScheduler::initialize(DAG);
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric   MF = &DAG->MF;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   // FIXME: This is also necessary, because some passes that run after
340b57cec5SDimitry Andric   // scheduling and before regalloc increase register pressure.
35*349cc55cSDimitry Andric   const unsigned ErrorMargin = 3;
360b57cec5SDimitry Andric 
37*349cc55cSDimitry Andric   SGPRExcessLimit =
38*349cc55cSDimitry Andric       Context->RegClassInfo->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass);
39*349cc55cSDimitry Andric   VGPRExcessLimit =
40*349cc55cSDimitry Andric       Context->RegClassInfo->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass);
410b57cec5SDimitry Andric 
42*349cc55cSDimitry Andric   SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
43*349cc55cSDimitry Andric   // Set the initial TargetOccupnacy to the maximum occupancy that we can
44*349cc55cSDimitry Andric   // achieve for this function. This effectively sets a lower bound on the
45*349cc55cSDimitry Andric   // 'Critical' register limits in the scheduler.
46*349cc55cSDimitry Andric   TargetOccupancy = MFI.getOccupancy();
47*349cc55cSDimitry Andric   SGPRCriticalLimit =
48*349cc55cSDimitry Andric       std::min(ST.getMaxNumSGPRs(TargetOccupancy, true), SGPRExcessLimit);
49*349cc55cSDimitry Andric   VGPRCriticalLimit =
50*349cc55cSDimitry Andric       std::min(ST.getMaxNumVGPRs(TargetOccupancy), VGPRExcessLimit);
51*349cc55cSDimitry Andric 
52*349cc55cSDimitry Andric   // Subtract error margin from register limits and avoid overflow.
53*349cc55cSDimitry Andric   SGPRCriticalLimit =
54*349cc55cSDimitry Andric       std::min(SGPRCriticalLimit - ErrorMargin, SGPRCriticalLimit);
55*349cc55cSDimitry Andric   VGPRCriticalLimit =
56*349cc55cSDimitry Andric       std::min(VGPRCriticalLimit - ErrorMargin, VGPRCriticalLimit);
57*349cc55cSDimitry Andric   SGPRExcessLimit = std::min(SGPRExcessLimit - ErrorMargin, SGPRExcessLimit);
58*349cc55cSDimitry Andric   VGPRExcessLimit = std::min(VGPRExcessLimit - ErrorMargin, VGPRExcessLimit);
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 
748bcb0991SDimitry Andric   Pressure.clear();
758bcb0991SDimitry 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 
855ffd83dbSDimitry Andric   unsigned NewSGPRPressure = Pressure[AMDGPU::RegisterPressureSets::SReg_32];
865ffd83dbSDimitry Andric   unsigned NewVGPRPressure = Pressure[AMDGPU::RegisterPressureSets::VGPR_32];
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 
1068bcb0991SDimitry Andric   // We only need to update the RPDelta for instructions that increase register
1078bcb0991SDimitry Andric   // pressure. Instructions that decrease or keep reg pressure the same will be
1088bcb0991SDimitry Andric   // marked as RegExcess in tryCandidate() when they are compared with
1098bcb0991SDimitry Andric   // instructions that increase the register pressure.
1100b57cec5SDimitry Andric   if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) {
111fe6060f1SDimitry Andric     HasExcessPressure = true;
1125ffd83dbSDimitry Andric     Cand.RPDelta.Excess = PressureChange(AMDGPU::RegisterPressureSets::VGPR_32);
1130b57cec5SDimitry Andric     Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit);
1140b57cec5SDimitry Andric   }
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) {
117fe6060f1SDimitry Andric     HasExcessPressure = true;
1185ffd83dbSDimitry Andric     Cand.RPDelta.Excess = PressureChange(AMDGPU::RegisterPressureSets::SReg_32);
1190b57cec5SDimitry Andric     Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit);
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   // Register pressure is considered 'CRITICAL' if it is approaching a value
1230b57cec5SDimitry Andric   // that would reduce the wave occupancy for the execution unit.  When
124*349cc55cSDimitry Andric   // register pressure is 'CRITICAL', increasing SGPR and VGPR pressure both
1250b57cec5SDimitry Andric   // has the same cost, so we don't need to prefer one over the other.
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit;
1280b57cec5SDimitry Andric   int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   if (SGPRDelta >= 0 || VGPRDelta >= 0) {
131fe6060f1SDimitry Andric     HasExcessPressure = true;
1320b57cec5SDimitry Andric     if (SGPRDelta > VGPRDelta) {
1335ffd83dbSDimitry Andric       Cand.RPDelta.CriticalMax =
1345ffd83dbSDimitry Andric         PressureChange(AMDGPU::RegisterPressureSets::SReg_32);
1350b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta);
1360b57cec5SDimitry Andric     } else {
1375ffd83dbSDimitry Andric       Cand.RPDelta.CriticalMax =
1385ffd83dbSDimitry Andric         PressureChange(AMDGPU::RegisterPressureSets::VGPR_32);
1390b57cec5SDimitry Andric       Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta);
1400b57cec5SDimitry Andric     }
1410b57cec5SDimitry Andric   }
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric // This function is mostly cut and pasted from
1450b57cec5SDimitry Andric // GenericScheduler::pickNodeFromQueue()
1460b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone,
1470b57cec5SDimitry Andric                                          const CandPolicy &ZonePolicy,
1480b57cec5SDimitry Andric                                          const RegPressureTracker &RPTracker,
1490b57cec5SDimitry Andric                                          SchedCandidate &Cand) {
1500b57cec5SDimitry Andric   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
1510b57cec5SDimitry Andric   ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos();
1525ffd83dbSDimitry Andric   unsigned SGPRPressure = Pressure[AMDGPU::RegisterPressureSets::SReg_32];
1535ffd83dbSDimitry Andric   unsigned VGPRPressure = Pressure[AMDGPU::RegisterPressureSets::VGPR_32];
1540b57cec5SDimitry Andric   ReadyQueue &Q = Zone.Available;
1550b57cec5SDimitry Andric   for (SUnit *SU : Q) {
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric     SchedCandidate TryCand(ZonePolicy);
1580b57cec5SDimitry Andric     initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI,
1590b57cec5SDimitry Andric                   SGPRPressure, VGPRPressure);
1600b57cec5SDimitry Andric     // Pass SchedBoundary only when comparing nodes from the same boundary.
1610b57cec5SDimitry Andric     SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
1620b57cec5SDimitry Andric     GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg);
1630b57cec5SDimitry Andric     if (TryCand.Reason != NoCand) {
1640b57cec5SDimitry Andric       // Initialize resource delta if needed in case future heuristics query it.
1650b57cec5SDimitry Andric       if (TryCand.ResDelta == SchedResourceDelta())
1660b57cec5SDimitry Andric         TryCand.initResourceDelta(Zone.DAG, SchedModel);
1670b57cec5SDimitry Andric       Cand.setBest(TryCand);
1688bcb0991SDimitry Andric       LLVM_DEBUG(traceCandidate(Cand));
1690b57cec5SDimitry Andric     }
1700b57cec5SDimitry Andric   }
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric // This function is mostly cut and pasted from
1740b57cec5SDimitry Andric // GenericScheduler::pickNodeBidirectional()
1750b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) {
1760b57cec5SDimitry Andric   // Schedule as far as possible in the direction of no choice. This is most
1770b57cec5SDimitry Andric   // efficient, but also provides the best heuristics for CriticalPSets.
1780b57cec5SDimitry Andric   if (SUnit *SU = Bot.pickOnlyChoice()) {
1790b57cec5SDimitry Andric     IsTopNode = false;
1800b57cec5SDimitry Andric     return SU;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric   if (SUnit *SU = Top.pickOnlyChoice()) {
1830b57cec5SDimitry Andric     IsTopNode = true;
1840b57cec5SDimitry Andric     return SU;
1850b57cec5SDimitry Andric   }
1860b57cec5SDimitry Andric   // Set the bottom-up policy based on the state of the current bottom zone and
1870b57cec5SDimitry Andric   // the instructions outside the zone, including the top zone.
1880b57cec5SDimitry Andric   CandPolicy BotPolicy;
1890b57cec5SDimitry Andric   setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
1900b57cec5SDimitry Andric   // Set the top-down policy based on the state of the current top zone and
1910b57cec5SDimitry Andric   // the instructions outside the zone, including the bottom zone.
1920b57cec5SDimitry Andric   CandPolicy TopPolicy;
1930b57cec5SDimitry Andric   setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   // See if BotCand is still valid (because we previously scheduled from Top).
1960b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking from Bot:\n");
1970b57cec5SDimitry Andric   if (!BotCand.isValid() || BotCand.SU->isScheduled ||
1980b57cec5SDimitry Andric       BotCand.Policy != BotPolicy) {
1990b57cec5SDimitry Andric     BotCand.reset(CandPolicy());
2000b57cec5SDimitry Andric     pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
2010b57cec5SDimitry Andric     assert(BotCand.Reason != NoCand && "failed to find the first candidate");
2020b57cec5SDimitry Andric   } else {
2030b57cec5SDimitry Andric     LLVM_DEBUG(traceCandidate(BotCand));
2048bcb0991SDimitry Andric #ifndef NDEBUG
2058bcb0991SDimitry Andric     if (VerifyScheduling) {
2068bcb0991SDimitry Andric       SchedCandidate TCand;
2078bcb0991SDimitry Andric       TCand.reset(CandPolicy());
2088bcb0991SDimitry Andric       pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), TCand);
2098bcb0991SDimitry Andric       assert(TCand.SU == BotCand.SU &&
2108bcb0991SDimitry Andric              "Last pick result should correspond to re-picking right now");
2118bcb0991SDimitry Andric     }
2128bcb0991SDimitry Andric #endif
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   // Check if the top Q has a better candidate.
2160b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking from Top:\n");
2170b57cec5SDimitry Andric   if (!TopCand.isValid() || TopCand.SU->isScheduled ||
2180b57cec5SDimitry Andric       TopCand.Policy != TopPolicy) {
2190b57cec5SDimitry Andric     TopCand.reset(CandPolicy());
2200b57cec5SDimitry Andric     pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
2210b57cec5SDimitry Andric     assert(TopCand.Reason != NoCand && "failed to find the first candidate");
2220b57cec5SDimitry Andric   } else {
2230b57cec5SDimitry Andric     LLVM_DEBUG(traceCandidate(TopCand));
2248bcb0991SDimitry Andric #ifndef NDEBUG
2258bcb0991SDimitry Andric     if (VerifyScheduling) {
2268bcb0991SDimitry Andric       SchedCandidate TCand;
2278bcb0991SDimitry Andric       TCand.reset(CandPolicy());
2288bcb0991SDimitry Andric       pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TCand);
2298bcb0991SDimitry Andric       assert(TCand.SU == TopCand.SU &&
2308bcb0991SDimitry Andric            "Last pick result should correspond to re-picking right now");
2318bcb0991SDimitry Andric     }
2328bcb0991SDimitry Andric #endif
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   // Pick best from BotCand and TopCand.
2360b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Top Cand: "; traceCandidate(TopCand);
2370b57cec5SDimitry Andric              dbgs() << "Bot Cand: "; traceCandidate(BotCand););
2385ffd83dbSDimitry Andric   SchedCandidate Cand = BotCand;
2390b57cec5SDimitry Andric   TopCand.Reason = NoCand;
2400b57cec5SDimitry Andric   GenericScheduler::tryCandidate(Cand, TopCand, nullptr);
2410b57cec5SDimitry Andric   if (TopCand.Reason != NoCand) {
2420b57cec5SDimitry Andric     Cand.setBest(TopCand);
2430b57cec5SDimitry Andric   }
2440b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Picking: "; traceCandidate(Cand););
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   IsTopNode = Cand.AtTop;
2470b57cec5SDimitry Andric   return Cand.SU;
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric // This function is mostly cut and pasted from
2510b57cec5SDimitry Andric // GenericScheduler::pickNode()
2520b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) {
2530b57cec5SDimitry Andric   if (DAG->top() == DAG->bottom()) {
2540b57cec5SDimitry Andric     assert(Top.Available.empty() && Top.Pending.empty() &&
2550b57cec5SDimitry Andric            Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
2560b57cec5SDimitry Andric     return nullptr;
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric   SUnit *SU;
2590b57cec5SDimitry Andric   do {
2600b57cec5SDimitry Andric     if (RegionPolicy.OnlyTopDown) {
2610b57cec5SDimitry Andric       SU = Top.pickOnlyChoice();
2620b57cec5SDimitry Andric       if (!SU) {
2630b57cec5SDimitry Andric         CandPolicy NoPolicy;
2640b57cec5SDimitry Andric         TopCand.reset(NoPolicy);
2650b57cec5SDimitry Andric         pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
2660b57cec5SDimitry Andric         assert(TopCand.Reason != NoCand && "failed to find a candidate");
2670b57cec5SDimitry Andric         SU = TopCand.SU;
2680b57cec5SDimitry Andric       }
2690b57cec5SDimitry Andric       IsTopNode = true;
2700b57cec5SDimitry Andric     } else if (RegionPolicy.OnlyBottomUp) {
2710b57cec5SDimitry Andric       SU = Bot.pickOnlyChoice();
2720b57cec5SDimitry Andric       if (!SU) {
2730b57cec5SDimitry Andric         CandPolicy NoPolicy;
2740b57cec5SDimitry Andric         BotCand.reset(NoPolicy);
2750b57cec5SDimitry Andric         pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
2760b57cec5SDimitry Andric         assert(BotCand.Reason != NoCand && "failed to find a candidate");
2770b57cec5SDimitry Andric         SU = BotCand.SU;
2780b57cec5SDimitry Andric       }
2790b57cec5SDimitry Andric       IsTopNode = false;
2800b57cec5SDimitry Andric     } else {
2810b57cec5SDimitry Andric       SU = pickNodeBidirectional(IsTopNode);
2820b57cec5SDimitry Andric     }
2830b57cec5SDimitry Andric   } while (SU->isScheduled);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   if (SU->isTopReady())
2860b57cec5SDimitry Andric     Top.removeReady(SU);
2870b57cec5SDimitry Andric   if (SU->isBottomReady())
2880b57cec5SDimitry Andric     Bot.removeReady(SU);
2890b57cec5SDimitry Andric 
290fe6060f1SDimitry Andric   if (!HasClusteredNodes && SU->getInstr()->mayLoadOrStore()) {
291fe6060f1SDimitry Andric     for (SDep &Dep : SU->Preds) {
292fe6060f1SDimitry Andric       if (Dep.isCluster()) {
293fe6060f1SDimitry Andric         HasClusteredNodes = true;
294fe6060f1SDimitry Andric         break;
295fe6060f1SDimitry Andric       }
296fe6060f1SDimitry Andric     }
297fe6060f1SDimitry Andric   }
298fe6060f1SDimitry Andric 
2990b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") "
3000b57cec5SDimitry Andric                     << *SU->getInstr());
3010b57cec5SDimitry Andric   return SU;
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C,
3050b57cec5SDimitry Andric                         std::unique_ptr<MachineSchedStrategy> S) :
3060b57cec5SDimitry Andric   ScheduleDAGMILive(C, std::move(S)),
3070b57cec5SDimitry Andric   ST(MF.getSubtarget<GCNSubtarget>()),
3080b57cec5SDimitry Andric   MFI(*MF.getInfo<SIMachineFunctionInfo>()),
3090b57cec5SDimitry Andric   StartingOccupancy(MFI.getOccupancy()),
3105ffd83dbSDimitry Andric   MinOccupancy(StartingOccupancy), Stage(Collect), RegionIdx(0) {
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n");
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric void GCNScheduleDAGMILive::schedule() {
3165ffd83dbSDimitry Andric   if (Stage == Collect) {
3170b57cec5SDimitry Andric     // Just record regions at the first pass.
3180b57cec5SDimitry Andric     Regions.push_back(std::make_pair(RegionBegin, RegionEnd));
3190b57cec5SDimitry Andric     return;
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   std::vector<MachineInstr*> Unsched;
3230b57cec5SDimitry Andric   Unsched.reserve(NumRegionInstrs);
3240b57cec5SDimitry Andric   for (auto &I : *this) {
3250b57cec5SDimitry Andric     Unsched.push_back(&I);
3260b57cec5SDimitry Andric   }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric   GCNRegPressure PressureBefore;
3290b57cec5SDimitry Andric   if (LIS) {
3300b57cec5SDimitry Andric     PressureBefore = Pressure[RegionIdx];
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Pressure before scheduling:\nRegion live-ins:";
3330b57cec5SDimitry Andric                GCNRPTracker::printLiveRegs(dbgs(), LiveIns[RegionIdx], MRI);
3340b57cec5SDimitry Andric                dbgs() << "Region live-in pressure:  ";
3350b57cec5SDimitry Andric                llvm::getRegPressure(MRI, LiveIns[RegionIdx]).print(dbgs());
3360b57cec5SDimitry Andric                dbgs() << "Region register pressure: ";
3370b57cec5SDimitry Andric                PressureBefore.print(dbgs()));
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric 
340fe6060f1SDimitry Andric   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
341fe6060f1SDimitry Andric   // Set HasClusteredNodes to true for late stages where we have already
342fe6060f1SDimitry Andric   // collected it. That way pickNode() will not scan SDep's when not needed.
343fe6060f1SDimitry Andric   S.HasClusteredNodes = Stage > InitialSchedule;
344fe6060f1SDimitry Andric   S.HasExcessPressure = false;
3450b57cec5SDimitry Andric   ScheduleDAGMILive::schedule();
3460b57cec5SDimitry Andric   Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
3475ffd83dbSDimitry Andric   RescheduleRegions[RegionIdx] = false;
348fe6060f1SDimitry Andric   if (Stage == InitialSchedule && S.HasClusteredNodes)
349fe6060f1SDimitry Andric     RegionsWithClusters[RegionIdx] = true;
350fe6060f1SDimitry Andric   if (S.HasExcessPressure)
351fe6060f1SDimitry Andric     RegionsWithHighRP[RegionIdx] = true;
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   if (!LIS)
3540b57cec5SDimitry Andric     return;
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   // Check the results of scheduling.
3570b57cec5SDimitry Andric   auto PressureAfter = getRealRegPressure();
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Pressure after scheduling: ";
3600b57cec5SDimitry Andric              PressureAfter.print(dbgs()));
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit &&
363fe6060f1SDimitry Andric       PressureAfter.getVGPRNum(ST.hasGFX90AInsts()) <= S.VGPRCriticalLimit) {
3640b57cec5SDimitry Andric     Pressure[RegionIdx] = PressureAfter;
3650b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Pressure in desired limits, done.\n");
3660b57cec5SDimitry Andric     return;
3670b57cec5SDimitry Andric   }
368*349cc55cSDimitry Andric 
369*349cc55cSDimitry Andric   unsigned WavesAfter =
370*349cc55cSDimitry Andric       std::min(S.TargetOccupancy, PressureAfter.getOccupancy(ST));
371*349cc55cSDimitry Andric   unsigned WavesBefore =
372*349cc55cSDimitry Andric       std::min(S.TargetOccupancy, PressureBefore.getOccupancy(ST));
3730b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore
3740b57cec5SDimitry Andric                     << ", after " << WavesAfter << ".\n");
3750b57cec5SDimitry Andric 
376*349cc55cSDimitry Andric   // We may not be able to keep the current target occupancy because of the just
377*349cc55cSDimitry Andric   // scheduled region. We might still be able to revert scheduling if the
378*349cc55cSDimitry Andric   // occupancy before was higher, or if the current schedule has register
379*349cc55cSDimitry Andric   // pressure higher than the excess limits which could lead to more spilling.
3800b57cec5SDimitry Andric   unsigned NewOccupancy = std::max(WavesAfter, WavesBefore);
3810b57cec5SDimitry Andric   // Allow memory bound functions to drop to 4 waves if not limited by an
3820b57cec5SDimitry Andric   // attribute.
3830b57cec5SDimitry Andric   if (WavesAfter < WavesBefore && WavesAfter < MinOccupancy &&
3840b57cec5SDimitry Andric       WavesAfter >= MFI.getMinAllowedOccupancy()) {
3850b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Function is memory bound, allow occupancy drop up to "
3860b57cec5SDimitry Andric                       << MFI.getMinAllowedOccupancy() << " waves\n");
3870b57cec5SDimitry Andric     NewOccupancy = WavesAfter;
3880b57cec5SDimitry Andric   }
389*349cc55cSDimitry Andric 
3900b57cec5SDimitry Andric   if (NewOccupancy < MinOccupancy) {
3910b57cec5SDimitry Andric     MinOccupancy = NewOccupancy;
3920b57cec5SDimitry Andric     MFI.limitOccupancy(MinOccupancy);
3930b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Occupancy lowered for the function to "
3940b57cec5SDimitry Andric                       << MinOccupancy << ".\n");
3950b57cec5SDimitry Andric   }
3960b57cec5SDimitry Andric 
3975ffd83dbSDimitry Andric   unsigned MaxVGPRs = ST.getMaxNumVGPRs(MF);
3985ffd83dbSDimitry Andric   unsigned MaxSGPRs = ST.getMaxNumSGPRs(MF);
399fe6060f1SDimitry Andric   if (PressureAfter.getVGPRNum(false) > MaxVGPRs ||
400fe6060f1SDimitry Andric       PressureAfter.getAGPRNum() > MaxVGPRs ||
401fe6060f1SDimitry Andric       PressureAfter.getSGPRNum() > MaxSGPRs) {
4025ffd83dbSDimitry Andric     RescheduleRegions[RegionIdx] = true;
403fe6060f1SDimitry Andric     RegionsWithHighRP[RegionIdx] = true;
404fe6060f1SDimitry Andric   }
4055ffd83dbSDimitry Andric 
406*349cc55cSDimitry Andric   // If this condition is true, then either the occupancy before and after
407*349cc55cSDimitry Andric   // scheduling is the same, or we are allowing the occupancy to drop because
408*349cc55cSDimitry Andric   // the function is memory bound. Even if we are OK with the current occupancy,
409*349cc55cSDimitry Andric   // we still need to verify that we will not introduce any extra chance of
410*349cc55cSDimitry Andric   // spilling.
4110b57cec5SDimitry Andric   if (WavesAfter >= MinOccupancy) {
4125ffd83dbSDimitry Andric     if (Stage == UnclusteredReschedule &&
4135ffd83dbSDimitry Andric         !PressureAfter.less(ST, PressureBefore)) {
4145ffd83dbSDimitry Andric       LLVM_DEBUG(dbgs() << "Unclustered reschedule did not help.\n");
4155ffd83dbSDimitry Andric     } else if (WavesAfter > MFI.getMinWavesPerEU() ||
416480093f4SDimitry Andric         PressureAfter.less(ST, PressureBefore) ||
4175ffd83dbSDimitry Andric         !RescheduleRegions[RegionIdx]) {
4180b57cec5SDimitry Andric       Pressure[RegionIdx] = PressureAfter;
419fe6060f1SDimitry Andric       if (!RegionsWithClusters[RegionIdx] &&
420fe6060f1SDimitry Andric           (Stage + 1) == UnclusteredReschedule)
421fe6060f1SDimitry Andric         RescheduleRegions[RegionIdx] = false;
4220b57cec5SDimitry Andric       return;
4235ffd83dbSDimitry Andric     } else {
424480093f4SDimitry Andric       LLVM_DEBUG(dbgs() << "New pressure will result in more spilling.\n");
425480093f4SDimitry Andric     }
4265ffd83dbSDimitry Andric   }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Attempting to revert scheduling.\n");
429fe6060f1SDimitry Andric   RescheduleRegions[RegionIdx] = RegionsWithClusters[RegionIdx] ||
430fe6060f1SDimitry Andric                                  (Stage + 1) != UnclusteredReschedule;
4310b57cec5SDimitry Andric   RegionEnd = RegionBegin;
4320b57cec5SDimitry Andric   for (MachineInstr *MI : Unsched) {
4330b57cec5SDimitry Andric     if (MI->isDebugInstr())
4340b57cec5SDimitry Andric       continue;
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric     if (MI->getIterator() != RegionEnd) {
4370b57cec5SDimitry Andric       BB->remove(MI);
4380b57cec5SDimitry Andric       BB->insert(RegionEnd, MI);
4390b57cec5SDimitry Andric       if (!MI->isDebugInstr())
4400b57cec5SDimitry Andric         LIS->handleMove(*MI, true);
4410b57cec5SDimitry Andric     }
4420b57cec5SDimitry Andric     // Reset read-undef flags and update them later.
4430b57cec5SDimitry Andric     for (auto &Op : MI->operands())
4440b57cec5SDimitry Andric       if (Op.isReg() && Op.isDef())
4450b57cec5SDimitry Andric         Op.setIsUndef(false);
4460b57cec5SDimitry Andric     RegisterOperands RegOpers;
4470b57cec5SDimitry Andric     RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
4480b57cec5SDimitry Andric     if (!MI->isDebugInstr()) {
4490b57cec5SDimitry Andric       if (ShouldTrackLaneMasks) {
4500b57cec5SDimitry Andric         // Adjust liveness and add missing dead+read-undef flags.
4510b57cec5SDimitry Andric         SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
4520b57cec5SDimitry Andric         RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
4530b57cec5SDimitry Andric       } else {
4540b57cec5SDimitry Andric         // Adjust for missing dead-def flags.
4550b57cec5SDimitry Andric         RegOpers.detectDeadDefs(*MI, *LIS);
4560b57cec5SDimitry Andric       }
4570b57cec5SDimitry Andric     }
4580b57cec5SDimitry Andric     RegionEnd = MI->getIterator();
4590b57cec5SDimitry Andric     ++RegionEnd;
4600b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Scheduling " << *MI);
4610b57cec5SDimitry Andric   }
4620b57cec5SDimitry Andric   RegionBegin = Unsched.front()->getIterator();
4630b57cec5SDimitry Andric   Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   placeDebugValues();
4660b57cec5SDimitry Andric }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const {
4690b57cec5SDimitry Andric   GCNDownwardRPTracker RPTracker(*LIS);
4700b57cec5SDimitry Andric   RPTracker.advance(begin(), end(), &LiveIns[RegionIdx]);
4710b57cec5SDimitry Andric   return RPTracker.moveMaxPressure();
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric void GCNScheduleDAGMILive::computeBlockPressure(const MachineBasicBlock *MBB) {
4750b57cec5SDimitry Andric   GCNDownwardRPTracker RPTracker(*LIS);
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   // If the block has the only successor then live-ins of that successor are
4780b57cec5SDimitry Andric   // live-outs of the current block. We can reuse calculated live set if the
4790b57cec5SDimitry Andric   // successor will be sent to scheduling past current block.
4800b57cec5SDimitry Andric   const MachineBasicBlock *OnlySucc = nullptr;
4810b57cec5SDimitry Andric   if (MBB->succ_size() == 1 && !(*MBB->succ_begin())->empty()) {
4820b57cec5SDimitry Andric     SlotIndexes *Ind = LIS->getSlotIndexes();
4830b57cec5SDimitry Andric     if (Ind->getMBBStartIdx(MBB) < Ind->getMBBStartIdx(*MBB->succ_begin()))
4840b57cec5SDimitry Andric       OnlySucc = *MBB->succ_begin();
4850b57cec5SDimitry Andric   }
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   // Scheduler sends regions from the end of the block upwards.
4880b57cec5SDimitry Andric   size_t CurRegion = RegionIdx;
4890b57cec5SDimitry Andric   for (size_t E = Regions.size(); CurRegion != E; ++CurRegion)
4900b57cec5SDimitry Andric     if (Regions[CurRegion].first->getParent() != MBB)
4910b57cec5SDimitry Andric       break;
4920b57cec5SDimitry Andric   --CurRegion;
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   auto I = MBB->begin();
4950b57cec5SDimitry Andric   auto LiveInIt = MBBLiveIns.find(MBB);
4960b57cec5SDimitry Andric   if (LiveInIt != MBBLiveIns.end()) {
4970b57cec5SDimitry Andric     auto LiveIn = std::move(LiveInIt->second);
4980b57cec5SDimitry Andric     RPTracker.reset(*MBB->begin(), &LiveIn);
4990b57cec5SDimitry Andric     MBBLiveIns.erase(LiveInIt);
5000b57cec5SDimitry Andric   } else {
5010b57cec5SDimitry Andric     auto &Rgn = Regions[CurRegion];
5020b57cec5SDimitry Andric     I = Rgn.first;
5030b57cec5SDimitry Andric     auto *NonDbgMI = &*skipDebugInstructionsForward(Rgn.first, Rgn.second);
5040b57cec5SDimitry Andric     auto LRS = BBLiveInMap.lookup(NonDbgMI);
505fe6060f1SDimitry Andric #ifdef EXPENSIVE_CHECKS
5060b57cec5SDimitry Andric     assert(isEqual(getLiveRegsBefore(*NonDbgMI, *LIS), LRS));
507fe6060f1SDimitry Andric #endif
5080b57cec5SDimitry Andric     RPTracker.reset(*I, &LRS);
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric   for ( ; ; ) {
5120b57cec5SDimitry Andric     I = RPTracker.getNext();
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric     if (Regions[CurRegion].first == I) {
5150b57cec5SDimitry Andric       LiveIns[CurRegion] = RPTracker.getLiveRegs();
5160b57cec5SDimitry Andric       RPTracker.clearMaxPressure();
5170b57cec5SDimitry Andric     }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric     if (Regions[CurRegion].second == I) {
5200b57cec5SDimitry Andric       Pressure[CurRegion] = RPTracker.moveMaxPressure();
5210b57cec5SDimitry Andric       if (CurRegion-- == RegionIdx)
5220b57cec5SDimitry Andric         break;
5230b57cec5SDimitry Andric     }
5240b57cec5SDimitry Andric     RPTracker.advanceToNext();
5250b57cec5SDimitry Andric     RPTracker.advanceBeforeNext();
5260b57cec5SDimitry Andric   }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric   if (OnlySucc) {
5290b57cec5SDimitry Andric     if (I != MBB->end()) {
5300b57cec5SDimitry Andric       RPTracker.advanceToNext();
5310b57cec5SDimitry Andric       RPTracker.advance(MBB->end());
5320b57cec5SDimitry Andric     }
5330b57cec5SDimitry Andric     RPTracker.reset(*OnlySucc->begin(), &RPTracker.getLiveRegs());
5340b57cec5SDimitry Andric     RPTracker.advanceBeforeNext();
5350b57cec5SDimitry Andric     MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs();
5360b57cec5SDimitry Andric   }
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet>
5400b57cec5SDimitry Andric GCNScheduleDAGMILive::getBBLiveInMap() const {
5410b57cec5SDimitry Andric   assert(!Regions.empty());
5420b57cec5SDimitry Andric   std::vector<MachineInstr *> BBStarters;
5430b57cec5SDimitry Andric   BBStarters.reserve(Regions.size());
5440b57cec5SDimitry Andric   auto I = Regions.rbegin(), E = Regions.rend();
5450b57cec5SDimitry Andric   auto *BB = I->first->getParent();
5460b57cec5SDimitry Andric   do {
5470b57cec5SDimitry Andric     auto *MI = &*skipDebugInstructionsForward(I->first, I->second);
5480b57cec5SDimitry Andric     BBStarters.push_back(MI);
5490b57cec5SDimitry Andric     do {
5500b57cec5SDimitry Andric       ++I;
5510b57cec5SDimitry Andric     } while (I != E && I->first->getParent() == BB);
5520b57cec5SDimitry Andric   } while (I != E);
5530b57cec5SDimitry Andric   return getLiveRegMap(BBStarters, false /*After*/, *LIS);
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric 
5560b57cec5SDimitry Andric void GCNScheduleDAGMILive::finalizeSchedule() {
5570b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n");
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric   LiveIns.resize(Regions.size());
5600b57cec5SDimitry Andric   Pressure.resize(Regions.size());
5615ffd83dbSDimitry Andric   RescheduleRegions.resize(Regions.size());
562fe6060f1SDimitry Andric   RegionsWithClusters.resize(Regions.size());
563fe6060f1SDimitry Andric   RegionsWithHighRP.resize(Regions.size());
5645ffd83dbSDimitry Andric   RescheduleRegions.set();
565fe6060f1SDimitry Andric   RegionsWithClusters.reset();
566fe6060f1SDimitry Andric   RegionsWithHighRP.reset();
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   if (!Regions.empty())
5690b57cec5SDimitry Andric     BBLiveInMap = getBBLiveInMap();
5700b57cec5SDimitry Andric 
5715ffd83dbSDimitry Andric   std::vector<std::unique_ptr<ScheduleDAGMutation>> SavedMutations;
5725ffd83dbSDimitry Andric 
5730b57cec5SDimitry Andric   do {
5740b57cec5SDimitry Andric     Stage++;
5750b57cec5SDimitry Andric     RegionIdx = 0;
5760b57cec5SDimitry Andric     MachineBasicBlock *MBB = nullptr;
5770b57cec5SDimitry Andric 
5785ffd83dbSDimitry Andric     if (Stage > InitialSchedule) {
5795ffd83dbSDimitry Andric       if (!LIS)
5805ffd83dbSDimitry Andric         break;
5815ffd83dbSDimitry Andric 
5820b57cec5SDimitry Andric       // Retry function scheduling if we found resulting occupancy and it is
5830b57cec5SDimitry Andric       // lower than used for first pass scheduling. This will give more freedom
5840b57cec5SDimitry Andric       // to schedule low register pressure blocks.
5850b57cec5SDimitry Andric       // Code is partially copied from MachineSchedulerBase::scheduleRegions().
5860b57cec5SDimitry Andric 
5875ffd83dbSDimitry Andric       if (Stage == UnclusteredReschedule) {
5885ffd83dbSDimitry Andric         if (RescheduleRegions.none())
5895ffd83dbSDimitry Andric           continue;
5905ffd83dbSDimitry Andric         LLVM_DEBUG(dbgs() <<
5915ffd83dbSDimitry Andric           "Retrying function scheduling without clustering.\n");
5925ffd83dbSDimitry Andric       }
5935ffd83dbSDimitry Andric 
5945ffd83dbSDimitry Andric       if (Stage == ClusteredLowOccupancyReschedule) {
5955ffd83dbSDimitry Andric         if (StartingOccupancy <= MinOccupancy)
5960b57cec5SDimitry Andric           break;
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric         LLVM_DEBUG(
5990b57cec5SDimitry Andric             dbgs()
6000b57cec5SDimitry Andric             << "Retrying function scheduling with lowest recorded occupancy "
6010b57cec5SDimitry Andric             << MinOccupancy << ".\n");
6020b57cec5SDimitry Andric       }
6035ffd83dbSDimitry Andric     }
6045ffd83dbSDimitry Andric 
6055ffd83dbSDimitry Andric     if (Stage == UnclusteredReschedule)
6065ffd83dbSDimitry Andric       SavedMutations.swap(Mutations);
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric     for (auto Region : Regions) {
609fe6060f1SDimitry Andric       if ((Stage == UnclusteredReschedule && !RescheduleRegions[RegionIdx]) ||
610fe6060f1SDimitry Andric           (Stage == ClusteredLowOccupancyReschedule &&
611fe6060f1SDimitry Andric            !RegionsWithClusters[RegionIdx] && !RegionsWithHighRP[RegionIdx])) {
612fe6060f1SDimitry Andric 
613e8d8bef9SDimitry Andric         ++RegionIdx;
6145ffd83dbSDimitry Andric         continue;
615e8d8bef9SDimitry Andric       }
6165ffd83dbSDimitry Andric 
6170b57cec5SDimitry Andric       RegionBegin = Region.first;
6180b57cec5SDimitry Andric       RegionEnd = Region.second;
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric       if (RegionBegin->getParent() != MBB) {
6210b57cec5SDimitry Andric         if (MBB) finishBlock();
6220b57cec5SDimitry Andric         MBB = RegionBegin->getParent();
6230b57cec5SDimitry Andric         startBlock(MBB);
6245ffd83dbSDimitry Andric         if (Stage == InitialSchedule)
6250b57cec5SDimitry Andric           computeBlockPressure(MBB);
6260b57cec5SDimitry Andric       }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric       unsigned NumRegionInstrs = std::distance(begin(), end());
6290b57cec5SDimitry Andric       enterRegion(MBB, begin(), end(), NumRegionInstrs);
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric       // Skip empty scheduling regions (0 or 1 schedulable instructions).
6320b57cec5SDimitry Andric       if (begin() == end() || begin() == std::prev(end())) {
6330b57cec5SDimitry Andric         exitRegion();
6340b57cec5SDimitry Andric         continue;
6350b57cec5SDimitry Andric       }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "********** MI Scheduling **********\n");
6380b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << MF.getName() << ":" << printMBBReference(*MBB) << " "
6390b57cec5SDimitry Andric                         << MBB->getName() << "\n  From: " << *begin()
6400b57cec5SDimitry Andric                         << "    To: ";
6410b57cec5SDimitry Andric                  if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
6420b57cec5SDimitry Andric                  else dbgs() << "End";
6430b57cec5SDimitry Andric                  dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric       schedule();
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric       exitRegion();
6480b57cec5SDimitry Andric       ++RegionIdx;
6490b57cec5SDimitry Andric     }
6500b57cec5SDimitry Andric     finishBlock();
6510b57cec5SDimitry Andric 
6525ffd83dbSDimitry Andric     if (Stage == UnclusteredReschedule)
6535ffd83dbSDimitry Andric       SavedMutations.swap(Mutations);
6545ffd83dbSDimitry Andric   } while (Stage != LastStage);
6550b57cec5SDimitry Andric }
656