WebAssemblyFrameLowering.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
  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. /// \file
  10. /// This file contains the WebAssembly implementation of
  11. /// TargetFrameLowering class.
  12. ///
  13. /// On WebAssembly, there aren't a lot of things to do here. There are no
  14. /// callee-saved registers to save, and no spill slots.
  15. ///
  16. /// The stack grows downward.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #include "WebAssemblyFrameLowering.h"
  20. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  21. #include "Utils/WebAssemblyTypeUtilities.h"
  22. #include "WebAssembly.h"
  23. #include "WebAssemblyInstrInfo.h"
  24. #include "WebAssemblyMachineFunctionInfo.h"
  25. #include "WebAssemblySubtarget.h"
  26. #include "WebAssemblyTargetMachine.h"
  27. #include "llvm/CodeGen/Analysis.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineFunction.h"
  30. #include "llvm/CodeGen/MachineInstrBuilder.h"
  31. #include "llvm/CodeGen/MachineModuleInfoImpls.h"
  32. #include "llvm/CodeGen/MachineRegisterInfo.h"
  33. #include "llvm/IR/Instructions.h"
  34. #include "llvm/MC/MCAsmInfo.h"
  35. #include "llvm/Support/Debug.h"
  36. using namespace llvm;
  37. #define DEBUG_TYPE "wasm-frame-info"
  38. // TODO: wasm64
  39. // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
  40. // In an ideal world, when objects are added to the MachineFrameInfo by
  41. // FunctionLoweringInfo::set, we could somehow hook into target-specific code to
  42. // ensure they are assigned the right stack ID. However there isn't a hook that
  43. // runs between then and DAG building time, though, so instead we hoist stack
  44. // objects lazily when they are first used, and comprehensively after the DAG is
  45. // built via the PreprocessISelDAG hook, called by the
  46. // SelectionDAGISel::runOnMachineFunction. We have to do it in two places
  47. // because we want to do it while building the selection DAG for uses of alloca,
  48. // but not all alloca instructions are used so we have to follow up afterwards.
  49. std::optional<unsigned>
  50. WebAssemblyFrameLowering::getLocalForStackObject(MachineFunction &MF,
  51. int FrameIndex) {
  52. MachineFrameInfo &MFI = MF.getFrameInfo();
  53. // If already hoisted to a local, done.
  54. if (MFI.getStackID(FrameIndex) == TargetStackID::WasmLocal)
  55. return static_cast<unsigned>(MFI.getObjectOffset(FrameIndex));
  56. // If not allocated in the object address space, this object will be in
  57. // linear memory.
  58. const AllocaInst *AI = MFI.getObjectAllocation(FrameIndex);
  59. if (!AI || !WebAssembly::isWasmVarAddressSpace(AI->getAddressSpace()))
  60. return std::nullopt;
  61. // Otherwise, allocate this object in the named value stack, outside of linear
  62. // memory.
  63. SmallVector<EVT, 4> ValueVTs;
  64. const WebAssemblyTargetLowering &TLI =
  65. *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
  66. WebAssemblyFunctionInfo *FuncInfo = MF.getInfo<WebAssemblyFunctionInfo>();
  67. ComputeValueVTs(TLI, MF.getDataLayout(), AI->getAllocatedType(), ValueVTs);
  68. MFI.setStackID(FrameIndex, TargetStackID::WasmLocal);
  69. // Abuse SP offset to record the index of the first local in the object.
  70. unsigned Local = FuncInfo->getParams().size() + FuncInfo->getLocals().size();
  71. MFI.setObjectOffset(FrameIndex, Local);
  72. // Allocate WebAssembly locals for each non-aggregate component of the
  73. // allocation.
  74. for (EVT ValueVT : ValueVTs)
  75. FuncInfo->addLocal(ValueVT.getSimpleVT());
  76. // Abuse object size to record number of WebAssembly locals allocated to
  77. // this object.
  78. MFI.setObjectSize(FrameIndex, ValueVTs.size());
  79. return static_cast<unsigned>(Local);
  80. }
  81. /// We need a base pointer in the case of having items on the stack that
  82. /// require stricter alignment than the stack pointer itself. Because we need
  83. /// to shift the stack pointer by some unknown amount to force the alignment,
  84. /// we need to record the value of the stack pointer on entry to the function.
  85. bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
  86. const auto *RegInfo =
  87. MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
  88. return RegInfo->hasStackRealignment(MF);
  89. }
  90. /// Return true if the specified function should have a dedicated frame pointer
  91. /// register.
  92. bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
  93. const MachineFrameInfo &MFI = MF.getFrameInfo();
  94. // When we have var-sized objects, we move the stack pointer by an unknown
  95. // amount, and need to emit a frame pointer to restore the stack to where we
  96. // were on function entry.
  97. // If we already need a base pointer, we use that to fix up the stack pointer.
  98. // If there are no fixed-size objects, we would have no use of a frame
  99. // pointer, and thus should not emit one.
  100. bool HasFixedSizedObjects = MFI.getStackSize() > 0;
  101. bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
  102. return MFI.isFrameAddressTaken() ||
  103. (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
  104. MFI.hasStackMap() || MFI.hasPatchPoint();
  105. }
  106. /// Under normal circumstances, when a frame pointer is not required, we reserve
  107. /// argument space for call sites in the function immediately on entry to the
  108. /// current function. This eliminates the need for add/sub sp brackets around
  109. /// call sites. Returns true if the call frame is included as part of the stack
  110. /// frame.
  111. bool WebAssemblyFrameLowering::hasReservedCallFrame(
  112. const MachineFunction &MF) const {
  113. return !MF.getFrameInfo().hasVarSizedObjects();
  114. }
  115. // Returns true if this function needs a local user-space stack pointer for its
  116. // local frame (not for exception handling).
  117. bool WebAssemblyFrameLowering::needsSPForLocalFrame(
  118. const MachineFunction &MF) const {
  119. auto &MFI = MF.getFrameInfo();
  120. return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
  121. }
  122. // In function with EH pads, we need to make a copy of the value of
  123. // __stack_pointer global in SP32/64 register, in order to use it when
  124. // restoring __stack_pointer after an exception is caught.
  125. bool WebAssemblyFrameLowering::needsPrologForEH(
  126. const MachineFunction &MF) const {
  127. auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
  128. return EHType == ExceptionHandling::Wasm &&
  129. MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
  130. }
  131. /// Returns true if this function needs a local user-space stack pointer.
  132. /// Unlike a machine stack pointer, the wasm user stack pointer is a global
  133. /// variable, so it is loaded into a register in the prolog.
  134. bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
  135. return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
  136. }
  137. /// Returns true if the local user-space stack pointer needs to be written back
  138. /// to __stack_pointer global by this function (this is not meaningful if
  139. /// needsSP is false). If false, the stack red zone can be used and only a local
  140. /// SP is needed.
  141. bool WebAssemblyFrameLowering::needsSPWriteback(
  142. const MachineFunction &MF) const {
  143. auto &MFI = MF.getFrameInfo();
  144. assert(needsSP(MF));
  145. // When we don't need a local stack pointer for its local frame but only to
  146. // support EH, we don't need to write SP back in the epilog, because we don't
  147. // bump down the stack pointer in the prolog. We need to write SP back in the
  148. // epilog only if
  149. // 1. We need SP not only for EH support but also because we actually use
  150. // stack or we have a frame address taken.
  151. // 2. We cannot use the red zone.
  152. bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
  153. !MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
  154. return needsSPForLocalFrame(MF) && !CanUseRedZone;
  155. }
  156. unsigned WebAssemblyFrameLowering::getSPReg(const MachineFunction &MF) {
  157. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  158. ? WebAssembly::SP64
  159. : WebAssembly::SP32;
  160. }
  161. unsigned WebAssemblyFrameLowering::getFPReg(const MachineFunction &MF) {
  162. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  163. ? WebAssembly::FP64
  164. : WebAssembly::FP32;
  165. }
  166. unsigned
  167. WebAssemblyFrameLowering::getOpcConst(const MachineFunction &MF) {
  168. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  169. ? WebAssembly::CONST_I64
  170. : WebAssembly::CONST_I32;
  171. }
  172. unsigned WebAssemblyFrameLowering::getOpcAdd(const MachineFunction &MF) {
  173. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  174. ? WebAssembly::ADD_I64
  175. : WebAssembly::ADD_I32;
  176. }
  177. unsigned WebAssemblyFrameLowering::getOpcSub(const MachineFunction &MF) {
  178. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  179. ? WebAssembly::SUB_I64
  180. : WebAssembly::SUB_I32;
  181. }
  182. unsigned WebAssemblyFrameLowering::getOpcAnd(const MachineFunction &MF) {
  183. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  184. ? WebAssembly::AND_I64
  185. : WebAssembly::AND_I32;
  186. }
  187. unsigned
  188. WebAssemblyFrameLowering::getOpcGlobGet(const MachineFunction &MF) {
  189. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  190. ? WebAssembly::GLOBAL_GET_I64
  191. : WebAssembly::GLOBAL_GET_I32;
  192. }
  193. unsigned
  194. WebAssemblyFrameLowering::getOpcGlobSet(const MachineFunction &MF) {
  195. return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
  196. ? WebAssembly::GLOBAL_SET_I64
  197. : WebAssembly::GLOBAL_SET_I32;
  198. }
  199. void WebAssemblyFrameLowering::writeSPToGlobal(
  200. unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
  201. MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
  202. const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  203. const char *ES = "__stack_pointer";
  204. auto *SPSymbol = MF.createExternalSymbolName(ES);
  205. BuildMI(MBB, InsertStore, DL, TII->get(getOpcGlobSet(MF)))
  206. .addExternalSymbol(SPSymbol)
  207. .addReg(SrcReg);
  208. }
  209. MachineBasicBlock::iterator
  210. WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
  211. MachineFunction &MF, MachineBasicBlock &MBB,
  212. MachineBasicBlock::iterator I) const {
  213. assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
  214. "Call frame pseudos should only be used for dynamic stack adjustment");
  215. auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
  216. const auto *TII = ST.getInstrInfo();
  217. if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
  218. needsSPWriteback(MF)) {
  219. DebugLoc DL = I->getDebugLoc();
  220. writeSPToGlobal(getSPReg(MF), MF, MBB, I, DL);
  221. }
  222. return MBB.erase(I);
  223. }
  224. void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
  225. MachineBasicBlock &MBB) const {
  226. // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
  227. auto &MFI = MF.getFrameInfo();
  228. assert(MFI.getCalleeSavedInfo().empty() &&
  229. "WebAssembly should not have callee-saved registers");
  230. if (!needsSP(MF))
  231. return;
  232. uint64_t StackSize = MFI.getStackSize();
  233. auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
  234. const auto *TII = ST.getInstrInfo();
  235. auto &MRI = MF.getRegInfo();
  236. auto InsertPt = MBB.begin();
  237. while (InsertPt != MBB.end() &&
  238. WebAssembly::isArgument(InsertPt->getOpcode()))
  239. ++InsertPt;
  240. DebugLoc DL;
  241. const TargetRegisterClass *PtrRC =
  242. MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
  243. unsigned SPReg = getSPReg(MF);
  244. if (StackSize)
  245. SPReg = MRI.createVirtualRegister(PtrRC);
  246. const char *ES = "__stack_pointer";
  247. auto *SPSymbol = MF.createExternalSymbolName(ES);
  248. BuildMI(MBB, InsertPt, DL, TII->get(getOpcGlobGet(MF)), SPReg)
  249. .addExternalSymbol(SPSymbol);
  250. bool HasBP = hasBP(MF);
  251. if (HasBP) {
  252. auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
  253. Register BasePtr = MRI.createVirtualRegister(PtrRC);
  254. FI->setBasePointerVreg(BasePtr);
  255. BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
  256. .addReg(SPReg);
  257. }
  258. if (StackSize) {
  259. // Subtract the frame size
  260. Register OffsetReg = MRI.createVirtualRegister(PtrRC);
  261. BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
  262. .addImm(StackSize);
  263. BuildMI(MBB, InsertPt, DL, TII->get(getOpcSub(MF)), getSPReg(MF))
  264. .addReg(SPReg)
  265. .addReg(OffsetReg);
  266. }
  267. if (HasBP) {
  268. Register BitmaskReg = MRI.createVirtualRegister(PtrRC);
  269. Align Alignment = MFI.getMaxAlign();
  270. BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), BitmaskReg)
  271. .addImm((int64_t) ~(Alignment.value() - 1));
  272. BuildMI(MBB, InsertPt, DL, TII->get(getOpcAnd(MF)), getSPReg(MF))
  273. .addReg(getSPReg(MF))
  274. .addReg(BitmaskReg);
  275. }
  276. if (hasFP(MF)) {
  277. // Unlike most conventional targets (where FP points to the saved FP),
  278. // FP points to the bottom of the fixed-size locals, so we can use positive
  279. // offsets in load/store instructions.
  280. BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), getFPReg(MF))
  281. .addReg(getSPReg(MF));
  282. }
  283. if (StackSize && needsSPWriteback(MF)) {
  284. writeSPToGlobal(getSPReg(MF), MF, MBB, InsertPt, DL);
  285. }
  286. }
  287. void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
  288. MachineBasicBlock &MBB) const {
  289. uint64_t StackSize = MF.getFrameInfo().getStackSize();
  290. if (!needsSP(MF) || !needsSPWriteback(MF))
  291. return;
  292. auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
  293. const auto *TII = ST.getInstrInfo();
  294. auto &MRI = MF.getRegInfo();
  295. auto InsertPt = MBB.getFirstTerminator();
  296. DebugLoc DL;
  297. if (InsertPt != MBB.end())
  298. DL = InsertPt->getDebugLoc();
  299. // Restore the stack pointer. If we had fixed-size locals, add the offset
  300. // subtracted in the prolog.
  301. unsigned SPReg = 0;
  302. unsigned SPFPReg = hasFP(MF) ? getFPReg(MF) : getSPReg(MF);
  303. if (hasBP(MF)) {
  304. auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
  305. SPReg = FI->getBasePointerVreg();
  306. } else if (StackSize) {
  307. const TargetRegisterClass *PtrRC =
  308. MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
  309. Register OffsetReg = MRI.createVirtualRegister(PtrRC);
  310. BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
  311. .addImm(StackSize);
  312. // In the epilog we don't need to write the result back to the SP32/64
  313. // physreg because it won't be used again. We can use a stackified register
  314. // instead.
  315. SPReg = MRI.createVirtualRegister(PtrRC);
  316. BuildMI(MBB, InsertPt, DL, TII->get(getOpcAdd(MF)), SPReg)
  317. .addReg(SPFPReg)
  318. .addReg(OffsetReg);
  319. } else {
  320. SPReg = SPFPReg;
  321. }
  322. writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
  323. }
  324. bool WebAssemblyFrameLowering::isSupportedStackID(
  325. TargetStackID::Value ID) const {
  326. // Use the Object stack for WebAssembly locals which can only be accessed
  327. // by name, not via an address in linear memory.
  328. if (ID == TargetStackID::WasmLocal)
  329. return true;
  330. return TargetFrameLowering::isSupportedStackID(ID);
  331. }
  332. TargetFrameLowering::DwarfFrameBase
  333. WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
  334. DwarfFrameBase Loc;
  335. Loc.Kind = DwarfFrameBase::WasmFrameBase;
  336. const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  337. if (needsSP(MF) && MFI.isFrameBaseVirtual()) {
  338. unsigned LocalNum = MFI.getFrameBaseLocal();
  339. Loc.Location.WasmLoc = {WebAssembly::TI_LOCAL, LocalNum};
  340. } else {
  341. // TODO: This should work on a breakpoint at a function with no frame,
  342. // but probably won't work for traversing up the stack.
  343. Loc.Location.WasmLoc = {WebAssembly::TI_GLOBAL_RELOC, 0};
  344. }
  345. return Loc;
  346. }