This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]