[ValueTracking, InstSimplify] extend isKnownNonZero() to handle vector constants

Similar in spirit to D20497 :
If all elements of a constant vector are known non-zero, then we can say that the
whole vector is known non-zero.

It seems like we could extend this to FP scalar/vector too, but isKnownNonZero()
says it only works for integers and pointers for now.

Differential Revision: http://reviews.llvm.org/D20544

llvm-svn: 270562
This commit is contained in:
Sanjay Patel 2016-05-24 14:18:49 +00:00
parent 0295fbe1bb
commit 23019d1006
2 changed files with 19 additions and 22 deletions
llvm
lib/Analysis
test/Transforms/InstSimplify

View File

@ -1678,7 +1678,20 @@ bool isKnownNonZero(Value *V, unsigned Depth, const Query &Q) {
if (isa<ConstantInt>(C)) if (isa<ConstantInt>(C))
// Must be non-zero due to null test above. // Must be non-zero due to null test above.
return true; return true;
// TODO: Handle vectors
// For constant vectors, check that all elements are undefined or known
// non-zero to determine that the whole vector is known non-zero.
if (auto *VecTy = dyn_cast<VectorType>(C->getType())) {
for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
Constant *Elt = C->getAggregateElement(i);
if (!Elt || Elt->isNullValue())
return false;
if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
return false;
}
return true;
}
return false; return false;
} }

View File

@ -1,13 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instsimplify -S | FileCheck %s ; RUN: opt < %s -instsimplify -S | FileCheck %s
; FIXME: isKnownNonZero should work for integer vectors.
define <2 x i1> @nonzero_vec_splat(<2 x i32> %x) { define <2 x i1> @nonzero_vec_splat(<2 x i32> %x) {
; CHECK-LABEL: @nonzero_vec_splat( ; CHECK-LABEL: @nonzero_vec_splat(
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> %x, <i32 1, i32 1> ; CHECK-NEXT: ret <2 x i1> zeroinitializer
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[Y]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[C]]
; ;
%y = or <2 x i32> %x, <i32 1, i32 1> %y = or <2 x i32> %x, <i32 1, i32 1>
%c = icmp eq <2 x i32> %y, zeroinitializer %c = icmp eq <2 x i32> %y, zeroinitializer
@ -16,9 +12,7 @@ define <2 x i1> @nonzero_vec_splat(<2 x i32> %x) {
define <2 x i1> @nonzero_vec_nonsplat(<2 x i32> %x) { define <2 x i1> @nonzero_vec_nonsplat(<2 x i32> %x) {
; CHECK-LABEL: @nonzero_vec_nonsplat( ; CHECK-LABEL: @nonzero_vec_nonsplat(
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> %x, <i32 2, i32 1> ; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i32> [[Y]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[C]]
; ;
%y = or <2 x i32> %x, <i32 2, i32 1> %y = or <2 x i32> %x, <i32 2, i32 1>
%c = icmp ne <2 x i32> %y, zeroinitializer %c = icmp ne <2 x i32> %y, zeroinitializer
@ -27,9 +21,7 @@ define <2 x i1> @nonzero_vec_nonsplat(<2 x i32> %x) {
define <2 x i1> @nonzero_vec_undef_elt(<2 x i32> %x) { define <2 x i1> @nonzero_vec_undef_elt(<2 x i32> %x) {
; CHECK-LABEL: @nonzero_vec_undef_elt( ; CHECK-LABEL: @nonzero_vec_undef_elt(
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> %x, <i32 undef, i32 1> ; CHECK-NEXT: ret <2 x i1> zeroinitializer
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[Y]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[C]]
; ;
%y = or <2 x i32> %x, <i32 undef, i32 1> %y = or <2 x i32> %x, <i32 undef, i32 1>
%c = icmp eq <2 x i32> %y, zeroinitializer %c = icmp eq <2 x i32> %y, zeroinitializer
@ -50,11 +42,7 @@ define <2 x i1> @may_be_zero_vec(<2 x i32> %x) {
; Multiplies of non-zero numbers are non-zero if there is no unsigned overflow. ; Multiplies of non-zero numbers are non-zero if there is no unsigned overflow.
define <2 x i1> @nonzero_vec_mul_nuw(<2 x i32> %x, <2 x i32> %y) { define <2 x i1> @nonzero_vec_mul_nuw(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @nonzero_vec_mul_nuw( ; CHECK-LABEL: @nonzero_vec_mul_nuw(
; CHECK-NEXT: [[XNZ:%.*]] = or <2 x i32> %x, <i32 1, i32 2> ; CHECK-NEXT: ret <2 x i1> zeroinitializer
; CHECK-NEXT: [[YNZ:%.*]] = or <2 x i32> %y, <i32 3, i32 undef>
; CHECK-NEXT: [[M:%.*]] = mul nuw <2 x i32> [[XNZ]], [[YNZ]]
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[M]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[C]]
; ;
%xnz = or <2 x i32> %x, <i32 1, i32 2> %xnz = or <2 x i32> %x, <i32 1, i32 2>
%ynz = or <2 x i32> %y, <i32 3, i32 undef> %ynz = or <2 x i32> %y, <i32 3, i32 undef>
@ -66,11 +54,7 @@ define <2 x i1> @nonzero_vec_mul_nuw(<2 x i32> %x, <2 x i32> %y) {
; Multiplies of non-zero numbers are non-zero if there is no signed overflow. ; Multiplies of non-zero numbers are non-zero if there is no signed overflow.
define <2 x i1> @nonzero_vec_mul_nsw(<2 x i32> %x, <2 x i32> %y) { define <2 x i1> @nonzero_vec_mul_nsw(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @nonzero_vec_mul_nsw( ; CHECK-LABEL: @nonzero_vec_mul_nsw(
; CHECK-NEXT: [[XNZ:%.*]] = or <2 x i32> %x, <i32 undef, i32 2> ; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
; CHECK-NEXT: [[YNZ:%.*]] = or <2 x i32> %y, <i32 3, i32 4>
; CHECK-NEXT: [[M:%.*]] = mul nsw <2 x i32> [[XNZ]], [[YNZ]]
; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i32> [[M]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[C]]
; ;
%xnz = or <2 x i32> %x, <i32 undef, i32 2> %xnz = or <2 x i32> %x, <i32 undef, i32 2>
%ynz = or <2 x i32> %y, <i32 3, i32 4> %ynz = or <2 x i32> %y, <i32 3, i32 4>