Index: llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td +++ llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td @@ -230,6 +230,19 @@ defm "" : Splat; defm "" : Splat; +// scalar_to_vector leaves high lanes undefined, so can be a splat +class ScalarSplatPat : + Pat<(vec_t (scalar_to_vector (lane_t reg_t:$x))), + (!cast("SPLAT_"#vec_t) reg_t:$x)>; + +def : ScalarSplatPat; +def : ScalarSplatPat; +def : ScalarSplatPat; +def : ScalarSplatPat; +def : ScalarSplatPat; +def : ScalarSplatPat; + //===----------------------------------------------------------------------===// // Accessing lanes //===----------------------------------------------------------------------===// Index: llvm/test/CodeGen/WebAssembly/simd-scalar-to-vector.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/WebAssembly/simd-scalar-to-vector.ll @@ -0,0 +1,27 @@ +; RUN: llc < %s -asm-verbose=false -verify-machineinstrs -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+simd128 | FileCheck %s + +; Test that scalar_to_vector is lowered into a splat correctly. +; TODO: find small test cases that produce scalar_to_vector dag nodes +; to make this test more readable and comprehensive. + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown-wasm" + +; CHECK-LABEL: foo: +; CHECK: i32x4.splat +define void @foo() { +entry: + %a = load <2 x i16>, <2 x i16>* undef, align 1 + %b = shufflevector <2 x i16> %a, <2 x i16> undef, <8 x i32> + %0 = bitcast <8 x i16> %b to <16 x i8> + %shuffle.i214 = shufflevector <16 x i8> %0, <16 x i8> , <16 x i32> + %1 = bitcast <16 x i8> %shuffle.i214 to <8 x i16> + %add82 = add <8 x i16> %1, zeroinitializer + %2 = select <8 x i1> undef, <8 x i16> undef, <8 x i16> %add82 + %3 = bitcast <8 x i16> %2 to <16 x i8> + %shuffle.i204 = shufflevector <16 x i8> %3, <16 x i8> undef, <16 x i32> + %4 = bitcast <16 x i8> %shuffle.i204 to <8 x i16> + %dst2.0.vec.extract = shufflevector <8 x i16> %4, <8 x i16> undef, <4 x i32> + store <4 x i16> %dst2.0.vec.extract, <4 x i16>* undef, align 1 + ret void +}