blob: 2d5aff28d27b27a3a5d4b88efca40f9ff5766cd4 [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file contains the WebAssembly implementation of
Dan Gohman10e730a2015-06-29 23:51:55 +000012/// 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"
Dan Gohmand934cb82017-02-24 23:18:00 +000027#include "WebAssemblyUtilities.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman82607f52017-02-24 23:46:05 +000031#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Heejin Ahn78d19102018-08-21 21:23:07 +000033#include "llvm/MC/MCAsmInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000034#include "llvm/Support/Debug.h"
35using namespace llvm;
36
JF Bastien03855df2015-07-01 23:41:25 +000037#define DEBUG_TYPE "wasm-frame-info"
Dan Gohman10e730a2015-06-29 23:51:55 +000038
Derek Schuff9769deb2015-12-11 23:49:46 +000039// TODO: wasm64
Derek Schuff9769deb2015-12-11 23:49:46 +000040// TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
Dan Gohman10e730a2015-06-29 23:51:55 +000041
Derek Schuff0d41b7b2016-11-07 22:00:48 +000042/// We need a base pointer in the case of having items on the stack that
43/// require stricter alignment than the stack pointer itself. Because we need
44/// to shift the stack pointer by some unknown amount to force the alignment,
45/// we need to record the value of the stack pointer on entry to the function.
Heejin Ahnf208f632018-09-05 01:27:38 +000046bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
Derek Schuff0d41b7b2016-11-07 22:00:48 +000047 const auto *RegInfo =
48 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
49 return RegInfo->needsStackRealignment(MF);
50}
51
Dan Gohman10e730a2015-06-29 23:51:55 +000052/// Return true if the specified function should have a dedicated frame pointer
53/// register.
54bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000055 const MachineFrameInfo &MFI = MF.getFrameInfo();
Derek Schuff0d41b7b2016-11-07 22:00:48 +000056
57 // When we have var-sized objects, we move the stack pointer by an unknown
58 // amount, and need to emit a frame pointer to restore the stack to where we
59 // were on function entry.
60 // If we already need a base pointer, we use that to fix up the stack pointer.
61 // If there are no fixed-size objects, we would have no use of a frame
62 // pointer, and thus should not emit one.
63 bool HasFixedSizedObjects = MFI.getStackSize() > 0;
64 bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
65
66 return MFI.isFrameAddressTaken() ||
67 (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
68 MFI.hasStackMap() || MFI.hasPatchPoint();
Dan Gohman10e730a2015-06-29 23:51:55 +000069}
70
71/// Under normal circumstances, when a frame pointer is not required, we reserve
72/// argument space for call sites in the function immediately on entry to the
73/// current function. This eliminates the need for add/sub sp brackets around
74/// call sites. Returns true if the call frame is included as part of the stack
75/// frame.
76bool WebAssemblyFrameLowering::hasReservedCallFrame(
77 const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000078 return !MF.getFrameInfo().hasVarSizedObjects();
Dan Gohman10e730a2015-06-29 23:51:55 +000079}
80
Heejin Ahn972fc352018-08-22 21:13:49 +000081// Returns true if this function needs a local user-space stack pointer for its
82// local frame (not for exception handling).
83bool WebAssemblyFrameLowering::needsSPForLocalFrame(
84 const MachineFunction &MF) const {
85 auto &MFI = MF.getFrameInfo();
86 return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
87}
88
Heejin Ahn78d19102018-08-21 21:23:07 +000089// In function with EH pads, we need to make a copy of the value of
90// __stack_pointer global in SP32 register, in order to use it when restoring
91// __stack_pointer after an exception is caught.
92bool WebAssemblyFrameLowering::needsPrologForEH(
93 const MachineFunction &MF) const {
94 auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
95 return EHType == ExceptionHandling::Wasm &&
96 MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
97}
Derek Schuff4b3bb212016-02-23 18:13:07 +000098
99/// Returns true if this function needs a local user-space stack pointer.
100/// Unlike a machine stack pointer, the wasm user stack pointer is a global
101/// variable, so it is loaded into a register in the prolog.
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000102bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
Heejin Ahn972fc352018-08-22 21:13:49 +0000103 return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
Derek Schuff4b3bb212016-02-23 18:13:07 +0000104}
105
106/// Returns true if the local user-space stack pointer needs to be written back
Heejin Ahnc4df1d12018-08-22 00:20:02 +0000107/// to __stack_pointer global by this function (this is not meaningful if
108/// needsSP is false). If false, the stack red zone can be used and only a local
109/// SP is needed.
Derek Schuff4b3bb212016-02-23 18:13:07 +0000110bool WebAssemblyFrameLowering::needsSPWriteback(
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000111 const MachineFunction &MF) const {
112 auto &MFI = MF.getFrameInfo();
113 assert(needsSP(MF));
Heejin Ahn972fc352018-08-22 21:13:49 +0000114 // When we don't need a local stack pointer for its local frame but only to
115 // support EH, we don't need to write SP back in the epilog, because we don't
116 // bump down the stack pointer in the prolog. We need to write SP back in the
117 // epilog only if
118 // 1. We need SP not only for EH support but also because we actually use
119 // stack or we have a frame address taken.
120 // 2. We cannot use the red zone.
121 bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
122 !MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
123 return needsSPForLocalFrame(MF) && !CanUseRedZone;
Derek Schuff4b3bb212016-02-23 18:13:07 +0000124}
125
Heejin Ahn78d19102018-08-21 21:23:07 +0000126void WebAssemblyFrameLowering::writeSPToGlobal(
127 unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
128 MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000129 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
130
Sam Clegg9d24fb72017-06-16 23:59:10 +0000131 const char *ES = "__stack_pointer";
132 auto *SPSymbol = MF.createExternalSymbolName(ES);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000133 BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::GLOBAL_SET_I32))
Nicholas Wilsone408a892018-08-03 14:33:37 +0000134 .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL)
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000135 .addReg(SrcReg);
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000136}
137
Hans Wennborge1a2e902016-03-31 18:33:38 +0000138MachineBasicBlock::iterator
139WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
Derek Schuff8bb5f292015-12-16 23:21:30 +0000140 MachineFunction &MF, MachineBasicBlock &MBB,
141 MachineBasicBlock::iterator I) const {
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000142 assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000143 "Call frame pseudos should only be used for dynamic stack adjustment");
144 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff4b3bb212016-02-23 18:13:07 +0000145 if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000146 needsSPWriteback(MF)) {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000147 DebugLoc DL = I->getDebugLoc();
Heejin Ahn20c9c442018-08-21 19:52:19 +0000148 writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL);
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000149 }
Hans Wennborge1a2e902016-03-31 18:33:38 +0000150 return MBB.erase(I);
Derek Schuff8bb5f292015-12-16 23:21:30 +0000151}
152
153void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
154 MachineBasicBlock &MBB) const {
155 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
Matthias Braun941a7052016-07-28 18:40:00 +0000156 auto &MFI = MF.getFrameInfo();
157 assert(MFI.getCalleeSavedInfo().empty() &&
Derek Schuff8bb5f292015-12-16 23:21:30 +0000158 "WebAssembly should not have callee-saved registers");
Derek Schuff6ea637a2016-01-29 18:37:49 +0000159
Heejin Ahnf208f632018-09-05 01:27:38 +0000160 if (!needsSP(MF))
161 return;
Matthias Braun941a7052016-07-28 18:40:00 +0000162 uint64_t StackSize = MFI.getStackSize();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000163
Dan Gohman31966502016-01-19 14:53:19 +0000164 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff6ea637a2016-01-29 18:37:49 +0000165 auto &MRI = MF.getRegInfo();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000166
167 auto InsertPt = MBB.begin();
Dan Gohmand934cb82017-02-24 23:18:00 +0000168 while (InsertPt != MBB.end() && WebAssembly::isArgument(*InsertPt))
169 ++InsertPt;
Derek Schuff8bb5f292015-12-16 23:21:30 +0000170 DebugLoc DL;
171
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000172 const TargetRegisterClass *PtrRC =
173 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000174 unsigned SPReg = WebAssembly::SP32;
175 if (StackSize)
176 SPReg = MRI.createVirtualRegister(PtrRC);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000177
178 const char *ES = "__stack_pointer";
179 auto *SPSymbol = MF.createExternalSymbolName(ES);
Thomas Lively6a87dda2019-01-08 06:25:55 +0000180 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GLOBAL_GET_I32), SPReg)
Nicholas Wilsone408a892018-08-03 14:33:37 +0000181 .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000182
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000183 bool HasBP = hasBP(MF);
184 if (HasBP) {
185 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
186 unsigned BasePtr = MRI.createVirtualRegister(PtrRC);
187 FI->setBasePointerVreg(BasePtr);
188 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
189 .addReg(SPReg);
190 }
Derek Schuff6ea637a2016-01-29 18:37:49 +0000191 if (StackSize) {
192 // Subtract the frame size
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000193 unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000194 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
195 .addImm(StackSize);
Derek Schuff3f063292016-02-11 20:57:09 +0000196 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32),
Derek Schuff6ea637a2016-01-29 18:37:49 +0000197 WebAssembly::SP32)
198 .addReg(SPReg)
199 .addReg(OffsetReg);
200 }
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000201 if (HasBP) {
202 unsigned BitmaskReg = MRI.createVirtualRegister(PtrRC);
203 unsigned Alignment = MFI.getMaxAlignment();
Dan Gohmanf295cc82016-12-02 20:13:05 +0000204 assert((1u << countTrailingZeros(Alignment)) == Alignment &&
Heejin Ahnf208f632018-09-05 01:27:38 +0000205 "Alignment must be a power of 2");
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000206 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg)
207 .addImm((int)~(Alignment - 1));
208 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32),
209 WebAssembly::SP32)
210 .addReg(WebAssembly::SP32)
211 .addReg(BitmaskReg);
212 }
Derek Schuff6ea637a2016-01-29 18:37:49 +0000213 if (hasFP(MF)) {
214 // Unlike most conventional targets (where FP points to the saved FP),
215 // FP points to the bottom of the fixed-size locals, so we can use positive
216 // offsets in load/store instructions.
Heejin Ahnf208f632018-09-05 01:27:38 +0000217 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), WebAssembly::FP32)
Derek Schuff6ea637a2016-01-29 18:37:49 +0000218 .addReg(WebAssembly::SP32);
219 }
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000220 if (StackSize && needsSPWriteback(MF)) {
Heejin Ahn20c9c442018-08-21 19:52:19 +0000221 writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000222 }
Derek Schuff8bb5f292015-12-16 23:21:30 +0000223}
224
Derek Schuff9769deb2015-12-11 23:49:46 +0000225void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
226 MachineBasicBlock &MBB) const {
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000227 uint64_t StackSize = MF.getFrameInfo().getStackSize();
Heejin Ahnf208f632018-09-05 01:27:38 +0000228 if (!needsSP(MF) || !needsSPWriteback(MF))
229 return;
Dan Gohman31966502016-01-19 14:53:19 +0000230 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000231 auto &MRI = MF.getRegInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000232 auto InsertPt = MBB.getFirstTerminator();
233 DebugLoc DL;
234
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000235 if (InsertPt != MBB.end())
Derek Schuff9769deb2015-12-11 23:49:46 +0000236 DL = InsertPt->getDebugLoc();
Dan Gohman450a8072016-05-05 20:41:15 +0000237
Derek Schuff6ea637a2016-01-29 18:37:49 +0000238 // Restore the stack pointer. If we had fixed-size locals, add the offset
239 // subtracted in the prolog.
Derek Schuffd4207ba2016-03-17 17:00:29 +0000240 unsigned SPReg = 0;
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000241 if (hasBP(MF)) {
242 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
243 SPReg = FI->getBasePointerVreg();
244 } else if (StackSize) {
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000245 const TargetRegisterClass *PtrRC =
246 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
247 unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
Heejin Ahnc2c33c82018-08-20 23:02:15 +0000248 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
249 .addImm(StackSize);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000250 // In the epilog we don't need to write the result back to the SP32 physreg
251 // because it won't be used again. We can use a stackified register instead.
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000252 SPReg = MRI.createVirtualRegister(PtrRC);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000253 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg)
Derek Schuff6ea637a2016-01-29 18:37:49 +0000254 .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32)
255 .addReg(OffsetReg);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000256 } else {
257 SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
Derek Schuff6ea637a2016-01-29 18:37:49 +0000258 }
259
Heejin Ahn20c9c442018-08-21 19:52:19 +0000260 writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
Dan Gohman10e730a2015-06-29 23:51:55 +0000261}