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// Duplicating a BR_TABLE is almost never a good idea. In particular, it can 44// lead to some nasty irreducibility due to tail merging when the br_table is in 45// a loop. 46let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 in { 47 48defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops), 49 (outs), (ins brlist:$brl), 50 [(WebAssemblybr_table I32:$index)], 51 "br_table \t$index", "br_table \t$brl", 52 0x0e>; 53// TODO: SelectionDAG's lowering insists on using a pointer as the index for 54// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode 55// currently. 56defm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops), 57 (outs), (ins brlist:$brl), 58 [(WebAssemblybr_table I64:$index)], 59 "br_table \t$index", "br_table \t$brl", 60 0x0e>; 61} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 62 63// This is technically a control-flow instruction, since all it affects is the 64// IP. 65defm NOP : NRI<(outs), (ins), [], "nop", 0x01>; 66 67// Placemarkers to indicate the start or end of a block or loop scope. 68// These use/clobber VALUE_STACK to prevent them from being moved into the 69// middle of an expression tree. 70let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 71defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>; 72defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>; 73 74defm IF : I<(outs), (ins Signature:$sig, I32:$cond), 75 (outs), (ins Signature:$sig), 76 [], "if \t$sig, $cond", "if \t$sig", 0x04>; 77defm ELSE : NRI<(outs), (ins), [], "else", 0x05>; 78 79// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same 80// opcode in wasm. 81defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>; 82defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>; 83defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>; 84// Generic instruction, for disassembler. 85let IsCanonical = 1 in 86defm END : NRI<(outs), (ins), [], "end", 0x0b>; 87let isTerminator = 1, isBarrier = 1 in 88defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>; 89} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 90 91 92let hasCtrlDep = 1, isBarrier = 1 in { 93let isTerminator = 1 in { 94let isReturn = 1 in { 95 96defm RETURN : I<(outs), (ins variable_ops), (outs), (ins), 97 [(WebAssemblyreturn)], 98 "return", "return", 0x0f>; 99// Equivalent to RETURN, for use at the end of a function when wasm 100// semantics return by falling off the end of the block. 101let isCodeGenOnly = 1 in 102defm FALLTHROUGH_RETURN : I<(outs), (ins variable_ops), (outs), (ins), []>; 103 104} // isReturn = 1 105 106let isTrap = 1 in 107defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>; 108 109} // isTerminator = 1 110 111// debugtrap explicitly returns despite trapping because it is supposed to just 112// get the attention of the debugger. Unfortunately, because UNREACHABLE is a 113// terminator, lowering debugtrap to UNREACHABLE can create an invalid 114// MachineBasicBlock when there is additional code after it. Lower it to this 115// non-terminator version instead. 116// TODO: Actually execute the debugger statement when running on the Web 117let isTrap = 1 in 118defm DEBUG_UNREACHABLE : NRI<(outs), (ins), [(debugtrap)], "unreachable", 0x00>; 119 120} // hasCtrlDep = 1, isBarrier = 1 121 122//===----------------------------------------------------------------------===// 123// Exception handling instructions 124//===----------------------------------------------------------------------===// 125 126let Predicates = [HasExceptionHandling] in { 127 128// Throwing an exception: throw / rethrow 129let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { 130defm THROW : I<(outs), (ins event_op:$tag, variable_ops), 131 (outs), (ins event_op:$tag), 132 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))], 133 "throw \t$tag", "throw \t$tag", 0x08>; 134defm RETHROW : I<(outs), (ins EXNREF:$exn), (outs), (ins), [], 135 "rethrow \t$exn", "rethrow", 0x09>; 136// Pseudo instruction to be the lowering target of int_wasm_rethrow_in_catch 137// intrinsic. Will be converted to the real rethrow instruction later. 138let isPseudo = 1 in 139defm RETHROW_IN_CATCH : NRI<(outs), (ins), [(int_wasm_rethrow_in_catch)], 140 "rethrow_in_catch", 0>; 141} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 142 143// Region within which an exception is caught: try / end_try 144let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 145defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; 146defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>; 147} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 148 149// Catching an exception: catch / extract_exception 150let hasCtrlDep = 1, hasSideEffects = 1 in 151defm CATCH : I<(outs EXNREF:$dst), (ins), (outs), (ins), [], 152 "catch \t$dst", "catch", 0x07>; 153 154// Querying / extracing exception: br_on_exn 155// br_on_exn queries an exnref to see if it matches the corresponding exception 156// tag index. If true it branches to the given label and pushes the 157// corresponding argument values of the exception onto the stack. 158let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in 159defm BR_ON_EXN : I<(outs), (ins bb_op:$dst, event_op:$tag, EXNREF:$exn), 160 (outs), (ins bb_op:$dst, event_op:$tag), [], 161 "br_on_exn \t$dst, $tag, $exn", "br_on_exn \t$dst, $tag", 162 0x0a>; 163// This is a pseudo instruction that simulates popping a value from stack, which 164// has been pushed by br_on_exn 165let isCodeGenOnly = 1, hasSideEffects = 1 in 166defm EXTRACT_EXCEPTION_I32 : NRI<(outs I32:$dst), (ins), 167 [(set I32:$dst, (int_wasm_extract_exception))], 168 "extract_exception\t$dst">; 169 170// Pseudo instructions: cleanupret / catchret 171let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 172 isPseudo = 1, isEHScopeReturn = 1 in { 173 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>; 174 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), 175 [(catchret bb:$dst, bb:$from)], "catchret", 0>; 176} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 177 // isPseudo = 1, isEHScopeReturn = 1 178} // Predicates = [HasExceptionHandling] 179