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
11*5f8f34e4SAdrian 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 
462726b88cSDan Gohman void llvm::ComputeSignatureVTs(const Function &F, const TargetMachine &TM,
472726b88cSDan Gohman                                SmallVectorImpl<MVT> &Params,
482726b88cSDan Gohman                                SmallVectorImpl<MVT> &Results) {
492726b88cSDan Gohman   ComputeLegalValueVTs(F, TM, F.getReturnType(), Results);
502726b88cSDan Gohman 
512726b88cSDan Gohman   if (Results.size() > 1) {
522726b88cSDan Gohman     // WebAssembly currently can't lower returns of multiple values without
532726b88cSDan Gohman     // demoting to sret (see WebAssemblyTargetLowering::CanLowerReturn). So
542726b88cSDan Gohman     // replace multiple return values with a pointer parameter.
552726b88cSDan Gohman     Results.clear();
562726b88cSDan Gohman     Params.push_back(
572726b88cSDan Gohman         MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits()));
582726b88cSDan Gohman   }
592726b88cSDan Gohman 
602726b88cSDan Gohman   for (auto &Arg : F.args())
612726b88cSDan Gohman     ComputeLegalValueVTs(F, TM, Arg.getType(), Params);
622726b88cSDan Gohman }
63