@@ -755,6 +755,19 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
755
755
}
756
756
}
757
757
758
+ void RISCVFrameLowering::deallocateStack (MachineFunction &MF,
759
+ MachineBasicBlock &MBB,
760
+ MachineBasicBlock::iterator MBBI,
761
+ const DebugLoc &DL, uint64_t StackSize,
762
+ int64_t CFAOffset) const {
763
+ const RISCVRegisterInfo *RI = STI.getRegisterInfo ();
764
+
765
+ Register SPReg = getSPReg (STI);
766
+
767
+ RI->adjustReg (MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed (StackSize),
768
+ MachineInstr::FrameDestroy, getStackAlign ());
769
+ }
770
+
758
771
void RISCVFrameLowering::emitEpilogue (MachineFunction &MF,
759
772
MachineBasicBlock &MBB) const {
760
773
const RISCVRegisterInfo *RI = STI.getRegisterInfo ();
@@ -786,20 +799,51 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
786
799
--MBBI;
787
800
}
788
801
789
- const auto &CSI = getUnmanagedCSI (MF, MFI.getCalleeSavedInfo () );
802
+ const auto &CSI = MFI.getCalleeSavedInfo ();
790
803
791
804
// Skip to before the restores of scalar callee-saved registers
792
805
// FIXME: assumes exactly one instruction is used to restore each
793
806
// callee-saved register.
794
- auto LastFrameDestroy = MBBI;
795
- if (!CSI.empty ())
796
- LastFrameDestroy = std::prev (MBBI, CSI.size ());
807
+ auto LastFrameDestroy = std::prev (MBBI, getUnmanagedCSI (MF, CSI).size ());
797
808
798
- uint64_t RealStackSize = getStackSizeWithRVVPadding (MF);
799
- uint64_t StackSize = RealStackSize - RVFI->getReservedSpillsSize ();
800
- uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize ();
809
+ uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount (MF);
810
+ uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
811
+ : getStackSizeWithRVVPadding (MF);
812
+ uint64_t StackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
813
+ : getStackSizeWithRVVPadding (MF) -
814
+ RVFI->getReservedSpillsSize ();
815
+ uint64_t FPOffset = FirstSPAdjustAmount ? FirstSPAdjustAmount
816
+ : getStackSizeWithRVVPadding (MF) -
817
+ RVFI->getVarArgsSaveSize ();
801
818
uint64_t RVVStackSize = RVFI->getRVVStackSize ();
802
819
820
+ bool RestoreFP = RI->hasStackRealignment (MF) || MFI.hasVarSizedObjects () ||
821
+ !hasReservedCallFrame (MF);
822
+
823
+ if (RVVStackSize) {
824
+ // If restoreFP the stack pointer will be restored using the frame pointer
825
+ // value.
826
+ if (!RestoreFP) {
827
+ adjustStackForRVV (MF, MBB, LastFrameDestroy, DL, RVVStackSize,
828
+ MachineInstr::FrameDestroy);
829
+ }
830
+ }
831
+
832
+ if (FirstSPAdjustAmount) {
833
+ uint64_t SecondSPAdjustAmount =
834
+ getStackSizeWithRVVPadding (MF) - FirstSPAdjustAmount;
835
+ assert (SecondSPAdjustAmount > 0 &&
836
+ " SecondSPAdjustAmount should be greater than zero" );
837
+
838
+ // If restoreFP the stack pointer will be restored using the frame pointer
839
+ // value.
840
+ if (!RestoreFP) {
841
+ RI->adjustReg (MBB, LastFrameDestroy, DL, SPReg, SPReg,
842
+ StackOffset::getFixed (SecondSPAdjustAmount),
843
+ MachineInstr::FrameDestroy, getStackAlign ());
844
+ }
845
+ }
846
+
803
847
// Restore the stack pointer using the value of the frame pointer. Only
804
848
// necessary if the stack pointer was modified, meaning the stack size is
805
849
// unknown.
@@ -810,50 +854,37 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
810
854
// normally it's just checking the variable sized object is present or not
811
855
// is enough, but we also don't preserve that at prologue/epilogue when
812
856
// have vector objects in stack.
813
- if (RI->hasStackRealignment (MF) || MFI.hasVarSizedObjects () ||
814
- !hasReservedCallFrame (MF)) {
857
+ if (RestoreFP) {
815
858
assert (hasFP (MF) && " frame pointer should not have been eliminated" );
816
- RI->adjustReg (MBB, LastFrameDestroy, DL, SPReg, FPReg,
817
- StackOffset::getFixed (-FPOffset),
818
- MachineInstr::FrameDestroy, getStackAlign ());
819
- } else {
820
- if (RVVStackSize)
821
- adjustStackForRVV (MF, MBB, LastFrameDestroy, DL, RVVStackSize,
822
- MachineInstr::FrameDestroy);
823
- }
824
-
825
- uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount (MF);
826
- if (FirstSPAdjustAmount) {
827
- uint64_t SecondSPAdjustAmount =
828
- getStackSizeWithRVVPadding (MF) - FirstSPAdjustAmount;
829
- assert (SecondSPAdjustAmount > 0 &&
830
- " SecondSPAdjustAmount should be greater than zero" );
831
859
832
- RI->adjustReg (MBB, LastFrameDestroy, DL, SPReg, SPReg ,
833
- StackOffset::getFixed (SecondSPAdjustAmount) ,
834
- MachineInstr::FrameDestroy, getStackAlign ());
860
+ RI->adjustReg (MBB, LastFrameDestroy, DL, SPReg, FPReg ,
861
+ StackOffset::getFixed (-FPOffset), MachineInstr::FrameDestroy ,
862
+ getStackAlign ());
835
863
}
836
864
837
- if (FirstSPAdjustAmount)
838
- StackSize = FirstSPAdjustAmount;
839
-
840
- if (RVFI->isPushable (MF) && MBBI != MBB.end () &&
841
- MBBI->getOpcode () == RISCV::CM_POP) {
865
+ bool ApplyPop = RVFI->isPushable (MF) && MBBI != MBB.end () &&
866
+ MBBI->getOpcode () == RISCV::CM_POP;
867
+ if (ApplyPop) {
842
868
// Use available stack adjustment in pop instruction to deallocate stack
843
869
// space. Align the stack size down to a multiple of 16. This is needed for
844
870
// RVE.
845
871
// FIXME: Can we increase the stack size to a multiple of 16 instead?
846
872
uint64_t Spimm = std::min (alignDown (StackSize, 16 ), (uint64_t )48 );
847
873
MBBI->getOperand (1 ).setImm (Spimm);
848
874
StackSize -= Spimm;
849
- }
850
875
851
- // Deallocate stack
852
- if (StackSize != 0 ) {
853
- RI->adjustReg (MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed (StackSize),
854
- MachineInstr::FrameDestroy, getStackAlign ());
876
+ if (StackSize != 0 )
877
+ deallocateStack (MF, MBB, MBBI, DL, StackSize,
878
+ /* stack_adj of cm.pop instr*/ RealStackSize - StackSize);
879
+
880
+ ++MBBI;
855
881
}
856
882
883
+ // Deallocate stack if StackSize isn't a zero and if we didn't already do it
884
+ // during cm.pop handling.
885
+ if (StackSize != 0 && !ApplyPop)
886
+ deallocateStack (MF, MBB, MBBI, DL, StackSize, 0 );
887
+
857
888
// Emit epilogue for shadow call stack.
858
889
emitSCSEpilogue (MF, MBB, MBBI, DL);
859
890
}
0 commit comments