@@ -312,6 +312,33 @@ bool SIMachineFunctionInfo::isCalleeSavedReg(const MCPhysReg *CSRegs,
312
312
return false ;
313
313
}
314
314
315
+ void SIMachineFunctionInfo::shiftSpillPhysVGPRsToLowestRange (
316
+ MachineFunction &MF) {
317
+ const SIRegisterInfo *TRI = MF.getSubtarget <GCNSubtarget>().getRegisterInfo ();
318
+ MachineRegisterInfo &MRI = MF.getRegInfo ();
319
+ for (unsigned I = 0 , E = SpillPhysVGPRs.size (); I < E; ++I) {
320
+ Register Reg = SpillPhysVGPRs[I];
321
+ Register NewReg =
322
+ TRI->findUnusedRegister (MRI, &AMDGPU::VGPR_32RegClass, MF);
323
+ if (!NewReg || NewReg >= Reg)
324
+ break ;
325
+
326
+ MRI.replaceRegWith (Reg, NewReg);
327
+
328
+ // Update various tables with the new VGPR.
329
+ SpillPhysVGPRs[I] = NewReg;
330
+ WWMReservedRegs.remove (Reg);
331
+ WWMReservedRegs.insert (NewReg);
332
+ WWMSpills.insert (std::make_pair (NewReg, WWMSpills[Reg]));
333
+ WWMSpills.erase (Reg);
334
+
335
+ for (MachineBasicBlock &MBB : MF) {
336
+ MBB.removeLiveIn (Reg);
337
+ MBB.sortUniqueLiveIns ();
338
+ }
339
+ }
340
+ }
341
+
315
342
bool SIMachineFunctionInfo::allocateVirtualVGPRForSGPRSpills (
316
343
MachineFunction &MF, int FI, unsigned LaneIndex) {
317
344
MachineRegisterInfo &MRI = MF.getRegInfo ();
@@ -329,13 +356,17 @@ bool SIMachineFunctionInfo::allocateVirtualVGPRForSGPRSpills(
329
356
}
330
357
331
358
bool SIMachineFunctionInfo::allocatePhysicalVGPRForSGPRSpills (
332
- MachineFunction &MF, int FI, unsigned LaneIndex) {
359
+ MachineFunction &MF, int FI, unsigned LaneIndex, bool IsPrologEpilog ) {
333
360
const GCNSubtarget &ST = MF.getSubtarget <GCNSubtarget>();
334
361
const SIRegisterInfo *TRI = ST.getRegisterInfo ();
335
362
MachineRegisterInfo &MRI = MF.getRegInfo ();
336
363
Register LaneVGPR;
337
364
if (!LaneIndex) {
338
- LaneVGPR = TRI->findUnusedRegister (MRI, &AMDGPU::VGPR_32RegClass, MF);
365
+ // Find the highest available register if called before RA to ensure the
366
+ // lowest registers are available for allocation. The LaneVGPR, in that
367
+ // case, will be shifted back to the lowest range after VGPR allocation.
368
+ LaneVGPR = TRI->findUnusedRegister (MRI, &AMDGPU::VGPR_32RegClass, MF,
369
+ !IsPrologEpilog);
339
370
if (LaneVGPR == AMDGPU::NoRegister) {
340
371
// We have no VGPRs left for spilling SGPRs. Reset because we will not
341
372
// partially spill the SGPR to VGPRs.
@@ -359,12 +390,12 @@ bool SIMachineFunctionInfo::allocatePhysicalVGPRForSGPRSpills(
359
390
return true ;
360
391
}
361
392
362
- bool SIMachineFunctionInfo::allocateSGPRSpillToVGPRLane (MachineFunction &MF,
363
- int FI,
364
- bool IsPrologEpilog) {
393
+ bool SIMachineFunctionInfo::allocateSGPRSpillToVGPRLane (
394
+ MachineFunction &MF, int FI, bool SpillToPhysVGPRLane ,
395
+ bool IsPrologEpilog) {
365
396
std::vector<SIRegisterInfo::SpilledReg> &SpillLanes =
366
- IsPrologEpilog ? SGPRSpillsToPhysicalVGPRLanes[FI]
367
- : SGPRSpillsToVirtualVGPRLanes[FI];
397
+ SpillToPhysVGPRLane ? SGPRSpillsToPhysicalVGPRLanes[FI]
398
+ : SGPRSpillsToVirtualVGPRLanes[FI];
368
399
369
400
// This has already been allocated.
370
401
if (!SpillLanes.empty ())
@@ -384,14 +415,15 @@ bool SIMachineFunctionInfo::allocateSGPRSpillToVGPRLane(MachineFunction &MF,
384
415
assert (ST.getRegisterInfo ()->spillSGPRToVGPR () &&
385
416
" not spilling SGPRs to VGPRs" );
386
417
387
- unsigned &NumSpillLanes =
388
- IsPrologEpilog ? NumPhysicalVGPRSpillLanes : NumVirtualVGPRSpillLanes;
418
+ unsigned &NumSpillLanes = SpillToPhysVGPRLane ? NumPhysicalVGPRSpillLanes
419
+ : NumVirtualVGPRSpillLanes;
389
420
390
421
for (unsigned I = 0 ; I < NumLanes; ++I, ++NumSpillLanes) {
391
422
unsigned LaneIndex = (NumSpillLanes % WaveSize);
392
423
393
- bool Allocated = IsPrologEpilog
394
- ? allocatePhysicalVGPRForSGPRSpills (MF, FI, LaneIndex)
424
+ bool Allocated = SpillToPhysVGPRLane
425
+ ? allocatePhysicalVGPRForSGPRSpills (MF, FI, LaneIndex,
426
+ IsPrologEpilog)
395
427
: allocateVirtualVGPRForSGPRSpills (MF, FI, LaneIndex);
396
428
if (!Allocated) {
397
429
NumSpillLanes -= I;
0 commit comments