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