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
15#include "WebAssemblyMachineFunctionInfo.h"
16#include "Utils/WebAssemblyTypeUtilities.h"
17#include "WebAssemblyISelLowering.h"
18#include "WebAssemblySubtarget.h"
19#include "WebAssemblyUtilities.h"
20#include "llvm/CodeGen/Analysis.h"
21#include "llvm/IR/Module.h"
22#include "llvm/Target/TargetMachine.h"
23using namespace llvm;
24
25WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() = default; // anchor.
26
27MachineFunctionInfo *WebAssemblyFunctionInfo::clone(
28 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
29 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
30 const {
31 return DestMF.cloneInfo<WebAssemblyFunctionInfo>(Old: *this);
32}
33
34void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
35 assert(WARegs.empty());
36 unsigned Reg = WebAssembly::UnusedReg;
37 WARegs.resize(new_size: MRI.getNumVirtRegs(), x: Reg);
38}
39
40void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
41 LLVMContext &Ctx, const DataLayout &DL,
42 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
43 SmallVector<EVT, 4> VTs;
44 ComputeValueVTs(TLI, DL, Ty, ValueVTs&: VTs);
45
46 for (EVT VT : VTs) {
47 unsigned NumRegs = TLI.getNumRegisters(Context&: Ctx, VT);
48 MVT RegisterVT = TLI.getRegisterType(Context&: Ctx, VT);
49 for (unsigned I = 0; I != NumRegs; ++I)
50 ValueVTs.push_back(Elt: RegisterVT);
51 }
52}
53
54void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
55 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
56 const DataLayout &DL(F.getDataLayout());
57 const WebAssemblyTargetLowering &TLI =
58 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
59 computeLegalValueVTs(TLI, Ctx&: F.getContext(), DL, Ty, ValueVTs);
60}
61
62void llvm::computeSignatureVTs(const FunctionType *Ty,
63 const Function *TargetFunc,
64 const Function &ContextFunc,
65 const TargetMachine &TM,
66 SmallVectorImpl<MVT> &Params,
67 SmallVectorImpl<MVT> &Results) {
68 computeLegalValueVTs(F: ContextFunc, TM, Ty: Ty->getReturnType(), ValueVTs&: Results);
69
70 const DataLayout &DL = ContextFunc.getDataLayout();
71 MVT PtrVT = MVT::getIntegerVT(BitWidth: DL.getPointerSizeInBits());
72 if (!WebAssembly::canLowerReturn(
73 ResultSize: Results.size(),
74 Subtarget: &TM.getSubtarget<WebAssemblySubtarget>(F: 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(Elt: PtrVT);
81 }
82
83 std::optional<size_t> SwiftAsyncIdx;
84 for (auto [I, Param] : enumerate(First: Ty->params())) {
85 if (TargetFunc && TargetFunc->hasParamAttribute(ArgNo: I, Kind: Attribute::SwiftAsync))
86 SwiftAsyncIdx = Params.size();
87 computeLegalValueVTs(F: ContextFunc, TM, Ty: Param, ValueVTs&: Params);
88 }
89 // ABI contract: the async context must always be available in local 0.
90 if (SwiftAsyncIdx)
91 WebAssembly::moveToFront(Range&: Params, Idx: *SwiftAsyncIdx);
92 if (Ty->isVarArg())
93 Params.push_back(Elt: 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(BitWidth: 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(Kind: Attribute::SwiftError);
108 HasSwiftSelfArg |= Arg.hasAttribute(Kind: Attribute::SwiftSelf);
109 HasSwiftAsyncArg |= Arg.hasAttribute(Kind: Attribute::SwiftAsync);
110 }
111 if (!HasSwiftSelfArg)
112 Params.push_back(Elt: PtrVT);
113 if (!HasSwiftErrorArg)
114 Params.push_back(Elt: PtrVT);
115 if (TargetFunc->getCallingConv() == CallingConv::SwiftTail &&
116 !HasSwiftAsyncArg)
117 Params.insert(I: Params.begin(), Elt: PtrVT);
118 }
119}
120
121void llvm::valTypesFromMVTs(ArrayRef<MVT> In,
122 SmallVectorImpl<wasm::ValType> &Out) {
123 for (MVT Ty : In)
124 Out.push_back(Elt: WebAssembly::toValType(Type: Ty));
125}
126
127wasm::WasmSignature *
128llvm::signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl<MVT> &Results,
129 const SmallVectorImpl<MVT> &Params) {
130 auto Sig = Ctx.createWasmSignature();
131 valTypesFromMVTs(In: Results, Out&: Sig->Returns);
132 valTypesFromMVTs(In: Params, Out&: Sig->Params);
133 return Sig;
134}
135
136yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
137 const llvm::MachineFunction &MF, const llvm::WebAssemblyFunctionInfo &MFI)
138 : CFGStackified(MFI.isCFGStackified()) {
139 for (auto VT : MFI.getParams())
140 Params.push_back(x: EVT(VT).getEVTString());
141 for (auto VT : MFI.getResults())
142 Results.push_back(x: EVT(VT).getEVTString());
143}
144
145void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
146 MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, MFI&: *this);
147}
148
149void WebAssemblyFunctionInfo::initializeBaseYamlFields(
150 MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI) {
151 CFGStackified = YamlMFI.CFGStackified;
152 for (auto VT : YamlMFI.Params)
153 addParam(VT: WebAssembly::parseMVT(Type: VT.Value));
154 for (auto VT : YamlMFI.Results)
155 addResult(VT: WebAssembly::parseMVT(Type: VT.Value));
156}
157