Patchwork Fix VRP MULT_EXPR handling (PR tree-optimization/54471)

login
register
mail settings
Submitter Jakub Jelinek
Date Nov. 21, 2012, 8:38 p.m.
Message ID <20121121203828.GI2315@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/200855/
State New
Headers show

Comments

Jakub Jelinek - Nov. 21, 2012, 8:38 p.m.
Hi!

If a type has 2 * HWI precision, sizem1 is maximum double_int (all ones)
and thus size = sizem1 + double_int_one overflows into 0.  If either min0
or min1 is also zero, we might wrongly canonicalize the range into a signed
one.  Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
ok for trunk?

2012-11-21  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/54471
	* tree-vrp.c (extract_range_from_binary_expr_1): For MULT_EXPR,
	don't canonicalize range if min2 is zero.

	* gcc.dg/tree-ssa/vrp86.c: New test.
	* gcc.c-torture/execute/pr54471.c: New test.


	Jakub
Richard Guenther - Nov. 25, 2012, 1:27 p.m.
On Wed, Nov 21, 2012 at 9:38 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> If a type has 2 * HWI precision, sizem1 is maximum double_int (all ones)
> and thus size = sizem1 + double_int_one overflows into 0.  If either min0
> or min1 is also zero, we might wrongly canonicalize the range into a signed
> one.  Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
> ok for trunk?

Ok.

Thanks,
Richard.

> 2012-11-21  Jakub Jelinek  <jakub@redhat.com>
>
>         PR tree-optimization/54471
>         * tree-vrp.c (extract_range_from_binary_expr_1): For MULT_EXPR,
>         don't canonicalize range if min2 is zero.
>
>         * gcc.dg/tree-ssa/vrp86.c: New test.
>         * gcc.c-torture/execute/pr54471.c: New test.
>
> --- gcc/tree-vrp.c.jj   2012-11-21 16:00:05.000085079 +0100
> +++ gcc/tree-vrp.c      2012-11-21 18:24:58.385290035 +0100
> @@ -2653,7 +2653,7 @@ extract_range_from_binary_expr_1 (value_
>           if (TYPE_UNSIGNED (expr_type))
>             {
>               double_int min2 = size - min0;
> -             if (min2.cmp (max0, true) < 0)
> +             if (!min2.is_zero () && min2.cmp (max0, true) < 0)
>                 {
>                   min0 = -min2;
>                   max0 -= size;
> @@ -2661,7 +2661,7 @@ extract_range_from_binary_expr_1 (value_
>                 }
>
>               min2 = size - min1;
> -             if (min2.cmp (max1, true) < 0)
> +             if (!min2.is_zero () && min2.cmp (max1, true) < 0)
>                 {
>                   min1 = -min2;
>                   max1 -= size;
> --- gcc/testsuite/gcc.dg/tree-ssa/vrp86.c.jj    2012-11-21 18:27:53.389280736 +0100
> +++ gcc/testsuite/gcc.dg/tree-ssa/vrp86.c       2012-11-21 18:27:47.000000000 +0100
> @@ -0,0 +1,28 @@
> +/* PR tree-optimization/54471 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-vrp1" } */
> +
> +#ifdef __SIZEOF_INT128__
> +#define T __int128
> +#else
> +#define T long long
> +#endif
> +
> +void fn1call (void);
> +void fn2call (void);
> +
> +void
> +foo (unsigned T x)
> +{
> +  if (x > (unsigned T) -3)
> +    return;
> +  unsigned T y = 2 * x;
> +  if (y == 42)
> +    fn1call ();
> +  else
> +    fn2call ();
> +}
> +
> +/* { dg-final { scan-tree-dump "fn1call" "vrp1"} } */
> +/* { dg-final { scan-tree-dump "fn2call" "vrp1"} } */
> +/* { dg-final { cleanup-tree-dump "vrp1" } } */
> --- gcc/testsuite/gcc.c-torture/execute/pr54471.c.jj    2012-11-21 18:24:58.386289954 +0100
> +++ gcc/testsuite/gcc.c-torture/execute/pr54471.c       2012-11-21 18:24:58.386289954 +0100
> @@ -0,0 +1,36 @@
> +/* PR tree-optimization/54471 */
> +
> +#ifdef __SIZEOF_INT128__
> +#define T __int128
> +#else
> +#define T long long
> +#endif
> +
> +extern void abort (void);
> +
> +__attribute__ ((noinline))
> +unsigned T
> +foo (T ixi, unsigned ctr)
> +{
> +  unsigned T irslt = 1;
> +  T ix = ixi;
> +
> +  for (; ctr; ctr--)
> +    {
> +      irslt *= ix;
> +      ix *= ix;
> +    }
> +
> +  if (irslt != 14348907)
> +    abort ();
> +  return irslt;
> +}
> +
> +int
> +main ()
> +{
> +  unsigned T res;
> +
> +  res = foo (3, 4);
> +  return 0;
> +}
>
>         Jakub

Patch

--- gcc/tree-vrp.c.jj	2012-11-21 16:00:05.000085079 +0100
+++ gcc/tree-vrp.c	2012-11-21 18:24:58.385290035 +0100
@@ -2653,7 +2653,7 @@  extract_range_from_binary_expr_1 (value_
 	  if (TYPE_UNSIGNED (expr_type))
 	    {
 	      double_int min2 = size - min0;
-	      if (min2.cmp (max0, true) < 0)
+	      if (!min2.is_zero () && min2.cmp (max0, true) < 0)
 		{
 		  min0 = -min2;
 		  max0 -= size;
@@ -2661,7 +2661,7 @@  extract_range_from_binary_expr_1 (value_
 		}
 
 	      min2 = size - min1;
-	      if (min2.cmp (max1, true) < 0)
+	      if (!min2.is_zero () && min2.cmp (max1, true) < 0)
 		{
 		  min1 = -min2;
 		  max1 -= size;
--- gcc/testsuite/gcc.dg/tree-ssa/vrp86.c.jj	2012-11-21 18:27:53.389280736 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/vrp86.c	2012-11-21 18:27:47.000000000 +0100
@@ -0,0 +1,28 @@ 
+/* PR tree-optimization/54471 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-vrp1" } */
+
+#ifdef __SIZEOF_INT128__
+#define T __int128
+#else
+#define T long long
+#endif
+
+void fn1call (void);
+void fn2call (void);
+
+void
+foo (unsigned T x)
+{
+  if (x > (unsigned T) -3)
+    return;
+  unsigned T y = 2 * x;
+  if (y == 42)
+    fn1call ();
+  else
+    fn2call ();
+}
+
+/* { dg-final { scan-tree-dump "fn1call" "vrp1"} } */
+/* { dg-final { scan-tree-dump "fn2call" "vrp1"} } */
+/* { dg-final { cleanup-tree-dump "vrp1" } } */
--- gcc/testsuite/gcc.c-torture/execute/pr54471.c.jj	2012-11-21 18:24:58.386289954 +0100
+++ gcc/testsuite/gcc.c-torture/execute/pr54471.c	2012-11-21 18:24:58.386289954 +0100
@@ -0,0 +1,36 @@ 
+/* PR tree-optimization/54471 */
+
+#ifdef __SIZEOF_INT128__
+#define T __int128
+#else
+#define T long long
+#endif
+
+extern void abort (void);
+
+__attribute__ ((noinline))
+unsigned T
+foo (T ixi, unsigned ctr)
+{
+  unsigned T irslt = 1;
+  T ix = ixi;
+
+  for (; ctr; ctr--)
+    {
+      irslt *= ix;
+      ix *= ix;
+    }
+
+  if (irslt != 14348907)
+    abort ();
+  return irslt;
+}
+
+int
+main ()
+{
+  unsigned T res;
+
+  res = foo (3, 4);
+  return 0;
+}