public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <rv@rasmusvillemoes.dk>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Richard Biener <richard.guenther@gmail.com>,
	Andrew Pinski <pinskia@gmail.com>,
	Rasmus Villemoes <rv@rasmusvillemoes.dk>
Subject: [PATCH 2/4] match.pd: Add x & ~(x & y) -> x & ~y pattern
Date: Wed, 21 Jan 2015 10:55:00 -0000	[thread overview]
Message-ID: <1421837394-7619-3-git-send-email-rv@rasmusvillemoes.dk> (raw)
In-Reply-To: <1421837394-7619-1-git-send-email-rv@rasmusvillemoes.dk>

gcc.dg/20150120-2.c: New test

Clearing a certain subset of bits, for example to round down x to a
multiple of a power of 2, is sometimes written x & ~(x & y), where y
may or may not be a constant. It is shorter to use x & ~y,
particularly when y is a constant. gcc already does this when it is
spelled x - (x & y) or x ^ (x & y).

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 gcc/match.pd                      |  6 ++++++
 gcc/testsuite/gcc.dg/20150120-2.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/20150120-2.c

diff --git gcc/match.pd gcc/match.pd
index ecefcfb..d25fc3a 100644
--- gcc/match.pd
+++ gcc/match.pd
@@ -261,6 +261,12 @@ along with GCC; see the file COPYING3.  If not see
  (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
   (bit_and (plus @0 @1) (bit_not @1))))
 
+/* x & ~(x & y) -> x & ~y */
+(simplify
+ (bit_and:c @0 (bit_not (bit_and:c@2 @0 @1)))
+ (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
+  (bit_and @0 (bit_not @1))))
+
 (simplify
  (abs (negate @0))
  (abs @0))
diff --git gcc/testsuite/gcc.dg/20150120-2.c gcc/testsuite/gcc.dg/20150120-2.c
new file mode 100644
index 0000000..976b654
--- /dev/null
+++ gcc/testsuite/gcc.dg/20150120-2.c
@@ -0,0 +1,32 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+/* x & ~(x & y) -> x & ~y */
+int fn1 (int x, int y)
+{
+	return x & ~(x & y);
+}
+int fn2 (int x, int y)
+{
+	return ~(x & y) & x;
+}
+int fn3 (int x, int y)
+{
+	return x & ~(y & x);
+}
+int fn4 (int x, int y)
+{
+	return ~(y & x) & x;
+}
+int fn5 (int z)
+{
+	return z & ~(z & 3);
+}
+int fn6 (int z)
+{
+	return ~(z & 3) & z;
+}
+
+
+/* { dg-final { scan-tree-dump-times "~y & x" 4 "original" } } */
+/* { dg-final { scan-tree-dump-times "z & -4" 2 "original" } } */
-- 
2.1.3

  parent reply	other threads:[~2015-01-21 10:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 22:47 RFC: Two minor optimization patterns Rasmus Villemoes
2015-01-13 22:56 ` Andrew Pinski
2015-01-14  9:52   ` Richard Biener
2015-01-14 12:45     ` Rasmus Villemoes
2015-01-14 14:01       ` Richard Biener
2015-01-21 10:50         ` [PATCH 0/4] A few " Rasmus Villemoes
2015-01-21 10:50           ` [PATCH 1/4] match.pd: Add x + (x & 1) -> (x + 1) & ~1 pattern Rasmus Villemoes
2015-04-30  9:34             ` Richard Biener
2015-05-01 18:26             ` Jeff Law
2015-01-21 10:55           ` Rasmus Villemoes [this message]
2015-05-01 18:29             ` [PATCH 2/4] match.pd: Add x & ~(x & y) -> x & ~y pattern Jeff Law
2015-01-21 10:58           ` [PATCH 3/4] match.pd: Add x | ~(x | y) -> x | " Rasmus Villemoes
2015-01-21 11:32             ` Marek Polacek
2015-01-21 11:17           ` [PATCH 4/4] match.pd: Add x + ((-x) & m) -> (x + m) & ~m pattern Rasmus Villemoes
2015-04-30  9:42             ` Richard Biener
2015-04-30 11:56               ` Marc Glisse
2015-04-30 12:25                 ` Richard Biener
2015-01-14 13:14 ` RFC: Two minor optimization patterns Marc Glisse
2015-01-14 13:58   ` Richard Biener
2015-01-14 14:31     ` Marc Glisse
2015-01-14 14:49       ` Richard Biener
2015-01-21 21:24 [PATCH 2/4] match.pd: Add x & ~(x & y) -> x & ~y pattern Marc Glisse
2015-01-22  9:24 ` Richard Biener
2015-01-22 14:24   ` Rasmus Villemoes

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=1421837394-7619-3-git-send-email-rv@rasmusvillemoes.dk \
    --to=rv@rasmusvillemoes.dk \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=pinskia@gmail.com \
    --cc=richard.guenther@gmail.com \
    /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).