Patchwork Fix build_range_check (PR tree-optimization/58385)

login
register
mail settings
Submitter Jakub Jelinek
Date Sept. 10, 2013, 9:33 p.m.
Message ID <20130910213346.GK1817@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/274075/
State New
Headers show

Comments

Jakub Jelinek - Sept. 10, 2013, 9:33 p.m.
Hi!

If exp has side effects, for [-,-] ranges we would optimize them away.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk/4.8?

2013-09-10  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/58385
	* fold-const.c (build_range_check): If both low and high are NULL,
	use omit_one_operand_loc to preserve exp side-effects.

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


	Jakub
Richard Guenther - Sept. 11, 2013, 7:38 a.m.
On Tue, 10 Sep 2013, Jakub Jelinek wrote:

> Hi!
> 
> If exp has side effects, for [-,-] ranges we would optimize them away.
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> trunk/4.8?

Ok.

Thanks,
Richard.

> 2013-09-10  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR tree-optimization/58385
> 	* fold-const.c (build_range_check): If both low and high are NULL,
> 	use omit_one_operand_loc to preserve exp side-effects.
> 
> 	* gcc.c-torture/execute/pr58385.c: New test.
> 
> --- gcc/fold-const.c.jj	2013-09-09 11:32:39.000000000 +0200
> +++ gcc/fold-const.c	2013-09-10 20:48:12.018170411 +0200
> @@ -4299,7 +4299,7 @@ build_range_check (location_t loc, tree
>      }
>  
>    if (low == 0 && high == 0)
> -    return build_int_cst (type, 1);
> +    return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
>  
>    if (low == 0)
>      return fold_build2_loc (loc, LE_EXPR, type, exp,
> --- gcc/testsuite/gcc.c-torture/execute/pr58385.c.jj	2013-09-10 20:50:02.909589473 +0200
> +++ gcc/testsuite/gcc.c-torture/execute/pr58385.c	2013-09-10 20:48:38.000000000 +0200
> @@ -0,0 +1,21 @@
> +/* PR tree-optimization/58385 */
> +
> +extern void abort (void);
> +
> +int a, b = 1;
> +
> +int
> +foo ()
> +{
> +  b = 0;
> +  return 0;
> +}
> +
> +int
> +main ()
> +{
> +  ((0 || a) & foo () >= 0) <= 1 && 1;
> +  if (b)
> +    abort ();
> +  return 0;
> +}
> 
> 	Jakub
> 
>

Patch

--- gcc/fold-const.c.jj	2013-09-09 11:32:39.000000000 +0200
+++ gcc/fold-const.c	2013-09-10 20:48:12.018170411 +0200
@@ -4299,7 +4299,7 @@  build_range_check (location_t loc, tree
     }
 
   if (low == 0 && high == 0)
-    return build_int_cst (type, 1);
+    return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
 
   if (low == 0)
     return fold_build2_loc (loc, LE_EXPR, type, exp,
--- gcc/testsuite/gcc.c-torture/execute/pr58385.c.jj	2013-09-10 20:50:02.909589473 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr58385.c	2013-09-10 20:48:38.000000000 +0200
@@ -0,0 +1,21 @@ 
+/* PR tree-optimization/58385 */
+
+extern void abort (void);
+
+int a, b = 1;
+
+int
+foo ()
+{
+  b = 0;
+  return 0;
+}
+
+int
+main ()
+{
+  ((0 || a) & foo () >= 0) <= 1 && 1;
+  if (b)
+    abort ();
+  return 0;
+}