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_I32IMM" in
75def i32imm_op : Operand<i32>;
76
77let OperandType = "OPERAND_I64IMM" in
78def i64imm_op : Operand<i64>;
79
80let OperandType = "OPERAND_F32IMM" in
81def f32imm_op : Operand<f32>;
82
83let OperandType = "OPERAND_F64IMM" in
84def f64imm_op : Operand<f64>;
85
86let OperandType = "OPERAND_P2ALIGN" in {
87def P2Align : Operand<i32> {
88  let PrintMethod = "printWebAssemblyP2AlignOperand";
89}
90} // OperandType = "OPERAND_P2ALIGN"
91
92let OperandType = "OPERAND_SIGNATURE" in {
93def Signature : Operand<i32> {
94  let PrintMethod = "printWebAssemblySignatureOperand";
95}
96} // OperandType = "OPERAND_SIGNATURE"
97
98} // OperandNamespace = "WebAssembly"
99
100//===----------------------------------------------------------------------===//
101// WebAssembly Instruction Format Definitions.
102//===----------------------------------------------------------------------===//
103
104include "WebAssemblyInstrFormats.td"
105
106//===----------------------------------------------------------------------===//
107// Additional instructions.
108//===----------------------------------------------------------------------===//
109
110multiclass ARGUMENT<WebAssemblyRegClass vt> {
111  let hasSideEffects = 1, Uses = [ARGUMENTS], isCodeGenOnly = 1 in
112  def ARGUMENT_#vt : I<(outs vt:$res), (ins i32imm:$argno),
113                       [(set vt:$res, (WebAssemblyargument timm:$argno))]>;
114}
115multiclass SIMD_ARGUMENT<ValueType vt> {
116  let hasSideEffects = 1, Uses = [ARGUMENTS], isCodeGenOnly = 1 in
117  def ARGUMENT_#vt : SIMD_I<(outs V128:$res), (ins i32imm:$argno),
118                            [(set (vt V128:$res),
119                                  (WebAssemblyargument timm:$argno))]>;
120}
121defm : ARGUMENT<I32>;
122defm : ARGUMENT<I64>;
123defm : ARGUMENT<F32>;
124defm : ARGUMENT<F64>;
125defm : SIMD_ARGUMENT<v16i8>;
126defm : SIMD_ARGUMENT<v8i16>;
127defm : SIMD_ARGUMENT<v4i32>;
128defm : SIMD_ARGUMENT<v4f32>;
129
130let Defs = [ARGUMENTS] in {
131
132// get_local and set_local are not generated by instruction selection; they
133// are implied by virtual register uses and defs.
134multiclass LOCAL<WebAssemblyRegClass vt> {
135let hasSideEffects = 0 in {
136  // COPY_LOCAL is not an actual instruction in wasm, but since we allow
137  // get_local and set_local to be implicit, we can have a COPY_LOCAL which
138  // is actually a no-op because all the work is done in the implied
139  // get_local and set_local.
140  let isAsCheapAsAMove = 1 in
141  def COPY_LOCAL_#vt : I<(outs vt:$res), (ins vt:$src), [],
142                         "copy_local\t$res, $src">;
143
144  // TEE_LOCAL is similar to COPY_LOCAL, but writes two copies of its result.
145  // Typically this would be used to stackify one result and write the other
146  // result to a local.
147  let isAsCheapAsAMove = 1 in
148  def TEE_LOCAL_#vt : I<(outs vt:$res, vt:$also), (ins vt:$src), [],
149                        "tee_local\t$res, $also, $src">;
150} // hasSideEffects = 0
151}
152defm : LOCAL<I32>;
153defm : LOCAL<I64>;
154defm : LOCAL<F32>;
155defm : LOCAL<F64>;
156defm : LOCAL<V128>, Requires<[HasSIMD128]>;
157
158let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in {
159def CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm),
160                  [(set I32:$res, imm:$imm)],
161                  "i32.const\t$res, $imm">;
162def CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm),
163                  [(set I64:$res, imm:$imm)],
164                  "i64.const\t$res, $imm">;
165def CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm),
166                  [(set F32:$res, fpimm:$imm)],
167                  "f32.const\t$res, $imm">;
168def CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm),
169                  [(set F64:$res, fpimm:$imm)],
170                  "f64.const\t$res, $imm">;
171} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1
172
173} // Defs = [ARGUMENTS]
174
175def : Pat<(i32 (WebAssemblywrapper tglobaladdr:$addr)),
176          (CONST_I32 tglobaladdr:$addr)>;
177def : Pat<(i32 (WebAssemblywrapper texternalsym:$addr)),
178          (CONST_I32 texternalsym:$addr)>;
179
180//===----------------------------------------------------------------------===//
181// Additional sets of instructions.
182//===----------------------------------------------------------------------===//
183
184include "WebAssemblyInstrMemory.td"
185include "WebAssemblyInstrCall.td"
186include "WebAssemblyInstrControl.td"
187include "WebAssemblyInstrInteger.td"
188include "WebAssemblyInstrConv.td"
189include "WebAssemblyInstrFloat.td"
190include "WebAssemblyInstrAtomics.td"
191include "WebAssemblyInstrSIMD.td"
192