1// WebAssemblyInstrInfo.td-Describe the WebAssembly Instructions-*- tablegen -*-
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief WebAssembly Instruction definitions.
12///
13//===----------------------------------------------------------------------===//
14
15//===----------------------------------------------------------------------===//
16// WebAssembly Instruction Predicate Definitions.
17//===----------------------------------------------------------------------===//
18
19def HasAddr32 : Predicate<"!Subtarget->hasAddr64()">;
20def HasAddr64 : Predicate<"Subtarget->hasAddr64()">;
21def HasSIMD128 : Predicate<"Subtarget->hasSIMD128()">,
22                           AssemblerPredicate<"FeatureSIMD128", "simd128">;
23
24//===----------------------------------------------------------------------===//
25// WebAssembly-specific DAG Node Types.
26//===----------------------------------------------------------------------===//
27
28def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>,
29                                                  SDTCisVT<1, iPTR>]>;
30def SDT_WebAssemblyCallSeqEnd :
31    SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
32def SDT_WebAssemblyCall0    : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
33def SDT_WebAssemblyCall1    : SDTypeProfile<1, -1, [SDTCisPtrTy<1>]>;
34def SDT_WebAssemblyBrTable  : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
35def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>;
36def SDT_WebAssemblyReturn   : SDTypeProfile<0, -1, []>;
37def SDT_WebAssemblyWrapper  : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>,
38                                                   SDTCisPtrTy<0>]>;
39
40//===----------------------------------------------------------------------===//
41// WebAssembly-specific DAG Nodes.
42//===----------------------------------------------------------------------===//
43
44def WebAssemblycallseq_start :
45    SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart,
46           [SDNPHasChain, SDNPOutGlue]>;
47def WebAssemblycallseq_end :
48    SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd,
49           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
50def WebAssemblycall0 : SDNode<"WebAssemblyISD::CALL0",
51                              SDT_WebAssemblyCall0,
52                              [SDNPHasChain, SDNPVariadic]>;
53def WebAssemblycall1 : SDNode<"WebAssemblyISD::CALL1",
54                              SDT_WebAssemblyCall1,
55                              [SDNPHasChain, SDNPVariadic]>;
56def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE",
57                                 SDT_WebAssemblyBrTable,
58                                 [SDNPHasChain, SDNPVariadic]>;
59def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT",
60                                 SDT_WebAssemblyArgument>;
61def WebAssemblyreturn   : SDNode<"WebAssemblyISD::RETURN",
62                                 SDT_WebAssemblyReturn, [SDNPHasChain]>;
63def WebAssemblywrapper  : SDNode<"WebAssemblyISD::Wrapper",
64                                 SDT_WebAssemblyWrapper>;
65
66//===----------------------------------------------------------------------===//
67// WebAssembly-specific Operands.
68//===----------------------------------------------------------------------===//
69
70let OperandNamespace = "WebAssembly" in {
71
72let OperandType = "OPERAND_BASIC_BLOCK" in
73def bb_op : Operand<OtherVT>;
74
75let OperandType = "OPERAND_LOCAL" in
76def local_op : Operand<i32>;
77
78let OperandType = "OPERAND_GLOBAL" in
79def global_op : Operand<i32>;
80
81let OperandType = "OPERAND_I32IMM" in
82def i32imm_op : Operand<i32>;
83
84let OperandType = "OPERAND_I64IMM" in
85def i64imm_op : Operand<i64>;
86
87let OperandType = "OPERAND_F32IMM" in
88def f32imm_op : Operand<f32>;
89
90let OperandType = "OPERAND_F64IMM" in
91def f64imm_op : Operand<f64>;
92
93let OperandType = "OPERAND_FUNCTION32" in
94def function32_op : Operand<i32>;
95
96let OperandType = "OPERAND_OFFSET32" in
97def offset32_op : Operand<i32>;
98
99let OperandType = "OPERAND_P2ALIGN" in {
100def P2Align : Operand<i32> {
101  let PrintMethod = "printWebAssemblyP2AlignOperand";
102}
103} // OperandType = "OPERAND_P2ALIGN"
104
105let OperandType = "OPERAND_SIGNATURE" in {
106def Signature : Operand<i32> {
107  let PrintMethod = "printWebAssemblySignatureOperand";
108}
109} // OperandType = "OPERAND_SIGNATURE"
110
111let OperandType = "OPERAND_TYPEINDEX" in
112def TypeIndex : Operand<i32>;
113
114} // OperandNamespace = "WebAssembly"
115
116//===----------------------------------------------------------------------===//
117// WebAssembly Instruction Format Definitions.
118//===----------------------------------------------------------------------===//
119
120include "WebAssemblyInstrFormats.td"
121
122//===----------------------------------------------------------------------===//
123// Additional instructions.
124//===----------------------------------------------------------------------===//
125
126multiclass ARGUMENT<WebAssemblyRegClass vt> {
127  let hasSideEffects = 1, Uses = [ARGUMENTS], isCodeGenOnly = 1 in
128  def ARGUMENT_#vt : I<(outs vt:$res), (ins i32imm:$argno),
129                       [(set vt:$res, (WebAssemblyargument timm:$argno))]>;
130}
131multiclass SIMD_ARGUMENT<ValueType vt> {
132  let hasSideEffects = 1, Uses = [ARGUMENTS], isCodeGenOnly = 1 in
133  def ARGUMENT_#vt : SIMD_I<(outs V128:$res), (ins i32imm:$argno),
134                            [(set (vt V128:$res),
135                                  (WebAssemblyargument timm:$argno))]>;
136}
137defm : ARGUMENT<I32>;
138defm : ARGUMENT<I64>;
139defm : ARGUMENT<F32>;
140defm : ARGUMENT<F64>;
141defm : SIMD_ARGUMENT<v16i8>;
142defm : SIMD_ARGUMENT<v8i16>;
143defm : SIMD_ARGUMENT<v4i32>;
144defm : SIMD_ARGUMENT<v4f32>;
145
146let Defs = [ARGUMENTS] in {
147
148// get_local and set_local are not generated by instruction selection; they
149// are implied by virtual register uses and defs.
150multiclass LOCAL<WebAssemblyRegClass vt> {
151let hasSideEffects = 0 in {
152  // COPY is not an actual instruction in wasm, but since we allow get_local and
153  // set_local to be implicit during most of codegen, we can have a COPY which
154  // is actually a no-op because all the work is done in the implied get_local
155  // and set_local. COPYs are eliminated (and replaced with
156  // get_local/set_local) in the ExplicitLocals pass.
157  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
158  def COPY_#vt : I<(outs vt:$res), (ins vt:$src), [], "copy_local\t$res, $src">;
159
160  // TEE is similar to COPY, but writes two copies of its result. Typically
161  // this would be used to stackify one result and write the other result to a
162  // local.
163  let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in
164  def TEE_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), [],
165                  "tee_local\t$res, $also, $src">;
166
167  // This is the actual get_local instruction in wasm. These are made explicit
168  // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm
169  // local, which is a side effect not otherwise modeled in LLVM.
170  let mayLoad = 1, isAsCheapAsAMove = 1 in
171  def GET_LOCAL_#vt : I<(outs vt:$res), (ins local_op:$local), [],
172                        "get_local\t$res, $local", 0x20>;
173
174  // This is the actual set_local instruction in wasm. These are made explicit
175  // by the ExplicitLocals pass. It has mayStore because it writes to a wasm
176  // local, which is a side effect not otherwise modeled in LLVM.
177  let mayStore = 1, isAsCheapAsAMove = 1 in
178  def SET_LOCAL_#vt : I<(outs), (ins local_op:$local, vt:$src), [],
179                        "set_local\t$local, $src", 0x21>;
180
181  // This is the actual tee_local instruction in wasm. TEEs are turned into
182  // TEE_LOCALs by the ExplicitLocals pass. It has mayStore for the same reason
183  // as SET_LOCAL.
184  let mayStore = 1, isAsCheapAsAMove = 1 in
185  def TEE_LOCAL_#vt : I<(outs vt:$res), (ins local_op:$local, vt:$src), [],
186                         "tee_local\t$res, $local, $src", 0x22>;
187
188  // Unused values must be dropped in some contexts.
189  def DROP_#vt : I<(outs), (ins vt:$src), [],
190                   "drop\t$src", 0x1a>;
191
192  let mayLoad = 1 in
193  def GET_GLOBAL_#vt : I<(outs vt:$res), (ins global_op:$local), [],
194                         "get_global\t$res, $local", 0x23>;
195
196  let mayStore = 1 in
197  def SET_GLOBAL_#vt : I<(outs), (ins global_op:$local, vt:$src), [],
198                         "set_global\t$local, $src", 0x24>;
199
200} // hasSideEffects = 0
201}
202defm : LOCAL<I32>;
203defm : LOCAL<I64>;
204defm : LOCAL<F32>;
205defm : LOCAL<F64>;
206defm : LOCAL<V128>, Requires<[HasSIMD128]>;
207
208let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
209def CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
210                  [(set I32:$res, imm:$imm)],
211                  "i32.const\t$res, $imm", 0x41>;
212def CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
213                  [(set I64:$res, imm:$imm)],
214                  "i64.const\t$res, $imm", 0x42>;
215def CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
216                  [(set F32:$res, fpimm:$imm)],
217                  "f32.const\t$res, $imm", 0x43>;
218def CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
219                  [(set F64:$res, fpimm:$imm)],
220                  "f64.const\t$res, $imm", 0x44>;
221} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
222
223} // Defs = [ARGUMENTS]
224
225def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
226          (CONST_I32 tglobaladdr:$addr)>;
227def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
228          (CONST_I32 texternalsym:$addr)>;
229
230//===----------------------------------------------------------------------===//
231// Additional sets of instructions.
232//===----------------------------------------------------------------------===//
233
234include "WebAssemblyInstrMemory.td"
235include "WebAssemblyInstrCall.td"
236include "WebAssemblyInstrControl.td"
237include "WebAssemblyInstrInteger.td"
238include "WebAssemblyInstrConv.td"
239include "WebAssemblyInstrFloat.td"
240include "WebAssemblyInstrAtomics.td"
241include "WebAssemblyInstrSIMD.td"
242