Skip to content

Commit d733fa1

Browse files
authored
[RISCV] Consolidate VLS codepaths in stack frame manipulation [nfc] (#117605)
We can move the logic from adjustStackForRVV into adjustReg, which results in the remaining logic being trivially inlined to the two callers and allows a duplicate copy of the same logic in eliminateFrameIndex to be pruned.
1 parent c94d715 commit d733fa1

File tree

3 files changed

+26
-50
lines changed

3 files changed

+26
-50
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -498,36 +498,6 @@ getPushOrLibCallsSavedInfo(const MachineFunction &MF,
498498
return PushOrLibCallsCSI;
499499
}
500500

501-
void RISCVFrameLowering::adjustStackForRVV(MachineFunction &MF,
502-
MachineBasicBlock &MBB,
503-
MachineBasicBlock::iterator MBBI,
504-
const DebugLoc &DL, int64_t Amount,
505-
MachineInstr::MIFlag Flag) const {
506-
assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
507-
508-
// Optimize compile time offset case
509-
StackOffset Offset = StackOffset::getScalable(Amount);
510-
if (auto VLEN = STI.getRealVLen()) {
511-
// 1. Multiply the number of v-slots by the (constant) length of register
512-
const int64_t VLENB = *VLEN / 8;
513-
assert(Amount % 8 == 0 &&
514-
"Reserve the stack by the multiple of one vector size.");
515-
const int64_t NumOfVReg = Amount / 8;
516-
const int64_t FixedOffset = NumOfVReg * VLENB;
517-
if (!isInt<32>(FixedOffset)) {
518-
report_fatal_error(
519-
"Frame size outside of the signed 32-bit range not supported");
520-
}
521-
Offset = StackOffset::getFixed(FixedOffset);
522-
}
523-
524-
const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
525-
// We must keep the stack pointer aligned through any intermediate
526-
// updates.
527-
RI.adjustReg(MBB, MBBI, DL, SPReg, SPReg, Offset,
528-
Flag, getStackAlign());
529-
}
530-
531501
static void appendScalableVectorExpression(const TargetRegisterInfo &TRI,
532502
SmallVectorImpl<char> &Expr,
533503
int FixedOffset, int ScalableOffset,
@@ -799,8 +769,12 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
799769
}
800770

801771
if (RVVStackSize) {
802-
adjustStackForRVV(MF, MBB, MBBI, DL, -RVVStackSize,
803-
MachineInstr::FrameSetup);
772+
// We must keep the stack pointer aligned through any intermediate
773+
// updates.
774+
RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
775+
StackOffset::getScalable(-RVVStackSize),
776+
MachineInstr::FrameSetup, getStackAlign());
777+
804778
if (!hasFP(MF)) {
805779
// Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
806780
unsigned CFIIndex = MF.addFrameInst(createDefCFAExpression(
@@ -925,8 +899,9 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
925899
// If RestoreSPFromFP the stack pointer will be restored using the frame
926900
// pointer value.
927901
if (!RestoreSPFromFP)
928-
adjustStackForRVV(MF, MBB, LastFrameDestroy, DL, RVVStackSize,
929-
MachineInstr::FrameDestroy);
902+
RI->adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg,
903+
StackOffset::getScalable(RVVStackSize),
904+
MachineInstr::FrameDestroy, getStackAlign());
930905

931906
if (!hasFP(MF)) {
932907
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(

llvm/lib/Target/RISCV/RISCVFrameLowering.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ class RISCVFrameLowering : public TargetFrameLowering {
8888

8989
private:
9090
void determineFrameLayout(MachineFunction &MF) const;
91-
void adjustStackForRVV(MachineFunction &MF, MachineBasicBlock &MBB,
92-
MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
93-
int64_t Amount, MachineInstr::MIFlag Flag) const;
9491
void emitCalleeSavedRVVPrologCFI(MachineBasicBlock &MBB,
9592
MachineBasicBlock::iterator MI,
9693
bool HasFP) const;

llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ void RISCVRegisterInfo::adjustReg(MachineBasicBlock &MBB,
184184
const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
185185
const RISCVInstrInfo *TII = ST.getInstrInfo();
186186

187+
// Optimize compile time offset case
188+
if (Offset.getScalable()) {
189+
if (auto VLEN = ST.getRealVLen()) {
190+
// 1. Multiply the number of v-slots by the (constant) length of register
191+
const int64_t VLENB = *VLEN / 8;
192+
assert(Offset.getScalable() % (RISCV::RVVBitsPerBlock / 8) == 0 &&
193+
"Reserve the stack by the multiple of one vector size.");
194+
const int64_t NumOfVReg = Offset.getScalable() / 8;
195+
const int64_t FixedOffset = NumOfVReg * VLENB;
196+
if (!isInt<32>(FixedOffset)) {
197+
report_fatal_error(
198+
"Frame size outside of the signed 32-bit range not supported");
199+
}
200+
Offset = StackOffset::getFixed(FixedOffset + Offset.getFixed());
201+
}
202+
}
203+
187204
bool KillSrcReg = false;
188205

189206
if (Offset.getScalable()) {
@@ -467,19 +484,6 @@ bool RISCVRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
467484
if (!IsRVVSpill)
468485
Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
469486

470-
if (Offset.getScalable() &&
471-
ST.getRealMinVLen() == ST.getRealMaxVLen()) {
472-
// For an exact VLEN value, scalable offsets become constant and thus
473-
// can be converted entirely into fixed offsets.
474-
int64_t FixedValue = Offset.getFixed();
475-
int64_t ScalableValue = Offset.getScalable();
476-
assert(ScalableValue % 8 == 0 &&
477-
"Scalable offset is not a multiple of a single vector size.");
478-
int64_t NumOfVReg = ScalableValue / 8;
479-
int64_t VLENB = ST.getRealMinVLen() / 8;
480-
Offset = StackOffset::getFixed(FixedValue + NumOfVReg * VLENB);
481-
}
482-
483487
if (!isInt<32>(Offset.getFixed())) {
484488
report_fatal_error(
485489
"Frame offsets outside of the signed 32-bit range not supported");

0 commit comments

Comments
 (0)