diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index f197ae61550a9..343a93785766c 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -12535,9 +12535,10 @@ SDValue DAGCombiner::foldVSelectOfConstants(SDNode *N) { for (unsigned i = 0; i != Elts; ++i) { SDValue N1Elt = N1.getOperand(i); SDValue N2Elt = N2.getOperand(i); - if (N1Elt.isUndef() || N2Elt.isUndef()) + if (N1Elt.isUndef()) continue; - if (N1Elt.getValueType() != N2Elt.getValueType()) { + // N2 should not contain undef values since it will be reused in the fold. + if (N2Elt.isUndef() || N1Elt.getValueType() != N2Elt.getValueType()) { AllAddOne = false; AllSubOne = false; break; diff --git a/llvm/test/CodeGen/X86/vselect-constants.ll b/llvm/test/CodeGen/X86/vselect-constants.ll index 901f7e4a00eb5..34bda718db8f6 100644 --- a/llvm/test/CodeGen/X86/vselect-constants.ll +++ b/llvm/test/CodeGen/X86/vselect-constants.ll @@ -302,3 +302,21 @@ define i32 @wrong_min_signbits(<2 x i16> %x) { %t1 = bitcast <2 x i16> %sel to i32 ret i32 %t1 } + +define i32 @pr129181() { +; SSE-LABEL: pr129181: +; SSE: # %bb.0: # %entry +; SSE-NEXT: xorl %eax, %eax +; SSE-NEXT: retq +; +; AVX-LABEL: pr129181: +; AVX: # %bb.0: # %entry +; AVX-NEXT: xorl %eax, %eax +; AVX-NEXT: retq +entry: + %x = insertelement <4 x i32> zeroinitializer, i32 0, i32 0 + %cmp = icmp ult <4 x i32> %x, splat (i32 1) + %sel = select <4 x i1> %cmp, <4 x i32> zeroinitializer, <4 x i32> + %reduce = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %sel) + ret i32 %reduce +}