* [PATCH] match: `a CMP nonnegative ? a : ABS<a>` simplified to just `ABS<a>` [PR112392]
@ 2024-05-08 3:24 Andrew Pinski
2024-05-08 7:53 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2024-05-08 3:24 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
We can optimize `a == nonnegative ? a : ABS<a>`, `a > nonnegative ? a : ABS<a>`
and `a >= nonnegative ? a : ABS<a>` into `ABS<a>`. This allows removal of
some extra comparison and extra conditional moves in some cases.
I don't remember where I had found though but it is simple to add so
let's add it.
Bootstrapped and tested on x86_64-linux-gnu with no regressions.
Note I have a secondary pattern for the equal case as either a or nonnegative
could be used.
PR tree-optimization/112392
gcc/ChangeLog:
* match.pd (`x CMP nonnegative ? x : ABS<x>`): New pattern;
where CMP is ==, > and >=.
(`x CMP nonnegative@y ? y : ABS<x>`): New pattern.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/phi-opt-41.c: New test.
Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
gcc/match.pd | 15 ++++++++++
gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c | 34 ++++++++++++++++++++++
2 files changed, 49 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 03a03c31233..07e743ae464 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5876,6 +5876,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(convert (absu:utype @0)))
@3))))
+/* X > Positive ? X : ABS(X) -> ABS(X) */
+/* X >= Positive ? X : ABS(X) -> ABS(X) */
+/* X == Positive ? X : ABS(X) -> ABS(X) */
+(for cmp (eq gt ge)
+ (simplify
+ (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
+ (if (INTEGRAL_TYPE_P (type))
+ @3)))
+
+/* X == Positive ? Positive : ABS(X) -> ABS(X) */
+(simplify
+ (cond (eq:c @0 tree_expr_nonnegative_p@1) @1 (abs@3 @0))
+ (if (INTEGRAL_TYPE_P (type))
+ @3))
+
/* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
(simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c
new file mode 100644
index 00000000000..9774e283a7b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt1" } */
+/* PR tree-optimization/112392 */
+
+int feq_1(int a, unsigned char b)
+{
+ int absb = b;
+ if (a == absb) return absb;
+ return a > 0 ? a : -a;
+}
+int feq_2(int a, unsigned char b)
+{
+ int absb = b;
+ if (a == absb) return a;
+ return a > 0 ? a : -a;
+}
+
+int fgt(int a, unsigned char b)
+{
+ int absb = b;
+ if (a > absb) return a;
+ return a > 0 ? a : -a;
+}
+
+int fge(int a, unsigned char b)
+{
+ int absb = b;
+ if (a >= absb) return a;
+ return a > 0 ? a : -a;
+}
+
+
+/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 4 "phiopt1" } } */
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] match: `a CMP nonnegative ? a : ABS<a>` simplified to just `ABS<a>` [PR112392]
2024-05-08 3:24 [PATCH] match: `a CMP nonnegative ? a : ABS<a>` simplified to just `ABS<a>` [PR112392] Andrew Pinski
@ 2024-05-08 7:53 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2024-05-08 7:53 UTC (permalink / raw)
To: Andrew Pinski; +Cc: gcc-patches
On Wed, May 8, 2024 at 5:25 AM Andrew Pinski <quic_apinski@quicinc.com> wrote:
>
> We can optimize `a == nonnegative ? a : ABS<a>`, `a > nonnegative ? a : ABS<a>`
> and `a >= nonnegative ? a : ABS<a>` into `ABS<a>`. This allows removal of
> some extra comparison and extra conditional moves in some cases.
> I don't remember where I had found though but it is simple to add so
> let's add it.
>
> Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> Note I have a secondary pattern for the equal case as either a or nonnegative
> could be used.
OK
> PR tree-optimization/112392
>
> gcc/ChangeLog:
>
> * match.pd (`x CMP nonnegative ? x : ABS<x>`): New pattern;
> where CMP is ==, > and >=.
> (`x CMP nonnegative@y ? y : ABS<x>`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/phi-opt-41.c: New test.
>
> Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
> ---
> gcc/match.pd | 15 ++++++++++
> gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c | 34 ++++++++++++++++++++++
> 2 files changed, 49 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 03a03c31233..07e743ae464 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5876,6 +5876,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (convert (absu:utype @0)))
> @3))))
>
> +/* X > Positive ? X : ABS(X) -> ABS(X) */
> +/* X >= Positive ? X : ABS(X) -> ABS(X) */
> +/* X == Positive ? X : ABS(X) -> ABS(X) */
> +(for cmp (eq gt ge)
> + (simplify
> + (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
> + (if (INTEGRAL_TYPE_P (type))
> + @3)))
> +
> +/* X == Positive ? Positive : ABS(X) -> ABS(X) */
> +(simplify
> + (cond (eq:c @0 tree_expr_nonnegative_p@1) @1 (abs@3 @0))
> + (if (INTEGRAL_TYPE_P (type))
> + @3))
> +
> /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
> X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
> (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c
> new file mode 100644
> index 00000000000..9774e283a7b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c
> @@ -0,0 +1,34 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-phiopt1" } */
> +/* PR tree-optimization/112392 */
> +
> +int feq_1(int a, unsigned char b)
> +{
> + int absb = b;
> + if (a == absb) return absb;
> + return a > 0 ? a : -a;
> +}
> +int feq_2(int a, unsigned char b)
> +{
> + int absb = b;
> + if (a == absb) return a;
> + return a > 0 ? a : -a;
> +}
> +
> +int fgt(int a, unsigned char b)
> +{
> + int absb = b;
> + if (a > absb) return a;
> + return a > 0 ? a : -a;
> +}
> +
> +int fge(int a, unsigned char b)
> +{
> + int absb = b;
> + if (a >= absb) return a;
> + return a > 0 ? a : -a;
> +}
> +
> +
> +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 4 "phiopt1" } } */
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-05-08 7:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-08 3:24 [PATCH] match: `a CMP nonnegative ? a : ABS<a>` simplified to just `ABS<a>` [PR112392] Andrew Pinski
2024-05-08 7:53 ` Richard Biener
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).