public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-6433] match.pd: Fold x == ~x to false [PR96782]
@ 2021-01-04  9:37 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2021-01-04  9:37 UTC (permalink / raw)
  To: gcc-cvs

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

commit r11-6433-gad64e807ffca93e927b68f1aa0cea54dacbe9afd
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jan 4 10:37:12 2021 +0100

    match.pd: Fold x == ~x to false [PR96782]
    
    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.

Diff:
---
 gcc/match.pd                            |  7 +++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr96782.c | 17 +++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index f5020d869be..56fb583f7e1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
index 00000000000..0444eef1f54
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr96782.c
@@ -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;
+}


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

only message in thread, other threads:[~2021-01-04  9:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-04  9:37 [gcc r11-6433] match.pd: Fold x == ~x to false [PR96782] Jakub Jelinek

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).