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 | |
| 37 | // TODO: Implement a red zone? |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame^] | 38 | // TODO: wasm64 |
| 39 | // TODO: Prolog/epilog should be stackified too. This pass runs after register |
| 40 | // stackification, so we'll have to do it manually. |
| 41 | // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 42 | |
| 43 | /// Return true if the specified function should have a dedicated frame pointer |
| 44 | /// register. |
| 45 | bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 46 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dan Gohman | e419a7c | 2015-08-24 16:46:31 +0000 | [diff] [blame] | 47 | const auto *RegInfo = |
| 48 | MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame^] | 49 | return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() || |
| 50 | MFI->hasStackMap() || MFI->hasPatchPoint() || |
| 51 | RegInfo->needsStackRealignment(MF); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /// Under normal circumstances, when a frame pointer is not required, we reserve |
| 55 | /// argument space for call sites in the function immediately on entry to the |
| 56 | /// current function. This eliminates the need for add/sub sp brackets around |
| 57 | /// call sites. Returns true if the call frame is included as part of the stack |
| 58 | /// frame. |
| 59 | bool WebAssemblyFrameLowering::hasReservedCallFrame( |
| 60 | const MachineFunction &MF) const { |
| 61 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 62 | } |
| 63 | |
| 64 | void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 65 | MachineFunction & /*MF*/, MachineBasicBlock & /*MBB*/, |
| 66 | MachineBasicBlock::iterator /*I*/) const { |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 67 | llvm_unreachable("TODO: implement eliminateCallFramePseudoInstr"); |
| 68 | } |
| 69 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame^] | 70 | void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, |
| 71 | MachineBasicBlock &MBB) const { |
| 72 | // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions |
| 73 | auto *MFI = MF.getFrameInfo(); |
| 74 | assert(MFI->getCalleeSavedInfo().empty() && |
| 75 | "WebAssembly should not have callee-saved registers"); |
| 76 | assert(!hasFP(MF) && "Functions needing frame pointers not yet supported"); |
| 77 | assert(!MFI->adjustsStack() && "Dynamic stack adjustmet not yet supported"); |
| 78 | uint64_t StackSize = MFI->getStackSize(); |
| 79 | if (!StackSize) |
| 80 | return; |
| 81 | |
| 82 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
| 83 | auto &MRI = MF.getRegInfo(); |
| 84 | auto InsertPt = MBB.begin(); |
| 85 | DebugLoc DL; |
| 86 | |
| 87 | // Get the current stacktop |
| 88 | // TODO: To support dynamic alloc, also copy to FP |
| 89 | unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 90 | auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer"); |
| 91 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPReg) |
| 92 | .addExternalSymbol(SPSymbol); |
| 93 | // This MachinePointerInfo should reference __stack_pointer as well but |
| 94 | // doesn't because MachinePointerInfo() takes a GV which we don't have for |
| 95 | // __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry |
| 96 | // is appropriate instead. (likewise for EmitEpologue below) |
| 97 | auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(), |
| 98 | MachineMemOperand::MOLoad, 4, 4); |
| 99 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), SPReg) |
| 100 | .addImm(0) |
| 101 | .addReg(SPReg) |
| 102 | .addMemOperand(LoadMMO); |
| 103 | // Subtract the frame size |
| 104 | unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 105 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 106 | .addImm(StackSize); |
| 107 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32), WebAssembly::SP32) |
| 108 | .addReg(SPReg) |
| 109 | .addReg(OffsetReg); |
| 110 | // The SP32 register now has the new stacktop. Also write it back to memory. |
| 111 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 112 | .addExternalSymbol(SPSymbol); |
| 113 | auto *MMO = new MachineMemOperand(MachinePointerInfo(), |
| 114 | MachineMemOperand::MOStore, 4, 4); |
| 115 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32) |
| 116 | .addImm(0) |
| 117 | .addReg(OffsetReg) |
| 118 | .addReg(WebAssembly::SP32) |
| 119 | .addMemOperand(MMO); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame^] | 122 | void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, |
| 123 | MachineBasicBlock &MBB) const { |
| 124 | uint64_t StackSize = MF.getFrameInfo()->getStackSize(); |
| 125 | if (!StackSize) |
| 126 | return; |
| 127 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
| 128 | auto &MRI = MF.getRegInfo(); |
| 129 | unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 130 | auto InsertPt = MBB.getFirstTerminator(); |
| 131 | DebugLoc DL; |
| 132 | |
| 133 | if (InsertPt != MBB.end()) { |
| 134 | DL = InsertPt->getDebugLoc(); |
| 135 | } |
| 136 | |
| 137 | // Restore the stack pointer. Without FP its value is just SP32 - stacksize |
| 138 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 139 | .addImm(StackSize); |
| 140 | auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer"); |
| 141 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32) |
| 142 | .addReg(WebAssembly::SP32) |
| 143 | .addReg(OffsetReg); |
| 144 | // Re-use OffsetReg to hold the address of the stacktop |
| 145 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) |
| 146 | .addExternalSymbol(SPSymbol); |
| 147 | auto *MMO = new MachineMemOperand(MachinePointerInfo(), |
| 148 | MachineMemOperand::MOStore, 4, 4); |
| 149 | BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32) |
| 150 | .addImm(0) |
| 151 | .addReg(OffsetReg) |
| 152 | .addReg(WebAssembly::SP32) |
| 153 | .addMemOperand(MMO); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 154 | } |