LLVM  14.0.0git
WebAssemblyMachineFunctionInfo.cpp
Go to the documentation of this file.
1 //=- WebAssemblyMachineFunctionInfo.cpp - WebAssembly Machine Function Info -=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements WebAssembly-specific per-machine-function
11 /// information.
12 ///
13 //===----------------------------------------------------------------------===//
14 
19 #include "WebAssemblySubtarget.h"
20 #include "llvm/CodeGen/Analysis.h"
23 using namespace llvm;
24 
26 
28  assert(WARegs.empty());
29  unsigned Reg = UnusedReg;
30  WARegs.resize(MRI.getNumVirtRegs(), Reg);
31 }
32 
34  Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
35  const DataLayout &DL(F.getParent()->getDataLayout());
36  const WebAssemblyTargetLowering &TLI =
37  *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
39  ComputeValueVTs(TLI, DL, Ty, VTs);
40 
41  for (EVT VT : VTs) {
42  unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
43  MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
44  for (unsigned I = 0; I != NumRegs; ++I)
45  ValueVTs.push_back(RegisterVT);
46  }
47 }
48 
50  const Function *TargetFunc,
51  const Function &ContextFunc,
52  const TargetMachine &TM,
53  SmallVectorImpl<MVT> &Params,
55  computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
56 
57  MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
58  if (Results.size() > 1 &&
59  !TM.getSubtarget<WebAssemblySubtarget>(ContextFunc).hasMultivalue()) {
60  // WebAssembly can't lower returns of multiple values without demoting to
61  // sret unless multivalue is enabled (see
62  // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
63  // values with a poitner parameter.
64  Results.clear();
65  Params.push_back(PtrVT);
66  }
67 
68  for (auto *Param : Ty->params())
69  computeLegalValueVTs(ContextFunc, TM, Param, Params);
70  if (Ty->isVarArg())
71  Params.push_back(PtrVT);
72 
73  // For swiftcc, emit additional swiftself and swifterror parameters
74  // if there aren't. These additional parameters are also passed for caller.
75  // They are necessary to match callee and caller signature for indirect
76  // call.
77 
78  if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
79  MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
80  bool HasSwiftErrorArg = false;
81  bool HasSwiftSelfArg = false;
82  for (const auto &Arg : TargetFunc->args()) {
83  HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
84  HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
85  }
86  if (!HasSwiftErrorArg)
87  Params.push_back(PtrVT);
88  if (!HasSwiftSelfArg)
89  Params.push_back(PtrVT);
90  }
91 }
92 
95  for (MVT Ty : In)
96  Out.push_back(WebAssembly::toValType(Ty));
97 }
98 
99 std::unique_ptr<wasm::WasmSignature>
101  const SmallVectorImpl<MVT> &Params) {
102  auto Sig = std::make_unique<wasm::WasmSignature>();
103  valTypesFromMVTs(Results, Sig->Returns);
104  valTypesFromMVTs(Params, Sig->Params);
105  return Sig;
106 }
107 
110  : CFGStackified(MFI.isCFGStackified()) {
111  auto *EHInfo = MFI.getWasmEHFuncInfo();
112  const llvm::MachineFunction &MF = MFI.getMachineFunction();
113 
114  for (auto VT : MFI.getParams())
115  Params.push_back(EVT(VT).getEVTString());
116  for (auto VT : MFI.getResults())
117  Results.push_back(EVT(VT).getEVTString());
118 
119  // MFI.getWasmEHFuncInfo() is non-null only for functions with the
120  // personality function.
121  if (EHInfo) {
122  // SrcToUnwindDest can contain stale mappings in case BBs are removed in
123  // optimizations, in case, for example, they are unreachable. We should not
124  // include their info.
126  for (const auto &MBB : MF)
127  MBBs.insert(&MBB);
128  for (auto KV : EHInfo->SrcToUnwindDest) {
129  auto *SrcBB = KV.first.get<MachineBasicBlock *>();
130  auto *DestBB = KV.second.get<MachineBasicBlock *>();
131  if (MBBs.count(SrcBB) && MBBs.count(DestBB))
132  SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
133  }
134  }
135 }
136 
139 }
140 
142  const yaml::WebAssemblyFunctionInfo &YamlMFI) {
143  CFGStackified = YamlMFI.CFGStackified;
144  for (auto VT : YamlMFI.Params)
145  addParam(WebAssembly::parseMVT(VT.Value));
146  for (auto VT : YamlMFI.Results)
147  addResult(WebAssembly::parseMVT(VT.Value));
148  if (WasmEHInfo) {
149  for (auto KV : YamlMFI.SrcToUnwindDest)
150  WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
151  MF.getBlockNumbered(KV.second));
152  }
153 }
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:65
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::Function::args
iterator_range< arg_iterator > args()
Definition: Function.h:772
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
WebAssemblyISelLowering.h
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::Function
Definition: Function.h:61
llvm::WebAssemblySubtarget::hasMultivalue
bool hasMultivalue() const
Definition: WebAssemblySubtarget.h:99
llvm::signatureFromMVTs
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
Definition: WebAssemblyMachineFunctionInfo.cpp:100
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::WebAssemblyFunctionInfo::getMachineFunction
const MachineFunction & getMachineFunction() const
Definition: WebAssemblyMachineFunctionInfo.h:77
llvm::yaml::WebAssemblyFunctionInfo::WebAssemblyFunctionInfo
WebAssemblyFunctionInfo()=default
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
YamlIO
IO & YamlIO
Definition: ELFYAML.cpp:1171
llvm::MachineRegisterInfo::getNumVirtRegs
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
Definition: MachineRegisterInfo.h:757
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
Results
Function Alias Analysis Results
Definition: AliasAnalysis.cpp:847
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:52
llvm::yaml::WebAssemblyFunctionInfo::Params
std::vector< FlowStringValue > Params
Definition: WebAssemblyMachineFunctionInfo.h:191
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::yaml::WebAssemblyFunctionInfo::mappingImpl
void mappingImpl(yaml::IO &YamlIO) override
Definition: WebAssemblyMachineFunctionInfo.cpp:137
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:123
llvm::ComputeValueVTs
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< uint64_t > *Offsets=nullptr, uint64_t StartingOffset=0)
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition: Analysis.cpp:124
llvm::WebAssemblyTargetLowering
Definition: WebAssemblyISelLowering.h:43
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
TargetMachine.h
llvm::EVT
Extended Value Type.
Definition: ValueTypes.h:35
Param
Value * Param
Definition: NVPTXLowerArgs.cpp:163
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:131
WebAssemblyTypeUtilities.h
llvm::yaml::WebAssemblyFunctionInfo::CFGStackified
bool CFGStackified
Definition: WebAssemblyMachineFunctionInfo.h:193
llvm::FunctionType::params
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:130
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::yaml::WebAssemblyFunctionInfo::SrcToUnwindDest
BBNumberMap SrcToUnwindDest
Definition: WebAssemblyMachineFunctionInfo.h:196
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::WebAssemblyFunctionInfo::UnusedReg
static const unsigned UnusedReg
Definition: WebAssemblyMachineFunctionInfo.h:122
llvm::Function::getCallingConv
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:239
llvm::valTypesFromMVTs
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
Definition: WebAssemblyMachineFunctionInfo.cpp:93
WebAssemblyInstPrinter.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
Analysis.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:79
llvm::WebAssemblyFunctionInfo::initializeBaseYamlFields
void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI)
Definition: WebAssemblyMachineFunctionInfo.cpp:141
llvm::WebAssemblyFunctionInfo
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
Definition: WebAssemblyMachineFunctionInfo.h:33
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:31
WebAssemblyMachineFunctionInfo.h
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::WebAssemblyFunctionInfo::getResults
const std::vector< MVT > & getResults() const
Definition: WebAssemblyMachineFunctionInfo.h:85
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::computeLegalValueVTs
void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty, SmallVectorImpl< MVT > &ValueVTs)
Definition: WebAssemblyMachineFunctionInfo.cpp:33
llvm::MachineBasicBlock::getNumber
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Definition: MachineBasicBlock.h:1056
llvm::WebAssemblyFunctionInfo::~WebAssemblyFunctionInfo
~WebAssemblyFunctionInfo() override
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::WebAssemblySubtarget
Definition: WebAssemblySubtarget.h:35
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::WebAssemblyFunctionInfo::getWasmEHFuncInfo
WasmEHFuncInfo * getWasmEHFuncInfo() const
Definition: WebAssemblyMachineFunctionInfo.h:165
llvm::computeSignatureVTs
void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc, const Function &ContextFunc, const TargetMachine &TM, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results)
Definition: WebAssemblyMachineFunctionInfo.cpp:49
llvm::WebAssemblyFunctionInfo::initWARegs
void initWARegs(MachineRegisterInfo &MRI)
Definition: WebAssemblyMachineFunctionInfo.cpp:27
WasmEHFuncInfo.h
WebAssemblySubtarget.h
llvm::yaml::WebAssemblyFunctionInfo::Results
std::vector< FlowStringValue > Results
Definition: WebAssemblyMachineFunctionInfo.h:192
llvm::WebAssemblyFunctionInfo::getParams
const std::vector< MVT > & getParams() const
Definition: WebAssemblyMachineFunctionInfo.h:82
llvm::TargetLoweringBase::getRegisterType
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
Definition: TargetLowering.h:1460
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::CallingConv::Swift
@ Swift
Definition: CallingConv.h:73
llvm::TargetLoweringBase::getNumRegisters
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, Optional< MVT > RegisterVT=None) const
Return the number of registers that this ValueType will eventually require.
Definition: TargetLowering.h:1498
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:124
llvm::yaml::WebAssemblyFunctionInfo
Definition: WebAssemblyMachineFunctionInfo.h:190
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::Function::size
size_t size() const
Definition: Function.h:738
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:364
llvm::MVT::getIntegerVT
static MVT getIntegerVT(unsigned BitWidth)
Definition: MachineValueType.h:1158