Skip to content

Commit 5eedfff

Browse files
committed
[ValueTracking] Add additional cases for isKnownNonZero(mul X, Y)
If either `X` or `Y` is odd and the other is non-zero, the result is non-zero. Alive2 Link: https://alive2.llvm.org/ce/z/9V7-es Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D149418
1 parent d840391 commit 5eedfff

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,11 +2872,24 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
28722872
// If X and Y are non-zero then so is X * Y as long as the multiplication
28732873
// does not overflow.
28742874
const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
2875-
if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
2876-
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) &&
2877-
isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
2878-
return true;
2879-
break;
2875+
if (Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO))
2876+
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) &&
2877+
isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);
2878+
2879+
// If either X or Y is odd, then if the other is non-zero the result can't
2880+
// be zero.
2881+
KnownBits XKnown =
2882+
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2883+
if (XKnown.One[0])
2884+
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);
2885+
2886+
KnownBits YKnown =
2887+
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2888+
if (YKnown.One[0])
2889+
return XKnown.isNonZero() ||
2890+
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
2891+
2892+
return KnownBits::mul(XKnown, YKnown).isNonZero();
28802893
}
28812894
case Instruction::Select:
28822895
// (C ? X : Y) != 0 if X != 0 and Y != 0.

llvm/test/Analysis/ValueTracking/known-non-zero.ll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,13 +767,9 @@ define i1 @cttz_nonzero_fail_maybe_odd(i8 %xx, i8 %cnt, i8 %ind) {
767767

768768
define i1 @mul_nonzero_odd(i8 %xx, i8 %y, i8 %ind) {
769769
; CHECK-LABEL: @mul_nonzero_odd(
770-
; CHECK-NEXT: [[XO:%.*]] = or i8 [[XX:%.*]], 1
771770
; CHECK-NEXT: [[Y_NZ:%.*]] = icmp ne i8 [[Y:%.*]], 0
772771
; CHECK-NEXT: call void @llvm.assume(i1 [[Y_NZ]])
773-
; CHECK-NEXT: [[X:%.*]] = mul i8 [[XO]], [[Y]]
774-
; CHECK-NEXT: [[Z:%.*]] = or i8 [[X]], [[IND:%.*]]
775-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[Z]], 0
776-
; CHECK-NEXT: ret i1 [[R]]
772+
; CHECK-NEXT: ret i1 false
777773
;
778774
%xo = or i8 %xx, 1
779775
%y_nz = icmp ne i8 %y, 0

0 commit comments

Comments
 (0)