blob: 98e1536104857a448d7ed6dc7cdeff3476172797 [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
Sam Clegg16c16822018-05-10 22:16:44 +000060// This is technically a control-flow instruction, since all it affects is the
61// IP.
62def NOP : I<(outs), (ins), [], "nop", 0x01>;
63
Heejin Ahne4a8dee2018-03-02 01:03:40 +000064// Placemarkers to indicate the start or end of a block or loop scope.
Heejin Ahnac62b052017-06-30 00:43:15 +000065// These use/clobber VALUE_STACK to prevent them from being moved into the
66// middle of an expression tree.
Dan Gohmane0405332016-10-03 22:43:53 +000067let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
Dan Gohmanc9682972016-10-24 20:21:49 +000068def BLOCK : I<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>;
69def LOOP : I<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>;
Dan Gohman4becc582016-10-24 20:32:04 +000070
Heejin Ahne4a8dee2018-03-02 01:03:40 +000071// END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode in
72// wasm.
Dan Gohman3acb1872016-10-24 23:27:49 +000073def END_BLOCK : I<(outs), (ins), [], "end_block", 0x0b>;
74def END_LOOP : I<(outs), (ins), [], "end_loop", 0x0b>;
Dan Gohmand934cb82017-02-24 23:18:00 +000075let isTerminator = 1, isBarrier = 1 in
76def END_FUNCTION : I<(outs), (ins), [], "end_function", 0x0b>;
Dan Gohmane0405332016-10-03 22:43:53 +000077} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000078
JF Bastien8f9aea02015-08-01 04:48:44 +000079multiclass RETURN<WebAssemblyRegClass vt> {
Dan Gohmanaf29bd42015-11-05 20:42:30 +000080 def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)],
Dan Gohman3acb1872016-10-24 23:27:49 +000081 "return \t$val", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +000082 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
83 // semantics return by falling off the end of the block.
84 let isCodeGenOnly = 1 in
85 def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>;
JF Bastien8f9aea02015-08-01 04:48:44 +000086}
Derek Schuffffa143c2015-11-10 00:30:57 +000087
Derek Schuff39bf39f2016-08-02 23:16:09 +000088multiclass SIMD_RETURN<ValueType vt> {
89 def RETURN_#vt : SIMD_I<(outs), (ins V128:$val),
90 [(WebAssemblyreturn (vt V128:$val))],
Dan Gohman3acb1872016-10-24 23:27:49 +000091 "return \t$val", 0x0f>;
Derek Schuff39bf39f2016-08-02 23:16:09 +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
95 def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>;
96}
97
Derek Schuffffa143c2015-11-10 00:30:57 +000098let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman9850e872016-10-03 21:33:09 +000099
Derek Schuffffa143c2015-11-10 00:30:57 +0000100let isReturn = 1 in {
Dan Gohmand0bf9812015-09-26 01:09:44 +0000101 defm : RETURN<I32>;
102 defm : RETURN<I64>;
103 defm : RETURN<F32>;
104 defm : RETURN<F64>;
Heejin Ahn0de58722018-03-08 04:05:37 +0000105 defm : RETURN<EXCEPT_REF>;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000106 defm : SIMD_RETURN<v16i8>;
107 defm : SIMD_RETURN<v8i16>;
108 defm : SIMD_RETURN<v4i32>;
109 defm : SIMD_RETURN<v4f32>;
110
Dan Gohman3acb1872016-10-24 23:27:49 +0000111 def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000112
113 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
114 let isCodeGenOnly = 1 in
115 def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000116} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000117
Dan Gohman3acb1872016-10-24 23:27:49 +0000118def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable", 0x00>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000119} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000120
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000121//===----------------------------------------------------------------------===//
122// Exception handling instructions
123//===----------------------------------------------------------------------===//
124
125// Throwing an exception: throw / rethrow
126let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
127def THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$val),
128 [(int_wasm_throw imm:$tag, I32:$val)], "throw \t$tag, $val",
Heejin Ahnac62b052017-06-30 00:43:15 +0000129 0x08>;
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000130def THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$val),
131 [(int_wasm_throw imm:$tag, I64:$val)], "throw \t$tag, $val",
Heejin Ahnac62b052017-06-30 00:43:15 +0000132 0x08>;
133def RETHROW : I<(outs), (ins i32imm:$rel_depth), [], "rethrow \t$rel_depth",
134 0x09>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000135} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000136
Heejin Ahne4a8dee2018-03-02 01:03:40 +0000137// Region within which an exception is caught: try / end_try
138let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in {
139def TRY : I<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
140def END_TRY : I<(outs), (ins), [], "end_try", 0x0b>;
141} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
142
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000143// Catching an exception: catch / catch_all
144let hasCtrlDep = 1 in {
145def CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag),
146 [(set I32:$dst, (int_wasm_catch imm:$tag))],
147 "i32.catch \t$dst, $tag", 0x07>;
148def CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag),
149 [(set I64:$dst, (int_wasm_catch imm:$tag))],
150 "i64.catch \t$dst, $tag", 0x07>;
151def CATCH_ALL : I<(outs), (ins), [], "catch_all", 0x05>;
152}
Heejin Ahnac62b052017-06-30 00:43:15 +0000153
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000154// Pseudo instructions: cleanupret / catchret
155// They are not return instructions in wasm, but setting 'isReturn' to true as
156// in X86 is necessary for computing funclet membership.
157let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
158 isCodeGenOnly = 1, isReturn = 1 in {
159 def CLEANUPRET : I<(outs), (ins), [(cleanupret)], "", 0>;
160 def CATCHRET : I<(outs), (ins bb_op:$dst, bb_op:$from),
161 [(catchret bb:$dst, bb:$from)], "", 0>;
162}
163
164} // Defs = [ARGUMENTS]