Index: lib/Target/WebAssembly/WebAssemblyISD.def =================================================================== --- lib/Target/WebAssembly/WebAssemblyISD.def +++ lib/Target/WebAssembly/WebAssemblyISD.def @@ -21,5 +21,6 @@ HANDLE_NODETYPE(Wrapper) HANDLE_NODETYPE(BR_IF) HANDLE_NODETYPE(BR_TABLE) +HANDLE_NODETYPE(SHUFFLE) // add memory opcodes starting at ISD::FIRST_TARGET_MEMORY_OPCODE here... Index: lib/Target/WebAssembly/WebAssemblyISelLowering.h =================================================================== --- lib/Target/WebAssembly/WebAssemblyISelLowering.h +++ lib/Target/WebAssembly/WebAssemblyISelLowering.h @@ -98,6 +98,7 @@ SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const; SDValue LowerCopyToReg(SDValue Op, SelectionDAG &DAG) const; SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const; + SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const; }; namespace WebAssembly { Index: lib/Target/WebAssembly/WebAssemblyISelLowering.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -127,6 +127,17 @@ // There is no i64x2.mul instruction setOperationAction(ISD::MUL, MVT::v2i64, Expand); + // We have custom shuffle lowering to expose the shuffle mask + if (Subtarget->hasSIMD128()) { + for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) { + setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom); + } + if (EnableUnimplementedWasmSIMDInstrs) { + setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); + setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); + } + } + // As a special case, these operators use the type to mean the type to // sign-extend from. setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); @@ -822,6 +833,8 @@ return LowerCopyToReg(Op, DAG); case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); + case ISD::VECTOR_SHUFFLE: + return LowerVECTOR_SHUFFLE(Op, DAG); } } @@ -969,6 +982,30 @@ } } +SDValue +WebAssemblyTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, + SelectionDAG &DAG) const { + SDLoc DL(Op); + ArrayRef Mask = cast(Op.getNode())->getMask(); + size_t LaneBytes = 16 / Mask.size(); + + // Space for two vector args and sixteen mask indices + SDValue Ops[18]; + size_t OpIdx = 0; + Ops[OpIdx++] = Op.getOperand(0); + Ops[OpIdx++] = Op.getOperand(1); + + // Expand mask indices to byte indices and materialize them as operands + for (size_t I = 0; I < Mask.size(); ++I) { + for (size_t J = 0; J < LaneBytes; ++J) { + Ops[OpIdx++] = + DAG.getConstant((uint64_t)Mask[I] * LaneBytes + J, DL, MVT::i32); + } + } + + return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, MVT::v16i8, Ops); +} + //===----------------------------------------------------------------------===// // WebAssembly Optimization Hooks //===----------------------------------------------------------------------===// Index: lib/Target/WebAssembly/WebAssemblyInstrSIMD.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrSIMD.td +++ lib/Target/WebAssembly/WebAssemblyInstrSIMD.td @@ -218,6 +218,28 @@ defm "" : Splat; defm "" : Splat; +defm SHUFFLE_v16i8 : + SIMD_I<(outs V128:$dst), + (ins V128:$x, V128:$y, + i32imm_op:$m0, i32imm_op:$m1, i32imm_op:$m2, i32imm_op:$m3, + i32imm_op:$m4, i32imm_op:$m5, i32imm_op:$m6, i32imm_op:$m7, + i32imm_op:$m8, i32imm_op:$m9, i32imm_op:$mA, i32imm_op:$mB, + i32imm_op:$mC, i32imm_op:$mD, i32imm_op:$mE, i32imm_op:$mF), + (outs), + (ins + i32imm_op:$m0, i32imm_op:$m1, i32imm_op:$m2, i32imm_op:$m3, + i32imm_op:$m4, i32imm_op:$m5, i32imm_op:$m6, i32imm_op:$m7, + i32imm_op:$m8, i32imm_op:$m9, i32imm_op:$mA, i32imm_op:$mB, + i32imm_op:$mC, i32imm_op:$mD, i32imm_op:$mE, i32imm_op:$mF), + [], + "v8x16.shuffle\t$dst, $x, $y, "# + "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "# + "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF", + "v8x16.shuffle\t"# + "$m0, $m1, $m2, $m3, $m4, $m5, $m6, $m7, "# + "$m8, $m9, $mA, $mB, $mC, $mD, $mE, $mF", + 23>; + let isCommutable = 1 in { defm ADD : SIMDBinaryInt; defm ADD : SIMDBinaryFP; @@ -282,6 +304,30 @@ ) in def : Pat<(t1 (bitconvert (t2 V128:$v))), (t1 V128:$v)>; +// Shuffles after custom lowering +def wasm_shuffle_t : SDTypeProfile<1, 18, []>; +def wasm_shuffle : SDNode<"WebAssemblyISD::SHUFFLE", wasm_shuffle_t>; +foreach vec_t = [v16i8, v8i16, v4i32, v2i64, v4f32, v2f64] in { +def : Pat<(v16i8 (wasm_shuffle (vec_t V128:$x), (vec_t V128:$y), + (i32 LaneIdx32:$m0), (i32 LaneIdx32:$m1), + (i32 LaneIdx32:$m2), (i32 LaneIdx32:$m3), + (i32 LaneIdx32:$m4), (i32 LaneIdx32:$m5), + (i32 LaneIdx32:$m6), (i32 LaneIdx32:$m7), + (i32 LaneIdx32:$m8), (i32 LaneIdx32:$m9), + (i32 LaneIdx32:$mA), (i32 LaneIdx32:$mB), + (i32 LaneIdx32:$mC), (i32 LaneIdx32:$mD), + (i32 LaneIdx32:$mE), (i32 LaneIdx32:$mF))), + (v16i8 (SHUFFLE_v16i8 (vec_t V128:$x), (vec_t V128:$y), + (i32 LaneIdx32:$m0), (i32 LaneIdx32:$m1), + (i32 LaneIdx32:$m2), (i32 LaneIdx32:$m3), + (i32 LaneIdx32:$m4), (i32 LaneIdx32:$m5), + (i32 LaneIdx32:$m6), (i32 LaneIdx32:$m7), + (i32 LaneIdx32:$m8), (i32 LaneIdx32:$m9), + (i32 LaneIdx32:$mA), (i32 LaneIdx32:$mB), + (i32 LaneIdx32:$mC), (i32 LaneIdx32:$mD), + (i32 LaneIdx32:$mE), (i32 LaneIdx32:$mF)))>; +} + // arbitrary other BUILD_VECTOR patterns def : Pat<(v16i8 (build_vector (i32 I32:$x0), (i32 I32:$x1), (i32 I32:$x2), (i32 I32:$x3), Index: test/CodeGen/WebAssembly/simd.ll =================================================================== --- test/CodeGen/WebAssembly/simd.ll +++ test/CodeGen/WebAssembly/simd.ll @@ -90,6 +90,23 @@ ret <16 x i8> %res } +; CHECK-LABEL: shuffle_v16i8: +; NO-SIMD128-NOT: v8x16 +; SIMD128: .param v128, v128{{$}} +; SIMD128: .result v128{{$}} +; SIMD128: v8x16.shuffle $push0=, $0, $1, +; SIMD128-SAME: 0, 17, 2, 19, 4, 21, 6, 23, 8, 25, 10, 27, 12, 29, 14, 31 +; SIMD128-SAME: # encoding: [0xfd,0x17, +; SIMD128-SAME: 0x00,0x11,0x02,0x13,0x04,0x15,0x06,0x17, +; SIMD128-SAME: 0x08,0x19,0x0a,0x1b,0x0c,0x1d,0x0e,0x1f] +; SIMD128: return $pop0 # +define <16 x i8> @shuffle_v16i8(<16 x i8> %x, <16 x i8> %y) { + %res = shufflevector <16 x i8> %x, <16 x i8> %y, + <16 x i32> + ret <16 x i8> %res +} + ; CHECK-LABEL: build_v16i8: ; NO-SIMD128-NOT: i8x16 ; SIMD128: .param i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32{{$}} @@ -214,6 +231,22 @@ ret <8 x i16> %res } +; CHECK-LABEL: shuffle_v8i16: +; NO-SIMD128-NOT: v8x16 +; SIMD128: .param v128, v128{{$}} +; SIMD128: .result v128{{$}} +; SIMD128: v8x16.shuffle $push0=, $0, $1, +; SIMD128-SAME: 0, 1, 18, 19, 4, 5, 22, 23, 8, 9, 26, 27, 12, 13, 30, 31 +; SIMD128-SAME: # encoding: [0xfd,0x17, +; SIMD128-SAME: 0x00,0x01,0x12,0x13,0x04,0x05,0x16,0x17, +; SIMD128-SAME: 0x08,0x09,0x1a,0x1b,0x0c,0x0d,0x1e,0x1f] +; SIMD128: return $pop0 # +define <8 x i16> @shuffle_v8i16(<8 x i16> %x, <8 x i16> %y) { + %res = shufflevector <8 x i16> %x, <8 x i16> %y, + <8 x i32> + ret <8 x i16> %res +} + ; CHECK-LABEL: build_v8i16: ; NO-SIMD128-NOT: i16x8 ; SIMD128: .param i32, i32, i32, i32, i32, i32, i32, i32{{$}} @@ -295,6 +328,22 @@ ret <4 x i32> %res } +; CHECK-LABEL: shuffle_v4i32: +; NO-SIMD128-NOT: v8x16 +; SIMD128: .param v128, v128{{$}} +; SIMD128: .result v128{{$}} +; SIMD128: v8x16.shuffle $push0=, $0, $1, +; SIMD128-SAME: 0, 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 11, 28, 29, 30, 31 +; SIMD128-SAME: # encoding: [0xfd,0x17, +; SIMD128-SAME: 0x00,0x01,0x02,0x03,0x14,0x15,0x16,0x17, +; SIMD128-SAME: 0x08,0x09,0x0a,0x0b,0x1c,0x1d,0x1e,0x1f] +; SIMD128: return $pop0 # +define <4 x i32> @shuffle_v4i32(<4 x i32> %x, <4 x i32> %y) { + %res = shufflevector <4 x i32> %x, <4 x i32> %y, + <4 x i32> + ret <4 x i32> %res +} + ; CHECK-LABEL: build_v4i32: ; NO-SIMD128-NOT: i32x4 ; SIMD128: .param i32, i32, i32, i32{{$}} @@ -370,6 +419,21 @@ ret <2 x i64> %res } +; CHECK-LABEL: shuffle_v2i64: +; NO-SIMD128-NOT: v8x16 +; SIMD128: .param v128, v128{{$}} +; SIMD128: .result v128{{$}} +; SIMD128: v8x16.shuffle $push0=, $0, $1, +; SIMD128-SAME: 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 +; SIMD128-SAME: # encoding: [0xfd,0x17, +; SIMD128-SAME: 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, +; SIMD128-SAME: 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f] +; SIMD128: return $pop0 # +define <2 x i64> @shuffle_v2i64(<2 x i64> %x, <2 x i64> %y) { + %res = shufflevector <2 x i64> %x, <2 x i64> %y, <2 x i32> + ret <2 x i64> %res +} + ; CHECK-LABEL: build_v2i64: ; NO-SIMD128-NOT: i64x2 ; SIMD128-VM-NOT: i64x2 @@ -441,6 +505,22 @@ ret <4 x float> %res } +; CHECK-LABEL: shuffle_v4f32: +; NO-SIMD128-NOT: v8x16 +; SIMD128: .param v128, v128{{$}} +; SIMD128: .result v128{{$}} +; SIMD128: v8x16.shuffle $push0=, $0, $1, +; SIMD128-SAME: 0, 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 11, 28, 29, 30, 31 +; SIMD128-SAME: # encoding: [0xfd,0x17, +; SIMD128-SAME: 0x00,0x01,0x02,0x03,0x14,0x15,0x16,0x17, +; SIMD128-SAME: 0x08,0x09,0x0a,0x0b,0x1c,0x1d,0x1e,0x1f] +; SIMD128: return $pop0 # +define <4 x float> @shuffle_v4f32(<4 x float> %x, <4 x float> %y) { + %res = shufflevector <4 x float> %x, <4 x float> %y, + <4 x i32> + ret <4 x float> %res +} + ; CHECK-LABEL: build_v4f32: ; NO-SIMD128-NOT: f32x4 ; SIMD128: .param f32, f32, f32, f32{{$}} @@ -515,6 +595,22 @@ ret <2 x double> %res } +; CHECK-LABEL: shuffle_v2f64: +; NO-SIMD128-NOT: v8x16 +; SIMD128: .param v128, v128{{$}} +; SIMD128: .result v128{{$}} +; SIMD128: v8x16.shuffle $push0=, $0, $1, +; SIMD128-SAME: 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 +; SIMD128-SAME: # encoding: [0xfd,0x17, +; SIMD128-SAME: 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, +; SIMD128-SAME: 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f] +; SIMD128: return $pop0 # +define <2 x double> @shuffle_v2f64(<2 x double> %x, <2 x double> %y) { + %res = shufflevector <2 x double> %x, <2 x double> %y, + <2 x i32> + ret <2 x double> %res +} + ; CHECK-LABEL: build_v2f64: ; NO-SIMD128-NOT: f64x2 ; SIMD128-VM-NOT: f64x2 Index: test/MC/Disassembler/WebAssembly/wasm.txt =================================================================== --- test/MC/Disassembler/WebAssembly/wasm.txt +++ test/MC/Disassembler/WebAssembly/wasm.txt @@ -34,3 +34,6 @@ # v128.const is arbitrarily disassembled as v16i8 # CHECK: v128.const 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 0xFD 0x00 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F + +# CHECK: v8x16.shuffle 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 +0xFD 0x17 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F