LLVM  16.0.0git
WebAssemblyMachineFunctionInfo.cpp
Go to the documentation of this file.
1 //=- WebAssemblyMachineFunctionInfo.cpp - WebAssembly Machine Function Info -=//
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 implements WebAssembly-specific per-machine-function
11 /// information.
12 ///
13 //===----------------------------------------------------------------------===//
14 
19 #include "WebAssemblySubtarget.h"
20 #include "llvm/CodeGen/Analysis.h"
23 using namespace llvm;
24 
26 
30  const {
31  // TODO: Implement cloning for WasmEHFuncInfo. This will have invalid block
32  // references.
33  return DestMF.cloneInfo<WebAssemblyFunctionInfo>(*this);
34 }
35 
37  assert(WARegs.empty());
38  unsigned Reg = UnusedReg;
39  WARegs.resize(MRI.getNumVirtRegs(), Reg);
40 }
41 
43  LLVMContext &Ctx, const DataLayout &DL,
44  Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
46  ComputeValueVTs(TLI, DL, Ty, VTs);
47 
48  for (EVT VT : VTs) {
49  unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
50  MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
51  for (unsigned I = 0; I != NumRegs; ++I)
52  ValueVTs.push_back(RegisterVT);
53  }
54 }
55 
57  Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
58  const DataLayout &DL(F.getParent()->getDataLayout());
59  const WebAssemblyTargetLowering &TLI =
60  *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
61  computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
62 }
63 
65  const Function *TargetFunc,
66  const Function &ContextFunc,
67  const TargetMachine &TM,
68  SmallVectorImpl<MVT> &Params,
70  computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
71 
72  MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
73  if (Results.size() > 1 &&
74  !TM.getSubtarget<WebAssemblySubtarget>(ContextFunc).hasMultivalue()) {
75  // WebAssembly can't lower returns of multiple values without demoting to
76  // sret unless multivalue is enabled (see
77  // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
78  // values with a poitner parameter.
79  Results.clear();
80  Params.push_back(PtrVT);
81  }
82 
83  for (auto *Param : Ty->params())
84  computeLegalValueVTs(ContextFunc, TM, Param, Params);
85  if (Ty->isVarArg())
86  Params.push_back(PtrVT);
87 
88  // For swiftcc, emit additional swiftself and swifterror parameters
89  // if there aren't. These additional parameters are also passed for caller.
90  // They are necessary to match callee and caller signature for indirect
91  // call.
92 
93  if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
94  MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
95  bool HasSwiftErrorArg = false;
96  bool HasSwiftSelfArg = false;
97  for (const auto &Arg : TargetFunc->args()) {
98  HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
99  HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
100  }
101  if (!HasSwiftErrorArg)
102  Params.push_back(PtrVT);
103  if (!HasSwiftSelfArg)
104  Params.push_back(PtrVT);
105  }
106 }
107 
110  for (MVT Ty : In)
111  Out.push_back(WebAssembly::toValType(Ty));
112 }
113 
114 std::unique_ptr<wasm::WasmSignature>
116  const SmallVectorImpl<MVT> &Params) {
117  auto Sig = std::make_unique<wasm::WasmSignature>();
118  valTypesFromMVTs(Results, Sig->Returns);
119  valTypesFromMVTs(Params, Sig->Params);
120  return Sig;
121 }
122 
125  : CFGStackified(MFI.isCFGStackified()) {
126  for (auto VT : MFI.getParams())
127  Params.push_back(EVT(VT).getEVTString());
128  for (auto VT : MFI.getResults())
129  Results.push_back(EVT(VT).getEVTString());
130 
131  // MFI.getWasmEHFuncInfo() is non-null only for functions with the
132  // personality function.
133 
134  if (auto *EHInfo = MF.getWasmEHFuncInfo()) {
135  // SrcToUnwindDest can contain stale mappings in case BBs are removed in
136  // optimizations, in case, for example, they are unreachable. We should not
137  // include their info.
139  for (const auto &MBB : MF)
140  MBBs.insert(&MBB);
141  for (auto KV : EHInfo->SrcToUnwindDest) {
142  auto *SrcBB = KV.first.get<MachineBasicBlock *>();
143  auto *DestBB = KV.second.get<MachineBasicBlock *>();
144  if (MBBs.count(SrcBB) && MBBs.count(DestBB))
145  SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
146  }
147  }
148 }
149 
152 }
153 
155  MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI) {
156  CFGStackified = YamlMFI.CFGStackified;
157  for (auto VT : YamlMFI.Params)
158  addParam(WebAssembly::parseMVT(VT.Value));
159  for (auto VT : YamlMFI.Results)
160  addResult(WebAssembly::parseMVT(VT.Value));
161 
162  // FIXME: WasmEHInfo is defined in the MachineFunction, but serialized
163  // here. Either WasmEHInfo should be moved out of MachineFunction, or the
164  // serialization handling should be moved to MachineFunction.
165  if (WasmEHFuncInfo *WasmEHInfo = MF.getWasmEHFuncInfo()) {
166  for (auto KV : YamlMFI.SrcToUnwindDest)
167  WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
168  MF.getBlockNumbered(KV.second));
169  }
170 }
llvm::WasmEHFuncInfo
Definition: WasmEHFuncInfo.h:32
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:59
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::Function::args
iterator_range< arg_iterator > args()
Definition: Function.h:746
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
WebAssemblyISelLowering.h
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:50
llvm::Function
Definition: Function.h:60
llvm::WebAssemblySubtarget::hasMultivalue
bool hasMultivalue() const
Definition: WebAssemblySubtarget.h:100
llvm::signatureFromMVTs
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
Definition: WebAssemblyMachineFunctionInfo.cpp:115
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
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::yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo()=default
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
YamlIO
IO & YamlIO
Definition: ELFYAML.cpp:1250
llvm::MachineRegisterInfo::getNumVirtRegs
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
Definition: MachineRegisterInfo.h:770
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
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
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:123
llvm::ComputeValueVTs
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< uint64_t > *Offsets=nullptr, uint64_t StartingOffset=0)
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition: Analysis.cpp:121
llvm::WebAssemblyTargetLowering
Definition: WebAssemblyISelLowering.h:43
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:187
TargetMachine.h
llvm::WebAssemblyFunctionInfo::initializeBaseYamlFields
void initializeBaseYamlFields(MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI)
Definition: WebAssemblyMachineFunctionInfo.cpp:154
llvm::EVT
Extended Value Type.
Definition: ValueTypes.h:34
llvm::CallingConv::Swift
@ Swift
Calling convention for Swift.
Definition: CallingConv.h:69
Param
Value * Param
Definition: NVPTXLowerArgs.cpp:165
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:125
WebAssemblyTypeUtilities.h
llvm::yaml::WebAssemblyFunctionInfo::CFGStackified
bool CFGStackified
Definition: WebAssemblyMachineFunctionInfo.h:192
llvm::FunctionType::params
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:130
llvm::MachineFunction::getWasmEHFuncInfo
const WasmEHFuncInfo * getWasmEHFuncInfo() const
getWasmEHFuncInfo - Return information about how the current function uses Wasm exception handling.
Definition: MachineFunction.h:695
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::yaml::WebAssemblyFunctionInfo::SrcToUnwindDest
BBNumberMap SrcToUnwindDest
Definition: WebAssemblyMachineFunctionInfo.h:195
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::WebAssemblyFunctionInfo::UnusedReg
static const unsigned UnusedReg
Definition: WebAssemblyMachineFunctionInfo.h:120
llvm::Function::getCallingConv
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:238
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:63
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::DenseMap
Definition: DenseMap.h:714
llvm::valTypesFromMVTs
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
Definition: WebAssemblyMachineFunctionInfo.cpp:108
WebAssemblyInstPrinter.h
I
#define I(x, y, z)
Definition: MD5.cpp:58
Analysis.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
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
WebAssemblyMachineFunctionInfo.h
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
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::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::MachineBasicBlock::getNumber
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Definition: MachineBasicBlock.h:1115
llvm::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo() override
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::WebAssemblySubtarget
Definition: WebAssemblySubtarget.h:35
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::MachineFunction::cloneInfo
Ty * cloneInfo(const Ty &Old)
Definition: MachineFunction.h:766
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getBlockNumbered
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
Definition: MachineFunction.h:789
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::WebAssemblyFunctionInfo::initWARegs
void initWARegs(MachineRegisterInfo &MRI)
Definition: WebAssemblyMachineFunctionInfo.cpp:36
WasmEHFuncInfo.h
WebAssemblySubtarget.h
llvm::yaml::WebAssemblyFunctionInfo::Results
std::vector< FlowStringValue > Results
Definition: WebAssemblyMachineFunctionInfo.h:191
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:143
llvm::WebAssemblyFunctionInfo::getParams
const std::vector< MVT > & getParams() const
Definition: WebAssemblyMachineFunctionInfo.h:80
llvm::TargetLoweringBase::getRegisterType
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
Definition: TargetLowering.h:1544
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::TargetLoweringBase::getNumRegisters
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, Optional< MVT > RegisterVT=None) const
Return the number of registers that this ValueType will eventually require.
Definition: TargetLowering.h:1582
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::MachineFunctionInfo
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
Definition: MachineFunction.h:95
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:124
llvm::yaml::WebAssemblyFunctionInfo
Definition: WebAssemblyMachineFunctionInfo.h:189
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::Function::size
size_t size() const
Definition: Function.h:712
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
llvm::MVT::getIntegerVT
static MVT getIntegerVT(unsigned BitWidth)
Definition: MachineValueType.h:1207