10b57cec5SDimitry Andric// WebAssemblyInstrSIMD.td - WebAssembly SIMD codegen support -*- tablegen -*-// 20b57cec5SDimitry Andric// 30b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric// 70b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric/// 90b57cec5SDimitry Andric/// \file 100b57cec5SDimitry Andric/// WebAssembly SIMD operand code-gen constructs. 110b57cec5SDimitry Andric/// 120b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 14349cc55cSDimitry Andric// Instructions using the SIMD opcode prefix and requiring one of the SIMD 15349cc55cSDimitry Andric// feature predicates. 16349cc55cSDimitry Andricmulticlass ABSTRACT_SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s, 17349cc55cSDimitry Andric list<dag> pattern_r, string asmstr_r, 18349cc55cSDimitry Andric string asmstr_s, bits<32> simdop, 19*0fca6ea1SDimitry Andric list<Predicate> reqs> { 200b57cec5SDimitry Andric defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s, 21e8d8bef9SDimitry Andric !if(!ge(simdop, 0x100), 22e8d8bef9SDimitry Andric !or(0xfd0000, !and(0xffff, simdop)), 23e8d8bef9SDimitry Andric !or(0xfd00, !and(0xff, simdop)))>, 24*0fca6ea1SDimitry Andric Requires<reqs>; 250b57cec5SDimitry Andric} 260b57cec5SDimitry Andric 27349cc55cSDimitry Andricmulticlass SIMD_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s, 28349cc55cSDimitry Andric list<dag> pattern_r, string asmstr_r = "", 29*0fca6ea1SDimitry Andric string asmstr_s = "", bits<32> simdop = -1, 30*0fca6ea1SDimitry Andric list<Predicate> reqs = []> { 31349cc55cSDimitry Andric defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, 32*0fca6ea1SDimitry Andric asmstr_s, simdop, !listconcat([HasSIMD128], reqs)>; 33349cc55cSDimitry Andric} 34349cc55cSDimitry Andric 35349cc55cSDimitry Andricmulticlass RELAXED_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s, 36349cc55cSDimitry Andric list<dag> pattern_r, string asmstr_r = "", 37349cc55cSDimitry Andric string asmstr_s = "", bits<32> simdop = -1> { 38349cc55cSDimitry Andric defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, 39*0fca6ea1SDimitry Andric asmstr_s, simdop, [HasRelaxedSIMD]>; 40*0fca6ea1SDimitry Andric} 41*0fca6ea1SDimitry Andric 42*0fca6ea1SDimitry Andricmulticlass HALF_PRECISION_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s, 43*0fca6ea1SDimitry Andric list<dag> pattern_r, string asmstr_r = "", 44*0fca6ea1SDimitry Andric string asmstr_s = "", bits<32> simdop = -1> { 45*0fca6ea1SDimitry Andric defm "" : ABSTRACT_SIMD_I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, 46*0fca6ea1SDimitry Andric asmstr_s, simdop, [HasHalfPrecision]>; 47349cc55cSDimitry Andric} 48349cc55cSDimitry Andric 49349cc55cSDimitry Andric 500b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v16i8>; 510b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v8i16>; 520b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v4i32>; 530b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v2i64>; 540b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v4f32>; 550b57cec5SDimitry Andricdefm "" : ARGUMENT<V128, v2f64>; 56*0fca6ea1SDimitry Andricdefm "" : ARGUMENT<V128, v8f16>; 570b57cec5SDimitry Andric 58*0fca6ea1SDimitry Andric// Constrained immediate argument types. Allow any value from the minimum signed 59*0fca6ea1SDimitry Andric// value to the maximum unsigned value for the lane size. 600b57cec5SDimitry Andricforeach SIZE = [8, 16] in 610b57cec5SDimitry Andricdef ImmI#SIZE : ImmLeaf<i32, 62*0fca6ea1SDimitry Andric // -2^(n-1) <= Imm < 2^n 63*0fca6ea1SDimitry Andric "return -(1 << ("#SIZE#" - 1)) <= Imm && Imm < (1 << "#SIZE#");" 640b57cec5SDimitry Andric>; 650b57cec5SDimitry Andricforeach SIZE = [2, 4, 8, 16, 32] in 660b57cec5SDimitry Andricdef LaneIdx#SIZE : ImmLeaf<i32, "return 0 <= Imm && Imm < "#SIZE#";">; 670b57cec5SDimitry Andric 68e8d8bef9SDimitry Andricclass Vec { 69e8d8bef9SDimitry Andric ValueType vt; 70e8d8bef9SDimitry Andric ValueType int_vt; 71e8d8bef9SDimitry Andric ValueType lane_vt; 72e8d8bef9SDimitry Andric WebAssemblyRegClass lane_rc; 73e8d8bef9SDimitry Andric int lane_bits; 74e8d8bef9SDimitry Andric ImmLeaf lane_idx; 75bdd1243dSDimitry Andric SDPatternOperator lane_load; 76e8d8bef9SDimitry Andric PatFrag splat; 77e8d8bef9SDimitry Andric string prefix; 78e8d8bef9SDimitry Andric Vec split; 79e8d8bef9SDimitry Andric} 80e8d8bef9SDimitry Andric 81e8d8bef9SDimitry Andricdef I8x16 : Vec { 82e8d8bef9SDimitry Andric let vt = v16i8; 83e8d8bef9SDimitry Andric let int_vt = vt; 84e8d8bef9SDimitry Andric let lane_vt = i32; 85e8d8bef9SDimitry Andric let lane_rc = I32; 86e8d8bef9SDimitry Andric let lane_bits = 8; 87e8d8bef9SDimitry Andric let lane_idx = LaneIdx16; 88bdd1243dSDimitry Andric let lane_load = extloadi8; 89bdd1243dSDimitry Andric let splat = PatFrag<(ops node:$x), (v16i8 (splat_vector (i8 $x)))>; 90e8d8bef9SDimitry Andric let prefix = "i8x16"; 91e8d8bef9SDimitry Andric} 92e8d8bef9SDimitry Andric 93e8d8bef9SDimitry Andricdef I16x8 : Vec { 94e8d8bef9SDimitry Andric let vt = v8i16; 95e8d8bef9SDimitry Andric let int_vt = vt; 96e8d8bef9SDimitry Andric let lane_vt = i32; 97e8d8bef9SDimitry Andric let lane_rc = I32; 98e8d8bef9SDimitry Andric let lane_bits = 16; 99e8d8bef9SDimitry Andric let lane_idx = LaneIdx8; 100bdd1243dSDimitry Andric let lane_load = extloadi16; 101bdd1243dSDimitry Andric let splat = PatFrag<(ops node:$x), (v8i16 (splat_vector (i16 $x)))>; 102e8d8bef9SDimitry Andric let prefix = "i16x8"; 103e8d8bef9SDimitry Andric let split = I8x16; 104e8d8bef9SDimitry Andric} 105e8d8bef9SDimitry Andric 106e8d8bef9SDimitry Andricdef I32x4 : Vec { 107e8d8bef9SDimitry Andric let vt = v4i32; 108e8d8bef9SDimitry Andric let int_vt = vt; 109e8d8bef9SDimitry Andric let lane_vt = i32; 110e8d8bef9SDimitry Andric let lane_rc = I32; 111e8d8bef9SDimitry Andric let lane_bits = 32; 112e8d8bef9SDimitry Andric let lane_idx = LaneIdx4; 113bdd1243dSDimitry Andric let lane_load = load; 114bdd1243dSDimitry Andric let splat = PatFrag<(ops node:$x), (v4i32 (splat_vector (i32 $x)))>; 115e8d8bef9SDimitry Andric let prefix = "i32x4"; 116e8d8bef9SDimitry Andric let split = I16x8; 117e8d8bef9SDimitry Andric} 118e8d8bef9SDimitry Andric 119e8d8bef9SDimitry Andricdef I64x2 : Vec { 120e8d8bef9SDimitry Andric let vt = v2i64; 121e8d8bef9SDimitry Andric let int_vt = vt; 122e8d8bef9SDimitry Andric let lane_vt = i64; 123e8d8bef9SDimitry Andric let lane_rc = I64; 124e8d8bef9SDimitry Andric let lane_bits = 64; 125e8d8bef9SDimitry Andric let lane_idx = LaneIdx2; 126bdd1243dSDimitry Andric let lane_load = load; 127bdd1243dSDimitry Andric let splat = PatFrag<(ops node:$x), (v2i64 (splat_vector (i64 $x)))>; 128e8d8bef9SDimitry Andric let prefix = "i64x2"; 129e8d8bef9SDimitry Andric let split = I32x4; 130e8d8bef9SDimitry Andric} 131e8d8bef9SDimitry Andric 132e8d8bef9SDimitry Andricdef F32x4 : Vec { 133e8d8bef9SDimitry Andric let vt = v4f32; 134e8d8bef9SDimitry Andric let int_vt = v4i32; 135e8d8bef9SDimitry Andric let lane_vt = f32; 136e8d8bef9SDimitry Andric let lane_rc = F32; 137e8d8bef9SDimitry Andric let lane_bits = 32; 138e8d8bef9SDimitry Andric let lane_idx = LaneIdx4; 139bdd1243dSDimitry Andric let lane_load = load; 140bdd1243dSDimitry Andric let splat = PatFrag<(ops node:$x), (v4f32 (splat_vector (f32 $x)))>; 141e8d8bef9SDimitry Andric let prefix = "f32x4"; 142e8d8bef9SDimitry Andric} 143e8d8bef9SDimitry Andric 144e8d8bef9SDimitry Andricdef F64x2 : Vec { 145e8d8bef9SDimitry Andric let vt = v2f64; 146e8d8bef9SDimitry Andric let int_vt = v2i64; 147e8d8bef9SDimitry Andric let lane_vt = f64; 148e8d8bef9SDimitry Andric let lane_rc = F64; 149e8d8bef9SDimitry Andric let lane_bits = 64; 150e8d8bef9SDimitry Andric let lane_idx = LaneIdx2; 151bdd1243dSDimitry Andric let lane_load = load; 152bdd1243dSDimitry Andric let splat = PatFrag<(ops node:$x), (v2f64 (splat_vector (f64 $x)))>; 153e8d8bef9SDimitry Andric let prefix = "f64x2"; 154e8d8bef9SDimitry Andric} 155e8d8bef9SDimitry Andric 156*0fca6ea1SDimitry Andricdef F16x8 : Vec { 157*0fca6ea1SDimitry Andric let vt = v8f16; 158*0fca6ea1SDimitry Andric let int_vt = v8i16; 159*0fca6ea1SDimitry Andric let lane_vt = f32; 160*0fca6ea1SDimitry Andric let lane_rc = F32; 161*0fca6ea1SDimitry Andric let lane_bits = 16; 162*0fca6ea1SDimitry Andric let lane_idx = LaneIdx8; 163*0fca6ea1SDimitry Andric let lane_load = int_wasm_loadf16_f32; 164*0fca6ea1SDimitry Andric let splat = PatFrag<(ops node:$x), (v8f16 (splat_vector (f16 $x)))>; 165*0fca6ea1SDimitry Andric let prefix = "f16x8"; 166*0fca6ea1SDimitry Andric} 167*0fca6ea1SDimitry Andric 168*0fca6ea1SDimitry Andric// TODO: Include F16x8 here when half precision is better supported. 169e8d8bef9SDimitry Andricdefvar AllVecs = [I8x16, I16x8, I32x4, I64x2, F32x4, F64x2]; 170e8d8bef9SDimitry Andricdefvar IntVecs = [I8x16, I16x8, I32x4, I64x2]; 171e8d8bef9SDimitry Andric 1720b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 1730b57cec5SDimitry Andric// Load and store 1740b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric// Load: v128.load 1775ffd83dbSDimitry Andriclet mayLoad = 1, UseNamedOperandTable = 1 in { 1785ffd83dbSDimitry Andricdefm LOAD_V128_A32 : 1790b57cec5SDimitry Andric SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr), 1800b57cec5SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off), [], 1810b57cec5SDimitry Andric "v128.load\t$dst, ${off}(${addr})$p2align", 1820b57cec5SDimitry Andric "v128.load\t$off$p2align", 0>; 1835ffd83dbSDimitry Andricdefm LOAD_V128_A64 : 1845ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr), 1855ffd83dbSDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off), [], 1865ffd83dbSDimitry Andric "v128.load\t$dst, ${off}(${addr})$p2align", 1875ffd83dbSDimitry Andric "v128.load\t$off$p2align", 0>; 1885ffd83dbSDimitry Andric} 1890b57cec5SDimitry Andric 190e8d8bef9SDimitry Andric// Def load patterns from WebAssemblyInstrMemory.td for vector types 191e8d8bef9SDimitry Andricforeach vec = AllVecs in { 192bdd1243dSDimitry Andricdefm : LoadPat<vec.vt, load, "LOAD_V128">; 1930b57cec5SDimitry Andric} 1940b57cec5SDimitry Andric 195e8d8bef9SDimitry Andric// v128.loadX_splat 196e8d8bef9SDimitry Andricmulticlass SIMDLoadSplat<int size, bits<32> simdop> { 1975ffd83dbSDimitry Andric let mayLoad = 1, UseNamedOperandTable = 1 in { 198e8d8bef9SDimitry Andric defm LOAD#size#_SPLAT_A32 : 1995ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), 2005ffd83dbSDimitry Andric (ins P2Align:$p2align, offset32_op:$off, I32:$addr), 2015ffd83dbSDimitry Andric (outs), 2025ffd83dbSDimitry Andric (ins P2Align:$p2align, offset32_op:$off), [], 203e8d8bef9SDimitry Andric "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align", 204e8d8bef9SDimitry Andric "v128.load"#size#"_splat\t$off$p2align", simdop>; 205e8d8bef9SDimitry Andric defm LOAD#size#_SPLAT_A64 : 2065ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), 2075ffd83dbSDimitry Andric (ins P2Align:$p2align, offset64_op:$off, I64:$addr), 2085ffd83dbSDimitry Andric (outs), 2095ffd83dbSDimitry Andric (ins P2Align:$p2align, offset64_op:$off), [], 210e8d8bef9SDimitry Andric "v128.load"#size#"_splat\t$dst, ${off}(${addr})$p2align", 211e8d8bef9SDimitry Andric "v128.load"#size#"_splat\t$off$p2align", simdop>; 2128bcb0991SDimitry Andric } 2135ffd83dbSDimitry Andric} 2148bcb0991SDimitry Andric 215e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<8, 7>; 216e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<16, 8>; 217e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<32, 9>; 218e8d8bef9SDimitry Andricdefm "" : SIMDLoadSplat<64, 10>; 2198bcb0991SDimitry Andric 220e8d8bef9SDimitry Andricforeach vec = AllVecs in { 221e8d8bef9SDimitry Andric defvar inst = "LOAD"#vec.lane_bits#"_SPLAT"; 222bdd1243dSDimitry Andric defm : LoadPat<vec.vt, 223bdd1243dSDimitry Andric PatFrag<(ops node:$addr), (splat_vector (vec.lane_vt (vec.lane_load node:$addr)))>, 224bdd1243dSDimitry Andric inst>; 2258bcb0991SDimitry Andric} 2268bcb0991SDimitry Andric 2278bcb0991SDimitry Andric// Load and extend 228e8d8bef9SDimitry Andricmulticlass SIMDLoadExtend<Vec vec, string loadPat, bits<32> simdop> { 229e8d8bef9SDimitry Andric defvar signed = vec.prefix#".load"#loadPat#"_s"; 230e8d8bef9SDimitry Andric defvar unsigned = vec.prefix#".load"#loadPat#"_u"; 2315ffd83dbSDimitry Andric let mayLoad = 1, UseNamedOperandTable = 1 in { 232e8d8bef9SDimitry Andric defm LOAD_EXTEND_S_#vec#_A32 : 2335ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), 2345ffd83dbSDimitry Andric (ins P2Align:$p2align, offset32_op:$off, I32:$addr), 2358bcb0991SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off), [], 236e8d8bef9SDimitry Andric signed#"\t$dst, ${off}(${addr})$p2align", 237e8d8bef9SDimitry Andric signed#"\t$off$p2align", simdop>; 238e8d8bef9SDimitry Andric defm LOAD_EXTEND_U_#vec#_A32 : 2395ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), 2405ffd83dbSDimitry Andric (ins P2Align:$p2align, offset32_op:$off, I32:$addr), 2418bcb0991SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off), [], 242e8d8bef9SDimitry Andric unsigned#"\t$dst, ${off}(${addr})$p2align", 243e8d8bef9SDimitry Andric unsigned#"\t$off$p2align", !add(simdop, 1)>; 244e8d8bef9SDimitry Andric defm LOAD_EXTEND_S_#vec#_A64 : 2455ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), 2465ffd83dbSDimitry Andric (ins P2Align:$p2align, offset64_op:$off, I64:$addr), 2475ffd83dbSDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off), [], 248e8d8bef9SDimitry Andric signed#"\t$dst, ${off}(${addr})$p2align", 249e8d8bef9SDimitry Andric signed#"\t$off$p2align", simdop>; 250e8d8bef9SDimitry Andric defm LOAD_EXTEND_U_#vec#_A64 : 2515ffd83dbSDimitry Andric SIMD_I<(outs V128:$dst), 2525ffd83dbSDimitry Andric (ins P2Align:$p2align, offset64_op:$off, I64:$addr), 2535ffd83dbSDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off), [], 254e8d8bef9SDimitry Andric unsigned#"\t$dst, ${off}(${addr})$p2align", 255e8d8bef9SDimitry Andric unsigned#"\t$off$p2align", !add(simdop, 1)>; 2568bcb0991SDimitry Andric } 2578bcb0991SDimitry Andric} 2588bcb0991SDimitry Andric 259e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I16x8, "8x8", 1>; 260e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I32x4, "16x4", 3>; 261e8d8bef9SDimitry Andricdefm "" : SIMDLoadExtend<I64x2, "32x2", 5>; 2628bcb0991SDimitry Andric 263e8d8bef9SDimitry Andricforeach vec = [I16x8, I32x4, I64x2] in 264e8d8bef9SDimitry Andricforeach exts = [["sextloadvi", "_S"], 265e8d8bef9SDimitry Andric ["zextloadvi", "_U"], 266e8d8bef9SDimitry Andric ["extloadvi", "_U"]] in { 267e8d8bef9SDimitry Andricdefvar loadpat = !cast<PatFrag>(exts[0]#vec.split.lane_bits); 268e8d8bef9SDimitry Andricdefvar inst = "LOAD_EXTEND"#exts[1]#"_"#vec; 269bdd1243dSDimitry Andricdefm : LoadPat<vec.vt, loadpat, inst>; 2708bcb0991SDimitry Andric} 2718bcb0991SDimitry Andric 272e8d8bef9SDimitry Andric// Load lane into zero vector 273e8d8bef9SDimitry Andricmulticlass SIMDLoadZero<Vec vec, bits<32> simdop> { 274e8d8bef9SDimitry Andric defvar name = "v128.load"#vec.lane_bits#"_zero"; 275e8d8bef9SDimitry Andric let mayLoad = 1, UseNamedOperandTable = 1 in { 276e8d8bef9SDimitry Andric defm LOAD_ZERO_#vec#_A32 : 277e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), 278e8d8bef9SDimitry Andric (ins P2Align:$p2align, offset32_op:$off, I32:$addr), 279e8d8bef9SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off), [], 280e8d8bef9SDimitry Andric name#"\t$dst, ${off}(${addr})$p2align", 281e8d8bef9SDimitry Andric name#"\t$off$p2align", simdop>; 282e8d8bef9SDimitry Andric defm LOAD_ZERO_#vec#_A64 : 283e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), 284e8d8bef9SDimitry Andric (ins P2Align:$p2align, offset64_op:$off, I64:$addr), 285e8d8bef9SDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off), [], 286e8d8bef9SDimitry Andric name#"\t$dst, ${off}(${addr})$p2align", 287e8d8bef9SDimitry Andric name#"\t$off$p2align", simdop>; 288e8d8bef9SDimitry Andric } // mayLoad = 1, UseNamedOperandTable = 1 289e8d8bef9SDimitry Andric} 290e8d8bef9SDimitry Andric 291fe6060f1SDimitry Andricdefm "" : SIMDLoadZero<I32x4, 0x5c>; 292fe6060f1SDimitry Andricdefm "" : SIMDLoadZero<I64x2, 0x5d>; 293e8d8bef9SDimitry Andric 294349cc55cSDimitry Andric// Use load_zero to load scalars into vectors as well where possible. 295bdd1243dSDimitry Andric// TODO: i16, and i8 scalars 296bdd1243dSDimitry Andricforeach vec = [I32x4, I64x2] in { 297bdd1243dSDimitry Andric defvar inst = "LOAD_ZERO_"#vec; 298bdd1243dSDimitry Andric defvar pat = PatFrag<(ops node:$addr), (scalar_to_vector (vec.lane_vt (load $addr)))>; 299bdd1243dSDimitry Andric defm : LoadPat<vec.vt, pat, inst>; 300bdd1243dSDimitry Andric} 301349cc55cSDimitry Andric 302fe6060f1SDimitry Andric// TODO: f32x4 and f64x2 as well 303e8d8bef9SDimitry Andricforeach vec = [I32x4, I64x2] in { 304e8d8bef9SDimitry Andric defvar inst = "LOAD_ZERO_"#vec; 305fe6060f1SDimitry Andric defvar pat = PatFrag<(ops node:$ptr), 306fe6060f1SDimitry Andric (vector_insert (vec.splat (vec.lane_vt 0)), (vec.lane_vt (load $ptr)), 0)>; 307bdd1243dSDimitry Andric defm : LoadPat<vec.vt, pat, inst>; 308e8d8bef9SDimitry Andric} 309e8d8bef9SDimitry Andric 310e8d8bef9SDimitry Andric// Load lane 311e8d8bef9SDimitry Andricmulticlass SIMDLoadLane<Vec vec, bits<32> simdop> { 312e8d8bef9SDimitry Andric defvar name = "v128.load"#vec.lane_bits#"_lane"; 313e8d8bef9SDimitry Andric let mayLoad = 1, UseNamedOperandTable = 1 in { 314e8d8bef9SDimitry Andric defm LOAD_LANE_#vec#_A32 : 315e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), 316e8d8bef9SDimitry Andric (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx, 317e8d8bef9SDimitry Andric I32:$addr, V128:$vec), 318e8d8bef9SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx), 319e8d8bef9SDimitry Andric [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx", 320e8d8bef9SDimitry Andric name#"\t$off$p2align, $idx", simdop>; 321e8d8bef9SDimitry Andric defm LOAD_LANE_#vec#_A64 : 322e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), 323e8d8bef9SDimitry Andric (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx, 324e8d8bef9SDimitry Andric I64:$addr, V128:$vec), 325e8d8bef9SDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx), 326e8d8bef9SDimitry Andric [], name#"\t$dst, ${off}(${addr})$p2align, $vec, $idx", 327e8d8bef9SDimitry Andric name#"\t$off$p2align, $idx", simdop>; 328e8d8bef9SDimitry Andric } // mayLoad = 1, UseNamedOperandTable = 1 329e8d8bef9SDimitry Andric} 330e8d8bef9SDimitry Andric 331fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I8x16, 0x54>; 332fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I16x8, 0x55>; 333fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I32x4, 0x56>; 334fe6060f1SDimitry Andricdefm "" : SIMDLoadLane<I64x2, 0x57>; 335e8d8bef9SDimitry Andric 336e8d8bef9SDimitry Andric// Select loads with no constant offset. 337fe6060f1SDimitry Andricmulticlass LoadLanePatNoOffset<Vec vec, SDPatternOperator kind> { 338e8d8bef9SDimitry Andric defvar load_lane_a32 = !cast<NI>("LOAD_LANE_"#vec#"_A32"); 339e8d8bef9SDimitry Andric defvar load_lane_a64 = !cast<NI>("LOAD_LANE_"#vec#"_A64"); 340e8d8bef9SDimitry Andric def : Pat<(vec.vt (kind (i32 I32:$addr), 341e8d8bef9SDimitry Andric (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))), 342e8d8bef9SDimitry Andric (load_lane_a32 0, 0, imm:$idx, $addr, $vec)>, 343e8d8bef9SDimitry Andric Requires<[HasAddr32]>; 344e8d8bef9SDimitry Andric def : Pat<(vec.vt (kind (i64 I64:$addr), 345e8d8bef9SDimitry Andric (vec.vt V128:$vec), (i32 vec.lane_idx:$idx))), 346e8d8bef9SDimitry Andric (load_lane_a64 0, 0, imm:$idx, $addr, $vec)>, 347e8d8bef9SDimitry Andric Requires<[HasAddr64]>; 348e8d8bef9SDimitry Andric} 349e8d8bef9SDimitry Andric 350fe6060f1SDimitry Andricdef load8_lane : 351fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 352fe6060f1SDimitry Andric (vector_insert $vec, (i32 (extloadi8 $ptr)), $idx)>; 353fe6060f1SDimitry Andricdef load16_lane : 354fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 355fe6060f1SDimitry Andric (vector_insert $vec, (i32 (extloadi16 $ptr)), $idx)>; 356fe6060f1SDimitry Andricdef load32_lane : 357fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 358fe6060f1SDimitry Andric (vector_insert $vec, (i32 (load $ptr)), $idx)>; 359fe6060f1SDimitry Andricdef load64_lane : 360fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 361fe6060f1SDimitry Andric (vector_insert $vec, (i64 (load $ptr)), $idx)>; 362fe6060f1SDimitry Andric// TODO: floating point lanes as well 363fe6060f1SDimitry Andric 364fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I8x16, load8_lane>; 365fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I16x8, load16_lane>; 366fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I32x4, load32_lane>; 367fe6060f1SDimitry Andricdefm : LoadLanePatNoOffset<I64x2, load64_lane>; 368e8d8bef9SDimitry Andric 369e8d8bef9SDimitry Andric// TODO: Also support the other load patterns for load_lane once the instructions 370e8d8bef9SDimitry Andric// are merged to the proposal. 3718bcb0991SDimitry Andric 3720b57cec5SDimitry Andric// Store: v128.store 3735ffd83dbSDimitry Andriclet mayStore = 1, UseNamedOperandTable = 1 in { 3745ffd83dbSDimitry Andricdefm STORE_V128_A32 : 3750b57cec5SDimitry Andric SIMD_I<(outs), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, V128:$vec), 3760b57cec5SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off), [], 3770b57cec5SDimitry Andric "v128.store\t${off}(${addr})$p2align, $vec", 3785ffd83dbSDimitry Andric "v128.store\t$off$p2align", 11>; 3795ffd83dbSDimitry Andricdefm STORE_V128_A64 : 3805ffd83dbSDimitry Andric SIMD_I<(outs), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, V128:$vec), 3815ffd83dbSDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off), [], 3825ffd83dbSDimitry Andric "v128.store\t${off}(${addr})$p2align, $vec", 3835ffd83dbSDimitry Andric "v128.store\t$off$p2align", 11>; 3845ffd83dbSDimitry Andric} 385e8d8bef9SDimitry Andric 386e8d8bef9SDimitry Andric// Def store patterns from WebAssemblyInstrMemory.td for vector types 387e8d8bef9SDimitry Andricforeach vec = AllVecs in { 388bdd1243dSDimitry Andricdefm : StorePat<vec.vt, store, "STORE_V128">; 3890b57cec5SDimitry Andric} 3900b57cec5SDimitry Andric 391e8d8bef9SDimitry Andric// Store lane 392e8d8bef9SDimitry Andricmulticlass SIMDStoreLane<Vec vec, bits<32> simdop> { 393e8d8bef9SDimitry Andric defvar name = "v128.store"#vec.lane_bits#"_lane"; 394e8d8bef9SDimitry Andric let mayStore = 1, UseNamedOperandTable = 1 in { 395e8d8bef9SDimitry Andric defm STORE_LANE_#vec#_A32 : 396e8d8bef9SDimitry Andric SIMD_I<(outs), 397e8d8bef9SDimitry Andric (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx, 398e8d8bef9SDimitry Andric I32:$addr, V128:$vec), 399e8d8bef9SDimitry Andric (outs), (ins P2Align:$p2align, offset32_op:$off, vec_i8imm_op:$idx), 400e8d8bef9SDimitry Andric [], name#"\t${off}(${addr})$p2align, $vec, $idx", 401e8d8bef9SDimitry Andric name#"\t$off$p2align, $idx", simdop>; 402e8d8bef9SDimitry Andric defm STORE_LANE_#vec#_A64 : 40306c3fb27SDimitry Andric SIMD_I<(outs), 404e8d8bef9SDimitry Andric (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx, 405e8d8bef9SDimitry Andric I64:$addr, V128:$vec), 406e8d8bef9SDimitry Andric (outs), (ins P2Align:$p2align, offset64_op:$off, vec_i8imm_op:$idx), 407e8d8bef9SDimitry Andric [], name#"\t${off}(${addr})$p2align, $vec, $idx", 408e8d8bef9SDimitry Andric name#"\t$off$p2align, $idx", simdop>; 409e8d8bef9SDimitry Andric } // mayStore = 1, UseNamedOperandTable = 1 410e8d8bef9SDimitry Andric} 411e8d8bef9SDimitry Andric 412fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I8x16, 0x58>; 413fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I16x8, 0x59>; 414fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I32x4, 0x5a>; 415fe6060f1SDimitry Andricdefm "" : SIMDStoreLane<I64x2, 0x5b>; 416e8d8bef9SDimitry Andric 417bdd1243dSDimitry Andricmulticlass StoreLanePat<Vec vec, SDPatternOperator kind> { 418bdd1243dSDimitry Andric def : Pat<(kind (AddrOps32 offset32_op:$offset, I32:$addr), 419bdd1243dSDimitry Andric (vec.vt V128:$vec), 420bdd1243dSDimitry Andric (i32 vec.lane_idx:$idx)), 421bdd1243dSDimitry Andric (!cast<NI>("STORE_LANE_"#vec#"_A32") 0, $offset, imm:$idx, $addr, $vec)>, 422e8d8bef9SDimitry Andric Requires<[HasAddr32]>; 423bdd1243dSDimitry Andric def : Pat<(kind (AddrOps64 offset64_op:$offset, I64:$addr), 424bdd1243dSDimitry Andric (vec.vt V128:$vec), 425bdd1243dSDimitry Andric (i32 vec.lane_idx:$idx)), 426bdd1243dSDimitry Andric (!cast<NI>("STORE_LANE_"#vec#"_A64") 0, $offset, imm:$idx, $addr, $vec)>, 427e8d8bef9SDimitry Andric Requires<[HasAddr64]>; 428e8d8bef9SDimitry Andric} 429e8d8bef9SDimitry Andric 430fe6060f1SDimitry Andricdef store8_lane : 431fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 432fe6060f1SDimitry Andric (truncstorei8 (i32 (vector_extract $vec, $idx)), $ptr)>; 433fe6060f1SDimitry Andricdef store16_lane : 434fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 435fe6060f1SDimitry Andric (truncstorei16 (i32 (vector_extract $vec, $idx)), $ptr)>; 436fe6060f1SDimitry Andricdef store32_lane : 437fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 438fe6060f1SDimitry Andric (store (i32 (vector_extract $vec, $idx)), $ptr)>; 439fe6060f1SDimitry Andricdef store64_lane : 440fe6060f1SDimitry Andric PatFrag<(ops node:$ptr, node:$vec, node:$idx), 441fe6060f1SDimitry Andric (store (i64 (vector_extract $vec, $idx)), $ptr)>; 442fe6060f1SDimitry Andric// TODO: floating point lanes as well 443e8d8bef9SDimitry Andric 444fe6060f1SDimitry Andriclet AddedComplexity = 1 in { 445bdd1243dSDimitry Andricdefm : StoreLanePat<I8x16, store8_lane>; 446bdd1243dSDimitry Andricdefm : StoreLanePat<I16x8, store16_lane>; 447bdd1243dSDimitry Andricdefm : StoreLanePat<I32x4, store32_lane>; 448bdd1243dSDimitry Andricdefm : StoreLanePat<I64x2, store64_lane>; 449fe6060f1SDimitry Andric} 450e8d8bef9SDimitry Andric 4510b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 4520b57cec5SDimitry Andric// Constructing SIMD values 4530b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric// Constant: v128.const 456e8d8bef9SDimitry Andricmulticlass ConstVec<Vec vec, dag ops, dag pat, string args> { 457fe6060f1SDimitry Andric let isMoveImm = 1, isReMaterializable = 1 in 458e8d8bef9SDimitry Andric defm CONST_V128_#vec : SIMD_I<(outs V128:$dst), ops, (outs), ops, 459e8d8bef9SDimitry Andric [(set V128:$dst, (vec.vt pat))], 4600b57cec5SDimitry Andric "v128.const\t$dst, "#args, 4615ffd83dbSDimitry Andric "v128.const\t"#args, 12>; 4620b57cec5SDimitry Andric} 4630b57cec5SDimitry Andric 464e8d8bef9SDimitry Andricdefm "" : ConstVec<I8x16, 4650b57cec5SDimitry Andric (ins vec_i8imm_op:$i0, vec_i8imm_op:$i1, 4660b57cec5SDimitry Andric vec_i8imm_op:$i2, vec_i8imm_op:$i3, 4670b57cec5SDimitry Andric vec_i8imm_op:$i4, vec_i8imm_op:$i5, 4680b57cec5SDimitry Andric vec_i8imm_op:$i6, vec_i8imm_op:$i7, 4690b57cec5SDimitry Andric vec_i8imm_op:$i8, vec_i8imm_op:$i9, 4700b57cec5SDimitry Andric vec_i8imm_op:$iA, vec_i8imm_op:$iB, 4710b57cec5SDimitry Andric vec_i8imm_op:$iC, vec_i8imm_op:$iD, 4720b57cec5SDimitry Andric vec_i8imm_op:$iE, vec_i8imm_op:$iF), 4730b57cec5SDimitry Andric (build_vector ImmI8:$i0, ImmI8:$i1, ImmI8:$i2, ImmI8:$i3, 4740b57cec5SDimitry Andric ImmI8:$i4, ImmI8:$i5, ImmI8:$i6, ImmI8:$i7, 4750b57cec5SDimitry Andric ImmI8:$i8, ImmI8:$i9, ImmI8:$iA, ImmI8:$iB, 4760b57cec5SDimitry Andric ImmI8:$iC, ImmI8:$iD, ImmI8:$iE, ImmI8:$iF), 4770b57cec5SDimitry Andric !strconcat("$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7, ", 4780b57cec5SDimitry Andric "$i8, $i9, $iA, $iB, $iC, $iD, $iE, $iF")>; 479e8d8bef9SDimitry Andricdefm "" : ConstVec<I16x8, 4800b57cec5SDimitry Andric (ins vec_i16imm_op:$i0, vec_i16imm_op:$i1, 4810b57cec5SDimitry Andric vec_i16imm_op:$i2, vec_i16imm_op:$i3, 4820b57cec5SDimitry Andric vec_i16imm_op:$i4, vec_i16imm_op:$i5, 4830b57cec5SDimitry Andric vec_i16imm_op:$i6, vec_i16imm_op:$i7), 4840b57cec5SDimitry Andric (build_vector 4850b57cec5SDimitry Andric ImmI16:$i0, ImmI16:$i1, ImmI16:$i2, ImmI16:$i3, 4860b57cec5SDimitry Andric ImmI16:$i4, ImmI16:$i5, ImmI16:$i6, ImmI16:$i7), 4870b57cec5SDimitry Andric "$i0, $i1, $i2, $i3, $i4, $i5, $i6, $i7">; 4880b57cec5SDimitry Andriclet IsCanonical = 1 in 489e8d8bef9SDimitry Andricdefm "" : ConstVec<I32x4, 4900b57cec5SDimitry Andric (ins vec_i32imm_op:$i0, vec_i32imm_op:$i1, 4910b57cec5SDimitry Andric vec_i32imm_op:$i2, vec_i32imm_op:$i3), 4920b57cec5SDimitry Andric (build_vector (i32 imm:$i0), (i32 imm:$i1), 4930b57cec5SDimitry Andric (i32 imm:$i2), (i32 imm:$i3)), 4940b57cec5SDimitry Andric "$i0, $i1, $i2, $i3">; 495e8d8bef9SDimitry Andricdefm "" : ConstVec<I64x2, 4960b57cec5SDimitry Andric (ins vec_i64imm_op:$i0, vec_i64imm_op:$i1), 4970b57cec5SDimitry Andric (build_vector (i64 imm:$i0), (i64 imm:$i1)), 4980b57cec5SDimitry Andric "$i0, $i1">; 499e8d8bef9SDimitry Andricdefm "" : ConstVec<F32x4, 5000b57cec5SDimitry Andric (ins f32imm_op:$i0, f32imm_op:$i1, 5010b57cec5SDimitry Andric f32imm_op:$i2, f32imm_op:$i3), 5020b57cec5SDimitry Andric (build_vector (f32 fpimm:$i0), (f32 fpimm:$i1), 5030b57cec5SDimitry Andric (f32 fpimm:$i2), (f32 fpimm:$i3)), 5040b57cec5SDimitry Andric "$i0, $i1, $i2, $i3">; 505e8d8bef9SDimitry Andricdefm "" : ConstVec<F64x2, 5060b57cec5SDimitry Andric (ins f64imm_op:$i0, f64imm_op:$i1), 5070b57cec5SDimitry Andric (build_vector (f64 fpimm:$i0), (f64 fpimm:$i1)), 5080b57cec5SDimitry Andric "$i0, $i1">; 5090b57cec5SDimitry Andric 510bdd1243dSDimitry Andric// Match splat(x) -> const.v128(x, ..., x) 511bdd1243dSDimitry Andricforeach vec = AllVecs in { 512bdd1243dSDimitry Andric defvar numEls = !div(vec.vt.Size, vec.lane_bits); 513bdd1243dSDimitry Andric defvar isFloat = !or(!eq(vec.lane_vt, f32), !eq(vec.lane_vt, f64)); 514bdd1243dSDimitry Andric defvar immKind = !if(isFloat, fpimm, imm); 515bdd1243dSDimitry Andric def : Pat<(vec.splat (vec.lane_vt immKind:$x)), 516bdd1243dSDimitry Andric !dag(!cast<NI>("CONST_V128_"#vec), 517bdd1243dSDimitry Andric !listsplat((vec.lane_vt immKind:$x), numEls), 518bdd1243dSDimitry Andric ?)>; 519bdd1243dSDimitry Andric} 520bdd1243dSDimitry Andric 5210b57cec5SDimitry Andric// Shuffle lanes: shuffle 5220b57cec5SDimitry Andricdefm SHUFFLE : 5230b57cec5SDimitry Andric SIMD_I<(outs V128:$dst), 5240b57cec5SDimitry Andric (ins V128:$x, V128:$y, 5250b57cec5SDimitry Andric vec_i8imm_op:$m0, vec_i8imm_op:$m1, 5260b57cec5SDimitry Andric vec_i8imm_op:$m2, vec_i8imm_op:$m3, 5270b57cec5SDimitry Andric vec_i8imm_op:$m4, vec_i8imm_op:$m5, 5280b57cec5SDimitry Andric vec_i8imm_op:$m6, vec_i8imm_op:$m7, 5290b57cec5SDimitry Andric vec_i8imm_op:$m8, vec_i8imm_op:$m9, 5300b57cec5SDimitry Andric vec_i8imm_op:$mA, vec_i8imm_op:$mB, 5310b57cec5SDimitry Andric vec_i8imm_op:$mC, vec_i8imm_op:$mD, 5320b57cec5SDimitry Andric vec_i8imm_op:$mE, vec_i8imm_op:$mF), 5330b57cec5SDimitry Andric (outs), 5340b57cec5SDimitry Andric (ins 5350b57cec5SDimitry Andric vec_i8imm_op:$m0, vec_i8imm_op:$m1, 5360b57cec5SDimitry Andric vec_i8imm_op:$m2, vec_i8imm_op:$m3, 5370b57cec5SDimitry Andric vec_i8imm_op:$m4, vec_i8imm_op:$m5, 5380b57cec5SDimitry Andric vec_i8imm_op:$m6, vec_i8imm_op:$m7, 5390b57cec5SDimitry Andric vec_i8imm_op:$m8, vec_i8imm_op:$m9, 5400b57cec5SDimitry Andric vec_i8imm_op:$mA, vec_i8imm_op:$mB, 5410b57cec5SDimitry Andric vec_i8imm_op:$mC, vec_i8imm_op:$mD, 5420b57cec5SDimitry Andric vec_i8imm_op:$mE, vec_i8imm_op:$mF), 5430b57cec5SDimitry Andric [], 544e8d8bef9SDimitry Andric "i8x16.shuffle\t$dst, $x, $y, "# 5450b57cec5SDimitry Andric "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "# 5460b57cec5SDimitry Andric "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF", 547e8d8bef9SDimitry Andric "i8x16.shuffle\t"# 5480b57cec5SDimitry Andric "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "# 5490b57cec5SDimitry Andric "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF", 5505ffd83dbSDimitry Andric 13>; 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric// Shuffles after custom lowering 5530b57cec5SDimitry Andricdef wasm_shuffle_t : SDTypeProfile<1, 18, []>; 5540b57cec5SDimitry Andricdef wasm_shuffle : SDNode<"WebAssemblyISD::SHUFFLE", wasm_shuffle_t>; 555e8d8bef9SDimitry Andricforeach vec = AllVecs in { 55606c3fb27SDimitry Andric// The @llvm.wasm.shuffle intrinsic has immediate arguments that become TargetConstants. 55706c3fb27SDimitry Andricdef : Pat<(vec.vt (wasm_shuffle (vec.vt V128:$x), (vec.vt V128:$y), 55806c3fb27SDimitry Andric (i32 timm:$m0), (i32 timm:$m1), 55906c3fb27SDimitry Andric (i32 timm:$m2), (i32 timm:$m3), 56006c3fb27SDimitry Andric (i32 timm:$m4), (i32 timm:$m5), 56106c3fb27SDimitry Andric (i32 timm:$m6), (i32 timm:$m7), 56206c3fb27SDimitry Andric (i32 timm:$m8), (i32 timm:$m9), 56306c3fb27SDimitry Andric (i32 timm:$mA), (i32 timm:$mB), 56406c3fb27SDimitry Andric (i32 timm:$mC), (i32 timm:$mD), 56506c3fb27SDimitry Andric (i32 timm:$mE), (i32 timm:$mF))), 56606c3fb27SDimitry Andric (SHUFFLE $x, $y, 56706c3fb27SDimitry Andric imm:$m0, imm:$m1, imm:$m2, imm:$m3, 56806c3fb27SDimitry Andric imm:$m4, imm:$m5, imm:$m6, imm:$m7, 56906c3fb27SDimitry Andric imm:$m8, imm:$m9, imm:$mA, imm:$mB, 57006c3fb27SDimitry Andric imm:$mC, imm:$mD, imm:$mE, imm:$mF)>; 57106c3fb27SDimitry Andric// Normal shufflevector instructions may have normal constant arguemnts. 572e8d8bef9SDimitry Andricdef : Pat<(vec.vt (wasm_shuffle (vec.vt V128:$x), (vec.vt V128:$y), 5730b57cec5SDimitry Andric (i32 LaneIdx32:$m0), (i32 LaneIdx32:$m1), 5740b57cec5SDimitry Andric (i32 LaneIdx32:$m2), (i32 LaneIdx32:$m3), 5750b57cec5SDimitry Andric (i32 LaneIdx32:$m4), (i32 LaneIdx32:$m5), 5760b57cec5SDimitry Andric (i32 LaneIdx32:$m6), (i32 LaneIdx32:$m7), 5770b57cec5SDimitry Andric (i32 LaneIdx32:$m8), (i32 LaneIdx32:$m9), 5780b57cec5SDimitry Andric (i32 LaneIdx32:$mA), (i32 LaneIdx32:$mB), 5790b57cec5SDimitry Andric (i32 LaneIdx32:$mC), (i32 LaneIdx32:$mD), 5800b57cec5SDimitry Andric (i32 LaneIdx32:$mE), (i32 LaneIdx32:$mF))), 581e8d8bef9SDimitry Andric (SHUFFLE $x, $y, 582e8d8bef9SDimitry Andric imm:$m0, imm:$m1, imm:$m2, imm:$m3, 583e8d8bef9SDimitry Andric imm:$m4, imm:$m5, imm:$m6, imm:$m7, 584e8d8bef9SDimitry Andric imm:$m8, imm:$m9, imm:$mA, imm:$mB, 585e8d8bef9SDimitry Andric imm:$mC, imm:$mD, imm:$mE, imm:$mF)>; 5860b57cec5SDimitry Andric} 5870b57cec5SDimitry Andric 588e8d8bef9SDimitry Andric// Swizzle lanes: i8x16.swizzle 5898bcb0991SDimitry Andricdef wasm_swizzle_t : SDTypeProfile<1, 2, []>; 5908bcb0991SDimitry Andricdef wasm_swizzle : SDNode<"WebAssemblyISD::SWIZZLE", wasm_swizzle_t>; 5918bcb0991SDimitry Andricdefm SWIZZLE : 5928bcb0991SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins), 5938bcb0991SDimitry Andric [(set (v16i8 V128:$dst), 5948bcb0991SDimitry Andric (wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))], 595e8d8bef9SDimitry Andric "i8x16.swizzle\t$dst, $src, $mask", "i8x16.swizzle", 14>; 5968bcb0991SDimitry Andric 5978bcb0991SDimitry Andricdef : Pat<(int_wasm_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)), 598e8d8bef9SDimitry Andric (SWIZZLE $src, $mask)>; 5998bcb0991SDimitry Andric 600e8d8bef9SDimitry Andricmulticlass Splat<Vec vec, bits<32> simdop> { 601e8d8bef9SDimitry Andric defm SPLAT_#vec : SIMD_I<(outs V128:$dst), (ins vec.lane_rc:$x), 602e8d8bef9SDimitry Andric (outs), (ins), 603e8d8bef9SDimitry Andric [(set (vec.vt V128:$dst), 604e8d8bef9SDimitry Andric (vec.splat vec.lane_rc:$x))], 605e8d8bef9SDimitry Andric vec.prefix#".splat\t$dst, $x", vec.prefix#".splat", 606e8d8bef9SDimitry Andric simdop>; 6070b57cec5SDimitry Andric} 6080b57cec5SDimitry Andric 609e8d8bef9SDimitry Andricdefm "" : Splat<I8x16, 15>; 610e8d8bef9SDimitry Andricdefm "" : Splat<I16x8, 16>; 611e8d8bef9SDimitry Andricdefm "" : Splat<I32x4, 17>; 612e8d8bef9SDimitry Andricdefm "" : Splat<I64x2, 18>; 613e8d8bef9SDimitry Andricdefm "" : Splat<F32x4, 19>; 614e8d8bef9SDimitry Andricdefm "" : Splat<F64x2, 20>; 6150b57cec5SDimitry Andric 616*0fca6ea1SDimitry Andric// Half values are not fully supported so an intrinsic is used instead of a 617*0fca6ea1SDimitry Andric// regular Splat pattern as above. 618*0fca6ea1SDimitry Andricdefm SPLAT_F16x8 : 619*0fca6ea1SDimitry Andric HALF_PRECISION_I<(outs V128:$dst), (ins F32:$x), 620*0fca6ea1SDimitry Andric (outs), (ins), 621*0fca6ea1SDimitry Andric [(set (v8f16 V128:$dst), (int_wasm_splat_f16x8 F32:$x))], 622*0fca6ea1SDimitry Andric "f16x8.splat\t$dst, $x", "f16x8.splat", 0x120>; 623*0fca6ea1SDimitry Andric 6240b57cec5SDimitry Andric// scalar_to_vector leaves high lanes undefined, so can be a splat 625e8d8bef9SDimitry Andricforeach vec = AllVecs in 626e8d8bef9SDimitry Andricdef : Pat<(vec.vt (scalar_to_vector (vec.lane_vt vec.lane_rc:$x))), 627e8d8bef9SDimitry Andric (!cast<Instruction>("SPLAT_"#vec) $x)>; 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 6300b57cec5SDimitry Andric// Accessing lanes 6310b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric// Extract lane as a scalar: extract_lane / extract_lane_s / extract_lane_u 634e8d8bef9SDimitry Andricmulticlass ExtractLane<Vec vec, bits<32> simdop, string suffix = ""> { 635e8d8bef9SDimitry Andric defm EXTRACT_LANE_#vec#suffix : 636e8d8bef9SDimitry Andric SIMD_I<(outs vec.lane_rc:$dst), (ins V128:$vec, vec_i8imm_op:$idx), 6375ffd83dbSDimitry Andric (outs), (ins vec_i8imm_op:$idx), [], 638e8d8bef9SDimitry Andric vec.prefix#".extract_lane"#suffix#"\t$dst, $vec, $idx", 639e8d8bef9SDimitry Andric vec.prefix#".extract_lane"#suffix#"\t$idx", simdop>; 6400b57cec5SDimitry Andric} 6410b57cec5SDimitry Andric 642e8d8bef9SDimitry Andricdefm "" : ExtractLane<I8x16, 21, "_s">; 643e8d8bef9SDimitry Andricdefm "" : ExtractLane<I8x16, 22, "_u">; 644e8d8bef9SDimitry Andricdefm "" : ExtractLane<I16x8, 24, "_s">; 645e8d8bef9SDimitry Andricdefm "" : ExtractLane<I16x8, 25, "_u">; 646e8d8bef9SDimitry Andricdefm "" : ExtractLane<I32x4, 27>; 647e8d8bef9SDimitry Andricdefm "" : ExtractLane<I64x2, 29>; 648e8d8bef9SDimitry Andricdefm "" : ExtractLane<F32x4, 31>; 649e8d8bef9SDimitry Andricdefm "" : ExtractLane<F64x2, 33>; 6500b57cec5SDimitry Andric 6515ffd83dbSDimitry Andricdef : Pat<(vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), 652e8d8bef9SDimitry Andric (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>; 6535ffd83dbSDimitry Andricdef : Pat<(vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), 654e8d8bef9SDimitry Andric (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>; 6555ffd83dbSDimitry Andricdef : Pat<(vector_extract (v4i32 V128:$vec), (i32 LaneIdx4:$idx)), 656e8d8bef9SDimitry Andric (EXTRACT_LANE_I32x4 $vec, imm:$idx)>; 6575ffd83dbSDimitry Andricdef : Pat<(vector_extract (v4f32 V128:$vec), (i32 LaneIdx4:$idx)), 658e8d8bef9SDimitry Andric (EXTRACT_LANE_F32x4 $vec, imm:$idx)>; 6595ffd83dbSDimitry Andricdef : Pat<(vector_extract (v2i64 V128:$vec), (i32 LaneIdx2:$idx)), 660e8d8bef9SDimitry Andric (EXTRACT_LANE_I64x2 $vec, imm:$idx)>; 6615ffd83dbSDimitry Andricdef : Pat<(vector_extract (v2f64 V128:$vec), (i32 LaneIdx2:$idx)), 662e8d8bef9SDimitry Andric (EXTRACT_LANE_F64x2 $vec, imm:$idx)>; 6630b57cec5SDimitry Andric 6645ffd83dbSDimitry Andricdef : Pat< 6655ffd83dbSDimitry Andric (sext_inreg (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), i8), 666e8d8bef9SDimitry Andric (EXTRACT_LANE_I8x16_s $vec, imm:$idx)>; 6675ffd83dbSDimitry Andricdef : Pat< 6685ffd83dbSDimitry Andric (and (vector_extract (v16i8 V128:$vec), (i32 LaneIdx16:$idx)), (i32 0xff)), 669e8d8bef9SDimitry Andric (EXTRACT_LANE_I8x16_u $vec, imm:$idx)>; 6705ffd83dbSDimitry Andricdef : Pat< 6715ffd83dbSDimitry Andric (sext_inreg (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), i16), 672e8d8bef9SDimitry Andric (EXTRACT_LANE_I16x8_s $vec, imm:$idx)>; 6735ffd83dbSDimitry Andricdef : Pat< 6745ffd83dbSDimitry Andric (and (vector_extract (v8i16 V128:$vec), (i32 LaneIdx8:$idx)), (i32 0xffff)), 675e8d8bef9SDimitry Andric (EXTRACT_LANE_I16x8_u $vec, imm:$idx)>; 6760b57cec5SDimitry Andric 677*0fca6ea1SDimitry Andricdefm EXTRACT_LANE_F16x8 : 678*0fca6ea1SDimitry Andric HALF_PRECISION_I<(outs F32:$dst), (ins V128:$vec, vec_i8imm_op:$idx), 679*0fca6ea1SDimitry Andric (outs), (ins vec_i8imm_op:$idx), 680*0fca6ea1SDimitry Andric [(set (f32 F32:$dst), (int_wasm_extract_lane_f16x8 681*0fca6ea1SDimitry Andric (v8f16 V128:$vec), (i32 LaneIdx16:$idx)))], 682*0fca6ea1SDimitry Andric "f16x8.extract_lane\t$dst, $vec, $idx", 683*0fca6ea1SDimitry Andric "f16x8.extract_lane\t$idx", 0x121>; 684*0fca6ea1SDimitry Andric 6850b57cec5SDimitry Andric// Replace lane value: replace_lane 686e8d8bef9SDimitry Andricmulticlass ReplaceLane<Vec vec, bits<32> simdop> { 687e8d8bef9SDimitry Andric defm REPLACE_LANE_#vec : 688e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$vec, vec_i8imm_op:$idx, vec.lane_rc:$x), 6890b57cec5SDimitry Andric (outs), (ins vec_i8imm_op:$idx), 6900b57cec5SDimitry Andric [(set V128:$dst, (vector_insert 691e8d8bef9SDimitry Andric (vec.vt V128:$vec), 692e8d8bef9SDimitry Andric (vec.lane_vt vec.lane_rc:$x), 693e8d8bef9SDimitry Andric (i32 vec.lane_idx:$idx)))], 694e8d8bef9SDimitry Andric vec.prefix#".replace_lane\t$dst, $vec, $idx, $x", 695e8d8bef9SDimitry Andric vec.prefix#".replace_lane\t$idx", simdop>; 6960b57cec5SDimitry Andric} 6970b57cec5SDimitry Andric 698e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I8x16, 23>; 699e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I16x8, 26>; 700e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I32x4, 28>; 701e8d8bef9SDimitry Andricdefm "" : ReplaceLane<I64x2, 30>; 702e8d8bef9SDimitry Andricdefm "" : ReplaceLane<F32x4, 32>; 703e8d8bef9SDimitry Andricdefm "" : ReplaceLane<F64x2, 34>; 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric// Lower undef lane indices to zero 7060b57cec5SDimitry Andricdef : Pat<(vector_insert (v16i8 V128:$vec), I32:$x, undef), 707e8d8bef9SDimitry Andric (REPLACE_LANE_I8x16 $vec, 0, $x)>; 7080b57cec5SDimitry Andricdef : Pat<(vector_insert (v8i16 V128:$vec), I32:$x, undef), 709e8d8bef9SDimitry Andric (REPLACE_LANE_I16x8 $vec, 0, $x)>; 7100b57cec5SDimitry Andricdef : Pat<(vector_insert (v4i32 V128:$vec), I32:$x, undef), 711e8d8bef9SDimitry Andric (REPLACE_LANE_I32x4 $vec, 0, $x)>; 7120b57cec5SDimitry Andricdef : Pat<(vector_insert (v2i64 V128:$vec), I64:$x, undef), 713e8d8bef9SDimitry Andric (REPLACE_LANE_I64x2 $vec, 0, $x)>; 7140b57cec5SDimitry Andricdef : Pat<(vector_insert (v4f32 V128:$vec), F32:$x, undef), 715e8d8bef9SDimitry Andric (REPLACE_LANE_F32x4 $vec, 0, $x)>; 7160b57cec5SDimitry Andricdef : Pat<(vector_insert (v2f64 V128:$vec), F64:$x, undef), 717e8d8bef9SDimitry Andric (REPLACE_LANE_F64x2 $vec, 0, $x)>; 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 7200b57cec5SDimitry Andric// Comparisons 7210b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 7220b57cec5SDimitry Andric 723*0fca6ea1SDimitry Andricmulticlass SIMDCondition<Vec vec, string name, CondCode cond, bits<32> simdop, 724*0fca6ea1SDimitry Andric list<Predicate> reqs = []> { 725e8d8bef9SDimitry Andric defm _#vec : 7260b57cec5SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins), 727e8d8bef9SDimitry Andric [(set (vec.int_vt V128:$dst), 728e8d8bef9SDimitry Andric (setcc (vec.vt V128:$lhs), (vec.vt V128:$rhs), cond))], 729e8d8bef9SDimitry Andric vec.prefix#"."#name#"\t$dst, $lhs, $rhs", 730*0fca6ea1SDimitry Andric vec.prefix#"."#name, simdop, reqs>; 731*0fca6ea1SDimitry Andric} 732*0fca6ea1SDimitry Andric 733*0fca6ea1SDimitry Andricmulticlass HalfPrecisionCondition<Vec vec, string name, CondCode cond, 734*0fca6ea1SDimitry Andric bits<32> simdop> { 735*0fca6ea1SDimitry Andric defm "" : SIMDCondition<vec, name, cond, simdop, [HasHalfPrecision]>; 7360b57cec5SDimitry Andric} 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andricmulticlass SIMDConditionInt<string name, CondCode cond, bits<32> baseInst> { 739e8d8bef9SDimitry Andric defm "" : SIMDCondition<I8x16, name, cond, baseInst>; 740e8d8bef9SDimitry Andric defm "" : SIMDCondition<I16x8, name, cond, !add(baseInst, 10)>; 741e8d8bef9SDimitry Andric defm "" : SIMDCondition<I32x4, name, cond, !add(baseInst, 20)>; 7420b57cec5SDimitry Andric} 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andricmulticlass SIMDConditionFP<string name, CondCode cond, bits<32> baseInst> { 745e8d8bef9SDimitry Andric defm "" : SIMDCondition<F32x4, name, cond, baseInst>; 746e8d8bef9SDimitry Andric defm "" : SIMDCondition<F64x2, name, cond, !add(baseInst, 6)>; 747*0fca6ea1SDimitry Andric defm "" : HalfPrecisionCondition<F16x8, name, cond, !add(baseInst, 255)>; 7480b57cec5SDimitry Andric} 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric// Equality: eq 7510b57cec5SDimitry Andriclet isCommutable = 1 in { 7525ffd83dbSDimitry Andricdefm EQ : SIMDConditionInt<"eq", SETEQ, 35>; 753fe6060f1SDimitry Andricdefm EQ : SIMDCondition<I64x2, "eq", SETEQ, 214>; 7545ffd83dbSDimitry Andricdefm EQ : SIMDConditionFP<"eq", SETOEQ, 65>; 7550b57cec5SDimitry Andric} // isCommutable = 1 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric// Non-equality: ne 7580b57cec5SDimitry Andriclet isCommutable = 1 in { 7595ffd83dbSDimitry Andricdefm NE : SIMDConditionInt<"ne", SETNE, 36>; 760fe6060f1SDimitry Andricdefm NE : SIMDCondition<I64x2, "ne", SETNE, 215>; 7615ffd83dbSDimitry Andricdefm NE : SIMDConditionFP<"ne", SETUNE, 66>; 7620b57cec5SDimitry Andric} // isCommutable = 1 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric// Less than: lt_s / lt_u / lt 7655ffd83dbSDimitry Andricdefm LT_S : SIMDConditionInt<"lt_s", SETLT, 37>; 766fe6060f1SDimitry Andricdefm LT_S : SIMDCondition<I64x2, "lt_s", SETLT, 216>; 7675ffd83dbSDimitry Andricdefm LT_U : SIMDConditionInt<"lt_u", SETULT, 38>; 7685ffd83dbSDimitry Andricdefm LT : SIMDConditionFP<"lt", SETOLT, 67>; 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric// Greater than: gt_s / gt_u / gt 7715ffd83dbSDimitry Andricdefm GT_S : SIMDConditionInt<"gt_s", SETGT, 39>; 772fe6060f1SDimitry Andricdefm GT_S : SIMDCondition<I64x2, "gt_s", SETGT, 217>; 7735ffd83dbSDimitry Andricdefm GT_U : SIMDConditionInt<"gt_u", SETUGT, 40>; 7745ffd83dbSDimitry Andricdefm GT : SIMDConditionFP<"gt", SETOGT, 68>; 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric// Less than or equal: le_s / le_u / le 7775ffd83dbSDimitry Andricdefm LE_S : SIMDConditionInt<"le_s", SETLE, 41>; 778fe6060f1SDimitry Andricdefm LE_S : SIMDCondition<I64x2, "le_s", SETLE, 218>; 7795ffd83dbSDimitry Andricdefm LE_U : SIMDConditionInt<"le_u", SETULE, 42>; 7805ffd83dbSDimitry Andricdefm LE : SIMDConditionFP<"le", SETOLE, 69>; 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric// Greater than or equal: ge_s / ge_u / ge 7835ffd83dbSDimitry Andricdefm GE_S : SIMDConditionInt<"ge_s", SETGE, 43>; 784fe6060f1SDimitry Andricdefm GE_S : SIMDCondition<I64x2, "ge_s", SETGE, 219>; 7855ffd83dbSDimitry Andricdefm GE_U : SIMDConditionInt<"ge_u", SETUGE, 44>; 7865ffd83dbSDimitry Andricdefm GE : SIMDConditionFP<"ge", SETOGE, 70>; 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric// Lower float comparisons that don't care about NaN to standard WebAssembly 7890b57cec5SDimitry Andric// float comparisons. These instructions are generated with nnan and in the 7900b57cec5SDimitry Andric// target-independent expansion of unordered comparisons and ordered ne. 791e8d8bef9SDimitry Andricforeach nodes = [[seteq, EQ_F32x4], [setne, NE_F32x4], [setlt, LT_F32x4], 792e8d8bef9SDimitry Andric [setgt, GT_F32x4], [setle, LE_F32x4], [setge, GE_F32x4]] in 7930b57cec5SDimitry Andricdef : Pat<(v4i32 (nodes[0] (v4f32 V128:$lhs), (v4f32 V128:$rhs))), 794e8d8bef9SDimitry Andric (nodes[1] $lhs, $rhs)>; 7950b57cec5SDimitry Andric 796e8d8bef9SDimitry Andricforeach nodes = [[seteq, EQ_F64x2], [setne, NE_F64x2], [setlt, LT_F64x2], 797e8d8bef9SDimitry Andric [setgt, GT_F64x2], [setle, LE_F64x2], [setge, GE_F64x2]] in 7980b57cec5SDimitry Andricdef : Pat<(v2i64 (nodes[0] (v2f64 V128:$lhs), (v2f64 V128:$rhs))), 799e8d8bef9SDimitry Andric (nodes[1] $lhs, $rhs)>; 800e8d8bef9SDimitry Andric 8010b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 8020b57cec5SDimitry Andric// Bitwise operations 8030b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 8040b57cec5SDimitry Andric 805*0fca6ea1SDimitry Andricmulticlass SIMDBinary<Vec vec, SDPatternOperator node, string name, 806*0fca6ea1SDimitry Andric bits<32> simdop, list<Predicate> reqs = []> { 807e8d8bef9SDimitry Andric defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), 8080b57cec5SDimitry Andric (outs), (ins), 809e8d8bef9SDimitry Andric [(set (vec.vt V128:$dst), 810e8d8bef9SDimitry Andric (node (vec.vt V128:$lhs), (vec.vt V128:$rhs)))], 811e8d8bef9SDimitry Andric vec.prefix#"."#name#"\t$dst, $lhs, $rhs", 812*0fca6ea1SDimitry Andric vec.prefix#"."#name, simdop, reqs>; 813*0fca6ea1SDimitry Andric} 814*0fca6ea1SDimitry Andric 815*0fca6ea1SDimitry Andricmulticlass HalfPrecisionBinary<Vec vec, SDPatternOperator node, string name, 816*0fca6ea1SDimitry Andric bits<32> simdop> { 817*0fca6ea1SDimitry Andric defm "" : SIMDBinary<vec, node, name, simdop, [HasHalfPrecision]>; 8180b57cec5SDimitry Andric} 8190b57cec5SDimitry Andric 820fe6060f1SDimitry Andricmulticlass SIMDBitwise<SDPatternOperator node, string name, bits<32> simdop, 821fe6060f1SDimitry Andric bit commutable = false> { 822e8d8bef9SDimitry Andric let isCommutable = commutable in 823e8d8bef9SDimitry Andric defm "" : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), 824e8d8bef9SDimitry Andric (outs), (ins), [], 825e8d8bef9SDimitry Andric "v128."#name#"\t$dst, $lhs, $rhs", "v128."#name, simdop>; 826e8d8bef9SDimitry Andric foreach vec = IntVecs in 827e8d8bef9SDimitry Andric def : Pat<(node (vec.vt V128:$lhs), (vec.vt V128:$rhs)), 828e8d8bef9SDimitry Andric (!cast<NI>(NAME) $lhs, $rhs)>; 8290b57cec5SDimitry Andric} 8300b57cec5SDimitry Andric 831*0fca6ea1SDimitry Andricmulticlass SIMDUnary<Vec vec, SDPatternOperator node, string name, 832*0fca6ea1SDimitry Andric bits<32> simdop, list<Predicate> reqs = []> { 833e8d8bef9SDimitry Andric defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins), 834e8d8bef9SDimitry Andric [(set (vec.vt V128:$dst), 835e8d8bef9SDimitry Andric (vec.vt (node (vec.vt V128:$v))))], 836e8d8bef9SDimitry Andric vec.prefix#"."#name#"\t$dst, $v", 837*0fca6ea1SDimitry Andric vec.prefix#"."#name, simdop, reqs>; 838*0fca6ea1SDimitry Andric} 839*0fca6ea1SDimitry Andric 840*0fca6ea1SDimitry Andricmulticlass HalfPrecisionUnary<Vec vec, SDPatternOperator node, string name, 841*0fca6ea1SDimitry Andric bits<32> simdop> { 842*0fca6ea1SDimitry Andric defm "" : SIMDUnary<vec, node, name, simdop, [HasHalfPrecision]>; 8430b57cec5SDimitry Andric} 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric// Bitwise logic: v128.not 846e8d8bef9SDimitry Andricdefm NOT : SIMD_I<(outs V128:$dst), (ins V128:$v), (outs), (ins), [], 847e8d8bef9SDimitry Andric "v128.not\t$dst, $v", "v128.not", 77>; 848e8d8bef9SDimitry Andricforeach vec = IntVecs in 849e8d8bef9SDimitry Andricdef : Pat<(vnot (vec.vt V128:$v)), (NOT $v)>; 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric// Bitwise logic: v128.and / v128.or / v128.xor 852e8d8bef9SDimitry Andricdefm AND : SIMDBitwise<and, "and", 78, true>; 853e8d8bef9SDimitry Andricdefm OR : SIMDBitwise<or, "or", 80, true>; 854e8d8bef9SDimitry Andricdefm XOR : SIMDBitwise<xor, "xor", 81, true>; 8550b57cec5SDimitry Andric 8568bcb0991SDimitry Andric// Bitwise logic: v128.andnot 8578bcb0991SDimitry Andricdef andnot : PatFrag<(ops node:$left, node:$right), (and $left, (vnot $right))>; 8585ffd83dbSDimitry Andricdefm ANDNOT : SIMDBitwise<andnot, "andnot", 79>; 8598bcb0991SDimitry Andric 8600b57cec5SDimitry Andric// Bitwise select: v128.bitselect 861e8d8bef9SDimitry Andricdefm BITSELECT : 862e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$v1, V128:$v2, V128:$c), (outs), (ins), [], 8635ffd83dbSDimitry Andric "v128.bitselect\t$dst, $v1, $v2, $c", "v128.bitselect", 82>; 8640b57cec5SDimitry Andric 865e8d8bef9SDimitry Andricforeach vec = AllVecs in 866e8d8bef9SDimitry Andricdef : Pat<(vec.vt (int_wasm_bitselect 867e8d8bef9SDimitry Andric (vec.vt V128:$v1), (vec.vt V128:$v2), (vec.vt V128:$c))), 868e8d8bef9SDimitry Andric (BITSELECT $v1, $v2, $c)>; 869e8d8bef9SDimitry Andric 8700b57cec5SDimitry Andric// Bitselect is equivalent to (c & v1) | (~c & v2) 871e8d8bef9SDimitry Andricforeach vec = IntVecs in 872e8d8bef9SDimitry Andricdef : Pat<(vec.vt (or (and (vec.vt V128:$c), (vec.vt V128:$v1)), 873e8d8bef9SDimitry Andric (and (vnot V128:$c), (vec.vt V128:$v2)))), 874e8d8bef9SDimitry Andric (BITSELECT $v1, $v2, $c)>; 875e8d8bef9SDimitry Andric 876bdd1243dSDimitry Andric// Bitselect is also equivalent to ((v1 ^ v2) & c) ^ v2 877bdd1243dSDimitry Andricforeach vec = IntVecs in 878bdd1243dSDimitry Andricdef : Pat<(vec.vt (xor (and (xor (vec.vt V128:$v1), (vec.vt V128:$v2)), 879bdd1243dSDimitry Andric (vec.vt V128:$c)), 880bdd1243dSDimitry Andric (vec.vt V128:$v2))), 881bdd1243dSDimitry Andric (BITSELECT $v1, $v2, $c)>; 882bdd1243dSDimitry Andric 883bdd1243dSDimitry Andric// Same pattern with `c` negated so `a` and `b` get swapped. 884bdd1243dSDimitry Andricforeach vec = IntVecs in 885bdd1243dSDimitry Andricdef : Pat<(vec.vt (xor (and (xor (vec.vt V128:$v1), (vec.vt V128:$v2)), 886bdd1243dSDimitry Andric (vnot (vec.vt V128:$c))), 887bdd1243dSDimitry Andric (vec.vt V128:$v2))), 888bdd1243dSDimitry Andric (BITSELECT $v2, $v1, $c)>; 889bdd1243dSDimitry Andric 890e8d8bef9SDimitry Andric// Also implement vselect in terms of bitselect 891e8d8bef9SDimitry Andricforeach vec = AllVecs in 892e8d8bef9SDimitry Andricdef : Pat<(vec.vt (vselect 893e8d8bef9SDimitry Andric (vec.int_vt V128:$c), (vec.vt V128:$v1), (vec.vt V128:$v2))), 894e8d8bef9SDimitry Andric (BITSELECT $v1, $v2, $c)>; 895e8d8bef9SDimitry Andric 896e8d8bef9SDimitry Andric// MVP select on v128 values 897e8d8bef9SDimitry Andricdefm SELECT_V128 : 898e8d8bef9SDimitry Andric I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, I32:$cond), (outs), (ins), [], 899e8d8bef9SDimitry Andric "v128.select\t$dst, $lhs, $rhs, $cond", "v128.select", 0x1b>; 900e8d8bef9SDimitry Andric 901e8d8bef9SDimitry Andricforeach vec = AllVecs in { 902e8d8bef9SDimitry Andricdef : Pat<(select I32:$cond, (vec.vt V128:$lhs), (vec.vt V128:$rhs)), 903e8d8bef9SDimitry Andric (SELECT_V128 $lhs, $rhs, $cond)>; 904e8d8bef9SDimitry Andric 905e8d8bef9SDimitry Andric// ISD::SELECT requires its operand to conform to getBooleanContents, but 906e8d8bef9SDimitry Andric// WebAssembly's select interprets any non-zero value as true, so we can fold 907e8d8bef9SDimitry Andric// a setne with 0 into a select. 908e8d8bef9SDimitry Andricdef : Pat<(select 909e8d8bef9SDimitry Andric (i32 (setne I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)), 910e8d8bef9SDimitry Andric (SELECT_V128 $lhs, $rhs, $cond)>; 911e8d8bef9SDimitry Andric 912e8d8bef9SDimitry Andric// And again, this time with seteq instead of setne and the arms reversed. 913e8d8bef9SDimitry Andricdef : Pat<(select 914e8d8bef9SDimitry Andric (i32 (seteq I32:$cond, 0)), (vec.vt V128:$lhs), (vec.vt V128:$rhs)), 915e8d8bef9SDimitry Andric (SELECT_V128 $rhs, $lhs, $cond)>; 916e8d8bef9SDimitry Andric} // foreach vec 917e8d8bef9SDimitry Andric 9180b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 9190b57cec5SDimitry Andric// Integer unary arithmetic 9200b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 9210b57cec5SDimitry Andric 922fe6060f1SDimitry Andricmulticlass SIMDUnaryInt<SDPatternOperator node, string name, bits<32> baseInst> { 923e8d8bef9SDimitry Andric defm "" : SIMDUnary<I8x16, node, name, baseInst>; 924e8d8bef9SDimitry Andric defm "" : SIMDUnary<I16x8, node, name, !add(baseInst, 32)>; 925e8d8bef9SDimitry Andric defm "" : SIMDUnary<I32x4, node, name, !add(baseInst, 64)>; 926e8d8bef9SDimitry Andric defm "" : SIMDUnary<I64x2, node, name, !add(baseInst, 96)>; 9270b57cec5SDimitry Andric} 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric// Integer vector negation 930e8d8bef9SDimitry Andricdef ivneg : PatFrag<(ops node:$in), (sub immAllZerosV, $in)>; 9310b57cec5SDimitry Andric 9325ffd83dbSDimitry Andric// Integer absolute value: abs 9335ffd83dbSDimitry Andricdefm ABS : SIMDUnaryInt<abs, "abs", 96>; 9345ffd83dbSDimitry Andric 9350b57cec5SDimitry Andric// Integer negation: neg 9365ffd83dbSDimitry Andricdefm NEG : SIMDUnaryInt<ivneg, "neg", 97>; 9370b57cec5SDimitry Andric 938fe6060f1SDimitry Andric// Population count: popcnt 939fe6060f1SDimitry Andricdefm POPCNT : SIMDUnary<I8x16, ctpop, "popcnt", 0x62>; 940fe6060f1SDimitry Andric 9410b57cec5SDimitry Andric// Any lane true: any_true 942fe6060f1SDimitry Andricdefm ANYTRUE : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins), [], 943fe6060f1SDimitry Andric "v128.any_true\t$dst, $vec", "v128.any_true", 0x53>; 944fe6060f1SDimitry Andric 945fe6060f1SDimitry Andricforeach vec = IntVecs in 946fe6060f1SDimitry Andricdef : Pat<(int_wasm_anytrue (vec.vt V128:$vec)), (ANYTRUE V128:$vec)>; 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric// All lanes true: all_true 949fe6060f1SDimitry Andricmulticlass SIMDAllTrue<Vec vec, bits<32> simdop> { 950fe6060f1SDimitry Andric defm ALLTRUE_#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins), 951fe6060f1SDimitry Andric [(set I32:$dst, 952fe6060f1SDimitry Andric (i32 (int_wasm_alltrue (vec.vt V128:$vec))))], 953fe6060f1SDimitry Andric vec.prefix#".all_true\t$dst, $vec", 954fe6060f1SDimitry Andric vec.prefix#".all_true", simdop>; 955fe6060f1SDimitry Andric} 9560b57cec5SDimitry Andric 957fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I8x16, 0x63>; 958fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I16x8, 0x83>; 959fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I32x4, 0xa3>; 960fe6060f1SDimitry Andricdefm "" : SIMDAllTrue<I64x2, 0xc3>; 961e8d8bef9SDimitry Andric 9620b57cec5SDimitry Andric// Reductions already return 0 or 1, so and 1, setne 0, and seteq 1 9630b57cec5SDimitry Andric// can be folded out 9640b57cec5SDimitry Andricforeach reduction = 965fe6060f1SDimitry Andric [["int_wasm_anytrue", "ANYTRUE", "I8x16"], 966fe6060f1SDimitry Andric ["int_wasm_anytrue", "ANYTRUE", "I16x8"], 967fe6060f1SDimitry Andric ["int_wasm_anytrue", "ANYTRUE", "I32x4"], 968fe6060f1SDimitry Andric ["int_wasm_anytrue", "ANYTRUE", "I64x2"], 969fe6060f1SDimitry Andric ["int_wasm_alltrue", "ALLTRUE_I8x16", "I8x16"], 970fe6060f1SDimitry Andric ["int_wasm_alltrue", "ALLTRUE_I16x8", "I16x8"], 971fe6060f1SDimitry Andric ["int_wasm_alltrue", "ALLTRUE_I32x4", "I32x4"], 972fe6060f1SDimitry Andric ["int_wasm_alltrue", "ALLTRUE_I64x2", "I64x2"]] in { 973e8d8bef9SDimitry Andricdefvar intrinsic = !cast<Intrinsic>(reduction[0]); 974fe6060f1SDimitry Andricdefvar inst = !cast<NI>(reduction[1]); 975fe6060f1SDimitry Andricdefvar vec = !cast<Vec>(reduction[2]); 976e8d8bef9SDimitry Andricdef : Pat<(i32 (and (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>; 977e8d8bef9SDimitry Andricdef : Pat<(i32 (setne (i32 (intrinsic (vec.vt V128:$x))), (i32 0))), (inst $x)>; 978e8d8bef9SDimitry Andricdef : Pat<(i32 (seteq (i32 (intrinsic (vec.vt V128:$x))), (i32 1))), (inst $x)>; 9790b57cec5SDimitry Andric} 9800b57cec5SDimitry Andric 981e8d8bef9SDimitry Andricmulticlass SIMDBitmask<Vec vec, bits<32> simdop> { 982e8d8bef9SDimitry Andric defm _#vec : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins), 9835ffd83dbSDimitry Andric [(set I32:$dst, 984e8d8bef9SDimitry Andric (i32 (int_wasm_bitmask (vec.vt V128:$vec))))], 985e8d8bef9SDimitry Andric vec.prefix#".bitmask\t$dst, $vec", vec.prefix#".bitmask", 986e8d8bef9SDimitry Andric simdop>; 9875ffd83dbSDimitry Andric} 9885ffd83dbSDimitry Andric 989e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I8x16, 100>; 990e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I16x8, 132>; 991e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I32x4, 164>; 992e8d8bef9SDimitry Andricdefm BITMASK : SIMDBitmask<I64x2, 196>; 9935ffd83dbSDimitry Andric 9940b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 9950b57cec5SDimitry Andric// Bit shifts 9960b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 9970b57cec5SDimitry Andric 998e8d8bef9SDimitry Andricmulticlass SIMDShift<Vec vec, SDNode node, string name, bits<32> simdop> { 999e8d8bef9SDimitry Andric defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$vec, I32:$x), (outs), (ins), 1000e8d8bef9SDimitry Andric [(set (vec.vt V128:$dst), (node V128:$vec, I32:$x))], 1001e8d8bef9SDimitry Andric vec.prefix#"."#name#"\t$dst, $vec, $x", 1002e8d8bef9SDimitry Andric vec.prefix#"."#name, simdop>; 10030b57cec5SDimitry Andric} 10040b57cec5SDimitry Andric 10050b57cec5SDimitry Andricmulticlass SIMDShiftInt<SDNode node, string name, bits<32> baseInst> { 1006e8d8bef9SDimitry Andric defm "" : SIMDShift<I8x16, node, name, baseInst>; 1007e8d8bef9SDimitry Andric defm "" : SIMDShift<I16x8, node, name, !add(baseInst, 32)>; 1008e8d8bef9SDimitry Andric defm "" : SIMDShift<I32x4, node, name, !add(baseInst, 64)>; 1009e8d8bef9SDimitry Andric defm "" : SIMDShift<I64x2, node, name, !add(baseInst, 96)>; 10100b57cec5SDimitry Andric} 10110b57cec5SDimitry Andric 10125ffd83dbSDimitry Andric// WebAssembly SIMD shifts are nonstandard in that the shift amount is 10135ffd83dbSDimitry Andric// an i32 rather than a vector, so they need custom nodes. 1014e8d8bef9SDimitry Andricdef wasm_shift_t : 1015e8d8bef9SDimitry Andric SDTypeProfile<1, 2, [SDTCisVec<0>, SDTCisSameAs<0, 1>, SDTCisVT<2, i32>]>; 10160b57cec5SDimitry Andricdef wasm_shl : SDNode<"WebAssemblyISD::VEC_SHL", wasm_shift_t>; 10170b57cec5SDimitry Andricdef wasm_shr_s : SDNode<"WebAssemblyISD::VEC_SHR_S", wasm_shift_t>; 10180b57cec5SDimitry Andricdef wasm_shr_u : SDNode<"WebAssemblyISD::VEC_SHR_U", wasm_shift_t>; 10195ffd83dbSDimitry Andric 10205ffd83dbSDimitry Andric// Left shift by scalar: shl 10215ffd83dbSDimitry Andricdefm SHL : SIMDShiftInt<wasm_shl, "shl", 107>; 10225ffd83dbSDimitry Andric 10235ffd83dbSDimitry Andric// Right shift by scalar: shr_s / shr_u 10245ffd83dbSDimitry Andricdefm SHR_S : SIMDShiftInt<wasm_shr_s, "shr_s", 108>; 10255ffd83dbSDimitry Andricdefm SHR_U : SIMDShiftInt<wasm_shr_u, "shr_u", 109>; 10260b57cec5SDimitry Andric 1027fe6060f1SDimitry Andric// Optimize away an explicit mask on a shift count. 1028fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v16i8 V128:$lhs), (and I32:$rhs, 7)), 1029fe6060f1SDimitry Andric (SHL_I8x16 V128:$lhs, I32:$rhs)>; 1030fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v16i8 V128:$lhs), (and I32:$rhs, 7)), 1031fe6060f1SDimitry Andric (SHR_S_I8x16 V128:$lhs, I32:$rhs)>; 1032fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v16i8 V128:$lhs), (and I32:$rhs, 7)), 1033fe6060f1SDimitry Andric (SHR_U_I8x16 V128:$lhs, I32:$rhs)>; 1034fe6060f1SDimitry Andric 1035fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v8i16 V128:$lhs), (and I32:$rhs, 15)), 1036fe6060f1SDimitry Andric (SHL_I16x8 V128:$lhs, I32:$rhs)>; 1037fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v8i16 V128:$lhs), (and I32:$rhs, 15)), 1038fe6060f1SDimitry Andric (SHR_S_I16x8 V128:$lhs, I32:$rhs)>; 1039fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v8i16 V128:$lhs), (and I32:$rhs, 15)), 1040fe6060f1SDimitry Andric (SHR_U_I16x8 V128:$lhs, I32:$rhs)>; 1041fe6060f1SDimitry Andric 1042fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v4i32 V128:$lhs), (and I32:$rhs, 31)), 1043fe6060f1SDimitry Andric (SHL_I32x4 V128:$lhs, I32:$rhs)>; 1044fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v4i32 V128:$lhs), (and I32:$rhs, 31)), 1045fe6060f1SDimitry Andric (SHR_S_I32x4 V128:$lhs, I32:$rhs)>; 1046fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v4i32 V128:$lhs), (and I32:$rhs, 31)), 1047fe6060f1SDimitry Andric (SHR_U_I32x4 V128:$lhs, I32:$rhs)>; 1048fe6060f1SDimitry Andric 104906c3fb27SDimitry Andricdef : Pat<(wasm_shl (v2i64 V128:$lhs), (and I32:$rhs, 63)), 105006c3fb27SDimitry Andric (SHL_I64x2 V128:$lhs, I32:$rhs)>; 105106c3fb27SDimitry Andricdef : Pat<(wasm_shr_s (v2i64 V128:$lhs), (and I32:$rhs, 63)), 105206c3fb27SDimitry Andric (SHR_S_I64x2 V128:$lhs, I32:$rhs)>; 105306c3fb27SDimitry Andricdef : Pat<(wasm_shr_u (v2i64 V128:$lhs), (and I32:$rhs, 63)), 105406c3fb27SDimitry Andric (SHR_U_I64x2 V128:$lhs, I32:$rhs)>; 1055fe6060f1SDimitry Andricdef : Pat<(wasm_shl (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))), 1056fe6060f1SDimitry Andric (SHL_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>; 1057fe6060f1SDimitry Andricdef : Pat<(wasm_shr_s (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))), 1058fe6060f1SDimitry Andric (SHR_S_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>; 1059fe6060f1SDimitry Andricdef : Pat<(wasm_shr_u (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))), 1060fe6060f1SDimitry Andric (SHR_U_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>; 1061fe6060f1SDimitry Andric 10620b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 10630b57cec5SDimitry Andric// Integer binary arithmetic 10640b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 10650b57cec5SDimitry Andric 1066fe6060f1SDimitry Andricmulticlass SIMDBinaryIntNoI8x16<SDPatternOperator node, string name, bits<32> baseInst> { 1067e8d8bef9SDimitry Andric defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>; 1068e8d8bef9SDimitry Andric defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>; 1069e8d8bef9SDimitry Andric defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>; 10705ffd83dbSDimitry Andric} 10715ffd83dbSDimitry Andric 1072fe6060f1SDimitry Andricmulticlass SIMDBinaryIntSmall<SDPatternOperator node, string name, bits<32> baseInst> { 1073e8d8bef9SDimitry Andric defm "" : SIMDBinary<I8x16, node, name, baseInst>; 1074e8d8bef9SDimitry Andric defm "" : SIMDBinary<I16x8, node, name, !add(baseInst, 32)>; 10750b57cec5SDimitry Andric} 10760b57cec5SDimitry Andric 1077fe6060f1SDimitry Andricmulticlass SIMDBinaryIntNoI64x2<SDPatternOperator node, string name, bits<32> baseInst> { 10780b57cec5SDimitry Andric defm "" : SIMDBinaryIntSmall<node, name, baseInst>; 1079e8d8bef9SDimitry Andric defm "" : SIMDBinary<I32x4, node, name, !add(baseInst, 64)>; 10800b57cec5SDimitry Andric} 10810b57cec5SDimitry Andric 1082fe6060f1SDimitry Andricmulticlass SIMDBinaryInt<SDPatternOperator node, string name, bits<32> baseInst> { 10830b57cec5SDimitry Andric defm "" : SIMDBinaryIntNoI64x2<node, name, baseInst>; 1084e8d8bef9SDimitry Andric defm "" : SIMDBinary<I64x2, node, name, !add(baseInst, 96)>; 10850b57cec5SDimitry Andric} 10860b57cec5SDimitry Andric 1087fe6060f1SDimitry Andric// Integer addition: add / add_sat_s / add_sat_u 10880b57cec5SDimitry Andriclet isCommutable = 1 in { 10895ffd83dbSDimitry Andricdefm ADD : SIMDBinaryInt<add, "add", 110>; 1090fe6060f1SDimitry Andricdefm ADD_SAT_S : SIMDBinaryIntSmall<saddsat, "add_sat_s", 111>; 1091fe6060f1SDimitry Andricdefm ADD_SAT_U : SIMDBinaryIntSmall<uaddsat, "add_sat_u", 112>; 10920b57cec5SDimitry Andric} // isCommutable = 1 10930b57cec5SDimitry Andric 1094fe6060f1SDimitry Andric// Integer subtraction: sub / sub_sat_s / sub_sat_u 10955ffd83dbSDimitry Andricdefm SUB : SIMDBinaryInt<sub, "sub", 113>; 10960b57cec5SDimitry Andricdefm SUB_SAT_S : 1097fe6060f1SDimitry Andric SIMDBinaryIntSmall<int_wasm_sub_sat_signed, "sub_sat_s", 114>; 10980b57cec5SDimitry Andricdefm SUB_SAT_U : 1099fe6060f1SDimitry Andric SIMDBinaryIntSmall<int_wasm_sub_sat_unsigned, "sub_sat_u", 115>; 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric// Integer multiplication: mul 1102480093f4SDimitry Andriclet isCommutable = 1 in 11035ffd83dbSDimitry Andricdefm MUL : SIMDBinaryIntNoI8x16<mul, "mul", 117>; 11040b57cec5SDimitry Andric 1105480093f4SDimitry Andric// Integer min_s / min_u / max_s / max_u 1106480093f4SDimitry Andriclet isCommutable = 1 in { 11075ffd83dbSDimitry Andricdefm MIN_S : SIMDBinaryIntNoI64x2<smin, "min_s", 118>; 11085ffd83dbSDimitry Andricdefm MIN_U : SIMDBinaryIntNoI64x2<umin, "min_u", 119>; 11095ffd83dbSDimitry Andricdefm MAX_S : SIMDBinaryIntNoI64x2<smax, "max_s", 120>; 11105ffd83dbSDimitry Andricdefm MAX_U : SIMDBinaryIntNoI64x2<umax, "max_u", 121>; 1111480093f4SDimitry Andric} // isCommutable = 1 1112480093f4SDimitry Andric 1113480093f4SDimitry Andric// Integer unsigned rounding average: avgr_u 11145ffd83dbSDimitry Andriclet isCommutable = 1 in { 1115e8d8bef9SDimitry Andricdefm AVGR_U : SIMDBinary<I8x16, int_wasm_avgr_unsigned, "avgr_u", 123>; 1116e8d8bef9SDimitry Andricdefm AVGR_U : SIMDBinary<I16x8, int_wasm_avgr_unsigned, "avgr_u", 155>; 1117480093f4SDimitry Andric} 1118480093f4SDimitry Andric 1119e8d8bef9SDimitry Andricdef add_nuw : PatFrag<(ops node:$lhs, node:$rhs), (add $lhs, $rhs), 1120480093f4SDimitry Andric "return N->getFlags().hasNoUnsignedWrap();">; 1121480093f4SDimitry Andric 1122e8d8bef9SDimitry Andricforeach vec = [I8x16, I16x8] in { 1123e8d8bef9SDimitry Andricdefvar inst = !cast<NI>("AVGR_U_"#vec); 11245ffd83dbSDimitry Andricdef : Pat<(wasm_shr_u 1125480093f4SDimitry Andric (add_nuw 1126e8d8bef9SDimitry Andric (add_nuw (vec.vt V128:$lhs), (vec.vt V128:$rhs)), 1127e8d8bef9SDimitry Andric (vec.splat (i32 1))), 1128e8d8bef9SDimitry Andric (i32 1)), 1129e8d8bef9SDimitry Andric (inst $lhs, $rhs)>; 1130e8d8bef9SDimitry Andric} 1131480093f4SDimitry Andric 1132480093f4SDimitry Andric// Widening dot product: i32x4.dot_i16x8_s 1133480093f4SDimitry Andriclet isCommutable = 1 in 1134480093f4SDimitry Andricdefm DOT : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins), 1135480093f4SDimitry Andric [(set V128:$dst, (int_wasm_dot V128:$lhs, V128:$rhs))], 1136480093f4SDimitry Andric "i32x4.dot_i16x8_s\t$dst, $lhs, $rhs", "i32x4.dot_i16x8_s", 1137e8d8bef9SDimitry Andric 186>; 1138e8d8bef9SDimitry Andric 1139e8d8bef9SDimitry Andric// Extending multiplication: extmul_{low,high}_P, extmul_high 1140fe6060f1SDimitry Andricdef extend_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>; 1141fe6060f1SDimitry Andricdef extend_low_s : SDNode<"WebAssemblyISD::EXTEND_LOW_S", extend_t>; 1142fe6060f1SDimitry Andricdef extend_high_s : SDNode<"WebAssemblyISD::EXTEND_HIGH_S", extend_t>; 1143fe6060f1SDimitry Andricdef extend_low_u : SDNode<"WebAssemblyISD::EXTEND_LOW_U", extend_t>; 1144fe6060f1SDimitry Andricdef extend_high_u : SDNode<"WebAssemblyISD::EXTEND_HIGH_U", extend_t>; 1145fe6060f1SDimitry Andric 1146fe6060f1SDimitry Andricmulticlass SIMDExtBinary<Vec vec, SDPatternOperator node, string name, 1147fe6060f1SDimitry Andric bits<32> simdop> { 1148e8d8bef9SDimitry Andric defm _#vec : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), 1149e8d8bef9SDimitry Andric (outs), (ins), 1150e8d8bef9SDimitry Andric [(set (vec.vt V128:$dst), (node 1151e8d8bef9SDimitry Andric (vec.split.vt V128:$lhs),(vec.split.vt V128:$rhs)))], 1152e8d8bef9SDimitry Andric vec.prefix#"."#name#"\t$dst, $lhs, $rhs", 1153e8d8bef9SDimitry Andric vec.prefix#"."#name, simdop>; 1154e8d8bef9SDimitry Andric} 1155e8d8bef9SDimitry Andric 1156fe6060f1SDimitry Andricclass ExtMulPat<SDNode extend> : 1157fe6060f1SDimitry Andric PatFrag<(ops node:$lhs, node:$rhs), 1158fe6060f1SDimitry Andric (mul (extend $lhs), (extend $rhs))> {} 1159fe6060f1SDimitry Andric 1160fe6060f1SDimitry Andricdef extmul_low_s : ExtMulPat<extend_low_s>; 1161fe6060f1SDimitry Andricdef extmul_high_s : ExtMulPat<extend_high_s>; 1162fe6060f1SDimitry Andricdef extmul_low_u : ExtMulPat<extend_low_u>; 1163fe6060f1SDimitry Andricdef extmul_high_u : ExtMulPat<extend_high_u>; 1164e8d8bef9SDimitry Andric 1165e8d8bef9SDimitry Andricdefm EXTMUL_LOW_S : 1166fe6060f1SDimitry Andric SIMDExtBinary<I16x8, extmul_low_s, "extmul_low_i8x16_s", 0x9c>; 1167e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_S : 1168fe6060f1SDimitry Andric SIMDExtBinary<I16x8, extmul_high_s, "extmul_high_i8x16_s", 0x9d>; 1169e8d8bef9SDimitry Andricdefm EXTMUL_LOW_U : 1170fe6060f1SDimitry Andric SIMDExtBinary<I16x8, extmul_low_u, "extmul_low_i8x16_u", 0x9e>; 1171e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_U : 1172fe6060f1SDimitry Andric SIMDExtBinary<I16x8, extmul_high_u, "extmul_high_i8x16_u", 0x9f>; 1173e8d8bef9SDimitry Andric 1174e8d8bef9SDimitry Andricdefm EXTMUL_LOW_S : 1175fe6060f1SDimitry Andric SIMDExtBinary<I32x4, extmul_low_s, "extmul_low_i16x8_s", 0xbc>; 1176e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_S : 1177fe6060f1SDimitry Andric SIMDExtBinary<I32x4, extmul_high_s, "extmul_high_i16x8_s", 0xbd>; 1178e8d8bef9SDimitry Andricdefm EXTMUL_LOW_U : 1179fe6060f1SDimitry Andric SIMDExtBinary<I32x4, extmul_low_u, "extmul_low_i16x8_u", 0xbe>; 1180e8d8bef9SDimitry Andricdefm EXTMUL_HIGH_U : 1181fe6060f1SDimitry Andric SIMDExtBinary<I32x4, extmul_high_u, "extmul_high_i16x8_u", 0xbf>; 1182fe6060f1SDimitry Andric 1183fe6060f1SDimitry Andricdefm EXTMUL_LOW_S : 1184fe6060f1SDimitry Andric SIMDExtBinary<I64x2, extmul_low_s, "extmul_low_i32x4_s", 0xdc>; 1185fe6060f1SDimitry Andricdefm EXTMUL_HIGH_S : 1186fe6060f1SDimitry Andric SIMDExtBinary<I64x2, extmul_high_s, "extmul_high_i32x4_s", 0xdd>; 1187fe6060f1SDimitry Andricdefm EXTMUL_LOW_U : 1188fe6060f1SDimitry Andric SIMDExtBinary<I64x2, extmul_low_u, "extmul_low_i32x4_u", 0xde>; 1189fe6060f1SDimitry Andricdefm EXTMUL_HIGH_U : 1190fe6060f1SDimitry Andric SIMDExtBinary<I64x2, extmul_high_u, "extmul_high_i32x4_u", 0xdf>; 1191480093f4SDimitry Andric 11920b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 11930b57cec5SDimitry Andric// Floating-point unary arithmetic 11940b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andricmulticlass SIMDUnaryFP<SDNode node, string name, bits<32> baseInst> { 1197e8d8bef9SDimitry Andric defm "" : SIMDUnary<F32x4, node, name, baseInst>; 1198e8d8bef9SDimitry Andric defm "" : SIMDUnary<F64x2, node, name, !add(baseInst, 12)>; 1199*0fca6ea1SDimitry Andric // Unlike F32x4 and F64x2 there's not a gap in the opcodes between "neg" and 1200*0fca6ea1SDimitry Andric // "sqrt" so subtract one from the offset. 1201*0fca6ea1SDimitry Andric defm "" : HalfPrecisionUnary<F16x8, node, name, 1202*0fca6ea1SDimitry Andric !add(baseInst,!if(!eq(name, "sqrt"), 80, 81))>; 12030b57cec5SDimitry Andric} 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric// Absolute value: abs 12065ffd83dbSDimitry Andricdefm ABS : SIMDUnaryFP<fabs, "abs", 224>; 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric// Negation: neg 12095ffd83dbSDimitry Andricdefm NEG : SIMDUnaryFP<fneg, "neg", 225>; 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric// Square root: sqrt 12125ffd83dbSDimitry Andricdefm SQRT : SIMDUnaryFP<fsqrt, "sqrt", 227>; 12135ffd83dbSDimitry Andric 12145ffd83dbSDimitry Andric// Rounding: ceil, floor, trunc, nearest 1215fe6060f1SDimitry Andricdefm CEIL : SIMDUnary<F32x4, fceil, "ceil", 0x67>; 1216fe6060f1SDimitry Andricdefm FLOOR : SIMDUnary<F32x4, ffloor, "floor", 0x68>; 1217fe6060f1SDimitry Andricdefm TRUNC: SIMDUnary<F32x4, ftrunc, "trunc", 0x69>; 1218fe6060f1SDimitry Andricdefm NEAREST: SIMDUnary<F32x4, fnearbyint, "nearest", 0x6a>; 1219fe6060f1SDimitry Andricdefm CEIL : SIMDUnary<F64x2, fceil, "ceil", 0x74>; 1220fe6060f1SDimitry Andricdefm FLOOR : SIMDUnary<F64x2, ffloor, "floor", 0x75>; 1221fe6060f1SDimitry Andricdefm TRUNC: SIMDUnary<F64x2, ftrunc, "trunc", 0x7a>; 1222fe6060f1SDimitry Andricdefm NEAREST: SIMDUnary<F64x2, fnearbyint, "nearest", 0x94>; 1223*0fca6ea1SDimitry Andricdefm CEIL : HalfPrecisionUnary<F16x8, fceil, "ceil", 0x13c>; 1224*0fca6ea1SDimitry Andricdefm FLOOR : HalfPrecisionUnary<F16x8, ffloor, "floor", 0x13d>; 1225*0fca6ea1SDimitry Andricdefm TRUNC : HalfPrecisionUnary<F16x8, ftrunc, "trunc", 0x13e>; 1226*0fca6ea1SDimitry Andricdefm NEAREST : HalfPrecisionUnary<F16x8, fnearbyint, "nearest", 0x13f>; 12270b57cec5SDimitry Andric 122806c3fb27SDimitry Andric// WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint. 122906c3fb27SDimitry Andricdef : Pat<(v4f32 (frint (v4f32 V128:$src))), (NEAREST_F32x4 V128:$src)>; 123006c3fb27SDimitry Andricdef : Pat<(v2f64 (frint (v2f64 V128:$src))), (NEAREST_F64x2 V128:$src)>; 1231*0fca6ea1SDimitry Andricdef : Pat<(v8f16 (frint (v8f16 V128:$src))), (NEAREST_F16x8 V128:$src)>; 123206c3fb27SDimitry Andric 123306c3fb27SDimitry Andric// WebAssembly always rounds ties-to-even, so map froundeven to fnearbyint. 123406c3fb27SDimitry Andricdef : Pat<(v4f32 (froundeven (v4f32 V128:$src))), (NEAREST_F32x4 V128:$src)>; 123506c3fb27SDimitry Andricdef : Pat<(v2f64 (froundeven (v2f64 V128:$src))), (NEAREST_F64x2 V128:$src)>; 1236*0fca6ea1SDimitry Andricdef : Pat<(v8f16 (froundeven (v8f16 V128:$src))), (NEAREST_F16x8 V128:$src)>; 123706c3fb27SDimitry Andric 12380b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 12390b57cec5SDimitry Andric// Floating-point binary arithmetic 12400b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 12410b57cec5SDimitry Andric 1242fe6060f1SDimitry Andricmulticlass SIMDBinaryFP<SDPatternOperator node, string name, bits<32> baseInst> { 1243e8d8bef9SDimitry Andric defm "" : SIMDBinary<F32x4, node, name, baseInst>; 1244e8d8bef9SDimitry Andric defm "" : SIMDBinary<F64x2, node, name, !add(baseInst, 12)>; 1245*0fca6ea1SDimitry Andric defm "" : HalfPrecisionBinary<F16x8, node, name, !add(baseInst, 80)>; 12460b57cec5SDimitry Andric} 12470b57cec5SDimitry Andric 12480b57cec5SDimitry Andric// Addition: add 12490b57cec5SDimitry Andriclet isCommutable = 1 in 12505ffd83dbSDimitry Andricdefm ADD : SIMDBinaryFP<fadd, "add", 228>; 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric// Subtraction: sub 12535ffd83dbSDimitry Andricdefm SUB : SIMDBinaryFP<fsub, "sub", 229>; 12540b57cec5SDimitry Andric 12550b57cec5SDimitry Andric// Multiplication: mul 12560b57cec5SDimitry Andriclet isCommutable = 1 in 12575ffd83dbSDimitry Andricdefm MUL : SIMDBinaryFP<fmul, "mul", 230>; 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric// Division: div 12605ffd83dbSDimitry Andricdefm DIV : SIMDBinaryFP<fdiv, "div", 231>; 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric// NaN-propagating minimum: min 12635ffd83dbSDimitry Andricdefm MIN : SIMDBinaryFP<fminimum, "min", 232>; 12640b57cec5SDimitry Andric 12650b57cec5SDimitry Andric// NaN-propagating maximum: max 12665ffd83dbSDimitry Andricdefm MAX : SIMDBinaryFP<fmaximum, "max", 233>; 12675ffd83dbSDimitry Andric 12685ffd83dbSDimitry Andric// Pseudo-minimum: pmin 126906c3fb27SDimitry Andricdef pmin : PatFrags<(ops node:$lhs, node:$rhs), [ 127006c3fb27SDimitry Andric (vselect (setolt $rhs, $lhs), $rhs, $lhs), 127106c3fb27SDimitry Andric (vselect (setole $rhs, $lhs), $rhs, $lhs), 127206c3fb27SDimitry Andric (vselect (setogt $lhs, $rhs), $rhs, $lhs), 127306c3fb27SDimitry Andric (vselect (setoge $lhs, $rhs), $rhs, $lhs) 127406c3fb27SDimitry Andric]>; 1275fe6060f1SDimitry Andricdefm PMIN : SIMDBinaryFP<pmin, "pmin", 234>; 12765ffd83dbSDimitry Andric 12775ffd83dbSDimitry Andric// Pseudo-maximum: pmax 127806c3fb27SDimitry Andricdef pmax : PatFrags<(ops node:$lhs, node:$rhs), [ 127906c3fb27SDimitry Andric (vselect (setogt $rhs, $lhs), $rhs, $lhs), 128006c3fb27SDimitry Andric (vselect (setoge $rhs, $lhs), $rhs, $lhs), 128106c3fb27SDimitry Andric (vselect (setolt $lhs, $rhs), $rhs, $lhs), 128206c3fb27SDimitry Andric (vselect (setole $lhs, $rhs), $rhs, $lhs) 128306c3fb27SDimitry Andric]>; 1284fe6060f1SDimitry Andricdefm PMAX : SIMDBinaryFP<pmax, "pmax", 235>; 1285fe6060f1SDimitry Andric 1286fe6060f1SDimitry Andric// Also match the pmin/pmax cases where the operands are int vectors (but the 1287fe6060f1SDimitry Andric// comparison is still a floating point comparison). This can happen when using 1288fe6060f1SDimitry Andric// the wasm_simd128.h intrinsics because v128_t is an integer vector. 1289*0fca6ea1SDimitry Andricforeach vec = [F32x4, F64x2, F16x8] in { 1290fe6060f1SDimitry Andricdefvar pmin = !cast<NI>("PMIN_"#vec); 1291fe6060f1SDimitry Andricdefvar pmax = !cast<NI>("PMAX_"#vec); 1292fe6060f1SDimitry Andricdef : Pat<(vec.int_vt (vselect 1293fe6060f1SDimitry Andric (setolt (vec.vt (bitconvert V128:$rhs)), 1294fe6060f1SDimitry Andric (vec.vt (bitconvert V128:$lhs))), 1295fe6060f1SDimitry Andric V128:$rhs, V128:$lhs)), 1296fe6060f1SDimitry Andric (pmin $lhs, $rhs)>; 1297fe6060f1SDimitry Andricdef : Pat<(vec.int_vt (vselect 1298fe6060f1SDimitry Andric (setolt (vec.vt (bitconvert V128:$lhs)), 1299fe6060f1SDimitry Andric (vec.vt (bitconvert V128:$rhs))), 1300fe6060f1SDimitry Andric V128:$rhs, V128:$lhs)), 1301fe6060f1SDimitry Andric (pmax $lhs, $rhs)>; 1302fe6060f1SDimitry Andric} 13030b57cec5SDimitry Andric 1304349cc55cSDimitry Andric// And match the pmin/pmax LLVM intrinsics as well 1305349cc55cSDimitry Andricdef : Pat<(v4f32 (int_wasm_pmin (v4f32 V128:$lhs), (v4f32 V128:$rhs))), 1306349cc55cSDimitry Andric (PMIN_F32x4 V128:$lhs, V128:$rhs)>; 1307349cc55cSDimitry Andricdef : Pat<(v4f32 (int_wasm_pmax (v4f32 V128:$lhs), (v4f32 V128:$rhs))), 1308349cc55cSDimitry Andric (PMAX_F32x4 V128:$lhs, V128:$rhs)>; 1309349cc55cSDimitry Andricdef : Pat<(v2f64 (int_wasm_pmin (v2f64 V128:$lhs), (v2f64 V128:$rhs))), 1310349cc55cSDimitry Andric (PMIN_F64x2 V128:$lhs, V128:$rhs)>; 1311349cc55cSDimitry Andricdef : Pat<(v2f64 (int_wasm_pmax (v2f64 V128:$lhs), (v2f64 V128:$rhs))), 1312349cc55cSDimitry Andric (PMAX_F64x2 V128:$lhs, V128:$rhs)>; 1313*0fca6ea1SDimitry Andricdef : Pat<(v8f16 (int_wasm_pmin (v8f16 V128:$lhs), (v8f16 V128:$rhs))), 1314*0fca6ea1SDimitry Andric (PMIN_F16x8 V128:$lhs, V128:$rhs)>; 1315*0fca6ea1SDimitry Andricdef : Pat<(v8f16 (int_wasm_pmax (v8f16 V128:$lhs), (v8f16 V128:$rhs))), 1316*0fca6ea1SDimitry Andric (PMAX_F16x8 V128:$lhs, V128:$rhs)>; 1317349cc55cSDimitry Andric 13180b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 13190b57cec5SDimitry Andric// Conversions 13200b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 13210b57cec5SDimitry Andric 1322fe6060f1SDimitry Andricmulticlass SIMDConvert<Vec vec, Vec arg, SDPatternOperator op, string name, 1323*0fca6ea1SDimitry Andric bits<32> simdop, list<Predicate> reqs = []> { 1324e8d8bef9SDimitry Andric defm op#_#vec : 13250b57cec5SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins), 1326e8d8bef9SDimitry Andric [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))], 1327*0fca6ea1SDimitry Andric vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop, reqs>; 1328*0fca6ea1SDimitry Andric} 1329*0fca6ea1SDimitry Andric 1330*0fca6ea1SDimitry Andricmulticlass HalfPrecisionConvert<Vec vec, Vec arg, SDPatternOperator op, 1331*0fca6ea1SDimitry Andric string name, bits<32> simdop> { 1332*0fca6ea1SDimitry Andric defm "" : SIMDConvert<vec, arg, op, name, simdop, [HasHalfPrecision]>; 13330b57cec5SDimitry Andric} 13340b57cec5SDimitry Andric 13350b57cec5SDimitry Andric// Floating point to integer with saturation: trunc_sat 1336e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, F32x4, fp_to_sint, "trunc_sat_f32x4_s", 248>; 1337e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, F32x4, fp_to_uint, "trunc_sat_f32x4_u", 249>; 1338*0fca6ea1SDimitry Andricdefm "" : HalfPrecisionConvert<I16x8, F16x8, fp_to_sint, "trunc_sat_f16x8_s", 0x148>; 1339*0fca6ea1SDimitry Andricdefm "" : HalfPrecisionConvert<I16x8, F16x8, fp_to_uint, "trunc_sat_f16x8_u", 0x149>; 13405ffd83dbSDimitry Andric 1341fe6060f1SDimitry Andric// Support the saturating variety as well. 1342fe6060f1SDimitry Andricdef trunc_s_sat32 : PatFrag<(ops node:$x), (fp_to_sint_sat $x, i32)>; 1343fe6060f1SDimitry Andricdef trunc_u_sat32 : PatFrag<(ops node:$x), (fp_to_uint_sat $x, i32)>; 1344fe6060f1SDimitry Andricdef : Pat<(v4i32 (trunc_s_sat32 (v4f32 V128:$src))), (fp_to_sint_I32x4 $src)>; 1345fe6060f1SDimitry Andricdef : Pat<(v4i32 (trunc_u_sat32 (v4f32 V128:$src))), (fp_to_uint_I32x4 $src)>; 1346fe6060f1SDimitry Andric 1347fe6060f1SDimitry Andricdef trunc_sat_zero_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>; 1348fe6060f1SDimitry Andricdef trunc_sat_zero_s : 1349fe6060f1SDimitry Andric SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_S", trunc_sat_zero_t>; 1350fe6060f1SDimitry Andricdef trunc_sat_zero_u : 1351fe6060f1SDimitry Andric SDNode<"WebAssemblyISD::TRUNC_SAT_ZERO_U", trunc_sat_zero_t>; 135281ad6265SDimitry Andricdefm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_s, "trunc_sat_f64x2_s_zero", 1353fe6060f1SDimitry Andric 0xfc>; 135481ad6265SDimitry Andricdefm "" : SIMDConvert<I32x4, F64x2, trunc_sat_zero_u, "trunc_sat_f64x2_u_zero", 1355fe6060f1SDimitry Andric 0xfd>; 1356fe6060f1SDimitry Andric 13575ffd83dbSDimitry Andric// Integer to floating point: convert 1358fe6060f1SDimitry Andricdef convert_low_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>; 1359fe6060f1SDimitry Andricdef convert_low_s : SDNode<"WebAssemblyISD::CONVERT_LOW_S", convert_low_t>; 1360fe6060f1SDimitry Andricdef convert_low_u : SDNode<"WebAssemblyISD::CONVERT_LOW_U", convert_low_t>; 1361e8d8bef9SDimitry Andricdefm "" : SIMDConvert<F32x4, I32x4, sint_to_fp, "convert_i32x4_s", 250>; 1362e8d8bef9SDimitry Andricdefm "" : SIMDConvert<F32x4, I32x4, uint_to_fp, "convert_i32x4_u", 251>; 1363fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, I32x4, convert_low_s, "convert_low_i32x4_s", 0xfe>; 1364fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, I32x4, convert_low_u, "convert_low_i32x4_u", 0xff>; 1365*0fca6ea1SDimitry Andricdefm "" : HalfPrecisionConvert<F16x8, I16x8, sint_to_fp, "convert_i16x8_s", 0x14a>; 1366*0fca6ea1SDimitry Andricdefm "" : HalfPrecisionConvert<F16x8, I16x8, uint_to_fp, "convert_i16x8_u", 0x14b>; 13678bcb0991SDimitry Andric 1368fe6060f1SDimitry Andric// Extending operations 1369e8d8bef9SDimitry Andric// TODO: refactor this to be uniform for i64x2 if the numbering is not changed. 1370fe6060f1SDimitry Andricmulticlass SIMDExtend<Vec vec, bits<32> baseInst> { 1371fe6060f1SDimitry Andric defm "" : SIMDConvert<vec, vec.split, extend_low_s, 1372fe6060f1SDimitry Andric "extend_low_"#vec.split.prefix#"_s", baseInst>; 1373fe6060f1SDimitry Andric defm "" : SIMDConvert<vec, vec.split, extend_high_s, 1374fe6060f1SDimitry Andric "extend_high_"#vec.split.prefix#"_s", !add(baseInst, 1)>; 1375fe6060f1SDimitry Andric defm "" : SIMDConvert<vec, vec.split, extend_low_u, 1376fe6060f1SDimitry Andric "extend_low_"#vec.split.prefix#"_u", !add(baseInst, 2)>; 1377fe6060f1SDimitry Andric defm "" : SIMDConvert<vec, vec.split, extend_high_u, 1378fe6060f1SDimitry Andric "extend_high_"#vec.split.prefix#"_u", !add(baseInst, 3)>; 1379e8d8bef9SDimitry Andric} 1380e8d8bef9SDimitry Andric 1381fe6060f1SDimitry Andricdefm "" : SIMDExtend<I16x8, 0x87>; 1382fe6060f1SDimitry Andricdefm "" : SIMDExtend<I32x4, 0xa7>; 1383fe6060f1SDimitry Andricdefm "" : SIMDExtend<I64x2, 0xc7>; 1384e8d8bef9SDimitry Andric 1385e8d8bef9SDimitry Andric// Narrowing operations 1386e8d8bef9SDimitry Andricmulticlass SIMDNarrow<Vec vec, bits<32> baseInst> { 1387e8d8bef9SDimitry Andric defvar name = vec.split.prefix#".narrow_"#vec.prefix; 1388e8d8bef9SDimitry Andric defm NARROW_S_#vec.split : 1389e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins), 1390e8d8bef9SDimitry Andric [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_signed 1391e8d8bef9SDimitry Andric (vec.vt V128:$low), (vec.vt V128:$high))))], 1392e8d8bef9SDimitry Andric name#"_s\t$dst, $low, $high", name#"_s", baseInst>; 1393e8d8bef9SDimitry Andric defm NARROW_U_#vec.split : 1394e8d8bef9SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$low, V128:$high), (outs), (ins), 1395e8d8bef9SDimitry Andric [(set (vec.split.vt V128:$dst), (vec.split.vt (int_wasm_narrow_unsigned 1396e8d8bef9SDimitry Andric (vec.vt V128:$low), (vec.vt V128:$high))))], 1397e8d8bef9SDimitry Andric name#"_u\t$dst, $low, $high", name#"_u", !add(baseInst, 1)>; 1398e8d8bef9SDimitry Andric} 1399e8d8bef9SDimitry Andric 1400e8d8bef9SDimitry Andricdefm "" : SIMDNarrow<I16x8, 101>; 1401e8d8bef9SDimitry Andricdefm "" : SIMDNarrow<I32x4, 133>; 1402e8d8bef9SDimitry Andric 14030eae32dcSDimitry Andric// WebAssemblyISD::NARROW_U 14040eae32dcSDimitry Andricdef wasm_narrow_t : SDTypeProfile<1, 2, []>; 14050eae32dcSDimitry Andricdef wasm_narrow_u : SDNode<"WebAssemblyISD::NARROW_U", wasm_narrow_t>; 14060eae32dcSDimitry Andricdef : Pat<(v16i8 (wasm_narrow_u (v8i16 V128:$left), (v8i16 V128:$right))), 14070eae32dcSDimitry Andric (NARROW_U_I8x16 $left, $right)>; 14080eae32dcSDimitry Andricdef : Pat<(v8i16 (wasm_narrow_u (v4i32 V128:$left), (v4i32 V128:$right))), 14090eae32dcSDimitry Andric (NARROW_U_I16x8 $left, $right)>; 14100eae32dcSDimitry Andric 14110b57cec5SDimitry Andric// Bitcasts are nops 14120b57cec5SDimitry Andric// Matching bitcast t1 to t1 causes strange errors, so avoid repeating types 1413fe6060f1SDimitry Andricforeach t1 = AllVecs in 1414fe6060f1SDimitry Andricforeach t2 = AllVecs in 1415fe6060f1SDimitry Andricif !ne(t1, t2) then 1416fe6060f1SDimitry Andricdef : Pat<(t1.vt (bitconvert (t2.vt V128:$v))), (t1.vt V128:$v)>; 14178bcb0991SDimitry Andric 1418e8d8bef9SDimitry Andric// Extended pairwise addition 1419e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_signed, 1420fe6060f1SDimitry Andric "extadd_pairwise_i8x16_s", 0x7c>; 1421e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I16x8, I8x16, int_wasm_extadd_pairwise_unsigned, 1422fe6060f1SDimitry Andric "extadd_pairwise_i8x16_u", 0x7d>; 1423e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_signed, 1424fe6060f1SDimitry Andric "extadd_pairwise_i16x8_s", 0x7e>; 1425e8d8bef9SDimitry Andricdefm "" : SIMDConvert<I32x4, I16x8, int_wasm_extadd_pairwise_unsigned, 1426fe6060f1SDimitry Andric "extadd_pairwise_i16x8_u", 0x7f>; 1427e8d8bef9SDimitry Andric 1428fe6060f1SDimitry Andric// f64x2 <-> f32x4 conversions 1429fe6060f1SDimitry Andricdef demote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>; 1430fe6060f1SDimitry Andricdef demote_zero : SDNode<"WebAssemblyISD::DEMOTE_ZERO", demote_t>; 1431fe6060f1SDimitry Andricdefm "" : SIMDConvert<F32x4, F64x2, demote_zero, 143281ad6265SDimitry Andric "demote_f64x2_zero", 0x5e>; 1433e8d8bef9SDimitry Andric 1434fe6060f1SDimitry Andricdef promote_t : SDTypeProfile<1, 1, [SDTCisVec<0>, SDTCisVec<1>]>; 1435fe6060f1SDimitry Andricdef promote_low : SDNode<"WebAssemblyISD::PROMOTE_LOW", promote_t>; 1436fe6060f1SDimitry Andricdefm "" : SIMDConvert<F64x2, F32x4, promote_low, "promote_low_f32x4", 0x5f>; 1437e8d8bef9SDimitry Andric 1438349cc55cSDimitry Andric// Lower extending loads to load64_zero + promote_low 1439349cc55cSDimitry Andricdef extloadv2f32 : PatFrag<(ops node:$ptr), (extload node:$ptr)> { 1440349cc55cSDimitry Andric let MemoryVT = v2f32; 1441349cc55cSDimitry Andric} 1442349cc55cSDimitry Andric// Adapted from the body of LoadPatNoOffset 1443349cc55cSDimitry Andric// TODO: other addressing patterns 1444349cc55cSDimitry Andricdef : Pat<(v2f64 (extloadv2f32 (i32 I32:$addr))), 1445349cc55cSDimitry Andric (promote_low_F64x2 (LOAD_ZERO_I64x2_A32 0, 0, I32:$addr))>, 1446349cc55cSDimitry Andric Requires<[HasAddr32]>; 1447349cc55cSDimitry Andricdef : Pat<(v2f64 (extloadv2f32 (i64 I64:$addr))), 1448349cc55cSDimitry Andric (promote_low_F64x2 (LOAD_ZERO_I64x2_A64 0, 0, I64:$addr))>, 1449349cc55cSDimitry Andric Requires<[HasAddr64]>; 1450349cc55cSDimitry Andric 1451e8d8bef9SDimitry Andric//===----------------------------------------------------------------------===// 1452e8d8bef9SDimitry Andric// Saturating Rounding Q-Format Multiplication 1453e8d8bef9SDimitry Andric//===----------------------------------------------------------------------===// 1454e8d8bef9SDimitry Andric 1455e8d8bef9SDimitry Andricdefm Q15MULR_SAT_S : 1456fe6060f1SDimitry Andric SIMDBinary<I16x8, int_wasm_q15mulr_sat_signed, "q15mulr_sat_s", 0x82>; 1457349cc55cSDimitry Andric 1458349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 145981ad6265SDimitry Andric// Relaxed swizzle 146081ad6265SDimitry Andric//===----------------------------------------------------------------------===// 146181ad6265SDimitry Andric 146281ad6265SDimitry Andricdefm RELAXED_SWIZZLE : 146381ad6265SDimitry Andric RELAXED_I<(outs V128:$dst), (ins V128:$src, V128:$mask), (outs), (ins), 146481ad6265SDimitry Andric [(set (v16i8 V128:$dst), 146581ad6265SDimitry Andric (int_wasm_relaxed_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))], 146681ad6265SDimitry Andric "i8x16.relaxed_swizzle\t$dst, $src, $mask", "i8x16.relaxed_swizzle", 0x100>; 146781ad6265SDimitry Andric 146881ad6265SDimitry Andric//===----------------------------------------------------------------------===// 146981ad6265SDimitry Andric// Relaxed floating-point to int conversions 147081ad6265SDimitry Andric//===----------------------------------------------------------------------===// 147181ad6265SDimitry Andric 147281ad6265SDimitry Andricmulticlass RelaxedConvert<Vec vec, Vec arg, SDPatternOperator op, string name, bits<32> simdop> { 147381ad6265SDimitry Andric defm op#_#vec : 147481ad6265SDimitry Andric RELAXED_I<(outs V128:$dst), (ins V128:$vec), (outs), (ins), 147581ad6265SDimitry Andric [(set (vec.vt V128:$dst), (vec.vt (op (arg.vt V128:$vec))))], 147681ad6265SDimitry Andric vec.prefix#"."#name#"\t$dst, $vec", vec.prefix#"."#name, simdop>; 147781ad6265SDimitry Andric} 147881ad6265SDimitry Andric 147981ad6265SDimitry Andricdefm "" : RelaxedConvert<I32x4, F32x4, int_wasm_relaxed_trunc_signed, 148081ad6265SDimitry Andric "relaxed_trunc_f32x4_s", 0x101>; 148181ad6265SDimitry Andricdefm "" : RelaxedConvert<I32x4, F32x4, int_wasm_relaxed_trunc_unsigned, 148281ad6265SDimitry Andric "relaxed_trunc_f32x4_u", 0x102>; 148381ad6265SDimitry Andricdefm "" : RelaxedConvert<I32x4, F64x2, int_wasm_relaxed_trunc_signed_zero, 148481ad6265SDimitry Andric "relaxed_trunc_f64x2_s_zero", 0x103>; 148581ad6265SDimitry Andricdefm "" : RelaxedConvert<I32x4, F64x2, int_wasm_relaxed_trunc_unsigned_zero, 148681ad6265SDimitry Andric "relaxed_trunc_f64x2_u_zero", 0x104>; 148781ad6265SDimitry Andric 148881ad6265SDimitry Andric//===----------------------------------------------------------------------===// 1489bdd1243dSDimitry Andric// Relaxed (Negative) Multiply-Add (madd/nmadd) 1490349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 1491349cc55cSDimitry Andric 1492*0fca6ea1SDimitry Andricmulticlass SIMDMADD<Vec vec, bits<32> simdopA, bits<32> simdopS, list<Predicate> reqs> { 1493bdd1243dSDimitry Andric defm MADD_#vec : 1494*0fca6ea1SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins), 1495bdd1243dSDimitry Andric [(set (vec.vt V128:$dst), (int_wasm_relaxed_madd 1496349cc55cSDimitry Andric (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))], 1497bdd1243dSDimitry Andric vec.prefix#".relaxed_madd\t$dst, $a, $b, $c", 1498*0fca6ea1SDimitry Andric vec.prefix#".relaxed_madd", simdopA, reqs>; 1499bdd1243dSDimitry Andric defm NMADD_#vec : 1500*0fca6ea1SDimitry Andric SIMD_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins), 1501bdd1243dSDimitry Andric [(set (vec.vt V128:$dst), (int_wasm_relaxed_nmadd 1502349cc55cSDimitry Andric (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))], 1503bdd1243dSDimitry Andric vec.prefix#".relaxed_nmadd\t$dst, $a, $b, $c", 1504*0fca6ea1SDimitry Andric vec.prefix#".relaxed_nmadd", simdopS, reqs>; 1505349cc55cSDimitry Andric} 1506349cc55cSDimitry Andric 1507*0fca6ea1SDimitry Andricdefm "" : SIMDMADD<F32x4, 0x105, 0x106, [HasRelaxedSIMD]>; 1508*0fca6ea1SDimitry Andricdefm "" : SIMDMADD<F64x2, 0x107, 0x108, [HasRelaxedSIMD]>; 1509*0fca6ea1SDimitry Andricdefm "" : SIMDMADD<F16x8, 0x146, 0x147, [HasHalfPrecision]>; 1510349cc55cSDimitry Andric 1511349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 1512349cc55cSDimitry Andric// Laneselect 1513349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 1514349cc55cSDimitry Andric 1515349cc55cSDimitry Andricmulticlass SIMDLANESELECT<Vec vec, bits<32> op> { 1516349cc55cSDimitry Andric defm LANESELECT_#vec : 1517349cc55cSDimitry Andric RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b, V128:$c), (outs), (ins), 1518bdd1243dSDimitry Andric [(set (vec.vt V128:$dst), (int_wasm_relaxed_laneselect 1519349cc55cSDimitry Andric (vec.vt V128:$a), (vec.vt V128:$b), (vec.vt V128:$c)))], 152081ad6265SDimitry Andric vec.prefix#".relaxed_laneselect\t$dst, $a, $b, $c", 152181ad6265SDimitry Andric vec.prefix#".relaxed_laneselect", op>; 1522349cc55cSDimitry Andric} 1523349cc55cSDimitry Andric 152481ad6265SDimitry Andricdefm "" : SIMDLANESELECT<I8x16, 0x109>; 152581ad6265SDimitry Andricdefm "" : SIMDLANESELECT<I16x8, 0x10a>; 152681ad6265SDimitry Andricdefm "" : SIMDLANESELECT<I32x4, 0x10b>; 152781ad6265SDimitry Andricdefm "" : SIMDLANESELECT<I64x2, 0x10c>; 1528349cc55cSDimitry Andric 1529349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 1530349cc55cSDimitry Andric// Relaxed floating-point min and max. 1531349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 1532349cc55cSDimitry Andric 153381ad6265SDimitry Andricmulticlass RelaxedBinary<Vec vec, SDPatternOperator node, string name, 153481ad6265SDimitry Andric bits<32> simdop> { 153581ad6265SDimitry Andric defm _#vec : RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), 153681ad6265SDimitry Andric (outs), (ins), 153781ad6265SDimitry Andric [(set (vec.vt V128:$dst), 153881ad6265SDimitry Andric (node (vec.vt V128:$lhs), (vec.vt V128:$rhs)))], 153981ad6265SDimitry Andric vec.prefix#"."#name#"\t$dst, $lhs, $rhs", 154081ad6265SDimitry Andric vec.prefix#"."#name, simdop>; 1541349cc55cSDimitry Andric} 1542349cc55cSDimitry Andric 154381ad6265SDimitry Andricdefm SIMD_RELAXED_FMIN : 154481ad6265SDimitry Andric RelaxedBinary<F32x4, int_wasm_relaxed_min, "relaxed_min", 0x10d>; 154581ad6265SDimitry Andricdefm SIMD_RELAXED_FMAX : 154681ad6265SDimitry Andric RelaxedBinary<F32x4, int_wasm_relaxed_max, "relaxed_max", 0x10e>; 154781ad6265SDimitry Andricdefm SIMD_RELAXED_FMIN : 154881ad6265SDimitry Andric RelaxedBinary<F64x2, int_wasm_relaxed_min, "relaxed_min", 0x10f>; 154981ad6265SDimitry Andricdefm SIMD_RELAXED_FMAX : 155081ad6265SDimitry Andric RelaxedBinary<F64x2, int_wasm_relaxed_max, "relaxed_max", 0x110>; 1551349cc55cSDimitry Andric 1552349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 155381ad6265SDimitry Andric// Relaxed rounding q15 multiplication 1554349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 1555349cc55cSDimitry Andric 155681ad6265SDimitry Andricdefm RELAXED_Q15MULR_S : 155781ad6265SDimitry Andric RelaxedBinary<I16x8, int_wasm_relaxed_q15mulr_signed, "relaxed_q15mulr_s", 155881ad6265SDimitry Andric 0x111>; 1559349cc55cSDimitry Andric 156081ad6265SDimitry Andric//===----------------------------------------------------------------------===// 156181ad6265SDimitry Andric// Relaxed integer dot product 156281ad6265SDimitry Andric//===----------------------------------------------------------------------===// 1563349cc55cSDimitry Andric 156481ad6265SDimitry Andricdefm RELAXED_DOT : 156581ad6265SDimitry Andric RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins), 1566bdd1243dSDimitry Andric [(set (v8i16 V128:$dst), (int_wasm_relaxed_dot_i8x16_i7x16_signed 156781ad6265SDimitry Andric (v16i8 V128:$lhs), (v16i8 V128:$rhs)))], 1568bdd1243dSDimitry Andric "i16x8.relaxed_dot_i8x16_i7x16_s\t$dst, $lhs, $rhs", 1569bdd1243dSDimitry Andric "i16x8.relaxed_dot_i8x16_i7x16_s", 0x112>; 157081ad6265SDimitry Andric 157181ad6265SDimitry Andricdefm RELAXED_DOT_ADD : 157281ad6265SDimitry Andric RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, V128:$acc), 157381ad6265SDimitry Andric (outs), (ins), 1574bdd1243dSDimitry Andric [(set (v4i32 V128:$dst), (int_wasm_relaxed_dot_i8x16_i7x16_add_signed 157581ad6265SDimitry Andric (v16i8 V128:$lhs), (v16i8 V128:$rhs), (v4i32 V128:$acc)))], 1576bdd1243dSDimitry Andric "i32x4.relaxed_dot_i8x16_i7x16_add_s\t$dst, $lhs, $rhs, $acc", 1577bdd1243dSDimitry Andric "i32x4.relaxed_dot_i8x16_i7x16_add_s", 0x113>; 1578bdd1243dSDimitry Andric 1579bdd1243dSDimitry Andric//===----------------------------------------------------------------------===// 1580bdd1243dSDimitry Andric// Relaxed BFloat16 dot product 1581bdd1243dSDimitry Andric//===----------------------------------------------------------------------===// 1582bdd1243dSDimitry Andric 1583bdd1243dSDimitry Andricdefm RELAXED_DOT_BFLOAT : 1584bdd1243dSDimitry Andric RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, V128:$acc), 1585bdd1243dSDimitry Andric (outs), (ins), 1586bdd1243dSDimitry Andric [(set (v4f32 V128:$dst), (int_wasm_relaxed_dot_bf16x8_add_f32 1587bdd1243dSDimitry Andric (v8i16 V128:$lhs), (v8i16 V128:$rhs), (v4f32 V128:$acc)))], 1588bdd1243dSDimitry Andric "f32x4.relaxed_dot_bf16x8_add_f32\t$dst, $lhs, $rhs, $acc", 1589bdd1243dSDimitry Andric "f32x4.relaxed_dot_bf16x8_add_f32", 0x114>; 1590