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 |
| 11 | /// \brief This file contains the WebAssembly implementation of |
| 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" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | using namespace llvm; |
| 34 | |
JF Bastien | 03855df | 2015-07-01 23:41:25 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "wasm-frame-info" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 36 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 37 | // TODO: wasm64 |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 38 | // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 39 | |
| 40 | /// Return true if the specified function should have a dedicated frame pointer |
| 41 | /// register. |
| 42 | bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 43 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dan Gohman | e419a7c | 2015-08-24 16:46:31 +0000 | [diff] [blame] | 44 | const auto *RegInfo = |
| 45 | MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); |
Dan Gohman | 94c6566 | 2016-02-16 23:48:04 +0000 | [diff] [blame] | 46 | return MFI->isFrameAddressTaken() || MFI->hasVarSizedObjects() || |
| 47 | MFI->hasStackMap() || MFI->hasPatchPoint() || |
| 48 | RegInfo->needsStackRealignment(MF); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// Under normal circumstances, when a frame pointer is not required, we reserve |
| 52 | /// argument space for call sites in the function immediately on entry to the |
| 53 | /// current function. This eliminates the need for add/sub sp brackets around |
| 54 | /// call sites. Returns true if the call frame is included as part of the stack |
| 55 | /// frame. |
| 56 | bool WebAssemblyFrameLowering::hasReservedCallFrame( |
| 57 | const MachineFunction &MF) const { |
| 58 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 59 | } |
| 60 | |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 61 | |
| 62 | /// Returns true if this function needs a local user-space stack pointer. |
| 63 | /// Unlike a machine stack pointer, the wasm user stack pointer is a global |
| 64 | /// variable, so it is loaded into a register in the prolog. |
| 65 | bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF, |
| 66 | const MachineFrameInfo &MFI) const { |
| 67 | return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF); |
| 68 | } |
| 69 | |
| 70 | /// Returns true if the local user-space stack pointer needs to be written back |
| 71 | /// to memory by this function (this is not meaningful if needsSP is false). If |
| 72 | /// false, the stack red zone can be used and only a local SP is needed. |
| 73 | bool WebAssemblyFrameLowering::needsSPWriteback( |
| 74 | const MachineFunction &MF, const MachineFrameInfo &MFI) const { |
Dan Gohman | 450a807 | 2016-05-05 20:41:15 +0000 | [diff] [blame] | 75 | assert(needsSP(MF, MFI)); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 76 | return MFI.getStackSize() > RedZoneSize || MFI.hasCalls() || |
| 77 | MF.getFunction()->hasFnAttribute(Attribute::NoRedZone); |
| 78 | } |
| 79 | |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 80 | static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF, |
| 81 | MachineBasicBlock &MBB, |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 82 | MachineBasicBlock::iterator &InsertAddr, |
| 83 | MachineBasicBlock::iterator &InsertStore, |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 84 | DebugLoc DL) { |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame^] | 85 | const char *ES = "__stack_pointer"; |
| 86 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 87 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 88 | const TargetRegisterClass *PtrRC = |
| 89 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
| 90 | unsigned SPAddr = MRI.createVirtualRegister(PtrRC); |
| 91 | unsigned Discard = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 92 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 93 | |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 94 | BuildMI(MBB, InsertAddr, DL, TII->get(WebAssembly::CONST_I32), SPAddr) |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 95 | .addExternalSymbol(SPSymbol); |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame^] | 96 | auto *MMO = new MachineMemOperand(MachinePointerInfo(MF.getPSVManager() |
| 97 | .getExternalSymbolCallEntry(ES)), |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 98 | MachineMemOperand::MOStore, 4, 4); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 99 | BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::STORE_I32), |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 100 | Discard) |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 101 | .addImm(0) |
| 102 | .addReg(SPAddr) |
| 103 | .addImm(2) // p2align |
| 104 | .addReg(SrcReg) |
| 105 | .addMemOperand(MMO); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 108 | MachineBasicBlock::iterator |
| 109 | WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 110 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 111 | MachineBasicBlock::iterator I) const { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 112 | assert(!I->getOperand(0).getImm() && hasFP(MF) && |
| 113 | "Call frame pseudos should only be used for dynamic stack adjustment"); |
| 114 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 115 | if (I->getOpcode() == TII->getCallFrameDestroyOpcode() && |
| 116 | needsSPWriteback(MF, *MF.getFrameInfo())) { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 117 | DebugLoc DL = I->getDebugLoc(); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 118 | writeSPToMemory(WebAssembly::SP32, MF, MBB, I, I, DL); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 119 | } |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 120 | return MBB.erase(I); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, |
| 124 | MachineBasicBlock &MBB) const { |
| 125 | // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions |
| 126 | auto *MFI = MF.getFrameInfo(); |
| 127 | assert(MFI->getCalleeSavedInfo().empty() && |
| 128 | "WebAssembly should not have callee-saved registers"); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 129 | |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 130 | if (!needsSP(MF, *MFI)) return; |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 131 | uint64_t StackSize = MFI->getStackSize(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 132 | |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 133 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 134 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 135 | |
| 136 | auto InsertPt = MBB.begin(); |
| 137 | DebugLoc DL; |
| 138 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 139 | const TargetRegisterClass *PtrRC = |
| 140 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
| 141 | unsigned SPAddr = MRI.createVirtualRegister(PtrRC); |
| 142 | unsigned SPReg = MRI.createVirtualRegister(PtrRC); |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame^] | 143 | const char *ES = "__stack_pointer"; |
| 144 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Derek Schuff | dc5f6aa | 2016-02-20 21:46:50 +0000 | [diff] [blame] | 145 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPAddr) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 146 | .addExternalSymbol(SPSymbol); |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame^] | 147 | auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(MF.getPSVManager() |
| 148 | .getExternalSymbolCallEntry(ES)), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 149 | MachineMemOperand::MOLoad, 4, 4); |
| 150 | // Load the SP value. |
| 151 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), |
JF Bastien | 3ca3ea6 | 2016-01-30 11:19:26 +0000 | [diff] [blame] | 152 | StackSize ? SPReg : (unsigned)WebAssembly::SP32) |
Derek Schuff | dc5f6aa | 2016-02-20 21:46:50 +0000 | [diff] [blame] | 153 | .addImm(0) // offset |
| 154 | .addReg(SPAddr) // addr |
| 155 | .addImm(2) // p2align |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 156 | .addMemOperand(LoadMMO); |
| 157 | |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 158 | if (StackSize) { |
| 159 | // Subtract the frame size |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 160 | unsigned OffsetReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 161 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 162 | .addImm(StackSize); |
Derek Schuff | 3f06329 | 2016-02-11 20:57:09 +0000 | [diff] [blame] | 163 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 164 | WebAssembly::SP32) |
| 165 | .addReg(SPReg) |
| 166 | .addReg(OffsetReg); |
| 167 | } |
| 168 | if (hasFP(MF)) { |
| 169 | // Unlike most conventional targets (where FP points to the saved FP), |
| 170 | // FP points to the bottom of the fixed-size locals, so we can use positive |
| 171 | // offsets in load/store instructions. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 172 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 173 | WebAssembly::FP32) |
| 174 | .addReg(WebAssembly::SP32); |
| 175 | } |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 176 | if (StackSize && needsSPWriteback(MF, *MFI)) { |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 177 | writeSPToMemory(WebAssembly::SP32, MF, MBB, InsertPt, InsertPt, DL); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 178 | } |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 181 | void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, |
| 182 | MachineBasicBlock &MBB) const { |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 183 | auto *MFI = MF.getFrameInfo(); |
| 184 | uint64_t StackSize = MFI->getStackSize(); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 185 | if (!needsSP(MF, *MFI) || !needsSPWriteback(MF, *MFI)) return; |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 186 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 187 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 188 | auto InsertPt = MBB.getFirstTerminator(); |
| 189 | DebugLoc DL; |
| 190 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 191 | if (InsertPt != MBB.end()) |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 192 | DL = InsertPt->getDebugLoc(); |
Dan Gohman | 450a807 | 2016-05-05 20:41:15 +0000 | [diff] [blame] | 193 | |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 194 | // Restore the stack pointer. If we had fixed-size locals, add the offset |
| 195 | // subtracted in the prolog. |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 196 | unsigned SPReg = 0; |
| 197 | MachineBasicBlock::iterator InsertAddr = InsertPt; |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 198 | if (StackSize) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 199 | const TargetRegisterClass *PtrRC = |
| 200 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
| 201 | unsigned OffsetReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 202 | InsertAddr = |
| 203 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 204 | .addImm(StackSize); |
| 205 | // In the epilog we don't need to write the result back to the SP32 physreg |
| 206 | // 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] | 207 | SPReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 208 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 209 | .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32) |
| 210 | .addReg(OffsetReg); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 211 | } else { |
| 212 | SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32; |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 215 | writeSPToMemory(SPReg, MF, MBB, InsertAddr, InsertPt, DL); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 216 | } |