13dac3a9bSDimitry Andric //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
23dac3a9bSDimitry Andric //
33dac3a9bSDimitry Andric //                     The LLVM Compiler Infrastructure
43dac3a9bSDimitry Andric //
53dac3a9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
63dac3a9bSDimitry Andric // License. See LICENSE.TXT for details.
73dac3a9bSDimitry Andric //
83dac3a9bSDimitry Andric //===----------------------------------------------------------------------===//
93dac3a9bSDimitry Andric ///
103dac3a9bSDimitry Andric /// \file
114ba319b5SDimitry Andric /// This file contains the WebAssembly implementation of
123dac3a9bSDimitry Andric /// TargetFrameLowering class.
133dac3a9bSDimitry Andric ///
143dac3a9bSDimitry Andric /// On WebAssembly, there aren't a lot of things to do here. There are no
153dac3a9bSDimitry Andric /// callee-saved registers to save, and no spill slots.
163dac3a9bSDimitry Andric ///
173dac3a9bSDimitry Andric /// The stack grows downward.
183dac3a9bSDimitry Andric ///
193dac3a9bSDimitry Andric //===----------------------------------------------------------------------===//
203dac3a9bSDimitry Andric 
213dac3a9bSDimitry Andric #include "WebAssemblyFrameLowering.h"
223dac3a9bSDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
233dac3a9bSDimitry Andric #include "WebAssemblyInstrInfo.h"
243dac3a9bSDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
253dac3a9bSDimitry Andric #include "WebAssemblySubtarget.h"
263dac3a9bSDimitry Andric #include "WebAssemblyTargetMachine.h"
277a7e6055SDimitry Andric #include "WebAssemblyUtilities.h"
283dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
293dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
303dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
317a7e6055SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
323dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
33*b5893f02SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
343dac3a9bSDimitry Andric #include "llvm/Support/Debug.h"
353dac3a9bSDimitry Andric using namespace llvm;
363dac3a9bSDimitry Andric 
373dac3a9bSDimitry Andric #define DEBUG_TYPE "wasm-frame-info"
383dac3a9bSDimitry Andric 
397d523365SDimitry Andric // TODO: wasm64
407d523365SDimitry Andric // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
413dac3a9bSDimitry Andric 
42d88c1a5aSDimitry Andric /// We need a base pointer in the case of having items on the stack that
43d88c1a5aSDimitry Andric /// require stricter alignment than the stack pointer itself.  Because we need
44d88c1a5aSDimitry Andric /// to shift the stack pointer by some unknown amount to force the alignment,
45d88c1a5aSDimitry Andric /// we need to record the value of the stack pointer on entry to the function.
hasBP(const MachineFunction & MF) const46*b5893f02SDimitry Andric bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
47d88c1a5aSDimitry Andric   const auto *RegInfo =
48d88c1a5aSDimitry Andric       MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
49d88c1a5aSDimitry Andric   return RegInfo->needsStackRealignment(MF);
50d88c1a5aSDimitry Andric }
51d88c1a5aSDimitry Andric 
523dac3a9bSDimitry Andric /// Return true if the specified function should have a dedicated frame pointer
533dac3a9bSDimitry Andric /// register.
hasFP(const MachineFunction & MF) const543dac3a9bSDimitry Andric bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
55d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
56d88c1a5aSDimitry Andric 
57d88c1a5aSDimitry Andric   // When we have var-sized objects, we move the stack pointer by an unknown
58d88c1a5aSDimitry Andric   // amount, and need to emit a frame pointer to restore the stack to where we
59d88c1a5aSDimitry Andric   // were on function entry.
60d88c1a5aSDimitry Andric   // If we already need a base pointer, we use that to fix up the stack pointer.
61d88c1a5aSDimitry Andric   // If there are no fixed-size objects, we would have no use of a frame
62d88c1a5aSDimitry Andric   // pointer, and thus should not emit one.
63d88c1a5aSDimitry Andric   bool HasFixedSizedObjects = MFI.getStackSize() > 0;
64d88c1a5aSDimitry Andric   bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
65d88c1a5aSDimitry Andric 
66d88c1a5aSDimitry Andric   return MFI.isFrameAddressTaken() ||
67d88c1a5aSDimitry Andric          (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
68d88c1a5aSDimitry Andric          MFI.hasStackMap() || MFI.hasPatchPoint();
693dac3a9bSDimitry Andric }
703dac3a9bSDimitry Andric 
713dac3a9bSDimitry Andric /// Under normal circumstances, when a frame pointer is not required, we reserve
723dac3a9bSDimitry Andric /// argument space for call sites in the function immediately on entry to the
733dac3a9bSDimitry Andric /// current function. This eliminates the need for add/sub sp brackets around
743dac3a9bSDimitry Andric /// call sites. Returns true if the call frame is included as part of the stack
753dac3a9bSDimitry Andric /// frame.
hasReservedCallFrame(const MachineFunction & MF) const763dac3a9bSDimitry Andric bool WebAssemblyFrameLowering::hasReservedCallFrame(
773dac3a9bSDimitry Andric     const MachineFunction &MF) const {
78d88c1a5aSDimitry Andric   return !MF.getFrameInfo().hasVarSizedObjects();
793dac3a9bSDimitry Andric }
803dac3a9bSDimitry Andric 
81*b5893f02SDimitry Andric // Returns true if this function needs a local user-space stack pointer for its
82*b5893f02SDimitry Andric // local frame (not for exception handling).
needsSPForLocalFrame(const MachineFunction & MF) const83*b5893f02SDimitry Andric bool WebAssemblyFrameLowering::needsSPForLocalFrame(
84*b5893f02SDimitry Andric     const MachineFunction &MF) const {
85*b5893f02SDimitry Andric   auto &MFI = MF.getFrameInfo();
86*b5893f02SDimitry Andric   return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
87*b5893f02SDimitry Andric }
88*b5893f02SDimitry Andric 
89*b5893f02SDimitry Andric // In function with EH pads, we need to make a copy of the value of
90*b5893f02SDimitry Andric // __stack_pointer global in SP32 register, in order to use it when restoring
91*b5893f02SDimitry Andric // __stack_pointer after an exception is caught.
needsPrologForEH(const MachineFunction & MF) const92*b5893f02SDimitry Andric bool WebAssemblyFrameLowering::needsPrologForEH(
93*b5893f02SDimitry Andric     const MachineFunction &MF) const {
94*b5893f02SDimitry Andric   auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
95*b5893f02SDimitry Andric   return EHType == ExceptionHandling::Wasm &&
96*b5893f02SDimitry Andric          MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
97*b5893f02SDimitry Andric }
987d523365SDimitry Andric 
993ca95b02SDimitry Andric /// Returns true if this function needs a local user-space stack pointer.
1003ca95b02SDimitry Andric /// Unlike a machine stack pointer, the wasm user stack pointer is a global
1013ca95b02SDimitry Andric /// variable, so it is loaded into a register in the prolog.
needsSP(const MachineFunction & MF) const102*b5893f02SDimitry Andric bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
103*b5893f02SDimitry Andric   return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
1043ca95b02SDimitry Andric }
1053ca95b02SDimitry Andric 
1063ca95b02SDimitry Andric /// Returns true if the local user-space stack pointer needs to be written back
107*b5893f02SDimitry Andric /// to __stack_pointer global by this function (this is not meaningful if
108*b5893f02SDimitry Andric /// needsSP is false). If false, the stack red zone can be used and only a local
109*b5893f02SDimitry Andric /// SP is needed.
needsSPWriteback(const MachineFunction & MF) const1103ca95b02SDimitry Andric bool WebAssemblyFrameLowering::needsSPWriteback(
111*b5893f02SDimitry Andric     const MachineFunction &MF) const {
112*b5893f02SDimitry Andric   auto &MFI = MF.getFrameInfo();
113*b5893f02SDimitry Andric   assert(needsSP(MF));
114*b5893f02SDimitry Andric   // When we don't need a local stack pointer for its local frame but only to
115*b5893f02SDimitry Andric   // support EH, we don't need to write SP back in the epilog, because we don't
116*b5893f02SDimitry Andric   // bump down the stack pointer in the prolog. We need to write SP back in the
117*b5893f02SDimitry Andric   // epilog only if
118*b5893f02SDimitry Andric   // 1. We need SP not only for EH support but also because we actually use
119*b5893f02SDimitry Andric   // stack or we have a frame address taken.
120*b5893f02SDimitry Andric   // 2. We cannot use the red zone.
121*b5893f02SDimitry Andric   bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
122*b5893f02SDimitry Andric                        !MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
123*b5893f02SDimitry Andric   return needsSPForLocalFrame(MF) && !CanUseRedZone;
1243ca95b02SDimitry Andric }
1253ca95b02SDimitry Andric 
writeSPToGlobal(unsigned SrcReg,MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator & InsertStore,const DebugLoc & DL) const126*b5893f02SDimitry Andric void WebAssemblyFrameLowering::writeSPToGlobal(
127*b5893f02SDimitry Andric     unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
128*b5893f02SDimitry Andric     MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
1297a7e6055SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1307a7e6055SDimitry Andric 
1313ca95b02SDimitry Andric   const char *ES = "__stack_pointer";
1323ca95b02SDimitry Andric   auto *SPSymbol = MF.createExternalSymbolName(ES);
133*b5893f02SDimitry Andric   BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::GLOBAL_SET_I32))
134*b5893f02SDimitry Andric       .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL)
1357a7e6055SDimitry Andric       .addReg(SrcReg);
1367a7e6055SDimitry Andric }
1377d523365SDimitry Andric 
1383ca95b02SDimitry Andric MachineBasicBlock::iterator
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const1393ca95b02SDimitry Andric WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
1403dac3a9bSDimitry Andric     MachineFunction &MF, MachineBasicBlock &MBB,
1413dac3a9bSDimitry Andric     MachineBasicBlock::iterator I) const {
142d88c1a5aSDimitry Andric   assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
1433ca95b02SDimitry Andric          "Call frame pseudos should only be used for dynamic stack adjustment");
1443ca95b02SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1453ca95b02SDimitry Andric   if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
146*b5893f02SDimitry Andric       needsSPWriteback(MF)) {
1477d523365SDimitry Andric     DebugLoc DL = I->getDebugLoc();
148*b5893f02SDimitry Andric     writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL);
1493ca95b02SDimitry Andric   }
1503ca95b02SDimitry Andric   return MBB.erase(I);
1513dac3a9bSDimitry Andric }
1523dac3a9bSDimitry Andric 
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const1533dac3a9bSDimitry Andric void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
1543dac3a9bSDimitry Andric                                             MachineBasicBlock &MBB) const {
1557d523365SDimitry Andric   // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
156d88c1a5aSDimitry Andric   auto &MFI = MF.getFrameInfo();
157d88c1a5aSDimitry Andric   assert(MFI.getCalleeSavedInfo().empty() &&
1587d523365SDimitry Andric          "WebAssembly should not have callee-saved registers");
1597d523365SDimitry Andric 
160*b5893f02SDimitry Andric   if (!needsSP(MF))
161*b5893f02SDimitry Andric     return;
162d88c1a5aSDimitry Andric   uint64_t StackSize = MFI.getStackSize();
1633ca95b02SDimitry Andric 
1643ca95b02SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1653ca95b02SDimitry Andric   auto &MRI = MF.getRegInfo();
1667d523365SDimitry Andric 
1677d523365SDimitry Andric   auto InsertPt = MBB.begin();
1687a7e6055SDimitry Andric   while (InsertPt != MBB.end() && WebAssembly::isArgument(*InsertPt))
1697a7e6055SDimitry Andric     ++InsertPt;
1707d523365SDimitry Andric   DebugLoc DL;
1717d523365SDimitry Andric 
1723ca95b02SDimitry Andric   const TargetRegisterClass *PtrRC =
1733ca95b02SDimitry Andric       MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
174d88c1a5aSDimitry Andric   unsigned SPReg = WebAssembly::SP32;
175d88c1a5aSDimitry Andric   if (StackSize)
176d88c1a5aSDimitry Andric     SPReg = MRI.createVirtualRegister(PtrRC);
177edd7eaddSDimitry Andric 
1783ca95b02SDimitry Andric   const char *ES = "__stack_pointer";
1793ca95b02SDimitry Andric   auto *SPSymbol = MF.createExternalSymbolName(ES);
180*b5893f02SDimitry Andric   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GLOBAL_GET_I32), SPReg)
181*b5893f02SDimitry Andric       .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL);
1823ca95b02SDimitry Andric 
183d88c1a5aSDimitry Andric   bool HasBP = hasBP(MF);
184d88c1a5aSDimitry Andric   if (HasBP) {
185d88c1a5aSDimitry Andric     auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
186d88c1a5aSDimitry Andric     unsigned BasePtr = MRI.createVirtualRegister(PtrRC);
187d88c1a5aSDimitry Andric     FI->setBasePointerVreg(BasePtr);
188d88c1a5aSDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
189d88c1a5aSDimitry Andric         .addReg(SPReg);
190d88c1a5aSDimitry Andric   }
1913ca95b02SDimitry Andric   if (StackSize) {
1923ca95b02SDimitry Andric     // Subtract the frame size
1933ca95b02SDimitry Andric     unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
1943ca95b02SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
1953ca95b02SDimitry Andric         .addImm(StackSize);
1963ca95b02SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32),
1973ca95b02SDimitry Andric             WebAssembly::SP32)
1983ca95b02SDimitry Andric         .addReg(SPReg)
1993ca95b02SDimitry Andric         .addReg(OffsetReg);
2003ca95b02SDimitry Andric   }
201d88c1a5aSDimitry Andric   if (HasBP) {
202d88c1a5aSDimitry Andric     unsigned BitmaskReg = MRI.createVirtualRegister(PtrRC);
203d88c1a5aSDimitry Andric     unsigned Alignment = MFI.getMaxAlignment();
204d88c1a5aSDimitry Andric     assert((1u << countTrailingZeros(Alignment)) == Alignment &&
205d88c1a5aSDimitry Andric            "Alignment must be a power of 2");
206d88c1a5aSDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg)
207d88c1a5aSDimitry Andric         .addImm((int)~(Alignment - 1));
208d88c1a5aSDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32),
209d88c1a5aSDimitry Andric             WebAssembly::SP32)
210d88c1a5aSDimitry Andric         .addReg(WebAssembly::SP32)
211d88c1a5aSDimitry Andric         .addReg(BitmaskReg);
212d88c1a5aSDimitry Andric   }
2133ca95b02SDimitry Andric   if (hasFP(MF)) {
2143ca95b02SDimitry Andric     // Unlike most conventional targets (where FP points to the saved FP),
2153ca95b02SDimitry Andric     // FP points to the bottom of the fixed-size locals, so we can use positive
2163ca95b02SDimitry Andric     // offsets in load/store instructions.
217*b5893f02SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), WebAssembly::FP32)
2183ca95b02SDimitry Andric         .addReg(WebAssembly::SP32);
2193ca95b02SDimitry Andric   }
220*b5893f02SDimitry Andric   if (StackSize && needsSPWriteback(MF)) {
221*b5893f02SDimitry Andric     writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL);
2223ca95b02SDimitry Andric   }
2233dac3a9bSDimitry Andric }
2243dac3a9bSDimitry Andric 
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const2253dac3a9bSDimitry Andric void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
2263dac3a9bSDimitry Andric                                             MachineBasicBlock &MBB) const {
227*b5893f02SDimitry Andric   uint64_t StackSize = MF.getFrameInfo().getStackSize();
228*b5893f02SDimitry Andric   if (!needsSP(MF) || !needsSPWriteback(MF))
229*b5893f02SDimitry Andric     return;
2303ca95b02SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
2317d523365SDimitry Andric   auto &MRI = MF.getRegInfo();
2327d523365SDimitry Andric   auto InsertPt = MBB.getFirstTerminator();
2337d523365SDimitry Andric   DebugLoc DL;
2347d523365SDimitry Andric 
2353ca95b02SDimitry Andric   if (InsertPt != MBB.end())
2367d523365SDimitry Andric     DL = InsertPt->getDebugLoc();
2373dac3a9bSDimitry Andric 
2383ca95b02SDimitry Andric   // Restore the stack pointer. If we had fixed-size locals, add the offset
2393ca95b02SDimitry Andric   // subtracted in the prolog.
2403ca95b02SDimitry Andric   unsigned SPReg = 0;
241d88c1a5aSDimitry Andric   if (hasBP(MF)) {
242d88c1a5aSDimitry Andric     auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
243d88c1a5aSDimitry Andric     SPReg = FI->getBasePointerVreg();
244d88c1a5aSDimitry Andric   } else if (StackSize) {
2453ca95b02SDimitry Andric     const TargetRegisterClass *PtrRC =
2463ca95b02SDimitry Andric         MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
2473ca95b02SDimitry Andric     unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
2487d523365SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
2497d523365SDimitry Andric         .addImm(StackSize);
2503ca95b02SDimitry Andric     // In the epilog we don't need to write the result back to the SP32 physreg
2513ca95b02SDimitry Andric     // because it won't be used again. We can use a stackified register instead.
2523ca95b02SDimitry Andric     SPReg = MRI.createVirtualRegister(PtrRC);
2533ca95b02SDimitry Andric     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg)
2543ca95b02SDimitry Andric         .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32)
2557d523365SDimitry Andric         .addReg(OffsetReg);
2563ca95b02SDimitry Andric   } else {
2573ca95b02SDimitry Andric     SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
2583ca95b02SDimitry Andric   }
2593ca95b02SDimitry Andric 
260*b5893f02SDimitry Andric   writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
2613dac3a9bSDimitry Andric }
262