JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 1 | //===- 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 Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 15 | let Defs = [ARGUMENTS] in { |
| 16 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 17 | let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in { |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 18 | // The condition operand is a boolean value which WebAssembly represents as i32. |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 19 | def BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond), |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 20 | [(brcond I32:$cond, bb:$dst)], |
Dan Gohman | 5a68ec7 | 2016-10-05 21:24:08 +0000 | [diff] [blame^] | 21 | "br_if \t$dst, $cond", 0x07>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 22 | let isCodeGenOnly = 1 in |
Dan Gohman | 5a68ec7 | 2016-10-05 21:24:08 +0000 | [diff] [blame^] | 23 | def BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), []>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 24 | let isBarrier = 1 in { |
| 25 | def BR : I<(outs), (ins bb_op:$dst), |
Dan Gohman | af29bd4 | 2015-11-05 20:42:30 +0000 | [diff] [blame] | 26 | [(br bb:$dst)], |
Dan Gohman | 5a68ec7 | 2016-10-05 21:24:08 +0000 | [diff] [blame^] | 27 | "br \t$dst", 0x06>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 28 | } // isBarrier = 1 |
| 29 | } // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 |
| 30 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 31 | } // Defs = [ARGUMENTS] |
| 32 | |
| 33 | def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 34 | (BR_IF bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 35 | def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 36 | (BR_UNLESS bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 37 | |
| 38 | let Defs = [ARGUMENTS] in { |
| 39 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 40 | // TODO: SelectionDAG's lowering insists on using a pointer as the index for |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 41 | // jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 42 | // currently. |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 43 | // Set TSFlags{0} to 1 to indicate that the variable_ops are immediates. |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 44 | // Set TSFlags{1} to 1 to indicate that the immediates represent labels. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 45 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 46 | def BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops), |
| 47 | [(WebAssemblybr_table I32:$index)], |
Dan Gohman | 5a68ec7 | 2016-10-05 21:24:08 +0000 | [diff] [blame^] | 48 | "br_table \t$index", 0x08> { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 49 | let TSFlags{0} = 1; |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 50 | let TSFlags{1} = 1; |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 51 | } |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 52 | def BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops), |
| 53 | [(WebAssemblybr_table I64:$index)], |
| 54 | "br_table \t$index"> { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 55 | let TSFlags{0} = 1; |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 56 | let TSFlags{1} = 1; |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 57 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 58 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
| 59 | |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 60 | // Placemarkers to indicate the start or end of a block or loop scope. These |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 61 | // use/clobber VALUE_STACK to prevent them from being moved into the middle of |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 62 | // an expression tree. |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 63 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
Dan Gohman | 5a68ec7 | 2016-10-05 21:24:08 +0000 | [diff] [blame^] | 64 | def BLOCK : I<(outs), (ins), [], "block", 0x01>; |
| 65 | def LOOP : I<(outs), (ins), [], "loop", 0x02>; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 66 | def END_BLOCK : I<(outs), (ins), [], "end_block">; |
| 67 | def END_LOOP : I<(outs), (ins), [], "end_loop">; |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 68 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 69 | |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 70 | multiclass RETURN<WebAssemblyRegClass vt> { |
Dan Gohman | af29bd4 | 2015-11-05 20:42:30 +0000 | [diff] [blame] | 71 | def RETURN_#vt : I<(outs), (ins vt:$val), [(WebAssemblyreturn vt:$val)], |
Dan Gohman | 94ef41f | 2015-11-18 17:05:35 +0000 | [diff] [blame] | 72 | "return \t$val">; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 73 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 74 | // semantics return by falling off the end of the block. |
| 75 | let isCodeGenOnly = 1 in |
| 76 | def FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), []>; |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 77 | } |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 78 | |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 79 | multiclass SIMD_RETURN<ValueType vt> { |
| 80 | def RETURN_#vt : SIMD_I<(outs), (ins V128:$val), |
| 81 | [(WebAssemblyreturn (vt V128:$val))], |
| 82 | "return \t$val">; |
| 83 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 84 | // semantics return by falling off the end of the block. |
| 85 | let isCodeGenOnly = 1 in |
| 86 | def FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), []>; |
| 87 | } |
| 88 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 89 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 90 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 91 | let isReturn = 1 in { |
Dan Gohman | d0bf981 | 2015-09-26 01:09:44 +0000 | [diff] [blame] | 92 | defm : RETURN<I32>; |
| 93 | defm : RETURN<I64>; |
| 94 | defm : RETURN<F32>; |
| 95 | defm : RETURN<F64>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 96 | defm : SIMD_RETURN<v16i8>; |
| 97 | defm : SIMD_RETURN<v8i16>; |
| 98 | defm : SIMD_RETURN<v4i32>; |
| 99 | defm : SIMD_RETURN<v4f32>; |
| 100 | |
Dan Gohman | af29bd4 | 2015-11-05 20:42:30 +0000 | [diff] [blame] | 101 | def RETURN_VOID : I<(outs), (ins), [(WebAssemblyreturn)], "return">; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 102 | |
| 103 | // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt. |
| 104 | let isCodeGenOnly = 1 in |
| 105 | def FALLTHROUGH_RETURN_VOID : I<(outs), (ins), []>; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 106 | } // isReturn = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 107 | |
| 108 | def UNREACHABLE : I<(outs), (ins), [(trap)], "unreachable">; |
| 109 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 110 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 111 | |
| 112 | } // Defs = [ARGUMENTS] |