xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
20b57cec5SDimitry Andric//
30b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric//
70b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric///
90b57cec5SDimitry Andric/// \file
100b57cec5SDimitry Andric/// WebAssembly control-flow code-gen constructs.
110b57cec5SDimitry Andric///
120b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andriclet isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
150b57cec5SDimitry Andric// The condition operand is a boolean value which WebAssembly represents as i32.
160b57cec5SDimitry Andricdefm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
170b57cec5SDimitry Andric               (outs), (ins bb_op:$dst),
180b57cec5SDimitry Andric               [(brcond I32:$cond, bb:$dst)],
190b57cec5SDimitry Andric                "br_if   \t$dst, $cond", "br_if   \t$dst", 0x0d>;
200b57cec5SDimitry Andriclet isCodeGenOnly = 1 in
210b57cec5SDimitry Andricdefm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
220b57cec5SDimitry Andric                   (outs), (ins bb_op:$dst), []>;
230b57cec5SDimitry Andriclet isBarrier = 1 in
240b57cec5SDimitry Andricdefm BR   : NRI<(outs), (ins bb_op:$dst),
250b57cec5SDimitry Andric                [(br bb:$dst)],
260b57cec5SDimitry Andric                "br      \t$dst", 0x0c>;
270b57cec5SDimitry Andric} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
280b57cec5SDimitry Andric
290b57cec5SDimitry Andricdef : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
300b57cec5SDimitry Andric          (BR_IF bb_op:$dst, I32:$cond)>;
310b57cec5SDimitry Andricdef : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
320b57cec5SDimitry Andric          (BR_UNLESS bb_op:$dst, I32:$cond)>;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric// A list of branch targets enclosed in {} and separated by comma.
350b57cec5SDimitry Andric// Used by br_table only.
360b57cec5SDimitry Andricdef BrListAsmOperand : AsmOperandClass { let Name = "BrList"; }
370b57cec5SDimitry Andriclet OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in
380b57cec5SDimitry Andricdef brlist : Operand<i32> {
390b57cec5SDimitry Andric  let ParserMatchClass = BrListAsmOperand;
400b57cec5SDimitry Andric  let PrintMethod = "printBrList";
410b57cec5SDimitry Andric}
420b57cec5SDimitry Andric
435ffd83dbSDimitry Andric// Duplicating a BR_TABLE is almost never a good idea. In particular, it can
445ffd83dbSDimitry Andric// lead to some nasty irreducibility due to tail merging when the br_table is in
455ffd83dbSDimitry Andric// a loop.
465ffd83dbSDimitry Andriclet isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 in {
475ffd83dbSDimitry Andric
480b57cec5SDimitry Andricdefm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
490b57cec5SDimitry Andric                      (outs), (ins brlist:$brl),
500b57cec5SDimitry Andric                      [(WebAssemblybr_table I32:$index)],
510b57cec5SDimitry Andric                      "br_table \t$index", "br_table \t$brl",
520b57cec5SDimitry Andric                      0x0e>;
535ffd83dbSDimitry Andric// TODO: SelectionDAG's lowering insists on using a pointer as the index for
545ffd83dbSDimitry Andric// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
555ffd83dbSDimitry Andric// currently.
560b57cec5SDimitry Andricdefm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
570b57cec5SDimitry Andric                      (outs), (ins brlist:$brl),
580b57cec5SDimitry Andric                      [(WebAssemblybr_table I64:$index)],
590b57cec5SDimitry Andric                      "br_table \t$index", "br_table \t$brl",
600b57cec5SDimitry Andric                      0x0e>;
615ffd83dbSDimitry Andric} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric// This is technically a control-flow instruction, since all it affects is the
640b57cec5SDimitry Andric// IP.
650b57cec5SDimitry Andricdefm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric// Placemarkers to indicate the start or end of a block or loop scope.
680b57cec5SDimitry Andric// These use/clobber VALUE_STACK to prevent them from being moved into the
690b57cec5SDimitry Andric// middle of an expression tree.
700b57cec5SDimitry Andriclet Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
710b57cec5SDimitry Andricdefm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block   \t$sig", 0x02>;
720b57cec5SDimitry Andricdefm LOOP  : NRI<(outs), (ins Signature:$sig), [], "loop    \t$sig", 0x03>;
730b57cec5SDimitry Andric
740b57cec5SDimitry Andricdefm IF : I<(outs), (ins Signature:$sig, I32:$cond),
750b57cec5SDimitry Andric            (outs), (ins Signature:$sig),
760b57cec5SDimitry Andric            [], "if    \t$sig, $cond", "if    \t$sig", 0x04>;
770b57cec5SDimitry Andricdefm ELSE : NRI<(outs), (ins), [], "else", 0x05>;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same
800b57cec5SDimitry Andric// opcode in wasm.
810b57cec5SDimitry Andricdefm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
820b57cec5SDimitry Andricdefm END_LOOP  : NRI<(outs), (ins), [], "end_loop", 0x0b>;
830b57cec5SDimitry Andricdefm END_IF    : NRI<(outs), (ins), [], "end_if", 0x0b>;
840b57cec5SDimitry Andric// Generic instruction, for disassembler.
850b57cec5SDimitry Andriclet IsCanonical = 1 in
860b57cec5SDimitry Andricdefm END       : NRI<(outs), (ins), [], "end", 0x0b>;
870b57cec5SDimitry Andriclet isTerminator = 1, isBarrier = 1 in
880b57cec5SDimitry Andricdefm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
890b57cec5SDimitry Andric} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric
925ffd83dbSDimitry Andriclet hasCtrlDep = 1, isBarrier = 1 in {
935ffd83dbSDimitry Andriclet isTerminator = 1 in {
940b57cec5SDimitry Andriclet isReturn = 1 in {
950b57cec5SDimitry Andric
968bcb0991SDimitry Andricdefm RETURN : I<(outs), (ins variable_ops), (outs), (ins),
978bcb0991SDimitry Andric                [(WebAssemblyreturn)],
988bcb0991SDimitry Andric                "return", "return", 0x0f>;
998bcb0991SDimitry Andric// Equivalent to RETURN, for use at the end of a function when wasm
1008bcb0991SDimitry Andric// semantics return by falling off the end of the block.
1010b57cec5SDimitry Andriclet isCodeGenOnly = 1 in
1028bcb0991SDimitry Andricdefm FALLTHROUGH_RETURN : I<(outs), (ins variable_ops), (outs), (ins), []>;
1038bcb0991SDimitry Andric
1040b57cec5SDimitry Andric} // isReturn = 1
1050b57cec5SDimitry Andric
106*e8d8bef9SDimitry Andriclet IsCanonical = 1, isTrap = 1 in
1070b57cec5SDimitry Andricdefm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
1085ffd83dbSDimitry Andric
1095ffd83dbSDimitry Andric} // isTerminator = 1
1105ffd83dbSDimitry Andric
1115ffd83dbSDimitry Andric// debugtrap explicitly returns despite trapping because it is supposed to just
1125ffd83dbSDimitry Andric// get the attention of the debugger. Unfortunately, because UNREACHABLE is a
1135ffd83dbSDimitry Andric// terminator, lowering debugtrap to UNREACHABLE can create an invalid
1145ffd83dbSDimitry Andric// MachineBasicBlock when there is additional code after it. Lower it to this
1155ffd83dbSDimitry Andric// non-terminator version instead.
1165ffd83dbSDimitry Andric// TODO: Actually execute the debugger statement when running on the Web
1175ffd83dbSDimitry Andriclet isTrap = 1 in
1185ffd83dbSDimitry Andricdefm DEBUG_UNREACHABLE : NRI<(outs), (ins), [(debugtrap)], "unreachable", 0x00>;
1195ffd83dbSDimitry Andric
1205ffd83dbSDimitry Andric} // hasCtrlDep = 1, isBarrier = 1
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1230b57cec5SDimitry Andric// Exception handling instructions
1240b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andriclet Predicates = [HasExceptionHandling] in {
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric// Throwing an exception: throw / rethrow
1290b57cec5SDimitry Andriclet isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
1300b57cec5SDimitry Andricdefm THROW : I<(outs), (ins event_op:$tag, variable_ops),
1310b57cec5SDimitry Andric               (outs), (ins event_op:$tag),
1320b57cec5SDimitry Andric               [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))],
1330b57cec5SDimitry Andric               "throw   \t$tag", "throw   \t$tag", 0x08>;
134*e8d8bef9SDimitry Andricdefm RETHROW : NRI<(outs), (ins i32imm:$depth), [], "rethrow \t$depth", 0x09>;
1350b57cec5SDimitry Andric} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
136*e8d8bef9SDimitry Andric// For C++ support, we only rethrow the latest exception, thus always setting
137*e8d8bef9SDimitry Andric// the depth to 0.
138*e8d8bef9SDimitry Andricdef : Pat<(int_wasm_rethrow), (RETHROW 0)>;
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric// Region within which an exception is caught: try / end_try
1410b57cec5SDimitry Andriclet Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
1420b57cec5SDimitry Andricdefm TRY     : NRI<(outs), (ins Signature:$sig), [], "try     \t$sig", 0x06>;
1430b57cec5SDimitry Andricdefm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
1440b57cec5SDimitry Andric} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
1450b57cec5SDimitry Andric
146*e8d8bef9SDimitry Andric// Catching an exception: catch / catch_all
147*e8d8bef9SDimitry Andriclet hasCtrlDep = 1, hasSideEffects = 1 in {
148*e8d8bef9SDimitry Andric// Currently 'catch' can only extract an i32, which is sufficient for C++
149*e8d8bef9SDimitry Andric// support, but according to the spec 'catch' can extract any number of values
150*e8d8bef9SDimitry Andric// based on the event type.
151*e8d8bef9SDimitry Andricdefm CATCH : I<(outs I32:$dst), (ins event_op:$tag),
152*e8d8bef9SDimitry Andric               (outs), (ins event_op:$tag),
153*e8d8bef9SDimitry Andric               [(set I32:$dst,
154*e8d8bef9SDimitry Andric                 (WebAssemblycatch (WebAssemblywrapper texternalsym:$tag)))],
155*e8d8bef9SDimitry Andric               "catch   \t$dst, $tag", "catch   \t$tag", 0x07>;
156*e8d8bef9SDimitry Andricdefm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>;
157*e8d8bef9SDimitry Andric}
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric// Pseudo instructions: cleanupret / catchret
1600b57cec5SDimitry Andriclet isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
1610b57cec5SDimitry Andric    isPseudo = 1, isEHScopeReturn = 1 in {
1620b57cec5SDimitry Andric  defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>;
1630b57cec5SDimitry Andric  defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
1640b57cec5SDimitry Andric                      [(catchret bb:$dst, bb:$from)], "catchret", 0>;
1650b57cec5SDimitry Andric} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
1660b57cec5SDimitry Andric  // isPseudo = 1, isEHScopeReturn = 1
1670b57cec5SDimitry Andric} // Predicates = [HasExceptionHandling]
168