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();
65 /// Adjust the stack pointer by a constant amount.
66 static void adjustStackPointer(unsigned StackSize,
69 MachineBasicBlock& MBB,
70 const TargetInstrInfo* TII,
71 MachineBasicBlock::iterator InsertPt,
73 auto &MRI = MF.getRegInfo();
74 unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
75 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
76 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPReg)
77 .addExternalSymbol(SPSymbol);
78 // This MachinePointerInfo should reference __stack_pointer as well but
79 // doesn't because MachinePointerInfo() takes a GV which we don't have for
80 // __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry
81 // is appropriate instead. (likewise for EmitEpologue below)
82 auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(),
83 MachineMemOperand::MOLoad, 4, 4);
84 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), SPReg)
87 .addMemOperand(LoadMMO);
88 // Add/Subtract the frame size
89 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
90 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
92 BuildMI(MBB, InsertPt, DL,
93 TII->get(AdjustUp ? WebAssembly::ADD_I32 : WebAssembly::SUB_I32),
97 // The SP32 register now has the new stacktop. Also write it back to memory.
98 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
99 .addExternalSymbol(SPSymbol);
100 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
101 MachineMemOperand::MOStore, 4, 4);
102 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
105 .addReg(WebAssembly::SP32)
109 void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
110 MachineFunction &MF, MachineBasicBlock &MBB,
111 MachineBasicBlock::iterator I) const {
113 static_cast<const WebAssemblyInstrInfo*>(MF.getSubtarget().getInstrInfo());
114 DebugLoc DL = I->getDebugLoc();
115 unsigned Opc = I->getOpcode();
116 bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode();
117 unsigned Amount = I->getOperand(0).getImm();
119 adjustStackPointer(Amount, IsDestroy, MF, MBB,
124 void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
125 MachineBasicBlock &MBB) const {
126 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
127 auto *MFI = MF.getFrameInfo();
128 assert(MFI->getCalleeSavedInfo().empty() &&
129 "WebAssembly should not have callee-saved registers");
130 assert(!hasFP(MF) && "Functions needing frame pointers not yet supported");
131 uint64_t StackSize = MFI->getStackSize();
132 if (!StackSize && (!MFI->adjustsStack() || MFI->getMaxCallFrameSize() == 0))
135 const auto *TII = MF.getSubtarget().getInstrInfo();
137 auto InsertPt = MBB.begin();
140 adjustStackPointer(StackSize, false, MF, MBB, TII, InsertPt, DL);
143 void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
144 MachineBasicBlock &MBB) const {
145 uint64_t StackSize = MF.getFrameInfo()->getStackSize();
148 const auto *TII = MF.getSubtarget().getInstrInfo();
149 auto &MRI = MF.getRegInfo();
150 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
151 auto InsertPt = MBB.getFirstTerminator();
154 if (InsertPt != MBB.end()) {
155 DL = InsertPt->getDebugLoc();
158 // Restore the stack pointer. Without FP its value is just SP32 - stacksize
159 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
161 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
162 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32)
163 .addReg(WebAssembly::SP32)
165 // Re-use OffsetReg to hold the address of the stacktop
166 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
167 .addExternalSymbol(SPSymbol);
168 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
169 MachineMemOperand::MOStore, 4, 4);
170 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
173 .addReg(WebAssembly::SP32)