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       .addImm(2) // p2align
88       .addMemOperand(LoadMMO);
89   // Add/Subtract the frame size
90   unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
91   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
92       .addImm(StackSize);
93   BuildMI(MBB, InsertPt, DL,
94           TII->get(AdjustUp ? WebAssembly::ADD_I32 : WebAssembly::SUB_I32),
95           WebAssembly::SP32)
96       .addReg(SPReg)
97       .addReg(OffsetReg);
98   // The SP32 register now has the new stacktop. Also write it back to memory.
99   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
100       .addExternalSymbol(SPSymbol);
101   auto *MMO = new MachineMemOperand(MachinePointerInfo(),
102                                     MachineMemOperand::MOStore, 4, 4);
103   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
104       .addImm(0)
105       .addReg(OffsetReg)
106       .addImm(2) // p2align
107       .addReg(WebAssembly::SP32)
108       .addMemOperand(MMO);
109 }
110 
111 void WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
112     MachineFunction &MF, MachineBasicBlock &MBB,
113     MachineBasicBlock::iterator I) const {
114   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
115   DebugLoc DL = I->getDebugLoc();
116   unsigned Opc = I->getOpcode();
117   bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode();
118   unsigned Amount = I->getOperand(0).getImm();
119   if (Amount)
120     adjustStackPointer(Amount, IsDestroy, MF, MBB,
121                        TII, I, DL);
122   MBB.erase(I);
123 }
124 
125 void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
126                                             MachineBasicBlock &MBB) const {
127   // TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
128   auto *MFI = MF.getFrameInfo();
129   assert(MFI->getCalleeSavedInfo().empty() &&
130          "WebAssembly should not have callee-saved registers");
131   assert(!hasFP(MF) && "Functions needing frame pointers not yet supported");
132   uint64_t StackSize = MFI->getStackSize();
133   if (!StackSize && (!MFI->adjustsStack() || MFI->getMaxCallFrameSize() == 0))
134     return;
135 
136   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
137 
138   auto InsertPt = MBB.begin();
139   DebugLoc DL;
140 
141   adjustStackPointer(StackSize, false, MF, MBB, TII, InsertPt, DL);
142 }
143 
144 void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
145                                             MachineBasicBlock &MBB) const {
146   uint64_t StackSize = MF.getFrameInfo()->getStackSize();
147   if (!StackSize)
148     return;
149   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
150   auto &MRI = MF.getRegInfo();
151   unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
152   auto InsertPt = MBB.getFirstTerminator();
153   DebugLoc DL;
154 
155   if (InsertPt != MBB.end()) {
156     DL = InsertPt->getDebugLoc();
157   }
158 
159   // Restore the stack pointer. Without FP its value is just SP32 - stacksize
160   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
161       .addImm(StackSize);
162   auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
163   // TODO: Fold this add into the const offset field of the store.
164   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), WebAssembly::SP32)
165       .addReg(WebAssembly::SP32)
166       .addReg(OffsetReg);
167   // Re-use OffsetReg to hold the address of the stacktop
168   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
169       .addExternalSymbol(SPSymbol);
170   auto *MMO = new MachineMemOperand(MachinePointerInfo(),
171                                     MachineMemOperand::MOStore, 4, 4);
172   BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::STORE_I32), WebAssembly::SP32)
173       .addImm(0)
174       .addReg(OffsetReg)
175       .addImm(2) // p2align
176       .addReg(WebAssembly::SP32)
177       .addMemOperand(MMO);
178 }
179