LLVM 17.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
23using namespace llvm;
24
26
28 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
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,
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)
112}
113
114std::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
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}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Function Alias Analysis Results
IO & YamlIO
Definition: ELFYAML.cpp:1259
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
const char LLVMTargetMachineRef TM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the interfaces that WebAssembly uses to lower LLVM code into a selection DAG.
This class prints an WebAssembly MCInst to wasm file syntax.
This file declares WebAssembly-specific per-machine-function information.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific type parsing utility functions.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:114
Class to represent function types.
Definition: DerivedTypes.h:103
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:130
bool isVarArg() const
Definition: DerivedTypes.h:123
Type * getReturnType() const
Definition: DerivedTypes.h:124
iterator_range< arg_iterator > args()
Definition: Function.h:790
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:237
size_t size() const
Definition: Function.h:756
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Machine Value Type.
static MVT getIntegerVT(unsigned BitWidth)
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
const WasmEHFuncInfo * getWasmEHFuncInfo() const
getWasmEHFuncInfo - Return information about how the current function uses Wasm exception handling.
Ty * cloneInfo(const Ty &Old)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
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
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
const std::vector< MVT > & getResults() const
MachineFunctionInfo * clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, const DenseMap< MachineBasicBlock *, MachineBasicBlock * > &Src2DstMBB) const override
Make a functionally equivalent copy of this MachineFunctionInfo in MF.
void initWARegs(MachineRegisterInfo &MRI)
void initializeBaseYamlFields(MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI)
const std::vector< MVT > & getParams() const
@ Swift
Calling convention for Swift.
Definition: CallingConv.h:69
wasm::ValType toValType(MVT Type)
MVT parseMVT(StringRef Type)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc, const Function &ContextFunc, const TargetMachine &TM, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results)
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
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
void computeLegalValueVTs(const WebAssemblyTargetLowering &TLI, LLVMContext &Ctx, const DataLayout &DL, Type *Ty, SmallVectorImpl< MVT > &ValueVTs)
Extended Value Type.
Definition: ValueTypes.h:34
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...