110e730a2SDan Gohman //=- WebAssemblyMachineFunctionInfo.cpp - WebAssembly Machine Function Info -=//
210e730a2SDan Gohman //
310e730a2SDan Gohman //                     The LLVM Compiler Infrastructure
410e730a2SDan Gohman //
510e730a2SDan Gohman // This file is distributed under the University of Illinois Open Source
610e730a2SDan Gohman // License. See LICENSE.TXT for details.
710e730a2SDan Gohman //
810e730a2SDan Gohman //===----------------------------------------------------------------------===//
910e730a2SDan Gohman ///
1010e730a2SDan Gohman /// \file
115f8f34e4SAdrian Prantl /// This file implements WebAssembly-specific per-machine-function
1210e730a2SDan Gohman /// information.
1310e730a2SDan Gohman ///
1410e730a2SDan Gohman //===----------------------------------------------------------------------===//
1510e730a2SDan Gohman 
1610e730a2SDan Gohman #include "WebAssemblyMachineFunctionInfo.h"
172726b88cSDan Gohman #include "WebAssemblyISelLowering.h"
182726b88cSDan Gohman #include "WebAssemblySubtarget.h"
192726b88cSDan Gohman #include "llvm/CodeGen/Analysis.h"
2010e730a2SDan Gohman using namespace llvm;
2110e730a2SDan Gohman 
2210e730a2SDan Gohman WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() {}
23058fce54SDan Gohman 
24058fce54SDan Gohman void WebAssemblyFunctionInfo::initWARegs() {
25058fce54SDan Gohman   assert(WARegs.empty());
26058fce54SDan Gohman   unsigned Reg = UnusedReg;
27058fce54SDan Gohman   WARegs.resize(MF.getRegInfo().getNumVirtRegs(), Reg);
28058fce54SDan Gohman }
292726b88cSDan Gohman 
302726b88cSDan Gohman void llvm::ComputeLegalValueVTs(const Function &F, const TargetMachine &TM,
312726b88cSDan Gohman                                 Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
322726b88cSDan Gohman   const DataLayout &DL(F.getParent()->getDataLayout());
332726b88cSDan Gohman   const WebAssemblyTargetLowering &TLI =
342726b88cSDan Gohman       *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
352726b88cSDan Gohman   SmallVector<EVT, 4> VTs;
362726b88cSDan Gohman   ComputeValueVTs(TLI, DL, Ty, VTs);
372726b88cSDan Gohman 
382726b88cSDan Gohman   for (EVT VT : VTs) {
392726b88cSDan Gohman     unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
402726b88cSDan Gohman     MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
412726b88cSDan Gohman     for (unsigned i = 0; i != NumRegs; ++i)
422726b88cSDan Gohman       ValueVTs.push_back(RegisterVT);
432726b88cSDan Gohman   }
442726b88cSDan Gohman }
452726b88cSDan Gohman 
4677a7a380SDerek Schuff void llvm::ComputeSignatureVTs(const FunctionType *Ty, const Function &F,
4777a7a380SDerek Schuff                                const TargetMachine &TM,
482726b88cSDan Gohman                                SmallVectorImpl<MVT> &Params,
492726b88cSDan Gohman                                SmallVectorImpl<MVT> &Results) {
5077a7a380SDerek Schuff   ComputeLegalValueVTs(F, TM, Ty->getReturnType(), Results);
512726b88cSDan Gohman 
5277a7a380SDerek Schuff   MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
532726b88cSDan Gohman   if (Results.size() > 1) {
542726b88cSDan Gohman     // WebAssembly currently can't lower returns of multiple values without
552726b88cSDan Gohman     // demoting to sret (see WebAssemblyTargetLowering::CanLowerReturn). So
562726b88cSDan Gohman     // replace multiple return values with a pointer parameter.
572726b88cSDan Gohman     Results.clear();
5877a7a380SDerek Schuff     Params.push_back(PtrVT);
592726b88cSDan Gohman   }
602726b88cSDan Gohman 
6177a7a380SDerek Schuff   for (auto *Param : Ty->params())
6277a7a380SDerek Schuff     ComputeLegalValueVTs(F, TM, Param, Params);
6377a7a380SDerek Schuff   if (Ty->isVarArg())
6477a7a380SDerek Schuff     Params.push_back(PtrVT);
6577a7a380SDerek Schuff }
6677a7a380SDerek Schuff 
67*49482f82SWouter van Oortmerssen void llvm::ValTypesFromMVTs(const ArrayRef<MVT> &In,
68*49482f82SWouter van Oortmerssen                             SmallVectorImpl<wasm::ValType> &Out) {
69*49482f82SWouter van Oortmerssen   for (MVT Ty : In)
70*49482f82SWouter van Oortmerssen     Out.push_back(WebAssembly::toValType(Ty));
71*49482f82SWouter van Oortmerssen }
72*49482f82SWouter van Oortmerssen 
7377a7a380SDerek Schuff std::unique_ptr<wasm::WasmSignature>
7477a7a380SDerek Schuff llvm::SignatureFromMVTs(const SmallVectorImpl<MVT> &Results,
7577a7a380SDerek Schuff                         const SmallVectorImpl<MVT> &Params) {
7677a7a380SDerek Schuff   auto Sig = make_unique<wasm::WasmSignature>();
77*49482f82SWouter van Oortmerssen   ValTypesFromMVTs(Results, Sig->Returns);
78*49482f82SWouter van Oortmerssen   ValTypesFromMVTs(Params, Sig->Params);
7977a7a380SDerek Schuff   return Sig;
802726b88cSDan Gohman }
81