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<"FeatureSIMD128", "simd128">;
28
29def HasUnimplementedSIMD128 :
30    Predicate<"Subtarget->hasUnimplementedSIMD128()">,
31    AssemblerPredicate<"FeatureUnimplementedSIMD128", "unimplemented-simd128">;
32
33def HasAtomics :
34    Predicate<"Subtarget->hasAtomics()">,
35    AssemblerPredicate<"FeatureAtomics", "atomics">;
36
37def HasNontrappingFPToInt :
38    Predicate<"Subtarget->hasNontrappingFPToInt()">,
39    AssemblerPredicate<"FeatureNontrappingFPToInt", "nontrapping-fptoint">;
40
41def NotHasNontrappingFPToInt :
42    Predicate<"!Subtarget->hasNontrappingFPToInt()">,
43    AssemblerPredicate<"!FeatureNontrappingFPToInt", "nontrapping-fptoint">;
44
45def HasSignExt :
46    Predicate<"Subtarget->hasSignExt()">,
47    AssemblerPredicate<"FeatureSignExt", "sign-ext">;
48
49def NotHasSignExt :
50    Predicate<"!Subtarget->hasSignExt()">,
51    AssemblerPredicate<"!FeatureSignExt", "sign-ext">;
52
53def HasExceptionHandling :
54    Predicate<"Subtarget->hasExceptionHandling()">,
55    AssemblerPredicate<"FeatureExceptionHandling", "exception-handling">;
56
57def NotHasExceptionHandling :
58    Predicate<"!Subtarget->hasExceptionHandling()">,
59    AssemblerPredicate<"!FeatureExceptionHandling", "exception-handling">;
60
61def HasBulkMemory :
62    Predicate<"Subtarget->hasBulkMemory()">,
63    AssemblerPredicate<"FeatureBulkMemory", "bulk-memory">;
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_WebAssemblyCall0      : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
74def SDT_WebAssemblyCall1      : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
75def SDT_WebAssemblyBrTable    : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
76def SDT_WebAssemblyArgument   : SDTypeProfile<1, 1, [SDTCisVT<1, 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, [SDTCisPtrTy<0>]>;
83
84//===----------------------------------------------------------------------===//
85// WebAssembly-specific DAG Nodes.
86//===----------------------------------------------------------------------===//
87
88def WebAssemblycallseq_start :
89    SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
90           [SDNPHasChain, SDNPOutGlue]>;
91def WebAssemblycallseq_end :
92    SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
93           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
94def WebAssemblycall0 : SDNode<"WebAssemblyISD::CALL0",
95                              SDT_WebAssemblyCall0,
96                              [SDNPHasChain, SDNPVariadic]>;
97def WebAssemblycall1 : SDNode<"WebAssemblyISD::CALL1",
98                              SDT_WebAssemblyCall1,
99                              [SDNPHasChain, SDNPVariadic]>;
100def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
101                                 SDT_WebAssemblyBrTable,
102                                 [SDNPHasChain, SDNPVariadic]>;
103def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
104                                 SDT_WebAssemblyArgument>;
105def WebAssemblyreturn   : SDNode<"WebAssemblyISD::RETURN",
106                                 SDT_WebAssemblyReturn, [SDNPHasChain]>;
107def WebAssemblywrapper  : SDNode<"WebAssemblyISD::Wrapper",
108                                 SDT_WebAssemblyWrapper>;
109def WebAssemblywrapperPIC  : SDNode<"WebAssemblyISD::WrapperPIC",
110                                     SDT_WebAssemblyWrapperPIC>;
111def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
112                              [SDNPHasChain, SDNPVariadic]>;
113
114//===----------------------------------------------------------------------===//
115// WebAssembly-specific Operands.
116//===----------------------------------------------------------------------===//
117
118let OperandNamespace = "WebAssembly" in {
119
120let OperandType = "OPERAND_BASIC_BLOCK" in
121def bb_op : Operand<OtherVT>;
122
123let OperandType = "OPERAND_LOCAL" in
124def local_op : Operand<i32>;
125
126let OperandType = "OPERAND_GLOBAL" in
127def global_op : Operand<i32>;
128
129let OperandType = "OPERAND_I32IMM" in
130def i32imm_op : Operand<i32>;
131
132let OperandType = "OPERAND_I64IMM" in
133def i64imm_op : Operand<i64>;
134
135let OperandType = "OPERAND_F32IMM" in
136def f32imm_op : Operand<f32>;
137
138let OperandType = "OPERAND_F64IMM" in
139def f64imm_op : Operand<f64>;
140
141let OperandType = "OPERAND_VEC_I8IMM" in
142def vec_i8imm_op : Operand<i32>;
143
144let OperandType = "OPERAND_VEC_I16IMM" in
145def vec_i16imm_op : Operand<i32>;
146
147let OperandType = "OPERAND_VEC_I32IMM" in
148def vec_i32imm_op : Operand<i32>;
149
150let OperandType = "OPERAND_VEC_I64IMM" in
151def vec_i64imm_op : Operand<i64>;
152
153let OperandType = "OPERAND_FUNCTION32" in
154def function32_op : Operand<i32>;
155
156let OperandType = "OPERAND_OFFSET32" in
157def offset32_op : Operand<i32>;
158
159let OperandType = "OPERAND_P2ALIGN" in {
160def P2Align : Operand<i32> {
161  let PrintMethod = "printWebAssemblyP2AlignOperand";
162}
163
164let OperandType = "OPERAND_EVENT" in
165def event_op : Operand<i32>;
166
167} // OperandType = "OPERAND_P2ALIGN"
168
169let OperandType = "OPERAND_SIGNATURE" in
170def Signature : Operand<i32> {
171  let PrintMethod = "printWebAssemblySignatureOperand";
172}
173
174let OperandType = "OPERAND_TYPEINDEX" in
175def TypeIndex : Operand<i32>;
176
177} // OperandNamespace = "WebAssembly"
178
179//===----------------------------------------------------------------------===//
180// WebAssembly Register to Stack instruction mapping
181//===----------------------------------------------------------------------===//
182
183class StackRel;
184def getStackOpcode : InstrMapping {
185  let FilterClass = "StackRel";
186  let RowFields = ["BaseName"];
187  let ColFields = ["StackBased"];
188  let KeyCol = ["false"];
189  let ValueCols = [["true"]];
190}
191
192//===----------------------------------------------------------------------===//
193// WebAssembly Instruction Format Definitions.
194//===----------------------------------------------------------------------===//
195
196include "WebAssemblyInstrFormats.td"
197
198//===----------------------------------------------------------------------===//
199// Additional instructions.
200//===----------------------------------------------------------------------===//
201
202multiclass ARGUMENT<WebAssemblyRegClass reg, ValueType vt> {
203  let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>,
204      Uses = [ARGUMENTS] in
205  defm ARGUMENT_#vt :
206    I<(outs reg:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno),
207      [(set (vt reg:$res), (WebAssemblyargument timm:$argno))]>;
208}
209defm "": ARGUMENT<I32, i32>;
210defm "": ARGUMENT<I64, i64>;
211defm "": ARGUMENT<F32, f32>;
212defm "": ARGUMENT<F64, f64>;
213defm "": ARGUMENT<EXCEPT_REF, ExceptRef>;
214
215// local.get and local.set are not generated by instruction selection; they
216// are implied by virtual register uses and defs.
217multiclass LOCAL<WebAssemblyRegClass vt> {
218  let hasSideEffects = 0 in {
219  // COPY is not an actual instruction in wasm, but since we allow local.get and
220  // local.set to be implicit during most of codegen, we can have a COPY which
221  // is actually a no-op because all the work is done in the implied local.get
222  // and local.set. COPYs are eliminated (and replaced with
223  // local.get/local.set) in the ExplicitLocals pass.
224  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
225  defm COPY_#vt : I<(outs vt:$res), (ins vt:$src), (outs), (ins), [],
226                    "local.copy\t$res, $src", "local.copy">;
227
228  // TEE is similar to COPY, but writes two copies of its result. Typically
229  // this would be used to stackify one result and write the other result to a
230  // local.
231  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
232  defm TEE_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), (outs), (ins), [],
233                   "local.tee\t$res, $also, $src", "local.tee">;
234
235  // This is the actual local.get instruction in wasm. These are made explicit
236  // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
237  // local, which is a side effect not otherwise modeled in LLVM.
238  let mayLoad = 1, isAsCheapAsAMove = 1 in
239  defm LOCAL_GET_#vt : I<(outs vt:$res), (ins local_op:$local),
240                         (outs), (ins local_op:$local), [],
241                         "local.get\t$res, $local", "local.get\t$local", 0x20>;
242
243  // This is the actual local.set instruction in wasm. These are made explicit
244  // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
245  // local, which is a side effect not otherwise modeled in LLVM.
246  let mayStore = 1, isAsCheapAsAMove = 1 in
247  defm LOCAL_SET_#vt : I<(outs), (ins local_op:$local, vt:$src),
248                         (outs), (ins local_op:$local), [],
249                         "local.set\t$local, $src", "local.set\t$local", 0x21>;
250
251  // This is the actual local.tee instruction in wasm. TEEs are turned into
252  // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason
253  // as LOCAL_SET.
254  let mayStore = 1, isAsCheapAsAMove = 1 in
255  defm LOCAL_TEE_#vt : I<(outs vt:$res), (ins local_op:$local, vt:$src),
256                         (outs), (ins local_op:$local), [],
257                         "local.tee\t$res, $local, $src", "local.tee\t$local",
258                         0x22>;
259
260  // Unused values must be dropped in some contexts.
261  defm DROP_#vt : I<(outs), (ins vt:$src), (outs), (ins), [],
262                    "drop\t$src", "drop", 0x1a>;
263
264  let mayLoad = 1 in
265  defm GLOBAL_GET_#vt : I<(outs vt:$res), (ins global_op:$local),
266                          (outs), (ins global_op:$local), [],
267                          "global.get\t$res, $local", "global.get\t$local",
268                          0x23>;
269
270  let mayStore = 1 in
271  defm GLOBAL_SET_#vt : I<(outs), (ins global_op:$local, vt:$src),
272                          (outs), (ins global_op:$local), [],
273                          "global.set\t$local, $src", "global.set\t$local",
274                          0x24>;
275
276} // hasSideEffects = 0
277}
278defm "" : LOCAL<I32>;
279defm "" : LOCAL<I64>;
280defm "" : LOCAL<F32>;
281defm "" : LOCAL<F64>;
282defm "" : LOCAL<V128>, Requires<[HasSIMD128]>;
283defm "" : LOCAL<EXCEPT_REF>, Requires<[HasExceptionHandling]>;
284
285let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
286defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
287                   (outs), (ins i32imm_op:$imm),
288                   [(set I32:$res, imm:$imm)],
289                   "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>;
290defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
291                   (outs), (ins i64imm_op:$imm),
292                   [(set I64:$res, imm:$imm)],
293                   "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>;
294defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
295                   (outs), (ins f32imm_op:$imm),
296                   [(set F32:$res, fpimm:$imm)],
297                   "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>;
298defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
299                   (outs), (ins f64imm_op:$imm),
300                   [(set F64:$res, fpimm:$imm)],
301                   "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>;
302} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
303
304def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
305          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC]>;
306
307def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
308          (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC]>;
309
310def : Pat<(i32 (WebAssemblywrapperPIC tglobaladdr:$addr)),
311          (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC]>;
312
313def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
314          (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC]>;
315
316def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
317          (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC]>;
318
319def : Pat<(i32 (WebAssemblywrapper mcsym:$sym)), (CONST_I32 mcsym:$sym)>;
320def : Pat<(i64 (WebAssemblywrapper mcsym:$sym)), (CONST_I64 mcsym:$sym)>;
321
322//===----------------------------------------------------------------------===//
323// Additional sets of instructions.
324//===----------------------------------------------------------------------===//
325
326include "WebAssemblyInstrMemory.td"
327include "WebAssemblyInstrCall.td"
328include "WebAssemblyInstrControl.td"
329include "WebAssemblyInstrInteger.td"
330include "WebAssemblyInstrConv.td"
331include "WebAssemblyInstrFloat.td"
332include "WebAssemblyInstrAtomics.td"
333include "WebAssemblyInstrSIMD.td"
334include "WebAssemblyInstrExceptRef.td"
335include "WebAssemblyInstrBulkMemory.td"
336