diff mbox

Fix division-by-unsigned optimization in VRP (PR tree-optimization/68431)

Message ID 20151119145703.GH21807@redhat.com
State New
Headers show

Commit Message

Marek Polacek Nov. 19, 2015, 2:57 p.m. UTC
This fixes a failure to optimize division by an unsigned.  The comment before
the condition I'm fixing says "When vr0.max < 0, vr1.min != 0 and ..." but
"&& !compare_values (vr1.min, zero)" actually ensures that vr1.min is zero.
(Indeed, the following int_const_binop would attemp to divide by zero and
return NULL_TREE.)

With this, (a / x) gets a correct range [-2, 0] and we're able to optimize
the condition away, thus even the abort () call.  (This won't work on targets
where long long == int.)

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-11-19  Marek Polacek  <polacek@redhat.com>

	PR tree-optimization/68431
	* tree-vrp.c (extract_range_from_binary_expr_1): Fix condition.

	* gcc.dg/tree-ssa/pr68431.c: New test.


	Marek

Comments

Richard Biener Nov. 19, 2015, 3 p.m. UTC | #1
On November 19, 2015 3:57:03 PM GMT+01:00, Marek Polacek <polacek@redhat.com> wrote:
>This fixes a failure to optimize division by an unsigned.  The comment
>before
>the condition I'm fixing says "When vr0.max < 0, vr1.min != 0 and ..."
>but
>"&& !compare_values (vr1.min, zero)" actually ensures that vr1.min is
>zero.
>(Indeed, the following int_const_binop would attemp to divide by zero
>and
>return NULL_TREE.)
>
>With this, (a / x) gets a correct range [-2, 0] and we're able to
>optimize
>the condition away, thus even the abort () call.  (This won't work on
>targets
>where long long == int.)
>
>Bootstrapped/regtested on x86_64-linux, ok for trunk?

OK.

Richard.

>2015-11-19  Marek Polacek  <polacek@redhat.com>
>
>	PR tree-optimization/68431
>	* tree-vrp.c (extract_range_from_binary_expr_1): Fix condition.
>
>	* gcc.dg/tree-ssa/pr68431.c: New test.
>
>diff --git gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
>gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
>index e69de29..3bd3843 100644
>--- gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
>+++ gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
>@@ -0,0 +1,16 @@
>+/* PR tree-optimization/68431 */
>+/* { dg-options "-O2 -fdump-tree-vrp1-details" } */
>+
>+unsigned int x = 1;
>+int
>+main (void)
>+{
>+  long long int a = -2LL;
>+  int t = 1 <= (a / x);
>+  if (t != 0)
>+    __builtin_abort ();
>+
>+  return 0;
>+}
>+
>+/* { dg-final { scan-tree-dump-times "Folding predicate .*to 0" 1
>"vrp1" } } */
>diff --git gcc/tree-vrp.c gcc/tree-vrp.c
>index e67048e..736082b 100644
>--- gcc/tree-vrp.c
>+++ gcc/tree-vrp.c
>@@ -2975,7 +2975,7 @@ extract_range_from_binary_expr_1 (value_range
>*vr,
> 		  if (vr1.type == VR_RANGE
> 		      && !symbolic_range_p (&vr0)
> 		      && !symbolic_range_p (&vr1)
>-		      && !compare_values (vr1.min, zero))
>+		      && compare_values (vr1.min, zero) != 0)
> 		    max = int_const_binop (code, vr0.max, vr1.min);
> 		  else
> 		    max = zero;
>
>	Marek
diff mbox

Patch

diff --git gcc/testsuite/gcc.dg/tree-ssa/pr68431.c gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
index e69de29..3bd3843 100644
--- gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
+++ gcc/testsuite/gcc.dg/tree-ssa/pr68431.c
@@ -0,0 +1,16 @@ 
+/* PR tree-optimization/68431 */
+/* { dg-options "-O2 -fdump-tree-vrp1-details" } */
+
+unsigned int x = 1;
+int
+main (void)
+{
+  long long int a = -2LL;
+  int t = 1 <= (a / x);
+  if (t != 0)
+    __builtin_abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Folding predicate .*to 0" 1 "vrp1" } } */
diff --git gcc/tree-vrp.c gcc/tree-vrp.c
index e67048e..736082b 100644
--- gcc/tree-vrp.c
+++ gcc/tree-vrp.c
@@ -2975,7 +2975,7 @@  extract_range_from_binary_expr_1 (value_range *vr,
 		  if (vr1.type == VR_RANGE
 		      && !symbolic_range_p (&vr0)
 		      && !symbolic_range_p (&vr1)
-		      && !compare_values (vr1.min, zero))
+		      && compare_values (vr1.min, zero) != 0)
 		    max = int_const_binop (code, vr0.max, vr1.min);
 		  else
 		    max = zero;