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