https://gcc.gnu.org/g:dae2b6246c00f4389b617ffaa30459bd22d9fe13
commit r15-7114-gdae2b6246c00f4389b617ffaa30459bd22d9fe13
Author: Andrew Pinski <quic_apin...@quicinc.com>
Date:   Wed Jan 15 20:17:09 2025 -0800

    match: Improve the `x ==/!= ~x` pattern [PR118483]
    
    This improves this pattern by 2 ways:
    * Allow for an optional convert, similar to how the few other
      `a OP ~a` patterns also allow for an optional convert.
    * Use bitwise_inverted_equal_p/maybe_bit_not instead of directly
      matching bit_not. Just like the other patterns do too.
    
    Note pr118483-2.c used to optimized for aarch64-linux-gnu with GCC 4.9.4
    on the RTL level even though the gimple level was missing it.
    
            PR tree-optimization/118483
    
    gcc/ChangeLog:
    
            * match.pd (`x ==/!= ~x`): Allow for an optional convert
            and use itwise_inverted_equal_p/maybe_bit_not instead of
            directly matching bit_not.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr118483-1.c: New test.
            * gcc.dg/tree-ssa/pr118483-2.c: New test.
            * gcc.dg/tree-ssa/pr118483-3.c: New test.
            * gcc.dg/tree-ssa/pr118483-4.c: New test.
    
    Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>

Diff:
---
 gcc/match.pd                               |  7 +++++--
 gcc/testsuite/gcc.dg/tree-ssa/pr118483-1.c | 18 ++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr118483-2.c | 18 ++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr118483-3.c | 14 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr118483-4.c | 11 +++++++++++
 5 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index f4359d3a005a..1cdc7e94f1fe 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6959,8 +6959,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* x != ~x -> true */
 (for cmp (eq ne)
  (simplify
-  (cmp:c @0 (bit_not @0))
-  { constant_boolean_node (cmp == NE_EXPR, type); }))
+  (cmp:c (convert? @0) (convert? (maybe_bit_not @1)))
+  (with { bool wascmp; }
+   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+        && bitwise_inverted_equal_p (@0, @1, wascmp))
+    { constant_boolean_node (cmp == NE_EXPR, type); }))))
 
 /* Fold ~X op ~Y as Y op X.  */
 (for cmp (simple_comparison)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr118483-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-1.c
new file mode 100644
index 000000000000..e31876c940a2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-1.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump-not "abort " "optimized" } } */
+
+
+/* The value of `l == e` is always false as it is
+   `(b == 0) == (b != 0)`. */
+
+int d;
+int f(int b)
+{
+  int e = b == 0;
+  d = e;
+  int l = b != 0;
+  if (l == e)
+    __builtin_abort ();
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr118483-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-2.c
new file mode 100644
index 000000000000..84867719867d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-2.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump-not "abort " "optimized" } } */
+
+
+/* The value of `l == e` is always false as it is
+   `(b == 0) == (b != 0)`. */
+
+int d;
+int f(int b)
+{
+  int e = b == 0;
+  d = e;
+  int l = !e;
+  if (l == e)
+    __builtin_abort ();
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr118483-3.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-3.c
new file mode 100644
index 000000000000..65efaf5c30fd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-3.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
+
+/* This should optimize down to just `return 0;` */
+/* as `(short)a == ~(short)a` is always false. */
+int f(int a)
+{
+  short b = a;
+  int e = ~a;
+  short c = e;
+  return b == c;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr118483-4.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-4.c
new file mode 100644
index 000000000000..c6e389c46743
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr118483-4.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
+
+/* This should optimize down to just `return 0;` */
+/* as `a == 0` and `a != 0` are opposites. */
+int f(int a)
+{
+  return (a == 0) == (a != 0);
+}

Reply via email to