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] tree-optimization: [PR103218] Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit
Date: Sat, 13 Nov 2021 12:13:47 -0800	[thread overview]
Message-ID: <1636834427-22589-1-git-send-email-apinski@marvell.com> (raw)

From: Andrew Pinski <apinski@marvell.com>

This folds Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit inside match.pd.
This was already handled in fold-cost by:
/* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
I have not removed as we only simplify "a ? POW2 : 0" at the gimple level to "a << CST1"
and fold actually does the reverse of folding "(a<0)<<CST" into "(a<0) ? 1<<CST : 0".

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

	PR tree-optimization/103218

gcc/ChangeLog:

	* match.pd: New pattern for "((type)(a<0)) << SIGNBITOFA".

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/pr103218-1.c: New test.
---
 gcc/match.pd                               | 10 ++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr103218-1.c | 28 ++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr103218-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index a319aefa808..df31964e02f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -865,6 +865,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     { tree utype = unsigned_type_for (type); }
     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
 
+/* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
+(simplify
+ (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
+ (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
+      && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
+  (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
+   (bit_and (convert @0)
+            { wide_int_to_tree (type,
+				wi::lshift (wone, wi::to_wide (@2))); }))))
+
 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
 (for cst (INTEGER_CST VECTOR_CST)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr103218-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr103218-1.c
new file mode 100644
index 00000000000..f086f073b38
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103218-1.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/103218 */
+
+/* These first two are removed during forwprop1 */
+signed char f(signed char a)
+{
+  signed char t = a < 0;
+  int tt = (unsigned char)(t << 7);
+  return tt;
+}
+signed char f0(signed char a)
+{
+  unsigned char t = a < 0;
+  int tt = (unsigned char)(t << 7);
+  return tt;
+}
+
+/* This one is removed during phiopt. */
+signed char  f1(signed char a)
+{
+    if (a < 0)
+      return 1u<<7;
+    return 0;
+}
+
+/* These three examples should remove "a < 0" by optimized. */
+/* { dg-final { scan-tree-dump-times "< 0" 0 "optimized"} } */
-- 
2.17.1


             reply	other threads:[~2021-11-13 20:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-13 20:13 apinski [this message]
2021-11-16 10:03 ` 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=1636834427-22589-1-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).