blob: 085c5a77e68af79de223bdd232c0341b3608b0e6 [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">;
Derek Schuff18ba1922017-08-30 18:07:45 +000023def HasAtomics : Predicate<"Subtarget->hasAtomics()">,
24 AssemblerPredicate<"FeatureAtomics", "atomics">;
Dan Gohmancdd48b82017-11-28 01:13:40 +000025def HasNontrappingFPToInt :
26 Predicate<"Subtarget->hasNontrappingFPToInt()">,
27 AssemblerPredicate<"FeatureNontrappingFPToInt",
28 "nontrapping-fptoint">;
29def NotHasNontrappingFPToInt :
30 Predicate<"!Subtarget->hasNontrappingFPToInt()">,
31 AssemblerPredicate<"!FeatureNontrappingFPToInt",
32 "nontrapping-fptoint">;
Dan Gohman5d2b9352018-01-19 17:16:24 +000033def HasSignExt :
34 Predicate<"Subtarget->hasSignExt()">,
35 AssemblerPredicate<"FeatureSignExt",
36 "sign-ext">;
37def NotHasSignExt :
38 Predicate<"!Subtarget->hasSignExt()">,
39 AssemblerPredicate<"!FeatureSignExt",
40 "sign-ext">;
JF Bastien03855df2015-07-01 23:41:25 +000041
Heejin Ahn9386bde2018-02-24 00:40:50 +000042def HasExceptionHandling :
43 Predicate<"Subtarget->hasExceptionHandling()">,
44 AssemblerPredicate<"FeatureExceptionHandling",
45 "exception-handling">;
46
47def NotHasExceptionHandling :
48 Predicate<"!Subtarget->hasExceptionHandling()">,
49 AssemblerPredicate<"!FeatureExceptionHandling",
50 "exception-handling">;
51
Dan Gohman10e730a2015-06-29 23:51:55 +000052//===----------------------------------------------------------------------===//
53// WebAssembly-specific DAG Node Types.
54//===----------------------------------------------------------------------===//
55
Serge Pavlovd526b132017-05-09 13:35:13 +000056def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>,
57 SDTCisVT<1, iPTR>]>;
JF Bastienaf111db2015-08-24 22:16:48 +000058def SDT_WebAssemblyCallSeqEnd :
59 SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
Dan Gohmanf71abef2015-09-09 16:13:47 +000060def SDT_WebAssemblyCall0 : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
61def SDT_WebAssemblyCall1 : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
Dan Gohman14026062016-03-08 03:18:12 +000062def SDT_WebAssemblyBrTable : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
JF Bastien600aee92015-07-31 17:53:38 +000063def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
JF Bastien4a2d5602015-07-31 21:04:18 +000064def SDT_WebAssemblyReturn : SDTypeProfile<0, -1, []>;
JF Bastienaf111db2015-08-24 22:16:48 +000065def SDT_WebAssemblyWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
66 SDTCisPtrTy<0>]>;
Heejin Ahnda419bd2018-11-14 02:46:21 +000067def SDT_WebAssemblyThrow : SDTypeProfile<0, 2, [SDTCisPtrTy<0>]>;
JF Bastien600aee92015-07-31 17:53:38 +000068
Dan Gohman10e730a2015-06-29 23:51:55 +000069//===----------------------------------------------------------------------===//
70// WebAssembly-specific DAG Nodes.
71//===----------------------------------------------------------------------===//
72
JF Bastienaf111db2015-08-24 22:16:48 +000073def WebAssemblycallseq_start :
74 SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
75 [SDNPHasChain, SDNPOutGlue]>;
76def WebAssemblycallseq_end :
77 SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
78 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
Dan Gohmanf71abef2015-09-09 16:13:47 +000079def WebAssemblycall0 : SDNode<"WebAssemblyISD::CALL0",
80 SDT_WebAssemblyCall0,
81 [SDNPHasChain, SDNPVariadic]>;
82def WebAssemblycall1 : SDNode<"WebAssemblyISD::CALL1",
83 SDT_WebAssemblyCall1,
84 [SDNPHasChain, SDNPVariadic]>;
Dan Gohman14026062016-03-08 03:18:12 +000085def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
86 SDT_WebAssemblyBrTable,
87 [SDNPHasChain, SDNPVariadic]>;
JF Bastien600aee92015-07-31 17:53:38 +000088def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
89 SDT_WebAssemblyArgument>;
90def WebAssemblyreturn : SDNode<"WebAssemblyISD::RETURN",
JF Bastienaf111db2015-08-24 22:16:48 +000091 SDT_WebAssemblyReturn, [SDNPHasChain]>;
92def WebAssemblywrapper : SDNode<"WebAssemblyISD::Wrapper",
93 SDT_WebAssemblyWrapper>;
Heejin Ahnda419bd2018-11-14 02:46:21 +000094def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
95 [SDNPHasChain]>;
JF Bastien600aee92015-07-31 17:53:38 +000096
Dan Gohman10e730a2015-06-29 23:51:55 +000097//===----------------------------------------------------------------------===//
98// WebAssembly-specific Operands.
99//===----------------------------------------------------------------------===//
100
Dan Gohmana11fb232016-01-12 03:09:16 +0000101let OperandNamespace = "WebAssembly" in {
102
103let OperandType = "OPERAND_BASIC_BLOCK" in
Dan Gohman950a13c2015-09-16 16:51:30 +0000104def bb_op : Operand<OtherVT>;
JF Bastienaf111db2015-08-24 22:16:48 +0000105
Dan Gohman4fc4e422016-10-24 19:49:43 +0000106let OperandType = "OPERAND_LOCAL" in
107def local_op : Operand<i32>;
108
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000109let OperandType = "OPERAND_GLOBAL" in
110def global_op : Operand<i32>;
111
Dan Gohman5a68ec72016-10-05 21:24:08 +0000112let OperandType = "OPERAND_I32IMM" in
113def i32imm_op : Operand<i32>;
114
115let OperandType = "OPERAND_I64IMM" in
116def i64imm_op : Operand<i64>;
117
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000118let OperandType = "OPERAND_F32IMM" in
Dan Gohmana11fb232016-01-12 03:09:16 +0000119def f32imm_op : Operand<f32>;
Dan Gohmanaa742912016-02-16 15:14:23 +0000120
Dan Gohman4b8e8be2016-10-03 21:31:31 +0000121let OperandType = "OPERAND_F64IMM" in
Dan Gohmana11fb232016-01-12 03:09:16 +0000122def f64imm_op : Operand<f64>;
Dan Gohmana11fb232016-01-12 03:09:16 +0000123
Thomas Lively22442922018-08-21 21:03:18 +0000124let OperandType = "OPERAND_VEC_I8IMM" in
125def vec_i8imm_op : Operand<i32>;
126
127let OperandType = "OPERAND_VEC_I16IMM" in
128def vec_i16imm_op : Operand<i32>;
129
130let OperandType = "OPERAND_VEC_I32IMM" in
131def vec_i32imm_op : Operand<i32>;
132
133let OperandType = "OPERAND_VEC_I64IMM" in
134def vec_i64imm_op : Operand<i64>;
135
Dan Gohman00d734d2016-12-23 03:23:52 +0000136let OperandType = "OPERAND_FUNCTION32" in
137def function32_op : Operand<i32>;
138
139let OperandType = "OPERAND_OFFSET32" in
140def offset32_op : Operand<i32>;
141
Dan Gohmanbb372242016-01-26 03:39:31 +0000142let OperandType = "OPERAND_P2ALIGN" in {
143def P2Align : Operand<i32> {
144 let PrintMethod = "printWebAssemblyP2AlignOperand";
145}
Heejin Ahnda419bd2018-11-14 02:46:21 +0000146
147let OperandType = "OPERAND_EVENT" in
148def event_op : Operand<i32>;
149
Dan Gohmanbb372242016-01-26 03:39:31 +0000150} // OperandType = "OPERAND_P2ALIGN"
151
Dan Gohman2726b882016-10-06 22:29:32 +0000152let OperandType = "OPERAND_SIGNATURE" in {
153def Signature : Operand<i32> {
154 let PrintMethod = "printWebAssemblySignatureOperand";
155}
156} // OperandType = "OPERAND_SIGNATURE"
157
Dan Gohmand934cb82017-02-24 23:18:00 +0000158let OperandType = "OPERAND_TYPEINDEX" in
159def TypeIndex : Operand<i32>;
160
Dan Gohmana11fb232016-01-12 03:09:16 +0000161} // OperandNamespace = "WebAssembly"
162
Dan Gohman10e730a2015-06-29 23:51:55 +0000163//===----------------------------------------------------------------------===//
Thomas Livelyc63b5fc2018-10-22 21:55:26 +0000164// WebAssembly Register to Stack instruction mapping
165//===----------------------------------------------------------------------===//
166
167class StackRel;
168def getStackOpcode : InstrMapping {
169 let FilterClass = "StackRel";
170 let RowFields = ["BaseName"];
171 let ColFields = ["StackBased"];
172 let KeyCol = ["false"];
173 let ValueCols = [["true"]];
174}
175
176//===----------------------------------------------------------------------===//
Dan Gohman10e730a2015-06-29 23:51:55 +0000177// WebAssembly Instruction Format Definitions.
178//===----------------------------------------------------------------------===//
179
180include "WebAssemblyInstrFormats.td"
181
Dan Gohman4ba48162015-11-18 16:12:01 +0000182//===----------------------------------------------------------------------===//
183// Additional instructions.
184//===----------------------------------------------------------------------===//
185
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000186multiclass ARGUMENT<WebAssemblyRegClass reg, ValueType vt> {
Thomas Livelyf04bed82018-10-11 20:21:22 +0000187 let hasSideEffects = 1, isCodeGenOnly = 1,
188 Defs = []<Register>, Uses = [ARGUMENTS] in
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000189 defm ARGUMENT_#vt :
190 I<(outs reg:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
191 [(set (vt reg:$res), (WebAssemblyargument timm:$argno))]>;
JF Bastien8f9aea02015-08-01 04:48:44 +0000192}
Thomas Lively0ff82ac2018-10-13 07:09:10 +0000193defm "": ARGUMENT<I32, i32>;
194defm "": ARGUMENT<I64, i64>;
195defm "": ARGUMENT<F32, f32>;
196defm "": ARGUMENT<F64, f64>;
197defm "": ARGUMENT<EXCEPT_REF, ExceptRef>;
JF Bastien600aee92015-07-31 17:53:38 +0000198
Dan Gohman4ba48162015-11-18 16:12:01 +0000199// get_local and set_local are not generated by instruction selection; they
Dan Gohmanadf28172016-01-28 01:22:44 +0000200// are implied by virtual register uses and defs.
Dan Gohman4ba48162015-11-18 16:12:01 +0000201multiclass LOCAL<WebAssemblyRegClass vt> {
Dan Gohman4817a752016-05-16 19:16:32 +0000202let hasSideEffects = 0 in {
Dan Gohman4fc4e422016-10-24 19:49:43 +0000203 // COPY is not an actual instruction in wasm, but since we allow get_local and
204 // set_local to be implicit during most of codegen, we can have a COPY which
205 // is actually a no-op because all the work is done in the implied get_local
206 // and set_local. COPYs are eliminated (and replaced with
207 // get_local/set_local) in the ExplicitLocals pass.
208 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000209 defm COPY_#vt : I<(outs vt:$res), (ins vt:$src), (outs), (ins), [],
210 "copy_local\t$res, $src", "copy_local">;
Dan Gohmanadf28172016-01-28 01:22:44 +0000211
Dan Gohman4fc4e422016-10-24 19:49:43 +0000212 // TEE is similar to COPY, but writes two copies of its result. Typically
213 // this would be used to stackify one result and write the other result to a
214 // local.
215 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000216 defm TEE_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), (outs), (ins), [],
217 "tee_local\t$res, $also, $src", "tee_local">;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000218
219 // This is the actual get_local instruction in wasm. These are made explicit
220 // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
221 // local, which is a side effect not otherwise modeled in LLVM.
222 let mayLoad = 1, isAsCheapAsAMove = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000223 defm GET_LOCAL_#vt : I<(outs vt:$res), (ins local_op:$local),
224 (outs), (ins local_op:$local), [],
225 "get_local\t$res, $local", "get_local\t$local", 0x20>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000226
227 // This is the actual set_local instruction in wasm. These are made explicit
228 // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
229 // local, which is a side effect not otherwise modeled in LLVM.
230 let mayStore = 1, isAsCheapAsAMove = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000231 defm SET_LOCAL_#vt : I<(outs), (ins local_op:$local, vt:$src),
232 (outs), (ins local_op:$local), [],
233 "set_local\t$local, $src", "set_local\t$local", 0x21>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000234
235 // This is the actual tee_local instruction in wasm. TEEs are turned into
236 // TEE_LOCALs by the ExplicitLocals pass. It has mayStore for the same reason
237 // as SET_LOCAL.
238 let mayStore = 1, isAsCheapAsAMove = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000239 defm TEE_LOCAL_#vt : I<(outs vt:$res), (ins local_op:$local, vt:$src),
240 (outs), (ins local_op:$local), [],
241 "tee_local\t$res, $local, $src", "tee_local\t$local",
242 0x22>;
Dan Gohman4fc4e422016-10-24 19:49:43 +0000243
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000244 // Unused values must be dropped in some contexts.
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000245 defm DROP_#vt : I<(outs), (ins vt:$src), (outs), (ins), [],
246 "drop\t$src", "drop", 0x1a>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000247
248 let mayLoad = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000249 defm GET_GLOBAL_#vt : I<(outs vt:$res), (ins global_op:$local),
250 (outs), (ins global_op:$local), [],
251 "get_global\t$res, $local", "get_global\t$local",
252 0x23>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000253
254 let mayStore = 1 in
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000255 defm SET_GLOBAL_#vt : I<(outs), (ins global_op:$local, vt:$src),
256 (outs), (ins global_op:$local), [],
257 "set_global\t$local, $src", "set_global\t$local",
258 0x24>;
Dan Gohmanb89f2d32017-02-02 19:29:44 +0000259
Dan Gohman4817a752016-05-16 19:16:32 +0000260} // hasSideEffects = 0
Dan Gohman4ba48162015-11-18 16:12:01 +0000261}
Sam Clegg537afe62018-06-04 16:59:26 +0000262defm "" : LOCAL<I32>;
263defm "" : LOCAL<I64>;
264defm "" : LOCAL<F32>;
265defm "" : LOCAL<F64>;
266defm "" : LOCAL<V128>, Requires<[HasSIMD128]>;
267defm "" : LOCAL<EXCEPT_REF>, Requires<[HasExceptionHandling]>;
JF Bastien4a642252015-08-10 22:36:48 +0000268
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000269let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
Wouter van Oortmerssen48dac312018-06-18 21:22:44 +0000270defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
271 (outs), (ins i32imm_op:$imm),
272 [(set I32:$res, imm:$imm)],
273 "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
274defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
275 (outs), (ins i64imm_op:$imm),
276 [(set I64:$res, imm:$imm)],
277 "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
278defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
279 (outs), (ins f32imm_op:$imm),
280 [(set F32:$res, fpimm:$imm)],
281 "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
282defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
283 (outs), (ins f64imm_op:$imm),
284 [(set F64:$res, fpimm:$imm)],
285 "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000286} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
JF Bastien4a642252015-08-10 22:36:48 +0000287
Dan Gohman8633eed2016-01-08 00:50:33 +0000288def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
289 (CONST_I32 tglobaladdr:$addr)>;
290def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
291 (CONST_I32 texternalsym:$addr)>;
Heejin Ahn24faf852018-10-25 23:55:10 +0000292def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
293def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
Derek Schuff71e81692015-11-17 00:20:44 +0000294
Dan Gohman10e730a2015-06-29 23:51:55 +0000295//===----------------------------------------------------------------------===//
296// Additional sets of instructions.
297//===----------------------------------------------------------------------===//
298
JF Bastien5ca0bac2015-07-10 18:23:10 +0000299include "WebAssemblyInstrMemory.td"
300include "WebAssemblyInstrCall.td"
JF Bastien600aee92015-07-31 17:53:38 +0000301include "WebAssemblyInstrControl.td"
JF Bastien5ca0bac2015-07-10 18:23:10 +0000302include "WebAssemblyInstrInteger.td"
JF Bastien5ca0bac2015-07-10 18:23:10 +0000303include "WebAssemblyInstrConv.td"
Dan Gohmanf170ba02015-12-10 04:55:31 +0000304include "WebAssemblyInstrFloat.td"
Dan Gohman10e730a2015-06-29 23:51:55 +0000305include "WebAssemblyInstrAtomics.td"
306include "WebAssemblyInstrSIMD.td"
Heejin Ahn0de58722018-03-08 04:05:37 +0000307include "WebAssemblyInstrExceptRef.td"