blob: d172537bb340e600cc8ecd8f454115c107601921 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001// WebAssemblyInstrInfo.td-Describe the WebAssembly Instructions-*- 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//===----------------------------------------------------------------------===//
JF Bastien5ca0bac2015-07-10 18:23:10 +00009///
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// WebAssembly Instruction definitions.
JF Bastien5ca0bac2015-07-10 18:23:10 +000012///
Dan Gohman10e730a2015-06-29 23:51:55 +000013//===----------------------------------------------------------------------===//
14
15//===----------------------------------------------------------------------===//
16// WebAssembly Instruction Predicate Definitions.
17//===----------------------------------------------------------------------===//
18
JF Bastien03855df2015-07-01 23:41:25 +000019def HasAddr32 : Predicate<"!Subtarget->hasAddr64()">;
20def HasAddr64 : Predicate<"Subtarget->hasAddr64()">;
21def HasSIMD128 : Predicate<"Subtarget->hasSIMD128()">,
22 AssemblerPredicate<"FeatureSIMD128", "simd128">;
Thomas Lively8dbf29af2018-12-20 02:10:22 +000023def HasUnimplementedSIMD : Predicate<"EnableUnimplementedWasmSIMDInstrs">;
Derek Schuff18ba1922017-08-30 18:07:45 +000024def HasAtomics : Predicate<"Subtarget->hasAtomics()">,
25 AssemblerPredicate<"FeatureAtomics", "atomics">;
Dan Gohmancdd48b82017-11-28 01:13:40 +000026def HasNontrappingFPToInt :
27 Predicate<"Subtarget->hasNontrappingFPToInt()">,
28 AssemblerPredicate<"FeatureNontrappingFPToInt",
29 "nontrapping-fptoint">;
30def NotHasNontrappingFPToInt :
31 Predicate<"!Subtarget->hasNontrappingFPToInt()">,
32 AssemblerPredicate<"!FeatureNontrappingFPToInt",
33 "nontrapping-fptoint">;
Dan Gohman5d2b9352018-01-19 17:16:24 +000034def HasSignExt :
35 Predicate<"Subtarget->hasSignExt()">,
36 AssemblerPredicate<"FeatureSignExt",
37 "sign-ext">;
38def NotHasSignExt :
39 Predicate<"!Subtarget->hasSignExt()">,
40 AssemblerPredicate<"!FeatureSignExt",
41 "sign-ext">;
JF Bastien03855df2015-07-01 23:41:25 +000042
Heejin Ahn9386bde2018-02-24 00:40:50 +000043def HasExceptionHandling :
44 Predicate<"Subtarget->hasExceptionHandling()">,
45 AssemblerPredicate<"FeatureExceptionHandling",
46 "exception-handling">;
47
48def NotHasExceptionHandling :
49 Predicate<"!Subtarget->hasExceptionHandling()">,
50 AssemblerPredicate<"!FeatureExceptionHandling",
51 "exception-handling">;
52
Dan Gohman10e730a2015-06-29 23:51:55 +000053//===----------------------------------------------------------------------===//
54// WebAssembly-specific DAG Node Types.
55//===----------------------------------------------------------------------===//
56
Serge Pavlovd526b132017-05-09 13:35:13 +000057def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>,
58 SDTCisVT<1, iPTR>]>;
JF Bastienaf111db2015-08-24 22:16:48 +000059def SDT_WebAssemblyCallSeqEnd :
60 SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
Dan Gohmanf71abef2015-09-09 16:13:47 +000061def SDT_WebAssemblyCall0 : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
62def SDT_WebAssemblyCall1 : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
Dan Gohman14026062016-03-08 03:18:12 +000063def SDT_WebAssemblyBrTable : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
JF Bastien600aee92015-07-31 17:53:38 +000064def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
JF Bastien4a2d5602015-07-31 21:04:18 +000065def SDT_WebAssemblyReturn : SDTypeProfile<0, -1, []>;
JF Bastienaf111db2015-08-24 22:16:48 +000066def SDT_WebAssemblyWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
67 SDTCisPtrTy<0>]>;
Heejin Ahnda419bd2018-11-14 02:46:21 +000068def SDT_WebAssemblyThrow : SDTypeProfile<0, 2, [SDTCisPtrTy<0>]>;
JF Bastien600aee92015-07-31 17:53:38 +000069
Dan Gohman10e730a2015-06-29 23:51:55 +000070//===----------------------------------------------------------------------===//
71// WebAssembly-specific DAG Nodes.
72//===----------------------------------------------------------------------===//
73
JF Bastienaf111db2015-08-24 22:16:48 +000074def WebAssemblycallseq_start :
75 SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
76 [SDNPHasChain, SDNPOutGlue]>;
77def WebAssemblycallseq_end :
78 SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
79 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
Dan Gohmanf71abef2015-09-09 16:13:47 +000080def WebAssemblycall0 : SDNode<"WebAssemblyISD::CALL0",
81 SDT_WebAssemblyCall0,
82 [SDNPHasChain, SDNPVariadic]>;
83def WebAssemblycall1 : SDNode<"WebAssemblyISD::CALL1",
84 SDT_WebAssemblyCall1,
85 [SDNPHasChain, SDNPVariadic]>;
Dan Gohman14026062016-03-08 03:18:12 +000086def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
87 SDT_WebAssemblyBrTable,
88 [SDNPHasChain, SDNPVariadic]>;
JF Bastien600aee92015-07-31 17:53:38 +000089def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
90 SDT_WebAssemblyArgument>;
91def WebAssemblyreturn : SDNode<"WebAssemblyISD::RETURN",
JF Bastienaf111db2015-08-24 22:16:48 +000092 SDT_WebAssemblyReturn, [SDNPHasChain]>;
93def WebAssemblywrapper : SDNode<"WebAssemblyISD::Wrapper",
94 SDT_WebAssemblyWrapper>;
Heejin Ahnda419bd2018-11-14 02:46:21 +000095def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
96 [SDNPHasChain]>;
JF Bastien600aee92015-07-31 17:53:38 +000097
Dan Gohman10e730a2015-06-29 23:51:55 +000098//===----------------------------------------------------------------------===//
99// WebAssembly-specific Operands.
100//===----------------------------------------------------------------------===//
101
Dan Gohmana11fb232016-01-12 03:09:16 +0000102let OperandNamespace = "WebAssembly" in {
103
104let OperandType = "OPERAND_BASIC_BLOCK" in
Dan Gohman950a13c2015-09-16 16:51:30 +0000105def bb_op : Operand<OtherVT>;
JF Bastienaf111db2015-08-24 22:16:48 +0000106
Dan Gohman4fc4e422016-10-24 19:49:43 +0000107let OperandType = "OPERAND_LOCAL" in
108def local_op : Operand<i32>;
109
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000110let OperandType = "OPERAND_GLOBAL" in
111def global_op : Operand<i32>;
112
Dan Gohman5a68ec72016-10-05 21:24:08 +0000113let OperandType = "OPERAND_I32IMM" in
114def i32imm_op : Operand<i32>;
115
116let OperandType = "OPERAND_I64IMM" in
117def i64imm_op : Operand<i64>;
118
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000119let OperandType = "OPERAND_F32IMM" in
Dan Gohmana11fb232016-01-12 03:09:16 +0000120def f32imm_op : Operand<f32>;
Dan Gohmanaa742912016-02-16 15:14:23 +0000121
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000122let OperandType = "OPERAND_F64IMM" in
Dan Gohmana11fb232016-01-12 03:09:16 +0000123def f64imm_op : Operand<f64>;
Dan Gohmana11fb232016-01-12 03:09:16 +0000124
Thomas Lively22442922018-08-21 21:03:18 +0000125let OperandType = "OPERAND_VEC_I8IMM" in
126def vec_i8imm_op : Operand<i32>;
127
128let OperandType = "OPERAND_VEC_I16IMM" in
129def vec_i16imm_op : Operand<i32>;
130
131let OperandType = "OPERAND_VEC_I32IMM" in
132def vec_i32imm_op : Operand<i32>;
133
134let OperandType = "OPERAND_VEC_I64IMM" in
135def vec_i64imm_op : Operand<i64>;
136
Dan Gohman00d734d2016-12-23 03:23:52 +0000137let OperandType = "OPERAND_FUNCTION32" in
138def function32_op : Operand<i32>;
139
140let OperandType = "OPERAND_OFFSET32" in
141def offset32_op : Operand<i32>;
142
Dan Gohmanbb372242016-01-26 03:39:31 +0000143let OperandType = "OPERAND_P2ALIGN" in {
144def P2Align : Operand<i32> {
145 let PrintMethod = "printWebAssemblyP2AlignOperand";
146}
Heejin Ahnda419bd2018-11-14 02:46:21 +0000147
148let OperandType = "OPERAND_EVENT" in
149def event_op : Operand<i32>;
150
Dan Gohmanbb372242016-01-26 03:39:31 +0000151} // OperandType = "OPERAND_P2ALIGN"
152
Dan Gohman2726b882016-10-06 22:29:32 +0000153let OperandType = "OPERAND_SIGNATURE" in {
154def Signature : Operand<i32> {
155 let PrintMethod = "printWebAssemblySignatureOperand";
156}
157} // OperandType = "OPERAND_SIGNATURE"
158
Dan Gohmand934cb82017-02-24 23:18:00 +0000159let OperandType = "OPERAND_TYPEINDEX" in
160def TypeIndex : Operand<i32>;
161
Dan Gohmana11fb232016-01-12 03:09:16 +0000162} // OperandNamespace = "WebAssembly"
163
Dan Gohman10e730a2015-06-29 23:51:55 +0000164//===----------------------------------------------------------------------===//
Thomas Livelyc63b5fc2018-10-22 21:55:26 +0000165// WebAssembly Register to Stack instruction mapping
166//===----------------------------------------------------------------------===//
167
168class StackRel;
169def getStackOpcode : InstrMapping {
170 let FilterClass = "StackRel";
171 let RowFields = ["BaseName"];
172 let ColFields = ["StackBased"];
173 let KeyCol = ["false"];
174 let ValueCols = [["true"]];
175}
176
177//===----------------------------------------------------------------------===//
Dan Gohman10e730a2015-06-29 23:51:55 +0000178// WebAssembly Instruction Format Definitions.
179//===----------------------------------------------------------------------===//
180
181include "WebAssemblyInstrFormats.td"
182
Dan Gohman4ba48162015-11-18 16:12:01 +0000183//===----------------------------------------------------------------------===//
184// Additional instructions.
185//===----------------------------------------------------------------------===//
186
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000187multiclass ARGUMENT<WebAssemblyRegClass reg, ValueType vt> {
Thomas Livelyf04bed82018-10-11 20:21:22 +0000188 let hasSideEffects = 1, isCodeGenOnly = 1,
189 Defs = []<Register>, Uses = [ARGUMENTS] in
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000190 defm ARGUMENT_#vt :
191 I<(outs reg:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
192 [(set (vt reg:$res), (WebAssemblyargument timm:$argno))]>;
JF Bastien8f9aea02015-08-01 04:48:44 +0000193}
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000194defm "": ARGUMENT<I32, i32>;
195defm "": ARGUMENT<I64, i64>;
196defm "": ARGUMENT<F32, f32>;
197defm "": ARGUMENT<F64, f64>;
198defm "": ARGUMENT<EXCEPT_REF, ExceptRef>;
JF Bastien600aee92015-07-31 17:53:38 +0000199
Thomas Lively6a87dda2019-01-08 06:25:55 +0000200// local.get and local.set are not generated by instruction selection; they
Dan Gohmanadf28172016-01-28 01:22:44 +0000201// are implied by virtual register uses and defs.
Dan Gohman4ba48162015-11-18 16:12:01 +0000202multiclass LOCAL<WebAssemblyRegClass vt> {
Dan Gohman4817a752016-05-16 19:16:32 +0000203let hasSideEffects = 0 in {
Thomas Lively6a87dda2019-01-08 06:25:55 +0000204 // COPY is not an actual instruction in wasm, but since we allow local.get and
205 // local.set to be implicit during most of codegen, we can have a COPY which
206 // is actually a no-op because all the work is done in the implied local.get
207 // and local.set. COPYs are eliminated (and replaced with
208 // local.get/local.set) in the ExplicitLocals pass.
Dan Gohman4fc4e422016-10-24 19:49:43 +0000209 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000210 defm COPY_#vt : I<(outs vt:$res), (ins vt:$src), (outs), (ins), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000211 "local.copy\t$res, $src", "local.copy">;
Dan Gohmanadf28172016-01-28 01:22:44 +0000212
Dan Gohman4fc4e422016-10-24 19:49:43 +0000213 // TEE is similar to COPY, but writes two copies of its result. Typically
214 // this would be used to stackify one result and write the other result to a
215 // local.
216 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000217 defm TEE_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), (outs), (ins), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000218 "local.tee\t$res, $also, $src", "local.tee">;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000219
Thomas Lively6a87dda2019-01-08 06:25:55 +0000220 // This is the actual local.get instruction in wasm. These are made explicit
Dan Gohman4fc4e422016-10-24 19:49:43 +0000221 // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
222 // local, which is a side effect not otherwise modeled in LLVM.
223 let mayLoad = 1, isAsCheapAsAMove = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000224 defm LOCAL_GET_#vt : I<(outs vt:$res), (ins local_op:$local),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000225 (outs), (ins local_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000226 "local.get\t$res, $local", "local.get\t$local", 0x20>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000227
Thomas Lively6a87dda2019-01-08 06:25:55 +0000228 // This is the actual local.set instruction in wasm. These are made explicit
Dan Gohman4fc4e422016-10-24 19:49:43 +0000229 // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
230 // local, which is a side effect not otherwise modeled in LLVM.
231 let mayStore = 1, isAsCheapAsAMove = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000232 defm LOCAL_SET_#vt : I<(outs), (ins local_op:$local, vt:$src),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000233 (outs), (ins local_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000234 "local.set\t$local, $src", "local.set\t$local", 0x21>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000235
Thomas Lively6a87dda2019-01-08 06:25:55 +0000236 // This is the actual local.tee instruction in wasm. TEEs are turned into
237 // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
238 // as LOCAL_SET.
Dan Gohman4fc4e422016-10-24 19:49:43 +0000239 let mayStore = 1, isAsCheapAsAMove = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000240 defm LOCAL_TEE_#vt : I<(outs vt:$res), (ins local_op:$local, vt:$src),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000241 (outs), (ins local_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000242 "local.tee\t$res, $local, $src", "local.tee\t$local",
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000243 0x22>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000244
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000245 // Unused values must be dropped in some contexts.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000246 defm DROP_#vt : I<(outs), (ins vt:$src), (outs), (ins), [],
247 "drop\t$src", "drop", 0x1a>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000248
249 let mayLoad = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000250 defm GLOBAL_GET_#vt : I<(outs vt:$res), (ins global_op:$local),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000251 (outs), (ins global_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000252 "global.get\t$res, $local", "global.get\t$local",
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000253 0x23>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000254
255 let mayStore = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000256 defm GLOBAL_SET_#vt : I<(outs), (ins global_op:$local, vt:$src),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000257 (outs), (ins global_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000258 "global.set\t$local, $src", "global.set\t$local",
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000259 0x24>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000260
Dan Gohman4817a752016-05-16 19:16:32 +0000261} // hasSideEffects = 0
Dan Gohman4ba48162015-11-18 16:12:01 +0000262}
Sam Clegg537afe62018-06-04 16:59:26 +0000263defm "" : LOCAL<I32>;
264defm "" : LOCAL<I64>;
265defm "" : LOCAL<F32>;
266defm "" : LOCAL<F64>;
267defm "" : LOCAL<V128>, Requires<[HasSIMD128]>;
268defm "" : LOCAL<EXCEPT_REF>, Requires<[HasExceptionHandling]>;
JF Bastien4a642252015-08-10 22:36:48 +0000269
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000270let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000271defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
272 (outs), (ins i32imm_op:$imm),
273 [(set I32:$res, imm:$imm)],
274 "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
275defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
276 (outs), (ins i64imm_op:$imm),
277 [(set I64:$res, imm:$imm)],
278 "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
279defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
280 (outs), (ins f32imm_op:$imm),
281 [(set F32:$res, fpimm:$imm)],
282 "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
283defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
284 (outs), (ins f64imm_op:$imm),
285 [(set F64:$res, fpimm:$imm)],
286 "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000287} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
JF Bastien4a642252015-08-10 22:36:48 +0000288
Dan Gohman8633eed2016-01-08 00:50:33 +0000289def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
290 (CONST_I32 tglobaladdr:$addr)>;
291def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
292 (CONST_I32 texternalsym:$addr)>;
Heejin Ahn24faf852018-10-25 23:55:10 +0000293def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
294def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
Derek Schuff71e81692015-11-17 00:20:44 +0000295
Dan Gohman10e730a2015-06-29 23:51:55 +0000296//===----------------------------------------------------------------------===//
297// Additional sets of instructions.
298//===----------------------------------------------------------------------===//
299
JF Bastien5ca0bac2015-07-10 18:23:10 +0000300include "WebAssemblyInstrMemory.td"
301include "WebAssemblyInstrCall.td"
JF Bastien600aee92015-07-31 17:53:38 +0000302include "WebAssemblyInstrControl.td"
JF Bastien5ca0bac2015-07-10 18:23:10 +0000303include "WebAssemblyInstrInteger.td"
JF Bastien5ca0bac2015-07-10 18:23:10 +0000304include "WebAssemblyInstrConv.td"
Dan Gohmanf170ba02015-12-10 04:55:31 +0000305include "WebAssemblyInstrFloat.td"
Dan Gohman10e730a2015-06-29 23:51:55 +0000306include "WebAssemblyInstrAtomics.td"
307include "WebAssemblyInstrSIMD.td"
Heejin Ahn0de58722018-03-08 04:05:37 +0000308include "WebAssemblyInstrExceptRef.td"