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 IsCanonical = 1, 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 : NRI<(outs), (ins i32imm:$depth), [], "rethrow \t$depth", 0x09>; 135} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 136// For C++ support, we only rethrow the latest exception, thus always setting 137// the depth to 0. 138def : Pat<(int_wasm_rethrow), (RETHROW 0)>; 139 140// Region within which an exception is caught: try / end_try 141let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 142defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; 143defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>; 144} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 145 146// Catching an exception: catch / catch_all 147let hasCtrlDep = 1, hasSideEffects = 1 in { 148// Currently 'catch' can only extract an i32, which is sufficient for C++ 149// support, but according to the spec 'catch' can extract any number of values 150// based on the event type. 151defm CATCH : I<(outs I32:$dst), (ins event_op:$tag), 152 (outs), (ins event_op:$tag), 153 [(set I32:$dst, 154 (WebAssemblycatch (WebAssemblywrapper texternalsym:$tag)))], 155 "catch \t$dst, $tag", "catch \t$tag", 0x07>; 156defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>; 157} 158 159// Pseudo instructions: cleanupret / catchret 160let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 161 isPseudo = 1, isEHScopeReturn = 1 in { 162 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>; 163 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), 164 [(catchret bb:$dst, bb:$from)], "catchret", 0>; 165} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 166 // isPseudo = 1, isEHScopeReturn = 1 167} // Predicates = [HasExceptionHandling] 168