xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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
140b57cec5SDimitry Andric// Instructions requiring HasSIMD128 and the simd128 prefix byte
150b57cec5SDimitry Andricmulticlass SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
160b57cec5SDimitry Andric                  list<dag> pattern_r, string asmstr_r = "",
170b57cec5SDimitry Andric                  string asmstr_s = "", bits<32> simdop = -1> {
180b57cec5SDimitry Andric  defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s,
19e8d8bef9SDimitry Andric              !if(!ge(simdop, 0x100),
20e8d8bef9SDimitry Andric                  !or(0xfd0000, !and(0xffff, simdop)),
21e8d8bef9SDimitry Andric                  !or(0xfd00, !and(0xff, simdop)))>,
220b57cec5SDimitry Andric            Requires<[HasSIMD128]>;
230b57cec5SDimitry Andric}
240b57cec5SDimitry Andric
250b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v16i8>;
260b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v8i16>;
270b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v4i32>;
280b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v2i64>;
290b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v4f32>;
300b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v2f64>;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric// Constrained immediate argument types
330b57cec5SDimitry Andricforeach SIZE = [8, 16] in
340b57cec5SDimitry Andricdef ImmI#SIZE : ImmLeaf<i32,
350b57cec5SDimitry Andric  "return -(1 << ("#SIZE#" - 1)) <= Imm && Imm < (1 << ("#SIZE#" - 1));"
360b57cec5SDimitry Andric>;
370b57cec5SDimitry Andricforeach SIZE = [2, 4, 8, 16, 32] in
380b57cec5SDimitry Andricdef LaneIdx#SIZE : ImmLeaf<i32, "return 0 <= Imm && Imm < "#SIZE#";">;
390b57cec5SDimitry Andric
40e8d8bef9SDimitry Andric// Create vector with identical lanes: splat
41e8d8bef9SDimitry Andricdef splat2 : PatFrag<(ops node:$x), (build_vector $x, $x)>;
42e8d8bef9SDimitry Andricdef splat4 : PatFrag<(ops node:$x), (build_vector $x, $x, $x, $x)>;
43e8d8bef9SDimitry Andricdef splat8 : PatFrag<(ops node:$x), (build_vector $x, $x, $x, $x,
44e8d8bef9SDimitry Andric                                                  $x, $x, $x, $x)>;
45e8d8bef9SDimitry Andricdef splat16 : PatFrag<(ops node:$x),
46e8d8bef9SDimitry Andric                      (build_vector $x, $x, $x, $x, $x, $x, $x, $x,
47e8d8bef9SDimitry Andric                                    $x, $x, $x, $x, $x, $x, $x, $x)>;
48e8d8bef9SDimitry Andric
49e8d8bef9SDimitry Andricclass Vec {
50e8d8bef9SDimitry Andric  ValueType vt;
51e8d8bef9SDimitry Andric  ValueType int_vt;
52e8d8bef9SDimitry Andric  ValueType lane_vt;
53e8d8bef9SDimitry Andric  WebAssemblyRegClass lane_rc;
54e8d8bef9SDimitry Andric  int lane_bits;
55e8d8bef9SDimitry Andric  ImmLeaf lane_idx;
56e8d8bef9SDimitry Andric  PatFrag splat;
57e8d8bef9SDimitry Andric  string prefix;
58e8d8bef9SDimitry Andric  Vec split;
59e8d8bef9SDimitry Andric}
60e8d8bef9SDimitry Andric
61e8d8bef9SDimitry Andricdef I8x16 : Vec {
62e8d8bef9SDimitry Andric  let vt = v16i8;
63e8d8bef9SDimitry Andric  let int_vt = vt;
64e8d8bef9SDimitry Andric  let lane_vt = i32;
65e8d8bef9SDimitry Andric  let lane_rc = I32;
66e8d8bef9SDimitry Andric  let lane_bits = 8;
67e8d8bef9SDimitry Andric  let lane_idx = LaneIdx16;
68e8d8bef9SDimitry Andric  let splat = splat16;
69e8d8bef9SDimitry Andric  let prefix = "i8x16";
70e8d8bef9SDimitry Andric}
71e8d8bef9SDimitry Andric
72e8d8bef9SDimitry Andricdef I16x8 : Vec {
73e8d8bef9SDimitry Andric  let vt = v8i16;
74e8d8bef9SDimitry Andric  let int_vt = vt;
75e8d8bef9SDimitry Andric  let lane_vt = i32;
76e8d8bef9SDimitry Andric  let lane_rc = I32;
77e8d8bef9SDimitry Andric  let lane_bits = 16;
78e8d8bef9SDimitry Andric  let lane_idx = LaneIdx8;
79e8d8bef9SDimitry Andric  let splat = splat8;
80e8d8bef9SDimitry Andric  let prefix = "i16x8";
81e8d8bef9SDimitry Andric  let split = I8x16;
82e8d8bef9SDimitry Andric}
83e8d8bef9SDimitry Andric
84e8d8bef9SDimitry Andricdef I32x4 : Vec {
85e8d8bef9SDimitry Andric  let vt = v4i32;
86e8d8bef9SDimitry Andric  let int_vt = vt;
87e8d8bef9SDimitry Andric  let lane_vt = i32;
88e8d8bef9SDimitry Andric  let lane_rc = I32;
89e8d8bef9SDimitry Andric  let lane_bits = 32;
90e8d8bef9SDimitry Andric  let lane_idx = LaneIdx4;
91e8d8bef9SDimitry Andric  let splat = splat4;
92e8d8bef9SDimitry Andric  let prefix = "i32x4";
93e8d8bef9SDimitry Andric  let split = I16x8;
94e8d8bef9SDimitry Andric}
95e8d8bef9SDimitry Andric
96e8d8bef9SDimitry Andricdef I64x2 : Vec {
97e8d8bef9SDimitry Andric  let vt = v2i64;
98e8d8bef9SDimitry Andric  let int_vt = vt;
99e8d8bef9SDimitry Andric  let lane_vt = i64;
100e8d8bef9SDimitry Andric  let lane_rc = I64;
101e8d8bef9SDimitry Andric  let lane_bits = 64;
102e8d8bef9SDimitry Andric  let lane_idx = LaneIdx2;
103e8d8bef9SDimitry Andric  let splat = splat2;
104e8d8bef9SDimitry Andric  let prefix = "i64x2";
105e8d8bef9SDimitry Andric  let split = I32x4;
106e8d8bef9SDimitry Andric}
107e8d8bef9SDimitry Andric
108e8d8bef9SDimitry Andricdef F32x4 : Vec {
109e8d8bef9SDimitry Andric  let vt = v4f32;
110e8d8bef9SDimitry Andric  let int_vt = v4i32;
111e8d8bef9SDimitry Andric  let lane_vt = f32;
112e8d8bef9SDimitry Andric  let lane_rc = F32;
113e8d8bef9SDimitry Andric  let lane_bits = 32;
114e8d8bef9SDimitry Andric  let lane_idx = LaneIdx4;
115e8d8bef9SDimitry Andric  let splat = splat4;
116e8d8bef9SDimitry Andric  let prefix = "f32x4";
117e8d8bef9SDimitry Andric}
118e8d8bef9SDimitry Andric
119e8d8bef9SDimitry Andricdef F64x2 : Vec {
120e8d8bef9SDimitry Andric  let vt = v2f64;
121e8d8bef9SDimitry Andric  let int_vt = v2i64;
122e8d8bef9SDimitry Andric  let lane_vt = f64;
123e8d8bef9SDimitry Andric  let lane_rc = F64;
124e8d8bef9SDimitry Andric  let lane_bits = 64;
125e8d8bef9SDimitry Andric  let lane_idx = LaneIdx2;
126e8d8bef9SDimitry Andric  let splat = splat2;
127e8d8bef9SDimitry Andric  let prefix = "f64x2";
128e8d8bef9SDimitry Andric}
129e8d8bef9SDimitry Andric
130e8d8bef9SDimitry Andricdefvar AllVecs = [I8x16, I16x8, I32x4, I64x2, F32x4, F64x2];
131e8d8bef9SDimitry Andricdefvar IntVecs = [I8x16, I16x8, I32x4, I64x2];
132e8d8bef9SDimitry Andric
1330b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1340b57cec5SDimitry Andric// Load and store
1350b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric// Load: v128.load
1385ffd83dbSDimitry Andriclet mayLoad = 1, UseNamedOperandTable = 1 in {
1395ffd83dbSDimitry Andricdefm LOAD_V128_A32 :
1400b57cec5SDimitry Andric  SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
1410b57cec5SDimitry Andric         (outs), (ins P2Align:$p2align, offset32_op:$off), [],
1420b57cec5SDimitry Andric         "v128.load\t$dst, ${off}(${addr})$p2align",
1430b57cec5SDimitry Andric         "v128.load\t$off$p2align", 0>;
1445ffd83dbSDimitry Andricdefm LOAD_V128_A64 :
1455ffd83dbSDimitry Andric  SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
1465ffd83dbSDimitry Andric         (outs), (ins P2Align:$p2align, offset64_op:$off), [],
1475ffd83dbSDimitry Andric         "v128.load\t$dst, ${off}(${addr})$p2align",
1485ffd83dbSDimitry Andric         "v128.load\t$off$p2align", 0>;
1495ffd83dbSDimitry Andric}
1500b57cec5SDimitry Andric
151e8d8bef9SDimitry Andric// Def load patterns from WebAssemblyInstrMemory.td for vector types
152e8d8bef9SDimitry Andricforeach vec = AllVecs in {
153e8d8bef9SDimitry Andricdefm : LoadPatNoOffset<vec.vt, load, "LOAD_V128">;
154e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load, regPlusImm, "LOAD_V128">;
155e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load, or_is_add, "LOAD_V128">;
156e8d8bef9SDimitry Andricdefm : LoadPatOffsetOnly<vec.vt, load, "LOAD_V128">;
157e8d8bef9SDimitry Andricdefm : LoadPatGlobalAddrOffOnly<vec.vt, load, "LOAD_V128">;
1580b57cec5SDimitry Andric}
1590b57cec5SDimitry Andric
160e8d8bef9SDimitry Andric// v128.loadX_splat
161e8d8bef9SDimitry Andricmulticlass SIMDLoadSplat<int size, bits<32> simdop> {
1625ffd83dbSDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
163e8d8bef9SDimitry Andric  defm LOAD#size#_SPLAT_A32 :
1645ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
1655ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
1665ffd83dbSDimitry Andric           (outs),
1675ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off), [],
168e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align",
169e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$off$p2align", simdop>;
170e8d8bef9SDimitry Andric  defm LOAD#size#_SPLAT_A64 :
1715ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
1725ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
1735ffd83dbSDimitry Andric           (outs),
1745ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off), [],
175e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align",
176e8d8bef9SDimitry Andric           "v128.load"#size#"_splat\t$off$p2align", simdop>;
1778bcb0991SDimitry Andric  }
1785ffd83dbSDimitry Andric}
1798bcb0991SDimitry Andric
180e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<8, 7>;
181e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<16, 8>;
182e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<32, 9>;
183e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<64, 10>;
1848bcb0991SDimitry Andric
185480093f4SDimitry Andricdef wasm_load_splat_t : SDTypeProfile<1, 1, [SDTCisPtrTy<1>]>;
186480093f4SDimitry Andricdef wasm_load_splat : SDNode<"WebAssemblyISD::LOAD_SPLAT", wasm_load_splat_t,
187480093f4SDimitry Andric                             [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
188480093f4SDimitry Andricdef load_splat : PatFrag<(ops node:$addr), (wasm_load_splat node:$addr)>;
1898bcb0991SDimitry Andric
190e8d8bef9SDimitry Andricforeach vec = AllVecs in {
191e8d8bef9SDimitry Andricdefvar inst = "LOAD"#vec.lane_bits#"_SPLAT";
192e8d8bef9SDimitry Andricdefm : LoadPatNoOffset<vec.vt, load_splat, inst>;
193e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load_splat, regPlusImm, inst>;
194e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, load_splat, or_is_add, inst>;
195e8d8bef9SDimitry Andricdefm : LoadPatOffsetOnly<vec.vt, load_splat, inst>;
196e8d8bef9SDimitry Andricdefm : LoadPatGlobalAddrOffOnly<vec.vt, load_splat, inst>;
1978bcb0991SDimitry Andric}
1988bcb0991SDimitry Andric
1998bcb0991SDimitry Andric// Load and extend
200e8d8bef9SDimitry Andricmulticlass SIMDLoadExtend<Vec vec, string loadPat, bits<32> simdop> {
201e8d8bef9SDimitry Andric  defvar signed = vec.prefix#".load"#loadPat#"_s";
202e8d8bef9SDimitry Andric  defvar unsigned = vec.prefix#".load"#loadPat#"_u";
2035ffd83dbSDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
204e8d8bef9SDimitry Andric  defm LOAD_EXTEND_S_#vec#_A32 :
2055ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2065ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
2078bcb0991SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
208e8d8bef9SDimitry Andric           signed#"\t$dst, ${off}(${addr})$p2align",
209e8d8bef9SDimitry Andric           signed#"\t$off$p2align", simdop>;
210e8d8bef9SDimitry Andric  defm LOAD_EXTEND_U_#vec#_A32 :
2115ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2125ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
2138bcb0991SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
214e8d8bef9SDimitry Andric           unsigned#"\t$dst, ${off}(${addr})$p2align",
215e8d8bef9SDimitry Andric           unsigned#"\t$off$p2align", !add(simdop, 1)>;
216e8d8bef9SDimitry Andric  defm LOAD_EXTEND_S_#vec#_A64 :
2175ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2185ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
2195ffd83dbSDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
220e8d8bef9SDimitry Andric           signed#"\t$dst, ${off}(${addr})$p2align",
221e8d8bef9SDimitry Andric           signed#"\t$off$p2align", simdop>;
222e8d8bef9SDimitry Andric  defm LOAD_EXTEND_U_#vec#_A64 :
2235ffd83dbSDimitry Andric    SIMD_I<(outs V128:$dst),
2245ffd83dbSDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
2255ffd83dbSDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
226e8d8bef9SDimitry Andric           unsigned#"\t$dst, ${off}(${addr})$p2align",
227e8d8bef9SDimitry Andric           unsigned#"\t$off$p2align", !add(simdop, 1)>;
2288bcb0991SDimitry Andric  }
2298bcb0991SDimitry Andric}
2308bcb0991SDimitry Andric
231e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I16x8, "8x8", 1>;
232e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I32x4, "16x4", 3>;
233e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I64x2, "32x2", 5>;
2348bcb0991SDimitry Andric
235e8d8bef9SDimitry Andricforeach vec = [I16x8, I32x4, I64x2] in
236e8d8bef9SDimitry Andricforeach exts = [["sextloadvi", "_S"],
237e8d8bef9SDimitry Andric                ["zextloadvi", "_U"],
238e8d8bef9SDimitry Andric                ["extloadvi", "_U"]] in {
239e8d8bef9SDimitry Andricdefvar loadpat = !cast<PatFrag>(exts[0]#vec.split.lane_bits);
240e8d8bef9SDimitry Andricdefvar inst = "LOAD_EXTEND"#exts[1]#"_"#vec;
241e8d8bef9SDimitry Andricdefm : LoadPatNoOffset<vec.vt, loadpat, inst>;
242e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, loadpat, regPlusImm, inst>;
243e8d8bef9SDimitry Andricdefm : LoadPatImmOff<vec.vt, loadpat, or_is_add, inst>;
244e8d8bef9SDimitry Andricdefm : LoadPatOffsetOnly<vec.vt, loadpat, inst>;
245e8d8bef9SDimitry Andricdefm : LoadPatGlobalAddrOffOnly<vec.vt, loadpat, inst>;
2468bcb0991SDimitry Andric}
2478bcb0991SDimitry Andric
248e8d8bef9SDimitry Andric// Load lane into zero vector
249e8d8bef9SDimitry Andricmulticlass SIMDLoadZero<Vec vec, bits<32> simdop> {
250e8d8bef9SDimitry Andric  defvar name = "v128.load"#vec.lane_bits#"_zero";
251e8d8bef9SDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
252e8d8bef9SDimitry Andric  defm LOAD_ZERO_#vec#_A32 :
253e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
254e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
255e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
256e8d8bef9SDimitry Andric           name#"\t$dst, ${off}(${addr})$p2align",
257e8d8bef9SDimitry Andric           name#"\t$off$p2align", simdop>;
258e8d8bef9SDimitry Andric  defm LOAD_ZERO_#vec#_A64 :
259e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
260e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
261e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
262e8d8bef9SDimitry Andric           name#"\t$dst, ${off}(${addr})$p2align",
263e8d8bef9SDimitry Andric           name#"\t$off$p2align", simdop>;
264e8d8bef9SDimitry Andric  } // mayLoad = 1, UseNamedOperandTable = 1
265e8d8bef9SDimitry Andric}
266e8d8bef9SDimitry Andric
267*fe6060f1SDimitry Andricdefm "" : SIMDLoadZero<I32x4, 0x5c>;
268*fe6060f1SDimitry Andricdefm "" : SIMDLoadZero<I64x2, 0x5d>;
269e8d8bef9SDimitry Andric
270*fe6060f1SDimitry Andric// TODO: f32x4 and f64x2 as well
271e8d8bef9SDimitry Andricforeach vec = [I32x4, I64x2] in {
272e8d8bef9SDimitry Andric  defvar inst = "LOAD_ZERO_"#vec;
273*fe6060f1SDimitry Andric  defvar pat = PatFrag<(ops node:$ptr),
274*fe6060f1SDimitry Andric    (vector_insert (vec.splat (vec.lane_vt 0)), (vec.lane_vt (load $ptr)), 0)>;
275*fe6060f1SDimitry Andric  defm : LoadPatNoOffset<vec.vt, pat, inst>;
276*fe6060f1SDimitry Andric  defm : LoadPatImmOff<vec.vt, pat, regPlusImm, inst>;
277*fe6060f1SDimitry Andric  defm : LoadPatImmOff<vec.vt, pat, or_is_add, inst>;
278*fe6060f1SDimitry Andric  defm : LoadPatOffsetOnly<vec.vt, pat, inst>;
279*fe6060f1SDimitry Andric  defm : LoadPatGlobalAddrOffOnly<vec.vt, pat, inst>;
280e8d8bef9SDimitry Andric}
281e8d8bef9SDimitry Andric
282e8d8bef9SDimitry Andric// Load lane
283e8d8bef9SDimitry Andricmulticlass SIMDLoadLane<Vec vec, bits<32> simdop> {
284e8d8bef9SDimitry Andric  defvar name = "v128.load"#vec.lane_bits#"_lane";
285e8d8bef9SDimitry Andric  let mayLoad = 1, UseNamedOperandTable = 1 in {
286e8d8bef9SDimitry Andric  defm LOAD_LANE_#vec#_A32 :
287e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
288e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx,
289e8d8bef9SDimitry Andric                I32:$addr, V128:$vec),
290e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx),
291e8d8bef9SDimitry Andric           [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx",
292e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
293e8d8bef9SDimitry Andric  defm LOAD_LANE_#vec#_A64 :
294e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
295e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx,
296e8d8bef9SDimitry Andric                I64:$addr, V128:$vec),
297e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx),
298e8d8bef9SDimitry Andric           [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx",
299e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
300e8d8bef9SDimitry Andric  } // mayLoad = 1, UseNamedOperandTable = 1
301e8d8bef9SDimitry Andric}
302e8d8bef9SDimitry Andric
303*fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I8x16, 0x54>;
304*fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I16x8, 0x55>;
305*fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I32x4, 0x56>;
306*fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I64x2, 0x57>;
307e8d8bef9SDimitry Andric
308e8d8bef9SDimitry Andric// Select loads with no constant offset.
309*fe6060f1SDimitry Andricmulticlass LoadLanePatNoOffset<Vec vec, SDPatternOperator kind> {
310e8d8bef9SDimitry Andric  defvar load_lane_a32 = !cast<NI>("LOAD_LANE_"#vec#"_A32");
311e8d8bef9SDimitry Andric  defvar load_lane_a64 = !cast<NI>("LOAD_LANE_"#vec#"_A64");
312e8d8bef9SDimitry Andric  def : Pat<(vec.vt (kind (i32 I32:$addr),
313e8d8bef9SDimitry Andric              (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))),
314e8d8bef9SDimitry Andric            (load_lane_a32 0, 0, imm:$idx, $addr, $vec)>,
315e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
316e8d8bef9SDimitry Andric  def : Pat<(vec.vt (kind (i64 I64:$addr),
317e8d8bef9SDimitry Andric              (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))),
318e8d8bef9SDimitry Andric            (load_lane_a64 0, 0, imm:$idx, $addr, $vec)>,
319e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
320e8d8bef9SDimitry Andric}
321e8d8bef9SDimitry Andric
322*fe6060f1SDimitry Andricdef load8_lane :
323*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
324*fe6060f1SDimitry Andric          (vector_insert $vec, (i32 (extloadi8 $ptr)), $idx)>;
325*fe6060f1SDimitry Andricdef load16_lane :
326*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
327*fe6060f1SDimitry Andric          (vector_insert $vec, (i32 (extloadi16 $ptr)), $idx)>;
328*fe6060f1SDimitry Andricdef load32_lane :
329*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
330*fe6060f1SDimitry Andric          (vector_insert $vec, (i32 (load $ptr)), $idx)>;
331*fe6060f1SDimitry Andricdef load64_lane :
332*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
333*fe6060f1SDimitry Andric          (vector_insert $vec, (i64 (load $ptr)), $idx)>;
334*fe6060f1SDimitry Andric// TODO: floating point lanes as well
335*fe6060f1SDimitry Andric
336*fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I8x16, load8_lane>;
337*fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I16x8, load16_lane>;
338*fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I32x4, load32_lane>;
339*fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I64x2, load64_lane>;
340e8d8bef9SDimitry Andric
341e8d8bef9SDimitry Andric// TODO: Also support the other load patterns for load_lane once the instructions
342e8d8bef9SDimitry Andric// are merged to the proposal.
3438bcb0991SDimitry Andric
3440b57cec5SDimitry Andric// Store: v128.store
3455ffd83dbSDimitry Andriclet mayStore = 1, UseNamedOperandTable = 1 in {
3465ffd83dbSDimitry Andricdefm STORE_V128_A32 :
3470b57cec5SDimitry Andric  SIMD_I<(outs), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, V128:$vec),
3480b57cec5SDimitry Andric         (outs), (ins P2Align:$p2align, offset32_op:$off), [],
3490b57cec5SDimitry Andric         "v128.store\t${off}(${addr})$p2align, $vec",
3505ffd83dbSDimitry Andric         "v128.store\t$off$p2align", 11>;
3515ffd83dbSDimitry Andricdefm STORE_V128_A64 :
3525ffd83dbSDimitry Andric  SIMD_I<(outs), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, V128:$vec),
3535ffd83dbSDimitry Andric         (outs), (ins P2Align:$p2align, offset64_op:$off), [],
3545ffd83dbSDimitry Andric         "v128.store\t${off}(${addr})$p2align, $vec",
3555ffd83dbSDimitry Andric         "v128.store\t$off$p2align", 11>;
3565ffd83dbSDimitry Andric}
357e8d8bef9SDimitry Andric
358e8d8bef9SDimitry Andric// Def store patterns from WebAssemblyInstrMemory.td for vector types
359e8d8bef9SDimitry Andricforeach vec = AllVecs in {
360e8d8bef9SDimitry Andricdefm : StorePatNoOffset<vec.vt, store, "STORE_V128">;
361e8d8bef9SDimitry Andricdefm : StorePatImmOff<vec.vt, store, regPlusImm, "STORE_V128">;
362e8d8bef9SDimitry Andricdefm : StorePatImmOff<vec.vt, store, or_is_add, "STORE_V128">;
363e8d8bef9SDimitry Andricdefm : StorePatOffsetOnly<vec.vt, store, "STORE_V128">;
364e8d8bef9SDimitry Andricdefm : StorePatGlobalAddrOffOnly<vec.vt, store, "STORE_V128">;
3650b57cec5SDimitry Andric}
3660b57cec5SDimitry Andric
367e8d8bef9SDimitry Andric// Store lane
368e8d8bef9SDimitry Andricmulticlass SIMDStoreLane<Vec vec, bits<32> simdop> {
369e8d8bef9SDimitry Andric  defvar name = "v128.store"#vec.lane_bits#"_lane";
370e8d8bef9SDimitry Andric  let mayStore = 1, UseNamedOperandTable = 1 in {
371e8d8bef9SDimitry Andric  defm STORE_LANE_#vec#_A32 :
372e8d8bef9SDimitry Andric    SIMD_I<(outs),
373e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx,
374e8d8bef9SDimitry Andric                I32:$addr, V128:$vec),
375e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx),
376e8d8bef9SDimitry Andric           [], name#"\t${off}(${addr})$p2align, $vec, $idx",
377e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
378e8d8bef9SDimitry Andric  defm STORE_LANE_#vec#_A64 :
379e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst),
380e8d8bef9SDimitry Andric           (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx,
381e8d8bef9SDimitry Andric                I64:$addr, V128:$vec),
382e8d8bef9SDimitry Andric           (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx),
383e8d8bef9SDimitry Andric           [], name#"\t${off}(${addr})$p2align, $vec, $idx",
384e8d8bef9SDimitry Andric           name#"\t$off$p2align, $idx", simdop>;
385e8d8bef9SDimitry Andric  } // mayStore = 1, UseNamedOperandTable = 1
386e8d8bef9SDimitry Andric}
387e8d8bef9SDimitry Andric
388*fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I8x16, 0x58>;
389*fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I16x8, 0x59>;
390*fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I32x4, 0x5a>;
391*fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I64x2, 0x5b>;
392e8d8bef9SDimitry Andric
393e8d8bef9SDimitry Andric// Select stores with no constant offset.
394*fe6060f1SDimitry Andricmulticlass StoreLanePatNoOffset<Vec vec, SDPatternOperator kind> {
395e8d8bef9SDimitry Andric  def : Pat<(kind (i32 I32:$addr), (vec.vt V128:$vec), (i32 vec.lane_idx:$idx)),
396e8d8bef9SDimitry Andric            (!cast<NI>("STORE_LANE_"#vec#"_A32") 0, 0, imm:$idx, $addr, $vec)>,
397e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
398e8d8bef9SDimitry Andric  def : Pat<(kind (i64 I64:$addr), (vec.vt V128:$vec), (i32 vec.lane_idx:$idx)),
399e8d8bef9SDimitry Andric            (!cast<NI>("STORE_LANE_"#vec#"_A64") 0, 0, imm:$idx, $addr, $vec)>,
400e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
401e8d8bef9SDimitry Andric}
402e8d8bef9SDimitry Andric
403*fe6060f1SDimitry Andricdef store8_lane :
404*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
405*fe6060f1SDimitry Andric          (truncstorei8 (i32 (vector_extract $vec, $idx)), $ptr)>;
406*fe6060f1SDimitry Andricdef store16_lane :
407*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
408*fe6060f1SDimitry Andric          (truncstorei16 (i32 (vector_extract $vec, $idx)), $ptr)>;
409*fe6060f1SDimitry Andricdef store32_lane :
410*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
411*fe6060f1SDimitry Andric          (store (i32 (vector_extract $vec, $idx)), $ptr)>;
412*fe6060f1SDimitry Andricdef store64_lane :
413*fe6060f1SDimitry Andric  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
414*fe6060f1SDimitry Andric          (store (i64 (vector_extract $vec, $idx)), $ptr)>;
415*fe6060f1SDimitry Andric// TODO: floating point lanes as well
416e8d8bef9SDimitry Andric
417*fe6060f1SDimitry Andriclet AddedComplexity = 1 in {
418*fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I8x16, store8_lane>;
419*fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I16x8, store16_lane>;
420*fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I32x4, store32_lane>;
421*fe6060f1SDimitry Andricdefm : StoreLanePatNoOffset<I64x2, store64_lane>;
422*fe6060f1SDimitry Andric}
423e8d8bef9SDimitry Andric
4240b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
4250b57cec5SDimitry Andric// Constructing SIMD values
4260b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
4270b57cec5SDimitry Andric
4280b57cec5SDimitry Andric// Constant: v128.const
429e8d8bef9SDimitry Andricmulticlass ConstVec<Vec vec, dag ops, dag pat, string args> {
430*fe6060f1SDimitry Andric  let isMoveImm = 1, isReMaterializable = 1 in
431e8d8bef9SDimitry Andric  defm CONST_V128_#vec : SIMD_I<(outs V128:$dst), ops, (outs), ops,
432e8d8bef9SDimitry Andric                                 [(set V128:$dst, (vec.vt pat))],
4330b57cec5SDimitry Andric                                 "v128.const\t$dst, "#args,
4345ffd83dbSDimitry Andric                                 "v128.const\t"#args, 12>;
4350b57cec5SDimitry Andric}
4360b57cec5SDimitry Andric
437e8d8bef9SDimitry Andricdefm "" : ConstVec<I8x16,
4380b57cec5SDimitry Andric                   (ins vec_i8imm_op:$i0, vec_i8imm_op:$i1,
4390b57cec5SDimitry Andric                        vec_i8imm_op:$i2, vec_i8imm_op:$i3,
4400b57cec5SDimitry Andric                        vec_i8imm_op:$i4, vec_i8imm_op:$i5,
4410b57cec5SDimitry Andric                        vec_i8imm_op:$i6, vec_i8imm_op:$i7,
4420b57cec5SDimitry Andric                        vec_i8imm_op:$i8, vec_i8imm_op:$i9,
4430b57cec5SDimitry Andric                        vec_i8imm_op:$iA, vec_i8imm_op:$iB,
4440b57cec5SDimitry Andric                        vec_i8imm_op:$iC, vec_i8imm_op:$iD,
4450b57cec5SDimitry Andric                        vec_i8imm_op:$iE, vec_i8imm_op:$iF),
4460b57cec5SDimitry Andric                   (build_vector ImmI8:$i0, ImmI8:$i1, ImmI8:$i2, ImmI8:$i3,
4470b57cec5SDimitry Andric                                 ImmI8:$i4, ImmI8:$i5, ImmI8:$i6, ImmI8:$i7,
4480b57cec5SDimitry Andric                                 ImmI8:$i8, ImmI8:$i9, ImmI8:$iA, ImmI8:$iB,
4490b57cec5SDimitry Andric                                 ImmI8:$iC, ImmI8:$iD, ImmI8:$iE, ImmI8:$iF),
4500b57cec5SDimitry Andric                   !strconcat("$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7, ",
4510b57cec5SDimitry Andric                              "$i8, $i9, $iA, $iB, $iC, $iD, $iE, $iF")>;
452e8d8bef9SDimitry Andricdefm "" : ConstVec<I16x8,
4530b57cec5SDimitry Andric                   (ins vec_i16imm_op:$i0, vec_i16imm_op:$i1,
4540b57cec5SDimitry Andric                        vec_i16imm_op:$i2, vec_i16imm_op:$i3,
4550b57cec5SDimitry Andric                        vec_i16imm_op:$i4, vec_i16imm_op:$i5,
4560b57cec5SDimitry Andric                        vec_i16imm_op:$i6, vec_i16imm_op:$i7),
4570b57cec5SDimitry Andric                   (build_vector
4580b57cec5SDimitry Andric                     ImmI16:$i0, ImmI16:$i1, ImmI16:$i2, ImmI16:$i3,
4590b57cec5SDimitry Andric                     ImmI16:$i4, ImmI16:$i5, ImmI16:$i6, ImmI16:$i7),
4600b57cec5SDimitry Andric                   "$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7">;
4610b57cec5SDimitry Andriclet IsCanonical = 1 in
462e8d8bef9SDimitry Andricdefm "" : ConstVec<I32x4,
4630b57cec5SDimitry Andric                   (ins vec_i32imm_op:$i0, vec_i32imm_op:$i1,
4640b57cec5SDimitry Andric                        vec_i32imm_op:$i2, vec_i32imm_op:$i3),
4650b57cec5SDimitry Andric                   (build_vector (i32 imm:$i0), (i32 imm:$i1),
4660b57cec5SDimitry Andric                                 (i32 imm:$i2), (i32 imm:$i3)),
4670b57cec5SDimitry Andric                   "$i0, $i1, $i2, $i3">;
468e8d8bef9SDimitry Andricdefm "" : ConstVec<I64x2,
4690b57cec5SDimitry Andric                   (ins vec_i64imm_op:$i0, vec_i64imm_op:$i1),
4700b57cec5SDimitry Andric                   (build_vector (i64 imm:$i0), (i64 imm:$i1)),
4710b57cec5SDimitry Andric                   "$i0, $i1">;
472e8d8bef9SDimitry Andricdefm "" : ConstVec<F32x4,
4730b57cec5SDimitry Andric                   (ins f32imm_op:$i0, f32imm_op:$i1,
4740b57cec5SDimitry Andric                        f32imm_op:$i2, f32imm_op:$i3),
4750b57cec5SDimitry Andric                   (build_vector (f32 fpimm:$i0), (f32 fpimm:$i1),
4760b57cec5SDimitry Andric                                 (f32 fpimm:$i2), (f32 fpimm:$i3)),
4770b57cec5SDimitry Andric                   "$i0, $i1, $i2, $i3">;
478e8d8bef9SDimitry Andricdefm "" : ConstVec<F64x2,
4790b57cec5SDimitry Andric                  (ins f64imm_op:$i0, f64imm_op:$i1),
4800b57cec5SDimitry Andric                  (build_vector (f64 fpimm:$i0), (f64 fpimm:$i1)),
4810b57cec5SDimitry Andric                  "$i0, $i1">;
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric// Shuffle lanes: shuffle
4840b57cec5SDimitry Andricdefm SHUFFLE :
4850b57cec5SDimitry Andric  SIMD_I<(outs V128:$dst),
4860b57cec5SDimitry Andric         (ins V128:$x, V128:$y,
4870b57cec5SDimitry Andric           vec_i8imm_op:$m0, vec_i8imm_op:$m1,
4880b57cec5SDimitry Andric           vec_i8imm_op:$m2, vec_i8imm_op:$m3,
4890b57cec5SDimitry Andric           vec_i8imm_op:$m4, vec_i8imm_op:$m5,
4900b57cec5SDimitry Andric           vec_i8imm_op:$m6, vec_i8imm_op:$m7,
4910b57cec5SDimitry Andric           vec_i8imm_op:$m8, vec_i8imm_op:$m9,
4920b57cec5SDimitry Andric           vec_i8imm_op:$mA, vec_i8imm_op:$mB,
4930b57cec5SDimitry Andric           vec_i8imm_op:$mC, vec_i8imm_op:$mD,
4940b57cec5SDimitry Andric           vec_i8imm_op:$mE, vec_i8imm_op:$mF),
4950b57cec5SDimitry Andric         (outs),
4960b57cec5SDimitry Andric         (ins
4970b57cec5SDimitry Andric           vec_i8imm_op:$m0, vec_i8imm_op:$m1,
4980b57cec5SDimitry Andric           vec_i8imm_op:$m2, vec_i8imm_op:$m3,
4990b57cec5SDimitry Andric           vec_i8imm_op:$m4, vec_i8imm_op:$m5,
5000b57cec5SDimitry Andric           vec_i8imm_op:$m6, vec_i8imm_op:$m7,
5010b57cec5SDimitry Andric           vec_i8imm_op:$m8, vec_i8imm_op:$m9,
5020b57cec5SDimitry Andric           vec_i8imm_op:$mA, vec_i8imm_op:$mB,
5030b57cec5SDimitry Andric           vec_i8imm_op:$mC, vec_i8imm_op:$mD,
5040b57cec5SDimitry Andric           vec_i8imm_op:$mE, vec_i8imm_op:$mF),
5050b57cec5SDimitry Andric         [],
506e8d8bef9SDimitry Andric         "i8x16.shuffle\t$dst, $x, $y, "#
5070b57cec5SDimitry Andric           "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "#
5080b57cec5SDimitry Andric           "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF",
509e8d8bef9SDimitry Andric         "i8x16.shuffle\t"#
5100b57cec5SDimitry Andric           "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "#
5110b57cec5SDimitry Andric           "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF",
5125ffd83dbSDimitry Andric         13>;
5130b57cec5SDimitry Andric
5140b57cec5SDimitry Andric// Shuffles after custom lowering
5150b57cec5SDimitry Andricdef wasm_shuffle_t : SDTypeProfile<1, 18, []>;
5160b57cec5SDimitry Andricdef wasm_shuffle : SDNode<"WebAssemblyISD::SHUFFLE", wasm_shuffle_t>;
517e8d8bef9SDimitry Andricforeach vec = AllVecs in {
518e8d8bef9SDimitry Andricdef : Pat<(vec.vt (wasm_shuffle (vec.vt V128:$x), (vec.vt V128:$y),
5190b57cec5SDimitry Andric            (i32 LaneIdx32:$m0), (i32 LaneIdx32:$m1),
5200b57cec5SDimitry Andric            (i32 LaneIdx32:$m2), (i32 LaneIdx32:$m3),
5210b57cec5SDimitry Andric            (i32 LaneIdx32:$m4), (i32 LaneIdx32:$m5),
5220b57cec5SDimitry Andric            (i32 LaneIdx32:$m6), (i32 LaneIdx32:$m7),
5230b57cec5SDimitry Andric            (i32 LaneIdx32:$m8), (i32 LaneIdx32:$m9),
5240b57cec5SDimitry Andric            (i32 LaneIdx32:$mA), (i32 LaneIdx32:$mB),
5250b57cec5SDimitry Andric            (i32 LaneIdx32:$mC), (i32 LaneIdx32:$mD),
5260b57cec5SDimitry Andric            (i32 LaneIdx32:$mE), (i32 LaneIdx32:$mF))),
527e8d8bef9SDimitry Andric          (SHUFFLE $x, $y,
528e8d8bef9SDimitry Andric            imm:$m0, imm:$m1, imm:$m2, imm:$m3,
529e8d8bef9SDimitry Andric            imm:$m4, imm:$m5, imm:$m6, imm:$m7,
530e8d8bef9SDimitry Andric            imm:$m8, imm:$m9, imm:$mA, imm:$mB,
531e8d8bef9SDimitry Andric            imm:$mC, imm:$mD, imm:$mE, imm:$mF)>;
5320b57cec5SDimitry Andric}
5330b57cec5SDimitry Andric
534e8d8bef9SDimitry Andric// Swizzle lanes: i8x16.swizzle
5358bcb0991SDimitry Andricdef wasm_swizzle_t : SDTypeProfile<1, 2, []>;
5368bcb0991SDimitry Andricdef wasm_swizzle : SDNode<"WebAssemblyISD::SWIZZLE", wasm_swizzle_t>;
5378bcb0991SDimitry Andricdefm SWIZZLE :
5388bcb0991SDimitry Andric  SIMD_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins),
5398bcb0991SDimitry Andric         [(set (v16i8 V128:$dst),
5408bcb0991SDimitry Andric           (wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))],
541e8d8bef9SDimitry Andric         "i8x16.swizzle\t$dst, $src, $mask", "i8x16.swizzle", 14>;
5428bcb0991SDimitry Andric
5438bcb0991SDimitry Andricdef : Pat<(int_wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)),
544e8d8bef9SDimitry Andric          (SWIZZLE $src, $mask)>;
5458bcb0991SDimitry Andric
546e8d8bef9SDimitry Andricmulticlass Splat<Vec vec, bits<32> simdop> {
547e8d8bef9SDimitry Andric  defm SPLAT_#vec : SIMD_I<(outs V128:$dst), (ins vec.lane_rc:$x),
548e8d8bef9SDimitry Andric                           (outs), (ins),
549e8d8bef9SDimitry Andric                           [(set (vec.vt V128:$dst),
550e8d8bef9SDimitry Andric                              (vec.splat vec.lane_rc:$x))],
551e8d8bef9SDimitry Andric                           vec.prefix#".splat\t$dst, $x", vec.prefix#".splat",
552e8d8bef9SDimitry Andric                           simdop>;
5530b57cec5SDimitry Andric}
5540b57cec5SDimitry Andric
555e8d8bef9SDimitry Andricdefm "" : Splat<I8x16, 15>;
556e8d8bef9SDimitry Andricdefm "" : Splat<I16x8, 16>;
557e8d8bef9SDimitry Andricdefm "" : Splat<I32x4, 17>;
558e8d8bef9SDimitry Andricdefm "" : Splat<I64x2, 18>;
559e8d8bef9SDimitry Andricdefm "" : Splat<F32x4, 19>;
560e8d8bef9SDimitry Andricdefm "" : Splat<F64x2, 20>;
5610b57cec5SDimitry Andric
5620b57cec5SDimitry Andric// scalar_to_vector leaves high lanes undefined, so can be a splat
563e8d8bef9SDimitry Andricforeach vec = AllVecs in
564e8d8bef9SDimitry Andricdef : Pat<(vec.vt (scalar_to_vector (vec.lane_vt vec.lane_rc:$x))),
565e8d8bef9SDimitry Andric          (!cast<Instruction>("SPLAT_"#vec) $x)>;
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
5680b57cec5SDimitry Andric// Accessing lanes
5690b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric// Extract lane as a scalar: extract_lane / extract_lane_s / extract_lane_u
572e8d8bef9SDimitry Andricmulticlass ExtractLane<Vec vec, bits<32> simdop, string suffix = ""> {
573e8d8bef9SDimitry Andric  defm EXTRACT_LANE_#vec#suffix :
574e8d8bef9SDimitry Andric      SIMD_I<(outs vec.lane_rc:$dst), (ins V128:$vec, vec_i8imm_op:$idx),
5755ffd83dbSDimitry Andric             (outs), (ins vec_i8imm_op:$idx), [],
576e8d8bef9SDimitry Andric             vec.prefix#".extract_lane"#suffix#"\t$dst, $vec, $idx",
577e8d8bef9SDimitry Andric             vec.prefix#".extract_lane"#suffix#"\t$idx", simdop>;
5780b57cec5SDimitry Andric}
5790b57cec5SDimitry Andric
580e8d8bef9SDimitry Andricdefm "" : ExtractLane<I8x16, 21, "_s">;
581e8d8bef9SDimitry Andricdefm "" : ExtractLane<I8x16, 22, "_u">;
582e8d8bef9SDimitry Andricdefm "" : ExtractLane<I16x8, 24, "_s">;
583e8d8bef9SDimitry Andricdefm "" : ExtractLane<I16x8, 25, "_u">;
584e8d8bef9SDimitry Andricdefm "" : ExtractLane<I32x4, 27>;
585e8d8bef9SDimitry Andricdefm "" : ExtractLane<I64x2, 29>;
586e8d8bef9SDimitry Andricdefm "" : ExtractLane<F32x4, 31>;
587e8d8bef9SDimitry Andricdefm "" : ExtractLane<F64x2, 33>;
5880b57cec5SDimitry Andric
5895ffd83dbSDimitry Andricdef : Pat<(vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)),
590e8d8bef9SDimitry Andric          (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>;
5915ffd83dbSDimitry Andricdef : Pat<(vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)),
592e8d8bef9SDimitry Andric          (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>;
5935ffd83dbSDimitry Andricdef : Pat<(vector_extract (v4i32 V128:$vec), (i32 LaneIdx4:$idx)),
594e8d8bef9SDimitry Andric          (EXTRACT_LANE_I32x4 $vec, imm:$idx)>;
5955ffd83dbSDimitry Andricdef : Pat<(vector_extract (v4f32 V128:$vec), (i32 LaneIdx4:$idx)),
596e8d8bef9SDimitry Andric          (EXTRACT_LANE_F32x4 $vec, imm:$idx)>;
5975ffd83dbSDimitry Andricdef : Pat<(vector_extract (v2i64 V128:$vec), (i32 LaneIdx2:$idx)),
598e8d8bef9SDimitry Andric          (EXTRACT_LANE_I64x2 $vec, imm:$idx)>;
5995ffd83dbSDimitry Andricdef : Pat<(vector_extract (v2f64 V128:$vec), (i32 LaneIdx2:$idx)),
600e8d8bef9SDimitry Andric          (EXTRACT_LANE_F64x2 $vec, imm:$idx)>;
6010b57cec5SDimitry Andric
6025ffd83dbSDimitry Andricdef : Pat<
6035ffd83dbSDimitry Andric  (sext_inreg (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), i8),
604e8d8bef9SDimitry Andric  (EXTRACT_LANE_I8x16_s $vec, imm:$idx)>;
6055ffd83dbSDimitry Andricdef : Pat<
6065ffd83dbSDimitry Andric  (and (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), (i32 0xff)),
607e8d8bef9SDimitry Andric  (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>;
6085ffd83dbSDimitry Andricdef : Pat<
6095ffd83dbSDimitry Andric  (sext_inreg (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), i16),
610e8d8bef9SDimitry Andric  (EXTRACT_LANE_I16x8_s $vec, imm:$idx)>;
6115ffd83dbSDimitry Andricdef : Pat<
6125ffd83dbSDimitry Andric  (and (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), (i32 0xffff)),
613e8d8bef9SDimitry Andric  (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>;
6140b57cec5SDimitry Andric
6150b57cec5SDimitry Andric// Replace lane value: replace_lane
616e8d8bef9SDimitry Andricmulticlass ReplaceLane<Vec vec, bits<32> simdop> {
617e8d8bef9SDimitry Andric  defm REPLACE_LANE_#vec :
618e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$vec, vec_i8imm_op:$idx, vec.lane_rc:$x),
6190b57cec5SDimitry Andric           (outs), (ins vec_i8imm_op:$idx),
6200b57cec5SDimitry Andric           [(set V128:$dst, (vector_insert
621e8d8bef9SDimitry Andric             (vec.vt V128:$vec),
622e8d8bef9SDimitry Andric             (vec.lane_vt vec.lane_rc:$x),
623e8d8bef9SDimitry Andric             (i32 vec.lane_idx:$idx)))],
624e8d8bef9SDimitry Andric           vec.prefix#".replace_lane\t$dst, $vec, $idx, $x",
625e8d8bef9SDimitry Andric           vec.prefix#".replace_lane\t$idx", simdop>;
6260b57cec5SDimitry Andric}
6270b57cec5SDimitry Andric
628e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I8x16, 23>;
629e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I16x8, 26>;
630e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I32x4, 28>;
631e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I64x2, 30>;
632e8d8bef9SDimitry Andricdefm "" : ReplaceLane<F32x4, 32>;
633e8d8bef9SDimitry Andricdefm "" : ReplaceLane<F64x2, 34>;
6340b57cec5SDimitry Andric
6350b57cec5SDimitry Andric// Lower undef lane indices to zero
6360b57cec5SDimitry Andricdef : Pat<(vector_insert (v16i8 V128:$vec), I32:$x, undef),
637e8d8bef9SDimitry Andric          (REPLACE_LANE_I8x16 $vec, 0, $x)>;
6380b57cec5SDimitry Andricdef : Pat<(vector_insert (v8i16 V128:$vec), I32:$x, undef),
639e8d8bef9SDimitry Andric          (REPLACE_LANE_I16x8 $vec, 0, $x)>;
6400b57cec5SDimitry Andricdef : Pat<(vector_insert (v4i32 V128:$vec), I32:$x, undef),
641e8d8bef9SDimitry Andric          (REPLACE_LANE_I32x4 $vec, 0, $x)>;
6420b57cec5SDimitry Andricdef : Pat<(vector_insert (v2i64 V128:$vec), I64:$x, undef),
643e8d8bef9SDimitry Andric          (REPLACE_LANE_I64x2 $vec, 0, $x)>;
6440b57cec5SDimitry Andricdef : Pat<(vector_insert (v4f32 V128:$vec), F32:$x, undef),
645e8d8bef9SDimitry Andric          (REPLACE_LANE_F32x4 $vec, 0, $x)>;
6460b57cec5SDimitry Andricdef : Pat<(vector_insert (v2f64 V128:$vec), F64:$x, undef),
647e8d8bef9SDimitry Andric          (REPLACE_LANE_F64x2 $vec, 0, $x)>;
6480b57cec5SDimitry Andric
6490b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
6500b57cec5SDimitry Andric// Comparisons
6510b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
6520b57cec5SDimitry Andric
653e8d8bef9SDimitry Andricmulticlass SIMDCondition<Vec vec, string name, CondCode cond, bits<32> simdop> {
654e8d8bef9SDimitry Andric  defm _#vec :
6550b57cec5SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
656e8d8bef9SDimitry Andric           [(set (vec.int_vt V128:$dst),
657e8d8bef9SDimitry Andric             (setcc (vec.vt V128:$lhs), (vec.vt V128:$rhs), cond))],
658e8d8bef9SDimitry Andric           vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
659e8d8bef9SDimitry Andric           vec.prefix#"."#name, simdop>;
6600b57cec5SDimitry Andric}
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andricmulticlass SIMDConditionInt<string name, CondCode cond, bits<32> baseInst> {
663e8d8bef9SDimitry Andric  defm "" : SIMDCondition<I8x16, name, cond, baseInst>;
664e8d8bef9SDimitry Andric  defm "" : SIMDCondition<I16x8, name, cond, !add(baseInst, 10)>;
665e8d8bef9SDimitry Andric  defm "" : SIMDCondition<I32x4, name, cond, !add(baseInst, 20)>;
6660b57cec5SDimitry Andric}
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andricmulticlass SIMDConditionFP<string name, CondCode cond, bits<32> baseInst> {
669e8d8bef9SDimitry Andric  defm "" : SIMDCondition<F32x4, name, cond, baseInst>;
670e8d8bef9SDimitry Andric  defm "" : SIMDCondition<F64x2, name, cond, !add(baseInst, 6)>;
6710b57cec5SDimitry Andric}
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric// Equality: eq
6740b57cec5SDimitry Andriclet isCommutable = 1 in {
6755ffd83dbSDimitry Andricdefm EQ : SIMDConditionInt<"eq", SETEQ, 35>;
676*fe6060f1SDimitry Andricdefm EQ : SIMDCondition<I64x2, "eq", SETEQ, 214>;
6775ffd83dbSDimitry Andricdefm EQ : SIMDConditionFP<"eq", SETOEQ, 65>;
6780b57cec5SDimitry Andric} // isCommutable = 1
6790b57cec5SDimitry Andric
6800b57cec5SDimitry Andric// Non-equality: ne
6810b57cec5SDimitry Andriclet isCommutable = 1 in {
6825ffd83dbSDimitry Andricdefm NE : SIMDConditionInt<"ne", SETNE, 36>;
683*fe6060f1SDimitry Andricdefm NE : SIMDCondition<I64x2, "ne", SETNE, 215>;
6845ffd83dbSDimitry Andricdefm NE : SIMDConditionFP<"ne", SETUNE, 66>;
6850b57cec5SDimitry Andric} // isCommutable = 1
6860b57cec5SDimitry Andric
6870b57cec5SDimitry Andric// Less than: lt_s / lt_u / lt
6885ffd83dbSDimitry Andricdefm LT_S : SIMDConditionInt<"lt_s", SETLT, 37>;
689*fe6060f1SDimitry Andricdefm LT_S : SIMDCondition<I64x2, "lt_s", SETLT, 216>;
6905ffd83dbSDimitry Andricdefm LT_U : SIMDConditionInt<"lt_u", SETULT, 38>;
6915ffd83dbSDimitry Andricdefm LT : SIMDConditionFP<"lt", SETOLT, 67>;
6920b57cec5SDimitry Andric
6930b57cec5SDimitry Andric// Greater than: gt_s / gt_u / gt
6945ffd83dbSDimitry Andricdefm GT_S : SIMDConditionInt<"gt_s", SETGT, 39>;
695*fe6060f1SDimitry Andricdefm GT_S : SIMDCondition<I64x2, "gt_s", SETGT, 217>;
6965ffd83dbSDimitry Andricdefm GT_U : SIMDConditionInt<"gt_u", SETUGT, 40>;
6975ffd83dbSDimitry Andricdefm GT : SIMDConditionFP<"gt", SETOGT, 68>;
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric// Less than or equal: le_s / le_u / le
7005ffd83dbSDimitry Andricdefm LE_S : SIMDConditionInt<"le_s", SETLE, 41>;
701*fe6060f1SDimitry Andricdefm LE_S : SIMDCondition<I64x2, "le_s", SETLE, 218>;
7025ffd83dbSDimitry Andricdefm LE_U : SIMDConditionInt<"le_u", SETULE, 42>;
7035ffd83dbSDimitry Andricdefm LE : SIMDConditionFP<"le", SETOLE, 69>;
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric// Greater than or equal: ge_s / ge_u / ge
7065ffd83dbSDimitry Andricdefm GE_S : SIMDConditionInt<"ge_s", SETGE, 43>;
707*fe6060f1SDimitry Andricdefm GE_S : SIMDCondition<I64x2, "ge_s", SETGE, 219>;
7085ffd83dbSDimitry Andricdefm GE_U : SIMDConditionInt<"ge_u", SETUGE, 44>;
7095ffd83dbSDimitry Andricdefm GE : SIMDConditionFP<"ge", SETOGE, 70>;
7100b57cec5SDimitry Andric
7110b57cec5SDimitry Andric// Lower float comparisons that don't care about NaN to standard WebAssembly
7120b57cec5SDimitry Andric// float comparisons. These instructions are generated with nnan and in the
7130b57cec5SDimitry Andric// target-independent expansion of unordered comparisons and ordered ne.
714e8d8bef9SDimitry Andricforeach nodes = [[seteq, EQ_F32x4], [setne, NE_F32x4], [setlt, LT_F32x4],
715e8d8bef9SDimitry Andric                 [setgt, GT_F32x4], [setle, LE_F32x4], [setge, GE_F32x4]] in
7160b57cec5SDimitry Andricdef : Pat<(v4i32 (nodes[0] (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
717e8d8bef9SDimitry Andric          (nodes[1] $lhs, $rhs)>;
7180b57cec5SDimitry Andric
719e8d8bef9SDimitry Andricforeach nodes = [[seteq, EQ_F64x2], [setne, NE_F64x2], [setlt, LT_F64x2],
720e8d8bef9SDimitry Andric                 [setgt, GT_F64x2], [setle, LE_F64x2], [setge, GE_F64x2]] in
7210b57cec5SDimitry Andricdef : Pat<(v2i64 (nodes[0] (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
722e8d8bef9SDimitry Andric          (nodes[1] $lhs, $rhs)>;
723e8d8bef9SDimitry Andric
7240b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
7250b57cec5SDimitry Andric// Bitwise operations
7260b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
7270b57cec5SDimitry Andric
728*fe6060f1SDimitry Andricmulticlass SIMDBinary<Vec vec, SDPatternOperator node, string name, bits<32> simdop> {
729e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
7300b57cec5SDimitry Andric                      (outs), (ins),
731e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst),
732e8d8bef9SDimitry Andric                        (node (vec.vt V128:$lhs), (vec.vt V128:$rhs)))],
733e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
734e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
7350b57cec5SDimitry Andric}
7360b57cec5SDimitry Andric
737*fe6060f1SDimitry Andricmulticlass SIMDBitwise<SDPatternOperator node, string name, bits<32> simdop,
738*fe6060f1SDimitry Andric                       bit commutable = false> {
739e8d8bef9SDimitry Andric  let isCommutable = commutable in
740e8d8bef9SDimitry Andric  defm "" : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
741e8d8bef9SDimitry Andric                   (outs), (ins), [],
742e8d8bef9SDimitry Andric                   "v128."#name#"\t$dst, $lhs, $rhs", "v128."#name, simdop>;
743e8d8bef9SDimitry Andric  foreach vec = IntVecs in
744e8d8bef9SDimitry Andric  def : Pat<(node (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
745e8d8bef9SDimitry Andric            (!cast<NI>(NAME) $lhs, $rhs)>;
7460b57cec5SDimitry Andric}
7470b57cec5SDimitry Andric
748*fe6060f1SDimitry Andricmulticlass SIMDUnary<Vec vec, SDPatternOperator node, string name, bits<32> simdop> {
749e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins),
750e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst),
751e8d8bef9SDimitry Andric                        (vec.vt (node (vec.vt V128:$v))))],
752e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $v",
753e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
7540b57cec5SDimitry Andric}
7550b57cec5SDimitry Andric
7560b57cec5SDimitry Andric// Bitwise logic: v128.not
757e8d8bef9SDimitry Andricdefm NOT : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins), [],
758e8d8bef9SDimitry Andric                  "v128.not\t$dst, $v", "v128.not", 77>;
759e8d8bef9SDimitry Andricforeach vec = IntVecs in
760e8d8bef9SDimitry Andricdef : Pat<(vnot (vec.vt V128:$v)), (NOT $v)>;
7610b57cec5SDimitry Andric
7620b57cec5SDimitry Andric// Bitwise logic: v128.and / v128.or / v128.xor
763e8d8bef9SDimitry Andricdefm AND : SIMDBitwise<and, "and", 78, true>;
764e8d8bef9SDimitry Andricdefm OR : SIMDBitwise<or, "or", 80, true>;
765e8d8bef9SDimitry Andricdefm XOR : SIMDBitwise<xor, "xor", 81, true>;
7660b57cec5SDimitry Andric
7678bcb0991SDimitry Andric// Bitwise logic: v128.andnot
7688bcb0991SDimitry Andricdef andnot : PatFrag<(ops node:$left, node:$right), (and $left, (vnot $right))>;
7695ffd83dbSDimitry Andricdefm ANDNOT : SIMDBitwise<andnot, "andnot", 79>;
7708bcb0991SDimitry Andric
7710b57cec5SDimitry Andric// Bitwise select: v128.bitselect
772e8d8bef9SDimitry Andricdefm BITSELECT :
773e8d8bef9SDimitry Andric  SIMD_I<(outs V128:$dst), (ins V128:$v1, V128:$v2, V128:$c), (outs), (ins), [],
7745ffd83dbSDimitry Andric         "v128.bitselect\t$dst, $v1, $v2, $c", "v128.bitselect", 82>;
7750b57cec5SDimitry Andric
776e8d8bef9SDimitry Andricforeach vec = AllVecs in
777e8d8bef9SDimitry Andricdef : Pat<(vec.vt (int_wasm_bitselect
778e8d8bef9SDimitry Andric            (vec.vt V128:$v1), (vec.vt V128:$v2), (vec.vt V128:$c))),
779e8d8bef9SDimitry Andric          (BITSELECT $v1, $v2, $c)>;
780e8d8bef9SDimitry Andric
7810b57cec5SDimitry Andric// Bitselect is equivalent to (c & v1) | (~c & v2)
782e8d8bef9SDimitry Andricforeach vec = IntVecs in
783e8d8bef9SDimitry Andricdef : Pat<(vec.vt (or (and (vec.vt V128:$c), (vec.vt V128:$v1)),
784e8d8bef9SDimitry Andric            (and (vnot V128:$c), (vec.vt V128:$v2)))),
785e8d8bef9SDimitry Andric          (BITSELECT $v1, $v2, $c)>;
786e8d8bef9SDimitry Andric
787e8d8bef9SDimitry Andric// Also implement vselect in terms of bitselect
788e8d8bef9SDimitry Andricforeach vec = AllVecs in
789e8d8bef9SDimitry Andricdef : Pat<(vec.vt (vselect
790e8d8bef9SDimitry Andric            (vec.int_vt V128:$c), (vec.vt V128:$v1), (vec.vt V128:$v2))),
791e8d8bef9SDimitry Andric          (BITSELECT $v1, $v2, $c)>;
792e8d8bef9SDimitry Andric
793e8d8bef9SDimitry Andric// MVP select on v128 values
794e8d8bef9SDimitry Andricdefm SELECT_V128 :
795e8d8bef9SDimitry Andric  I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, I32:$cond), (outs), (ins), [],
796e8d8bef9SDimitry Andric    "v128.select\t$dst, $lhs, $rhs, $cond", "v128.select", 0x1b>;
797e8d8bef9SDimitry Andric
798e8d8bef9SDimitry Andricforeach vec = AllVecs in {
799e8d8bef9SDimitry Andricdef : Pat<(select I32:$cond, (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
800e8d8bef9SDimitry Andric          (SELECT_V128 $lhs, $rhs, $cond)>;
801e8d8bef9SDimitry Andric
802e8d8bef9SDimitry Andric// ISD::SELECT requires its operand to conform to getBooleanContents, but
803e8d8bef9SDimitry Andric// WebAssembly's select interprets any non-zero value as true, so we can fold
804e8d8bef9SDimitry Andric// a setne with 0 into a select.
805e8d8bef9SDimitry Andricdef : Pat<(select
806e8d8bef9SDimitry Andric            (i32 (setne I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
807e8d8bef9SDimitry Andric          (SELECT_V128 $lhs, $rhs, $cond)>;
808e8d8bef9SDimitry Andric
809e8d8bef9SDimitry Andric// And again, this time with seteq instead of setne and the arms reversed.
810e8d8bef9SDimitry Andricdef : Pat<(select
811e8d8bef9SDimitry Andric            (i32 (seteq I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
812e8d8bef9SDimitry Andric          (SELECT_V128 $rhs, $lhs, $cond)>;
813e8d8bef9SDimitry Andric} // foreach vec
814e8d8bef9SDimitry Andric
8150b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8160b57cec5SDimitry Andric// Integer unary arithmetic
8170b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8180b57cec5SDimitry Andric
819*fe6060f1SDimitry Andricmulticlass SIMDUnaryInt<SDPatternOperator node, string name, bits<32> baseInst> {
820e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I8x16, node, name, baseInst>;
821e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I16x8, node, name, !add(baseInst, 32)>;
822e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I32x4, node, name, !add(baseInst, 64)>;
823e8d8bef9SDimitry Andric  defm "" : SIMDUnary<I64x2, node, name, !add(baseInst, 96)>;
8240b57cec5SDimitry Andric}
8250b57cec5SDimitry Andric
8260b57cec5SDimitry Andric// Integer vector negation
827e8d8bef9SDimitry Andricdef ivneg : PatFrag<(ops node:$in), (sub immAllZerosV, $in)>;
8280b57cec5SDimitry Andric
8295ffd83dbSDimitry Andric// Integer absolute value: abs
8305ffd83dbSDimitry Andricdefm ABS : SIMDUnaryInt<abs, "abs", 96>;
8315ffd83dbSDimitry Andric
8320b57cec5SDimitry Andric// Integer negation: neg
8335ffd83dbSDimitry Andricdefm NEG : SIMDUnaryInt<ivneg, "neg", 97>;
8340b57cec5SDimitry Andric
835*fe6060f1SDimitry Andric// Population count: popcnt
836*fe6060f1SDimitry Andricdefm POPCNT : SIMDUnary<I8x16, ctpop, "popcnt", 0x62>;
837*fe6060f1SDimitry Andric
8380b57cec5SDimitry Andric// Any lane true: any_true
839*fe6060f1SDimitry Andricdefm ANYTRUE : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins), [],
840*fe6060f1SDimitry Andric                      "v128.any_true\t$dst, $vec", "v128.any_true", 0x53>;
841*fe6060f1SDimitry Andric
842*fe6060f1SDimitry Andricforeach vec = IntVecs in
843*fe6060f1SDimitry Andricdef : Pat<(int_wasm_anytrue (vec.vt V128:$vec)), (ANYTRUE V128:$vec)>;
8440b57cec5SDimitry Andric
8450b57cec5SDimitry Andric// All lanes true: all_true
846*fe6060f1SDimitry Andricmulticlass SIMDAllTrue<Vec vec, bits<32> simdop> {
847*fe6060f1SDimitry Andric  defm ALLTRUE_#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
848*fe6060f1SDimitry Andric                             [(set I32:$dst,
849*fe6060f1SDimitry Andric                               (i32 (int_wasm_alltrue (vec.vt V128:$vec))))],
850*fe6060f1SDimitry Andric                             vec.prefix#".all_true\t$dst, $vec",
851*fe6060f1SDimitry Andric                             vec.prefix#".all_true", simdop>;
852*fe6060f1SDimitry Andric}
8530b57cec5SDimitry Andric
854*fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I8x16, 0x63>;
855*fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I16x8, 0x83>;
856*fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I32x4, 0xa3>;
857*fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I64x2, 0xc3>;
858e8d8bef9SDimitry Andric
8590b57cec5SDimitry Andric// Reductions already return 0 or 1, so and 1, setne 0, and seteq 1
8600b57cec5SDimitry Andric// can be folded out
8610b57cec5SDimitry Andricforeach reduction =
862*fe6060f1SDimitry Andric  [["int_wasm_anytrue", "ANYTRUE", "I8x16"],
863*fe6060f1SDimitry Andric   ["int_wasm_anytrue", "ANYTRUE", "I16x8"],
864*fe6060f1SDimitry Andric   ["int_wasm_anytrue", "ANYTRUE", "I32x4"],
865*fe6060f1SDimitry Andric   ["int_wasm_anytrue", "ANYTRUE", "I64x2"],
866*fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I8x16", "I8x16"],
867*fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I16x8", "I16x8"],
868*fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I32x4", "I32x4"],
869*fe6060f1SDimitry Andric   ["int_wasm_alltrue", "ALLTRUE_I64x2", "I64x2"]] in {
870e8d8bef9SDimitry Andricdefvar intrinsic = !cast<Intrinsic>(reduction[0]);
871*fe6060f1SDimitry Andricdefvar inst = !cast<NI>(reduction[1]);
872*fe6060f1SDimitry Andricdefvar vec = !cast<Vec>(reduction[2]);
873e8d8bef9SDimitry Andricdef : Pat<(i32 (and (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>;
874e8d8bef9SDimitry Andricdef : Pat<(i32 (setne (i32 (intrinsic (vec.vt V128:$x))), (i32 0))), (inst $x)>;
875e8d8bef9SDimitry Andricdef : Pat<(i32 (seteq (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>;
8760b57cec5SDimitry Andric}
8770b57cec5SDimitry Andric
878e8d8bef9SDimitry Andricmulticlass SIMDBitmask<Vec vec, bits<32> simdop> {
879e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
8805ffd83dbSDimitry Andric                      [(set I32:$dst,
881e8d8bef9SDimitry Andric                         (i32 (int_wasm_bitmask (vec.vt V128:$vec))))],
882e8d8bef9SDimitry Andric                      vec.prefix#".bitmask\t$dst, $vec", vec.prefix#".bitmask",
883e8d8bef9SDimitry Andric                      simdop>;
8845ffd83dbSDimitry Andric}
8855ffd83dbSDimitry Andric
886e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I8x16, 100>;
887e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I16x8, 132>;
888e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I32x4, 164>;
889e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I64x2, 196>;
8905ffd83dbSDimitry Andric
8910b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8920b57cec5SDimitry Andric// Bit shifts
8930b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8940b57cec5SDimitry Andric
895e8d8bef9SDimitry Andricmulticlass SIMDShift<Vec vec, SDNode node, string name, bits<32> simdop> {
896e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$vec, I32:$x), (outs), (ins),
897e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst), (node V128:$vec, I32:$x))],
898e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $vec, $x",
899e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
9000b57cec5SDimitry Andric}
9010b57cec5SDimitry Andric
9020b57cec5SDimitry Andricmulticlass SIMDShiftInt<SDNode node, string name, bits<32> baseInst> {
903e8d8bef9SDimitry Andric  defm "" : SIMDShift<I8x16, node, name, baseInst>;
904e8d8bef9SDimitry Andric  defm "" : SIMDShift<I16x8, node, name, !add(baseInst, 32)>;
905e8d8bef9SDimitry Andric  defm "" : SIMDShift<I32x4, node, name, !add(baseInst, 64)>;
906e8d8bef9SDimitry Andric  defm "" : SIMDShift<I64x2, node, name, !add(baseInst, 96)>;
9070b57cec5SDimitry Andric}
9080b57cec5SDimitry Andric
9095ffd83dbSDimitry Andric// WebAssembly SIMD shifts are nonstandard in that the shift amount is
9105ffd83dbSDimitry Andric// an i32 rather than a vector, so they need custom nodes.
911e8d8bef9SDimitry Andricdef wasm_shift_t :
912e8d8bef9SDimitry Andric  SDTypeProfile<1, 2, [SDTCisVec<0>, SDTCisSameAs<0, 1>, SDTCisVT<2, i32>]>;
9130b57cec5SDimitry Andricdef wasm_shl : SDNode<"WebAssemblyISD::VEC_SHL", wasm_shift_t>;
9140b57cec5SDimitry Andricdef wasm_shr_s : SDNode<"WebAssemblyISD::VEC_SHR_S", wasm_shift_t>;
9150b57cec5SDimitry Andricdef wasm_shr_u : SDNode<"WebAssemblyISD::VEC_SHR_U", wasm_shift_t>;
9165ffd83dbSDimitry Andric
9175ffd83dbSDimitry Andric// Left shift by scalar: shl
9185ffd83dbSDimitry Andricdefm SHL : SIMDShiftInt<wasm_shl, "shl", 107>;
9195ffd83dbSDimitry Andric
9205ffd83dbSDimitry Andric// Right shift by scalar: shr_s / shr_u
9215ffd83dbSDimitry Andricdefm SHR_S : SIMDShiftInt<wasm_shr_s, "shr_s", 108>;
9225ffd83dbSDimitry Andricdefm SHR_U : SIMDShiftInt<wasm_shr_u, "shr_u", 109>;
9230b57cec5SDimitry Andric
924*fe6060f1SDimitry Andric// Optimize away an explicit mask on a shift count.
925*fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v16i8 V128:$lhs), (and I32:$rhs, 7)),
926*fe6060f1SDimitry Andric          (SHL_I8x16 V128:$lhs, I32:$rhs)>;
927*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v16i8 V128:$lhs), (and I32:$rhs, 7)),
928*fe6060f1SDimitry Andric          (SHR_S_I8x16 V128:$lhs, I32:$rhs)>;
929*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v16i8 V128:$lhs), (and I32:$rhs, 7)),
930*fe6060f1SDimitry Andric          (SHR_U_I8x16 V128:$lhs, I32:$rhs)>;
931*fe6060f1SDimitry Andric
932*fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v8i16 V128:$lhs), (and I32:$rhs, 15)),
933*fe6060f1SDimitry Andric          (SHL_I16x8 V128:$lhs, I32:$rhs)>;
934*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v8i16 V128:$lhs), (and I32:$rhs, 15)),
935*fe6060f1SDimitry Andric          (SHR_S_I16x8 V128:$lhs, I32:$rhs)>;
936*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v8i16 V128:$lhs), (and I32:$rhs, 15)),
937*fe6060f1SDimitry Andric          (SHR_U_I16x8 V128:$lhs, I32:$rhs)>;
938*fe6060f1SDimitry Andric
939*fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v4i32 V128:$lhs), (and I32:$rhs, 31)),
940*fe6060f1SDimitry Andric          (SHL_I32x4 V128:$lhs, I32:$rhs)>;
941*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v4i32 V128:$lhs), (and I32:$rhs, 31)),
942*fe6060f1SDimitry Andric          (SHR_S_I32x4 V128:$lhs, I32:$rhs)>;
943*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v4i32 V128:$lhs), (and I32:$rhs, 31)),
944*fe6060f1SDimitry Andric          (SHR_U_I32x4 V128:$lhs, I32:$rhs)>;
945*fe6060f1SDimitry Andric
946*fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
947*fe6060f1SDimitry Andric          (SHL_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
948*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
949*fe6060f1SDimitry Andric          (SHR_S_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
950*fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
951*fe6060f1SDimitry Andric          (SHR_U_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
952*fe6060f1SDimitry Andric
9530b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9540b57cec5SDimitry Andric// Integer binary arithmetic
9550b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9560b57cec5SDimitry Andric
957*fe6060f1SDimitry Andricmulticlass SIMDBinaryIntNoI8x16<SDPatternOperator node, string name, bits<32> baseInst> {
958e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>;
959e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>;
960e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>;
9615ffd83dbSDimitry Andric}
9625ffd83dbSDimitry Andric
963*fe6060f1SDimitry Andricmulticlass SIMDBinaryIntSmall<SDPatternOperator node, string name, bits<32> baseInst> {
964e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I8x16, node, name, baseInst>;
965e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>;
9660b57cec5SDimitry Andric}
9670b57cec5SDimitry Andric
968*fe6060f1SDimitry Andricmulticlass SIMDBinaryIntNoI64x2<SDPatternOperator node, string name, bits<32> baseInst> {
9690b57cec5SDimitry Andric  defm "" : SIMDBinaryIntSmall<node, name, baseInst>;
970e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>;
9710b57cec5SDimitry Andric}
9720b57cec5SDimitry Andric
973*fe6060f1SDimitry Andricmulticlass SIMDBinaryInt<SDPatternOperator node, string name, bits<32> baseInst> {
9740b57cec5SDimitry Andric  defm "" : SIMDBinaryIntNoI64x2<node, name, baseInst>;
975e8d8bef9SDimitry Andric  defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>;
9760b57cec5SDimitry Andric}
9770b57cec5SDimitry Andric
978*fe6060f1SDimitry Andric// Integer addition: add / add_sat_s / add_sat_u
9790b57cec5SDimitry Andriclet isCommutable = 1 in {
9805ffd83dbSDimitry Andricdefm ADD : SIMDBinaryInt<add, "add", 110>;
981*fe6060f1SDimitry Andricdefm ADD_SAT_S : SIMDBinaryIntSmall<saddsat, "add_sat_s", 111>;
982*fe6060f1SDimitry Andricdefm ADD_SAT_U : SIMDBinaryIntSmall<uaddsat, "add_sat_u", 112>;
9830b57cec5SDimitry Andric} // isCommutable = 1
9840b57cec5SDimitry Andric
985*fe6060f1SDimitry Andric// Integer subtraction: sub / sub_sat_s / sub_sat_u
9865ffd83dbSDimitry Andricdefm SUB : SIMDBinaryInt<sub, "sub", 113>;
9870b57cec5SDimitry Andricdefm SUB_SAT_S :
988*fe6060f1SDimitry Andric  SIMDBinaryIntSmall<int_wasm_sub_sat_signed, "sub_sat_s", 114>;
9890b57cec5SDimitry Andricdefm SUB_SAT_U :
990*fe6060f1SDimitry Andric  SIMDBinaryIntSmall<int_wasm_sub_sat_unsigned, "sub_sat_u", 115>;
9910b57cec5SDimitry Andric
9920b57cec5SDimitry Andric// Integer multiplication: mul
993480093f4SDimitry Andriclet isCommutable = 1 in
9945ffd83dbSDimitry Andricdefm MUL : SIMDBinaryIntNoI8x16<mul, "mul", 117>;
9950b57cec5SDimitry Andric
996480093f4SDimitry Andric// Integer min_s / min_u / max_s / max_u
997480093f4SDimitry Andriclet isCommutable = 1 in {
9985ffd83dbSDimitry Andricdefm MIN_S : SIMDBinaryIntNoI64x2<smin, "min_s", 118>;
9995ffd83dbSDimitry Andricdefm MIN_U : SIMDBinaryIntNoI64x2<umin, "min_u", 119>;
10005ffd83dbSDimitry Andricdefm MAX_S : SIMDBinaryIntNoI64x2<smax, "max_s", 120>;
10015ffd83dbSDimitry Andricdefm MAX_U : SIMDBinaryIntNoI64x2<umax, "max_u", 121>;
1002480093f4SDimitry Andric} // isCommutable = 1
1003480093f4SDimitry Andric
1004480093f4SDimitry Andric// Integer unsigned rounding average: avgr_u
10055ffd83dbSDimitry Andriclet isCommutable = 1 in {
1006e8d8bef9SDimitry Andricdefm AVGR_U : SIMDBinary<I8x16, int_wasm_avgr_unsigned, "avgr_u", 123>;
1007e8d8bef9SDimitry Andricdefm AVGR_U : SIMDBinary<I16x8, int_wasm_avgr_unsigned, "avgr_u", 155>;
1008480093f4SDimitry Andric}
1009480093f4SDimitry Andric
1010e8d8bef9SDimitry Andricdef add_nuw : PatFrag<(ops node:$lhs, node:$rhs), (add $lhs, $rhs),
1011480093f4SDimitry Andric                      "return N->getFlags().hasNoUnsignedWrap();">;
1012480093f4SDimitry Andric
1013e8d8bef9SDimitry Andricforeach vec = [I8x16, I16x8] in {
1014e8d8bef9SDimitry Andricdefvar inst = !cast<NI>("AVGR_U_"#vec);
10155ffd83dbSDimitry Andricdef : Pat<(wasm_shr_u
1016480093f4SDimitry Andric            (add_nuw
1017e8d8bef9SDimitry Andric              (add_nuw (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
1018e8d8bef9SDimitry Andric              (vec.splat (i32 1))),
1019e8d8bef9SDimitry Andric            (i32 1)),
1020e8d8bef9SDimitry Andric          (inst $lhs, $rhs)>;
1021e8d8bef9SDimitry Andric}
1022480093f4SDimitry Andric
1023480093f4SDimitry Andric// Widening dot product: i32x4.dot_i16x8_s
1024480093f4SDimitry Andriclet isCommutable = 1 in
1025480093f4SDimitry Andricdefm DOT : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
1026480093f4SDimitry Andric                  [(set V128:$dst, (int_wasm_dot V128:$lhs, V128:$rhs))],
1027480093f4SDimitry Andric                  "i32x4.dot_i16x8_s\t$dst, $lhs, $rhs", "i32x4.dot_i16x8_s",
1028e8d8bef9SDimitry Andric                  186>;
1029e8d8bef9SDimitry Andric
1030e8d8bef9SDimitry Andric// Extending multiplication: extmul_{low,high}_P, extmul_high
1031*fe6060f1SDimitry Andricdef extend_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1032*fe6060f1SDimitry Andricdef extend_low_s : SDNode<"WebAssemblyISD::EXTEND_LOW_S", extend_t>;
1033*fe6060f1SDimitry Andricdef extend_high_s : SDNode<"WebAssemblyISD::EXTEND_HIGH_S", extend_t>;
1034*fe6060f1SDimitry Andricdef extend_low_u : SDNode<"WebAssemblyISD::EXTEND_LOW_U", extend_t>;
1035*fe6060f1SDimitry Andricdef extend_high_u : SDNode<"WebAssemblyISD::EXTEND_HIGH_U", extend_t>;
1036*fe6060f1SDimitry Andric
1037*fe6060f1SDimitry Andricmulticlass SIMDExtBinary<Vec vec, SDPatternOperator node, string name,
1038*fe6060f1SDimitry Andric                         bits<32> simdop> {
1039e8d8bef9SDimitry Andric  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
1040e8d8bef9SDimitry Andric                      (outs), (ins),
1041e8d8bef9SDimitry Andric                      [(set (vec.vt V128:$dst), (node
1042e8d8bef9SDimitry Andric                         (vec.split.vt V128:$lhs),(vec.split.vt V128:$rhs)))],
1043e8d8bef9SDimitry Andric                      vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
1044e8d8bef9SDimitry Andric                      vec.prefix#"."#name, simdop>;
1045e8d8bef9SDimitry Andric}
1046e8d8bef9SDimitry Andric
1047*fe6060f1SDimitry Andricclass ExtMulPat<SDNode extend> :
1048*fe6060f1SDimitry Andric  PatFrag<(ops node:$lhs, node:$rhs),
1049*fe6060f1SDimitry Andric          (mul (extend $lhs), (extend $rhs))> {}
1050*fe6060f1SDimitry Andric
1051*fe6060f1SDimitry Andricdef extmul_low_s : ExtMulPat<extend_low_s>;
1052*fe6060f1SDimitry Andricdef extmul_high_s : ExtMulPat<extend_high_s>;
1053*fe6060f1SDimitry Andricdef extmul_low_u : ExtMulPat<extend_low_u>;
1054*fe6060f1SDimitry Andricdef extmul_high_u : ExtMulPat<extend_high_u>;
1055e8d8bef9SDimitry Andric
1056e8d8bef9SDimitry Andricdefm EXTMUL_LOW_S :
1057*fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_low_s, "extmul_low_i8x16_s", 0x9c>;
1058e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_S :
1059*fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_high_s, "extmul_high_i8x16_s", 0x9d>;
1060e8d8bef9SDimitry Andricdefm EXTMUL_LOW_U :
1061*fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_low_u, "extmul_low_i8x16_u", 0x9e>;
1062e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_U :
1063*fe6060f1SDimitry Andric  SIMDExtBinary<I16x8, extmul_high_u, "extmul_high_i8x16_u", 0x9f>;
1064e8d8bef9SDimitry Andric
1065e8d8bef9SDimitry Andricdefm EXTMUL_LOW_S :
1066*fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_low_s, "extmul_low_i16x8_s", 0xbc>;
1067e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_S :
1068*fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_high_s, "extmul_high_i16x8_s", 0xbd>;
1069e8d8bef9SDimitry Andricdefm EXTMUL_LOW_U :
1070*fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_low_u, "extmul_low_i16x8_u", 0xbe>;
1071e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_U :
1072*fe6060f1SDimitry Andric  SIMDExtBinary<I32x4, extmul_high_u, "extmul_high_i16x8_u", 0xbf>;
1073*fe6060f1SDimitry Andric
1074*fe6060f1SDimitry Andricdefm EXTMUL_LOW_S :
1075*fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_low_s, "extmul_low_i32x4_s", 0xdc>;
1076*fe6060f1SDimitry Andricdefm EXTMUL_HIGH_S :
1077*fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_high_s, "extmul_high_i32x4_s", 0xdd>;
1078*fe6060f1SDimitry Andricdefm EXTMUL_LOW_U :
1079*fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_low_u, "extmul_low_i32x4_u", 0xde>;
1080*fe6060f1SDimitry Andricdefm EXTMUL_HIGH_U :
1081*fe6060f1SDimitry Andric  SIMDExtBinary<I64x2, extmul_high_u, "extmul_high_i32x4_u", 0xdf>;
1082480093f4SDimitry Andric
10830b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
10840b57cec5SDimitry Andric// Floating-point unary arithmetic
10850b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
10860b57cec5SDimitry Andric
10870b57cec5SDimitry Andricmulticlass SIMDUnaryFP<SDNode node, string name, bits<32> baseInst> {
1088e8d8bef9SDimitry Andric  defm "" : SIMDUnary<F32x4, node, name, baseInst>;
1089e8d8bef9SDimitry Andric  defm "" : SIMDUnary<F64x2, node, name, !add(baseInst, 12)>;
10900b57cec5SDimitry Andric}
10910b57cec5SDimitry Andric
10920b57cec5SDimitry Andric// Absolute value: abs
10935ffd83dbSDimitry Andricdefm ABS : SIMDUnaryFP<fabs, "abs", 224>;
10940b57cec5SDimitry Andric
10950b57cec5SDimitry Andric// Negation: neg
10965ffd83dbSDimitry Andricdefm NEG : SIMDUnaryFP<fneg, "neg", 225>;
10970b57cec5SDimitry Andric
10980b57cec5SDimitry Andric// Square root: sqrt
10995ffd83dbSDimitry Andricdefm SQRT : SIMDUnaryFP<fsqrt, "sqrt", 227>;
11005ffd83dbSDimitry Andric
11015ffd83dbSDimitry Andric// Rounding: ceil, floor, trunc, nearest
1102*fe6060f1SDimitry Andricdefm CEIL : SIMDUnary<F32x4, fceil, "ceil", 0x67>;
1103*fe6060f1SDimitry Andricdefm FLOOR : SIMDUnary<F32x4, ffloor, "floor", 0x68>;
1104*fe6060f1SDimitry Andricdefm TRUNC: SIMDUnary<F32x4, ftrunc, "trunc", 0x69>;
1105*fe6060f1SDimitry Andricdefm NEAREST: SIMDUnary<F32x4, fnearbyint, "nearest", 0x6a>;
1106*fe6060f1SDimitry Andricdefm CEIL : SIMDUnary<F64x2, fceil, "ceil", 0x74>;
1107*fe6060f1SDimitry Andricdefm FLOOR : SIMDUnary<F64x2, ffloor, "floor", 0x75>;
1108*fe6060f1SDimitry Andricdefm TRUNC: SIMDUnary<F64x2, ftrunc, "trunc", 0x7a>;
1109*fe6060f1SDimitry Andricdefm NEAREST: SIMDUnary<F64x2, fnearbyint, "nearest", 0x94>;
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11120b57cec5SDimitry Andric// Floating-point binary arithmetic
11130b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11140b57cec5SDimitry Andric
1115*fe6060f1SDimitry Andricmulticlass SIMDBinaryFP<SDPatternOperator node, string name, bits<32> baseInst> {
1116e8d8bef9SDimitry Andric  defm "" : SIMDBinary<F32x4, node, name, baseInst>;
1117e8d8bef9SDimitry Andric  defm "" : SIMDBinary<F64x2, node, name, !add(baseInst, 12)>;
11180b57cec5SDimitry Andric}
11190b57cec5SDimitry Andric
11200b57cec5SDimitry Andric// Addition: add
11210b57cec5SDimitry Andriclet isCommutable = 1 in
11225ffd83dbSDimitry Andricdefm ADD : SIMDBinaryFP<fadd, "add", 228>;
11230b57cec5SDimitry Andric
11240b57cec5SDimitry Andric// Subtraction: sub
11255ffd83dbSDimitry Andricdefm SUB : SIMDBinaryFP<fsub, "sub", 229>;
11260b57cec5SDimitry Andric
11270b57cec5SDimitry Andric// Multiplication: mul
11280b57cec5SDimitry Andriclet isCommutable = 1 in
11295ffd83dbSDimitry Andricdefm MUL : SIMDBinaryFP<fmul, "mul", 230>;
11300b57cec5SDimitry Andric
11310b57cec5SDimitry Andric// Division: div
11325ffd83dbSDimitry Andricdefm DIV : SIMDBinaryFP<fdiv, "div", 231>;
11330b57cec5SDimitry Andric
11340b57cec5SDimitry Andric// NaN-propagating minimum: min
11355ffd83dbSDimitry Andricdefm MIN : SIMDBinaryFP<fminimum, "min", 232>;
11360b57cec5SDimitry Andric
11370b57cec5SDimitry Andric// NaN-propagating maximum: max
11385ffd83dbSDimitry Andricdefm MAX : SIMDBinaryFP<fmaximum, "max", 233>;
11395ffd83dbSDimitry Andric
11405ffd83dbSDimitry Andric// Pseudo-minimum: pmin
1141*fe6060f1SDimitry Andricdef pmin : PatFrag<(ops node:$lhs, node:$rhs),
1142*fe6060f1SDimitry Andric                   (vselect (setolt $rhs, $lhs), $rhs, $lhs)>;
1143*fe6060f1SDimitry Andricdefm PMIN : SIMDBinaryFP<pmin, "pmin", 234>;
11445ffd83dbSDimitry Andric
11455ffd83dbSDimitry Andric// Pseudo-maximum: pmax
1146*fe6060f1SDimitry Andricdef pmax : PatFrag<(ops node:$lhs, node:$rhs),
1147*fe6060f1SDimitry Andric                   (vselect (setolt $lhs, $rhs), $rhs, $lhs)>;
1148*fe6060f1SDimitry Andricdefm PMAX : SIMDBinaryFP<pmax, "pmax", 235>;
1149*fe6060f1SDimitry Andric
1150*fe6060f1SDimitry Andric// Also match the pmin/pmax cases where the operands are int vectors (but the
1151*fe6060f1SDimitry Andric// comparison is still a floating point comparison). This can happen when using
1152*fe6060f1SDimitry Andric// the wasm_simd128.h intrinsics because v128_t is an integer vector.
1153*fe6060f1SDimitry Andricforeach vec = [F32x4, F64x2] in {
1154*fe6060f1SDimitry Andricdefvar pmin = !cast<NI>("PMIN_"#vec);
1155*fe6060f1SDimitry Andricdefvar pmax = !cast<NI>("PMAX_"#vec);
1156*fe6060f1SDimitry Andricdef : Pat<(vec.int_vt (vselect
1157*fe6060f1SDimitry Andric            (setolt (vec.vt (bitconvert V128:$rhs)),
1158*fe6060f1SDimitry Andric                    (vec.vt (bitconvert V128:$lhs))),
1159*fe6060f1SDimitry Andric            V128:$rhs, V128:$lhs)),
1160*fe6060f1SDimitry Andric          (pmin $lhs, $rhs)>;
1161*fe6060f1SDimitry Andricdef : Pat<(vec.int_vt (vselect
1162*fe6060f1SDimitry Andric            (setolt (vec.vt (bitconvert V128:$lhs)),
1163*fe6060f1SDimitry Andric                    (vec.vt (bitconvert V128:$rhs))),
1164*fe6060f1SDimitry Andric            V128:$rhs, V128:$lhs)),
1165*fe6060f1SDimitry Andric          (pmax $lhs, $rhs)>;
1166*fe6060f1SDimitry Andric}
11670b57cec5SDimitry Andric
11680b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11690b57cec5SDimitry Andric// Conversions
11700b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
11710b57cec5SDimitry Andric
1172*fe6060f1SDimitry Andricmulticlass SIMDConvert<Vec vec, Vec arg, SDPatternOperator op, string name,
1173e8d8bef9SDimitry Andric                       bits<32> simdop> {
1174e8d8bef9SDimitry Andric  defm op#_#vec :
11750b57cec5SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins),
1176e8d8bef9SDimitry Andric           [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))],
1177e8d8bef9SDimitry Andric           vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop>;
11780b57cec5SDimitry Andric}
11790b57cec5SDimitry Andric
11800b57cec5SDimitry Andric// Floating point to integer with saturation: trunc_sat
1181e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, F32x4, fp_to_sint, "trunc_sat_f32x4_s", 248>;
1182e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, F32x4, fp_to_uint, "trunc_sat_f32x4_u", 249>;
11835ffd83dbSDimitry Andric
1184*fe6060f1SDimitry Andric// Support the saturating variety as well.
1185*fe6060f1SDimitry Andricdef trunc_s_sat32 : PatFrag<(ops node:$x), (fp_to_sint_sat $x, i32)>;
1186*fe6060f1SDimitry Andricdef trunc_u_sat32 : PatFrag<(ops node:$x), (fp_to_uint_sat $x, i32)>;
1187*fe6060f1SDimitry Andricdef : Pat<(v4i32 (trunc_s_sat32 (v4f32 V128:$src))), (fp_to_sint_I32x4 $src)>;
1188*fe6060f1SDimitry Andricdef : Pat<(v4i32 (trunc_u_sat32 (v4f32 V128:$src))), (fp_to_uint_I32x4 $src)>;
1189*fe6060f1SDimitry Andric
1190*fe6060f1SDimitry Andricdef trunc_sat_zero_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1191*fe6060f1SDimitry Andricdef trunc_sat_zero_s :
1192*fe6060f1SDimitry Andric  SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_S", trunc_sat_zero_t>;
1193*fe6060f1SDimitry Andricdef trunc_sat_zero_u :
1194*fe6060f1SDimitry Andric  SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_U", trunc_sat_zero_t>;
1195*fe6060f1SDimitry Andricdefm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_s, "trunc_sat_zero_f64x2_s",
1196*fe6060f1SDimitry Andric                      0xfc>;
1197*fe6060f1SDimitry Andricdefm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_u, "trunc_sat_zero_f64x2_u",
1198*fe6060f1SDimitry Andric                      0xfd>;
1199*fe6060f1SDimitry Andric
12005ffd83dbSDimitry Andric// Integer to floating point: convert
1201*fe6060f1SDimitry Andricdef convert_low_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1202*fe6060f1SDimitry Andricdef convert_low_s : SDNode<"WebAssemblyISD::CONVERT_LOW_S", convert_low_t>;
1203*fe6060f1SDimitry Andricdef convert_low_u : SDNode<"WebAssemblyISD::CONVERT_LOW_U", convert_low_t>;
1204e8d8bef9SDimitry Andricdefm "" : SIMDConvert<F32x4, I32x4, sint_to_fp, "convert_i32x4_s", 250>;
1205e8d8bef9SDimitry Andricdefm "" : SIMDConvert<F32x4, I32x4, uint_to_fp, "convert_i32x4_u", 251>;
1206*fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, I32x4, convert_low_s, "convert_low_i32x4_s", 0xfe>;
1207*fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, I32x4, convert_low_u, "convert_low_i32x4_u", 0xff>;
12088bcb0991SDimitry Andric
1209*fe6060f1SDimitry Andric// Extending operations
1210e8d8bef9SDimitry Andric// TODO: refactor this to be uniform for i64x2 if the numbering is not changed.
1211*fe6060f1SDimitry Andricmulticlass SIMDExtend<Vec vec, bits<32> baseInst> {
1212*fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_low_s,
1213*fe6060f1SDimitry Andric                        "extend_low_"#vec.split.prefix#"_s", baseInst>;
1214*fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_high_s,
1215*fe6060f1SDimitry Andric                        "extend_high_"#vec.split.prefix#"_s", !add(baseInst, 1)>;
1216*fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_low_u,
1217*fe6060f1SDimitry Andric                        "extend_low_"#vec.split.prefix#"_u", !add(baseInst, 2)>;
1218*fe6060f1SDimitry Andric  defm "" : SIMDConvert<vec, vec.split, extend_high_u,
1219*fe6060f1SDimitry Andric                        "extend_high_"#vec.split.prefix#"_u", !add(baseInst, 3)>;
1220e8d8bef9SDimitry Andric}
1221e8d8bef9SDimitry Andric
1222*fe6060f1SDimitry Andricdefm "" : SIMDExtend<I16x8, 0x87>;
1223*fe6060f1SDimitry Andricdefm "" : SIMDExtend<I32x4, 0xa7>;
1224*fe6060f1SDimitry Andricdefm "" : SIMDExtend<I64x2, 0xc7>;
1225e8d8bef9SDimitry Andric
1226e8d8bef9SDimitry Andric// Narrowing operations
1227e8d8bef9SDimitry Andricmulticlass SIMDNarrow<Vec vec, bits<32> baseInst> {
1228e8d8bef9SDimitry Andric  defvar name = vec.split.prefix#".narrow_"#vec.prefix;
1229e8d8bef9SDimitry Andric  defm NARROW_S_#vec.split :
1230e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins),
1231e8d8bef9SDimitry Andric           [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_signed
1232e8d8bef9SDimitry Andric             (vec.vt V128:$low), (vec.vt V128:$high))))],
1233e8d8bef9SDimitry Andric           name#"_s\t$dst, $low, $high", name#"_s", baseInst>;
1234e8d8bef9SDimitry Andric  defm NARROW_U_#vec.split :
1235e8d8bef9SDimitry Andric    SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins),
1236e8d8bef9SDimitry Andric           [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_unsigned
1237e8d8bef9SDimitry Andric             (vec.vt V128:$low), (vec.vt V128:$high))))],
1238e8d8bef9SDimitry Andric           name#"_u\t$dst, $low, $high", name#"_u", !add(baseInst, 1)>;
1239e8d8bef9SDimitry Andric}
1240e8d8bef9SDimitry Andric
1241e8d8bef9SDimitry Andricdefm "" : SIMDNarrow<I16x8, 101>;
1242e8d8bef9SDimitry Andricdefm "" : SIMDNarrow<I32x4, 133>;
1243e8d8bef9SDimitry Andric
1244e8d8bef9SDimitry Andric// Use narrowing operations for truncating stores. Since the narrowing
1245e8d8bef9SDimitry Andric// operations are saturating instead of truncating, we need to mask
1246e8d8bef9SDimitry Andric// the stored values first.
1247e8d8bef9SDimitry Andricdef store_v8i8_trunc_v8i16 :
1248e8d8bef9SDimitry Andric  OutPatFrag<(ops node:$val),
1249e8d8bef9SDimitry Andric             (EXTRACT_LANE_I64x2
1250e8d8bef9SDimitry Andric               (NARROW_U_I8x16
1251*fe6060f1SDimitry Andric                 (AND
1252*fe6060f1SDimitry Andric                   (CONST_V128_I16x8
1253*fe6060f1SDimitry Andric                     0x00ff, 0x00ff, 0x00ff, 0x00ff,
1254*fe6060f1SDimitry Andric                     0x00ff, 0x00ff, 0x00ff, 0x00ff),
1255*fe6060f1SDimitry Andric                   node:$val),
1256e8d8bef9SDimitry Andric                 $val), // Unused input
1257e8d8bef9SDimitry Andric               0)>;
1258e8d8bef9SDimitry Andric
1259e8d8bef9SDimitry Andricdef store_v4i16_trunc_v4i32 :
1260e8d8bef9SDimitry Andric  OutPatFrag<(ops node:$val),
1261e8d8bef9SDimitry Andric             (EXTRACT_LANE_I64x2
1262e8d8bef9SDimitry Andric               (NARROW_U_I16x8
1263*fe6060f1SDimitry Andric                 (AND
1264*fe6060f1SDimitry Andric                  (CONST_V128_I32x4
1265*fe6060f1SDimitry Andric                    0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff),
1266*fe6060f1SDimitry Andric                  node:$val),
1267e8d8bef9SDimitry Andric                 $val), // Unused input
1268e8d8bef9SDimitry Andric               0)>;
1269e8d8bef9SDimitry Andric
1270e8d8bef9SDimitry Andric// Store patterns adapted from WebAssemblyInstrMemory.td
1271e8d8bef9SDimitry Andricmulticlass NarrowingStorePatNoOffset<Vec vec, OutPatFrag out> {
1272e8d8bef9SDimitry Andric  defvar node = !cast<PatFrag>("truncstorevi"#vec.split.lane_bits);
1273e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, I32:$addr),
1274e8d8bef9SDimitry Andric            (STORE_I64_A32 0, 0, $addr, (out $val))>,
1275e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
1276e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, I64:$addr),
1277e8d8bef9SDimitry Andric            (STORE_I64_A64 0, 0, $addr, (out $val))>,
1278e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
1279e8d8bef9SDimitry Andric}
1280e8d8bef9SDimitry Andric
1281e8d8bef9SDimitry Andricdefm : NarrowingStorePatNoOffset<I16x8, store_v8i8_trunc_v8i16>;
1282e8d8bef9SDimitry Andricdefm : NarrowingStorePatNoOffset<I32x4, store_v4i16_trunc_v4i32>;
1283e8d8bef9SDimitry Andric
1284e8d8bef9SDimitry Andricmulticlass NarrowingStorePatImmOff<Vec vec, PatFrag operand, OutPatFrag out> {
1285e8d8bef9SDimitry Andric  defvar node = !cast<PatFrag>("truncstorevi"#vec.split.lane_bits);
1286e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, (operand I32:$addr, imm:$off)),
1287e8d8bef9SDimitry Andric            (STORE_I64_A32 0, imm:$off, $addr, (out $val))>,
1288e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
1289e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, (operand I64:$addr, imm:$off)),
1290e8d8bef9SDimitry Andric            (STORE_I64_A64 0, imm:$off, $addr, (out $val))>,
1291e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
1292e8d8bef9SDimitry Andric}
1293e8d8bef9SDimitry Andric
1294e8d8bef9SDimitry Andricdefm : NarrowingStorePatImmOff<I16x8, regPlusImm, store_v8i8_trunc_v8i16>;
1295e8d8bef9SDimitry Andricdefm : NarrowingStorePatImmOff<I32x4, regPlusImm, store_v4i16_trunc_v4i32>;
1296e8d8bef9SDimitry Andricdefm : NarrowingStorePatImmOff<I16x8, or_is_add, store_v8i8_trunc_v8i16>;
1297e8d8bef9SDimitry Andricdefm : NarrowingStorePatImmOff<I32x4, or_is_add, store_v4i16_trunc_v4i32>;
1298e8d8bef9SDimitry Andric
1299e8d8bef9SDimitry Andricmulticlass NarrowingStorePatOffsetOnly<Vec vec, OutPatFrag out> {
1300e8d8bef9SDimitry Andric  defvar node = !cast<PatFrag>("truncstorevi"#vec.split.lane_bits);
1301e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, imm:$off),
1302e8d8bef9SDimitry Andric            (STORE_I64_A32 0, imm:$off, (CONST_I32 0), (out $val))>,
1303e8d8bef9SDimitry Andric        Requires<[HasAddr32]>;
1304e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, imm:$off),
1305e8d8bef9SDimitry Andric            (STORE_I64_A64 0, imm:$off, (CONST_I64 0), (out $val))>,
1306e8d8bef9SDimitry Andric        Requires<[HasAddr64]>;
1307e8d8bef9SDimitry Andric}
1308e8d8bef9SDimitry Andric
1309e8d8bef9SDimitry Andricdefm : NarrowingStorePatOffsetOnly<I16x8, store_v8i8_trunc_v8i16>;
1310e8d8bef9SDimitry Andricdefm : NarrowingStorePatOffsetOnly<I32x4, store_v4i16_trunc_v4i32>;
1311e8d8bef9SDimitry Andric
1312e8d8bef9SDimitry Andricmulticlass NarrowingStorePatGlobalAddrOffOnly<Vec vec, OutPatFrag out> {
1313e8d8bef9SDimitry Andric  defvar node = !cast<PatFrag>("truncstorevi"#vec.split.lane_bits);
1314e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, (WebAssemblywrapper tglobaladdr:$off)),
1315e8d8bef9SDimitry Andric            (STORE_I64_A32 0, tglobaladdr:$off, (CONST_I32 0), (out $val))>,
1316e8d8bef9SDimitry Andric        Requires<[IsNotPIC, HasAddr32]>;
1317e8d8bef9SDimitry Andric  def : Pat<(node vec.vt:$val, (WebAssemblywrapper tglobaladdr:$off)),
1318e8d8bef9SDimitry Andric            (STORE_I64_A64  0, tglobaladdr:$off, (CONST_I64 0), (out $val))>,
1319e8d8bef9SDimitry Andric        Requires<[IsNotPIC, HasAddr64]>;
1320e8d8bef9SDimitry Andric}
1321e8d8bef9SDimitry Andric
1322e8d8bef9SDimitry Andricdefm : NarrowingStorePatGlobalAddrOffOnly<I16x8, store_v8i8_trunc_v8i16>;
1323e8d8bef9SDimitry Andricdefm : NarrowingStorePatGlobalAddrOffOnly<I32x4, store_v4i16_trunc_v4i32>;
13240b57cec5SDimitry Andric
13250b57cec5SDimitry Andric// Bitcasts are nops
13260b57cec5SDimitry Andric// Matching bitcast t1 to t1 causes strange errors, so avoid repeating types
1327*fe6060f1SDimitry Andricforeach t1 = AllVecs in
1328*fe6060f1SDimitry Andricforeach t2 = AllVecs in
1329*fe6060f1SDimitry Andricif !ne(t1, t2) then
1330*fe6060f1SDimitry Andricdef : Pat<(t1.vt (bitconvert (t2.vt V128:$v))), (t1.vt V128:$v)>;
13318bcb0991SDimitry Andric
1332e8d8bef9SDimitry Andric// Extended pairwise addition
1333e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_signed,
1334*fe6060f1SDimitry Andric                      "extadd_pairwise_i8x16_s", 0x7c>;
1335e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_unsigned,
1336*fe6060f1SDimitry Andric                      "extadd_pairwise_i8x16_u", 0x7d>;
1337e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_signed,
1338*fe6060f1SDimitry Andric                      "extadd_pairwise_i16x8_s", 0x7e>;
1339e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_unsigned,
1340*fe6060f1SDimitry Andric                      "extadd_pairwise_i16x8_u", 0x7f>;
1341e8d8bef9SDimitry Andric
1342*fe6060f1SDimitry Andric// f64x2 <-> f32x4 conversions
1343*fe6060f1SDimitry Andricdef demote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1344*fe6060f1SDimitry Andricdef demote_zero : SDNode<"WebAssemblyISD::DEMOTE_ZERO", demote_t>;
1345*fe6060f1SDimitry Andricdefm "" : SIMDConvert<F32x4, F64x2, demote_zero,
1346*fe6060f1SDimitry Andric                      "demote_zero_f64x2", 0x5e>;
1347e8d8bef9SDimitry Andric
1348*fe6060f1SDimitry Andricdef promote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1349*fe6060f1SDimitry Andricdef promote_low : SDNode<"WebAssemblyISD::PROMOTE_LOW", promote_t>;
1350*fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, F32x4, promote_low, "promote_low_f32x4", 0x5f>;
1351e8d8bef9SDimitry Andric
1352e8d8bef9SDimitry Andric//===----------------------------------------------------------------------===//
1353e8d8bef9SDimitry Andric// Saturating Rounding Q-Format Multiplication
1354e8d8bef9SDimitry Andric//===----------------------------------------------------------------------===//
1355e8d8bef9SDimitry Andric
1356e8d8bef9SDimitry Andricdefm Q15MULR_SAT_S :
1357*fe6060f1SDimitry Andric  SIMDBinary<I16x8, int_wasm_q15mulr_sat_signed, "q15mulr_sat_s", 0x82>;
1358