This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix division-by-unsigned optimization in VRP (PR tree-optimization/68431)
- From: Marek Polacek <polacek at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 19 Nov 2015 15:57:03 +0100
- Subject: [PATCH] Fix division-by-unsigned optimization in VRP (PR tree-optimization/68431)
- Authentication-results: sourceware.org; auth=none
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