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_WebAssemblyReturn     : SDTypeProfile<0, -1, []>;
76def SDT_WebAssemblyWrapper    : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
77                                                     SDTCisPtrTy<0>]>;
78def SDT_WebAssemblyWrapperPIC : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
79                                                     SDTCisPtrTy<0>]>;
80def SDT_WebAssemblyThrow      : SDTypeProfile<0, -1, []>;
81def SDT_WebAssemblyCatch      : SDTypeProfile<1, 1, [SDTCisPtrTy<0>]>;
82def SDT_WebAssemblyGlobalGet  : SDTypeProfile<1, 1, [SDTCisPtrTy<1>]>;
83def SDT_WebAssemblyGlobalSet  : SDTypeProfile<0, 2, [SDTCisPtrTy<1>]>;
84
85//===----------------------------------------------------------------------===//
86// WebAssembly-specific DAG Nodes.
87//===----------------------------------------------------------------------===//
88
89def WebAssemblycallseq_start :
90    SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
91           [SDNPHasChain, SDNPOutGlue]>;
92def WebAssemblycallseq_end :
93    SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
94           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
95def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
96                                 SDT_WebAssemblyBrTable,
97                                 [SDNPHasChain, SDNPVariadic]>;
98def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
99                                 SDT_WebAssemblyArgument>;
100def WebAssemblyreturn   : SDNode<"WebAssemblyISD::RETURN",
101                                 SDT_WebAssemblyReturn,
102                                 [SDNPHasChain, SDNPVariadic]>;
103def WebAssemblywrapper  : SDNode<"WebAssemblyISD::Wrapper",
104                                 SDT_WebAssemblyWrapper>;
105def WebAssemblywrapperPIC  : SDNode<"WebAssemblyISD::WrapperPIC",
106                                     SDT_WebAssemblyWrapperPIC>;
107def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
108                              [SDNPHasChain, SDNPVariadic]>;
109def WebAssemblycatch : SDNode<"WebAssemblyISD::CATCH", SDT_WebAssemblyCatch,
110                              [SDNPHasChain, SDNPSideEffect]>;
111def WebAssemblyglobal_get :
112    SDNode<"WebAssemblyISD::GLOBAL_GET", SDT_WebAssemblyGlobalGet,
113           [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
114def WebAssemblyglobal_set :
115    SDNode<"WebAssemblyISD::GLOBAL_SET", SDT_WebAssemblyGlobalSet,
116           [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
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_EVENT" in
192def event_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 32 to 64-bit instruction mapping
231//===----------------------------------------------------------------------===//
232
233class Wasm64Rel;
234def getWasm64Opcode : InstrMapping {
235  let FilterClass = "Wasm64Rel";
236  let RowFields = ["Wasm32Name"];
237  let ColFields = ["IsWasm64"];
238  let KeyCol = ["false"];
239  let ValueCols = [["true"]];
240}
241
242//===----------------------------------------------------------------------===//
243// WebAssembly Instruction Format Definitions.
244//===----------------------------------------------------------------------===//
245
246include "WebAssemblyInstrFormats.td"
247
248//===----------------------------------------------------------------------===//
249// Additional instructions.
250//===----------------------------------------------------------------------===//
251
252multiclass ARGUMENT<WebAssemblyRegClass rc, ValueType vt> {
253  let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>,
254      Uses = [ARGUMENTS] in
255  defm ARGUMENT_#vt :
256    I<(outs rc:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
257      [(set (vt rc:$res), (WebAssemblyargument timm:$argno))]>;
258}
259defm "": ARGUMENT<I32, i32>;
260defm "": ARGUMENT<I64, i64>;
261defm "": ARGUMENT<F32, f32>;
262defm "": ARGUMENT<F64, f64>;
263defm "": ARGUMENT<FUNCREF, funcref>;
264defm "": ARGUMENT<EXTERNREF, externref>;
265
266// local.get and local.set are not generated by instruction selection; they
267// are implied by virtual register uses and defs.
268multiclass LOCAL<WebAssemblyRegClass rc, Operand global_op> {
269  let hasSideEffects = 0 in {
270  // COPY is not an actual instruction in wasm, but since we allow local.get and
271  // local.set to be implicit during most of codegen, we can have a COPY which
272  // is actually a no-op because all the work is done in the implied local.get
273  // and local.set. COPYs are eliminated (and replaced with
274  // local.get/local.set) in the ExplicitLocals pass.
275  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
276  defm COPY_#rc : I<(outs rc:$res), (ins rc:$src), (outs), (ins), [],
277                    "local.copy\t$res, $src", "local.copy">;
278
279  // TEE is similar to COPY, but writes two copies of its result. Typically
280  // this would be used to stackify one result and write the other result to a
281  // local.
282  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
283  defm TEE_#rc : I<(outs rc:$res, rc:$also), (ins rc:$src), (outs), (ins), [],
284                   "local.tee\t$res, $also, $src", "local.tee">;
285
286  // This is the actual local.get instruction in wasm. These are made explicit
287  // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
288  // local, which is a side effect not otherwise modeled in LLVM.
289  let mayLoad = 1, isAsCheapAsAMove = 1 in
290  defm LOCAL_GET_#rc : I<(outs rc:$res), (ins local_op:$local),
291                         (outs), (ins local_op:$local), [],
292                         "local.get\t$res, $local", "local.get\t$local", 0x20>;
293
294  // This is the actual local.set instruction in wasm. These are made explicit
295  // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
296  // local, which is a side effect not otherwise modeled in LLVM.
297  let mayStore = 1, isAsCheapAsAMove = 1 in
298  defm LOCAL_SET_#rc : I<(outs), (ins local_op:$local, rc:$src),
299                         (outs), (ins local_op:$local), [],
300                         "local.set\t$local, $src", "local.set\t$local", 0x21>;
301
302  // This is the actual local.tee instruction in wasm. TEEs are turned into
303  // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
304  // as LOCAL_SET.
305  let mayStore = 1, isAsCheapAsAMove = 1 in
306  defm LOCAL_TEE_#rc : I<(outs rc:$res), (ins local_op:$local, rc:$src),
307                         (outs), (ins local_op:$local), [],
308                         "local.tee\t$res, $local, $src", "local.tee\t$local",
309                         0x22>;
310
311  // Unused values must be dropped in some contexts.
312  defm DROP_#rc : I<(outs), (ins rc:$src), (outs), (ins), [],
313                    "drop\t$src", "drop", 0x1a>;
314
315  let mayLoad = 1 in
316  defm GLOBAL_GET_#rc : I<(outs rc:$res), (ins global_op:$addr),
317                          (outs), (ins global_op:$addr), [],
318                           "global.get\t$res, $addr", "global.get\t$addr",
319                          0x23>;
320
321  let mayStore = 1 in
322  defm GLOBAL_SET_#rc : I<(outs), (ins global_op:$addr, rc:$src),
323                          (outs), (ins global_op:$addr), [],
324                          "global.set\t$addr, $src", "global.set\t$addr",
325                          0x24>;
326
327  } // hasSideEffects = 0
328  foreach vt = rc.RegTypes in {
329    def : Pat<(vt (WebAssemblyglobal_get
330                   (WebAssemblywrapper tglobaladdr:$addr))),
331              (!cast<NI>("GLOBAL_GET_" # rc) tglobaladdr:$addr)>;
332    def : Pat<(WebAssemblyglobal_set
333               vt:$src, (WebAssemblywrapper tglobaladdr:$addr)),
334              (!cast<NI>("GLOBAL_SET_" # rc) tglobaladdr:$addr, vt:$src)>;
335  }
336}
337defm "" : LOCAL<I32, global_op32>;
338defm "" : LOCAL<I64, global_op64>;  // 64-bit only needed for pointers.
339defm "" : LOCAL<F32, global_op32>;
340defm "" : LOCAL<F64, global_op32>;
341defm "" : LOCAL<V128, global_op32>, Requires<[HasSIMD128]>;
342defm "" : LOCAL<FUNCREF, global_op32>, Requires<[HasReferenceTypes]>;
343defm "" : LOCAL<EXTERNREF, global_op32>, Requires<[HasReferenceTypes]>;
344
345let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
346defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
347                   (outs), (ins i32imm_op:$imm),
348                   [(set I32:$res, imm:$imm)],
349                   "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
350defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
351                   (outs), (ins i64imm_op:$imm),
352                   [(set I64:$res, imm:$imm)],
353                   "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
354defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
355                   (outs), (ins f32imm_op:$imm),
356                   [(set F32:$res, fpimm:$imm)],
357                   "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
358defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
359                   (outs), (ins f64imm_op:$imm),
360                   [(set F64:$res, fpimm:$imm)],
361                   "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
362} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
363
364def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
365          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
366def : Pat<(i64 (WebAssemblywrapper tglobaladdr:$addr)),
367          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
368
369def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
370          (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
371def : Pat<(i64 (WebAssemblywrapper tglobaladdr:$addr)),
372          (GLOBAL_GET_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
373
374def : Pat<(i32 (WebAssemblywrapperPIC tglobaladdr:$addr)),
375          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
376def : Pat<(i64 (WebAssemblywrapperPIC tglobaladdr:$addr)),
377          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
378
379def : Pat<(i32 (WebAssemblywrapper tglobaltlsaddr:$addr)),
380          (CONST_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>;
381def : Pat<(i64 (WebAssemblywrapper tglobaltlsaddr:$addr)),
382          (CONST_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>;
383
384def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
385          (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>;
386def : Pat<(i64 (WebAssemblywrapper texternalsym:$addr)),
387          (GLOBAL_GET_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>;
388
389def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
390          (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
391def : Pat<(i64 (WebAssemblywrapper texternalsym:$addr)),
392          (CONST_I64 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
393
394def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
395def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
396
397//===----------------------------------------------------------------------===//
398// Additional sets of instructions.
399//===----------------------------------------------------------------------===//
400
401include "WebAssemblyInstrMemory.td"
402include "WebAssemblyInstrCall.td"
403include "WebAssemblyInstrControl.td"
404include "WebAssemblyInstrInteger.td"
405include "WebAssemblyInstrConv.td"
406include "WebAssemblyInstrFloat.td"
407include "WebAssemblyInstrAtomics.td"
408include "WebAssemblyInstrSIMD.td"
409include "WebAssemblyInstrRef.td"
410include "WebAssemblyInstrBulkMemory.td"
411include "WebAssemblyInstrTable.td"
412