1//===-- WebAssemblyUtilities - WebAssembly Utility Functions ---*- C++ -*-====//
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 contains the declaration of the WebAssembly-specific
11/// utility functions.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYUTILITIES_H
16#define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYUTILITIES_H
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/CodeGen/SelectionDAGNodes.h"
20#include "llvm/Support/CommandLine.h"
21
22namespace llvm {
23
24class MachineBasicBlock;
25class MachineInstr;
26class MachineOperand;
27class MCContext;
28class MCSymbolWasm;
29class MCRegisterClass;
30using TargetRegisterClass = MCRegisterClass;
31class WebAssemblyFunctionInfo;
32class WebAssemblySubtarget;
33class MachineSDNode;
34class SDLoc;
35class SelectionDAG;
36
37namespace WebAssembly {
38
39enum class WebAssemblyABI {
40 MVP,
41 ExperimentalMV,
42};
43
44/// Parse an ABI name into the corresponding enum.
45inline WebAssemblyABI getABI(StringRef Name) {
46 return Name == "experimental-mv" ? WebAssemblyABI::ExperimentalMV
47 : WebAssemblyABI::MVP;
48}
49
50bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI);
51bool mayThrow(const MachineInstr &MI);
52
53// Exception-related function names
54extern const char *const ClangCallTerminateFn;
55extern const char *const CxaBeginCatchFn;
56extern const char *const CxaRethrowFn;
57extern const char *const StdTerminateFn;
58extern const char *const PersonalityWrapperFn;
59
60/// Returns the operand number of a callee, assuming the argument is a call
61/// instruction.
62const MachineOperand &getCalleeOp(const MachineInstr &MI);
63
64/// Returns the __indirect_function_table, for use in call_indirect and in
65/// function bitcasts.
66MCSymbolWasm *
67getOrCreateFunctionTableSymbol(MCContext &Ctx,
68 const WebAssemblySubtarget *Subtarget);
69
70/// Returns the __funcref_call_table, for use in funcref calls when lowered to
71/// table.set + call_indirect.
72MCSymbolWasm *
73getOrCreateFuncrefCallTableSymbol(MCContext &Ctx,
74 const WebAssemblySubtarget *Subtarget);
75
76/// Find a catch instruction from an EH pad. Returns null if no catch
77/// instruction found or the catch is in an invalid location.
78MachineInstr *findCatch(MachineBasicBlock *EHPad);
79
80/// Returns the appropriate copy opcode for the given register class.
81unsigned getCopyOpcodeForRegClass(const TargetRegisterClass *RC);
82
83/// Returns true if multivalue returns of a function can be lowered directly,
84/// i.e., not indirectly via a pointer parameter that points to the value in
85/// memory.
86bool canLowerMultivalueReturn(const WebAssemblySubtarget *Subtarget);
87
88/// Returns true if the function's return value(s) can be lowered directly,
89/// i.e., not indirectly via a pointer parameter that points to the value in
90/// memory.
91bool canLowerReturn(size_t ResultSize, const WebAssemblySubtarget *Subtarget);
92
93// Get the TLS base value for the current target
94// If using libcall thread context, calls
95// __wasm_get_tls_base, otherwise, global.get __tls_base
96MachineSDNode *getTLSBase(SelectionDAG &DAG, const SDLoc &DL,
97 const WebAssemblySubtarget *Subtarget,
98 const SDValue Chain = SDValue());
99
100} // end namespace WebAssembly
101
102} // end namespace llvm
103
104#endif
105