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