LLVM 22.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
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.
139 SmallPtrSet<const MachineBasicBlock *, 16> MBBs;
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
154
157 CFGStackified = YamlMFI.CFGStackified;
158 for (auto VT : YamlMFI.Params)
160 for (auto VT : YamlMFI.Results)
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
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
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
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Class to represent function types.
iterator_range< arg_iterator > args()
Definition Function.h:890
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Context object for machine code objects.
Definition MCContext.h:83
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,...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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.
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 ...
WebAssemblyFunctionInfo(const Function &F, const TargetSubtargetInfo *STI)
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.
void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc, const Function &ContextFunc, const TargetMachine &TM, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results)
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
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:119
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...
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:62