@@ -206,22 +206,19 @@ void LoongArchFrameLowering::emitPrologue(MachineFunction &MF,
206
206
if (StackSize == 0 && !MFI.adjustsStack ())
207
207
return ;
208
208
209
- uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount (MF, true );
210
- uint64_t SecondSPAdjustAmount = RealStackSize - FirstSPAdjustAmount;
209
+ uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount (MF);
211
210
// Split the SP adjustment to reduce the offsets of callee saved spill.
212
211
if (FirstSPAdjustAmount)
213
212
StackSize = FirstSPAdjustAmount;
214
213
215
214
// Adjust stack.
216
215
adjustReg (MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
217
- if (FirstSPAdjustAmount != 2048 || SecondSPAdjustAmount == 0 ) {
218
- // Emit ".cfi_def_cfa_offset StackSize".
219
- unsigned CFIIndex =
220
- MF.addFrameInst (MCCFIInstruction::cfiDefCfaOffset (nullptr , StackSize));
221
- BuildMI (MBB, MBBI, DL, TII->get (TargetOpcode::CFI_INSTRUCTION))
222
- .addCFIIndex (CFIIndex)
223
- .setMIFlag (MachineInstr::FrameSetup);
224
- }
216
+ // Emit ".cfi_def_cfa_offset StackSize".
217
+ unsigned CFIIndex =
218
+ MF.addFrameInst (MCCFIInstruction::cfiDefCfaOffset (nullptr , StackSize));
219
+ BuildMI (MBB, MBBI, DL, TII->get (TargetOpcode::CFI_INSTRUCTION))
220
+ .addCFIIndex (CFIIndex)
221
+ .setMIFlag (MachineInstr::FrameSetup);
225
222
226
223
const auto &CSI = MFI.getCalleeSavedInfo ();
227
224
@@ -258,25 +255,14 @@ void LoongArchFrameLowering::emitPrologue(MachineFunction &MF,
258
255
}
259
256
260
257
// Emit the second SP adjustment after saving callee saved registers.
261
- if (FirstSPAdjustAmount && SecondSPAdjustAmount) {
262
- if (hasFP (MF)) {
263
- assert (SecondSPAdjustAmount > 0 &&
264
- " SecondSPAdjustAmount should be greater than zero" );
265
- adjustReg (MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
266
- MachineInstr::FrameSetup);
267
- } else {
268
- // FIXME: RegScavenger will place the spill instruction before the
269
- // prologue if a VReg is created in the prologue. This will pollute the
270
- // caller's stack data. Therefore, until there is better way, we just use
271
- // the `addi.w/d` instruction for stack adjustment to ensure that VReg
272
- // will not be created.
273
- for (int Val = SecondSPAdjustAmount; Val > 0 ; Val -= 2048 )
274
- BuildMI (MBB, MBBI, DL,
275
- TII->get (IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W), SPReg)
276
- .addReg (SPReg)
277
- .addImm (Val < 2048 ? -Val : -2048 )
278
- .setMIFlag (MachineInstr::FrameSetup);
258
+ if (FirstSPAdjustAmount) {
259
+ uint64_t SecondSPAdjustAmount = RealStackSize - FirstSPAdjustAmount;
260
+ assert (SecondSPAdjustAmount > 0 &&
261
+ " SecondSPAdjustAmount should be greater than zero" );
262
+ adjustReg (MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
263
+ MachineInstr::FrameSetup);
279
264
265
+ if (!hasFP (MF)) {
280
266
// If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0",
281
267
// don't emit an sp-based .cfi_def_cfa_offset
282
268
// Emit ".cfi_def_cfa_offset RealStackSize"
@@ -369,27 +355,20 @@ void LoongArchFrameLowering::emitEpilogue(MachineFunction &MF,
369
355
// st.d $ra, $sp, 2024
370
356
// st.d $fp, $sp, 2016
371
357
// addi.d $sp, $sp, -16
372
- uint64_t
373
- LoongArchFrameLowering::getFirstSPAdjustAmount (const MachineFunction &MF,
374
- bool IsPrologue) const {
358
+ uint64_t LoongArchFrameLowering::getFirstSPAdjustAmount (
359
+ const MachineFunction &MF) const {
375
360
const MachineFrameInfo &MFI = MF.getFrameInfo ();
376
361
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo ();
377
362
378
363
// Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
379
364
// 12-bit and there exists a callee-saved register needing to be pushed.
380
- if (!isInt<12 >(MFI.getStackSize ())) {
365
+ if (!isInt<12 >(MFI.getStackSize ()) && (CSI. size () > 0 ) ) {
381
366
// FirstSPAdjustAmount is chosen as (2048 - StackAlign) because 2048 will
382
367
// cause sp = sp + 2048 in the epilogue to be split into multiple
383
368
// instructions. Offsets smaller than 2048 can fit in a single load/store
384
369
// instruction, and we have to stick with the stack alignment.
385
370
// So (2048 - StackAlign) will satisfy the stack alignment.
386
- //
387
- // FIXME: This place may seem odd. When using multiple ADDI instructions to
388
- // adjust the stack in Prologue, and there are no callee-saved registers, we
389
- // can take advantage of the logic of split sp ajustment to reduce code
390
- // changes.
391
- return CSI.size () > 0 ? 2048 - getStackAlign ().value ()
392
- : (IsPrologue ? 2048 : 0 );
371
+ return 2048 - getStackAlign ().value ();
393
372
}
394
373
return 0 ;
395
374
}
0 commit comments