LLVM  15.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 
80  const override;
81 
82  const MachineFunction &getMachineFunction() const { return *MF; }
83 
85 
86  void addParam(MVT VT) { Params.push_back(VT); }
87  const std::vector<MVT> &getParams() const { return Params; }
88 
89  void addResult(MVT VT) { Results.push_back(VT); }
90  const std::vector<MVT> &getResults() const { return Results; }
91 
93  Params.clear();
94  Results.clear();
95  }
96 
97  void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); }
98  void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
99  void addLocal(MVT VT) { Locals.push_back(VT); }
100  const std::vector<MVT> &getLocals() const { return Locals; }
101 
102  unsigned getVarargBufferVreg() const {
103  assert(VarargVreg != -1U && "Vararg vreg hasn't been set");
104  return VarargVreg;
105  }
106  void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; }
107 
108  unsigned getBasePointerVreg() const {
109  assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set");
110  return BasePtrVreg;
111  }
112  void setFrameBaseVreg(unsigned Reg) { FrameBaseVreg = Reg; }
113  unsigned getFrameBaseVreg() const {
114  assert(FrameBaseVreg != -1U && "Frame base vreg hasn't been set");
115  return FrameBaseVreg;
116  }
117  void clearFrameBaseVreg() { FrameBaseVreg = -1U; }
118  // Return true if the frame base physreg has been replaced by a virtual reg.
119  bool isFrameBaseVirtual() const { return FrameBaseVreg != -1U; }
120  void setFrameBaseLocal(unsigned Local) { FrameBaseLocal = Local; }
121  unsigned getFrameBaseLocal() const {
122  assert(FrameBaseLocal != -1U && "Frame base local hasn't been set");
123  return FrameBaseLocal;
124  }
125  void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; }
126 
127  static const unsigned UnusedReg = -1u;
128 
129  void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg) {
130  assert(MRI.getUniqueVRegDef(VReg));
131  auto I = Register::virtReg2Index(VReg);
132  if (I >= VRegStackified.size())
133  VRegStackified.resize(I + 1);
134  VRegStackified.set(I);
135  }
136  void unstackifyVReg(unsigned VReg) {
137  auto I = Register::virtReg2Index(VReg);
138  if (I < VRegStackified.size())
139  VRegStackified.reset(I);
140  }
141  bool isVRegStackified(unsigned VReg) const {
142  auto I = Register::virtReg2Index(VReg);
143  if (I >= VRegStackified.size())
144  return false;
145  return VRegStackified.test(I);
146  }
147 
149  void setWAReg(unsigned VReg, unsigned WAReg) {
150  assert(WAReg != UnusedReg);
151  auto I = Register::virtReg2Index(VReg);
152  assert(I < WARegs.size());
153  WARegs[I] = WAReg;
154  }
155  unsigned getWAReg(unsigned VReg) const {
156  auto I = Register::virtReg2Index(VReg);
157  assert(I < WARegs.size());
158  return WARegs[I];
159  }
160 
161  // For a given stackified WAReg, return the id number to print with push/pop.
162  static unsigned getWARegStackId(unsigned Reg) {
163  assert(Reg & INT32_MIN);
164  return Reg & INT32_MAX;
165  }
166 
167  bool isCFGStackified() const { return CFGStackified; }
168  void setCFGStackified(bool Value = true) { CFGStackified = Value; }
169 
170  WasmEHFuncInfo *getWasmEHFuncInfo() const { return WasmEHInfo; }
171  void setWasmEHFuncInfo(WasmEHFuncInfo *Info) { WasmEHInfo = Info; }
172 };
173 
174 void computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
175  LLVMContext &Ctx, const DataLayout &DL, Type *Ty,
176  SmallVectorImpl<MVT> &ValueVTs);
177 
178 void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
179  SmallVectorImpl<MVT> &ValueVTs);
180 
181 // Compute the signature for a given FunctionType (Ty). Note that it's not the
182 // signature for ContextFunc (ContextFunc is just used to get varous context)
183 void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc,
184  const Function &ContextFunc, const TargetMachine &TM,
185  SmallVectorImpl<MVT> &Params,
186  SmallVectorImpl<MVT> &Results);
187 
188 void valTypesFromMVTs(const ArrayRef<MVT> &In,
189  SmallVectorImpl<wasm::ValType> &Out);
190 
191 std::unique_ptr<wasm::WasmSignature>
192 signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
193  const SmallVectorImpl<MVT> &Params);
194 
195 namespace yaml {
196 
198 
200  std::vector<FlowStringValue> Params;
201  std::vector<FlowStringValue> Results;
202  bool CFGStackified = false;
203  // The same as WasmEHFuncInfo's SrcToUnwindDest, but stored in the mapping of
204  // BB numbers
206 
207  WebAssemblyFunctionInfo() = default;
209 
210  void mappingImpl(yaml::IO &YamlIO) override;
211  ~WebAssemblyFunctionInfo() = default;
212 };
213 
215  static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI) {
216  YamlIO.mapOptional("params", MFI.Params, std::vector<FlowStringValue>());
217  YamlIO.mapOptional("results", MFI.Results, std::vector<FlowStringValue>());
218  YamlIO.mapOptional("isCFGStackified", MFI.CFGStackified, false);
219  YamlIO.mapOptional("wasmEHFuncInfo", MFI.SrcToUnwindDest);
220  }
221 };
222 
223 template <> struct CustomMappingTraits<BBNumberMap> {
224  static void inputOne(IO &YamlIO, StringRef Key,
225  BBNumberMap &SrcToUnwindDest) {
226  YamlIO.mapRequired(Key.str().c_str(),
227  SrcToUnwindDest[std::atoi(Key.str().c_str())]);
228  }
229 
230  static void output(IO &YamlIO, BBNumberMap &SrcToUnwindDest) {
231  for (auto KV : SrcToUnwindDest)
232  YamlIO.mapRequired(std::to_string(KV.first).c_str(), KV.second);
233  }
234 };
235 
236 } // end namespace yaml
237 
238 } // end namespace llvm
239 
240 #endif
i
i
Definition: README.txt:29
llvm::WasmEHFuncInfo
Definition: WasmEHFuncInfo.h:32
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::WebAssemblyFunctionInfo::getLocals
const std::vector< MVT > & getLocals() const
Definition: WebAssemblyMachineFunctionInfo.h:100
FunctionType
Definition: ItaniumDemangle.h:765
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:50
llvm::BitVector::set
BitVector & set()
Definition: BitVector.h:344
llvm::yaml::MachineFunctionInfo
Targets should override this in a way that mirrors the implementation of llvm::MachineFunctionInfo.
Definition: MIRYamlMapping.h:676
llvm::WebAssemblyFunctionInfo::addResult
void addResult(MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:89
llvm::WebAssemblyFunctionInfo::setVarargBufferVreg
void setVarargBufferVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:106
llvm::signatureFromMVTs
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
Definition: WebAssemblyMachineFunctionInfo.cpp:116
llvm::yaml::CustomMappingTraits< BBNumberMap >::output
static void output(IO &YamlIO, BBNumberMap &SrcToUnwindDest)
Definition: WebAssemblyMachineFunctionInfo.h:230
llvm::WebAssemblyFunctionInfo::setCFGStackified
void setCFGStackified(bool Value=true)
Definition: WebAssemblyMachineFunctionInfo.h:168
llvm::WebAssemblyFunctionInfo::getMachineFunction
const MachineFunction & getMachineFunction() const
Definition: WebAssemblyMachineFunctionInfo.h:82
llvm::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo(MachineFunction &MF_)
Definition: WebAssemblyMachineFunctionInfo.h:73
llvm::X86Disassembler::Reg
Reg
All possible values of the reg field in the ModR/M byte.
Definition: X86DisassemblerDecoder.h:462
llvm::WebAssemblyFunctionInfo::clone
MachineFunctionInfo * clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, const DenseMap< MachineBasicBlock *, MachineBasicBlock * > &Src2DstMBB) const override
Make a functionally equivalent copy of this MachineFunctionInfo in MF.
Definition: WebAssemblyMachineFunctionInfo.cpp:27
llvm::WebAssemblyFunctionInfo::setFrameBaseVreg
void setFrameBaseVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:112
llvm::yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo()=default
llvm::WebAssemblyFunctionInfo::addParam
void addParam(MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:86
llvm::WebAssemblyFunctionInfo::setBasePointerVreg
void setBasePointerVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:125
llvm::WebAssemblyFunctionInfo::unstackifyVReg
void unstackifyVReg(unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:136
llvm::BitVector::resize
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:334
YamlIO
IO & YamlIO
Definition: ELFYAML.cpp:1235
Results
Function Alias Analysis Results
Definition: AliasAnalysis.cpp:848
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:51
llvm::yaml::WebAssemblyFunctionInfo::Params
std::vector< FlowStringValue > Params
Definition: WebAssemblyMachineFunctionInfo.h:200
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::yaml::WebAssemblyFunctionInfo::mappingImpl
void mappingImpl(yaml::IO &YamlIO) override
Definition: WebAssemblyMachineFunctionInfo.cpp:153
MachineRegisterInfo.h
llvm::yaml::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo()=default
llvm::WebAssemblyFunctionInfo::getBasePointerVreg
unsigned getBasePointerVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:108
MIRYamlMapping.h
llvm::WebAssemblyFunctionInfo::getVarargBufferVreg
unsigned getVarargBufferVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:102
llvm::WebAssemblyFunctionInfo::clearFrameBaseVreg
void clearFrameBaseVreg()
Definition: WebAssemblyMachineFunctionInfo.h:117
MCSymbolWasm.h
llvm::BitVector::size
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:152
llvm::WebAssemblyFunctionInfo::clearParamsAndResults
void clearParamsAndResults()
Definition: WebAssemblyMachineFunctionInfo.h:92
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
llvm::WebAssemblyFunctionInfo::isCFGStackified
bool isCFGStackified() const
Definition: WebAssemblyMachineFunctionInfo.h:167
llvm::yaml::WebAssemblyFunctionInfo::CFGStackified
bool CFGStackified
Definition: WebAssemblyMachineFunctionInfo.h:202
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::BitVector
Definition: BitVector.h:75
llvm::yaml::MappingTraits< WebAssemblyFunctionInfo >::mapping
static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI)
Definition: WebAssemblyMachineFunctionInfo.h:215
WebAssemblyMCTargetDesc.h
llvm::yaml::WebAssemblyFunctionInfo::SrcToUnwindDest
BBNumberMap SrcToUnwindDest
Definition: WebAssemblyMachineFunctionInfo.h:205
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::WebAssemblyFunctionInfo::stackifyVReg
void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:129
llvm::WebAssemblyFunctionInfo::UnusedReg
static const unsigned UnusedReg
Definition: WebAssemblyMachineFunctionInfo.h:127
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:63
llvm::DenseMap
Definition: DenseMap.h:716
llvm::valTypesFromMVTs
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
Definition: WebAssemblyMachineFunctionInfo.cpp:109
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::WebAssemblyFunctionInfo::setWasmEHFuncInfo
void setWasmEHFuncInfo(WasmEHFuncInfo *Info)
Definition: WebAssemblyMachineFunctionInfo.h:171
llvm::WebAssemblyFunctionInfo::getWAReg
unsigned getWAReg(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:155
llvm::WebAssemblyFunctionInfo::isVRegStackified
bool isVRegStackified(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:141
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WebAssemblyFunctionInfo::initializeBaseYamlFields
void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI)
Definition: WebAssemblyMachineFunctionInfo.cpp:157
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:257
llvm::computeLegalValueVTs
void computeLegalValueVTs(const WebAssemblyTargetLowering &TLI, LLVMContext &Ctx, const DataLayout &DL, Type *Ty, SmallVectorImpl< MVT > &ValueVTs)
Definition: WebAssemblyMachineFunctionInfo.cpp:43
llvm::WebAssemblyFunctionInfo::getResults
const std::vector< MVT > & getResults() const
Definition: WebAssemblyMachineFunctionInfo.h:90
llvm::WebAssemblyFunctionInfo::setFrameBaseLocal
void setFrameBaseLocal(unsigned Local)
Definition: WebAssemblyMachineFunctionInfo.h:120
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:149
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::WebAssemblyFunctionInfo::setNumLocals
void setNumLocals(size_t NumLocals)
Definition: WebAssemblyMachineFunctionInfo.h:97
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::WebAssemblyFunctionInfo::getWasmEHFuncInfo
WasmEHFuncInfo * getWasmEHFuncInfo() const
Definition: WebAssemblyMachineFunctionInfo.h:170
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:65
llvm::BitVector::test
bool test(unsigned Idx) const
Definition: BitVector.h:454
llvm::WebAssemblyFunctionInfo::initWARegs
void initWARegs(MachineRegisterInfo &MRI)
Definition: WebAssemblyMachineFunctionInfo.cpp:37
llvm::MVT::i32
@ i32
Definition: MachineValueType.h:48
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::WebAssemblyFunctionInfo::isFrameBaseVirtual
bool isFrameBaseVirtual() const
Definition: WebAssemblyMachineFunctionInfo.h:119
llvm::yaml::CustomMappingTraits< BBNumberMap >::inputOne
static void inputOne(IO &YamlIO, StringRef Key, BBNumberMap &SrcToUnwindDest)
Definition: WebAssemblyMachineFunctionInfo.h:224
llvm::yaml::WebAssemblyFunctionInfo::Results
std::vector< FlowStringValue > Results
Definition: WebAssemblyMachineFunctionInfo.h:201
llvm::BitVector::reset
BitVector & reset()
Definition: BitVector.h:385
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:142
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:85
llvm::WebAssemblyFunctionInfo::getParams
const std::vector< MVT > & getParams() const
Definition: WebAssemblyMachineFunctionInfo.h:87
llvm::WebAssemblyFunctionInfo::setLocal
void setLocal(size_t i, MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:98
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::WebAssemblyFunctionInfo::getFrameBaseVreg
unsigned getFrameBaseVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:113
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:99
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:121
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::yaml::WebAssemblyFunctionInfo
Definition: WebAssemblyMachineFunctionInfo.h:199
llvm::WebAssemblyFunctionInfo::getWARegStackId
static unsigned getWARegStackId(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:162