From de39bfc89c4977eba820890119464bb2428b92b7 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 9 Apr 2020 07:55:55 -0400 Subject: [PATCH] [InstCombine] replace undef in vector constant for safe shift transform (PR45447) As noted in PR45447, we have a vector-constant-with-undef-element transform bug: https://bugs.llvm.org/show_bug.cgi?id=45447 We replace undefs with a safe constant (0 or -1) based on the (non-)negative predicate constraint. So this is correct: http://volta.cs.utah.edu:8080/z/WZE36H ...but this is not: http://volta.cs.utah.edu:8080/z/boj8gJ Previously, we were relying on getSafeVectorConstantForBinop() in the related fold (D76800). But that's making an assumption about what qualifies as "safe", and that assumption may not always hold. Differential Revision: https://reviews.llvm.org/D77739 --- .../InstCombine/InstCombineAndOrXor.cpp | 16 +++++++++++----- test/Transforms/InstCombine/vector-xor.ll | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 42eff6af433..b4e70a563b0 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -3071,16 +3071,22 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { Constant *C; if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) && match(C, m_Negative())) { - Constant *NewC = ConstantExpr::getNot(C); - if (C->getType()->isVectorTy()) - NewC = getSafeVectorConstantForBinop(Instruction::LShr, NewC, false); - return BinaryOperator::CreateLShr(NewC, Y); + // We matched a negative constant, so propagating undef is unsafe. + // Clamp undef elements to -1. + Type *EltTy = C->getType()->getScalarType(); + C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy)); + return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y); } // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits) if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) && - match(C, m_NonNegative())) + match(C, m_NonNegative())) { + // We matched a non-negative constant, so propagating undef is unsafe. + // Clamp undef elements to 0. + Type *EltTy = C->getType()->getScalarType(); + C = Constant::replaceUndefsWith(C, ConstantInt::getNullValue(EltTy)); return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y); + } // ~(X + C) --> -(C + 1) - X if (match(Op0, m_Add(m_Value(X), m_Constant(C)))) diff --git a/test/Transforms/InstCombine/vector-xor.ll b/test/Transforms/InstCombine/vector-xor.ll index 52e904902b5..61ced0262e3 100644 --- a/test/Transforms/InstCombine/vector-xor.ll +++ b/test/Transforms/InstCombine/vector-xor.ll @@ -172,7 +172,7 @@ define <4 x i32> @test_v4i32_not_lshr_nonnegative_const(<4 x i32> %a0) { define <4 x i32> @test_v4i32_not_lshr_nonnegative_const_undef(<4 x i32> %a0) { ; CHECK-LABEL: @test_v4i32_not_lshr_nonnegative_const_undef( -; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> , [[A0:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> , [[A0:%.*]] ; CHECK-NEXT: ret <4 x i32> [[TMP1]] ; %1 = lshr <4 x i32> , %a0