blob: a1c567fa3c07321e43cc94febce3db4ee4887409 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman10e730a2015-06-29 23:51:55 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file contains the WebAssembly implementation of
Dan Gohman10e730a2015-06-29 23:51:55 +000011/// TargetFrameLowering class.
12///
13/// On WebAssembly, there aren't a lot of things to do here. There are no
14/// callee-saved registers to save, and no spill slots.
15///
16/// The stack grows downward.
17///
18//===----------------------------------------------------------------------===//
19
20#include "WebAssemblyFrameLowering.h"
21#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
22#include "WebAssemblyInstrInfo.h"
23#include "WebAssemblyMachineFunctionInfo.h"
24#include "WebAssemblySubtarget.h"
25#include "WebAssemblyTargetMachine.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000026#include "WebAssemblyUtilities.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman82607f52017-02-24 23:46:05 +000030#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Heejin Ahn78d19102018-08-21 21:23:07 +000032#include "llvm/MC/MCAsmInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000033#include "llvm/Support/Debug.h"
34using namespace llvm;
35
JF Bastien03855df2015-07-01 23:41:25 +000036#define DEBUG_TYPE "wasm-frame-info"
Dan Gohman10e730a2015-06-29 23:51:55 +000037
Derek Schuff9769deb2015-12-11 23:49:46 +000038// TODO: wasm64
Derek Schuff9769deb2015-12-11 23:49:46 +000039// TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
Dan Gohman10e730a2015-06-29 23:51:55 +000040
Derek Schuff0d41b7b2016-11-07 22:00:48 +000041/// We need a base pointer in the case of having items on the stack that
42/// require stricter alignment than the stack pointer itself. Because we need
43/// to shift the stack pointer by some unknown amount to force the alignment,
44/// we need to record the value of the stack pointer on entry to the function.
Heejin Ahnf208f632018-09-05 01:27:38 +000045bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
Derek Schuff0d41b7b2016-11-07 22:00:48 +000046 const auto *RegInfo =
47 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
48 return RegInfo->needsStackRealignment(MF);
49}
50
Dan Gohman10e730a2015-06-29 23:51:55 +000051/// Return true if the specified function should have a dedicated frame pointer
52/// register.
53bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000054 const MachineFrameInfo &MFI = MF.getFrameInfo();
Derek Schuff0d41b7b2016-11-07 22:00:48 +000055
56 // When we have var-sized objects, we move the stack pointer by an unknown
57 // amount, and need to emit a frame pointer to restore the stack to where we
58 // were on function entry.
59 // If we already need a base pointer, we use that to fix up the stack pointer.
60 // If there are no fixed-size objects, we would have no use of a frame
61 // pointer, and thus should not emit one.
62 bool HasFixedSizedObjects = MFI.getStackSize() > 0;
63 bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
64
65 return MFI.isFrameAddressTaken() ||
66 (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
67 MFI.hasStackMap() || MFI.hasPatchPoint();
Dan Gohman10e730a2015-06-29 23:51:55 +000068}
69
70/// Under normal circumstances, when a frame pointer is not required, we reserve
71/// argument space for call sites in the function immediately on entry to the
72/// current function. This eliminates the need for add/sub sp brackets around
73/// call sites. Returns true if the call frame is included as part of the stack
74/// frame.
75bool WebAssemblyFrameLowering::hasReservedCallFrame(
76 const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000077 return !MF.getFrameInfo().hasVarSizedObjects();
Dan Gohman10e730a2015-06-29 23:51:55 +000078}
79
Heejin Ahn972fc352018-08-22 21:13:49 +000080// Returns true if this function needs a local user-space stack pointer for its
81// local frame (not for exception handling).
82bool WebAssemblyFrameLowering::needsSPForLocalFrame(
83 const MachineFunction &MF) const {
84 auto &MFI = MF.getFrameInfo();
85 return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
86}
87
Heejin Ahn78d19102018-08-21 21:23:07 +000088// In function with EH pads, we need to make a copy of the value of
89// __stack_pointer global in SP32 register, in order to use it when restoring
90// __stack_pointer after an exception is caught.
91bool WebAssemblyFrameLowering::needsPrologForEH(
92 const MachineFunction &MF) const {
93 auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
94 return EHType == ExceptionHandling::Wasm &&
95 MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
96}
Derek Schuff4b3bb212016-02-23 18:13:07 +000097
98/// Returns true if this function needs a local user-space stack pointer.
99/// Unlike a machine stack pointer, the wasm user stack pointer is a global
100/// variable, so it is loaded into a register in the prolog.
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000101bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
Heejin Ahn972fc352018-08-22 21:13:49 +0000102 return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
Derek Schuff4b3bb212016-02-23 18:13:07 +0000103}
104
105/// Returns true if the local user-space stack pointer needs to be written back
Heejin Ahnc4df1d12018-08-22 00:20:02 +0000106/// to __stack_pointer global by this function (this is not meaningful if
107/// needsSP is false). If false, the stack red zone can be used and only a local
108/// SP is needed.
Derek Schuff4b3bb212016-02-23 18:13:07 +0000109bool WebAssemblyFrameLowering::needsSPWriteback(
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000110 const MachineFunction &MF) const {
111 auto &MFI = MF.getFrameInfo();
112 assert(needsSP(MF));
Heejin Ahn972fc352018-08-22 21:13:49 +0000113 // When we don't need a local stack pointer for its local frame but only to
114 // support EH, we don't need to write SP back in the epilog, because we don't
115 // bump down the stack pointer in the prolog. We need to write SP back in the
116 // epilog only if
117 // 1. We need SP not only for EH support but also because we actually use
118 // stack or we have a frame address taken.
119 // 2. We cannot use the red zone.
120 bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
121 !MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
122 return needsSPForLocalFrame(MF) && !CanUseRedZone;
Derek Schuff4b3bb212016-02-23 18:13:07 +0000123}
124
Heejin Ahn78d19102018-08-21 21:23:07 +0000125void WebAssemblyFrameLowering::writeSPToGlobal(
126 unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
127 MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000128 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
129
Sam Clegg9d24fb72017-06-16 23:59:10 +0000130 const char *ES = "__stack_pointer";
131 auto *SPSymbol = MF.createExternalSymbolName(ES);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000132 BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::GLOBAL_SET_I32))
Sam Cleggef4c66c2019-04-03 00:17:29 +0000133 .addExternalSymbol(SPSymbol)
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000134 .addReg(SrcReg);
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000135}
136
Hans Wennborge1a2e902016-03-31 18:33:38 +0000137MachineBasicBlock::iterator
138WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
Derek Schuff8bb5f292015-12-16 23:21:30 +0000139 MachineFunction &MF, MachineBasicBlock &MBB,
140 MachineBasicBlock::iterator I) const {
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000141 assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000142 "Call frame pseudos should only be used for dynamic stack adjustment");
143 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff4b3bb212016-02-23 18:13:07 +0000144 if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000145 needsSPWriteback(MF)) {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000146 DebugLoc DL = I->getDebugLoc();
Heejin Ahn20c9c442018-08-21 19:52:19 +0000147 writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL);
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000148 }
Hans Wennborge1a2e902016-03-31 18:33:38 +0000149 return MBB.erase(I);
Derek Schuff8bb5f292015-12-16 23:21:30 +0000150}
151
152void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
153 MachineBasicBlock &MBB) const {
154 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
Matthias Braun941a7052016-07-28 18:40:00 +0000155 auto &MFI = MF.getFrameInfo();
156 assert(MFI.getCalleeSavedInfo().empty() &&
Derek Schuff8bb5f292015-12-16 23:21:30 +0000157 "WebAssembly should not have callee-saved registers");
Derek Schuff6ea637a2016-01-29 18:37:49 +0000158
Heejin Ahnf208f632018-09-05 01:27:38 +0000159 if (!needsSP(MF))
160 return;
Matthias Braun941a7052016-07-28 18:40:00 +0000161 uint64_t StackSize = MFI.getStackSize();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000162
Dan Gohman31966502016-01-19 14:53:19 +0000163 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff6ea637a2016-01-29 18:37:49 +0000164 auto &MRI = MF.getRegInfo();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000165
166 auto InsertPt = MBB.begin();
Dan Gohmand934cb82017-02-24 23:18:00 +0000167 while (InsertPt != MBB.end() && WebAssembly::isArgument(*InsertPt))
168 ++InsertPt;
Derek Schuff8bb5f292015-12-16 23:21:30 +0000169 DebugLoc DL;
170
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000171 const TargetRegisterClass *PtrRC =
172 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000173 unsigned SPReg = WebAssembly::SP32;
174 if (StackSize)
175 SPReg = MRI.createVirtualRegister(PtrRC);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000176
177 const char *ES = "__stack_pointer";
178 auto *SPSymbol = MF.createExternalSymbolName(ES);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000179 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GLOBAL_GET_I32), SPReg)
Sam Cleggef4c66c2019-04-03 00:17:29 +0000180 .addExternalSymbol(SPSymbol);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000181
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000182 bool HasBP = hasBP(MF);
183 if (HasBP) {
184 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
185 unsigned BasePtr = MRI.createVirtualRegister(PtrRC);
186 FI->setBasePointerVreg(BasePtr);
187 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
188 .addReg(SPReg);
189 }
Derek Schuff6ea637a2016-01-29 18:37:49 +0000190 if (StackSize) {
191 // Subtract the frame size
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000192 unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000193 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
194 .addImm(StackSize);
Derek Schuff3f063292016-02-11 20:57:09 +0000195 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32),
Derek Schuff6ea637a2016-01-29 18:37:49 +0000196 WebAssembly::SP32)
197 .addReg(SPReg)
198 .addReg(OffsetReg);
199 }
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000200 if (HasBP) {
201 unsigned BitmaskReg = MRI.createVirtualRegister(PtrRC);
202 unsigned Alignment = MFI.getMaxAlignment();
Dan Gohmanf295cc82016-12-02 20:13:05 +0000203 assert((1u << countTrailingZeros(Alignment)) == Alignment &&
Heejin Ahnf208f632018-09-05 01:27:38 +0000204 "Alignment must be a power of 2");
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000205 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg)
206 .addImm((int)~(Alignment - 1));
207 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32),
208 WebAssembly::SP32)
209 .addReg(WebAssembly::SP32)
210 .addReg(BitmaskReg);
211 }
Derek Schuff6ea637a2016-01-29 18:37:49 +0000212 if (hasFP(MF)) {
213 // Unlike most conventional targets (where FP points to the saved FP),
214 // FP points to the bottom of the fixed-size locals, so we can use positive
215 // offsets in load/store instructions.
Heejin Ahnf208f632018-09-05 01:27:38 +0000216 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), WebAssembly::FP32)
Derek Schuff6ea637a2016-01-29 18:37:49 +0000217 .addReg(WebAssembly::SP32);
218 }
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000219 if (StackSize && needsSPWriteback(MF)) {
Heejin Ahn20c9c442018-08-21 19:52:19 +0000220 writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000221 }
Derek Schuff8bb5f292015-12-16 23:21:30 +0000222}
223
Derek Schuff9769deb2015-12-11 23:49:46 +0000224void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
225 MachineBasicBlock &MBB) const {
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000226 uint64_t StackSize = MF.getFrameInfo().getStackSize();
Heejin Ahnf208f632018-09-05 01:27:38 +0000227 if (!needsSP(MF) || !needsSPWriteback(MF))
228 return;
Dan Gohman31966502016-01-19 14:53:19 +0000229 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000230 auto &MRI = MF.getRegInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000231 auto InsertPt = MBB.getFirstTerminator();
232 DebugLoc DL;
233
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000234 if (InsertPt != MBB.end())
Derek Schuff9769deb2015-12-11 23:49:46 +0000235 DL = InsertPt->getDebugLoc();
Dan Gohman450a8072016-05-05 20:41:15 +0000236
Derek Schuff6ea637a2016-01-29 18:37:49 +0000237 // Restore the stack pointer. If we had fixed-size locals, add the offset
238 // subtracted in the prolog.
Derek Schuffd4207ba2016-03-17 17:00:29 +0000239 unsigned SPReg = 0;
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000240 if (hasBP(MF)) {
241 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
242 SPReg = FI->getBasePointerVreg();
243 } else if (StackSize) {
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000244 const TargetRegisterClass *PtrRC =
245 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
246 unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
Heejin Ahnc2c33c82018-08-20 23:02:15 +0000247 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
248 .addImm(StackSize);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000249 // In the epilog we don't need to write the result back to the SP32 physreg
250 // because it won't be used again. We can use a stackified register instead.
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000251 SPReg = MRI.createVirtualRegister(PtrRC);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000252 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg)
Derek Schuff6ea637a2016-01-29 18:37:49 +0000253 .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32)
254 .addReg(OffsetReg);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000255 } else {
256 SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
Derek Schuff6ea637a2016-01-29 18:37:49 +0000257 }
258
Heejin Ahn20c9c442018-08-21 19:52:19 +0000259 writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
Dan Gohman10e730a2015-06-29 23:51:55 +0000260}