blob: e357af63bae5bc1049854da2d9fa53ed5716c001 [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
11/// \brief WebAssembly control-flow code-gen constructs.
12///
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 Gohman06b49582016-02-08 21:50:13 +000021 "br_if \t$dst, $cond">;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000022let isCodeGenOnly = 1 in
Dan Gohman06b49582016-02-08 21:50:13 +000023def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), [],
24 "br_unless\t$dst, $cond">;
Dan Gohman950a13c2015-09-16 16:51:30 +000025let isBarrier = 1 in {
26def BR : I<(outs), (ins bb_op:$dst),
Dan Gohmanaf29bd42015-11-05 20:42:30 +000027 [(br bb:$dst)],
Dan Gohman94ef41f2015-11-18 17:05:35 +000028 "br \t$dst">;
Dan Gohman950a13c2015-09-16 16:51:30 +000029} // isBarrier = 1
30} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1
31
Dan Gohmanf0b165a2015-12-05 03:03:35 +000032} // Defs = [ARGUMENTS]
33
34def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000035 (BR_IF bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000036def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst),
Dan Gohman06b49582016-02-08 21:50:13 +000037 (BR_UNLESS bb_op:$dst, I32:$cond)>;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000038
39let Defs = [ARGUMENTS] in {
40
Dan Gohman950a13c2015-09-16 16:51:30 +000041// TODO: SelectionDAG's lowering insists on using a pointer as the index for
Dan Gohman14026062016-03-08 03:18:12 +000042// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode
Dan Gohman950a13c2015-09-16 16:51:30 +000043// currently.
Dan Gohman85159ca2016-01-12 01:45:12 +000044// Set TSFlags{0} to 1 to indicate that the variable_ops are immediates.
Dan Gohman3469ee12016-01-12 20:30:51 +000045// Set TSFlags{1} to 1 to indicate that the immediates represent labels.
Dan Gohman950a13c2015-09-16 16:51:30 +000046let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman14026062016-03-08 03:18:12 +000047def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops),
48 [(WebAssemblybr_table I32:$index)],
49 "br_table \t$index"> {
Dan Gohman85159ca2016-01-12 01:45:12 +000050 let TSFlags{0} = 1;
Dan Gohman3469ee12016-01-12 20:30:51 +000051 let TSFlags{1} = 1;
Dan Gohman85159ca2016-01-12 01:45:12 +000052}
Dan Gohman14026062016-03-08 03:18:12 +000053def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops),
54 [(WebAssemblybr_table I64:$index)],
55 "br_table \t$index"> {
Dan Gohman85159ca2016-01-12 01:45:12 +000056 let TSFlags{0} = 1;
Dan Gohman3469ee12016-01-12 20:30:51 +000057 let TSFlags{1} = 1;
Dan Gohman85159ca2016-01-12 01:45:12 +000058}
Dan Gohman950a13c2015-09-16 16:51:30 +000059} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
60
Dan Gohman1d68e80f2016-01-12 19:14:46 +000061// Placemarkers to indicate the start or end of a block or loop scope. These
Dan Gohman8887d1f2015-12-25 00:31:02 +000062// use/clobber EXPR_STACK to prevent them from being moved into the middle of
63// an expression tree.
64let Uses = [EXPR_STACK], Defs = [EXPR_STACK] in {
Dan Gohman1d68e80f2016-01-12 19:14:46 +000065def BLOCK : I<(outs), (ins), [], "block">;
66def LOOP : I<(outs), (ins), [], "loop">;
67def END_BLOCK : I<(outs), (ins), [], "end_block">;
68def END_LOOP : I<(outs), (ins), [], "end_loop">;
Dan Gohman8887d1f2015-12-25 00:31:02 +000069} // Uses = [EXPR_STACK], Defs = [EXPR_STACK]
Dan Gohman950a13c2015-09-16 16:51:30 +000070
JF Bastien8f9aea02015-08-01 04:48:44 +000071multiclass RETURN<WebAssemblyRegClass vt> {
Dan Gohmanaf29bd42015-11-05 20:42:30 +000072 def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)],
Dan Gohman94ef41f2015-11-18 17:05:35 +000073 "return \t$val">;
Dan Gohmanb7c24002016-05-21 00:21:56 +000074 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
75 // semantics return by falling off the end of the block.
76 let isCodeGenOnly = 1 in
77 def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>;
JF Bastien8f9aea02015-08-01 04:48:44 +000078}
Derek Schuffffa143c2015-11-10 00:30:57 +000079
Derek Schuff39bf39f2016-08-02 23:16:09 +000080multiclass SIMD_RETURN<ValueType vt> {
81 def RETURN_#vt : SIMD_I<(outs), (ins V128:$val),
82 [(WebAssemblyreturn (vt V128:$val))],
83 "return \t$val">;
84 // Equivalent to RETURN_#vt, for use at the end of a function when wasm
85 // semantics return by falling off the end of the block.
86 let isCodeGenOnly = 1 in
87 def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>;
88}
89
Derek Schuffffa143c2015-11-10 00:30:57 +000090let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
Dan Gohman9850e872016-10-03 21:33:09 +000091
Derek Schuffffa143c2015-11-10 00:30:57 +000092let isReturn = 1 in {
Dan Gohmand0bf9812015-09-26 01:09:44 +000093 defm : RETURN<I32>;
94 defm : RETURN<I64>;
95 defm : RETURN<F32>;
96 defm : RETURN<F64>;
Derek Schuff39bf39f2016-08-02 23:16:09 +000097 defm : SIMD_RETURN<v16i8>;
98 defm : SIMD_RETURN<v8i16>;
99 defm : SIMD_RETURN<v4i32>;
100 defm : SIMD_RETURN<v4f32>;
101
Dan Gohmanaf29bd42015-11-05 20:42:30 +0000102 def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return">;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000103
104 // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt.
105 let isCodeGenOnly = 1 in
106 def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>;
Derek Schuffffa143c2015-11-10 00:30:57 +0000107} // isReturn = 1
Dan Gohman9850e872016-10-03 21:33:09 +0000108
109def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable">;
110
Derek Schuffffa143c2015-11-10 00:30:57 +0000111} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1
Dan Gohmanfb3e0592015-11-25 19:36:19 +0000112
113} // Defs = [ARGUMENTS]