blob: 129c6b5db8b6548b209f46a4083574208420f570 [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.
46bool WebAssemblyFrameLowering::hasBP(
47 const MachineFunction &MF) const {
48 const auto *RegInfo =
49 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
50 return RegInfo->needsStackRealignment(MF);
51}
52
Dan Gohman10e730a2015-06-29 23:51:55 +000053/// Return true if the specified function should have a dedicated frame pointer
54/// register.
55bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000056 const MachineFrameInfo &MFI = MF.getFrameInfo();
Derek Schuff0d41b7b2016-11-07 22:00:48 +000057
58 // When we have var-sized objects, we move the stack pointer by an unknown
59 // amount, and need to emit a frame pointer to restore the stack to where we
60 // were on function entry.
61 // If we already need a base pointer, we use that to fix up the stack pointer.
62 // If there are no fixed-size objects, we would have no use of a frame
63 // pointer, and thus should not emit one.
64 bool HasFixedSizedObjects = MFI.getStackSize() > 0;
65 bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
66
67 return MFI.isFrameAddressTaken() ||
68 (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
69 MFI.hasStackMap() || MFI.hasPatchPoint();
Dan Gohman10e730a2015-06-29 23:51:55 +000070}
71
72/// Under normal circumstances, when a frame pointer is not required, we reserve
73/// argument space for call sites in the function immediately on entry to the
74/// current function. This eliminates the need for add/sub sp brackets around
75/// call sites. Returns true if the call frame is included as part of the stack
76/// frame.
77bool WebAssemblyFrameLowering::hasReservedCallFrame(
78 const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000079 return !MF.getFrameInfo().hasVarSizedObjects();
Dan Gohman10e730a2015-06-29 23:51:55 +000080}
81
Heejin Ahn78d19102018-08-21 21:23:07 +000082// In function with EH pads, we need to make a copy of the value of
83// __stack_pointer global in SP32 register, in order to use it when restoring
84// __stack_pointer after an exception is caught.
85bool WebAssemblyFrameLowering::needsPrologForEH(
86 const MachineFunction &MF) const {
87 auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
88 return EHType == ExceptionHandling::Wasm &&
89 MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
90}
Derek Schuff4b3bb212016-02-23 18:13:07 +000091
92/// Returns true if this function needs a local user-space stack pointer.
93/// Unlike a machine stack pointer, the wasm user stack pointer is a global
94/// variable, so it is loaded into a register in the prolog.
Heejin Ahnbc6d8972018-08-22 18:53:48 +000095bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
96 auto &MFI = MF.getFrameInfo();
Heejin Ahn78d19102018-08-21 21:23:07 +000097 return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF) ||
98 needsPrologForEH(MF);
Derek Schuff4b3bb212016-02-23 18:13:07 +000099}
100
101/// Returns true if the local user-space stack pointer needs to be written back
Heejin Ahnc4df1d12018-08-22 00:20:02 +0000102/// to __stack_pointer global by this function (this is not meaningful if
103/// needsSP is false). If false, the stack red zone can be used and only a local
104/// SP is needed.
Derek Schuff4b3bb212016-02-23 18:13:07 +0000105bool WebAssemblyFrameLowering::needsSPWriteback(
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000106 const MachineFunction &MF) const {
107 auto &MFI = MF.getFrameInfo();
108 assert(needsSP(MF));
Derek Schuff4b3bb212016-02-23 18:13:07 +0000109 return MFI.getStackSize() > RedZoneSize || MFI.hasCalls() ||
David Blaikie21109242017-12-15 23:52:06 +0000110 MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
Derek Schuff4b3bb212016-02-23 18:13:07 +0000111}
112
Heejin Ahn78d19102018-08-21 21:23:07 +0000113void WebAssemblyFrameLowering::writeSPToGlobal(
114 unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000116 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
117
Sam Clegg9d24fb72017-06-16 23:59:10 +0000118 const char *ES = "__stack_pointer";
119 auto *SPSymbol = MF.createExternalSymbolName(ES);
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000120 BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::SET_GLOBAL_I32))
Nicholas Wilsone408a892018-08-03 14:33:37 +0000121 .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL)
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000122 .addReg(SrcReg);
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000123}
124
Hans Wennborge1a2e902016-03-31 18:33:38 +0000125MachineBasicBlock::iterator
126WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
Derek Schuff8bb5f292015-12-16 23:21:30 +0000127 MachineFunction &MF, MachineBasicBlock &MBB,
128 MachineBasicBlock::iterator I) const {
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000129 assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000130 "Call frame pseudos should only be used for dynamic stack adjustment");
131 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff4b3bb212016-02-23 18:13:07 +0000132 if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000133 needsSPWriteback(MF)) {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000134 DebugLoc DL = I->getDebugLoc();
Heejin Ahn20c9c442018-08-21 19:52:19 +0000135 writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL);
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000136 }
Hans Wennborge1a2e902016-03-31 18:33:38 +0000137 return MBB.erase(I);
Derek Schuff8bb5f292015-12-16 23:21:30 +0000138}
139
140void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
141 MachineBasicBlock &MBB) const {
142 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
Matthias Braun941a7052016-07-28 18:40:00 +0000143 auto &MFI = MF.getFrameInfo();
144 assert(MFI.getCalleeSavedInfo().empty() &&
Derek Schuff8bb5f292015-12-16 23:21:30 +0000145 "WebAssembly should not have callee-saved registers");
Derek Schuff6ea637a2016-01-29 18:37:49 +0000146
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000147 if (!needsSP(MF)) return;
Matthias Braun941a7052016-07-28 18:40:00 +0000148 uint64_t StackSize = MFI.getStackSize();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000149
Dan Gohman31966502016-01-19 14:53:19 +0000150 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff6ea637a2016-01-29 18:37:49 +0000151 auto &MRI = MF.getRegInfo();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000152
153 auto InsertPt = MBB.begin();
Dan Gohmand934cb82017-02-24 23:18:00 +0000154 while (InsertPt != MBB.end() && WebAssembly::isArgument(*InsertPt))
155 ++InsertPt;
Derek Schuff8bb5f292015-12-16 23:21:30 +0000156 DebugLoc DL;
157
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000158 const TargetRegisterClass *PtrRC =
159 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000160 unsigned SPReg = WebAssembly::SP32;
161 if (StackSize)
162 SPReg = MRI.createVirtualRegister(PtrRC);
Sam Clegg9d24fb72017-06-16 23:59:10 +0000163
164 const char *ES = "__stack_pointer";
165 auto *SPSymbol = MF.createExternalSymbolName(ES);
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000166 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GET_GLOBAL_I32), SPReg)
Nicholas Wilsone408a892018-08-03 14:33:37 +0000167 .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000168
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000169 bool HasBP = hasBP(MF);
170 if (HasBP) {
171 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
172 unsigned BasePtr = MRI.createVirtualRegister(PtrRC);
173 FI->setBasePointerVreg(BasePtr);
174 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
175 .addReg(SPReg);
176 }
Derek Schuff6ea637a2016-01-29 18:37:49 +0000177 if (StackSize) {
178 // Subtract the frame size
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000179 unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000180 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
181 .addImm(StackSize);
Derek Schuff3f063292016-02-11 20:57:09 +0000182 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32),
Derek Schuff6ea637a2016-01-29 18:37:49 +0000183 WebAssembly::SP32)
184 .addReg(SPReg)
185 .addReg(OffsetReg);
186 }
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000187 if (HasBP) {
188 unsigned BitmaskReg = MRI.createVirtualRegister(PtrRC);
189 unsigned Alignment = MFI.getMaxAlignment();
Dan Gohmanf295cc82016-12-02 20:13:05 +0000190 assert((1u << countTrailingZeros(Alignment)) == Alignment &&
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000191 "Alignment must be a power of 2");
192 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg)
193 .addImm((int)~(Alignment - 1));
194 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32),
195 WebAssembly::SP32)
196 .addReg(WebAssembly::SP32)
197 .addReg(BitmaskReg);
198 }
Derek Schuff6ea637a2016-01-29 18:37:49 +0000199 if (hasFP(MF)) {
200 // Unlike most conventional targets (where FP points to the saved FP),
201 // FP points to the bottom of the fixed-size locals, so we can use positive
202 // offsets in load/store instructions.
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000203 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY),
Derek Schuff6ea637a2016-01-29 18:37:49 +0000204 WebAssembly::FP32)
205 .addReg(WebAssembly::SP32);
206 }
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000207 if (StackSize && needsSPWriteback(MF)) {
Heejin Ahn20c9c442018-08-21 19:52:19 +0000208 writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL);
Derek Schuff6ea637a2016-01-29 18:37:49 +0000209 }
Derek Schuff8bb5f292015-12-16 23:21:30 +0000210}
211
Derek Schuff9769deb2015-12-11 23:49:46 +0000212void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
213 MachineBasicBlock &MBB) const {
Heejin Ahnbc6d8972018-08-22 18:53:48 +0000214 uint64_t StackSize = MF.getFrameInfo().getStackSize();
215 if (!needsSP(MF) || !needsSPWriteback(MF)) return;
Dan Gohman31966502016-01-19 14:53:19 +0000216 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000217 auto &MRI = MF.getRegInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000218 auto InsertPt = MBB.getFirstTerminator();
219 DebugLoc DL;
220
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000221 if (InsertPt != MBB.end())
Derek Schuff9769deb2015-12-11 23:49:46 +0000222 DL = InsertPt->getDebugLoc();
Dan Gohman450a8072016-05-05 20:41:15 +0000223
Derek Schuff6ea637a2016-01-29 18:37:49 +0000224 // Restore the stack pointer. If we had fixed-size locals, add the offset
225 // subtracted in the prolog.
Derek Schuffd4207ba2016-03-17 17:00:29 +0000226 unsigned SPReg = 0;
Derek Schuff0d41b7b2016-11-07 22:00:48 +0000227 if (hasBP(MF)) {
228 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
229 SPReg = FI->getBasePointerVreg();
230 } else if (StackSize) {
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000231 const TargetRegisterClass *PtrRC =
232 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
233 unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
Heejin Ahnc2c33c82018-08-20 23:02:15 +0000234 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
235 .addImm(StackSize);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000236 // In the epilog we don't need to write the result back to the SP32 physreg
237 // because it won't be used again. We can use a stackified register instead.
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000238 SPReg = MRI.createVirtualRegister(PtrRC);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000239 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg)
Derek Schuff6ea637a2016-01-29 18:37:49 +0000240 .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32)
241 .addReg(OffsetReg);
Derek Schuffd4207ba2016-03-17 17:00:29 +0000242 } else {
243 SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
Derek Schuff6ea637a2016-01-29 18:37:49 +0000244 }
245
Heejin Ahn20c9c442018-08-21 19:52:19 +0000246 writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
Dan Gohman10e730a2015-06-29 23:51:55 +0000247}