1 //===-- RISCVFrameLowering.cpp - RISC-V Frame Information -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the RISC-V implementation of TargetFrameLowering class.
11 //===----------------------------------------------------------------------===//
13 #include "RISCVFrameLowering.h"
14 #include "RISCVMachineFunctionInfo.h"
15 #include "RISCVSubtarget.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/RegisterScavenging.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/Support/LEB128.h"
30 static Align
getABIStackAlignment(RISCVABI::ABI ABI
) {
31 if (ABI
== RISCVABI::ABI_ILP32E
)
33 if (ABI
== RISCVABI::ABI_LP64E
)
38 RISCVFrameLowering::RISCVFrameLowering(const RISCVSubtarget
&STI
)
39 : TargetFrameLowering(
40 StackGrowsDown
, getABIStackAlignment(STI
.getTargetABI()),
41 /*LocalAreaOffset=*/0,
42 /*TransientStackAlignment=*/getABIStackAlignment(STI
.getTargetABI())),
45 static const MCPhysReg AllPopRegs
[] = {
46 RISCV::X1
, RISCV::X8
, RISCV::X9
, RISCV::X18
, RISCV::X19
,
47 RISCV::X20
, RISCV::X21
, RISCV::X22
, RISCV::X23
, RISCV::X24
,
48 RISCV::X25
, RISCV::X26
, RISCV::X27
};
50 // For now we use x3, a.k.a gp, as pointer to shadow call stack.
51 // User should not use x3 in their asm.
52 static void emitSCSPrologue(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
53 MachineBasicBlock::iterator MI
,
55 if (!MF
.getFunction().hasFnAttribute(Attribute::ShadowCallStack
))
58 const auto &STI
= MF
.getSubtarget
<RISCVSubtarget
>();
59 const llvm::RISCVRegisterInfo
*TRI
= STI
.getRegisterInfo();
60 Register RAReg
= TRI
->getRARegister();
62 // Do not save RA to the SCS if it's not saved to the regular stack,
63 // i.e. RA is not at risk of being overwritten.
64 std::vector
<CalleeSavedInfo
> &CSI
= MF
.getFrameInfo().getCalleeSavedInfo();
66 CSI
, [&](CalleeSavedInfo
&CSR
) { return CSR
.getReg() == RAReg
; }))
69 const RISCVInstrInfo
*TII
= STI
.getInstrInfo();
70 if (!STI
.hasForcedSWShadowStack() && STI
.hasStdExtZicfiss()) {
71 BuildMI(MBB
, MI
, DL
, TII
->get(RISCV::SSPUSH
)).addReg(RAReg
);
75 Register SCSPReg
= RISCVABI::getSCSPReg();
77 bool IsRV64
= STI
.hasFeature(RISCV::Feature64Bit
);
78 int64_t SlotSize
= STI
.getXLen() / 8;
79 // Store return address to shadow call stack
81 // s[w|d] ra, -[4|8](gp)
82 BuildMI(MBB
, MI
, DL
, TII
->get(RISCV::ADDI
))
83 .addReg(SCSPReg
, RegState::Define
)
86 .setMIFlag(MachineInstr::FrameSetup
);
87 BuildMI(MBB
, MI
, DL
, TII
->get(IsRV64
? RISCV::SD
: RISCV::SW
))
91 .setMIFlag(MachineInstr::FrameSetup
);
93 // Emit a CFI instruction that causes SlotSize to be subtracted from the value
94 // of the shadow stack pointer when unwinding past this frame.
95 char DwarfSCSReg
= TRI
->getDwarfRegNum(SCSPReg
, /*IsEH*/ true);
96 assert(DwarfSCSReg
< 32 && "SCS Register should be < 32 (X3).");
98 char Offset
= static_cast<char>(-SlotSize
) & 0x7f;
99 const char CFIInst
[] = {
100 dwarf::DW_CFA_val_expression
,
101 DwarfSCSReg
, // register
103 static_cast<char>(unsigned(dwarf::DW_OP_breg0
+ DwarfSCSReg
)),
104 Offset
, // addend (sleb128)
107 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createEscape(
108 nullptr, StringRef(CFIInst
, sizeof(CFIInst
))));
109 BuildMI(MBB
, MI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
110 .addCFIIndex(CFIIndex
)
111 .setMIFlag(MachineInstr::FrameSetup
);
114 static void emitSCSEpilogue(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
115 MachineBasicBlock::iterator MI
,
116 const DebugLoc
&DL
) {
117 if (!MF
.getFunction().hasFnAttribute(Attribute::ShadowCallStack
))
120 const auto &STI
= MF
.getSubtarget
<RISCVSubtarget
>();
121 Register RAReg
= STI
.getRegisterInfo()->getRARegister();
123 // See emitSCSPrologue() above.
124 std::vector
<CalleeSavedInfo
> &CSI
= MF
.getFrameInfo().getCalleeSavedInfo();
126 CSI
, [&](CalleeSavedInfo
&CSR
) { return CSR
.getReg() == RAReg
; }))
129 const RISCVInstrInfo
*TII
= STI
.getInstrInfo();
130 if (!STI
.hasForcedSWShadowStack() && STI
.hasStdExtZicfiss()) {
131 BuildMI(MBB
, MI
, DL
, TII
->get(RISCV::SSPOPCHK
)).addReg(RAReg
);
135 Register SCSPReg
= RISCVABI::getSCSPReg();
137 bool IsRV64
= STI
.hasFeature(RISCV::Feature64Bit
);
138 int64_t SlotSize
= STI
.getXLen() / 8;
139 // Load return address from shadow call stack
140 // l[w|d] ra, -[4|8](gp)
141 // addi gp, gp, -[4|8]
142 BuildMI(MBB
, MI
, DL
, TII
->get(IsRV64
? RISCV::LD
: RISCV::LW
))
143 .addReg(RAReg
, RegState::Define
)
146 .setMIFlag(MachineInstr::FrameDestroy
);
147 BuildMI(MBB
, MI
, DL
, TII
->get(RISCV::ADDI
))
148 .addReg(SCSPReg
, RegState::Define
)
151 .setMIFlag(MachineInstr::FrameDestroy
);
152 // Restore the SCS pointer
153 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestore(
154 nullptr, STI
.getRegisterInfo()->getDwarfRegNum(SCSPReg
, /*IsEH*/ true)));
155 BuildMI(MBB
, MI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
156 .addCFIIndex(CFIIndex
)
157 .setMIFlags(MachineInstr::FrameDestroy
);
160 // Get the ID of the libcall used for spilling and restoring callee saved
161 // registers. The ID is representative of the number of registers saved or
162 // restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
164 static int getLibCallID(const MachineFunction
&MF
,
165 const std::vector
<CalleeSavedInfo
> &CSI
) {
166 const auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
168 if (CSI
.empty() || !RVFI
->useSaveRestoreLibCalls(MF
))
171 Register MaxReg
= RISCV::NoRegister
;
173 // RISCVRegisterInfo::hasReservedSpillSlot assigns negative frame indexes to
174 // registers which can be saved by libcall.
175 if (CS
.getFrameIdx() < 0)
176 MaxReg
= std::max(MaxReg
.id(), CS
.getReg().id());
178 if (MaxReg
== RISCV::NoRegister
)
183 llvm_unreachable("Something has gone wrong!");
184 case /*s11*/ RISCV::X27
: return 12;
185 case /*s10*/ RISCV::X26
: return 11;
186 case /*s9*/ RISCV::X25
: return 10;
187 case /*s8*/ RISCV::X24
: return 9;
188 case /*s7*/ RISCV::X23
: return 8;
189 case /*s6*/ RISCV::X22
: return 7;
190 case /*s5*/ RISCV::X21
: return 6;
191 case /*s4*/ RISCV::X20
: return 5;
192 case /*s3*/ RISCV::X19
: return 4;
193 case /*s2*/ RISCV::X18
: return 3;
194 case /*s1*/ RISCV::X9
: return 2;
195 case /*s0*/ RISCV::X8
: return 1;
196 case /*ra*/ RISCV::X1
: return 0;
200 // Get the name of the libcall used for spilling callee saved registers.
201 // If this function will not use save/restore libcalls, then return a nullptr.
203 getSpillLibCallName(const MachineFunction
&MF
,
204 const std::vector
<CalleeSavedInfo
> &CSI
) {
205 static const char *const SpillLibCalls
[] = {
221 int LibCallID
= getLibCallID(MF
, CSI
);
224 return SpillLibCalls
[LibCallID
];
227 // Get the name of the libcall used for restoring callee saved registers.
228 // If this function will not use save/restore libcalls, then return a nullptr.
230 getRestoreLibCallName(const MachineFunction
&MF
,
231 const std::vector
<CalleeSavedInfo
> &CSI
) {
232 static const char *const RestoreLibCalls
[] = {
243 "__riscv_restore_10",
244 "__riscv_restore_11",
248 int LibCallID
= getLibCallID(MF
, CSI
);
251 return RestoreLibCalls
[LibCallID
];
254 // Return encoded value and register count for PUSH/POP instruction,
255 // representing registers to store/load.
256 static std::pair
<unsigned, unsigned>
257 getPushPopEncodingAndNum(const Register MaxReg
) {
260 llvm_unreachable("Unexpected Reg for Push/Pop Inst");
261 case RISCV::X27
: /*s11*/
262 case RISCV::X26
: /*s10*/
263 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S11
, 13);
264 case RISCV::X25
: /*s9*/
265 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S9
, 11);
266 case RISCV::X24
: /*s8*/
267 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S8
, 10);
268 case RISCV::X23
: /*s7*/
269 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S7
, 9);
270 case RISCV::X22
: /*s6*/
271 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S6
, 8);
272 case RISCV::X21
: /*s5*/
273 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S5
, 7);
274 case RISCV::X20
: /*s4*/
275 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S4
, 6);
276 case RISCV::X19
: /*s3*/
277 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S3
, 5);
278 case RISCV::X18
: /*s2*/
279 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S2
, 4);
280 case RISCV::X9
: /*s1*/
281 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S1
, 3);
282 case RISCV::X8
: /*s0*/
283 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0
, 2);
284 case RISCV::X1
: /*ra*/
285 return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA
, 1);
289 // Get the max reg of Push/Pop for restoring callee saved registers.
290 static Register
getMaxPushPopReg(const MachineFunction
&MF
,
291 const std::vector
<CalleeSavedInfo
> &CSI
) {
292 Register MaxPushPopReg
= RISCV::NoRegister
;
293 for (auto &CS
: CSI
) {
294 if (llvm::is_contained(AllPopRegs
, CS
.getReg().id()))
295 MaxPushPopReg
= std::max(MaxPushPopReg
.id(), CS
.getReg().id());
297 // if rlist is {rs, s0-s10}, then s11 will also be included
298 if (MaxPushPopReg
== RISCV::X26
)
299 MaxPushPopReg
= RISCV::X27
;
300 return MaxPushPopReg
;
303 // Return true if the specified function should have a dedicated frame
304 // pointer register. This is true if frame pointer elimination is
305 // disabled, if it needs dynamic stack realignment, if the function has
306 // variable sized allocas, or if the frame address is taken.
307 bool RISCVFrameLowering::hasFP(const MachineFunction
&MF
) const {
308 const TargetRegisterInfo
*RegInfo
= MF
.getSubtarget().getRegisterInfo();
310 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
311 return MF
.getTarget().Options
.DisableFramePointerElim(MF
) ||
312 RegInfo
->hasStackRealignment(MF
) || MFI
.hasVarSizedObjects() ||
313 MFI
.isFrameAddressTaken();
316 bool RISCVFrameLowering::hasBP(const MachineFunction
&MF
) const {
317 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
318 const TargetRegisterInfo
*TRI
= STI
.getRegisterInfo();
320 // If we do not reserve stack space for outgoing arguments in prologue,
321 // we will adjust the stack pointer before call instruction. After the
322 // adjustment, we can not use SP to access the stack objects for the
323 // arguments. Instead, use BP to access these stack objects.
324 return (MFI
.hasVarSizedObjects() ||
325 (!hasReservedCallFrame(MF
) && (!MFI
.isMaxCallFrameSizeComputed() ||
326 MFI
.getMaxCallFrameSize() != 0))) &&
327 TRI
->hasStackRealignment(MF
);
330 // Determines the size of the frame and maximum call frame size.
331 void RISCVFrameLowering::determineFrameLayout(MachineFunction
&MF
) const {
332 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
333 auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
335 // Get the number of bytes to allocate from the FrameInfo.
336 uint64_t FrameSize
= MFI
.getStackSize();
338 // Get the alignment.
339 Align StackAlign
= getStackAlign();
341 // Make sure the frame is aligned.
342 FrameSize
= alignTo(FrameSize
, StackAlign
);
344 // Update frame info.
345 MFI
.setStackSize(FrameSize
);
347 // When using SP or BP to access stack objects, we may require extra padding
348 // to ensure the bottom of the RVV stack is correctly aligned within the main
349 // stack. We calculate this as the amount required to align the scalar local
350 // variable section up to the RVV alignment.
351 const TargetRegisterInfo
*TRI
= STI
.getRegisterInfo();
352 if (RVFI
->getRVVStackSize() && (!hasFP(MF
) || TRI
->hasStackRealignment(MF
))) {
353 int ScalarLocalVarSize
= FrameSize
- RVFI
->getCalleeSavedStackSize() -
354 RVFI
->getVarArgsSaveSize();
355 if (auto RVVPadding
=
356 offsetToAlignment(ScalarLocalVarSize
, RVFI
->getRVVStackAlign()))
357 RVFI
->setRVVPadding(RVVPadding
);
361 // Returns the stack size including RVV padding (when required), rounded back
362 // up to the required stack alignment.
363 uint64_t RISCVFrameLowering::getStackSizeWithRVVPadding(
364 const MachineFunction
&MF
) const {
365 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
366 auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
367 return alignTo(MFI
.getStackSize() + RVFI
->getRVVPadding(), getStackAlign());
370 // Returns the register used to hold the frame pointer.
371 static Register
getFPReg(const RISCVSubtarget
&STI
) { return RISCV::X8
; }
373 // Returns the register used to hold the stack pointer.
374 static Register
getSPReg(const RISCVSubtarget
&STI
) { return RISCV::X2
; }
376 static SmallVector
<CalleeSavedInfo
, 8>
377 getUnmanagedCSI(const MachineFunction
&MF
,
378 const std::vector
<CalleeSavedInfo
> &CSI
) {
379 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
380 SmallVector
<CalleeSavedInfo
, 8> NonLibcallCSI
;
382 for (auto &CS
: CSI
) {
383 int FI
= CS
.getFrameIdx();
384 if (FI
>= 0 && MFI
.getStackID(FI
) == TargetStackID::Default
)
385 NonLibcallCSI
.push_back(CS
);
388 return NonLibcallCSI
;
391 static SmallVector
<CalleeSavedInfo
, 8>
392 getRVVCalleeSavedInfo(const MachineFunction
&MF
,
393 const std::vector
<CalleeSavedInfo
> &CSI
) {
394 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
395 SmallVector
<CalleeSavedInfo
, 8> RVVCSI
;
397 for (auto &CS
: CSI
) {
398 int FI
= CS
.getFrameIdx();
399 if (FI
>= 0 && MFI
.getStackID(FI
) == TargetStackID::ScalableVector
)
400 RVVCSI
.push_back(CS
);
406 void RISCVFrameLowering::adjustStackForRVV(MachineFunction
&MF
,
407 MachineBasicBlock
&MBB
,
408 MachineBasicBlock::iterator MBBI
,
409 const DebugLoc
&DL
, int64_t Amount
,
410 MachineInstr::MIFlag Flag
) const {
411 assert(Amount
!= 0 && "Did not need to adjust stack pointer for RVV.");
413 const Register SPReg
= getSPReg(STI
);
415 // Optimize compile time offset case
416 StackOffset Offset
= StackOffset::getScalable(Amount
);
417 if (auto VLEN
= STI
.getRealVLen()) {
418 // 1. Multiply the number of v-slots by the (constant) length of register
419 const int64_t VLENB
= *VLEN
/ 8;
420 assert(Amount
% 8 == 0 &&
421 "Reserve the stack by the multiple of one vector size.");
422 const int64_t NumOfVReg
= Amount
/ 8;
423 const int64_t FixedOffset
= NumOfVReg
* VLENB
;
424 if (!isInt
<32>(FixedOffset
)) {
426 "Frame size outside of the signed 32-bit range not supported");
428 Offset
= StackOffset::getFixed(FixedOffset
);
431 const RISCVRegisterInfo
&RI
= *STI
.getRegisterInfo();
432 // We must keep the stack pointer aligned through any intermediate
434 RI
.adjustReg(MBB
, MBBI
, DL
, SPReg
, SPReg
, Offset
,
435 Flag
, getStackAlign());
438 static void appendScalableVectorExpression(const TargetRegisterInfo
&TRI
,
439 SmallVectorImpl
<char> &Expr
,
440 int FixedOffset
, int ScalableOffset
,
441 llvm::raw_string_ostream
&Comment
) {
442 unsigned DwarfVLenB
= TRI
.getDwarfRegNum(RISCV::VLENB
, true);
445 Expr
.push_back(dwarf::DW_OP_consts
);
446 Expr
.append(Buffer
, Buffer
+ encodeSLEB128(FixedOffset
, Buffer
));
447 Expr
.push_back((uint8_t)dwarf::DW_OP_plus
);
448 Comment
<< (FixedOffset
< 0 ? " - " : " + ") << std::abs(FixedOffset
);
451 Expr
.push_back((uint8_t)dwarf::DW_OP_consts
);
452 Expr
.append(Buffer
, Buffer
+ encodeSLEB128(ScalableOffset
, Buffer
));
454 Expr
.push_back((uint8_t)dwarf::DW_OP_bregx
);
455 Expr
.append(Buffer
, Buffer
+ encodeULEB128(DwarfVLenB
, Buffer
));
458 Expr
.push_back((uint8_t)dwarf::DW_OP_mul
);
459 Expr
.push_back((uint8_t)dwarf::DW_OP_plus
);
461 Comment
<< (ScalableOffset
< 0 ? " - " : " + ") << std::abs(ScalableOffset
)
465 static MCCFIInstruction
createDefCFAExpression(const TargetRegisterInfo
&TRI
,
467 uint64_t FixedOffset
,
468 uint64_t ScalableOffset
) {
469 assert(ScalableOffset
!= 0 && "Did not need to adjust CFA for RVV");
470 SmallString
<64> Expr
;
471 std::string CommentBuffer
;
472 llvm::raw_string_ostream
Comment(CommentBuffer
);
473 // Build up the expression (Reg + FixedOffset + ScalableOffset * VLENB).
474 unsigned DwarfReg
= TRI
.getDwarfRegNum(Reg
, true);
475 Expr
.push_back((uint8_t)(dwarf::DW_OP_breg0
+ DwarfReg
));
477 if (Reg
== RISCV::X2
)
480 Comment
<< printReg(Reg
, &TRI
);
482 appendScalableVectorExpression(TRI
, Expr
, FixedOffset
, ScalableOffset
,
485 SmallString
<64> DefCfaExpr
;
487 DefCfaExpr
.push_back(dwarf::DW_CFA_def_cfa_expression
);
488 DefCfaExpr
.append(Buffer
, Buffer
+ encodeULEB128(Expr
.size(), Buffer
));
489 DefCfaExpr
.append(Expr
.str());
491 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr
.str(), SMLoc(),
495 static MCCFIInstruction
createDefCFAOffset(const TargetRegisterInfo
&TRI
,
496 Register Reg
, uint64_t FixedOffset
,
497 uint64_t ScalableOffset
) {
498 assert(ScalableOffset
!= 0 && "Did not need to adjust CFA for RVV");
499 SmallString
<64> Expr
;
500 std::string CommentBuffer
;
501 llvm::raw_string_ostream
Comment(CommentBuffer
);
502 Comment
<< printReg(Reg
, &TRI
) << " @ cfa";
504 // Build up the expression (FixedOffset + ScalableOffset * VLENB).
505 appendScalableVectorExpression(TRI
, Expr
, FixedOffset
, ScalableOffset
,
508 SmallString
<64> DefCfaExpr
;
510 unsigned DwarfReg
= TRI
.getDwarfRegNum(Reg
, true);
511 DefCfaExpr
.push_back(dwarf::DW_CFA_expression
);
512 DefCfaExpr
.append(Buffer
, Buffer
+ encodeULEB128(DwarfReg
, Buffer
));
513 DefCfaExpr
.append(Buffer
, Buffer
+ encodeULEB128(Expr
.size(), Buffer
));
514 DefCfaExpr
.append(Expr
.str());
516 return MCCFIInstruction::createEscape(nullptr, DefCfaExpr
.str(), SMLoc(),
520 void RISCVFrameLowering::emitPrologue(MachineFunction
&MF
,
521 MachineBasicBlock
&MBB
) const {
522 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
523 auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
524 const RISCVRegisterInfo
*RI
= STI
.getRegisterInfo();
525 const RISCVInstrInfo
*TII
= STI
.getInstrInfo();
526 MachineBasicBlock::iterator MBBI
= MBB
.begin();
528 Register FPReg
= getFPReg(STI
);
529 Register SPReg
= getSPReg(STI
);
530 Register BPReg
= RISCVABI::getBPReg();
532 // Debug location must be unknown since the first debug location is used
533 // to determine the end of the prologue.
536 // All calls are tail calls in GHC calling conv, and functions have no
537 // prologue/epilogue.
538 if (MF
.getFunction().getCallingConv() == CallingConv::GHC
)
541 // Emit prologue for shadow call stack.
542 emitSCSPrologue(MF
, MBB
, MBBI
, DL
);
544 auto FirstFrameSetup
= MBBI
;
546 // Since spillCalleeSavedRegisters may have inserted a libcall, skip past
547 // any instructions marked as FrameSetup
548 while (MBBI
!= MBB
.end() && MBBI
->getFlag(MachineInstr::FrameSetup
))
551 // Determine the correct frame layout
552 determineFrameLayout(MF
);
554 // If libcalls are used to spill and restore callee-saved registers, the frame
555 // has two sections; the opaque section managed by the libcalls, and the
556 // section managed by MachineFrameInfo which can also hold callee saved
557 // registers in fixed stack slots, both of which have negative frame indices.
558 // This gets even more complicated when incoming arguments are passed via the
559 // stack, as these too have negative frame indices. An example is detailed
562 // | incoming arg | <- FI[-3]
564 // | calleespill | <- FI[-2]
565 // | calleespill | <- FI[-1]
566 // | this_frame | <- FI[0]
568 // For negative frame indices, the offset from the frame pointer will differ
569 // depending on which of these groups the frame index applies to.
570 // The following calculates the correct offset knowing the number of callee
571 // saved registers spilt by the two methods.
572 if (int LibCallRegs
= getLibCallID(MF
, MFI
.getCalleeSavedInfo()) + 1) {
573 // Calculate the size of the frame managed by the libcall. The stack
574 // alignment of these libcalls should be the same as how we set it in
575 // getABIStackAlignment.
576 unsigned LibCallFrameSize
=
577 alignTo((STI
.getXLen() / 8) * LibCallRegs
, getStackAlign());
578 RVFI
->setLibCallStackSize(LibCallFrameSize
);
581 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
582 // investigation. Get the number of bytes to allocate from the FrameInfo.
583 uint64_t RealStackSize
= getStackSizeWithRVVPadding(MF
);
584 uint64_t StackSize
= RealStackSize
- RVFI
->getReservedSpillsSize();
585 uint64_t RVVStackSize
= RVFI
->getRVVStackSize();
587 // Early exit if there is no need to allocate on the stack
588 if (RealStackSize
== 0 && !MFI
.adjustsStack() && RVVStackSize
== 0)
591 // If the stack pointer has been marked as reserved, then produce an error if
592 // the frame requires stack allocation
593 if (STI
.isRegisterReservedByUser(SPReg
))
594 MF
.getFunction().getContext().diagnose(DiagnosticInfoUnsupported
{
595 MF
.getFunction(), "Stack pointer required, but has been reserved."});
597 uint64_t FirstSPAdjustAmount
= getFirstSPAdjustAmount(MF
);
598 // Split the SP adjustment to reduce the offsets of callee saved spill.
599 if (FirstSPAdjustAmount
) {
600 StackSize
= FirstSPAdjustAmount
;
601 RealStackSize
= FirstSPAdjustAmount
;
604 if (RVFI
->isPushable(MF
) && FirstFrameSetup
!= MBB
.end() &&
605 FirstFrameSetup
->getOpcode() == RISCV::CM_PUSH
) {
606 // Use available stack adjustment in push instruction to allocate additional
607 // stack space. Align the stack size down to a multiple of 16. This is
609 // FIXME: Can we increase the stack size to a multiple of 16 instead?
610 uint64_t Spimm
= std::min(alignDown(StackSize
, 16), (uint64_t)48);
611 FirstFrameSetup
->getOperand(1).setImm(Spimm
);
615 if (StackSize
!= 0) {
616 // Allocate space on the stack if necessary.
617 RI
->adjustReg(MBB
, MBBI
, DL
, SPReg
, SPReg
,
618 StackOffset::getFixed(-StackSize
), MachineInstr::FrameSetup
,
622 // Emit ".cfi_def_cfa_offset RealStackSize"
623 unsigned CFIIndex
= MF
.addFrameInst(
624 MCCFIInstruction::cfiDefCfaOffset(nullptr, RealStackSize
));
625 BuildMI(MBB
, MBBI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
626 .addCFIIndex(CFIIndex
)
627 .setMIFlag(MachineInstr::FrameSetup
);
629 const auto &CSI
= MFI
.getCalleeSavedInfo();
631 // The frame pointer is callee-saved, and code has been generated for us to
632 // save it to the stack. We need to skip over the storing of callee-saved
633 // registers as the frame pointer must be modified after it has been saved
634 // to the stack, not before.
635 // FIXME: assumes exactly one instruction is used to save each callee-saved
637 std::advance(MBBI
, getUnmanagedCSI(MF
, CSI
).size());
639 // Iterate over list of callee-saved registers and emit .cfi_offset
641 for (const auto &Entry
: CSI
) {
642 int FrameIdx
= Entry
.getFrameIdx();
644 MFI
.getStackID(FrameIdx
) == TargetStackID::ScalableVector
)
647 int64_t Offset
= MFI
.getObjectOffset(FrameIdx
);
648 Register Reg
= Entry
.getReg();
649 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createOffset(
650 nullptr, RI
->getDwarfRegNum(Reg
, true), Offset
));
651 BuildMI(MBB
, MBBI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
652 .addCFIIndex(CFIIndex
)
653 .setMIFlag(MachineInstr::FrameSetup
);
658 if (STI
.isRegisterReservedByUser(FPReg
))
659 MF
.getFunction().getContext().diagnose(DiagnosticInfoUnsupported
{
660 MF
.getFunction(), "Frame pointer required, but has been reserved."});
661 // The frame pointer does need to be reserved from register allocation.
662 assert(MF
.getRegInfo().isReserved(FPReg
) && "FP not reserved");
664 RI
->adjustReg(MBB
, MBBI
, DL
, FPReg
, SPReg
,
665 StackOffset::getFixed(RealStackSize
- RVFI
->getVarArgsSaveSize()),
666 MachineInstr::FrameSetup
, getStackAlign());
668 // Emit ".cfi_def_cfa $fp, RVFI->getVarArgsSaveSize()"
669 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::cfiDefCfa(
670 nullptr, RI
->getDwarfRegNum(FPReg
, true), RVFI
->getVarArgsSaveSize()));
671 BuildMI(MBB
, MBBI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
672 .addCFIIndex(CFIIndex
)
673 .setMIFlag(MachineInstr::FrameSetup
);
676 // Emit the second SP adjustment after saving callee saved registers.
677 if (FirstSPAdjustAmount
) {
678 uint64_t SecondSPAdjustAmount
=
679 getStackSizeWithRVVPadding(MF
) - FirstSPAdjustAmount
;
680 assert(SecondSPAdjustAmount
> 0 &&
681 "SecondSPAdjustAmount should be greater than zero");
682 RI
->adjustReg(MBB
, MBBI
, DL
, SPReg
, SPReg
,
683 StackOffset::getFixed(-SecondSPAdjustAmount
),
684 MachineInstr::FrameSetup
, getStackAlign());
686 // If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0",
687 // don't emit an sp-based .cfi_def_cfa_offset
689 // Emit ".cfi_def_cfa_offset StackSize"
690 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(
691 nullptr, getStackSizeWithRVVPadding(MF
)));
692 BuildMI(MBB
, MBBI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
693 .addCFIIndex(CFIIndex
)
694 .setMIFlag(MachineInstr::FrameSetup
);
699 adjustStackForRVV(MF
, MBB
, MBBI
, DL
, -RVVStackSize
,
700 MachineInstr::FrameSetup
);
702 // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
703 unsigned CFIIndex
= MF
.addFrameInst(createDefCFAExpression(
704 *RI
, SPReg
, getStackSizeWithRVVPadding(MF
), RVVStackSize
/ 8));
705 BuildMI(MBB
, MBBI
, DL
, TII
->get(TargetOpcode::CFI_INSTRUCTION
))
706 .addCFIIndex(CFIIndex
)
707 .setMIFlag(MachineInstr::FrameSetup
);
710 std::advance(MBBI
, getRVVCalleeSavedInfo(MF
, CSI
).size());
711 emitCalleeSavedRVVPrologCFI(MBB
, MBBI
, hasFP(MF
));
716 const RISCVRegisterInfo
*RI
= STI
.getRegisterInfo();
717 if (RI
->hasStackRealignment(MF
)) {
718 Align MaxAlignment
= MFI
.getMaxAlign();
720 const RISCVInstrInfo
*TII
= STI
.getInstrInfo();
721 if (isInt
<12>(-(int)MaxAlignment
.value())) {
722 BuildMI(MBB
, MBBI
, DL
, TII
->get(RISCV::ANDI
), SPReg
)
724 .addImm(-(int)MaxAlignment
.value())
725 .setMIFlag(MachineInstr::FrameSetup
);
727 unsigned ShiftAmount
= Log2(MaxAlignment
);
729 MF
.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass
);
730 BuildMI(MBB
, MBBI
, DL
, TII
->get(RISCV::SRLI
), VR
)
733 .setMIFlag(MachineInstr::FrameSetup
);
734 BuildMI(MBB
, MBBI
, DL
, TII
->get(RISCV::SLLI
), SPReg
)
737 .setMIFlag(MachineInstr::FrameSetup
);
739 // FP will be used to restore the frame in the epilogue, so we need
740 // another base register BP to record SP after re-alignment. SP will
741 // track the current stack after allocating variable sized objects.
744 BuildMI(MBB
, MBBI
, DL
, TII
->get(RISCV::ADDI
), BPReg
)
747 .setMIFlag(MachineInstr::FrameSetup
);
753 void RISCVFrameLowering::emitEpilogue(MachineFunction
&MF
,
754 MachineBasicBlock
&MBB
) const {
755 const RISCVRegisterInfo
*RI
= STI
.getRegisterInfo();
756 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
757 auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
758 Register FPReg
= getFPReg(STI
);
759 Register SPReg
= getSPReg(STI
);
761 // All calls are tail calls in GHC calling conv, and functions have no
762 // prologue/epilogue.
763 if (MF
.getFunction().getCallingConv() == CallingConv::GHC
)
766 // Get the insert location for the epilogue. If there were no terminators in
767 // the block, get the last instruction.
768 MachineBasicBlock::iterator MBBI
= MBB
.end();
771 MBBI
= MBB
.getLastNonDebugInstr();
772 if (MBBI
!= MBB
.end())
773 DL
= MBBI
->getDebugLoc();
775 MBBI
= MBB
.getFirstTerminator();
777 // If callee-saved registers are saved via libcall, place stack adjustment
779 while (MBBI
!= MBB
.begin() &&
780 std::prev(MBBI
)->getFlag(MachineInstr::FrameDestroy
))
784 const auto &CSI
= getUnmanagedCSI(MF
, MFI
.getCalleeSavedInfo());
786 // Skip to before the restores of scalar callee-saved registers
787 // FIXME: assumes exactly one instruction is used to restore each
788 // callee-saved register.
789 auto LastFrameDestroy
= MBBI
;
791 LastFrameDestroy
= std::prev(MBBI
, CSI
.size());
793 uint64_t RealStackSize
= getStackSizeWithRVVPadding(MF
);
794 uint64_t StackSize
= RealStackSize
- RVFI
->getReservedSpillsSize();
795 uint64_t FPOffset
= RealStackSize
- RVFI
->getVarArgsSaveSize();
796 uint64_t RVVStackSize
= RVFI
->getRVVStackSize();
798 // Restore the stack pointer using the value of the frame pointer. Only
799 // necessary if the stack pointer was modified, meaning the stack size is
802 // In order to make sure the stack point is right through the EH region,
803 // we also need to restore stack pointer from the frame pointer if we
804 // don't preserve stack space within prologue/epilogue for outgoing variables,
805 // normally it's just checking the variable sized object is present or not
806 // is enough, but we also don't preserve that at prologue/epilogue when
807 // have vector objects in stack.
808 if (RI
->hasStackRealignment(MF
) || MFI
.hasVarSizedObjects() ||
809 !hasReservedCallFrame(MF
)) {
810 assert(hasFP(MF
) && "frame pointer should not have been eliminated");
811 RI
->adjustReg(MBB
, LastFrameDestroy
, DL
, SPReg
, FPReg
,
812 StackOffset::getFixed(-FPOffset
),
813 MachineInstr::FrameDestroy
, getStackAlign());
816 adjustStackForRVV(MF
, MBB
, LastFrameDestroy
, DL
, RVVStackSize
,
817 MachineInstr::FrameDestroy
);
820 uint64_t FirstSPAdjustAmount
= getFirstSPAdjustAmount(MF
);
821 if (FirstSPAdjustAmount
) {
822 uint64_t SecondSPAdjustAmount
=
823 getStackSizeWithRVVPadding(MF
) - FirstSPAdjustAmount
;
824 assert(SecondSPAdjustAmount
> 0 &&
825 "SecondSPAdjustAmount should be greater than zero");
827 RI
->adjustReg(MBB
, LastFrameDestroy
, DL
, SPReg
, SPReg
,
828 StackOffset::getFixed(SecondSPAdjustAmount
),
829 MachineInstr::FrameDestroy
, getStackAlign());
832 if (FirstSPAdjustAmount
)
833 StackSize
= FirstSPAdjustAmount
;
835 if (RVFI
->isPushable(MF
) && MBBI
!= MBB
.end() &&
836 MBBI
->getOpcode() == RISCV::CM_POP
) {
837 // Use available stack adjustment in pop instruction to deallocate stack
838 // space. Align the stack size down to a multiple of 16. This is needed for
840 // FIXME: Can we increase the stack size to a multiple of 16 instead?
841 uint64_t Spimm
= std::min(alignDown(StackSize
, 16), (uint64_t)48);
842 MBBI
->getOperand(1).setImm(Spimm
);
847 if (StackSize
!= 0) {
848 RI
->adjustReg(MBB
, MBBI
, DL
, SPReg
, SPReg
, StackOffset::getFixed(StackSize
),
849 MachineInstr::FrameDestroy
, getStackAlign());
852 // Emit epilogue for shadow call stack.
853 emitSCSEpilogue(MF
, MBB
, MBBI
, DL
);
857 RISCVFrameLowering::getFrameIndexReference(const MachineFunction
&MF
, int FI
,
858 Register
&FrameReg
) const {
859 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
860 const TargetRegisterInfo
*RI
= MF
.getSubtarget().getRegisterInfo();
861 const auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
863 // Callee-saved registers should be referenced relative to the stack
864 // pointer (positive offset), otherwise use the frame pointer (negative
866 const auto &CSI
= getUnmanagedCSI(MF
, MFI
.getCalleeSavedInfo());
870 auto StackID
= MFI
.getStackID(FI
);
872 assert((StackID
== TargetStackID::Default
||
873 StackID
== TargetStackID::ScalableVector
) &&
874 "Unexpected stack ID for the frame object.");
875 if (StackID
== TargetStackID::Default
) {
877 StackOffset::getFixed(MFI
.getObjectOffset(FI
) - getOffsetOfLocalArea() +
878 MFI
.getOffsetAdjustment());
879 } else if (StackID
== TargetStackID::ScalableVector
) {
880 Offset
= StackOffset::getScalable(MFI
.getObjectOffset(FI
));
883 uint64_t FirstSPAdjustAmount
= getFirstSPAdjustAmount(MF
);
886 MinCSFI
= CSI
[0].getFrameIdx();
887 MaxCSFI
= CSI
[CSI
.size() - 1].getFrameIdx();
890 if (FI
>= MinCSFI
&& FI
<= MaxCSFI
) {
891 FrameReg
= RISCV::X2
;
893 if (FirstSPAdjustAmount
)
894 Offset
+= StackOffset::getFixed(FirstSPAdjustAmount
);
896 Offset
+= StackOffset::getFixed(getStackSizeWithRVVPadding(MF
));
900 if (RI
->hasStackRealignment(MF
) && !MFI
.isFixedObjectIndex(FI
)) {
901 // If the stack was realigned, the frame pointer is set in order to allow
902 // SP to be restored, so we need another base register to record the stack
903 // after realignment.
904 // |--------------------------| -- <-- FP
905 // | callee-allocated save | | <----|
906 // | area for register varargs| | |
907 // |--------------------------| | |
908 // | callee-saved registers | | |
909 // |--------------------------| -- |
910 // | realignment (the size of | | |
911 // | this area is not counted | | |
912 // | in MFI.getStackSize()) | | |
913 // |--------------------------| -- |-- MFI.getStackSize()
914 // | RVV alignment padding | | |
915 // | (not counted in | | |
916 // | MFI.getStackSize() but | | |
917 // | counted in | | |
918 // | RVFI.getRVVStackSize()) | | |
919 // |--------------------------| -- |
920 // | RVV objects | | |
921 // | (not counted in | | |
922 // | MFI.getStackSize()) | | |
923 // |--------------------------| -- |
924 // | padding before RVV | | |
925 // | (not counted in | | |
926 // | MFI.getStackSize() or in | | |
927 // | RVFI.getRVVStackSize()) | | |
928 // |--------------------------| -- |
929 // | scalar local variables | | <----'
930 // |--------------------------| -- <-- BP (if var sized objects present)
931 // | VarSize objects | |
932 // |--------------------------| -- <-- SP
934 FrameReg
= RISCVABI::getBPReg();
936 // VarSize objects must be empty in this case!
937 assert(!MFI
.hasVarSizedObjects());
938 FrameReg
= RISCV::X2
;
941 FrameReg
= RI
->getFrameRegister(MF
);
944 if (FrameReg
== getFPReg(STI
)) {
945 Offset
+= StackOffset::getFixed(RVFI
->getVarArgsSaveSize());
946 // When using FP to access scalable vector objects, we need to minus
949 // |--------------------------| -- <-- FP
950 // | callee-allocated save | |
951 // | area for register varargs| |
952 // |--------------------------| |
953 // | callee-saved registers | |
954 // |--------------------------| | MFI.getStackSize()
955 // | scalar local variables | |
956 // |--------------------------| -- (Offset of RVV objects is from here.)
958 // |--------------------------|
959 // | VarSize objects |
960 // |--------------------------| <-- SP
961 if (MFI
.getStackID(FI
) == TargetStackID::ScalableVector
) {
962 assert(!RI
->hasStackRealignment(MF
) &&
963 "Can't index across variable sized realign");
964 // We don't expect any extra RVV alignment padding, as the stack size
965 // and RVV object sections should be correct aligned in their own
967 assert(MFI
.getStackSize() == getStackSizeWithRVVPadding(MF
) &&
968 "Inconsistent stack layout");
969 Offset
-= StackOffset::getFixed(MFI
.getStackSize());
974 // This case handles indexing off both SP and BP.
975 // If indexing off SP, there must not be any var sized objects
976 assert(FrameReg
== RISCVABI::getBPReg() || !MFI
.hasVarSizedObjects());
978 // When using SP to access frame objects, we need to add RVV stack size.
980 // |--------------------------| -- <-- FP
981 // | callee-allocated save | | <----|
982 // | area for register varargs| | |
983 // |--------------------------| | |
984 // | callee-saved registers | | |
985 // |--------------------------| -- |
986 // | RVV alignment padding | | |
987 // | (not counted in | | |
988 // | MFI.getStackSize() but | | |
989 // | counted in | | |
990 // | RVFI.getRVVStackSize()) | | |
991 // |--------------------------| -- |
992 // | RVV objects | | |-- MFI.getStackSize()
993 // | (not counted in | | |
994 // | MFI.getStackSize()) | | |
995 // |--------------------------| -- |
996 // | padding before RVV | | |
997 // | (not counted in | | |
998 // | MFI.getStackSize()) | | |
999 // |--------------------------| -- |
1000 // | scalar local variables | | <----'
1001 // |--------------------------| -- <-- BP (if var sized objects present)
1002 // | VarSize objects | |
1003 // |--------------------------| -- <-- SP
1005 // The total amount of padding surrounding RVV objects is described by
1006 // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
1007 // objects to the required alignment.
1008 if (MFI
.getStackID(FI
) == TargetStackID::Default
) {
1009 if (MFI
.isFixedObjectIndex(FI
)) {
1010 assert(!RI
->hasStackRealignment(MF
) &&
1011 "Can't index across variable sized realign");
1012 Offset
+= StackOffset::get(getStackSizeWithRVVPadding(MF
),
1013 RVFI
->getRVVStackSize());
1015 Offset
+= StackOffset::getFixed(MFI
.getStackSize());
1017 } else if (MFI
.getStackID(FI
) == TargetStackID::ScalableVector
) {
1018 // Ensure the base of the RVV stack is correctly aligned: add on the
1019 // alignment padding.
1020 int ScalarLocalVarSize
= MFI
.getStackSize() -
1021 RVFI
->getCalleeSavedStackSize() -
1022 RVFI
->getRVPushStackSize() -
1023 RVFI
->getVarArgsSaveSize() + RVFI
->getRVVPadding();
1024 Offset
+= StackOffset::get(ScalarLocalVarSize
, RVFI
->getRVVStackSize());
1029 void RISCVFrameLowering::determineCalleeSaves(MachineFunction
&MF
,
1030 BitVector
&SavedRegs
,
1031 RegScavenger
*RS
) const {
1032 TargetFrameLowering::determineCalleeSaves(MF
, SavedRegs
, RS
);
1033 // Unconditionally spill RA and FP only if the function uses a frame
1036 SavedRegs
.set(RISCV::X1
);
1037 SavedRegs
.set(RISCV::X8
);
1039 // Mark BP as used if function has dedicated base pointer.
1041 SavedRegs
.set(RISCVABI::getBPReg());
1044 std::pair
<int64_t, Align
>
1045 RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFunction
&MF
) const {
1046 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1047 // Create a buffer of RVV objects to allocate.
1048 SmallVector
<int, 8> ObjectsToAllocate
;
1049 auto pushRVVObjects
= [&](int FIBegin
, int FIEnd
) {
1050 for (int I
= FIBegin
, E
= FIEnd
; I
!= E
; ++I
) {
1051 unsigned StackID
= MFI
.getStackID(I
);
1052 if (StackID
!= TargetStackID::ScalableVector
)
1054 if (MFI
.isDeadObjectIndex(I
))
1057 ObjectsToAllocate
.push_back(I
);
1060 // First push RVV Callee Saved object, then push RVV stack object
1061 std::vector
<CalleeSavedInfo
> &CSI
= MF
.getFrameInfo().getCalleeSavedInfo();
1062 const auto &RVVCSI
= getRVVCalleeSavedInfo(MF
, CSI
);
1063 if (!RVVCSI
.empty())
1064 pushRVVObjects(RVVCSI
[0].getFrameIdx(),
1065 RVVCSI
[RVVCSI
.size() - 1].getFrameIdx() + 1);
1066 pushRVVObjects(0, MFI
.getObjectIndexEnd() - RVVCSI
.size());
1068 // The minimum alignment is 16 bytes.
1069 Align
RVVStackAlign(16);
1070 const auto &ST
= MF
.getSubtarget
<RISCVSubtarget
>();
1072 if (!ST
.hasVInstructions()) {
1073 assert(ObjectsToAllocate
.empty() &&
1074 "Can't allocate scalable-vector objects without V instructions");
1075 return std::make_pair(0, RVVStackAlign
);
1078 // Allocate all RVV locals and spills
1080 for (int FI
: ObjectsToAllocate
) {
1081 // ObjectSize in bytes.
1082 int64_t ObjectSize
= MFI
.getObjectSize(FI
);
1083 auto ObjectAlign
= std::max(Align(8), MFI
.getObjectAlign(FI
));
1084 // If the data type is the fractional vector type, reserve one vector
1088 Offset
= alignTo(Offset
+ ObjectSize
, ObjectAlign
);
1089 MFI
.setObjectOffset(FI
, -Offset
);
1090 // Update the maximum alignment of the RVV stack section
1091 RVVStackAlign
= std::max(RVVStackAlign
, ObjectAlign
);
1094 // Ensure the alignment of the RVV stack. Since we want the most-aligned
1095 // object right at the bottom (i.e., any padding at the top of the frame),
1096 // readjust all RVV objects down by the alignment padding.
1097 uint64_t StackSize
= Offset
;
1098 if (auto AlignmentPadding
= offsetToAlignment(StackSize
, RVVStackAlign
)) {
1099 StackSize
+= AlignmentPadding
;
1100 for (int FI
: ObjectsToAllocate
)
1101 MFI
.setObjectOffset(FI
, MFI
.getObjectOffset(FI
) - AlignmentPadding
);
1104 return std::make_pair(StackSize
, RVVStackAlign
);
1107 static unsigned getScavSlotsNumForRVV(MachineFunction
&MF
) {
1108 // For RVV spill, scalable stack offsets computing requires up to two scratch
1110 static constexpr unsigned ScavSlotsNumRVVSpillScalableObject
= 2;
1112 // For RVV spill, non-scalable stack offsets computing requires up to one
1113 // scratch register.
1114 static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject
= 1;
1116 // ADDI instruction's destination register can be used for computing
1117 // offsets. So Scalable stack offsets require up to one scratch register.
1118 static constexpr unsigned ScavSlotsADDIScalableObject
= 1;
1120 static constexpr unsigned MaxScavSlotsNumKnown
=
1121 std::max({ScavSlotsADDIScalableObject
, ScavSlotsNumRVVSpillScalableObject
,
1122 ScavSlotsNumRVVSpillNonScalableObject
});
1124 unsigned MaxScavSlotsNum
= 0;
1125 if (!MF
.getSubtarget
<RISCVSubtarget
>().hasVInstructions())
1127 for (const MachineBasicBlock
&MBB
: MF
)
1128 for (const MachineInstr
&MI
: MBB
) {
1129 bool IsRVVSpill
= RISCV::isRVVSpill(MI
);
1130 for (auto &MO
: MI
.operands()) {
1133 bool IsScalableVectorID
= MF
.getFrameInfo().getStackID(MO
.getIndex()) ==
1134 TargetStackID::ScalableVector
;
1136 MaxScavSlotsNum
= std::max(
1137 MaxScavSlotsNum
, IsScalableVectorID
1138 ? ScavSlotsNumRVVSpillScalableObject
1139 : ScavSlotsNumRVVSpillNonScalableObject
);
1140 } else if (MI
.getOpcode() == RISCV::ADDI
&& IsScalableVectorID
) {
1142 std::max(MaxScavSlotsNum
, ScavSlotsADDIScalableObject
);
1145 if (MaxScavSlotsNum
== MaxScavSlotsNumKnown
)
1146 return MaxScavSlotsNumKnown
;
1148 return MaxScavSlotsNum
;
1151 static bool hasRVVFrameObject(const MachineFunction
&MF
) {
1152 // Originally, the function will scan all the stack objects to check whether
1153 // if there is any scalable vector object on the stack or not. However, it
1154 // causes errors in the register allocator. In issue 53016, it returns false
1155 // before RA because there is no RVV stack objects. After RA, it returns true
1156 // because there are spilling slots for RVV values during RA. It will not
1157 // reserve BP during register allocation and generate BP access in the PEI
1158 // pass due to the inconsistent behavior of the function.
1160 // The function is changed to use hasVInstructions() as the return value. It
1161 // is not precise, but it can make the register allocation correct.
1163 // FIXME: Find a better way to make the decision or revisit the solution in
1166 // Refer to https://github.com/llvm/llvm-project/issues/53016.
1167 return MF
.getSubtarget
<RISCVSubtarget
>().hasVInstructions();
1170 static unsigned estimateFunctionSizeInBytes(const MachineFunction
&MF
,
1171 const RISCVInstrInfo
&TII
) {
1172 unsigned FnSize
= 0;
1173 for (auto &MBB
: MF
) {
1174 for (auto &MI
: MBB
) {
1175 // Far branches over 20-bit offset will be relaxed in branch relaxation
1176 // pass. In the worst case, conditional branches will be relaxed into
1177 // the following instruction sequence. Unconditional branches are
1178 // relaxed in the same way, with the exception that there is no first
1179 // branch instruction.
1182 // bne t5, t6, .rev_cond # `TII->getInstSizeInBytes(MI)` bytes
1183 // sd s11, 0(sp) # 4 bytes, or 2 bytes in RVC
1184 // jump .restore, s11 # 8 bytes
1187 // j .dest_bb # 4 bytes, or 2 bytes in RVC
1189 // ld s11, 0(sp) # 4 bytes, or 2 bytes in RVC
1192 if (MI
.isConditionalBranch())
1193 FnSize
+= TII
.getInstSizeInBytes(MI
);
1194 if (MI
.isConditionalBranch() || MI
.isUnconditionalBranch()) {
1195 if (MF
.getSubtarget
<RISCVSubtarget
>().hasStdExtCOrZca())
1196 FnSize
+= 2 + 8 + 2 + 2;
1198 FnSize
+= 4 + 8 + 4 + 4;
1202 FnSize
+= TII
.getInstSizeInBytes(MI
);
1208 void RISCVFrameLowering::processFunctionBeforeFrameFinalized(
1209 MachineFunction
&MF
, RegScavenger
*RS
) const {
1210 const RISCVRegisterInfo
*RegInfo
=
1211 MF
.getSubtarget
<RISCVSubtarget
>().getRegisterInfo();
1212 const RISCVInstrInfo
*TII
= MF
.getSubtarget
<RISCVSubtarget
>().getInstrInfo();
1213 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1214 const TargetRegisterClass
*RC
= &RISCV::GPRRegClass
;
1215 auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
1217 int64_t RVVStackSize
;
1218 Align RVVStackAlign
;
1219 std::tie(RVVStackSize
, RVVStackAlign
) = assignRVVStackObjectOffsets(MF
);
1221 RVFI
->setRVVStackSize(RVVStackSize
);
1222 RVFI
->setRVVStackAlign(RVVStackAlign
);
1224 if (hasRVVFrameObject(MF
)) {
1225 // Ensure the entire stack is aligned to at least the RVV requirement: some
1226 // scalable-vector object alignments are not considered by the
1227 // target-independent code.
1228 MFI
.ensureMaxAlignment(RVVStackAlign
);
1231 unsigned ScavSlotsNum
= 0;
1233 // estimateStackSize has been observed to under-estimate the final stack
1234 // size, so give ourselves wiggle-room by checking for stack size
1235 // representable an 11-bit signed field rather than 12-bits.
1236 if (!isInt
<11>(MFI
.estimateStackSize(MF
)))
1239 // Far branches over 20-bit offset require a spill slot for scratch register.
1240 bool IsLargeFunction
= !isInt
<20>(estimateFunctionSizeInBytes(MF
, *TII
));
1241 if (IsLargeFunction
)
1242 ScavSlotsNum
= std::max(ScavSlotsNum
, 1u);
1244 // RVV loads & stores have no capacity to hold the immediate address offsets
1245 // so we must always reserve an emergency spill slot if the MachineFunction
1246 // contains any RVV spills.
1247 ScavSlotsNum
= std::max(ScavSlotsNum
, getScavSlotsNumForRVV(MF
));
1249 for (unsigned I
= 0; I
< ScavSlotsNum
; I
++) {
1250 int FI
= MFI
.CreateStackObject(RegInfo
->getSpillSize(*RC
),
1251 RegInfo
->getSpillAlign(*RC
), false);
1252 RS
->addScavengingFrameIndex(FI
);
1254 if (IsLargeFunction
&& RVFI
->getBranchRelaxationScratchFrameIndex() == -1)
1255 RVFI
->setBranchRelaxationScratchFrameIndex(FI
);
1258 unsigned Size
= RVFI
->getReservedSpillsSize();
1259 for (const auto &Info
: MFI
.getCalleeSavedInfo()) {
1260 int FrameIdx
= Info
.getFrameIdx();
1261 if (FrameIdx
< 0 || MFI
.getStackID(FrameIdx
) != TargetStackID::Default
)
1264 Size
+= MFI
.getObjectSize(FrameIdx
);
1266 RVFI
->setCalleeSavedStackSize(Size
);
1269 // Not preserve stack space within prologue for outgoing variables when the
1270 // function contains variable size objects or there are vector objects accessed
1271 // by the frame pointer.
1272 // Let eliminateCallFramePseudoInstr preserve stack space for it.
1273 bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction
&MF
) const {
1274 return !MF
.getFrameInfo().hasVarSizedObjects() &&
1275 !(hasFP(MF
) && hasRVVFrameObject(MF
));
1278 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
1279 MachineBasicBlock::iterator
RISCVFrameLowering::eliminateCallFramePseudoInstr(
1280 MachineFunction
&MF
, MachineBasicBlock
&MBB
,
1281 MachineBasicBlock::iterator MI
) const {
1282 Register SPReg
= RISCV::X2
;
1283 DebugLoc DL
= MI
->getDebugLoc();
1285 if (!hasReservedCallFrame(MF
)) {
1286 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
1287 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
1288 // pointer. This is necessary when there is a variable length stack
1289 // allocation (e.g. alloca), which means it's not possible to allocate
1290 // space for outgoing arguments from within the function prologue.
1291 int64_t Amount
= MI
->getOperand(0).getImm();
1294 // Ensure the stack remains aligned after adjustment.
1295 Amount
= alignSPAdjust(Amount
);
1297 if (MI
->getOpcode() == RISCV::ADJCALLSTACKDOWN
)
1300 const RISCVRegisterInfo
&RI
= *STI
.getRegisterInfo();
1301 RI
.adjustReg(MBB
, MI
, DL
, SPReg
, SPReg
, StackOffset::getFixed(Amount
),
1302 MachineInstr::NoFlags
, getStackAlign());
1306 return MBB
.erase(MI
);
1309 // We would like to split the SP adjustment to reduce prologue/epilogue
1310 // as following instructions. In this way, the offset of the callee saved
1311 // register could fit in a single store. Supposed that the first sp adjust
1321 RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction
&MF
) const {
1322 const auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
1323 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1324 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
.getCalleeSavedInfo();
1325 uint64_t StackSize
= getStackSizeWithRVVPadding(MF
);
1327 // Disable SplitSPAdjust if save-restore libcall is used. The callee-saved
1328 // registers will be pushed by the save-restore libcalls, so we don't have to
1329 // split the SP adjustment in this case.
1330 if (RVFI
->getReservedSpillsSize())
1333 // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
1334 // 12-bit and there exists a callee-saved register needing to be pushed.
1335 if (!isInt
<12>(StackSize
) && (CSI
.size() > 0)) {
1336 // FirstSPAdjustAmount is chosen at most as (2048 - StackAlign) because
1337 // 2048 will cause sp = sp + 2048 in the epilogue to be split into multiple
1338 // instructions. Offsets smaller than 2048 can fit in a single load/store
1339 // instruction, and we have to stick with the stack alignment. 2048 has
1340 // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
1341 // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
1342 const uint64_t StackAlign
= getStackAlign().value();
1344 // Amount of (2048 - StackAlign) will prevent callee saved and restored
1345 // instructions be compressed, so try to adjust the amount to the largest
1346 // offset that stack compression instructions accept when target supports
1347 // compression instructions.
1348 if (STI
.hasStdExtCOrZca()) {
1349 // The compression extensions may support the following instructions:
1350 // riscv32: c.lwsp rd, offset[7:2] => 2^(6 + 2)
1351 // c.swsp rs2, offset[7:2] => 2^(6 + 2)
1352 // c.flwsp rd, offset[7:2] => 2^(6 + 2)
1353 // c.fswsp rs2, offset[7:2] => 2^(6 + 2)
1354 // riscv64: c.ldsp rd, offset[8:3] => 2^(6 + 3)
1355 // c.sdsp rs2, offset[8:3] => 2^(6 + 3)
1356 // c.fldsp rd, offset[8:3] => 2^(6 + 3)
1357 // c.fsdsp rs2, offset[8:3] => 2^(6 + 3)
1358 const uint64_t RVCompressLen
= STI
.getXLen() * 8;
1359 // Compared with amount (2048 - StackAlign), StackSize needs to
1360 // satisfy the following conditions to avoid using more instructions
1361 // to adjust the sp after adjusting the amount, such as
1362 // StackSize meets the condition (StackSize <= 2048 + RVCompressLen),
1363 // case1: Amount is 2048 - StackAlign: use addi + addi to adjust sp.
1364 // case2: Amount is RVCompressLen: use addi + addi to adjust sp.
1365 auto CanCompress
= [&](uint64_t CompressLen
) -> bool {
1366 if (StackSize
<= 2047 + CompressLen
||
1367 (StackSize
> 2048 * 2 - StackAlign
&&
1368 StackSize
<= 2047 * 2 + CompressLen
) ||
1369 StackSize
> 2048 * 3 - StackAlign
)
1374 // In the epilogue, addi sp, sp, 496 is used to recover the sp and it
1375 // can be compressed(C.ADDI16SP, offset can be [-512, 496]), but
1376 // addi sp, sp, 512 can not be compressed. So try to use 496 first.
1377 const uint64_t ADDI16SPCompressLen
= 496;
1378 if (STI
.is64Bit() && CanCompress(ADDI16SPCompressLen
))
1379 return ADDI16SPCompressLen
;
1380 if (CanCompress(RVCompressLen
))
1381 return RVCompressLen
;
1383 return 2048 - StackAlign
;
1388 // Offsets which need to be scale by XLen representing locations of CSRs which
1389 // are given a fixed location by save/restore libcalls or Zcmp Push/Pop.
1390 static const std::pair
<MCPhysReg
, int8_t> FixedCSRFIMap
[] = {
1391 {/*ra*/ RISCV::X1
, -1}, {/*s0*/ RISCV::X8
, -2},
1392 {/*s1*/ RISCV::X9
, -3}, {/*s2*/ RISCV::X18
, -4},
1393 {/*s3*/ RISCV::X19
, -5}, {/*s4*/ RISCV::X20
, -6},
1394 {/*s5*/ RISCV::X21
, -7}, {/*s6*/ RISCV::X22
, -8},
1395 {/*s7*/ RISCV::X23
, -9}, {/*s8*/ RISCV::X24
, -10},
1396 {/*s9*/ RISCV::X25
, -11}, {/*s10*/ RISCV::X26
, -12},
1397 {/*s11*/ RISCV::X27
, -13}};
1399 bool RISCVFrameLowering::assignCalleeSavedSpillSlots(
1400 MachineFunction
&MF
, const TargetRegisterInfo
*TRI
,
1401 std::vector
<CalleeSavedInfo
> &CSI
, unsigned &MinCSFrameIndex
,
1402 unsigned &MaxCSFrameIndex
) const {
1403 // Early exit if no callee saved registers are modified!
1407 auto *RVFI
= MF
.getInfo
<RISCVMachineFunctionInfo
>();
1409 if (RVFI
->isPushable(MF
)) {
1410 // Determine how many GPRs we need to push and save it to RVFI.
1411 Register MaxReg
= getMaxPushPopReg(MF
, CSI
);
1412 if (MaxReg
!= RISCV::NoRegister
) {
1413 auto [RegEnc
, PushedRegNum
] = getPushPopEncodingAndNum(MaxReg
);
1414 RVFI
->setRVPushRegs(PushedRegNum
);
1415 RVFI
->setRVPushStackSize(alignTo((STI
.getXLen() / 8) * PushedRegNum
, 16));
1417 // Use encoded number to represent registers to spill.
1418 RVFI
->setRVPushRlist(RegEnc
);
1422 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1423 const TargetRegisterInfo
*RegInfo
= MF
.getSubtarget().getRegisterInfo();
1425 for (auto &CS
: CSI
) {
1426 unsigned Reg
= CS
.getReg();
1427 const TargetRegisterClass
*RC
= RegInfo
->getMinimalPhysRegClass(Reg
);
1428 unsigned Size
= RegInfo
->getSpillSize(*RC
);
1430 // This might need a fixed stack slot.
1431 if (RVFI
->useSaveRestoreLibCalls(MF
) || RVFI
->isPushable(MF
)) {
1432 const auto *FII
= llvm::find_if(
1433 FixedCSRFIMap
, [&](auto P
) { return P
.first
== CS
.getReg(); });
1434 if (FII
!= std::end(FixedCSRFIMap
)) {
1436 if (RVFI
->isPushable(MF
))
1437 Offset
= -((FII
->second
+ RVFI
->getRVPushRegs() + 1) * (int64_t)Size
);
1439 Offset
= FII
->second
* (int64_t)Size
;
1441 int FrameIdx
= MFI
.CreateFixedSpillStackObject(Size
, Offset
);
1442 assert(FrameIdx
< 0);
1443 CS
.setFrameIdx(FrameIdx
);
1448 // Not a fixed slot.
1449 Align Alignment
= RegInfo
->getSpillAlign(*RC
);
1450 // We may not be able to satisfy the desired alignment specification of
1451 // the TargetRegisterClass if the stack alignment is smaller. Use the
1453 Alignment
= std::min(Alignment
, getStackAlign());
1454 int FrameIdx
= MFI
.CreateStackObject(Size
, Alignment
, true);
1455 if ((unsigned)FrameIdx
< MinCSFrameIndex
)
1456 MinCSFrameIndex
= FrameIdx
;
1457 if ((unsigned)FrameIdx
> MaxCSFrameIndex
)
1458 MaxCSFrameIndex
= FrameIdx
;
1459 CS
.setFrameIdx(FrameIdx
);
1462 // Allocate a fixed object that covers the full push or libcall size.
1463 if (RVFI
->isPushable(MF
)) {
1464 if (int64_t PushSize
= RVFI
->getRVPushStackSize())
1465 MFI
.CreateFixedSpillStackObject(PushSize
, -PushSize
);
1466 } else if (int LibCallRegs
= getLibCallID(MF
, CSI
) + 1) {
1467 int64_t LibCallFrameSize
=
1468 alignTo((STI
.getXLen() / 8) * LibCallRegs
, getStackAlign());
1469 MFI
.CreateFixedSpillStackObject(LibCallFrameSize
, -LibCallFrameSize
);
1475 bool RISCVFrameLowering::spillCalleeSavedRegisters(
1476 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
1477 ArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
1481 MachineFunction
*MF
= MBB
.getParent();
1482 const TargetInstrInfo
&TII
= *MF
->getSubtarget().getInstrInfo();
1484 if (MI
!= MBB
.end() && !MI
->isDebugInstr())
1485 DL
= MI
->getDebugLoc();
1487 // Emit CM.PUSH with base SPimm & evaluate Push stack
1488 RISCVMachineFunctionInfo
*RVFI
= MF
->getInfo
<RISCVMachineFunctionInfo
>();
1489 if (RVFI
->isPushable(*MF
)) {
1490 unsigned PushedRegNum
= RVFI
->getRVPushRegs();
1491 if (PushedRegNum
> 0) {
1492 // Use encoded number to represent registers to spill.
1493 int RegEnc
= RVFI
->getRVPushRlist();
1494 MachineInstrBuilder PushBuilder
=
1495 BuildMI(MBB
, MI
, DL
, TII
.get(RISCV::CM_PUSH
))
1496 .setMIFlag(MachineInstr::FrameSetup
);
1497 PushBuilder
.addImm((int64_t)RegEnc
);
1498 PushBuilder
.addImm(0);
1500 for (unsigned i
= 0; i
< PushedRegNum
; i
++)
1501 PushBuilder
.addUse(AllPopRegs
[i
], RegState::Implicit
);
1503 } else if (const char *SpillLibCall
= getSpillLibCallName(*MF
, CSI
)) {
1504 // Add spill libcall via non-callee-saved register t0.
1505 BuildMI(MBB
, MI
, DL
, TII
.get(RISCV::PseudoCALLReg
), RISCV::X5
)
1506 .addExternalSymbol(SpillLibCall
, RISCVII::MO_CALL
)
1507 .setMIFlag(MachineInstr::FrameSetup
);
1509 // Add registers spilled in libcall as liveins.
1510 for (auto &CS
: CSI
)
1511 MBB
.addLiveIn(CS
.getReg());
1514 // Manually spill values not spilled by libcall & Push/Pop.
1515 const auto &UnmanagedCSI
= getUnmanagedCSI(*MF
, CSI
);
1516 const auto &RVVCSI
= getRVVCalleeSavedInfo(*MF
, CSI
);
1518 auto storeRegToStackSlot
= [&](decltype(UnmanagedCSI
) CSInfo
) {
1519 for (auto &CS
: CSInfo
) {
1520 // Insert the spill to the stack frame.
1521 Register Reg
= CS
.getReg();
1522 const TargetRegisterClass
*RC
= TRI
->getMinimalPhysRegClass(Reg
);
1523 TII
.storeRegToStackSlot(MBB
, MI
, Reg
, !MBB
.isLiveIn(Reg
),
1524 CS
.getFrameIdx(), RC
, TRI
, Register());
1527 storeRegToStackSlot(UnmanagedCSI
);
1528 storeRegToStackSlot(RVVCSI
);
1533 void RISCVFrameLowering::emitCalleeSavedRVVPrologCFI(
1534 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
, bool HasFP
) const {
1535 MachineFunction
*MF
= MBB
.getParent();
1536 const MachineFrameInfo
&MFI
= MF
->getFrameInfo();
1537 RISCVMachineFunctionInfo
*RVFI
= MF
->getInfo
<RISCVMachineFunctionInfo
>();
1538 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
1539 DebugLoc DL
= MBB
.findDebugLoc(MI
);
1541 const auto &RVVCSI
= getRVVCalleeSavedInfo(*MF
, MFI
.getCalleeSavedInfo());
1545 uint64_t FixedSize
= getStackSizeWithRVVPadding(*MF
);
1547 uint64_t ScalarLocalVarSize
=
1548 MFI
.getStackSize() - RVFI
->getCalleeSavedStackSize() -
1549 RVFI
->getRVPushStackSize() - RVFI
->getVarArgsSaveSize() +
1550 RVFI
->getRVVPadding();
1551 FixedSize
-= ScalarLocalVarSize
;
1554 for (auto &CS
: RVVCSI
) {
1555 // Insert the spill to the stack frame.
1556 int FI
= CS
.getFrameIdx();
1557 if (FI
>= 0 && MFI
.getStackID(FI
) == TargetStackID::ScalableVector
) {
1558 unsigned CFIIndex
= MF
->addFrameInst(
1559 createDefCFAOffset(*STI
.getRegisterInfo(), CS
.getReg(), -FixedSize
,
1560 MFI
.getObjectOffset(FI
) / 8));
1561 BuildMI(MBB
, MI
, DL
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
1562 .addCFIIndex(CFIIndex
)
1563 .setMIFlag(MachineInstr::FrameSetup
);
1568 bool RISCVFrameLowering::restoreCalleeSavedRegisters(
1569 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
1570 MutableArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
1574 MachineFunction
*MF
= MBB
.getParent();
1575 const TargetInstrInfo
&TII
= *MF
->getSubtarget().getInstrInfo();
1577 if (MI
!= MBB
.end() && !MI
->isDebugInstr())
1578 DL
= MI
->getDebugLoc();
1580 // Manually restore values not restored by libcall & Push/Pop.
1581 // Reverse the restore order in epilog. In addition, the return
1582 // address will be restored first in the epilogue. It increases
1583 // the opportunity to avoid the load-to-use data hazard between
1584 // loading RA and return by RA. loadRegFromStackSlot can insert
1585 // multiple instructions.
1586 const auto &UnmanagedCSI
= getUnmanagedCSI(*MF
, CSI
);
1587 const auto &RVVCSI
= getRVVCalleeSavedInfo(*MF
, CSI
);
1589 auto loadRegFromStackSlot
= [&](decltype(UnmanagedCSI
) CSInfo
) {
1590 for (auto &CS
: CSInfo
) {
1591 Register Reg
= CS
.getReg();
1592 const TargetRegisterClass
*RC
= TRI
->getMinimalPhysRegClass(Reg
);
1593 TII
.loadRegFromStackSlot(MBB
, MI
, Reg
, CS
.getFrameIdx(), RC
, TRI
,
1595 assert(MI
!= MBB
.begin() &&
1596 "loadRegFromStackSlot didn't insert any code!");
1599 loadRegFromStackSlot(RVVCSI
);
1600 loadRegFromStackSlot(UnmanagedCSI
);
1602 RISCVMachineFunctionInfo
*RVFI
= MF
->getInfo
<RISCVMachineFunctionInfo
>();
1603 if (RVFI
->isPushable(*MF
)) {
1604 int RegEnc
= RVFI
->getRVPushRlist();
1605 if (RegEnc
!= llvm::RISCVZC::RLISTENCODE::INVALID_RLIST
) {
1606 MachineInstrBuilder PopBuilder
=
1607 BuildMI(MBB
, MI
, DL
, TII
.get(RISCV::CM_POP
))
1608 .setMIFlag(MachineInstr::FrameDestroy
);
1609 // Use encoded number to represent registers to restore.
1610 PopBuilder
.addImm(RegEnc
);
1611 PopBuilder
.addImm(0);
1613 for (unsigned i
= 0; i
< RVFI
->getRVPushRegs(); i
++)
1614 PopBuilder
.addDef(AllPopRegs
[i
], RegState::ImplicitDefine
);
1617 const char *RestoreLibCall
= getRestoreLibCallName(*MF
, CSI
);
1618 if (RestoreLibCall
) {
1619 // Add restore libcall via tail call.
1620 MachineBasicBlock::iterator NewMI
=
1621 BuildMI(MBB
, MI
, DL
, TII
.get(RISCV::PseudoTAIL
))
1622 .addExternalSymbol(RestoreLibCall
, RISCVII::MO_CALL
)
1623 .setMIFlag(MachineInstr::FrameDestroy
);
1625 // Remove trailing returns, since the terminator is now a tail call to the
1626 // restore function.
1627 if (MI
!= MBB
.end() && MI
->getOpcode() == RISCV::PseudoRET
) {
1628 NewMI
->copyImplicitOps(*MF
, *MI
);
1629 MI
->eraseFromParent();
1636 bool RISCVFrameLowering::enableShrinkWrapping(const MachineFunction
&MF
) const {
1637 // Keep the conventional code flow when not optimizing.
1638 if (MF
.getFunction().hasOptNone())
1644 bool RISCVFrameLowering::canUseAsPrologue(const MachineBasicBlock
&MBB
) const {
1645 MachineBasicBlock
*TmpMBB
= const_cast<MachineBasicBlock
*>(&MBB
);
1646 const MachineFunction
*MF
= MBB
.getParent();
1647 const auto *RVFI
= MF
->getInfo
<RISCVMachineFunctionInfo
>();
1649 if (!RVFI
->useSaveRestoreLibCalls(*MF
))
1652 // Inserting a call to a __riscv_save libcall requires the use of the register
1653 // t0 (X5) to hold the return address. Therefore if this register is already
1654 // used we can't insert the call.
1657 RS
.enterBasicBlock(*TmpMBB
);
1658 return !RS
.isRegUsed(RISCV::X5
);
1661 bool RISCVFrameLowering::canUseAsEpilogue(const MachineBasicBlock
&MBB
) const {
1662 const MachineFunction
*MF
= MBB
.getParent();
1663 MachineBasicBlock
*TmpMBB
= const_cast<MachineBasicBlock
*>(&MBB
);
1664 const auto *RVFI
= MF
->getInfo
<RISCVMachineFunctionInfo
>();
1666 if (!RVFI
->useSaveRestoreLibCalls(*MF
))
1669 // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
1670 // This means if we still need to continue executing code within this function
1671 // the restore cannot take place in this basic block.
1673 if (MBB
.succ_size() > 1)
1676 MachineBasicBlock
*SuccMBB
=
1677 MBB
.succ_empty() ? TmpMBB
->getFallThrough() : *MBB
.succ_begin();
1679 // Doing a tail call should be safe if there are no successors, because either
1680 // we have a returning block or the end of the block is unreachable, so the
1681 // restore will be eliminated regardless.
1685 // The successor can only contain a return, since we would effectively be
1686 // replacing the successor with our own tail return at the end of our block.
1687 return SuccMBB
->isReturnBlock() && SuccMBB
->size() == 1;
1690 bool RISCVFrameLowering::isSupportedStackID(TargetStackID::Value ID
) const {
1692 case TargetStackID::Default
:
1693 case TargetStackID::ScalableVector
:
1695 case TargetStackID::NoAlloc
:
1696 case TargetStackID::SGPRSpill
:
1697 case TargetStackID::WasmLocal
:
1700 llvm_unreachable("Invalid TargetStackID::Value");
1703 TargetStackID::Value
RISCVFrameLowering::getStackIDForScalableVectors() const {
1704 return TargetStackID::ScalableVector
;