[llvm] [ValueTracking] improve `isKnownNonZero` precision for `smax` (PR #88170)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 9 22:29:43 PDT 2024
================
@@ -2828,23 +2828,47 @@ 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();
- };
- KnownBits XKnown =
+ // if either arg is strictly positive 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);
+ // Avoid re-computing isKnownNonZero if we already failed once.
+ bool OpsMaybeZero = false;
+ if (Op1Known.isNonNegative()) {
+ if (Op1Known.isNonZero() ||
+ isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q))
+ return true;
+ OpsMaybeZero = true;
----------------
nikic wrote:
Alternatively we could use something like this. Would that make things cleaner or more complicated?
```
std::optional<bool> Op0NonZero, Op1NonZero;
bool IsNonZero = [&](Value *Op, std::optional<bool> &NonZero) {
if (!NonZero)
NonZero = isKnownNonZero(Op, DemandedElts, Depth, Q);
return *NonZero;
};
// ...
IsNonZero(II->getArgOperand(0), Op1NonZero)
```
https://github.com/llvm/llvm-project/pull/88170
More information about the llvm-commits
mailing list