WebAssemblyInstrControl.td revision 321369
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/// \brief 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.
19def BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
20              [(brcond I32:$cond, bb:$dst)],
21               "br_if   \t$dst, $cond", 0x0d>;
22let isCodeGenOnly = 1 in
23def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), []>;
24let isBarrier = 1 in {
25def BR   : I<(outs), (ins bb_op:$dst),
26             [(br bb:$dst)],
27             "br      \t$dst", 0x0c>;
28} // isBarrier = 1
29} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
30
31} // Defs = [ARGUMENTS]
32
33def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
34          (BR_IF bb_op:$dst, I32:$cond)>;
35def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
36          (BR_UNLESS bb_op:$dst, I32:$cond)>;
37
38let Defs = [ARGUMENTS] in {
39
40// TODO: SelectionDAG's lowering insists on using a pointer as the index for
41// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
42// currently.
43// Set TSFlags{0} to 1 to indicate that the variable_ops are immediates.
44// Set TSFlags{1} to 1 to indicate that the immediates represent labels.
45let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
46def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
47                     [(WebAssemblybr_table I32:$index)],
48                     "br_table \t$index", 0x0e> {
49  let TSFlags{0} = 1;
50  let TSFlags{1} = 1;
51}
52def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
53                     [(WebAssemblybr_table I64:$index)],
54                     "br_table \t$index"> {
55  let TSFlags{0} = 1;
56  let TSFlags{1} = 1;
57}
58} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
59
60// Placemarkers to indicate the start or end of a block, loop, or try scope.
61// These use/clobber VALUE_STACK to prevent them from being moved into the
62// middle of an expression tree.
63let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
64def BLOCK     : I<(outs), (ins Signature:$sig), [], "block   \t$sig", 0x02>;
65def LOOP      : I<(outs), (ins Signature:$sig), [], "loop    \t$sig", 0x03>;
66def TRY       : I<(outs), (ins Signature:$sig), [], "try     \t$sig", 0x06>;
67
68// END_BLOCK, END_LOOP, END_TRY, and END_FUNCTION are represented with the same
69// opcode in wasm.
70def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>;
71def END_LOOP  : I<(outs), (ins), [], "end_loop", 0x0b>;
72def END_TRY   : I<(outs), (ins), [], "end_try", 0x0b>;
73let isTerminator = 1, isBarrier = 1 in
74def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>;
75} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
76
77multiclass RETURN<WebAssemblyRegClass vt> {
78  def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)],
79                     "return  \t$val", 0x0f>;
80  // Equivalent to RETURN_#vt, for use at the end of a function when wasm
81  // semantics return by falling off the end of the block.
82  let isCodeGenOnly = 1 in
83  def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>;
84}
85
86multiclass SIMD_RETURN<ValueType vt> {
87  def RETURN_#vt : SIMD_I<(outs), (ins V128:$val),
88                          [(WebAssemblyreturn (vt V128:$val))],
89                          "return  \t$val", 0x0f>;
90  // Equivalent to RETURN_#vt, for use at the end of a function when wasm
91  // semantics return by falling off the end of the block.
92  let isCodeGenOnly = 1 in
93  def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>;
94}
95
96let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
97
98let isReturn = 1 in {
99  defm : RETURN<I32>;
100  defm : RETURN<I64>;
101  defm : RETURN<F32>;
102  defm : RETURN<F64>;
103  defm : SIMD_RETURN<v16i8>;
104  defm : SIMD_RETURN<v8i16>;
105  defm : SIMD_RETURN<v4i32>;
106  defm : SIMD_RETURN<v4f32>;
107
108  def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
109
110  // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
111  let isCodeGenOnly = 1 in
112  def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>;
113} // isReturn = 1
114
115def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>;
116
117def THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$obj),
118                  [(int_wasm_throw imm:$tag, I32:$obj)], "throw   \t$tag, $obj",
119                  0x08>;
120def THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$obj),
121                  [(int_wasm_throw imm:$tag, I64:$obj)], "throw   \t$tag, $obj",
122                  0x08>;
123def RETHROW : I<(outs), (ins i32imm:$rel_depth), [], "rethrow \t$rel_depth",
124                0x09>;
125
126} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
127
128} // Defs = [ARGUMENTS]
129
130// rethrow takes a relative depth as an argument, for which currently only 0 is
131// possible for C++. Once other languages need depths other than 0, depths will
132// be computed in CFGStackify.
133def : Pat<(int_wasm_rethrow), (RETHROW 0)>;
134