1 //===-- GCNSchedStrategy.cpp - GCN Scheduler Strategy ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// This contains a MachineSchedStrategy implementation for maximizing wave
12 /// occupancy on GCN hardware.
13 //===----------------------------------------------------------------------===//
14 
15 #include "GCNSchedStrategy.h"
16 #include "AMDGPUSubtarget.h"
17 #include "SIInstrInfo.h"
18 #include "SIMachineFunctionInfo.h"
19 #include "SIRegisterInfo.h"
20 #include "llvm/CodeGen/RegisterClassInfo.h"
21 #include "llvm/Support/MathExtras.h"
22 
23 #define DEBUG_TYPE "misched"
24 
25 using namespace llvm;
26 
27 GCNMaxOccupancySchedStrategy::GCNMaxOccupancySchedStrategy(
28     const MachineSchedContext *C) :
29     GenericScheduler(C), TargetOccupancy(0), MF(nullptr) { }
30 
31 static unsigned getMaxWaves(unsigned SGPRs, unsigned VGPRs,
32                             const MachineFunction &MF) {
33 
34   const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
35   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
36   unsigned MinRegOccupancy = std::min(ST.getOccupancyWithNumSGPRs(SGPRs),
37                                       ST.getOccupancyWithNumVGPRs(VGPRs));
38   return std::min(MinRegOccupancy,
39                   ST.getOccupancyWithLocalMemSize(MFI->getLDSSize(),
40                                                   *MF.getFunction()));
41 }
42 
43 void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) {
44   GenericScheduler::initialize(DAG);
45 
46   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
47 
48   MF = &DAG->MF;
49 
50   const SISubtarget &ST = MF->getSubtarget<SISubtarget>();
51 
52   // FIXME: This is also necessary, because some passes that run after
53   // scheduling and before regalloc increase register pressure.
54   const int ErrorMargin = 3;
55 
56   SGPRExcessLimit = Context->RegClassInfo
57     ->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass) - ErrorMargin;
58   VGPRExcessLimit = Context->RegClassInfo
59     ->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass) - ErrorMargin;
60   if (TargetOccupancy) {
61     SGPRCriticalLimit = ST.getMaxNumSGPRs(TargetOccupancy, true);
62     VGPRCriticalLimit = ST.getMaxNumVGPRs(TargetOccupancy);
63   } else {
64     SGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
65                                                     SRI->getSGPRPressureSet());
66     VGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
67                                                     SRI->getVGPRPressureSet());
68   }
69 
70   SGPRCriticalLimit -= ErrorMargin;
71   VGPRCriticalLimit -= ErrorMargin;
72 }
73 
74 void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU,
75                                      bool AtTop, const RegPressureTracker &RPTracker,
76                                      const SIRegisterInfo *SRI,
77                                      unsigned SGPRPressure,
78                                      unsigned VGPRPressure) {
79 
80   Cand.SU = SU;
81   Cand.AtTop = AtTop;
82 
83   // getDownwardPressure() and getUpwardPressure() make temporary changes to
84   // the the tracker, so we need to pass those function a non-const copy.
85   RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
86 
87   std::vector<unsigned> Pressure;
88   std::vector<unsigned> MaxPressure;
89 
90   if (AtTop)
91     TempTracker.getDownwardPressure(SU->getInstr(), Pressure, MaxPressure);
92   else {
93     // FIXME: I think for bottom up scheduling, the register pressure is cached
94     // and can be retrieved by DAG->getPressureDif(SU).
95     TempTracker.getUpwardPressure(SU->getInstr(), Pressure, MaxPressure);
96   }
97 
98   unsigned NewSGPRPressure = Pressure[SRI->getSGPRPressureSet()];
99   unsigned NewVGPRPressure = Pressure[SRI->getVGPRPressureSet()];
100 
101   // If two instructions increase the pressure of different register sets
102   // by the same amount, the generic scheduler will prefer to schedule the
103   // instruction that increases the set with the least amount of registers,
104   // which in our case would be SGPRs.  This is rarely what we want, so
105   // when we report excess/critical register pressure, we do it either
106   // only for VGPRs or only for SGPRs.
107 
108   // FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs.
109   const unsigned MaxVGPRPressureInc = 16;
110   bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit;
111   bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit;
112 
113 
114   // FIXME: We have to enter REG-EXCESS before we reach the actual threshold
115   // to increase the likelihood we don't go over the limits.  We should improve
116   // the analysis to look through dependencies to find the path with the least
117   // register pressure.
118 
119   // We only need to update the RPDelata for instructions that increase
120   // register pressure.  Instructions that decrease or keep reg pressure
121   // the same will be marked as RegExcess in tryCandidate() when they
122   // are compared with instructions that increase the register pressure.
123   if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) {
124     Cand.RPDelta.Excess = PressureChange(SRI->getVGPRPressureSet());
125     Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit);
126   }
127 
128   if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) {
129     Cand.RPDelta.Excess = PressureChange(SRI->getSGPRPressureSet());
130     Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit);
131   }
132 
133   // Register pressure is considered 'CRITICAL' if it is approaching a value
134   // that would reduce the wave occupancy for the execution unit.  When
135   // register pressure is 'CRITICAL', increading SGPR and VGPR pressure both
136   // has the same cost, so we don't need to prefer one over the other.
137 
138   int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit;
139   int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit;
140 
141   if (SGPRDelta >= 0 || VGPRDelta >= 0) {
142     if (SGPRDelta > VGPRDelta) {
143       Cand.RPDelta.CriticalMax = PressureChange(SRI->getSGPRPressureSet());
144       Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta);
145     } else {
146       Cand.RPDelta.CriticalMax = PressureChange(SRI->getVGPRPressureSet());
147       Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta);
148     }
149   }
150 }
151 
152 // This function is mostly cut and pasted from
153 // GenericScheduler::pickNodeFromQueue()
154 void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone,
155                                          const CandPolicy &ZonePolicy,
156                                          const RegPressureTracker &RPTracker,
157                                          SchedCandidate &Cand) {
158   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
159   ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos();
160   unsigned SGPRPressure = Pressure[SRI->getSGPRPressureSet()];
161   unsigned VGPRPressure = Pressure[SRI->getVGPRPressureSet()];
162   ReadyQueue &Q = Zone.Available;
163   for (SUnit *SU : Q) {
164 
165     SchedCandidate TryCand(ZonePolicy);
166     initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI,
167                   SGPRPressure, VGPRPressure);
168     // Pass SchedBoundary only when comparing nodes from the same boundary.
169     SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
170     GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg);
171     if (TryCand.Reason != NoCand) {
172       // Initialize resource delta if needed in case future heuristics query it.
173       if (TryCand.ResDelta == SchedResourceDelta())
174         TryCand.initResourceDelta(Zone.DAG, SchedModel);
175       Cand.setBest(TryCand);
176     }
177   }
178 }
179 
180 // This function is mostly cut and pasted from
181 // GenericScheduler::pickNodeBidirectional()
182 SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) {
183   // Schedule as far as possible in the direction of no choice. This is most
184   // efficient, but also provides the best heuristics for CriticalPSets.
185   if (SUnit *SU = Bot.pickOnlyChoice()) {
186     IsTopNode = false;
187     return SU;
188   }
189   if (SUnit *SU = Top.pickOnlyChoice()) {
190     IsTopNode = true;
191     return SU;
192   }
193   // Set the bottom-up policy based on the state of the current bottom zone and
194   // the instructions outside the zone, including the top zone.
195   CandPolicy BotPolicy;
196   setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
197   // Set the top-down policy based on the state of the current top zone and
198   // the instructions outside the zone, including the bottom zone.
199   CandPolicy TopPolicy;
200   setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
201 
202   // See if BotCand is still valid (because we previously scheduled from Top).
203   DEBUG(dbgs() << "Picking from Bot:\n");
204   if (!BotCand.isValid() || BotCand.SU->isScheduled ||
205       BotCand.Policy != BotPolicy) {
206     BotCand.reset(CandPolicy());
207     pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
208     assert(BotCand.Reason != NoCand && "failed to find the first candidate");
209   } else {
210     DEBUG(traceCandidate(BotCand));
211   }
212 
213   // Check if the top Q has a better candidate.
214   DEBUG(dbgs() << "Picking from Top:\n");
215   if (!TopCand.isValid() || TopCand.SU->isScheduled ||
216       TopCand.Policy != TopPolicy) {
217     TopCand.reset(CandPolicy());
218     pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
219     assert(TopCand.Reason != NoCand && "failed to find the first candidate");
220   } else {
221     DEBUG(traceCandidate(TopCand));
222   }
223 
224   // Pick best from BotCand and TopCand.
225   DEBUG(
226     dbgs() << "Top Cand: ";
227     traceCandidate(TopCand);
228     dbgs() << "Bot Cand: ";
229     traceCandidate(BotCand);
230   );
231   SchedCandidate Cand;
232   if (TopCand.Reason == BotCand.Reason) {
233     Cand = BotCand;
234     GenericSchedulerBase::CandReason TopReason = TopCand.Reason;
235     TopCand.Reason = NoCand;
236     GenericScheduler::tryCandidate(Cand, TopCand, nullptr);
237     if (TopCand.Reason != NoCand) {
238       Cand.setBest(TopCand);
239     } else {
240       TopCand.Reason = TopReason;
241     }
242   } else {
243     if (TopCand.Reason == RegExcess && TopCand.RPDelta.Excess.getUnitInc() <= 0) {
244       Cand = TopCand;
245     } else if (BotCand.Reason == RegExcess && BotCand.RPDelta.Excess.getUnitInc() <= 0) {
246       Cand = BotCand;
247     } else if (TopCand.Reason == RegCritical && TopCand.RPDelta.CriticalMax.getUnitInc() <= 0) {
248       Cand = TopCand;
249     } else if (BotCand.Reason == RegCritical && BotCand.RPDelta.CriticalMax.getUnitInc() <= 0) {
250       Cand = BotCand;
251     } else {
252       if (BotCand.Reason > TopCand.Reason) {
253         Cand = TopCand;
254       } else {
255         Cand = BotCand;
256       }
257     }
258   }
259   DEBUG(
260     dbgs() << "Picking: ";
261     traceCandidate(Cand);
262   );
263 
264   IsTopNode = Cand.AtTop;
265   return Cand.SU;
266 }
267 
268 // This function is mostly cut and pasted from
269 // GenericScheduler::pickNode()
270 SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) {
271   if (DAG->top() == DAG->bottom()) {
272     assert(Top.Available.empty() && Top.Pending.empty() &&
273            Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
274     return nullptr;
275   }
276   SUnit *SU;
277   do {
278     if (RegionPolicy.OnlyTopDown) {
279       SU = Top.pickOnlyChoice();
280       if (!SU) {
281         CandPolicy NoPolicy;
282         TopCand.reset(NoPolicy);
283         pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
284         assert(TopCand.Reason != NoCand && "failed to find a candidate");
285         SU = TopCand.SU;
286       }
287       IsTopNode = true;
288     } else if (RegionPolicy.OnlyBottomUp) {
289       SU = Bot.pickOnlyChoice();
290       if (!SU) {
291         CandPolicy NoPolicy;
292         BotCand.reset(NoPolicy);
293         pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
294         assert(BotCand.Reason != NoCand && "failed to find a candidate");
295         SU = BotCand.SU;
296       }
297       IsTopNode = false;
298     } else {
299       SU = pickNodeBidirectional(IsTopNode);
300     }
301   } while (SU->isScheduled);
302 
303   if (SU->isTopReady())
304     Top.removeReady(SU);
305   if (SU->isBottomReady())
306     Bot.removeReady(SU);
307 
308   DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr());
309   return SU;
310 }
311 
312 GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C,
313                         std::unique_ptr<MachineSchedStrategy> S) :
314   ScheduleDAGMILive(C, std::move(S)),
315   ST(MF.getSubtarget<SISubtarget>()),
316   MFI(*MF.getInfo<SIMachineFunctionInfo>()),
317   StartingOccupancy(ST.getOccupancyWithLocalMemSize(MFI.getLDSSize(),
318                                                     *MF.getFunction())),
319   MinOccupancy(StartingOccupancy), Stage(0) {
320 
321   DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n");
322 }
323 
324 void GCNScheduleDAGMILive::enterRegion(MachineBasicBlock *bb,
325                                        MachineBasicBlock::iterator begin,
326                                        MachineBasicBlock::iterator end,
327                                        unsigned regioninstrs) {
328   ScheduleDAGMILive::enterRegion(bb, begin, end, regioninstrs);
329 
330   if (Stage == 0)
331     Regions.push_back(std::make_pair(begin, end));
332 }
333 
334 void GCNScheduleDAGMILive::schedule() {
335   std::vector<MachineInstr*> Unsched;
336   Unsched.reserve(NumRegionInstrs);
337   for (auto &I : *this)
338     Unsched.push_back(&I);
339 
340   std::pair<unsigned, unsigned> PressureBefore;
341   if (LIS) {
342     DEBUG(dbgs() << "Pressure before scheduling:\n");
343     discoverLiveIns();
344     PressureBefore = getRealRegPressure();
345   }
346 
347   ScheduleDAGMILive::schedule();
348   if (!LIS)
349     return;
350 
351   // Check the results of scheduling.
352   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
353   DEBUG(dbgs() << "Pressure after scheduling:\n");
354   auto PressureAfter = getRealRegPressure();
355   LiveIns.clear();
356 
357   if (PressureAfter.first <= S.SGPRCriticalLimit &&
358       PressureAfter.second <= S.VGPRCriticalLimit) {
359     DEBUG(dbgs() << "Pressure in desired limits, done.\n");
360     return;
361   }
362   unsigned WavesAfter = getMaxWaves(PressureAfter.first,
363                                     PressureAfter.second, MF);
364   unsigned WavesBefore = getMaxWaves(PressureBefore.first,
365                                       PressureBefore.second, MF);
366   DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore <<
367                   ", after " << WavesAfter << ".\n");
368 
369   // We could not keep current target occupancy because of the just scheduled
370   // region. Record new occupancy for next scheduling cycle.
371   unsigned NewOccupancy = std::max(WavesAfter, WavesBefore);
372   if (NewOccupancy < MinOccupancy) {
373     MinOccupancy = NewOccupancy;
374     DEBUG(dbgs() << "Occupancy lowered for the function to "
375                  << MinOccupancy << ".\n");
376   }
377 
378   if (WavesAfter >= WavesBefore)
379     return;
380 
381   DEBUG(dbgs() << "Attempting to revert scheduling.\n");
382   RegionEnd = RegionBegin;
383   for (MachineInstr *MI : Unsched) {
384     if (MI->getIterator() != RegionEnd) {
385       BB->remove(MI);
386       BB->insert(RegionEnd, MI);
387       LIS->handleMove(*MI, true);
388     }
389     // Reset read-undef flags and update them later.
390     for (auto &Op : MI->operands())
391       if (Op.isReg() && Op.isDef())
392         Op.setIsUndef(false);
393     RegisterOperands RegOpers;
394     RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
395     if (ShouldTrackLaneMasks) {
396       // Adjust liveness and add missing dead+read-undef flags.
397       SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
398       RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
399     } else {
400       // Adjust for missing dead-def flags.
401       RegOpers.detectDeadDefs(*MI, *LIS);
402     }
403     RegionEnd = MI->getIterator();
404     ++RegionEnd;
405     DEBUG(dbgs() << "Scheduling " << *MI);
406   }
407   RegionBegin = Unsched.front()->getIterator();
408 
409   placeDebugValues();
410 }
411 
412 static inline void setMask(const MachineRegisterInfo &MRI,
413                            const SIRegisterInfo *SRI, unsigned Reg,
414                            LaneBitmask &PrevMask, LaneBitmask NewMask,
415                            unsigned &SGPRs, unsigned &VGPRs) {
416   int NewRegs = countPopulation(NewMask.getAsInteger()) -
417                 countPopulation(PrevMask.getAsInteger());
418   if (SRI->isSGPRReg(MRI, Reg))
419     SGPRs += NewRegs;
420   if (SRI->isVGPR(MRI, Reg))
421     VGPRs += NewRegs;
422   assert ((int)SGPRs >= 0 && (int)VGPRs >= 0);
423   PrevMask = NewMask;
424 }
425 
426 void GCNScheduleDAGMILive::discoverLiveIns() {
427   unsigned SGPRs = 0;
428   unsigned VGPRs = 0;
429 
430   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
431   SlotIndex SI = LIS->getInstructionIndex(*begin()).getBaseIndex();
432   assert (SI.isValid());
433 
434   DEBUG(dbgs() << "Region live-ins:");
435   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
436     unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
437     if (MRI.reg_nodbg_empty(Reg))
438       continue;
439     const LiveInterval &LI = LIS->getInterval(Reg);
440     LaneBitmask LaneMask = LaneBitmask::getNone();
441     if (LI.hasSubRanges()) {
442       for (const auto &S : LI.subranges())
443         if (S.liveAt(SI))
444           LaneMask |= S.LaneMask;
445     } else if (LI.liveAt(SI)) {
446       LaneMask = MRI.getMaxLaneMaskForVReg(Reg);
447     }
448 
449     if (LaneMask.any()) {
450       setMask(MRI, SRI, Reg, LiveIns[Reg], LaneMask, SGPRs, VGPRs);
451 
452       DEBUG(dbgs() << ' ' << PrintVRegOrUnit(Reg, SRI) << ':'
453                    << PrintLaneMask(LiveIns[Reg]));
454     }
455   }
456 
457   LiveInPressure = std::make_pair(SGPRs, VGPRs);
458 
459   DEBUG(dbgs() << "\nLive-in pressure:\nSGPR = " << SGPRs
460                << "\nVGPR = " << VGPRs << '\n');
461 }
462 
463 std::pair<unsigned, unsigned>
464 GCNScheduleDAGMILive::getRealRegPressure() const {
465   unsigned SGPRs, MaxSGPRs, VGPRs, MaxVGPRs;
466   SGPRs = MaxSGPRs = LiveInPressure.first;
467   VGPRs = MaxVGPRs = LiveInPressure.second;
468 
469   const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
470   DenseMap<unsigned, LaneBitmask> LiveRegs(LiveIns);
471 
472   for (const MachineInstr &MI : *this) {
473     if (MI.isDebugValue())
474       continue;
475     SlotIndex SI = LIS->getInstructionIndex(MI).getBaseIndex();
476     assert (SI.isValid());
477 
478     // Remove dead registers or mask bits.
479     for (auto &It : LiveRegs) {
480       if (It.second.none())
481         continue;
482       const LiveInterval &LI = LIS->getInterval(It.first);
483       if (LI.hasSubRanges()) {
484         for (const auto &S : LI.subranges())
485           if (!S.liveAt(SI))
486             setMask(MRI, SRI, It.first, It.second, It.second & ~S.LaneMask,
487                     SGPRs, VGPRs);
488       } else if (!LI.liveAt(SI)) {
489         setMask(MRI, SRI, It.first, It.second, LaneBitmask::getNone(),
490                 SGPRs, VGPRs);
491       }
492     }
493 
494     // Add new registers or mask bits.
495     for (const auto &MO : MI.defs()) {
496       if (!MO.isReg())
497         continue;
498       unsigned Reg = MO.getReg();
499       if (!TargetRegisterInfo::isVirtualRegister(Reg))
500         continue;
501       unsigned SubRegIdx = MO.getSubReg();
502       LaneBitmask LaneMask = SubRegIdx != 0
503                              ? TRI->getSubRegIndexLaneMask(SubRegIdx)
504                              : MRI.getMaxLaneMaskForVReg(Reg);
505       LaneBitmask &LM = LiveRegs[Reg];
506       setMask(MRI, SRI, Reg, LM, LM | LaneMask, SGPRs, VGPRs);
507     }
508     MaxSGPRs = std::max(MaxSGPRs, SGPRs);
509     MaxVGPRs = std::max(MaxVGPRs, VGPRs);
510   }
511 
512   DEBUG(dbgs() << "Real region's register pressure:\nSGPR = " << MaxSGPRs
513                << "\nVGPR = " << MaxVGPRs << '\n');
514 
515   return std::make_pair(MaxSGPRs, MaxVGPRs);
516 }
517 
518 void GCNScheduleDAGMILive::finalizeSchedule() {
519   // Retry function scheduling if we found resulting occupancy and it is
520   // lower than used for first pass scheduling. This will give more freedom
521   // to schedule low register pressure blocks.
522   // Code is partially copied from MachineSchedulerBase::scheduleRegions().
523 
524   if (!LIS || StartingOccupancy <= MinOccupancy)
525     return;
526 
527   DEBUG(dbgs() << "Retrying function scheduling with lowest recorded occupancy "
528                << MinOccupancy << ".\n");
529 
530   Stage++;
531   GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
532   S.setTargetOccupancy(MinOccupancy);
533 
534   MachineBasicBlock *MBB = nullptr;
535   for (auto Region : Regions) {
536     RegionBegin = Region.first;
537     RegionEnd = Region.second;
538 
539     if (RegionBegin->getParent() != MBB) {
540       if (MBB) finishBlock();
541       MBB = RegionBegin->getParent();
542       startBlock(MBB);
543     }
544 
545     unsigned NumRegionInstrs = std::distance(begin(), end());
546     enterRegion(MBB, begin(), end(), NumRegionInstrs);
547 
548     // Skip empty scheduling regions (0 or 1 schedulable instructions).
549     if (begin() == end() || begin() == std::prev(end())) {
550       exitRegion();
551       continue;
552     }
553     DEBUG(dbgs() << "********** MI Scheduling **********\n");
554     DEBUG(dbgs() << MF.getName()
555           << ":BB#" << MBB->getNumber() << " " << MBB->getName()
556           << "\n  From: " << *begin() << "    To: ";
557           if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
558           else dbgs() << "End";
559           dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
560 
561     schedule();
562 
563     exitRegion();
564   }
565   finishBlock();
566   LiveIns.shrink_and_clear();
567 }
568