[PATCH] Handle overflow in dependence analysis lambda ops gracefully

Jakub Jelinek jakub@redhat.com
Wed Jan 20 11:33:58 GMT 2021


On Wed, Jan 20, 2021 at 12:29:23PM +0100, Richard Biener wrote:
> +/* Compute the product of signed A and B and indicate in *OVERFLOW whether
> +   that operation overflowed.  */
> +
> +inline HOST_WIDE_INT
> +mul_hwi (HOST_WIDE_INT a, HOST_WIDE_INT b, bool *overflow)
> +{
> +  unsigned HOST_WIDE_INT result = a * (unsigned HOST_WIDE_INT)b;
> +  if (a != 0 && (HOST_WIDE_INT)result / a != b)
> +    *overflow = true;
> +  else
> +    *overflow = false;

This isn't sufficiently bulletproof.
It should be
  if ((a == -1 && b == HOST_WIDE_INT_MIN)
      || (a != 0 && (HOST_WIDE_INT)result / a != b))
    *overflow = true;
  else
    *overflow = false;
or so.

And except that a == -1 && b == min checks my recent widening_mul changes
should match that to __builtin_mul_overflow, so it will not be perfect code,
but at least will not be unnecessarily slow on x86.

	Jakub



More information about the Gcc-patches mailing list