[patch] Fix PR tree-optimization/80426

Jakub Jelinek jakub@redhat.com
Wed Apr 19 09:56:00 GMT 2017


On Wed, Apr 19, 2017 at 08:20:40AM +0200, Eric Botcazou wrote:
> --- tree-vrp.c	(revision 246960)
> +++ tree-vrp.c	(working copy)
> @@ -2461,7 +2461,19 @@ extract_range_from_binary_expr_1 (value_
>  	  else if (min_op0)
>  	    wmin = min_op0;
>  	  else if (min_op1)
> -	    wmin = minus_p ? wi::neg (min_op1) : min_op1;
> +	    {
> +	      if (minus_p)
> +		{
> +		  wmin = wi::neg (min_op1);
> +
> +		  /* Check for overflow.  */
> +		  if (wi::cmp (0, min_op1, sgn)
> +		      != wi::cmp (wmin, 0, sgn))

I know this attempts to be a copy of what is used elsewhere, but
at least there it is a result of wi::sub etc.
Wouldn't it be simpler to
  if (sgn == SIGNED && wi::neg_p (min_op1) && wi::neg_p (wmin))
    min_ovf = 1;
  else if (sgn == UNSIGNED && wi::ne_p (min_op1, 0))
    min_ovf = -1;

I mean, for SIGNED if min_op1 is 0, then wmin is 0 to and we want
min_ovf = 0;
If it is positive, wmin will be surely negative and again we want
min_ovf = 0.  Only if it is negative and its negation is negative
too we want min_ovf = 1 (i.e. wi::cmps (0, most_negative) result).
For UNSIGNED, if min_op1 is 0, again all 3 wi::cmp will yield
0 and min_ovf = 0.  If it is non-zero, it is > 0, therefore it
the first wi::cmp will return -1, the second wi::cmp returns
1 and the third one -1.

Is that what we want (e.g. the UNSIGNED case to overflow pretty much always
except for 0 which should be optimized away anyway)?

Or, shouldn't we just set if (!min_op0 && min_op1 && minus_p) min_op0 =
build_int_cst (expr_type, 0); before the if (min_op0 && min_op1) case
and don't duplicate that?

	Jakub



More information about the Gcc-patches mailing list