blob: 0eefd57f1f2cf788e8700c288eb1ffe5524db99e [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 {
112 const auto *TII =
113 static_cast<const WebAssemblyInstrInfo*>(MF.getSubtarget().getInstrInfo());
114 DebugLoc DL = I->getDebugLoc();
115 unsigned Opc = I->getOpcode();
116 bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode();
117 unsigned Amount = I->getOperand(0).getImm();
118 if (Amount)
119 adjustStackPointer(Amount, IsDestroy, MF, MBB,
120 TII, I, DL);
121 MBB.erase(I);
122}
123
124void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
125 MachineBasicBlock &MBB) const {
126 // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
127 auto *MFI = MF.getFrameInfo();
128 assert(MFI->getCalleeSavedInfo().empty() &&
129 "WebAssembly should not have callee-saved registers");
130 assert(!hasFP(MF) && "Functions needing frame pointers not yet supported");
131 uint64_t StackSize = MFI->getStackSize();
132 if (!StackSize && (!MFI->adjustsStack() || MFI->getMaxCallFrameSize() == 0))
133 return;
134
135 const auto *TII = MF.getSubtarget().getInstrInfo();
136
137 auto InsertPt = MBB.begin();
138 DebugLoc DL;
139
140 adjustStackPointer(StackSize, false, MF, MBB, TII, InsertPt, DL);
141}
142
Derek Schuff9769deb2015-12-11 23:49:46 +0000143void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
144 MachineBasicBlock &MBB) const {
145 uint64_t StackSize = MF.getFrameInfo()->getStackSize();
146 if (!StackSize)
147 return;
148 const auto *TII = MF.getSubtarget().getInstrInfo();
149 auto &MRI = MF.getRegInfo();
150 unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
151 auto InsertPt = MBB.getFirstTerminator();
152 DebugLoc DL;
153
154 if (InsertPt != MBB.end()) {
155 DL = InsertPt->getDebugLoc();
156 }
157
158 // Restore the stack pointer. Without FP its value is just SP32 - stacksize
159 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
160 .addImm(StackSize);
161 auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
162 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32)
163 .addReg(WebAssembly::SP32)
164 .addReg(OffsetReg);
165 // Re-use OffsetReg to hold the address of the stacktop
166 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
167 .addExternalSymbol(SPSymbol);
168 auto *MMO = new MachineMemOperand(MachinePointerInfo(),
169 MachineMemOperand::MOStore, 4, 4);
170 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
171 .addImm(0)
172 .addReg(OffsetReg)
173 .addReg(WebAssembly::SP32)
174 .addMemOperand(MMO);
Dan Gohman10e730a2015-06-29 23:51:55 +0000175}