1// WebAssemblyInstrInfo.td-Describe the WebAssembly Instructions-*- tablegen -*-
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// WebAssembly Instruction definitions.
11///
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15// WebAssembly Instruction Predicate Definitions.
16//===----------------------------------------------------------------------===//
17
18def IsPIC     : Predicate<"TM.isPositionIndependent()">;
19def IsNotPIC  : Predicate<"!TM.isPositionIndependent()">;
20
21def HasAddr32 : Predicate<"!Subtarget->hasAddr64()">;
22
23def HasAddr64 : Predicate<"Subtarget->hasAddr64()">;
24
25def HasSIMD128 :
26    Predicate<"Subtarget->hasSIMD128()">,
27    AssemblerPredicate<(all_of FeatureSIMD128), "simd128">;
28
29def HasUnimplementedSIMD128 :
30    Predicate<"Subtarget->hasUnimplementedSIMD128()">,
31    AssemblerPredicate<(all_of FeatureUnimplementedSIMD128), "unimplemented-simd128">;
32
33def HasAtomics :
34    Predicate<"Subtarget->hasAtomics()">,
35    AssemblerPredicate<(all_of FeatureAtomics), "atomics">;
36
37def HasMultivalue :
38    Predicate<"Subtarget->hasMultivalue()">,
39    AssemblerPredicate<(all_of FeatureMultivalue), "multivalue">;
40
41def HasNontrappingFPToInt :
42    Predicate<"Subtarget->hasNontrappingFPToInt()">,
43    AssemblerPredicate<(all_of FeatureNontrappingFPToInt), "nontrapping-fptoint">;
44
45def NotHasNontrappingFPToInt :
46    Predicate<"!Subtarget->hasNontrappingFPToInt()">,
47    AssemblerPredicate<(all_of (not FeatureNontrappingFPToInt)), "nontrapping-fptoint">;
48
49def HasSignExt :
50    Predicate<"Subtarget->hasSignExt()">,
51    AssemblerPredicate<(all_of FeatureSignExt), "sign-ext">;
52
53def HasTailCall :
54    Predicate<"Subtarget->hasTailCall()">,
55    AssemblerPredicate<(all_of FeatureTailCall), "tail-call">;
56
57def HasExceptionHandling :
58    Predicate<"Subtarget->hasExceptionHandling()">,
59    AssemblerPredicate<(all_of FeatureExceptionHandling), "exception-handling">;
60
61def HasBulkMemory :
62    Predicate<"Subtarget->hasBulkMemory()">,
63    AssemblerPredicate<(all_of FeatureBulkMemory), "bulk-memory">;
64
65def HasReferenceTypes :
66    Predicate<"Subtarget->hasReferenceTypes()">,
67    AssemblerPredicate<(all_of FeatureReferenceTypes), "reference-types">;
68
69//===----------------------------------------------------------------------===//
70// WebAssembly-specific DAG Node Types.
71//===----------------------------------------------------------------------===//
72
73def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>,
74                                                  SDTCisVT<1, iPTR>]>;
75def SDT_WebAssemblyCallSeqEnd :
76    SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
77def SDT_WebAssemblyCall0      : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
78def SDT_WebAssemblyCall1      : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
79def SDT_WebAssemblyBrTable    : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
80def SDT_WebAssemblyArgument   : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
81def SDT_WebAssemblyReturn     : SDTypeProfile<0, -1, []>;
82def SDT_WebAssemblyWrapper    : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
83                                                     SDTCisPtrTy<0>]>;
84def SDT_WebAssemblyWrapperPIC : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
85                                                     SDTCisPtrTy<0>]>;
86def SDT_WebAssemblyThrow      : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
87
88//===----------------------------------------------------------------------===//
89// WebAssembly-specific DAG Nodes.
90//===----------------------------------------------------------------------===//
91
92def WebAssemblycallseq_start :
93    SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
94           [SDNPHasChain, SDNPOutGlue]>;
95def WebAssemblycallseq_end :
96    SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
97           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
98def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
99                                 SDT_WebAssemblyBrTable,
100                                 [SDNPHasChain, SDNPVariadic]>;
101def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
102                                 SDT_WebAssemblyArgument>;
103def WebAssemblyreturn   : SDNode<"WebAssemblyISD::RETURN",
104                                 SDT_WebAssemblyReturn,
105                                 [SDNPHasChain, SDNPVariadic]>;
106def WebAssemblywrapper  : SDNode<"WebAssemblyISD::Wrapper",
107                                 SDT_WebAssemblyWrapper>;
108def WebAssemblywrapperPIC  : SDNode<"WebAssemblyISD::WrapperPIC",
109                                     SDT_WebAssemblyWrapperPIC>;
110def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
111                              [SDNPHasChain, SDNPVariadic]>;
112
113//===----------------------------------------------------------------------===//
114// WebAssembly-specific Operands.
115//===----------------------------------------------------------------------===//
116
117// Default Operand has AsmOperandClass "Imm" which is for integers (and
118// symbols), so specialize one for floats:
119def FPImmAsmOperand : AsmOperandClass {
120  let Name = "FPImm";
121  let PredicateMethod = "isFPImm";
122}
123
124class FPOperand<ValueType ty> : Operand<ty> {
125  AsmOperandClass ParserMatchClass = FPImmAsmOperand;
126}
127
128let OperandNamespace = "WebAssembly" in {
129
130let OperandType = "OPERAND_BASIC_BLOCK" in
131def bb_op : Operand<OtherVT>;
132
133let OperandType = "OPERAND_LOCAL" in
134def local_op : Operand<i32>;
135
136let OperandType = "OPERAND_GLOBAL" in
137def global_op : Operand<i32>;
138
139let OperandType = "OPERAND_I32IMM" in
140def i32imm_op : Operand<i32>;
141
142let OperandType = "OPERAND_I64IMM" in
143def i64imm_op : Operand<i64>;
144
145let OperandType = "OPERAND_F32IMM" in
146def f32imm_op : FPOperand<f32>;
147
148let OperandType = "OPERAND_F64IMM" in
149def f64imm_op : FPOperand<f64>;
150
151let OperandType = "OPERAND_VEC_I8IMM" in
152def vec_i8imm_op : Operand<i32>;
153
154let OperandType = "OPERAND_VEC_I16IMM" in
155def vec_i16imm_op : Operand<i32>;
156
157let OperandType = "OPERAND_VEC_I32IMM" in
158def vec_i32imm_op : Operand<i32>;
159
160let OperandType = "OPERAND_VEC_I64IMM" in
161def vec_i64imm_op : Operand<i64>;
162
163let OperandType = "OPERAND_FUNCTION32" in
164def function32_op : Operand<i32>;
165
166let OperandType = "OPERAND_TABLE" in
167def table32_op : Operand<i32>;
168
169let OperandType = "OPERAND_OFFSET32" in
170def offset32_op : Operand<i32>;
171
172let OperandType = "OPERAND_OFFSET64" in
173def offset64_op : Operand<i64>;
174
175let OperandType = "OPERAND_P2ALIGN" in {
176def P2Align : Operand<i32> {
177  let PrintMethod = "printWebAssemblyP2AlignOperand";
178}
179
180let OperandType = "OPERAND_EVENT" in
181def event_op : Operand<i32>;
182
183} // OperandType = "OPERAND_P2ALIGN"
184
185let OperandType = "OPERAND_SIGNATURE" in
186def Signature : Operand<i32> {
187  let PrintMethod = "printWebAssemblySignatureOperand";
188}
189
190let OperandType = "OPERAND_HEAPTYPE" in
191def HeapType : Operand<i32> {
192  let PrintMethod = "printWebAssemblyHeapTypeOperand";
193}
194
195let OperandType = "OPERAND_TYPEINDEX" in
196def TypeIndex : Operand<i32>;
197
198} // OperandNamespace = "WebAssembly"
199
200//===----------------------------------------------------------------------===//
201// WebAssembly Register to Stack instruction mapping
202//===----------------------------------------------------------------------===//
203
204class StackRel;
205def getStackOpcode : InstrMapping {
206  let FilterClass = "StackRel";
207  let RowFields = ["BaseName"];
208  let ColFields = ["StackBased"];
209  let KeyCol = ["false"];
210  let ValueCols = [["true"]];
211}
212
213//===----------------------------------------------------------------------===//
214// WebAssembly 32 to 64-bit instruction mapping
215//===----------------------------------------------------------------------===//
216
217class Wasm64Rel;
218def getWasm64Opcode : InstrMapping {
219  let FilterClass = "Wasm64Rel";
220  let RowFields = ["Wasm32Name"];
221  let ColFields = ["IsWasm64"];
222  let KeyCol = ["false"];
223  let ValueCols = [["true"]];
224}
225
226//===----------------------------------------------------------------------===//
227// WebAssembly Instruction Format Definitions.
228//===----------------------------------------------------------------------===//
229
230include "WebAssemblyInstrFormats.td"
231
232//===----------------------------------------------------------------------===//
233// Additional instructions.
234//===----------------------------------------------------------------------===//
235
236multiclass ARGUMENT<WebAssemblyRegClass reg, ValueType vt> {
237  let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>,
238      Uses = [ARGUMENTS] in
239  defm ARGUMENT_#vt :
240    I<(outs reg:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
241      [(set (vt reg:$res), (WebAssemblyargument timm:$argno))]>;
242}
243defm "": ARGUMENT<I32, i32>;
244defm "": ARGUMENT<I64, i64>;
245defm "": ARGUMENT<F32, f32>;
246defm "": ARGUMENT<F64, f64>;
247defm "": ARGUMENT<EXNREF, exnref>;
248defm "": ARGUMENT<FUNCREF, funcref>;
249defm "": ARGUMENT<EXTERNREF, externref>;
250
251// local.get and local.set are not generated by instruction selection; they
252// are implied by virtual register uses and defs.
253multiclass LOCAL<WebAssemblyRegClass vt> {
254  let hasSideEffects = 0 in {
255  // COPY is not an actual instruction in wasm, but since we allow local.get and
256  // local.set to be implicit during most of codegen, we can have a COPY which
257  // is actually a no-op because all the work is done in the implied local.get
258  // and local.set. COPYs are eliminated (and replaced with
259  // local.get/local.set) in the ExplicitLocals pass.
260  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
261  defm COPY_#vt : I<(outs vt:$res), (ins vt:$src), (outs), (ins), [],
262                    "local.copy\t$res, $src", "local.copy">;
263
264  // TEE is similar to COPY, but writes two copies of its result. Typically
265  // this would be used to stackify one result and write the other result to a
266  // local.
267  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
268  defm TEE_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), (outs), (ins), [],
269                   "local.tee\t$res, $also, $src", "local.tee">;
270
271  // This is the actual local.get instruction in wasm. These are made explicit
272  // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
273  // local, which is a side effect not otherwise modeled in LLVM.
274  let mayLoad = 1, isAsCheapAsAMove = 1 in
275  defm LOCAL_GET_#vt : I<(outs vt:$res), (ins local_op:$local),
276                         (outs), (ins local_op:$local), [],
277                         "local.get\t$res, $local", "local.get\t$local", 0x20>;
278
279  // This is the actual local.set instruction in wasm. These are made explicit
280  // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
281  // local, which is a side effect not otherwise modeled in LLVM.
282  let mayStore = 1, isAsCheapAsAMove = 1 in
283  defm LOCAL_SET_#vt : I<(outs), (ins local_op:$local, vt:$src),
284                         (outs), (ins local_op:$local), [],
285                         "local.set\t$local, $src", "local.set\t$local", 0x21>;
286
287  // This is the actual local.tee instruction in wasm. TEEs are turned into
288  // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
289  // as LOCAL_SET.
290  let mayStore = 1, isAsCheapAsAMove = 1 in
291  defm LOCAL_TEE_#vt : I<(outs vt:$res), (ins local_op:$local, vt:$src),
292                         (outs), (ins local_op:$local), [],
293                         "local.tee\t$res, $local, $src", "local.tee\t$local",
294                         0x22>;
295
296  // Unused values must be dropped in some contexts.
297  defm DROP_#vt : I<(outs), (ins vt:$src), (outs), (ins), [],
298                    "drop\t$src", "drop", 0x1a>;
299
300  let mayLoad = 1 in
301  defm GLOBAL_GET_#vt : I<(outs vt:$res), (ins global_op:$local),
302                          (outs), (ins global_op:$local), [],
303                          "global.get\t$res, $local", "global.get\t$local",
304                          0x23>;
305
306  let mayStore = 1 in
307  defm GLOBAL_SET_#vt : I<(outs), (ins global_op:$local, vt:$src),
308                          (outs), (ins global_op:$local), [],
309                          "global.set\t$local, $src", "global.set\t$local",
310                          0x24>;
311
312} // hasSideEffects = 0
313}
314defm "" : LOCAL<I32>;
315defm "" : LOCAL<I64>;
316defm "" : LOCAL<F32>;
317defm "" : LOCAL<F64>;
318defm "" : LOCAL<V128>, Requires<[HasSIMD128]>;
319defm "" : LOCAL<EXNREF>, Requires<[HasExceptionHandling]>;
320defm "" : LOCAL<FUNCREF>, Requires<[HasReferenceTypes]>;
321defm "" : LOCAL<EXTERNREF>, Requires<[HasReferenceTypes]>;
322
323let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
324defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
325                   (outs), (ins i32imm_op:$imm),
326                   [(set I32:$res, imm:$imm)],
327                   "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
328defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
329                   (outs), (ins i64imm_op:$imm),
330                   [(set I64:$res, imm:$imm)],
331                   "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
332defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
333                   (outs), (ins f32imm_op:$imm),
334                   [(set F32:$res, fpimm:$imm)],
335                   "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
336defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
337                   (outs), (ins f64imm_op:$imm),
338                   [(set F64:$res, fpimm:$imm)],
339                   "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
340} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
341
342def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
343          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
344def : Pat<(i64 (WebAssemblywrapper tglobaladdr:$addr)),
345          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
346
347def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
348          (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
349
350def : Pat<(i32 (WebAssemblywrapperPIC tglobaladdr:$addr)),
351          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
352def : Pat<(i64 (WebAssemblywrapperPIC tglobaladdr:$addr)),
353          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
354
355def : Pat<(i32 (WebAssemblywrapper tglobaltlsaddr:$addr)),
356          (CONST_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>;
357def : Pat<(i64 (WebAssemblywrapper tglobaltlsaddr:$addr)),
358          (CONST_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>;
359
360def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
361          (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>;
362
363def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
364          (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
365def : Pat<(i64 (WebAssemblywrapper texternalsym:$addr)),
366          (CONST_I64 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
367
368def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
369def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
370
371//===----------------------------------------------------------------------===//
372// Additional sets of instructions.
373//===----------------------------------------------------------------------===//
374
375include "WebAssemblyInstrMemory.td"
376include "WebAssemblyInstrCall.td"
377include "WebAssemblyInstrControl.td"
378include "WebAssemblyInstrInteger.td"
379include "WebAssemblyInstrConv.td"
380include "WebAssemblyInstrFloat.td"
381include "WebAssemblyInstrAtomics.td"
382include "WebAssemblyInstrSIMD.td"
383include "WebAssemblyInstrRef.td"
384include "WebAssemblyInstrBulkMemory.td"
385include "WebAssemblyInstrTable.td"
386