1 //===-- RISCVFrameLowering.cpp - RISCV Frame Information ------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the RISCV implementation of TargetFrameLowering class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "RISCVFrameLowering.h" 14 #include "RISCVMachineFunctionInfo.h" 15 #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" 20 #include "llvm/CodeGen/RegisterScavenging.h" 21 #include "llvm/IR/DiagnosticInfo.h" 22 #include "llvm/MC/MCDwarf.h" 23 24 using namespace llvm; 25 26 bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const { 27 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 28 29 const MachineFrameInfo &MFI = MF.getFrameInfo(); 30 return MF.getTarget().Options.DisableFramePointerElim(MF) || 31 RegInfo->needsStackRealignment(MF) || MFI.hasVarSizedObjects() || 32 MFI.isFrameAddressTaken(); 33 } 34 35 bool RISCVFrameLowering::hasBP(const MachineFunction &MF) const { 36 const MachineFrameInfo &MFI = MF.getFrameInfo(); 37 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 38 39 return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF); 40 } 41 42 // Determines the size of the frame and maximum call frame size. 43 void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const { 44 MachineFrameInfo &MFI = MF.getFrameInfo(); 45 const RISCVRegisterInfo *RI = STI.getRegisterInfo(); 46 47 // Get the number of bytes to allocate from the FrameInfo. 48 uint64_t FrameSize = MFI.getStackSize(); 49 50 // Get the alignment. 51 unsigned StackAlign = getStackAlignment(); 52 if (RI->needsStackRealignment(MF)) { 53 unsigned MaxStackAlign = std::max(StackAlign, MFI.getMaxAlignment()); 54 FrameSize += (MaxStackAlign - StackAlign); 55 StackAlign = MaxStackAlign; 56 } 57 58 // Set Max Call Frame Size 59 uint64_t MaxCallSize = alignTo(MFI.getMaxCallFrameSize(), StackAlign); 60 MFI.setMaxCallFrameSize(MaxCallSize); 61 62 // Make sure the frame is aligned. 63 FrameSize = alignTo(FrameSize, StackAlign); 64 65 // Update frame info. 66 MFI.setStackSize(FrameSize); 67 } 68 69 void RISCVFrameLowering::adjustReg(MachineBasicBlock &MBB, 70 MachineBasicBlock::iterator MBBI, 71 const DebugLoc &DL, Register DestReg, 72 Register SrcReg, int64_t Val, 73 MachineInstr::MIFlag Flag) const { 74 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 75 const RISCVInstrInfo *TII = STI.getInstrInfo(); 76 77 if (DestReg == SrcReg && Val == 0) 78 return; 79 80 if (isInt<12>(Val)) { 81 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg) 82 .addReg(SrcReg) 83 .addImm(Val) 84 .setMIFlag(Flag); 85 } else { 86 unsigned Opc = RISCV::ADD; 87 bool isSub = Val < 0; 88 if (isSub) { 89 Val = -Val; 90 Opc = RISCV::SUB; 91 } 92 93 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); 94 TII->movImm(MBB, MBBI, DL, ScratchReg, Val, Flag); 95 BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg) 96 .addReg(SrcReg) 97 .addReg(ScratchReg, RegState::Kill) 98 .setMIFlag(Flag); 99 } 100 } 101 102 // Returns the register used to hold the frame pointer. 103 static Register getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; } 104 105 // Returns the register used to hold the stack pointer. 106 static Register getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; } 107 108 void RISCVFrameLowering::emitPrologue(MachineFunction &MF, 109 MachineBasicBlock &MBB) const { 110 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 111 112 MachineFrameInfo &MFI = MF.getFrameInfo(); 113 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); 114 const RISCVRegisterInfo *RI = STI.getRegisterInfo(); 115 const RISCVInstrInfo *TII = STI.getInstrInfo(); 116 MachineBasicBlock::iterator MBBI = MBB.begin(); 117 118 Register FPReg = getFPReg(STI); 119 Register SPReg = getSPReg(STI); 120 Register BPReg = RISCVABI::getBPReg(); 121 122 // Debug location must be unknown since the first debug location is used 123 // to determine the end of the prologue. 124 DebugLoc DL; 125 126 // Determine the correct frame layout 127 determineFrameLayout(MF); 128 129 // FIXME (note copied from Lanai): This appears to be overallocating. Needs 130 // investigation. Get the number of bytes to allocate from the FrameInfo. 131 uint64_t StackSize = MFI.getStackSize(); 132 133 // Early exit if there is no need to allocate on the stack 134 if (StackSize == 0 && !MFI.adjustsStack()) 135 return; 136 137 // If the stack pointer has been marked as reserved, then produce an error if 138 // the frame requires stack allocation 139 if (STI.isRegisterReservedByUser(SPReg)) 140 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{ 141 MF.getFunction(), "Stack pointer required, but has been reserved."}); 142 143 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF); 144 // Split the SP adjustment to reduce the offsets of callee saved spill. 145 if (FirstSPAdjustAmount) 146 StackSize = FirstSPAdjustAmount; 147 148 // Allocate space on the stack if necessary. 149 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup); 150 151 // Emit ".cfi_def_cfa_offset StackSize" 152 unsigned CFIIndex = MF.addFrameInst( 153 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize)); 154 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) 155 .addCFIIndex(CFIIndex); 156 157 // The frame pointer is callee-saved, and code has been generated for us to 158 // save it to the stack. We need to skip over the storing of callee-saved 159 // registers as the frame pointer must be modified after it has been saved 160 // to the stack, not before. 161 // FIXME: assumes exactly one instruction is used to save each callee-saved 162 // register. 163 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 164 std::advance(MBBI, CSI.size()); 165 166 // Iterate over list of callee-saved registers and emit .cfi_offset 167 // directives. 168 for (const auto &Entry : CSI) { 169 int64_t Offset = MFI.getObjectOffset(Entry.getFrameIdx()); 170 Register Reg = Entry.getReg(); 171 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 172 nullptr, RI->getDwarfRegNum(Reg, true), Offset)); 173 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) 174 .addCFIIndex(CFIIndex); 175 } 176 177 // Generate new FP. 178 if (hasFP(MF)) { 179 if (STI.isRegisterReservedByUser(FPReg)) 180 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{ 181 MF.getFunction(), "Frame pointer required, but has been reserved."}); 182 183 adjustReg(MBB, MBBI, DL, FPReg, SPReg, 184 StackSize - RVFI->getVarArgsSaveSize(), MachineInstr::FrameSetup); 185 186 // Emit ".cfi_def_cfa $fp, 0" 187 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa( 188 nullptr, RI->getDwarfRegNum(FPReg, true), 0)); 189 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) 190 .addCFIIndex(CFIIndex); 191 } 192 193 // Emit the second SP adjustment after saving callee saved registers. 194 if (FirstSPAdjustAmount) { 195 uint64_t SecondSPAdjustAmount = MFI.getStackSize() - FirstSPAdjustAmount; 196 assert(SecondSPAdjustAmount > 0 && 197 "SecondSPAdjustAmount should be greater than zero"); 198 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount, 199 MachineInstr::FrameSetup); 200 201 // If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0", 202 // don't emit an sp-based .cfi_def_cfa_offset 203 if (!hasFP(MF)) { 204 // Emit ".cfi_def_cfa_offset StackSize" 205 unsigned CFIIndex = MF.addFrameInst( 206 MCCFIInstruction::createDefCfaOffset(nullptr, -MFI.getStackSize())); 207 BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) 208 .addCFIIndex(CFIIndex); 209 } 210 } 211 212 if (hasFP(MF)) { 213 // Realign Stack 214 const RISCVRegisterInfo *RI = STI.getRegisterInfo(); 215 if (RI->needsStackRealignment(MF)) { 216 unsigned MaxAlignment = MFI.getMaxAlignment(); 217 218 const RISCVInstrInfo *TII = STI.getInstrInfo(); 219 if (isInt<12>(-(int)MaxAlignment)) { 220 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg) 221 .addReg(SPReg) 222 .addImm(-(int)MaxAlignment); 223 } else { 224 unsigned ShiftAmount = countTrailingZeros(MaxAlignment); 225 Register VR = 226 MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass); 227 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR) 228 .addReg(SPReg) 229 .addImm(ShiftAmount); 230 BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg) 231 .addReg(VR) 232 .addImm(ShiftAmount); 233 } 234 // FP will be used to restore the frame in the epilogue, so we need 235 // another base register BP to record SP after re-alignment. SP will 236 // track the current stack after allocating variable sized objects. 237 if (hasBP(MF)) { 238 // move BP, SP 239 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg) 240 .addReg(SPReg) 241 .addImm(0); 242 } 243 } 244 } 245 } 246 247 void RISCVFrameLowering::emitEpilogue(MachineFunction &MF, 248 MachineBasicBlock &MBB) const { 249 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 250 const RISCVRegisterInfo *RI = STI.getRegisterInfo(); 251 MachineFrameInfo &MFI = MF.getFrameInfo(); 252 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); 253 DebugLoc DL = MBBI->getDebugLoc(); 254 Register FPReg = getFPReg(STI); 255 Register SPReg = getSPReg(STI); 256 257 // Skip to before the restores of callee-saved registers 258 // FIXME: assumes exactly one instruction is used to restore each 259 // callee-saved register. 260 auto LastFrameDestroy = std::prev(MBBI, MFI.getCalleeSavedInfo().size()); 261 262 uint64_t StackSize = MFI.getStackSize(); 263 uint64_t FPOffset = StackSize - RVFI->getVarArgsSaveSize(); 264 265 // Restore the stack pointer using the value of the frame pointer. Only 266 // necessary if the stack pointer was modified, meaning the stack size is 267 // unknown. 268 if (RI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) { 269 assert(hasFP(MF) && "frame pointer should not have been eliminated"); 270 adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, -FPOffset, 271 MachineInstr::FrameDestroy); 272 } 273 274 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF); 275 if (FirstSPAdjustAmount) { 276 uint64_t SecondSPAdjustAmount = MFI.getStackSize() - FirstSPAdjustAmount; 277 assert(SecondSPAdjustAmount > 0 && 278 "SecondSPAdjustAmount should be greater than zero"); 279 280 adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg, SecondSPAdjustAmount, 281 MachineInstr::FrameDestroy); 282 } 283 284 if (FirstSPAdjustAmount) 285 StackSize = FirstSPAdjustAmount; 286 287 // Deallocate stack 288 adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy); 289 } 290 291 int RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, 292 int FI, 293 unsigned &FrameReg) const { 294 const MachineFrameInfo &MFI = MF.getFrameInfo(); 295 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); 296 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); 297 298 // Callee-saved registers should be referenced relative to the stack 299 // pointer (positive offset), otherwise use the frame pointer (negative 300 // offset). 301 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 302 int MinCSFI = 0; 303 int MaxCSFI = -1; 304 305 int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea() + 306 MFI.getOffsetAdjustment(); 307 308 uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF); 309 310 if (CSI.size()) { 311 MinCSFI = CSI[0].getFrameIdx(); 312 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx(); 313 } 314 315 if (FI >= MinCSFI && FI <= MaxCSFI) { 316 FrameReg = RISCV::X2; 317 318 if (FirstSPAdjustAmount) 319 Offset += FirstSPAdjustAmount; 320 else 321 Offset += MF.getFrameInfo().getStackSize(); 322 } else if (RI->needsStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) { 323 // If the stack was realigned, the frame pointer is set in order to allow 324 // SP to be restored, so we need another base register to record the stack 325 // after realignment. 326 if (hasBP(MF)) 327 FrameReg = RISCVABI::getBPReg(); 328 else 329 FrameReg = RISCV::X2; 330 Offset += MF.getFrameInfo().getStackSize(); 331 } else { 332 FrameReg = RI->getFrameRegister(MF); 333 if (hasFP(MF)) 334 Offset += RVFI->getVarArgsSaveSize(); 335 else 336 Offset += MF.getFrameInfo().getStackSize(); 337 } 338 return Offset; 339 } 340 341 void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF, 342 BitVector &SavedRegs, 343 RegScavenger *RS) const { 344 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 345 // Unconditionally spill RA and FP only if the function uses a frame 346 // pointer. 347 if (hasFP(MF)) { 348 SavedRegs.set(RISCV::X1); 349 SavedRegs.set(RISCV::X8); 350 } 351 // Mark BP as used if function has dedicated base pointer. 352 if (hasBP(MF)) 353 SavedRegs.set(RISCVABI::getBPReg()); 354 355 // If interrupt is enabled and there are calls in the handler, 356 // unconditionally save all Caller-saved registers and 357 // all FP registers, regardless whether they are used. 358 MachineFrameInfo &MFI = MF.getFrameInfo(); 359 360 if (MF.getFunction().hasFnAttribute("interrupt") && MFI.hasCalls()) { 361 362 static const MCPhysReg CSRegs[] = { RISCV::X1, /* ra */ 363 RISCV::X5, RISCV::X6, RISCV::X7, /* t0-t2 */ 364 RISCV::X10, RISCV::X11, /* a0-a1, a2-a7 */ 365 RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17, 366 RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31, 0 /* t3-t6 */ 367 }; 368 369 for (unsigned i = 0; CSRegs[i]; ++i) 370 SavedRegs.set(CSRegs[i]); 371 372 if (MF.getSubtarget<RISCVSubtarget>().hasStdExtD() || 373 MF.getSubtarget<RISCVSubtarget>().hasStdExtF()) { 374 375 // If interrupt is enabled, this list contains all FP registers. 376 const MCPhysReg * Regs = MF.getRegInfo().getCalleeSavedRegs(); 377 378 for (unsigned i = 0; Regs[i]; ++i) 379 if (RISCV::FPR32RegClass.contains(Regs[i]) || 380 RISCV::FPR64RegClass.contains(Regs[i])) 381 SavedRegs.set(Regs[i]); 382 } 383 } 384 } 385 386 void RISCVFrameLowering::processFunctionBeforeFrameFinalized( 387 MachineFunction &MF, RegScavenger *RS) const { 388 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 389 MachineFrameInfo &MFI = MF.getFrameInfo(); 390 const TargetRegisterClass *RC = &RISCV::GPRRegClass; 391 // estimateStackSize has been observed to under-estimate the final stack 392 // size, so give ourselves wiggle-room by checking for stack size 393 // representable an 11-bit signed field rather than 12-bits. 394 // FIXME: It may be possible to craft a function with a small stack that 395 // still needs an emergency spill slot for branch relaxation. This case 396 // would currently be missed. 397 if (!isInt<11>(MFI.estimateStackSize(MF))) { 398 int RegScavFI = MFI.CreateStackObject( 399 RegInfo->getSpillSize(*RC), RegInfo->getSpillAlignment(*RC), false); 400 RS->addScavengingFrameIndex(RegScavFI); 401 } 402 } 403 404 // Not preserve stack space within prologue for outgoing variables when the 405 // function contains variable size objects and let eliminateCallFramePseudoInstr 406 // preserve stack space for it. 407 bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 408 return !MF.getFrameInfo().hasVarSizedObjects(); 409 } 410 411 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions. 412 MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr( 413 MachineFunction &MF, MachineBasicBlock &MBB, 414 MachineBasicBlock::iterator MI) const { 415 Register SPReg = RISCV::X2; 416 DebugLoc DL = MI->getDebugLoc(); 417 418 if (!hasReservedCallFrame(MF)) { 419 // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and 420 // ADJCALLSTACKUP must be converted to instructions manipulating the stack 421 // pointer. This is necessary when there is a variable length stack 422 // allocation (e.g. alloca), which means it's not possible to allocate 423 // space for outgoing arguments from within the function prologue. 424 int64_t Amount = MI->getOperand(0).getImm(); 425 426 if (Amount != 0) { 427 // Ensure the stack remains aligned after adjustment. 428 Amount = alignSPAdjust(Amount); 429 430 if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN) 431 Amount = -Amount; 432 433 adjustReg(MBB, MI, DL, SPReg, SPReg, Amount, MachineInstr::NoFlags); 434 } 435 } 436 437 return MBB.erase(MI); 438 } 439 440 // We would like to split the SP adjustment to reduce prologue/epilogue 441 // as following instructions. In this way, the offset of the callee saved 442 // register could fit in a single store. 443 // add sp,sp,-2032 444 // sw ra,2028(sp) 445 // sw s0,2024(sp) 446 // sw s1,2020(sp) 447 // sw s3,2012(sp) 448 // sw s4,2008(sp) 449 // add sp,sp,-64 450 uint64_t 451 RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF) const { 452 const MachineFrameInfo &MFI = MF.getFrameInfo(); 453 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 454 uint64_t StackSize = MFI.getStackSize(); 455 uint64_t StackAlign = getStackAlignment(); 456 457 // FIXME: Disable SplitSPAdjust if save-restore libcall enabled when the patch 458 // landing. The callee saved registers will be pushed by the 459 // save-restore libcalls, so we don't have to split the SP adjustment 460 // in this case. 461 // 462 // Return the FirstSPAdjustAmount if the StackSize can not fit in signed 463 // 12-bit and there exists a callee saved register need to be pushed. 464 if (!isInt<12>(StackSize) && (CSI.size() > 0)) { 465 // FirstSPAdjustAmount is choosed as (2048 - StackAlign) 466 // because 2048 will cause sp = sp + 2048 in epilogue split into 467 // multi-instructions. The offset smaller than 2048 can fit in signle 468 // load/store instruction and we have to stick with the stack alignment. 469 // 2048 is 16-byte alignment. The stack alignment for RV32 and RV64 is 16, 470 // for RV32E is 4. So (2048 - StackAlign) will satisfy the stack alignment. 471 return 2048 - StackAlign; 472 } 473 return 0; 474 } 475