@@ -2870,23 +2870,44 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2870
2870
case Intrinsic::uadd_sat:
2871
2871
return isKnownNonZero (II->getArgOperand (1 ), DemandedElts, Depth, Q) ||
2872
2872
isKnownNonZero (II->getArgOperand (0 ), DemandedElts, Depth, Q);
2873
- case Intrinsic::smin:
2874
2873
case Intrinsic::smax: {
2875
- auto KnownOpImpliesNonZero = [&](const KnownBits &K) {
2876
- return II->getIntrinsicID () == Intrinsic::smin
2877
- ? K.isNegative ()
2878
- : K.isStrictlyPositive ();
2874
+ // If either arg is strictly positive the result is non-zero. Otherwise
2875
+ // the result is non-zero if both ops are non-zero.
2876
+ auto IsNonZero = [&](Value *Op, std::optional<bool > &OpNonZero,
2877
+ const KnownBits &OpKnown) {
2878
+ if (!OpNonZero.has_value ())
2879
+ OpNonZero = OpKnown.isNonZero () ||
2880
+ isKnownNonZero (Op, DemandedElts, Depth, Q);
2881
+ return *OpNonZero;
2879
2882
};
2880
- KnownBits XKnown =
2883
+ // Avoid re-computing isKnownNonZero.
2884
+ std::optional<bool > Op0NonZero, Op1NonZero;
2885
+ KnownBits Op1Known =
2886
+ computeKnownBits (II->getArgOperand (1 ), DemandedElts, Depth, Q);
2887
+ if (Op1Known.isNonNegative () &&
2888
+ IsNonZero (II->getArgOperand (1 ), Op1NonZero, Op1Known))
2889
+ return true ;
2890
+ KnownBits Op0Known =
2881
2891
computeKnownBits (II->getArgOperand (0 ), DemandedElts, Depth, Q);
2882
- if (KnownOpImpliesNonZero (XKnown))
2892
+ if (Op0Known.isNonNegative () &&
2893
+ IsNonZero (II->getArgOperand (0 ), Op0NonZero, Op0Known))
2883
2894
return true ;
2884
- KnownBits YKnown =
2895
+ return IsNonZero (II->getArgOperand (1 ), Op1NonZero, Op1Known) &&
2896
+ IsNonZero (II->getArgOperand (0 ), Op0NonZero, Op0Known);
2897
+ }
2898
+ case Intrinsic::smin: {
2899
+ // If either arg is negative the result is non-zero. Otherwise
2900
+ // the result is non-zero if both ops are non-zero.
2901
+ KnownBits Op1Known =
2885
2902
computeKnownBits (II->getArgOperand (1 ), DemandedElts, Depth, Q);
2886
- if (KnownOpImpliesNonZero (YKnown))
2903
+ if (Op1Known.isNegative ())
2904
+ return true ;
2905
+ KnownBits Op0Known =
2906
+ computeKnownBits (II->getArgOperand (0 ), DemandedElts, Depth, Q);
2907
+ if (Op0Known.isNegative ())
2887
2908
return true ;
2888
2909
2889
- if (XKnown .isNonZero () && YKnown .isNonZero ())
2910
+ if (Op1Known .isNonZero () && Op0Known .isNonZero ())
2890
2911
return true ;
2891
2912
}
2892
2913
[[fallthrough]];
0 commit comments