diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -41361,14 +41361,22 @@ getTargetConstantBitsFromNode(N0, NumBitsPerElt, UndefElts, EltBits)) { assert(EltBits.size() == VT.getVectorNumElements() && "Unexpected shift value type"); - for (APInt &Elt : EltBits) { - if (X86ISD::VSHLI == Opcode) + // Undef elements need to fold to 0. It's possible SimplifyDemandedBits + // created an undef input due to no input bits being demanded, but user + // still expects 0 in other bits. + for (unsigned i = 0, e = EltBits.size(); i != e; ++i) { + APInt &Elt = EltBits[i]; + if (UndefElts[i]) + Elt = 0; + else if (X86ISD::VSHLI == Opcode) Elt <<= ShiftVal; else if (X86ISD::VSRAI == Opcode) Elt.ashrInPlace(ShiftVal); else Elt.lshrInPlace(ShiftVal); } + // Reset undef elements since they were zeroed above. + UndefElts = 0; return getConstVector(EltBits, UndefElts, VT.getSimpleVT(), DAG, SDLoc(N)); } diff --git a/llvm/test/CodeGen/X86/vec_shift5.ll b/llvm/test/CodeGen/X86/vec_shift5.ll --- a/llvm/test/CodeGen/X86/vec_shift5.ll +++ b/llvm/test/CodeGen/X86/vec_shift5.ll @@ -149,7 +149,7 @@ define <2 x i64> @test11() { ; X32-LABEL: test11: ; X32: # %bb.0: -; X32-NEXT: movaps {{.*#+}} xmm0 = +; X32-NEXT: movaps {{.*#+}} xmm0 = [0,0,3,0] ; X32-NEXT: retl ; ; X64-LABEL: test11: @@ -219,7 +219,7 @@ define <2 x i64> @test16() { ; X32-LABEL: test16: ; X32: # %bb.0: -; X32-NEXT: movaps {{.*#+}} xmm0 = +; X32-NEXT: movaps {{.*#+}} xmm0 = [0,0,248,0] ; X32-NEXT: retl ; ; X64-LABEL: test16: