LLVM 19.0.0git
Go to the documentation of this file.
1//=- WebAssemblyMachineFunctionInfo.cpp - WebAssembly Machine Function Info -=//
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
9/// \file
10/// This file implements WebAssembly-specific per-machine-function
11/// information.
24using namespace llvm;
29 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
31 const {
32 // TODO: Implement cloning for WasmEHFuncInfo. This will have invalid block
33 // references.
34 return DestMF.cloneInfo<WebAssemblyFunctionInfo>(*this);
38 assert(WARegs.empty());
39 unsigned Reg = WebAssembly::UnusedReg;
40 WARegs.resize(MRI.getNumVirtRegs(), Reg);
44 LLVMContext &Ctx, const DataLayout &DL,
45 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
47 ComputeValueVTs(TLI, DL, Ty, VTs);
49 for (EVT VT : VTs) {
50 unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
51 MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
52 for (unsigned I = 0; I != NumRegs; ++I)
53 ValueVTs.push_back(RegisterVT);
54 }
58 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
59 const DataLayout &DL(F.getParent()->getDataLayout());
60 const WebAssemblyTargetLowering &TLI =
61 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
62 computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
66 const Function *TargetFunc,
67 const Function &ContextFunc,
68 const TargetMachine &TM,
71 computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
73 MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
75 Results.size(),
76 &TM.getSubtarget<WebAssemblySubtarget>(ContextFunc))) {
77 // WebAssembly can't lower returns of multiple values without demoting to
78 // sret unless multivalue is enabled (see
79 // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
80 // values with a poitner parameter.
81 Results.clear();
82 Params.push_back(PtrVT);
83 }
85 for (auto *Param : Ty->params())
86 computeLegalValueVTs(ContextFunc, TM, Param, Params);
87 if (Ty->isVarArg())
88 Params.push_back(PtrVT);
90 // For swiftcc, emit additional swiftself and swifterror parameters
91 // if there aren't. These additional parameters are also passed for caller.
92 // They are necessary to match callee and caller signature for indirect
93 // call.
95 if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
96 MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
97 bool HasSwiftErrorArg = false;
98 bool HasSwiftSelfArg = false;
99 for (const auto &Arg : TargetFunc->args()) {
100 HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
101 HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
102 }
103 if (!HasSwiftErrorArg)
104 Params.push_back(PtrVT);
105 if (!HasSwiftSelfArg)
106 Params.push_back(PtrVT);
107 }
112 for (MVT Ty : In)
118 const SmallVectorImpl<MVT> &Params) {
119 auto Sig = Ctx.createWasmSignature();
120 valTypesFromMVTs(Results, Sig->Returns);
121 valTypesFromMVTs(Params, Sig->Params);
122 return Sig;
127 : CFGStackified(MFI.isCFGStackified()) {
128 for (auto VT : MFI.getParams())
129 Params.push_back(EVT(VT).getEVTString());
130 for (auto VT : MFI.getResults())
131 Results.push_back(EVT(VT).getEVTString());
133 // MFI.getWasmEHFuncInfo() is non-null only for functions with the
134 // personality function.
136 if (auto *EHInfo = MF.getWasmEHFuncInfo()) {
137 // SrcToUnwindDest can contain stale mappings in case BBs are removed in
138 // optimizations, in case, for example, they are unreachable. We should not
139 // include their info.
141 for (const auto &MBB : MF)
142 MBBs.insert(&MBB);
143 for (auto KV : EHInfo->SrcToUnwindDest) {
144 auto *SrcBB = KV.first.get<MachineBasicBlock *>();
145 auto *DestBB = KV.second.get<MachineBasicBlock *>();
146 if (MBBs.count(SrcBB) && MBBs.count(DestBB))
147 SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
148 }
149 }
159 for (auto VT : YamlMFI.Params)
160 addParam(WebAssembly::parseMVT(VT.Value));
161 for (auto VT : YamlMFI.Results)
162 addResult(WebAssembly::parseMVT(VT.Value));
164 // FIXME: WasmEHInfo is defined in the MachineFunction, but serialized
165 // here. Either WasmEHInfo should be moved out of MachineFunction, or the
166 // serialization handling should be moved to MachineFunction.
167 if (WasmEHFuncInfo *WasmEHInfo = MF.getWasmEHFuncInfo()) {
168 for (auto KV : YamlMFI.SrcToUnwindDest)
169 WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
170 MF.getBlockNumbered(KV.second));
171 }
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
IO & YamlIO
Definition: ELFYAML.cpp:1292
#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.
This file contains the declaration of the WebAssembly-specific 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:110
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:845
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:264
size_t size() const
Definition: Function.h:811
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Context object for machine code objects.
Definition: MCContext.h:82
wasm::WasmSignature * createWasmSignature()
Allocates and returns a new WasmSignature instance (with empty parameter and return type lists).
Definition: MCContext.cpp:388
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:360
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:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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:76
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)
static const unsigned UnusedReg
bool canLowerReturn(size_t ResultSize, const WebAssemblySubtarget *Subtarget)
Returns true if the function's return value(s) can be lowered directly, i.e., not indirectly via a po...
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 ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition: Analysis.cpp:79
void valTypesFromMVTs(ArrayRef< MVT > In, SmallVectorImpl< wasm::ValType > &Out)
wasm::WasmSignature * signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
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...