LLVM 23.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
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 and swifttailcc, emit additional swiftself, swifterror, and
90 // (for swifttailcc) swiftasync parameters if there aren't. These additional
91 // parameters are also passed for caller. They are necessary to match callee
92 // and caller signature for indirect call.
93
94 if (TargetFunc && (TargetFunc->getCallingConv() == CallingConv::Swift ||
95 TargetFunc->getCallingConv() == CallingConv::SwiftTail)) {
97 bool HasSwiftErrorArg = false;
98 bool HasSwiftSelfArg = false;
99 bool HasSwiftAsyncArg = false;
100 for (const auto &Arg : TargetFunc->args()) {
101 HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
102 HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
103 HasSwiftAsyncArg |= Arg.hasAttribute(Attribute::SwiftAsync);
104 }
105 if (!HasSwiftSelfArg)
106 Params.push_back(PtrVT);
107 if (!HasSwiftErrorArg)
108 Params.push_back(PtrVT);
109 if (TargetFunc->getCallingConv() == CallingConv::SwiftTail &&
110 !HasSwiftAsyncArg)
111 Params.push_back(PtrVT);
112 }
113}
114
120
123 const SmallVectorImpl<MVT> &Params) {
124 auto Sig = Ctx.createWasmSignature();
125 valTypesFromMVTs(Results, Sig->Returns);
126 valTypesFromMVTs(Params, Sig->Params);
127 return Sig;
128}
129
132 : CFGStackified(MFI.isCFGStackified()) {
133 for (auto VT : MFI.getParams())
134 Params.push_back(EVT(VT).getEVTString());
135 for (auto VT : MFI.getResults())
136 Results.push_back(EVT(VT).getEVTString());
137
138 // MFI.getWasmEHFuncInfo() is non-null only for functions with the
139 // personality function.
140
141 if (auto *EHInfo = MF.getWasmEHFuncInfo()) {
142 // SrcToUnwindDest can contain stale mappings in case BBs are removed in
143 // optimizations, in case, for example, they are unreachable. We should not
144 // include their info.
145 SmallPtrSet<const MachineBasicBlock *, 16> MBBs;
146 for (const auto &MBB : MF)
147 MBBs.insert(&MBB);
148 for (auto KV : EHInfo->SrcToUnwindDest) {
149 auto *SrcBB = cast<MachineBasicBlock *>(KV.first);
150 auto *DestBB = cast<MachineBasicBlock *>(KV.second);
151 if (MBBs.count(SrcBB) && MBBs.count(DestBB))
152 SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
153 }
154 }
155}
156
160
163 CFGStackified = YamlMFI.CFGStackified;
164 for (auto VT : YamlMFI.Params)
166 for (auto VT : YamlMFI.Results)
168
169 // FIXME: WasmEHInfo is defined in the MachineFunction, but serialized
170 // here. Either WasmEHInfo should be moved out of MachineFunction, or the
171 // serialization handling should be moved to MachineFunction.
172 if (WasmEHFuncInfo *WasmEHInfo = MF.getWasmEHFuncInfo()) {
173 for (auto KV : YamlMFI.SrcToUnwindDest)
174 WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
175 MF.getBlockNumbered(KV.second));
176 }
177}
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:54
#define I(x, y, z)
Definition MD5.cpp:57
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:40
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
unsigned getPointerSizeInBits(unsigned AS=0) const
The size in bits of the pointer representation in a given address space.
Definition DataLayout.h:494
Class to represent function types.
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:272
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,...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
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.
const DataLayout createDataLayout() const
Create a DataLayout.
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
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
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
Definition CallingConv.h:87
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)
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, 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)
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
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