LLVM  13.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 
17 #include "WebAssemblySubtarget.h"
18 #include "llvm/CodeGen/Analysis.h"
20 using namespace llvm;
21 
23 
25  assert(WARegs.empty());
26  unsigned Reg = UnusedReg;
27  WARegs.resize(MRI.getNumVirtRegs(), Reg);
28 }
29 
31  Type *Ty, SmallVectorImpl<MVT> &ValueVTs) {
32  const DataLayout &DL(F.getParent()->getDataLayout());
33  const WebAssemblyTargetLowering &TLI =
34  *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering();
36  ComputeValueVTs(TLI, DL, Ty, VTs);
37 
38  for (EVT VT : VTs) {
39  unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT);
40  MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT);
41  for (unsigned I = 0; I != NumRegs; ++I)
42  ValueVTs.push_back(RegisterVT);
43  }
44 }
45 
47  const Function *TargetFunc,
48  const Function &ContextFunc,
49  const TargetMachine &TM,
50  SmallVectorImpl<MVT> &Params,
52  computeLegalValueVTs(ContextFunc, TM, Ty->getReturnType(), Results);
53 
54  MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
55  if (Results.size() > 1 &&
56  !TM.getSubtarget<WebAssemblySubtarget>(ContextFunc).hasMultivalue()) {
57  // WebAssembly can't lower returns of multiple values without demoting to
58  // sret unless multivalue is enabled (see
59  // WebAssemblyTargetLowering::CanLowerReturn). So replace multiple return
60  // values with a poitner parameter.
61  Results.clear();
62  Params.push_back(PtrVT);
63  }
64 
65  for (auto *Param : Ty->params())
66  computeLegalValueVTs(ContextFunc, TM, Param, Params);
67  if (Ty->isVarArg())
68  Params.push_back(PtrVT);
69 
70  // For swiftcc, emit additional swiftself and swifterror parameters
71  // if there aren't. These additional parameters are also passed for caller.
72  // They are necessary to match callee and caller signature for indirect
73  // call.
74 
75  if (TargetFunc && TargetFunc->getCallingConv() == CallingConv::Swift) {
76  MVT PtrVT = MVT::getIntegerVT(TM.createDataLayout().getPointerSizeInBits());
77  bool HasSwiftErrorArg = false;
78  bool HasSwiftSelfArg = false;
79  for (const auto &Arg : TargetFunc->args()) {
80  HasSwiftErrorArg |= Arg.hasAttribute(Attribute::SwiftError);
81  HasSwiftSelfArg |= Arg.hasAttribute(Attribute::SwiftSelf);
82  }
83  if (!HasSwiftErrorArg)
84  Params.push_back(PtrVT);
85  if (!HasSwiftSelfArg)
86  Params.push_back(PtrVT);
87  }
88 }
89 
92  for (MVT Ty : In)
93  Out.push_back(WebAssembly::toValType(Ty));
94 }
95 
96 std::unique_ptr<wasm::WasmSignature>
98  const SmallVectorImpl<MVT> &Params) {
99  auto Sig = std::make_unique<wasm::WasmSignature>();
100  valTypesFromMVTs(Results, Sig->Returns);
101  valTypesFromMVTs(Params, Sig->Params);
102  return Sig;
103 }
104 
107  : CFGStackified(MFI.isCFGStackified()) {
108  auto *EHInfo = MFI.getWasmEHFuncInfo();
109  const llvm::MachineFunction &MF = MFI.getMachineFunction();
110  // MFI.getWasmEHFuncInfo() is non-null only for functions with the
111  // personality function.
112  if (EHInfo) {
113  // SrcToUnwindDest can contain stale mappings in case BBs are removed in
114  // optimizations, in case, for example, they are unreachable. We should not
115  // include their info.
117  for (const auto &MBB : MF)
118  MBBs.insert(&MBB);
119  for (auto KV : EHInfo->SrcToUnwindDest) {
120  auto *SrcBB = KV.first.get<MachineBasicBlock *>();
121  auto *DestBB = KV.second.get<MachineBasicBlock *>();
122  if (MBBs.count(SrcBB) && MBBs.count(DestBB))
123  SrcToUnwindDest[SrcBB->getNumber()] = DestBB->getNumber();
124  }
125  }
126 }
127 
130 }
131 
133  const yaml::WebAssemblyFunctionInfo &YamlMFI) {
134  CFGStackified = YamlMFI.CFGStackified;
135  if (WasmEHInfo) {
136  for (auto KV : YamlMFI.SrcToUnwindDest)
137  WasmEHInfo->setUnwindDest(MF.getBlockNumbered(KV.first),
138  MF.getBlockNumbered(KV.second));
139  }
140 }
llvm
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::Function::args
iterator_range< arg_iterator > args()
Definition: Function.h:807
llvm::SystemZISD::TM
@ TM
Definition: SystemZISelLowering.h:65
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:97
llvm::signatureFromMVTs
std::unique_ptr< wasm::WasmSignature > signatureFromMVTs(const SmallVectorImpl< MVT > &Results, const SmallVectorImpl< MVT > &Params)
Definition: WebAssemblyMachineFunctionInfo.cpp:97
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:46
YamlIO
IO & YamlIO
Definition: ELFYAML.cpp:1162
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:849
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:50
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::yaml::WebAssemblyFunctionInfo::mappingImpl
void mappingImpl(yaml::IO &YamlIO) override
Definition: WebAssemblyMachineFunctionInfo.cpp:128
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:122
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:205
TargetMachine.h
llvm::EVT
Extended Value Type.
Definition: ValueTypes.h:35
Param
Value * Param
Definition: NVPTXLowerArgs.cpp:163
llvm::yaml::WebAssemblyFunctionInfo::CFGStackified
bool CFGStackified
Definition: WebAssemblyMachineFunctionInfo.h:191
llvm::FunctionType::params
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:129
llvm::CallingConv::Swift
@ Swift
Definition: CallingConv.h:73
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::yaml::WebAssemblyFunctionInfo::SrcToUnwindDest
BBNumberMap SrcToUnwindDest
Definition: WebAssemblyMachineFunctionInfo.h:194
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:228
llvm::valTypesFromMVTs
void valTypesFromMVTs(const ArrayRef< MVT > &In, SmallVectorImpl< wasm::ValType > &Out)
Definition: WebAssemblyMachineFunctionInfo.cpp:90
I
#define I(x, y, z)
Definition: MD5.cpp:59
Analysis.h
llvm::WebAssembly::toValType
wasm::ValType toValType(const MVT &Ty)
Definition: WebAssemblyMCTargetDesc.cpp:133
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
llvm::WebAssemblyFunctionInfo::initializeBaseYamlFields
void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI)
Definition: WebAssemblyMachineFunctionInfo.cpp:132
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:30
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:227
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:33
llvm::computeLegalValueVTs
void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty, SmallVectorImpl< MVT > &ValueVTs)
Definition: WebAssemblyMachineFunctionInfo.cpp:30
llvm::MachineBasicBlock::getNumber
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Definition: MachineBasicBlock.h:965
llvm::TargetLoweringBase::getNumRegisters
unsigned getNumRegisters(LLVMContext &Context, EVT VT) const
Return the number of registers that this ValueType will eventually require.
Definition: TargetLowering.h:1467
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:46
llvm::WebAssemblyFunctionInfo::initWARegs
void initWARegs(MachineRegisterInfo &MRI)
Definition: WebAssemblyMachineFunctionInfo.cpp:24
WebAssemblySubtarget.h
llvm::TargetLoweringBase::getRegisterType
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
Definition: TargetLowering.h:1433
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:123
llvm::yaml::WebAssemblyFunctionInfo
Definition: WebAssemblyMachineFunctionInfo.h:190
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:102
llvm::Function::size
size_t size() const
Definition: Function.h:773
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:1100