15ca0bacaSJF Bastien//===- WebAssemblyInstrCall.td-WebAssembly Call codegen support -*- tablegen -*-
25ca0bacaSJF Bastien//
32946cd70SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ca0bacaSJF Bastien//
75ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
85ca0bacaSJF Bastien///
95ca0bacaSJF Bastien/// \file
105f8f34e4SAdrian Prantl/// WebAssembly Call operand code-gen constructs.
115ca0bacaSJF Bastien///
125ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
135ca0bacaSJF Bastien
14e2a7a827SDan Gohman// TODO: addr64: These currently assume the callee address is 32-bit.
1548dac310SWouter van Oortmerssen// FIXME: add $type to first call_indirect asmstr (and maybe $flags)
16e2a7a827SDan Gohman
17905bef5cSDan Gohman// Call sequence markers. These have an immediate which represents the amount of
18905bef5cSDan Gohman// stack space to allocate or free, which is used for varargs lowering.
195a143063SDerek Schufflet Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1 in {
2048dac310SWouter van Oortmerssendefm ADJCALLSTACKDOWN : NRI<(outs), (ins i32imm:$amt, i32imm:$amt2),
21d526b13eSSerge Pavlov                            [(WebAssemblycallseq_start timm:$amt, timm:$amt2)]>;
2248dac310SWouter van Oortmerssendefm ADJCALLSTACKUP : NRI<(outs), (ins i32imm:$amt, i32imm:$amt2),
238bb5f292SDerek Schuff                          [(WebAssemblycallseq_end timm:$amt, timm:$amt2)]>;
244367587fSHeejin Ahn} // Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1
25af111db8SJF Bastien
26a1d97a96SThomas Livelymulticlass CALL<ValueType vt, WebAssemblyRegClass rt, string prefix,
27a1d97a96SThomas Lively                list<Predicate> preds = []> {
28a1d97a96SThomas Lively  defm CALL_#vt :
29a1d97a96SThomas Lively    I<(outs rt:$dst), (ins function32_op:$callee, variable_ops),
3048dac310SWouter van Oortmerssen      (outs), (ins function32_op:$callee),
31a1d97a96SThomas Lively      [(set (vt rt:$dst), (WebAssemblycall1 (i32 imm:$callee)))],
323acb187dSDan Gohman      !strconcat(prefix, "call\t$dst, $callee"),
3348dac310SWouter van Oortmerssen      !strconcat(prefix, "call\t$callee"),
34c9c711a0SHeejin Ahn      0x10>,
35a1d97a96SThomas Lively    Requires<preds>;
36d934cb88SDan Gohman
374367587fSHeejin Ahn  let isCodeGenOnly = 1 in
38a1d97a96SThomas Lively  defm PCALL_INDIRECT_#vt :
39a1d97a96SThomas Lively    I<(outs rt:$dst), (ins I32:$callee, variable_ops),
4048dac310SWouter van Oortmerssen      (outs), (ins I32:$callee),
41a1d97a96SThomas Lively      [(set (vt rt:$dst), (WebAssemblycall1 I32:$callee))],
4248dac310SWouter van Oortmerssen      "PSEUDO CALL INDIRECT\t$callee",
43c9c711a0SHeejin Ahn      "PSEUDO CALL INDIRECT\t$callee">,
44a1d97a96SThomas Lively    Requires<preds>;
45f50d964bSDan Gohman
46a1d97a96SThomas Lively  defm CALL_INDIRECT_#vt :
47a1d97a96SThomas Lively    I<(outs rt:$dst),
48c9c711a0SHeejin Ahn      (ins TypeIndex:$type, i32imm:$flags, variable_ops),
4948dac310SWouter van Oortmerssen      (outs), (ins TypeIndex:$type, i32imm:$flags),
506f69783fSDerek Schuff      [],
51c9c711a0SHeejin Ahn      !strconcat(prefix, "call_indirect\t$dst"),
5248dac310SWouter van Oortmerssen      !strconcat(prefix, "call_indirect\t$type"),
53c9c711a0SHeejin Ahn      0x11>,
54a1d97a96SThomas Lively    Requires<preds>;
5539bf39f3SDerek Schuff}
5639bf39f3SDerek Schuff
57*d5191096SThomas Lively// CALL should take both variadic arguments and produce variadic results, but
58*d5191096SThomas Lively// this is not possible to model directly. Instead, we select calls to a
59*d5191096SThomas Lively// CALL_PARAMS taking variadic aguments linked with a CALL_RESULTS that handles
60*d5191096SThomas Lively// producing the call's variadic results. We recombine the two in a custom
61*d5191096SThomas Lively// inserter hook after DAG ISel, so passes over MachineInstrs will only ever
62*d5191096SThomas Lively// observe CALL nodes with all of the expected variadic uses and defs.
63*d5191096SThomas Livelylet isPseudo = 1 in
64*d5191096SThomas Livelydefm CALL_PARAMS :
65*d5191096SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
66*d5191096SThomas Lively    (outs), (ins function32_op:$callee), [],
67*d5191096SThomas Lively    "call_params\t$callee", "call_params\t$callee", -1>;
68*d5191096SThomas Lively
69*d5191096SThomas Livelylet variadicOpsAreDefs = 1, usesCustomInserter = 1, isPseudo = 1 in
70*d5191096SThomas Livelydefm CALL_RESULTS :
71*d5191096SThomas Lively  I<(outs), (ins variable_ops), (outs), (ins), [],
72*d5191096SThomas Lively     "call_results", "call_results", -1>;
73*d5191096SThomas Lively
74af111db8SJF Bastienlet Uses = [SP32, SP64], isCall = 1 in {
757b64a590SThomas Lively
767b64a590SThomas Lively// TODO: Add an indirect version of the variadic call, delete CALL_*
777b64a590SThomas Livelydefm CALL :
787b64a590SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
797b64a590SThomas Lively    (outs), (ins function32_op:$callee), [],
807b64a590SThomas Lively    "call\t$callee", "call\t$callee", 0x10>;
817b64a590SThomas Lively
82a1d97a96SThomas Livelydefm "" : CALL<i32, I32, "i32.">;
83a1d97a96SThomas Livelydefm "" : CALL<i64, I64, "i64.">;
84a1d97a96SThomas Livelydefm "" : CALL<f32, F32, "f32.">;
85a1d97a96SThomas Livelydefm "" : CALL<f64, F64, "f64.">;
869f96a58cSHeejin Ahndefm "" : CALL<exnref, EXNREF, "exnref.", [HasExceptionHandling]>;
87a1d97a96SThomas Livelydefm "" : CALL<v16i8, V128, "v128.", [HasSIMD128]>;
88a1d97a96SThomas Livelydefm "" : CALL<v8i16, V128, "v128.", [HasSIMD128]>;
89a1d97a96SThomas Livelydefm "" : CALL<v4i32, V128, "v128.", [HasSIMD128]>;
90a1d97a96SThomas Livelydefm "" : CALL<v2i64, V128, "v128.", [HasSIMD128]>;
91a1d97a96SThomas Livelydefm "" : CALL<v4f32, V128, "v128.", [HasSIMD128]>;
92a1d97a96SThomas Livelydefm "" : CALL<v2f64, V128, "v128.", [HasSIMD128]>;
93f71abef7SDan Gohman
941a91cb04SWouter van Oortmerssenlet IsCanonical = 1 in {
957b64a590SThomas Lively
96a1d97a96SThomas Livelydefm CALL_VOID :
97a1d97a96SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
9848dac310SWouter van Oortmerssen    (outs), (ins function32_op:$callee),
99e2a7a827SDan Gohman    [(WebAssemblycall0 (i32 imm:$callee))],
10048dac310SWouter van Oortmerssen    "call    \t$callee", "call\t$callee", 0x10>;
101d934cb88SDan Gohman
102918e9055SThomas Livelylet isReturn = 1, isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in
103a1d97a96SThomas Livelydefm RET_CALL :
104a1d97a96SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
105a1d97a96SThomas Lively    (outs), (ins function32_op:$callee),
106a1d97a96SThomas Lively    [(WebAssemblyretcall (i32 imm:$callee))],
107a1d97a96SThomas Lively    "return_call    \t$callee", "return_call\t$callee", 0x12>,
108a1d97a96SThomas Lively  Requires<[HasTailCall]>;
109a1d97a96SThomas Lively
1104367587fSHeejin Ahnlet isCodeGenOnly = 1 in
111a1d97a96SThomas Livelydefm PCALL_INDIRECT_VOID :
112a1d97a96SThomas Lively  I<(outs), (ins I32:$callee, variable_ops),
11348dac310SWouter van Oortmerssen    (outs), (ins I32:$callee),
114cf4748f1SDan Gohman    [(WebAssemblycall0 I32:$callee)],
11548dac310SWouter van Oortmerssen    "PSEUDO CALL INDIRECT\t$callee",
1166f69783fSDerek Schuff    "PSEUDO CALL INDIRECT\t$callee">;
117f50d964bSDan Gohman
118a1d97a96SThomas Livelydefm CALL_INDIRECT_VOID :
119a1d97a96SThomas Lively  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
12048dac310SWouter van Oortmerssen    (outs), (ins TypeIndex:$type, i32imm:$flags),
1216f69783fSDerek Schuff    [],
12248dac310SWouter van Oortmerssen    "call_indirect\t", "call_indirect\t$type",
12348dac310SWouter van Oortmerssen    0x11>;
124a1d97a96SThomas Lively
125a1d97a96SThomas Livelylet isReturn = 1 in
126a1d97a96SThomas Livelydefm RET_CALL_INDIRECT :
127a1d97a96SThomas Lively  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
128a1d97a96SThomas Lively    (outs), (ins TypeIndex:$type, i32imm:$flags),
129a1d97a96SThomas Lively    [],
130a1d97a96SThomas Lively    "return_call_indirect\t", "return_call_indirect\t$type",
131a1d97a96SThomas Lively    0x13>,
132a1d97a96SThomas Lively  Requires<[HasTailCall]>;
133a1d97a96SThomas Lively
134a1d97a96SThomas Livelylet isCodeGenOnly = 1, isReturn = 1 in
135a1d97a96SThomas Livelydefm PRET_CALL_INDIRECT:
136a1d97a96SThomas Lively    I<(outs), (ins I32:$callee, variable_ops),
137a1d97a96SThomas Lively      (outs), (ins I32:$callee),
138a1d97a96SThomas Lively      [(WebAssemblyretcall I32:$callee)],
139a1d97a96SThomas Lively      "PSEUDO RET_CALL INDIRECT\t$callee",
140a1d97a96SThomas Lively      "PSEUDO RET_CALL INDIRECT\t$callee">,
141a1d97a96SThomas Lively    Requires<[HasTailCall]>;
142a1d97a96SThomas Lively
1434367587fSHeejin Ahn} // IsCanonical = 1
144af111db8SJF Bastien} // Uses = [SP32,SP64], isCall = 1
145fb3e0594SDan Gohman
146e2a7a827SDan Gohman// Patterns for matching a direct call to a global address.
147e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
148a1d97a96SThomas Lively          (CALL_i32 tglobaladdr:$callee)>;
149e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
150a1d97a96SThomas Lively          (CALL_i64 tglobaladdr:$callee)>;
151e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
152a1d97a96SThomas Lively          (CALL_f32 tglobaladdr:$callee)>;
153e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
154a1d97a96SThomas Lively          (CALL_f64 tglobaladdr:$callee)>;
15539bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
15639bf39f3SDerek Schuff          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
15739bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
15839bf39f3SDerek Schuff          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
15939bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16039bf39f3SDerek Schuff          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
16151ed131eSDerek Schuffdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16251ed131eSDerek Schuff          (CALL_v2i64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
16339bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16439bf39f3SDerek Schuff          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
16551ed131eSDerek Schuffdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16651ed131eSDerek Schuff          (CALL_v2f64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
1679f96a58cSHeejin Ahndef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
1689f96a58cSHeejin Ahn          (CALL_exnref tglobaladdr:$callee)>,
169a1d97a96SThomas Lively      Requires<[HasExceptionHandling]>;
170e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
171e2a7a827SDan Gohman          (CALL_VOID tglobaladdr:$callee)>;
172a1d97a96SThomas Livelydef : Pat<(WebAssemblyretcall (WebAssemblywrapper tglobaladdr:$callee)),
173a1d97a96SThomas Lively          (RET_CALL tglobaladdr:$callee)>, Requires<[HasTailCall]>;
174e2a7a827SDan Gohman
175e2a7a827SDan Gohman// Patterns for matching a direct call to an external symbol.
176e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
177a1d97a96SThomas Lively          (CALL_i32 texternalsym:$callee)>;
178e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
179a1d97a96SThomas Lively          (CALL_i64 texternalsym:$callee)>;
180e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
181a1d97a96SThomas Lively          (CALL_f32 texternalsym:$callee)>;
182e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
183a1d97a96SThomas Lively          (CALL_f64 texternalsym:$callee)>;
18439bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
18539bf39f3SDerek Schuff          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
18639bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
18739bf39f3SDerek Schuff          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
18839bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
18939bf39f3SDerek Schuff          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19051ed131eSDerek Schuffdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19151ed131eSDerek Schuff          (CALL_v2i64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19239bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19339bf39f3SDerek Schuff          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19451ed131eSDerek Schuffdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19551ed131eSDerek Schuff          (CALL_v2f64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
1969f96a58cSHeejin Ahndef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
1979f96a58cSHeejin Ahn          (CALL_exnref texternalsym:$callee)>,
198a1d97a96SThomas Lively      Requires<[HasExceptionHandling]>;
199e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
200e2a7a827SDan Gohman          (CALL_VOID texternalsym:$callee)>;
201a1d97a96SThomas Livelydef : Pat<(WebAssemblyretcall (WebAssemblywrapper texternalsym:$callee)),
202a1d97a96SThomas Lively          (RET_CALL texternalsym:$callee)>, Requires<[HasTailCall]>;
203