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#include <algorithm>
22
23namespace llvm {
24
25class MachineBasicBlock;
26class MachineFunction;
27class MachineInstr;
28class MachineOperand;
29class MCContext;
30class MCSymbolWasm;
31class MCRegisterClass;
32using TargetRegisterClass = MCRegisterClass;
33class WebAssemblyFunctionInfo;
34class WebAssemblySubtarget;
35class MachineSDNode;
36class SDLoc;
37class SelectionDAG;
38
39namespace WebAssembly {
40
41enum class WebAssemblyABI {
42 MVP,
43 ExperimentalMV,
44};
45
46/// Parse an ABI name into the corresponding enum.
47inline WebAssemblyABI getABI(StringRef Name) {
48 return Name == "experimental-mv" ? WebAssemblyABI::ExperimentalMV
49 : WebAssemblyABI::MVP;
50}
51
52bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI);
53bool mayThrow(const MachineInstr &MI);
54
55// Exception-related function names
56extern const char *const ClangCallTerminateFn;
57extern const char *const CxaBeginCatchFn;
58extern const char *const CxaRethrowFn;
59extern const char *const StdTerminateFn;
60extern const char *const PersonalityWrapperFn;
61
62/// Returns the operand number of a callee, assuming the argument is a call
63/// instruction.
64const MachineOperand &getCalleeOp(const MachineInstr &MI);
65
66/// Returns the __indirect_function_table, for use in call_indirect and in
67/// function bitcasts.
68MCSymbolWasm *
69getOrCreateFunctionTableSymbol(MCContext &Ctx,
70 const WebAssemblySubtarget *Subtarget);
71
72/// Returns the __funcref_call_table, for use in funcref calls when lowered to
73/// table.set + call_indirect.
74MCSymbolWasm *
75getOrCreateFuncrefCallTableSymbol(MCContext &Ctx,
76 const WebAssemblySubtarget *Subtarget);
77
78/// Find a catch instruction from an EH pad. Returns null if no catch
79/// instruction found or the catch is in an invalid location.
80MachineInstr *findCatch(MachineBasicBlock *EHPad);
81
82/// Returns the appropriate copy opcode for the given register class.
83unsigned getCopyOpcodeForRegClass(const TargetRegisterClass *RC);
84
85/// Returns true if multivalue returns of a function can be lowered directly,
86/// i.e., not indirectly via a pointer parameter that points to the value in
87/// memory.
88bool canLowerMultivalueReturn(const WebAssemblySubtarget *Subtarget);
89
90/// Returns true if the function's return value(s) can be lowered directly,
91/// i.e., not indirectly via a pointer parameter that points to the value in
92/// memory.
93bool canLowerReturn(size_t ResultSize, const WebAssemblySubtarget *Subtarget);
94
95// Get the TLS base value for the current target
96// If using libcall thread context, calls
97// __wasm_get_tls_base, otherwise, global.get __tls_base
98MachineSDNode *getTLSBase(SelectionDAG &DAG, const SDLoc &DL,
99 const WebAssemblySubtarget *Subtarget,
100 const SDValue Chain = SDValue());
101
102/// Moves the element at \p Idx of \p Range to the front, keeping the order of
103/// the other elements.
104template <typename RangeT> void moveToFront(RangeT &Range, size_t Idx) {
105 std::rotate(Range.begin(), Range.begin() + Idx, Range.begin() + Idx + 1);
106}
107
108/// Returns the virtual register defined by the ARGUMENT instruction of the
109/// swiftasync argument, or an invalid register if there is none. The argument
110/// arrives in local 0, which must hold the async context for the whole
111/// function.
112Register getSwiftAsyncArgReg(const MachineFunction &MF);
113
114} // end namespace WebAssembly
115
116} // end namespace llvm
117
118#endif
119