[WebAssembly] Implement eliminateCallFramePseudo
[oota-llvm.git] / lib / Target / WebAssembly / WebAssemblyFrameLowering.cpp
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   auto &MRI = MF.getRegInfo();
74   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);
88   // Add/Subtract the frame size
89   unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
90   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
91       .addImm(StackSize);
92   BuildMI(MBB, InsertPt, DL,
93           TII->get(AdjustUp ? WebAssembly::ADD_I32 : WebAssembly::SUB_I32),
94           WebAssembly::SP32)
95       .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);
107 }
108
109 void 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
124 void 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
143 void 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);
175 }