LLVM 20.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 = WebAssembly::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.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());
74 Results.size(),
75 &TM.getSubtarget<WebAssemblySubtarget>(ContextFunc))) {
76 // WebAssembly can't lower returns of multiple values without demoting to
77 // sret unless multivalue is enabled (see
78 // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
79 // values with a poitner parameter.
80 Results.clear();
81 Params.push_back(PtrVT);
82 }
83
84 for (auto *Param : Ty->params())
85 computeLegalValueVTs(ContextFunc, TM, Param, Params);
86 if (Ty->isVarArg())
87 Params.push_back(PtrVT);
88
89 // For swiftcc, emit additional swiftself and swifterror parameters
90 // if there aren't. These additional parameters are also passed for caller.
91 // They are necessary to match callee and caller signature for indirect
92 // call.
93
94 if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
95 MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
96 bool HasSwiftErrorArg = false;
97 bool HasSwiftSelfArg = false;
98 for (const auto &Arg : TargetFunc->args()) {
99 HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
100 HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
101 }
102 if (!HasSwiftErrorArg)
103 Params.push_back(PtrVT);
104 if (!HasSwiftSelfArg)
105 Params.push_back(PtrVT);
106 }
107}
108
111 for (MVT Ty : In)
113}
114
117 const SmallVectorImpl<MVT> &Params) {
118 auto Sig = Ctx.createWasmSignature();
119 valTypesFromMVTs(Results, Sig->Returns);
120 valTypesFromMVTs(Params, Sig->Params);
121 return Sig;
122}
123
126 : CFGStackified(MFI.isCFGStackified()) {
127 for (auto VT : MFI.getParams())
128 Params.push_back(EVT(VT).getEVTString());
129 for (auto VT : MFI.getResults())
130 Results.push_back(EVT(VT).getEVTString());
131
132 // MFI.getWasmEHFuncInfo() is non-null only for functions with the
133 // personality function.
134
135 if (auto *EHInfo = MF.getWasmEHFuncInfo()) {
136 // SrcToUnwindDest can contain stale mappings in case BBs are removed in
137 // optimizations, in case, for example, they are unreachable. We should not
138 // include their info.
140 for (const auto &MBB : MF)
141 MBBs.insert(&MBB);
142 for (auto KV : EHInfo->SrcToUnwindDest) {
143 auto *SrcBB = cast<MachineBasicBlock *>(KV.first);
144 auto *DestBB = cast<MachineBasicBlock *>(KV.second);
145 if (MBBs.count(SrcBB) && MBBs.count(DestBB))
146 SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
147 }
148 }
149}
150
153}
154
158 for (auto VT : YamlMFI.Params)
159 addParam(WebAssembly::parseMVT(VT.Value));
160 for (auto VT : YamlMFI.Results)
161 addResult(WebAssembly::parseMVT(VT.Value));
162
163 // FIXME: WasmEHInfo is defined in the MachineFunction, but serialized
164 // here. Either WasmEHInfo should be moved out of MachineFunction, or the
165 // serialization handling should be moved to MachineFunction.
166 if (WasmEHFuncInfo *WasmEHInfo = MF.getWasmEHFuncInfo()) {
167 for (auto KV : YamlMFI.SrcToUnwindDest)
168 WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
169 MF.getBlockNumbered(KV.second));
170 }
171}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
IO & YamlIO
Definition: ELFYAML.cpp:1312
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the interfaces that WebAssembly uses to lower LLVM code into a selection DAG.
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:63
Class to represent function types.
Definition: DerivedTypes.h:105
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:132
bool isVarArg() const
Definition: DerivedTypes.h:125
Type * getReturnType() const
Definition: DerivedTypes.h:126
iterator_range< arg_iterator > args()
Definition: Function.h:892
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:277
size_t size() const
Definition: Function.h:858
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:83
wasm::WasmSignature * createWasmSignature()
Allocates and returns a new WasmSignature instance (with empty parameter and return type lists).
Definition: MCContext.cpp:428
Machine Value Type.
static MVT getIntegerVT(unsigned BitWidth)
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:452
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
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:77
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:35
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...