match.pd: Simplify unsigned A - B - 1 >= A to B >= A [PR94913]
authorJakub Jelinek <jakub@redhat.com>
Fri, 8 May 2020 07:32:20 +0000 (09:32 +0200)
committerJakub Jelinek <jakub@redhat.com>
Fri, 8 May 2020 07:32:20 +0000 (09:32 +0200)
Implemented thusly.  The TYPE_OVERFLOW_WRAPS is there just because the
pattern above it has it too, if you want, I can throw it away from both.

2020-05-08  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/94913
* match.pd (A - B + -1 >= A to B >= A): New simplification.
(A - B > A to A < B): Don't test TYPE_OVERFLOW_WRAPS which is always
true for TYPE_UNSIGNED integral types.

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

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

index 27f6ea4..eb4924a 100644 (file)
@@ -1,5 +1,10 @@
 2020-05-08  Jakub Jelinek  <jakub@redhat.com>
 
+       PR tree-optimization/94913
+       * match.pd (A - B + -1 >= A to B >= A): New simplification.
+       (A - B > A to A < B): Don't test TYPE_OVERFLOW_WRAPS which is always
+       true for TYPE_UNSIGNED integral types.
+
        PR bootstrap/94961
        PR rtl-optimization/94516
        * rtl.h (remove_reg_equal_equiv_notes): Add a bool argument defaulted
index 9259dd4..cfe9697 100644 (file)
@@ -4787,10 +4787,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cmp:c (minus@2 @0 @1) @0)
   (if (single_use (@2)
        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
-       && TYPE_UNSIGNED (TREE_TYPE (@0))
-       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
+       && TYPE_UNSIGNED (TREE_TYPE (@0)))
    (cmp @1 @0))))
 
+/* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
+(for cmp (ge lt)
+ (simplify
+  (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
+   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
+       && TYPE_UNSIGNED (TREE_TYPE (@0)))
+    (cmp @1 @0))))
+
 /* Testing for overflow is unnecessary if we already know the result.  */
 /* A - B > A  */
 (for cmp (gt le)
index 088b80e..43e226e 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-08  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/94913
+       * gcc.dg/tree-ssa/pr94913.c: New test.
+
 2020-05-07  Segher Boessenkool  <segher@kernel.crashing.org>
 
        * gcc.target/powerpc/setnbc.h: New.
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr94913.c b/gcc/testsuite/gcc.dg/tree-ssa/pr94913.c
new file mode 100644 (file)
index 0000000..6b71f98
--- /dev/null
@@ -0,0 +1,33 @@
+/* PR tree-optimization/94913 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump " (?:b_\[0-9]+\\\(D\\\) >= a|a_\[0-9]+\\\(D\\\) <= b)_\[0-9]+\\\(D\\\);" "optimized" } } */
+/* { dg-final { scan-tree-dump " (?:c_\[0-9]+\\\(D\\\) > d|d_\[0-9]+\\\(D\\\) < c)_\[0-9]+\\\(D\\\);" "optimized" } } */
+/* { dg-final { scan-tree-dump " (?:f_\[0-9]+\\\(D\\\) >= e|e_\[0-9]+\\\(D\\\) <= f)_\[0-9]+\\\(D\\\);" "optimized" } } */
+/* { dg-final { scan-tree-dump " (?:g_\[0-9]+\\\(D\\\) > h|h_\[0-9]+\\\(D\\\) < g)_\[0-9]+\\\(D\\\);" "optimized" } } */
+
+int
+foo (unsigned a, unsigned b)
+{
+  return (a - b - 1) >= a;
+}
+
+int
+bar (unsigned c, unsigned d)
+{
+  return (c - d - 1) < c;
+}
+
+int
+baz (unsigned e, unsigned f)
+{
+  unsigned t = e - f;
+  return (t - 1) >= e;
+}
+
+int
+qux (unsigned g, unsigned h)
+{
+  unsigned t = g - h;
+  return (t - 1) < g;
+}