Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -908,7 +908,10 @@ // Beware of ConstantExpr: it may eventually evaluate to getNullValue, // even if currently isNullValue gives false. Constant *InC = dyn_cast(PN->getIncomingValue(i)); - if (InC && !isa(InC)) + // For vector constants, we cannot use isNullValue to fold into + // FalseVInPred versus TrueVInPred. It does not investigate the `nullness` + // of individual elements in the vector (and will always return false). + if (InC && !isa(InC) && !isa(InC->getType())) InV = InC->isNullValue() ? FalseVInPred : TrueVInPred; else InV = Builder->CreateSelect(PN->getIncomingValue(i), Index: test/Transforms/InstCombine/phi-select-constexpr.ll =================================================================== --- test/Transforms/InstCombine/phi-select-constexpr.ll +++ test/Transforms/InstCombine/phi-select-constexpr.ll @@ -9,6 +9,7 @@ delay: br label %final +; CHECK-LABEL: @foo ; CHECK-LABEL: final: ; CHECK: phi i32 [ 1, %entry ], [ select (i1 icmp eq (i32* @A, i32* @B), i32 2, i32 1), %delay ] final: @@ -17,3 +18,40 @@ ret i32 %value } + +; test folding of select into phi for vectors. +define <4 x i64> @vec1(i1 %which) { +entry: + br i1 %which, label %final, label %delay + +delay: + br label %final + +final: +; CHECK-LABEL: @vec1 +; CHECK-LABEL: final: +; CHECK: %phinode = phi <4 x i64> [ zeroinitializer, %entry ], [ , %delay ] +; CHECK-NOT: select +; CHECK: ret <4 x i64> %phinode + %phinode = phi <4 x i1> [ , %entry ], [ , %delay ] + %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> + ret <4 x i64> %sel +} + +define <4 x i64> @vec2(i1 %which) { +entry: + br i1 %which, label %final, label %delay + +delay: + br label %final + +final: +; CHECK-LABEL: @vec2 +; CHECK-LABEL: final: +; CHECK: %phinode = phi <4 x i64> [ , %entry ], [ , %delay ] +; CHECK-NOT: select +; CHECK: ret <4 x i64> %phinode + %phinode = phi <4 x i1> [ , %entry ], [ , %delay ] + %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> + ret <4 x i64> %sel +}