public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix make_range (PR middle-end/45262)
@ 2010-08-12 15:16 Jakub Jelinek
  2010-08-12 15:44 ` Richard Guenther
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2010-08-12 15:16 UTC (permalink / raw)
  To: gcc-patches

Hi!

The range folding code in fold-const.c expects ranges to have always
low <= high (with NULL being -inf resp. +inf), and for PLUS_EXPR/MINUS_EXPR
the code normalizes, but not so for NEGATE_EXPR.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux.
Ok for trunk/4.5/4.4?

2010-08-12  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/45262
	* fold-const.c (make_range) <case NEGATE_EXPR>: Punt if
	-a overflows.  Normalize the range.

	* gcc.c-torture/execute/pr45262.c: New test.

--- gcc/fold-const.c.jj	2010-07-09 13:44:24.000000000 +0200
+++ gcc/fold-const.c	2010-08-12 13:44:42.000000000 +0200
@@ -3985,9 +3985,9 @@ make_range (tree exp, int *pin_p, tree *
 	  n_high = range_binop (MINUS_EXPR, exp_type,
 				build_int_cst (exp_type, 0),
 				0, low, 0);
-	  low = n_low, high = n_high;
-	  exp = arg0;
-	  continue;
+	  if (n_high != 0 && TREE_OVERFLOW (n_high))
+	    break;
+	  goto normalize;
 
 	case BIT_NOT_EXPR:
 	  /* ~ X -> -X - 1  */
@@ -4021,6 +4021,7 @@ make_range (tree exp, int *pin_p, tree *
 	  if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
 	    *strict_overflow_p = true;
 
+	normalize:
 	  /* Check for an unsigned range which has wrapped around the maximum
 	     value thus making n_high < n_low, and normalize it.  */
 	  if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
--- gcc/testsuite/gcc.c-torture/execute/pr45262.c.jj	2010-08-12 13:51:49.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr45262.c	2010-08-12 13:51:21.000000000 +0200
@@ -0,0 +1,33 @@
+/* PR middle-end/45262 */
+
+extern void abort (void);
+
+int
+foo (unsigned int x)
+{
+  return ((int) x < 0) || ((int) (-x) < 0);
+}
+
+int
+bar (unsigned int x)
+{
+  return x >> 31 || (-x) >> 31;
+}
+
+int
+main (void)
+{
+  if (foo (1) != 1)
+    abort ();
+  if (foo (0) != 0)
+    abort ();
+  if (foo (-1) != 1)
+    abort ();
+  if (bar (1) != 1)
+    abort ();
+  if (bar (0) != 0)
+    abort ();
+  if (bar (-1) != 1)
+    abort ();
+  return 0;
+}

	Jakub

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix make_range (PR middle-end/45262)
  2010-08-12 15:16 [PATCH] Fix make_range (PR middle-end/45262) Jakub Jelinek
@ 2010-08-12 15:44 ` Richard Guenther
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Guenther @ 2010-08-12 15:44 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On Thu, Aug 12, 2010 at 4:35 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> The range folding code in fold-const.c expects ranges to have always
> low <= high (with NULL being -inf resp. +inf), and for PLUS_EXPR/MINUS_EXPR
> the code normalizes, but not so for NEGATE_EXPR.
>
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux.
> Ok for trunk/4.5/4.4?

Ok.

Thanks,
Richard.

> 2010-08-12  Jakub Jelinek  <jakub@redhat.com>
>
>        PR middle-end/45262
>        * fold-const.c (make_range) <case NEGATE_EXPR>: Punt if
>        -a overflows.  Normalize the range.
>
>        * gcc.c-torture/execute/pr45262.c: New test.
>
> --- gcc/fold-const.c.jj 2010-07-09 13:44:24.000000000 +0200
> +++ gcc/fold-const.c    2010-08-12 13:44:42.000000000 +0200
> @@ -3985,9 +3985,9 @@ make_range (tree exp, int *pin_p, tree *
>          n_high = range_binop (MINUS_EXPR, exp_type,
>                                build_int_cst (exp_type, 0),
>                                0, low, 0);
> -         low = n_low, high = n_high;
> -         exp = arg0;
> -         continue;
> +         if (n_high != 0 && TREE_OVERFLOW (n_high))
> +           break;
> +         goto normalize;
>
>        case BIT_NOT_EXPR:
>          /* ~ X -> -X - 1  */
> @@ -4021,6 +4021,7 @@ make_range (tree exp, int *pin_p, tree *
>          if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
>            *strict_overflow_p = true;
>
> +       normalize:
>          /* Check for an unsigned range which has wrapped around the maximum
>             value thus making n_high < n_low, and normalize it.  */
>          if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
> --- gcc/testsuite/gcc.c-torture/execute/pr45262.c.jj    2010-08-12 13:51:49.000000000 +0200
> +++ gcc/testsuite/gcc.c-torture/execute/pr45262.c       2010-08-12 13:51:21.000000000 +0200
> @@ -0,0 +1,33 @@
> +/* PR middle-end/45262 */
> +
> +extern void abort (void);
> +
> +int
> +foo (unsigned int x)
> +{
> +  return ((int) x < 0) || ((int) (-x) < 0);
> +}
> +
> +int
> +bar (unsigned int x)
> +{
> +  return x >> 31 || (-x) >> 31;
> +}
> +
> +int
> +main (void)
> +{
> +  if (foo (1) != 1)
> +    abort ();
> +  if (foo (0) != 0)
> +    abort ();
> +  if (foo (-1) != 1)
> +    abort ();
> +  if (bar (1) != 1)
> +    abort ();
> +  if (bar (0) != 0)
> +    abort ();
> +  if (bar (-1) != 1)
> +    abort ();
> +  return 0;
> +}
>
>        Jakub
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-08-12 15:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-12 15:16 [PATCH] Fix make_range (PR middle-end/45262) Jakub Jelinek
2010-08-12 15:44 ` Richard Guenther

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).