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/Target/TargetMachine.h"
22using namespace llvm;
23
24WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() = default; // anchor.
25
26MachineFunctionInfo *WebAssemblyFunctionInfo::clone(
27 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
28 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
29 const {
30 return DestMF.cloneInfo<WebAssemblyFunctionInfo>(Old: *this);
31}
32
33void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
34 assert(WARegs.empty());
35 unsigned Reg = WebAssembly::UnusedReg;
36 WARegs.resize(new_size: MRI.getNumVirtRegs(), x: Reg);
37}
38
39void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
40 LLVMContext &Ctx, const DataLayout &DL,
41 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
42 SmallVector<EVT, 4> VTs;
43 ComputeValueVTs(TLI, DL, Ty, ValueVTs&: VTs);
44
45 for (EVT VT : VTs) {
46 unsigned NumRegs = TLI.getNumRegisters(Context&: Ctx, VT);
47 MVT RegisterVT = TLI.getRegisterType(Context&: Ctx, VT);
48 for (unsigned I = 0; I != NumRegs; ++I)
49 ValueVTs.push_back(Elt: RegisterVT);
50 }
51}
52
53void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
54 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
55 const DataLayout &DL(F.getDataLayout());
56 const WebAssemblyTargetLowering &TLI =
57 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
58 computeLegalValueVTs(TLI, Ctx&: F.getContext(), DL, Ty, ValueVTs);
59}
60
61void llvm::computeSignatureVTs(const FunctionType *Ty,
62 const Function *TargetFunc,
63 const Function &ContextFunc,
64 const TargetMachine &TM,
65 SmallVectorImpl<MVT> &Params,
66 SmallVectorImpl<MVT> &Results) {
67 computeLegalValueVTs(F: ContextFunc, TM, Ty: Ty->getReturnType(), ValueVTs&: Results);
68
69 MVT PtrVT = MVT::getIntegerVT(BitWidth: TM.createDataLayout().getPointerSizeInBits());
70 if (!WebAssembly::canLowerReturn(
71 ResultSize: Results.size(),
72 Subtarget: &TM.getSubtarget<WebAssemblySubtarget>(F: ContextFunc))) {
73 // WebAssembly can't lower returns of multiple values without demoting to
74 // sret unless multivalue is enabled (see
75 // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
76 // values with a poitner parameter.
77 Results.clear();
78 Params.push_back(Elt: PtrVT);
79 }
80
81 for (auto *Param : Ty->params())
82 computeLegalValueVTs(F: ContextFunc, TM, Ty: Param, ValueVTs&: Params);
83 if (Ty->isVarArg())
84 Params.push_back(Elt: PtrVT);
85
86 // For swiftcc and swifttailcc, emit additional swiftself, swifterror, and
87 // (for swifttailcc) swiftasync parameters if there aren't. These additional
88 // parameters are also passed for caller. They are necessary to match callee
89 // and caller signature for indirect call.
90
91 if (TargetFunc && (TargetFunc->getCallingConv() == CallingConv::Swift ||
92 TargetFunc->getCallingConv() == CallingConv::SwiftTail)) {
93 MVT PtrVT = MVT::getIntegerVT(BitWidth: TM.createDataLayout().getPointerSizeInBits());
94 bool HasSwiftErrorArg = false;
95 bool HasSwiftSelfArg = false;
96 bool HasSwiftAsyncArg = false;
97 for (const auto &Arg : TargetFunc->args()) {
98 HasSwiftErrorArg |= Arg.hasAttribute(Kind: Attribute::SwiftError);
99 HasSwiftSelfArg |= Arg.hasAttribute(Kind: Attribute::SwiftSelf);
100 HasSwiftAsyncArg |= Arg.hasAttribute(Kind: Attribute::SwiftAsync);
101 }
102 if (!HasSwiftSelfArg)
103 Params.push_back(Elt: PtrVT);
104 if (!HasSwiftErrorArg)
105 Params.push_back(Elt: PtrVT);
106 if (TargetFunc->getCallingConv() == CallingConv::SwiftTail &&
107 !HasSwiftAsyncArg)
108 Params.push_back(Elt: PtrVT);
109 }
110}
111
112void llvm::valTypesFromMVTs(ArrayRef<MVT> In,
113 SmallVectorImpl<wasm::ValType> &Out) {
114 for (MVT Ty : In)
115 Out.push_back(Elt: WebAssembly::toValType(Type: Ty));
116}
117
118wasm::WasmSignature *
119llvm::signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl<MVT> &Results,
120 const SmallVectorImpl<MVT> &Params) {
121 auto Sig = Ctx.createWasmSignature();
122 valTypesFromMVTs(In: Results, Out&: Sig->Returns);
123 valTypesFromMVTs(In: Params, Out&: Sig->Params);
124 return Sig;
125}
126
127yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
128 const llvm::MachineFunction &MF, const llvm::WebAssemblyFunctionInfo &MFI)
129 : CFGStackified(MFI.isCFGStackified()) {
130 for (auto VT : MFI.getParams())
131 Params.push_back(x: EVT(VT).getEVTString());
132 for (auto VT : MFI.getResults())
133 Results.push_back(x: EVT(VT).getEVTString());
134}
135
136void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
137 MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, MFI&: *this);
138}
139
140void WebAssemblyFunctionInfo::initializeBaseYamlFields(
141 MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI) {
142 CFGStackified = YamlMFI.CFGStackified;
143 for (auto VT : YamlMFI.Params)
144 addParam(VT: WebAssembly::parseMVT(Type: VT.Value));
145 for (auto VT : YamlMFI.Results)
146 addResult(VT: WebAssembly::parseMVT(Type: VT.Value));
147}
148