xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 "MCTargetDesc/WebAssemblyInstPrinter.h"
17 #include "Utils/WebAssemblyTypeUtilities.h"
18 #include "WebAssemblyISelLowering.h"
19 #include "WebAssemblySubtarget.h"
20 #include "llvm/CodeGen/Analysis.h"
21 #include "llvm/CodeGen/WasmEHFuncInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 using namespace llvm;
24 
25 WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() = default; // anchor.
26 
27 MachineFunctionInfo *WebAssemblyFunctionInfo::clone(
28     BumpPtrAllocator &Allocator, MachineFunction &DestMF,
29     const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
30     const {
31   WebAssemblyFunctionInfo *Clone =
32       DestMF.cloneInfo<WebAssemblyFunctionInfo>(*this);
33   Clone->MF = &DestMF;
34   return Clone;
35 }
36 
37 void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
38   assert(WARegs.empty());
39   unsigned Reg = UnusedReg;
40   WARegs.resize(MRI.getNumVirtRegs(), Reg);
41 }
42 
43 void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
44                                 LLVMContext &Ctx, const DataLayout &DL,
45                                 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
46   SmallVector<EVT, 4> VTs;
47   ComputeValueVTs(TLI, DL, Ty, VTs);
48 
49   for (EVT VT : VTs) {
50     unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
51     MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
52     for (unsigned I = 0; I != NumRegs; ++I)
53       ValueVTs.push_back(RegisterVT);
54   }
55 }
56 
57 void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
58                                 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
59   const DataLayout &DL(F.getParent()->getDataLayout());
60   const WebAssemblyTargetLowering &TLI =
61       *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
62   computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
63 }
64 
65 void llvm::computeSignatureVTs(const FunctionType *Ty,
66                                const Function *TargetFunc,
67                                const Function &ContextFunc,
68                                const TargetMachine &TM,
69                                SmallVectorImpl<MVT> &Params,
70                                SmallVectorImpl<MVT> &Results) {
71   computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
72 
73   MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
74   if (Results.size() > 1 &&
75       !TM.getSubtarget<WebAssemblySubtarget>(ContextFunc).hasMultivalue()) {
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, emit additional swiftself and swifterror parameters
90   // if there aren't. These additional parameters are also passed for caller.
91   // They are necessary to match callee and caller signature for indirect
92   // call.
93 
94   if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
95     MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
96     bool HasSwiftErrorArg = false;
97     bool HasSwiftSelfArg = false;
98     for (const auto &Arg : TargetFunc->args()) {
99       HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
100       HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
101     }
102     if (!HasSwiftErrorArg)
103       Params.push_back(PtrVT);
104     if (!HasSwiftSelfArg)
105       Params.push_back(PtrVT);
106   }
107 }
108 
109 void llvm::valTypesFromMVTs(const ArrayRef<MVT> &In,
110                             SmallVectorImpl<wasm::ValType> &Out) {
111   for (MVT Ty : In)
112     Out.push_back(WebAssembly::toValType(Ty));
113 }
114 
115 std::unique_ptr<wasm::WasmSignature>
116 llvm::signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
117                         const SmallVectorImpl<MVT> &Params) {
118   auto Sig = std::make_unique<wasm::WasmSignature>();
119   valTypesFromMVTs(Results, Sig->Returns);
120   valTypesFromMVTs(Params, Sig->Params);
121   return Sig;
122 }
123 
124 yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
125     const llvm::WebAssemblyFunctionInfo &MFI)
126     : CFGStackified(MFI.isCFGStackified()) {
127   auto *EHInfo = MFI.getWasmEHFuncInfo();
128   const llvm::MachineFunction &MF = MFI.getMachineFunction();
129 
130   for (auto VT : MFI.getParams())
131     Params.push_back(EVT(VT).getEVTString());
132   for (auto VT : MFI.getResults())
133     Results.push_back(EVT(VT).getEVTString());
134 
135   //  MFI.getWasmEHFuncInfo() is non-null only for functions with the
136   //  personality function.
137   if (EHInfo) {
138     // SrcToUnwindDest can contain stale mappings in case BBs are removed in
139     // optimizations, in case, for example, they are unreachable. We should not
140     // include their info.
141     SmallPtrSet<const MachineBasicBlock *, 16> MBBs;
142     for (const auto &MBB : MF)
143       MBBs.insert(&MBB);
144     for (auto KV : EHInfo->SrcToUnwindDest) {
145       auto *SrcBB = KV.first.get<MachineBasicBlock *>();
146       auto *DestBB = KV.second.get<MachineBasicBlock *>();
147       if (MBBs.count(SrcBB) && MBBs.count(DestBB))
148         SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
149     }
150   }
151 }
152 
153 void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
154   MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, *this);
155 }
156 
157 void WebAssemblyFunctionInfo::initializeBaseYamlFields(
158     const yaml::WebAssemblyFunctionInfo &YamlMFI) {
159   CFGStackified = YamlMFI.CFGStackified;
160   for (auto VT : YamlMFI.Params)
161     addParam(WebAssembly::parseMVT(VT.Value));
162   for (auto VT : YamlMFI.Results)
163     addResult(WebAssembly::parseMVT(VT.Value));
164   if (WasmEHInfo) {
165     for (auto KV : YamlMFI.SrcToUnwindDest)
166       WasmEHInfo->setUnwindDest(MF->getBlockNumbered(KV.first),
167                                 MF->getBlockNumbered(KV.second));
168   }
169 }
170