xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp (revision 83e8c2317846dd0c889fb784a11f94f98b982062)
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  void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
28    assert(WARegs.empty());
29    unsigned Reg = UnusedReg;
30    WARegs.resize(MRI.getNumVirtRegs(), Reg);
31  }
32  
33  void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
34                                  LLVMContext &Ctx, const DataLayout &DL,
35                                  Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
36    SmallVector<EVT, 4> VTs;
37    ComputeValueVTs(TLI, DL, Ty, VTs);
38  
39    for (EVT VT : VTs) {
40      unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
41      MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
42      for (unsigned I = 0; I != NumRegs; ++I)
43        ValueVTs.push_back(RegisterVT);
44    }
45  }
46  
47  void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
48                                  Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
49    const DataLayout &DL(F.getParent()->getDataLayout());
50    const WebAssemblyTargetLowering &TLI =
51        *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
52    computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
53  }
54  
55  void llvm::computeSignatureVTs(const FunctionType *Ty,
56                                 const Function *TargetFunc,
57                                 const Function &ContextFunc,
58                                 const TargetMachine &TM,
59                                 SmallVectorImpl<MVT> &Params,
60                                 SmallVectorImpl<MVT> &Results) {
61    computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
62  
63    MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
64    if (Results.size() > 1 &&
65        !TM.getSubtarget<WebAssemblySubtarget>(ContextFunc).hasMultivalue()) {
66      // WebAssembly can't lower returns of multiple values without demoting to
67      // sret unless multivalue is enabled (see
68      // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
69      // values with a poitner parameter.
70      Results.clear();
71      Params.push_back(PtrVT);
72    }
73  
74    for (auto *Param : Ty->params())
75      computeLegalValueVTs(ContextFunc, TM, Param, Params);
76    if (Ty->isVarArg())
77      Params.push_back(PtrVT);
78  
79    // For swiftcc, emit additional swiftself and swifterror parameters
80    // if there aren't. These additional parameters are also passed for caller.
81    // They are necessary to match callee and caller signature for indirect
82    // call.
83  
84    if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
85      MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
86      bool HasSwiftErrorArg = false;
87      bool HasSwiftSelfArg = false;
88      for (const auto &Arg : TargetFunc->args()) {
89        HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
90        HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
91      }
92      if (!HasSwiftErrorArg)
93        Params.push_back(PtrVT);
94      if (!HasSwiftSelfArg)
95        Params.push_back(PtrVT);
96    }
97  }
98  
99  void llvm::valTypesFromMVTs(const ArrayRef<MVT> &In,
100                              SmallVectorImpl<wasm::ValType> &Out) {
101    for (MVT Ty : In)
102      Out.push_back(WebAssembly::toValType(Ty));
103  }
104  
105  std::unique_ptr<wasm::WasmSignature>
106  llvm::signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
107                          const SmallVectorImpl<MVT> &Params) {
108    auto Sig = std::make_unique<wasm::WasmSignature>();
109    valTypesFromMVTs(Results, Sig->Returns);
110    valTypesFromMVTs(Params, Sig->Params);
111    return Sig;
112  }
113  
114  yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
115      const llvm::WebAssemblyFunctionInfo &MFI)
116      : CFGStackified(MFI.isCFGStackified()) {
117    auto *EHInfo = MFI.getWasmEHFuncInfo();
118    const llvm::MachineFunction &MF = MFI.getMachineFunction();
119  
120    for (auto VT : MFI.getParams())
121      Params.push_back(EVT(VT).getEVTString());
122    for (auto VT : MFI.getResults())
123      Results.push_back(EVT(VT).getEVTString());
124  
125    //  MFI.getWasmEHFuncInfo() is non-null only for functions with the
126    //  personality function.
127    if (EHInfo) {
128      // SrcToUnwindDest can contain stale mappings in case BBs are removed in
129      // optimizations, in case, for example, they are unreachable. We should not
130      // include their info.
131      SmallPtrSet<const MachineBasicBlock *, 16> MBBs;
132      for (const auto &MBB : MF)
133        MBBs.insert(&MBB);
134      for (auto KV : EHInfo->SrcToUnwindDest) {
135        auto *SrcBB = KV.first.get<MachineBasicBlock *>();
136        auto *DestBB = KV.second.get<MachineBasicBlock *>();
137        if (MBBs.count(SrcBB) && MBBs.count(DestBB))
138          SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
139      }
140    }
141  }
142  
143  void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
144    MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, *this);
145  }
146  
147  void WebAssemblyFunctionInfo::initializeBaseYamlFields(
148      const yaml::WebAssemblyFunctionInfo &YamlMFI) {
149    CFGStackified = YamlMFI.CFGStackified;
150    for (auto VT : YamlMFI.Params)
151      addParam(WebAssembly::parseMVT(VT.Value));
152    for (auto VT : YamlMFI.Results)
153      addResult(WebAssembly::parseMVT(VT.Value));
154    if (WasmEHInfo) {
155      for (auto KV : YamlMFI.SrcToUnwindDest)
156        WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
157                                  MF.getBlockNumbered(KV.second));
158    }
159  }
160