blob: ad19741a427a8d92a5e17cb519335dd53bc09deb [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVFrameLowering.cpp - RISCV Frame Information ------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Bradbury89718422017-10-19 21:37:38 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the RISCV implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVFrameLowering.h"
Alex Bradburyc85be0d2018-01-10 19:41:03 +000014#include "RISCVMachineFunctionInfo.h"
Alex Bradbury89718422017-10-19 21:37:38 +000015#include "RISCVSubtarget.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Bradbury0715d352018-01-11 11:17:19 +000020#include "llvm/CodeGen/RegisterScavenging.h"
Hsiangkai Wang04ddf392019-06-12 03:04:22 +000021#include "llvm/MC/MCDwarf.h"
Alex Bradbury89718422017-10-19 21:37:38 +000022
23using namespace llvm;
24
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +000025bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const {
26 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
27
28 const MachineFrameInfo &MFI = MF.getFrameInfo();
29 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
30 RegInfo->needsStackRealignment(MF) || MFI.hasVarSizedObjects() ||
31 MFI.isFrameAddressTaken();
32}
Alex Bradbury89718422017-10-19 21:37:38 +000033
Alex Bradburyb014e3d2017-12-11 12:34:11 +000034// Determines the size of the frame and maximum call frame size.
35void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
36 MachineFrameInfo &MFI = MF.getFrameInfo();
37 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
38
39 // Get the number of bytes to allocate from the FrameInfo.
40 uint64_t FrameSize = MFI.getStackSize();
41
42 // Get the alignment.
Sam Elliottcd44aee2019-08-08 14:40:54 +000043 unsigned StackAlign = getStackAlignment();
44 if (RI->needsStackRealignment(MF)) {
45 unsigned MaxStackAlign = std::max(StackAlign, MFI.getMaxAlignment());
46 FrameSize += (MaxStackAlign - StackAlign);
47 StackAlign = MaxStackAlign;
48 }
49
50 // Set Max Call Frame Size
51 uint64_t MaxCallSize = alignTo(MFI.getMaxCallFrameSize(), StackAlign);
52 MFI.setMaxCallFrameSize(MaxCallSize);
Alex Bradburyb014e3d2017-12-11 12:34:11 +000053
Alex Bradburyb014e3d2017-12-11 12:34:11 +000054 // Make sure the frame is aligned.
55 FrameSize = alignTo(FrameSize, StackAlign);
56
57 // Update frame info.
58 MFI.setStackSize(FrameSize);
59}
60
61void RISCVFrameLowering::adjustReg(MachineBasicBlock &MBB,
62 MachineBasicBlock::iterator MBBI,
Luis Marquesfa06e952019-08-16 14:27:50 +000063 const DebugLoc &DL, Register DestReg,
64 Register SrcReg, int64_t Val,
Alex Bradburyb014e3d2017-12-11 12:34:11 +000065 MachineInstr::MIFlag Flag) const {
Alex Bradbury9fea4882018-01-10 19:53:46 +000066 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Alex Bradburyb014e3d2017-12-11 12:34:11 +000067 const RISCVInstrInfo *TII = STI.getInstrInfo();
68
69 if (DestReg == SrcReg && Val == 0)
70 return;
71
Alex Bradbury9fea4882018-01-10 19:53:46 +000072 if (isInt<12>(Val)) {
73 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
74 .addReg(SrcReg)
75 .addImm(Val)
76 .setMIFlag(Flag);
77 } else if (isInt<32>(Val)) {
78 unsigned Opc = RISCV::ADD;
79 bool isSub = Val < 0;
80 if (isSub) {
81 Val = -Val;
82 Opc = RISCV::SUB;
83 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +000084
Daniel Sanders38368742019-08-12 22:41:02 +000085 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
Alex Bradbury9fea4882018-01-10 19:53:46 +000086 TII->movImm32(MBB, MBBI, DL, ScratchReg, Val, Flag);
87 BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
88 .addReg(SrcReg)
89 .addReg(ScratchReg, RegState::Kill)
90 .setMIFlag(Flag);
91 } else {
92 report_fatal_error("adjustReg cannot yet handle adjustments >32 bits");
93 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +000094}
95
96// Returns the register used to hold the frame pointer.
Luis Marquesfa06e952019-08-16 14:27:50 +000097static Register getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
Alex Bradburyb014e3d2017-12-11 12:34:11 +000098
99// Returns the register used to hold the stack pointer.
Luis Marquesfa06e952019-08-16 14:27:50 +0000100static Register getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000101
Alex Bradbury89718422017-10-19 21:37:38 +0000102void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000103 MachineBasicBlock &MBB) const {
104 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
105
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000106 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000107 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000108 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
109 const RISCVInstrInfo *TII = STI.getInstrInfo();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000110 MachineBasicBlock::iterator MBBI = MBB.begin();
111
Sam Elliottcd44aee2019-08-08 14:40:54 +0000112 if (RI->needsStackRealignment(MF) && MFI.hasVarSizedObjects()) {
113 report_fatal_error(
114 "RISC-V backend can't currently handle functions that need stack "
115 "realignment and have variable sized objects");
116 }
117
Luis Marquesfa06e952019-08-16 14:27:50 +0000118 Register FPReg = getFPReg(STI);
119 Register SPReg = getSPReg(STI);
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000120
121 // Debug location must be unknown since the first debug location is used
122 // to determine the end of the prologue.
123 DebugLoc DL;
124
125 // Determine the correct frame layout
126 determineFrameLayout(MF);
127
128 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
129 // investigation. Get the number of bytes to allocate from the FrameInfo.
130 uint64_t StackSize = MFI.getStackSize();
131
132 // Early exit if there is no need to allocate on the stack
133 if (StackSize == 0 && !MFI.adjustsStack())
134 return;
135
136 // Allocate space on the stack if necessary.
137 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
138
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000139 // Emit ".cfi_def_cfa_offset StackSize"
140 unsigned CFIIndex = MF.addFrameInst(
141 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
142 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
143 .addCFIIndex(CFIIndex);
144
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000145 // The frame pointer is callee-saved, and code has been generated for us to
146 // save it to the stack. We need to skip over the storing of callee-saved
147 // registers as the frame pointer must be modified after it has been saved
148 // to the stack, not before.
149 // FIXME: assumes exactly one instruction is used to save each callee-saved
150 // register.
151 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
152 std::advance(MBBI, CSI.size());
153
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000154 // Iterate over list of callee-saved registers and emit .cfi_offset
155 // directives.
156 for (const auto &Entry : CSI) {
157 int64_t Offset = MFI.getObjectOffset(Entry.getFrameIdx());
Luis Marquesfa06e952019-08-16 14:27:50 +0000158 Register Reg = Entry.getReg();
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000159 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
160 nullptr, RI->getDwarfRegNum(Reg, true), Offset));
161 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
162 .addCFIIndex(CFIIndex);
163 }
164
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000165 // Generate new FP.
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000166 if (hasFP(MF)) {
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000167 adjustReg(MBB, MBBI, DL, FPReg, SPReg,
168 StackSize - RVFI->getVarArgsSaveSize(), MachineInstr::FrameSetup);
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000169
170 // Emit ".cfi_def_cfa $fp, 0"
171 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
172 nullptr, RI->getDwarfRegNum(FPReg, true), 0));
173 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
174 .addCFIIndex(CFIIndex);
Sam Elliottcd44aee2019-08-08 14:40:54 +0000175
176 // Realign Stack
177 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
178 if (RI->needsStackRealignment(MF)) {
179 unsigned MaxAlignment = MFI.getMaxAlignment();
180
181 const RISCVInstrInfo *TII = STI.getInstrInfo();
182 if (isInt<12>(-(int)MaxAlignment)) {
183 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
184 .addReg(SPReg)
185 .addImm(-(int)MaxAlignment);
186 } else {
187 unsigned ShiftAmount = countTrailingZeros(MaxAlignment);
Luis Marquesfa06e952019-08-16 14:27:50 +0000188 Register VR =
Sam Elliottcd44aee2019-08-08 14:40:54 +0000189 MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
190 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
191 .addReg(SPReg)
192 .addImm(ShiftAmount);
193 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
194 .addReg(VR)
195 .addImm(ShiftAmount);
196 }
197 }
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000198 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000199}
Alex Bradbury89718422017-10-19 21:37:38 +0000200
201void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000202 MachineBasicBlock &MBB) const {
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000203 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
204 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
205 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000206 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000207 DebugLoc DL = MBBI->getDebugLoc();
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000208 const RISCVInstrInfo *TII = STI.getInstrInfo();
Luis Marquesfa06e952019-08-16 14:27:50 +0000209 Register FPReg = getFPReg(STI);
210 Register SPReg = getSPReg(STI);
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000211
212 // Skip to before the restores of callee-saved registers
213 // FIXME: assumes exactly one instruction is used to restore each
214 // callee-saved register.
Ana Pazos61b28ede72018-08-24 23:13:59 +0000215 auto LastFrameDestroy = std::prev(MBBI, MFI.getCalleeSavedInfo().size());
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000216
217 uint64_t StackSize = MFI.getStackSize();
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000218 uint64_t FPOffset = StackSize - RVFI->getVarArgsSaveSize();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000219
220 // Restore the stack pointer using the value of the frame pointer. Only
221 // necessary if the stack pointer was modified, meaning the stack size is
222 // unknown.
223 if (RI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) {
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000224 assert(hasFP(MF) && "frame pointer should not have been eliminated");
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000225 adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, -FPOffset,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000226 MachineInstr::FrameDestroy);
227 }
228
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000229 if (hasFP(MF)) {
230 // To find the instruction restoring FP from stack.
231 for (auto &I = LastFrameDestroy; I != MBBI; ++I) {
232 if (I->mayLoad() && I->getOperand(0).isReg()) {
Daniel Sanders38368742019-08-12 22:41:02 +0000233 Register DestReg = I->getOperand(0).getReg();
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000234 if (DestReg == FPReg) {
235 // If there is frame pointer, after restoring $fp registers, we
236 // need adjust CFA to ($sp - FPOffset).
237 // Emit ".cfi_def_cfa $sp, -FPOffset"
238 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
239 nullptr, RI->getDwarfRegNum(SPReg, true), -FPOffset));
240 BuildMI(MBB, std::next(I), DL,
241 TII->get(TargetOpcode::CFI_INSTRUCTION))
242 .addCFIIndex(CFIIndex);
243 break;
244 }
245 }
246 }
247 }
248
249 // Add CFI directives for callee-saved registers.
250 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
251 // Iterate over list of callee-saved registers and emit .cfi_restore
252 // directives.
253 for (const auto &Entry : CSI) {
Luis Marquesfa06e952019-08-16 14:27:50 +0000254 Register Reg = Entry.getReg();
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000255 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(
256 nullptr, RI->getDwarfRegNum(Reg, true)));
257 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
258 .addCFIIndex(CFIIndex);
259 }
260
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000261 // Deallocate stack
262 adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
Hsiangkai Wang04ddf392019-06-12 03:04:22 +0000263
264 // After restoring $sp, we need to adjust CFA to $(sp + 0)
265 // Emit ".cfi_def_cfa_offset 0"
266 unsigned CFIIndex =
267 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
268 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
269 .addCFIIndex(CFIIndex);
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000270}
Alex Bradbury660bcce2017-12-11 11:53:54 +0000271
272int RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF,
273 int FI,
274 unsigned &FrameReg) const {
275 const MachineFrameInfo &MFI = MF.getFrameInfo();
276 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000277 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000278
279 // Callee-saved registers should be referenced relative to the stack
280 // pointer (positive offset), otherwise use the frame pointer (negative
281 // offset).
282 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
283 int MinCSFI = 0;
284 int MaxCSFI = -1;
285
286 int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea() +
287 MFI.getOffsetAdjustment();
288
289 if (CSI.size()) {
290 MinCSFI = CSI[0].getFrameIdx();
291 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
292 }
293
Alex Bradbury660bcce2017-12-11 11:53:54 +0000294 if (FI >= MinCSFI && FI <= MaxCSFI) {
295 FrameReg = RISCV::X2;
296 Offset += MF.getFrameInfo().getStackSize();
Sam Elliottcd44aee2019-08-08 14:40:54 +0000297 } else if (RI->needsStackRealignment(MF)) {
298 assert(!MFI.hasVarSizedObjects() &&
299 "Unexpected combination of stack realignment and varsized objects");
300 // If the stack was realigned, the frame pointer is set in order to allow
301 // SP to be restored, but we still access stack objects using SP.
302 FrameReg = RISCV::X2;
303 Offset += MF.getFrameInfo().getStackSize();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000304 } else {
305 FrameReg = RI->getFrameRegister(MF);
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000306 if (hasFP(MF))
307 Offset += RVFI->getVarArgsSaveSize();
308 else
309 Offset += MF.getFrameInfo().getStackSize();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000310 }
311 return Offset;
312}
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000313
314void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
315 BitVector &SavedRegs,
316 RegScavenger *RS) const {
317 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000318 // Unconditionally spill RA and FP only if the function uses a frame
319 // pointer.
320 if (hasFP(MF)) {
321 SavedRegs.set(RISCV::X1);
322 SavedRegs.set(RISCV::X8);
323 }
Ana Pazos2e4106b2018-07-26 17:49:43 +0000324
325 // If interrupt is enabled and there are calls in the handler,
326 // unconditionally save all Caller-saved registers and
327 // all FP registers, regardless whether they are used.
328 MachineFrameInfo &MFI = MF.getFrameInfo();
329
330 if (MF.getFunction().hasFnAttribute("interrupt") && MFI.hasCalls()) {
331
332 static const MCPhysReg CSRegs[] = { RISCV::X1, /* ra */
333 RISCV::X5, RISCV::X6, RISCV::X7, /* t0-t2 */
334 RISCV::X10, RISCV::X11, /* a0-a1, a2-a7 */
335 RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17,
336 RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31, 0 /* t3-t6 */
337 };
338
339 for (unsigned i = 0; CSRegs[i]; ++i)
340 SavedRegs.set(CSRegs[i]);
341
342 if (MF.getSubtarget<RISCVSubtarget>().hasStdExtD() ||
343 MF.getSubtarget<RISCVSubtarget>().hasStdExtF()) {
344
345 // If interrupt is enabled, this list contains all FP registers.
346 const MCPhysReg * Regs = MF.getRegInfo().getCalleeSavedRegs();
347
348 for (unsigned i = 0; Regs[i]; ++i)
349 if (RISCV::FPR32RegClass.contains(Regs[i]) ||
350 RISCV::FPR64RegClass.contains(Regs[i]))
351 SavedRegs.set(Regs[i]);
352 }
353 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000354}
Alex Bradbury0715d352018-01-11 11:17:19 +0000355
356void RISCVFrameLowering::processFunctionBeforeFrameFinalized(
357 MachineFunction &MF, RegScavenger *RS) const {
358 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
359 MachineFrameInfo &MFI = MF.getFrameInfo();
360 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
361 // estimateStackSize has been observed to under-estimate the final stack
362 // size, so give ourselves wiggle-room by checking for stack size
363 // representable an 11-bit signed field rather than 12-bits.
364 // FIXME: It may be possible to craft a function with a small stack that
365 // still needs an emergency spill slot for branch relaxation. This case
366 // would currently be missed.
367 if (!isInt<11>(MFI.estimateStackSize(MF))) {
368 int RegScavFI = MFI.CreateStackObject(
369 RegInfo->getSpillSize(*RC), RegInfo->getSpillAlignment(*RC), false);
370 RS->addScavengingFrameIndex(RegScavFI);
371 }
372}
Shiva Chencbd498a2018-03-20 01:39:17 +0000373
374// Not preserve stack space within prologue for outgoing variables when the
375// function contains variable size objects and let eliminateCallFramePseudoInstr
376// preserve stack space for it.
377bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
378 return !MF.getFrameInfo().hasVarSizedObjects();
379}
380
381// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
382MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
383 MachineFunction &MF, MachineBasicBlock &MBB,
384 MachineBasicBlock::iterator MI) const {
Luis Marquesfa06e952019-08-16 14:27:50 +0000385 Register SPReg = RISCV::X2;
Shiva Chencbd498a2018-03-20 01:39:17 +0000386 DebugLoc DL = MI->getDebugLoc();
387
388 if (!hasReservedCallFrame(MF)) {
389 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
390 // ADJCALLSTACKUP must be converted to instructions manipulating the stack
391 // pointer. This is necessary when there is a variable length stack
392 // allocation (e.g. alloca), which means it's not possible to allocate
393 // space for outgoing arguments from within the function prologue.
394 int64_t Amount = MI->getOperand(0).getImm();
395
396 if (Amount != 0) {
397 // Ensure the stack remains aligned after adjustment.
398 Amount = alignSPAdjust(Amount);
399
400 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
401 Amount = -Amount;
402
403 adjustReg(MBB, MI, DL, SPReg, SPReg, Amount, MachineInstr::NoFlags);
404 }
405 }
406
407 return MBB.erase(MI);
408}