blob: c667f931721a8842b311f0d1a8d117202ad0ea33 [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 Gohmanfb3e0592015-11-25 19:36:19 +000015let Defs = [ARGUMENTS] in {
16
Dan Gohman950a13c2015-09-16 16:51:30 +000017let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in {
Dan Gohmanf0b165a2015-12-05 03:03:35 +000018// The condition operand is a boolean value which WebAssembly represents as i32.
Dan Gohman06b49582016-02-08 21:50:13 +000019def BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond),
Dan Gohmanf0b165a2015-12-05 03:03:35 +000020 [(brcond I32:$cond, bb:$dst)],
Dan Gohmanc9682972016-10-24 20:21:49 +000021 "br_if \t$dst, $cond", 0x0d>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000022let isCodeGenOnly = 1 in
Dan Gohman5a68ec72016-10-05 21:24:08 +000023def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), []>;
Dan Gohman950a13c2015-09-16 16:51:30 +000024let isBarrier = 1 in {
25def BR : I<(outs), (ins bb_op:$dst),
Dan Gohmanaf29bd42015-11-05 20:42:30 +000026 [(br bb:$dst)],
Dan Gohmanc9682972016-10-24 20:21:49 +000027 "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 +000031} // Defs = [ARGUMENTS]
32
33def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000034 (BR_IF bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000035def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000036 (BR_UNLESS bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000037
38let Defs = [ARGUMENTS] in {
39
Dan Gohman950a13c2015-09-16 16:51:30 +000040// TODO: SelectionDAG's lowering insists on using a pointer as the index for
Dan Gohman14026062016-03-08 03:18:12 +000041// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
Dan Gohman950a13c2015-09-16 16:51:30 +000042// currently.
Dan Gohman85159ca2016-01-12 01:45:12 +000043// Set TSFlags{0} to 1 to indicate that the variable_ops are immediates.
Dan Gohman3469ee12016-01-12 20:30:51 +000044// Set TSFlags{1} to 1 to indicate that the immediates represent labels.
Dan Gohman950a13c2015-09-16 16:51:30 +000045let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman14026062016-03-08 03:18:12 +000046def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
47 [(WebAssemblybr_table I32:$index)],
Dan Gohmanc9682972016-10-24 20:21:49 +000048 "br_table \t$index", 0x0e> {
Dan Gohman85159ca2016-01-12 01:45:12 +000049 let TSFlags{0} = 1;
Dan Gohman3469ee12016-01-12 20:30:51 +000050 let TSFlags{1} = 1;
Dan Gohman85159ca2016-01-12 01:45:12 +000051}
Dan Gohman14026062016-03-08 03:18:12 +000052def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
53 [(WebAssemblybr_table I64:$index)],
54 "br_table \t$index"> {
Dan Gohman85159ca2016-01-12 01:45:12 +000055 let TSFlags{0} = 1;
Dan Gohman3469ee12016-01-12 20:30:51 +000056 let TSFlags{1} = 1;
Dan Gohman85159ca2016-01-12 01:45:12 +000057}
Dan Gohman950a13c2015-09-16 16:51:30 +000058} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
59
Heejin Ahne4a8dee2018-03-02 01:03:40 +000060// Placemarkers to indicate the start or end of a block or loop scope.
Heejin Ahnac62b052017-06-30 00:43:15 +000061// These use/clobber VALUE_STACK to prevent them from being moved into the
62// middle of an expression tree.
Dan Gohmane0405332016-10-03 22:43:53 +000063let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Dan Gohmanc9682972016-10-24 20:21:49 +000064def BLOCK : I<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
65def LOOP : I<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
Dan Gohman4becc582016-10-24 20:32:04 +000066
Heejin Ahne4a8dee2018-03-02 01:03:40 +000067// END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode in
68// wasm.
Dan Gohman3acb1872016-10-24 23:27:49 +000069def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>;
70def END_LOOP : I<(outs), (ins), [], "end_loop", 0x0b>;
Dan Gohmand934cb82017-02-24 23:18:00 +000071let isTerminator = 1, isBarrier = 1 in
72def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>;
Dan Gohmane0405332016-10-03 22:43:53 +000073} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000074
JF Bastien8f9aea02015-08-01 04:48:44 +000075multiclass RETURN<WebAssemblyRegClass vt> {
Dan Gohmanaf29bd42015-11-05 20:42:30 +000076 def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)],
Dan Gohman3acb1872016-10-24 23:27:49 +000077 "return \t$val", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +000078 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
79 // semantics return by falling off the end of the block.
80 let isCodeGenOnly = 1 in
81 def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>;
JF Bastien8f9aea02015-08-01 04:48:44 +000082}
Derek Schuffffa143c2015-11-10 00:30:57 +000083
Derek Schuff39bf39f2016-08-02 23:16:09 +000084multiclass SIMD_RETURN<ValueType vt> {
85 def RETURN_#vt : SIMD_I<(outs), (ins V128:$val),
86 [(WebAssemblyreturn (vt V128:$val))],
Dan Gohman3acb1872016-10-24 23:27:49 +000087 "return \t$val", 0x0f>;
Derek Schuff39bf39f2016-08-02 23:16:09 +000088 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
89 // semantics return by falling off the end of the block.
90 let isCodeGenOnly = 1 in
91 def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>;
92}
93
Derek Schuffffa143c2015-11-10 00:30:57 +000094let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman9850e872016-10-03 21:33:09 +000095
Derek Schuffffa143c2015-11-10 00:30:57 +000096let isReturn = 1 in {
Dan Gohmand0bf9812015-09-26 01:09:44 +000097 defm : RETURN<I32>;
98 defm : RETURN<I64>;
99 defm : RETURN<F32>;
100 defm : RETURN<F64>;
Heejin Ahn0de58722018-03-08 04:05:37 +0000101 defm : RETURN<EXCEPT_REF>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000102 defm : SIMD_RETURN<v16i8>;
103 defm : SIMD_RETURN<v8i16>;
104 defm : SIMD_RETURN<v4i32>;
105 defm : SIMD_RETURN<v4f32>;
106
Dan Gohman3acb1872016-10-24 23:27:49 +0000107 def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000108
109 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
110 let isCodeGenOnly = 1 in
111 def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000112} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000113
Dan Gohman3acb1872016-10-24 23:27:49 +0000114def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000115} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000116
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000117//===----------------------------------------------------------------------===//
118// Exception handling instructions
119//===----------------------------------------------------------------------===//
120
121// Throwing an exception: throw / rethrow
122let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
123def THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$val),
124 [(int_wasm_throw imm:$tag, I32:$val)], "throw \t$tag, $val",
Heejin Ahnac62b052017-06-30 00:43:15 +0000125 0x08>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000126def THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$val),
127 [(int_wasm_throw imm:$tag, I64:$val)], "throw \t$tag, $val",
Heejin Ahnac62b052017-06-30 00:43:15 +0000128 0x08>;
129def RETHROW : I<(outs), (ins i32imm:$rel_depth), [], "rethrow \t$rel_depth",
130 0x09>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000131} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000132
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000133// Region within which an exception is caught: try / end_try
134let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
135def TRY : I<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
136def END_TRY : I<(outs), (ins), [], "end_try", 0x0b>;
137} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
138
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000139} // Defs = [ARGUMENTS]
Heejin Ahnac62b052017-06-30 00:43:15 +0000140
141// rethrow takes a relative depth as an argument, for which currently only 0 is
142// possible for C++. Once other languages need depths other than 0, depths will
143// be computed in CFGStackify.
144def : Pat<(int_wasm_rethrow), (RETHROW 0)>;