xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td (revision 53120fbb68952b7d620c2c0e1cf05c5017fc1b27)
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 HasRelaxedSIMD :
30    Predicate<"Subtarget->hasRelaxedSIMD()">,
31    AssemblerPredicate<(all_of FeatureRelaxedSIMD), "relaxed-simd">;
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
69def HasExtendedConst :
70    Predicate<"Subtarget->hasExtendedConst()">,
71    AssemblerPredicate<(all_of FeatureExtendedConst), "extended-const">;
72
73def HasMultiMemory :
74    Predicate<"Subtarget->hasMultiMemory()">,
75    AssemblerPredicate<(all_of FeatureMultiMemory), "multimemory">;
76
77//===----------------------------------------------------------------------===//
78// WebAssembly-specific DAG Node Types.
79//===----------------------------------------------------------------------===//
80
81def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>,
82                                                  SDTCisVT<1, iPTR>]>;
83def SDT_WebAssemblyCallSeqEnd :
84    SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
85def SDT_WebAssemblyBrTable    : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
86def SDT_WebAssemblyArgument   : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
87def SDT_WebAssemblyLocalGet   : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
88def SDT_WebAssemblyLocalSet   : SDTypeProfile<0, 2, [SDTCisVT<0, i32>]>;
89def SDT_WebAssemblyReturn     : SDTypeProfile<0, -1, []>;
90def SDT_WebAssemblyWrapper    : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
91                                                     SDTCisPtrTy<0>]>;
92def SDT_WebAssemblyGlobalGet  : SDTypeProfile<1, 1, [SDTCisPtrTy<1>]>;
93def SDT_WebAssemblyGlobalSet  : SDTypeProfile<0, 2, [SDTCisPtrTy<1>]>;
94
95//===----------------------------------------------------------------------===//
96// WebAssembly-specific DAG Nodes.
97//===----------------------------------------------------------------------===//
98
99def WebAssemblycallseq_start :
100    SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
101           [SDNPHasChain, SDNPOutGlue]>;
102def WebAssemblycallseq_end :
103    SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
104           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
105def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
106                                 SDT_WebAssemblyBrTable,
107                                 [SDNPHasChain, SDNPVariadic]>;
108def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
109                                 SDT_WebAssemblyArgument>;
110def WebAssemblyreturn   : SDNode<"WebAssemblyISD::RETURN",
111                                 SDT_WebAssemblyReturn,
112                                 [SDNPHasChain, SDNPVariadic]>;
113def WebAssemblyWrapper  : SDNode<"WebAssemblyISD::Wrapper",
114                                 SDT_WebAssemblyWrapper>;
115def WebAssemblyWrapperREL  : SDNode<"WebAssemblyISD::WrapperREL",
116                                     SDT_WebAssemblyWrapper>;
117def WebAssemblyglobal_get :
118    SDNode<"WebAssemblyISD::GLOBAL_GET", SDT_WebAssemblyGlobalGet,
119           [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
120def WebAssemblyglobal_set :
121    SDNode<"WebAssemblyISD::GLOBAL_SET", SDT_WebAssemblyGlobalSet,
122           [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
123def WebAssemblylocal_get :
124    SDNode<"WebAssemblyISD::LOCAL_GET", SDT_WebAssemblyLocalGet,
125           [SDNPHasChain, SDNPMayLoad]>;
126def WebAssemblylocal_set :
127    SDNode<"WebAssemblyISD::LOCAL_SET", SDT_WebAssemblyLocalSet,
128           [SDNPHasChain, SDNPMayStore]>;
129
130//===----------------------------------------------------------------------===//
131// WebAssembly-specific Operands.
132//===----------------------------------------------------------------------===//
133
134// Default Operand has AsmOperandClass "Imm" which is for integers (and
135// symbols), so specialize one for floats:
136class FPImmAsmOperand<ValueType ty> : AsmOperandClass {
137  let Name = "FPImm" # ty;
138  let PredicateMethod = "isFPImm";
139}
140
141class FPOperand<ValueType ty> : Operand<ty> {
142  AsmOperandClass ParserMatchClass = FPImmAsmOperand<ty>;
143}
144
145let OperandNamespace = "WebAssembly" in {
146
147let OperandType = "OPERAND_BASIC_BLOCK" in
148def bb_op : Operand<OtherVT>;
149
150let OperandType = "OPERAND_LOCAL" in
151def local_op : Operand<i32>;
152
153let OperandType = "OPERAND_GLOBAL" in {
154  // The operand to global instructions is always a 32-bit index.
155  def global_op32 : Operand<i32>;
156  // In PIC mode however, we temporarily represent this index as an external
157  // symbol, which to LLVM is a pointer, so in wasm64 mode it is easiest to
158  // pretend we use a 64-bit index for it.
159  def global_op64 : Operand<i64>;
160}
161
162let OperandType = "OPERAND_I32IMM" in
163def i32imm_op : Operand<i32>;
164
165let OperandType = "OPERAND_I64IMM" in
166def i64imm_op : Operand<i64>;
167
168let OperandType = "OPERAND_F32IMM" in
169def f32imm_op : FPOperand<f32>;
170
171let OperandType = "OPERAND_F64IMM" in
172def f64imm_op : FPOperand<f64>;
173
174let OperandType = "OPERAND_VEC_I8IMM" in
175def vec_i8imm_op : Operand<i32>;
176
177let OperandType = "OPERAND_VEC_I16IMM" in
178def vec_i16imm_op : Operand<i32>;
179
180let OperandType = "OPERAND_VEC_I32IMM" in
181def vec_i32imm_op : Operand<i32>;
182
183let OperandType = "OPERAND_VEC_I64IMM" in
184def vec_i64imm_op : Operand<i64>;
185
186let OperandType = "OPERAND_FUNCTION32" in
187def function32_op : Operand<i32>;
188
189let OperandType = "OPERAND_TABLE" in
190def table32_op : Operand<i32>;
191
192let OperandType = "OPERAND_OFFSET32" in
193def offset32_op : Operand<i32>;
194
195let OperandType = "OPERAND_OFFSET64" in
196def offset64_op : Operand<i64>;
197
198let OperandType = "OPERAND_P2ALIGN" in {
199def P2Align : Operand<i32> {
200  let PrintMethod = "printWebAssemblyP2AlignOperand";
201}
202
203let OperandType = "OPERAND_TAG" in
204def tag_op : Operand<i32>;
205
206} // OperandType = "OPERAND_P2ALIGN"
207
208let OperandType = "OPERAND_SIGNATURE" in
209def Signature : Operand<i32> {
210  let PrintMethod = "printWebAssemblySignatureOperand";
211}
212
213let OperandType = "OPERAND_TYPEINDEX" in
214def TypeIndex : Operand<i32>;
215
216} // OperandNamespace = "WebAssembly"
217
218// TODO: Find more places to use this.
219def bool_node : PatLeaf<(i32 I32:$cond), [{
220  return CurDAG->computeKnownBits(SDValue(N, 0)).countMinLeadingZeros() == 31;
221}]>;
222
223//===----------------------------------------------------------------------===//
224// WebAssembly Register to Stack instruction mapping
225//===----------------------------------------------------------------------===//
226
227class StackRel;
228def getStackOpcode : InstrMapping {
229  let FilterClass = "StackRel";
230  let RowFields = ["BaseName"];
231  let ColFields = ["StackBased"];
232  let KeyCol = ["0"];
233  let ValueCols = [["1"]];
234}
235
236//===----------------------------------------------------------------------===//
237// WebAssembly Stack to Register instruction mapping
238//===----------------------------------------------------------------------===//
239
240class RegisterRel;
241def getRegisterOpcode : InstrMapping {
242  let FilterClass = "RegisterRel";
243  let RowFields = ["BaseName"];
244  let ColFields = ["StackBased"];
245  let KeyCol = ["1"];
246  let ValueCols = [["0"]];
247}
248
249//===----------------------------------------------------------------------===//
250// WebAssembly 32 to 64-bit instruction mapping
251//===----------------------------------------------------------------------===//
252
253class Wasm64Rel;
254def getWasm64Opcode : InstrMapping {
255  let FilterClass = "Wasm64Rel";
256  let RowFields = ["Wasm32Name"];
257  let ColFields = ["IsWasm64"];
258  let KeyCol = ["0"];
259  let ValueCols = [["1"]];
260}
261
262//===----------------------------------------------------------------------===//
263// WebAssembly Instruction Format Definitions.
264//===----------------------------------------------------------------------===//
265
266include "WebAssemblyInstrFormats.td"
267
268//===----------------------------------------------------------------------===//
269// Additional instructions.
270//===----------------------------------------------------------------------===//
271
272multiclass ARGUMENT<WebAssemblyRegClass rc, ValueType vt> {
273  let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>,
274      Uses = [ARGUMENTS] in
275  defm ARGUMENT_#vt :
276    I<(outs rc:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
277      [(set (vt rc:$res), (WebAssemblyargument timm:$argno))]>;
278}
279defm "": ARGUMENT<I32, i32>;
280defm "": ARGUMENT<I64, i64>;
281defm "": ARGUMENT<F32, f32>;
282defm "": ARGUMENT<F64, f64>;
283defm "": ARGUMENT<FUNCREF, funcref>;
284defm "": ARGUMENT<EXTERNREF, externref>;
285
286// local.get and local.set are not generated by instruction selection; they
287// are implied by virtual register uses and defs.
288multiclass LOCAL<WebAssemblyRegClass rc, Operand global_op> {
289  let hasSideEffects = 0 in {
290  // COPY is not an actual instruction in wasm, but since we allow local.get and
291  // local.set to be implicit during most of codegen, we can have a COPY which
292  // is actually a no-op because all the work is done in the implied local.get
293  // and local.set. COPYs are eliminated (and replaced with
294  // local.get/local.set) in the ExplicitLocals pass.
295  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
296  defm COPY_#rc : I<(outs rc:$res), (ins rc:$src), (outs), (ins), [],
297                    "local.copy\t$res, $src", "local.copy">;
298
299  // TEE is similar to COPY, but writes two copies of its result. Typically
300  // this would be used to stackify one result and write the other result to a
301  // local.
302  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
303  defm TEE_#rc : I<(outs rc:$res, rc:$also), (ins rc:$src), (outs), (ins), [],
304                   "local.tee\t$res, $also, $src", "local.tee">;
305
306  // This is the actual local.get instruction in wasm. These are made explicit
307  // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
308  // local, which is a side effect not otherwise modeled in LLVM.
309  let mayLoad = 1, isAsCheapAsAMove = 1 in
310  defm LOCAL_GET_#rc : I<(outs rc:$res), (ins local_op:$local),
311                         (outs), (ins local_op:$local), [],
312                         "local.get\t$res, $local", "local.get\t$local", 0x20>;
313
314  // This is the actual local.set instruction in wasm. These are made explicit
315  // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
316  // local, which is a side effect not otherwise modeled in LLVM.
317  let mayStore = 1, isAsCheapAsAMove = 1 in
318  defm LOCAL_SET_#rc : I<(outs), (ins local_op:$local, rc:$src),
319                         (outs), (ins local_op:$local), [],
320                         "local.set\t$local, $src", "local.set\t$local", 0x21>;
321
322  // This is the actual local.tee instruction in wasm. TEEs are turned into
323  // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
324  // as LOCAL_SET.
325  let mayStore = 1, isAsCheapAsAMove = 1 in
326  defm LOCAL_TEE_#rc : I<(outs rc:$res), (ins local_op:$local, rc:$src),
327                         (outs), (ins local_op:$local), [],
328                         "local.tee\t$res, $local, $src", "local.tee\t$local",
329                         0x22>;
330
331  // Unused values must be dropped in some contexts.
332  defm DROP_#rc : I<(outs), (ins rc:$src), (outs), (ins), [],
333                    "drop\t$src", "drop", 0x1a>;
334
335  let mayLoad = 1 in
336  defm GLOBAL_GET_#rc : I<(outs rc:$res), (ins global_op:$addr),
337                          (outs), (ins global_op:$addr), [],
338                           "global.get\t$res, $addr", "global.get\t$addr",
339                          0x23>;
340
341  let mayStore = 1 in
342  defm GLOBAL_SET_#rc : I<(outs), (ins global_op:$addr, rc:$src),
343                          (outs), (ins global_op:$addr), [],
344                          "global.set\t$addr, $src", "global.set\t$addr",
345                          0x24>;
346
347  } // hasSideEffects = 0
348  foreach vt = rc.RegTypes in {
349    def : Pat<(vt (WebAssemblyglobal_get
350                   (WebAssemblyWrapper tglobaladdr:$addr))),
351              (!cast<NI>("GLOBAL_GET_" # rc) tglobaladdr:$addr)>;
352    def : Pat<(WebAssemblyglobal_set
353               vt:$src, (WebAssemblyWrapper tglobaladdr:$addr)),
354              (!cast<NI>("GLOBAL_SET_" # rc) tglobaladdr:$addr, vt:$src)>;
355    def : Pat<(vt (WebAssemblylocal_get (i32 timm:$local))),
356              (!cast<NI>("LOCAL_GET_" # rc) timm:$local)>;
357    def : Pat<(WebAssemblylocal_set timm:$local, vt:$src),
358              (!cast<NI>("LOCAL_SET_" # rc) timm:$local, vt:$src)>;
359  }
360}
361defm "" : LOCAL<I32, global_op32>;
362defm "" : LOCAL<I64, global_op64>;  // 64-bit only needed for pointers.
363defm "" : LOCAL<F32, global_op32>;
364defm "" : LOCAL<F64, global_op32>;
365defm "" : LOCAL<V128, global_op32>, Requires<[HasSIMD128]>;
366defm "" : LOCAL<FUNCREF, global_op32>, Requires<[HasReferenceTypes]>;
367defm "" : LOCAL<EXTERNREF, global_op32>, Requires<[HasReferenceTypes]>;
368
369let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
370defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
371                   (outs), (ins i32imm_op:$imm),
372                   [(set I32:$res, imm:$imm)],
373                   "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
374defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
375                   (outs), (ins i64imm_op:$imm),
376                   [(set I64:$res, imm:$imm)],
377                   "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
378defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
379                   (outs), (ins f32imm_op:$imm),
380                   [(set F32:$res, fpimm:$imm)],
381                   "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
382defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
383                   (outs), (ins f64imm_op:$imm),
384                   [(set F64:$res, fpimm:$imm)],
385                   "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
386} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
387
388def : Pat<(i32 (WebAssemblyWrapper tglobaladdr:$addr)),
389          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
390def : Pat<(i64 (WebAssemblyWrapper tglobaladdr:$addr)),
391          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
392
393def : Pat<(i32 (WebAssemblyWrapper tglobaladdr:$addr)),
394          (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
395def : Pat<(i64 (WebAssemblyWrapper tglobaladdr:$addr)),
396          (GLOBAL_GET_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
397
398def : Pat<(i32 (WebAssemblyWrapperREL tglobaladdr:$addr)),
399          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
400def : Pat<(i64 (WebAssemblyWrapperREL tglobaladdr:$addr)),
401          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
402
403def : Pat<(i32 (WebAssemblyWrapperREL tglobaltlsaddr:$addr)),
404          (CONST_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>;
405def : Pat<(i64 (WebAssemblyWrapperREL tglobaltlsaddr:$addr)),
406          (CONST_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>;
407
408def : Pat<(i32 (WebAssemblyWrapper tglobaltlsaddr:$addr)),
409          (GLOBAL_GET_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>;
410def : Pat<(i64 (WebAssemblyWrapper tglobaltlsaddr:$addr)),
411          (GLOBAL_GET_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>;
412
413def : Pat<(i32 (WebAssemblyWrapper texternalsym:$addr)),
414          (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>;
415def : Pat<(i64 (WebAssemblyWrapper texternalsym:$addr)),
416          (GLOBAL_GET_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>;
417
418def : Pat<(i32 (WebAssemblyWrapper texternalsym:$addr)),
419          (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
420def : Pat<(i64 (WebAssemblyWrapper texternalsym:$addr)),
421          (CONST_I64 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
422
423def : Pat<(i32 (WebAssemblyWrapperREL texternalsym:$addr)),
424          (CONST_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>;
425def : Pat<(i64 (WebAssemblyWrapperREL texternalsym:$addr)),
426          (CONST_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>;
427
428//===----------------------------------------------------------------------===//
429// Additional sets of instructions.
430//===----------------------------------------------------------------------===//
431
432include "WebAssemblyInstrMemory.td"
433include "WebAssemblyInstrCall.td"
434include "WebAssemblyInstrControl.td"
435include "WebAssemblyInstrInteger.td"
436include "WebAssemblyInstrConv.td"
437include "WebAssemblyInstrFloat.td"
438include "WebAssemblyInstrAtomics.td"
439include "WebAssemblyInstrSIMD.td"
440include "WebAssemblyInstrRef.td"
441include "WebAssemblyInstrBulkMemory.td"
442include "WebAssemblyInstrTable.td"
443