1 //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the WebAssembly implementation of
11 /// 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 "Utils/WebAssemblyTypeUtilities.h"
23 #include "WebAssembly.h"
24 #include "WebAssemblyInstrInfo.h"
25 #include "WebAssemblyMachineFunctionInfo.h"
26 #include "WebAssemblySubtarget.h"
27 #include "WebAssemblyTargetMachine.h"
28 #include "llvm/CodeGen/Analysis.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/Support/Debug.h"
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "wasm-frame-info"
40 
41 // TODO: wasm64
42 // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
43 
44 // In an ideal world, when objects are added to the MachineFrameInfo by
45 // FunctionLoweringInfo::set, we could somehow hook into target-specific code to
46 // ensure they are assigned the right stack ID.  However there isn't a hook that
47 // runs between then and DAG building time, though, so instead we hoist stack
48 // objects lazily when they are first used, and comprehensively after the DAG is
49 // built via the PreprocessISelDAG hook, called by the
50 // SelectionDAGISel::runOnMachineFunction.  We have to do it in two places
51 // because we want to do it while building the selection DAG for uses of alloca,
52 // but not all alloca instructions are used so we have to follow up afterwards.
53 Optional<unsigned>
54 WebAssemblyFrameLowering::getLocalForStackObject(MachineFunction &MF,
55                                                  int FrameIndex) {
56   MachineFrameInfo &MFI = MF.getFrameInfo();
57 
58   // If already hoisted to a local, done.
59   if (MFI.getStackID(FrameIndex) == TargetStackID::WasmLocal)
60     return static_cast<unsigned>(MFI.getObjectOffset(FrameIndex));
61 
62   // If not allocated in the object address space, this object will be in
63   // linear memory.
64   const AllocaInst *AI = MFI.getObjectAllocation(FrameIndex);
65   if (!AI ||
66       !WebAssembly::isWasmVarAddressSpace(AI->getType()->getAddressSpace()))
67     return None;
68 
69   // Otherwise, allocate this object in the named value stack, outside of linear
70   // memory.
71   SmallVector<EVT, 4> ValueVTs;
72   const WebAssemblyTargetLowering &TLI =
73       *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
74   WebAssemblyFunctionInfo *FuncInfo = MF.getInfo<WebAssemblyFunctionInfo>();
75   ComputeValueVTs(TLI, MF.getDataLayout(), AI->getAllocatedType(), ValueVTs);
76   MFI.setStackID(FrameIndex, TargetStackID::WasmLocal);
77   // Abuse SP offset to record the index of the first local in the object.
78   unsigned Local = FuncInfo->getParams().size() + FuncInfo->getLocals().size();
79   MFI.setObjectOffset(FrameIndex, Local);
80   // Allocate WebAssembly locals for each non-aggregate component of the
81   // allocation.
82   for (EVT ValueVT : ValueVTs)
83     FuncInfo->addLocal(ValueVT.getSimpleVT());
84   // Abuse object size to record number of WebAssembly locals allocated to
85   // this object.
86   MFI.setObjectSize(FrameIndex, ValueVTs.size());
87   return static_cast<unsigned>(Local);
88 }
89 
90 /// We need a base pointer in the case of having items on the stack that
91 /// require stricter alignment than the stack pointer itself.  Because we need
92 /// to shift the stack pointer by some unknown amount to force the alignment,
93 /// we need to record the value of the stack pointer on entry to the function.
94 bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
95   const auto *RegInfo =
96       MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
97   return RegInfo->hasStackRealignment(MF);
98 }
99 
100 /// Return true if the specified function should have a dedicated frame pointer
101 /// register.
102 bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
103   const MachineFrameInfo &MFI = MF.getFrameInfo();
104 
105   // When we have var-sized objects, we move the stack pointer by an unknown
106   // amount, and need to emit a frame pointer to restore the stack to where we
107   // were on function entry.
108   // If we already need a base pointer, we use that to fix up the stack pointer.
109   // If there are no fixed-size objects, we would have no use of a frame
110   // pointer, and thus should not emit one.
111   bool HasFixedSizedObjects = MFI.getStackSize() > 0;
112   bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
113 
114   return MFI.isFrameAddressTaken() ||
115          (MFI.hasVarSizedObjects() && NeedsFixedReference) ||
116          MFI.hasStackMap() || MFI.hasPatchPoint();
117 }
118 
119 /// Under normal circumstances, when a frame pointer is not required, we reserve
120 /// argument space for call sites in the function immediately on entry to the
121 /// current function. This eliminates the need for add/sub sp brackets around
122 /// call sites. Returns true if the call frame is included as part of the stack
123 /// frame.
124 bool WebAssemblyFrameLowering::hasReservedCallFrame(
125     const MachineFunction &MF) const {
126   return !MF.getFrameInfo().hasVarSizedObjects();
127 }
128 
129 // Returns true if this function needs a local user-space stack pointer for its
130 // local frame (not for exception handling).
131 bool WebAssemblyFrameLowering::needsSPForLocalFrame(
132     const MachineFunction &MF) const {
133   auto &MFI = MF.getFrameInfo();
134   return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
135 }
136 
137 // In function with EH pads, we need to make a copy of the value of
138 // __stack_pointer global in SP32/64 register, in order to use it when
139 // restoring __stack_pointer after an exception is caught.
140 bool WebAssemblyFrameLowering::needsPrologForEH(
141     const MachineFunction &MF) const {
142   auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
143   return EHType == ExceptionHandling::Wasm &&
144          MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
145 }
146 
147 /// Returns true if this function needs a local user-space stack pointer.
148 /// Unlike a machine stack pointer, the wasm user stack pointer is a global
149 /// variable, so it is loaded into a register in the prolog.
150 bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
151   return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
152 }
153 
154 /// Returns true if the local user-space stack pointer needs to be written back
155 /// to __stack_pointer global by this function (this is not meaningful if
156 /// needsSP is false). If false, the stack red zone can be used and only a local
157 /// SP is needed.
158 bool WebAssemblyFrameLowering::needsSPWriteback(
159     const MachineFunction &MF) const {
160   auto &MFI = MF.getFrameInfo();
161   assert(needsSP(MF));
162   // When we don't need a local stack pointer for its local frame but only to
163   // support EH, we don't need to write SP back in the epilog, because we don't
164   // bump down the stack pointer in the prolog. We need to write SP back in the
165   // epilog only if
166   // 1. We need SP not only for EH support but also because we actually use
167   // stack or we have a frame address taken.
168   // 2. We cannot use the red zone.
169   bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
170                        !MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
171   return needsSPForLocalFrame(MF) && !CanUseRedZone;
172 }
173 
174 unsigned WebAssemblyFrameLowering::getSPReg(const MachineFunction &MF) {
175   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
176              ? WebAssembly::SP64
177              : WebAssembly::SP32;
178 }
179 
180 unsigned WebAssemblyFrameLowering::getFPReg(const MachineFunction &MF) {
181   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
182              ? WebAssembly::FP64
183              : WebAssembly::FP32;
184 }
185 
186 unsigned
187 WebAssemblyFrameLowering::getOpcConst(const MachineFunction &MF) {
188   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
189              ? WebAssembly::CONST_I64
190              : WebAssembly::CONST_I32;
191 }
192 
193 unsigned WebAssemblyFrameLowering::getOpcAdd(const MachineFunction &MF) {
194   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
195              ? WebAssembly::ADD_I64
196              : WebAssembly::ADD_I32;
197 }
198 
199 unsigned WebAssemblyFrameLowering::getOpcSub(const MachineFunction &MF) {
200   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
201              ? WebAssembly::SUB_I64
202              : WebAssembly::SUB_I32;
203 }
204 
205 unsigned WebAssemblyFrameLowering::getOpcAnd(const MachineFunction &MF) {
206   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
207              ? WebAssembly::AND_I64
208              : WebAssembly::AND_I32;
209 }
210 
211 unsigned
212 WebAssemblyFrameLowering::getOpcGlobGet(const MachineFunction &MF) {
213   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
214              ? WebAssembly::GLOBAL_GET_I64
215              : WebAssembly::GLOBAL_GET_I32;
216 }
217 
218 unsigned
219 WebAssemblyFrameLowering::getOpcGlobSet(const MachineFunction &MF) {
220   return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
221              ? WebAssembly::GLOBAL_SET_I64
222              : WebAssembly::GLOBAL_SET_I32;
223 }
224 
225 void WebAssemblyFrameLowering::writeSPToGlobal(
226     unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
227     MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
228   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
229 
230   const char *ES = "__stack_pointer";
231   auto *SPSymbol = MF.createExternalSymbolName(ES);
232 
233   BuildMI(MBB, InsertStore, DL, TII->get(getOpcGlobSet(MF)))
234       .addExternalSymbol(SPSymbol)
235       .addReg(SrcReg);
236 }
237 
238 MachineBasicBlock::iterator
239 WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
240     MachineFunction &MF, MachineBasicBlock &MBB,
241     MachineBasicBlock::iterator I) const {
242   assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
243          "Call frame pseudos should only be used for dynamic stack adjustment");
244   auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
245   const auto *TII = ST.getInstrInfo();
246   if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
247       needsSPWriteback(MF)) {
248     DebugLoc DL = I->getDebugLoc();
249     writeSPToGlobal(getSPReg(MF), MF, MBB, I, DL);
250   }
251   return MBB.erase(I);
252 }
253 
254 void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
255                                             MachineBasicBlock &MBB) const {
256   // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
257   auto &MFI = MF.getFrameInfo();
258   assert(MFI.getCalleeSavedInfo().empty() &&
259          "WebAssembly should not have callee-saved registers");
260 
261   if (!needsSP(MF))
262     return;
263   uint64_t StackSize = MFI.getStackSize();
264 
265   auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
266   const auto *TII = ST.getInstrInfo();
267   auto &MRI = MF.getRegInfo();
268 
269   auto InsertPt = MBB.begin();
270   while (InsertPt != MBB.end() &&
271          WebAssembly::isArgument(InsertPt->getOpcode()))
272     ++InsertPt;
273   DebugLoc DL;
274 
275   const TargetRegisterClass *PtrRC =
276       MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
277   unsigned SPReg = getSPReg(MF);
278   if (StackSize)
279     SPReg = MRI.createVirtualRegister(PtrRC);
280 
281   const char *ES = "__stack_pointer";
282   auto *SPSymbol = MF.createExternalSymbolName(ES);
283   BuildMI(MBB, InsertPt, DL, TII->get(getOpcGlobGet(MF)), SPReg)
284       .addExternalSymbol(SPSymbol);
285 
286   bool HasBP = hasBP(MF);
287   if (HasBP) {
288     auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
289     Register BasePtr = MRI.createVirtualRegister(PtrRC);
290     FI->setBasePointerVreg(BasePtr);
291     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
292         .addReg(SPReg);
293   }
294   if (StackSize) {
295     // Subtract the frame size
296     Register OffsetReg = MRI.createVirtualRegister(PtrRC);
297     BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
298         .addImm(StackSize);
299     BuildMI(MBB, InsertPt, DL, TII->get(getOpcSub(MF)), getSPReg(MF))
300         .addReg(SPReg)
301         .addReg(OffsetReg);
302   }
303   if (HasBP) {
304     Register BitmaskReg = MRI.createVirtualRegister(PtrRC);
305     Align Alignment = MFI.getMaxAlign();
306     BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), BitmaskReg)
307         .addImm((int64_t) ~(Alignment.value() - 1));
308     BuildMI(MBB, InsertPt, DL, TII->get(getOpcAnd(MF)), getSPReg(MF))
309         .addReg(getSPReg(MF))
310         .addReg(BitmaskReg);
311   }
312   if (hasFP(MF)) {
313     // Unlike most conventional targets (where FP points to the saved FP),
314     // FP points to the bottom of the fixed-size locals, so we can use positive
315     // offsets in load/store instructions.
316     BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), getFPReg(MF))
317         .addReg(getSPReg(MF));
318   }
319   if (StackSize && needsSPWriteback(MF)) {
320     writeSPToGlobal(getSPReg(MF), MF, MBB, InsertPt, DL);
321   }
322 }
323 
324 void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
325                                             MachineBasicBlock &MBB) const {
326   uint64_t StackSize = MF.getFrameInfo().getStackSize();
327   if (!needsSP(MF) || !needsSPWriteback(MF))
328     return;
329   auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
330   const auto *TII = ST.getInstrInfo();
331   auto &MRI = MF.getRegInfo();
332   auto InsertPt = MBB.getFirstTerminator();
333   DebugLoc DL;
334 
335   if (InsertPt != MBB.end())
336     DL = InsertPt->getDebugLoc();
337 
338   // Restore the stack pointer. If we had fixed-size locals, add the offset
339   // subtracted in the prolog.
340   unsigned SPReg = 0;
341   unsigned SPFPReg = hasFP(MF) ? getFPReg(MF) : getSPReg(MF);
342   if (hasBP(MF)) {
343     auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
344     SPReg = FI->getBasePointerVreg();
345   } else if (StackSize) {
346     const TargetRegisterClass *PtrRC =
347         MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
348     Register OffsetReg = MRI.createVirtualRegister(PtrRC);
349     BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
350         .addImm(StackSize);
351     // In the epilog we don't need to write the result back to the SP32/64
352     // physreg because it won't be used again. We can use a stackified register
353     // instead.
354     SPReg = MRI.createVirtualRegister(PtrRC);
355     BuildMI(MBB, InsertPt, DL, TII->get(getOpcAdd(MF)), SPReg)
356         .addReg(SPFPReg)
357         .addReg(OffsetReg);
358   } else {
359     SPReg = SPFPReg;
360   }
361 
362   writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
363 }
364 
365 bool WebAssemblyFrameLowering::isSupportedStackID(
366     TargetStackID::Value ID) const {
367   // Use the Object stack for WebAssembly locals which can only be accessed
368   // by name, not via an address in linear memory.
369   if (ID == TargetStackID::WasmLocal)
370     return true;
371 
372   return TargetFrameLowering::isSupportedStackID(ID);
373 }
374 
375 TargetFrameLowering::DwarfFrameBase
376 WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
377   DwarfFrameBase Loc;
378   Loc.Kind = DwarfFrameBase::WasmFrameBase;
379   const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
380   if (needsSP(MF) && MFI.isFrameBaseVirtual()) {
381     unsigned LocalNum = MFI.getFrameBaseLocal();
382     Loc.Location.WasmLoc = {WebAssembly::TI_LOCAL, LocalNum};
383   } else {
384     // TODO: This should work on a breakpoint at a function with no frame,
385     // but probably won't work for traversing up the stack.
386     Loc.Location.WasmLoc = {WebAssembly::TI_GLOBAL_RELOC, 0};
387   }
388   return Loc;
389 }
390