blob: 8160cab75202e5af5bdddd0efee248945ff29c5c [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), []>;
Dan Gohman950a13c2015-09-16 16:51:30 +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} // isBarrier = 1
28} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
29
Dan Gohmanf0b165a2015-12-05 03:03:35 +000030def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000031 (BR_IF bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000032def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000033 (BR_UNLESS bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000034
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000035// A list of branch targets enclosed in {} and separated by comma.
36// Used by br_table only.
37def BrListAsmOperand : AsmOperandClass { let Name = "BrList"; }
Wouter van Oortmerssen820c6262019-01-03 23:01:30 +000038let OperandNamespace = "WebAssembly" in {
39let 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}
Heejin Ahn8e2bac82019-01-08 01:15:15 +000044} // OPERAND_BRLIST
45} // OperandNamespace = "WebAssembly"
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000046
Dan Gohman950a13c2015-09-16 16:51:30 +000047// TODO: SelectionDAG's lowering insists on using a pointer as the index for
Dan Gohman14026062016-03-08 03:18:12 +000048// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
Dan Gohman950a13c2015-09-16 16:51:30 +000049// currently.
50let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahn8e2bac82019-01-08 01:15:15 +000051defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
52 (outs), (ins brlist:$brl),
53 [(WebAssemblybr_table I32:$index)],
54 "br_table \t$index", "br_table \t$brl",
55 0x0e>;
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>;
Dan Gohman950a13c2015-09-16 16:51:30 +000061} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
62
Sam Clegg16c16822018-05-10 22:16:44 +000063// This is technically a control-flow instruction, since all it affects is the
64// IP.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000065defm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
Sam Clegg16c16822018-05-10 22:16:44 +000066
Heejin Ahne4a8dee2018-03-02 01:03:40 +000067// Placemarkers to indicate the start or end of a block or loop scope.
Heejin Ahnac62b052017-06-30 00:43:15 +000068// These use/clobber VALUE_STACK to prevent them from being moved into the
69// middle of an expression tree.
Dan Gohmane0405332016-10-03 22:43:53 +000070let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000071defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
72defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
Dan Gohman4becc582016-10-24 20:32:04 +000073
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000074defm 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.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000081defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
82defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>;
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000083defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>;
Dan Gohmand934cb82017-02-24 23:18:00 +000084let isTerminator = 1, isBarrier = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000085defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
Dan Gohmane0405332016-10-03 22:43:53 +000086} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000087
JF Bastien8f9aea02015-08-01 04:48:44 +000088multiclass RETURN<WebAssemblyRegClass vt> {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000089 defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins),
90 [(WebAssemblyreturn vt:$val)],
91 "return \t$val", "return", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +000092 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
93 // semantics return by falling off the end of the block.
94 let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000095 defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>;
JF Bastien8f9aea02015-08-01 04:48:44 +000096}
Derek Schuffffa143c2015-11-10 00:30:57 +000097
Derek Schuff39bf39f2016-08-02 23:16:09 +000098multiclass SIMD_RETURN<ValueType vt> {
Heejin Ahnc9c711a2018-08-14 19:03:36 +000099 defm RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
100 [(WebAssemblyreturn (vt V128:$val))],
101 "return \t$val", "return", 0x0f>,
102 Requires<[HasSIMD128]>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000103 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
104 // semantics return by falling off the end of the block.
105 let isCodeGenOnly = 1 in
Heejin Ahnc9c711a2018-08-14 19:03:36 +0000106 defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
107 []>,
108 Requires<[HasSIMD128]>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000109}
110
Derek Schuffffa143c2015-11-10 00:30:57 +0000111let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman9850e872016-10-03 21:33:09 +0000112
Derek Schuffffa143c2015-11-10 00:30:57 +0000113let isReturn = 1 in {
Sam Clegg537afe62018-06-04 16:59:26 +0000114 defm "": RETURN<I32>;
115 defm "": RETURN<I64>;
116 defm "": RETURN<F32>;
117 defm "": RETURN<F64>;
118 defm "": RETURN<EXCEPT_REF>;
119 defm "": SIMD_RETURN<v16i8>;
120 defm "": SIMD_RETURN<v8i16>;
121 defm "": SIMD_RETURN<v4i32>;
Derek Schuff51ed1312018-08-07 21:24:01 +0000122 defm "": SIMD_RETURN<v2i64>;
Sam Clegg537afe62018-06-04 16:59:26 +0000123 defm "": SIMD_RETURN<v4f32>;
Derek Schuff51ed1312018-08-07 21:24:01 +0000124 defm "": SIMD_RETURN<v2f64>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000125
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000126 defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000127
128 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
129 let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000130 defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000131} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000132
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000133defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000134} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000135
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000136//===----------------------------------------------------------------------===//
137// Exception handling instructions
138//===----------------------------------------------------------------------===//
139
Heejin Ahn47068a42018-07-18 21:42:22 +0000140let Predicates = [HasExceptionHandling] in {
141
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000142// Throwing an exception: throw / rethrow
143let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000144defm THROW : I<(outs), (ins event_op:$tag, variable_ops),
145 (outs), (ins event_op:$tag),
146 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))],
147 "throw \t$tag", "throw \t$tag", 0x08>;
148defm RETHROW : NRI<(outs), (ins), [(int_wasm_rethrow)], "rethrow", 0x09>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000149} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000150
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000151// Region within which an exception is caught: try / end_try
152let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000153defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
154defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000155} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
156
Heejin Ahnd6f48782019-01-30 03:21:57 +0000157// Catching an exception: catch / extract_exception
158let hasCtrlDep = 1, hasSideEffects = 1 in
159defm CATCH : I<(outs EXCEPT_REF:$dst), (ins), (outs), (ins), [],
160 "catch \t$dst", "catch", 0x07>;
161
162// Querying / extracing exception: br_on_exn
163// br_on_exn queries an except_ref to see if it matches the corresponding
164// exception tag index. If true it branches to the given label and pushes the
165// corresponding argument values of the exception onto the stack.
166let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in
167defm BR_ON_EXN : I<(outs), (ins bb_op:$dst, event_op:$tag, EXCEPT_REF:$exn),
168 (outs), (ins bb_op:$dst, event_op:$tag), [],
169 "br_on_exn \t$dst, $tag, $exn", "br_on_exn \t$dst, $tag",
170 0x0a>;
171// This is a pseudo instruction that simulates popping a value from stack, which
172// has been pushed by br_on_exn
173let isCodeGenOnly = 1, hasSideEffects = 1 in
174defm EXTRACT_EXCEPTION_I32 : NRI<(outs I32:$dst), (ins),
175 [(set I32:$dst, (int_wasm_extract_exception))],
176 "extract_exception\t$dst">;
Heejin Ahnac62b052017-06-30 00:43:15 +0000177
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000178// Pseudo instructions: cleanupret / catchret
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000179let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
Heejin Ahnd6f48782019-01-30 03:21:57 +0000180 isPseudo = 1, isEHScopeReturn = 1 in {
181 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>;
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000182 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
Heejin Ahnd6f48782019-01-30 03:21:57 +0000183 [(catchret bb:$dst, bb:$from)], "catchret", 0>;
184} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
185 // isPseudo = 1, isEHScopeReturn = 1
Heejin Ahn47068a42018-07-18 21:42:22 +0000186}