From f2b2f1fda33ed5e3aed3f17a9e04e8b0e2861d57 Mon Sep 17 00:00:00 2001 From: Zvi Rackover Date: Wed, 24 Jan 2018 17:22:00 +0000 Subject: [PATCH] InstSimplify: If divisor element is undef simplify to undef Summary: If any vector divisor element is undef, we can arbitrarily choose it be zero which would make the div/rem an undef value by definition. Reviewers: spatel, reames Reviewed By: spatel Subscribers: magabari, llvm-commits Differential Revision: https://reviews.llvm.org/D42485 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@323343 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/InstructionSimplify.cpp | 5 +++-- test/Transforms/InstSimplify/div.ll | 16 ++++++++++++++++ test/Transforms/InstSimplify/rem.ll | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 245133fb912..a258c4df898 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -868,13 +868,14 @@ static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) { if (match(Op1, m_Zero())) return UndefValue::get(Ty); - // If any element of a constant divisor vector is zero, the whole op is undef. + // If any element of a constant divisor vector is zero or undef, the whole op + // is undef. auto *Op1C = dyn_cast(Op1); if (Op1C && Ty->isVectorTy()) { unsigned NumElts = Ty->getVectorNumElements(); for (unsigned i = 0; i != NumElts; ++i) { Constant *Elt = Op1C->getAggregateElement(i); - if (Elt && Elt->isNullValue()) + if (Elt && (Elt->isNullValue() || isa(Elt))) return UndefValue::get(Ty); } } diff --git a/test/Transforms/InstSimplify/div.ll b/test/Transforms/InstSimplify/div.ll index a1cc8572724..1eb767e17bf 100644 --- a/test/Transforms/InstSimplify/div.ll +++ b/test/Transforms/InstSimplify/div.ll @@ -34,6 +34,22 @@ define <2 x i8> @udiv_zero_elt_vec(<2 x i8> %x) { ret <2 x i8> %div } +define <2 x i8> @sdiv_undef_elt_vec(<2 x i8> %x) { +; CHECK-LABEL: @sdiv_undef_elt_vec( +; CHECK-NEXT: ret <2 x i8> undef +; + %div = sdiv <2 x i8> %x, + ret <2 x i8> %div +} + +define <2 x i8> @udiv_undef_elt_vec(<2 x i8> %x) { +; CHECK-LABEL: @udiv_undef_elt_vec( +; CHECK-NEXT: ret <2 x i8> undef +; + %div = udiv <2 x i8> %x, + ret <2 x i8> %div +} + ; Division-by-zero is undef. UB in any vector lane means the whole op is undef. ; Thus, we can simplify this: if any element of 'y' is 0, we can do anything. ; Therefore, assume that all elements of 'y' must be 1. diff --git a/test/Transforms/InstSimplify/rem.ll b/test/Transforms/InstSimplify/rem.ll index a015d4c43e0..e1fc6684cf9 100644 --- a/test/Transforms/InstSimplify/rem.ll +++ b/test/Transforms/InstSimplify/rem.ll @@ -35,6 +35,22 @@ define <2 x i8> @urem_zero_elt_vec(<2 x i8> %x) { ret <2 x i8> %rem } +define <2 x i8> @srem_undef_elt_vec(<2 x i8> %x) { +; CHECK-LABEL: @srem_undef_elt_vec( +; CHECK-NEXT: ret <2 x i8> undef +; + %rem = srem <2 x i8> %x, + ret <2 x i8> %rem +} + +define <2 x i8> @urem_undef_elt_vec(<2 x i8> %x) { +; CHECK-LABEL: @urem_undef_elt_vec( +; CHECK-NEXT: ret <2 x i8> undef +; + %rem = urem <2 x i8> %x, + ret <2 x i8> %rem +} + ; Division-by-zero is undef. UB in any vector lane means the whole op is undef. ; Thus, we can simplify this: if any element of 'y' is 0, we can do anything. ; Therefore, assume that all elements of 'y' must be 1. -- 2.11.0