Skip to content

Commit 51f0d64

Browse files
author
Zvi Rackover
committedJan 24, 2018
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 llvm-svn: 323343
1 parent 262ed0e commit 51f0d64

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed
 

‎llvm/lib/Analysis/InstructionSimplify.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,14 @@ static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) {
868868
if (match(Op1, m_Zero()))
869869
return UndefValue::get(Ty);
870870

871-
// If any element of a constant divisor vector is zero, the whole op is undef.
871+
// If any element of a constant divisor vector is zero or undef, the whole op
872+
// is undef.
872873
auto *Op1C = dyn_cast<Constant>(Op1);
873874
if (Op1C && Ty->isVectorTy()) {
874875
unsigned NumElts = Ty->getVectorNumElements();
875876
for (unsigned i = 0; i != NumElts; ++i) {
876877
Constant *Elt = Op1C->getAggregateElement(i);
877-
if (Elt && Elt->isNullValue())
878+
if (Elt && (Elt->isNullValue() || isa<UndefValue>(Elt)))
878879
return UndefValue::get(Ty);
879880
}
880881
}

‎llvm/test/Transforms/InstSimplify/div.ll

+16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ define <2 x i8> @udiv_zero_elt_vec(<2 x i8> %x) {
3434
ret <2 x i8> %div
3535
}
3636

37+
define <2 x i8> @sdiv_undef_elt_vec(<2 x i8> %x) {
38+
; CHECK-LABEL: @sdiv_undef_elt_vec(
39+
; CHECK-NEXT: ret <2 x i8> undef
40+
;
41+
%div = sdiv <2 x i8> %x, <i8 -42, i8 undef>
42+
ret <2 x i8> %div
43+
}
44+
45+
define <2 x i8> @udiv_undef_elt_vec(<2 x i8> %x) {
46+
; CHECK-LABEL: @udiv_undef_elt_vec(
47+
; CHECK-NEXT: ret <2 x i8> undef
48+
;
49+
%div = udiv <2 x i8> %x, <i8 undef, i8 42>
50+
ret <2 x i8> %div
51+
}
52+
3753
; Division-by-zero is undef. UB in any vector lane means the whole op is undef.
3854
; Thus, we can simplify this: if any element of 'y' is 0, we can do anything.
3955
; Therefore, assume that all elements of 'y' must be 1.

‎llvm/test/Transforms/InstSimplify/rem.ll

+16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ define <2 x i8> @urem_zero_elt_vec(<2 x i8> %x) {
3535
ret <2 x i8> %rem
3636
}
3737

38+
define <2 x i8> @srem_undef_elt_vec(<2 x i8> %x) {
39+
; CHECK-LABEL: @srem_undef_elt_vec(
40+
; CHECK-NEXT: ret <2 x i8> undef
41+
;
42+
%rem = srem <2 x i8> %x, <i8 -42, i8 undef>
43+
ret <2 x i8> %rem
44+
}
45+
46+
define <2 x i8> @urem_undef_elt_vec(<2 x i8> %x) {
47+
; CHECK-LABEL: @urem_undef_elt_vec(
48+
; CHECK-NEXT: ret <2 x i8> undef
49+
;
50+
%rem = urem <2 x i8> %x, <i8 undef, i8 42>
51+
ret <2 x i8> %rem
52+
}
53+
3854
; Division-by-zero is undef. UB in any vector lane means the whole op is undef.
3955
; Thus, we can simplify this: if any element of 'y' is 0, we can do anything.
4056
; Therefore, assume that all elements of 'y' must be 1.

0 commit comments

Comments
 (0)
Please sign in to comment.