RISCVFrameLowering.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. #include "RISCVFrameLowering.h"
  13. #include "RISCVMachineFunctionInfo.h"
  14. #include "RISCVSubtarget.h"
  15. #include "llvm/BinaryFormat/Dwarf.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. #include "llvm/Support/LEB128.h"
  24. #include <algorithm>
  25. using namespace llvm;
  26. // For now we use x18, a.k.a s2, as pointer to shadow call stack.
  27. // User should explicitly set -ffixed-x18 and not use x18 in their asm.
  28. static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
  29. MachineBasicBlock::iterator MI,
  30. const DebugLoc &DL) {
  31. if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
  32. return;
  33. const auto &STI = MF.getSubtarget<RISCVSubtarget>();
  34. Register RAReg = STI.getRegisterInfo()->getRARegister();
  35. // Do not save RA to the SCS if it's not saved to the regular stack,
  36. // i.e. RA is not at risk of being overwritten.
  37. std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
  38. if (llvm::none_of(
  39. CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
  40. return;
  41. Register SCSPReg = RISCVABI::getSCSPReg();
  42. auto &Ctx = MF.getFunction().getContext();
  43. if (!STI.isRegisterReservedByUser(SCSPReg)) {
  44. Ctx.diagnose(DiagnosticInfoUnsupported{
  45. MF.getFunction(), "x18 not reserved by user for Shadow Call Stack."});
  46. return;
  47. }
  48. const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  49. if (RVFI->useSaveRestoreLibCalls(MF)) {
  50. Ctx.diagnose(DiagnosticInfoUnsupported{
  51. MF.getFunction(),
  52. "Shadow Call Stack cannot be combined with Save/Restore LibCalls."});
  53. return;
  54. }
  55. const RISCVInstrInfo *TII = STI.getInstrInfo();
  56. bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
  57. int64_t SlotSize = STI.getXLen() / 8;
  58. // Store return address to shadow call stack
  59. // s[w|d] ra, 0(s2)
  60. // addi s2, s2, [4|8]
  61. BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::SD : RISCV::SW))
  62. .addReg(RAReg)
  63. .addReg(SCSPReg)
  64. .addImm(0)
  65. .setMIFlag(MachineInstr::FrameSetup);
  66. BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
  67. .addReg(SCSPReg, RegState::Define)
  68. .addReg(SCSPReg)
  69. .addImm(SlotSize)
  70. .setMIFlag(MachineInstr::FrameSetup);
  71. }
  72. static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
  73. MachineBasicBlock::iterator MI,
  74. const DebugLoc &DL) {
  75. if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
  76. return;
  77. const auto &STI = MF.getSubtarget<RISCVSubtarget>();
  78. Register RAReg = STI.getRegisterInfo()->getRARegister();
  79. // See emitSCSPrologue() above.
  80. std::vector<CalleeSavedInfo> &CSI = MF.getFrameInfo().getCalleeSavedInfo();
  81. if (llvm::none_of(
  82. CSI, [&](CalleeSavedInfo &CSR) { return CSR.getReg() == RAReg; }))
  83. return;
  84. Register SCSPReg = RISCVABI::getSCSPReg();
  85. auto &Ctx = MF.getFunction().getContext();
  86. if (!STI.isRegisterReservedByUser(SCSPReg)) {
  87. Ctx.diagnose(DiagnosticInfoUnsupported{
  88. MF.getFunction(), "x18 not reserved by user for Shadow Call Stack."});
  89. return;
  90. }
  91. const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  92. if (RVFI->useSaveRestoreLibCalls(MF)) {
  93. Ctx.diagnose(DiagnosticInfoUnsupported{
  94. MF.getFunction(),
  95. "Shadow Call Stack cannot be combined with Save/Restore LibCalls."});
  96. return;
  97. }
  98. const RISCVInstrInfo *TII = STI.getInstrInfo();
  99. bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
  100. int64_t SlotSize = STI.getXLen() / 8;
  101. // Load return address from shadow call stack
  102. // l[w|d] ra, -[4|8](s2)
  103. // addi s2, s2, -[4|8]
  104. BuildMI(MBB, MI, DL, TII->get(IsRV64 ? RISCV::LD : RISCV::LW))
  105. .addReg(RAReg, RegState::Define)
  106. .addReg(SCSPReg)
  107. .addImm(-SlotSize)
  108. .setMIFlag(MachineInstr::FrameDestroy);
  109. BuildMI(MBB, MI, DL, TII->get(RISCV::ADDI))
  110. .addReg(SCSPReg, RegState::Define)
  111. .addReg(SCSPReg)
  112. .addImm(-SlotSize)
  113. .setMIFlag(MachineInstr::FrameDestroy);
  114. }
  115. // Get the ID of the libcall used for spilling and restoring callee saved
  116. // registers. The ID is representative of the number of registers saved or
  117. // restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
  118. // single register.
  119. static int getLibCallID(const MachineFunction &MF,
  120. const std::vector<CalleeSavedInfo> &CSI) {
  121. const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  122. if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
  123. return -1;
  124. Register MaxReg = RISCV::NoRegister;
  125. for (auto &CS : CSI)
  126. // RISCVRegisterInfo::hasReservedSpillSlot assigns negative frame indexes to
  127. // registers which can be saved by libcall.
  128. if (CS.getFrameIdx() < 0)
  129. MaxReg = std::max(MaxReg.id(), CS.getReg().id());
  130. if (MaxReg == RISCV::NoRegister)
  131. return -1;
  132. switch (MaxReg) {
  133. default:
  134. llvm_unreachable("Something has gone wrong!");
  135. case /*s11*/ RISCV::X27: return 12;
  136. case /*s10*/ RISCV::X26: return 11;
  137. case /*s9*/ RISCV::X25: return 10;
  138. case /*s8*/ RISCV::X24: return 9;
  139. case /*s7*/ RISCV::X23: return 8;
  140. case /*s6*/ RISCV::X22: return 7;
  141. case /*s5*/ RISCV::X21: return 6;
  142. case /*s4*/ RISCV::X20: return 5;
  143. case /*s3*/ RISCV::X19: return 4;
  144. case /*s2*/ RISCV::X18: return 3;
  145. case /*s1*/ RISCV::X9: return 2;
  146. case /*s0*/ RISCV::X8: return 1;
  147. case /*ra*/ RISCV::X1: return 0;
  148. }
  149. }
  150. // Get the name of the libcall used for spilling callee saved registers.
  151. // If this function will not use save/restore libcalls, then return a nullptr.
  152. static const char *
  153. getSpillLibCallName(const MachineFunction &MF,
  154. const std::vector<CalleeSavedInfo> &CSI) {
  155. static const char *const SpillLibCalls[] = {
  156. "__riscv_save_0",
  157. "__riscv_save_1",
  158. "__riscv_save_2",
  159. "__riscv_save_3",
  160. "__riscv_save_4",
  161. "__riscv_save_5",
  162. "__riscv_save_6",
  163. "__riscv_save_7",
  164. "__riscv_save_8",
  165. "__riscv_save_9",
  166. "__riscv_save_10",
  167. "__riscv_save_11",
  168. "__riscv_save_12"
  169. };
  170. int LibCallID = getLibCallID(MF, CSI);
  171. if (LibCallID == -1)
  172. return nullptr;
  173. return SpillLibCalls[LibCallID];
  174. }
  175. // Get the name of the libcall used for restoring callee saved registers.
  176. // If this function will not use save/restore libcalls, then return a nullptr.
  177. static const char *
  178. getRestoreLibCallName(const MachineFunction &MF,
  179. const std::vector<CalleeSavedInfo> &CSI) {
  180. static const char *const RestoreLibCalls[] = {
  181. "__riscv_restore_0",
  182. "__riscv_restore_1",
  183. "__riscv_restore_2",
  184. "__riscv_restore_3",
  185. "__riscv_restore_4",
  186. "__riscv_restore_5",
  187. "__riscv_restore_6",
  188. "__riscv_restore_7",
  189. "__riscv_restore_8",
  190. "__riscv_restore_9",
  191. "__riscv_restore_10",
  192. "__riscv_restore_11",
  193. "__riscv_restore_12"
  194. };
  195. int LibCallID = getLibCallID(MF, CSI);
  196. if (LibCallID == -1)
  197. return nullptr;
  198. return RestoreLibCalls[LibCallID];
  199. }
  200. // Return true if the specified function should have a dedicated frame
  201. // pointer register. This is true if frame pointer elimination is
  202. // disabled, if it needs dynamic stack realignment, if the function has
  203. // variable sized allocas, or if the frame address is taken.
  204. bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const {
  205. const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
  206. const MachineFrameInfo &MFI = MF.getFrameInfo();
  207. return MF.getTarget().Options.DisableFramePointerElim(MF) ||
  208. RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
  209. MFI.isFrameAddressTaken();
  210. }
  211. bool RISCVFrameLowering::hasBP(const MachineFunction &MF) const {
  212. const MachineFrameInfo &MFI = MF.getFrameInfo();
  213. const TargetRegisterInfo *TRI = STI.getRegisterInfo();
  214. // If we do not reserve stack space for outgoing arguments in prologue,
  215. // we will adjust the stack pointer before call instruction. After the
  216. // adjustment, we can not use SP to access the stack objects for the
  217. // arguments. Instead, use BP to access these stack objects.
  218. return (MFI.hasVarSizedObjects() ||
  219. (!hasReservedCallFrame(MF) && (!MFI.isMaxCallFrameSizeComputed() ||
  220. MFI.getMaxCallFrameSize() != 0))) &&
  221. TRI->hasStackRealignment(MF);
  222. }
  223. // Determines the size of the frame and maximum call frame size.
  224. void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
  225. MachineFrameInfo &MFI = MF.getFrameInfo();
  226. auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  227. // Get the number of bytes to allocate from the FrameInfo.
  228. uint64_t FrameSize = MFI.getStackSize();
  229. // Get the alignment.
  230. Align StackAlign = getStackAlign();
  231. // Make sure the frame is aligned.
  232. FrameSize = alignTo(FrameSize, StackAlign);
  233. // Update frame info.
  234. MFI.setStackSize(FrameSize);
  235. // When using SP or BP to access stack objects, we may require extra padding
  236. // to ensure the bottom of the RVV stack is correctly aligned within the main
  237. // stack. We calculate this as the amount required to align the scalar local
  238. // variable section up to the RVV alignment.
  239. const TargetRegisterInfo *TRI = STI.getRegisterInfo();
  240. if (RVFI->getRVVStackSize() && (!hasFP(MF) || TRI->hasStackRealignment(MF))) {
  241. int ScalarLocalVarSize = FrameSize - RVFI->getCalleeSavedStackSize() -
  242. RVFI->getVarArgsSaveSize();
  243. if (auto RVVPadding =
  244. offsetToAlignment(ScalarLocalVarSize, RVFI->getRVVStackAlign()))
  245. RVFI->setRVVPadding(RVVPadding);
  246. }
  247. }
  248. // Returns the stack size including RVV padding (when required), rounded back
  249. // up to the required stack alignment.
  250. uint64_t RISCVFrameLowering::getStackSizeWithRVVPadding(
  251. const MachineFunction &MF) const {
  252. const MachineFrameInfo &MFI = MF.getFrameInfo();
  253. auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  254. return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
  255. }
  256. // Returns the register used to hold the frame pointer.
  257. static Register getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
  258. // Returns the register used to hold the stack pointer.
  259. static Register getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
  260. static SmallVector<CalleeSavedInfo, 8>
  261. getNonLibcallCSI(const MachineFunction &MF,
  262. const std::vector<CalleeSavedInfo> &CSI) {
  263. const MachineFrameInfo &MFI = MF.getFrameInfo();
  264. SmallVector<CalleeSavedInfo, 8> NonLibcallCSI;
  265. for (auto &CS : CSI) {
  266. int FI = CS.getFrameIdx();
  267. if (FI >= 0 && MFI.getStackID(FI) == TargetStackID::Default)
  268. NonLibcallCSI.push_back(CS);
  269. }
  270. return NonLibcallCSI;
  271. }
  272. void RISCVFrameLowering::adjustStackForRVV(MachineFunction &MF,
  273. MachineBasicBlock &MBB,
  274. MachineBasicBlock::iterator MBBI,
  275. const DebugLoc &DL, int64_t Amount,
  276. MachineInstr::MIFlag Flag) const {
  277. assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
  278. const Register SPReg = getSPReg(STI);
  279. // Optimize compile time offset case
  280. StackOffset Offset = StackOffset::getScalable(Amount);
  281. if (STI.getRealMinVLen() == STI.getRealMaxVLen()) {
  282. // 1. Multiply the number of v-slots by the (constant) length of register
  283. const int64_t VLENB = STI.getRealMinVLen() / 8;
  284. assert(Amount % 8 == 0 &&
  285. "Reserve the stack by the multiple of one vector size.");
  286. const int64_t NumOfVReg = Amount / 8;
  287. const int64_t FixedOffset = NumOfVReg * VLENB;
  288. if (!isInt<32>(FixedOffset)) {
  289. report_fatal_error(
  290. "Frame size outside of the signed 32-bit range not supported");
  291. }
  292. Offset = StackOffset::getFixed(FixedOffset);
  293. }
  294. const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
  295. // We must keep the stack pointer aligned through any intermediate
  296. // updates.
  297. RI.adjustReg(MBB, MBBI, DL, SPReg, SPReg, Offset,
  298. Flag, getStackAlign());
  299. }
  300. static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI,
  301. Register Reg,
  302. uint64_t FixedOffset,
  303. uint64_t ScalableOffset) {
  304. assert(ScalableOffset != 0 && "Did not need to adjust CFA for RVV");
  305. SmallString<64> Expr;
  306. std::string CommentBuffer;
  307. llvm::raw_string_ostream Comment(CommentBuffer);
  308. // Build up the expression (Reg + FixedOffset + ScalableOffset * VLENB).
  309. unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
  310. Expr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
  311. Expr.push_back(0);
  312. if (Reg == RISCV::X2)
  313. Comment << "sp";
  314. else
  315. Comment << printReg(Reg, &TRI);
  316. uint8_t buffer[16];
  317. if (FixedOffset) {
  318. Expr.push_back(dwarf::DW_OP_consts);
  319. Expr.append(buffer, buffer + encodeSLEB128(FixedOffset, buffer));
  320. Expr.push_back((uint8_t)dwarf::DW_OP_plus);
  321. Comment << " + " << FixedOffset;
  322. }
  323. Expr.push_back((uint8_t)dwarf::DW_OP_consts);
  324. Expr.append(buffer, buffer + encodeSLEB128(ScalableOffset, buffer));
  325. unsigned DwarfVlenb = TRI.getDwarfRegNum(RISCV::VLENB, true);
  326. Expr.push_back((uint8_t)dwarf::DW_OP_bregx);
  327. Expr.append(buffer, buffer + encodeULEB128(DwarfVlenb, buffer));
  328. Expr.push_back(0);
  329. Expr.push_back((uint8_t)dwarf::DW_OP_mul);
  330. Expr.push_back((uint8_t)dwarf::DW_OP_plus);
  331. Comment << " + " << ScalableOffset << " * vlenb";
  332. SmallString<64> DefCfaExpr;
  333. DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
  334. DefCfaExpr.append(buffer, buffer + encodeULEB128(Expr.size(), buffer));
  335. DefCfaExpr.append(Expr.str());
  336. return MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str(),
  337. Comment.str());
  338. }
  339. void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
  340. MachineBasicBlock &MBB) const {
  341. MachineFrameInfo &MFI = MF.getFrameInfo();
  342. auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  343. const RISCVRegisterInfo *RI = STI.getRegisterInfo();
  344. const RISCVInstrInfo *TII = STI.getInstrInfo();
  345. MachineBasicBlock::iterator MBBI = MBB.begin();
  346. Register FPReg = getFPReg(STI);
  347. Register SPReg = getSPReg(STI);
  348. Register BPReg = RISCVABI::getBPReg();
  349. // Debug location must be unknown since the first debug location is used
  350. // to determine the end of the prologue.
  351. DebugLoc DL;
  352. // All calls are tail calls in GHC calling conv, and functions have no
  353. // prologue/epilogue.
  354. if (MF.getFunction().getCallingConv() == CallingConv::GHC)
  355. return;
  356. // Emit prologue for shadow call stack.
  357. emitSCSPrologue(MF, MBB, MBBI, DL);
  358. // Since spillCalleeSavedRegisters may have inserted a libcall, skip past
  359. // any instructions marked as FrameSetup
  360. while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
  361. ++MBBI;
  362. // Determine the correct frame layout
  363. determineFrameLayout(MF);
  364. // If libcalls are used to spill and restore callee-saved registers, the frame
  365. // has two sections; the opaque section managed by the libcalls, and the
  366. // section managed by MachineFrameInfo which can also hold callee saved
  367. // registers in fixed stack slots, both of which have negative frame indices.
  368. // This gets even more complicated when incoming arguments are passed via the
  369. // stack, as these too have negative frame indices. An example is detailed
  370. // below:
  371. //
  372. // | incoming arg | <- FI[-3]
  373. // | libcallspill |
  374. // | calleespill | <- FI[-2]
  375. // | calleespill | <- FI[-1]
  376. // | this_frame | <- FI[0]
  377. //
  378. // For negative frame indices, the offset from the frame pointer will differ
  379. // depending on which of these groups the frame index applies to.
  380. // The following calculates the correct offset knowing the number of callee
  381. // saved registers spilt by the two methods.
  382. if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
  383. // Calculate the size of the frame managed by the libcall. The libcalls are
  384. // implemented such that the stack will always be 16 byte aligned.
  385. unsigned LibCallFrameSize = alignTo((STI.getXLen() / 8) * LibCallRegs, 16);
  386. RVFI->setLibCallStackSize(LibCallFrameSize);
  387. }
  388. // FIXME (note copied from Lanai): This appears to be overallocating. Needs
  389. // investigation. Get the number of bytes to allocate from the FrameInfo.
  390. uint64_t StackSize = getStackSizeWithRVVPadding(MF);
  391. uint64_t RealStackSize = StackSize + RVFI->getLibCallStackSize();
  392. uint64_t RVVStackSize = RVFI->getRVVStackSize();
  393. // Early exit if there is no need to allocate on the stack
  394. if (RealStackSize == 0 && !MFI.adjustsStack() && RVVStackSize == 0)
  395. return;
  396. // If the stack pointer has been marked as reserved, then produce an error if
  397. // the frame requires stack allocation
  398. if (STI.isRegisterReservedByUser(SPReg))
  399. MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
  400. MF.getFunction(), "Stack pointer required, but has been reserved."});
  401. uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
  402. // Split the SP adjustment to reduce the offsets of callee saved spill.
  403. if (FirstSPAdjustAmount) {
  404. StackSize = FirstSPAdjustAmount;
  405. RealStackSize = FirstSPAdjustAmount;
  406. }
  407. // Allocate space on the stack if necessary.
  408. RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(-StackSize),
  409. MachineInstr::FrameSetup, getStackAlign());
  410. // Emit ".cfi_def_cfa_offset RealStackSize"
  411. unsigned CFIIndex = MF.addFrameInst(
  412. MCCFIInstruction::cfiDefCfaOffset(nullptr, RealStackSize));
  413. BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
  414. .addCFIIndex(CFIIndex)
  415. .setMIFlag(MachineInstr::FrameSetup);
  416. const auto &CSI = MFI.getCalleeSavedInfo();
  417. // The frame pointer is callee-saved, and code has been generated for us to
  418. // save it to the stack. We need to skip over the storing of callee-saved
  419. // registers as the frame pointer must be modified after it has been saved
  420. // to the stack, not before.
  421. // FIXME: assumes exactly one instruction is used to save each callee-saved
  422. // register.
  423. std::advance(MBBI, getNonLibcallCSI(MF, CSI).size());
  424. // Iterate over list of callee-saved registers and emit .cfi_offset
  425. // directives.
  426. for (const auto &Entry : CSI) {
  427. int FrameIdx = Entry.getFrameIdx();
  428. int64_t Offset;
  429. // Offsets for objects with fixed locations (IE: those saved by libcall) are
  430. // simply calculated from the frame index.
  431. if (FrameIdx < 0)
  432. Offset = FrameIdx * (int64_t) STI.getXLen() / 8;
  433. else
  434. Offset = MFI.getObjectOffset(Entry.getFrameIdx()) -
  435. RVFI->getLibCallStackSize();
  436. Register Reg = Entry.getReg();
  437. unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
  438. nullptr, RI->getDwarfRegNum(Reg, true), Offset));
  439. BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
  440. .addCFIIndex(CFIIndex)
  441. .setMIFlag(MachineInstr::FrameSetup);
  442. }
  443. // Generate new FP.
  444. if (hasFP(MF)) {
  445. if (STI.isRegisterReservedByUser(FPReg))
  446. MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
  447. MF.getFunction(), "Frame pointer required, but has been reserved."});
  448. // The frame pointer does need to be reserved from register allocation.
  449. assert(MF.getRegInfo().isReserved(FPReg) && "FP not reserved");
  450. RI->adjustReg(MBB, MBBI, DL, FPReg, SPReg,
  451. StackOffset::getFixed(RealStackSize - RVFI->getVarArgsSaveSize()),
  452. MachineInstr::FrameSetup, getStackAlign());
  453. // Emit ".cfi_def_cfa $fp, RVFI->getVarArgsSaveSize()"
  454. unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
  455. nullptr, RI->getDwarfRegNum(FPReg, true), RVFI->getVarArgsSaveSize()));
  456. BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
  457. .addCFIIndex(CFIIndex)
  458. .setMIFlag(MachineInstr::FrameSetup);
  459. }
  460. // Emit the second SP adjustment after saving callee saved registers.
  461. if (FirstSPAdjustAmount) {
  462. uint64_t SecondSPAdjustAmount =
  463. getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
  464. assert(SecondSPAdjustAmount > 0 &&
  465. "SecondSPAdjustAmount should be greater than zero");
  466. RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg,
  467. StackOffset::getFixed(-SecondSPAdjustAmount),
  468. MachineInstr::FrameSetup, getStackAlign());
  469. // If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0",
  470. // don't emit an sp-based .cfi_def_cfa_offset
  471. if (!hasFP(MF)) {
  472. // Emit ".cfi_def_cfa_offset StackSize"
  473. unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(
  474. nullptr, getStackSizeWithRVVPadding(MF)));
  475. BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
  476. .addCFIIndex(CFIIndex)
  477. .setMIFlag(MachineInstr::FrameSetup);
  478. }
  479. }
  480. if (RVVStackSize) {
  481. adjustStackForRVV(MF, MBB, MBBI, DL, -RVVStackSize,
  482. MachineInstr::FrameSetup);
  483. if (!hasFP(MF)) {
  484. // Emit .cfi_def_cfa_expression "sp + StackSize + RVVStackSize * vlenb".
  485. unsigned CFIIndex = MF.addFrameInst(createDefCFAExpression(
  486. *RI, SPReg, getStackSizeWithRVVPadding(MF), RVVStackSize / 8));
  487. BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
  488. .addCFIIndex(CFIIndex)
  489. .setMIFlag(MachineInstr::FrameSetup);
  490. }
  491. }
  492. if (hasFP(MF)) {
  493. // Realign Stack
  494. const RISCVRegisterInfo *RI = STI.getRegisterInfo();
  495. if (RI->hasStackRealignment(MF)) {
  496. Align MaxAlignment = MFI.getMaxAlign();
  497. const RISCVInstrInfo *TII = STI.getInstrInfo();
  498. if (isInt<12>(-(int)MaxAlignment.value())) {
  499. BuildMI(MBB, MBBI, DL, TII->get(RISCV::ANDI), SPReg)
  500. .addReg(SPReg)
  501. .addImm(-(int)MaxAlignment.value())
  502. .setMIFlag(MachineInstr::FrameSetup);
  503. } else {
  504. unsigned ShiftAmount = Log2(MaxAlignment);
  505. Register VR =
  506. MF.getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
  507. BuildMI(MBB, MBBI, DL, TII->get(RISCV::SRLI), VR)
  508. .addReg(SPReg)
  509. .addImm(ShiftAmount)
  510. .setMIFlag(MachineInstr::FrameSetup);
  511. BuildMI(MBB, MBBI, DL, TII->get(RISCV::SLLI), SPReg)
  512. .addReg(VR)
  513. .addImm(ShiftAmount)
  514. .setMIFlag(MachineInstr::FrameSetup);
  515. }
  516. // FP will be used to restore the frame in the epilogue, so we need
  517. // another base register BP to record SP after re-alignment. SP will
  518. // track the current stack after allocating variable sized objects.
  519. if (hasBP(MF)) {
  520. // move BP, SP
  521. BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), BPReg)
  522. .addReg(SPReg)
  523. .addImm(0)
  524. .setMIFlag(MachineInstr::FrameSetup);
  525. }
  526. }
  527. }
  528. }
  529. void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
  530. MachineBasicBlock &MBB) const {
  531. const RISCVRegisterInfo *RI = STI.getRegisterInfo();
  532. MachineFrameInfo &MFI = MF.getFrameInfo();
  533. auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  534. Register FPReg = getFPReg(STI);
  535. Register SPReg = getSPReg(STI);
  536. // All calls are tail calls in GHC calling conv, and functions have no
  537. // prologue/epilogue.
  538. if (MF.getFunction().getCallingConv() == CallingConv::GHC)
  539. return;
  540. // Get the insert location for the epilogue. If there were no terminators in
  541. // the block, get the last instruction.
  542. MachineBasicBlock::iterator MBBI = MBB.end();
  543. DebugLoc DL;
  544. if (!MBB.empty()) {
  545. MBBI = MBB.getLastNonDebugInstr();
  546. if (MBBI != MBB.end())
  547. DL = MBBI->getDebugLoc();
  548. MBBI = MBB.getFirstTerminator();
  549. // If callee-saved registers are saved via libcall, place stack adjustment
  550. // before this call.
  551. while (MBBI != MBB.begin() &&
  552. std::prev(MBBI)->getFlag(MachineInstr::FrameDestroy))
  553. --MBBI;
  554. }
  555. const auto &CSI = getNonLibcallCSI(MF, MFI.getCalleeSavedInfo());
  556. // Skip to before the restores of callee-saved registers
  557. // FIXME: assumes exactly one instruction is used to restore each
  558. // callee-saved register.
  559. auto LastFrameDestroy = MBBI;
  560. if (!CSI.empty())
  561. LastFrameDestroy = std::prev(MBBI, CSI.size());
  562. uint64_t StackSize = getStackSizeWithRVVPadding(MF);
  563. uint64_t RealStackSize = StackSize + RVFI->getLibCallStackSize();
  564. uint64_t FPOffset = RealStackSize - RVFI->getVarArgsSaveSize();
  565. uint64_t RVVStackSize = RVFI->getRVVStackSize();
  566. // Restore the stack pointer using the value of the frame pointer. Only
  567. // necessary if the stack pointer was modified, meaning the stack size is
  568. // unknown.
  569. //
  570. // In order to make sure the stack point is right through the EH region,
  571. // we also need to restore stack pointer from the frame pointer if we
  572. // don't preserve stack space within prologue/epilogue for outgoing variables,
  573. // normally it's just checking the variable sized object is present or not
  574. // is enough, but we also don't preserve that at prologue/epilogue when
  575. // have vector objects in stack.
  576. if (RI->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
  577. !hasReservedCallFrame(MF)) {
  578. assert(hasFP(MF) && "frame pointer should not have been eliminated");
  579. RI->adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg,
  580. StackOffset::getFixed(-FPOffset),
  581. MachineInstr::FrameDestroy, getStackAlign());
  582. } else {
  583. if (RVVStackSize)
  584. adjustStackForRVV(MF, MBB, LastFrameDestroy, DL, RVVStackSize,
  585. MachineInstr::FrameDestroy);
  586. }
  587. uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
  588. if (FirstSPAdjustAmount) {
  589. uint64_t SecondSPAdjustAmount =
  590. getStackSizeWithRVVPadding(MF) - FirstSPAdjustAmount;
  591. assert(SecondSPAdjustAmount > 0 &&
  592. "SecondSPAdjustAmount should be greater than zero");
  593. RI->adjustReg(MBB, LastFrameDestroy, DL, SPReg, SPReg,
  594. StackOffset::getFixed(SecondSPAdjustAmount),
  595. MachineInstr::FrameDestroy, getStackAlign());
  596. }
  597. if (FirstSPAdjustAmount)
  598. StackSize = FirstSPAdjustAmount;
  599. // Deallocate stack
  600. RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(StackSize),
  601. MachineInstr::FrameDestroy, getStackAlign());
  602. // Emit epilogue for shadow call stack.
  603. emitSCSEpilogue(MF, MBB, MBBI, DL);
  604. }
  605. StackOffset
  606. RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
  607. Register &FrameReg) const {
  608. const MachineFrameInfo &MFI = MF.getFrameInfo();
  609. const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
  610. const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  611. // Callee-saved registers should be referenced relative to the stack
  612. // pointer (positive offset), otherwise use the frame pointer (negative
  613. // offset).
  614. const auto &CSI = getNonLibcallCSI(MF, MFI.getCalleeSavedInfo());
  615. int MinCSFI = 0;
  616. int MaxCSFI = -1;
  617. StackOffset Offset;
  618. auto StackID = MFI.getStackID(FI);
  619. assert((StackID == TargetStackID::Default ||
  620. StackID == TargetStackID::ScalableVector) &&
  621. "Unexpected stack ID for the frame object.");
  622. if (StackID == TargetStackID::Default) {
  623. Offset =
  624. StackOffset::getFixed(MFI.getObjectOffset(FI) - getOffsetOfLocalArea() +
  625. MFI.getOffsetAdjustment());
  626. } else if (StackID == TargetStackID::ScalableVector) {
  627. Offset = StackOffset::getScalable(MFI.getObjectOffset(FI));
  628. }
  629. uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
  630. if (CSI.size()) {
  631. MinCSFI = CSI[0].getFrameIdx();
  632. MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
  633. }
  634. if (FI >= MinCSFI && FI <= MaxCSFI) {
  635. FrameReg = RISCV::X2;
  636. if (FirstSPAdjustAmount)
  637. Offset += StackOffset::getFixed(FirstSPAdjustAmount);
  638. else
  639. Offset += StackOffset::getFixed(getStackSizeWithRVVPadding(MF));
  640. return Offset;
  641. }
  642. if (RI->hasStackRealignment(MF) && !MFI.isFixedObjectIndex(FI)) {
  643. // If the stack was realigned, the frame pointer is set in order to allow
  644. // SP to be restored, so we need another base register to record the stack
  645. // after realignment.
  646. // |--------------------------| -- <-- FP
  647. // | callee-allocated save | | <----|
  648. // | area for register varargs| | |
  649. // |--------------------------| | |
  650. // | callee-saved registers | | |
  651. // |--------------------------| -- |
  652. // | realignment (the size of | | |
  653. // | this area is not counted | | |
  654. // | in MFI.getStackSize()) | | |
  655. // |--------------------------| -- |-- MFI.getStackSize()
  656. // | RVV alignment padding | | |
  657. // | (not counted in | | |
  658. // | MFI.getStackSize() but | | |
  659. // | counted in | | |
  660. // | RVFI.getRVVStackSize()) | | |
  661. // |--------------------------| -- |
  662. // | RVV objects | | |
  663. // | (not counted in | | |
  664. // | MFI.getStackSize()) | | |
  665. // |--------------------------| -- |
  666. // | padding before RVV | | |
  667. // | (not counted in | | |
  668. // | MFI.getStackSize() or in | | |
  669. // | RVFI.getRVVStackSize()) | | |
  670. // |--------------------------| -- |
  671. // | scalar local variables | | <----'
  672. // |--------------------------| -- <-- BP (if var sized objects present)
  673. // | VarSize objects | |
  674. // |--------------------------| -- <-- SP
  675. if (hasBP(MF)) {
  676. FrameReg = RISCVABI::getBPReg();
  677. } else {
  678. // VarSize objects must be empty in this case!
  679. assert(!MFI.hasVarSizedObjects());
  680. FrameReg = RISCV::X2;
  681. }
  682. } else {
  683. FrameReg = RI->getFrameRegister(MF);
  684. }
  685. if (FrameReg == getFPReg(STI)) {
  686. Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
  687. if (FI >= 0)
  688. Offset -= StackOffset::getFixed(RVFI->getLibCallStackSize());
  689. // When using FP to access scalable vector objects, we need to minus
  690. // the frame size.
  691. //
  692. // |--------------------------| -- <-- FP
  693. // | callee-allocated save | |
  694. // | area for register varargs| |
  695. // |--------------------------| |
  696. // | callee-saved registers | |
  697. // |--------------------------| | MFI.getStackSize()
  698. // | scalar local variables | |
  699. // |--------------------------| -- (Offset of RVV objects is from here.)
  700. // | RVV objects |
  701. // |--------------------------|
  702. // | VarSize objects |
  703. // |--------------------------| <-- SP
  704. if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
  705. assert(!RI->hasStackRealignment(MF) &&
  706. "Can't index across variable sized realign");
  707. // We don't expect any extra RVV alignment padding, as the stack size
  708. // and RVV object sections should be correct aligned in their own
  709. // right.
  710. assert(MFI.getStackSize() == getStackSizeWithRVVPadding(MF) &&
  711. "Inconsistent stack layout");
  712. Offset -= StackOffset::getFixed(MFI.getStackSize());
  713. }
  714. return Offset;
  715. }
  716. // This case handles indexing off both SP and BP.
  717. // If indexing off SP, there must not be any var sized objects
  718. assert(FrameReg == RISCVABI::getBPReg() || !MFI.hasVarSizedObjects());
  719. // When using SP to access frame objects, we need to add RVV stack size.
  720. //
  721. // |--------------------------| -- <-- FP
  722. // | callee-allocated save | | <----|
  723. // | area for register varargs| | |
  724. // |--------------------------| | |
  725. // | callee-saved registers | | |
  726. // |--------------------------| -- |
  727. // | RVV alignment padding | | |
  728. // | (not counted in | | |
  729. // | MFI.getStackSize() but | | |
  730. // | counted in | | |
  731. // | RVFI.getRVVStackSize()) | | |
  732. // |--------------------------| -- |
  733. // | RVV objects | | |-- MFI.getStackSize()
  734. // | (not counted in | | |
  735. // | MFI.getStackSize()) | | |
  736. // |--------------------------| -- |
  737. // | padding before RVV | | |
  738. // | (not counted in | | |
  739. // | MFI.getStackSize()) | | |
  740. // |--------------------------| -- |
  741. // | scalar local variables | | <----'
  742. // |--------------------------| -- <-- BP (if var sized objects present)
  743. // | VarSize objects | |
  744. // |--------------------------| -- <-- SP
  745. //
  746. // The total amount of padding surrounding RVV objects is described by
  747. // RVV->getRVVPadding() and it can be zero. It allows us to align the RVV
  748. // objects to the required alignment.
  749. if (MFI.getStackID(FI) == TargetStackID::Default) {
  750. if (MFI.isFixedObjectIndex(FI)) {
  751. assert(!RI->hasStackRealignment(MF) &&
  752. "Can't index across variable sized realign");
  753. Offset += StackOffset::get(getStackSizeWithRVVPadding(MF) +
  754. RVFI->getLibCallStackSize(),
  755. RVFI->getRVVStackSize());
  756. } else {
  757. Offset += StackOffset::getFixed(MFI.getStackSize());
  758. }
  759. } else if (MFI.getStackID(FI) == TargetStackID::ScalableVector) {
  760. // Ensure the base of the RVV stack is correctly aligned: add on the
  761. // alignment padding.
  762. int ScalarLocalVarSize =
  763. MFI.getStackSize() - RVFI->getCalleeSavedStackSize() -
  764. RVFI->getVarArgsSaveSize() + RVFI->getRVVPadding();
  765. Offset += StackOffset::get(ScalarLocalVarSize, RVFI->getRVVStackSize());
  766. }
  767. return Offset;
  768. }
  769. void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
  770. BitVector &SavedRegs,
  771. RegScavenger *RS) const {
  772. TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
  773. // Unconditionally spill RA and FP only if the function uses a frame
  774. // pointer.
  775. if (hasFP(MF)) {
  776. SavedRegs.set(RISCV::X1);
  777. SavedRegs.set(RISCV::X8);
  778. }
  779. // Mark BP as used if function has dedicated base pointer.
  780. if (hasBP(MF))
  781. SavedRegs.set(RISCVABI::getBPReg());
  782. // If interrupt is enabled and there are calls in the handler,
  783. // unconditionally save all Caller-saved registers and
  784. // all FP registers, regardless whether they are used.
  785. MachineFrameInfo &MFI = MF.getFrameInfo();
  786. if (MF.getFunction().hasFnAttribute("interrupt") && MFI.hasCalls()) {
  787. static const MCPhysReg CSRegs[] = { RISCV::X1, /* ra */
  788. RISCV::X5, RISCV::X6, RISCV::X7, /* t0-t2 */
  789. RISCV::X10, RISCV::X11, /* a0-a1, a2-a7 */
  790. RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17,
  791. RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31, 0 /* t3-t6 */
  792. };
  793. for (unsigned i = 0; CSRegs[i]; ++i)
  794. SavedRegs.set(CSRegs[i]);
  795. if (MF.getSubtarget<RISCVSubtarget>().hasStdExtF()) {
  796. // If interrupt is enabled, this list contains all FP registers.
  797. const MCPhysReg * Regs = MF.getRegInfo().getCalleeSavedRegs();
  798. for (unsigned i = 0; Regs[i]; ++i)
  799. if (RISCV::FPR16RegClass.contains(Regs[i]) ||
  800. RISCV::FPR32RegClass.contains(Regs[i]) ||
  801. RISCV::FPR64RegClass.contains(Regs[i]))
  802. SavedRegs.set(Regs[i]);
  803. }
  804. }
  805. }
  806. std::pair<int64_t, Align>
  807. RISCVFrameLowering::assignRVVStackObjectOffsets(MachineFunction &MF) const {
  808. MachineFrameInfo &MFI = MF.getFrameInfo();
  809. // Create a buffer of RVV objects to allocate.
  810. SmallVector<int, 8> ObjectsToAllocate;
  811. for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
  812. unsigned StackID = MFI.getStackID(I);
  813. if (StackID != TargetStackID::ScalableVector)
  814. continue;
  815. if (MFI.isDeadObjectIndex(I))
  816. continue;
  817. ObjectsToAllocate.push_back(I);
  818. }
  819. // The minimum alignment is 16 bytes.
  820. Align RVVStackAlign(16);
  821. const auto &ST = MF.getSubtarget<RISCVSubtarget>();
  822. if (!ST.hasVInstructions()) {
  823. assert(ObjectsToAllocate.empty() &&
  824. "Can't allocate scalable-vector objects without V instructions");
  825. return std::make_pair(0, RVVStackAlign);
  826. }
  827. // Allocate all RVV locals and spills
  828. int64_t Offset = 0;
  829. for (int FI : ObjectsToAllocate) {
  830. // ObjectSize in bytes.
  831. int64_t ObjectSize = MFI.getObjectSize(FI);
  832. auto ObjectAlign = std::max(Align(8), MFI.getObjectAlign(FI));
  833. // If the data type is the fractional vector type, reserve one vector
  834. // register for it.
  835. if (ObjectSize < 8)
  836. ObjectSize = 8;
  837. Offset = alignTo(Offset + ObjectSize, ObjectAlign);
  838. MFI.setObjectOffset(FI, -Offset);
  839. // Update the maximum alignment of the RVV stack section
  840. RVVStackAlign = std::max(RVVStackAlign, ObjectAlign);
  841. }
  842. // Ensure the alignment of the RVV stack. Since we want the most-aligned
  843. // object right at the bottom (i.e., any padding at the top of the frame),
  844. // readjust all RVV objects down by the alignment padding.
  845. uint64_t StackSize = Offset;
  846. if (auto AlignmentPadding = offsetToAlignment(StackSize, RVVStackAlign)) {
  847. StackSize += AlignmentPadding;
  848. for (int FI : ObjectsToAllocate)
  849. MFI.setObjectOffset(FI, MFI.getObjectOffset(FI) - AlignmentPadding);
  850. }
  851. return std::make_pair(StackSize, RVVStackAlign);
  852. }
  853. static unsigned getScavSlotsNumForRVV(MachineFunction &MF) {
  854. // For RVV spill, scalable stack offsets computing requires up to two scratch
  855. // registers
  856. static constexpr unsigned ScavSlotsNumRVVSpillScalableObject = 2;
  857. // For RVV spill, non-scalable stack offsets computing requires up to one
  858. // scratch register.
  859. static constexpr unsigned ScavSlotsNumRVVSpillNonScalableObject = 1;
  860. // ADDI instruction's destination register can be used for computing
  861. // offsets. So Scalable stack offsets require up to one scratch register.
  862. static constexpr unsigned ScavSlotsADDIScalableObject = 1;
  863. static constexpr unsigned MaxScavSlotsNumKnown =
  864. std::max({ScavSlotsADDIScalableObject, ScavSlotsNumRVVSpillScalableObject,
  865. ScavSlotsNumRVVSpillNonScalableObject});
  866. unsigned MaxScavSlotsNum = 0;
  867. if (!MF.getSubtarget<RISCVSubtarget>().hasVInstructions())
  868. return false;
  869. for (const MachineBasicBlock &MBB : MF)
  870. for (const MachineInstr &MI : MBB) {
  871. bool IsRVVSpill = RISCV::isRVVSpill(MI);
  872. for (auto &MO : MI.operands()) {
  873. if (!MO.isFI())
  874. continue;
  875. bool IsScalableVectorID = MF.getFrameInfo().getStackID(MO.getIndex()) ==
  876. TargetStackID::ScalableVector;
  877. if (IsRVVSpill) {
  878. MaxScavSlotsNum = std::max(
  879. MaxScavSlotsNum, IsScalableVectorID
  880. ? ScavSlotsNumRVVSpillScalableObject
  881. : ScavSlotsNumRVVSpillNonScalableObject);
  882. } else if (MI.getOpcode() == RISCV::ADDI && IsScalableVectorID) {
  883. MaxScavSlotsNum =
  884. std::max(MaxScavSlotsNum, ScavSlotsADDIScalableObject);
  885. }
  886. }
  887. if (MaxScavSlotsNum == MaxScavSlotsNumKnown)
  888. return MaxScavSlotsNumKnown;
  889. }
  890. return MaxScavSlotsNum;
  891. }
  892. static bool hasRVVFrameObject(const MachineFunction &MF) {
  893. // Originally, the function will scan all the stack objects to check whether
  894. // if there is any scalable vector object on the stack or not. However, it
  895. // causes errors in the register allocator. In issue 53016, it returns false
  896. // before RA because there is no RVV stack objects. After RA, it returns true
  897. // because there are spilling slots for RVV values during RA. It will not
  898. // reserve BP during register allocation and generate BP access in the PEI
  899. // pass due to the inconsistent behavior of the function.
  900. //
  901. // The function is changed to use hasVInstructions() as the return value. It
  902. // is not precise, but it can make the register allocation correct.
  903. //
  904. // FIXME: Find a better way to make the decision or revisit the solution in
  905. // D103622.
  906. //
  907. // Refer to https://github.com/llvm/llvm-project/issues/53016.
  908. return MF.getSubtarget<RISCVSubtarget>().hasVInstructions();
  909. }
  910. static unsigned estimateFunctionSizeInBytes(const MachineFunction &MF,
  911. const RISCVInstrInfo &TII) {
  912. unsigned FnSize = 0;
  913. for (auto &MBB : MF) {
  914. for (auto &MI : MBB) {
  915. // Far branches over 20-bit offset will be relaxed in branch relaxation
  916. // pass. In the worst case, conditional branches will be relaxed into
  917. // the following instruction sequence. Unconditional branches are
  918. // relaxed in the same way, with the exception that there is no first
  919. // branch instruction.
  920. //
  921. // foo
  922. // bne t5, t6, .rev_cond # `TII->getInstSizeInBytes(MI)` bytes
  923. // sd s11, 0(sp) # 4 bytes, or 2 bytes in RVC
  924. // jump .restore, s11 # 8 bytes
  925. // .rev_cond
  926. // bar
  927. // j .dest_bb # 4 bytes, or 2 bytes in RVC
  928. // .restore:
  929. // ld s11, 0(sp) # 4 bytes, or 2 bytes in RVC
  930. // .dest:
  931. // baz
  932. if (MI.isConditionalBranch())
  933. FnSize += TII.getInstSizeInBytes(MI);
  934. if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
  935. if (MF.getSubtarget<RISCVSubtarget>().hasStdExtC())
  936. FnSize += 2 + 8 + 2 + 2;
  937. else
  938. FnSize += 4 + 8 + 4 + 4;
  939. continue;
  940. }
  941. FnSize += TII.getInstSizeInBytes(MI);
  942. }
  943. }
  944. return FnSize;
  945. }
  946. void RISCVFrameLowering::processFunctionBeforeFrameFinalized(
  947. MachineFunction &MF, RegScavenger *RS) const {
  948. const RISCVRegisterInfo *RegInfo =
  949. MF.getSubtarget<RISCVSubtarget>().getRegisterInfo();
  950. const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
  951. MachineFrameInfo &MFI = MF.getFrameInfo();
  952. const TargetRegisterClass *RC = &RISCV::GPRRegClass;
  953. auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  954. int64_t RVVStackSize;
  955. Align RVVStackAlign;
  956. std::tie(RVVStackSize, RVVStackAlign) = assignRVVStackObjectOffsets(MF);
  957. RVFI->setRVVStackSize(RVVStackSize);
  958. RVFI->setRVVStackAlign(RVVStackAlign);
  959. if (hasRVVFrameObject(MF)) {
  960. // Ensure the entire stack is aligned to at least the RVV requirement: some
  961. // scalable-vector object alignments are not considered by the
  962. // target-independent code.
  963. MFI.ensureMaxAlignment(RVVStackAlign);
  964. }
  965. unsigned ScavSlotsNum = 0;
  966. // estimateStackSize has been observed to under-estimate the final stack
  967. // size, so give ourselves wiggle-room by checking for stack size
  968. // representable an 11-bit signed field rather than 12-bits.
  969. if (!isInt<11>(MFI.estimateStackSize(MF)))
  970. ScavSlotsNum = 1;
  971. // Far branches over 20-bit offset require a spill slot for scratch register.
  972. bool IsLargeFunction = !isInt<20>(estimateFunctionSizeInBytes(MF, *TII));
  973. if (IsLargeFunction)
  974. ScavSlotsNum = std::max(ScavSlotsNum, 1u);
  975. // RVV loads & stores have no capacity to hold the immediate address offsets
  976. // so we must always reserve an emergency spill slot if the MachineFunction
  977. // contains any RVV spills.
  978. ScavSlotsNum = std::max(ScavSlotsNum, getScavSlotsNumForRVV(MF));
  979. for (unsigned I = 0; I < ScavSlotsNum; I++) {
  980. int FI = MFI.CreateStackObject(RegInfo->getSpillSize(*RC),
  981. RegInfo->getSpillAlign(*RC), false);
  982. RS->addScavengingFrameIndex(FI);
  983. if (IsLargeFunction && RVFI->getBranchRelaxationScratchFrameIndex() == -1)
  984. RVFI->setBranchRelaxationScratchFrameIndex(FI);
  985. }
  986. if (MFI.getCalleeSavedInfo().empty() || RVFI->useSaveRestoreLibCalls(MF)) {
  987. RVFI->setCalleeSavedStackSize(0);
  988. return;
  989. }
  990. unsigned Size = 0;
  991. for (const auto &Info : MFI.getCalleeSavedInfo()) {
  992. int FrameIdx = Info.getFrameIdx();
  993. if (MFI.getStackID(FrameIdx) != TargetStackID::Default)
  994. continue;
  995. Size += MFI.getObjectSize(FrameIdx);
  996. }
  997. RVFI->setCalleeSavedStackSize(Size);
  998. }
  999. // Not preserve stack space within prologue for outgoing variables when the
  1000. // function contains variable size objects or there are vector objects accessed
  1001. // by the frame pointer.
  1002. // Let eliminateCallFramePseudoInstr preserve stack space for it.
  1003. bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
  1004. return !MF.getFrameInfo().hasVarSizedObjects() &&
  1005. !(hasFP(MF) && hasRVVFrameObject(MF));
  1006. }
  1007. // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions.
  1008. MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
  1009. MachineFunction &MF, MachineBasicBlock &MBB,
  1010. MachineBasicBlock::iterator MI) const {
  1011. Register SPReg = RISCV::X2;
  1012. DebugLoc DL = MI->getDebugLoc();
  1013. if (!hasReservedCallFrame(MF)) {
  1014. // If space has not been reserved for a call frame, ADJCALLSTACKDOWN and
  1015. // ADJCALLSTACKUP must be converted to instructions manipulating the stack
  1016. // pointer. This is necessary when there is a variable length stack
  1017. // allocation (e.g. alloca), which means it's not possible to allocate
  1018. // space for outgoing arguments from within the function prologue.
  1019. int64_t Amount = MI->getOperand(0).getImm();
  1020. if (Amount != 0) {
  1021. // Ensure the stack remains aligned after adjustment.
  1022. Amount = alignSPAdjust(Amount);
  1023. if (MI->getOpcode() == RISCV::ADJCALLSTACKDOWN)
  1024. Amount = -Amount;
  1025. const RISCVRegisterInfo &RI = *STI.getRegisterInfo();
  1026. RI.adjustReg(MBB, MI, DL, SPReg, SPReg, StackOffset::getFixed(Amount),
  1027. MachineInstr::NoFlags, getStackAlign());
  1028. }
  1029. }
  1030. return MBB.erase(MI);
  1031. }
  1032. // We would like to split the SP adjustment to reduce prologue/epilogue
  1033. // as following instructions. In this way, the offset of the callee saved
  1034. // register could fit in a single store.
  1035. // add sp,sp,-2032
  1036. // sw ra,2028(sp)
  1037. // sw s0,2024(sp)
  1038. // sw s1,2020(sp)
  1039. // sw s3,2012(sp)
  1040. // sw s4,2008(sp)
  1041. // add sp,sp,-64
  1042. uint64_t
  1043. RISCVFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF) const {
  1044. const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
  1045. const MachineFrameInfo &MFI = MF.getFrameInfo();
  1046. const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
  1047. uint64_t StackSize = getStackSizeWithRVVPadding(MF);
  1048. // Disable SplitSPAdjust if save-restore libcall is used. The callee-saved
  1049. // registers will be pushed by the save-restore libcalls, so we don't have to
  1050. // split the SP adjustment in this case.
  1051. if (RVFI->getLibCallStackSize())
  1052. return 0;
  1053. // Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
  1054. // 12-bit and there exists a callee-saved register needing to be pushed.
  1055. if (!isInt<12>(StackSize) && (CSI.size() > 0)) {
  1056. // FirstSPAdjustAmount is chosen as (2048 - StackAlign) because 2048 will
  1057. // cause sp = sp + 2048 in the epilogue to be split into multiple
  1058. // instructions. Offsets smaller than 2048 can fit in a single load/store
  1059. // instruction, and we have to stick with the stack alignment. 2048 has
  1060. // 16-byte alignment. The stack alignment for RV32 and RV64 is 16 and for
  1061. // RV32E it is 4. So (2048 - StackAlign) will satisfy the stack alignment.
  1062. return 2048 - getStackAlign().value();
  1063. }
  1064. return 0;
  1065. }
  1066. bool RISCVFrameLowering::spillCalleeSavedRegisters(
  1067. MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  1068. ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
  1069. if (CSI.empty())
  1070. return true;
  1071. MachineFunction *MF = MBB.getParent();
  1072. const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
  1073. DebugLoc DL;
  1074. if (MI != MBB.end() && !MI->isDebugInstr())
  1075. DL = MI->getDebugLoc();
  1076. const char *SpillLibCall = getSpillLibCallName(*MF, CSI);
  1077. if (SpillLibCall) {
  1078. // Add spill libcall via non-callee-saved register t0.
  1079. BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
  1080. .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
  1081. .setMIFlag(MachineInstr::FrameSetup);
  1082. // Add registers spilled in libcall as liveins.
  1083. for (auto &CS : CSI)
  1084. MBB.addLiveIn(CS.getReg());
  1085. }
  1086. // Manually spill values not spilled by libcall.
  1087. const auto &NonLibcallCSI = getNonLibcallCSI(*MF, CSI);
  1088. for (auto &CS : NonLibcallCSI) {
  1089. // Insert the spill to the stack frame.
  1090. Register Reg = CS.getReg();
  1091. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  1092. TII.storeRegToStackSlot(MBB, MI, Reg, !MBB.isLiveIn(Reg), CS.getFrameIdx(),
  1093. RC, TRI, Register());
  1094. }
  1095. return true;
  1096. }
  1097. bool RISCVFrameLowering::restoreCalleeSavedRegisters(
  1098. MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  1099. MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
  1100. if (CSI.empty())
  1101. return true;
  1102. MachineFunction *MF = MBB.getParent();
  1103. const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
  1104. DebugLoc DL;
  1105. if (MI != MBB.end() && !MI->isDebugInstr())
  1106. DL = MI->getDebugLoc();
  1107. // Manually restore values not restored by libcall.
  1108. // Keep the same order as in the prologue. There is no need to reverse the
  1109. // order in the epilogue. In addition, the return address will be restored
  1110. // first in the epilogue. It increases the opportunity to avoid the
  1111. // load-to-use data hazard between loading RA and return by RA.
  1112. // loadRegFromStackSlot can insert multiple instructions.
  1113. const auto &NonLibcallCSI = getNonLibcallCSI(*MF, CSI);
  1114. for (auto &CS : NonLibcallCSI) {
  1115. Register Reg = CS.getReg();
  1116. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
  1117. TII.loadRegFromStackSlot(MBB, MI, Reg, CS.getFrameIdx(), RC, TRI,
  1118. Register());
  1119. assert(MI != MBB.begin() && "loadRegFromStackSlot didn't insert any code!");
  1120. }
  1121. const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);
  1122. if (RestoreLibCall) {
  1123. // Add restore libcall via tail call.
  1124. MachineBasicBlock::iterator NewMI =
  1125. BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
  1126. .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
  1127. .setMIFlag(MachineInstr::FrameDestroy);
  1128. // Remove trailing returns, since the terminator is now a tail call to the
  1129. // restore function.
  1130. if (MI != MBB.end() && MI->getOpcode() == RISCV::PseudoRET) {
  1131. NewMI->copyImplicitOps(*MF, *MI);
  1132. MI->eraseFromParent();
  1133. }
  1134. }
  1135. return true;
  1136. }
  1137. bool RISCVFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
  1138. // Keep the conventional code flow when not optimizing.
  1139. if (MF.getFunction().hasOptNone())
  1140. return false;
  1141. return true;
  1142. }
  1143. bool RISCVFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
  1144. MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
  1145. const MachineFunction *MF = MBB.getParent();
  1146. const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
  1147. if (!RVFI->useSaveRestoreLibCalls(*MF))
  1148. return true;
  1149. // Inserting a call to a __riscv_save libcall requires the use of the register
  1150. // t0 (X5) to hold the return address. Therefore if this register is already
  1151. // used we can't insert the call.
  1152. RegScavenger RS;
  1153. RS.enterBasicBlock(*TmpMBB);
  1154. return !RS.isRegUsed(RISCV::X5);
  1155. }
  1156. bool RISCVFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
  1157. const MachineFunction *MF = MBB.getParent();
  1158. MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
  1159. const auto *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
  1160. if (!RVFI->useSaveRestoreLibCalls(*MF))
  1161. return true;
  1162. // Using the __riscv_restore libcalls to restore CSRs requires a tail call.
  1163. // This means if we still need to continue executing code within this function
  1164. // the restore cannot take place in this basic block.
  1165. if (MBB.succ_size() > 1)
  1166. return false;
  1167. MachineBasicBlock *SuccMBB =
  1168. MBB.succ_empty() ? TmpMBB->getFallThrough() : *MBB.succ_begin();
  1169. // Doing a tail call should be safe if there are no successors, because either
  1170. // we have a returning block or the end of the block is unreachable, so the
  1171. // restore will be eliminated regardless.
  1172. if (!SuccMBB)
  1173. return true;
  1174. // The successor can only contain a return, since we would effectively be
  1175. // replacing the successor with our own tail return at the end of our block.
  1176. return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
  1177. }
  1178. bool RISCVFrameLowering::isSupportedStackID(TargetStackID::Value ID) const {
  1179. switch (ID) {
  1180. case TargetStackID::Default:
  1181. case TargetStackID::ScalableVector:
  1182. return true;
  1183. case TargetStackID::NoAlloc:
  1184. case TargetStackID::SGPRSpill:
  1185. case TargetStackID::WasmLocal:
  1186. return false;
  1187. }
  1188. llvm_unreachable("Invalid TargetStackID::Value");
  1189. }
  1190. TargetStackID::Value RISCVFrameLowering::getStackIDForScalableVectors() const {
  1191. return TargetStackID::ScalableVector;
  1192. }