blob: 7eb6cbf4d2496575bff9bac9f89fb70a6e09886d [file] [log] [blame]
JF Bastien600aee92015-07-31 17:53:38 +00001//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*-
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// WebAssembly control-flow code-gen constructs.
JF Bastien600aee92015-07-31 17:53:38 +000012///
13//===----------------------------------------------------------------------===//
14
Dan Gohman950a13c2015-09-16 16:51:30 +000015let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
Dan Gohmanf0b165a2015-12-05 03:03:35 +000016// The condition operand is a boolean value which WebAssembly represents as i32.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000017defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
18 (outs), (ins bb_op:$dst),
19 [(brcond I32:$cond, bb:$dst)],
20 "br_if \t$dst, $cond", "br_if \t$dst", 0x0d>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000021let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000022defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond),
23 (outs), (ins bb_op:$dst), []>;
Dan Gohman950a13c2015-09-16 16:51:30 +000024let isBarrier = 1 in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000025defm BR : NRI<(outs), (ins bb_op:$dst),
26 [(br bb:$dst)],
27 "br \t$dst", 0x0c>;
Dan Gohman950a13c2015-09-16 16:51:30 +000028} // isBarrier = 1
29} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
30
Dan Gohmanf0b165a2015-12-05 03:03:35 +000031def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000032 (BR_IF bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000033def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000034 (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"; }
Wouter van Oortmerssen820c6262019-01-03 23:01:30 +000039let OperandNamespace = "WebAssembly" in {
40let OperandType = "OPERAND_BRLIST" in {
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000041def brlist : Operand<i32> {
42 let ParserMatchClass = BrListAsmOperand;
43 let PrintMethod = "printBrList";
44}
Heejin Ahn8e2bac82019-01-08 01:15:15 +000045} // OPERAND_BRLIST
46} // OperandNamespace = "WebAssembly"
Wouter van Oortmerssend3c544a2018-12-17 22:04:44 +000047
Dan Gohman950a13c2015-09-16 16:51:30 +000048// TODO: SelectionDAG's lowering insists on using a pointer as the index for
Dan Gohman14026062016-03-08 03:18:12 +000049// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
Dan Gohman950a13c2015-09-16 16:51:30 +000050// currently.
51let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahn8e2bac82019-01-08 01:15:15 +000052defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
53 (outs), (ins brlist:$brl),
54 [(WebAssemblybr_table I32:$index)],
55 "br_table \t$index", "br_table \t$brl",
56 0x0e>;
57defm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
58 (outs), (ins brlist:$brl),
59 [(WebAssemblybr_table I64:$index)],
60 "br_table \t$index", "br_table \t$brl",
61 0x0e>;
Dan Gohman950a13c2015-09-16 16:51:30 +000062} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
63
Sam Clegg16c16822018-05-10 22:16:44 +000064// This is technically a control-flow instruction, since all it affects is the
65// IP.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000066defm NOP : NRI<(outs), (ins), [], "nop", 0x01>;
Sam Clegg16c16822018-05-10 22:16:44 +000067
Heejin Ahne4a8dee2018-03-02 01:03:40 +000068// Placemarkers to indicate the start or end of a block or loop scope.
Heejin Ahnac62b052017-06-30 00:43:15 +000069// These use/clobber VALUE_STACK to prevent them from being moved into the
70// middle of an expression tree.
Dan Gohmane0405332016-10-03 22:43:53 +000071let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000072defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
73defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
Dan Gohman4becc582016-10-24 20:32:04 +000074
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000075defm IF : I<(outs), (ins Signature:$sig, I32:$cond),
76 (outs), (ins Signature:$sig),
77 [], "if \t$sig, $cond", "if \t$sig", 0x04>;
78defm ELSE : NRI<(outs), (ins), [], "else", 0x05>;
79
80// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same
81// opcode in wasm.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000082defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>;
83defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>;
Wouter van Oortmerssenf2276212018-12-26 22:55:26 +000084defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>;
Dan Gohmand934cb82017-02-24 23:18:00 +000085let isTerminator = 1, isBarrier = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000086defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>;
Dan Gohmane0405332016-10-03 22:43:53 +000087} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000088
JF Bastien8f9aea02015-08-01 04:48:44 +000089multiclass RETURN<WebAssemblyRegClass vt> {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000090 defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins),
91 [(WebAssemblyreturn vt:$val)],
92 "return \t$val", "return", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +000093 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
94 // semantics return by falling off the end of the block.
95 let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +000096 defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>;
JF Bastien8f9aea02015-08-01 04:48:44 +000097}
Derek Schuffffa143c2015-11-10 00:30:57 +000098
Derek Schuff39bf39f2016-08-02 23:16:09 +000099multiclass SIMD_RETURN<ValueType vt> {
Heejin Ahnc9c711a2018-08-14 19:03:36 +0000100 defm RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
101 [(WebAssemblyreturn (vt V128:$val))],
102 "return \t$val", "return", 0x0f>,
103 Requires<[HasSIMD128]>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000104 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
105 // semantics return by falling off the end of the block.
106 let isCodeGenOnly = 1 in
Heejin Ahnc9c711a2018-08-14 19:03:36 +0000107 defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins),
108 []>,
109 Requires<[HasSIMD128]>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000110}
111
Derek Schuffffa143c2015-11-10 00:30:57 +0000112let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman9850e872016-10-03 21:33:09 +0000113
Derek Schuffffa143c2015-11-10 00:30:57 +0000114let isReturn = 1 in {
Sam Clegg537afe62018-06-04 16:59:26 +0000115 defm "": RETURN<I32>;
116 defm "": RETURN<I64>;
117 defm "": RETURN<F32>;
118 defm "": RETURN<F64>;
119 defm "": RETURN<EXCEPT_REF>;
120 defm "": SIMD_RETURN<v16i8>;
121 defm "": SIMD_RETURN<v8i16>;
122 defm "": SIMD_RETURN<v4i32>;
Derek Schuff51ed1312018-08-07 21:24:01 +0000123 defm "": SIMD_RETURN<v2i64>;
Sam Clegg537afe62018-06-04 16:59:26 +0000124 defm "": SIMD_RETURN<v4f32>;
Derek Schuff51ed1312018-08-07 21:24:01 +0000125 defm "": SIMD_RETURN<v2f64>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000126
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000127 defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000128
129 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
130 let isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000131 defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000132} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000133
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000134defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000135} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000136
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000137//===----------------------------------------------------------------------===//
138// Exception handling instructions
139//===----------------------------------------------------------------------===//
140
Heejin Ahn47068a42018-07-18 21:42:22 +0000141let Predicates = [HasExceptionHandling] in {
142
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000143// Throwing an exception: throw / rethrow
144let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Heejin Ahnda419bd2018-11-14 02:46:21 +0000145defm THROW_I32 : I<(outs), (ins event_op:$tag, I32:$val),
146 (outs), (ins event_op:$tag),
147 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag),
148 I32:$val)],
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000149 "throw \t$tag, $val", "throw \t$tag",
150 0x08>;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000151defm THROW_I64 : I<(outs), (ins event_op:$tag, I64:$val),
152 (outs), (ins event_op:$tag),
153 [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag),
154 I64:$val)],
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000155 "throw \t$tag, $val", "throw \t$tag",
156 0x08>;
Heejin Ahn6279d712018-06-18 23:54:29 +0000157defm RETHROW : NRI<(outs), (ins bb_op:$dst), [], "rethrow \t$dst", 0x09>;
158let isCodeGenOnly = 1 in
159// This is used when the destination for rethrow is the caller function. This
160// will be converted to a rethrow in CFGStackify.
161defm RETHROW_TO_CALLER : NRI<(outs), (ins), [], "rethrow">;
Derek Schuffffa143c2015-11-10 00:30:57 +0000162} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000163
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000164// Region within which an exception is caught: try / end_try
165let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000166defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
167defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000168} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
169
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000170// Catching an exception: catch / catch_all
Heejin Ahnff363532018-08-22 18:22:45 +0000171let hasCtrlDep = 1, hasSideEffects = 1 in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000172defm CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag),
173 (outs), (ins i32imm:$tag),
174 [(set I32:$dst, (int_wasm_catch imm:$tag))],
175 "i32.catch \t$dst, $tag", "i32.catch \t$tag", 0x07>;
176defm CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag),
177 (outs), (ins i32imm:$tag),
178 [(set I64:$dst, (int_wasm_catch imm:$tag))],
179 "i64.catch \t$dst, $tag", "i64.catch \t$tag", 0x07>;
180defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>;
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000181}
Heejin Ahnac62b052017-06-30 00:43:15 +0000182
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000183// Pseudo instructions: cleanupret / catchret
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000184let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
Heejin Ahned5e06b2018-08-21 19:44:11 +0000185 isCodeGenOnly = 1, isEHScopeReturn = 1 in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000186 defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "", 0>;
187 defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from),
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000188 [(catchret bb:$dst, bb:$from)], "", 0>;
189}
Heejin Ahn47068a42018-07-18 21:42:22 +0000190}