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 32 to 64-bit instruction mapping
239//===----------------------------------------------------------------------===//
240
241class Wasm64Rel;
242def getWasm64Opcode : InstrMapping {
243  let FilterClass = "Wasm64Rel";
244  let RowFields = ["Wasm32Name"];
245  let ColFields = ["IsWasm64"];
246  let KeyCol = ["false"];
247  let ValueCols = [["true"]];
248}
249
250//===----------------------------------------------------------------------===//
251// WebAssembly Instruction Format Definitions.
252//===----------------------------------------------------------------------===//
253
254include "WebAssemblyInstrFormats.td"
255
256//===----------------------------------------------------------------------===//
257// Additional instructions.
258//===----------------------------------------------------------------------===//
259
260multiclass ARGUMENT<WebAssemblyRegClass rc, ValueType vt> {
261  let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>,
262      Uses = [ARGUMENTS] in
263  defm ARGUMENT_#vt :
264    I<(outs rc:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
265      [(set (vt rc:$res), (WebAssemblyargument timm:$argno))]>;
266}
267defm "": ARGUMENT<I32, i32>;
268defm "": ARGUMENT<I64, i64>;
269defm "": ARGUMENT<F32, f32>;
270defm "": ARGUMENT<F64, f64>;
271defm "": ARGUMENT<FUNCREF, funcref>;
272defm "": ARGUMENT<EXTERNREF, externref>;
273
274// local.get and local.set are not generated by instruction selection; they
275// are implied by virtual register uses and defs.
276multiclass LOCAL<WebAssemblyRegClass rc, Operand global_op> {
277  let hasSideEffects = 0 in {
278  // COPY is not an actual instruction in wasm, but since we allow local.get and
279  // local.set to be implicit during most of codegen, we can have a COPY which
280  // is actually a no-op because all the work is done in the implied local.get
281  // and local.set. COPYs are eliminated (and replaced with
282  // local.get/local.set) in the ExplicitLocals pass.
283  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
284  defm COPY_#rc : I<(outs rc:$res), (ins rc:$src), (outs), (ins), [],
285                    "local.copy\t$res, $src", "local.copy">;
286
287  // TEE is similar to COPY, but writes two copies of its result. Typically
288  // this would be used to stackify one result and write the other result to a
289  // local.
290  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
291  defm TEE_#rc : I<(outs rc:$res, rc:$also), (ins rc:$src), (outs), (ins), [],
292                   "local.tee\t$res, $also, $src", "local.tee">;
293
294  // This is the actual local.get instruction in wasm. These are made explicit
295  // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
296  // local, which is a side effect not otherwise modeled in LLVM.
297  let mayLoad = 1, isAsCheapAsAMove = 1 in
298  defm LOCAL_GET_#rc : I<(outs rc:$res), (ins local_op:$local),
299                         (outs), (ins local_op:$local), [],
300                         "local.get\t$res, $local", "local.get\t$local", 0x20>;
301
302  // This is the actual local.set instruction in wasm. These are made explicit
303  // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
304  // local, which is a side effect not otherwise modeled in LLVM.
305  let mayStore = 1, isAsCheapAsAMove = 1 in
306  defm LOCAL_SET_#rc : I<(outs), (ins local_op:$local, rc:$src),
307                         (outs), (ins local_op:$local), [],
308                         "local.set\t$local, $src", "local.set\t$local", 0x21>;
309
310  // This is the actual local.tee instruction in wasm. TEEs are turned into
311  // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
312  // as LOCAL_SET.
313  let mayStore = 1, isAsCheapAsAMove = 1 in
314  defm LOCAL_TEE_#rc : I<(outs rc:$res), (ins local_op:$local, rc:$src),
315                         (outs), (ins local_op:$local), [],
316                         "local.tee\t$res, $local, $src", "local.tee\t$local",
317                         0x22>;
318
319  // Unused values must be dropped in some contexts.
320  defm DROP_#rc : I<(outs), (ins rc:$src), (outs), (ins), [],
321                    "drop\t$src", "drop", 0x1a>;
322
323  let mayLoad = 1 in
324  defm GLOBAL_GET_#rc : I<(outs rc:$res), (ins global_op:$addr),
325                          (outs), (ins global_op:$addr), [],
326                           "global.get\t$res, $addr", "global.get\t$addr",
327                          0x23>;
328
329  let mayStore = 1 in
330  defm GLOBAL_SET_#rc : I<(outs), (ins global_op:$addr, rc:$src),
331                          (outs), (ins global_op:$addr), [],
332                          "global.set\t$addr, $src", "global.set\t$addr",
333                          0x24>;
334
335  } // hasSideEffects = 0
336  foreach vt = rc.RegTypes in {
337    def : Pat<(vt (WebAssemblyglobal_get
338                   (WebAssemblywrapper tglobaladdr:$addr))),
339              (!cast<NI>("GLOBAL_GET_" # rc) tglobaladdr:$addr)>;
340    def : Pat<(WebAssemblyglobal_set
341               vt:$src, (WebAssemblywrapper tglobaladdr:$addr)),
342              (!cast<NI>("GLOBAL_SET_" # rc) tglobaladdr:$addr, vt:$src)>;
343    def : Pat<(vt (WebAssemblylocal_get (i32 timm:$local))),
344              (!cast<NI>("LOCAL_GET_" # rc) timm:$local)>;
345    def : Pat<(WebAssemblylocal_set timm:$local, vt:$src),
346              (!cast<NI>("LOCAL_SET_" # rc) timm:$local, vt:$src)>;
347  }
348}
349defm "" : LOCAL<I32, global_op32>;
350defm "" : LOCAL<I64, global_op64>;  // 64-bit only needed for pointers.
351defm "" : LOCAL<F32, global_op32>;
352defm "" : LOCAL<F64, global_op32>;
353defm "" : LOCAL<V128, global_op32>, Requires<[HasSIMD128]>;
354defm "" : LOCAL<FUNCREF, global_op32>, Requires<[HasReferenceTypes]>;
355defm "" : LOCAL<EXTERNREF, global_op32>, Requires<[HasReferenceTypes]>;
356
357let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
358defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
359                   (outs), (ins i32imm_op:$imm),
360                   [(set I32:$res, imm:$imm)],
361                   "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
362defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
363                   (outs), (ins i64imm_op:$imm),
364                   [(set I64:$res, imm:$imm)],
365                   "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
366defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
367                   (outs), (ins f32imm_op:$imm),
368                   [(set F32:$res, fpimm:$imm)],
369                   "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
370defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
371                   (outs), (ins f64imm_op:$imm),
372                   [(set F64:$res, fpimm:$imm)],
373                   "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
374} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
375
376def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
377          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
378def : Pat<(i64 (WebAssemblywrapper tglobaladdr:$addr)),
379          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
380
381def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
382          (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
383def : Pat<(i64 (WebAssemblywrapper tglobaladdr:$addr)),
384          (GLOBAL_GET_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
385
386def : Pat<(i32 (WebAssemblywrapperPIC tglobaladdr:$addr)),
387          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>;
388def : Pat<(i64 (WebAssemblywrapperPIC tglobaladdr:$addr)),
389          (CONST_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>;
390
391def : Pat<(i32 (WebAssemblywrapper tglobaltlsaddr:$addr)),
392          (CONST_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>;
393def : Pat<(i64 (WebAssemblywrapper tglobaltlsaddr:$addr)),
394          (CONST_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>;
395
396def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
397          (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>;
398def : Pat<(i64 (WebAssemblywrapper texternalsym:$addr)),
399          (GLOBAL_GET_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>;
400
401def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
402          (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr32]>;
403def : Pat<(i64 (WebAssemblywrapper texternalsym:$addr)),
404          (CONST_I64 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr64]>;
405
406def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
407def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
408
409//===----------------------------------------------------------------------===//
410// Additional sets of instructions.
411//===----------------------------------------------------------------------===//
412
413include "WebAssemblyInstrMemory.td"
414include "WebAssemblyInstrCall.td"
415include "WebAssemblyInstrControl.td"
416include "WebAssemblyInstrInteger.td"
417include "WebAssemblyInstrConv.td"
418include "WebAssemblyInstrFloat.td"
419include "WebAssemblyInstrAtomics.td"
420include "WebAssemblyInstrSIMD.td"
421include "WebAssemblyInstrRef.td"
422include "WebAssemblyInstrBulkMemory.td"
423include "WebAssemblyInstrTable.td"
424