match.pd: Fold x == ~x to false [PR96782]
authorJakub Jelinek <jakub@redhat.com>
Mon, 4 Jan 2021 09:37:12 +0000 (10:37 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 4 Jan 2021 09:37:12 +0000 (10:37 +0100)
x is never equal to ~x, so we can fold such comparisons to constants.

2021-01-04  Jakub Jelinek  <jakub@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
gcc/testsuite/gcc.dg/tree-ssa/pr96782.c [new file with mode: 0644]

index f5020d869befa54bc7a232d2a7e08cd3b854aede..56fb583f7e125c911c5e0594d39a185dfb1ea0cf 100644 (file)
@@ -4045,6 +4045,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (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
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr96782.c b/gcc/testsuite/gcc.dg/tree-ssa/pr96782.c
new file mode 100644 (file)
index 0000000..0444eef
--- /dev/null
@@ -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;
+}