1 //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the WebAssembly implementation of
12 /// TargetFrameLowering class.
14 /// On WebAssembly, there aren't a lot of things to do here. There are no
15 /// callee-saved registers to save, and no spill slots.
17 /// The stack grows downward.
19 //===----------------------------------------------------------------------===//
21 #include "WebAssemblyFrameLowering.h"
22 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
23 #include "WebAssemblyInstrInfo.h"
24 #include "WebAssemblyMachineFunctionInfo.h"
25 #include "WebAssemblySubtarget.h"
26 #include "WebAssemblyTargetMachine.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/Support/Debug.h"
35 #define DEBUG_TYPE "wasm-frame-info"
37 // TODO: Implement a red zone?
39 // TODO: Prolog/epilog should be stackified too. This pass runs after register
40 // stackification, so we'll have to do it manually.
41 // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
43 /// Return true if the specified function should have a dedicated frame pointer
45 bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
46 const MachineFrameInfo *MFI = MF.getFrameInfo();
48 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
49 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() ||
50 MFI->hasStackMap() || MFI->hasPatchPoint() ||
51 RegInfo->needsStackRealignment(MF);
54 /// Under normal circumstances, when a frame pointer is not required, we reserve
55 /// argument space for call sites in the function immediately on entry to the
56 /// current function. This eliminates the need for add/sub sp brackets around
57 /// call sites. Returns true if the call frame is included as part of the stack
59 bool WebAssemblyFrameLowering::hasReservedCallFrame(
60 const MachineFunction &MF) const {
61 return !MF.getFrameInfo()->hasVarSizedObjects();
64 void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
65 MachineFunction & /*MF*/, MachineBasicBlock & /*MBB*/,
66 MachineBasicBlock::iterator /*I*/) const {
67 llvm_unreachable("TODO: implement eliminateCallFramePseudoInstr");
70 void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
71 MachineBasicBlock &MBB) const {
72 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
73 auto *MFI = MF.getFrameInfo();
74 assert(MFI->getCalleeSavedInfo().empty() &&
75 "WebAssembly should not have callee-saved registers");
76 assert(!hasFP(MF) && "Functions needing frame pointers not yet supported");
77 assert(!MFI->adjustsStack() && "Dynamic stack adjustmet not yet supported");
78 uint64_t StackSize = MFI->getStackSize();
82 const auto *TII = MF.getSubtarget().getInstrInfo();
83 auto &MRI = MF.getRegInfo();
84 auto InsertPt = MBB.begin();
87 // Get the current stacktop
88 // TODO: To support dynamic alloc, also copy to FP
89 unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
90 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
91 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPReg)
92 .addExternalSymbol(SPSymbol);
93 // This MachinePointerInfo should reference __stack_pointer as well but
94 // doesn't because MachinePointerInfo() takes a GV which we don't have for
95 // __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry
96 // is appropriate instead. (likewise for EmitEpologue below)
97 auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(),
98 MachineMemOperand::MOLoad, 4, 4);
99 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), SPReg)
102 .addMemOperand(LoadMMO);
103 // Subtract the frame size
104 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
105 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
107 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), WebAssembly::SP32)
110 // The SP32 register now has the new stacktop. Also write it back to memory.
111 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
112 .addExternalSymbol(SPSymbol);
113 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
114 MachineMemOperand::MOStore, 4, 4);
115 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
118 .addReg(WebAssembly::SP32)
122 void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
123 MachineBasicBlock &MBB) const {
124 uint64_t StackSize = MF.getFrameInfo()->getStackSize();
127 const auto *TII = MF.getSubtarget().getInstrInfo();
128 auto &MRI = MF.getRegInfo();
129 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
130 auto InsertPt = MBB.getFirstTerminator();
133 if (InsertPt != MBB.end()) {
134 DL = InsertPt->getDebugLoc();
137 // Restore the stack pointer. Without FP its value is just SP32 - stacksize
138 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
140 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
141 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32)
142 .addReg(WebAssembly::SP32)
144 // Re-use OffsetReg to hold the address of the stacktop
145 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
146 .addExternalSymbol(SPSymbol);
147 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
148 MachineMemOperand::MOStore, 4, 4);
149 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
152 .addReg(WebAssembly::SP32)