JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 1 | //===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*- |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// WebAssembly control-flow code-gen constructs. |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 14 | let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in { |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 15 | // 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] | 16 | defm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond), |
| 17 | (outs), (ins bb_op:$dst), |
| 18 | [(brcond I32:$cond, bb:$dst)], |
| 19 | "br_if \t$dst, $cond", "br_if \t$dst", 0x0d>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 20 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 21 | defm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), |
| 22 | (outs), (ins bb_op:$dst), []>; |
Heejin Ahn | 4367587 | 2019-02-06 00:17:03 +0000 | [diff] [blame] | 23 | let isBarrier = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 24 | defm BR : NRI<(outs), (ins bb_op:$dst), |
| 25 | [(br bb:$dst)], |
| 26 | "br \t$dst", 0x0c>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 27 | } // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 |
| 28 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 29 | def : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 30 | (BR_IF bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 31 | def : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst), |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 32 | (BR_UNLESS bb_op:$dst, I32:$cond)>; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 33 | |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 34 | // A list of branch targets enclosed in {} and separated by comma. |
| 35 | // Used by br_table only. |
| 36 | def BrListAsmOperand : AsmOperandClass { let Name = "BrList"; } |
Heejin Ahn | 4367587 | 2019-02-06 00:17:03 +0000 | [diff] [blame] | 37 | let OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 38 | def brlist : Operand<i32> { |
| 39 | let ParserMatchClass = BrListAsmOperand; |
| 40 | let PrintMethod = "printBrList"; |
| 41 | } |
| 42 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 43 | // 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] | 44 | // 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] | 45 | // currently. |
| 46 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Heejin Ahn | 8e2bac8 | 2019-01-08 01:15:15 +0000 | [diff] [blame] | 47 | defm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops), |
| 48 | (outs), (ins brlist:$brl), |
| 49 | [(WebAssemblybr_table I32:$index)], |
| 50 | "br_table \t$index", "br_table \t$brl", |
| 51 | 0x0e>; |
| 52 | defm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops), |
| 53 | (outs), (ins brlist:$brl), |
| 54 | [(WebAssemblybr_table I64:$index)], |
| 55 | "br_table \t$index", "br_table \t$brl", |
| 56 | 0x0e>; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 57 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
| 58 | |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 59 | // This is technically a control-flow instruction, since all it affects is the |
| 60 | // IP. |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 61 | defm NOP : NRI<(outs), (ins), [], "nop", 0x01>; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 62 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 63 | // 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] | 64 | // These use/clobber VALUE_STACK to prevent them from being moved into the |
| 65 | // middle of an expression tree. |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 66 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
Wouter van Oortmerssen | f227621 | 2018-12-26 22:55:26 +0000 | [diff] [blame] | 67 | defm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>; |
| 68 | defm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>; |
Dan Gohman | 4becc58 | 2016-10-24 20:32:04 +0000 | [diff] [blame] | 69 | |
Wouter van Oortmerssen | f227621 | 2018-12-26 22:55:26 +0000 | [diff] [blame] | 70 | defm IF : I<(outs), (ins Signature:$sig, I32:$cond), |
| 71 | (outs), (ins Signature:$sig), |
| 72 | [], "if \t$sig, $cond", "if \t$sig", 0x04>; |
| 73 | defm ELSE : NRI<(outs), (ins), [], "else", 0x05>; |
| 74 | |
| 75 | // END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same |
| 76 | // opcode in wasm. |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 77 | defm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>; |
| 78 | defm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>; |
Wouter van Oortmerssen | f227621 | 2018-12-26 22:55:26 +0000 | [diff] [blame] | 79 | defm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>; |
Wouter van Oortmerssen | 1a91cb0 | 2019-02-05 01:19:45 +0000 | [diff] [blame] | 80 | // Generic instruction, for disassembler. |
Heejin Ahn | 4367587 | 2019-02-06 00:17:03 +0000 | [diff] [blame] | 81 | let IsCanonical = 1 in |
Wouter van Oortmerssen | 1a91cb0 | 2019-02-05 01:19:45 +0000 | [diff] [blame] | 82 | defm END : NRI<(outs), (ins), [], "end", 0x0b>; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 83 | let isTerminator = 1, isBarrier = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 84 | defm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>; |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 85 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 86 | |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 87 | multiclass RETURN<WebAssemblyRegClass vt> { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 88 | defm RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), |
| 89 | [(WebAssemblyreturn vt:$val)], |
| 90 | "return \t$val", "return", 0x0f>; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 91 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 92 | // semantics return by falling off the end of the block. |
| 93 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 94 | defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins vt:$val), (outs), (ins), []>; |
JF Bastien | 8f9aea0 | 2015-08-01 04:48:44 +0000 | [diff] [blame] | 95 | } |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 96 | |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 97 | multiclass SIMD_RETURN<ValueType vt> { |
Heejin Ahn | c9c711a | 2018-08-14 19:03:36 +0000 | [diff] [blame] | 98 | defm RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins), |
| 99 | [(WebAssemblyreturn (vt V128:$val))], |
| 100 | "return \t$val", "return", 0x0f>, |
| 101 | Requires<[HasSIMD128]>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 102 | // Equivalent to RETURN_#vt, for use at the end of a function when wasm |
| 103 | // semantics return by falling off the end of the block. |
| 104 | let isCodeGenOnly = 1 in |
Heejin Ahn | c9c711a | 2018-08-14 19:03:36 +0000 | [diff] [blame] | 105 | defm FALLTHROUGH_RETURN_#vt : I<(outs), (ins V128:$val), (outs), (ins), |
| 106 | []>, |
Heejin Ahn | 4367587 | 2019-02-06 00:17:03 +0000 | [diff] [blame] | 107 | Requires<[HasSIMD128]>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 110 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 111 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 112 | let isReturn = 1 in { |
Sam Clegg | 537afe6 | 2018-06-04 16:59:26 +0000 | [diff] [blame] | 113 | defm "": RETURN<I32>; |
| 114 | defm "": RETURN<I64>; |
| 115 | defm "": RETURN<F32>; |
| 116 | defm "": RETURN<F64>; |
Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 117 | defm "": RETURN<EXNREF>; |
Sam Clegg | 537afe6 | 2018-06-04 16:59:26 +0000 | [diff] [blame] | 118 | defm "": SIMD_RETURN<v16i8>; |
| 119 | defm "": SIMD_RETURN<v8i16>; |
| 120 | defm "": SIMD_RETURN<v4i32>; |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 121 | defm "": SIMD_RETURN<v2i64>; |
Sam Clegg | 537afe6 | 2018-06-04 16:59:26 +0000 | [diff] [blame] | 122 | defm "": SIMD_RETURN<v4f32>; |
Derek Schuff | 51ed131 | 2018-08-07 21:24:01 +0000 | [diff] [blame] | 123 | defm "": SIMD_RETURN<v2f64>; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 124 | |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 125 | defm RETURN_VOID : NRI<(outs), (ins), [(WebAssemblyreturn)], "return", 0x0f>; |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 126 | |
| 127 | // This is to RETURN_VOID what FALLTHROUGH_RETURN_#vt is to RETURN_#vt. |
| 128 | let isCodeGenOnly = 1 in |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 129 | defm FALLTHROUGH_RETURN_VOID : NRI<(outs), (ins), []>; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 130 | } // isReturn = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 131 | |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 132 | defm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>; |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 133 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | 9850e87 | 2016-10-03 21:33:09 +0000 | [diff] [blame] | 134 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 135 | //===----------------------------------------------------------------------===// |
| 136 | // Exception handling instructions |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
Heejin Ahn | 47068a4 | 2018-07-18 21:42:22 +0000 | [diff] [blame] | 139 | let Predicates = [HasExceptionHandling] in { |
| 140 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 141 | // Throwing an exception: throw / rethrow |
| 142 | let isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 143 | defm THROW : I<(outs), (ins event_op:$tag, variable_ops), |
| 144 | (outs), (ins event_op:$tag), |
| 145 | [(WebAssemblythrow (WebAssemblywrapper texternalsym:$tag))], |
| 146 | "throw \t$tag", "throw \t$tag", 0x08>; |
Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 147 | defm RETHROW : I<(outs), (ins EXNREF:$exn), (outs), (ins), [], |
| 148 | "rethrow \t$exn", "rethrow", 0x09>; |
Heejin Ahn | 66ce419 | 2019-03-16 05:38:57 +0000 | [diff] [blame] | 149 | // Pseudo instruction to be the lowering target of int_wasm_rethrow_in_catch |
| 150 | // intrinsic. Will be converted to the real rethrow instruction later. |
| 151 | let isPseudo = 1 in |
| 152 | defm RETHROW_IN_CATCH : NRI<(outs), (ins), [(int_wasm_rethrow_in_catch)], |
| 153 | "rethrow_in_catch", 0>; |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 154 | } // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 |
Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 155 | |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 156 | // Region within which an exception is caught: try / end_try |
| 157 | let Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 158 | defm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; |
| 159 | defm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>; |
Heejin Ahn | e4a8dee | 2018-03-02 01:03:40 +0000 | [diff] [blame] | 160 | } // Uses = [VALUE_STACK], Defs = [VALUE_STACK] |
| 161 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 162 | // Catching an exception: catch / extract_exception |
| 163 | let hasCtrlDep = 1, hasSideEffects = 1 in |
Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 164 | defm CATCH : I<(outs EXNREF:$dst), (ins), (outs), (ins), [], |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 165 | "catch \t$dst", "catch", 0x07>; |
| 166 | |
| 167 | // Querying / extracing exception: br_on_exn |
Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 168 | // br_on_exn queries an exnref to see if it matches the corresponding exception |
| 169 | // tag index. If true it branches to the given label and pushes the |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 170 | // corresponding argument values of the exception onto the stack. |
| 171 | let isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in |
Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 172 | defm BR_ON_EXN : I<(outs), (ins bb_op:$dst, event_op:$tag, EXNREF:$exn), |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 173 | (outs), (ins bb_op:$dst, event_op:$tag), [], |
| 174 | "br_on_exn \t$dst, $tag, $exn", "br_on_exn \t$dst, $tag", |
| 175 | 0x0a>; |
| 176 | // This is a pseudo instruction that simulates popping a value from stack, which |
| 177 | // has been pushed by br_on_exn |
| 178 | let isCodeGenOnly = 1, hasSideEffects = 1 in |
| 179 | defm EXTRACT_EXCEPTION_I32 : NRI<(outs I32:$dst), (ins), |
| 180 | [(set I32:$dst, (int_wasm_extract_exception))], |
| 181 | "extract_exception\t$dst">; |
Heejin Ahn | ac62b05 | 2017-06-30 00:43:15 +0000 | [diff] [blame] | 182 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 183 | // Pseudo instructions: cleanupret / catchret |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 184 | let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 185 | isPseudo = 1, isEHScopeReturn = 1 in { |
| 186 | defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "cleanupret", 0>; |
Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 187 | defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 188 | [(catchret bb:$dst, bb:$from)], "catchret", 0>; |
| 189 | } // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, |
| 190 | // isPseudo = 1, isEHScopeReturn = 1 |
Heejin Ahn | 4367587 | 2019-02-06 00:17:03 +0000 | [diff] [blame] | 191 | } // Predicates = [HasExceptionHandling] |