blob: e3d795f2aab10efee9ab5c42e5710660edc58b02 [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 Lively64a39a12019-01-10 22:32:11 +000023def HasUnimplementedSIMD128 :
24 Predicate<"Subtarget->hasUnimplementedSIMD128()">,
25 AssemblerPredicate<"FeatureUnimplementedSIMD128", "unimplemented-simd128">;
Derek Schuff18ba1922017-08-30 18:07:45 +000026def HasAtomics : Predicate<"Subtarget->hasAtomics()">,
27 AssemblerPredicate<"FeatureAtomics", "atomics">;
Dan Gohmancdd48b82017-11-28 01:13:40 +000028def HasNontrappingFPToInt :
29 Predicate<"Subtarget->hasNontrappingFPToInt()">,
30 AssemblerPredicate<"FeatureNontrappingFPToInt",
31 "nontrapping-fptoint">;
32def NotHasNontrappingFPToInt :
33 Predicate<"!Subtarget->hasNontrappingFPToInt()">,
34 AssemblerPredicate<"!FeatureNontrappingFPToInt",
35 "nontrapping-fptoint">;
Dan Gohman5d2b9352018-01-19 17:16:24 +000036def HasSignExt :
37 Predicate<"Subtarget->hasSignExt()">,
38 AssemblerPredicate<"FeatureSignExt",
39 "sign-ext">;
40def NotHasSignExt :
41 Predicate<"!Subtarget->hasSignExt()">,
42 AssemblerPredicate<"!FeatureSignExt",
43 "sign-ext">;
JF Bastien03855df2015-07-01 23:41:25 +000044
Heejin Ahn9386bde2018-02-24 00:40:50 +000045def HasExceptionHandling :
46 Predicate<"Subtarget->hasExceptionHandling()">,
47 AssemblerPredicate<"FeatureExceptionHandling",
48 "exception-handling">;
49
50def NotHasExceptionHandling :
51 Predicate<"!Subtarget->hasExceptionHandling()">,
52 AssemblerPredicate<"!FeatureExceptionHandling",
53 "exception-handling">;
54
Dan Gohman10e730a2015-06-29 23:51:55 +000055//===----------------------------------------------------------------------===//
56// WebAssembly-specific DAG Node Types.
57//===----------------------------------------------------------------------===//
58
Serge Pavlovd526b132017-05-09 13:35:13 +000059def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>,
60 SDTCisVT<1, iPTR>]>;
JF Bastienaf111db2015-08-24 22:16:48 +000061def SDT_WebAssemblyCallSeqEnd :
62 SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
Dan Gohmanf71abef2015-09-09 16:13:47 +000063def SDT_WebAssemblyCall0 : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
64def SDT_WebAssemblyCall1 : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
Dan Gohman14026062016-03-08 03:18:12 +000065def SDT_WebAssemblyBrTable : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
JF Bastien600aee92015-07-31 17:53:38 +000066def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
JF Bastien4a2d5602015-07-31 21:04:18 +000067def SDT_WebAssemblyReturn : SDTypeProfile<0, -1, []>;
JF Bastienaf111db2015-08-24 22:16:48 +000068def SDT_WebAssemblyWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
69 SDTCisPtrTy<0>]>;
Heejin Ahnda419bd2018-11-14 02:46:21 +000070def SDT_WebAssemblyThrow : SDTypeProfile<0, 2, [SDTCisPtrTy<0>]>;
JF Bastien600aee92015-07-31 17:53:38 +000071
Dan Gohman10e730a2015-06-29 23:51:55 +000072//===----------------------------------------------------------------------===//
73// WebAssembly-specific DAG Nodes.
74//===----------------------------------------------------------------------===//
75
JF Bastienaf111db2015-08-24 22:16:48 +000076def WebAssemblycallseq_start :
77 SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
78 [SDNPHasChain, SDNPOutGlue]>;
79def WebAssemblycallseq_end :
80 SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
81 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
Dan Gohmanf71abef2015-09-09 16:13:47 +000082def WebAssemblycall0 : SDNode<"WebAssemblyISD::CALL0",
83 SDT_WebAssemblyCall0,
84 [SDNPHasChain, SDNPVariadic]>;
85def WebAssemblycall1 : SDNode<"WebAssemblyISD::CALL1",
86 SDT_WebAssemblyCall1,
87 [SDNPHasChain, SDNPVariadic]>;
Dan Gohman14026062016-03-08 03:18:12 +000088def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
89 SDT_WebAssemblyBrTable,
90 [SDNPHasChain, SDNPVariadic]>;
JF Bastien600aee92015-07-31 17:53:38 +000091def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
92 SDT_WebAssemblyArgument>;
93def WebAssemblyreturn : SDNode<"WebAssemblyISD::RETURN",
JF Bastienaf111db2015-08-24 22:16:48 +000094 SDT_WebAssemblyReturn, [SDNPHasChain]>;
95def WebAssemblywrapper : SDNode<"WebAssemblyISD::Wrapper",
96 SDT_WebAssemblyWrapper>;
Heejin Ahnda419bd2018-11-14 02:46:21 +000097def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
98 [SDNPHasChain]>;
JF Bastien600aee92015-07-31 17:53:38 +000099
Dan Gohman10e730a2015-06-29 23:51:55 +0000100//===----------------------------------------------------------------------===//
101// WebAssembly-specific Operands.
102//===----------------------------------------------------------------------===//
103
Dan Gohmana11fb232016-01-12 03:09:16 +0000104let OperandNamespace = "WebAssembly" in {
105
106let OperandType = "OPERAND_BASIC_BLOCK" in
Dan Gohman950a13c2015-09-16 16:51:30 +0000107def bb_op : Operand<OtherVT>;
JF Bastienaf111db2015-08-24 22:16:48 +0000108
Dan Gohman4fc4e422016-10-24 19:49:43 +0000109let OperandType = "OPERAND_LOCAL" in
110def local_op : Operand<i32>;
111
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000112let OperandType = "OPERAND_GLOBAL" in
113def global_op : Operand<i32>;
114
Dan Gohman5a68ec72016-10-05 21:24:08 +0000115let OperandType = "OPERAND_I32IMM" in
116def i32imm_op : Operand<i32>;
117
118let OperandType = "OPERAND_I64IMM" in
119def i64imm_op : Operand<i64>;
120
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000121let OperandType = "OPERAND_F32IMM" in
Dan Gohmana11fb232016-01-12 03:09:16 +0000122def f32imm_op : Operand<f32>;
Dan Gohmanaa742912016-02-16 15:14:23 +0000123
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000124let OperandType = "OPERAND_F64IMM" in
Dan Gohmana11fb232016-01-12 03:09:16 +0000125def f64imm_op : Operand<f64>;
Dan Gohmana11fb232016-01-12 03:09:16 +0000126
Thomas Lively22442922018-08-21 21:03:18 +0000127let OperandType = "OPERAND_VEC_I8IMM" in
128def vec_i8imm_op : Operand<i32>;
129
130let OperandType = "OPERAND_VEC_I16IMM" in
131def vec_i16imm_op : Operand<i32>;
132
133let OperandType = "OPERAND_VEC_I32IMM" in
134def vec_i32imm_op : Operand<i32>;
135
136let OperandType = "OPERAND_VEC_I64IMM" in
137def vec_i64imm_op : Operand<i64>;
138
Dan Gohman00d734d2016-12-23 03:23:52 +0000139let OperandType = "OPERAND_FUNCTION32" in
140def function32_op : Operand<i32>;
141
142let OperandType = "OPERAND_OFFSET32" in
143def offset32_op : Operand<i32>;
144
Dan Gohmanbb372242016-01-26 03:39:31 +0000145let OperandType = "OPERAND_P2ALIGN" in {
146def P2Align : Operand<i32> {
147 let PrintMethod = "printWebAssemblyP2AlignOperand";
148}
Heejin Ahnda419bd2018-11-14 02:46:21 +0000149
150let OperandType = "OPERAND_EVENT" in
151def event_op : Operand<i32>;
152
Dan Gohmanbb372242016-01-26 03:39:31 +0000153} // OperandType = "OPERAND_P2ALIGN"
154
Dan Gohman2726b882016-10-06 22:29:32 +0000155let OperandType = "OPERAND_SIGNATURE" in {
156def Signature : Operand<i32> {
157 let PrintMethod = "printWebAssemblySignatureOperand";
158}
159} // OperandType = "OPERAND_SIGNATURE"
160
Dan Gohmand934cb82017-02-24 23:18:00 +0000161let OperandType = "OPERAND_TYPEINDEX" in
162def TypeIndex : Operand<i32>;
163
Dan Gohmana11fb232016-01-12 03:09:16 +0000164} // OperandNamespace = "WebAssembly"
165
Dan Gohman10e730a2015-06-29 23:51:55 +0000166//===----------------------------------------------------------------------===//
Thomas Livelyc63b5fc2018-10-22 21:55:26 +0000167// WebAssembly Register to Stack instruction mapping
168//===----------------------------------------------------------------------===//
169
170class StackRel;
171def getStackOpcode : InstrMapping {
172 let FilterClass = "StackRel";
173 let RowFields = ["BaseName"];
174 let ColFields = ["StackBased"];
175 let KeyCol = ["false"];
176 let ValueCols = [["true"]];
177}
178
179//===----------------------------------------------------------------------===//
Dan Gohman10e730a2015-06-29 23:51:55 +0000180// WebAssembly Instruction Format Definitions.
181//===----------------------------------------------------------------------===//
182
183include "WebAssemblyInstrFormats.td"
184
Dan Gohman4ba48162015-11-18 16:12:01 +0000185//===----------------------------------------------------------------------===//
186// Additional instructions.
187//===----------------------------------------------------------------------===//
188
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000189multiclass ARGUMENT<WebAssemblyRegClass reg, ValueType vt> {
Thomas Livelyf04bed82018-10-11 20:21:22 +0000190 let hasSideEffects = 1, isCodeGenOnly = 1,
191 Defs = []<Register>, Uses = [ARGUMENTS] in
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000192 defm ARGUMENT_#vt :
193 I<(outs reg:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
194 [(set (vt reg:$res), (WebAssemblyargument timm:$argno))]>;
JF Bastien8f9aea02015-08-01 04:48:44 +0000195}
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000196defm "": ARGUMENT<I32, i32>;
197defm "": ARGUMENT<I64, i64>;
198defm "": ARGUMENT<F32, f32>;
199defm "": ARGUMENT<F64, f64>;
200defm "": ARGUMENT<EXCEPT_REF, ExceptRef>;
JF Bastien600aee92015-07-31 17:53:38 +0000201
Thomas Lively6a87dda2019-01-08 06:25:55 +0000202// local.get and local.set are not generated by instruction selection; they
Dan Gohmanadf28172016-01-28 01:22:44 +0000203// are implied by virtual register uses and defs.
Dan Gohman4ba48162015-11-18 16:12:01 +0000204multiclass LOCAL<WebAssemblyRegClass vt> {
Dan Gohman4817a752016-05-16 19:16:32 +0000205let hasSideEffects = 0 in {
Thomas Lively6a87dda2019-01-08 06:25:55 +0000206 // COPY is not an actual instruction in wasm, but since we allow local.get and
207 // local.set to be implicit during most of codegen, we can have a COPY which
208 // is actually a no-op because all the work is done in the implied local.get
209 // and local.set. COPYs are eliminated (and replaced with
210 // local.get/local.set) in the ExplicitLocals pass.
Dan Gohman4fc4e422016-10-24 19:49:43 +0000211 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000212 defm COPY_#vt : I<(outs vt:$res), (ins vt:$src), (outs), (ins), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000213 "local.copy\t$res, $src", "local.copy">;
Dan Gohmanadf28172016-01-28 01:22:44 +0000214
Dan Gohman4fc4e422016-10-24 19:49:43 +0000215 // TEE is similar to COPY, but writes two copies of its result. Typically
216 // this would be used to stackify one result and write the other result to a
217 // local.
218 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000219 defm TEE_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), (outs), (ins), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000220 "local.tee\t$res, $also, $src", "local.tee">;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000221
Thomas Lively6a87dda2019-01-08 06:25:55 +0000222 // This is the actual local.get instruction in wasm. These are made explicit
Dan Gohman4fc4e422016-10-24 19:49:43 +0000223 // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
224 // local, which is a side effect not otherwise modeled in LLVM.
225 let mayLoad = 1, isAsCheapAsAMove = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000226 defm LOCAL_GET_#vt : I<(outs vt:$res), (ins local_op:$local),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000227 (outs), (ins local_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000228 "local.get\t$res, $local", "local.get\t$local", 0x20>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000229
Thomas Lively6a87dda2019-01-08 06:25:55 +0000230 // This is the actual local.set instruction in wasm. These are made explicit
Dan Gohman4fc4e422016-10-24 19:49:43 +0000231 // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
232 // local, which is a side effect not otherwise modeled in LLVM.
233 let mayStore = 1, isAsCheapAsAMove = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000234 defm LOCAL_SET_#vt : I<(outs), (ins local_op:$local, vt:$src),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000235 (outs), (ins local_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000236 "local.set\t$local, $src", "local.set\t$local", 0x21>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000237
Thomas Lively6a87dda2019-01-08 06:25:55 +0000238 // This is the actual local.tee instruction in wasm. TEEs are turned into
239 // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
240 // as LOCAL_SET.
Dan Gohman4fc4e422016-10-24 19:49:43 +0000241 let mayStore = 1, isAsCheapAsAMove = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000242 defm LOCAL_TEE_#vt : I<(outs vt:$res), (ins local_op:$local, vt:$src),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000243 (outs), (ins local_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000244 "local.tee\t$res, $local, $src", "local.tee\t$local",
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000245 0x22>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000246
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000247 // Unused values must be dropped in some contexts.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000248 defm DROP_#vt : I<(outs), (ins vt:$src), (outs), (ins), [],
249 "drop\t$src", "drop", 0x1a>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000250
251 let mayLoad = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000252 defm GLOBAL_GET_#vt : I<(outs vt:$res), (ins global_op:$local),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000253 (outs), (ins global_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000254 "global.get\t$res, $local", "global.get\t$local",
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000255 0x23>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000256
257 let mayStore = 1 in
Thomas Lively6a87dda2019-01-08 06:25:55 +0000258 defm GLOBAL_SET_#vt : I<(outs), (ins global_op:$local, vt:$src),
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000259 (outs), (ins global_op:$local), [],
Thomas Lively6a87dda2019-01-08 06:25:55 +0000260 "global.set\t$local, $src", "global.set\t$local",
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000261 0x24>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000262
Dan Gohman4817a752016-05-16 19:16:32 +0000263} // hasSideEffects = 0
Dan Gohman4ba48162015-11-18 16:12:01 +0000264}
Sam Clegg537afe62018-06-04 16:59:26 +0000265defm "" : LOCAL<I32>;
266defm "" : LOCAL<I64>;
267defm "" : LOCAL<F32>;
268defm "" : LOCAL<F64>;
269defm "" : LOCAL<V128>, Requires<[HasSIMD128]>;
270defm "" : LOCAL<EXCEPT_REF>, Requires<[HasExceptionHandling]>;
JF Bastien4a642252015-08-10 22:36:48 +0000271
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000272let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000273defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
274 (outs), (ins i32imm_op:$imm),
275 [(set I32:$res, imm:$imm)],
276 "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
277defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
278 (outs), (ins i64imm_op:$imm),
279 [(set I64:$res, imm:$imm)],
280 "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
281defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
282 (outs), (ins f32imm_op:$imm),
283 [(set F32:$res, fpimm:$imm)],
284 "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
285defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
286 (outs), (ins f64imm_op:$imm),
287 [(set F64:$res, fpimm:$imm)],
288 "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000289} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
JF Bastien4a642252015-08-10 22:36:48 +0000290
Dan Gohman8633eed2016-01-08 00:50:33 +0000291def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
292 (CONST_I32 tglobaladdr:$addr)>;
293def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
294 (CONST_I32 texternalsym:$addr)>;
Heejin Ahn24faf852018-10-25 23:55:10 +0000295def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
296def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
Derek Schuff71e81692015-11-17 00:20:44 +0000297
Dan Gohman10e730a2015-06-29 23:51:55 +0000298//===----------------------------------------------------------------------===//
299// Additional sets of instructions.
300//===----------------------------------------------------------------------===//
301
JF Bastien5ca0bac2015-07-10 18:23:10 +0000302include "WebAssemblyInstrMemory.td"
303include "WebAssemblyInstrCall.td"
JF Bastien600aee92015-07-31 17:53:38 +0000304include "WebAssemblyInstrControl.td"
JF Bastien5ca0bac2015-07-10 18:23:10 +0000305include "WebAssemblyInstrInteger.td"
JF Bastien5ca0bac2015-07-10 18:23:10 +0000306include "WebAssemblyInstrConv.td"
Dan Gohmanf170ba02015-12-10 04:55:31 +0000307include "WebAssemblyInstrFloat.td"
Dan Gohman10e730a2015-06-29 23:51:55 +0000308include "WebAssemblyInstrAtomics.td"
309include "WebAssemblyInstrSIMD.td"
Heejin Ahn0de58722018-03-08 04:05:37 +0000310include "WebAssemblyInstrExceptRef.td"