@@ -95,6 +95,11 @@ static const std::pair<MCPhysReg, int8_t> FixedCSRFIQCIInterruptMap[] = {
9595 /* -21, -22, -23, -24 are reserved */
9696};
9797
98+ // / Returns true if DWARF CFI instructions ("frame moves") should be emitted.
99+ static bool needsDwarfCFI (const MachineFunction &MF) {
100+ return MF.needsFrameMoves ();
101+ }
102+
98103// For now we use x3, a.k.a gp, as pointer to shadow call stack.
99104// User should not use x3 in their asm.
100105static void emitSCSPrologue (MachineFunction &MF, MachineBasicBlock &MBB,
@@ -141,6 +146,9 @@ static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
141146 .addImm (-SlotSize)
142147 .setMIFlag (MachineInstr::FrameSetup);
143148
149+ if (!needsDwarfCFI (MF))
150+ return ;
151+
144152 // Emit a CFI instruction that causes SlotSize to be subtracted from the value
145153 // of the shadow stack pointer when unwinding past this frame.
146154 char DwarfSCSReg = TRI->getDwarfRegNum (SCSPReg, /* IsEH*/ true );
@@ -199,8 +207,10 @@ static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
199207 .addReg (SCSPReg)
200208 .addImm (-SlotSize)
201209 .setMIFlag (MachineInstr::FrameDestroy);
202- // Restore the SCS pointer
203- CFIInstBuilder (MBB, MI, MachineInstr::FrameDestroy).buildRestore (SCSPReg);
210+ if (needsDwarfCFI (MF)) {
211+ // Restore the SCS pointer
212+ CFIInstBuilder (MBB, MI, MachineInstr::FrameDestroy).buildRestore (SCSPReg);
213+ }
204214}
205215
206216// Insert instruction to swap mscratchsw with sp
@@ -935,6 +945,7 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
935945 MBBI = std::prev (MBBI, getRVVCalleeSavedInfo (MF, CSI).size () +
936946 getUnmanagedCSI (MF, CSI).size ());
937947 CFIInstBuilder CFIBuilder (MBB, MBBI, MachineInstr::FrameSetup);
948+ bool NeedsDwarfCFI = needsDwarfCFI (MF);
938949
939950 // If libcalls are used to spill and restore callee-saved registers, the frame
940951 // has two sections; the opaque section managed by the libcalls, and the
@@ -962,10 +973,12 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
962973 alignTo ((STI.getXLen () / 8 ) * LibCallRegs, getStackAlign ());
963974 RVFI->setLibCallStackSize (LibCallFrameSize);
964975
965- CFIBuilder.buildDefCFAOffset (LibCallFrameSize);
966- for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo (MF, CSI))
967- CFIBuilder.buildOffset (CS.getReg (),
968- MFI.getObjectOffset (CS.getFrameIdx ()));
976+ if (NeedsDwarfCFI) {
977+ CFIBuilder.buildDefCFAOffset (LibCallFrameSize);
978+ for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo (MF, CSI))
979+ CFIBuilder.buildOffset (CS.getReg (),
980+ MFI.getObjectOffset (CS.getFrameIdx ()));
981+ }
969982 }
970983
971984 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
@@ -996,14 +1009,17 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
9961009 // could only be the next instruction.
9971010 ++PossiblePush;
9981011
999- // Insert the CFI metadata before where we think the `(QC.)CM.PUSH(FP)`
1000- // could be. The PUSH will also get its own CFI metadata for its own
1001- // modifications, which should come after the PUSH.
1002- CFIInstBuilder PushCFIBuilder (MBB, PossiblePush, MachineInstr::FrameSetup);
1003- PushCFIBuilder.buildDefCFAOffset (QCIInterruptPushAmount);
1004- for (const CalleeSavedInfo &CS : getQCISavedInfo (MF, CSI))
1005- PushCFIBuilder.buildOffset (CS.getReg (),
1006- MFI.getObjectOffset (CS.getFrameIdx ()));
1012+ if (NeedsDwarfCFI) {
1013+ // Insert the CFI metadata before where we think the `(QC.)CM.PUSH(FP)`
1014+ // could be. The PUSH will also get its own CFI metadata for its own
1015+ // modifications, which should come after the PUSH.
1016+ CFIInstBuilder PushCFIBuilder (MBB, PossiblePush,
1017+ MachineInstr::FrameSetup);
1018+ PushCFIBuilder.buildDefCFAOffset (QCIInterruptPushAmount);
1019+ for (const CalleeSavedInfo &CS : getQCISavedInfo (MF, CSI))
1020+ PushCFIBuilder.buildOffset (CS.getReg (),
1021+ MFI.getObjectOffset (CS.getFrameIdx ()));
1022+ }
10071023 }
10081024
10091025 if (RVFI->isPushable (MF) && PossiblePush != MBB.end () &&
@@ -1017,10 +1033,12 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
10171033 PossiblePush->getOperand (1 ).setImm (StackAdj);
10181034 StackSize -= StackAdj;
10191035
1020- CFIBuilder.buildDefCFAOffset (RealStackSize - StackSize);
1021- for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo (MF, CSI))
1022- CFIBuilder.buildOffset (CS.getReg (),
1023- MFI.getObjectOffset (CS.getFrameIdx ()));
1036+ if (NeedsDwarfCFI) {
1037+ CFIBuilder.buildDefCFAOffset (RealStackSize - StackSize);
1038+ for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo (MF, CSI))
1039+ CFIBuilder.buildOffset (CS.getReg (),
1040+ MFI.getObjectOffset (CS.getFrameIdx ()));
1041+ }
10241042 }
10251043
10261044 // Allocate space on the stack if necessary.
@@ -1031,7 +1049,7 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
10311049 bool DynAllocation =
10321050 MF.getInfo <RISCVMachineFunctionInfo>()->hasDynamicAllocation ();
10331051 if (StackSize != 0 )
1034- allocateStack (MBB, MBBI, MF, StackSize, RealStackSize, /* EmitCFI= */ true ,
1052+ allocateStack (MBB, MBBI, MF, StackSize, RealStackSize, NeedsDwarfCFI ,
10351053 NeedProbe, ProbeSize, DynAllocation,
10361054 MachineInstr::FrameSetup);
10371055
@@ -1049,8 +1067,10 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
10491067
10501068 // Iterate over list of callee-saved registers and emit .cfi_offset
10511069 // directives.
1052- for (const CalleeSavedInfo &CS : getUnmanagedCSI (MF, CSI))
1053- CFIBuilder.buildOffset (CS.getReg (), MFI.getObjectOffset (CS.getFrameIdx ()));
1070+ if (NeedsDwarfCFI)
1071+ for (const CalleeSavedInfo &CS : getUnmanagedCSI (MF, CSI))
1072+ CFIBuilder.buildOffset (CS.getReg (),
1073+ MFI.getObjectOffset (CS.getFrameIdx ()));
10541074
10551075 // Generate new FP.
10561076 if (hasFP (MF)) {
@@ -1069,7 +1089,8 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
10691089 MachineInstr::FrameSetup, getStackAlign ());
10701090 }
10711091
1072- CFIBuilder.buildDefCFA (FPReg, RVFI->getVarArgsSaveSize ());
1092+ if (NeedsDwarfCFI)
1093+ CFIBuilder.buildDefCFA (FPReg, RVFI->getVarArgsSaveSize ());
10731094 }
10741095
10751096 uint64_t SecondSPAdjustAmount = 0 ;
@@ -1080,15 +1101,16 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
10801101 " SecondSPAdjustAmount should be greater than zero" );
10811102
10821103 allocateStack (MBB, MBBI, MF, SecondSPAdjustAmount,
1083- getStackSizeWithRVVPadding (MF), !hasFP (MF), NeedProbe,
1084- ProbeSize, DynAllocation, MachineInstr::FrameSetup);
1104+ getStackSizeWithRVVPadding (MF), NeedsDwarfCFI && !hasFP (MF),
1105+ NeedProbe, ProbeSize, DynAllocation,
1106+ MachineInstr::FrameSetup);
10851107 }
10861108
10871109 if (RVVStackSize) {
10881110 if (NeedProbe) {
10891111 allocateAndProbeStackForRVV (MF, MBB, MBBI, DL, RVVStackSize,
1090- MachineInstr::FrameSetup, ! hasFP (MF),
1091- DynAllocation);
1112+ MachineInstr::FrameSetup,
1113+ NeedsDwarfCFI && ! hasFP (MF), DynAllocation);
10921114 } else {
10931115 // We must keep the stack pointer aligned through any intermediate
10941116 // updates.
@@ -1097,14 +1119,15 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
10971119 MachineInstr::FrameSetup, getStackAlign ());
10981120 }
10991121
1100- if (!hasFP (MF)) {
1122+ if (NeedsDwarfCFI && !hasFP (MF)) {
11011123 // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
11021124 CFIBuilder.insertCFIInst (createDefCFAExpression (
11031125 *RI, SPReg, getStackSizeWithRVVPadding (MF), RVVStackSize / 8 ));
11041126 }
11051127
11061128 std::advance (MBBI, getRVVCalleeSavedInfo (MF, CSI).size ());
1107- emitCalleeSavedRVVPrologCFI (MBB, MBBI, hasFP (MF));
1129+ if (NeedsDwarfCFI)
1130+ emitCalleeSavedRVVPrologCFI (MBB, MBBI, hasFP (MF));
11081131 }
11091132
11101133 if (hasFP (MF)) {
@@ -1171,8 +1194,9 @@ void RISCVFrameLowering::deallocateStack(MachineFunction &MF,
11711194 MachineInstr::FrameDestroy, getStackAlign ());
11721195 StackSize = 0 ;
11731196
1174- CFIInstBuilder (MBB, MBBI, MachineInstr::FrameDestroy)
1175- .buildDefCFAOffset (CFAOffset);
1197+ if (needsDwarfCFI (MF))
1198+ CFIInstBuilder (MBB, MBBI, MachineInstr::FrameDestroy)
1199+ .buildDefCFAOffset (CFAOffset);
11761200}
11771201
11781202void RISCVFrameLowering::emitEpilogue (MachineFunction &MF,
@@ -1212,6 +1236,7 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
12121236 std::next (MBBI, getRVVCalleeSavedInfo (MF, CSI).size ());
12131237 CFIInstBuilder CFIBuilder (MBB, FirstScalarCSRRestoreInsn,
12141238 MachineInstr::FrameDestroy);
1239+ bool NeedsDwarfCFI = needsDwarfCFI (MF);
12151240
12161241 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount (MF);
12171242 uint64_t RealStackSize = FirstSPAdjustAmount ? FirstSPAdjustAmount
@@ -1232,10 +1257,11 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
12321257 StackOffset::getScalable (RVVStackSize),
12331258 MachineInstr::FrameDestroy, getStackAlign ());
12341259
1235- if (!hasFP (MF))
1236- CFIBuilder.buildDefCFA (SPReg, RealStackSize);
1237-
1238- emitCalleeSavedRVVEpilogCFI (MBB, FirstScalarCSRRestoreInsn);
1260+ if (NeedsDwarfCFI) {
1261+ if (!hasFP (MF))
1262+ CFIBuilder.buildDefCFA (SPReg, RealStackSize);
1263+ emitCalleeSavedRVVEpilogCFI (MBB, FirstScalarCSRRestoreInsn);
1264+ }
12391265 }
12401266
12411267 if (FirstSPAdjustAmount) {
@@ -1251,7 +1277,7 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
12511277 StackOffset::getFixed (SecondSPAdjustAmount),
12521278 MachineInstr::FrameDestroy, getStackAlign ());
12531279
1254- if (!hasFP (MF))
1280+ if (NeedsDwarfCFI && !hasFP (MF))
12551281 CFIBuilder.buildDefCFAOffset (FirstSPAdjustAmount);
12561282 }
12571283
@@ -1272,7 +1298,7 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
12721298 getStackAlign ());
12731299 }
12741300
1275- if (hasFP (MF))
1301+ if (NeedsDwarfCFI && hasFP (MF))
12761302 CFIBuilder.buildDefCFA (SPReg, RealStackSize);
12771303
12781304 // Skip to after the restores of scalar callee-saved registers
@@ -1295,8 +1321,9 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
12951321 }
12961322
12971323 // Recover callee-saved registers.
1298- for (const CalleeSavedInfo &CS : getUnmanagedCSI (MF, CSI))
1299- CFIBuilder.buildRestore (CS.getReg ());
1324+ if (NeedsDwarfCFI)
1325+ for (const CalleeSavedInfo &CS : getUnmanagedCSI (MF, CSI))
1326+ CFIBuilder.buildRestore (CS.getReg ());
13001327
13011328 if (RVFI->isPushable (MF) && MBBI != MBB.end () && isPop (MBBI->getOpcode ())) {
13021329 // Use available stack adjustment in pop instruction to deallocate stack
@@ -1315,15 +1342,17 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
13151342 auto NextI = next_nodbg (MBBI, MBB.end ());
13161343 if (NextI == MBB.end () || NextI->getOpcode () != RISCV::PseudoRET) {
13171344 ++MBBI;
1318- CFIBuilder.setInsertPoint (MBBI);
1345+ if (NeedsDwarfCFI) {
1346+ CFIBuilder.setInsertPoint (MBBI);
13191347
1320- for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo (MF, CSI))
1321- CFIBuilder.buildRestore (CS.getReg ());
1348+ for (const CalleeSavedInfo &CS : getPushOrLibCallsSavedInfo (MF, CSI))
1349+ CFIBuilder.buildRestore (CS.getReg ());
13221350
1323- // Update CFA Offset. If this is a QCI interrupt function, there will be a
1324- // leftover offset which is deallocated by `QC.C.MILEAVERET`, otherwise
1325- // getQCIInterruptStackSize() will be 0.
1326- CFIBuilder.buildDefCFAOffset (RVFI->getQCIInterruptStackSize ());
1351+ // Update CFA Offset. If this is a QCI interrupt function, there will
1352+ // be a leftover offset which is deallocated by `QC.C.MILEAVERET`,
1353+ // otherwise getQCIInterruptStackSize() will be 0.
1354+ CFIBuilder.buildDefCFAOffset (RVFI->getQCIInterruptStackSize ());
1355+ }
13271356 }
13281357 }
13291358
@@ -1812,7 +1841,8 @@ MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
18121841 // allocateStack.
18131842 bool DynAllocation =
18141843 MF.getInfo <RISCVMachineFunctionInfo>()->hasDynamicAllocation ();
1815- allocateStack (MBB, MI, MF, -Amount, -Amount, !hasFP (MF),
1844+ allocateStack (MBB, MI, MF, -Amount, -Amount,
1845+ needsDwarfCFI (MF) && !hasFP (MF),
18161846 /* NeedProbe=*/ true , ProbeSize, DynAllocation,
18171847 MachineInstr::NoFlags);
18181848 } else {
0 commit comments