From f57ae33381f27445d2b174bfae2cd94d49852398 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Mon, 27 Mar 2017 13:52:51 +0000 Subject: [PATCH] [InstCombine] Avoid incorrect folding of select into phi nodes when incoming element is a vector type Summary: We are incorrectly folding selects into phi nodes when the incoming value of a phi node is a constant vector. This optimization is done in `FoldOpIntoPhi` when the select condition is a phi node with constant incoming values. Without the fix, we are miscompiling (i.e. incorrectly folding the select into the phi node) when the vector contains non-zero elements. This patch fixes the miscompile and we will correctly fold based on the select vector operand (see added test cases). Reviewers: majnemer, sanjoy, spatel Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D31189 llvm-svn: 298845 --- .../InstCombine/InstructionCombining.cpp | 6 +++- .../Transforms/InstCombine/phi-select-constexpr.ll | 38 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 9cd22f6..020e8c9 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(PN->getIncomingValue(i)); - if (InC && !isa(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(InC) && !isa(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 054e069..272594d 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 ], [ , %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 +} -- 2.7.4