LLVM  16.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  std::vector<MVT> Params;
35  std::vector<MVT> Results;
36  std::vector<MVT> Locals;
37 
38  /// A mapping from CodeGen vreg index to WebAssembly register number.
39  std::vector<unsigned> WARegs;
40 
41  /// A mapping from CodeGen vreg index to a boolean value indicating whether
42  /// the given register is considered to be "stackified", meaning it has been
43  /// determined or made to meet the stack requirements:
44  /// - single use (per path)
45  /// - single def (per path)
46  /// - defined and used in LIFO order with other stack registers
47  BitVector VRegStackified;
48 
49  // A virtual register holding the pointer to the vararg buffer for vararg
50  // functions. It is created and set in TLI::LowerFormalArguments and read by
51  // TLI::LowerVASTART
52  unsigned VarargVreg = -1U;
53 
54  // A virtual register holding the base pointer for functions that have
55  // overaligned values on the user stack.
56  unsigned BasePtrVreg = -1U;
57  // A virtual register holding the frame base. This is either FP or SP
58  // after it has been replaced by a vreg
59  unsigned FrameBaseVreg = -1U;
60  // The local holding the frame base. This is either FP or SP
61  // after WebAssemblyExplicitLocals
62  unsigned FrameBaseLocal = -1U;
63 
64  // Function properties.
65  bool CFGStackified = false;
66 
67 public:
69  ~WebAssemblyFunctionInfo() override;
70 
74  const override;
75 
77  const yaml::WebAssemblyFunctionInfo &YamlMFI);
78 
79  void addParam(MVT VT) { Params.push_back(VT); }
80  const std::vector<MVT> &getParams() const { return Params; }
81 
82  void addResult(MVT VT) { Results.push_back(VT); }
83  const std::vector<MVT> &getResults() const { return Results; }
84 
86  Params.clear();
87  Results.clear();
88  }
89 
90  void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); }
91  void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
92  void addLocal(MVT VT) { Locals.push_back(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  static const unsigned UnusedReg = -1u;
121 
122  void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg) {
123  assert(MRI.getUniqueVRegDef(VReg));
124  auto I = Register::virtReg2Index(VReg);
125  if (I >= VRegStackified.size())
126  VRegStackified.resize(I + 1);
127  VRegStackified.set(I);
128  }
129  void unstackifyVReg(unsigned VReg) {
130  auto I = Register::virtReg2Index(VReg);
131  if (I < VRegStackified.size())
132  VRegStackified.reset(I);
133  }
134  bool isVRegStackified(unsigned VReg) const {
135  auto I = Register::virtReg2Index(VReg);
136  if (I >= VRegStackified.size())
137  return false;
138  return VRegStackified.test(I);
139  }
140 
142  void setWAReg(unsigned VReg, unsigned WAReg) {
143  assert(WAReg != UnusedReg);
144  auto I = Register::virtReg2Index(VReg);
145  assert(I < WARegs.size());
146  WARegs[I] = WAReg;
147  }
148  unsigned getWAReg(unsigned VReg) const {
149  auto I = Register::virtReg2Index(VReg);
150  assert(I < WARegs.size());
151  return WARegs[I];
152  }
153 
154  // For a given stackified WAReg, return the id number to print with push/pop.
155  static unsigned getWARegStackId(unsigned Reg) {
156  assert(Reg & INT32_MIN);
157  return Reg & INT32_MAX;
158  }
159 
160  bool isCFGStackified() const { return CFGStackified; }
161  void setCFGStackified(bool Value = true) { CFGStackified = Value; }
162 };
163 
164 void computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
165  LLVMContext &Ctx, const DataLayout &DL, Type *Ty,
166  SmallVectorImpl<MVT> &ValueVTs);
167 
168 void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
169  SmallVectorImpl<MVT> &ValueVTs);
170 
171 // Compute the signature for a given FunctionType (Ty). Note that it's not the
172 // signature for ContextFunc (ContextFunc is just used to get varous context)
173 void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc,
174  const Function &ContextFunc, const TargetMachine &TM,
175  SmallVectorImpl<MVT> &Params,
176  SmallVectorImpl<MVT> &Results);
177 
178 void valTypesFromMVTs(const ArrayRef<MVT> &In,
179  SmallVectorImpl<wasm::ValType> &Out);
180 
181 std::unique_ptr<wasm::WasmSignature>
182 signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
183  const SmallVectorImpl<MVT> &Params);
184 
185 namespace yaml {
186 
188 
190  std::vector<FlowStringValue> Params;
191  std::vector<FlowStringValue> Results;
192  bool CFGStackified = false;
193  // The same as WasmEHFuncInfo's SrcToUnwindDest, but stored in the mapping of
194  // BB numbers
196 
197  WebAssemblyFunctionInfo() = default;
199  const llvm::WebAssemblyFunctionInfo &MFI);
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::WebAssemblyFunctionInfo::getLocals
const std::vector< MVT > & getLocals() const
Definition: WebAssemblyMachineFunctionInfo.h:93
FunctionType
Definition: ItaniumDemangle.h:775
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:82
llvm::WebAssemblyFunctionInfo::setVarargBufferVreg
void setVarargBufferVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:99
llvm::signatureFromMVTs
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
Definition: WebAssemblyMachineFunctionInfo.cpp:115
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:161
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:105
llvm::yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo()=default
llvm::WebAssemblyFunctionInfo::addParam
void addParam(MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:79
llvm::WebAssemblyFunctionInfo::setBasePointerVreg
void setBasePointerVreg(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:118
llvm::WebAssemblyFunctionInfo::unstackifyVReg
void unstackifyVReg(unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:129
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:1250
Results
Function Alias Analysis Results
Definition: AliasAnalysis.cpp:772
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:52
llvm::yaml::WebAssemblyFunctionInfo::Params
std::vector< FlowStringValue > Params
Definition: WebAssemblyMachineFunctionInfo.h:190
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::yaml::WebAssemblyFunctionInfo::mappingImpl
void mappingImpl(yaml::IO &YamlIO) override
Definition: WebAssemblyMachineFunctionInfo.cpp:150
MachineRegisterInfo.h
llvm::yaml::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo()=default
llvm::WebAssemblyFunctionInfo::getBasePointerVreg
unsigned getBasePointerVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:101
MIRYamlMapping.h
llvm::WebAssemblyFunctionInfo::getVarargBufferVreg
unsigned getVarargBufferVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:95
llvm::WebAssemblyFunctionInfo::initializeBaseYamlFields
void initializeBaseYamlFields(MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI)
Definition: WebAssemblyMachineFunctionInfo.cpp:154
llvm::WebAssemblyFunctionInfo::clearFrameBaseVreg
void clearFrameBaseVreg()
Definition: WebAssemblyMachineFunctionInfo.h:110
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:85
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
llvm::WebAssemblyFunctionInfo::isCFGStackified
bool isCFGStackified() const
Definition: WebAssemblyMachineFunctionInfo.h:160
llvm::yaml::WebAssemblyFunctionInfo::CFGStackified
bool CFGStackified
Definition: WebAssemblyMachineFunctionInfo.h:192
llvm::BitVector
Definition: BitVector.h:75
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:195
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::WebAssemblyFunctionInfo::stackifyVReg
void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:122
llvm::WebAssemblyFunctionInfo::UnusedReg
static const unsigned UnusedReg
Definition: WebAssemblyMachineFunctionInfo.h:120
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:63
llvm::DenseMap
Definition: DenseMap.h:714
llvm::valTypesFromMVTs
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
Definition: WebAssemblyMachineFunctionInfo.cpp:108
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::WebAssemblyFunctionInfo::getWAReg
unsigned getWAReg(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:148
llvm::WebAssemblyFunctionInfo::isVRegStackified
bool isVRegStackified(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:134
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:42
llvm::WebAssemblyFunctionInfo::getResults
const std::vector< MVT > & getResults() const
Definition: WebAssemblyMachineFunctionInfo.h:83
llvm::WebAssemblyFunctionInfo::setFrameBaseLocal
void setFrameBaseLocal(unsigned Local)
Definition: WebAssemblyMachineFunctionInfo.h:113
llvm::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo() override
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::WebAssemblyFunctionInfo::setWAReg
void setWAReg(unsigned VReg, unsigned WAReg)
Definition: WebAssemblyMachineFunctionInfo.h:142
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::WebAssemblyFunctionInfo::setNumLocals
void setNumLocals(size_t NumLocals)
Definition: WebAssemblyMachineFunctionInfo.h:90
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
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:64
llvm::BitVector::test
bool test(unsigned Idx) const
Definition: BitVector.h:454
llvm::WebAssemblyFunctionInfo::initWARegs
void initWARegs(MachineRegisterInfo &MRI)
Definition: WebAssemblyMachineFunctionInfo.cpp:36
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:112
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:191
llvm::BitVector::reset
BitVector & reset()
Definition: BitVector.h:385
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:143
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:80
llvm::WebAssemblyFunctionInfo::setLocal
void setLocal(size_t i, MVT VT)
Definition: WebAssemblyMachineFunctionInfo.h:91
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::WebAssemblyFunctionInfo::getFrameBaseVreg
unsigned getFrameBaseVreg() const
Definition: WebAssemblyMachineFunctionInfo.h:106
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:92
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:114
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::yaml::WebAssemblyFunctionInfo
Definition: WebAssemblyMachineFunctionInfo.h:189
llvm::WebAssemblyFunctionInfo::getWARegStackId
static unsigned getWARegStackId(unsigned Reg)
Definition: WebAssemblyMachineFunctionInfo.h:155
llvm::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo(MachineFunction &)
Definition: WebAssemblyMachineFunctionInfo.h:68