LLVM 24.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
21#include "llvm/IR/Module.h"
23using namespace llvm;
24
26
33
35 assert(WARegs.empty());
36 unsigned Reg = WebAssembly::UnusedReg;
37 WARegs.resize(MRI.getNumVirtRegs(), Reg);
38}
39
41 LLVMContext &Ctx, const DataLayout &DL,
42 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
44 ComputeValueVTs(TLI, DL, Ty, VTs);
45
46 for (EVT VT : VTs) {
47 unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
48 MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
49 for (unsigned I = 0; I != NumRegs; ++I)
50 ValueVTs.push_back(RegisterVT);
51 }
52}
53
55 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
56 const DataLayout &DL(F.getDataLayout());
58 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
59 computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
60}
61
63 const Function *TargetFunc,
64 const Function &ContextFunc,
65 const TargetMachine &TM,
68 computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
69
70 const DataLayout &DL = ContextFunc.getDataLayout();
71 MVT PtrVT = MVT::getIntegerVT(DL.getPointerSizeInBits());
73 Results.size(),
74 &TM.getSubtarget<WebAssemblySubtarget>(ContextFunc))) {
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 pointer parameter.
79 Results.clear();
80 Params.push_back(PtrVT);
81 }
82
83 std::optional<size_t> SwiftAsyncIdx;
84 for (auto [I, Param] : enumerate(Ty->params())) {
85 if (TargetFunc && TargetFunc->hasParamAttribute(I, Attribute::SwiftAsync))
86 SwiftAsyncIdx = Params.size();
87 computeLegalValueVTs(ContextFunc, TM, Param, Params);
88 }
89 // ABI contract: the async context must always be available in local 0.
90 if (SwiftAsyncIdx)
91 WebAssembly::moveToFront(Params, *SwiftAsyncIdx);
92 if (Ty->isVarArg())
93 Params.push_back(PtrVT);
94
95 // For swiftcc and swifttailcc, emit additional swiftself, swifterror, and
96 // (for swifttailcc) swiftasync parameters if there aren't. These additional
97 // parameters are also passed for caller. They are necessary to match callee
98 // and caller signature for indirect call.
99
100 if (TargetFunc && (TargetFunc->getCallingConv() == CallingConv::Swift ||
101 TargetFunc->getCallingConv() == CallingConv::SwiftTail)) {
102 MVT PtrVT = MVT::getIntegerVT(DL.getPointerSizeInBits());
103 bool HasSwiftErrorArg = false;
104 bool HasSwiftSelfArg = false;
105 bool HasSwiftAsyncArg = false;
106 for (const auto &Arg : TargetFunc->args()) {
107 HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
108 HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
109 HasSwiftAsyncArg |= Arg.hasAttribute(Attribute::SwiftAsync);
110 }
111 if (!HasSwiftSelfArg)
112 Params.push_back(PtrVT);
113 if (!HasSwiftErrorArg)
114 Params.push_back(PtrVT);
115 if (TargetFunc->getCallingConv() == CallingConv::SwiftTail &&
116 !HasSwiftAsyncArg)
117 Params.insert(Params.begin(), PtrVT);
118 }
119}
120
123 for (MVT Ty : In)
124 Out.push_back(WebAssembly::toValType(Ty));
125}
126
129 const SmallVectorImpl<MVT> &Params) {
130 auto Sig = Ctx.createWasmSignature();
131 valTypesFromMVTs(Results, Sig->Returns);
132 valTypesFromMVTs(Params, Sig->Params);
133 return Sig;
134}
135
138 : CFGStackified(MFI.isCFGStackified()) {
139 for (auto VT : MFI.getParams())
140 Params.push_back(EVT(VT).getEVTString());
141 for (auto VT : MFI.getResults())
142 Results.push_back(EVT(VT).getEVTString());
143}
144
148
151 CFGStackified = YamlMFI.CFGStackified;
152 for (auto VT : YamlMFI.Params)
154 for (auto VT : YamlMFI.Results)
156}
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
MachineRegisterInfo & MRI
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.
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
Class to represent function types.
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition Function.cpp:356
iterator_range< arg_iterator > args()
Definition Function.h:877
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:743
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
Context object for machine code objects.
Definition MCContext.h:83
Machine Value Type.
static MVT getIntegerVT(unsigned BitWidth)
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...
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Primary interface to the complete machine description for the target machine.
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
void moveToFront(RangeT &Range, size_t Idx)
Moves the element at Idx of Range to the front, keeping the order of the other elements.
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)
LLVM_ABI 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:121
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
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:391
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:63