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 or loop scope. These
61 // use/clobber VALUE_STACK to prevent them from being moved into the middle of
62 // 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>;
67 // END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode
69 def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>;
70 def END_LOOP : I<(outs), (ins), [], "end_loop", 0x0b>;
71 let isTerminator = 1, isBarrier = 1 in
72 def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>;
73 } // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
75 multiclass RETURN<WebAssemblyRegClass vt> {
76 def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)],
77 "return \t$val", 0x0f>;
78 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
79 // semantics return by falling off the end of the block.
80 let isCodeGenOnly = 1 in
81 def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>;
84 multiclass SIMD_RETURN<ValueType vt> {
85 def RETURN_#vt : SIMD_I<(outs), (ins V128:$val),
86 [(WebAssemblyreturn (vt V128:$val))],
87 "return \t$val", 0x0f>;
88 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
89 // semantics return by falling off the end of the block.
90 let isCodeGenOnly = 1 in
91 def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>;
94 let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
101 defm : SIMD_RETURN<v16i8>;
102 defm : SIMD_RETURN<v8i16>;
103 defm : SIMD_RETURN<v4i32>;
104 defm : SIMD_RETURN<v4f32>;
106 def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
108 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
109 let isCodeGenOnly = 1 in
110 def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>;
113 def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>;
115 } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
117 } // Defs = [ARGUMENTS]