Skip to content

Commit 23019d1

Browse files
committedMay 24, 2016
[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
1 parent 0295fbe commit 23019d1

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed
 

‎llvm/lib/Analysis/ValueTracking.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,20 @@ bool isKnownNonZero(Value *V, unsigned Depth, const Query &Q) {
16781678
if (isa<ConstantInt>(C))
16791679
// Must be non-zero due to null test above.
16801680
return true;
1681-
// TODO: Handle vectors
1681+
1682+
// For constant vectors, check that all elements are undefined or known
1683+
// non-zero to determine that the whole vector is known non-zero.
1684+
if (auto *VecTy = dyn_cast<VectorType>(C->getType())) {
1685+
for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
1686+
Constant *Elt = C->getAggregateElement(i);
1687+
if (!Elt || Elt->isNullValue())
1688+
return false;
1689+
if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
1690+
return false;
1691+
}
1692+
return true;
1693+
}
1694+
16821695
return false;
16831696
}
16841697

‎llvm/test/Transforms/InstSimplify/vec-cmp.ll

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -instsimplify -S | FileCheck %s
33

4-
; FIXME: isKnownNonZero should work for integer vectors.
5-
64
define <2 x i1> @nonzero_vec_splat(<2 x i32> %x) {
75
; CHECK-LABEL: @nonzero_vec_splat(
8-
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> %x, <i32 1, i32 1>
9-
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[Y]], zeroinitializer
10-
; CHECK-NEXT: ret <2 x i1> [[C]]
6+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
117
;
128
%y = or <2 x i32> %x, <i32 1, i32 1>
139
%c = icmp eq <2 x i32> %y, zeroinitializer
@@ -16,9 +12,7 @@ define <2 x i1> @nonzero_vec_splat(<2 x i32> %x) {
1612

1713
define <2 x i1> @nonzero_vec_nonsplat(<2 x i32> %x) {
1814
; CHECK-LABEL: @nonzero_vec_nonsplat(
19-
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> %x, <i32 2, i32 1>
20-
; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i32> [[Y]], zeroinitializer
21-
; CHECK-NEXT: ret <2 x i1> [[C]]
15+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
2216
;
2317
%y = or <2 x i32> %x, <i32 2, i32 1>
2418
%c = icmp ne <2 x i32> %y, zeroinitializer
@@ -27,9 +21,7 @@ define <2 x i1> @nonzero_vec_nonsplat(<2 x i32> %x) {
2721

2822
define <2 x i1> @nonzero_vec_undef_elt(<2 x i32> %x) {
2923
; CHECK-LABEL: @nonzero_vec_undef_elt(
30-
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> %x, <i32 undef, i32 1>
31-
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[Y]], zeroinitializer
32-
; CHECK-NEXT: ret <2 x i1> [[C]]
24+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
3325
;
3426
%y = or <2 x i32> %x, <i32 undef, i32 1>
3527
%c = icmp eq <2 x i32> %y, zeroinitializer
@@ -50,11 +42,7 @@ define <2 x i1> @may_be_zero_vec(<2 x i32> %x) {
5042
; Multiplies of non-zero numbers are non-zero if there is no unsigned overflow.
5143
define <2 x i1> @nonzero_vec_mul_nuw(<2 x i32> %x, <2 x i32> %y) {
5244
; CHECK-LABEL: @nonzero_vec_mul_nuw(
53-
; CHECK-NEXT: [[XNZ:%.*]] = or <2 x i32> %x, <i32 1, i32 2>
54-
; CHECK-NEXT: [[YNZ:%.*]] = or <2 x i32> %y, <i32 3, i32 undef>
55-
; CHECK-NEXT: [[M:%.*]] = mul nuw <2 x i32> [[XNZ]], [[YNZ]]
56-
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[M]], zeroinitializer
57-
; CHECK-NEXT: ret <2 x i1> [[C]]
45+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
5846
;
5947
%xnz = or <2 x i32> %x, <i32 1, i32 2>
6048
%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) {
6654
; Multiplies of non-zero numbers are non-zero if there is no signed overflow.
6755
define <2 x i1> @nonzero_vec_mul_nsw(<2 x i32> %x, <2 x i32> %y) {
6856
; CHECK-LABEL: @nonzero_vec_mul_nsw(
69-
; CHECK-NEXT: [[XNZ:%.*]] = or <2 x i32> %x, <i32 undef, i32 2>
70-
; CHECK-NEXT: [[YNZ:%.*]] = or <2 x i32> %y, <i32 3, i32 4>
71-
; CHECK-NEXT: [[M:%.*]] = mul nsw <2 x i32> [[XNZ]], [[YNZ]]
72-
; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i32> [[M]], zeroinitializer
73-
; CHECK-NEXT: ret <2 x i1> [[C]]
57+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
7458
;
7559
%xnz = or <2 x i32> %x, <i32 undef, i32 2>
7660
%ynz = or <2 x i32> %y, <i32 3, i32 4>

0 commit comments

Comments
 (0)