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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// WebAssembly control-flow code-gen constructs. |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 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. |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 19 | defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond), |
| 20 | (outs), (ins bb_op:$dst), |
| 21 | [(brcond I32:$cond, bb:$dst)], |
| 22 | "br_if \t$dst, $cond", "br_if \t$dst", 0x0d>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 23 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 24 | defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), |
| 25 | (outs), (ins bb_op:$dst), []>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 26 | let isBarrier = 1 in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 27 | defm BR : NRI<(outs), (ins bb_op:$dst), |
| 28 | [(br bb:$dst)], |
| 29 | "br \t$dst", 0x0c>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 30 | } // isBarrier = 1 |
| 31 | } // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 |
| 32 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 33 | } // Defs = [ARGUMENTS] |
| 34 | |
| 35 | def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 36 | (BR_IF bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 37 | def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 38 | (BR_UNLESS bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 39 | |
| 40 | let Defs = [ARGUMENTS] in { |
| 41 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 42 | // 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] | 43 | // 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] | 44 | // currently. |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 45 | // 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] | 46 | // Set TSFlags{1} to 1 to indicate that the immediates represent labels. |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 47 | // FIXME: this can't inherit from I<> since there is no way to inherit from a |
| 48 | // multiclass and still have the let statements. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 49 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 50 | def BR_TABLE_I32 : NI<(outs), (ins I32:$index, variable_ops), |
| 51 | [(WebAssemblybr_table I32:$index)], 0, |
| 52 | "br_table \t$index", 0x0e> { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 53 | let TSFlags{0} = 1; |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 54 | let TSFlags{1} = 1; |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 55 | } |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 56 | def BR_TABLE_I32_S : NI<(outs), (ins I32:$index), |
| 57 | [], 1, |
| 58 | "br_table \t$index", 0x0e> { |
| 59 | let TSFlags{0} = 1; |
| 60 | let TSFlags{1} = 1; |
| 61 | } |
| 62 | def BR_TABLE_I64 : NI<(outs), (ins I64:$index, variable_ops), |
| 63 | [(WebAssemblybr_table I64:$index)], 0, |
| 64 | "br_table \t$index"> { |
| 65 | let TSFlags{0} = 1; |
| 66 | let TSFlags{1} = 1; |
| 67 | } |
| 68 | def BR_TABLE_I64_S : NI<(outs), (ins I64:$index), |
| 69 | [], 1, |
| 70 | "br_table \t$index"> { |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 71 | let TSFlags{0} = 1; |
Dan Gohman | 3469ee1 | 2016-01-12 20:30:51 +0000 | [diff] [blame] | 72 | let TSFlags{1} = 1; |
Dan Gohman | 85159ca | 2016-01-12 01:45:12 +0000 | [diff] [blame] | 73 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 74 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
| 75 | |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 76 | // This is technically a control-flow instruction, since all it affects is the |
| 77 | // IP. |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 78 | defm NOP : NRI<(outs), (ins), [], "nop", 0x01>; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 79 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 80 | // Placemarkers to indicate the start or end of a block or loop scope. |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 81 | // These use/clobber VALUE_STACK to prevent them from being moved into the |
| 82 | // middle of an expression tree. |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 83 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 84 | defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>; |
| 85 | defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>; |
Dan Gohman | 4becc58 | 2016-10-24 20:32:04 +0000 | [diff] [blame] | 86 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 87 | // END_BLOCK, END_LOOP, and END_FUNCTION are represented with the same opcode in |
| 88 | // wasm. |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 89 | defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>; |
| 90 | defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 91 | let isTerminator = 1, isBarrier = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 92 | defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>; |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 93 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 94 | |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 95 | multiclass RETURN<WebAssemblyRegClass vt> { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 96 | defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), |
| 97 | [(WebAssemblyreturn vt:$val)], |
| 98 | "return \t$val", "return", 0x0f>; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 99 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 100 | // semantics return by falling off the end of the block. |
| 101 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 102 | defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>; |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 103 | } |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 104 | |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 105 | multiclass SIMD_RETURN<ValueType vt> { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 106 | defm RETURN_#vt : SIMD_I<(outs), (ins V128:$val), (outs), (ins), |
| 107 | [(WebAssemblyreturn (vt V128:$val))], |
| 108 | "return \t$val", "return", 0x0f>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 109 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 110 | // semantics return by falling off the end of the block. |
| 111 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 112 | defm FALLTHROUGH_RETURN_#vt : SIMD_I<(outs), (ins V128:$val), (outs), (ins), |
| 113 | []>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 116 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 117 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 118 | let isReturn = 1 in { |
Sam Clegg | 537afe6 | 2018-06-04 16:59:26 +0000 | [diff] [blame] | 119 | defm "": RETURN<I32>; |
| 120 | defm "": RETURN<I64>; |
| 121 | defm "": RETURN<F32>; |
| 122 | defm "": RETURN<F64>; |
| 123 | defm "": RETURN<EXCEPT_REF>; |
| 124 | defm "": SIMD_RETURN<v16i8>; |
| 125 | defm "": SIMD_RETURN<v8i16>; |
| 126 | defm "": SIMD_RETURN<v4i32>; |
| 127 | defm "": SIMD_RETURN<v4f32>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 128 | |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 129 | defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 130 | |
| 131 | // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt. |
| 132 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 133 | defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 134 | } // isReturn = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 135 | |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 136 | defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>; |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 137 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 138 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | // Exception handling instructions |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
| 143 | // Throwing an exception: throw / rethrow |
| 144 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 145 | defm THROW_I32 : I<(outs), (ins i32imm:$tag, I32:$val), |
| 146 | (outs), (ins i32imm:$tag), |
| 147 | [(int_wasm_throw imm:$tag, I32:$val)], |
| 148 | "throw \t$tag, $val", "throw \t$tag", |
| 149 | 0x08>; |
| 150 | defm THROW_I64 : I<(outs), (ins i32imm:$tag, I64:$val), |
| 151 | (outs), (ins i32imm:$tag), |
| 152 | [(int_wasm_throw imm:$tag, I64:$val)], |
| 153 | "throw \t$tag, $val", "throw \t$tag", |
| 154 | 0x08>; |
Heejin Ahn | 6279d71 | 2018-06-18 23:54:29 +0000 | [diff] [blame] | 155 | defm RETHROW : NRI<(outs), (ins bb_op:$dst), [], "rethrow \t$dst", 0x09>; |
| 156 | let isCodeGenOnly = 1 in |
| 157 | // This is used when the destination for rethrow is the caller function. This |
| 158 | // will be converted to a rethrow in CFGStackify. |
| 159 | defm RETHROW_TO_CALLER : NRI<(outs), (ins), [], "rethrow">; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 160 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 161 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 162 | // Region within which an exception is caught: try / end_try |
| 163 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 164 | defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; |
| 165 | defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>; |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 166 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
| 167 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 168 | // Catching an exception: catch / catch_all |
| 169 | let hasCtrlDep = 1 in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 170 | defm CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag), |
| 171 | (outs), (ins i32imm:$tag), |
| 172 | [(set I32:$dst, (int_wasm_catch imm:$tag))], |
| 173 | "i32.catch \t$dst, $tag", "i32.catch \t$tag", 0x07>; |
| 174 | defm CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag), |
| 175 | (outs), (ins i32imm:$tag), |
| 176 | [(set I64:$dst, (int_wasm_catch imm:$tag))], |
| 177 | "i64.catch \t$dst, $tag", "i64.catch \t$tag", 0x07>; |
| 178 | defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>; |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 179 | } |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 180 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 181 | // Pseudo instructions: cleanupret / catchret |
| 182 | // They are not return instructions in wasm, but setting 'isReturn' to true as |
Heejin Ahn | 9786946 | 2018-06-17 00:37:56 +0000 | [diff] [blame] | 183 | // in X86 is necessary for computing EH scope membership. |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 184 | let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, |
| 185 | isCodeGenOnly = 1, isReturn = 1 in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 186 | defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "", 0>; |
| 187 | defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 188 | [(catchret bb:$dst, bb:$from)], "", 0>; |
| 189 | } |
| 190 | |
| 191 | } // Defs = [ARGUMENTS] |