xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
10b57cec5SDimitry Andric// WebAssemblyInstrSIMD.td - WebAssembly SIMD codegen support -*- tablegen -*-//
20b57cec5SDimitry Andric//
30b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric//
70b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric///
90b57cec5SDimitry Andric/// \file
100b57cec5SDimitry Andric/// WebAssembly SIMD operand code-gen constructs.
110b57cec5SDimitry Andric///
120b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
14349cc55cSDimitry Andric// Instructions using the SIMD opcode prefix and requiring one of the SIMD
15349cc55cSDimitry Andric// feature predicates.
16349cc55cSDimitry Andricmulticlass ABSTRACT_SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
17349cc55cSDimitry Andric                           list<dag> pattern_r, string asmstr_r,
18349cc55cSDimitry Andric                           string asmstr_s, bits<32> simdop,
19349cc55cSDimitry Andric                           Predicate simd_level> {
200b57cec5SDimitry Andric  defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s,
21e8d8bef9SDimitry Andric              !if(!ge(simdop, 0x100),
22e8d8bef9SDimitry Andric                  !or(0xfd0000, !and(0xffff, simdop)),
23e8d8bef9SDimitry Andric                  !or(0xfd00, !and(0xff, simdop)))>,
24349cc55cSDimitry Andric            Requires<[simd_level]>;
250b57cec5SDimitry Andric}
260b57cec5SDimitry Andric
27349cc55cSDimitry Andricmulticlass SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
28349cc55cSDimitry Andric                  list<dag> pattern_r, string asmstr_r = "",
29349cc55cSDimitry Andric                  string asmstr_s = "", bits<32> simdop = -1> {
30349cc55cSDimitry Andric  defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r,
31349cc55cSDimitry Andric                            asmstr_s, simdop, HasSIMD128>;
32349cc55cSDimitry Andric}
33349cc55cSDimitry Andric
34349cc55cSDimitry Andricmulticlass RELAXED_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
35349cc55cSDimitry Andric                     list<dag> pattern_r, string asmstr_r = "",
36349cc55cSDimitry Andric                     string asmstr_s = "", bits<32> simdop = -1> {
37349cc55cSDimitry Andric  defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r,
38349cc55cSDimitry Andric                            asmstr_s, simdop, HasRelaxedSIMD>;
39349cc55cSDimitry Andric}
40349cc55cSDimitry Andric
41349cc55cSDimitry Andric
420b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v16i8>;
430b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v8i16>;
440b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v4i32>;
450b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v2i64>;
460b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v4f32>;
470b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v2f64>;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric// Constrained immediate argument types
500b57cec5SDimitry Andricforeach SIZE = [8, 16] in
510b57cec5SDimitry Andricdef ImmI#SIZE : ImmLeaf<i32,
520b57cec5SDimitry Andric  "return -(1 << ("#SIZE#" - 1)) <= Imm && Imm < (1 << ("#SIZE#" - 1));"
530b57cec5SDimitry Andric>;
540b57cec5SDimitry Andricforeach SIZE = [2, 4, 8, 16, 32] in
550b57cec5SDimitry Andricdef LaneIdx#SIZE : ImmLeaf<i32, "return 0 <= Imm && Imm < "#SIZE#";">;
560b57cec5SDimitry Andric
57e8d8bef9SDimitry Andric// Create vector with identical lanes: splat
58e8d8bef9SDimitry Andricdef splat2 : PatFrag<(ops node:$x), (build_vector $x, $x)>;
59e8d8bef9SDimitry Andricdef splat4 : PatFrag<(ops node:$x), (build_vector $x, $x, $x, $x)>;
60e8d8bef9SDimitry Andricdef splat8 : PatFrag<(ops node:$x), (build_vector $x, $x, $x, $x,
61e8d8bef9SDimitry Andric                                                  $x, $x, $x, $x)>;
62e8d8bef9SDimitry Andricdef splat16 : PatFrag<(ops node:$x),
63e8d8bef9SDimitry Andric                      (build_vector $x, $x, $x, $x, $x, $x, $x, $x,
64e8d8bef9SDimitry Andric                                    $x, $x, $x, $x, $x, $x, $x, $x)>;
65e8d8bef9SDimitry Andric
66e8d8bef9SDimitry Andricclass Vec {
67e8d8bef9SDimitry Andric  ValueType vt;
68e8d8bef9SDimitry Andric  ValueType int_vt;
69e8d8bef9SDimitry Andric  ValueType lane_vt;
70e8d8bef9SDimitry Andric  WebAssemblyRegClass lane_rc;
71e8d8bef9SDimitry Andric  int lane_bits;
72e8d8bef9SDimitry Andric  ImmLeaf lane_idx;
73e8d8bef9SDimitry Andric  PatFrag splat;
74e8d8bef9SDimitry Andric  string prefix;
75e8d8bef9SDimitry Andric  Vec split;
76e8d8bef9SDimitry Andric}
77e8d8bef9SDimitry Andric
78e8d8bef9SDimitry Andricdef I8x16 : Vec {
79e8d8bef9SDimitry Andric  let vt = v16i8;
80e8d8bef9SDimitry Andric  let int_vt = vt;
81e8d8bef9SDimitry Andric  let lane_vt = i32;
82e8d8bef9SDimitry Andric  let lane_rc = I32;
83e8d8bef9SDimitry Andric  let lane_bits = 8;
84e8d8bef9SDimitry Andric  let lane_idx = LaneIdx16;
85e8d8bef9SDimitry Andric  let splat = splat16;
86e8d8bef9SDimitry Andric  let prefix = "i8x16";
87e8d8bef9SDimitry Andric}
88e8d8bef9SDimitry Andric
89e8d8bef9SDimitry Andricdef I16x8 : Vec {
90e8d8bef9SDimitry Andric  let vt = v8i16;
91e8d8bef9SDimitry Andric  let int_vt = vt;
92e8d8bef9SDimitry Andric  let lane_vt = i32;
93e8d8bef9SDimitry Andric  let lane_rc = I32;
94e8d8bef9SDimitry Andric  let lane_bits = 16;
95e8d8bef9SDimitry Andric  let lane_idx = LaneIdx8;
96e8d8bef9SDimitry Andric  let splat = splat8;
97e8d8bef9SDimitry Andric  let prefix = "i16x8";
98e8d8bef9SDimitry Andric  let split = I8x16;
99e8d8bef9SDimitry Andric}
100e8d8bef9SDimitry Andric
101e8d8bef9SDimitry Andricdef I32x4 : Vec {
102e8d8bef9SDimitry Andric  let vt = v4i32;
103e8d8bef9SDimitry Andric  let int_vt = vt;
104e8d8bef9SDimitry Andric  let lane_vt = i32;
105e8d8bef9SDimitry Andric  let lane_rc = I32;
106e8d8bef9SDimitry Andric  let lane_bits = 32;
107e8d8bef9SDimitry Andric  let lane_idx = LaneIdx4;
108e8d8bef9SDimitry Andric  let splat = splat4;
109e8d8bef9SDimitry Andric  let prefix = "i32x4";
110e8d8bef9SDimitry Andric  let split = I16x8;
111e8d8bef9SDimitry Andric}
112e8d8bef9SDimitry Andric
113e8d8bef9SDimitry Andricdef I64x2 : Vec {
114e8d8bef9SDimitry Andric  let vt = v2i64;
115e8d8bef9SDimitry Andric  let int_vt = vt;
116e8d8bef9SDimitry Andric  let lane_vt = i64;
117e8d8bef9SDimitry Andric  let lane_rc = I64;
118e8d8bef9SDimitry Andric  let lane_bits = 64;
119e8d8bef9SDimitry Andric  let lane_idx = LaneIdx2;
120e8d8bef9SDimitry Andric  let splat = splat2;
121e8d8bef9SDimitry Andric  let prefix = "i64x2";
122e8d8bef9SDimitry Andric  let split = I32x4;
123e8d8bef9SDimitry Andric}
124e8d8bef9SDimitry Andric
125e8d8bef9SDimitry Andricdef F32x4 : Vec {
126e8d8bef9SDimitry Andric  let vt = v4f32;
127e8d8bef9SDimitry Andric  let int_vt = v4i32;
128e8d8bef9SDimitry Andric  let lane_vt = f32;
129e8d8bef9SDimitry Andric  let lane_rc = F32;
130e8d8bef9SDimitry Andric  let lane_bits = 32;
131e8d8bef9SDimitry Andric  let lane_idx = LaneIdx4;
132e8d8bef9SDimitry Andric  let splat = splat4;
133e8d8bef9SDimitry Andric  let prefix = "f32x4";
134e8d8bef9SDimitry Andric}
135e8d8bef9SDimitry Andric
136e8d8bef9SDimitry Andricdef F64x2 : Vec {
137e8d8bef9SDimitry Andric  let vt = v2f64;
138e8d8bef9SDimitry Andric  let int_vt = v2i64;
139e8d8bef9SDimitry Andric  let lane_vt = f64;
140e8d8bef9SDimitry Andric  let lane_rc = F64;
141e8d8bef9SDimitry Andric  let lane_bits = 64;
142e8d8bef9SDimitry Andric  let lane_idx = LaneIdx2;
143e8d8bef9SDimitry Andric  let splat = splat2;
144e8d8bef9SDimitry Andric  let prefix = "f64x2";
145e8d8bef9SDimitry Andric}
146e8d8bef9SDimitry Andric
147e8d8bef9SDimitry Andricdefvar AllVecs = [I8x16, I16x8, I32x4, I64x2, F32x4, F64x2];
148e8d8bef9SDimitry Andricdefvar IntVecs = [I8x16, I16x8, I32x4, I64x2];
149e8d8bef9SDimitry Andric
1500b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1510b57cec5SDimitry Andric// Load and store
1520b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric// Load: v128.load
1555ffd83dbSDimitry Andriclet mayLoad = 1, UseNamedOperandTable = 1 in {
1565ffd83dbSDimitry Andricdefm LOAD_V128_A32 :
1570b57cec5SDimitry Andric  SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
1580b57cec5SDimitry Andric         (outs), (ins P2Align:$p2align, offset32_op:$off), [],
1590b57cec5SDimitry Andric         "v128.load\t$dst, ${off}(${addr})$p2align",
1600b57cec5SDimitry Andric         "v128.load\t$off$p2align", 0>;
1615ffd83dbSDimitry Andricdefm LOAD_V128_A64 :
1625ffd83dbSDimitry Andric  SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
1635ffd83dbSDimitry Andric         (outs), (ins P2Align:$p2align, offset64_op:$off), [],
1645ffd83dbSDimitry Andric         "v128.load\t$dst, ${off}(${addr})$p2align",
1655ffd83dbSDimitry Andric         "v128.load\t$off$p2align", 0>;
1665ffd83dbSDimitry Andric}
1670b57cec5SDimitry Andric
168e8d8bef9SDimitry Andric// Def load patterns from WebAssemblyInstrMemory.td for vector types
169e8d8bef9SDimitry Andricforeach vec = AllVecs in {
170e8d8bef9SDimitry Andricdefm : LoadPatNoOffset<vec.vt, load, "LOAD_V128">;
171e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load, regPlusImm, "LOAD_V128">;
172e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load, or_is_add, "LOAD_V128">;
173e8d8bef9SDimitry Andricdefm : LoadPatOffsetOnly<vec.vt, load, "LOAD_V128">;
174e8d8bef9SDimitry Andricdefm : LoadPatGlobalAddrOffOnly<vec.vt, load, "LOAD_V128">;
1750b57cec5SDimitry Andric}
1760b57cec5SDimitry Andric
177e8d8bef9SDimitry Andric// v128.loadX_splat
178e8d8bef9SDimitry Andricmulticlass SIMDLoadSplat<int size, bits<32> simdop> {
1795ffd83dbSDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
180e8d8bef9SDimitry Andric  defm LOAD#size#_SPLAT_A32 :
1815ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
1825ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
1835ffd83dbSDimitry Andric           (outs),
1845ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off), [],
185e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align",
186e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$off$p2align", simdop>;
187e8d8bef9SDimitry Andric  defm LOAD#size#_SPLAT_A64 :
1885ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
1895ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
1905ffd83dbSDimitry Andric           (outs),
1915ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off), [],
192e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align",
193e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$off$p2align", simdop>;
1948bcb0991SDimitry Andric  }
1955ffd83dbSDimitry Andric}
1968bcb0991SDimitry Andric
197e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<8, 7>;
198e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<16, 8>;
199e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<32, 9>;
200e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<64, 10>;
2018bcb0991SDimitry Andric
202480093f4SDimitry Andricdef wasm_load_splat_t : SDTypeProfile<1, 1, [SDTCisPtrTy<1>]>;
203480093f4SDimitry Andricdef wasm_load_splat : SDNode<"WebAssemblyISD::LOAD_SPLAT", wasm_load_splat_t,
204480093f4SDimitry Andric                             [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
205480093f4SDimitry Andricdef load_splat : PatFrag<(ops node:$addr), (wasm_load_splat node:$addr)>;
2068bcb0991SDimitry Andric
207e8d8bef9SDimitry Andricforeach vec = AllVecs in {
208e8d8bef9SDimitry Andricdefvar inst = "LOAD"#vec.lane_bits#"_SPLAT";
209e8d8bef9SDimitry Andricdefm : LoadPatNoOffset<vec.vt, load_splat, inst>;
210e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load_splat, regPlusImm, inst>;
211e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load_splat, or_is_add, inst>;
212e8d8bef9SDimitry Andricdefm : LoadPatOffsetOnly<vec.vt, load_splat, inst>;
213e8d8bef9SDimitry Andricdefm : LoadPatGlobalAddrOffOnly<vec.vt, load_splat, inst>;
2148bcb0991SDimitry Andric}
2158bcb0991SDimitry Andric
2168bcb0991SDimitry Andric// Load and extend
217e8d8bef9SDimitry Andricmulticlass SIMDLoadExtend<Vec vec, string loadPat, bits<32> simdop> {
218e8d8bef9SDimitry Andric  defvar signed = vec.prefix#".load"#loadPat#"_s";
219e8d8bef9SDimitry Andric  defvar unsigned = vec.prefix#".load"#loadPat#"_u";
2205ffd83dbSDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
221e8d8bef9SDimitry Andric  defm LOAD_EXTEND_S_#vec#_A32 :
2225ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2235ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
2248bcb0991SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
225e8d8bef9SDimitry Andric           signed#"\t$dst, ${off}(${addr})$p2align",
226e8d8bef9SDimitry Andric           signed#"\t$off$p2align", simdop>;
227e8d8bef9SDimitry Andric  defm LOAD_EXTEND_U_#vec#_A32 :
2285ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2295ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
2308bcb0991SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
231e8d8bef9SDimitry Andric           unsigned#"\t$dst, ${off}(${addr})$p2align",
232e8d8bef9SDimitry Andric           unsigned#"\t$off$p2align", !add(simdop, 1)>;
233e8d8bef9SDimitry Andric  defm LOAD_EXTEND_S_#vec#_A64 :
2345ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2355ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
2365ffd83dbSDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
237e8d8bef9SDimitry Andric           signed#"\t$dst, ${off}(${addr})$p2align",
238e8d8bef9SDimitry Andric           signed#"\t$off$p2align", simdop>;
239e8d8bef9SDimitry Andric  defm LOAD_EXTEND_U_#vec#_A64 :
2405ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2415ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
2425ffd83dbSDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
243e8d8bef9SDimitry Andric           unsigned#"\t$dst, ${off}(${addr})$p2align",
244e8d8bef9SDimitry Andric           unsigned#"\t$off$p2align", !add(simdop, 1)>;
2458bcb0991SDimitry Andric  }
2468bcb0991SDimitry Andric}
2478bcb0991SDimitry Andric
248e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I16x8, "8x8", 1>;
249e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I32x4, "16x4", 3>;
250e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I64x2, "32x2", 5>;
2518bcb0991SDimitry Andric
252e8d8bef9SDimitry Andricforeach vec = [I16x8, I32x4, I64x2] in
253e8d8bef9SDimitry Andricforeach exts = [["sextloadvi", "_S"],
254e8d8bef9SDimitry Andric                ["zextloadvi", "_U"],
255e8d8bef9SDimitry Andric                ["extloadvi", "_U"]] in {
256e8d8bef9SDimitry Andricdefvar loadpat = !cast<PatFrag>(exts[0]#vec.split.lane_bits);
257e8d8bef9SDimitry Andricdefvar inst = "LOAD_EXTEND"#exts[1]#"_"#vec;
258e8d8bef9SDimitry Andricdefm : LoadPatNoOffset<vec.vt, loadpat, inst>;
259e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, loadpat, regPlusImm, inst>;
260e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, loadpat, or_is_add, inst>;
261e8d8bef9SDimitry Andricdefm : LoadPatOffsetOnly<vec.vt, loadpat, inst>;
262e8d8bef9SDimitry Andricdefm : LoadPatGlobalAddrOffOnly<vec.vt, loadpat, inst>;
2638bcb0991SDimitry Andric}
2648bcb0991SDimitry Andric
265e8d8bef9SDimitry Andric// Load lane into zero vector
266e8d8bef9SDimitry Andricmulticlass SIMDLoadZero<Vec vec, bits<32> simdop> {
267e8d8bef9SDimitry Andric  defvar name = "v128.load"#vec.lane_bits#"_zero";
268e8d8bef9SDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
269e8d8bef9SDimitry Andric  defm LOAD_ZERO_#vec#_A32 :
270e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
271e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
272e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
273e8d8bef9SDimitry Andric           name#"\t$dst, ${off}(${addr})$p2align",
274e8d8bef9SDimitry Andric           name#"\t$off$p2align", simdop>;
275e8d8bef9SDimitry Andric  defm LOAD_ZERO_#vec#_A64 :
276e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
277e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
278e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
279e8d8bef9SDimitry Andric           name#"\t$dst, ${off}(${addr})$p2align",
280e8d8bef9SDimitry Andric           name#"\t$off$p2align", simdop>;
281e8d8bef9SDimitry Andric  } // mayLoad = 1, UseNamedOperandTable = 1
282e8d8bef9SDimitry Andric}
283e8d8bef9SDimitry Andric
284fe6060f1SDimitry Andricdefm "" : SIMDLoadZero<I32x4, 0x5c>;
285fe6060f1SDimitry Andricdefm "" : SIMDLoadZero<I64x2, 0x5d>;
286e8d8bef9SDimitry Andric
287349cc55cSDimitry Andric// Use load_zero to load scalars into vectors as well where possible.
288349cc55cSDimitry Andric// TODO: i32, i16, and i8 scalars
289349cc55cSDimitry Andricdef load_scalar :
290349cc55cSDimitry Andric  PatFrag<(ops node:$addr), (scalar_to_vector (i64 (load $addr)))>;
291349cc55cSDimitry Andricdefm : LoadPatNoOffset<v2i64, load_scalar, "LOAD_ZERO_I64x2">;
292349cc55cSDimitry Andricdefm : LoadPatImmOff<v2i64, load_scalar, regPlusImm, "LOAD_ZERO_I64x2">;
293349cc55cSDimitry Andricdefm : LoadPatImmOff<v2i64, load_scalar, or_is_add, "LOAD_ZERO_I64x2">;
294349cc55cSDimitry Andricdefm : LoadPatOffsetOnly<v2i64, load_scalar, "LOAD_ZERO_I64x2">;
295349cc55cSDimitry Andricdefm : LoadPatGlobalAddrOffOnly<v2i64, load_scalar, "LOAD_ZERO_I64x2">;
296349cc55cSDimitry Andric
297fe6060f1SDimitry Andric// TODO: f32x4 and f64x2 as well
298e8d8bef9SDimitry Andricforeach vec = [I32x4, I64x2] in {
299e8d8bef9SDimitry Andric  defvar inst = "LOAD_ZERO_"#vec;
300fe6060f1SDimitry Andric  defvar pat = PatFrag<(ops node:$ptr),
301fe6060f1SDimitry Andric    (vector_insert (vec.splat (vec.lane_vt 0)), (vec.lane_vt (load $ptr)), 0)>;
302fe6060f1SDimitry Andric  defm : LoadPatNoOffset<vec.vt, pat, inst>;
303fe6060f1SDimitry Andric  defm : LoadPatImmOff<vec.vt, pat, regPlusImm, inst>;
304fe6060f1SDimitry Andric  defm : LoadPatImmOff<vec.vt, pat, or_is_add, inst>;
305fe6060f1SDimitry Andric  defm : LoadPatOffsetOnly<vec.vt, pat, inst>;
306fe6060f1SDimitry Andric  defm : LoadPatGlobalAddrOffOnly<vec.vt, pat, inst>;
307e8d8bef9SDimitry Andric}
308e8d8bef9SDimitry Andric
309e8d8bef9SDimitry Andric// Load lane
310e8d8bef9SDimitry Andricmulticlass SIMDLoadLane<Vec vec, bits<32> simdop> {
311e8d8bef9SDimitry Andric  defvar name = "v128.load"#vec.lane_bits#"_lane";
312e8d8bef9SDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
313e8d8bef9SDimitry Andric  defm LOAD_LANE_#vec#_A32 :
314e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
315e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx,
316e8d8bef9SDimitry Andric                I32:$addr, V128:$vec),
317e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx),
318e8d8bef9SDimitry Andric           [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx",
319e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
320e8d8bef9SDimitry Andric  defm LOAD_LANE_#vec#_A64 :
321e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
322e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx,
323e8d8bef9SDimitry Andric                I64:$addr, V128:$vec),
324e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx),
325e8d8bef9SDimitry Andric           [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx",
326e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
327e8d8bef9SDimitry Andric  } // mayLoad = 1, UseNamedOperandTable = 1
328e8d8bef9SDimitry Andric}
329e8d8bef9SDimitry Andric
330fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I8x16, 0x54>;
331fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I16x8, 0x55>;
332fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I32x4, 0x56>;
333fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I64x2, 0x57>;
334e8d8bef9SDimitry Andric
335e8d8bef9SDimitry Andric// Select loads with no constant offset.
336fe6060f1SDimitry Andricmulticlass LoadLanePatNoOffset<Vec vec, SDPatternOperator kind> {
337e8d8bef9SDimitry Andric  defvar load_lane_a32 = !cast<NI>("LOAD_LANE_"#vec#"_A32");
338e8d8bef9SDimitry Andric  defvar load_lane_a64 = !cast<NI>("LOAD_LANE_"#vec#"_A64");
339e8d8bef9SDimitry Andric  def : Pat<(vec.vt (kind (i32 I32:$addr),
340e8d8bef9SDimitry Andric              (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))),
341e8d8bef9SDimitry Andric            (load_lane_a32 0, 0, imm:$idx, $addr, $vec)>,
342e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
343e8d8bef9SDimitry Andric  def : Pat<(vec.vt (kind (i64 I64:$addr),
344e8d8bef9SDimitry Andric              (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))),
345e8d8bef9SDimitry Andric            (load_lane_a64 0, 0, imm:$idx, $addr, $vec)>,
346e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
347e8d8bef9SDimitry Andric}
348e8d8bef9SDimitry Andric
349fe6060f1SDimitry Andricdef load8_lane :
350fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
351fe6060f1SDimitry Andric          (vector_insert $vec, (i32 (extloadi8 $ptr)), $idx)>;
352fe6060f1SDimitry Andricdef load16_lane :
353fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
354fe6060f1SDimitry Andric          (vector_insert $vec, (i32 (extloadi16 $ptr)), $idx)>;
355fe6060f1SDimitry Andricdef load32_lane :
356fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
357fe6060f1SDimitry Andric          (vector_insert $vec, (i32 (load $ptr)), $idx)>;
358fe6060f1SDimitry Andricdef load64_lane :
359fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
360fe6060f1SDimitry Andric          (vector_insert $vec, (i64 (load $ptr)), $idx)>;
361fe6060f1SDimitry Andric// TODO: floating point lanes as well
362fe6060f1SDimitry Andric
363fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I8x16, load8_lane>;
364fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I16x8, load16_lane>;
365fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I32x4, load32_lane>;
366fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I64x2, load64_lane>;
367e8d8bef9SDimitry Andric
368e8d8bef9SDimitry Andric// TODO: Also support the other load patterns for load_lane once the instructions
369e8d8bef9SDimitry Andric// are merged to the proposal.
3708bcb0991SDimitry Andric
3710b57cec5SDimitry Andric// Store: v128.store
3725ffd83dbSDimitry Andriclet mayStore = 1, UseNamedOperandTable = 1 in {
3735ffd83dbSDimitry Andricdefm STORE_V128_A32 :
3740b57cec5SDimitry Andric  SIMD_I<(outs), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, V128:$vec),
3750b57cec5SDimitry Andric         (outs), (ins P2Align:$p2align, offset32_op:$off), [],
3760b57cec5SDimitry Andric         "v128.store\t${off}(${addr})$p2align, $vec",
3775ffd83dbSDimitry Andric         "v128.store\t$off$p2align", 11>;
3785ffd83dbSDimitry Andricdefm STORE_V128_A64 :
3795ffd83dbSDimitry Andric  SIMD_I<(outs), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, V128:$vec),
3805ffd83dbSDimitry Andric         (outs), (ins P2Align:$p2align, offset64_op:$off), [],
3815ffd83dbSDimitry Andric         "v128.store\t${off}(${addr})$p2align, $vec",
3825ffd83dbSDimitry Andric         "v128.store\t$off$p2align", 11>;
3835ffd83dbSDimitry Andric}
384e8d8bef9SDimitry Andric
385e8d8bef9SDimitry Andric// Def store patterns from WebAssemblyInstrMemory.td for vector types
386e8d8bef9SDimitry Andricforeach vec = AllVecs in {
387e8d8bef9SDimitry Andricdefm : StorePatNoOffset<vec.vt, store, "STORE_V128">;
388e8d8bef9SDimitry Andricdefm : StorePatImmOff<vec.vt, store, regPlusImm, "STORE_V128">;
389e8d8bef9SDimitry Andricdefm : StorePatImmOff<vec.vt, store, or_is_add, "STORE_V128">;
390e8d8bef9SDimitry Andricdefm : StorePatOffsetOnly<vec.vt, store, "STORE_V128">;
391e8d8bef9SDimitry Andricdefm : StorePatGlobalAddrOffOnly<vec.vt, store, "STORE_V128">;
3920b57cec5SDimitry Andric}
3930b57cec5SDimitry Andric
394e8d8bef9SDimitry Andric// Store lane
395e8d8bef9SDimitry Andricmulticlass SIMDStoreLane<Vec vec, bits<32> simdop> {
396e8d8bef9SDimitry Andric  defvar name = "v128.store"#vec.lane_bits#"_lane";
397e8d8bef9SDimitry Andric  let mayStore = 1, UseNamedOperandTable = 1 in {
398e8d8bef9SDimitry Andric  defm STORE_LANE_#vec#_A32 :
399e8d8bef9SDimitry Andric    SIMD_I<(outs),
400e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx,
401e8d8bef9SDimitry Andric                I32:$addr, V128:$vec),
402e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx),
403e8d8bef9SDimitry Andric           [], name#"\t${off}(${addr})$p2align, $vec, $idx",
404e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
405e8d8bef9SDimitry Andric  defm STORE_LANE_#vec#_A64 :
406e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
407e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx,
408e8d8bef9SDimitry Andric                I64:$addr, V128:$vec),
409e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx),
410e8d8bef9SDimitry Andric           [], name#"\t${off}(${addr})$p2align, $vec, $idx",
411e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
412e8d8bef9SDimitry Andric  } // mayStore = 1, UseNamedOperandTable = 1
413e8d8bef9SDimitry Andric}
414e8d8bef9SDimitry Andric
415fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I8x16, 0x58>;
416fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I16x8, 0x59>;
417fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I32x4, 0x5a>;
418fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I64x2, 0x5b>;
419e8d8bef9SDimitry Andric
420e8d8bef9SDimitry Andric// Select stores with no constant offset.
421fe6060f1SDimitry Andricmulticlass StoreLanePatNoOffset<Vec vec, SDPatternOperator kind> {
422e8d8bef9SDimitry Andric  def : Pat<(kind (i32 I32:$addr), (vec.vt V128:$vec), (i32 vec.lane_idx:$idx)),
423e8d8bef9SDimitry Andric            (!cast<NI>("STORE_LANE_"#vec#"_A32") 0, 0, imm:$idx, $addr, $vec)>,
424e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
425e8d8bef9SDimitry Andric  def : Pat<(kind (i64 I64:$addr), (vec.vt V128:$vec), (i32 vec.lane_idx:$idx)),
426e8d8bef9SDimitry Andric            (!cast<NI>("STORE_LANE_"#vec#"_A64") 0, 0, imm:$idx, $addr, $vec)>,
427e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
428e8d8bef9SDimitry Andric}
429e8d8bef9SDimitry Andric
430fe6060f1SDimitry Andricdef store8_lane :
431fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
432fe6060f1SDimitry Andric          (truncstorei8 (i32 (vector_extract $vec, $idx)), $ptr)>;
433fe6060f1SDimitry Andricdef store16_lane :
434fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
435fe6060f1SDimitry Andric          (truncstorei16 (i32 (vector_extract $vec, $idx)), $ptr)>;
436fe6060f1SDimitry Andricdef store32_lane :
437fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
438fe6060f1SDimitry Andric          (store (i32 (vector_extract $vec, $idx)), $ptr)>;
439fe6060f1SDimitry Andricdef store64_lane :
440fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
441fe6060f1SDimitry Andric          (store (i64 (vector_extract $vec, $idx)), $ptr)>;
442fe6060f1SDimitry Andric// TODO: floating point lanes as well
443e8d8bef9SDimitry Andric
444fe6060f1SDimitry Andriclet AddedComplexity = 1 in {
445fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I8x16, store8_lane>;
446fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I16x8, store16_lane>;
447fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I32x4, store32_lane>;
448fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I64x2, store64_lane>;
449fe6060f1SDimitry Andric}
450e8d8bef9SDimitry Andric
4510b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
4520b57cec5SDimitry Andric// Constructing SIMD values
4530b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric// Constant: v128.const
456e8d8bef9SDimitry Andricmulticlass ConstVec<Vec vec, dag ops, dag pat, string args> {
457fe6060f1SDimitry Andric  let isMoveImm = 1, isReMaterializable = 1 in
458e8d8bef9SDimitry Andric  defm CONST_V128_#vec : SIMD_I<(outs V128:$dst), ops, (outs), ops,
459e8d8bef9SDimitry Andric                                 [(set V128:$dst, (vec.vt pat))],
4600b57cec5SDimitry Andric                                 "v128.const\t$dst, "#args,
4615ffd83dbSDimitry Andric                                 "v128.const\t"#args, 12>;
4620b57cec5SDimitry Andric}
4630b57cec5SDimitry Andric
464e8d8bef9SDimitry Andricdefm "" : ConstVec<I8x16,
4650b57cec5SDimitry Andric                   (ins vec_i8imm_op:$i0, vec_i8imm_op:$i1,
4660b57cec5SDimitry Andric                        vec_i8imm_op:$i2, vec_i8imm_op:$i3,
4670b57cec5SDimitry Andric                        vec_i8imm_op:$i4, vec_i8imm_op:$i5,
4680b57cec5SDimitry Andric                        vec_i8imm_op:$i6, vec_i8imm_op:$i7,
4690b57cec5SDimitry Andric                        vec_i8imm_op:$i8, vec_i8imm_op:$i9,
4700b57cec5SDimitry Andric                        vec_i8imm_op:$iA, vec_i8imm_op:$iB,
4710b57cec5SDimitry Andric                        vec_i8imm_op:$iC, vec_i8imm_op:$iD,
4720b57cec5SDimitry Andric                        vec_i8imm_op:$iE, vec_i8imm_op:$iF),
4730b57cec5SDimitry Andric                   (build_vector ImmI8:$i0, ImmI8:$i1, ImmI8:$i2, ImmI8:$i3,
4740b57cec5SDimitry Andric                                 ImmI8:$i4, ImmI8:$i5, ImmI8:$i6, ImmI8:$i7,
4750b57cec5SDimitry Andric                                 ImmI8:$i8, ImmI8:$i9, ImmI8:$iA, ImmI8:$iB,
4760b57cec5SDimitry Andric                                 ImmI8:$iC, ImmI8:$iD, ImmI8:$iE, ImmI8:$iF),
4770b57cec5SDimitry Andric                   !strconcat("$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7, ",
4780b57cec5SDimitry Andric                              "$i8, $i9, $iA, $iB, $iC, $iD, $iE, $iF")>;
479e8d8bef9SDimitry Andricdefm "" : ConstVec<I16x8,
4800b57cec5SDimitry Andric                   (ins vec_i16imm_op:$i0, vec_i16imm_op:$i1,
4810b57cec5SDimitry Andric                        vec_i16imm_op:$i2, vec_i16imm_op:$i3,
4820b57cec5SDimitry Andric                        vec_i16imm_op:$i4, vec_i16imm_op:$i5,
4830b57cec5SDimitry Andric                        vec_i16imm_op:$i6, vec_i16imm_op:$i7),
4840b57cec5SDimitry Andric                   (build_vector
4850b57cec5SDimitry Andric                     ImmI16:$i0, ImmI16:$i1, ImmI16:$i2, ImmI16:$i3,
4860b57cec5SDimitry Andric                     ImmI16:$i4, ImmI16:$i5, ImmI16:$i6, ImmI16:$i7),
4870b57cec5SDimitry Andric                   "$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7">;
4880b57cec5SDimitry Andriclet IsCanonical = 1 in
489e8d8bef9SDimitry Andricdefm "" : ConstVec<I32x4,
4900b57cec5SDimitry Andric                   (ins vec_i32imm_op:$i0, vec_i32imm_op:$i1,
4910b57cec5SDimitry Andric                        vec_i32imm_op:$i2, vec_i32imm_op:$i3),
4920b57cec5SDimitry Andric                   (build_vector (i32 imm:$i0), (i32 imm:$i1),
4930b57cec5SDimitry Andric                                 (i32 imm:$i2), (i32 imm:$i3)),
4940b57cec5SDimitry Andric                   "$i0, $i1, $i2, $i3">;
495e8d8bef9SDimitry Andricdefm "" : ConstVec<I64x2,
4960b57cec5SDimitry Andric                   (ins vec_i64imm_op:$i0, vec_i64imm_op:$i1),
4970b57cec5SDimitry Andric                   (build_vector (i64 imm:$i0), (i64 imm:$i1)),
4980b57cec5SDimitry Andric                   "$i0, $i1">;
499e8d8bef9SDimitry Andricdefm "" : ConstVec<F32x4,
5000b57cec5SDimitry Andric                   (ins f32imm_op:$i0, f32imm_op:$i1,
5010b57cec5SDimitry Andric                        f32imm_op:$i2, f32imm_op:$i3),
5020b57cec5SDimitry Andric                   (build_vector (f32 fpimm:$i0), (f32 fpimm:$i1),
5030b57cec5SDimitry Andric                                 (f32 fpimm:$i2), (f32 fpimm:$i3)),
5040b57cec5SDimitry Andric                   "$i0, $i1, $i2, $i3">;
505e8d8bef9SDimitry Andricdefm "" : ConstVec<F64x2,
5060b57cec5SDimitry Andric                  (ins f64imm_op:$i0, f64imm_op:$i1),
5070b57cec5SDimitry Andric                  (build_vector (f64 fpimm:$i0), (f64 fpimm:$i1)),
5080b57cec5SDimitry Andric                  "$i0, $i1">;
5090b57cec5SDimitry Andric
5100b57cec5SDimitry Andric// Shuffle lanes: shuffle
5110b57cec5SDimitry Andricdefm SHUFFLE :
5120b57cec5SDimitry Andric  SIMD_I<(outs V128:$dst),
5130b57cec5SDimitry Andric         (ins V128:$x, V128:$y,
5140b57cec5SDimitry Andric           vec_i8imm_op:$m0, vec_i8imm_op:$m1,
5150b57cec5SDimitry Andric           vec_i8imm_op:$m2, vec_i8imm_op:$m3,
5160b57cec5SDimitry Andric           vec_i8imm_op:$m4, vec_i8imm_op:$m5,
5170b57cec5SDimitry Andric           vec_i8imm_op:$m6, vec_i8imm_op:$m7,
5180b57cec5SDimitry Andric           vec_i8imm_op:$m8, vec_i8imm_op:$m9,
5190b57cec5SDimitry Andric           vec_i8imm_op:$mA, vec_i8imm_op:$mB,
5200b57cec5SDimitry Andric           vec_i8imm_op:$mC, vec_i8imm_op:$mD,
5210b57cec5SDimitry Andric           vec_i8imm_op:$mE, vec_i8imm_op:$mF),
5220b57cec5SDimitry Andric         (outs),
5230b57cec5SDimitry Andric         (ins
5240b57cec5SDimitry Andric           vec_i8imm_op:$m0, vec_i8imm_op:$m1,
5250b57cec5SDimitry Andric           vec_i8imm_op:$m2, vec_i8imm_op:$m3,
5260b57cec5SDimitry Andric           vec_i8imm_op:$m4, vec_i8imm_op:$m5,
5270b57cec5SDimitry Andric           vec_i8imm_op:$m6, vec_i8imm_op:$m7,
5280b57cec5SDimitry Andric           vec_i8imm_op:$m8, vec_i8imm_op:$m9,
5290b57cec5SDimitry Andric           vec_i8imm_op:$mA, vec_i8imm_op:$mB,
5300b57cec5SDimitry Andric           vec_i8imm_op:$mC, vec_i8imm_op:$mD,
5310b57cec5SDimitry Andric           vec_i8imm_op:$mE, vec_i8imm_op:$mF),
5320b57cec5SDimitry Andric         [],
533e8d8bef9SDimitry Andric         "i8x16.shuffle\t$dst, $x, $y, "#
5340b57cec5SDimitry Andric           "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "#
5350b57cec5SDimitry Andric           "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF",
536e8d8bef9SDimitry Andric         "i8x16.shuffle\t"#
5370b57cec5SDimitry Andric           "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "#
5380b57cec5SDimitry Andric           "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF",
5395ffd83dbSDimitry Andric         13>;
5400b57cec5SDimitry Andric
5410b57cec5SDimitry Andric// Shuffles after custom lowering
5420b57cec5SDimitry Andricdef wasm_shuffle_t : SDTypeProfile<1, 18, []>;
5430b57cec5SDimitry Andricdef wasm_shuffle : SDNode<"WebAssemblyISD::SHUFFLE", wasm_shuffle_t>;
544e8d8bef9SDimitry Andricforeach vec = AllVecs in {
545e8d8bef9SDimitry Andricdef : Pat<(vec.vt (wasm_shuffle (vec.vt V128:$x), (vec.vt V128:$y),
5460b57cec5SDimitry Andric            (i32 LaneIdx32:$m0), (i32 LaneIdx32:$m1),
5470b57cec5SDimitry Andric            (i32 LaneIdx32:$m2), (i32 LaneIdx32:$m3),
5480b57cec5SDimitry Andric            (i32 LaneIdx32:$m4), (i32 LaneIdx32:$m5),
5490b57cec5SDimitry Andric            (i32 LaneIdx32:$m6), (i32 LaneIdx32:$m7),
5500b57cec5SDimitry Andric            (i32 LaneIdx32:$m8), (i32 LaneIdx32:$m9),
5510b57cec5SDimitry Andric            (i32 LaneIdx32:$mA), (i32 LaneIdx32:$mB),
5520b57cec5SDimitry Andric            (i32 LaneIdx32:$mC), (i32 LaneIdx32:$mD),
5530b57cec5SDimitry Andric            (i32 LaneIdx32:$mE), (i32 LaneIdx32:$mF))),
554e8d8bef9SDimitry Andric          (SHUFFLE $x, $y,
555e8d8bef9SDimitry Andric            imm:$m0, imm:$m1, imm:$m2, imm:$m3,
556e8d8bef9SDimitry Andric            imm:$m4, imm:$m5, imm:$m6, imm:$m7,
557e8d8bef9SDimitry Andric            imm:$m8, imm:$m9, imm:$mA, imm:$mB,
558e8d8bef9SDimitry Andric            imm:$mC, imm:$mD, imm:$mE, imm:$mF)>;
5590b57cec5SDimitry Andric}
5600b57cec5SDimitry Andric
561e8d8bef9SDimitry Andric// Swizzle lanes: i8x16.swizzle
5628bcb0991SDimitry Andricdef wasm_swizzle_t : SDTypeProfile<1, 2, []>;
5638bcb0991SDimitry Andricdef wasm_swizzle : SDNode<"WebAssemblyISD::SWIZZLE", wasm_swizzle_t>;
5648bcb0991SDimitry Andricdefm SWIZZLE :
5658bcb0991SDimitry Andric  SIMD_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins),
5668bcb0991SDimitry Andric         [(set (v16i8 V128:$dst),
5678bcb0991SDimitry Andric           (wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))],
568e8d8bef9SDimitry Andric         "i8x16.swizzle\t$dst, $src, $mask", "i8x16.swizzle", 14>;
5698bcb0991SDimitry Andric
5708bcb0991SDimitry Andricdef : Pat<(int_wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)),
571e8d8bef9SDimitry Andric          (SWIZZLE $src, $mask)>;
5728bcb0991SDimitry Andric
573e8d8bef9SDimitry Andricmulticlass Splat<Vec vec, bits<32> simdop> {
574e8d8bef9SDimitry Andric  defm SPLAT_#vec : SIMD_I<(outs V128:$dst), (ins vec.lane_rc:$x),
575e8d8bef9SDimitry Andric                           (outs), (ins),
576e8d8bef9SDimitry Andric                           [(set (vec.vt V128:$dst),
577e8d8bef9SDimitry Andric                              (vec.splat vec.lane_rc:$x))],
578e8d8bef9SDimitry Andric                           vec.prefix#".splat\t$dst, $x", vec.prefix#".splat",
579e8d8bef9SDimitry Andric                           simdop>;
5800b57cec5SDimitry Andric}
5810b57cec5SDimitry Andric
582e8d8bef9SDimitry Andricdefm "" : Splat<I8x16, 15>;
583e8d8bef9SDimitry Andricdefm "" : Splat<I16x8, 16>;
584e8d8bef9SDimitry Andricdefm "" : Splat<I32x4, 17>;
585e8d8bef9SDimitry Andricdefm "" : Splat<I64x2, 18>;
586e8d8bef9SDimitry Andricdefm "" : Splat<F32x4, 19>;
587e8d8bef9SDimitry Andricdefm "" : Splat<F64x2, 20>;
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andric// scalar_to_vector leaves high lanes undefined, so can be a splat
590e8d8bef9SDimitry Andricforeach vec = AllVecs in
591e8d8bef9SDimitry Andricdef : Pat<(vec.vt (scalar_to_vector (vec.lane_vt vec.lane_rc:$x))),
592e8d8bef9SDimitry Andric          (!cast<Instruction>("SPLAT_"#vec) $x)>;
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
5950b57cec5SDimitry Andric// Accessing lanes
5960b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andric// Extract lane as a scalar: extract_lane / extract_lane_s / extract_lane_u
599e8d8bef9SDimitry Andricmulticlass ExtractLane<Vec vec, bits<32> simdop, string suffix = ""> {
600e8d8bef9SDimitry Andric  defm EXTRACT_LANE_#vec#suffix :
601e8d8bef9SDimitry Andric      SIMD_I<(outs vec.lane_rc:$dst), (ins V128:$vec, vec_i8imm_op:$idx),
6025ffd83dbSDimitry Andric             (outs), (ins vec_i8imm_op:$idx), [],
603e8d8bef9SDimitry Andric             vec.prefix#".extract_lane"#suffix#"\t$dst, $vec, $idx",
604e8d8bef9SDimitry Andric             vec.prefix#".extract_lane"#suffix#"\t$idx", simdop>;
6050b57cec5SDimitry Andric}
6060b57cec5SDimitry Andric
607e8d8bef9SDimitry Andricdefm "" : ExtractLane<I8x16, 21, "_s">;
608e8d8bef9SDimitry Andricdefm "" : ExtractLane<I8x16, 22, "_u">;
609e8d8bef9SDimitry Andricdefm "" : ExtractLane<I16x8, 24, "_s">;
610e8d8bef9SDimitry Andricdefm "" : ExtractLane<I16x8, 25, "_u">;
611e8d8bef9SDimitry Andricdefm "" : ExtractLane<I32x4, 27>;
612e8d8bef9SDimitry Andricdefm "" : ExtractLane<I64x2, 29>;
613e8d8bef9SDimitry Andricdefm "" : ExtractLane<F32x4, 31>;
614e8d8bef9SDimitry Andricdefm "" : ExtractLane<F64x2, 33>;
6150b57cec5SDimitry Andric
6165ffd83dbSDimitry Andricdef : Pat<(vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)),
617e8d8bef9SDimitry Andric          (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>;
6185ffd83dbSDimitry Andricdef : Pat<(vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)),
619e8d8bef9SDimitry Andric          (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>;
6205ffd83dbSDimitry Andricdef : Pat<(vector_extract (v4i32 V128:$vec), (i32 LaneIdx4:$idx)),
621e8d8bef9SDimitry Andric          (EXTRACT_LANE_I32x4 $vec, imm:$idx)>;
6225ffd83dbSDimitry Andricdef : Pat<(vector_extract (v4f32 V128:$vec), (i32 LaneIdx4:$idx)),
623e8d8bef9SDimitry Andric          (EXTRACT_LANE_F32x4 $vec, imm:$idx)>;
6245ffd83dbSDimitry Andricdef : Pat<(vector_extract (v2i64 V128:$vec), (i32 LaneIdx2:$idx)),
625e8d8bef9SDimitry Andric          (EXTRACT_LANE_I64x2 $vec, imm:$idx)>;
6265ffd83dbSDimitry Andricdef : Pat<(vector_extract (v2f64 V128:$vec), (i32 LaneIdx2:$idx)),
627e8d8bef9SDimitry Andric          (EXTRACT_LANE_F64x2 $vec, imm:$idx)>;
6280b57cec5SDimitry Andric
6295ffd83dbSDimitry Andricdef : Pat<
6305ffd83dbSDimitry Andric  (sext_inreg (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), i8),
631e8d8bef9SDimitry Andric  (EXTRACT_LANE_I8x16_s $vec, imm:$idx)>;
6325ffd83dbSDimitry Andricdef : Pat<
6335ffd83dbSDimitry Andric  (and (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), (i32 0xff)),
634e8d8bef9SDimitry Andric  (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>;
6355ffd83dbSDimitry Andricdef : Pat<
6365ffd83dbSDimitry Andric  (sext_inreg (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), i16),
637e8d8bef9SDimitry Andric  (EXTRACT_LANE_I16x8_s $vec, imm:$idx)>;
6385ffd83dbSDimitry Andricdef : Pat<
6395ffd83dbSDimitry Andric  (and (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), (i32 0xffff)),
640e8d8bef9SDimitry Andric  (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>;
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric// Replace lane value: replace_lane
643e8d8bef9SDimitry Andricmulticlass ReplaceLane<Vec vec, bits<32> simdop> {
644e8d8bef9SDimitry Andric  defm REPLACE_LANE_#vec :
645e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$vec, vec_i8imm_op:$idx, vec.lane_rc:$x),
6460b57cec5SDimitry Andric           (outs), (ins vec_i8imm_op:$idx),
6470b57cec5SDimitry Andric           [(set V128:$dst, (vector_insert
648e8d8bef9SDimitry Andric             (vec.vt V128:$vec),
649e8d8bef9SDimitry Andric             (vec.lane_vt vec.lane_rc:$x),
650e8d8bef9SDimitry Andric             (i32 vec.lane_idx:$idx)))],
651e8d8bef9SDimitry Andric           vec.prefix#".replace_lane\t$dst, $vec, $idx, $x",
652e8d8bef9SDimitry Andric           vec.prefix#".replace_lane\t$idx", simdop>;
6530b57cec5SDimitry Andric}
6540b57cec5SDimitry Andric
655e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I8x16, 23>;
656e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I16x8, 26>;
657e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I32x4, 28>;
658e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I64x2, 30>;
659e8d8bef9SDimitry Andricdefm "" : ReplaceLane<F32x4, 32>;
660e8d8bef9SDimitry Andricdefm "" : ReplaceLane<F64x2, 34>;
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric// Lower undef lane indices to zero
6630b57cec5SDimitry Andricdef : Pat<(vector_insert (v16i8 V128:$vec), I32:$x, undef),
664e8d8bef9SDimitry Andric          (REPLACE_LANE_I8x16 $vec, 0, $x)>;
6650b57cec5SDimitry Andricdef : Pat<(vector_insert (v8i16 V128:$vec), I32:$x, undef),
666e8d8bef9SDimitry Andric          (REPLACE_LANE_I16x8 $vec, 0, $x)>;
6670b57cec5SDimitry Andricdef : Pat<(vector_insert (v4i32 V128:$vec), I32:$x, undef),
668e8d8bef9SDimitry Andric          (REPLACE_LANE_I32x4 $vec, 0, $x)>;
6690b57cec5SDimitry Andricdef : Pat<(vector_insert (v2i64 V128:$vec), I64:$x, undef),
670e8d8bef9SDimitry Andric          (REPLACE_LANE_I64x2 $vec, 0, $x)>;
6710b57cec5SDimitry Andricdef : Pat<(vector_insert (v4f32 V128:$vec), F32:$x, undef),
672e8d8bef9SDimitry Andric          (REPLACE_LANE_F32x4 $vec, 0, $x)>;
6730b57cec5SDimitry Andricdef : Pat<(vector_insert (v2f64 V128:$vec), F64:$x, undef),
674e8d8bef9SDimitry Andric          (REPLACE_LANE_F64x2 $vec, 0, $x)>;
6750b57cec5SDimitry Andric
6760b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
6770b57cec5SDimitry Andric// Comparisons
6780b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
6790b57cec5SDimitry Andric
680e8d8bef9SDimitry Andricmulticlass SIMDCondition<Vec vec, string name, CondCode cond, bits<32> simdop> {
681e8d8bef9SDimitry Andric  defm _#vec :
6820b57cec5SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
683e8d8bef9SDimitry Andric           [(set (vec.int_vt V128:$dst),
684e8d8bef9SDimitry Andric             (setcc (vec.vt V128:$lhs), (vec.vt V128:$rhs), cond))],
685e8d8bef9SDimitry Andric           vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
686e8d8bef9SDimitry Andric           vec.prefix#"."#name, simdop>;
6870b57cec5SDimitry Andric}
6880b57cec5SDimitry Andric
6890b57cec5SDimitry Andricmulticlass SIMDConditionInt<string name, CondCode cond, bits<32> baseInst> {
690e8d8bef9SDimitry Andric  defm "" : SIMDCondition<I8x16, name, cond, baseInst>;
691e8d8bef9SDimitry Andric  defm "" : SIMDCondition<I16x8, name, cond, !add(baseInst, 10)>;
692e8d8bef9SDimitry Andric  defm "" : SIMDCondition<I32x4, name, cond, !add(baseInst, 20)>;
6930b57cec5SDimitry Andric}
6940b57cec5SDimitry Andric
6950b57cec5SDimitry Andricmulticlass SIMDConditionFP<string name, CondCode cond, bits<32> baseInst> {
696e8d8bef9SDimitry Andric  defm "" : SIMDCondition<F32x4, name, cond, baseInst>;
697e8d8bef9SDimitry Andric  defm "" : SIMDCondition<F64x2, name, cond, !add(baseInst, 6)>;
6980b57cec5SDimitry Andric}
6990b57cec5SDimitry Andric
7000b57cec5SDimitry Andric// Equality: eq
7010b57cec5SDimitry Andriclet isCommutable = 1 in {
7025ffd83dbSDimitry Andricdefm EQ : SIMDConditionInt<"eq", SETEQ, 35>;
703fe6060f1SDimitry Andricdefm EQ : SIMDCondition<I64x2, "eq", SETEQ, 214>;
7045ffd83dbSDimitry Andricdefm EQ : SIMDConditionFP<"eq", SETOEQ, 65>;
7050b57cec5SDimitry Andric} // isCommutable = 1
7060b57cec5SDimitry Andric
7070b57cec5SDimitry Andric// Non-equality: ne
7080b57cec5SDimitry Andriclet isCommutable = 1 in {
7095ffd83dbSDimitry Andricdefm NE : SIMDConditionInt<"ne", SETNE, 36>;
710fe6060f1SDimitry Andricdefm NE : SIMDCondition<I64x2, "ne", SETNE, 215>;
7115ffd83dbSDimitry Andricdefm NE : SIMDConditionFP<"ne", SETUNE, 66>;
7120b57cec5SDimitry Andric} // isCommutable = 1
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andric// Less than: lt_s / lt_u / lt
7155ffd83dbSDimitry Andricdefm LT_S : SIMDConditionInt<"lt_s", SETLT, 37>;
716fe6060f1SDimitry Andricdefm LT_S : SIMDCondition<I64x2, "lt_s", SETLT, 216>;
7175ffd83dbSDimitry Andricdefm LT_U : SIMDConditionInt<"lt_u", SETULT, 38>;
7185ffd83dbSDimitry Andricdefm LT : SIMDConditionFP<"lt", SETOLT, 67>;
7190b57cec5SDimitry Andric
7200b57cec5SDimitry Andric// Greater than: gt_s / gt_u / gt
7215ffd83dbSDimitry Andricdefm GT_S : SIMDConditionInt<"gt_s", SETGT, 39>;
722fe6060f1SDimitry Andricdefm GT_S : SIMDCondition<I64x2, "gt_s", SETGT, 217>;
7235ffd83dbSDimitry Andricdefm GT_U : SIMDConditionInt<"gt_u", SETUGT, 40>;
7245ffd83dbSDimitry Andricdefm GT : SIMDConditionFP<"gt", SETOGT, 68>;
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric// Less than or equal: le_s / le_u / le
7275ffd83dbSDimitry Andricdefm LE_S : SIMDConditionInt<"le_s", SETLE, 41>;
728fe6060f1SDimitry Andricdefm LE_S : SIMDCondition<I64x2, "le_s", SETLE, 218>;
7295ffd83dbSDimitry Andricdefm LE_U : SIMDConditionInt<"le_u", SETULE, 42>;
7305ffd83dbSDimitry Andricdefm LE : SIMDConditionFP<"le", SETOLE, 69>;
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric// Greater than or equal: ge_s / ge_u / ge
7335ffd83dbSDimitry Andricdefm GE_S : SIMDConditionInt<"ge_s", SETGE, 43>;
734fe6060f1SDimitry Andricdefm GE_S : SIMDCondition<I64x2, "ge_s", SETGE, 219>;
7355ffd83dbSDimitry Andricdefm GE_U : SIMDConditionInt<"ge_u", SETUGE, 44>;
7365ffd83dbSDimitry Andricdefm GE : SIMDConditionFP<"ge", SETOGE, 70>;
7370b57cec5SDimitry Andric
7380b57cec5SDimitry Andric// Lower float comparisons that don't care about NaN to standard WebAssembly
7390b57cec5SDimitry Andric// float comparisons. These instructions are generated with nnan and in the
7400b57cec5SDimitry Andric// target-independent expansion of unordered comparisons and ordered ne.
741e8d8bef9SDimitry Andricforeach nodes = [[seteq, EQ_F32x4], [setne, NE_F32x4], [setlt, LT_F32x4],
742e8d8bef9SDimitry Andric                 [setgt, GT_F32x4], [setle, LE_F32x4], [setge, GE_F32x4]] in
7430b57cec5SDimitry Andricdef : Pat<(v4i32 (nodes[0] (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
744e8d8bef9SDimitry Andric          (nodes[1] $lhs, $rhs)>;
7450b57cec5SDimitry Andric
746e8d8bef9SDimitry Andricforeach nodes = [[seteq, EQ_F64x2], [setne, NE_F64x2], [setlt, LT_F64x2],
747e8d8bef9SDimitry Andric                 [setgt, GT_F64x2], [setle, LE_F64x2], [setge, GE_F64x2]] in
7480b57cec5SDimitry Andricdef : Pat<(v2i64 (nodes[0] (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
749e8d8bef9SDimitry Andric          (nodes[1] $lhs, $rhs)>;
750e8d8bef9SDimitry Andric
7510b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
7520b57cec5SDimitry Andric// Bitwise operations
7530b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
7540b57cec5SDimitry Andric
755fe6060f1SDimitry Andricmulticlass SIMDBinary<Vec vec, SDPatternOperator node, string name, bits<32> simdop> {
756e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
7570b57cec5SDimitry Andric                      (outs), (ins),
758e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst),
759e8d8bef9SDimitry Andric                        (node (vec.vt V128:$lhs), (vec.vt V128:$rhs)))],
760e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
761e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
7620b57cec5SDimitry Andric}
7630b57cec5SDimitry Andric
764fe6060f1SDimitry Andricmulticlass SIMDBitwise<SDPatternOperator node, string name, bits<32> simdop,
765fe6060f1SDimitry Andric                       bit commutable = false> {
766e8d8bef9SDimitry Andric  let isCommutable = commutable in
767e8d8bef9SDimitry Andric  defm "" : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
768e8d8bef9SDimitry Andric                   (outs), (ins), [],
769e8d8bef9SDimitry Andric                   "v128."#name#"\t$dst, $lhs, $rhs", "v128."#name, simdop>;
770e8d8bef9SDimitry Andric  foreach vec = IntVecs in
771e8d8bef9SDimitry Andric  def : Pat<(node (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
772e8d8bef9SDimitry Andric            (!cast<NI>(NAME) $lhs, $rhs)>;
7730b57cec5SDimitry Andric}
7740b57cec5SDimitry Andric
775fe6060f1SDimitry Andricmulticlass SIMDUnary<Vec vec, SDPatternOperator node, string name, bits<32> simdop> {
776e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins),
777e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst),
778e8d8bef9SDimitry Andric                        (vec.vt (node (vec.vt V128:$v))))],
779e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $v",
780e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
7810b57cec5SDimitry Andric}
7820b57cec5SDimitry Andric
7830b57cec5SDimitry Andric// Bitwise logic: v128.not
784e8d8bef9SDimitry Andricdefm NOT : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins), [],
785e8d8bef9SDimitry Andric                  "v128.not\t$dst, $v", "v128.not", 77>;
786e8d8bef9SDimitry Andricforeach vec = IntVecs in
787e8d8bef9SDimitry Andricdef : Pat<(vnot (vec.vt V128:$v)), (NOT $v)>;
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric// Bitwise logic: v128.and / v128.or / v128.xor
790e8d8bef9SDimitry Andricdefm AND : SIMDBitwise<and, "and", 78, true>;
791e8d8bef9SDimitry Andricdefm OR : SIMDBitwise<or, "or", 80, true>;
792e8d8bef9SDimitry Andricdefm XOR : SIMDBitwise<xor, "xor", 81, true>;
7930b57cec5SDimitry Andric
7948bcb0991SDimitry Andric// Bitwise logic: v128.andnot
7958bcb0991SDimitry Andricdef andnot : PatFrag<(ops node:$left, node:$right), (and $left, (vnot $right))>;
7965ffd83dbSDimitry Andricdefm ANDNOT : SIMDBitwise<andnot, "andnot", 79>;
7978bcb0991SDimitry Andric
7980b57cec5SDimitry Andric// Bitwise select: v128.bitselect
799e8d8bef9SDimitry Andricdefm BITSELECT :
800e8d8bef9SDimitry Andric  SIMD_I<(outs V128:$dst), (ins V128:$v1, V128:$v2, V128:$c), (outs), (ins), [],
8015ffd83dbSDimitry Andric         "v128.bitselect\t$dst, $v1, $v2, $c", "v128.bitselect", 82>;
8020b57cec5SDimitry Andric
803e8d8bef9SDimitry Andricforeach vec = AllVecs in
804e8d8bef9SDimitry Andricdef : Pat<(vec.vt (int_wasm_bitselect
805e8d8bef9SDimitry Andric            (vec.vt V128:$v1), (vec.vt V128:$v2), (vec.vt V128:$c))),
806e8d8bef9SDimitry Andric          (BITSELECT $v1, $v2, $c)>;
807e8d8bef9SDimitry Andric
8080b57cec5SDimitry Andric// Bitselect is equivalent to (c & v1) | (~c & v2)
809e8d8bef9SDimitry Andricforeach vec = IntVecs in
810e8d8bef9SDimitry Andricdef : Pat<(vec.vt (or (and (vec.vt V128:$c), (vec.vt V128:$v1)),
811e8d8bef9SDimitry Andric            (and (vnot V128:$c), (vec.vt V128:$v2)))),
812e8d8bef9SDimitry Andric          (BITSELECT $v1, $v2, $c)>;
813e8d8bef9SDimitry Andric
814e8d8bef9SDimitry Andric// Also implement vselect in terms of bitselect
815e8d8bef9SDimitry Andricforeach vec = AllVecs in
816e8d8bef9SDimitry Andricdef : Pat<(vec.vt (vselect
817e8d8bef9SDimitry Andric            (vec.int_vt V128:$c), (vec.vt V128:$v1), (vec.vt V128:$v2))),
818e8d8bef9SDimitry Andric          (BITSELECT $v1, $v2, $c)>;
819e8d8bef9SDimitry Andric
820e8d8bef9SDimitry Andric// MVP select on v128 values
821e8d8bef9SDimitry Andricdefm SELECT_V128 :
822e8d8bef9SDimitry Andric  I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, I32:$cond), (outs), (ins), [],
823e8d8bef9SDimitry Andric    "v128.select\t$dst, $lhs, $rhs, $cond", "v128.select", 0x1b>;
824e8d8bef9SDimitry Andric
825e8d8bef9SDimitry Andricforeach vec = AllVecs in {
826e8d8bef9SDimitry Andricdef : Pat<(select I32:$cond, (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
827e8d8bef9SDimitry Andric          (SELECT_V128 $lhs, $rhs, $cond)>;
828e8d8bef9SDimitry Andric
829e8d8bef9SDimitry Andric// ISD::SELECT requires its operand to conform to getBooleanContents, but
830e8d8bef9SDimitry Andric// WebAssembly's select interprets any non-zero value as true, so we can fold
831e8d8bef9SDimitry Andric// a setne with 0 into a select.
832e8d8bef9SDimitry Andricdef : Pat<(select
833e8d8bef9SDimitry Andric            (i32 (setne I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
834e8d8bef9SDimitry Andric          (SELECT_V128 $lhs, $rhs, $cond)>;
835e8d8bef9SDimitry Andric
836e8d8bef9SDimitry Andric// And again, this time with seteq instead of setne and the arms reversed.
837e8d8bef9SDimitry Andricdef : Pat<(select
838e8d8bef9SDimitry Andric            (i32 (seteq I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
839e8d8bef9SDimitry Andric          (SELECT_V128 $rhs, $lhs, $cond)>;
840e8d8bef9SDimitry Andric} // foreach vec
841e8d8bef9SDimitry Andric
8420b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8430b57cec5SDimitry Andric// Integer unary arithmetic
8440b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8450b57cec5SDimitry Andric
846fe6060f1SDimitry Andricmulticlass SIMDUnaryInt<SDPatternOperator node, string name, bits<32> baseInst> {
847e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I8x16, node, name, baseInst>;
848e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I16x8, node, name, !add(baseInst, 32)>;
849e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I32x4, node, name, !add(baseInst, 64)>;
850e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I64x2, node, name, !add(baseInst, 96)>;
8510b57cec5SDimitry Andric}
8520b57cec5SDimitry Andric
8530b57cec5SDimitry Andric// Integer vector negation
854e8d8bef9SDimitry Andricdef ivneg : PatFrag<(ops node:$in), (sub immAllZerosV, $in)>;
8550b57cec5SDimitry Andric
8565ffd83dbSDimitry Andric// Integer absolute value: abs
8575ffd83dbSDimitry Andricdefm ABS : SIMDUnaryInt<abs, "abs", 96>;
8585ffd83dbSDimitry Andric
8590b57cec5SDimitry Andric// Integer negation: neg
8605ffd83dbSDimitry Andricdefm NEG : SIMDUnaryInt<ivneg, "neg", 97>;
8610b57cec5SDimitry Andric
862fe6060f1SDimitry Andric// Population count: popcnt
863fe6060f1SDimitry Andricdefm POPCNT : SIMDUnary<I8x16, ctpop, "popcnt", 0x62>;
864fe6060f1SDimitry Andric
8650b57cec5SDimitry Andric// Any lane true: any_true
866fe6060f1SDimitry Andricdefm ANYTRUE : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins), [],
867fe6060f1SDimitry Andric                      "v128.any_true\t$dst, $vec", "v128.any_true", 0x53>;
868fe6060f1SDimitry Andric
869fe6060f1SDimitry Andricforeach vec = IntVecs in
870fe6060f1SDimitry Andricdef : Pat<(int_wasm_anytrue (vec.vt V128:$vec)), (ANYTRUE V128:$vec)>;
8710b57cec5SDimitry Andric
8720b57cec5SDimitry Andric// All lanes true: all_true
873fe6060f1SDimitry Andricmulticlass SIMDAllTrue<Vec vec, bits<32> simdop> {
874fe6060f1SDimitry Andric  defm ALLTRUE_#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
875fe6060f1SDimitry Andric                             [(set I32:$dst,
876fe6060f1SDimitry Andric                               (i32 (int_wasm_alltrue (vec.vt V128:$vec))))],
877fe6060f1SDimitry Andric                             vec.prefix#".all_true\t$dst, $vec",
878fe6060f1SDimitry Andric                             vec.prefix#".all_true", simdop>;
879fe6060f1SDimitry Andric}
8800b57cec5SDimitry Andric
881fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I8x16, 0x63>;
882fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I16x8, 0x83>;
883fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I32x4, 0xa3>;
884fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I64x2, 0xc3>;
885e8d8bef9SDimitry Andric
8860b57cec5SDimitry Andric// Reductions already return 0 or 1, so and 1, setne 0, and seteq 1
8870b57cec5SDimitry Andric// can be folded out
8880b57cec5SDimitry Andricforeach reduction =
889fe6060f1SDimitry Andric  [["int_wasm_anytrue", "ANYTRUE", "I8x16"],
890fe6060f1SDimitry Andric   ["int_wasm_anytrue", "ANYTRUE", "I16x8"],
891fe6060f1SDimitry Andric   ["int_wasm_anytrue", "ANYTRUE", "I32x4"],
892fe6060f1SDimitry Andric   ["int_wasm_anytrue", "ANYTRUE", "I64x2"],
893fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I8x16", "I8x16"],
894fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I16x8", "I16x8"],
895fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I32x4", "I32x4"],
896fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I64x2", "I64x2"]] in {
897e8d8bef9SDimitry Andricdefvar intrinsic = !cast<Intrinsic>(reduction[0]);
898fe6060f1SDimitry Andricdefvar inst = !cast<NI>(reduction[1]);
899fe6060f1SDimitry Andricdefvar vec = !cast<Vec>(reduction[2]);
900e8d8bef9SDimitry Andricdef : Pat<(i32 (and (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>;
901e8d8bef9SDimitry Andricdef : Pat<(i32 (setne (i32 (intrinsic (vec.vt V128:$x))), (i32 0))), (inst $x)>;
902e8d8bef9SDimitry Andricdef : Pat<(i32 (seteq (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>;
9030b57cec5SDimitry Andric}
9040b57cec5SDimitry Andric
905e8d8bef9SDimitry Andricmulticlass SIMDBitmask<Vec vec, bits<32> simdop> {
906e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
9075ffd83dbSDimitry Andric                      [(set I32:$dst,
908e8d8bef9SDimitry Andric                         (i32 (int_wasm_bitmask (vec.vt V128:$vec))))],
909e8d8bef9SDimitry Andric                      vec.prefix#".bitmask\t$dst, $vec", vec.prefix#".bitmask",
910e8d8bef9SDimitry Andric                      simdop>;
9115ffd83dbSDimitry Andric}
9125ffd83dbSDimitry Andric
913e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I8x16, 100>;
914e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I16x8, 132>;
915e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I32x4, 164>;
916e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I64x2, 196>;
9175ffd83dbSDimitry Andric
9180b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9190b57cec5SDimitry Andric// Bit shifts
9200b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9210b57cec5SDimitry Andric
922e8d8bef9SDimitry Andricmulticlass SIMDShift<Vec vec, SDNode node, string name, bits<32> simdop> {
923e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$vec, I32:$x), (outs), (ins),
924e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst), (node V128:$vec, I32:$x))],
925e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $vec, $x",
926e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
9270b57cec5SDimitry Andric}
9280b57cec5SDimitry Andric
9290b57cec5SDimitry Andricmulticlass SIMDShiftInt<SDNode node, string name, bits<32> baseInst> {
930e8d8bef9SDimitry Andric  defm "" : SIMDShift<I8x16, node, name, baseInst>;
931e8d8bef9SDimitry Andric  defm "" : SIMDShift<I16x8, node, name, !add(baseInst, 32)>;
932e8d8bef9SDimitry Andric  defm "" : SIMDShift<I32x4, node, name, !add(baseInst, 64)>;
933e8d8bef9SDimitry Andric  defm "" : SIMDShift<I64x2, node, name, !add(baseInst, 96)>;
9340b57cec5SDimitry Andric}
9350b57cec5SDimitry Andric
9365ffd83dbSDimitry Andric// WebAssembly SIMD shifts are nonstandard in that the shift amount is
9375ffd83dbSDimitry Andric// an i32 rather than a vector, so they need custom nodes.
938e8d8bef9SDimitry Andricdef wasm_shift_t :
939e8d8bef9SDimitry Andric  SDTypeProfile<1, 2, [SDTCisVec<0>, SDTCisSameAs<0, 1>, SDTCisVT<2, i32>]>;
9400b57cec5SDimitry Andricdef wasm_shl : SDNode<"WebAssemblyISD::VEC_SHL", wasm_shift_t>;
9410b57cec5SDimitry Andricdef wasm_shr_s : SDNode<"WebAssemblyISD::VEC_SHR_S", wasm_shift_t>;
9420b57cec5SDimitry Andricdef wasm_shr_u : SDNode<"WebAssemblyISD::VEC_SHR_U", wasm_shift_t>;
9435ffd83dbSDimitry Andric
9445ffd83dbSDimitry Andric// Left shift by scalar: shl
9455ffd83dbSDimitry Andricdefm SHL : SIMDShiftInt<wasm_shl, "shl", 107>;
9465ffd83dbSDimitry Andric
9475ffd83dbSDimitry Andric// Right shift by scalar: shr_s / shr_u
9485ffd83dbSDimitry Andricdefm SHR_S : SIMDShiftInt<wasm_shr_s, "shr_s", 108>;
9495ffd83dbSDimitry Andricdefm SHR_U : SIMDShiftInt<wasm_shr_u, "shr_u", 109>;
9500b57cec5SDimitry Andric
951fe6060f1SDimitry Andric// Optimize away an explicit mask on a shift count.
952fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v16i8 V128:$lhs), (and I32:$rhs, 7)),
953fe6060f1SDimitry Andric          (SHL_I8x16 V128:$lhs, I32:$rhs)>;
954fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v16i8 V128:$lhs), (and I32:$rhs, 7)),
955fe6060f1SDimitry Andric          (SHR_S_I8x16 V128:$lhs, I32:$rhs)>;
956fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v16i8 V128:$lhs), (and I32:$rhs, 7)),
957fe6060f1SDimitry Andric          (SHR_U_I8x16 V128:$lhs, I32:$rhs)>;
958fe6060f1SDimitry Andric
959fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v8i16 V128:$lhs), (and I32:$rhs, 15)),
960fe6060f1SDimitry Andric          (SHL_I16x8 V128:$lhs, I32:$rhs)>;
961fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v8i16 V128:$lhs), (and I32:$rhs, 15)),
962fe6060f1SDimitry Andric          (SHR_S_I16x8 V128:$lhs, I32:$rhs)>;
963fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v8i16 V128:$lhs), (and I32:$rhs, 15)),
964fe6060f1SDimitry Andric          (SHR_U_I16x8 V128:$lhs, I32:$rhs)>;
965fe6060f1SDimitry Andric
966fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v4i32 V128:$lhs), (and I32:$rhs, 31)),
967fe6060f1SDimitry Andric          (SHL_I32x4 V128:$lhs, I32:$rhs)>;
968fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v4i32 V128:$lhs), (and I32:$rhs, 31)),
969fe6060f1SDimitry Andric          (SHR_S_I32x4 V128:$lhs, I32:$rhs)>;
970fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v4i32 V128:$lhs), (and I32:$rhs, 31)),
971fe6060f1SDimitry Andric          (SHR_U_I32x4 V128:$lhs, I32:$rhs)>;
972fe6060f1SDimitry Andric
973fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
974fe6060f1SDimitry Andric          (SHL_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
975fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
976fe6060f1SDimitry Andric          (SHR_S_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
977fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
978fe6060f1SDimitry Andric          (SHR_U_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
979fe6060f1SDimitry Andric
9800b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9810b57cec5SDimitry Andric// Integer binary arithmetic
9820b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9830b57cec5SDimitry Andric
984fe6060f1SDimitry Andricmulticlass SIMDBinaryIntNoI8x16<SDPatternOperator node, string name, bits<32> baseInst> {
985e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>;
986e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>;
987e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>;
9885ffd83dbSDimitry Andric}
9895ffd83dbSDimitry Andric
990fe6060f1SDimitry Andricmulticlass SIMDBinaryIntSmall<SDPatternOperator node, string name, bits<32> baseInst> {
991e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I8x16, node, name, baseInst>;
992e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>;
9930b57cec5SDimitry Andric}
9940b57cec5SDimitry Andric
995fe6060f1SDimitry Andricmulticlass SIMDBinaryIntNoI64x2<SDPatternOperator node, string name, bits<32> baseInst> {
9960b57cec5SDimitry Andric  defm "" : SIMDBinaryIntSmall<node, name, baseInst>;
997e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>;
9980b57cec5SDimitry Andric}
9990b57cec5SDimitry Andric
1000fe6060f1SDimitry Andricmulticlass SIMDBinaryInt<SDPatternOperator node, string name, bits<32> baseInst> {
10010b57cec5SDimitry Andric  defm "" : SIMDBinaryIntNoI64x2<node, name, baseInst>;
1002e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>;
10030b57cec5SDimitry Andric}
10040b57cec5SDimitry Andric
1005fe6060f1SDimitry Andric// Integer addition: add / add_sat_s / add_sat_u
10060b57cec5SDimitry Andriclet isCommutable = 1 in {
10075ffd83dbSDimitry Andricdefm ADD : SIMDBinaryInt<add, "add", 110>;
1008fe6060f1SDimitry Andricdefm ADD_SAT_S : SIMDBinaryIntSmall<saddsat, "add_sat_s", 111>;
1009fe6060f1SDimitry Andricdefm ADD_SAT_U : SIMDBinaryIntSmall<uaddsat, "add_sat_u", 112>;
10100b57cec5SDimitry Andric} // isCommutable = 1
10110b57cec5SDimitry Andric
1012fe6060f1SDimitry Andric// Integer subtraction: sub / sub_sat_s / sub_sat_u
10135ffd83dbSDimitry Andricdefm SUB : SIMDBinaryInt<sub, "sub", 113>;
10140b57cec5SDimitry Andricdefm SUB_SAT_S :
1015fe6060f1SDimitry Andric  SIMDBinaryIntSmall<int_wasm_sub_sat_signed, "sub_sat_s", 114>;
10160b57cec5SDimitry Andricdefm SUB_SAT_U :
1017fe6060f1SDimitry Andric  SIMDBinaryIntSmall<int_wasm_sub_sat_unsigned, "sub_sat_u", 115>;
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andric// Integer multiplication: mul
1020480093f4SDimitry Andriclet isCommutable = 1 in
10215ffd83dbSDimitry Andricdefm MUL : SIMDBinaryIntNoI8x16<mul, "mul", 117>;
10220b57cec5SDimitry Andric
1023480093f4SDimitry Andric// Integer min_s / min_u / max_s / max_u
1024480093f4SDimitry Andriclet isCommutable = 1 in {
10255ffd83dbSDimitry Andricdefm MIN_S : SIMDBinaryIntNoI64x2<smin, "min_s", 118>;
10265ffd83dbSDimitry Andricdefm MIN_U : SIMDBinaryIntNoI64x2<umin, "min_u", 119>;
10275ffd83dbSDimitry Andricdefm MAX_S : SIMDBinaryIntNoI64x2<smax, "max_s", 120>;
10285ffd83dbSDimitry Andricdefm MAX_U : SIMDBinaryIntNoI64x2<umax, "max_u", 121>;
1029480093f4SDimitry Andric} // isCommutable = 1
1030480093f4SDimitry Andric
1031480093f4SDimitry Andric// Integer unsigned rounding average: avgr_u
10325ffd83dbSDimitry Andriclet isCommutable = 1 in {
1033e8d8bef9SDimitry Andricdefm AVGR_U : SIMDBinary<I8x16, int_wasm_avgr_unsigned, "avgr_u", 123>;
1034e8d8bef9SDimitry Andricdefm AVGR_U : SIMDBinary<I16x8, int_wasm_avgr_unsigned, "avgr_u", 155>;
1035480093f4SDimitry Andric}
1036480093f4SDimitry Andric
1037e8d8bef9SDimitry Andricdef add_nuw : PatFrag<(ops node:$lhs, node:$rhs), (add $lhs, $rhs),
1038480093f4SDimitry Andric                      "return N->getFlags().hasNoUnsignedWrap();">;
1039480093f4SDimitry Andric
1040e8d8bef9SDimitry Andricforeach vec = [I8x16, I16x8] in {
1041e8d8bef9SDimitry Andricdefvar inst = !cast<NI>("AVGR_U_"#vec);
10425ffd83dbSDimitry Andricdef : Pat<(wasm_shr_u
1043480093f4SDimitry Andric            (add_nuw
1044e8d8bef9SDimitry Andric              (add_nuw (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
1045e8d8bef9SDimitry Andric              (vec.splat (i32 1))),
1046e8d8bef9SDimitry Andric            (i32 1)),
1047e8d8bef9SDimitry Andric          (inst $lhs, $rhs)>;
1048e8d8bef9SDimitry Andric}
1049480093f4SDimitry Andric
1050480093f4SDimitry Andric// Widening dot product: i32x4.dot_i16x8_s
1051480093f4SDimitry Andriclet isCommutable = 1 in
1052480093f4SDimitry Andricdefm DOT : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
1053480093f4SDimitry Andric                  [(set V128:$dst, (int_wasm_dot V128:$lhs, V128:$rhs))],
1054480093f4SDimitry Andric                  "i32x4.dot_i16x8_s\t$dst, $lhs, $rhs", "i32x4.dot_i16x8_s",
1055e8d8bef9SDimitry Andric                  186>;
1056e8d8bef9SDimitry Andric
1057e8d8bef9SDimitry Andric// Extending multiplication: extmul_{low,high}_P, extmul_high
1058fe6060f1SDimitry Andricdef extend_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1059fe6060f1SDimitry Andricdef extend_low_s : SDNode<"WebAssemblyISD::EXTEND_LOW_S", extend_t>;
1060fe6060f1SDimitry Andricdef extend_high_s : SDNode<"WebAssemblyISD::EXTEND_HIGH_S", extend_t>;
1061fe6060f1SDimitry Andricdef extend_low_u : SDNode<"WebAssemblyISD::EXTEND_LOW_U", extend_t>;
1062fe6060f1SDimitry Andricdef extend_high_u : SDNode<"WebAssemblyISD::EXTEND_HIGH_U", extend_t>;
1063fe6060f1SDimitry Andric
1064fe6060f1SDimitry Andricmulticlass SIMDExtBinary<Vec vec, SDPatternOperator node, string name,
1065fe6060f1SDimitry Andric                         bits<32> simdop> {
1066e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
1067e8d8bef9SDimitry Andric                      (outs), (ins),
1068e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst), (node
1069e8d8bef9SDimitry Andric                         (vec.split.vt V128:$lhs),(vec.split.vt V128:$rhs)))],
1070e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
1071e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
1072e8d8bef9SDimitry Andric}
1073e8d8bef9SDimitry Andric
1074fe6060f1SDimitry Andricclass ExtMulPat<SDNode extend> :
1075fe6060f1SDimitry Andric  PatFrag<(ops node:$lhs, node:$rhs),
1076fe6060f1SDimitry Andric          (mul (extend $lhs), (extend $rhs))> {}
1077fe6060f1SDimitry Andric
1078fe6060f1SDimitry Andricdef extmul_low_s : ExtMulPat<extend_low_s>;
1079fe6060f1SDimitry Andricdef extmul_high_s : ExtMulPat<extend_high_s>;
1080fe6060f1SDimitry Andricdef extmul_low_u : ExtMulPat<extend_low_u>;
1081fe6060f1SDimitry Andricdef extmul_high_u : ExtMulPat<extend_high_u>;
1082e8d8bef9SDimitry Andric
1083e8d8bef9SDimitry Andricdefm EXTMUL_LOW_S :
1084fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_low_s, "extmul_low_i8x16_s", 0x9c>;
1085e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_S :
1086fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_high_s, "extmul_high_i8x16_s", 0x9d>;
1087e8d8bef9SDimitry Andricdefm EXTMUL_LOW_U :
1088fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_low_u, "extmul_low_i8x16_u", 0x9e>;
1089e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_U :
1090fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_high_u, "extmul_high_i8x16_u", 0x9f>;
1091e8d8bef9SDimitry Andric
1092e8d8bef9SDimitry Andricdefm EXTMUL_LOW_S :
1093fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_low_s, "extmul_low_i16x8_s", 0xbc>;
1094e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_S :
1095fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_high_s, "extmul_high_i16x8_s", 0xbd>;
1096e8d8bef9SDimitry Andricdefm EXTMUL_LOW_U :
1097fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_low_u, "extmul_low_i16x8_u", 0xbe>;
1098e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_U :
1099fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_high_u, "extmul_high_i16x8_u", 0xbf>;
1100fe6060f1SDimitry Andric
1101fe6060f1SDimitry Andricdefm EXTMUL_LOW_S :
1102fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_low_s, "extmul_low_i32x4_s", 0xdc>;
1103fe6060f1SDimitry Andricdefm EXTMUL_HIGH_S :
1104fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_high_s, "extmul_high_i32x4_s", 0xdd>;
1105fe6060f1SDimitry Andricdefm EXTMUL_LOW_U :
1106fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_low_u, "extmul_low_i32x4_u", 0xde>;
1107fe6060f1SDimitry Andricdefm EXTMUL_HIGH_U :
1108fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_high_u, "extmul_high_i32x4_u", 0xdf>;
1109480093f4SDimitry Andric
11100b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11110b57cec5SDimitry Andric// Floating-point unary arithmetic
11120b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11130b57cec5SDimitry Andric
11140b57cec5SDimitry Andricmulticlass SIMDUnaryFP<SDNode node, string name, bits<32> baseInst> {
1115e8d8bef9SDimitry Andric  defm "" : SIMDUnary<F32x4, node, name, baseInst>;
1116e8d8bef9SDimitry Andric  defm "" : SIMDUnary<F64x2, node, name, !add(baseInst, 12)>;
11170b57cec5SDimitry Andric}
11180b57cec5SDimitry Andric
11190b57cec5SDimitry Andric// Absolute value: abs
11205ffd83dbSDimitry Andricdefm ABS : SIMDUnaryFP<fabs, "abs", 224>;
11210b57cec5SDimitry Andric
11220b57cec5SDimitry Andric// Negation: neg
11235ffd83dbSDimitry Andricdefm NEG : SIMDUnaryFP<fneg, "neg", 225>;
11240b57cec5SDimitry Andric
11250b57cec5SDimitry Andric// Square root: sqrt
11265ffd83dbSDimitry Andricdefm SQRT : SIMDUnaryFP<fsqrt, "sqrt", 227>;
11275ffd83dbSDimitry Andric
11285ffd83dbSDimitry Andric// Rounding: ceil, floor, trunc, nearest
1129fe6060f1SDimitry Andricdefm CEIL : SIMDUnary<F32x4, fceil, "ceil", 0x67>;
1130fe6060f1SDimitry Andricdefm FLOOR : SIMDUnary<F32x4, ffloor, "floor", 0x68>;
1131fe6060f1SDimitry Andricdefm TRUNC: SIMDUnary<F32x4, ftrunc, "trunc", 0x69>;
1132fe6060f1SDimitry Andricdefm NEAREST: SIMDUnary<F32x4, fnearbyint, "nearest", 0x6a>;
1133fe6060f1SDimitry Andricdefm CEIL : SIMDUnary<F64x2, fceil, "ceil", 0x74>;
1134fe6060f1SDimitry Andricdefm FLOOR : SIMDUnary<F64x2, ffloor, "floor", 0x75>;
1135fe6060f1SDimitry Andricdefm TRUNC: SIMDUnary<F64x2, ftrunc, "trunc", 0x7a>;
1136fe6060f1SDimitry Andricdefm NEAREST: SIMDUnary<F64x2, fnearbyint, "nearest", 0x94>;
11370b57cec5SDimitry Andric
11380b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11390b57cec5SDimitry Andric// Floating-point binary arithmetic
11400b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11410b57cec5SDimitry Andric
1142fe6060f1SDimitry Andricmulticlass SIMDBinaryFP<SDPatternOperator node, string name, bits<32> baseInst> {
1143e8d8bef9SDimitry Andric  defm "" : SIMDBinary<F32x4, node, name, baseInst>;
1144e8d8bef9SDimitry Andric  defm "" : SIMDBinary<F64x2, node, name, !add(baseInst, 12)>;
11450b57cec5SDimitry Andric}
11460b57cec5SDimitry Andric
11470b57cec5SDimitry Andric// Addition: add
11480b57cec5SDimitry Andriclet isCommutable = 1 in
11495ffd83dbSDimitry Andricdefm ADD : SIMDBinaryFP<fadd, "add", 228>;
11500b57cec5SDimitry Andric
11510b57cec5SDimitry Andric// Subtraction: sub
11525ffd83dbSDimitry Andricdefm SUB : SIMDBinaryFP<fsub, "sub", 229>;
11530b57cec5SDimitry Andric
11540b57cec5SDimitry Andric// Multiplication: mul
11550b57cec5SDimitry Andriclet isCommutable = 1 in
11565ffd83dbSDimitry Andricdefm MUL : SIMDBinaryFP<fmul, "mul", 230>;
11570b57cec5SDimitry Andric
11580b57cec5SDimitry Andric// Division: div
11595ffd83dbSDimitry Andricdefm DIV : SIMDBinaryFP<fdiv, "div", 231>;
11600b57cec5SDimitry Andric
11610b57cec5SDimitry Andric// NaN-propagating minimum: min
11625ffd83dbSDimitry Andricdefm MIN : SIMDBinaryFP<fminimum, "min", 232>;
11630b57cec5SDimitry Andric
11640b57cec5SDimitry Andric// NaN-propagating maximum: max
11655ffd83dbSDimitry Andricdefm MAX : SIMDBinaryFP<fmaximum, "max", 233>;
11665ffd83dbSDimitry Andric
11675ffd83dbSDimitry Andric// Pseudo-minimum: pmin
1168fe6060f1SDimitry Andricdef pmin : PatFrag<(ops node:$lhs, node:$rhs),
1169fe6060f1SDimitry Andric                   (vselect (setolt $rhs, $lhs), $rhs, $lhs)>;
1170fe6060f1SDimitry Andricdefm PMIN : SIMDBinaryFP<pmin, "pmin", 234>;
11715ffd83dbSDimitry Andric
11725ffd83dbSDimitry Andric// Pseudo-maximum: pmax
1173fe6060f1SDimitry Andricdef pmax : PatFrag<(ops node:$lhs, node:$rhs),
1174fe6060f1SDimitry Andric                   (vselect (setolt $lhs, $rhs), $rhs, $lhs)>;
1175fe6060f1SDimitry Andricdefm PMAX : SIMDBinaryFP<pmax, "pmax", 235>;
1176fe6060f1SDimitry Andric
1177fe6060f1SDimitry Andric// Also match the pmin/pmax cases where the operands are int vectors (but the
1178fe6060f1SDimitry Andric// comparison is still a floating point comparison). This can happen when using
1179fe6060f1SDimitry Andric// the wasm_simd128.h intrinsics because v128_t is an integer vector.
1180fe6060f1SDimitry Andricforeach vec = [F32x4, F64x2] in {
1181fe6060f1SDimitry Andricdefvar pmin = !cast<NI>("PMIN_"#vec);
1182fe6060f1SDimitry Andricdefvar pmax = !cast<NI>("PMAX_"#vec);
1183fe6060f1SDimitry Andricdef : Pat<(vec.int_vt (vselect
1184fe6060f1SDimitry Andric            (setolt (vec.vt (bitconvert V128:$rhs)),
1185fe6060f1SDimitry Andric                    (vec.vt (bitconvert V128:$lhs))),
1186fe6060f1SDimitry Andric            V128:$rhs, V128:$lhs)),
1187fe6060f1SDimitry Andric          (pmin $lhs, $rhs)>;
1188fe6060f1SDimitry Andricdef : Pat<(vec.int_vt (vselect
1189fe6060f1SDimitry Andric            (setolt (vec.vt (bitconvert V128:$lhs)),
1190fe6060f1SDimitry Andric                    (vec.vt (bitconvert V128:$rhs))),
1191fe6060f1SDimitry Andric            V128:$rhs, V128:$lhs)),
1192fe6060f1SDimitry Andric          (pmax $lhs, $rhs)>;
1193fe6060f1SDimitry Andric}
11940b57cec5SDimitry Andric
1195349cc55cSDimitry Andric// And match the pmin/pmax LLVM intrinsics as well
1196349cc55cSDimitry Andricdef : Pat<(v4f32 (int_wasm_pmin (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
1197349cc55cSDimitry Andric          (PMIN_F32x4 V128:$lhs, V128:$rhs)>;
1198349cc55cSDimitry Andricdef : Pat<(v4f32 (int_wasm_pmax (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
1199349cc55cSDimitry Andric          (PMAX_F32x4 V128:$lhs, V128:$rhs)>;
1200349cc55cSDimitry Andricdef : Pat<(v2f64 (int_wasm_pmin (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
1201349cc55cSDimitry Andric          (PMIN_F64x2 V128:$lhs, V128:$rhs)>;
1202349cc55cSDimitry Andricdef : Pat<(v2f64 (int_wasm_pmax (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
1203349cc55cSDimitry Andric          (PMAX_F64x2 V128:$lhs, V128:$rhs)>;
1204349cc55cSDimitry Andric
12050b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
12060b57cec5SDimitry Andric// Conversions
12070b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
12080b57cec5SDimitry Andric
1209fe6060f1SDimitry Andricmulticlass SIMDConvert<Vec vec, Vec arg, SDPatternOperator op, string name,
1210e8d8bef9SDimitry Andric                       bits<32> simdop> {
1211e8d8bef9SDimitry Andric  defm op#_#vec :
12120b57cec5SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins),
1213e8d8bef9SDimitry Andric           [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))],
1214e8d8bef9SDimitry Andric           vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop>;
12150b57cec5SDimitry Andric}
12160b57cec5SDimitry Andric
12170b57cec5SDimitry Andric// Floating point to integer with saturation: trunc_sat
1218e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, F32x4, fp_to_sint, "trunc_sat_f32x4_s", 248>;
1219e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, F32x4, fp_to_uint, "trunc_sat_f32x4_u", 249>;
12205ffd83dbSDimitry Andric
1221fe6060f1SDimitry Andric// Support the saturating variety as well.
1222fe6060f1SDimitry Andricdef trunc_s_sat32 : PatFrag<(ops node:$x), (fp_to_sint_sat $x, i32)>;
1223fe6060f1SDimitry Andricdef trunc_u_sat32 : PatFrag<(ops node:$x), (fp_to_uint_sat $x, i32)>;
1224fe6060f1SDimitry Andricdef : Pat<(v4i32 (trunc_s_sat32 (v4f32 V128:$src))), (fp_to_sint_I32x4 $src)>;
1225fe6060f1SDimitry Andricdef : Pat<(v4i32 (trunc_u_sat32 (v4f32 V128:$src))), (fp_to_uint_I32x4 $src)>;
1226fe6060f1SDimitry Andric
1227fe6060f1SDimitry Andricdef trunc_sat_zero_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1228fe6060f1SDimitry Andricdef trunc_sat_zero_s :
1229fe6060f1SDimitry Andric  SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_S", trunc_sat_zero_t>;
1230fe6060f1SDimitry Andricdef trunc_sat_zero_u :
1231fe6060f1SDimitry Andric  SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_U", trunc_sat_zero_t>;
1232fe6060f1SDimitry Andricdefm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_s, "trunc_sat_zero_f64x2_s",
1233fe6060f1SDimitry Andric                      0xfc>;
1234fe6060f1SDimitry Andricdefm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_u, "trunc_sat_zero_f64x2_u",
1235fe6060f1SDimitry Andric                      0xfd>;
1236fe6060f1SDimitry Andric
12375ffd83dbSDimitry Andric// Integer to floating point: convert
1238fe6060f1SDimitry Andricdef convert_low_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1239fe6060f1SDimitry Andricdef convert_low_s : SDNode<"WebAssemblyISD::CONVERT_LOW_S", convert_low_t>;
1240fe6060f1SDimitry Andricdef convert_low_u : SDNode<"WebAssemblyISD::CONVERT_LOW_U", convert_low_t>;
1241e8d8bef9SDimitry Andricdefm "" : SIMDConvert<F32x4, I32x4, sint_to_fp, "convert_i32x4_s", 250>;
1242e8d8bef9SDimitry Andricdefm "" : SIMDConvert<F32x4, I32x4, uint_to_fp, "convert_i32x4_u", 251>;
1243fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, I32x4, convert_low_s, "convert_low_i32x4_s", 0xfe>;
1244fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, I32x4, convert_low_u, "convert_low_i32x4_u", 0xff>;
12458bcb0991SDimitry Andric
1246fe6060f1SDimitry Andric// Extending operations
1247e8d8bef9SDimitry Andric// TODO: refactor this to be uniform for i64x2 if the numbering is not changed.
1248fe6060f1SDimitry Andricmulticlass SIMDExtend<Vec vec, bits<32> baseInst> {
1249fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_low_s,
1250fe6060f1SDimitry Andric                        "extend_low_"#vec.split.prefix#"_s", baseInst>;
1251fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_high_s,
1252fe6060f1SDimitry Andric                        "extend_high_"#vec.split.prefix#"_s", !add(baseInst, 1)>;
1253fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_low_u,
1254fe6060f1SDimitry Andric                        "extend_low_"#vec.split.prefix#"_u", !add(baseInst, 2)>;
1255fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_high_u,
1256fe6060f1SDimitry Andric                        "extend_high_"#vec.split.prefix#"_u", !add(baseInst, 3)>;
1257e8d8bef9SDimitry Andric}
1258e8d8bef9SDimitry Andric
1259fe6060f1SDimitry Andricdefm "" : SIMDExtend<I16x8, 0x87>;
1260fe6060f1SDimitry Andricdefm "" : SIMDExtend<I32x4, 0xa7>;
1261fe6060f1SDimitry Andricdefm "" : SIMDExtend<I64x2, 0xc7>;
1262e8d8bef9SDimitry Andric
1263e8d8bef9SDimitry Andric// Narrowing operations
1264e8d8bef9SDimitry Andricmulticlass SIMDNarrow<Vec vec, bits<32> baseInst> {
1265e8d8bef9SDimitry Andric  defvar name = vec.split.prefix#".narrow_"#vec.prefix;
1266e8d8bef9SDimitry Andric  defm NARROW_S_#vec.split :
1267e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins),
1268e8d8bef9SDimitry Andric           [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_signed
1269e8d8bef9SDimitry Andric             (vec.vt V128:$low), (vec.vt V128:$high))))],
1270e8d8bef9SDimitry Andric           name#"_s\t$dst, $low, $high", name#"_s", baseInst>;
1271e8d8bef9SDimitry Andric  defm NARROW_U_#vec.split :
1272e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins),
1273e8d8bef9SDimitry Andric           [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_unsigned
1274e8d8bef9SDimitry Andric             (vec.vt V128:$low), (vec.vt V128:$high))))],
1275e8d8bef9SDimitry Andric           name#"_u\t$dst, $low, $high", name#"_u", !add(baseInst, 1)>;
1276e8d8bef9SDimitry Andric}
1277e8d8bef9SDimitry Andric
1278e8d8bef9SDimitry Andricdefm "" : SIMDNarrow<I16x8, 101>;
1279e8d8bef9SDimitry Andricdefm "" : SIMDNarrow<I32x4, 133>;
1280e8d8bef9SDimitry Andric
1281*0eae32dcSDimitry Andric// WebAssemblyISD::NARROW_U
1282*0eae32dcSDimitry Andricdef wasm_narrow_t : SDTypeProfile<1, 2, []>;
1283*0eae32dcSDimitry Andricdef wasm_narrow_u : SDNode<"WebAssemblyISD::NARROW_U", wasm_narrow_t>;
1284*0eae32dcSDimitry Andricdef : Pat<(v16i8 (wasm_narrow_u (v8i16 V128:$left), (v8i16 V128:$right))),
1285*0eae32dcSDimitry Andric          (NARROW_U_I8x16 $left, $right)>;
1286*0eae32dcSDimitry Andricdef : Pat<(v8i16 (wasm_narrow_u (v4i32 V128:$left), (v4i32 V128:$right))),
1287*0eae32dcSDimitry Andric          (NARROW_U_I16x8 $left, $right)>;
1288*0eae32dcSDimitry Andric
12890b57cec5SDimitry Andric// Bitcasts are nops
12900b57cec5SDimitry Andric// Matching bitcast t1 to t1 causes strange errors, so avoid repeating types
1291fe6060f1SDimitry Andricforeach t1 = AllVecs in
1292fe6060f1SDimitry Andricforeach t2 = AllVecs in
1293fe6060f1SDimitry Andricif !ne(t1, t2) then
1294fe6060f1SDimitry Andricdef : Pat<(t1.vt (bitconvert (t2.vt V128:$v))), (t1.vt V128:$v)>;
12958bcb0991SDimitry Andric
1296e8d8bef9SDimitry Andric// Extended pairwise addition
1297e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_signed,
1298fe6060f1SDimitry Andric                      "extadd_pairwise_i8x16_s", 0x7c>;
1299e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_unsigned,
1300fe6060f1SDimitry Andric                      "extadd_pairwise_i8x16_u", 0x7d>;
1301e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_signed,
1302fe6060f1SDimitry Andric                      "extadd_pairwise_i16x8_s", 0x7e>;
1303e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_unsigned,
1304fe6060f1SDimitry Andric                      "extadd_pairwise_i16x8_u", 0x7f>;
1305e8d8bef9SDimitry Andric
1306fe6060f1SDimitry Andric// f64x2 <-> f32x4 conversions
1307fe6060f1SDimitry Andricdef demote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1308fe6060f1SDimitry Andricdef demote_zero : SDNode<"WebAssemblyISD::DEMOTE_ZERO", demote_t>;
1309fe6060f1SDimitry Andricdefm "" : SIMDConvert<F32x4, F64x2, demote_zero,
1310fe6060f1SDimitry Andric                      "demote_zero_f64x2", 0x5e>;
1311e8d8bef9SDimitry Andric
1312fe6060f1SDimitry Andricdef promote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1313fe6060f1SDimitry Andricdef promote_low : SDNode<"WebAssemblyISD::PROMOTE_LOW", promote_t>;
1314fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, F32x4, promote_low, "promote_low_f32x4", 0x5f>;
1315e8d8bef9SDimitry Andric
1316349cc55cSDimitry Andric// Lower extending loads to load64_zero + promote_low
1317349cc55cSDimitry Andricdef extloadv2f32 : PatFrag<(ops node:$ptr), (extload node:$ptr)> {
1318349cc55cSDimitry Andric  let MemoryVT = v2f32;
1319349cc55cSDimitry Andric}
1320349cc55cSDimitry Andric// Adapted from the body of LoadPatNoOffset
1321349cc55cSDimitry Andric// TODO: other addressing patterns
1322349cc55cSDimitry Andricdef : Pat<(v2f64 (extloadv2f32 (i32 I32:$addr))),
1323349cc55cSDimitry Andric          (promote_low_F64x2 (LOAD_ZERO_I64x2_A32 0, 0, I32:$addr))>,
1324349cc55cSDimitry Andric      Requires<[HasAddr32]>;
1325349cc55cSDimitry Andricdef : Pat<(v2f64 (extloadv2f32 (i64 I64:$addr))),
1326349cc55cSDimitry Andric          (promote_low_F64x2 (LOAD_ZERO_I64x2_A64 0, 0, I64:$addr))>,
1327349cc55cSDimitry Andric      Requires<[HasAddr64]>;
1328349cc55cSDimitry Andric
1329e8d8bef9SDimitry Andric//===----------------------------------------------------------------------===//
1330e8d8bef9SDimitry Andric// Saturating Rounding Q-Format Multiplication
1331e8d8bef9SDimitry Andric//===----------------------------------------------------------------------===//
1332e8d8bef9SDimitry Andric
1333e8d8bef9SDimitry Andricdefm Q15MULR_SAT_S :
1334fe6060f1SDimitry Andric  SIMDBinary<I16x8, int_wasm_q15mulr_sat_signed, "q15mulr_sat_s", 0x82>;
1335349cc55cSDimitry Andric
1336349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1337349cc55cSDimitry Andric// Fused Multiply- Add and Subtract (FMA/FMS)
1338349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1339349cc55cSDimitry Andric
1340349cc55cSDimitry Andricmulticlass SIMDFM<Vec vec, bits<32> simdopA, bits<32> simdopS> {
1341349cc55cSDimitry Andric  defm FMA_#vec :
1342349cc55cSDimitry Andric    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins),
1343349cc55cSDimitry Andric              [(set (vec.vt V128:$dst), (int_wasm_fma
1344349cc55cSDimitry Andric                (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))],
1345349cc55cSDimitry Andric              vec.prefix#".fma\t$dst, $a, $b, $c", vec.prefix#".fma", simdopA>;
1346349cc55cSDimitry Andric  defm FMS_#vec :
1347349cc55cSDimitry Andric    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins),
1348349cc55cSDimitry Andric              [(set (vec.vt V128:$dst), (int_wasm_fms
1349349cc55cSDimitry Andric                (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))],
1350349cc55cSDimitry Andric              vec.prefix#".fms\t$dst, $a, $b, $c", vec.prefix#".fms", simdopS>;
1351349cc55cSDimitry Andric}
1352349cc55cSDimitry Andric
1353349cc55cSDimitry Andricdefm "" : SIMDFM<F32x4, 0xaf, 0xb0>;
1354349cc55cSDimitry Andricdefm "" : SIMDFM<F64x2, 0xcf, 0xd0>;
1355349cc55cSDimitry Andric
1356349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1357349cc55cSDimitry Andric// Laneselect
1358349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1359349cc55cSDimitry Andric
1360349cc55cSDimitry Andricmulticlass SIMDLANESELECT<Vec vec, bits<32> op> {
1361349cc55cSDimitry Andric  defm LANESELECT_#vec :
1362349cc55cSDimitry Andric    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins),
1363349cc55cSDimitry Andric              [(set (vec.vt V128:$dst), (int_wasm_laneselect
1364349cc55cSDimitry Andric                (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))],
1365349cc55cSDimitry Andric              vec.prefix#".laneselect\t$dst, $a, $b, $c", vec.prefix#".laneselect", op>;
1366349cc55cSDimitry Andric}
1367349cc55cSDimitry Andric
1368349cc55cSDimitry Andricdefm "" : SIMDLANESELECT<I8x16, 0xb2>;
1369349cc55cSDimitry Andricdefm "" : SIMDLANESELECT<I16x8, 0xb3>;
1370349cc55cSDimitry Andricdefm "" : SIMDLANESELECT<I32x4, 0xd2>;
1371349cc55cSDimitry Andricdefm "" : SIMDLANESELECT<I64x2, 0xd3>;
1372349cc55cSDimitry Andric
1373349cc55cSDimitry Andric
1374349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1375349cc55cSDimitry Andric// Relaxed swizzle
1376349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1377349cc55cSDimitry Andric
1378349cc55cSDimitry Andricdefm RELAXED_SWIZZLE :
1379349cc55cSDimitry Andric  RELAXED_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins),
1380349cc55cSDimitry Andric         [(set (v16i8 V128:$dst),
1381349cc55cSDimitry Andric           (int_wasm_relaxed_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))],
1382349cc55cSDimitry Andric         "i8x16.relaxed_swizzle\t$dst, $src, $mask", "i8x16.relaxed_swizzle", 162>;
1383349cc55cSDimitry Andric
1384349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1385349cc55cSDimitry Andric// Relaxed floating-point min and max.
1386349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1387349cc55cSDimitry Andric
1388349cc55cSDimitry Andricmulticlass SIMD_RELAXED_FMINMAX<Vec vec, bits<32> simdopMin, bits<32> simdopMax> {
1389349cc55cSDimitry Andric  defm RELAXED_FMIN_#vec :
1390349cc55cSDimitry Andric    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b), (outs), (ins),
1391349cc55cSDimitry Andric              [(set (vec.vt V128:$dst), (int_wasm_relaxed_min
1392349cc55cSDimitry Andric                (vec.vt V128:$a), (vec.vt V128:$b)))],
1393349cc55cSDimitry Andric              vec.prefix#".relaxed_min\t$dst, $a, $b", vec.prefix#".relaxed_min", simdopMin>;
1394349cc55cSDimitry Andric  defm RELAXED_FMAX_#vec :
1395349cc55cSDimitry Andric    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b), (outs), (ins),
1396349cc55cSDimitry Andric              [(set (vec.vt V128:$dst), (int_wasm_relaxed_max
1397349cc55cSDimitry Andric                (vec.vt V128:$a), (vec.vt V128:$b)))],
1398349cc55cSDimitry Andric              vec.prefix#".relaxed_max\t$dst, $a, $b", vec.prefix#".relaxed_max", simdopMax>;
1399349cc55cSDimitry Andric}
1400349cc55cSDimitry Andric
1401349cc55cSDimitry Andricdefm "" : SIMD_RELAXED_FMINMAX<F32x4, 0xb4, 0xe2>;
1402349cc55cSDimitry Andricdefm "" : SIMD_RELAXED_FMINMAX<F64x2, 0xd4, 0xee>;
1403349cc55cSDimitry Andric
1404349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1405349cc55cSDimitry Andric// Relaxed floating-point to int conversions
1406349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
1407349cc55cSDimitry Andric
1408349cc55cSDimitry Andricmulticlass SIMD_RELAXED_CONVERT<Vec vec, Vec arg, SDPatternOperator op, string name, bits<32> simdop> {
1409349cc55cSDimitry Andric  defm op#_#vec :
1410349cc55cSDimitry Andric    RELAXED_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins),
1411349cc55cSDimitry Andric              [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))],
1412349cc55cSDimitry Andric              vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop>;
1413349cc55cSDimitry Andric}
1414349cc55cSDimitry Andric
1415349cc55cSDimitry Andricdefm "" : SIMD_RELAXED_CONVERT<I32x4, F32x4, int_wasm_relaxed_trunc_signed, "relaxed_trunc_f32x4_s", 0xa5>;
1416349cc55cSDimitry Andricdefm "" : SIMD_RELAXED_CONVERT<I32x4, F32x4, int_wasm_relaxed_trunc_unsigned, "relaxed_trunc_f32x4_u", 0xa6>;
1417349cc55cSDimitry Andric
1418349cc55cSDimitry Andricdefm "" : SIMD_RELAXED_CONVERT<I32x4, F64x2, int_wasm_relaxed_trunc_zero_signed, "relaxed_trunc_f64x2_s_zero", 0xc5>;
1419349cc55cSDimitry Andricdefm "" : SIMD_RELAXED_CONVERT<I32x4, F64x2, int_wasm_relaxed_trunc_zero_unsigned, "relaxed_trunc_f64x2_u_zero", 0xc6>;
1420