[llvm] f1ee458 - [ValueTracking] improve `isKnownNonZero` precision for `smax`
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 10 08:41:13 PDT 2024
Author: Noah Goldstein
Date: 2024-04-10T10:40:49-05:00
New Revision: f1ee458ddb45c9887b3df583ce9a4ba12aae8b3b
URL: https://github.com/llvm/llvm-project/commit/f1ee458ddb45c9887b3df583ce9a4ba12aae8b3b
DIFF: https://github.com/llvm/llvm-project/commit/f1ee458ddb45c9887b3df583ce9a4ba12aae8b3b.diff
LOG: [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.
Closes #88170
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstSimplify/known-non-zero.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index b32dc493ace919..5ef1969893b425 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2870,23 +2870,44 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
case Intrinsic::uadd_sat:
return isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q) ||
isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q);
- case Intrinsic::smin:
case Intrinsic::smax: {
- auto KnownOpImpliesNonZero = [&](const KnownBits &K) {
- return II->getIntrinsicID() == Intrinsic::smin
- ? K.isNegative()
- : K.isStrictlyPositive();
+ // If either arg is strictly positive the result is non-zero. Otherwise
+ // the result is non-zero if both ops are non-zero.
+ auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
+ const KnownBits &OpKnown) {
+ if (!OpNonZero.has_value())
+ OpNonZero = OpKnown.isNonZero() ||
+ isKnownNonZero(Op, DemandedElts, Depth, Q);
+ return *OpNonZero;
};
- KnownBits XKnown =
+ // Avoid re-computing isKnownNonZero.
+ std::optional<bool> Op0NonZero, Op1NonZero;
+ KnownBits Op1Known =
+ computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
+ if (Op1Known.isNonNegative() &&
+ IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
+ return true;
+ KnownBits Op0Known =
computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
- if (KnownOpImpliesNonZero(XKnown))
+ if (Op0Known.isNonNegative() &&
+ IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
return true;
- KnownBits YKnown =
+ return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
+ IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
+ }
+ case Intrinsic::smin: {
+ // If either arg is negative the result is non-zero. Otherwise
+ // the result is non-zero if both ops are non-zero.
+ KnownBits Op1Known =
computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
- if (KnownOpImpliesNonZero(YKnown))
+ if (Op1Known.isNegative())
+ return true;
+ KnownBits Op0Known =
+ computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
+ if (Op0Known.isNegative())
return true;
- if (XKnown.isNonZero() && YKnown.isNonZero())
+ if (Op1Known.isNonZero() && Op0Known.isNonZero())
return true;
}
[[fallthrough]];
diff --git a/llvm/test/Transforms/InstSimplify/known-non-zero.ll b/llvm/test/Transforms/InstSimplify/known-non-zero.ll
index 6ebc4e0f31a9cf..51f80f62c2f34c 100644
--- a/llvm/test/Transforms/InstSimplify/known-non-zero.ll
+++ b/llvm/test/Transforms/InstSimplify/known-non-zero.ll
@@ -169,11 +169,7 @@ B:
define i1 @smax_non_zero(i8 %xx, i8 %y) {
; CHECK-LABEL: @smax_non_zero(
-; CHECK-NEXT: [[X0:%.*]] = and i8 [[XX:%.*]], 63
-; CHECK-NEXT: [[X:%.*]] = add i8 [[X0]], 1
-; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%x0 = and i8 %xx, 63
%x = add i8 %x0, 1
More information about the llvm-commits
mailing list