Hi!

x is never equal to ~x, so we can fold such comparisons to constants.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2021-01-03  Jakub Jelinek  <ja...@redhat.com>

        PR tree-optimization/96782
        * match.pd (x == ~x -> false, x != ~x -> true): New simplifications.

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

--- gcc/match.pd.jj     2021-01-01 00:03:14.987440788 +0100
+++ gcc/match.pd        2021-01-02 10:40:34.289209121 +0100
@@ -4045,6 +4045,13 @@ (define_operator_list COND_TERNARY
  (if (!flag_trapping_math)
   { constant_boolean_node (false, type); }))
 
+/* x == ~x -> false */
+/* x != ~x -> true */
+(for cmp (eq ne)
+ (simplify
+  (cmp:c @0 (bit_not @0))
+  { constant_boolean_node (cmp == NE_EXPR, type); }))
+
 /* Fold ~X op ~Y as Y op X.  */
 (for cmp (simple_comparison)
  (simplify
--- gcc/testsuite/gcc.dg/tree-ssa/pr96782.c.jj  2021-01-02 10:39:50.740704373 
+0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr96782.c     2021-01-02 10:39:36.268868954 
+0100
@@ -0,0 +1,17 @@
+/* PR tree-optimization/96782 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "return 1;" 1 "optimized" } } */
+
+int
+foo (int a)
+{
+  return a == ~a;
+}
+
+int
+bar (int b)
+{
+  return ~b != b;
+}

        Jakub

Reply via email to