blob: 437b07bf8baf15229a4e63aa9b07242976d13c3e [file] [log] [blame]
JF Bastien600aee92015-07-31 17:53:38 +00001//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
JF Bastien600aee92015-07-31 17:53:38 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// WebAssembly control-flow code-gen constructs.
JF Bastien600aee92015-07-31 17:53:38 +000011///
12//===----------------------------------------------------------------------===//
13
Dan Gohman950a13c2015-09-16 16:51:30 +000014let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
Dan Gohmanf0b165a2015-12-05 03:03:35 +000015// The condition operand is a boolean value which WebAssembly represents as i32.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000016defm 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>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000020let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000021defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
22 (outs), (ins bb_op:$dst), []>;
Heejin Ahn43675872019-02-06 00:17:03 +000023let isBarrier = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000024defm BR : NRI<(outs), (ins bb_op:$dst),
25 [(br bb:$dst)],
26 "br \t$dst", 0x0c>;
Dan Gohman950a13c2015-09-16 16:51:30 +000027} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
28
Dan Gohmanf0b165a2015-12-05 03:03:35 +000029def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000030 (BR_IF bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000031def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000032 (BR_UNLESS bb_op:$dst, I32:$cond)>;
Sam Parker92e77712021-03-31 09:25:18 +010033def : Pat<(brcond (i32 (xor bool_node:$cond, (i32 1))), bb:$dst),
34 (BR_UNLESS bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000035
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000036// A list of branch targets enclosed in {} and separated by comma.
37// Used by br_table only.
38def BrListAsmOperand : AsmOperandClass { let Name = "BrList"; }
Heejin Ahn43675872019-02-06 00:17:03 +000039let OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000040def brlist : Operand<i32> {
41 let ParserMatchClass = BrListAsmOperand;
42 let PrintMethod = "printBrList";
43}
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000044
Thomas Livelyc5d01232020-06-11 15:11:45 -070045// Duplicating a BR_TABLE is almost never a good idea. In particular, it can
46// lead to some nasty irreducibility due to tail merging when the br_table is in
47// a loop.
48let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 in {
49
Heejin Ahn8e2bac82019-01-08 01:15:15 +000050defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
51 (outs), (ins brlist:$brl),
52 [(WebAssemblybr_table I32:$index)],
53 "br_table \t$index", "br_table \t$brl",
54 0x0e>;
Thomas Livelyc5d01232020-06-11 15:11:45 -070055// TODO: SelectionDAG's lowering insists on using a pointer as the index for
56// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
57// currently.
Heejin Ahn8e2bac82019-01-08 01:15:15 +000058defm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
59 (outs), (ins brlist:$brl),
60 [(WebAssemblybr_table I64:$index)],
61 "br_table \t$index", "br_table \t$brl",
62 0x0e>;
Thomas Livelyc5d01232020-06-11 15:11:45 -070063} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1
Dan Gohman950a13c2015-09-16 16:51:30 +000064
Sam Clegg16c16822018-05-10 22:16:44 +000065// This is technically a control-flow instruction, since all it affects is the
66// IP.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000067defm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
Sam Clegg16c16822018-05-10 22:16:44 +000068
Heejin Ahne4a8dee2018-03-02 01:03:40 +000069// Placemarkers to indicate the start or end of a block or loop scope.
Heejin Ahnac62b052017-06-30 00:43:15 +000070// These use/clobber VALUE_STACK to prevent them from being moved into the
71// middle of an expression tree.
Dan Gohmane0405332016-10-03 22:43:53 +000072let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000073defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
74defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
Dan Gohman4becc582016-10-24 20:32:04 +000075
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000076defm IF : I<(outs), (ins Signature:$sig, I32:$cond),
77 (outs), (ins Signature:$sig),
78 [], "if \t$sig, $cond", "if \t$sig", 0x04>;
79defm ELSE : NRI<(outs), (ins), [], "else", 0x05>;
80
81// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same
82// opcode in wasm.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000083defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
84defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>;
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000085defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>;
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000086// Generic instruction, for disassembler.
Heejin Ahn43675872019-02-06 00:17:03 +000087let IsCanonical = 1 in
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000088defm END : NRI<(outs), (ins), [], "end", 0x0b>;
Dan Gohmand934cb82017-02-24 23:18:00 +000089let isTerminator = 1, isBarrier = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000090defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
Dan Gohmane0405332016-10-03 22:43:53 +000091} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000092
Derek Schuff39bf39f2016-08-02 23:16:09 +000093
Thomas Livelya07c08f2020-06-04 13:25:10 -070094let hasCtrlDep = 1, isBarrier = 1 in {
95let isTerminator = 1 in {
Derek Schuffffa143c2015-11-10 00:30:57 +000096let isReturn = 1 in {
Derek Schuff39bf39f2016-08-02 23:16:09 +000097
Thomas Lively00f9e5a2019-10-09 21:42:08 +000098defm RETURN : I<(outs), (ins variable_ops), (outs), (ins),
99 [(WebAssemblyreturn)],
100 "return", "return", 0x0f>;
101// Equivalent to RETURN, for use at the end of a function when wasm
102// semantics return by falling off the end of the block.
103let isCodeGenOnly = 1 in
104defm FALLTHROUGH_RETURN : I<(outs), (ins variable_ops), (outs), (ins), []>;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000105
Derek Schuffffa143c2015-11-10 00:30:57 +0000106} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000107
Dominic Chen4252f302020-09-10 01:02:13 -0400108let IsCanonical = 1, isTrap = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000109defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
Thomas Livelya07c08f2020-06-04 13:25:10 -0700110
111} // isTerminator = 1
112
113// debugtrap explicitly returns despite trapping because it is supposed to just
114// get the attention of the debugger. Unfortunately, because UNREACHABLE is a
115// terminator, lowering debugtrap to UNREACHABLE can create an invalid
116// MachineBasicBlock when there is additional code after it. Lower it to this
117// non-terminator version instead.
118// TODO: Actually execute the debugger statement when running on the Web
119let isTrap = 1 in
120defm DEBUG_UNREACHABLE : NRI<(outs), (ins), [(debugtrap)], "unreachable", 0x00>;
121
122} // hasCtrlDep = 1, isBarrier = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000123
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000124//===----------------------------------------------------------------------===//
125// Exception handling instructions
126//===----------------------------------------------------------------------===//
127
Heejin Ahn47068a42018-07-18 21:42:22 +0000128let Predicates = [HasExceptionHandling] in {
129
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000130// Throwing an exception: throw / rethrow
131let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahn1d891d42021-06-15 01:49:43 -0700132defm THROW : I<(outs), (ins tag_op:$tag, variable_ops),
133 (outs), (ins tag_op:$tag),
Heejin Ahnd6f48782019-01-30 03:21:57 +0000134 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))],
135 "throw \t$tag", "throw \t$tag", 0x08>;
Heejin Ahn9e4eade2020-12-26 02:27:44 -0800136defm RETHROW : NRI<(outs), (ins i32imm:$depth), [], "rethrow \t$depth", 0x09>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000137} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Heejin Ahn35f5f792021-02-11 11:05:41 -0800138// The depth argument will be computed in CFGStackify. We set it to 0 here for
139// now.
Heejin Ahn9e4eade2020-12-26 02:27:44 -0800140def : Pat<(int_wasm_rethrow), (RETHROW 0)>;
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000141
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000142// Region within which an exception is caught: try / end_try
143let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000144defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
145defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000146} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
147
Heejin Ahn9e4eade2020-12-26 02:27:44 -0800148// Catching an exception: catch / catch_all
149let hasCtrlDep = 1, hasSideEffects = 1 in {
Heejin Ahn52e240a2020-12-25 20:23:33 -0800150// Currently 'catch' can only extract an i32, which is sufficient for C++
Heejin Ahn9e4eade2020-12-26 02:27:44 -0800151// support, but according to the spec 'catch' can extract any number of values
Heejin Ahn1d891d42021-06-15 01:49:43 -0700152// based on the tag type.
153defm CATCH : I<(outs I32:$dst), (ins tag_op:$tag),
154 (outs), (ins tag_op:$tag),
Heejin Ahn9e4eade2020-12-26 02:27:44 -0800155 [(set I32:$dst,
156 (WebAssemblycatch (WebAssemblywrapper texternalsym:$tag)))],
157 "catch \t$dst, $tag", "catch \t$tag", 0x07>;
Heejin Ahn7c594ba2021-02-17 05:27:19 -0800158defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x19>;
Heejin Ahn9e4eade2020-12-26 02:27:44 -0800159}
Heejin Ahnd6f48782019-01-30 03:21:57 +0000160
Heejin Ahned419452020-12-28 05:10:22 -0800161// Delegating an exception: delegate
162let isTerminator = 1, hasCtrlDep = 1, hasSideEffects = 1 in
163defm DELEGATE : NRI<(outs), (ins bb_op:$dst), [], "delegate \t $dst", 0x18>;
164
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000165// Pseudo instructions: cleanupret / catchret
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000166let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
Heejin Ahnd6f48782019-01-30 03:21:57 +0000167 isPseudo = 1, isEHScopeReturn = 1 in {
168 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>;
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000169 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
Heejin Ahnd6f48782019-01-30 03:21:57 +0000170 [(catchret bb:$dst, bb:$from)], "catchret", 0>;
171} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
172 // isPseudo = 1, isEHScopeReturn = 1
Heejin Ahn43675872019-02-06 00:17:03 +0000173} // Predicates = [HasExceptionHandling]