public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] match.pd: Optimize ((cst << x) & 1) [PR96669]
Date: Sat, 16 Jan 2021 01:59:50 +0100	[thread overview]
Message-ID: <20210116005950.GW1034503@tucnak> (raw)
In-Reply-To: <40E7C042-A3A3-4E55-B521-14B5E1100FA3@suse.de>

On Fri, Jan 15, 2021 at 09:21:07PM +0100, Richard Biener wrote:
> >BTW, I've tried to also fix what the PR wanted primarily by adding
> >/* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is
> >odd.  /
> >(simplify
> > (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
> >  (if ((wi::to_wide (@1) & 1) != 0)
> >   (eq @0 { build_zero_cst (TREE_TYPE (@0)); })
> >   ({ build_zero_cst (type); })))
> >simplifier before this one, but genmatch.c doesn't seem to put it into
> >the
> >resulting files.  Is there a way to figure out what is going on?

Actually, it was a PEBKAC, it helps when the comment termination is */
rather than just / alone.

The following works fine, no need to stick it into an unrelated pattern.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2021-01-15  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/96669
	* match.pd ((CST << x) & 1 -> x == 0): New simplification.

	* gcc.dg/tree-ssa/pr96669-1.c: Adjust regexp.
	* gcc.dg/tree-ssa/pr96669-2.c: New test.

--- gcc/match.pd.jj	2021-01-15 21:12:07.656060308 +0100
+++ gcc/match.pd	2021-01-15 23:51:34.038119715 +0100
@@ -3117,6 +3117,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
 
 
+/* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
+(simplify
+ (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
+  (if ((wi::to_wide (@1) & 1) != 0)
+   (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
+   { build_zero_cst (type); }))
+
 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
    either to false if D is smaller (unsigned comparison) than C, or to
    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
--- gcc/testsuite/gcc.dg/tree-ssa/pr96669-1.c.jj	2021-01-15 22:03:18.089279035 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr96669-1.c	2021-01-16 01:55:42.800379400 +0100
@@ -1,7 +1,7 @@
 /* PR tree-optimization/96669 */
 /* { dg-do compile } */
 /* { dg-options "-O2 -fdump-tree-original" } */
-/* { dg-final { scan-tree-dump "return a == 0;" "original" } } */
+/* { dg-final { scan-tree-dump "a == 0" "original" } } */
 /* { dg-final { scan-tree-dump "return 1;" "original" } } */
 /* { dg-final { scan-tree-dump "return c == 3;" "original" } } */
 /* { dg-final { scan-tree-dump "return d != 1;" "original" } } */
--- gcc/testsuite/gcc.dg/tree-ssa/pr96669-2.c.jj	2021-01-15 22:03:47.075951818 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr96669-2.c	2021-01-16 01:56:02.859156051 +0100
@@ -0,0 +1,30 @@
+/* PR tree-optimization/96669 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+/* { dg-final { scan-tree-dump "a == 0" "original" } } */
+/* { dg-final { scan-tree-dump-times "return 0;" 2 "original" } } */
+/* { dg-final { scan-tree-dump "c == 0" "original" } } */
+
+int
+f1 (int a)
+{
+  return ((1 << a) & 1);
+}
+
+int
+f2 (int b)
+{
+  return ((2 << b) & 1);
+}
+
+int
+f3 (int c)
+{
+  return ((35 << c) & 1);
+}
+
+int
+f4 (int d)
+{
+  return ((42 << d) & 1);
+}


	Jakub


  reply	other threads:[~2021-01-16  0:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-15 18:38 [PATCH] match.pd: Generalize the PR64309 simplifications [PR96669] Jakub Jelinek
2021-01-15 19:50 ` Richard Biener
2021-01-15 20:03   ` Jakub Jelinek
2021-01-15 20:21     ` Richard Biener
2021-01-16  0:59       ` Jakub Jelinek [this message]
2021-01-16  7:49         ` [PATCH] match.pd: Optimize ((cst << x) & 1) [PR96669] Richard Biener

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=20210116005950.GW1034503@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    /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).