LLVM  14.0.0git
WebAssemblyMachineFunctionInfo.h
Go to the documentation of this file.
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 
21 #include "llvm/MC/MCSymbolWasm.h"
22 
23 namespace llvm {
24 
25 struct WasmEHFuncInfo;
26 
27 namespace yaml {
28 struct WebAssemblyFunctionInfo;
29 }
30 
31 /// This class is derived from MachineFunctionInfo and contains private
32 /// WebAssembly-specific information for each MachineFunction.
34  const MachineFunction &MF;
35 
36  std::vector<MVT> Params;
37  std::vector<MVT> Results;
38  std::vector<MVT> Locals;
39 
40  /// A mapping from CodeGen vreg index to WebAssembly register number.
41  std::vector<unsigned> WARegs;
42 
43  /// A mapping from CodeGen vreg index to a boolean value indicating whether
44  /// the given register is considered to be "stackified", meaning it has been
45  /// determined or made to meet the stack requirements:
46  /// - single use (per path)
47  /// - single def (per path)
48  /// - defined and used in LIFO order with other stack registers
49  BitVector VRegStackified;
50 
51  // A virtual register holding the pointer to the vararg buffer for vararg
52  // functions. It is created and set in TLI::LowerFormalArguments and read by
53  // TLI::LowerVASTART
54  unsigned VarargVreg = -1U;
55 
56  // A virtual register holding the base pointer for functions that have
57  // overaligned values on the user stack.
58  unsigned BasePtrVreg = -1U;
59  // A virtual register holding the frame base. This is either FP or SP
60  // after it has been replaced by a vreg
61  unsigned FrameBaseVreg = -1U;
62  // The local holding the frame base. This is either FP or SP
63  // after WebAssemblyExplicitLocals
64  unsigned FrameBaseLocal = -1U;
65 
66  // Function properties.
67  bool CFGStackified = false;
68 
69  // Catchpad unwind destination info for wasm EH.
70  WasmEHFuncInfo *WasmEHInfo = nullptr;
71 
72 public:
74  : MF(MF), WasmEHInfo(MF.getWasmEHFuncInfo()) {}
75  ~WebAssemblyFunctionInfo() override;
76 
77  const MachineFunction &getMachineFunction() const { return MF; }
78 
80 
81  void addParam(MVT VT) { Params.push_back(VT); }
82  const std::vector<MVT> &getParams() const { return Params; }
83 
84  void addResult(MVT VT) { Results.push_back(VT); }
85  const std::vector<MVT> &getResults() const { return Results; }
86 
88  Params.clear();
89  Results.clear();
90  }
91 
92  void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); }
93  void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
94  void addLocal(MVT VT) { Locals.push_back(VT); }
95  const std::vector<MVT> &getLocals() const { return Locals; }
96 
97  unsigned getVarargBufferVreg() const {
98  assert(VarargVreg != -1U && "Vararg vreg hasn't been set");
99  return VarargVreg;
100  }
101  void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; }
102 
103  unsigned getBasePointerVreg() const {
104  assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set");
105  return BasePtrVreg;
106  }
107  void setFrameBaseVreg(unsigned Reg) { FrameBaseVreg = Reg; }
108  unsigned getFrameBaseVreg() const {
109  assert(FrameBaseVreg != -1U && "Frame base vreg hasn't been set");
110  return FrameBaseVreg;
111  }
112  void clearFrameBaseVreg() { FrameBaseVreg = -1U; }
113  // Return true if the frame base physreg has been replaced by a virtual reg.
114  bool isFrameBaseVirtual() const { return FrameBaseVreg != -1U; }
115  void setFrameBaseLocal(unsigned Local) { FrameBaseLocal = Local; }
116  unsigned getFrameBaseLocal() const {
117  assert(FrameBaseLocal != -1U && "Frame base local hasn't been set");
118  return FrameBaseLocal;
119  }
120  void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; }
121 
122  static const unsigned UnusedReg = -1u;
123 
124  void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg) {
125  assert(MRI.getUniqueVRegDef(VReg));
126  auto I = Register::virtReg2Index(VReg);
127  if (I >= VRegStackified.size())
128  VRegStackified.resize(I + 1);
129  VRegStackified.set(I);
130  }
131  void unstackifyVReg(unsigned VReg) {
132  auto I = Register::virtReg2Index(VReg);
133  if (I < VRegStackified.size())
134  VRegStackified.reset(I);
135  }
136  bool isVRegStackified(unsigned VReg) const {
137  auto I = Register::virtReg2Index(VReg);
138  if (I >= VRegStackified.size())
139  return false;
140  return VRegStackified.test(I);
141  }
142 
144  void setWAReg(unsigned VReg, unsigned WAReg) {
145  assert(WAReg != UnusedReg);
146  auto I = Register::virtReg2Index(VReg);
147  assert(I < WARegs.size());
148  WARegs[I] = WAReg;
149  }
150  unsigned getWAReg(unsigned VReg) const {
151  auto I = Register::virtReg2Index(VReg);
152  assert(I < WARegs.size());
153  return WARegs[I];
154  }
155 
156  // For a given stackified WAReg, return the id number to print with push/pop.
157  static unsigned getWARegStackId(unsigned Reg) {
158  assert(Reg & INT32_MIN);
159  return Reg & INT32_MAX;
160  }
161 
162  bool isCFGStackified() const { return CFGStackified; }
163  void setCFGStackified(bool Value = true) { CFGStackified = Value; }
164 
165  WasmEHFuncInfo *getWasmEHFuncInfo() const { return WasmEHInfo; }
166  void setWasmEHFuncInfo(WasmEHFuncInfo *Info) { WasmEHInfo = Info; }
167 };
168 
169 void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
170  SmallVectorImpl<MVT> &ValueVTs);
171 
172 // Compute the signature for a given FunctionType (Ty). Note that it's not the
173 // signature for ContextFunc (ContextFunc is just used to get varous context)
174 void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc,
175  const Function &ContextFunc, const TargetMachine &TM,
176  SmallVectorImpl<MVT> &Params,
177  SmallVectorImpl<MVT> &Results);
178 
179 void valTypesFromMVTs(const ArrayRef<MVT> &In,
180  SmallVectorImpl<wasm::ValType> &Out);
181 
182 std::unique_ptr<wasm::WasmSignature>
183 signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
184  const SmallVectorImpl<MVT> &Params);
185 
186 namespace yaml {
187 
189 
191  std::vector<FlowStringValue> Params;
192  std::vector<FlowStringValue> Results;
193  bool CFGStackified = false;
194  // The same as WasmEHFuncInfo's SrcToUnwindDest, but stored in the mapping of
195  // BB numbers
197 
198  WebAssemblyFunctionInfo() = default;
200 
201  void mappingImpl(yaml::IO &YamlIO) override;
202  ~WebAssemblyFunctionInfo() = default;
203 };
204 
206  static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI) {
207  YamlIO.mapOptional("params", MFI.Params, std::vector<FlowStringValue>());
208  YamlIO.mapOptional("results", MFI.Results, std::vector<FlowStringValue>());
209  YamlIO.mapOptional("isCFGStackified", MFI.CFGStackified, false);
210  YamlIO.mapOptional("wasmEHFuncInfo", MFI.SrcToUnwindDest);
211  }
212 };
213 
214 template <> struct CustomMappingTraits<BBNumberMap> {
215  static void inputOne(IO &YamlIO, StringRef Key,
216  BBNumberMap &SrcToUnwindDest) {
217  YamlIO.mapRequired(Key.str().c_str(),
218  SrcToUnwindDest[std::atoi(Key.str().c_str())]);
219  }
220 
221  static void output(IO &YamlIO, BBNumberMap &SrcToUnwindDest) {
222  for (auto KV : SrcToUnwindDest)
223  YamlIO.mapRequired(std::to_string(KV.first).c_str(), KV.second);
224  }
225 };
226 
227 } // end namespace yaml
228 
229 } // end namespace llvm
230 
231 #endif
i
i
Definition: README.txt:29
llvm::WasmEHFuncInfo
Definition: WasmEHFuncInfo.h:32
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::WebAssemblyFunctionInfo::getLocals
const std::vector< MVT > & getLocals() const
Definition: WebAssemblyMachineFunctionInfo.h:95
FunctionType
Definition: ItaniumDemangle.h:643
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::BitVector::set
BitVector & set()
Definition: BitVector.h:343
llvm::yaml::MachineFunctionInfo
Targets should override this in a way that mirrors the implementation of llvm::MachineFunctionInfo.
Definition: MIRYamlMapping.h:673
llvm::WebAssemblyFunctionInfo::addResult
void addResult(MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:84
llvm::WebAssemblyFunctionInfo::setVarargBufferVreg
void setVarargBufferVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:101
llvm::signatureFromMVTs
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
Definition: WebAssemblyMachineFunctionInfo.cpp:100
llvm::yaml::CustomMappingTraits< BBNumberMap >::output
static void output(IO &YamlIO, BBNumberMap &SrcToUnwindDest)
Definition: WebAssemblyMachineFunctionInfo.h:221
llvm::WebAssemblyFunctionInfo::setCFGStackified
void setCFGStackified(bool Value=true)
Definition: WebAssemblyMachineFunctionInfo.h:163
llvm::WebAssemblyFunctionInfo::getMachineFunction
const MachineFunction & getMachineFunction() const
Definition: WebAssemblyMachineFunctionInfo.h:77
llvm::WebAssemblyFunctionInfo::setFrameBaseVreg
void setFrameBaseVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:107
llvm::yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo()=default
llvm::WebAssemblyFunctionInfo::addParam
void addParam(MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:81
llvm::WebAssemblyFunctionInfo::setBasePointerVreg
void setBasePointerVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:120
llvm::WebAssemblyFunctionInfo::unstackifyVReg
void unstackifyVReg(unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:131
llvm::BitVector::resize
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:333
YamlIO
IO & YamlIO
Definition: ELFYAML.cpp:1171
Results
Function Alias Analysis Results
Definition: AliasAnalysis.cpp:853
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:52
llvm::yaml::WebAssemblyFunctionInfo::Params
std::vector< FlowStringValue > Params
Definition: WebAssemblyMachineFunctionInfo.h:191
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::yaml::WebAssemblyFunctionInfo::mappingImpl
void mappingImpl(yaml::IO &YamlIO) override
Definition: WebAssemblyMachineFunctionInfo.cpp:137
MachineRegisterInfo.h
llvm::yaml::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo()=default
llvm::WebAssemblyFunctionInfo::getBasePointerVreg
unsigned getBasePointerVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:103
MIRYamlMapping.h
llvm::WebAssemblyFunctionInfo::getVarargBufferVreg
unsigned getVarargBufferVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:97
llvm::WebAssemblyFunctionInfo::clearFrameBaseVreg
void clearFrameBaseVreg()
Definition: WebAssemblyMachineFunctionInfo.h:112
MCSymbolWasm.h
llvm::BitVector::size
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:151
llvm::WebAssemblyFunctionInfo::clearParamsAndResults
void clearParamsAndResults()
Definition: WebAssemblyMachineFunctionInfo.h:87
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::WebAssemblyFunctionInfo::isCFGStackified
bool isCFGStackified() const
Definition: WebAssemblyMachineFunctionInfo.h:162
llvm::yaml::WebAssemblyFunctionInfo::CFGStackified
bool CFGStackified
Definition: WebAssemblyMachineFunctionInfo.h:193
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::BitVector
Definition: BitVector.h:74
llvm::yaml::MappingTraits< WebAssemblyFunctionInfo >::mapping
static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI)
Definition: WebAssemblyMachineFunctionInfo.h:206
WebAssemblyMCTargetDesc.h
llvm::yaml::WebAssemblyFunctionInfo::SrcToUnwindDest
BBNumberMap SrcToUnwindDest
Definition: WebAssemblyMachineFunctionInfo.h:196
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::WebAssemblyFunctionInfo::stackifyVReg
void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:124
llvm::WebAssemblyFunctionInfo::UnusedReg
static const unsigned UnusedReg
Definition: WebAssemblyMachineFunctionInfo.h:122
llvm::DenseMap< int, int >
llvm::valTypesFromMVTs
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
Definition: WebAssemblyMachineFunctionInfo.cpp:93
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::WebAssemblyFunctionInfo::setWasmEHFuncInfo
void setWasmEHFuncInfo(WasmEHFuncInfo *Info)
Definition: WebAssemblyMachineFunctionInfo.h:166
llvm::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo(MachineFunction &MF)
Definition: WebAssemblyMachineFunctionInfo.h:73
llvm::WebAssemblyFunctionInfo::getWAReg
unsigned getWAReg(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:150
llvm::WebAssemblyFunctionInfo::isVRegStackified
bool isVRegStackified(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:136
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WebAssemblyFunctionInfo::initializeBaseYamlFields
void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI)
Definition: WebAssemblyMachineFunctionInfo.cpp:141
llvm::WebAssemblyFunctionInfo
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
Definition: WebAssemblyMachineFunctionInfo.h:33
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:31
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::WebAssemblyFunctionInfo::getResults
const std::vector< MVT > & getResults() const
Definition: WebAssemblyMachineFunctionInfo.h:85
llvm::WebAssemblyFunctionInfo::setFrameBaseLocal
void setFrameBaseLocal(unsigned Local)
Definition: WebAssemblyMachineFunctionInfo.h:115
llvm::computeLegalValueVTs
void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty, SmallVectorImpl< MVT > &ValueVTs)
Definition: WebAssemblyMachineFunctionInfo.cpp:33
llvm::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo() override
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::WebAssemblyFunctionInfo::setWAReg
void setWAReg(unsigned VReg, unsigned WAReg)
Definition: WebAssemblyMachineFunctionInfo.h:144
llvm::WebAssemblyFunctionInfo::setNumLocals
void setNumLocals(size_t NumLocals)
Definition: WebAssemblyMachineFunctionInfo.h:92
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::WebAssemblyFunctionInfo::getWasmEHFuncInfo
WasmEHFuncInfo * getWasmEHFuncInfo() const
Definition: WebAssemblyMachineFunctionInfo.h:165
llvm::computeSignatureVTs
void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc, const Function &ContextFunc, const TargetMachine &TM, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results)
Definition: WebAssemblyMachineFunctionInfo.cpp:49
llvm::BitVector::test
bool test(unsigned Idx) const
Definition: BitVector.h:447
llvm::WebAssemblyFunctionInfo::initWARegs
void initWARegs(MachineRegisterInfo &MRI)
Definition: WebAssemblyMachineFunctionInfo.cpp:27
llvm::MVT::i32
@ i32
Definition: MachineValueType.h:46
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::WebAssemblyFunctionInfo::isFrameBaseVirtual
bool isFrameBaseVirtual() const
Definition: WebAssemblyMachineFunctionInfo.h:114
llvm::yaml::CustomMappingTraits< BBNumberMap >::inputOne
static void inputOne(IO &YamlIO, StringRef Key, BBNumberMap &SrcToUnwindDest)
Definition: WebAssemblyMachineFunctionInfo.h:215
llvm::yaml::WebAssemblyFunctionInfo::Results
std::vector< FlowStringValue > Results
Definition: WebAssemblyMachineFunctionInfo.h:192
llvm::BitVector::reset
BitVector & reset()
Definition: BitVector.h:384
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:63
llvm::WebAssemblyFunctionInfo::getParams
const std::vector< MVT > & getParams() const
Definition: WebAssemblyMachineFunctionInfo.h:82
llvm::WebAssemblyFunctionInfo::setLocal
void setLocal(size_t i, MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:93
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::WebAssemblyFunctionInfo::getFrameBaseVreg
unsigned getFrameBaseVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:108
llvm::Register::virtReg2Index
static unsigned virtReg2Index(Register Reg)
Convert a virtual register number to a 0-based index.
Definition: Register.h:77
llvm::WebAssemblyFunctionInfo::addLocal
void addLocal(MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:94
llvm::MachineFunctionInfo
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
Definition: MachineFunction.h:95
llvm::WebAssemblyFunctionInfo::getFrameBaseLocal
unsigned getFrameBaseLocal() const
Definition: WebAssemblyMachineFunctionInfo.h:116
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::yaml::WebAssemblyFunctionInfo
Definition: WebAssemblyMachineFunctionInfo.h:190
llvm::WebAssemblyFunctionInfo::getWARegStackId
static unsigned getWARegStackId(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:157