@@ -384,9 +384,7 @@ static SVEPartitions partitionSVECS(MachineBasicBlock &MBB,
384384 MachineBasicBlock::iterator End =
385385 IsEpilogue ? MBB.begin () : MBB.getFirstTerminator ();
386386 auto AdjustI = [&](auto MBBI) { return IsEpilogue ? std::prev (MBBI) : MBBI; };
387-
388- // Process the SVE callee-saves to find the starts/ends of the ZPR and PPR
389- // areas.
387+ // Process the SVE CS to find the starts/ends of the ZPR and PPR areas.
390388 if (PPRCalleeSavesSize) {
391389 PPRsI = AdjustI (PPRsI);
392390 assert (isPartOfPPRCalleeSaves (*PPRsI) && " Unexpected instruction" );
@@ -400,7 +398,6 @@ static SVEPartitions partitionSVECS(MachineBasicBlock &MBB,
400398 while (ZPRsI != End && isPartOfZPRCalleeSaves (AdjustI (ZPRsI)))
401399 IsEpilogue ? (--ZPRsI) : (++ZPRsI);
402400 }
403-
404401 if (IsEpilogue)
405402 return {PPRsI, MBBI, ZPRsI, PPRsI};
406403 return {MBBI, PPRsI, PPRsI, ZPRsI};
@@ -766,82 +763,67 @@ void AArch64PrologueEmitter::emitPrologue() {
766763 if (AFL.windowsRequiresStackProbe (MF, NumBytes + RealignmentPadding))
767764 emitWindowsStackProbe (AfterGPRSavesI, DL, NumBytes, RealignmentPadding);
768765
766+ assert (!(AFL.canUseRedZone (MF) && NeedsRealignment) &&
767+ " Cannot use redzone with stack realignment" );
768+
769769 auto [PPRCalleeSavesSize, ZPRCalleeSavesSize, PPRLocalsSize, ZPRLocalsSize] =
770770 getSVEStackFrameSizes ();
771771
772+ StackOffset NonSVELocalsSize = StackOffset::getFixed (NumBytes);
773+ StackOffset SVECalleeSavesSize = ZPRCalleeSavesSize + PPRCalleeSavesSize;
772774 StackOffset CFAOffset =
773- StackOffset::getFixed (( int64_t ) MFI.getStackSize () - NumBytes) ;
775+ StackOffset::getFixed (MFI.getStackSize ()) - NonSVELocalsSize ;
774776
775- SVEPartitions SVECSPartitions;
776777 MachineBasicBlock::iterator AfterSVESavesI = AfterGPRSavesI;
778+ // Allocate space for the callee saves and PPR locals (if any).
777779 if (SVELayout != SVEStackLayout::CalleeSavesAboveFrameRecord) {
778- SVECSPartitions = partitionSVECS (MBB, AfterGPRSavesI, PPRCalleeSavesSize,
779- ZPRCalleeSavesSize, /* IsEpilogue=*/ false );
780+ SVEPartitions SVECSPartitions =
781+ partitionSVECS (MBB, AfterGPRSavesI, PPRCalleeSavesSize,
782+ ZPRCalleeSavesSize, /* IsEpilogue=*/ false );
780783 AfterSVESavesI = SVECSPartitions.ZPREnd ;
781- }
782-
783- if (EmitAsyncCFI)
784- emitCalleeSavedSVELocations (AfterSVESavesI);
785-
786- if (SVELayout == SVEStackLayout::Split) {
787- assert (!AFL.canUseRedZone (MF) &&
788- " Cannot use redzone with aarch64-split-sve-objects" );
789- // TODO: Handle HasWinCFI/NeedsWinCFI?
790- assert (!NeedsWinCFI &&
791- " WinCFI with aarch64-split-sve-objects is not supported" );
792-
793- // Split ZPR and PPR allocation.
794- // Allocate PPR callee saves
795- allocateStackSpace (SVECSPartitions.PPRBegin , 0 , PPRCalleeSavesSize,
784+ if (EmitAsyncCFI)
785+ emitCalleeSavedSVELocations (AfterSVESavesI);
786+
787+ StackOffset AllocateBeforePPRs = SVECalleeSavesSize;
788+ StackOffset AllocateAfterPPRs = PPRLocalsSize;
789+ if (SVELayout == SVEStackLayout::Split) {
790+ AllocateBeforePPRs = PPRCalleeSavesSize;
791+ AllocateAfterPPRs = PPRLocalsSize + ZPRCalleeSavesSize;
792+ }
793+ allocateStackSpace (SVECSPartitions.PPRBegin , 0 , AllocateBeforePPRs,
796794 EmitAsyncCFI && !HasFP, CFAOffset,
797- MFI.hasVarSizedObjects () || ZPRCalleeSavesSize ||
798- ZPRLocalsSize || PPRLocalsSize);
799- CFAOffset += PPRCalleeSavesSize;
800-
801- // Allocate PPR locals + ZPR callee saves
795+ MFI.hasVarSizedObjects () || AllocateAfterPPRs ||
796+ ZPRLocalsSize || NonSVELocalsSize);
797+ CFAOffset += AllocateBeforePPRs;
802798 assert (SVECSPartitions.PPREnd == SVECSPartitions.ZPRBegin &&
803799 " Expected ZPR callee saves after PPR locals" );
804800 allocateStackSpace (SVECSPartitions.PPREnd , RealignmentPadding,
805- PPRLocalsSize + ZPRCalleeSavesSize,
806- EmitAsyncCFI && !HasFP, CFAOffset,
807- MFI.hasVarSizedObjects () || ZPRLocalsSize);
808- CFAOffset += PPRLocalsSize + ZPRCalleeSavesSize;
809-
810- // Allocate ZPR locals
811- allocateStackSpace (SVECSPartitions.ZPREnd , RealignmentPadding,
812- ZPRLocalsSize + StackOffset::getFixed (NumBytes),
813- EmitAsyncCFI && !HasFP, CFAOffset,
814- MFI.hasVarSizedObjects ());
801+ AllocateAfterPPRs, EmitAsyncCFI && !HasFP, CFAOffset,
802+ MFI.hasVarSizedObjects () || ZPRLocalsSize ||
803+ NonSVELocalsSize);
804+ CFAOffset += AllocateAfterPPRs;
815805 } else {
816- StackOffset SVECalleeSavesSize = ZPRCalleeSavesSize + PPRCalleeSavesSize;
817- // Allocate space for the callee saves (if any).
818- StackOffset LocalsSize =
819- PPRLocalsSize + ZPRLocalsSize + StackOffset::getFixed (NumBytes);
820- if (SVELayout != SVEStackLayout::CalleeSavesAboveFrameRecord)
821- allocateStackSpace (AfterGPRSavesI, 0 , SVECalleeSavesSize,
822- EmitAsyncCFI && !HasFP, CFAOffset,
823- MFI.hasVarSizedObjects () || LocalsSize);
806+ // Note: With CalleeSavesAboveFrameRecord the SVE CS have already been
807+ // allocated (and separate PPRLocals are not supported).
808+ assert (!PPRLocalsSize && " Unexpected PPR locals!" );
824809 CFAOffset += SVECalleeSavesSize;
810+ }
825811
826- // Allocate space for the rest of the frame including SVE locals. Align the
827- // stack as necessary.
828- assert (!(AFL.canUseRedZone (MF) && NeedsRealignment) &&
829- " Cannot use redzone with stack realignment" );
830- if (!AFL.canUseRedZone (MF)) {
831- // FIXME: in the case of dynamic re-alignment, NumBytes doesn't have
832- // the correct value here, as NumBytes also includes padding bytes,
833- // which shouldn't be counted here.
834- StackOffset SVELocalsSize = PPRLocalsSize + ZPRLocalsSize;
835- allocateStackSpace (AfterSVESavesI, RealignmentPadding,
836- SVELocalsSize + StackOffset::getFixed (NumBytes),
837- EmitAsyncCFI && !HasFP, CFAOffset,
838- MFI.hasVarSizedObjects ());
839- }
812+ // Allocate space for the rest of the frame including ZPR locals. Align the
813+ // stack as necessary.
814+ if (!AFL.canUseRedZone (MF)) {
815+ // FIXME: in the case of dynamic re-alignment, NumBytes doesn't have the
816+ // correct value here, as NumBytes also includes padding bytes, which
817+ // shouldn't be counted here.
818+ allocateStackSpace (AfterSVESavesI, RealignmentPadding,
819+ ZPRLocalsSize + NonSVELocalsSize, EmitAsyncCFI && !HasFP,
820+ CFAOffset, MFI.hasVarSizedObjects ());
840821 }
841822
842823 // If we need a base pointer, set it up here. It's whatever the value of the
843- // stack pointer is at this point. Any variable size objects will be allocated
844- // after this, so we can still use the base pointer to reference locals.
824+ // stack pointer is at this point. Any variable size objects will be
825+ // allocated after this, so we can still use the base pointer to reference
826+ // locals.
845827 //
846828 // FIXME: Clarify FrameSetup flags here.
847829 // Note: Use emitFrameOffset() like above for FP if the FrameSetup flag is
@@ -1459,12 +1441,12 @@ void AArch64EpilogueEmitter::emitEpilogue() {
14591441
14601442 auto [PPRCalleeSavesSize, ZPRCalleeSavesSize, PPRLocalsSize, ZPRLocalsSize] =
14611443 getSVEStackFrameSizes ();
1462- StackOffset SVECalleeSavedSize = ZPRCalleeSavesSize + PPRCalleeSavesSize;
1463- StackOffset SVEStackSize = SVECalleeSavedSize + PPRLocalsSize + ZPRLocalsSize;
1444+ StackOffset SVECalleeSavesSize = ZPRCalleeSavesSize + PPRCalleeSavesSize;
1445+ StackOffset SVEStackSize = SVECalleeSavesSize + PPRLocalsSize + ZPRLocalsSize;
14641446
14651447 auto SVECSPartitions = partitionSVECS (
14661448 MBB,
1467- SVECalleeSavedSize &&
1449+ SVECalleeSavesSize &&
14681450 SVELayout == SVEStackLayout::CalleeSavesAboveFrameRecord
14691451 ? MBB.getFirstTerminator ()
14701452 : FirstGPRRestoreI,
@@ -1497,7 +1479,7 @@ void AArch64EpilogueEmitter::emitEpilogue() {
14971479
14981480 // Deallocate callee-save SVE registers.
14991481 emitFrameOffset (MBB, RestoreEnd, DL, AArch64::SP, AArch64::SP,
1500- SVECalleeSavedSize , TII, MachineInstr::FrameDestroy, false ,
1482+ SVECalleeSavesSize , TII, MachineInstr::FrameDestroy, false ,
15011483 NeedsWinCFI, &HasWinCFI);
15021484 } else if (AFI->hasSVEStackSize ()) {
15031485 auto [PPRRestoreBegin, PPRRestoreEnd, ZPRRestoreBegin, ZPRRestoreEnd] =
@@ -1508,7 +1490,7 @@ void AArch64EpilogueEmitter::emitEpilogue() {
15081490 Register BaseForSVEDealloc =
15091491 (AFI->isStackRealigned () || MFI.hasVarSizedObjects ()) ? AArch64::FP
15101492 : AArch64::SP;
1511- if (SVECalleeSavedSize && BaseForSVEDealloc == AArch64::FP) {
1493+ if (SVECalleeSavesSize && BaseForSVEDealloc == AArch64::FP) {
15121494 // TODO: Support stack realigment and variable-sized objects.
15131495 assert (
15141496 !AFI->hasSplitSVEObjects () &&
@@ -1530,12 +1512,12 @@ void AArch64EpilogueEmitter::emitEpilogue() {
15301512 // The code below will deallocate the stack space space by moving the SP
15311513 // to the start of the SVE callee-save area.
15321514 emitFrameOffset (MBB, RestoreBegin, DL, AArch64::SP, CalleeSaveBase,
1533- -SVECalleeSavedSize , TII, MachineInstr::FrameDestroy);
1515+ -SVECalleeSavesSize , TII, MachineInstr::FrameDestroy);
15341516 } else if (BaseForSVEDealloc == AArch64::SP) {
15351517 auto CFAOffset =
15361518 SVEStackSize + StackOffset::getFixed (NumBytes + PrologueSaveSize);
15371519
1538- if (SVECalleeSavedSize ) {
1520+ if (SVECalleeSavesSize ) {
15391521 // Deallocate the non-SVE locals first before we can deallocate (and
15401522 // restore callee saves) from the SVE area.
15411523 auto NonSVELocals = StackOffset::getFixed (NumBytes);
@@ -1553,7 +1535,7 @@ void AArch64EpilogueEmitter::emitEpilogue() {
15531535 CFAOffset -= ZPRLocalsSize;
15541536 }
15551537
1556- StackOffset SVECalleeSavesToDealloc = SVECalleeSavedSize ;
1538+ StackOffset SVECalleeSavesToDealloc = SVECalleeSavesSize ;
15571539 if (SVELayout == SVEStackLayout::Split &&
15581540 (PPRLocalsSize || ZPRCalleeSavesSize)) {
15591541 assert (PPRRestoreBegin == ZPRRestoreEnd &&
0 commit comments