public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-1822] middle-end/106414 - fix mistake in ~(x ^ y) -> x == y pattern
Date: Mon, 25 Jul 2022 11:25:02 +0000 (GMT)	[thread overview]
Message-ID: <20220725112502.A96FE3857B8A@sourceware.org> (raw)

https://gcc.gnu.org/g:613e3b86955ea3b4977490625328708e82bea6fa

commit r13-1822-g613e3b86955ea3b4977490625328708e82bea6fa
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Jul 25 12:10:48 2022 +0200

    middle-end/106414 - fix mistake in ~(x ^ y) -> x == y pattern
    
    When compares are integer typed the inversion with ~ isn't properly
    preserved by the equality comparison even when converting the
    result properly.  The following fixes this by restricting the
    input precisions accordingly.
    
            PR middle-end/106414
            * match.pd (~(x ^ y) -> x == y): Restrict to single bit
            precision types.
    
            * gcc.dg/torture/pr106414-1.c: New testcase.
            * gcc.dg/torture/pr106414-2.c: Likewise.

Diff:
---
 gcc/match.pd                              |  3 ++-
 gcc/testsuite/gcc.dg/torture/pr106414-1.c | 12 ++++++++++++
 gcc/testsuite/gcc.dg/torture/pr106414-2.c | 12 ++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 9736393061a..330c1db0c8e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1946,7 +1946,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* ~(a ^ b) is a == b for truth valued a and b.  */
 (simplify
  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
- (if (!VECTOR_TYPE_P (type))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
   (convert (eq @0 @1))))
 
 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
diff --git a/gcc/testsuite/gcc.dg/torture/pr106414-1.c b/gcc/testsuite/gcc.dg/torture/pr106414-1.c
new file mode 100644
index 00000000000..097471617b4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr106414-1.c
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+
+int a, c, e;
+const int b = 1;
+char d;
+int main() {
+  a = ~(e || 0) ^ b & ~d;
+  d = ~(a | ~2);
+  if (d)
+    __builtin_abort();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr106414-2.c b/gcc/testsuite/gcc.dg/torture/pr106414-2.c
new file mode 100644
index 00000000000..bed6a40c61e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr106414-2.c
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+
+int a, b, c, d;
+unsigned e;
+int main() {
+  c = e = -((a && 1) ^ ~(b || 0));
+  if (e < -1)
+    d = c;
+  if (!d)
+    __builtin_abort();
+  return 0;
+}


                 reply	other threads:[~2022-07-25 11:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220725112502.A96FE3857B8A@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).