match.pd: u + 3 < u is u > UINT_MAX - 3
authorMarc Glisse <marc.glisse@inria.fr>
Tue, 26 Apr 2016 15:03:08 +0000 (17:03 +0200)
committerMarc Glisse <glisse@gcc.gnu.org>
Tue, 26 Apr 2016 15:03:08 +0000 (15:03 +0000)
2016-04-26  Marc Glisse  <marc.glisse@inria.fr>

gcc/
* match.pd (X + CST CMP X): New transformation.

gcc/testsuite/
* gcc.dg/tree-ssa/overflow-1.c: New testcase.

From-SVN: r235448

gcc/ChangeLog
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/overflow-1.c [new file with mode: 0644]

index e45f9c9..325eb87 100644 (file)
@@ -1,5 +1,9 @@
 2016-04-26  Marc Glisse  <marc.glisse@inria.fr>
 
+       * match.pd (X + CST CMP X): New transformation.
+
+2016-04-26  Marc Glisse  <marc.glisse@inria.fr>
+
        * genmatch.c (write_predicate): Add ATTRIBUTE_UNUSED.
        * fold-const.c (fold_binary_loc): Remove 2 transformations
        superseded by match.pd.
index 5f22b13..4768187 100644 (file)
@@ -2482,6 +2482,32 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    replace if (x == 0) with tem = ~x; if (tem != 0) which is
    clearly less optimal and which we'll transform again in forwprop.  */
 
+/* When one argument is a constant, overflow detection can be simplified.
+   Currently restricted to single use so as not to interfere too much with
+   ADD_OVERFLOW detection in tree-ssa-math-opts.c.
+   A + CST CMP A  ->  A CMP' CST' */
+(for cmp (lt le ge gt)
+     out (gt gt le le)
+ (simplify
+  (cmp (plus@2 @0 INTEGER_CST@1) @0)
+  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
+       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
+       && wi::ne_p (@1, 0)
+       && single_use (@2))
+   (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
+              (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
+/* A CMP A + CST  ->  A CMP' CST' */
+(for cmp (gt ge le lt)
+     out (gt gt le le)
+ (simplify
+  (cmp @0 (plus@2 @0 INTEGER_CST@1))
+  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
+       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
+       && wi::ne_p (@1, 0)
+       && single_use (@2))
+   (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
+              (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
+
 
 /* Simplification of math builtins.  These rules must all be optimizations
    as well as IL simplifications.  If there is a possibility that the new
index 70baf06..b77a17b 100644 (file)
@@ -1,3 +1,7 @@
+2016-04-26  Marc Glisse  <marc.glisse@inria.fr>
+
+       * gcc.dg/tree-ssa/overflow-1.c: New testcase.
+
 2016-04-26  Marek Polacek  <polacek@redhat.com>
 
        PR c/67784
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/overflow-1.c b/gcc/testsuite/gcc.dg/tree-ssa/overflow-1.c
new file mode 100644 (file)
index 0000000..e126609
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+
+int f(unsigned a){
+    unsigned b=5;
+    unsigned c=a-b;
+    return c>a;
+}
+int g(unsigned a){
+    unsigned b=32;
+    unsigned c=a+b;
+    return c<a;
+}
+
+/* { dg-final { scan-tree-dump "a_\[0-9\]+.D. <= 4;" "optimized" } } */
+/* { dg-final { scan-tree-dump "a_\[0-9\]+.D. > 4294967263;" "optimized" } } */