Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file contains the WebAssembly implementation of |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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" |
Derek Schuff | ff171ac | 2019-12-18 14:50:19 -0800 | [diff] [blame] | 22 | #include "WebAssembly.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 23 | #include "WebAssemblyInstrInfo.h" |
| 24 | #include "WebAssemblyMachineFunctionInfo.h" |
| 25 | #include "WebAssemblySubtarget.h" |
| 26 | #include "WebAssemblyTargetMachine.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 27 | #include "WebAssemblyUtilities.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 29 | #include "llvm/CodeGen/MachineFunction.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Heejin Ahn | 78d1910 | 2018-08-21 21:23:07 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCAsmInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | using namespace llvm; |
| 36 | |
JF Bastien | 03855df | 2015-07-01 23:41:25 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "wasm-frame-info" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 38 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 39 | // TODO: wasm64 |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 40 | // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 41 | |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 42 | /// 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 Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 46 | bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const { |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 47 | const auto *RegInfo = |
| 48 | MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); |
| 49 | return RegInfo->needsStackRealignment(MF); |
| 50 | } |
| 51 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 52 | /// Return true if the specified function should have a dedicated frame pointer |
| 53 | /// register. |
| 54 | bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 55 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 56 | |
| 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 Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 69 | } |
| 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. |
| 76 | bool WebAssemblyFrameLowering::hasReservedCallFrame( |
| 77 | const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 78 | return !MF.getFrameInfo().hasVarSizedObjects(); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Heejin Ahn | 972fc35 | 2018-08-22 21:13:49 +0000 | [diff] [blame] | 81 | // Returns true if this function needs a local user-space stack pointer for its |
| 82 | // local frame (not for exception handling). |
| 83 | bool WebAssemblyFrameLowering::needsSPForLocalFrame( |
| 84 | const MachineFunction &MF) const { |
| 85 | auto &MFI = MF.getFrameInfo(); |
| 86 | return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF); |
| 87 | } |
| 88 | |
Heejin Ahn | 78d1910 | 2018-08-21 21:23:07 +0000 | [diff] [blame] | 89 | // 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. |
| 92 | bool 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 Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 98 | |
| 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 Ahn | bc6d897 | 2018-08-22 18:53:48 +0000 | [diff] [blame] | 102 | bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const { |
Heejin Ahn | 972fc35 | 2018-08-22 21:13:49 +0000 | [diff] [blame] | 103 | return needsSPForLocalFrame(MF) || needsPrologForEH(MF); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// Returns true if the local user-space stack pointer needs to be written back |
Heejin Ahn | c4df1d1 | 2018-08-22 00:20:02 +0000 | [diff] [blame] | 107 | /// 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 Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 110 | bool WebAssemblyFrameLowering::needsSPWriteback( |
Heejin Ahn | bc6d897 | 2018-08-22 18:53:48 +0000 | [diff] [blame] | 111 | const MachineFunction &MF) const { |
| 112 | auto &MFI = MF.getFrameInfo(); |
| 113 | assert(needsSP(MF)); |
Heejin Ahn | 972fc35 | 2018-08-22 21:13:49 +0000 | [diff] [blame] | 114 | // 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 Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Heejin Ahn | 78d1910 | 2018-08-21 21:23:07 +0000 | [diff] [blame] | 126 | void WebAssemblyFrameLowering::writeSPToGlobal( |
| 127 | unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB, |
| 128 | MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 129 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 130 | |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 131 | const char *ES = "__stack_pointer"; |
| 132 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 133 | BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::GLOBAL_SET_I32)) |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 134 | .addExternalSymbol(SPSymbol) |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 135 | .addReg(SrcReg); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 138 | MachineBasicBlock::iterator |
| 139 | WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 140 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 141 | MachineBasicBlock::iterator I) const { |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 142 | assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) && |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 143 | "Call frame pseudos should only be used for dynamic stack adjustment"); |
| 144 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 145 | if (I->getOpcode() == TII->getCallFrameDestroyOpcode() && |
Heejin Ahn | bc6d897 | 2018-08-22 18:53:48 +0000 | [diff] [blame] | 146 | needsSPWriteback(MF)) { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 147 | DebugLoc DL = I->getDebugLoc(); |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame] | 148 | writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 149 | } |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 150 | return MBB.erase(I); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, |
| 154 | MachineBasicBlock &MBB) const { |
| 155 | // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 156 | auto &MFI = MF.getFrameInfo(); |
| 157 | assert(MFI.getCalleeSavedInfo().empty() && |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 158 | "WebAssembly should not have callee-saved registers"); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 159 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 160 | if (!needsSP(MF)) |
| 161 | return; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 162 | uint64_t StackSize = MFI.getStackSize(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 163 | |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 164 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 165 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 166 | |
| 167 | auto InsertPt = MBB.begin(); |
Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 168 | while (InsertPt != MBB.end() && |
| 169 | WebAssembly::isArgument(InsertPt->getOpcode())) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 170 | ++InsertPt; |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 171 | DebugLoc DL; |
| 172 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 173 | const TargetRegisterClass *PtrRC = |
| 174 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 175 | unsigned SPReg = WebAssembly::SP32; |
| 176 | if (StackSize) |
| 177 | SPReg = MRI.createVirtualRegister(PtrRC); |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 178 | |
| 179 | const char *ES = "__stack_pointer"; |
| 180 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 181 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GLOBAL_GET_I32), SPReg) |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 182 | .addExternalSymbol(SPSymbol); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 183 | |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 184 | bool HasBP = hasBP(MF); |
| 185 | if (HasBP) { |
| 186 | auto FI = MF.getInfo<WebAssemblyFunctionInfo>(); |
Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 187 | Register BasePtr = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 188 | FI->setBasePointerVreg(BasePtr); |
| 189 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr) |
| 190 | .addReg(SPReg); |
| 191 | } |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 192 | if (StackSize) { |
| 193 | // Subtract the frame size |
Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 194 | Register OffsetReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 195 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 196 | .addImm(StackSize); |
Derek Schuff | 3f06329 | 2016-02-11 20:57:09 +0000 | [diff] [blame] | 197 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 198 | WebAssembly::SP32) |
| 199 | .addReg(SPReg) |
| 200 | .addReg(OffsetReg); |
| 201 | } |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 202 | if (HasBP) { |
Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 203 | Register BitmaskReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 204 | unsigned Alignment = MFI.getMaxAlignment(); |
Dan Gohman | f295cc8 | 2016-12-02 20:13:05 +0000 | [diff] [blame] | 205 | assert((1u << countTrailingZeros(Alignment)) == Alignment && |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 206 | "Alignment must be a power of 2"); |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 207 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg) |
| 208 | .addImm((int)~(Alignment - 1)); |
| 209 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32), |
| 210 | WebAssembly::SP32) |
| 211 | .addReg(WebAssembly::SP32) |
| 212 | .addReg(BitmaskReg); |
| 213 | } |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 214 | if (hasFP(MF)) { |
| 215 | // Unlike most conventional targets (where FP points to the saved FP), |
| 216 | // FP points to the bottom of the fixed-size locals, so we can use positive |
| 217 | // offsets in load/store instructions. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 218 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), WebAssembly::FP32) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 219 | .addReg(WebAssembly::SP32); |
| 220 | } |
Heejin Ahn | bc6d897 | 2018-08-22 18:53:48 +0000 | [diff] [blame] | 221 | if (StackSize && needsSPWriteback(MF)) { |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame] | 222 | writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 223 | } |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 226 | void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, |
| 227 | MachineBasicBlock &MBB) const { |
Heejin Ahn | bc6d897 | 2018-08-22 18:53:48 +0000 | [diff] [blame] | 228 | uint64_t StackSize = MF.getFrameInfo().getStackSize(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 229 | if (!needsSP(MF) || !needsSPWriteback(MF)) |
| 230 | return; |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 231 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 232 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 233 | auto InsertPt = MBB.getFirstTerminator(); |
| 234 | DebugLoc DL; |
| 235 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 236 | if (InsertPt != MBB.end()) |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 237 | DL = InsertPt->getDebugLoc(); |
Dan Gohman | 450a807 | 2016-05-05 20:41:15 +0000 | [diff] [blame] | 238 | |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 239 | // Restore the stack pointer. If we had fixed-size locals, add the offset |
| 240 | // subtracted in the prolog. |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 241 | unsigned SPReg = 0; |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 242 | if (hasBP(MF)) { |
| 243 | auto FI = MF.getInfo<WebAssemblyFunctionInfo>(); |
| 244 | SPReg = FI->getBasePointerVreg(); |
| 245 | } else if (StackSize) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 246 | const TargetRegisterClass *PtrRC = |
| 247 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 248 | Register OffsetReg = MRI.createVirtualRegister(PtrRC); |
Heejin Ahn | c2c33c8 | 2018-08-20 23:02:15 +0000 | [diff] [blame] | 249 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 250 | .addImm(StackSize); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 251 | // In the epilog we don't need to write the result back to the SP32 physreg |
| 252 | // because it won't be used again. We can use a stackified register instead. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 253 | SPReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 254 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 255 | .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32) |
| 256 | .addReg(OffsetReg); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 257 | } else { |
| 258 | SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32; |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame] | 261 | writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 262 | } |
Derek Schuff | ff171ac | 2019-12-18 14:50:19 -0800 | [diff] [blame] | 263 | |
| 264 | TargetFrameLowering::DwarfFrameBase |
| 265 | WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const { |
| 266 | DwarfFrameBase Loc; |
| 267 | Loc.Kind = DwarfFrameBase::WasmFrameBase; |
| 268 | const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
Derek Schuff | d966bf8 | 2020-01-28 13:46:03 -0800 | [diff] [blame] | 269 | if (needsSP(MF) && MFI.isFrameBaseVirtual()) { |
Derek Schuff | ff171ac | 2019-12-18 14:50:19 -0800 | [diff] [blame] | 270 | unsigned LocalNum = MFI.getFrameBaseLocal(); |
| 271 | Loc.Location.WasmLoc = {WebAssembly::TI_LOCAL_START, LocalNum}; |
| 272 | } else { |
| 273 | // TODO: This should work on a breakpoint at a function with no frame, |
| 274 | // but probably won't work for traversing up the stack. |
| 275 | // TODO: This needs a relocation for correct __stack_pointer |
| 276 | Loc.Location.WasmLoc = {WebAssembly::TI_GLOBAL_START, 0}; |
| 277 | } |
| 278 | return Loc; |
| 279 | } |