public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Roger Sayle <sayle@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-5128] PR tree-optimization/92342: Optimize b & -(a==c) in match.pd
Date: Thu, 12 Jan 2023 21:49:15 +0000 (GMT)	[thread overview]
Message-ID: <20230112214915.4ADB23858C66@sourceware.org> (raw)

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

commit r13-5128-gfd1f5373b8647a5da2f7f4b42282e676a4b04d98
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Thu Jan 12 21:47:40 2023 +0000

    PR tree-optimization/92342: Optimize b & -(a==c) in match.pd
    
    This patch is an update/tweak of Andrew Pinski's two patches for
    PR tree-optimization/92342, that were originally posted by in November:
    https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585111.html
    https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585112.html
    
    Technically, the first of those was approved by Richard Biener, though
    never committed, and my first thought was to simply push it for Andrew,
    but the review of the second piece expressed concerns over comparisons
    in non-integral modes, where the result may not be zero-one valued.
    Indeed both transformations misbehave in the presence of vector mode
    comparisons (these transformations are already implemented for
    vec_cond elsewhere in match.pd), so my minor contribution is to limit
    these new transformations to scalars, by testing that both the operands
    and results are INTEGRAL_TYPE_P.
    
    2023-01-12  Andrew Pinski  <apinski@marvell.com>
                Roger Sayle  <roger@nextmovesoftware.com>
    
    gcc/ChangeLog:
            PR tree-optimization/92342
            * match.pd ((m1 CMP m2) * d -> (m1 CMP m2) ? d : 0):
            Use tcc_comparison and :c for the multiply.
            (b & -(a CMP c) -> (a CMP c)?b:0): New pattern.
    
    gcc/testsuite/ChangeLog:
            PR tree-optimization/92342
            * gcc.dg/tree-ssa/andnegcmp-1.c: New test.
            * gcc.dg/tree-ssa/andnegcmp-2.c: New test.
            * gcc.dg/tree-ssa/multcmp-1.c: New test.
            * gcc.dg/tree-ssa/multcmp-2.c: New test.

Diff:
---
 gcc/match.pd                                | 16 +++++++++++++---
 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c | 14 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c | 14 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c   | 12 ++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c   | 12 ++++++++++++
 5 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index d1601567770..56ac743aa6d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2076,10 +2076,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
 (if (!canonicalize_math_p ())
- (for cmp (gt lt ge le)
+ (for cmp (tcc_comparison)
   (simplify
-   (mult (convert (cmp @0 @1)) @2)
-   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
+   (mult:c (convert (cmp@0 @1 @2)) @3)
+   (if (INTEGRAL_TYPE_P (type)
+	&& INTEGRAL_TYPE_P (TREE_TYPE (@0)))
+     (cond @0 @3 { build_zero_cst (type); })))
+/* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
+  (simplify
+   (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
+   (if (INTEGRAL_TYPE_P (type)
+	&& INTEGRAL_TYPE_P (TREE_TYPE (@0)))
+     (cond @0 @3 { build_zero_cst (type); })))
+ )
+)
 
 /* For integral types with undefined overflow and C != 0 fold
    x * C EQ/NE y * C into x EQ/NE y.  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
new file mode 100644
index 00000000000..6f16783f169
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/92342 */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 == m2;
+  d = -d;
+  int e = d & c;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
new file mode 100644
index 00000000000..0e25c8abc39
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/92342 */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 < m2;
+  d = -d;
+  int e = c & d;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
new file mode 100644
index 00000000000..fb44cacde77
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 == m2;
+  int e = d * c;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
new file mode 100644
index 00000000000..be38b2e0044
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 != m2;
+  int e = c * d;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */

                 reply	other threads:[~2023-01-12 21:49 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=20230112214915.4ADB23858C66@sourceware.org \
    --to=sayle@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).