diff options
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 6 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/phi-select-constexpr.ll | 38 |
2 files changed, 43 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 9cd22f65c99..020e8c9f046 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -908,7 +908,11 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { // Beware of ConstantExpr: it may eventually evaluate to getNullValue, // even if currently isNullValue gives false. Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)); - if (InC && !isa<ConstantExpr>(InC)) + // For vector constants, we cannot use isNullValue to fold into + // FalseVInPred versus TrueVInPred. When we have individual nonzero + // elements in the vector, we will incorrectly fold InC to + // `TrueVInPred`. + if (InC && !isa<ConstantExpr>(InC) && !isa<VectorType>(InC->getType())) InV = InC->isNullValue() ? FalseVInPred : TrueVInPred; else InV = Builder->CreateSelect(PN->getIncomingValue(i), diff --git a/llvm/test/Transforms/InstCombine/phi-select-constexpr.ll b/llvm/test/Transforms/InstCombine/phi-select-constexpr.ll index 054e0691d47..272594d7f4f 100644 --- a/llvm/test/Transforms/InstCombine/phi-select-constexpr.ll +++ b/llvm/test/Transforms/InstCombine/phi-select-constexpr.ll @@ -9,6 +9,7 @@ entry: 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 @@ final: 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 ], [ <i64 0, i64 0, i64 126, i64 127>, %delay ] +; CHECK-NOT: select +; CHECK: ret <4 x i64> %phinode + %phinode = phi <4 x i1> [ <i1 true, i1 true, i1 true, i1 true>, %entry ], [ <i1 true, i1 true, i1 false, i1 false>, %delay ] + %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127> + 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> [ <i64 124, i64 125, i64 126, i64 127>, %entry ], [ <i64 0, i64 125, i64 0, i64 127>, %delay ] +; CHECK-NOT: select +; CHECK: ret <4 x i64> %phinode + %phinode = phi <4 x i1> [ <i1 false, i1 false, i1 false, i1 false>, %entry ], [ <i1 true, i1 false, i1 true, i1 false>, %delay ] + %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127> + ret <4 x i64> %sel +} |

