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