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