1 //===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief WebAssembly control-flow code-gen constructs.
13 //===----------------------------------------------------------------------===//
15 let Defs = [ARGUMENTS] in {
17 let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
18 // The condition operand is a boolean value which WebAssembly represents as i32.
19 def BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
20 [(brcond I32:$cond, bb:$dst)],
21 "br_if \t$dst, $cond", 0x0d>;
22 let isCodeGenOnly = 1 in
23 def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), []>;
24 let isBarrier = 1 in {
25 def BR : I<(outs), (ins bb_op:$dst),
29 } // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
31 } // Defs = [ARGUMENTS]
33 def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
34 (BR_IF bb_op:$dst, I32:$cond)>;
35 def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
36 (BR_UNLESS bb_op:$dst, I32:$cond)>;
38 let Defs = [ARGUMENTS] in {
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
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.
45 let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
46 def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
47 [(WebAssemblybr_table I32:$index)],
48 "br_table \t$index", 0x0e> {
52 def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
53 [(WebAssemblybr_table I64:$index)],
54 "br_table \t$index"> {
58 } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
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.
63 let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
64 def BLOCK : I<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
65 def LOOP : I<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
66 def TRY : I<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
68 // END_BLOCK, END_LOOP, END_TRY, and END_FUNCTION are represented with the same
70 def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>;
71 def END_LOOP : I<(outs), (ins), [], "end_loop", 0x0b>;
72 def END_TRY : I<(outs), (ins), [], "end_try", 0x0b>;
73 let isTerminator = 1, isBarrier = 1 in
74 def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>;
75 } // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
77 multiclass 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), []>;
86 multiclass 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), []>;
96 let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
103 defm : SIMD_RETURN<v16i8>;
104 defm : SIMD_RETURN<v8i16>;
105 defm : SIMD_RETURN<v4i32>;
106 defm : SIMD_RETURN<v4f32>;
108 def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
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), []>;
115 def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>;
117 def THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$obj),
118 [(int_wasm_throw imm:$tag, I32:$obj)], "throw \t$tag, $obj",
120 def THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$obj),
121 [(int_wasm_throw imm:$tag, I64:$obj)], "throw \t$tag, $obj",
123 def RETHROW : I<(outs), (ins i32imm:$rel_depth), [], "rethrow \t$rel_depth",
126 } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
128 } // Defs = [ARGUMENTS]
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.
133 def : Pat<(int_wasm_rethrow), (RETHROW 0)>;