1//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// WebAssembly control-flow code-gen constructs.
12///
13//===----------------------------------------------------------------------===//
14
15let Defs = [ARGUMENTS] in {
16
17let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
18// The condition operand is a boolean value which WebAssembly represents as i32.
19defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
20               (outs), (ins bb_op:$dst),
21               [(brcond I32:$cond, bb:$dst)],
22                "br_if   \t$dst, $cond", "br_if   \t$dst", 0x0d>;
23let isCodeGenOnly = 1 in
24defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
25                   (outs), (ins bb_op:$dst), []>;
26let isBarrier = 1 in {
27defm BR   : NRI<(outs), (ins bb_op:$dst),
28                [(br bb:$dst)],
29                "br      \t$dst", 0x0c>;
30} // isBarrier = 1
31} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
32
33} // Defs = [ARGUMENTS]
34
35def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
36          (BR_IF bb_op:$dst, I32:$cond)>;
37def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
38          (BR_UNLESS bb_op:$dst, I32:$cond)>;
39
40let Defs = [ARGUMENTS] in {
41
42// TODO: SelectionDAG's lowering insists on using a pointer as the index for
43// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
44// currently.
45// Set TSFlags{0} to 1 to indicate that the variable_ops are immediates.
46// Set TSFlags{1} to 1 to indicate that the immediates represent labels.
47// FIXME: this can't inherit from I<> since there is no way to inherit from a
48// multiclass and still have the let statements.
49let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
50let isCodeGenOnly = 1 in
51def BR_TABLE_I32 : NI<(outs), (ins I32:$index, variable_ops),
52                      [(WebAssemblybr_table I32:$index)], 0,
53                      "br_table \t$index", 0x0e> {
54  let TSFlags{0} = 1;
55  let TSFlags{1} = 1;
56}
57def BR_TABLE_I32_S : NI<(outs), (ins variable_ops),
58                        [], 1,
59                        "br_table", 0x0e> {
60  let TSFlags{0} = 1;
61  let TSFlags{1} = 1;
62}
63let isCodeGenOnly = 1 in
64def BR_TABLE_I64 : NI<(outs), (ins I64:$index, variable_ops),
65                      [(WebAssemblybr_table I64:$index)], 0,
66                      "br_table \t$index"> {
67  let TSFlags{0} = 1;
68  let TSFlags{1} = 1;
69}
70def BR_TABLE_I64_S : NI<(outs), (ins variable_ops),
71                        [], 1,
72                        "br_table"> {
73  let TSFlags{0} = 1;
74  let TSFlags{1} = 1;
75}
76} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
77
78// This is technically a control-flow instruction, since all it affects is the
79// IP.
80defm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
81
82// Placemarkers to indicate the start or end of a block or loop scope.
83// These use/clobber VALUE_STACK to prevent them from being moved into the
84// middle of an expression tree.
85let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
86defm BLOCK     : NRI<(outs), (ins Signature:$sig), [], "block   \t$sig", 0x02>;
87defm LOOP      : NRI<(outs), (ins Signature:$sig), [], "loop    \t$sig", 0x03>;
88
89// END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode in
90// wasm.
91defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
92defm END_LOOP  : NRI<(outs), (ins), [], "end_loop", 0x0b>;
93let isTerminator = 1, isBarrier = 1 in
94defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
95} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
96
97multiclass RETURN<WebAssemblyRegClass vt> {
98  defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins),
99                      [(WebAssemblyreturn vt:$val)],
100                      "return  \t$val", "return", 0x0f>;
101  // Equivalent to RETURN_#vt, for use at the end of a function when wasm
102  // semantics return by falling off the end of the block.
103  let isCodeGenOnly = 1 in
104  defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>;
105}
106
107multiclass SIMD_RETURN<ValueType vt> {
108  defm RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
109                      [(WebAssemblyreturn (vt V128:$val))],
110                      "return  \t$val", "return", 0x0f>,
111                    Requires<[HasSIMD128]>;
112  // Equivalent to RETURN_#vt, for use at the end of a function when wasm
113  // semantics return by falling off the end of the block.
114  let isCodeGenOnly = 1 in
115  defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
116                                  []>,
117                                Requires<[HasSIMD128]>;
118}
119
120let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
121
122let isReturn = 1 in {
123  defm "": RETURN<I32>;
124  defm "": RETURN<I64>;
125  defm "": RETURN<F32>;
126  defm "": RETURN<F64>;
127  defm "": RETURN<EXCEPT_REF>;
128  defm "": SIMD_RETURN<v16i8>;
129  defm "": SIMD_RETURN<v8i16>;
130  defm "": SIMD_RETURN<v4i32>;
131  defm "": SIMD_RETURN<v2i64>;
132  defm "": SIMD_RETURN<v4f32>;
133  defm "": SIMD_RETURN<v2f64>;
134
135  defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
136
137  // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
138  let isCodeGenOnly = 1 in
139  defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>;
140} // isReturn = 1
141
142defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
143} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
144
145//===----------------------------------------------------------------------===//
146// Exception handling instructions
147//===----------------------------------------------------------------------===//
148
149let Predicates = [HasExceptionHandling] in {
150
151// Throwing an exception: throw / rethrow
152let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
153defm THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$val),
154                   (outs), (ins i32imm:$tag),
155                   [(int_wasm_throw imm:$tag, I32:$val)],
156                   "throw   \t$tag, $val", "throw   \t$tag",
157                   0x08>;
158defm THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$val),
159                   (outs), (ins i32imm:$tag),
160                   [(int_wasm_throw imm:$tag, I64:$val)],
161                   "throw   \t$tag, $val", "throw   \t$tag",
162                   0x08>;
163defm RETHROW : NRI<(outs), (ins bb_op:$dst), [], "rethrow \t$dst", 0x09>;
164let isCodeGenOnly = 1 in
165// This is used when the destination for rethrow is the caller function. This
166// will be converted to a rethrow in CFGStackify.
167defm RETHROW_TO_CALLER : NRI<(outs), (ins), [], "rethrow">;
168} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
169
170// Region within which an exception is caught: try / end_try
171let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
172defm TRY     : NRI<(outs), (ins Signature:$sig), [], "try     \t$sig", 0x06>;
173defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
174} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
175
176// Catching an exception: catch / catch_all
177let hasCtrlDep = 1, hasSideEffects = 1 in {
178defm CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag),
179                   (outs), (ins i32imm:$tag),
180                   [(set I32:$dst, (int_wasm_catch imm:$tag))],
181                   "i32.catch   \t$dst, $tag", "i32.catch   \t$tag", 0x07>;
182defm CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag),
183                   (outs), (ins i32imm:$tag),
184                   [(set I64:$dst, (int_wasm_catch imm:$tag))],
185                   "i64.catch   \t$dst, $tag", "i64.catch   \t$tag", 0x07>;
186defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>;
187}
188
189// Pseudo instructions: cleanupret / catchret
190let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
191    isCodeGenOnly = 1, isEHScopeReturn = 1 in {
192  defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "", 0>;
193  defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
194                   [(catchret bb:$dst, bb:$from)], "", 0>;
195}
196}
197
198} // Defs = [ARGUMENTS]
199