blob: 8b0f48f4bd15d332dcccd48090d2faeb8f10437a [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the WebAssembly implementation of
12/// TargetFrameLowering class.
13///
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.
16///
17/// The stack grows downward.
18///
19//===----------------------------------------------------------------------===//
20
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"
33using namespace llvm;
34
JF Bastien03855df2015-07-01 23:41:25 +000035#define DEBUG_TYPE "wasm-frame-info"
Dan Gohman10e730a2015-06-29 23:51:55 +000036
37// TODO: Implement a red zone?
Derek Schuff9769deb2015-12-11 23:49:46 +000038// TODO: wasm64
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
Dan Gohman10e730a2015-06-29 23:51:55 +000042
43/// Return true if the specified function should have a dedicated frame pointer
44/// register.
45bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
JF Bastienb9073fb2015-07-22 21:28:15 +000046 const MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohmane419a7c2015-08-24 16:46:31 +000047 const auto *RegInfo =
48 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +000049 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() ||
50 MFI->hasStackMap() || MFI->hasPatchPoint() ||
51 RegInfo->needsStackRealignment(MF);
Dan Gohman10e730a2015-06-29 23:51:55 +000052}
53
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
58/// frame.
59bool WebAssemblyFrameLowering::hasReservedCallFrame(
60 const MachineFunction &MF) const {
61 return !MF.getFrameInfo()->hasVarSizedObjects();
62}
63
64void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
Dan Gohman7a6b9822015-11-29 22:32:02 +000065 MachineFunction & /*MF*/, MachineBasicBlock & /*MBB*/,
66 MachineBasicBlock::iterator /*I*/) const {
Dan Gohman10e730a2015-06-29 23:51:55 +000067 llvm_unreachable("TODO: implement eliminateCallFramePseudoInstr");
68}
69
Derek Schuff9769deb2015-12-11 23:49:46 +000070void 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();
79 if (!StackSize)
80 return;
81
82 const auto *TII = MF.getSubtarget().getInstrInfo();
83 auto &MRI = MF.getRegInfo();
84 auto InsertPt = MBB.begin();
85 DebugLoc DL;
86
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)
100 .addImm(0)
101 .addReg(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)
106 .addImm(StackSize);
107 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), WebAssembly::SP32)
108 .addReg(SPReg)
109 .addReg(OffsetReg);
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)
116 .addImm(0)
117 .addReg(OffsetReg)
118 .addReg(WebAssembly::SP32)
119 .addMemOperand(MMO);
Dan Gohman10e730a2015-06-29 23:51:55 +0000120}
121
Derek Schuff9769deb2015-12-11 23:49:46 +0000122void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
123 MachineBasicBlock &MBB) const {
124 uint64_t StackSize = MF.getFrameInfo()->getStackSize();
125 if (!StackSize)
126 return;
127 const auto *TII = MF.getSubtarget().getInstrInfo();
128 auto &MRI = MF.getRegInfo();
129 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
130 auto InsertPt = MBB.getFirstTerminator();
131 DebugLoc DL;
132
133 if (InsertPt != MBB.end()) {
134 DL = InsertPt->getDebugLoc();
135 }
136
137 // Restore the stack pointer. Without FP its value is just SP32 - stacksize
138 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
139 .addImm(StackSize);
140 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
141 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32)
142 .addReg(WebAssembly::SP32)
143 .addReg(OffsetReg);
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)
150 .addImm(0)
151 .addReg(OffsetReg)
152 .addReg(WebAssembly::SP32)
153 .addMemOperand(MMO);
Dan Gohman10e730a2015-06-29 23:51:55 +0000154}