xref: /src/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
11a82d4c0SDimitry Andric //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
21a82d4c0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61a82d4c0SDimitry Andric //
71a82d4c0SDimitry Andric //===----------------------------------------------------------------------===//
81a82d4c0SDimitry Andric ///
91a82d4c0SDimitry Andric /// \file
10eb11fae6SDimitry Andric /// This file contains the WebAssembly implementation of
111a82d4c0SDimitry Andric /// TargetFrameLowering class.
121a82d4c0SDimitry Andric ///
131a82d4c0SDimitry Andric /// On WebAssembly, there aren't a lot of things to do here. There are no
141a82d4c0SDimitry Andric /// callee-saved registers to save, and no spill slots.
151a82d4c0SDimitry Andric ///
161a82d4c0SDimitry Andric /// The stack grows downward.
171a82d4c0SDimitry Andric ///
181a82d4c0SDimitry Andric //===----------------------------------------------------------------------===//
191a82d4c0SDimitry Andric 
201a82d4c0SDimitry Andric #include "WebAssemblyFrameLowering.h"
211a82d4c0SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
22ecbca9f5SDimitry Andric #include "Utils/WebAssemblyTypeUtilities.h"
23cfca06d7SDimitry Andric #include "WebAssembly.h"
241a82d4c0SDimitry Andric #include "WebAssemblyInstrInfo.h"
251a82d4c0SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
261a82d4c0SDimitry Andric #include "WebAssemblySubtarget.h"
271a82d4c0SDimitry Andric #include "WebAssemblyTargetMachine.h"
28344a3780SDimitry Andric #include "llvm/CodeGen/Analysis.h"
291a82d4c0SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
301a82d4c0SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
311a82d4c0SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
3271d5a254SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
331a82d4c0SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
34344a3780SDimitry Andric #include "llvm/IR/Instructions.h"
35d8e91e46SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
361a82d4c0SDimitry Andric #include "llvm/Support/Debug.h"
371a82d4c0SDimitry Andric using namespace llvm;
381a82d4c0SDimitry Andric 
391a82d4c0SDimitry Andric #define DEBUG_TYPE "wasm-frame-info"
401a82d4c0SDimitry Andric 
41dd58ef01SDimitry Andric // TODO: wasm64
42dd58ef01SDimitry Andric // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
431a82d4c0SDimitry Andric 
44344a3780SDimitry Andric // In an ideal world, when objects are added to the MachineFrameInfo by
45344a3780SDimitry Andric // FunctionLoweringInfo::set, we could somehow hook into target-specific code to
46344a3780SDimitry Andric // ensure they are assigned the right stack ID.  However there isn't a hook that
47344a3780SDimitry Andric // runs between then and DAG building time, though, so instead we hoist stack
48344a3780SDimitry Andric // objects lazily when they are first used, and comprehensively after the DAG is
49344a3780SDimitry Andric // built via the PreprocessISelDAG hook, called by the
50344a3780SDimitry Andric // SelectionDAGISel::runOnMachineFunction.  We have to do it in two places
51344a3780SDimitry Andric // because we want to do it while building the selection DAG for uses of alloca,
52344a3780SDimitry Andric // but not all alloca instructions are used so we have to follow up afterwards.
53e3b55780SDimitry Andric std::optional<unsigned>
getLocalForStackObject(MachineFunction & MF,int FrameIndex)54344a3780SDimitry Andric WebAssemblyFrameLowering::getLocalForStackObject(MachineFunction &MF,
55344a3780SDimitry Andric                                                  int FrameIndex) {
56344a3780SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
57344a3780SDimitry Andric 
58344a3780SDimitry Andric   // If already hoisted to a local, done.
59344a3780SDimitry Andric   if (MFI.getStackID(FrameIndex) == TargetStackID::WasmLocal)
60344a3780SDimitry Andric     return static_cast<unsigned>(MFI.getObjectOffset(FrameIndex));
61344a3780SDimitry Andric 
62344a3780SDimitry Andric   // If not allocated in the object address space, this object will be in
63344a3780SDimitry Andric   // linear memory.
64344a3780SDimitry Andric   const AllocaInst *AI = MFI.getObjectAllocation(FrameIndex);
65e3b55780SDimitry Andric   if (!AI || !WebAssembly::isWasmVarAddressSpace(AI->getAddressSpace()))
66e3b55780SDimitry Andric     return std::nullopt;
67344a3780SDimitry Andric 
68344a3780SDimitry Andric   // Otherwise, allocate this object in the named value stack, outside of linear
69344a3780SDimitry Andric   // memory.
70344a3780SDimitry Andric   SmallVector<EVT, 4> ValueVTs;
71344a3780SDimitry Andric   const WebAssemblyTargetLowering &TLI =
72344a3780SDimitry Andric       *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
73344a3780SDimitry Andric   WebAssemblyFunctionInfo *FuncInfo = MF.getInfo<WebAssemblyFunctionInfo>();
74344a3780SDimitry Andric   ComputeValueVTs(TLI, MF.getDataLayout(), AI->getAllocatedType(), ValueVTs);
75344a3780SDimitry Andric   MFI.setStackID(FrameIndex, TargetStackID::WasmLocal);
76344a3780SDimitry Andric   // Abuse SP offset to record the index of the first local in the object.
77344a3780SDimitry Andric   unsigned Local = FuncInfo->getParams().size() + FuncInfo->getLocals().size();
78344a3780SDimitry Andric   MFI.setObjectOffset(FrameIndex, Local);
79344a3780SDimitry Andric   // Allocate WebAssembly locals for each non-aggregate component of the
80344a3780SDimitry Andric   // allocation.
81344a3780SDimitry Andric   for (EVT ValueVT : ValueVTs)
82344a3780SDimitry Andric     FuncInfo->addLocal(ValueVT.getSimpleVT());
83344a3780SDimitry Andric   // Abuse object size to record number of WebAssembly locals allocated to
84344a3780SDimitry Andric   // this object.
85344a3780SDimitry Andric   MFI.setObjectSize(FrameIndex, ValueVTs.size());
86344a3780SDimitry Andric   return static_cast<unsigned>(Local);
87344a3780SDimitry Andric }
88344a3780SDimitry Andric 
89b915e9e0SDimitry Andric /// We need a base pointer in the case of having items on the stack that
90b915e9e0SDimitry Andric /// require stricter alignment than the stack pointer itself.  Because we need
91b915e9e0SDimitry Andric /// to shift the stack pointer by some unknown amount to force the alignment,
92b915e9e0SDimitry Andric /// we need to record the value of the stack pointer on entry to the function.
hasBP(const MachineFunction & MF) const93d8e91e46SDimitry Andric bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
94b915e9e0SDimitry Andric   const auto *RegInfo =
95b915e9e0SDimitry Andric       MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
96344a3780SDimitry Andric   return RegInfo->hasStackRealignment(MF);
97b915e9e0SDimitry Andric }
98b915e9e0SDimitry Andric 
991a82d4c0SDimitry Andric /// Return true if the specified function should have a dedicated frame pointer
1001a82d4c0SDimitry Andric /// register.
hasFP(const MachineFunction & MF) const1011a82d4c0SDimitry Andric bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
102b915e9e0SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
103b915e9e0SDimitry Andric 
104b915e9e0SDimitry Andric   // When we have var-sized objects, we move the stack pointer by an unknown
105b915e9e0SDimitry Andric   // amount, and need to emit a frame pointer to restore the stack to where we
106b915e9e0SDimitry Andric   // were on function entry.
107b915e9e0SDimitry Andric   // If we already need a base pointer, we use that to fix up the stack pointer.
108b915e9e0SDimitry Andric   // If there are no fixed-size objects, we would have no use of a frame
109b915e9e0SDimitry Andric   // pointer, and thus should not emit one.
110b915e9e0SDimitry Andric   bool HasFixedSizedObjects = MFI.getStackSize() > 0;
111b915e9e0SDimitry Andric   bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
112b915e9e0SDimitry Andric 
113b915e9e0SDimitry Andric   return MFI.isFrameAddressTaken() ||
114b915e9e0SDimitry Andric          (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
115b915e9e0SDimitry Andric          MFI.hasStackMap() || MFI.hasPatchPoint();
1161a82d4c0SDimitry Andric }
1171a82d4c0SDimitry Andric 
1181a82d4c0SDimitry Andric /// Under normal circumstances, when a frame pointer is not required, we reserve
1191a82d4c0SDimitry Andric /// argument space for call sites in the function immediately on entry to the
1201a82d4c0SDimitry Andric /// current function. This eliminates the need for add/sub sp brackets around
1211a82d4c0SDimitry Andric /// call sites. Returns true if the call frame is included as part of the stack
1221a82d4c0SDimitry Andric /// frame.
hasReservedCallFrame(const MachineFunction & MF) const1231a82d4c0SDimitry Andric bool WebAssemblyFrameLowering::hasReservedCallFrame(
1241a82d4c0SDimitry Andric     const MachineFunction &MF) const {
125b915e9e0SDimitry Andric   return !MF.getFrameInfo().hasVarSizedObjects();
1261a82d4c0SDimitry Andric }
1271a82d4c0SDimitry Andric 
128d8e91e46SDimitry Andric // Returns true if this function needs a local user-space stack pointer for its
129d8e91e46SDimitry Andric // local frame (not for exception handling).
needsSPForLocalFrame(const MachineFunction & MF) const130d8e91e46SDimitry Andric bool WebAssemblyFrameLowering::needsSPForLocalFrame(
131d8e91e46SDimitry Andric     const MachineFunction &MF) const {
132d8e91e46SDimitry Andric   auto &MFI = MF.getFrameInfo();
133b1c73532SDimitry Andric   auto &MRI = MF.getRegInfo();
134b1c73532SDimitry Andric   // llvm.stacksave can explicitly read SP register and it can appear without
135b1c73532SDimitry Andric   // dynamic alloca.
136b1c73532SDimitry Andric   bool HasExplicitSPUse =
137b1c73532SDimitry Andric       any_of(MRI.use_operands(getSPReg(MF)),
138b1c73532SDimitry Andric              [](MachineOperand &MO) { return !MO.isImplicit(); });
139b1c73532SDimitry Andric 
140b1c73532SDimitry Andric   return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF) ||
141b1c73532SDimitry Andric          HasExplicitSPUse;
142d8e91e46SDimitry Andric }
143d8e91e46SDimitry Andric 
144d8e91e46SDimitry Andric // In function with EH pads, we need to make a copy of the value of
145cfca06d7SDimitry Andric // __stack_pointer global in SP32/64 register, in order to use it when
146cfca06d7SDimitry Andric // restoring __stack_pointer after an exception is caught.
needsPrologForEH(const MachineFunction & MF) const147d8e91e46SDimitry Andric bool WebAssemblyFrameLowering::needsPrologForEH(
148d8e91e46SDimitry Andric     const MachineFunction &MF) const {
149d8e91e46SDimitry Andric   auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
150d8e91e46SDimitry Andric   return EHType == ExceptionHandling::Wasm &&
151d8e91e46SDimitry Andric          MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
152d8e91e46SDimitry Andric }
153dd58ef01SDimitry Andric 
15401095a5dSDimitry Andric /// Returns true if this function needs a local user-space stack pointer.
15501095a5dSDimitry Andric /// Unlike a machine stack pointer, the wasm user stack pointer is a global
15601095a5dSDimitry Andric /// variable, so it is loaded into a register in the prolog.
needsSP(const MachineFunction & MF) const157d8e91e46SDimitry Andric bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
158d8e91e46SDimitry Andric   return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
15901095a5dSDimitry Andric }
16001095a5dSDimitry Andric 
16101095a5dSDimitry Andric /// Returns true if the local user-space stack pointer needs to be written back
162d8e91e46SDimitry Andric /// to __stack_pointer global by this function (this is not meaningful if
163d8e91e46SDimitry Andric /// needsSP is false). If false, the stack red zone can be used and only a local
164d8e91e46SDimitry Andric /// SP is needed.
needsSPWriteback(const MachineFunction & MF) const16501095a5dSDimitry Andric bool WebAssemblyFrameLowering::needsSPWriteback(
166d8e91e46SDimitry Andric     const MachineFunction &MF) const {
167d8e91e46SDimitry Andric   auto &MFI = MF.getFrameInfo();
168d8e91e46SDimitry Andric   assert(needsSP(MF));
169d8e91e46SDimitry Andric   // When we don't need a local stack pointer for its local frame but only to
170d8e91e46SDimitry Andric   // support EH, we don't need to write SP back in the epilog, because we don't
171d8e91e46SDimitry Andric   // bump down the stack pointer in the prolog. We need to write SP back in the
172d8e91e46SDimitry Andric   // epilog only if
173d8e91e46SDimitry Andric   // 1. We need SP not only for EH support but also because we actually use
174d8e91e46SDimitry Andric   // stack or we have a frame address taken.
175d8e91e46SDimitry Andric   // 2. We cannot use the red zone.
176d8e91e46SDimitry Andric   bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
177d8e91e46SDimitry Andric                        !MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
178d8e91e46SDimitry Andric   return needsSPForLocalFrame(MF) && !CanUseRedZone;
17901095a5dSDimitry Andric }
18001095a5dSDimitry Andric 
getSPReg(const MachineFunction & MF)181cfca06d7SDimitry Andric unsigned WebAssemblyFrameLowering::getSPReg(const MachineFunction &MF) {
182cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
183cfca06d7SDimitry Andric              ? WebAssembly::SP64
184cfca06d7SDimitry Andric              : WebAssembly::SP32;
185cfca06d7SDimitry Andric }
186cfca06d7SDimitry Andric 
getFPReg(const MachineFunction & MF)187cfca06d7SDimitry Andric unsigned WebAssemblyFrameLowering::getFPReg(const MachineFunction &MF) {
188cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
189cfca06d7SDimitry Andric              ? WebAssembly::FP64
190cfca06d7SDimitry Andric              : WebAssembly::FP32;
191cfca06d7SDimitry Andric }
192cfca06d7SDimitry Andric 
193cfca06d7SDimitry Andric unsigned
getOpcConst(const MachineFunction & MF)194cfca06d7SDimitry Andric WebAssemblyFrameLowering::getOpcConst(const MachineFunction &MF) {
195cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
196cfca06d7SDimitry Andric              ? WebAssembly::CONST_I64
197cfca06d7SDimitry Andric              : WebAssembly::CONST_I32;
198cfca06d7SDimitry Andric }
199cfca06d7SDimitry Andric 
getOpcAdd(const MachineFunction & MF)200cfca06d7SDimitry Andric unsigned WebAssemblyFrameLowering::getOpcAdd(const MachineFunction &MF) {
201cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
202cfca06d7SDimitry Andric              ? WebAssembly::ADD_I64
203cfca06d7SDimitry Andric              : WebAssembly::ADD_I32;
204cfca06d7SDimitry Andric }
205cfca06d7SDimitry Andric 
getOpcSub(const MachineFunction & MF)206cfca06d7SDimitry Andric unsigned WebAssemblyFrameLowering::getOpcSub(const MachineFunction &MF) {
207cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
208cfca06d7SDimitry Andric              ? WebAssembly::SUB_I64
209cfca06d7SDimitry Andric              : WebAssembly::SUB_I32;
210cfca06d7SDimitry Andric }
211cfca06d7SDimitry Andric 
getOpcAnd(const MachineFunction & MF)212cfca06d7SDimitry Andric unsigned WebAssemblyFrameLowering::getOpcAnd(const MachineFunction &MF) {
213cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
214cfca06d7SDimitry Andric              ? WebAssembly::AND_I64
215cfca06d7SDimitry Andric              : WebAssembly::AND_I32;
216cfca06d7SDimitry Andric }
217cfca06d7SDimitry Andric 
218cfca06d7SDimitry Andric unsigned
getOpcGlobGet(const MachineFunction & MF)219cfca06d7SDimitry Andric WebAssemblyFrameLowering::getOpcGlobGet(const MachineFunction &MF) {
220cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
221cfca06d7SDimitry Andric              ? WebAssembly::GLOBAL_GET_I64
222cfca06d7SDimitry Andric              : WebAssembly::GLOBAL_GET_I32;
223cfca06d7SDimitry Andric }
224cfca06d7SDimitry Andric 
225cfca06d7SDimitry Andric unsigned
getOpcGlobSet(const MachineFunction & MF)226cfca06d7SDimitry Andric WebAssemblyFrameLowering::getOpcGlobSet(const MachineFunction &MF) {
227cfca06d7SDimitry Andric   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
228cfca06d7SDimitry Andric              ? WebAssembly::GLOBAL_SET_I64
229cfca06d7SDimitry Andric              : WebAssembly::GLOBAL_SET_I32;
230cfca06d7SDimitry Andric }
231cfca06d7SDimitry Andric 
writeSPToGlobal(unsigned SrcReg,MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator & InsertStore,const DebugLoc & DL) const232d8e91e46SDimitry Andric void WebAssemblyFrameLowering::writeSPToGlobal(
233d8e91e46SDimitry Andric     unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
234d8e91e46SDimitry Andric     MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
23571d5a254SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
23671d5a254SDimitry Andric 
23701095a5dSDimitry Andric   const char *ES = "__stack_pointer";
23801095a5dSDimitry Andric   auto *SPSymbol = MF.createExternalSymbolName(ES);
239cfca06d7SDimitry Andric 
240cfca06d7SDimitry Andric   BuildMI(MBB, InsertStore, DL, TII->get(getOpcGlobSet(MF)))
241e6d15924SDimitry Andric       .addExternalSymbol(SPSymbol)
24271d5a254SDimitry Andric       .addReg(SrcReg);
24371d5a254SDimitry Andric }
244dd58ef01SDimitry Andric 
24501095a5dSDimitry Andric MachineBasicBlock::iterator
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const24601095a5dSDimitry Andric WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
2471a82d4c0SDimitry Andric     MachineFunction &MF, MachineBasicBlock &MBB,
2481a82d4c0SDimitry Andric     MachineBasicBlock::iterator I) const {
249b915e9e0SDimitry Andric   assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
25001095a5dSDimitry Andric          "Call frame pseudos should only be used for dynamic stack adjustment");
251cfca06d7SDimitry Andric   auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
252cfca06d7SDimitry Andric   const auto *TII = ST.getInstrInfo();
25301095a5dSDimitry Andric   if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
254d8e91e46SDimitry Andric       needsSPWriteback(MF)) {
255dd58ef01SDimitry Andric     DebugLoc DL = I->getDebugLoc();
256cfca06d7SDimitry Andric     writeSPToGlobal(getSPReg(MF), MF, MBB, I, DL);
25701095a5dSDimitry Andric   }
25801095a5dSDimitry Andric   return MBB.erase(I);
2591a82d4c0SDimitry Andric }
2601a82d4c0SDimitry Andric 
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const2611a82d4c0SDimitry Andric void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
2621a82d4c0SDimitry Andric                                             MachineBasicBlock &MBB) const {
263dd58ef01SDimitry Andric   // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
264b915e9e0SDimitry Andric   auto &MFI = MF.getFrameInfo();
265b915e9e0SDimitry Andric   assert(MFI.getCalleeSavedInfo().empty() &&
266dd58ef01SDimitry Andric          "WebAssembly should not have callee-saved registers");
267dd58ef01SDimitry Andric 
268d8e91e46SDimitry Andric   if (!needsSP(MF))
269d8e91e46SDimitry Andric     return;
270b915e9e0SDimitry Andric   uint64_t StackSize = MFI.getStackSize();
27101095a5dSDimitry Andric 
272cfca06d7SDimitry Andric   auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
273cfca06d7SDimitry Andric   const auto *TII = ST.getInstrInfo();
27401095a5dSDimitry Andric   auto &MRI = MF.getRegInfo();
275dd58ef01SDimitry Andric 
276dd58ef01SDimitry Andric   auto InsertPt = MBB.begin();
277e6d15924SDimitry Andric   while (InsertPt != MBB.end() &&
278e6d15924SDimitry Andric          WebAssembly::isArgument(InsertPt->getOpcode()))
27971d5a254SDimitry Andric     ++InsertPt;
280dd58ef01SDimitry Andric   DebugLoc DL;
281dd58ef01SDimitry Andric 
28201095a5dSDimitry Andric   const TargetRegisterClass *PtrRC =
28301095a5dSDimitry Andric       MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
284cfca06d7SDimitry Andric   unsigned SPReg = getSPReg(MF);
285b915e9e0SDimitry Andric   if (StackSize)
286b915e9e0SDimitry Andric     SPReg = MRI.createVirtualRegister(PtrRC);
28708bbd35aSDimitry Andric 
28801095a5dSDimitry Andric   const char *ES = "__stack_pointer";
28901095a5dSDimitry Andric   auto *SPSymbol = MF.createExternalSymbolName(ES);
290cfca06d7SDimitry Andric   BuildMI(MBB, InsertPt, DL, TII->get(getOpcGlobGet(MF)), SPReg)
291e6d15924SDimitry Andric       .addExternalSymbol(SPSymbol);
29201095a5dSDimitry Andric 
293b915e9e0SDimitry Andric   bool HasBP = hasBP(MF);
294b915e9e0SDimitry Andric   if (HasBP) {
295b915e9e0SDimitry Andric     auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
2961d5ae102SDimitry Andric     Register BasePtr = MRI.createVirtualRegister(PtrRC);
297b915e9e0SDimitry Andric     FI->setBasePointerVreg(BasePtr);
298b915e9e0SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
299b915e9e0SDimitry Andric         .addReg(SPReg);
300b915e9e0SDimitry Andric   }
30101095a5dSDimitry Andric   if (StackSize) {
30201095a5dSDimitry Andric     // Subtract the frame size
3031d5ae102SDimitry Andric     Register OffsetReg = MRI.createVirtualRegister(PtrRC);
304cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
30501095a5dSDimitry Andric         .addImm(StackSize);
306cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(getOpcSub(MF)), getSPReg(MF))
30701095a5dSDimitry Andric         .addReg(SPReg)
30801095a5dSDimitry Andric         .addReg(OffsetReg);
30901095a5dSDimitry Andric   }
310b915e9e0SDimitry Andric   if (HasBP) {
3111d5ae102SDimitry Andric     Register BitmaskReg = MRI.createVirtualRegister(PtrRC);
312cfca06d7SDimitry Andric     Align Alignment = MFI.getMaxAlign();
313cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), BitmaskReg)
314cfca06d7SDimitry Andric         .addImm((int64_t) ~(Alignment.value() - 1));
315cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(getOpcAnd(MF)), getSPReg(MF))
316cfca06d7SDimitry Andric         .addReg(getSPReg(MF))
317b915e9e0SDimitry Andric         .addReg(BitmaskReg);
318b915e9e0SDimitry Andric   }
31901095a5dSDimitry Andric   if (hasFP(MF)) {
32001095a5dSDimitry Andric     // Unlike most conventional targets (where FP points to the saved FP),
32101095a5dSDimitry Andric     // FP points to the bottom of the fixed-size locals, so we can use positive
32201095a5dSDimitry Andric     // offsets in load/store instructions.
323cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), getFPReg(MF))
324cfca06d7SDimitry Andric         .addReg(getSPReg(MF));
32501095a5dSDimitry Andric   }
326d8e91e46SDimitry Andric   if (StackSize && needsSPWriteback(MF)) {
327cfca06d7SDimitry Andric     writeSPToGlobal(getSPReg(MF), MF, MBB, InsertPt, DL);
32801095a5dSDimitry Andric   }
3291a82d4c0SDimitry Andric }
3301a82d4c0SDimitry Andric 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const3311a82d4c0SDimitry Andric void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
3321a82d4c0SDimitry Andric                                             MachineBasicBlock &MBB) const {
333d8e91e46SDimitry Andric   uint64_t StackSize = MF.getFrameInfo().getStackSize();
334d8e91e46SDimitry Andric   if (!needsSP(MF) || !needsSPWriteback(MF))
335d8e91e46SDimitry Andric     return;
336cfca06d7SDimitry Andric   auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
337cfca06d7SDimitry Andric   const auto *TII = ST.getInstrInfo();
338dd58ef01SDimitry Andric   auto &MRI = MF.getRegInfo();
339dd58ef01SDimitry Andric   auto InsertPt = MBB.getFirstTerminator();
340dd58ef01SDimitry Andric   DebugLoc DL;
341dd58ef01SDimitry Andric 
34201095a5dSDimitry Andric   if (InsertPt != MBB.end())
343dd58ef01SDimitry Andric     DL = InsertPt->getDebugLoc();
3441a82d4c0SDimitry Andric 
34501095a5dSDimitry Andric   // Restore the stack pointer. If we had fixed-size locals, add the offset
34601095a5dSDimitry Andric   // subtracted in the prolog.
34701095a5dSDimitry Andric   unsigned SPReg = 0;
348cfca06d7SDimitry Andric   unsigned SPFPReg = hasFP(MF) ? getFPReg(MF) : getSPReg(MF);
349b915e9e0SDimitry Andric   if (hasBP(MF)) {
350b915e9e0SDimitry Andric     auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
351b915e9e0SDimitry Andric     SPReg = FI->getBasePointerVreg();
352b915e9e0SDimitry Andric   } else if (StackSize) {
35301095a5dSDimitry Andric     const TargetRegisterClass *PtrRC =
35401095a5dSDimitry Andric         MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
3551d5ae102SDimitry Andric     Register OffsetReg = MRI.createVirtualRegister(PtrRC);
356cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
357dd58ef01SDimitry Andric         .addImm(StackSize);
358cfca06d7SDimitry Andric     // In the epilog we don't need to write the result back to the SP32/64
359cfca06d7SDimitry Andric     // physreg because it won't be used again. We can use a stackified register
360cfca06d7SDimitry Andric     // instead.
36101095a5dSDimitry Andric     SPReg = MRI.createVirtualRegister(PtrRC);
362cfca06d7SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(getOpcAdd(MF)), SPReg)
363cfca06d7SDimitry Andric         .addReg(SPFPReg)
364dd58ef01SDimitry Andric         .addReg(OffsetReg);
36501095a5dSDimitry Andric   } else {
366cfca06d7SDimitry Andric     SPReg = SPFPReg;
36701095a5dSDimitry Andric   }
36801095a5dSDimitry Andric 
369d8e91e46SDimitry Andric   writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
3701a82d4c0SDimitry Andric }
371cfca06d7SDimitry Andric 
isSupportedStackID(TargetStackID::Value ID) const372344a3780SDimitry Andric bool WebAssemblyFrameLowering::isSupportedStackID(
373344a3780SDimitry Andric     TargetStackID::Value ID) const {
374344a3780SDimitry Andric   // Use the Object stack for WebAssembly locals which can only be accessed
375344a3780SDimitry Andric   // by name, not via an address in linear memory.
376344a3780SDimitry Andric   if (ID == TargetStackID::WasmLocal)
377344a3780SDimitry Andric     return true;
378344a3780SDimitry Andric 
379344a3780SDimitry Andric   return TargetFrameLowering::isSupportedStackID(ID);
380344a3780SDimitry Andric }
381344a3780SDimitry Andric 
382cfca06d7SDimitry Andric TargetFrameLowering::DwarfFrameBase
getDwarfFrameBase(const MachineFunction & MF) const383cfca06d7SDimitry Andric WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
384cfca06d7SDimitry Andric   DwarfFrameBase Loc;
385cfca06d7SDimitry Andric   Loc.Kind = DwarfFrameBase::WasmFrameBase;
386cfca06d7SDimitry Andric   const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
387cfca06d7SDimitry Andric   if (needsSP(MF) && MFI.isFrameBaseVirtual()) {
388cfca06d7SDimitry Andric     unsigned LocalNum = MFI.getFrameBaseLocal();
389cfca06d7SDimitry Andric     Loc.Location.WasmLoc = {WebAssembly::TI_LOCAL, LocalNum};
390cfca06d7SDimitry Andric   } else {
391cfca06d7SDimitry Andric     // TODO: This should work on a breakpoint at a function with no frame,
392cfca06d7SDimitry Andric     // but probably won't work for traversing up the stack.
393cfca06d7SDimitry Andric     Loc.Location.WasmLoc = {WebAssembly::TI_GLOBAL_RELOC, 0};
394cfca06d7SDimitry Andric   }
395cfca06d7SDimitry Andric   return Loc;
396cfca06d7SDimitry Andric }
397