public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pinski <apinski@marvell.com>
To: <gcc-patches@gcc.gnu.org>
Cc: Andrew Pinski <apinski@marvell.com>
Subject: [PATCH] MATCH: [PR111348] add missing :c to cmp in the `(a CMP b) ? minmax<a, c> : minmax<b, c>` pattern
Date: Mon, 11 Sep 2023 13:39:39 -0700	[thread overview]
Message-ID: <20230911203939.1394059-1-apinski@marvell.com> (raw)

When I added this pattern in r14-337-gc43819a9b4cd, I had missed the :c on the cmp
part of the pattern meaning there might be some missing optimizations happening.
The testcase shows an example of the missed optmization.

Committed as obvious after a bootstrap/test on x86_64-linux-gnu.

	PR tree-optimization/111348

gcc/ChangeLog:

	* match.pd (`(a CMP b) ? minmax<a, c> : minmax<b, c>`): Add :c on
	the cmp part of the pattern.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/minmax-26.c: New test.
---
 gcc/match.pd                              |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-26.c | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-26.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 209b0599382..e96e385c6fa 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5417,7 +5417,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (for minmax (min max)
  (for cmp (lt le gt ge ne)
   (simplify
-   (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
+   (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
    (with
     {
       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-26.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-26.c
new file mode 100644
index 00000000000..e4b7412e766
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-26.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized -fdump-tree-original" } */
+/* PR tree-optimization/111348 */
+
+int test1(int a, int b, int c)
+{
+        return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
+}
+
+
+int test1_(int a, int b, int c)
+{
+        return (b < a) ? ((a > c) ? a : c) : ((b > c) ? b : c);
+}
+
+/* test1 and test1_ should be able to optimize to `MAX_EXPR <MAX_EXPR <a, b>, c>;` during fold.  */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR <MAX_EXPR <a, b>, c>" 2 "original" } } */
+/* { dg-final { scan-tree-dump-not "b > a" "original" } } */
+/* { dg-final { scan-tree-dump-not "a > b" "original" } } */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR " 4 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "if " "optimized" } } */
+
-- 
2.31.1


                 reply	other threads:[~2023-09-11 20:39 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=20230911203939.1394059-1-apinski@marvell.com \
    --to=apinski@marvell.com \
    --cc=gcc-patches@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).