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 "WebAssemblyISelLowering.h" 17 #include "WebAssemblySubtarget.h" 18 #include "llvm/CodeGen/Analysis.h" 19 using namespace llvm; 20 21 WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() = default; // anchor. 22 23 void WebAssemblyFunctionInfo::initWARegs() { 24 assert(WARegs.empty()); 25 unsigned Reg = UnusedReg; 26 WARegs.resize(MF.getRegInfo().getNumVirtRegs(), Reg); 27 } 28 29 void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM, 30 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) { 31 const DataLayout &DL(F.getParent()->getDataLayout()); 32 const WebAssemblyTargetLowering &TLI = 33 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering(); 34 SmallVector<EVT, 4> VTs; 35 ComputeValueVTs(TLI, DL, Ty, VTs); 36 37 for (EVT VT : VTs) { 38 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT); 39 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT); 40 for (unsigned I = 0; I != NumRegs; ++I) 41 ValueVTs.push_back(RegisterVT); 42 } 43 } 44 45 void llvm::computeSignatureVTs(const FunctionType *Ty, const Function &F, 46 const TargetMachine &TM, 47 SmallVectorImpl<MVT> &Params, 48 SmallVectorImpl<MVT> &Results) { 49 computeLegalValueVTs(F, TM, Ty->getReturnType(), Results); 50 51 MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits()); 52 if (Results.size() > 1) { 53 // WebAssembly currently can't lower returns of multiple values without 54 // demoting to sret (see WebAssemblyTargetLowering::CanLowerReturn). So 55 // replace multiple return values with a pointer parameter. 56 Results.clear(); 57 Params.push_back(PtrVT); 58 } 59 60 for (auto *Param : Ty->params()) 61 computeLegalValueVTs(F, TM, Param, Params); 62 if (Ty->isVarArg()) 63 Params.push_back(PtrVT); 64 } 65 66 void llvm::valTypesFromMVTs(const ArrayRef<MVT> &In, 67 SmallVectorImpl<wasm::ValType> &Out) { 68 for (MVT Ty : In) 69 Out.push_back(WebAssembly::toValType(Ty)); 70 } 71 72 std::unique_ptr<wasm::WasmSignature> 73 llvm::signatureFromMVTs(const SmallVectorImpl<MVT> &Results, 74 const SmallVectorImpl<MVT> &Params) { 75 auto Sig = make_unique<wasm::WasmSignature>(); 76 valTypesFromMVTs(Results, Sig->Returns); 77 valTypesFromMVTs(Params, Sig->Params); 78 return Sig; 79 } 80 81 yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo( 82 const llvm::WebAssemblyFunctionInfo &MFI) 83 : CFGStackified(MFI.isCFGStackified()) {} 84 85 void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) { 86 MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, *this); 87 } 88 89 void WebAssemblyFunctionInfo::initializeBaseYamlFields( 90 const yaml::WebAssemblyFunctionInfo &YamlMFI) { 91 CFGStackified = YamlMFI.CFGStackified; 92 } 93