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 35 #define DEBUG_TYPE "wasm-frame-info" 36 37 // TODO: Implement a red zone? 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 42 43 /// Return true if the specified function should have a dedicated frame pointer 44 /// register. 45 bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const { 46 const MachineFrameInfo *MFI = MF.getFrameInfo(); 47 const auto *RegInfo = 48 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); 49 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() || 50 MFI->hasStackMap() || MFI->hasPatchPoint() || 51 RegInfo->needsStackRealignment(MF); 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 65 /// Adjust the stack pointer by a constant amount. 66 static void adjustStackPointer(unsigned StackSize, 67 bool AdjustUp, 68 MachineFunction& MF, 69 MachineBasicBlock& MBB, 70 const TargetInstrInfo* TII, 71 MachineBasicBlock::iterator InsertPt, 72 const DebugLoc& DL) { 73 assert((StackSize || !AdjustUp) && "Adjusting up by 0"); 74 auto &MRI = MF.getRegInfo(); 75 unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 76 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer"); 77 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPReg) 78 .addExternalSymbol(SPSymbol); 79 // This MachinePointerInfo should reference __stack_pointer as well but 80 // doesn't because MachinePointerInfo() takes a GV which we don't have for 81 // __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry 82 // is appropriate instead. (likewise for EmitEpologue below) 83 auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(), 84 MachineMemOperand::MOLoad, 4, 4); 85 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), SPReg) 86 .addImm(0) // offset 87 .addReg(SPReg) // addr 88 .addImm(2) // p2align 89 .addMemOperand(LoadMMO); 90 // Add/Subtract the frame size 91 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 92 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 93 .addImm(StackSize); 94 BuildMI(MBB, InsertPt, DL, 95 TII->get(AdjustUp ? WebAssembly::ADD_I32 : WebAssembly::SUB_I32), 96 WebAssembly::SP32) 97 .addReg(SPReg) 98 .addReg(OffsetReg); 99 // The SP32 register now has the new stacktop. Also write it back to memory. 100 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 101 .addExternalSymbol(SPSymbol); 102 auto *MMO = new MachineMemOperand(MachinePointerInfo(), 103 MachineMemOperand::MOStore, 4, 4); 104 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32) 105 .addImm(0) 106 .addReg(OffsetReg) 107 .addImm(2) // p2align 108 .addReg(WebAssembly::SP32) 109 .addMemOperand(MMO); 110 } 111 112 void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr( 113 MachineFunction &MF, MachineBasicBlock &MBB, 114 MachineBasicBlock::iterator I) const { 115 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 116 DebugLoc DL = I->getDebugLoc(); 117 unsigned Opc = I->getOpcode(); 118 bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode(); 119 unsigned Amount = I->getOperand(0).getImm(); 120 // TODO(dschuff): After we switch varargs to passing an explicit pointer 121 // rather than using an implicit call frame, assert here that Amount is 0 122 // and remove adjustStackPointer altogether. 123 if (Amount) 124 adjustStackPointer(Amount, IsDestroy, MF, MBB, 125 TII, I, DL); 126 MBB.erase(I); 127 } 128 129 void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, 130 MachineBasicBlock &MBB) const { 131 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions 132 auto *MFI = MF.getFrameInfo(); 133 assert(MFI->getCalleeSavedInfo().empty() && 134 "WebAssembly should not have callee-saved registers"); 135 136 uint64_t StackSize = MFI->getStackSize(); 137 if (!StackSize && !MFI->adjustsStack()) 138 return; 139 140 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 141 auto &MRI = MF.getRegInfo(); 142 143 auto InsertPt = MBB.begin(); 144 DebugLoc DL; 145 146 unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 147 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer"); 148 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPReg) 149 .addExternalSymbol(SPSymbol); 150 // This MachinePointerInfo should reference __stack_pointer as well but 151 // doesn't because MachinePointerInfo() takes a GV which we don't have for 152 // __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry 153 // is appropriate instead. (likewise for EmitEpologue below) 154 auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(), 155 MachineMemOperand::MOLoad, 4, 4); 156 // Load the SP value. 157 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), 158 StackSize ? SPReg : (unsigned)WebAssembly::SP32) 159 .addImm(0) // offset 160 .addReg(SPReg) // addr 161 .addImm(2) // p2align 162 .addMemOperand(LoadMMO); 163 164 unsigned OffsetReg = 0; 165 if (StackSize) { 166 // Subtract the frame size 167 OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 168 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 169 .addImm(StackSize); 170 BuildMI(MBB, InsertPt, DL, 171 TII->get(WebAssembly::SUB_I32), 172 WebAssembly::SP32) 173 .addReg(SPReg) 174 .addReg(OffsetReg); 175 } 176 if (hasFP(MF)) { 177 // Unlike most conventional targets (where FP points to the saved FP), 178 // FP points to the bottom of the fixed-size locals, so we can use positive 179 // offsets in load/store instructions. 180 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY_LOCAL_I32), 181 WebAssembly::FP32) 182 .addReg(WebAssembly::SP32); 183 } 184 if (StackSize) { 185 assert(OffsetReg); 186 // The SP32 register now has the new stacktop. Also write it back to memory. 187 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 188 .addExternalSymbol(SPSymbol); 189 auto *MMO = new MachineMemOperand(MachinePointerInfo(), 190 MachineMemOperand::MOStore, 4, 4); 191 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32) 192 .addImm(0) 193 .addReg(OffsetReg) 194 .addImm(2) // p2align 195 .addReg(WebAssembly::SP32) 196 .addMemOperand(MMO); 197 } 198 } 199 200 void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, 201 MachineBasicBlock &MBB) const { 202 auto *MFI = MF.getFrameInfo(); 203 uint64_t StackSize = MFI->getStackSize(); 204 if (!StackSize && !MFI->adjustsStack()) 205 return; 206 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 207 auto &MRI = MF.getRegInfo(); 208 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); 209 auto InsertPt = MBB.getFirstTerminator(); 210 DebugLoc DL; 211 212 if (InsertPt != MBB.end()) { 213 DL = InsertPt->getDebugLoc(); 214 } 215 216 // Restore the stack pointer. If we had fixed-size locals, add the offset 217 // subtracted in the prolog. 218 if (StackSize) { 219 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 220 .addImm(StackSize); 221 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32) 222 .addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32) 223 .addReg(OffsetReg); 224 } 225 226 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer"); 227 // Re-use OffsetReg to hold the address of the stacktop 228 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg) 229 .addExternalSymbol(SPSymbol); 230 auto *MMO = new MachineMemOperand(MachinePointerInfo(), 231 MachineMemOperand::MOStore, 4, 4); 232 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32) 233 .addImm(0) 234 .addReg(OffsetReg) 235 .addImm(2) // p2align 236 .addReg((!StackSize && hasFP(MF)) ? WebAssembly::FP32 : WebAssembly::SP32) 237 .addMemOperand(MMO); 238 } 239