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