Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===-- 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 Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file contains the WebAssembly implementation of |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 12 | /// 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 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" |
| 33 | #include "llvm/Support/Debug.h" |
| 34 | using namespace llvm; |
| 35 | |
JF Bastien | 03855df | 2015-07-01 23:41:25 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "wasm-frame-info" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 37 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 38 | // TODO: wasm64 |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 39 | // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 40 | |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 41 | /// 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. |
| 45 | bool WebAssemblyFrameLowering::hasBP( |
| 46 | const MachineFunction &MF) const { |
| 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 | |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 81 | |
| 82 | /// Returns true if this function needs a local user-space stack pointer. |
| 83 | /// Unlike a machine stack pointer, the wasm user stack pointer is a global |
| 84 | /// variable, so it is loaded into a register in the prolog. |
| 85 | bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF, |
| 86 | const MachineFrameInfo &MFI) const { |
| 87 | return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF); |
| 88 | } |
| 89 | |
| 90 | /// Returns true if the local user-space stack pointer needs to be written back |
| 91 | /// to memory by this function (this is not meaningful if needsSP is false). If |
| 92 | /// false, the stack red zone can be used and only a local SP is needed. |
| 93 | bool WebAssemblyFrameLowering::needsSPWriteback( |
| 94 | const MachineFunction &MF, const MachineFrameInfo &MFI) const { |
Dan Gohman | 450a807 | 2016-05-05 20:41:15 +0000 | [diff] [blame] | 95 | assert(needsSP(MF, MFI)); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 96 | return MFI.getStackSize() > RedZoneSize || MFI.hasCalls() || |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 97 | MF.getFunction().hasFnAttribute(Attribute::NoRedZone); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame^] | 100 | static void writeSPToGlobal(unsigned SrcReg, MachineFunction &MF, |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 101 | MachineBasicBlock &MBB, |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 102 | MachineBasicBlock::iterator &InsertStore, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 103 | const DebugLoc &DL) { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 104 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 105 | |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 106 | const char *ES = "__stack_pointer"; |
| 107 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 108 | BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::SET_GLOBAL_I32)) |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 109 | .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL) |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 110 | .addReg(SrcReg); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 113 | MachineBasicBlock::iterator |
| 114 | WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 115 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 116 | MachineBasicBlock::iterator I) const { |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 117 | assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) && |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 118 | "Call frame pseudos should only be used for dynamic stack adjustment"); |
| 119 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 120 | if (I->getOpcode() == TII->getCallFrameDestroyOpcode() && |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 121 | needsSPWriteback(MF, MF.getFrameInfo())) { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 122 | DebugLoc DL = I->getDebugLoc(); |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame^] | 123 | writeSPToGlobal(WebAssembly::SP32, MF, MBB, I, DL); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 124 | } |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 125 | return MBB.erase(I); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, |
| 129 | MachineBasicBlock &MBB) const { |
| 130 | // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 131 | auto &MFI = MF.getFrameInfo(); |
| 132 | assert(MFI.getCalleeSavedInfo().empty() && |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 133 | "WebAssembly should not have callee-saved registers"); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 134 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 135 | if (!needsSP(MF, MFI)) return; |
| 136 | uint64_t StackSize = MFI.getStackSize(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 137 | |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 138 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 139 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 140 | |
| 141 | auto InsertPt = MBB.begin(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 142 | while (InsertPt != MBB.end() && WebAssembly::isArgument(*InsertPt)) |
| 143 | ++InsertPt; |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 144 | DebugLoc DL; |
| 145 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 146 | const TargetRegisterClass *PtrRC = |
| 147 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 148 | unsigned SPReg = WebAssembly::SP32; |
| 149 | if (StackSize) |
| 150 | SPReg = MRI.createVirtualRegister(PtrRC); |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 151 | |
| 152 | const char *ES = "__stack_pointer"; |
| 153 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 154 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GET_GLOBAL_I32), SPReg) |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 155 | .addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 156 | |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 157 | bool HasBP = hasBP(MF); |
| 158 | if (HasBP) { |
| 159 | auto FI = MF.getInfo<WebAssemblyFunctionInfo>(); |
| 160 | unsigned BasePtr = MRI.createVirtualRegister(PtrRC); |
| 161 | FI->setBasePointerVreg(BasePtr); |
| 162 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr) |
| 163 | .addReg(SPReg); |
| 164 | } |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 165 | if (StackSize) { |
| 166 | // Subtract the frame size |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 167 | unsigned OffsetReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 168 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 169 | .addImm(StackSize); |
Derek Schuff | 3f06329 | 2016-02-11 20:57:09 +0000 | [diff] [blame] | 170 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 171 | WebAssembly::SP32) |
| 172 | .addReg(SPReg) |
| 173 | .addReg(OffsetReg); |
| 174 | } |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 175 | if (HasBP) { |
| 176 | unsigned BitmaskReg = MRI.createVirtualRegister(PtrRC); |
| 177 | unsigned Alignment = MFI.getMaxAlignment(); |
Dan Gohman | f295cc8 | 2016-12-02 20:13:05 +0000 | [diff] [blame] | 178 | assert((1u << countTrailingZeros(Alignment)) == Alignment && |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 179 | "Alignment must be a power of 2"); |
| 180 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg) |
| 181 | .addImm((int)~(Alignment - 1)); |
| 182 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32), |
| 183 | WebAssembly::SP32) |
| 184 | .addReg(WebAssembly::SP32) |
| 185 | .addReg(BitmaskReg); |
| 186 | } |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 187 | if (hasFP(MF)) { |
| 188 | // Unlike most conventional targets (where FP points to the saved FP), |
| 189 | // FP points to the bottom of the fixed-size locals, so we can use positive |
| 190 | // offsets in load/store instructions. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 191 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 192 | WebAssembly::FP32) |
| 193 | .addReg(WebAssembly::SP32); |
| 194 | } |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 195 | if (StackSize && needsSPWriteback(MF, MFI)) { |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame^] | 196 | writeSPToGlobal(WebAssembly::SP32, MF, MBB, InsertPt, DL); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 197 | } |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 200 | void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, |
| 201 | MachineBasicBlock &MBB) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 202 | auto &MFI = MF.getFrameInfo(); |
| 203 | uint64_t StackSize = MFI.getStackSize(); |
| 204 | if (!needsSP(MF, MFI) || !needsSPWriteback(MF, MFI)) return; |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 205 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 206 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 207 | auto InsertPt = MBB.getFirstTerminator(); |
| 208 | DebugLoc DL; |
| 209 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 210 | if (InsertPt != MBB.end()) |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 211 | DL = InsertPt->getDebugLoc(); |
Dan Gohman | 450a807 | 2016-05-05 20:41:15 +0000 | [diff] [blame] | 212 | |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 213 | // Restore the stack pointer. If we had fixed-size locals, add the offset |
| 214 | // subtracted in the prolog. |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 215 | unsigned SPReg = 0; |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 216 | if (hasBP(MF)) { |
| 217 | auto FI = MF.getInfo<WebAssemblyFunctionInfo>(); |
| 218 | SPReg = FI->getBasePointerVreg(); |
| 219 | } else if (StackSize) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 220 | const TargetRegisterClass *PtrRC = |
| 221 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
| 222 | unsigned OffsetReg = MRI.createVirtualRegister(PtrRC); |
Heejin Ahn | c2c33c8 | 2018-08-20 23:02:15 +0000 | [diff] [blame] | 223 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 224 | .addImm(StackSize); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 225 | // In the epilog we don't need to write the result back to the SP32 physreg |
| 226 | // 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] | 227 | SPReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 228 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 229 | .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32) |
| 230 | .addReg(OffsetReg); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 231 | } else { |
| 232 | SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32; |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Heejin Ahn | 20c9c44 | 2018-08-21 19:52:19 +0000 | [diff] [blame^] | 235 | writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 236 | } |