@@ -663,12 +663,21 @@ void SIFrameLowering::emitEntryFunctionFlatScratchInit(
663663}
664664
665665// Note SGPRSpill stack IDs should only be used for SGPR spilling to VGPRs, not
666- // memory. They should have been removed by now.
667- static bool allStackObjectsAreDead (const MachineFrameInfo &MFI) {
666+ // memory. They should have been removed by now, except CFI Saved Reg spills.
667+ static bool allStackObjectsAreDead (const MachineFunction &MF) {
668+ const MachineFrameInfo &MFI = MF.getFrameInfo ();
669+ const SIMachineFunctionInfo *FuncInfo = MF.getInfo <SIMachineFunctionInfo>();
668670 for (int I = MFI.getObjectIndexBegin (), E = MFI.getObjectIndexEnd ();
669671 I != E; ++I) {
670- if (!MFI.isDeadObjectIndex (I))
672+ if (!MFI.isDeadObjectIndex (I)) {
673+ // determineCalleeSaves() might have added the SGPRSpill stack IDs for
674+ // CFI saves into scratch VGPR, ignore them
675+ if (MFI.getStackID (I) == TargetStackID::SGPRSpill &&
676+ FuncInfo->checkIndexInPrologEpilogSGPRSpills (I)) {
677+ continue ;
678+ }
671679 return false ;
680+ }
672681 }
673682
674683 return true ;
@@ -688,8 +697,8 @@ Register SIFrameLowering::getEntryFunctionReservedScratchRsrcReg(
688697
689698 Register ScratchRsrcReg = MFI->getScratchRSrcReg ();
690699
691- if (!ScratchRsrcReg || (!MRI. isPhysRegUsed (ScratchRsrcReg) &&
692- allStackObjectsAreDead (MF. getFrameInfo () )))
700+ if (!ScratchRsrcReg ||
701+ (!MRI. isPhysRegUsed (ScratchRsrcReg) && allStackObjectsAreDead (MF)))
693702 return Register ();
694703
695704 if (ST.hasSGPRInitBug () ||
@@ -916,7 +925,7 @@ void SIFrameLowering::emitEntryFunctionPrologue(MachineFunction &MF,
916925 bool NeedsFlatScratchInit =
917926 MFI->getUserSGPRInfo ().hasFlatScratchInit () &&
918927 (MRI.isPhysRegUsed (AMDGPU::FLAT_SCR) || FrameInfo.hasCalls () ||
919- (!allStackObjectsAreDead (FrameInfo ) && ST.enableFlatScratch ()));
928+ (!allStackObjectsAreDead (MF ) && ST.enableFlatScratch ()));
920929
921930 if ((NeedsFlatScratchInit || ScratchRsrcReg) &&
922931 PreloadedScratchWaveOffsetReg && !ST.flatScratchIsArchitected ()) {
@@ -1309,6 +1318,11 @@ void SIFrameLowering::emitCSRSpillStores(MachineFunction &MF,
13091318 LiveUnits.addReg (Reg);
13101319 }
13111320 }
1321+
1322+ // Remove the spill entry created for EXEC. It is needed only for CFISaves in
1323+ // the prologue.
1324+ if (TRI.isCFISavedRegsSpillEnabled ())
1325+ FuncInfo->removePrologEpilogSGPRSpillEntry (TRI.getExec ());
13121326}
13131327
13141328void SIFrameLowering::emitCSRSpillRestores (
@@ -1790,14 +1804,14 @@ void SIFrameLowering::processFunctionBeforeFrameFinalized(
17901804 // can. Any remaining SGPR spills will go to memory, so move them back to the
17911805 // default stack.
17921806 bool HaveSGPRToVMemSpill =
1793- FuncInfo->removeDeadFrameIndices (MFI , /* ResetSGPRSpillStackIDs*/ true );
1807+ FuncInfo->removeDeadFrameIndices (MF , /* ResetSGPRSpillStackIDs*/ true );
17941808 assert (allSGPRSpillsAreDead (MF) &&
17951809 " SGPR spill should have been removed in SILowerSGPRSpills" );
17961810
17971811 // FIXME: The other checks should be redundant with allStackObjectsAreDead,
17981812 // but currently hasNonSpillStackObjects is set only from source
17991813 // allocas. Stack temps produced from legalization are not counted currently.
1800- if (!allStackObjectsAreDead (MFI )) {
1814+ if (!allStackObjectsAreDead (MF )) {
18011815 assert (RS && " RegScavenger required if spilling" );
18021816
18031817 // Add an emergency spill slot
@@ -1897,6 +1911,18 @@ void SIFrameLowering::determinePrologEpilogSGPRSaves(
18971911 MFI->setSGPRForEXECCopy (AMDGPU::NoRegister);
18981912 }
18991913
1914+ if (TRI->isCFISavedRegsSpillEnabled ()) {
1915+ Register Exec = TRI->getExec ();
1916+ assert (!MFI->hasPrologEpilogSGPRSpillEntry (Exec) &&
1917+ " Re-reserving spill slot for EXEC" );
1918+ // FIXME: Machine Copy Propagation currently optimizes away the EXEC copy to
1919+ // the scratch as we emit it only in the prolog. This optimization should
1920+ // not happen for frame related instructions. Until this is fixed ignore
1921+ // copy to scratch SGPR.
1922+ getVGPRSpillLaneOrTempRegister (MF, LiveUnits, Exec, RC,
1923+ /* IncludeScratchCopy=*/ false );
1924+ }
1925+
19001926 // hasFP only knows about stack objects that already exist. We're now
19011927 // determining the stack slots that will be created, so we have to predict
19021928 // them. Stack objects force FP usage with calls.
@@ -1906,8 +1932,7 @@ void SIFrameLowering::determinePrologEpilogSGPRSaves(
19061932 //
19071933 // FIXME: Is this really hasReservedCallFrame?
19081934 const bool WillHaveFP =
1909- FrameInfo.hasCalls () &&
1910- (SavedVGPRs.any () || !allStackObjectsAreDead (FrameInfo));
1935+ FrameInfo.hasCalls () && (SavedVGPRs.any () || !allStackObjectsAreDead (MF));
19111936
19121937 if (WillHaveFP || hasFP (MF)) {
19131938 Register FramePtrReg = MFI->getFrameOffsetReg ();
0 commit comments