10b57cec5SDimitry Andric //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This file contains the WebAssembly implementation of 110b57cec5SDimitry Andric /// TargetFrameLowering class. 120b57cec5SDimitry Andric /// 130b57cec5SDimitry Andric /// On WebAssembly, there aren't a lot of things to do here. There are no 140b57cec5SDimitry Andric /// callee-saved registers to save, and no spill slots. 150b57cec5SDimitry Andric /// 160b57cec5SDimitry Andric /// The stack grows downward. 170b57cec5SDimitry Andric /// 180b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #include "WebAssemblyFrameLowering.h" 210b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 220b57cec5SDimitry Andric #include "WebAssemblyInstrInfo.h" 230b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h" 240b57cec5SDimitry Andric #include "WebAssemblySubtarget.h" 250b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h" 260b57cec5SDimitry Andric #include "WebAssemblyUtilities.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 320b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 330b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-frame-info" 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric // TODO: wasm64 390b57cec5SDimitry Andric // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric /// We need a base pointer in the case of having items on the stack that 420b57cec5SDimitry Andric /// require stricter alignment than the stack pointer itself. Because we need 430b57cec5SDimitry Andric /// to shift the stack pointer by some unknown amount to force the alignment, 440b57cec5SDimitry Andric /// we need to record the value of the stack pointer on entry to the function. 450b57cec5SDimitry Andric bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const { 460b57cec5SDimitry Andric const auto *RegInfo = 470b57cec5SDimitry Andric MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); 480b57cec5SDimitry Andric return RegInfo->needsStackRealignment(MF); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric /// Return true if the specified function should have a dedicated frame pointer 520b57cec5SDimitry Andric /// register. 530b57cec5SDimitry Andric bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const { 540b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric // When we have var-sized objects, we move the stack pointer by an unknown 570b57cec5SDimitry Andric // amount, and need to emit a frame pointer to restore the stack to where we 580b57cec5SDimitry Andric // were on function entry. 590b57cec5SDimitry Andric // If we already need a base pointer, we use that to fix up the stack pointer. 600b57cec5SDimitry Andric // If there are no fixed-size objects, we would have no use of a frame 610b57cec5SDimitry Andric // pointer, and thus should not emit one. 620b57cec5SDimitry Andric bool HasFixedSizedObjects = MFI.getStackSize() > 0; 630b57cec5SDimitry Andric bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric return MFI.isFrameAddressTaken() || 660b57cec5SDimitry Andric (MFI.hasVarSizedObjects() && NeedsFixedReference) || 670b57cec5SDimitry Andric MFI.hasStackMap() || MFI.hasPatchPoint(); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric /// Under normal circumstances, when a frame pointer is not required, we reserve 710b57cec5SDimitry Andric /// argument space for call sites in the function immediately on entry to the 720b57cec5SDimitry Andric /// current function. This eliminates the need for add/sub sp brackets around 730b57cec5SDimitry Andric /// call sites. Returns true if the call frame is included as part of the stack 740b57cec5SDimitry Andric /// frame. 750b57cec5SDimitry Andric bool WebAssemblyFrameLowering::hasReservedCallFrame( 760b57cec5SDimitry Andric const MachineFunction &MF) const { 770b57cec5SDimitry Andric return !MF.getFrameInfo().hasVarSizedObjects(); 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Returns true if this function needs a local user-space stack pointer for its 810b57cec5SDimitry Andric // local frame (not for exception handling). 820b57cec5SDimitry Andric bool WebAssemblyFrameLowering::needsSPForLocalFrame( 830b57cec5SDimitry Andric const MachineFunction &MF) const { 840b57cec5SDimitry Andric auto &MFI = MF.getFrameInfo(); 850b57cec5SDimitry Andric return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF); 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric // In function with EH pads, we need to make a copy of the value of 890b57cec5SDimitry Andric // __stack_pointer global in SP32 register, in order to use it when restoring 900b57cec5SDimitry Andric // __stack_pointer after an exception is caught. 910b57cec5SDimitry Andric bool WebAssemblyFrameLowering::needsPrologForEH( 920b57cec5SDimitry Andric const MachineFunction &MF) const { 930b57cec5SDimitry Andric auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType(); 940b57cec5SDimitry Andric return EHType == ExceptionHandling::Wasm && 950b57cec5SDimitry Andric MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls(); 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric /// Returns true if this function needs a local user-space stack pointer. 990b57cec5SDimitry Andric /// Unlike a machine stack pointer, the wasm user stack pointer is a global 1000b57cec5SDimitry Andric /// variable, so it is loaded into a register in the prolog. 1010b57cec5SDimitry Andric bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const { 1020b57cec5SDimitry Andric return needsSPForLocalFrame(MF) || needsPrologForEH(MF); 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric /// Returns true if the local user-space stack pointer needs to be written back 1060b57cec5SDimitry Andric /// to __stack_pointer global by this function (this is not meaningful if 1070b57cec5SDimitry Andric /// needsSP is false). If false, the stack red zone can be used and only a local 1080b57cec5SDimitry Andric /// SP is needed. 1090b57cec5SDimitry Andric bool WebAssemblyFrameLowering::needsSPWriteback( 1100b57cec5SDimitry Andric const MachineFunction &MF) const { 1110b57cec5SDimitry Andric auto &MFI = MF.getFrameInfo(); 1120b57cec5SDimitry Andric assert(needsSP(MF)); 1130b57cec5SDimitry Andric // When we don't need a local stack pointer for its local frame but only to 1140b57cec5SDimitry Andric // support EH, we don't need to write SP back in the epilog, because we don't 1150b57cec5SDimitry Andric // bump down the stack pointer in the prolog. We need to write SP back in the 1160b57cec5SDimitry Andric // epilog only if 1170b57cec5SDimitry Andric // 1. We need SP not only for EH support but also because we actually use 1180b57cec5SDimitry Andric // stack or we have a frame address taken. 1190b57cec5SDimitry Andric // 2. We cannot use the red zone. 1200b57cec5SDimitry Andric bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() && 1210b57cec5SDimitry Andric !MF.getFunction().hasFnAttribute(Attribute::NoRedZone); 1220b57cec5SDimitry Andric return needsSPForLocalFrame(MF) && !CanUseRedZone; 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric void WebAssemblyFrameLowering::writeSPToGlobal( 1260b57cec5SDimitry Andric unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB, 1270b57cec5SDimitry Andric MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const { 1280b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric const char *ES = "__stack_pointer"; 1310b57cec5SDimitry Andric auto *SPSymbol = MF.createExternalSymbolName(ES); 1320b57cec5SDimitry Andric BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::GLOBAL_SET_I32)) 1330b57cec5SDimitry Andric .addExternalSymbol(SPSymbol) 1340b57cec5SDimitry Andric .addReg(SrcReg); 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric MachineBasicBlock::iterator 1380b57cec5SDimitry Andric WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( 1390b57cec5SDimitry Andric MachineFunction &MF, MachineBasicBlock &MBB, 1400b57cec5SDimitry Andric MachineBasicBlock::iterator I) const { 1410b57cec5SDimitry Andric assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) && 1420b57cec5SDimitry Andric "Call frame pseudos should only be used for dynamic stack adjustment"); 1430b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 1440b57cec5SDimitry Andric if (I->getOpcode() == TII->getCallFrameDestroyOpcode() && 1450b57cec5SDimitry Andric needsSPWriteback(MF)) { 1460b57cec5SDimitry Andric DebugLoc DL = I->getDebugLoc(); 1470b57cec5SDimitry Andric writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL); 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric return MBB.erase(I); 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, 1530b57cec5SDimitry Andric MachineBasicBlock &MBB) const { 1540b57cec5SDimitry Andric // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions 1550b57cec5SDimitry Andric auto &MFI = MF.getFrameInfo(); 1560b57cec5SDimitry Andric assert(MFI.getCalleeSavedInfo().empty() && 1570b57cec5SDimitry Andric "WebAssembly should not have callee-saved registers"); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric if (!needsSP(MF)) 1600b57cec5SDimitry Andric return; 1610b57cec5SDimitry Andric uint64_t StackSize = MFI.getStackSize(); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 1640b57cec5SDimitry Andric auto &MRI = MF.getRegInfo(); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric auto InsertPt = MBB.begin(); 1670b57cec5SDimitry Andric while (InsertPt != MBB.end() && 1680b57cec5SDimitry Andric WebAssembly::isArgument(InsertPt->getOpcode())) 1690b57cec5SDimitry Andric ++InsertPt; 1700b57cec5SDimitry Andric DebugLoc DL; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric const TargetRegisterClass *PtrRC = 1730b57cec5SDimitry Andric MRI.getTargetRegisterInfo()->getPointerRegClass(MF); 1740b57cec5SDimitry Andric unsigned SPReg = WebAssembly::SP32; 1750b57cec5SDimitry Andric if (StackSize) 1760b57cec5SDimitry Andric SPReg = MRI.createVirtualRegister(PtrRC); 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric const char *ES = "__stack_pointer"; 1790b57cec5SDimitry Andric auto *SPSymbol = MF.createExternalSymbolName(ES); 1800b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GLOBAL_GET_I32), SPReg) 1810b57cec5SDimitry Andric .addExternalSymbol(SPSymbol); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric bool HasBP = hasBP(MF); 1840b57cec5SDimitry Andric if (HasBP) { 1850b57cec5SDimitry Andric auto FI = MF.getInfo<WebAssemblyFunctionInfo>(); 186*8bcb0991SDimitry Andric Register BasePtr = MRI.createVirtualRegister(PtrRC); 1870b57cec5SDimitry Andric FI->setBasePointerVreg(BasePtr); 1880b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr) 1890b57cec5SDimitry Andric .addReg(SPReg); 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric if (StackSize) { 1920b57cec5SDimitry Andric // Subtract the frame size 193*8bcb0991SDimitry Andric Register OffsetReg = MRI.createVirtualRegister(PtrRC); 1940b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 1950b57cec5SDimitry Andric .addImm(StackSize); 1960b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), 1970b57cec5SDimitry Andric WebAssembly::SP32) 1980b57cec5SDimitry Andric .addReg(SPReg) 1990b57cec5SDimitry Andric .addReg(OffsetReg); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric if (HasBP) { 202*8bcb0991SDimitry Andric Register BitmaskReg = MRI.createVirtualRegister(PtrRC); 2030b57cec5SDimitry Andric unsigned Alignment = MFI.getMaxAlignment(); 2040b57cec5SDimitry Andric assert((1u << countTrailingZeros(Alignment)) == Alignment && 2050b57cec5SDimitry Andric "Alignment must be a power of 2"); 2060b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg) 2070b57cec5SDimitry Andric .addImm((int)~(Alignment - 1)); 2080b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32), 2090b57cec5SDimitry Andric WebAssembly::SP32) 2100b57cec5SDimitry Andric .addReg(WebAssembly::SP32) 2110b57cec5SDimitry Andric .addReg(BitmaskReg); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric if (hasFP(MF)) { 2140b57cec5SDimitry Andric // Unlike most conventional targets (where FP points to the saved FP), 2150b57cec5SDimitry Andric // FP points to the bottom of the fixed-size locals, so we can use positive 2160b57cec5SDimitry Andric // offsets in load/store instructions. 2170b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), WebAssembly::FP32) 2180b57cec5SDimitry Andric .addReg(WebAssembly::SP32); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric if (StackSize && needsSPWriteback(MF)) { 2210b57cec5SDimitry Andric writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL); 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, 2260b57cec5SDimitry Andric MachineBasicBlock &MBB) const { 2270b57cec5SDimitry Andric uint64_t StackSize = MF.getFrameInfo().getStackSize(); 2280b57cec5SDimitry Andric if (!needsSP(MF) || !needsSPWriteback(MF)) 2290b57cec5SDimitry Andric return; 2300b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 2310b57cec5SDimitry Andric auto &MRI = MF.getRegInfo(); 2320b57cec5SDimitry Andric auto InsertPt = MBB.getFirstTerminator(); 2330b57cec5SDimitry Andric DebugLoc DL; 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric if (InsertPt != MBB.end()) 2360b57cec5SDimitry Andric DL = InsertPt->getDebugLoc(); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric // Restore the stack pointer. If we had fixed-size locals, add the offset 2390b57cec5SDimitry Andric // subtracted in the prolog. 2400b57cec5SDimitry Andric unsigned SPReg = 0; 2410b57cec5SDimitry Andric if (hasBP(MF)) { 2420b57cec5SDimitry Andric auto FI = MF.getInfo<WebAssemblyFunctionInfo>(); 2430b57cec5SDimitry Andric SPReg = FI->getBasePointerVreg(); 2440b57cec5SDimitry Andric } else if (StackSize) { 2450b57cec5SDimitry Andric const TargetRegisterClass *PtrRC = 2460b57cec5SDimitry Andric MRI.getTargetRegisterInfo()->getPointerRegClass(MF); 247*8bcb0991SDimitry Andric Register OffsetReg = MRI.createVirtualRegister(PtrRC); 2480b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 2490b57cec5SDimitry Andric .addImm(StackSize); 2500b57cec5SDimitry Andric // In the epilog we don't need to write the result back to the SP32 physreg 2510b57cec5SDimitry Andric // because it won't be used again. We can use a stackified register instead. 2520b57cec5SDimitry Andric SPReg = MRI.createVirtualRegister(PtrRC); 2530b57cec5SDimitry Andric BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg) 2540b57cec5SDimitry Andric .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32) 2550b57cec5SDimitry Andric .addReg(OffsetReg); 2560b57cec5SDimitry Andric } else { 2570b57cec5SDimitry Andric SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32; 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL); 2610b57cec5SDimitry Andric } 262