* [PATCH] match.pd: Add (-(X < 0) | 1) * X -> abs (X) etc. simplifications [PR94785]
@ 2020-12-31 9:34 Jakub Jelinek
2020-12-31 9:56 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2020-12-31 9:34 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
Hi!
The following patch adds two simplifications to recognize idioms
for ABS_EXPR resp. ABSU_EXPR.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2020-12-31 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/94785
* match.pd ((-(X < 0) | 1) * X -> abs (X)): New simplification.
((-(X < 0) | 1U) * X -> absu (X)): Likewise.
* gcc.dg/tree-ssa/pr94785.c: New test.
--- gcc/match.pd.jj 2020-12-15 22:43:39.254276743 +0100
+++ gcc/match.pd 2020-12-30 12:00:00.924301784 +0100
@@ -1374,6 +1374,22 @@ (define_operator_list COND_TERNARY
(absu tree_expr_nonnegative_p@0)
(convert @0))
+/* Simplify (-(X < 0) | 1) * X into abs (X). */
+(simplify
+ (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
+ (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
+ (abs @0)))
+
+/* Similarly (-(X < 0) | 1U) * X into absu (X). */
+(simplify
+ (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
+ integer_onep) (nop_convert @0))
+ (if (INTEGRAL_TYPE_P (type)
+ && TYPE_UNSIGNED (type)
+ && INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (absu @0)))
+
/* A few cases of fold-const.c negate_expr_p predicate. */
(match negate_expr_p
INTEGER_CST
--- gcc/testsuite/gcc.dg/tree-ssa/pr94785.c.jj 2020-12-30 12:03:45.693776141 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr94785.c 2020-12-30 12:04:26.895313170 +0100
@@ -0,0 +1,36 @@
+/* PR tree-optimization/94785 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times " = ABS_EXPR <v_\[0-9]*\\\(D\\\)>;" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " = ABSU_EXPR <v_\[0-9]*\\\(D\\\)>;" 2 "optimized" } } */
+
+int
+f1 (int v)
+{
+ return (1 | -(v < 0)) * v;
+}
+
+unsigned
+f2 (int v)
+{
+ return (1U | -(v < 0)) * v;
+}
+
+int
+f3 (int v)
+{
+ int a = v < 0;
+ int b = -a;
+ int c = 1 | b;
+ return c * v;
+}
+
+unsigned
+f4 (int v)
+{
+ int a = v < 0;
+ int b = -a;
+ unsigned c = b;
+ unsigned d = c | 1;
+ return d * v;
+}
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] match.pd: Add (-(X < 0) | 1) * X -> abs (X) etc. simplifications [PR94785]
2020-12-31 9:34 [PATCH] match.pd: Add (-(X < 0) | 1) * X -> abs (X) etc. simplifications [PR94785] Jakub Jelinek
@ 2020-12-31 9:56 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2020-12-31 9:56 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On December 31, 2020 10:34:34 AM GMT+01:00, Jakub Jelinek <jakub@redhat.com> wrote:
>Hi!
>
>The following patch adds two simplifications to recognize idioms
>for ABS_EXPR resp. ABSU_EXPR.
>
>Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
Ok.
Richard.
>2020-12-31 Jakub Jelinek <jakub@redhat.com>
>
> PR tree-optimization/94785
> * match.pd ((-(X < 0) | 1) * X -> abs (X)): New simplification.
> ((-(X < 0) | 1U) * X -> absu (X)): Likewise.
>
> * gcc.dg/tree-ssa/pr94785.c: New test.
>
>--- gcc/match.pd.jj 2020-12-15 22:43:39.254276743 +0100
>+++ gcc/match.pd 2020-12-30 12:00:00.924301784 +0100
>@@ -1374,6 +1374,22 @@ (define_operator_list COND_TERNARY
> (absu tree_expr_nonnegative_p@0)
> (convert @0))
>
>+/* Simplify (-(X < 0) | 1) * X into abs (X). */
>+(simplify
>+ (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop)))
>integer_onep) @0)
>+ (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
>+ (abs @0)))
>+
>+/* Similarly (-(X < 0) | 1U) * X into absu (X). */
>+(simplify
>+ (mult:c (bit_ior (nop_convert (negate (convert? (lt @0
>integer_zerop))))
>+ integer_onep) (nop_convert @0))
>+ (if (INTEGRAL_TYPE_P (type)
>+ && TYPE_UNSIGNED (type)
>+ && INTEGRAL_TYPE_P (TREE_TYPE (@0))
>+ && !TYPE_UNSIGNED (TREE_TYPE (@0)))
>+ (absu @0)))
>+
> /* A few cases of fold-const.c negate_expr_p predicate. */
> (match negate_expr_p
> INTEGER_CST
>--- gcc/testsuite/gcc.dg/tree-ssa/pr94785.c.jj 2020-12-30
>12:03:45.693776141 +0100
>+++ gcc/testsuite/gcc.dg/tree-ssa/pr94785.c 2020-12-30
>12:04:26.895313170 +0100
>@@ -0,0 +1,36 @@
>+/* PR tree-optimization/94785 */
>+/* { dg-do compile } */
>+/* { dg-options "-O2 -fdump-tree-optimized" } */
>+/* { dg-final { scan-tree-dump-times " = ABS_EXPR
><v_\[0-9]*\\\(D\\\)>;" 2 "optimized" } } */
>+/* { dg-final { scan-tree-dump-times " = ABSU_EXPR
><v_\[0-9]*\\\(D\\\)>;" 2 "optimized" } } */
>+
>+int
>+f1 (int v)
>+{
>+ return (1 | -(v < 0)) * v;
>+}
>+
>+unsigned
>+f2 (int v)
>+{
>+ return (1U | -(v < 0)) * v;
>+}
>+
>+int
>+f3 (int v)
>+{
>+ int a = v < 0;
>+ int b = -a;
>+ int c = 1 | b;
>+ return c * v;
>+}
>+
>+unsigned
>+f4 (int v)
>+{
>+ int a = v < 0;
>+ int b = -a;
>+ unsigned c = b;
>+ unsigned d = c | 1;
>+ return d * v;
>+}
>
> Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-12-31 9:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-31 9:34 [PATCH] match.pd: Add (-(X < 0) | 1) * X -> abs (X) etc. simplifications [PR94785] Jakub Jelinek
2020-12-31 9:56 ` 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).