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
57af111db8SJF Bastienlet Uses = [SP32, SP64], isCall = 1 in {
58*7b64a590SThomas Lively
59*7b64a590SThomas Lively// TODO: Split CALL into separate nodes for operands and results.
60*7b64a590SThomas Lively// TODO: Add an indirect version of the variadic call, delete CALL_*
61*7b64a590SThomas Livelylet variadicOpsAreDefs = 1 in
62*7b64a590SThomas Livelydefm CALL :
63*7b64a590SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
64*7b64a590SThomas Lively    (outs), (ins function32_op:$callee), [],
65*7b64a590SThomas Lively    "call    \t$callee", "call\t$callee", 0x10>;
66*7b64a590SThomas Lively
67a1d97a96SThomas Livelydefm "" : CALL<i32, I32, "i32.">;
68a1d97a96SThomas Livelydefm "" : CALL<i64, I64, "i64.">;
69a1d97a96SThomas Livelydefm "" : CALL<f32, F32, "f32.">;
70a1d97a96SThomas Livelydefm "" : CALL<f64, F64, "f64.">;
719f96a58cSHeejin Ahndefm "" : CALL<exnref, EXNREF, "exnref.", [HasExceptionHandling]>;
72a1d97a96SThomas Livelydefm "" : CALL<v16i8, V128, "v128.", [HasSIMD128]>;
73a1d97a96SThomas Livelydefm "" : CALL<v8i16, V128, "v128.", [HasSIMD128]>;
74a1d97a96SThomas Livelydefm "" : CALL<v4i32, V128, "v128.", [HasSIMD128]>;
75a1d97a96SThomas Livelydefm "" : CALL<v2i64, V128, "v128.", [HasSIMD128]>;
76a1d97a96SThomas Livelydefm "" : CALL<v4f32, V128, "v128.", [HasSIMD128]>;
77a1d97a96SThomas Livelydefm "" : CALL<v2f64, V128, "v128.", [HasSIMD128]>;
78f71abef7SDan Gohman
791a91cb04SWouter van Oortmerssenlet IsCanonical = 1 in {
80*7b64a590SThomas Lively
81a1d97a96SThomas Livelydefm CALL_VOID :
82a1d97a96SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
8348dac310SWouter van Oortmerssen    (outs), (ins function32_op:$callee),
84e2a7a827SDan Gohman    [(WebAssemblycall0 (i32 imm:$callee))],
8548dac310SWouter van Oortmerssen    "call    \t$callee", "call\t$callee", 0x10>;
86d934cb88SDan Gohman
87918e9055SThomas Livelylet isReturn = 1, isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in
88a1d97a96SThomas Livelydefm RET_CALL :
89a1d97a96SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
90a1d97a96SThomas Lively    (outs), (ins function32_op:$callee),
91a1d97a96SThomas Lively    [(WebAssemblyretcall (i32 imm:$callee))],
92a1d97a96SThomas Lively    "return_call    \t$callee", "return_call\t$callee", 0x12>,
93a1d97a96SThomas Lively  Requires<[HasTailCall]>;
94a1d97a96SThomas Lively
954367587fSHeejin Ahnlet isCodeGenOnly = 1 in
96a1d97a96SThomas Livelydefm PCALL_INDIRECT_VOID :
97a1d97a96SThomas Lively  I<(outs), (ins I32:$callee, variable_ops),
9848dac310SWouter van Oortmerssen    (outs), (ins I32:$callee),
99cf4748f1SDan Gohman    [(WebAssemblycall0 I32:$callee)],
10048dac310SWouter van Oortmerssen    "PSEUDO CALL INDIRECT\t$callee",
1016f69783fSDerek Schuff    "PSEUDO CALL INDIRECT\t$callee">;
102f50d964bSDan Gohman
103a1d97a96SThomas Livelydefm CALL_INDIRECT_VOID :
104a1d97a96SThomas Lively  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
10548dac310SWouter van Oortmerssen    (outs), (ins TypeIndex:$type, i32imm:$flags),
1066f69783fSDerek Schuff    [],
10748dac310SWouter van Oortmerssen    "call_indirect\t", "call_indirect\t$type",
10848dac310SWouter van Oortmerssen    0x11>;
109a1d97a96SThomas Lively
110a1d97a96SThomas Livelylet isReturn = 1 in
111a1d97a96SThomas Livelydefm RET_CALL_INDIRECT :
112a1d97a96SThomas Lively  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
113a1d97a96SThomas Lively    (outs), (ins TypeIndex:$type, i32imm:$flags),
114a1d97a96SThomas Lively    [],
115a1d97a96SThomas Lively    "return_call_indirect\t", "return_call_indirect\t$type",
116a1d97a96SThomas Lively    0x13>,
117a1d97a96SThomas Lively  Requires<[HasTailCall]>;
118a1d97a96SThomas Lively
119a1d97a96SThomas Livelylet isCodeGenOnly = 1, isReturn = 1 in
120a1d97a96SThomas Livelydefm PRET_CALL_INDIRECT:
121a1d97a96SThomas Lively    I<(outs), (ins I32:$callee, variable_ops),
122a1d97a96SThomas Lively      (outs), (ins I32:$callee),
123a1d97a96SThomas Lively      [(WebAssemblyretcall I32:$callee)],
124a1d97a96SThomas Lively      "PSEUDO RET_CALL INDIRECT\t$callee",
125a1d97a96SThomas Lively      "PSEUDO RET_CALL INDIRECT\t$callee">,
126a1d97a96SThomas Lively    Requires<[HasTailCall]>;
127a1d97a96SThomas Lively
1284367587fSHeejin Ahn} // IsCanonical = 1
129af111db8SJF Bastien} // Uses = [SP32,SP64], isCall = 1
130fb3e0594SDan Gohman
131e2a7a827SDan Gohman// Patterns for matching a direct call to a global address.
132e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
133a1d97a96SThomas Lively          (CALL_i32 tglobaladdr:$callee)>;
134e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
135a1d97a96SThomas Lively          (CALL_i64 tglobaladdr:$callee)>;
136e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
137a1d97a96SThomas Lively          (CALL_f32 tglobaladdr:$callee)>;
138e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
139a1d97a96SThomas Lively          (CALL_f64 tglobaladdr:$callee)>;
14039bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
14139bf39f3SDerek Schuff          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
14239bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
14339bf39f3SDerek Schuff          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
14439bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
14539bf39f3SDerek Schuff          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
14651ed131eSDerek Schuffdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
14751ed131eSDerek Schuff          (CALL_v2i64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
14839bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
14939bf39f3SDerek Schuff          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
15051ed131eSDerek Schuffdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
15151ed131eSDerek Schuff          (CALL_v2f64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
1529f96a58cSHeejin Ahndef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
1539f96a58cSHeejin Ahn          (CALL_exnref tglobaladdr:$callee)>,
154a1d97a96SThomas Lively      Requires<[HasExceptionHandling]>;
155e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
156e2a7a827SDan Gohman          (CALL_VOID tglobaladdr:$callee)>;
157a1d97a96SThomas Livelydef : Pat<(WebAssemblyretcall (WebAssemblywrapper tglobaladdr:$callee)),
158a1d97a96SThomas Lively          (RET_CALL tglobaladdr:$callee)>, Requires<[HasTailCall]>;
159e2a7a827SDan Gohman
160e2a7a827SDan Gohman// Patterns for matching a direct call to an external symbol.
161e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
162a1d97a96SThomas Lively          (CALL_i32 texternalsym:$callee)>;
163e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
164a1d97a96SThomas Lively          (CALL_i64 texternalsym:$callee)>;
165e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
166a1d97a96SThomas Lively          (CALL_f32 texternalsym:$callee)>;
167e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
168a1d97a96SThomas Lively          (CALL_f64 texternalsym:$callee)>;
16939bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
17039bf39f3SDerek Schuff          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
17139bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
17239bf39f3SDerek Schuff          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
17339bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
17439bf39f3SDerek Schuff          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
17551ed131eSDerek Schuffdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
17651ed131eSDerek Schuff          (CALL_v2i64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
17739bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
17839bf39f3SDerek Schuff          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
17951ed131eSDerek Schuffdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
18051ed131eSDerek Schuff          (CALL_v2f64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
1819f96a58cSHeejin Ahndef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
1829f96a58cSHeejin Ahn          (CALL_exnref texternalsym:$callee)>,
183a1d97a96SThomas Lively      Requires<[HasExceptionHandling]>;
184e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
185e2a7a827SDan Gohman          (CALL_VOID texternalsym:$callee)>;
186a1d97a96SThomas Livelydef : Pat<(WebAssemblyretcall (WebAssemblywrapper texternalsym:$callee)),
187a1d97a96SThomas Lively          (RET_CALL texternalsym:$callee)>, Requires<[HasTailCall]>;
188