xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
27330f729Sjoerg//
37330f729Sjoerg// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg// See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg//
77330f729Sjoerg//===----------------------------------------------------------------------===//
87330f729Sjoerg///
97330f729Sjoerg/// \file
107330f729Sjoerg/// WebAssembly control-flow code-gen constructs.
117330f729Sjoerg///
127330f729Sjoerg//===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerglet isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
157330f729Sjoerg// The condition operand is a boolean value which WebAssembly represents as i32.
167330f729Sjoergdefm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
177330f729Sjoerg               (outs), (ins bb_op:$dst),
187330f729Sjoerg               [(brcond I32:$cond, bb:$dst)],
197330f729Sjoerg                "br_if   \t$dst, $cond", "br_if   \t$dst", 0x0d>;
207330f729Sjoerglet isCodeGenOnly = 1 in
217330f729Sjoergdefm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
227330f729Sjoerg                   (outs), (ins bb_op:$dst), []>;
237330f729Sjoerglet isBarrier = 1 in
247330f729Sjoergdefm BR   : NRI<(outs), (ins bb_op:$dst),
257330f729Sjoerg                [(br bb:$dst)],
267330f729Sjoerg                "br      \t$dst", 0x0c>;
277330f729Sjoerg} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
287330f729Sjoerg
297330f729Sjoergdef : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
307330f729Sjoerg          (BR_IF bb_op:$dst, I32:$cond)>;
317330f729Sjoergdef : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
327330f729Sjoerg          (BR_UNLESS bb_op:$dst, I32:$cond)>;
33*82d56013Sjoergdef : Pat<(brcond (i32 (xor bool_node:$cond, (i32 1))), bb:$dst),
34*82d56013Sjoerg          (BR_UNLESS bb_op:$dst, I32:$cond)>;
357330f729Sjoerg
367330f729Sjoerg// A list of branch targets enclosed in {} and separated by comma.
377330f729Sjoerg// Used by br_table only.
387330f729Sjoergdef BrListAsmOperand : AsmOperandClass { let Name = "BrList"; }
397330f729Sjoerglet OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in
407330f729Sjoergdef brlist : Operand<i32> {
417330f729Sjoerg  let ParserMatchClass = BrListAsmOperand;
427330f729Sjoerg  let PrintMethod = "printBrList";
437330f729Sjoerg}
447330f729Sjoerg
45*82d56013Sjoerg// Duplicating a BR_TABLE is almost never a good idea. In particular, it can
46*82d56013Sjoerg// lead to some nasty irreducibility due to tail merging when the br_table is in
47*82d56013Sjoerg// a loop.
48*82d56013Sjoerglet isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 in {
49*82d56013Sjoerg
507330f729Sjoergdefm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
517330f729Sjoerg                      (outs), (ins brlist:$brl),
527330f729Sjoerg                      [(WebAssemblybr_table I32:$index)],
537330f729Sjoerg                      "br_table \t$index", "br_table \t$brl",
547330f729Sjoerg                      0x0e>;
55*82d56013Sjoerg// TODO: SelectionDAG's lowering insists on using a pointer as the index for
56*82d56013Sjoerg// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
57*82d56013Sjoerg// currently.
587330f729Sjoergdefm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
597330f729Sjoerg                      (outs), (ins brlist:$brl),
607330f729Sjoerg                      [(WebAssemblybr_table I64:$index)],
617330f729Sjoerg                      "br_table \t$index", "br_table \t$brl",
627330f729Sjoerg                      0x0e>;
63*82d56013Sjoerg} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1
647330f729Sjoerg
657330f729Sjoerg// This is technically a control-flow instruction, since all it affects is the
667330f729Sjoerg// IP.
677330f729Sjoergdefm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
687330f729Sjoerg
697330f729Sjoerg// Placemarkers to indicate the start or end of a block or loop scope.
707330f729Sjoerg// These use/clobber VALUE_STACK to prevent them from being moved into the
717330f729Sjoerg// middle of an expression tree.
727330f729Sjoerglet Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
737330f729Sjoergdefm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block   \t$sig", 0x02>;
747330f729Sjoergdefm LOOP  : NRI<(outs), (ins Signature:$sig), [], "loop    \t$sig", 0x03>;
757330f729Sjoerg
767330f729Sjoergdefm IF : I<(outs), (ins Signature:$sig, I32:$cond),
777330f729Sjoerg            (outs), (ins Signature:$sig),
787330f729Sjoerg            [], "if    \t$sig, $cond", "if    \t$sig", 0x04>;
797330f729Sjoergdefm ELSE : NRI<(outs), (ins), [], "else", 0x05>;
807330f729Sjoerg
817330f729Sjoerg// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same
827330f729Sjoerg// opcode in wasm.
837330f729Sjoergdefm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
847330f729Sjoergdefm END_LOOP  : NRI<(outs), (ins), [], "end_loop", 0x0b>;
857330f729Sjoergdefm END_IF    : NRI<(outs), (ins), [], "end_if", 0x0b>;
867330f729Sjoerg// Generic instruction, for disassembler.
877330f729Sjoerglet IsCanonical = 1 in
887330f729Sjoergdefm END       : NRI<(outs), (ins), [], "end", 0x0b>;
897330f729Sjoerglet isTerminator = 1, isBarrier = 1 in
907330f729Sjoergdefm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
917330f729Sjoerg} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
927330f729Sjoerg
937330f729Sjoerg
94*82d56013Sjoerglet hasCtrlDep = 1, isBarrier = 1 in {
95*82d56013Sjoerglet isTerminator = 1 in {
967330f729Sjoerglet isReturn = 1 in {
977330f729Sjoerg
987330f729Sjoergdefm RETURN : I<(outs), (ins variable_ops), (outs), (ins),
997330f729Sjoerg                [(WebAssemblyreturn)],
1007330f729Sjoerg                "return", "return", 0x0f>;
1017330f729Sjoerg// Equivalent to RETURN, for use at the end of a function when wasm
1027330f729Sjoerg// semantics return by falling off the end of the block.
1037330f729Sjoerglet isCodeGenOnly = 1 in
1047330f729Sjoergdefm FALLTHROUGH_RETURN : I<(outs), (ins variable_ops), (outs), (ins), []>;
1057330f729Sjoerg
1067330f729Sjoerg} // isReturn = 1
1077330f729Sjoerg
108*82d56013Sjoerglet IsCanonical = 1, isTrap = 1 in
1097330f729Sjoergdefm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
110*82d56013Sjoerg
111*82d56013Sjoerg} // isTerminator = 1
112*82d56013Sjoerg
113*82d56013Sjoerg// debugtrap explicitly returns despite trapping because it is supposed to just
114*82d56013Sjoerg// get the attention of the debugger. Unfortunately, because UNREACHABLE is a
115*82d56013Sjoerg// terminator, lowering debugtrap to UNREACHABLE can create an invalid
116*82d56013Sjoerg// MachineBasicBlock when there is additional code after it. Lower it to this
117*82d56013Sjoerg// non-terminator version instead.
118*82d56013Sjoerg// TODO: Actually execute the debugger statement when running on the Web
119*82d56013Sjoerglet isTrap = 1 in
120*82d56013Sjoergdefm DEBUG_UNREACHABLE : NRI<(outs), (ins), [(debugtrap)], "unreachable", 0x00>;
121*82d56013Sjoerg
122*82d56013Sjoerg} // hasCtrlDep = 1, isBarrier = 1
1237330f729Sjoerg
1247330f729Sjoerg//===----------------------------------------------------------------------===//
1257330f729Sjoerg// Exception handling instructions
1267330f729Sjoerg//===----------------------------------------------------------------------===//
1277330f729Sjoerg
1287330f729Sjoerglet Predicates = [HasExceptionHandling] in {
1297330f729Sjoerg
1307330f729Sjoerg// Throwing an exception: throw / rethrow
1317330f729Sjoerglet isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
1327330f729Sjoergdefm THROW : I<(outs), (ins event_op:$tag, variable_ops),
1337330f729Sjoerg               (outs), (ins event_op:$tag),
1347330f729Sjoerg               [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))],
1357330f729Sjoerg               "throw   \t$tag", "throw   \t$tag", 0x08>;
136*82d56013Sjoergdefm RETHROW : NRI<(outs), (ins i32imm:$depth), [], "rethrow \t$depth", 0x09>;
1377330f729Sjoerg} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
138*82d56013Sjoerg// The depth argument will be computed in CFGStackify. We set it to 0 here for
139*82d56013Sjoerg// now.
140*82d56013Sjoergdef : Pat<(int_wasm_rethrow), (RETHROW 0)>;
1417330f729Sjoerg
1427330f729Sjoerg// Region within which an exception is caught: try / end_try
1437330f729Sjoerglet Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
1447330f729Sjoergdefm TRY     : NRI<(outs), (ins Signature:$sig), [], "try     \t$sig", 0x06>;
1457330f729Sjoergdefm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
1467330f729Sjoerg} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
1477330f729Sjoerg
148*82d56013Sjoerg// Catching an exception: catch / catch_all
149*82d56013Sjoerglet hasCtrlDep = 1, hasSideEffects = 1 in {
150*82d56013Sjoerg// Currently 'catch' can only extract an i32, which is sufficient for C++
151*82d56013Sjoerg// support, but according to the spec 'catch' can extract any number of values
152*82d56013Sjoerg// based on the event type.
153*82d56013Sjoergdefm CATCH : I<(outs I32:$dst), (ins event_op:$tag),
154*82d56013Sjoerg               (outs), (ins event_op:$tag),
155*82d56013Sjoerg               [(set I32:$dst,
156*82d56013Sjoerg                 (WebAssemblycatch (WebAssemblywrapper texternalsym:$tag)))],
157*82d56013Sjoerg               "catch   \t$dst, $tag", "catch   \t$tag", 0x07>;
158*82d56013Sjoergdefm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x19>;
159*82d56013Sjoerg}
1607330f729Sjoerg
161*82d56013Sjoerg// Delegating an exception: delegate
162*82d56013Sjoerglet isTerminator = 1, hasCtrlDep = 1, hasSideEffects = 1 in
163*82d56013Sjoergdefm DELEGATE : NRI<(outs), (ins bb_op:$dst), [], "delegate \t $dst", 0x18>;
1647330f729Sjoerg
1657330f729Sjoerg// Pseudo instructions: cleanupret / catchret
1667330f729Sjoerglet isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
1677330f729Sjoerg    isPseudo = 1, isEHScopeReturn = 1 in {
1687330f729Sjoerg  defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>;
1697330f729Sjoerg  defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
1707330f729Sjoerg                      [(catchret bb:$dst, bb:$from)], "catchret", 0>;
1717330f729Sjoerg} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
1727330f729Sjoerg  // isPseudo = 1, isEHScopeReturn = 1
1737330f729Sjoerg} // Predicates = [HasExceptionHandling]
174