10b57cec5SDimitry Andric //=- WebAssemblyMachineFunctionInfo.cpp - WebAssembly Machine Function Info -=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file implements WebAssembly-specific per-machine-function
110b57cec5SDimitry Andric /// information.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
16fe6060f1SDimitry Andric #include "MCTargetDesc/WebAssemblyInstPrinter.h"
17fe6060f1SDimitry Andric #include "Utils/WebAssemblyTypeUtilities.h"
180b57cec5SDimitry Andric #include "WebAssemblyISelLowering.h"
190b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
20*0fca6ea1SDimitry Andric #include "WebAssemblyUtilities.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
22fe6060f1SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h"
235ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() = default; // anchor.
270b57cec5SDimitry Andric
clone(BumpPtrAllocator & Allocator,MachineFunction & DestMF,const DenseMap<MachineBasicBlock *,MachineBasicBlock * > & Src2DstMBB) const2881ad6265SDimitry Andric MachineFunctionInfo *WebAssemblyFunctionInfo::clone(
2981ad6265SDimitry Andric BumpPtrAllocator &Allocator, MachineFunction &DestMF,
3081ad6265SDimitry Andric const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
3181ad6265SDimitry Andric const {
32bdd1243dSDimitry Andric // TODO: Implement cloning for WasmEHFuncInfo. This will have invalid block
33bdd1243dSDimitry Andric // references.
34bdd1243dSDimitry Andric return DestMF.cloneInfo<WebAssemblyFunctionInfo>(*this);
3581ad6265SDimitry Andric }
3681ad6265SDimitry Andric
initWARegs(MachineRegisterInfo & MRI)375ffd83dbSDimitry Andric void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
380b57cec5SDimitry Andric assert(WARegs.empty());
395f757f3fSDimitry Andric unsigned Reg = WebAssembly::UnusedReg;
405ffd83dbSDimitry Andric WARegs.resize(MRI.getNumVirtRegs(), Reg);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
computeLegalValueVTs(const WebAssemblyTargetLowering & TLI,LLVMContext & Ctx,const DataLayout & DL,Type * Ty,SmallVectorImpl<MVT> & ValueVTs)431fd87a68SDimitry Andric void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
441fd87a68SDimitry Andric LLVMContext &Ctx, const DataLayout &DL,
451fd87a68SDimitry Andric Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
461fd87a68SDimitry Andric SmallVector<EVT, 4> VTs;
471fd87a68SDimitry Andric ComputeValueVTs(TLI, DL, Ty, VTs);
481fd87a68SDimitry Andric
491fd87a68SDimitry Andric for (EVT VT : VTs) {
501fd87a68SDimitry Andric unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
511fd87a68SDimitry Andric MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
521fd87a68SDimitry Andric for (unsigned I = 0; I != NumRegs; ++I)
531fd87a68SDimitry Andric ValueVTs.push_back(RegisterVT);
541fd87a68SDimitry Andric }
551fd87a68SDimitry Andric }
561fd87a68SDimitry Andric
computeLegalValueVTs(const Function & F,const TargetMachine & TM,Type * Ty,SmallVectorImpl<MVT> & ValueVTs)570b57cec5SDimitry Andric void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
580b57cec5SDimitry Andric Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
59*0fca6ea1SDimitry Andric const DataLayout &DL(F.getDataLayout());
600b57cec5SDimitry Andric const WebAssemblyTargetLowering &TLI =
610b57cec5SDimitry Andric *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
621fd87a68SDimitry Andric computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
computeSignatureVTs(const FunctionType * Ty,const Function * TargetFunc,const Function & ContextFunc,const TargetMachine & TM,SmallVectorImpl<MVT> & Params,SmallVectorImpl<MVT> & Results)655ffd83dbSDimitry Andric void llvm::computeSignatureVTs(const FunctionType *Ty,
665ffd83dbSDimitry Andric const Function *TargetFunc,
675ffd83dbSDimitry Andric const Function &ContextFunc,
680b57cec5SDimitry Andric const TargetMachine &TM,
690b57cec5SDimitry Andric SmallVectorImpl<MVT> &Params,
700b57cec5SDimitry Andric SmallVectorImpl<MVT> &Results) {
715ffd83dbSDimitry Andric computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
74*0fca6ea1SDimitry Andric if (!WebAssembly::canLowerReturn(
75*0fca6ea1SDimitry Andric Results.size(),
76*0fca6ea1SDimitry Andric &TM.getSubtarget<WebAssemblySubtarget>(ContextFunc))) {
778bcb0991SDimitry Andric // WebAssembly can't lower returns of multiple values without demoting to
788bcb0991SDimitry Andric // sret unless multivalue is enabled (see
798bcb0991SDimitry Andric // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
808bcb0991SDimitry Andric // values with a poitner parameter.
810b57cec5SDimitry Andric Results.clear();
820b57cec5SDimitry Andric Params.push_back(PtrVT);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric for (auto *Param : Ty->params())
865ffd83dbSDimitry Andric computeLegalValueVTs(ContextFunc, TM, Param, Params);
870b57cec5SDimitry Andric if (Ty->isVarArg())
880b57cec5SDimitry Andric Params.push_back(PtrVT);
895ffd83dbSDimitry Andric
905ffd83dbSDimitry Andric // For swiftcc, emit additional swiftself and swifterror parameters
915ffd83dbSDimitry Andric // if there aren't. These additional parameters are also passed for caller.
925ffd83dbSDimitry Andric // They are necessary to match callee and caller signature for indirect
935ffd83dbSDimitry Andric // call.
945ffd83dbSDimitry Andric
955ffd83dbSDimitry Andric if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
965ffd83dbSDimitry Andric MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
975ffd83dbSDimitry Andric bool HasSwiftErrorArg = false;
985ffd83dbSDimitry Andric bool HasSwiftSelfArg = false;
995ffd83dbSDimitry Andric for (const auto &Arg : TargetFunc->args()) {
1005ffd83dbSDimitry Andric HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
1015ffd83dbSDimitry Andric HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
1025ffd83dbSDimitry Andric }
1035ffd83dbSDimitry Andric if (!HasSwiftErrorArg)
1045ffd83dbSDimitry Andric Params.push_back(PtrVT);
1055ffd83dbSDimitry Andric if (!HasSwiftSelfArg)
1065ffd83dbSDimitry Andric Params.push_back(PtrVT);
1075ffd83dbSDimitry Andric }
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric
valTypesFromMVTs(ArrayRef<MVT> In,SmallVectorImpl<wasm::ValType> & Out)110*0fca6ea1SDimitry Andric void llvm::valTypesFromMVTs(ArrayRef<MVT> In,
1110b57cec5SDimitry Andric SmallVectorImpl<wasm::ValType> &Out) {
1120b57cec5SDimitry Andric for (MVT Ty : In)
1130b57cec5SDimitry Andric Out.push_back(WebAssembly::toValType(Ty));
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
116*0fca6ea1SDimitry Andric wasm::WasmSignature *
signatureFromMVTs(MCContext & Ctx,const SmallVectorImpl<MVT> & Results,const SmallVectorImpl<MVT> & Params)117*0fca6ea1SDimitry Andric llvm::signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl<MVT> &Results,
1180b57cec5SDimitry Andric const SmallVectorImpl<MVT> &Params) {
119*0fca6ea1SDimitry Andric auto Sig = Ctx.createWasmSignature();
1200b57cec5SDimitry Andric valTypesFromMVTs(Results, Sig->Returns);
1210b57cec5SDimitry Andric valTypesFromMVTs(Params, Sig->Params);
1220b57cec5SDimitry Andric return Sig;
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric
WebAssemblyFunctionInfo(const llvm::MachineFunction & MF,const llvm::WebAssemblyFunctionInfo & MFI)1250b57cec5SDimitry Andric yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
126bdd1243dSDimitry Andric const llvm::MachineFunction &MF, const llvm::WebAssemblyFunctionInfo &MFI)
127fe6060f1SDimitry Andric : CFGStackified(MFI.isCFGStackified()) {
128fe6060f1SDimitry Andric for (auto VT : MFI.getParams())
129fe6060f1SDimitry Andric Params.push_back(EVT(VT).getEVTString());
130fe6060f1SDimitry Andric for (auto VT : MFI.getResults())
131fe6060f1SDimitry Andric Results.push_back(EVT(VT).getEVTString());
132fe6060f1SDimitry Andric
133fe6060f1SDimitry Andric // MFI.getWasmEHFuncInfo() is non-null only for functions with the
134fe6060f1SDimitry Andric // personality function.
135bdd1243dSDimitry Andric
136bdd1243dSDimitry Andric if (auto *EHInfo = MF.getWasmEHFuncInfo()) {
137fe6060f1SDimitry Andric // SrcToUnwindDest can contain stale mappings in case BBs are removed in
138fe6060f1SDimitry Andric // optimizations, in case, for example, they are unreachable. We should not
139fe6060f1SDimitry Andric // include their info.
140fe6060f1SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> MBBs;
141fe6060f1SDimitry Andric for (const auto &MBB : MF)
142fe6060f1SDimitry Andric MBBs.insert(&MBB);
143fe6060f1SDimitry Andric for (auto KV : EHInfo->SrcToUnwindDest) {
144fe6060f1SDimitry Andric auto *SrcBB = KV.first.get<MachineBasicBlock *>();
145fe6060f1SDimitry Andric auto *DestBB = KV.second.get<MachineBasicBlock *>();
146fe6060f1SDimitry Andric if (MBBs.count(SrcBB) && MBBs.count(DestBB))
147fe6060f1SDimitry Andric SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
148fe6060f1SDimitry Andric }
149fe6060f1SDimitry Andric }
150fe6060f1SDimitry Andric }
1510b57cec5SDimitry Andric
mappingImpl(yaml::IO & YamlIO)1520b57cec5SDimitry Andric void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
1530b57cec5SDimitry Andric MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, *this);
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric
initializeBaseYamlFields(MachineFunction & MF,const yaml::WebAssemblyFunctionInfo & YamlMFI)1560b57cec5SDimitry Andric void WebAssemblyFunctionInfo::initializeBaseYamlFields(
157bdd1243dSDimitry Andric MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI) {
1580b57cec5SDimitry Andric CFGStackified = YamlMFI.CFGStackified;
159fe6060f1SDimitry Andric for (auto VT : YamlMFI.Params)
160fe6060f1SDimitry Andric addParam(WebAssembly::parseMVT(VT.Value));
161fe6060f1SDimitry Andric for (auto VT : YamlMFI.Results)
162fe6060f1SDimitry Andric addResult(WebAssembly::parseMVT(VT.Value));
163bdd1243dSDimitry Andric
164bdd1243dSDimitry Andric // FIXME: WasmEHInfo is defined in the MachineFunction, but serialized
165bdd1243dSDimitry Andric // here. Either WasmEHInfo should be moved out of MachineFunction, or the
166bdd1243dSDimitry Andric // serialization handling should be moved to MachineFunction.
167bdd1243dSDimitry Andric if (WasmEHFuncInfo *WasmEHInfo = MF.getWasmEHFuncInfo()) {
168fe6060f1SDimitry Andric for (auto KV : YamlMFI.SrcToUnwindDest)
169bdd1243dSDimitry Andric WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
170bdd1243dSDimitry Andric MF.getBlockNumbered(KV.second));
171fe6060f1SDimitry Andric }
1720b57cec5SDimitry Andric }
173