1 //=- WebAssemblyMachineFunctionInfo.cpp - WebAssembly Machine Function Info -=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements WebAssembly-specific per-machine-function
14 //===----------------------------------------------------------------------===//
16 #include "WebAssemblyMachineFunctionInfo.h"
17 #include "WebAssemblyISelLowering.h"
18 #include "WebAssemblySubtarget.h"
19 #include "llvm/CodeGen/Analysis.h"
22 WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() {}
24 void WebAssemblyFunctionInfo::initWARegs() {
25 assert(WARegs.empty());
26 unsigned Reg = UnusedReg;
27 WARegs.resize(MF.getRegInfo().getNumVirtRegs(), Reg);
30 void llvm::ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
31 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
32 const DataLayout &DL(F.getParent()->getDataLayout());
33 const WebAssemblyTargetLowering &TLI =
34 *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
35 SmallVector<EVT, 4> VTs;
36 ComputeValueVTs(TLI, DL, Ty, VTs);
39 unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
40 MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
41 for (unsigned i = 0; i != NumRegs; ++i)
42 ValueVTs.push_back(RegisterVT);
46 void llvm::ComputeSignatureVTs(const FunctionType *Ty, const Function &F,
47 const TargetMachine &TM,
48 SmallVectorImpl<MVT> &Params,
49 SmallVectorImpl<MVT> &Results) {
50 ComputeLegalValueVTs(F, TM, Ty->getReturnType(), Results);
52 MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
53 if (Results.size() > 1) {
54 // WebAssembly currently can't lower returns of multiple values without
55 // demoting to sret (see WebAssemblyTargetLowering::CanLowerReturn). So
56 // replace multiple return values with a pointer parameter.
58 Params.push_back(PtrVT);
61 for (auto *Param : Ty->params())
62 ComputeLegalValueVTs(F, TM, Param, Params);
64 Params.push_back(PtrVT);
67 void llvm::ValTypesFromMVTs(const ArrayRef<MVT> &In,
68 SmallVectorImpl<wasm::ValType> &Out) {
70 Out.push_back(WebAssembly::toValType(Ty));
73 std::unique_ptr<wasm::WasmSignature>
74 llvm::SignatureFromMVTs(const SmallVectorImpl<MVT> &Results,
75 const SmallVectorImpl<MVT> &Params) {
76 auto Sig = make_unique<wasm::WasmSignature>();
77 ValTypesFromMVTs(Results, Sig->Returns);
78 ValTypesFromMVTs(Params, Sig->Params);