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"
200b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
21fe6060f1SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h"
225ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo() = default; // anchor.
260b57cec5SDimitry Andric
clone(BumpPtrAllocator & Allocator,MachineFunction & DestMF,const DenseMap<MachineBasicBlock *,MachineBasicBlock * > & Src2DstMBB) const2781ad6265SDimitry Andric MachineFunctionInfo *WebAssemblyFunctionInfo::clone(
2881ad6265SDimitry Andric BumpPtrAllocator &Allocator, MachineFunction &DestMF,
2981ad6265SDimitry Andric const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
3081ad6265SDimitry Andric const {
31bdd1243dSDimitry Andric // TODO: Implement cloning for WasmEHFuncInfo. This will have invalid block
32bdd1243dSDimitry Andric // references.
33bdd1243dSDimitry Andric return DestMF.cloneInfo<WebAssemblyFunctionInfo>(*this);
3481ad6265SDimitry Andric }
3581ad6265SDimitry Andric
initWARegs(MachineRegisterInfo & MRI)365ffd83dbSDimitry Andric void WebAssemblyFunctionInfo::initWARegs(MachineRegisterInfo &MRI) {
370b57cec5SDimitry Andric assert(WARegs.empty());
38*c9157d92SDimitry Andric unsigned Reg = WebAssembly::UnusedReg;
395ffd83dbSDimitry Andric WARegs.resize(MRI.getNumVirtRegs(), Reg);
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric
computeLegalValueVTs(const WebAssemblyTargetLowering & TLI,LLVMContext & Ctx,const DataLayout & DL,Type * Ty,SmallVectorImpl<MVT> & ValueVTs)421fd87a68SDimitry Andric void llvm::computeLegalValueVTs(const WebAssemblyTargetLowering &TLI,
431fd87a68SDimitry Andric LLVMContext &Ctx, const DataLayout &DL,
441fd87a68SDimitry Andric Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
451fd87a68SDimitry Andric SmallVector<EVT, 4> VTs;
461fd87a68SDimitry Andric ComputeValueVTs(TLI, DL, Ty, VTs);
471fd87a68SDimitry Andric
481fd87a68SDimitry Andric for (EVT VT : VTs) {
491fd87a68SDimitry Andric unsigned NumRegs = TLI.getNumRegisters(Ctx, VT);
501fd87a68SDimitry Andric MVT RegisterVT = TLI.getRegisterType(Ctx, VT);
511fd87a68SDimitry Andric for (unsigned I = 0; I != NumRegs; ++I)
521fd87a68SDimitry Andric ValueVTs.push_back(RegisterVT);
531fd87a68SDimitry Andric }
541fd87a68SDimitry Andric }
551fd87a68SDimitry Andric
computeLegalValueVTs(const Function & F,const TargetMachine & TM,Type * Ty,SmallVectorImpl<MVT> & ValueVTs)560b57cec5SDimitry Andric void llvm::computeLegalValueVTs(const Function &F, const TargetMachine &TM,
570b57cec5SDimitry Andric Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
580b57cec5SDimitry Andric const DataLayout &DL(F.getParent()->getDataLayout());
590b57cec5SDimitry Andric const WebAssemblyTargetLowering &TLI =
600b57cec5SDimitry Andric *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
611fd87a68SDimitry Andric computeLegalValueVTs(TLI, F.getContext(), DL, Ty, ValueVTs);
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric
computeSignatureVTs(const FunctionType * Ty,const Function * TargetFunc,const Function & ContextFunc,const TargetMachine & TM,SmallVectorImpl<MVT> & Params,SmallVectorImpl<MVT> & Results)645ffd83dbSDimitry Andric void llvm::computeSignatureVTs(const FunctionType *Ty,
655ffd83dbSDimitry Andric const Function *TargetFunc,
665ffd83dbSDimitry Andric const Function &ContextFunc,
670b57cec5SDimitry Andric const TargetMachine &TM,
680b57cec5SDimitry Andric SmallVectorImpl<MVT> &Params,
690b57cec5SDimitry Andric SmallVectorImpl<MVT> &Results) {
705ffd83dbSDimitry Andric computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
738bcb0991SDimitry Andric if (Results.size() > 1 &&
745ffd83dbSDimitry Andric !TM.getSubtarget<WebAssemblySubtarget>(ContextFunc).hasMultivalue()) {
758bcb0991SDimitry Andric // WebAssembly can't lower returns of multiple values without demoting to
768bcb0991SDimitry Andric // sret unless multivalue is enabled (see
778bcb0991SDimitry Andric // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
788bcb0991SDimitry Andric // values with a poitner parameter.
790b57cec5SDimitry Andric Results.clear();
800b57cec5SDimitry Andric Params.push_back(PtrVT);
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric for (auto *Param : Ty->params())
845ffd83dbSDimitry Andric computeLegalValueVTs(ContextFunc, TM, Param, Params);
850b57cec5SDimitry Andric if (Ty->isVarArg())
860b57cec5SDimitry Andric Params.push_back(PtrVT);
875ffd83dbSDimitry Andric
885ffd83dbSDimitry Andric // For swiftcc, emit additional swiftself and swifterror parameters
895ffd83dbSDimitry Andric // if there aren't. These additional parameters are also passed for caller.
905ffd83dbSDimitry Andric // They are necessary to match callee and caller signature for indirect
915ffd83dbSDimitry Andric // call.
925ffd83dbSDimitry Andric
935ffd83dbSDimitry Andric if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
945ffd83dbSDimitry Andric MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
955ffd83dbSDimitry Andric bool HasSwiftErrorArg = false;
965ffd83dbSDimitry Andric bool HasSwiftSelfArg = false;
975ffd83dbSDimitry Andric for (const auto &Arg : TargetFunc->args()) {
985ffd83dbSDimitry Andric HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
995ffd83dbSDimitry Andric HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
1005ffd83dbSDimitry Andric }
1015ffd83dbSDimitry Andric if (!HasSwiftErrorArg)
1025ffd83dbSDimitry Andric Params.push_back(PtrVT);
1035ffd83dbSDimitry Andric if (!HasSwiftSelfArg)
1045ffd83dbSDimitry Andric Params.push_back(PtrVT);
1055ffd83dbSDimitry Andric }
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric
valTypesFromMVTs(const ArrayRef<MVT> & In,SmallVectorImpl<wasm::ValType> & Out)1080b57cec5SDimitry Andric void llvm::valTypesFromMVTs(const ArrayRef<MVT> &In,
1090b57cec5SDimitry Andric SmallVectorImpl<wasm::ValType> &Out) {
1100b57cec5SDimitry Andric for (MVT Ty : In)
1110b57cec5SDimitry Andric Out.push_back(WebAssembly::toValType(Ty));
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric std::unique_ptr<wasm::WasmSignature>
signatureFromMVTs(const SmallVectorImpl<MVT> & Results,const SmallVectorImpl<MVT> & Params)1150b57cec5SDimitry Andric llvm::signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
1160b57cec5SDimitry Andric const SmallVectorImpl<MVT> &Params) {
1178bcb0991SDimitry Andric auto Sig = std::make_unique<wasm::WasmSignature>();
1180b57cec5SDimitry Andric valTypesFromMVTs(Results, Sig->Returns);
1190b57cec5SDimitry Andric valTypesFromMVTs(Params, Sig->Params);
1200b57cec5SDimitry Andric return Sig;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric
WebAssemblyFunctionInfo(const llvm::MachineFunction & MF,const llvm::WebAssemblyFunctionInfo & MFI)1230b57cec5SDimitry Andric yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo(
124bdd1243dSDimitry Andric const llvm::MachineFunction &MF, const llvm::WebAssemblyFunctionInfo &MFI)
125fe6060f1SDimitry Andric : CFGStackified(MFI.isCFGStackified()) {
126fe6060f1SDimitry Andric for (auto VT : MFI.getParams())
127fe6060f1SDimitry Andric Params.push_back(EVT(VT).getEVTString());
128fe6060f1SDimitry Andric for (auto VT : MFI.getResults())
129fe6060f1SDimitry Andric Results.push_back(EVT(VT).getEVTString());
130fe6060f1SDimitry Andric
131fe6060f1SDimitry Andric // MFI.getWasmEHFuncInfo() is non-null only for functions with the
132fe6060f1SDimitry Andric // personality function.
133bdd1243dSDimitry Andric
134bdd1243dSDimitry Andric if (auto *EHInfo = MF.getWasmEHFuncInfo()) {
135fe6060f1SDimitry Andric // SrcToUnwindDest can contain stale mappings in case BBs are removed in
136fe6060f1SDimitry Andric // optimizations, in case, for example, they are unreachable. We should not
137fe6060f1SDimitry Andric // include their info.
138fe6060f1SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> MBBs;
139fe6060f1SDimitry Andric for (const auto &MBB : MF)
140fe6060f1SDimitry Andric MBBs.insert(&MBB);
141fe6060f1SDimitry Andric for (auto KV : EHInfo->SrcToUnwindDest) {
142fe6060f1SDimitry Andric auto *SrcBB = KV.first.get<MachineBasicBlock *>();
143fe6060f1SDimitry Andric auto *DestBB = KV.second.get<MachineBasicBlock *>();
144fe6060f1SDimitry Andric if (MBBs.count(SrcBB) && MBBs.count(DestBB))
145fe6060f1SDimitry Andric SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
146fe6060f1SDimitry Andric }
147fe6060f1SDimitry Andric }
148fe6060f1SDimitry Andric }
1490b57cec5SDimitry Andric
mappingImpl(yaml::IO & YamlIO)1500b57cec5SDimitry Andric void yaml::WebAssemblyFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
1510b57cec5SDimitry Andric MappingTraits<WebAssemblyFunctionInfo>::mapping(YamlIO, *this);
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
initializeBaseYamlFields(MachineFunction & MF,const yaml::WebAssemblyFunctionInfo & YamlMFI)1540b57cec5SDimitry Andric void WebAssemblyFunctionInfo::initializeBaseYamlFields(
155bdd1243dSDimitry Andric MachineFunction &MF, const yaml::WebAssemblyFunctionInfo &YamlMFI) {
1560b57cec5SDimitry Andric CFGStackified = YamlMFI.CFGStackified;
157fe6060f1SDimitry Andric for (auto VT : YamlMFI.Params)
158fe6060f1SDimitry Andric addParam(WebAssembly::parseMVT(VT.Value));
159fe6060f1SDimitry Andric for (auto VT : YamlMFI.Results)
160fe6060f1SDimitry Andric addResult(WebAssembly::parseMVT(VT.Value));
161bdd1243dSDimitry Andric
162bdd1243dSDimitry Andric // FIXME: WasmEHInfo is defined in the MachineFunction, but serialized
163bdd1243dSDimitry Andric // here. Either WasmEHInfo should be moved out of MachineFunction, or the
164bdd1243dSDimitry Andric // serialization handling should be moved to MachineFunction.
165bdd1243dSDimitry Andric if (WasmEHFuncInfo *WasmEHInfo = MF.getWasmEHFuncInfo()) {
166fe6060f1SDimitry Andric for (auto KV : YamlMFI.SrcToUnwindDest)
167bdd1243dSDimitry Andric WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
168bdd1243dSDimitry Andric MF.getBlockNumbered(KV.second));
169fe6060f1SDimitry Andric }
1700b57cec5SDimitry Andric }
171