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
57// CALL should take both variadic arguments and produce variadic results, but
58// this is not possible to model directly. Instead, we select calls to a
59// CALL_PARAMS taking variadic aguments linked with a CALL_RESULTS that handles
60// producing the call's variadic results. We recombine the two in a custom
61// inserter hook after DAG ISel, so passes over MachineInstrs will only ever
62// observe CALL nodes with all of the expected variadic uses and defs.
63let isPseudo = 1 in
64defm CALL_PARAMS :
65  I<(outs), (ins function32_op:$callee, variable_ops),
66    (outs), (ins function32_op:$callee), [],
67    "call_params\t$callee", "call_params\t$callee", -1>;
68
69let variadicOpsAreDefs = 1, usesCustomInserter = 1, isPseudo = 1 in
70defm CALL_RESULTS :
71  I<(outs), (ins variable_ops), (outs), (ins), [],
72     "call_results", "call_results", -1>;
73
74let Uses = [SP32, SP64], isCall = 1 in {
75
76// TODO: Add an indirect version of the variadic call, delete CALL_*
77defm CALL :
78  I<(outs), (ins function32_op:$callee, variable_ops),
79    (outs), (ins function32_op:$callee), [],
80    "call\t$callee", "call\t$callee", 0x10>;
81
82defm "" : CALL<i32, I32, "i32.">;
83defm "" : CALL<i64, I64, "i64.">;
84defm "" : CALL<f32, F32, "f32.">;
85defm "" : CALL<f64, F64, "f64.">;
86defm "" : CALL<exnref, EXNREF, "exnref.", [HasExceptionHandling]>;
87defm "" : CALL<v16i8, V128, "v128.", [HasSIMD128]>;
88defm "" : CALL<v8i16, V128, "v128.", [HasSIMD128]>;
89defm "" : CALL<v4i32, V128, "v128.", [HasSIMD128]>;
90defm "" : CALL<v2i64, V128, "v128.", [HasSIMD128]>;
91defm "" : CALL<v4f32, V128, "v128.", [HasSIMD128]>;
92defm "" : CALL<v2f64, V128, "v128.", [HasSIMD128]>;
93
94let IsCanonical = 1 in {
95
96defm CALL_VOID :
97  I<(outs), (ins function32_op:$callee, variable_ops),
98    (outs), (ins function32_op:$callee),
99    [(WebAssemblycall0 (i32 imm:$callee))],
100    "call    \t$callee", "call\t$callee", 0x10>;
101
102let isReturn = 1 in
103defm RET_CALL :
104  I<(outs), (ins function32_op:$callee, variable_ops),
105    (outs), (ins function32_op:$callee),
106    [(WebAssemblyretcall (i32 imm:$callee))],
107    "return_call    \t$callee", "return_call\t$callee", 0x12>,
108  Requires<[HasTailCall]>;
109
110let isCodeGenOnly = 1 in
111defm PCALL_INDIRECT_VOID :
112  I<(outs), (ins I32:$callee, variable_ops),
113    (outs), (ins I32:$callee),
114    [(WebAssemblycall0 I32:$callee)],
115    "PSEUDO CALL INDIRECT\t$callee",
116    "PSEUDO CALL INDIRECT\t$callee">;
117
118defm CALL_INDIRECT_VOID :
119  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
120    (outs), (ins TypeIndex:$type, i32imm:$flags),
121    [],
122    "call_indirect\t", "call_indirect\t$type",
123    0x11>;
124
125let isReturn = 1 in
126defm RET_CALL_INDIRECT :
127  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
128    (outs), (ins TypeIndex:$type, i32imm:$flags),
129    [],
130    "return_call_indirect\t", "return_call_indirect\t$type",
131    0x13>,
132  Requires<[HasTailCall]>;
133
134let isCodeGenOnly = 1, isReturn = 1 in
135defm PRET_CALL_INDIRECT:
136    I<(outs), (ins I32:$callee, variable_ops),
137      (outs), (ins I32:$callee),
138      [(WebAssemblyretcall I32:$callee)],
139      "PSEUDO RET_CALL INDIRECT\t$callee",
140      "PSEUDO RET_CALL INDIRECT\t$callee">,
141    Requires<[HasTailCall]>;
142
143} // IsCanonical = 1
144} // Uses = [SP32,SP64], isCall = 1
145
146// Patterns for matching a direct call to a global address.
147def : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
148          (CALL_i32 tglobaladdr:$callee)>;
149def : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
150          (CALL_i64 tglobaladdr:$callee)>;
151def : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
152          (CALL_f32 tglobaladdr:$callee)>;
153def : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
154          (CALL_f64 tglobaladdr:$callee)>;
155def : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
156          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
157def : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
158          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
159def : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
160          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
161def : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
162          (CALL_v2i64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
163def : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
164          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
165def : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
166          (CALL_v2f64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
167def : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
168          (CALL_exnref tglobaladdr:$callee)>,
169      Requires<[HasExceptionHandling]>;
170def : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
171          (CALL_VOID tglobaladdr:$callee)>;
172def : Pat<(WebAssemblyretcall (WebAssemblywrapper tglobaladdr:$callee)),
173          (RET_CALL tglobaladdr:$callee)>, Requires<[HasTailCall]>;
174
175// Patterns for matching a direct call to an external symbol.
176def : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
177          (CALL_i32 texternalsym:$callee)>;
178def : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
179          (CALL_i64 texternalsym:$callee)>;
180def : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
181          (CALL_f32 texternalsym:$callee)>;
182def : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
183          (CALL_f64 texternalsym:$callee)>;
184def : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
185          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
186def : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
187          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
188def : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
189          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
190def : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
191          (CALL_v2i64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
192def : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
193          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
194def : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
195          (CALL_v2f64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
196def : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
197          (CALL_exnref texternalsym:$callee)>,
198      Requires<[HasExceptionHandling]>;
199def : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
200          (CALL_VOID texternalsym:$callee)>;
201def : Pat<(WebAssemblyretcall (WebAssemblywrapper texternalsym:$callee)),
202          (RET_CALL texternalsym:$callee)>, Requires<[HasTailCall]>;
203