xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrCall.td (revision f976241773df2260e6170317080761d1c5814fe5)
1//===- WebAssemblyInstrCall.td-WebAssembly Call codegen support -*- tablegen -*-
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/// WebAssembly Call operand code-gen constructs.
11///
12//===----------------------------------------------------------------------===//
13
14// TODO: addr64: These currently assume the callee address is 32-bit.
15// FIXME: add $type to first call_indirect asmstr (and maybe $flags)
16
17// Call sequence markers. These have an immediate which represents the amount of
18// stack space to allocate or free, which is used for varargs lowering.
19let Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1 in {
20defm ADJCALLSTACKDOWN : NRI<(outs), (ins i32imm:$amt, i32imm:$amt2),
21                            [(WebAssemblycallseq_start timm:$amt, timm:$amt2)]>;
22defm ADJCALLSTACKUP : NRI<(outs), (ins i32imm:$amt, i32imm:$amt2),
23                          [(WebAssemblycallseq_end timm:$amt, timm:$amt2)]>;
24} // Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1
25
26multiclass CALL<ValueType vt, WebAssemblyRegClass rt, string prefix,
27                list<Predicate> preds = []> {
28  defm CALL_#vt :
29    I<(outs rt:$dst), (ins function32_op:$callee, variable_ops),
30      (outs), (ins function32_op:$callee),
31      [(set (vt rt:$dst), (WebAssemblycall1 (i32 imm:$callee)))],
32      !strconcat(prefix, "call\t$dst, $callee"),
33      !strconcat(prefix, "call\t$callee"),
34      0x10>,
35    Requires<preds>;
36
37  let isCodeGenOnly = 1 in
38  defm PCALL_INDIRECT_#vt :
39    I<(outs rt:$dst), (ins I32:$callee, variable_ops),
40      (outs), (ins I32:$callee),
41      [(set (vt rt:$dst), (WebAssemblycall1 I32:$callee))],
42      "PSEUDO CALL INDIRECT\t$callee",
43      "PSEUDO CALL INDIRECT\t$callee">,
44    Requires<preds>;
45
46  defm CALL_INDIRECT_#vt :
47    I<(outs rt:$dst),
48      (ins TypeIndex:$type, i32imm:$flags, variable_ops),
49      (outs), (ins TypeIndex:$type, i32imm:$flags),
50      [],
51      !strconcat(prefix, "call_indirect\t$dst"),
52      !strconcat(prefix, "call_indirect\t$type"),
53      0x11>,
54    Requires<preds>;
55}
56
57let Uses = [SP32, SP64], isCall = 1 in {
58defm "" : CALL<i32, I32, "i32.">;
59defm "" : CALL<i64, I64, "i64.">;
60defm "" : CALL<f32, F32, "f32.">;
61defm "" : CALL<f64, F64, "f64.">;
62defm "" : CALL<exnref, EXNREF, "exnref.", [HasExceptionHandling]>;
63defm "" : CALL<v16i8, V128, "v128.", [HasSIMD128]>;
64defm "" : CALL<v8i16, V128, "v128.", [HasSIMD128]>;
65defm "" : CALL<v4i32, V128, "v128.", [HasSIMD128]>;
66defm "" : CALL<v2i64, V128, "v128.", [HasSIMD128]>;
67defm "" : CALL<v4f32, V128, "v128.", [HasSIMD128]>;
68defm "" : CALL<v2f64, V128, "v128.", [HasSIMD128]>;
69
70let IsCanonical = 1 in {
71defm CALL_VOID :
72  I<(outs), (ins function32_op:$callee, variable_ops),
73    (outs), (ins function32_op:$callee),
74    [(WebAssemblycall0 (i32 imm:$callee))],
75    "call    \t$callee", "call\t$callee", 0x10>;
76
77let isReturn = 1 in
78defm RET_CALL :
79  I<(outs), (ins function32_op:$callee, variable_ops),
80    (outs), (ins function32_op:$callee),
81    [(WebAssemblyretcall (i32 imm:$callee))],
82    "return_call    \t$callee", "return_call\t$callee", 0x12>,
83  Requires<[HasTailCall]>;
84
85let isCodeGenOnly = 1 in
86defm PCALL_INDIRECT_VOID :
87  I<(outs), (ins I32:$callee, variable_ops),
88    (outs), (ins I32:$callee),
89    [(WebAssemblycall0 I32:$callee)],
90    "PSEUDO CALL INDIRECT\t$callee",
91    "PSEUDO CALL INDIRECT\t$callee">;
92
93defm CALL_INDIRECT_VOID :
94  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
95    (outs), (ins TypeIndex:$type, i32imm:$flags),
96    [],
97    "call_indirect\t", "call_indirect\t$type",
98    0x11>;
99
100let isReturn = 1 in
101defm RET_CALL_INDIRECT :
102  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
103    (outs), (ins TypeIndex:$type, i32imm:$flags),
104    [],
105    "return_call_indirect\t", "return_call_indirect\t$type",
106    0x13>,
107  Requires<[HasTailCall]>;
108
109let isCodeGenOnly = 1, isReturn = 1 in
110defm PRET_CALL_INDIRECT:
111    I<(outs), (ins I32:$callee, variable_ops),
112      (outs), (ins I32:$callee),
113      [(WebAssemblyretcall I32:$callee)],
114      "PSEUDO RET_CALL INDIRECT\t$callee",
115      "PSEUDO RET_CALL INDIRECT\t$callee">,
116    Requires<[HasTailCall]>;
117
118} // IsCanonical = 1
119} // Uses = [SP32,SP64], isCall = 1
120
121// Patterns for matching a direct call to a global address.
122def : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
123          (CALL_i32 tglobaladdr:$callee)>;
124def : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
125          (CALL_i64 tglobaladdr:$callee)>;
126def : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
127          (CALL_f32 tglobaladdr:$callee)>;
128def : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
129          (CALL_f64 tglobaladdr:$callee)>;
130def : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
131          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
132def : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
133          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
134def : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
135          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
136def : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
137          (CALL_v2i64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
138def : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
139          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
140def : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
141          (CALL_v2f64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
142def : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
143          (CALL_exnref tglobaladdr:$callee)>,
144      Requires<[HasExceptionHandling]>;
145def : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
146          (CALL_VOID tglobaladdr:$callee)>;
147def : Pat<(WebAssemblyretcall (WebAssemblywrapper tglobaladdr:$callee)),
148          (RET_CALL tglobaladdr:$callee)>, Requires<[HasTailCall]>;
149
150// Patterns for matching a direct call to an external symbol.
151def : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
152          (CALL_i32 texternalsym:$callee)>;
153def : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
154          (CALL_i64 texternalsym:$callee)>;
155def : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
156          (CALL_f32 texternalsym:$callee)>;
157def : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
158          (CALL_f64 texternalsym:$callee)>;
159def : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
160          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
161def : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
162          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
163def : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
164          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
165def : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
166          (CALL_v2i64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
167def : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
168          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
169def : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
170          (CALL_v2f64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
171def : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
172          (CALL_exnref texternalsym:$callee)>,
173      Requires<[HasExceptionHandling]>;
174def : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
175          (CALL_VOID texternalsym:$callee)>;
176def : Pat<(WebAssemblyretcall (WebAssemblywrapper texternalsym:$callee)),
177          (RET_CALL texternalsym:$callee)>, Requires<[HasTailCall]>;
178