1//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*- 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8/// 9/// \file 10/// WebAssembly control-flow code-gen constructs. 11/// 12//===----------------------------------------------------------------------===// 13 14let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in { 15// The condition operand is a boolean value which WebAssembly represents as i32. 16defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond), 17 (outs), (ins bb_op:$dst), 18 [(brcond I32:$cond, bb:$dst)], 19 "br_if \t$dst, $cond", "br_if \t$dst", 0x0d>; 20let isCodeGenOnly = 1 in 21defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), 22 (outs), (ins bb_op:$dst), []>; 23let isBarrier = 1 in 24defm BR : NRI<(outs), (ins bb_op:$dst), 25 [(br bb:$dst)], 26 "br \t$dst", 0x0c>; 27} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 28 29def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst), 30 (BR_IF bb_op:$dst, I32:$cond)>; 31def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst), 32 (BR_UNLESS bb_op:$dst, I32:$cond)>; 33 34// A list of branch targets enclosed in {} and separated by comma. 35// Used by br_table only. 36def BrListAsmOperand : AsmOperandClass { let Name = "BrList"; } 37let OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in 38def brlist : Operand<i32> { 39 let ParserMatchClass = BrListAsmOperand; 40 let PrintMethod = "printBrList"; 41} 42 43// TODO: SelectionDAG's lowering insists on using a pointer as the index for 44// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode 45// currently. 46let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { 47defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops), 48 (outs), (ins brlist:$brl), 49 [(WebAssemblybr_table I32:$index)], 50 "br_table \t$index", "br_table \t$brl", 51 0x0e>; 52defm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops), 53 (outs), (ins brlist:$brl), 54 [(WebAssemblybr_table I64:$index)], 55 "br_table \t$index", "br_table \t$brl", 56 0x0e>; 57} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 58 59// This is technically a control-flow instruction, since all it affects is the 60// IP. 61defm NOP : NRI<(outs), (ins), [], "nop", 0x01>; 62 63// Placemarkers to indicate the start or end of a block or loop scope. 64// These use/clobber VALUE_STACK to prevent them from being moved into the 65// middle of an expression tree. 66let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 67defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>; 68defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>; 69 70defm IF : I<(outs), (ins Signature:$sig, I32:$cond), 71 (outs), (ins Signature:$sig), 72 [], "if \t$sig, $cond", "if \t$sig", 0x04>; 73defm ELSE : NRI<(outs), (ins), [], "else", 0x05>; 74 75// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same 76// opcode in wasm. 77defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>; 78defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>; 79defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>; 80// Generic instruction, for disassembler. 81let IsCanonical = 1 in 82defm END : NRI<(outs), (ins), [], "end", 0x0b>; 83let isTerminator = 1, isBarrier = 1 in 84defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>; 85} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 86 87multiclass RETURN<WebAssemblyRegClass vt> { 88 defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), 89 [(WebAssemblyreturn vt:$val)], 90 "return \t$val", "return", 0x0f>; 91 // Equivalent to RETURN_#vt, for use at the end of a function when wasm 92 // semantics return by falling off the end of the block. 93 let isCodeGenOnly = 1 in 94 defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>; 95} 96 97multiclass SIMD_RETURN<ValueType vt> { 98 defm RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins), 99 [(WebAssemblyreturn (vt V128:$val))], 100 "return \t$val", "return", 0x0f>, 101 Requires<[HasSIMD128]>; 102 // Equivalent to RETURN_#vt, for use at the end of a function when wasm 103 // semantics return by falling off the end of the block. 104 let isCodeGenOnly = 1 in 105 defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins), 106 []>, 107 Requires<[HasSIMD128]>; 108} 109 110let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { 111 112let isReturn = 1 in { 113 defm "": RETURN<I32>; 114 defm "": RETURN<I64>; 115 defm "": RETURN<F32>; 116 defm "": RETURN<F64>; 117 defm "": RETURN<EXNREF>; 118 defm "": SIMD_RETURN<v16i8>; 119 defm "": SIMD_RETURN<v8i16>; 120 defm "": SIMD_RETURN<v4i32>; 121 defm "": SIMD_RETURN<v2i64>; 122 defm "": SIMD_RETURN<v4f32>; 123 defm "": SIMD_RETURN<v2f64>; 124 125 defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>; 126 127 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt. 128 let isCodeGenOnly = 1 in 129 defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>; 130} // isReturn = 1 131 132defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>; 133} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 134 135//===----------------------------------------------------------------------===// 136// Exception handling instructions 137//===----------------------------------------------------------------------===// 138 139let Predicates = [HasExceptionHandling] in { 140 141// Throwing an exception: throw / rethrow 142let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { 143defm THROW : I<(outs), (ins event_op:$tag, variable_ops), 144 (outs), (ins event_op:$tag), 145 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))], 146 "throw \t$tag", "throw \t$tag", 0x08>; 147defm RETHROW : I<(outs), (ins EXNREF:$exn), (outs), (ins), [], 148 "rethrow \t$exn", "rethrow", 0x09>; 149// Pseudo instruction to be the lowering target of int_wasm_rethrow_in_catch 150// intrinsic. Will be converted to the real rethrow instruction later. 151let isPseudo = 1 in 152defm RETHROW_IN_CATCH : NRI<(outs), (ins), [(int_wasm_rethrow_in_catch)], 153 "rethrow_in_catch", 0>; 154} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 155 156// Region within which an exception is caught: try / end_try 157let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 158defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; 159defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>; 160} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 161 162// Catching an exception: catch / extract_exception 163let hasCtrlDep = 1, hasSideEffects = 1 in 164defm CATCH : I<(outs EXNREF:$dst), (ins), (outs), (ins), [], 165 "catch \t$dst", "catch", 0x07>; 166 167// Querying / extracing exception: br_on_exn 168// br_on_exn queries an exnref to see if it matches the corresponding exception 169// tag index. If true it branches to the given label and pushes the 170// corresponding argument values of the exception onto the stack. 171let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in 172defm BR_ON_EXN : I<(outs), (ins bb_op:$dst, event_op:$tag, EXNREF:$exn), 173 (outs), (ins bb_op:$dst, event_op:$tag), [], 174 "br_on_exn \t$dst, $tag, $exn", "br_on_exn \t$dst, $tag", 175 0x0a>; 176// This is a pseudo instruction that simulates popping a value from stack, which 177// has been pushed by br_on_exn 178let isCodeGenOnly = 1, hasSideEffects = 1 in 179defm EXTRACT_EXCEPTION_I32 : NRI<(outs I32:$dst), (ins), 180 [(set I32:$dst, (int_wasm_extract_exception))], 181 "extract_exception\t$dst">; 182 183// Pseudo instructions: cleanupret / catchret 184let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 185 isPseudo = 1, isEHScopeReturn = 1 in { 186 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>; 187 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), 188 [(catchret bb:$dst, bb:$from)], "catchret", 0>; 189} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 190 // isPseudo = 1, isEHScopeReturn = 1 191} // Predicates = [HasExceptionHandling] 192