public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-4662] MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
@ 2023-10-16 17:11 Andrew Pinski
  0 siblings, 0 replies; only message in thread
From: Andrew Pinski @ 2023-10-16 17:11 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8

commit r14-4662-gc7609acb8a8210188d21b2cd72ecc6d3b2de2ab8
Author: Andrew Pinski <pinskia@gmail.com>
Date:   Sun Oct 15 10:36:56 2023 -0700

    MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
    
    This improves the `A CMP 0 ? A : -A` set of match patterns to use
    bitwise_equal_p which allows an nop cast between signed and unsigned.
    This allows catching a few extra cases which were not being caught before.
    
    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
    
    gcc/ChangeLog:
    
            PR tree-optimization/101541
            * match.pd (A CMP 0 ? A : -A): Improve
            using bitwise_equal_p.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/101541
            * gcc.dg/tree-ssa/phi-opt-36.c: New test.
            * gcc.dg/tree-ssa/phi-opt-37.c: New test.

Diff:
---
 gcc/match.pd                               | 49 ++++++++++++++++------------
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 ++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 ++++++++++++++
 3 files changed, 104 insertions(+), 20 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index e76ec1ec034a..dbd554e2e7c1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5660,42 +5660,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  /* A == 0 ? A : -A    same as -A */
  (for cmp (eq uneq)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate@1 @0))
-    (if (!HONOR_SIGNED_ZEROS (type))
+   (cnd (cmp @0 zerop) @2 (negate@1 @2))
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @2))
      @1))
   (simplify
-   (cnd (cmp @0 zerop) zerop (negate@1 @0))
-    (if (!HONOR_SIGNED_ZEROS (type))
+   (cnd (cmp @0 zerop) zerop (negate@1 @2))
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @2))
      @1))
  )
  /* A != 0 ? A : -A    same as A */
  (for cmp (ne ltgt)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate @0))
-    (if (!HONOR_SIGNED_ZEROS (type))
-     @0))
+   (cnd (cmp @0 zerop) @1 (negate @1))
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @1))
+     @1))
   (simplify
-   (cnd (cmp @0 zerop) @0 integer_zerop)
-    (if (!HONOR_SIGNED_ZEROS (type))
-     @0))
+   (cnd (cmp @0 zerop) @1 integer_zerop)
+    (if (!HONOR_SIGNED_ZEROS (type)
+	 && bitwise_equal_p (@0, @1))
+     @1))
  )
  /* A >=/> 0 ? A : -A    same as abs (A) */
  (for cmp (ge gt)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate @0))
-    (if (!HONOR_SIGNED_ZEROS (type)
-	 && !TYPE_UNSIGNED (type))
-     (abs @0))))
+   (cnd (cmp @0 zerop) @1 (negate @1))
+    (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
+	 && !TYPE_UNSIGNED (TREE_TYPE(@0))
+	 && bitwise_equal_p (@0, @1))
+     (if (TYPE_UNSIGNED (type))
+      (absu:type @0)
+      (abs @0)))))
  /* A <=/< 0 ? A : -A    same as -abs (A) */
  (for cmp (le lt)
   (simplify
-   (cnd (cmp @0 zerop) @0 (negate @0))
-    (if (!HONOR_SIGNED_ZEROS (type)
-	 && !TYPE_UNSIGNED (type))
-     (if (ANY_INTEGRAL_TYPE_P (type)
-	  && !TYPE_OVERFLOW_WRAPS (type))
+   (cnd (cmp @0 zerop) @1 (negate @1))
+    (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
+	 && !TYPE_UNSIGNED (TREE_TYPE(@0))
+	 && bitwise_equal_p (@0, @1))
+     (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
+	   && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
+	  || TYPE_UNSIGNED (type))
       (with {
-	tree utype = unsigned_type_for (type);
+	tree utype = unsigned_type_for (TREE_TYPE(@0));
        }
        (convert (negate (absu:utype @0))))
        (negate (abs @0)))))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
new file mode 100644
index 000000000000..4baf9f82a22f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c
@@ -0,0 +1,51 @@
+/* { dg-options "-O2 -fdump-tree-phiopt" } */
+
+unsigned f0(int A)
+{
+  unsigned t = A;
+//     A == 0? A : -A    same as -A
+  if (A == 0)  return t;
+  return -t;
+}
+
+unsigned f1(int A)
+{
+  unsigned t = A;
+//     A != 0? A : -A    same as A
+  if (A != 0)  return t;
+  return -t;
+}
+unsigned f2(int A)
+{
+  unsigned t = A;
+//     A >= 0? A : -A    same as abs (A)
+  if (A >= 0)  return t;
+  return -t;
+}
+unsigned f3(int A)
+{
+  unsigned t = A;
+//     A > 0?  A : -A    same as abs (A)
+  if (A > 0)  return t;
+  return -t;
+}
+unsigned f4(int A)
+{
+  unsigned t = A;
+//     A <= 0? A : -A    same as -abs (A)
+  if (A <= 0)  return t;
+  return -t;
+}
+unsigned f5(int A)
+{
+  unsigned t = A;
+//     A < 0?  A : -A    same as -abs (A)
+  if (A < 0)  return t;
+  return -t;
+}
+
+/* f4 and f5 are not allowed to be optimized in early phi-opt. */
+/* { dg-final { scan-tree-dump-times "if " 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
+
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
new file mode 100644
index 000000000000..f1ff472aaff6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt1" } */
+
+unsigned abs_with_convert0 (int x)
+{
+    unsigned int y = x;
+
+    if (x < 0)
+        y = -y;
+
+  return y;
+}
+unsigned abs_with_convert1 (unsigned x)
+{
+    int y = x;
+
+    if (y < 0)
+        x = -x;
+
+  return x;
+}
+
+/* { dg-final { scan-tree-dump-times "ABSU_EXPR <"  2  "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not   "if "        "phiopt1" } } */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-16 17:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-16 17:11 [gcc r14-4662] MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).