@@ -2828,23 +2828,44 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2828
2828
case Intrinsic::uadd_sat:
2829
2829
return isKnownNonZero (II->getArgOperand (1 ), DemandedElts, Depth, Q) ||
2830
2830
isKnownNonZero (II->getArgOperand (0 ), DemandedElts, Depth, Q);
2831
- case Intrinsic::smin:
2832
2831
case Intrinsic::smax: {
2833
- auto KnownOpImpliesNonZero = [&](const KnownBits &K) {
2834
- return II->getIntrinsicID () == Intrinsic::smin
2835
- ? K.isNegative ()
2836
- : K.isStrictlyPositive ();
2832
+ // If either arg is strictly positive the result is non-zero. Otherwise
2833
+ // the result is non-zero if both ops are non-zero.
2834
+ auto IsNonZero = [&](Value *Op, std::optional<bool > &OpNonZero,
2835
+ KnownBits OpKnown) {
2836
+ if (!OpNonZero.has_value ())
2837
+ OpNonZero = OpKnown.isNonZero () ||
2838
+ isKnownNonZero (Op, DemandedElts, Depth, Q);
2839
+ return *OpNonZero;
2837
2840
};
2838
- KnownBits XKnown =
2841
+ // Avoid re-computing isKnownNonZero.
2842
+ std::optional<bool > Op0NonZero, Op1NonZero;
2843
+ KnownBits Op1Known =
2844
+ computeKnownBits (II->getArgOperand (1 ), DemandedElts, Depth, Q);
2845
+ if (Op1Known.isNonNegative () &&
2846
+ IsNonZero (II->getArgOperand (1 ), Op1NonZero, Op1Known))
2847
+ return true ;
2848
+ KnownBits Op0Known =
2839
2849
computeKnownBits (II->getArgOperand (0 ), DemandedElts, Depth, Q);
2840
- if (KnownOpImpliesNonZero (XKnown))
2850
+ if (Op0Known.isNonNegative () &&
2851
+ IsNonZero (II->getArgOperand (0 ), Op0NonZero, Op0Known))
2841
2852
return true ;
2842
- KnownBits YKnown =
2853
+ return IsNonZero (II->getArgOperand (1 ), Op1NonZero, Op1Known) &&
2854
+ IsNonZero (II->getArgOperand (0 ), Op0NonZero, Op0Known);
2855
+ }
2856
+ case Intrinsic::smin: {
2857
+ // If either arg is negative the result is non-zero. Otherwise
2858
+ // the result is non-zero if both ops are non-zero.
2859
+ KnownBits Op1Known =
2843
2860
computeKnownBits (II->getArgOperand (1 ), DemandedElts, Depth, Q);
2844
- if (KnownOpImpliesNonZero (YKnown))
2861
+ if (Op1Known.isNegative ())
2862
+ return true ;
2863
+ KnownBits Op0Known =
2864
+ computeKnownBits (II->getArgOperand (0 ), DemandedElts, Depth, Q);
2865
+ if (Op0Known.isNegative ())
2845
2866
return true ;
2846
2867
2847
- if (XKnown .isNonZero () && YKnown .isNonZero ())
2868
+ if (Op1Known .isNonZero () && Op0Known .isNonZero ())
2848
2869
return true ;
2849
2870
}
2850
2871
[[fallthrough]];
0 commit comments