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); |
Dan Gohman | 7100809 | 2016-05-17 23:19:03 +0000 | [diff] [blame] | 91 | unsigned Drop = 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); |
Dan Gohman | 7100809 | 2016-05-17 23:19:03 +0000 | [diff] [blame] | 99 | BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::STORE_I32), Drop) |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 100 | .addImm(0) |
| 101 | .addReg(SPAddr) |
| 102 | .addImm(2) // p2align |
| 103 | .addReg(SrcReg) |
| 104 | .addMemOperand(MMO); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 107 | MachineBasicBlock::iterator |
| 108 | WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 109 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 110 | MachineBasicBlock::iterator I) const { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 111 | assert(!I->getOperand(0).getImm() && hasFP(MF) && |
| 112 | "Call frame pseudos should only be used for dynamic stack adjustment"); |
| 113 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 114 | if (I->getOpcode() == TII->getCallFrameDestroyOpcode() && |
| 115 | needsSPWriteback(MF, *MF.getFrameInfo())) { |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 116 | DebugLoc DL = I->getDebugLoc(); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 117 | writeSPToMemory(WebAssembly::SP32, MF, MBB, I, I, DL); |
Derek Schuff | 27e3b8a | 2016-02-22 21:57:17 +0000 | [diff] [blame] | 118 | } |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 119 | return MBB.erase(I); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, |
| 123 | MachineBasicBlock &MBB) const { |
| 124 | // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions |
| 125 | auto *MFI = MF.getFrameInfo(); |
| 126 | assert(MFI->getCalleeSavedInfo().empty() && |
| 127 | "WebAssembly should not have callee-saved registers"); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 128 | |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 129 | if (!needsSP(MF, *MFI)) return; |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 130 | uint64_t StackSize = MFI->getStackSize(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 131 | |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 132 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 133 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 134 | |
| 135 | auto InsertPt = MBB.begin(); |
| 136 | DebugLoc DL; |
| 137 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 138 | const TargetRegisterClass *PtrRC = |
| 139 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
| 140 | unsigned SPAddr = MRI.createVirtualRegister(PtrRC); |
| 141 | unsigned SPReg = MRI.createVirtualRegister(PtrRC); |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 142 | const char *ES = "__stack_pointer"; |
| 143 | auto *SPSymbol = MF.createExternalSymbolName(ES); |
Derek Schuff | dc5f6aa | 2016-02-20 21:46:50 +0000 | [diff] [blame] | 144 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPAddr) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 145 | .addExternalSymbol(SPSymbol); |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 146 | auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(MF.getPSVManager() |
| 147 | .getExternalSymbolCallEntry(ES)), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 148 | MachineMemOperand::MOLoad, 4, 4); |
| 149 | // Load the SP value. |
| 150 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), |
JF Bastien | 3ca3ea6 | 2016-01-30 11:19:26 +0000 | [diff] [blame] | 151 | StackSize ? SPReg : (unsigned)WebAssembly::SP32) |
Derek Schuff | dc5f6aa | 2016-02-20 21:46:50 +0000 | [diff] [blame] | 152 | .addImm(0) // offset |
| 153 | .addReg(SPAddr) // addr |
| 154 | .addImm(2) // p2align |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 155 | .addMemOperand(LoadMMO); |
| 156 | |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 157 | if (StackSize) { |
| 158 | // Subtract the frame size |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 159 | unsigned OffsetReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 160 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 161 | .addImm(StackSize); |
Derek Schuff | 3f06329 | 2016-02-11 20:57:09 +0000 | [diff] [blame] | 162 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 163 | WebAssembly::SP32) |
| 164 | .addReg(SPReg) |
| 165 | .addReg(OffsetReg); |
| 166 | } |
| 167 | if (hasFP(MF)) { |
| 168 | // Unlike most conventional targets (where FP points to the saved FP), |
| 169 | // FP points to the bottom of the fixed-size locals, so we can use positive |
| 170 | // offsets in load/store instructions. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 171 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 172 | WebAssembly::FP32) |
| 173 | .addReg(WebAssembly::SP32); |
| 174 | } |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 175 | if (StackSize && needsSPWriteback(MF, *MFI)) { |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 176 | writeSPToMemory(WebAssembly::SP32, MF, MBB, InsertPt, InsertPt, DL); |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 177 | } |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 180 | void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, |
| 181 | MachineBasicBlock &MBB) const { |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 182 | auto *MFI = MF.getFrameInfo(); |
| 183 | uint64_t StackSize = MFI->getStackSize(); |
Derek Schuff | 4b3bb21 | 2016-02-23 18:13:07 +0000 | [diff] [blame] | 184 | if (!needsSP(MF, *MFI) || !needsSPWriteback(MF, *MFI)) return; |
Dan Gohman | 3196650 | 2016-01-19 14:53:19 +0000 | [diff] [blame] | 185 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 186 | auto &MRI = MF.getRegInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 187 | auto InsertPt = MBB.getFirstTerminator(); |
| 188 | DebugLoc DL; |
| 189 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 190 | if (InsertPt != MBB.end()) |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 191 | DL = InsertPt->getDebugLoc(); |
Dan Gohman | 450a807 | 2016-05-05 20:41:15 +0000 | [diff] [blame] | 192 | |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 193 | // Restore the stack pointer. If we had fixed-size locals, add the offset |
| 194 | // subtracted in the prolog. |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 195 | unsigned SPReg = 0; |
| 196 | MachineBasicBlock::iterator InsertAddr = InsertPt; |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 197 | if (StackSize) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 198 | const TargetRegisterClass *PtrRC = |
| 199 | MRI.getTargetRegisterInfo()->getPointerRegClass(MF); |
| 200 | unsigned OffsetReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 201 | InsertAddr = |
| 202 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 203 | .addImm(StackSize); |
| 204 | // In the epilog we don't need to write the result back to the SP32 physreg |
| 205 | // 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] | 206 | SPReg = MRI.createVirtualRegister(PtrRC); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 207 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 208 | .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32) |
| 209 | .addReg(OffsetReg); |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 210 | } else { |
| 211 | SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32; |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Derek Schuff | d4207ba | 2016-03-17 17:00:29 +0000 | [diff] [blame] | 214 | writeSPToMemory(SPReg, MF, MBB, InsertAddr, InsertPt, DL); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 215 | } |