1// WebAssemblyMachineFunctionInfo.h-WebAssembly machine function info-*- C++ -*-
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file declares WebAssembly-specific per-machine-function
11/// information.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H
16#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H
17
18#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
19#include "llvm/CodeGen/MIRYamlMapping.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/MC/MCSymbolWasm.h"
22
23namespace llvm {
24class WebAssemblyTargetLowering;
25
26namespace yaml {
27struct WebAssemblyFunctionInfo;
28}
29
30/// This class is derived from MachineFunctionInfo and contains private
31/// WebAssembly-specific information for each MachineFunction.
32class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
33 std::vector<MVT> Params;
34 std::vector<MVT> Results;
35 std::vector<MVT> Locals;
36
37 /// A mapping from CodeGen vreg index to WebAssembly register number.
38 std::vector<unsigned> WARegs;
39
40 /// A mapping from CodeGen vreg index to a boolean value indicating whether
41 /// the given register is considered to be "stackified", meaning it has been
42 /// determined or made to meet the stack requirements:
43 /// - single use (per path)
44 /// - single def (per path)
45 /// - defined and used in LIFO order with other stack registers
46 BitVector VRegStackified;
47
48 // A virtual register holding the pointer to the vararg buffer for vararg
49 // functions. It is created and set in TLI::LowerFormalArguments and read by
50 // TLI::LowerVASTART
51 unsigned VarargVreg = -1U;
52
53 // A virtual register holding the base pointer for functions that have
54 // overaligned values on the user stack.
55 unsigned BasePtrVreg = -1U;
56 // A virtual register holding the frame base. This is either FP or SP
57 // after it has been replaced by a vreg
58 unsigned FrameBaseVreg = -1U;
59 // The local holding the frame base. This is either FP or SP
60 // after WebAssemblyExplicitLocals
61 unsigned FrameBaseLocal = -1U;
62
63 // Function properties.
64 bool CFGStackified = false;
65
66public:
67 explicit WebAssemblyFunctionInfo(const Function &F,
68 const TargetSubtargetInfo *STI) {}
69 ~WebAssemblyFunctionInfo() override;
70
71 MachineFunctionInfo *
72 clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
73 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
74 const override;
75
76 void initializeBaseYamlFields(MachineFunction &MF,
77 const yaml::WebAssemblyFunctionInfo &YamlMFI);
78
79 void addParam(MVT VT) { Params.push_back(x: VT); }
80 const std::vector<MVT> &getParams() const { return Params; }
81
82 void addResult(MVT VT) { Results.push_back(x: VT); }
83 const std::vector<MVT> &getResults() const { return Results; }
84
85 void clearParamsAndResults() {
86 Params.clear();
87 Results.clear();
88 }
89
90 void setNumLocals(size_t NumLocals) { Locals.resize(new_size: NumLocals, x: MVT::i32); }
91 void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
92 void addLocal(MVT VT) { Locals.push_back(x: VT); }
93 const std::vector<MVT> &getLocals() const { return Locals; }
94
95 unsigned getVarargBufferVreg() const {
96 assert(VarargVreg != -1U && "Vararg vreg hasn't been set");
97 return VarargVreg;
98 }
99 void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; }
100
101 unsigned getBasePointerVreg() const {
102 assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set");
103 return BasePtrVreg;
104 }
105 void setFrameBaseVreg(unsigned Reg) { FrameBaseVreg = Reg; }
106 unsigned getFrameBaseVreg() const {
107 assert(FrameBaseVreg != -1U && "Frame base vreg hasn't been set");
108 return FrameBaseVreg;
109 }
110 void clearFrameBaseVreg() { FrameBaseVreg = -1U; }
111 // Return true if the frame base physreg has been replaced by a virtual reg.
112 bool isFrameBaseVirtual() const { return FrameBaseVreg != -1U; }
113 void setFrameBaseLocal(unsigned Local) { FrameBaseLocal = Local; }
114 unsigned getFrameBaseLocal() const {
115 assert(FrameBaseLocal != -1U && "Frame base local hasn't been set");
116 return FrameBaseLocal;
117 }
118 void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; }
119
120 void stackifyVReg(MachineRegisterInfo &MRI, Register VReg) {
121 assert(MRI.getUniqueVRegDef(VReg));
122 auto I = VReg.virtRegIndex();
123 if (I >= VRegStackified.size())
124 VRegStackified.resize(N: I + 1);
125 VRegStackified.set(I);
126 }
127 void unstackifyVReg(Register VReg) {
128 auto I = VReg.virtRegIndex();
129 if (I < VRegStackified.size())
130 VRegStackified.reset(Idx: I);
131 }
132 bool isVRegStackified(Register VReg) const {
133 auto I = VReg.virtRegIndex();
134 if (I >= VRegStackified.size())
135 return false;
136 return VRegStackified.test(Idx: I);
137 }
138
139 void initWARegs(MachineRegisterInfo &MRI);
140 void setWAReg(Register VReg, unsigned WAReg) {
141 assert(WAReg != WebAssembly::UnusedReg);
142 auto I = VReg.virtRegIndex();
143 assert(I < WARegs.size());
144 WARegs[I] = WAReg;
145 }
146 unsigned getWAReg(Register VReg) const {
147 auto I = VReg.virtRegIndex();
148 assert(I < WARegs.size());
149 return WARegs[I];
150 }
151
152 bool isCFGStackified() const { return CFGStackified; }
153 void setCFGStackified(bool Value = true) { CFGStackified = Value; }
154};
155
156void computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
157 LLVMContext &Ctx, const DataLayout &DL, Type *Ty,
158 SmallVectorImpl<MVT> &ValueVTs);
159
160void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
161 SmallVectorImpl<MVT> &ValueVTs);
162
163// Compute the signature for a given FunctionType (Ty). Note that it's not the
164// signature for ContextFunc (ContextFunc is just used to get varous context)
165void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc,
166 const Function &ContextFunc, const TargetMachine &TM,
167 SmallVectorImpl<MVT> &Params,
168 SmallVectorImpl<MVT> &Results);
169
170void valTypesFromMVTs(ArrayRef<MVT> In, SmallVectorImpl<wasm::ValType> &Out);
171
172wasm::WasmSignature *signatureFromMVTs(MCContext &Ctx,
173 const SmallVectorImpl<MVT> &Results,
174 const SmallVectorImpl<MVT> &Params);
175
176namespace yaml {
177
178using BBNumberMap = DenseMap<int, int>;
179
180struct WebAssemblyFunctionInfo final : public yaml::MachineFunctionInfo {
181 std::vector<FlowStringValue> Params;
182 std::vector<FlowStringValue> Results;
183 bool CFGStackified = false;
184
185 WebAssemblyFunctionInfo() = default;
186 WebAssemblyFunctionInfo(const llvm::MachineFunction &MF,
187 const llvm::WebAssemblyFunctionInfo &MFI);
188
189 void mappingImpl(yaml::IO &YamlIO) override;
190 ~WebAssemblyFunctionInfo() override = default;
191};
192
193template <> struct MappingTraits<WebAssemblyFunctionInfo> {
194 static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI) {
195 YamlIO.mapOptional(Key: "params", Val&: MFI.Params, Default: std::vector<FlowStringValue>());
196 YamlIO.mapOptional(Key: "results", Val&: MFI.Results, Default: std::vector<FlowStringValue>());
197 YamlIO.mapOptional(Key: "isCFGStackified", Val&: MFI.CFGStackified, Default: false);
198 }
199};
200
201} // end namespace yaml
202
203} // end namespace llvm
204
205#endif
206