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