1286425Sdim//===- WebAssemblyInstrCall.td-WebAssembly Call codegen support -*- tablegen -*-
2286425Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6286425Sdim//
7286425Sdim//===----------------------------------------------------------------------===//
8286425Sdim///
9286425Sdim/// \file
10341825Sdim/// WebAssembly Call operand code-gen constructs.
11286425Sdim///
12286425Sdim//===----------------------------------------------------------------------===//
13286425Sdim
14296417Sdim// TODO: addr64: These currently assume the callee address is 32-bit.
15341825Sdim// FIXME: add $type to first call_indirect asmstr (and maybe $flags)
16296417Sdim
17296417Sdim// Call sequence markers. These have an immediate which represents the amount of
18296417Sdim// stack space to allocate or free, which is used for varargs lowering.
19296417Sdimlet Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1 in {
20341825Sdimdefm ADJCALLSTACKDOWN : NRI<(outs), (ins i32imm:$amt, i32imm:$amt2),
21341825Sdim                            [(WebAssemblycallseq_start timm:$amt, timm:$amt2)]>;
22341825Sdimdefm ADJCALLSTACKUP : NRI<(outs), (ins i32imm:$amt, i32imm:$amt2),
23341825Sdim                          [(WebAssemblycallseq_end timm:$amt, timm:$amt2)]>;
24353358Sdim} // Uses = [SP32, SP64], Defs = [SP32, SP64], isCodeGenOnly = 1
25296417Sdim
26353358Sdimmulticlass CALL<ValueType vt, WebAssemblyRegClass rt, string prefix,
27353358Sdim                list<Predicate> preds = []> {
28353358Sdim  defm CALL_#vt :
29353358Sdim    I<(outs rt:$dst), (ins function32_op:$callee, variable_ops),
30353358Sdim      (outs), (ins function32_op:$callee),
31353358Sdim      [(set (vt rt:$dst), (WebAssemblycall1 (i32 imm:$callee)))],
32353358Sdim      !strconcat(prefix, "call\t$dst, $callee"),
33353358Sdim      !strconcat(prefix, "call\t$callee"),
34353358Sdim      0x10>,
35353358Sdim    Requires<preds>;
36321369Sdim
37353358Sdim  let isCodeGenOnly = 1 in
38353358Sdim  defm PCALL_INDIRECT_#vt :
39353358Sdim    I<(outs rt:$dst), (ins I32:$callee, variable_ops),
40353358Sdim      (outs), (ins I32:$callee),
41353358Sdim      [(set (vt rt:$dst), (WebAssemblycall1 I32:$callee))],
42353358Sdim      "PSEUDO CALL INDIRECT\t$callee",
43353358Sdim      "PSEUDO CALL INDIRECT\t$callee">,
44353358Sdim    Requires<preds>;
45314564Sdim
46353358Sdim  defm CALL_INDIRECT_#vt :
47353358Sdim    I<(outs rt:$dst),
48353358Sdim      (ins TypeIndex:$type, i32imm:$flags, variable_ops),
49353358Sdim      (outs), (ins TypeIndex:$type, i32imm:$flags),
50353358Sdim      [],
51353358Sdim      !strconcat(prefix, "call_indirect\t$dst"),
52353358Sdim      !strconcat(prefix, "call_indirect\t$type"),
53353358Sdim      0x11>,
54353358Sdim    Requires<preds>;
55296417Sdim}
56314564Sdim
57353358Sdimlet Uses = [SP32, SP64], isCall = 1 in {
58353358Sdimdefm "" : CALL<i32, I32, "i32.">;
59353358Sdimdefm "" : CALL<i64, I64, "i64.">;
60353358Sdimdefm "" : CALL<f32, F32, "f32.">;
61353358Sdimdefm "" : CALL<f64, F64, "f64.">;
62353358Sdimdefm "" : CALL<exnref, EXNREF, "exnref.", [HasExceptionHandling]>;
63353358Sdimdefm "" : CALL<v16i8, V128, "v128.", [HasSIMD128]>;
64353358Sdimdefm "" : CALL<v8i16, V128, "v128.", [HasSIMD128]>;
65353358Sdimdefm "" : CALL<v4i32, V128, "v128.", [HasSIMD128]>;
66353358Sdimdefm "" : CALL<v2i64, V128, "v128.", [HasSIMD128]>;
67353358Sdimdefm "" : CALL<v4f32, V128, "v128.", [HasSIMD128]>;
68353358Sdimdefm "" : CALL<v2f64, V128, "v128.", [HasSIMD128]>;
69321369Sdim
70353358Sdimlet IsCanonical = 1 in {
71353358Sdimdefm CALL_VOID :
72353358Sdim  I<(outs), (ins function32_op:$callee, variable_ops),
73353358Sdim    (outs), (ins function32_op:$callee),
74353358Sdim    [(WebAssemblycall0 (i32 imm:$callee))],
75353358Sdim    "call    \t$callee", "call\t$callee", 0x10>;
76344779Sdim
77353358Sdimlet isReturn = 1 in
78353358Sdimdefm RET_CALL :
79353358Sdim  I<(outs), (ins function32_op:$callee, variable_ops),
80353358Sdim    (outs), (ins function32_op:$callee),
81353358Sdim    [(WebAssemblyretcall (i32 imm:$callee))],
82353358Sdim    "return_call    \t$callee", "return_call\t$callee", 0x12>,
83353358Sdim  Requires<[HasTailCall]>;
84314564Sdim
85353358Sdimlet isCodeGenOnly = 1 in
86353358Sdimdefm PCALL_INDIRECT_VOID :
87353358Sdim  I<(outs), (ins I32:$callee, variable_ops),
88353358Sdim    (outs), (ins I32:$callee),
89353358Sdim    [(WebAssemblycall0 I32:$callee)],
90353358Sdim    "PSEUDO CALL INDIRECT\t$callee",
91353358Sdim    "PSEUDO CALL INDIRECT\t$callee">;
92314564Sdim
93353358Sdimdefm CALL_INDIRECT_VOID :
94353358Sdim  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
95353358Sdim    (outs), (ins TypeIndex:$type, i32imm:$flags),
96353358Sdim    [],
97353358Sdim    "call_indirect\t", "call_indirect\t$type",
98353358Sdim    0x11>;
99296417Sdim
100353358Sdimlet isReturn = 1 in
101353358Sdimdefm RET_CALL_INDIRECT :
102353358Sdim  I<(outs), (ins TypeIndex:$type, i32imm:$flags, variable_ops),
103353358Sdim    (outs), (ins TypeIndex:$type, i32imm:$flags),
104353358Sdim    [],
105353358Sdim    "return_call_indirect\t", "return_call_indirect\t$type",
106353358Sdim    0x13>,
107353358Sdim  Requires<[HasTailCall]>;
108321369Sdim
109353358Sdimlet isCodeGenOnly = 1, isReturn = 1 in
110353358Sdimdefm PRET_CALL_INDIRECT:
111353358Sdim    I<(outs), (ins I32:$callee, variable_ops),
112353358Sdim      (outs), (ins I32:$callee),
113353358Sdim      [(WebAssemblyretcall I32:$callee)],
114353358Sdim      "PSEUDO RET_CALL INDIRECT\t$callee",
115353358Sdim      "PSEUDO RET_CALL INDIRECT\t$callee">,
116353358Sdim    Requires<[HasTailCall]>;
117314564Sdim
118353358Sdim} // IsCanonical = 1
119296417Sdim} // Uses = [SP32,SP64], isCall = 1
120296417Sdim
121296417Sdim// Patterns for matching a direct call to a global address.
122296417Sdimdef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
123353358Sdim          (CALL_i32 tglobaladdr:$callee)>;
124296417Sdimdef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
125353358Sdim          (CALL_i64 tglobaladdr:$callee)>;
126296417Sdimdef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
127353358Sdim          (CALL_f32 tglobaladdr:$callee)>;
128296417Sdimdef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
129353358Sdim          (CALL_f64 tglobaladdr:$callee)>;
130314564Sdimdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
131314564Sdim          (CALL_v16i8 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
132314564Sdimdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
133314564Sdim          (CALL_v8i16 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
134314564Sdimdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
135314564Sdim          (CALL_v4i32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
136344779Sdimdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
137344779Sdim          (CALL_v2i64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
138314564Sdimdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
139314564Sdim          (CALL_v4f32 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
140344779Sdimdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
141344779Sdim          (CALL_v2f64 tglobaladdr:$callee)>, Requires<[HasSIMD128]>;
142353358Sdimdef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper tglobaladdr:$callee))),
143353358Sdim          (CALL_exnref tglobaladdr:$callee)>,
144353358Sdim      Requires<[HasExceptionHandling]>;
145296417Sdimdef : Pat<(WebAssemblycall0 (WebAssemblywrapper tglobaladdr:$callee)),
146296417Sdim          (CALL_VOID tglobaladdr:$callee)>;
147353358Sdimdef : Pat<(WebAssemblyretcall (WebAssemblywrapper tglobaladdr:$callee)),
148353358Sdim          (RET_CALL tglobaladdr:$callee)>, Requires<[HasTailCall]>;
149296417Sdim
150296417Sdim// Patterns for matching a direct call to an external symbol.
151296417Sdimdef : Pat<(i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
152353358Sdim          (CALL_i32 texternalsym:$callee)>;
153296417Sdimdef : Pat<(i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
154353358Sdim          (CALL_i64 texternalsym:$callee)>;
155296417Sdimdef : Pat<(f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
156353358Sdim          (CALL_f32 texternalsym:$callee)>;
157296417Sdimdef : Pat<(f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
158353358Sdim          (CALL_f64 texternalsym:$callee)>;
159314564Sdimdef : Pat<(v16i8 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
160314564Sdim          (CALL_v16i8 texternalsym:$callee)>, Requires<[HasSIMD128]>;
161314564Sdimdef : Pat<(v8i16 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
162314564Sdim          (CALL_v8i16 texternalsym:$callee)>, Requires<[HasSIMD128]>;
163314564Sdimdef : Pat<(v4i32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
164314564Sdim          (CALL_v4i32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
165344779Sdimdef : Pat<(v2i64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
166344779Sdim          (CALL_v2i64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
167314564Sdimdef : Pat<(v4f32 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
168314564Sdim          (CALL_v4f32 texternalsym:$callee)>, Requires<[HasSIMD128]>;
169344779Sdimdef : Pat<(v2f64 (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
170344779Sdim          (CALL_v2f64 texternalsym:$callee)>, Requires<[HasSIMD128]>;
171353358Sdimdef : Pat<(exnref (WebAssemblycall1 (WebAssemblywrapper texternalsym:$callee))),
172353358Sdim          (CALL_exnref texternalsym:$callee)>,
173353358Sdim      Requires<[HasExceptionHandling]>;
174296417Sdimdef : Pat<(WebAssemblycall0 (WebAssemblywrapper texternalsym:$callee)),
175296417Sdim          (CALL_VOID texternalsym:$callee)>;
176353358Sdimdef : Pat<(WebAssemblyretcall (WebAssemblywrapper texternalsym:$callee)),
177353358Sdim          (RET_CALL texternalsym:$callee)>, Requires<[HasTailCall]>;
178