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/CodeGen/WasmEHFuncInfo.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 // TODO: Implement cloning for WasmEHFuncInfo. This will have invalid block
32 // references.
33 return DestMF.cloneInfo<WebAssemblyFunctionInfo>(Old: *this);
34}
35
36void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
37 assert(WARegs.empty());
38 unsigned Reg = WebAssembly::UnusedReg;
39 WARegs.resize(new_size: MRI.getNumVirtRegs(), x: Reg);
40}
41
42void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
43 LLVMContext &Ctx, const DataLayout &DL,
44 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
45 SmallVector<EVT, 4> VTs;
46 ComputeValueVTs(TLI, DL, Ty, ValueVTs&: VTs);
47
48 for (EVT VT : VTs) {
49 unsigned NumRegs = TLI.getNumRegisters(Context&: Ctx, VT);
50 MVT RegisterVT = TLI.getRegisterType(Context&: Ctx, VT);
51 for (unsigned I = 0; I != NumRegs; ++I)
52 ValueVTs.push_back(Elt: RegisterVT);
53 }
54}
55
56void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
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, Ctx&: F.getContext(), DL, Ty, ValueVTs);
62}
63
64void llvm::computeSignatureVTs(const FunctionType *Ty,
65 const Function *TargetFunc,
66 const Function &ContextFunc,
67 const TargetMachine &TM,
68 SmallVectorImpl<MVT> &Params,
69 SmallVectorImpl<MVT> &Results) {
70 computeLegalValueVTs(F: ContextFunc, TM, Ty: Ty->getReturnType(), ValueVTs&: Results);
71
72 MVT PtrVT = MVT::getIntegerVT(BitWidth: TM.createDataLayout().getPointerSizeInBits());
73 if (!WebAssembly::canLowerReturn(
74 ResultSize: Results.size(),
75 Subtarget: &TM.getSubtarget<WebAssemblySubtarget>(F: 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(Elt: PtrVT);
82 }
83
84 for (auto *Param : Ty->params())
85 computeLegalValueVTs(F: ContextFunc, TM, Ty: Param, ValueVTs&: Params);
86 if (Ty->isVarArg())
87 Params.push_back(Elt: 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)) {
96 MVT PtrVT = MVT::getIntegerVT(BitWidth: TM.createDataLayout().getPointerSizeInBits());
97 bool HasSwiftErrorArg = false;
98 bool HasSwiftSelfArg = false;
99 bool HasSwiftAsyncArg = false;
100 for (const auto &Arg : TargetFunc->args()) {
101 HasSwiftErrorArg |= Arg.hasAttribute(Kind: Attribute::SwiftError);
102 HasSwiftSelfArg |= Arg.hasAttribute(Kind: Attribute::SwiftSelf);
103 HasSwiftAsyncArg |= Arg.hasAttribute(Kind: Attribute::SwiftAsync);
104 }
105 if (!HasSwiftSelfArg)
106 Params.push_back(Elt: PtrVT);
107 if (!HasSwiftErrorArg)
108 Params.push_back(Elt: PtrVT);
109 if (TargetFunc->getCallingConv() == CallingConv::SwiftTail &&
110 !HasSwiftAsyncArg)
111 Params.push_back(Elt: PtrVT);
112 }
113}
114
115void llvm::valTypesFromMVTs(ArrayRef<MVT> In,
116 SmallVectorImpl<wasm::ValType> &Out) {
117 for (MVT Ty : In)
118 Out.push_back(Elt: WebAssembly::toValType(Type: Ty));
119}
120
121wasm::WasmSignature *
122llvm::signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl<MVT> &Results,
123 const SmallVectorImpl<MVT> &Params) {
124 auto Sig = Ctx.createWasmSignature();
125 valTypesFromMVTs(In: Results, Out&: Sig->Returns);
126 valTypesFromMVTs(In: Params, Out&: Sig->Params);
127 return Sig;
128}
129
130yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
131 const llvm::MachineFunction &MF, const llvm::WebAssemblyFunctionInfo &MFI)
132 : CFGStackified(MFI.isCFGStackified()) {
133 for (auto VT : MFI.getParams())
134 Params.push_back(x: EVT(VT).getEVTString());
135 for (auto VT : MFI.getResults())
136 Results.push_back(x: 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(Ptr: &MBB);
148 for (auto KV : EHInfo->SrcToUnwindDest) {
149 auto *SrcBB = cast<MachineBasicBlock *>(Val&: KV.first);
150 auto *DestBB = cast<MachineBasicBlock *>(Val&: KV.second);
151 if (MBBs.count(Ptr: SrcBB) && MBBs.count(Ptr: DestBB))
152 SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
153 }
154 }
155}
156
157void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
158 MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, MFI&: *this);
159}
160
161void WebAssemblyFunctionInfo::initializeBaseYamlFields(
162 MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI) {
163 CFGStackified = YamlMFI.CFGStackified;
164 for (auto VT : YamlMFI.Params)
165 addParam(VT: WebAssembly::parseMVT(Type: VT.Value));
166 for (auto VT : YamlMFI.Results)
167 addResult(VT: WebAssembly::parseMVT(Type: VT.Value));
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(MBB: MF.getBlockNumbered(N: KV.first),
175 Dest: MF.getBlockNumbered(N: KV.second));
176 }
177}
178