Skip to content

Commit 346e682

Browse files
committed
[ValueTracking] improve isKnownNonZero precision for smax
Instead of relying on known-bits for strictly positive, use the `isKnownPositive` API. This will use `isKnownNonZero` which is more accurate.
1 parent 0b65ee4 commit 346e682

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,21 +2861,18 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
28612861
isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q);
28622862
case Intrinsic::smin:
28632863
case Intrinsic::smax: {
2864-
auto KnownOpImpliesNonZero = [&](const KnownBits &K) {
2865-
return II->getIntrinsicID() == Intrinsic::smin
2866-
? K.isNegative()
2867-
: K.isStrictlyPositive();
2864+
bool AllNonZero = true;
2865+
auto KnownOpImpliesNonZero = [&](const Value *Op) {
2866+
KnownBits TmpKnown(getBitWidth(Op->getType(), Q.DL));
2867+
bool Ret =
2868+
II->getIntrinsicID() == Intrinsic::smin
2869+
? ::isKnownNegative(Op, DemandedElts, TmpKnown, Q, Depth)
2870+
: ::isKnownPositive(Op, DemandedElts, TmpKnown, Q, Depth);
2871+
AllNonZero &= TmpKnown.isNonZero();
2872+
return Ret;
28682873
};
2869-
KnownBits XKnown =
2870-
computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
2871-
if (KnownOpImpliesNonZero(XKnown))
2872-
return true;
2873-
KnownBits YKnown =
2874-
computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
2875-
if (KnownOpImpliesNonZero(YKnown))
2876-
return true;
2877-
2878-
if (XKnown.isNonZero() && YKnown.isNonZero())
2874+
if (KnownOpImpliesNonZero(II->getArgOperand(0)) ||
2875+
KnownOpImpliesNonZero(II->getArgOperand(1)) || AllNonZero)
28792876
return true;
28802877
}
28812878
[[fallthrough]];

llvm/test/Transforms/InstSimplify/known-non-zero.ll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,7 @@ B:
169169

170170
define i1 @smax_non_zero(i8 %xx, i8 %y) {
171171
; CHECK-LABEL: @smax_non_zero(
172-
; CHECK-NEXT: [[X0:%.*]] = and i8 [[XX:%.*]], 63
173-
; CHECK-NEXT: [[X:%.*]] = add i8 [[X0]], 1
174-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[Y:%.*]])
175-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0
176-
; CHECK-NEXT: ret i1 [[R]]
172+
; CHECK-NEXT: ret i1 false
177173
;
178174
%x0 = and i8 %xx, 63
179175
%x = add i8 %x0, 1

0 commit comments

Comments
 (0)