blob: 43d9b53adafff9fe6f0bf3149e4ae5959297f21d [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- 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"
33using namespace llvm;
34
JF Bastien03855df2015-07-01 23:41:25 +000035#define DEBUG_TYPE "wasm-frame-info"
Dan Gohman10e730a2015-06-29 23:51:55 +000036
37// TODO: Implement a red zone?
Derek Schuff9769deb2015-12-11 23:49:46 +000038// 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 Gohman10e730a2015-06-29 23:51:55 +000042
43/// Return true if the specified function should have a dedicated frame pointer
44/// register.
45bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
JF Bastienb9073fb2015-07-22 21:28:15 +000046 const MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohmane419a7c2015-08-24 16:46:31 +000047 const auto *RegInfo =
48 MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +000049 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() ||
50 MFI->hasStackMap() || MFI->hasPatchPoint() ||
51 RegInfo->needsStackRealignment(MF);
Dan Gohman10e730a2015-06-29 23:51:55 +000052}
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.
59bool WebAssemblyFrameLowering::hasReservedCallFrame(
60 const MachineFunction &MF) const {
61 return !MF.getFrameInfo()->hasVarSizedObjects();
62}
63
Dan Gohman10e730a2015-06-29 23:51:55 +000064
Derek Schuff8bb5f292015-12-16 23:21:30 +000065/// Adjust the stack pointer by a constant amount.
66static 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) {
Derek Schuff9769deb2015-12-11 23:49:46 +000073 auto &MRI = MF.getRegInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +000074 unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
75 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
76 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPReg)
77 .addExternalSymbol(SPSymbol);
78 // This MachinePointerInfo should reference __stack_pointer as well but
79 // doesn't because MachinePointerInfo() takes a GV which we don't have for
80 // __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry
81 // is appropriate instead. (likewise for EmitEpologue below)
82 auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(),
83 MachineMemOperand::MOLoad, 4, 4);
84 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32), SPReg)
85 .addImm(0)
86 .addReg(SPReg)
87 .addMemOperand(LoadMMO);
Derek Schuff8bb5f292015-12-16 23:21:30 +000088 // Add/Subtract the frame size
Derek Schuff9769deb2015-12-11 23:49:46 +000089 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
90 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
91 .addImm(StackSize);
Derek Schuff8bb5f292015-12-16 23:21:30 +000092 BuildMI(MBB, InsertPt, DL,
93 TII->get(AdjustUp ? WebAssembly::ADD_I32 : WebAssembly::SUB_I32),
94 WebAssembly::SP32)
Derek Schuff9769deb2015-12-11 23:49:46 +000095 .addReg(SPReg)
96 .addReg(OffsetReg);
97 // The SP32 register now has the new stacktop. Also write it back to memory.
98 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
99 .addExternalSymbol(SPSymbol);
100 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
101 MachineMemOperand::MOStore, 4, 4);
102 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
103 .addImm(0)
104 .addReg(OffsetReg)
105 .addReg(WebAssembly::SP32)
106 .addMemOperand(MMO);
Dan Gohman10e730a2015-06-29 23:51:55 +0000107}
108
Derek Schuff8bb5f292015-12-16 23:21:30 +0000109void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
110 MachineFunction &MF, MachineBasicBlock &MBB,
111 MachineBasicBlock::iterator I) const {
Dan Gohman31966502016-01-19 14:53:19 +0000112 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000113 DebugLoc DL = I->getDebugLoc();
114 unsigned Opc = I->getOpcode();
115 bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode();
116 unsigned Amount = I->getOperand(0).getImm();
117 if (Amount)
118 adjustStackPointer(Amount, IsDestroy, MF, MBB,
119 TII, I, DL);
120 MBB.erase(I);
121}
122
123void 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");
129 assert(!hasFP(MF) && "Functions needing frame pointers not yet supported");
130 uint64_t StackSize = MFI->getStackSize();
131 if (!StackSize && (!MFI->adjustsStack() || MFI->getMaxCallFrameSize() == 0))
132 return;
133
Dan Gohman31966502016-01-19 14:53:19 +0000134 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff8bb5f292015-12-16 23:21:30 +0000135
136 auto InsertPt = MBB.begin();
137 DebugLoc DL;
138
139 adjustStackPointer(StackSize, false, MF, MBB, TII, InsertPt, DL);
140}
141
Derek Schuff9769deb2015-12-11 23:49:46 +0000142void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
143 MachineBasicBlock &MBB) const {
144 uint64_t StackSize = MF.getFrameInfo()->getStackSize();
145 if (!StackSize)
146 return;
Dan Gohman31966502016-01-19 14:53:19 +0000147 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Derek Schuff9769deb2015-12-11 23:49:46 +0000148 auto &MRI = MF.getRegInfo();
149 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
150 auto InsertPt = MBB.getFirstTerminator();
151 DebugLoc DL;
152
153 if (InsertPt != MBB.end()) {
154 DL = InsertPt->getDebugLoc();
155 }
156
157 // Restore the stack pointer. Without FP its value is just SP32 - stacksize
158 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
159 .addImm(StackSize);
160 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
161 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32)
162 .addReg(WebAssembly::SP32)
163 .addReg(OffsetReg);
164 // Re-use OffsetReg to hold the address of the stacktop
165 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
166 .addExternalSymbol(SPSymbol);
167 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
168 MachineMemOperand::MOStore, 4, 4);
169 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
170 .addImm(0)
171 .addReg(OffsetReg)
172 .addReg(WebAssembly::SP32)
173 .addMemOperand(MMO);
Dan Gohman10e730a2015-06-29 23:51:55 +0000174}