public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: <apinski@marvell.com>
To: <gcc-patches@gcc.gnu.org>
Cc: Andrew Pinski <apinski@marvell.com>
Subject: [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments
Date: Fri, 13 Aug 2021 17:19:21 -0700	[thread overview]
Message-ID: <1628900361-27676-2-git-send-email-apinski@marvell.com> (raw)
In-Reply-To: <1628900361-27676-1-git-send-email-apinski@marvell.com>

From: Andrew Pinski <apinski@marvell.com>

I noticed this while Richard B. fixing PR101756.
Basically min of two bools is the same as doing an "and"
and max of two bools is doing an "ior".

gcc/ChangeLog:

	* match.pd: Add min/max patterns for bool types.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/bool-12.c: New test.
---
 gcc/match.pd                            | 10 +++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-12.c | 27 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-12.c

diff --git a/gcc/match.pd b/gcc/match.pd
index b1f2aaaac02..8fd60d08cfe 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3103,6 +3103,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
    (cond (cmp @2 @3) @1 @0))))
 
+/* max(bool0, bool1) -> bool0 | bool1 */
+(simplify
+ (max gimple_truth_valued_p@0 gimple_truth_valued_p@1)
+ (bit_ior @0 @1))
+
+/* min(bool0, bool1) -> bool0 & bool1 */
+(simplify
+ (min gimple_truth_valued_p@0 gimple_truth_valued_p@1)
+ (bit_and @0 @1))
+
 /* Simplifications of shift and rotates.  */
 
 (for rotate (lrotate rrotate)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
new file mode 100644
index 00000000000..2d8ad9912d3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original" } */
+#define bool _Bool
+int maxbool(bool ab, bool bb)
+{
+  int a = ab;
+  int b = bb;
+  int c;
+  c = (a > b)?a : b;
+  return c;
+}
+int minbool(bool ab, bool bb)
+{
+  int a = ab;
+  int b = bb;
+  int c;
+  c = (a < b)?a : b;
+  return c;
+}
+/* Original should have one of each MAX/MIN expressions. */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "original" } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original"} } */
+
+/* By the time we reach optimized, the MAX and MIN expressions
+   should have been removed. */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized"} } */
-- 
2.27.0


  reply	other threads:[~2021-08-14  0:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-14  0:19 [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it apinski
2021-08-14  0:19 ` apinski [this message]
2021-08-16  6:29   ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments Richard Biener
2021-09-19  5:56   ` Jeff Law
2021-08-16  6:22 ` [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it Richard Biener
2021-09-19  5:55 ` Jeff Law

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=1628900361-27676-2-git-send-email-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).