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
57d5191096SThomas Lively// CALL should take both variadic arguments and produce variadic results, but
58d5191096SThomas Lively// this is not possible to model directly. Instead, we select calls to a
59d5191096SThomas Lively// CALL_PARAMS taking variadic aguments linked with a CALL_RESULTS that handles
60d5191096SThomas Lively// producing the call's variadic results. We recombine the two in a custom
61d5191096SThomas Lively// inserter hook after DAG ISel, so passes over MachineInstrs will only ever
62d5191096SThomas Lively// observe CALL nodes with all of the expected variadic uses and defs.
63d5191096SThomas Livelylet isPseudo = 1 in
64d5191096SThomas Livelydefm CALL_PARAMS :
65d5191096SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
66d5191096SThomas Lively    (outs), (ins function32_op:$callee), [],
67d5191096SThomas Lively    "call_params\t$callee", "call_params\t$callee", -1>;
68d5191096SThomas Lively
69d5191096SThomas Livelylet variadicOpsAreDefs = 1, usesCustomInserter = 1, isPseudo = 1 in
70d5191096SThomas Livelydefm CALL_RESULTS :
71d5191096SThomas Lively  I<(outs), (ins variable_ops), (outs), (ins), [],
72d5191096SThomas Lively     "call_results", "call_results", -1>;
73d5191096SThomas Lively
74af111db8SJF Bastienlet Uses = [SP32, SP64], isCall = 1 in {
757b64a590SThomas Lively
76*9d37f5afSThomas Lively// TODO: delete CALL_*
77*9d37f5afSThomas Livelylet variadicOpsAreDefs = 1 in
787b64a590SThomas Livelydefm CALL :
797b64a590SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
807b64a590SThomas Lively    (outs), (ins function32_op:$callee), [],
817b64a590SThomas Lively    "call\t$callee", "call\t$callee", 0x10>;
827b64a590SThomas Lively
83*9d37f5afSThomas Livelylet variadicOpsAreDefs = 1 in
84*9d37f5afSThomas Livelydefm CALL_INDIRECT :
85*9d37f5afSThomas Lively I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
86*9d37f5afSThomas Lively   (outs), (ins TypeIndex:$type, i32imm:$flags), [],
87*9d37f5afSThomas Lively   "call_indirect", "call_indirect\t$type", 0x11>;
88*9d37f5afSThomas Lively
89a1d97a96SThomas Livelydefm "" : CALL<i32, I32, "i32.">;
90a1d97a96SThomas Livelydefm "" : CALL<i64, I64, "i64.">;
91a1d97a96SThomas Livelydefm "" : CALL<f32, F32, "f32.">;
92a1d97a96SThomas Livelydefm "" : CALL<f64, F64, "f64.">;
939f96a58cSHeejin Ahndefm "" : CALL<exnref, EXNREF, "exnref.", [HasExceptionHandling]>;
94a1d97a96SThomas Livelydefm "" : CALL<v16i8, V128, "v128.", [HasSIMD128]>;
95a1d97a96SThomas Livelydefm "" : CALL<v8i16, V128, "v128.", [HasSIMD128]>;
96a1d97a96SThomas Livelydefm "" : CALL<v4i32, V128, "v128.", [HasSIMD128]>;
97a1d97a96SThomas Livelydefm "" : CALL<v2i64, V128, "v128.", [HasSIMD128]>;
98a1d97a96SThomas Livelydefm "" : CALL<v4f32, V128, "v128.", [HasSIMD128]>;
99a1d97a96SThomas Livelydefm "" : CALL<v2f64, V128, "v128.", [HasSIMD128]>;
100f71abef7SDan Gohman
1011a91cb04SWouter van Oortmerssenlet IsCanonical = 1 in {
1027b64a590SThomas Lively
103a1d97a96SThomas Livelydefm CALL_VOID :
104a1d97a96SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
10548dac310SWouter van Oortmerssen    (outs), (ins function32_op:$callee),
106e2a7a827SDan Gohman    [(WebAssemblycall0 (i32 imm:$callee))],
10748dac310SWouter van Oortmerssen    "call    \t$callee", "call\t$callee", 0x10>;
108d934cb88SDan Gohman
109918e9055SThomas Livelylet isReturn = 1, isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in
110a1d97a96SThomas Livelydefm RET_CALL :
111a1d97a96SThomas Lively  I<(outs), (ins function32_op:$callee, variable_ops),
112a1d97a96SThomas Lively    (outs), (ins function32_op:$callee),
113a1d97a96SThomas Lively    [(WebAssemblyretcall (i32 imm:$callee))],
114a1d97a96SThomas Lively    "return_call    \t$callee", "return_call\t$callee", 0x12>,
115a1d97a96SThomas Lively  Requires<[HasTailCall]>;
116a1d97a96SThomas Lively
1174367587fSHeejin Ahnlet isCodeGenOnly = 1 in
118a1d97a96SThomas Livelydefm PCALL_INDIRECT_VOID :
119a1d97a96SThomas Lively  I<(outs), (ins I32:$callee, variable_ops),
12048dac310SWouter van Oortmerssen    (outs), (ins I32:$callee),
121cf4748f1SDan Gohman    [(WebAssemblycall0 I32:$callee)],
12248dac310SWouter van Oortmerssen    "PSEUDO CALL INDIRECT\t$callee",
1236f69783fSDerek Schuff    "PSEUDO CALL INDIRECT\t$callee">;
124f50d964bSDan Gohman
125a1d97a96SThomas Livelydefm CALL_INDIRECT_VOID :
126a1d97a96SThomas Lively  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
12748dac310SWouter van Oortmerssen    (outs), (ins TypeIndex:$type, i32imm:$flags),
1286f69783fSDerek Schuff    [],
12948dac310SWouter van Oortmerssen    "call_indirect\t", "call_indirect\t$type",
13048dac310SWouter van Oortmerssen    0x11>;
131a1d97a96SThomas Lively
132a1d97a96SThomas Livelylet isReturn = 1 in
133a1d97a96SThomas Livelydefm RET_CALL_INDIRECT :
134a1d97a96SThomas Lively  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
135a1d97a96SThomas Lively    (outs), (ins TypeIndex:$type, i32imm:$flags),
136a1d97a96SThomas Lively    [],
137a1d97a96SThomas Lively    "return_call_indirect\t", "return_call_indirect\t$type",
138a1d97a96SThomas Lively    0x13>,
139a1d97a96SThomas Lively  Requires<[HasTailCall]>;
140a1d97a96SThomas Lively
141a1d97a96SThomas Livelylet isCodeGenOnly = 1, isReturn = 1 in
142a1d97a96SThomas Livelydefm PRET_CALL_INDIRECT:
143a1d97a96SThomas Lively    I<(outs), (ins I32:$callee, variable_ops),
144a1d97a96SThomas Lively      (outs), (ins I32:$callee),
145a1d97a96SThomas Lively      [(WebAssemblyretcall I32:$callee)],
146a1d97a96SThomas Lively      "PSEUDO RET_CALL INDIRECT\t$callee",
147a1d97a96SThomas Lively      "PSEUDO RET_CALL INDIRECT\t$callee">,
148a1d97a96SThomas Lively    Requires<[HasTailCall]>;
149a1d97a96SThomas Lively
1504367587fSHeejin Ahn} // IsCanonical = 1
151af111db8SJF Bastien} // Uses = [SP32,SP64], isCall = 1
152fb3e0594SDan Gohman
153e2a7a827SDan Gohman// Patterns for matching a direct call to a global address.
154e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
155a1d97a96SThomas Lively          (CALL_i32 tglobaladdr:$callee)>;
156e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
157a1d97a96SThomas Lively          (CALL_i64 tglobaladdr:$callee)>;
158e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
159a1d97a96SThomas Lively          (CALL_f32 tglobaladdr:$callee)>;
160e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
161a1d97a96SThomas Lively          (CALL_f64 tglobaladdr:$callee)>;
16239bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16339bf39f3SDerek Schuff          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
16439bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16539bf39f3SDerek Schuff          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
16639bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16739bf39f3SDerek Schuff          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
16851ed131eSDerek Schuffdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
16951ed131eSDerek Schuff          (CALL_v2i64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
17039bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
17139bf39f3SDerek Schuff          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
17251ed131eSDerek Schuffdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
17351ed131eSDerek Schuff          (CALL_v2f64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
1749f96a58cSHeejin Ahndef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
1759f96a58cSHeejin Ahn          (CALL_exnref tglobaladdr:$callee)>,
176a1d97a96SThomas Lively      Requires<[HasExceptionHandling]>;
177e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
178e2a7a827SDan Gohman          (CALL_VOID tglobaladdr:$callee)>;
179a1d97a96SThomas Livelydef : Pat<(WebAssemblyretcall (WebAssemblywrapper tglobaladdr:$callee)),
180a1d97a96SThomas Lively          (RET_CALL tglobaladdr:$callee)>, Requires<[HasTailCall]>;
181e2a7a827SDan Gohman
182e2a7a827SDan Gohman// Patterns for matching a direct call to an external symbol.
183e2a7a827SDan Gohmandef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
184a1d97a96SThomas Lively          (CALL_i32 texternalsym:$callee)>;
185e2a7a827SDan Gohmandef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
186a1d97a96SThomas Lively          (CALL_i64 texternalsym:$callee)>;
187e2a7a827SDan Gohmandef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
188a1d97a96SThomas Lively          (CALL_f32 texternalsym:$callee)>;
189e2a7a827SDan Gohmandef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
190a1d97a96SThomas Lively          (CALL_f64 texternalsym:$callee)>;
19139bf39f3SDerek Schuffdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19239bf39f3SDerek Schuff          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19339bf39f3SDerek Schuffdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19439bf39f3SDerek Schuff          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19539bf39f3SDerek Schuffdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19639bf39f3SDerek Schuff          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19751ed131eSDerek Schuffdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
19851ed131eSDerek Schuff          (CALL_v2i64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
19939bf39f3SDerek Schuffdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
20039bf39f3SDerek Schuff          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
20151ed131eSDerek Schuffdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
20251ed131eSDerek Schuff          (CALL_v2f64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
2039f96a58cSHeejin Ahndef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
2049f96a58cSHeejin Ahn          (CALL_exnref texternalsym:$callee)>,
205a1d97a96SThomas Lively      Requires<[HasExceptionHandling]>;
206e2a7a827SDan Gohmandef : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
207e2a7a827SDan Gohman          (CALL_VOID texternalsym:$callee)>;
208a1d97a96SThomas Livelydef : Pat<(WebAssemblyretcall (WebAssemblywrapper texternalsym:$callee)),
209a1d97a96SThomas Lively          (RET_CALL texternalsym:$callee)>, Requires<[HasTailCall]>;
210