Skip to content

Commit 52a1998

Browse files
committed
[ValueTracking] Don't accept undef in isKnownNonZero()
As the undef can be replaced with a zero value, this is not legal in the general case. We can only allow poison values. This matches what the other ValueTracking helpers like computeKnownBits() do.
1 parent c7902d8 commit 52a1998

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,7 +3034,7 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
30343034
// Must be non-zero due to null test above.
30353035
return true;
30363036

3037-
// For constant vectors, check that all elements are undefined or known
3037+
// For constant vectors, check that all elements are poison or known
30383038
// non-zero to determine that the whole vector is known non-zero.
30393039
if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
30403040
for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
@@ -3043,7 +3043,7 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
30433043
Constant *Elt = C->getAggregateElement(i);
30443044
if (!Elt || Elt->isNullValue())
30453045
return false;
3046-
if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
3046+
if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
30473047
return false;
30483048
}
30493049
return true;

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3093,10 +3093,14 @@ define <2 x i32> @dec_zext_add_nonzero_vec(<2 x i8> %x) {
30933093
ret <2 x i32> %c
30943094
}
30953095

3096+
; Negative test: Folding this with undef is not safe.
3097+
30963098
define <2 x i32> @dec_zext_add_nonzero_vec_undef0(<2 x i8> %x) {
30973099
; CHECK-LABEL: @dec_zext_add_nonzero_vec_undef0(
30983100
; CHECK-NEXT: [[O:%.*]] = or <2 x i8> [[X:%.*]], <i8 8, i8 undef>
3099-
; CHECK-NEXT: [[C:%.*]] = zext <2 x i8> [[O]] to <2 x i32>
3101+
; CHECK-NEXT: [[A:%.*]] = add <2 x i8> [[O]], <i8 -1, i8 -1>
3102+
; CHECK-NEXT: [[B:%.*]] = zext <2 x i8> [[A]] to <2 x i32>
3103+
; CHECK-NEXT: [[C:%.*]] = add nuw nsw <2 x i32> [[B]], <i32 1, i32 1>
31003104
; CHECK-NEXT: ret <2 x i32> [[C]]
31013105
;
31023106
%o = or <2 x i8> %x, <i8 8, i8 undef>

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ define <2 x i1> @nonzero_vec_nonsplat(<2 x i32> %x) {
2121

2222
define <2 x i1> @nonzero_vec_undef_elt(<2 x i32> %x) {
2323
; CHECK-LABEL: @nonzero_vec_undef_elt(
24-
; CHECK-NEXT: ret <2 x i1> zeroinitializer
24+
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> [[X:%.*]], <i32 undef, i32 1>
25+
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[Y]], zeroinitializer
26+
; CHECK-NEXT: ret <2 x i1> [[C]]
2527
;
2628
%y = or <2 x i32> %x, <i32 undef, i32 1>
2729
%c = icmp eq <2 x i32> %y, zeroinitializer
2830
ret <2 x i1> %c
2931
}
3032

33+
define <2 x i1> @nonzero_vec_poison_elt(<2 x i32> %x) {
34+
; CHECK-LABEL: @nonzero_vec_poison_elt(
35+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
36+
;
37+
%y = or <2 x i32> %x, <i32 poison, i32 1>
38+
%c = icmp eq <2 x i32> %y, zeroinitializer
39+
ret <2 x i1> %c
40+
}
41+
3142
define <2 x i1> @may_be_zero_vec(<2 x i32> %x) {
3243
; CHECK-LABEL: @may_be_zero_vec(
3344
; CHECK-NEXT: [[Y:%.*]] = or <2 x i32> [[X:%.*]], <i32 0, i32 1>
@@ -45,7 +56,7 @@ define <2 x i1> @nonzero_vec_mul_nuw(<2 x i32> %x, <2 x i32> %y) {
4556
; CHECK-NEXT: ret <2 x i1> zeroinitializer
4657
;
4758
%xnz = or <2 x i32> %x, <i32 1, i32 2>
48-
%ynz = or <2 x i32> %y, <i32 3, i32 undef>
59+
%ynz = or <2 x i32> %y, <i32 3, i32 poison>
4960
%m = mul nuw <2 x i32> %xnz, %ynz
5061
%c = icmp eq <2 x i32> %m, zeroinitializer
5162
ret <2 x i1> %c
@@ -56,7 +67,7 @@ define <2 x i1> @nonzero_vec_mul_nsw(<2 x i32> %x, <2 x i32> %y) {
5667
; CHECK-LABEL: @nonzero_vec_mul_nsw(
5768
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
5869
;
59-
%xnz = or <2 x i32> %x, <i32 undef, i32 2>
70+
%xnz = or <2 x i32> %x, <i32 poison, i32 2>
6071
%ynz = or <2 x i32> %y, <i32 3, i32 4>
6172
%m = mul nsw <2 x i32> %xnz, %ynz
6273
%c = icmp ne <2 x i32> %m, zeroinitializer

0 commit comments

Comments
 (0)