From: Andrew Pinski <pinskia@gmail.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] fold a * (a > 0 ? 1 : -1) to abs(a) and related optimizations
Date: Sat, 24 Jun 2017 03:59:00 -0000 [thread overview]
Message-ID: <CA+=Sn1knt0TcwbO0tvfJpC2LaeOEgTOT=SyqpEUESXTH54eDCg@mail.gmail.com> (raw)
In-Reply-To: <CA+=Sn1mWYuz8h5zBcNqwT2LG9N4B3DueNdoZHmyb4DRREFhwQQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1523 bytes --]
Forgot the patch
On Fri, Jun 23, 2017 at 8:59 PM, Andrew Pinski <pinskia@gmail.com> wrote:
> Hi,
> I saw this on llvm's review site (https://reviews.llvm.org/D34579)
> and I thought why not add it to GCC. I expanded more than what was
> done on the LLVM patch.
>
> I added the following optimizations:
> Transform X * (X > 0 ? 1 : -1) into ABS(X).
> Transform X * (X >= 0 ? 1 : -1) into ABS(X).
> Transform X * (X > 0.0 ? 1.0 : -1.0) into ABS(X).
> Transform X * (X >= 0.0 ? 1.0 : -1.0) into ABS(X).
> Transform X * (X > 0 ? -1 : 1) into -ABS(X).
> Transform X * (X >= 0 ? -1 : 1) into -ABS(X).
> Transform X * (X > 0.0 ? -1.0 : 1.0) into -ABS(X).
> Transform X * (X >= 0.0 ? -1.0 : 1.0) into -ABS(X).
> Transform X * (X < 0 ? 1 : -1) into -ABS(X).
> Transform X * (X <= 0 ? 1 : -1) into -ABS(X).
> Transform X * (X < 0.0 ? 1.0 : -1.0) into -ABS(X).
> Transform X * (X <= 0.0 ? 1.0 : -1.0) into -ABS(X).
> Transform X * (X < 0 ? -1 : 1) into ABS(X).
> Transform X * (X <= 0 ? -1 : 1) into ABS(X).
> Transform X * (X < 0.0 ? -1.0 : 1.0) into ABS(X).
> Transform X * (X <= 0.0 ? -1.0 : 1.0) into ABS(X).
>
> The floating points ones only happen when not honoring SNANS and not
> honoring signed zeros.
>
> OK? Bootstrapped and tested on aarch64-linux-gnu with no regressions.
>
> Thanks,
> Andrew Pinski
>
> ChangeLog:
> * match.pd ( X * (X >/>=/</<= 0 ? 1 : -1)): New patterns.
>
> Testsuite/ChangeLog:
> * testsuite/gcc.dg/tree-ssa/mult-abs-1.c: New testcase.
> * testsuite/gcc.dg/tree-ssa/mult-abs-2.c: New testcase.
[-- Attachment #2: mult-abs.diff.txt --]
[-- Type: text/plain, Size: 4029 bytes --]
Index: match.pd
===================================================================
--- match.pd (revision 249613)
+++ match.pd (working copy)
@@ -155,6 +155,55 @@
|| !COMPLEX_FLOAT_TYPE_P (type)))
(negate @0)))
+(for cmp (gt ge)
+ /* Transform X * (X > 0 ? 1 : -1) into ABS(X). */
+ /* Transform X * (X >= 0 ? 1 : -1) into ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 integer_zerop) integer_onep integer_all_onesp))
+ (abs @0))
+ /* Transform X * (X > 0.0 ? 1.0 : -1.0) into ABS(X). */
+ /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 real_zerop) real_onep real_minus_onep))
+ (if (!HONOR_SNANS (type) && !HONOR_SIGNED_ZEROS (type))
+ (abs @0)))
+ /* Transform X * (X > 0 ? -1 : 1) into -ABS(X). */
+ /* Transform X * (X >= 0 ? -1 : 1) into -ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 integer_zerop) integer_all_onesp integer_onep))
+ (negate (abs @0)))
+ /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -ABS(X). */
+ /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep))
+ (if (!HONOR_SNANS (type) && !HONOR_SIGNED_ZEROS (type))
+ (negate (abs @0)))))
+
+(for cmp (lt le)
+ /* Transform X * (X < 0 ? 1 : -1) into -ABS(X). */
+ /* Transform X * (X <= 0 ? 1 : -1) into -ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 integer_zerop) integer_onep integer_all_onesp))
+ (negate (abs @0)))
+ /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -ABS(X). */
+ /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 real_zerop) real_onep real_minus_onep))
+ (if (!HONOR_SNANS (type) && !HONOR_SIGNED_ZEROS (type))
+ (negate (abs @0))))
+ /* Transform X * (X < 0 ? -1 : 1) into ABS(X). */
+ /* Transform X * (X <= 0 ? -1 : 1) into ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 integer_zerop) integer_all_onesp integer_onep))
+ (abs @0))
+ /* Transform X * (X < 0.0 ? -1.0 : 1.0) into ABS(X). */
+ /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into ABS(X). */
+ (simplify
+ (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep))
+ (if (!HONOR_SNANS (type) && !HONOR_SIGNED_ZEROS (type))
+ (abs @0))))
+
+
/* X * 1, X / 1 -> X. */
(for op (mult trunc_div ceil_div floor_div round_div exact_div)
(simplify
Index: testsuite/gcc.dg/tree-ssa/mult-abs-1.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/mult-abs-1.c (nonexistent)
+++ testsuite/gcc.dg/tree-ssa/mult-abs-1.c (working copy)
@@ -0,0 +1,35 @@
+/* { dg-options "-O2 -fdump-tree-gimple" } */
+/* { dg-do compile } */
+int f(int x)
+{
+ return x * (x > 0 ? -1 : 1);
+}
+int f1(int x)
+{
+ return x * (x > 0 ? 1 : -1);
+}
+int g(int x)
+{
+ return x * (x >= 0 ? -1 : 1);
+}
+int g1(int x)
+{
+ return x * (x >= 0 ? 1 : -1);
+}
+int h(int x)
+{
+ return x * (x < 0 ? -1 : 1);
+}
+int h1(int x)
+{
+ return x * (x < 0 ? 1 : -1);
+}
+int i(int x)
+{
+ return x * (x <= 0 ? -1 : 1);
+}
+int i1(int x)
+{
+ return x * (x <= 0 ? 1 : -1);
+}
+/* { dg-final { scan-tree-dump-times "ABS" 8 "gimple"} } */
Index: testsuite/gcc.dg/tree-ssa/mult-abs-2.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/mult-abs-2.c (nonexistent)
+++ testsuite/gcc.dg/tree-ssa/mult-abs-2.c (working copy)
@@ -0,0 +1,35 @@
+/* { dg-options "-O2 -ffast-math -fdump-tree-gimple" } */
+/* { dg-do compile } */
+float f(float x)
+{
+ return x * (x > 0.f ? -1.f : 1.f);
+}
+float f1(float x)
+{
+ return x * (x > 0.f ? 1.f : -1.f);
+}
+float g(float x)
+{
+ return x * (x >= 0.f ? -1.f : 1.f);
+}
+float g1(float x)
+{
+ return x * (x >= 0.f ? 1.f : -1.f);
+}
+float h(float x)
+{
+ return x * (x < 0.f ? -1.f : 1.f);
+}
+float h1(float x)
+{
+ return x * (x < 0.f ? 1.f : -1.f);
+}
+float i(float x)
+{
+ return x * (x <= 0.f ? -1.f : 1.f);
+}
+float i1(float x)
+{
+ return x * (x <= 0.f ? 1.f : -1.f);
+}
+/* { dg-final { scan-tree-dump-times "ABS" 8 "gimple"} } */
next prev parent reply other threads:[~2017-06-24 3:59 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-24 3:59 Andrew Pinski
2017-06-24 3:59 ` Andrew Pinski [this message]
2017-06-24 6:50 ` Marc Glisse
2017-06-24 18:51 ` Andrew Pinski
2017-06-24 19:47 ` Marc Glisse
2017-06-24 20:53 ` Andrew Pinski
2017-06-26 15:02 ` Joseph Myers
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='CA+=Sn1knt0TcwbO0tvfJpC2LaeOEgTOT=SyqpEUESXTH54eDCg@mail.gmail.com' \
--to=pinskia@gmail.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).