xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1// WebAssemblyInstrSIMD.td - WebAssembly SIMD codegen support -*- tablegen -*-//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// WebAssembly SIMD operand code-gen constructs.
11///
12//===----------------------------------------------------------------------===//
13
14// Instructions using the SIMD opcode prefix and requiring one of the SIMD
15// feature predicates.
16multiclass ABSTRACT_SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
17                           list<dag> pattern_r, string asmstr_r,
18                           string asmstr_s, bits<32> simdop,
19                           Predicate simd_level> {
20  defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s,
21              !if(!ge(simdop, 0x100),
22                  !or(0xfd0000, !and(0xffff, simdop)),
23                  !or(0xfd00, !and(0xff, simdop)))>,
24            Requires<[simd_level]>;
25}
26
27multiclass SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
28                  list<dag> pattern_r, string asmstr_r = "",
29                  string asmstr_s = "", bits<32> simdop = -1> {
30  defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r,
31                            asmstr_s, simdop, HasSIMD128>;
32}
33
34multiclass RELAXED_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
35                     list<dag> pattern_r, string asmstr_r = "",
36                     string asmstr_s = "", bits<32> simdop = -1> {
37  defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r,
38                            asmstr_s, simdop, HasRelaxedSIMD>;
39}
40
41
42defm "" : ARGUMENT<V128, v16i8>;
43defm "" : ARGUMENT<V128, v8i16>;
44defm "" : ARGUMENT<V128, v4i32>;
45defm "" : ARGUMENT<V128, v2i64>;
46defm "" : ARGUMENT<V128, v4f32>;
47defm "" : ARGUMENT<V128, v2f64>;
48
49// Constrained immediate argument types
50foreach SIZE = [8, 16] in
51def ImmI#SIZE : ImmLeaf<i32,
52  "return -(1 << ("#SIZE#" - 1)) <= Imm && Imm < (1 << ("#SIZE#" - 1));"
53>;
54foreach SIZE = [2, 4, 8, 16, 32] in
55def LaneIdx#SIZE : ImmLeaf<i32, "return 0 <= Imm && Imm < "#SIZE#";">;
56
57class Vec {
58  ValueType vt;
59  ValueType int_vt;
60  ValueType lane_vt;
61  WebAssemblyRegClass lane_rc;
62  int lane_bits;
63  ImmLeaf lane_idx;
64  SDPatternOperator lane_load;
65  PatFrag splat;
66  string prefix;
67  Vec split;
68}
69
70def I8x16 : Vec {
71  let vt = v16i8;
72  let int_vt = vt;
73  let lane_vt = i32;
74  let lane_rc = I32;
75  let lane_bits = 8;
76  let lane_idx = LaneIdx16;
77  let lane_load = extloadi8;
78  let splat = PatFrag<(ops node:$x), (v16i8 (splat_vector (i8 $x)))>;
79  let prefix = "i8x16";
80}
81
82def I16x8 : Vec {
83  let vt = v8i16;
84  let int_vt = vt;
85  let lane_vt = i32;
86  let lane_rc = I32;
87  let lane_bits = 16;
88  let lane_idx = LaneIdx8;
89  let lane_load = extloadi16;
90  let splat = PatFrag<(ops node:$x), (v8i16 (splat_vector (i16 $x)))>;
91  let prefix = "i16x8";
92  let split = I8x16;
93}
94
95def I32x4 : Vec {
96  let vt = v4i32;
97  let int_vt = vt;
98  let lane_vt = i32;
99  let lane_rc = I32;
100  let lane_bits = 32;
101  let lane_idx = LaneIdx4;
102  let lane_load = load;
103  let splat = PatFrag<(ops node:$x), (v4i32 (splat_vector (i32 $x)))>;
104  let prefix = "i32x4";
105  let split = I16x8;
106}
107
108def I64x2 : Vec {
109  let vt = v2i64;
110  let int_vt = vt;
111  let lane_vt = i64;
112  let lane_rc = I64;
113  let lane_bits = 64;
114  let lane_idx = LaneIdx2;
115  let lane_load = load;
116  let splat = PatFrag<(ops node:$x), (v2i64 (splat_vector (i64 $x)))>;
117  let prefix = "i64x2";
118  let split = I32x4;
119}
120
121def F32x4 : Vec {
122  let vt = v4f32;
123  let int_vt = v4i32;
124  let lane_vt = f32;
125  let lane_rc = F32;
126  let lane_bits = 32;
127  let lane_idx = LaneIdx4;
128  let lane_load = load;
129  let splat = PatFrag<(ops node:$x), (v4f32 (splat_vector (f32 $x)))>;
130  let prefix = "f32x4";
131}
132
133def F64x2 : Vec {
134  let vt = v2f64;
135  let int_vt = v2i64;
136  let lane_vt = f64;
137  let lane_rc = F64;
138  let lane_bits = 64;
139  let lane_idx = LaneIdx2;
140  let lane_load = load;
141  let splat = PatFrag<(ops node:$x), (v2f64 (splat_vector (f64 $x)))>;
142  let prefix = "f64x2";
143}
144
145defvar AllVecs = [I8x16, I16x8, I32x4, I64x2, F32x4, F64x2];
146defvar IntVecs = [I8x16, I16x8, I32x4, I64x2];
147
148//===----------------------------------------------------------------------===//
149// Load and store
150//===----------------------------------------------------------------------===//
151
152// Load: v128.load
153let mayLoad = 1, UseNamedOperandTable = 1 in {
154defm LOAD_V128_A32 :
155  SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
156         (outs), (ins P2Align:$p2align, offset32_op:$off), [],
157         "v128.load\t$dst, ${off}(${addr})$p2align",
158         "v128.load\t$off$p2align", 0>;
159defm LOAD_V128_A64 :
160  SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
161         (outs), (ins P2Align:$p2align, offset64_op:$off), [],
162         "v128.load\t$dst, ${off}(${addr})$p2align",
163         "v128.load\t$off$p2align", 0>;
164}
165
166// Def load patterns from WebAssemblyInstrMemory.td for vector types
167foreach vec = AllVecs in {
168defm : LoadPat<vec.vt, load, "LOAD_V128">;
169}
170
171// v128.loadX_splat
172multiclass SIMDLoadSplat<int size, bits<32> simdop> {
173  let mayLoad = 1, UseNamedOperandTable = 1 in {
174  defm LOAD#size#_SPLAT_A32 :
175    SIMD_I<(outs V128:$dst),
176           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
177           (outs),
178           (ins P2Align:$p2align, offset32_op:$off), [],
179           "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align",
180           "v128.load"#size#"_splat\t$off$p2align", simdop>;
181  defm LOAD#size#_SPLAT_A64 :
182    SIMD_I<(outs V128:$dst),
183           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
184           (outs),
185           (ins P2Align:$p2align, offset64_op:$off), [],
186           "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align",
187           "v128.load"#size#"_splat\t$off$p2align", simdop>;
188  }
189}
190
191defm "" : SIMDLoadSplat<8, 7>;
192defm "" : SIMDLoadSplat<16, 8>;
193defm "" : SIMDLoadSplat<32, 9>;
194defm "" : SIMDLoadSplat<64, 10>;
195
196foreach vec = AllVecs in {
197  defvar inst = "LOAD"#vec.lane_bits#"_SPLAT";
198  defm : LoadPat<vec.vt,
199                 PatFrag<(ops node:$addr), (splat_vector (vec.lane_vt (vec.lane_load node:$addr)))>,
200                 inst>;
201}
202
203// Load and extend
204multiclass SIMDLoadExtend<Vec vec, string loadPat, bits<32> simdop> {
205  defvar signed = vec.prefix#".load"#loadPat#"_s";
206  defvar unsigned = vec.prefix#".load"#loadPat#"_u";
207  let mayLoad = 1, UseNamedOperandTable = 1 in {
208  defm LOAD_EXTEND_S_#vec#_A32 :
209    SIMD_I<(outs V128:$dst),
210           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
211           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
212           signed#"\t$dst, ${off}(${addr})$p2align",
213           signed#"\t$off$p2align", simdop>;
214  defm LOAD_EXTEND_U_#vec#_A32 :
215    SIMD_I<(outs V128:$dst),
216           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
217           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
218           unsigned#"\t$dst, ${off}(${addr})$p2align",
219           unsigned#"\t$off$p2align", !add(simdop, 1)>;
220  defm LOAD_EXTEND_S_#vec#_A64 :
221    SIMD_I<(outs V128:$dst),
222           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
223           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
224           signed#"\t$dst, ${off}(${addr})$p2align",
225           signed#"\t$off$p2align", simdop>;
226  defm LOAD_EXTEND_U_#vec#_A64 :
227    SIMD_I<(outs V128:$dst),
228           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
229           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
230           unsigned#"\t$dst, ${off}(${addr})$p2align",
231           unsigned#"\t$off$p2align", !add(simdop, 1)>;
232  }
233}
234
235defm "" : SIMDLoadExtend<I16x8, "8x8", 1>;
236defm "" : SIMDLoadExtend<I32x4, "16x4", 3>;
237defm "" : SIMDLoadExtend<I64x2, "32x2", 5>;
238
239foreach vec = [I16x8, I32x4, I64x2] in
240foreach exts = [["sextloadvi", "_S"],
241                ["zextloadvi", "_U"],
242                ["extloadvi", "_U"]] in {
243defvar loadpat = !cast<PatFrag>(exts[0]#vec.split.lane_bits);
244defvar inst = "LOAD_EXTEND"#exts[1]#"_"#vec;
245defm : LoadPat<vec.vt, loadpat, inst>;
246}
247
248// Load lane into zero vector
249multiclass SIMDLoadZero<Vec vec, bits<32> simdop> {
250  defvar name = "v128.load"#vec.lane_bits#"_zero";
251  let mayLoad = 1, UseNamedOperandTable = 1 in {
252  defm LOAD_ZERO_#vec#_A32 :
253    SIMD_I<(outs V128:$dst),
254           (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
255           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
256           name#"\t$dst, ${off}(${addr})$p2align",
257           name#"\t$off$p2align", simdop>;
258  defm LOAD_ZERO_#vec#_A64 :
259    SIMD_I<(outs V128:$dst),
260           (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
261           (outs), (ins P2Align:$p2align, offset64_op:$off), [],
262           name#"\t$dst, ${off}(${addr})$p2align",
263           name#"\t$off$p2align", simdop>;
264  } // mayLoad = 1, UseNamedOperandTable = 1
265}
266
267defm "" : SIMDLoadZero<I32x4, 0x5c>;
268defm "" : SIMDLoadZero<I64x2, 0x5d>;
269
270// Use load_zero to load scalars into vectors as well where possible.
271// TODO: i16, and i8 scalars
272foreach vec = [I32x4, I64x2] in {
273  defvar inst = "LOAD_ZERO_"#vec;
274  defvar pat = PatFrag<(ops node:$addr), (scalar_to_vector (vec.lane_vt (load $addr)))>;
275  defm : LoadPat<vec.vt, pat, inst>;
276}
277
278// TODO: f32x4 and f64x2 as well
279foreach vec = [I32x4, I64x2] in {
280  defvar inst = "LOAD_ZERO_"#vec;
281  defvar pat = PatFrag<(ops node:$ptr),
282    (vector_insert (vec.splat (vec.lane_vt 0)), (vec.lane_vt (load $ptr)), 0)>;
283  defm : LoadPat<vec.vt, pat, inst>;
284}
285
286// Load lane
287multiclass SIMDLoadLane<Vec vec, bits<32> simdop> {
288  defvar name = "v128.load"#vec.lane_bits#"_lane";
289  let mayLoad = 1, UseNamedOperandTable = 1 in {
290  defm LOAD_LANE_#vec#_A32 :
291    SIMD_I<(outs V128:$dst),
292           (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx,
293                I32:$addr, V128:$vec),
294           (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx),
295           [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx",
296           name#"\t$off$p2align, $idx", simdop>;
297  defm LOAD_LANE_#vec#_A64 :
298    SIMD_I<(outs V128:$dst),
299           (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx,
300                I64:$addr, V128:$vec),
301           (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx),
302           [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx",
303           name#"\t$off$p2align, $idx", simdop>;
304  } // mayLoad = 1, UseNamedOperandTable = 1
305}
306
307defm "" : SIMDLoadLane<I8x16, 0x54>;
308defm "" : SIMDLoadLane<I16x8, 0x55>;
309defm "" : SIMDLoadLane<I32x4, 0x56>;
310defm "" : SIMDLoadLane<I64x2, 0x57>;
311
312// Select loads with no constant offset.
313multiclass LoadLanePatNoOffset<Vec vec, SDPatternOperator kind> {
314  defvar load_lane_a32 = !cast<NI>("LOAD_LANE_"#vec#"_A32");
315  defvar load_lane_a64 = !cast<NI>("LOAD_LANE_"#vec#"_A64");
316  def : Pat<(vec.vt (kind (i32 I32:$addr),
317              (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))),
318            (load_lane_a32 0, 0, imm:$idx, $addr, $vec)>,
319        Requires<[HasAddr32]>;
320  def : Pat<(vec.vt (kind (i64 I64:$addr),
321              (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))),
322            (load_lane_a64 0, 0, imm:$idx, $addr, $vec)>,
323        Requires<[HasAddr64]>;
324}
325
326def load8_lane :
327  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
328          (vector_insert $vec, (i32 (extloadi8 $ptr)), $idx)>;
329def load16_lane :
330  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
331          (vector_insert $vec, (i32 (extloadi16 $ptr)), $idx)>;
332def load32_lane :
333  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
334          (vector_insert $vec, (i32 (load $ptr)), $idx)>;
335def load64_lane :
336  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
337          (vector_insert $vec, (i64 (load $ptr)), $idx)>;
338// TODO: floating point lanes as well
339
340defm : LoadLanePatNoOffset<I8x16, load8_lane>;
341defm : LoadLanePatNoOffset<I16x8, load16_lane>;
342defm : LoadLanePatNoOffset<I32x4, load32_lane>;
343defm : LoadLanePatNoOffset<I64x2, load64_lane>;
344
345// TODO: Also support the other load patterns for load_lane once the instructions
346// are merged to the proposal.
347
348// Store: v128.store
349let mayStore = 1, UseNamedOperandTable = 1 in {
350defm STORE_V128_A32 :
351  SIMD_I<(outs), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, V128:$vec),
352         (outs), (ins P2Align:$p2align, offset32_op:$off), [],
353         "v128.store\t${off}(${addr})$p2align, $vec",
354         "v128.store\t$off$p2align", 11>;
355defm STORE_V128_A64 :
356  SIMD_I<(outs), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, V128:$vec),
357         (outs), (ins P2Align:$p2align, offset64_op:$off), [],
358         "v128.store\t${off}(${addr})$p2align, $vec",
359         "v128.store\t$off$p2align", 11>;
360}
361
362// Def store patterns from WebAssemblyInstrMemory.td for vector types
363foreach vec = AllVecs in {
364defm : StorePat<vec.vt, store, "STORE_V128">;
365}
366
367// Store lane
368multiclass SIMDStoreLane<Vec vec, bits<32> simdop> {
369  defvar name = "v128.store"#vec.lane_bits#"_lane";
370  let mayStore = 1, UseNamedOperandTable = 1 in {
371  defm STORE_LANE_#vec#_A32 :
372    SIMD_I<(outs),
373           (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx,
374                I32:$addr, V128:$vec),
375           (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx),
376           [], name#"\t${off}(${addr})$p2align, $vec, $idx",
377           name#"\t$off$p2align, $idx", simdop>;
378  defm STORE_LANE_#vec#_A64 :
379    SIMD_I<(outs V128:$dst),
380           (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx,
381                I64:$addr, V128:$vec),
382           (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx),
383           [], name#"\t${off}(${addr})$p2align, $vec, $idx",
384           name#"\t$off$p2align, $idx", simdop>;
385  } // mayStore = 1, UseNamedOperandTable = 1
386}
387
388defm "" : SIMDStoreLane<I8x16, 0x58>;
389defm "" : SIMDStoreLane<I16x8, 0x59>;
390defm "" : SIMDStoreLane<I32x4, 0x5a>;
391defm "" : SIMDStoreLane<I64x2, 0x5b>;
392
393multiclass StoreLanePat<Vec vec, SDPatternOperator kind> {
394  def : Pat<(kind (AddrOps32 offset32_op:$offset, I32:$addr),
395                  (vec.vt V128:$vec),
396                  (i32 vec.lane_idx:$idx)),
397            (!cast<NI>("STORE_LANE_"#vec#"_A32") 0, $offset, imm:$idx, $addr, $vec)>,
398        Requires<[HasAddr32]>;
399  def : Pat<(kind (AddrOps64 offset64_op:$offset, I64:$addr),
400                  (vec.vt V128:$vec),
401                  (i32 vec.lane_idx:$idx)),
402            (!cast<NI>("STORE_LANE_"#vec#"_A64") 0, $offset, imm:$idx, $addr, $vec)>,
403        Requires<[HasAddr64]>;
404}
405
406def store8_lane :
407  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
408          (truncstorei8 (i32 (vector_extract $vec, $idx)), $ptr)>;
409def store16_lane :
410  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
411          (truncstorei16 (i32 (vector_extract $vec, $idx)), $ptr)>;
412def store32_lane :
413  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
414          (store (i32 (vector_extract $vec, $idx)), $ptr)>;
415def store64_lane :
416  PatFrag<(ops node:$ptr, node:$vec, node:$idx),
417          (store (i64 (vector_extract $vec, $idx)), $ptr)>;
418// TODO: floating point lanes as well
419
420let AddedComplexity = 1 in {
421defm : StoreLanePat<I8x16, store8_lane>;
422defm : StoreLanePat<I16x8, store16_lane>;
423defm : StoreLanePat<I32x4, store32_lane>;
424defm : StoreLanePat<I64x2, store64_lane>;
425}
426
427//===----------------------------------------------------------------------===//
428// Constructing SIMD values
429//===----------------------------------------------------------------------===//
430
431// Constant: v128.const
432multiclass ConstVec<Vec vec, dag ops, dag pat, string args> {
433  let isMoveImm = 1, isReMaterializable = 1 in
434  defm CONST_V128_#vec : SIMD_I<(outs V128:$dst), ops, (outs), ops,
435                                 [(set V128:$dst, (vec.vt pat))],
436                                 "v128.const\t$dst, "#args,
437                                 "v128.const\t"#args, 12>;
438}
439
440defm "" : ConstVec<I8x16,
441                   (ins vec_i8imm_op:$i0, vec_i8imm_op:$i1,
442                        vec_i8imm_op:$i2, vec_i8imm_op:$i3,
443                        vec_i8imm_op:$i4, vec_i8imm_op:$i5,
444                        vec_i8imm_op:$i6, vec_i8imm_op:$i7,
445                        vec_i8imm_op:$i8, vec_i8imm_op:$i9,
446                        vec_i8imm_op:$iA, vec_i8imm_op:$iB,
447                        vec_i8imm_op:$iC, vec_i8imm_op:$iD,
448                        vec_i8imm_op:$iE, vec_i8imm_op:$iF),
449                   (build_vector ImmI8:$i0, ImmI8:$i1, ImmI8:$i2, ImmI8:$i3,
450                                 ImmI8:$i4, ImmI8:$i5, ImmI8:$i6, ImmI8:$i7,
451                                 ImmI8:$i8, ImmI8:$i9, ImmI8:$iA, ImmI8:$iB,
452                                 ImmI8:$iC, ImmI8:$iD, ImmI8:$iE, ImmI8:$iF),
453                   !strconcat("$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7, ",
454                              "$i8, $i9, $iA, $iB, $iC, $iD, $iE, $iF")>;
455defm "" : ConstVec<I16x8,
456                   (ins vec_i16imm_op:$i0, vec_i16imm_op:$i1,
457                        vec_i16imm_op:$i2, vec_i16imm_op:$i3,
458                        vec_i16imm_op:$i4, vec_i16imm_op:$i5,
459                        vec_i16imm_op:$i6, vec_i16imm_op:$i7),
460                   (build_vector
461                     ImmI16:$i0, ImmI16:$i1, ImmI16:$i2, ImmI16:$i3,
462                     ImmI16:$i4, ImmI16:$i5, ImmI16:$i6, ImmI16:$i7),
463                   "$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7">;
464let IsCanonical = 1 in
465defm "" : ConstVec<I32x4,
466                   (ins vec_i32imm_op:$i0, vec_i32imm_op:$i1,
467                        vec_i32imm_op:$i2, vec_i32imm_op:$i3),
468                   (build_vector (i32 imm:$i0), (i32 imm:$i1),
469                                 (i32 imm:$i2), (i32 imm:$i3)),
470                   "$i0, $i1, $i2, $i3">;
471defm "" : ConstVec<I64x2,
472                   (ins vec_i64imm_op:$i0, vec_i64imm_op:$i1),
473                   (build_vector (i64 imm:$i0), (i64 imm:$i1)),
474                   "$i0, $i1">;
475defm "" : ConstVec<F32x4,
476                   (ins f32imm_op:$i0, f32imm_op:$i1,
477                        f32imm_op:$i2, f32imm_op:$i3),
478                   (build_vector (f32 fpimm:$i0), (f32 fpimm:$i1),
479                                 (f32 fpimm:$i2), (f32 fpimm:$i3)),
480                   "$i0, $i1, $i2, $i3">;
481defm "" : ConstVec<F64x2,
482                  (ins f64imm_op:$i0, f64imm_op:$i1),
483                  (build_vector (f64 fpimm:$i0), (f64 fpimm:$i1)),
484                  "$i0, $i1">;
485
486// Match splat(x) -> const.v128(x, ..., x)
487foreach vec = AllVecs in {
488  defvar numEls = !div(vec.vt.Size, vec.lane_bits);
489  defvar isFloat = !or(!eq(vec.lane_vt, f32), !eq(vec.lane_vt, f64));
490  defvar immKind = !if(isFloat, fpimm, imm);
491  def : Pat<(vec.splat (vec.lane_vt immKind:$x)),
492            !dag(!cast<NI>("CONST_V128_"#vec),
493                 !listsplat((vec.lane_vt immKind:$x), numEls),
494                 ?)>;
495}
496
497// Shuffle lanes: shuffle
498defm SHUFFLE :
499  SIMD_I<(outs V128:$dst),
500         (ins V128:$x, V128:$y,
501           vec_i8imm_op:$m0, vec_i8imm_op:$m1,
502           vec_i8imm_op:$m2, vec_i8imm_op:$m3,
503           vec_i8imm_op:$m4, vec_i8imm_op:$m5,
504           vec_i8imm_op:$m6, vec_i8imm_op:$m7,
505           vec_i8imm_op:$m8, vec_i8imm_op:$m9,
506           vec_i8imm_op:$mA, vec_i8imm_op:$mB,
507           vec_i8imm_op:$mC, vec_i8imm_op:$mD,
508           vec_i8imm_op:$mE, vec_i8imm_op:$mF),
509         (outs),
510         (ins
511           vec_i8imm_op:$m0, vec_i8imm_op:$m1,
512           vec_i8imm_op:$m2, vec_i8imm_op:$m3,
513           vec_i8imm_op:$m4, vec_i8imm_op:$m5,
514           vec_i8imm_op:$m6, vec_i8imm_op:$m7,
515           vec_i8imm_op:$m8, vec_i8imm_op:$m9,
516           vec_i8imm_op:$mA, vec_i8imm_op:$mB,
517           vec_i8imm_op:$mC, vec_i8imm_op:$mD,
518           vec_i8imm_op:$mE, vec_i8imm_op:$mF),
519         [],
520         "i8x16.shuffle\t$dst, $x, $y, "#
521           "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "#
522           "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF",
523         "i8x16.shuffle\t"#
524           "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "#
525           "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF",
526         13>;
527
528// Shuffles after custom lowering
529def wasm_shuffle_t : SDTypeProfile<1, 18, []>;
530def wasm_shuffle : SDNode<"WebAssemblyISD::SHUFFLE", wasm_shuffle_t>;
531foreach vec = AllVecs in {
532def : Pat<(vec.vt (wasm_shuffle (vec.vt V128:$x), (vec.vt V128:$y),
533            (i32 LaneIdx32:$m0), (i32 LaneIdx32:$m1),
534            (i32 LaneIdx32:$m2), (i32 LaneIdx32:$m3),
535            (i32 LaneIdx32:$m4), (i32 LaneIdx32:$m5),
536            (i32 LaneIdx32:$m6), (i32 LaneIdx32:$m7),
537            (i32 LaneIdx32:$m8), (i32 LaneIdx32:$m9),
538            (i32 LaneIdx32:$mA), (i32 LaneIdx32:$mB),
539            (i32 LaneIdx32:$mC), (i32 LaneIdx32:$mD),
540            (i32 LaneIdx32:$mE), (i32 LaneIdx32:$mF))),
541          (SHUFFLE $x, $y,
542            imm:$m0, imm:$m1, imm:$m2, imm:$m3,
543            imm:$m4, imm:$m5, imm:$m6, imm:$m7,
544            imm:$m8, imm:$m9, imm:$mA, imm:$mB,
545            imm:$mC, imm:$mD, imm:$mE, imm:$mF)>;
546}
547
548// Swizzle lanes: i8x16.swizzle
549def wasm_swizzle_t : SDTypeProfile<1, 2, []>;
550def wasm_swizzle : SDNode<"WebAssemblyISD::SWIZZLE", wasm_swizzle_t>;
551defm SWIZZLE :
552  SIMD_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins),
553         [(set (v16i8 V128:$dst),
554           (wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))],
555         "i8x16.swizzle\t$dst, $src, $mask", "i8x16.swizzle", 14>;
556
557def : Pat<(int_wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)),
558          (SWIZZLE $src, $mask)>;
559
560multiclass Splat<Vec vec, bits<32> simdop> {
561  defm SPLAT_#vec : SIMD_I<(outs V128:$dst), (ins vec.lane_rc:$x),
562                           (outs), (ins),
563                           [(set (vec.vt V128:$dst),
564                              (vec.splat vec.lane_rc:$x))],
565                           vec.prefix#".splat\t$dst, $x", vec.prefix#".splat",
566                           simdop>;
567}
568
569defm "" : Splat<I8x16, 15>;
570defm "" : Splat<I16x8, 16>;
571defm "" : Splat<I32x4, 17>;
572defm "" : Splat<I64x2, 18>;
573defm "" : Splat<F32x4, 19>;
574defm "" : Splat<F64x2, 20>;
575
576// scalar_to_vector leaves high lanes undefined, so can be a splat
577foreach vec = AllVecs in
578def : Pat<(vec.vt (scalar_to_vector (vec.lane_vt vec.lane_rc:$x))),
579          (!cast<Instruction>("SPLAT_"#vec) $x)>;
580
581//===----------------------------------------------------------------------===//
582// Accessing lanes
583//===----------------------------------------------------------------------===//
584
585// Extract lane as a scalar: extract_lane / extract_lane_s / extract_lane_u
586multiclass ExtractLane<Vec vec, bits<32> simdop, string suffix = ""> {
587  defm EXTRACT_LANE_#vec#suffix :
588      SIMD_I<(outs vec.lane_rc:$dst), (ins V128:$vec, vec_i8imm_op:$idx),
589             (outs), (ins vec_i8imm_op:$idx), [],
590             vec.prefix#".extract_lane"#suffix#"\t$dst, $vec, $idx",
591             vec.prefix#".extract_lane"#suffix#"\t$idx", simdop>;
592}
593
594defm "" : ExtractLane<I8x16, 21, "_s">;
595defm "" : ExtractLane<I8x16, 22, "_u">;
596defm "" : ExtractLane<I16x8, 24, "_s">;
597defm "" : ExtractLane<I16x8, 25, "_u">;
598defm "" : ExtractLane<I32x4, 27>;
599defm "" : ExtractLane<I64x2, 29>;
600defm "" : ExtractLane<F32x4, 31>;
601defm "" : ExtractLane<F64x2, 33>;
602
603def : Pat<(vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)),
604          (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>;
605def : Pat<(vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)),
606          (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>;
607def : Pat<(vector_extract (v4i32 V128:$vec), (i32 LaneIdx4:$idx)),
608          (EXTRACT_LANE_I32x4 $vec, imm:$idx)>;
609def : Pat<(vector_extract (v4f32 V128:$vec), (i32 LaneIdx4:$idx)),
610          (EXTRACT_LANE_F32x4 $vec, imm:$idx)>;
611def : Pat<(vector_extract (v2i64 V128:$vec), (i32 LaneIdx2:$idx)),
612          (EXTRACT_LANE_I64x2 $vec, imm:$idx)>;
613def : Pat<(vector_extract (v2f64 V128:$vec), (i32 LaneIdx2:$idx)),
614          (EXTRACT_LANE_F64x2 $vec, imm:$idx)>;
615
616def : Pat<
617  (sext_inreg (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), i8),
618  (EXTRACT_LANE_I8x16_s $vec, imm:$idx)>;
619def : Pat<
620  (and (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), (i32 0xff)),
621  (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>;
622def : Pat<
623  (sext_inreg (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), i16),
624  (EXTRACT_LANE_I16x8_s $vec, imm:$idx)>;
625def : Pat<
626  (and (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), (i32 0xffff)),
627  (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>;
628
629// Replace lane value: replace_lane
630multiclass ReplaceLane<Vec vec, bits<32> simdop> {
631  defm REPLACE_LANE_#vec :
632    SIMD_I<(outs V128:$dst), (ins V128:$vec, vec_i8imm_op:$idx, vec.lane_rc:$x),
633           (outs), (ins vec_i8imm_op:$idx),
634           [(set V128:$dst, (vector_insert
635             (vec.vt V128:$vec),
636             (vec.lane_vt vec.lane_rc:$x),
637             (i32 vec.lane_idx:$idx)))],
638           vec.prefix#".replace_lane\t$dst, $vec, $idx, $x",
639           vec.prefix#".replace_lane\t$idx", simdop>;
640}
641
642defm "" : ReplaceLane<I8x16, 23>;
643defm "" : ReplaceLane<I16x8, 26>;
644defm "" : ReplaceLane<I32x4, 28>;
645defm "" : ReplaceLane<I64x2, 30>;
646defm "" : ReplaceLane<F32x4, 32>;
647defm "" : ReplaceLane<F64x2, 34>;
648
649// Lower undef lane indices to zero
650def : Pat<(vector_insert (v16i8 V128:$vec), I32:$x, undef),
651          (REPLACE_LANE_I8x16 $vec, 0, $x)>;
652def : Pat<(vector_insert (v8i16 V128:$vec), I32:$x, undef),
653          (REPLACE_LANE_I16x8 $vec, 0, $x)>;
654def : Pat<(vector_insert (v4i32 V128:$vec), I32:$x, undef),
655          (REPLACE_LANE_I32x4 $vec, 0, $x)>;
656def : Pat<(vector_insert (v2i64 V128:$vec), I64:$x, undef),
657          (REPLACE_LANE_I64x2 $vec, 0, $x)>;
658def : Pat<(vector_insert (v4f32 V128:$vec), F32:$x, undef),
659          (REPLACE_LANE_F32x4 $vec, 0, $x)>;
660def : Pat<(vector_insert (v2f64 V128:$vec), F64:$x, undef),
661          (REPLACE_LANE_F64x2 $vec, 0, $x)>;
662
663//===----------------------------------------------------------------------===//
664// Comparisons
665//===----------------------------------------------------------------------===//
666
667multiclass SIMDCondition<Vec vec, string name, CondCode cond, bits<32> simdop> {
668  defm _#vec :
669    SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
670           [(set (vec.int_vt V128:$dst),
671             (setcc (vec.vt V128:$lhs), (vec.vt V128:$rhs), cond))],
672           vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
673           vec.prefix#"."#name, simdop>;
674}
675
676multiclass SIMDConditionInt<string name, CondCode cond, bits<32> baseInst> {
677  defm "" : SIMDCondition<I8x16, name, cond, baseInst>;
678  defm "" : SIMDCondition<I16x8, name, cond, !add(baseInst, 10)>;
679  defm "" : SIMDCondition<I32x4, name, cond, !add(baseInst, 20)>;
680}
681
682multiclass SIMDConditionFP<string name, CondCode cond, bits<32> baseInst> {
683  defm "" : SIMDCondition<F32x4, name, cond, baseInst>;
684  defm "" : SIMDCondition<F64x2, name, cond, !add(baseInst, 6)>;
685}
686
687// Equality: eq
688let isCommutable = 1 in {
689defm EQ : SIMDConditionInt<"eq", SETEQ, 35>;
690defm EQ : SIMDCondition<I64x2, "eq", SETEQ, 214>;
691defm EQ : SIMDConditionFP<"eq", SETOEQ, 65>;
692} // isCommutable = 1
693
694// Non-equality: ne
695let isCommutable = 1 in {
696defm NE : SIMDConditionInt<"ne", SETNE, 36>;
697defm NE : SIMDCondition<I64x2, "ne", SETNE, 215>;
698defm NE : SIMDConditionFP<"ne", SETUNE, 66>;
699} // isCommutable = 1
700
701// Less than: lt_s / lt_u / lt
702defm LT_S : SIMDConditionInt<"lt_s", SETLT, 37>;
703defm LT_S : SIMDCondition<I64x2, "lt_s", SETLT, 216>;
704defm LT_U : SIMDConditionInt<"lt_u", SETULT, 38>;
705defm LT : SIMDConditionFP<"lt", SETOLT, 67>;
706
707// Greater than: gt_s / gt_u / gt
708defm GT_S : SIMDConditionInt<"gt_s", SETGT, 39>;
709defm GT_S : SIMDCondition<I64x2, "gt_s", SETGT, 217>;
710defm GT_U : SIMDConditionInt<"gt_u", SETUGT, 40>;
711defm GT : SIMDConditionFP<"gt", SETOGT, 68>;
712
713// Less than or equal: le_s / le_u / le
714defm LE_S : SIMDConditionInt<"le_s", SETLE, 41>;
715defm LE_S : SIMDCondition<I64x2, "le_s", SETLE, 218>;
716defm LE_U : SIMDConditionInt<"le_u", SETULE, 42>;
717defm LE : SIMDConditionFP<"le", SETOLE, 69>;
718
719// Greater than or equal: ge_s / ge_u / ge
720defm GE_S : SIMDConditionInt<"ge_s", SETGE, 43>;
721defm GE_S : SIMDCondition<I64x2, "ge_s", SETGE, 219>;
722defm GE_U : SIMDConditionInt<"ge_u", SETUGE, 44>;
723defm GE : SIMDConditionFP<"ge", SETOGE, 70>;
724
725// Lower float comparisons that don't care about NaN to standard WebAssembly
726// float comparisons. These instructions are generated with nnan and in the
727// target-independent expansion of unordered comparisons and ordered ne.
728foreach nodes = [[seteq, EQ_F32x4], [setne, NE_F32x4], [setlt, LT_F32x4],
729                 [setgt, GT_F32x4], [setle, LE_F32x4], [setge, GE_F32x4]] in
730def : Pat<(v4i32 (nodes[0] (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
731          (nodes[1] $lhs, $rhs)>;
732
733foreach nodes = [[seteq, EQ_F64x2], [setne, NE_F64x2], [setlt, LT_F64x2],
734                 [setgt, GT_F64x2], [setle, LE_F64x2], [setge, GE_F64x2]] in
735def : Pat<(v2i64 (nodes[0] (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
736          (nodes[1] $lhs, $rhs)>;
737
738//===----------------------------------------------------------------------===//
739// Bitwise operations
740//===----------------------------------------------------------------------===//
741
742multiclass SIMDBinary<Vec vec, SDPatternOperator node, string name, bits<32> simdop> {
743  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
744                      (outs), (ins),
745                      [(set (vec.vt V128:$dst),
746                        (node (vec.vt V128:$lhs), (vec.vt V128:$rhs)))],
747                      vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
748                      vec.prefix#"."#name, simdop>;
749}
750
751multiclass SIMDBitwise<SDPatternOperator node, string name, bits<32> simdop,
752                       bit commutable = false> {
753  let isCommutable = commutable in
754  defm "" : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
755                   (outs), (ins), [],
756                   "v128."#name#"\t$dst, $lhs, $rhs", "v128."#name, simdop>;
757  foreach vec = IntVecs in
758  def : Pat<(node (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
759            (!cast<NI>(NAME) $lhs, $rhs)>;
760}
761
762multiclass SIMDUnary<Vec vec, SDPatternOperator node, string name, bits<32> simdop> {
763  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins),
764                      [(set (vec.vt V128:$dst),
765                        (vec.vt (node (vec.vt V128:$v))))],
766                      vec.prefix#"."#name#"\t$dst, $v",
767                      vec.prefix#"."#name, simdop>;
768}
769
770// Bitwise logic: v128.not
771defm NOT : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins), [],
772                  "v128.not\t$dst, $v", "v128.not", 77>;
773foreach vec = IntVecs in
774def : Pat<(vnot (vec.vt V128:$v)), (NOT $v)>;
775
776// Bitwise logic: v128.and / v128.or / v128.xor
777defm AND : SIMDBitwise<and, "and", 78, true>;
778defm OR : SIMDBitwise<or, "or", 80, true>;
779defm XOR : SIMDBitwise<xor, "xor", 81, true>;
780
781// Bitwise logic: v128.andnot
782def andnot : PatFrag<(ops node:$left, node:$right), (and $left, (vnot $right))>;
783defm ANDNOT : SIMDBitwise<andnot, "andnot", 79>;
784
785// Bitwise select: v128.bitselect
786defm BITSELECT :
787  SIMD_I<(outs V128:$dst), (ins V128:$v1, V128:$v2, V128:$c), (outs), (ins), [],
788         "v128.bitselect\t$dst, $v1, $v2, $c", "v128.bitselect", 82>;
789
790foreach vec = AllVecs in
791def : Pat<(vec.vt (int_wasm_bitselect
792            (vec.vt V128:$v1), (vec.vt V128:$v2), (vec.vt V128:$c))),
793          (BITSELECT $v1, $v2, $c)>;
794
795// Bitselect is equivalent to (c & v1) | (~c & v2)
796foreach vec = IntVecs in
797def : Pat<(vec.vt (or (and (vec.vt V128:$c), (vec.vt V128:$v1)),
798            (and (vnot V128:$c), (vec.vt V128:$v2)))),
799          (BITSELECT $v1, $v2, $c)>;
800
801// Bitselect is also equivalent to ((v1 ^ v2) & c) ^ v2
802foreach vec = IntVecs in
803def : Pat<(vec.vt (xor (and (xor (vec.vt V128:$v1), (vec.vt V128:$v2)),
804                            (vec.vt V128:$c)),
805                       (vec.vt V128:$v2))),
806          (BITSELECT $v1, $v2, $c)>;
807
808// Same pattern with `c` negated so `a` and `b` get swapped.
809foreach vec = IntVecs in
810def : Pat<(vec.vt (xor (and (xor (vec.vt V128:$v1), (vec.vt V128:$v2)),
811                            (vnot (vec.vt V128:$c))),
812                       (vec.vt V128:$v2))),
813          (BITSELECT $v2, $v1, $c)>;
814
815// Also implement vselect in terms of bitselect
816foreach vec = AllVecs in
817def : Pat<(vec.vt (vselect
818            (vec.int_vt V128:$c), (vec.vt V128:$v1), (vec.vt V128:$v2))),
819          (BITSELECT $v1, $v2, $c)>;
820
821// MVP select on v128 values
822defm SELECT_V128 :
823  I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, I32:$cond), (outs), (ins), [],
824    "v128.select\t$dst, $lhs, $rhs, $cond", "v128.select", 0x1b>;
825
826foreach vec = AllVecs in {
827def : Pat<(select I32:$cond, (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
828          (SELECT_V128 $lhs, $rhs, $cond)>;
829
830// ISD::SELECT requires its operand to conform to getBooleanContents, but
831// WebAssembly's select interprets any non-zero value as true, so we can fold
832// a setne with 0 into a select.
833def : Pat<(select
834            (i32 (setne I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
835          (SELECT_V128 $lhs, $rhs, $cond)>;
836
837// And again, this time with seteq instead of setne and the arms reversed.
838def : Pat<(select
839            (i32 (seteq I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
840          (SELECT_V128 $rhs, $lhs, $cond)>;
841} // foreach vec
842
843//===----------------------------------------------------------------------===//
844// Integer unary arithmetic
845//===----------------------------------------------------------------------===//
846
847multiclass SIMDUnaryInt<SDPatternOperator node, string name, bits<32> baseInst> {
848  defm "" : SIMDUnary<I8x16, node, name, baseInst>;
849  defm "" : SIMDUnary<I16x8, node, name, !add(baseInst, 32)>;
850  defm "" : SIMDUnary<I32x4, node, name, !add(baseInst, 64)>;
851  defm "" : SIMDUnary<I64x2, node, name, !add(baseInst, 96)>;
852}
853
854// Integer vector negation
855def ivneg : PatFrag<(ops node:$in), (sub immAllZerosV, $in)>;
856
857// Integer absolute value: abs
858defm ABS : SIMDUnaryInt<abs, "abs", 96>;
859
860// Integer negation: neg
861defm NEG : SIMDUnaryInt<ivneg, "neg", 97>;
862
863// Population count: popcnt
864defm POPCNT : SIMDUnary<I8x16, ctpop, "popcnt", 0x62>;
865
866// Any lane true: any_true
867defm ANYTRUE : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins), [],
868                      "v128.any_true\t$dst, $vec", "v128.any_true", 0x53>;
869
870foreach vec = IntVecs in
871def : Pat<(int_wasm_anytrue (vec.vt V128:$vec)), (ANYTRUE V128:$vec)>;
872
873// All lanes true: all_true
874multiclass SIMDAllTrue<Vec vec, bits<32> simdop> {
875  defm ALLTRUE_#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
876                             [(set I32:$dst,
877                               (i32 (int_wasm_alltrue (vec.vt V128:$vec))))],
878                             vec.prefix#".all_true\t$dst, $vec",
879                             vec.prefix#".all_true", simdop>;
880}
881
882defm "" : SIMDAllTrue<I8x16, 0x63>;
883defm "" : SIMDAllTrue<I16x8, 0x83>;
884defm "" : SIMDAllTrue<I32x4, 0xa3>;
885defm "" : SIMDAllTrue<I64x2, 0xc3>;
886
887// Reductions already return 0 or 1, so and 1, setne 0, and seteq 1
888// can be folded out
889foreach reduction =
890  [["int_wasm_anytrue", "ANYTRUE", "I8x16"],
891   ["int_wasm_anytrue", "ANYTRUE", "I16x8"],
892   ["int_wasm_anytrue", "ANYTRUE", "I32x4"],
893   ["int_wasm_anytrue", "ANYTRUE", "I64x2"],
894   ["int_wasm_alltrue", "ALLTRUE_I8x16", "I8x16"],
895   ["int_wasm_alltrue", "ALLTRUE_I16x8", "I16x8"],
896   ["int_wasm_alltrue", "ALLTRUE_I32x4", "I32x4"],
897   ["int_wasm_alltrue", "ALLTRUE_I64x2", "I64x2"]] in {
898defvar intrinsic = !cast<Intrinsic>(reduction[0]);
899defvar inst = !cast<NI>(reduction[1]);
900defvar vec = !cast<Vec>(reduction[2]);
901def : Pat<(i32 (and (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>;
902def : Pat<(i32 (setne (i32 (intrinsic (vec.vt V128:$x))), (i32 0))), (inst $x)>;
903def : Pat<(i32 (seteq (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>;
904}
905
906multiclass SIMDBitmask<Vec vec, bits<32> simdop> {
907  defm _#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
908                      [(set I32:$dst,
909                         (i32 (int_wasm_bitmask (vec.vt V128:$vec))))],
910                      vec.prefix#".bitmask\t$dst, $vec", vec.prefix#".bitmask",
911                      simdop>;
912}
913
914defm BITMASK : SIMDBitmask<I8x16, 100>;
915defm BITMASK : SIMDBitmask<I16x8, 132>;
916defm BITMASK : SIMDBitmask<I32x4, 164>;
917defm BITMASK : SIMDBitmask<I64x2, 196>;
918
919//===----------------------------------------------------------------------===//
920// Bit shifts
921//===----------------------------------------------------------------------===//
922
923multiclass SIMDShift<Vec vec, SDNode node, string name, bits<32> simdop> {
924  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$vec, I32:$x), (outs), (ins),
925                      [(set (vec.vt V128:$dst), (node V128:$vec, I32:$x))],
926                      vec.prefix#"."#name#"\t$dst, $vec, $x",
927                      vec.prefix#"."#name, simdop>;
928}
929
930multiclass SIMDShiftInt<SDNode node, string name, bits<32> baseInst> {
931  defm "" : SIMDShift<I8x16, node, name, baseInst>;
932  defm "" : SIMDShift<I16x8, node, name, !add(baseInst, 32)>;
933  defm "" : SIMDShift<I32x4, node, name, !add(baseInst, 64)>;
934  defm "" : SIMDShift<I64x2, node, name, !add(baseInst, 96)>;
935}
936
937// WebAssembly SIMD shifts are nonstandard in that the shift amount is
938// an i32 rather than a vector, so they need custom nodes.
939def wasm_shift_t :
940  SDTypeProfile<1, 2, [SDTCisVec<0>, SDTCisSameAs<0, 1>, SDTCisVT<2, i32>]>;
941def wasm_shl : SDNode<"WebAssemblyISD::VEC_SHL", wasm_shift_t>;
942def wasm_shr_s : SDNode<"WebAssemblyISD::VEC_SHR_S", wasm_shift_t>;
943def wasm_shr_u : SDNode<"WebAssemblyISD::VEC_SHR_U", wasm_shift_t>;
944
945// Left shift by scalar: shl
946defm SHL : SIMDShiftInt<wasm_shl, "shl", 107>;
947
948// Right shift by scalar: shr_s / shr_u
949defm SHR_S : SIMDShiftInt<wasm_shr_s, "shr_s", 108>;
950defm SHR_U : SIMDShiftInt<wasm_shr_u, "shr_u", 109>;
951
952// Optimize away an explicit mask on a shift count.
953def : Pat<(wasm_shl (v16i8 V128:$lhs), (and I32:$rhs, 7)),
954          (SHL_I8x16 V128:$lhs, I32:$rhs)>;
955def : Pat<(wasm_shr_s (v16i8 V128:$lhs), (and I32:$rhs, 7)),
956          (SHR_S_I8x16 V128:$lhs, I32:$rhs)>;
957def : Pat<(wasm_shr_u (v16i8 V128:$lhs), (and I32:$rhs, 7)),
958          (SHR_U_I8x16 V128:$lhs, I32:$rhs)>;
959
960def : Pat<(wasm_shl (v8i16 V128:$lhs), (and I32:$rhs, 15)),
961          (SHL_I16x8 V128:$lhs, I32:$rhs)>;
962def : Pat<(wasm_shr_s (v8i16 V128:$lhs), (and I32:$rhs, 15)),
963          (SHR_S_I16x8 V128:$lhs, I32:$rhs)>;
964def : Pat<(wasm_shr_u (v8i16 V128:$lhs), (and I32:$rhs, 15)),
965          (SHR_U_I16x8 V128:$lhs, I32:$rhs)>;
966
967def : Pat<(wasm_shl (v4i32 V128:$lhs), (and I32:$rhs, 31)),
968          (SHL_I32x4 V128:$lhs, I32:$rhs)>;
969def : Pat<(wasm_shr_s (v4i32 V128:$lhs), (and I32:$rhs, 31)),
970          (SHR_S_I32x4 V128:$lhs, I32:$rhs)>;
971def : Pat<(wasm_shr_u (v4i32 V128:$lhs), (and I32:$rhs, 31)),
972          (SHR_U_I32x4 V128:$lhs, I32:$rhs)>;
973
974def : Pat<(wasm_shl (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
975          (SHL_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
976def : Pat<(wasm_shr_s (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
977          (SHR_S_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
978def : Pat<(wasm_shr_u (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
979          (SHR_U_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
980
981//===----------------------------------------------------------------------===//
982// Integer binary arithmetic
983//===----------------------------------------------------------------------===//
984
985multiclass SIMDBinaryIntNoI8x16<SDPatternOperator node, string name, bits<32> baseInst> {
986  defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>;
987  defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>;
988  defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>;
989}
990
991multiclass SIMDBinaryIntSmall<SDPatternOperator node, string name, bits<32> baseInst> {
992  defm "" : SIMDBinary<I8x16, node, name, baseInst>;
993  defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>;
994}
995
996multiclass SIMDBinaryIntNoI64x2<SDPatternOperator node, string name, bits<32> baseInst> {
997  defm "" : SIMDBinaryIntSmall<node, name, baseInst>;
998  defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>;
999}
1000
1001multiclass SIMDBinaryInt<SDPatternOperator node, string name, bits<32> baseInst> {
1002  defm "" : SIMDBinaryIntNoI64x2<node, name, baseInst>;
1003  defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>;
1004}
1005
1006// Integer addition: add / add_sat_s / add_sat_u
1007let isCommutable = 1 in {
1008defm ADD : SIMDBinaryInt<add, "add", 110>;
1009defm ADD_SAT_S : SIMDBinaryIntSmall<saddsat, "add_sat_s", 111>;
1010defm ADD_SAT_U : SIMDBinaryIntSmall<uaddsat, "add_sat_u", 112>;
1011} // isCommutable = 1
1012
1013// Integer subtraction: sub / sub_sat_s / sub_sat_u
1014defm SUB : SIMDBinaryInt<sub, "sub", 113>;
1015defm SUB_SAT_S :
1016  SIMDBinaryIntSmall<int_wasm_sub_sat_signed, "sub_sat_s", 114>;
1017defm SUB_SAT_U :
1018  SIMDBinaryIntSmall<int_wasm_sub_sat_unsigned, "sub_sat_u", 115>;
1019
1020// Integer multiplication: mul
1021let isCommutable = 1 in
1022defm MUL : SIMDBinaryIntNoI8x16<mul, "mul", 117>;
1023
1024// Integer min_s / min_u / max_s / max_u
1025let isCommutable = 1 in {
1026defm MIN_S : SIMDBinaryIntNoI64x2<smin, "min_s", 118>;
1027defm MIN_U : SIMDBinaryIntNoI64x2<umin, "min_u", 119>;
1028defm MAX_S : SIMDBinaryIntNoI64x2<smax, "max_s", 120>;
1029defm MAX_U : SIMDBinaryIntNoI64x2<umax, "max_u", 121>;
1030} // isCommutable = 1
1031
1032// Integer unsigned rounding average: avgr_u
1033let isCommutable = 1 in {
1034defm AVGR_U : SIMDBinary<I8x16, int_wasm_avgr_unsigned, "avgr_u", 123>;
1035defm AVGR_U : SIMDBinary<I16x8, int_wasm_avgr_unsigned, "avgr_u", 155>;
1036}
1037
1038def add_nuw : PatFrag<(ops node:$lhs, node:$rhs), (add $lhs, $rhs),
1039                      "return N->getFlags().hasNoUnsignedWrap();">;
1040
1041foreach vec = [I8x16, I16x8] in {
1042defvar inst = !cast<NI>("AVGR_U_"#vec);
1043def : Pat<(wasm_shr_u
1044            (add_nuw
1045              (add_nuw (vec.vt V128:$lhs), (vec.vt V128:$rhs)),
1046              (vec.splat (i32 1))),
1047            (i32 1)),
1048          (inst $lhs, $rhs)>;
1049}
1050
1051// Widening dot product: i32x4.dot_i16x8_s
1052let isCommutable = 1 in
1053defm DOT : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
1054                  [(set V128:$dst, (int_wasm_dot V128:$lhs, V128:$rhs))],
1055                  "i32x4.dot_i16x8_s\t$dst, $lhs, $rhs", "i32x4.dot_i16x8_s",
1056                  186>;
1057
1058// Extending multiplication: extmul_{low,high}_P, extmul_high
1059def extend_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1060def extend_low_s : SDNode<"WebAssemblyISD::EXTEND_LOW_S", extend_t>;
1061def extend_high_s : SDNode<"WebAssemblyISD::EXTEND_HIGH_S", extend_t>;
1062def extend_low_u : SDNode<"WebAssemblyISD::EXTEND_LOW_U", extend_t>;
1063def extend_high_u : SDNode<"WebAssemblyISD::EXTEND_HIGH_U", extend_t>;
1064
1065multiclass SIMDExtBinary<Vec vec, SDPatternOperator node, string name,
1066                         bits<32> simdop> {
1067  defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
1068                      (outs), (ins),
1069                      [(set (vec.vt V128:$dst), (node
1070                         (vec.split.vt V128:$lhs),(vec.split.vt V128:$rhs)))],
1071                      vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
1072                      vec.prefix#"."#name, simdop>;
1073}
1074
1075class ExtMulPat<SDNode extend> :
1076  PatFrag<(ops node:$lhs, node:$rhs),
1077          (mul (extend $lhs), (extend $rhs))> {}
1078
1079def extmul_low_s : ExtMulPat<extend_low_s>;
1080def extmul_high_s : ExtMulPat<extend_high_s>;
1081def extmul_low_u : ExtMulPat<extend_low_u>;
1082def extmul_high_u : ExtMulPat<extend_high_u>;
1083
1084defm EXTMUL_LOW_S :
1085  SIMDExtBinary<I16x8, extmul_low_s, "extmul_low_i8x16_s", 0x9c>;
1086defm EXTMUL_HIGH_S :
1087  SIMDExtBinary<I16x8, extmul_high_s, "extmul_high_i8x16_s", 0x9d>;
1088defm EXTMUL_LOW_U :
1089  SIMDExtBinary<I16x8, extmul_low_u, "extmul_low_i8x16_u", 0x9e>;
1090defm EXTMUL_HIGH_U :
1091  SIMDExtBinary<I16x8, extmul_high_u, "extmul_high_i8x16_u", 0x9f>;
1092
1093defm EXTMUL_LOW_S :
1094  SIMDExtBinary<I32x4, extmul_low_s, "extmul_low_i16x8_s", 0xbc>;
1095defm EXTMUL_HIGH_S :
1096  SIMDExtBinary<I32x4, extmul_high_s, "extmul_high_i16x8_s", 0xbd>;
1097defm EXTMUL_LOW_U :
1098  SIMDExtBinary<I32x4, extmul_low_u, "extmul_low_i16x8_u", 0xbe>;
1099defm EXTMUL_HIGH_U :
1100  SIMDExtBinary<I32x4, extmul_high_u, "extmul_high_i16x8_u", 0xbf>;
1101
1102defm EXTMUL_LOW_S :
1103  SIMDExtBinary<I64x2, extmul_low_s, "extmul_low_i32x4_s", 0xdc>;
1104defm EXTMUL_HIGH_S :
1105  SIMDExtBinary<I64x2, extmul_high_s, "extmul_high_i32x4_s", 0xdd>;
1106defm EXTMUL_LOW_U :
1107  SIMDExtBinary<I64x2, extmul_low_u, "extmul_low_i32x4_u", 0xde>;
1108defm EXTMUL_HIGH_U :
1109  SIMDExtBinary<I64x2, extmul_high_u, "extmul_high_i32x4_u", 0xdf>;
1110
1111//===----------------------------------------------------------------------===//
1112// Floating-point unary arithmetic
1113//===----------------------------------------------------------------------===//
1114
1115multiclass SIMDUnaryFP<SDNode node, string name, bits<32> baseInst> {
1116  defm "" : SIMDUnary<F32x4, node, name, baseInst>;
1117  defm "" : SIMDUnary<F64x2, node, name, !add(baseInst, 12)>;
1118}
1119
1120// Absolute value: abs
1121defm ABS : SIMDUnaryFP<fabs, "abs", 224>;
1122
1123// Negation: neg
1124defm NEG : SIMDUnaryFP<fneg, "neg", 225>;
1125
1126// Square root: sqrt
1127defm SQRT : SIMDUnaryFP<fsqrt, "sqrt", 227>;
1128
1129// Rounding: ceil, floor, trunc, nearest
1130defm CEIL : SIMDUnary<F32x4, fceil, "ceil", 0x67>;
1131defm FLOOR : SIMDUnary<F32x4, ffloor, "floor", 0x68>;
1132defm TRUNC: SIMDUnary<F32x4, ftrunc, "trunc", 0x69>;
1133defm NEAREST: SIMDUnary<F32x4, fnearbyint, "nearest", 0x6a>;
1134defm CEIL : SIMDUnary<F64x2, fceil, "ceil", 0x74>;
1135defm FLOOR : SIMDUnary<F64x2, ffloor, "floor", 0x75>;
1136defm TRUNC: SIMDUnary<F64x2, ftrunc, "trunc", 0x7a>;
1137defm NEAREST: SIMDUnary<F64x2, fnearbyint, "nearest", 0x94>;
1138
1139//===----------------------------------------------------------------------===//
1140// Floating-point binary arithmetic
1141//===----------------------------------------------------------------------===//
1142
1143multiclass SIMDBinaryFP<SDPatternOperator node, string name, bits<32> baseInst> {
1144  defm "" : SIMDBinary<F32x4, node, name, baseInst>;
1145  defm "" : SIMDBinary<F64x2, node, name, !add(baseInst, 12)>;
1146}
1147
1148// Addition: add
1149let isCommutable = 1 in
1150defm ADD : SIMDBinaryFP<fadd, "add", 228>;
1151
1152// Subtraction: sub
1153defm SUB : SIMDBinaryFP<fsub, "sub", 229>;
1154
1155// Multiplication: mul
1156let isCommutable = 1 in
1157defm MUL : SIMDBinaryFP<fmul, "mul", 230>;
1158
1159// Division: div
1160defm DIV : SIMDBinaryFP<fdiv, "div", 231>;
1161
1162// NaN-propagating minimum: min
1163defm MIN : SIMDBinaryFP<fminimum, "min", 232>;
1164
1165// NaN-propagating maximum: max
1166defm MAX : SIMDBinaryFP<fmaximum, "max", 233>;
1167
1168// Pseudo-minimum: pmin
1169def pmin : PatFrag<(ops node:$lhs, node:$rhs),
1170                   (vselect (setolt $rhs, $lhs), $rhs, $lhs)>;
1171defm PMIN : SIMDBinaryFP<pmin, "pmin", 234>;
1172
1173// Pseudo-maximum: pmax
1174def pmax : PatFrag<(ops node:$lhs, node:$rhs),
1175                   (vselect (setolt $lhs, $rhs), $rhs, $lhs)>;
1176defm PMAX : SIMDBinaryFP<pmax, "pmax", 235>;
1177
1178// Also match the pmin/pmax cases where the operands are int vectors (but the
1179// comparison is still a floating point comparison). This can happen when using
1180// the wasm_simd128.h intrinsics because v128_t is an integer vector.
1181foreach vec = [F32x4, F64x2] in {
1182defvar pmin = !cast<NI>("PMIN_"#vec);
1183defvar pmax = !cast<NI>("PMAX_"#vec);
1184def : Pat<(vec.int_vt (vselect
1185            (setolt (vec.vt (bitconvert V128:$rhs)),
1186                    (vec.vt (bitconvert V128:$lhs))),
1187            V128:$rhs, V128:$lhs)),
1188          (pmin $lhs, $rhs)>;
1189def : Pat<(vec.int_vt (vselect
1190            (setolt (vec.vt (bitconvert V128:$lhs)),
1191                    (vec.vt (bitconvert V128:$rhs))),
1192            V128:$rhs, V128:$lhs)),
1193          (pmax $lhs, $rhs)>;
1194}
1195
1196// And match the pmin/pmax LLVM intrinsics as well
1197def : Pat<(v4f32 (int_wasm_pmin (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
1198          (PMIN_F32x4 V128:$lhs, V128:$rhs)>;
1199def : Pat<(v4f32 (int_wasm_pmax (v4f32 V128:$lhs), (v4f32 V128:$rhs))),
1200          (PMAX_F32x4 V128:$lhs, V128:$rhs)>;
1201def : Pat<(v2f64 (int_wasm_pmin (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
1202          (PMIN_F64x2 V128:$lhs, V128:$rhs)>;
1203def : Pat<(v2f64 (int_wasm_pmax (v2f64 V128:$lhs), (v2f64 V128:$rhs))),
1204          (PMAX_F64x2 V128:$lhs, V128:$rhs)>;
1205
1206//===----------------------------------------------------------------------===//
1207// Conversions
1208//===----------------------------------------------------------------------===//
1209
1210multiclass SIMDConvert<Vec vec, Vec arg, SDPatternOperator op, string name,
1211                       bits<32> simdop> {
1212  defm op#_#vec :
1213    SIMD_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins),
1214           [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))],
1215           vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop>;
1216}
1217
1218// Floating point to integer with saturation: trunc_sat
1219defm "" : SIMDConvert<I32x4, F32x4, fp_to_sint, "trunc_sat_f32x4_s", 248>;
1220defm "" : SIMDConvert<I32x4, F32x4, fp_to_uint, "trunc_sat_f32x4_u", 249>;
1221
1222// Support the saturating variety as well.
1223def trunc_s_sat32 : PatFrag<(ops node:$x), (fp_to_sint_sat $x, i32)>;
1224def trunc_u_sat32 : PatFrag<(ops node:$x), (fp_to_uint_sat $x, i32)>;
1225def : Pat<(v4i32 (trunc_s_sat32 (v4f32 V128:$src))), (fp_to_sint_I32x4 $src)>;
1226def : Pat<(v4i32 (trunc_u_sat32 (v4f32 V128:$src))), (fp_to_uint_I32x4 $src)>;
1227
1228def trunc_sat_zero_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1229def trunc_sat_zero_s :
1230  SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_S", trunc_sat_zero_t>;
1231def trunc_sat_zero_u :
1232  SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_U", trunc_sat_zero_t>;
1233defm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_s, "trunc_sat_f64x2_s_zero",
1234                      0xfc>;
1235defm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_u, "trunc_sat_f64x2_u_zero",
1236                      0xfd>;
1237
1238// Integer to floating point: convert
1239def convert_low_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1240def convert_low_s : SDNode<"WebAssemblyISD::CONVERT_LOW_S", convert_low_t>;
1241def convert_low_u : SDNode<"WebAssemblyISD::CONVERT_LOW_U", convert_low_t>;
1242defm "" : SIMDConvert<F32x4, I32x4, sint_to_fp, "convert_i32x4_s", 250>;
1243defm "" : SIMDConvert<F32x4, I32x4, uint_to_fp, "convert_i32x4_u", 251>;
1244defm "" : SIMDConvert<F64x2, I32x4, convert_low_s, "convert_low_i32x4_s", 0xfe>;
1245defm "" : SIMDConvert<F64x2, I32x4, convert_low_u, "convert_low_i32x4_u", 0xff>;
1246
1247// Extending operations
1248// TODO: refactor this to be uniform for i64x2 if the numbering is not changed.
1249multiclass SIMDExtend<Vec vec, bits<32> baseInst> {
1250  defm "" : SIMDConvert<vec, vec.split, extend_low_s,
1251                        "extend_low_"#vec.split.prefix#"_s", baseInst>;
1252  defm "" : SIMDConvert<vec, vec.split, extend_high_s,
1253                        "extend_high_"#vec.split.prefix#"_s", !add(baseInst, 1)>;
1254  defm "" : SIMDConvert<vec, vec.split, extend_low_u,
1255                        "extend_low_"#vec.split.prefix#"_u", !add(baseInst, 2)>;
1256  defm "" : SIMDConvert<vec, vec.split, extend_high_u,
1257                        "extend_high_"#vec.split.prefix#"_u", !add(baseInst, 3)>;
1258}
1259
1260defm "" : SIMDExtend<I16x8, 0x87>;
1261defm "" : SIMDExtend<I32x4, 0xa7>;
1262defm "" : SIMDExtend<I64x2, 0xc7>;
1263
1264// Narrowing operations
1265multiclass SIMDNarrow<Vec vec, bits<32> baseInst> {
1266  defvar name = vec.split.prefix#".narrow_"#vec.prefix;
1267  defm NARROW_S_#vec.split :
1268    SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins),
1269           [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_signed
1270             (vec.vt V128:$low), (vec.vt V128:$high))))],
1271           name#"_s\t$dst, $low, $high", name#"_s", baseInst>;
1272  defm NARROW_U_#vec.split :
1273    SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins),
1274           [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_unsigned
1275             (vec.vt V128:$low), (vec.vt V128:$high))))],
1276           name#"_u\t$dst, $low, $high", name#"_u", !add(baseInst, 1)>;
1277}
1278
1279defm "" : SIMDNarrow<I16x8, 101>;
1280defm "" : SIMDNarrow<I32x4, 133>;
1281
1282// WebAssemblyISD::NARROW_U
1283def wasm_narrow_t : SDTypeProfile<1, 2, []>;
1284def wasm_narrow_u : SDNode<"WebAssemblyISD::NARROW_U", wasm_narrow_t>;
1285def : Pat<(v16i8 (wasm_narrow_u (v8i16 V128:$left), (v8i16 V128:$right))),
1286          (NARROW_U_I8x16 $left, $right)>;
1287def : Pat<(v8i16 (wasm_narrow_u (v4i32 V128:$left), (v4i32 V128:$right))),
1288          (NARROW_U_I16x8 $left, $right)>;
1289
1290// Bitcasts are nops
1291// Matching bitcast t1 to t1 causes strange errors, so avoid repeating types
1292foreach t1 = AllVecs in
1293foreach t2 = AllVecs in
1294if !ne(t1, t2) then
1295def : Pat<(t1.vt (bitconvert (t2.vt V128:$v))), (t1.vt V128:$v)>;
1296
1297// Extended pairwise addition
1298defm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_signed,
1299                      "extadd_pairwise_i8x16_s", 0x7c>;
1300defm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_unsigned,
1301                      "extadd_pairwise_i8x16_u", 0x7d>;
1302defm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_signed,
1303                      "extadd_pairwise_i16x8_s", 0x7e>;
1304defm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_unsigned,
1305                      "extadd_pairwise_i16x8_u", 0x7f>;
1306
1307// f64x2 <-> f32x4 conversions
1308def demote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1309def demote_zero : SDNode<"WebAssemblyISD::DEMOTE_ZERO", demote_t>;
1310defm "" : SIMDConvert<F32x4, F64x2, demote_zero,
1311                      "demote_f64x2_zero", 0x5e>;
1312
1313def promote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>;
1314def promote_low : SDNode<"WebAssemblyISD::PROMOTE_LOW", promote_t>;
1315defm "" : SIMDConvert<F64x2, F32x4, promote_low, "promote_low_f32x4", 0x5f>;
1316
1317// Lower extending loads to load64_zero + promote_low
1318def extloadv2f32 : PatFrag<(ops node:$ptr), (extload node:$ptr)> {
1319  let MemoryVT = v2f32;
1320}
1321// Adapted from the body of LoadPatNoOffset
1322// TODO: other addressing patterns
1323def : Pat<(v2f64 (extloadv2f32 (i32 I32:$addr))),
1324          (promote_low_F64x2 (LOAD_ZERO_I64x2_A32 0, 0, I32:$addr))>,
1325      Requires<[HasAddr32]>;
1326def : Pat<(v2f64 (extloadv2f32 (i64 I64:$addr))),
1327          (promote_low_F64x2 (LOAD_ZERO_I64x2_A64 0, 0, I64:$addr))>,
1328      Requires<[HasAddr64]>;
1329
1330//===----------------------------------------------------------------------===//
1331// Saturating Rounding Q-Format Multiplication
1332//===----------------------------------------------------------------------===//
1333
1334defm Q15MULR_SAT_S :
1335  SIMDBinary<I16x8, int_wasm_q15mulr_sat_signed, "q15mulr_sat_s", 0x82>;
1336
1337//===----------------------------------------------------------------------===//
1338// Relaxed swizzle
1339//===----------------------------------------------------------------------===//
1340
1341defm RELAXED_SWIZZLE :
1342  RELAXED_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins),
1343         [(set (v16i8 V128:$dst),
1344           (int_wasm_relaxed_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))],
1345         "i8x16.relaxed_swizzle\t$dst, $src, $mask", "i8x16.relaxed_swizzle", 0x100>;
1346
1347//===----------------------------------------------------------------------===//
1348// Relaxed floating-point to int conversions
1349//===----------------------------------------------------------------------===//
1350
1351multiclass RelaxedConvert<Vec vec, Vec arg, SDPatternOperator op, string name, bits<32> simdop> {
1352  defm op#_#vec :
1353    RELAXED_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins),
1354              [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))],
1355              vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop>;
1356}
1357
1358defm "" : RelaxedConvert<I32x4, F32x4, int_wasm_relaxed_trunc_signed,
1359                         "relaxed_trunc_f32x4_s", 0x101>;
1360defm "" : RelaxedConvert<I32x4, F32x4, int_wasm_relaxed_trunc_unsigned,
1361                         "relaxed_trunc_f32x4_u", 0x102>;
1362defm "" : RelaxedConvert<I32x4, F64x2, int_wasm_relaxed_trunc_signed_zero,
1363                         "relaxed_trunc_f64x2_s_zero", 0x103>;
1364defm "" : RelaxedConvert<I32x4, F64x2, int_wasm_relaxed_trunc_unsigned_zero,
1365                         "relaxed_trunc_f64x2_u_zero", 0x104>;
1366
1367//===----------------------------------------------------------------------===//
1368// Relaxed (Negative) Multiply-Add  (madd/nmadd)
1369//===----------------------------------------------------------------------===//
1370
1371multiclass SIMDMADD<Vec vec, bits<32> simdopA, bits<32> simdopS> {
1372  defm MADD_#vec :
1373    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins),
1374              [(set (vec.vt V128:$dst), (int_wasm_relaxed_madd
1375                (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))],
1376              vec.prefix#".relaxed_madd\t$dst, $a, $b, $c",
1377              vec.prefix#".relaxed_madd", simdopA>;
1378  defm NMADD_#vec :
1379    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins),
1380              [(set (vec.vt V128:$dst), (int_wasm_relaxed_nmadd
1381                (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))],
1382              vec.prefix#".relaxed_nmadd\t$dst, $a, $b, $c",
1383              vec.prefix#".relaxed_nmadd", simdopS>;
1384}
1385
1386defm "" : SIMDMADD<F32x4, 0x105, 0x106>;
1387defm "" : SIMDMADD<F64x2, 0x107, 0x108>;
1388
1389//===----------------------------------------------------------------------===//
1390// Laneselect
1391//===----------------------------------------------------------------------===//
1392
1393multiclass SIMDLANESELECT<Vec vec, bits<32> op> {
1394  defm LANESELECT_#vec :
1395    RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins),
1396              [(set (vec.vt V128:$dst), (int_wasm_relaxed_laneselect
1397                (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))],
1398              vec.prefix#".relaxed_laneselect\t$dst, $a, $b, $c",
1399              vec.prefix#".relaxed_laneselect", op>;
1400}
1401
1402defm "" : SIMDLANESELECT<I8x16, 0x109>;
1403defm "" : SIMDLANESELECT<I16x8, 0x10a>;
1404defm "" : SIMDLANESELECT<I32x4, 0x10b>;
1405defm "" : SIMDLANESELECT<I64x2, 0x10c>;
1406
1407//===----------------------------------------------------------------------===//
1408// Relaxed floating-point min and max.
1409//===----------------------------------------------------------------------===//
1410
1411multiclass RelaxedBinary<Vec vec, SDPatternOperator node, string name,
1412                         bits<32> simdop> {
1413  defm _#vec : RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs),
1414                         (outs), (ins),
1415                         [(set (vec.vt V128:$dst),
1416                           (node (vec.vt V128:$lhs), (vec.vt V128:$rhs)))],
1417                         vec.prefix#"."#name#"\t$dst, $lhs, $rhs",
1418                         vec.prefix#"."#name, simdop>;
1419}
1420
1421defm SIMD_RELAXED_FMIN :
1422   RelaxedBinary<F32x4, int_wasm_relaxed_min, "relaxed_min", 0x10d>;
1423defm SIMD_RELAXED_FMAX :
1424   RelaxedBinary<F32x4, int_wasm_relaxed_max, "relaxed_max", 0x10e>;
1425defm SIMD_RELAXED_FMIN :
1426   RelaxedBinary<F64x2, int_wasm_relaxed_min, "relaxed_min", 0x10f>;
1427defm SIMD_RELAXED_FMAX :
1428   RelaxedBinary<F64x2, int_wasm_relaxed_max, "relaxed_max", 0x110>;
1429
1430//===----------------------------------------------------------------------===//
1431// Relaxed rounding q15 multiplication
1432//===----------------------------------------------------------------------===//
1433
1434defm RELAXED_Q15MULR_S :
1435  RelaxedBinary<I16x8, int_wasm_relaxed_q15mulr_signed, "relaxed_q15mulr_s",
1436                0x111>;
1437
1438//===----------------------------------------------------------------------===//
1439// Relaxed integer dot product
1440//===----------------------------------------------------------------------===//
1441
1442defm RELAXED_DOT :
1443  RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
1444            [(set (v8i16 V128:$dst), (int_wasm_relaxed_dot_i8x16_i7x16_signed
1445               (v16i8 V128:$lhs), (v16i8 V128:$rhs)))],
1446            "i16x8.relaxed_dot_i8x16_i7x16_s\t$dst, $lhs, $rhs",
1447            "i16x8.relaxed_dot_i8x16_i7x16_s", 0x112>;
1448
1449defm RELAXED_DOT_ADD :
1450  RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, V128:$acc),
1451            (outs), (ins),
1452            [(set (v4i32 V128:$dst), (int_wasm_relaxed_dot_i8x16_i7x16_add_signed
1453               (v16i8 V128:$lhs), (v16i8 V128:$rhs), (v4i32 V128:$acc)))],
1454            "i32x4.relaxed_dot_i8x16_i7x16_add_s\t$dst, $lhs, $rhs, $acc",
1455            "i32x4.relaxed_dot_i8x16_i7x16_add_s", 0x113>;
1456
1457//===----------------------------------------------------------------------===//
1458// Relaxed BFloat16 dot product
1459//===----------------------------------------------------------------------===//
1460
1461defm RELAXED_DOT_BFLOAT :
1462  RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, V128:$acc),
1463            (outs), (ins),
1464            [(set (v4f32 V128:$dst), (int_wasm_relaxed_dot_bf16x8_add_f32
1465               (v8i16 V128:$lhs), (v8i16 V128:$rhs), (v4f32 V128:$acc)))],
1466            "f32x4.relaxed_dot_bf16x8_add_f32\t$dst, $lhs, $rhs, $acc",
1467            "f32x4.relaxed_dot_bf16x8_add_f32", 0x114>;
1468