* [PATCH] match.pd: Optimize (x < 0) ^ (y < 0) to (x ^ y) < 0 etc. [PR96681]
@ 2021-01-15 18:25 Jakub Jelinek
2021-01-15 19:49 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2021-01-15 18:25 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
Hi!
This patch simplifies comparisons that test the sign bit xored together.
If the comparisons are both < 0 or both >= 0, then we should xor the operands
together and compare the result to < 0, if the comparisons are different,
we should compare to >= 0.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2021-01-15 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/96681
* match.pd ((x < 0) ^ (y < 0) to (x ^ y) < 0): New simplification.
((x >= 0) ^ (y >= 0) to (x ^ y) < 0): Likewise.
((x < 0) ^ (y >= 0) to (x ^ y) >= 0): Likewise.
((x >= 0) ^ (y < 0) to (x ^ y) >= 0): Likewise.
* gcc.dg/tree-ssa/pr96681.c: New test.
--- gcc/match.pd.jj 2021-01-15 13:12:11.232019067 +0100
+++ gcc/match.pd 2021-01-15 14:00:21.567135280 +0100
@@ -3993,6 +3993,24 @@ (define_operator_list COND_TERNARY
(if (single_use (@2))
(cmp @0 @1)))))
+/* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
+ (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
+(for cmp (lt ge)
+ (simplify
+ (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+ (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
+/* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
+ (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
+(simplify
+ (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+ (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
+
/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
signed arithmetic case. That form is created by the compiler
often enough for folding it to be of value. One example is in
--- gcc/testsuite/gcc.dg/tree-ssa/pr96681.c.jj 2021-01-15 14:16:25.254366911 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr96681.c 2021-01-15 14:18:30.618941775 +0100
@@ -0,0 +1,35 @@
+/* PR tree-optimization/96681 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times " \\^ " 5 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " (?:<|>=) 0" 5 "optimized" } } */
+
+int
+foo (int x, int y)
+{
+ return (x < 0) ^ (y < 0);
+}
+
+int
+bar (int x, int y)
+{
+ return (x > -1) ^ (y > -1);
+}
+
+int
+baz (int x, int y)
+{
+ return (x ^ y) < 0;
+}
+
+int
+qux (int x, int y)
+{
+ return (x ^ y) >= 0;
+}
+
+int
+corge (int x, int y)
+{
+ return (x >= 0) ^ (y < 0);
+}
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] match.pd: Optimize (x < 0) ^ (y < 0) to (x ^ y) < 0 etc. [PR96681]
2021-01-15 18:25 [PATCH] match.pd: Optimize (x < 0) ^ (y < 0) to (x ^ y) < 0 etc. [PR96681] Jakub Jelinek
@ 2021-01-15 19:49 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2021-01-15 19:49 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On January 15, 2021 7:25:33 PM GMT+01:00, Jakub Jelinek <jakub@redhat.com> wrote:
>Hi!
>
>This patch simplifies comparisons that test the sign bit xored
>together.
>If the comparisons are both < 0 or both >= 0, then we should xor the
>operands
>together and compare the result to < 0, if the comparisons are
>different,
>we should compare to >= 0.
>
>Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
Ok.
Thanks,
Richard.
>2021-01-15 Jakub Jelinek <jakub@redhat.com>
>
> PR tree-optimization/96681
> * match.pd ((x < 0) ^ (y < 0) to (x ^ y) < 0): New simplification.
> ((x >= 0) ^ (y >= 0) to (x ^ y) < 0): Likewise.
> ((x < 0) ^ (y >= 0) to (x ^ y) >= 0): Likewise.
> ((x >= 0) ^ (y < 0) to (x ^ y) >= 0): Likewise.
>
> * gcc.dg/tree-ssa/pr96681.c: New test.
>
>--- gcc/match.pd.jj 2021-01-15 13:12:11.232019067 +0100
>+++ gcc/match.pd 2021-01-15 14:00:21.567135280 +0100
>@@ -3993,6 +3993,24 @@ (define_operator_list COND_TERNARY
> (if (single_use (@2))
> (cmp @0 @1)))))
>
>+/* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
>+ (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
>+(for cmp (lt ge)
>+ (simplify
>+ (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
>+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
>+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
>+ (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
>+/* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
>+ (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
>+(simplify
>+ (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
>+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
>+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
>+ (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
>+
> /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
> signed arithmetic case. That form is created by the compiler
> often enough for folding it to be of value. One example is in
>--- gcc/testsuite/gcc.dg/tree-ssa/pr96681.c.jj 2021-01-15
>14:16:25.254366911 +0100
>+++ gcc/testsuite/gcc.dg/tree-ssa/pr96681.c 2021-01-15
>14:18:30.618941775 +0100
>@@ -0,0 +1,35 @@
>+/* PR tree-optimization/96681 */
>+/* { dg-do compile } */
>+/* { dg-options "-O2 -fdump-tree-optimized" } */
>+/* { dg-final { scan-tree-dump-times " \\^ " 5 "optimized" } } */
>+/* { dg-final { scan-tree-dump-times " (?:<|>=) 0" 5 "optimized" } }
>*/
>+
>+int
>+foo (int x, int y)
>+{
>+ return (x < 0) ^ (y < 0);
>+}
>+
>+int
>+bar (int x, int y)
>+{
>+ return (x > -1) ^ (y > -1);
>+}
>+
>+int
>+baz (int x, int y)
>+{
>+ return (x ^ y) < 0;
>+}
>+
>+int
>+qux (int x, int y)
>+{
>+ return (x ^ y) >= 0;
>+}
>+
>+int
>+corge (int x, int y)
>+{
>+ return (x >= 0) ^ (y < 0);
>+}
>
> Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-01-15 19:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-15 18:25 [PATCH] match.pd: Optimize (x < 0) ^ (y < 0) to (x ^ y) < 0 etc. [PR96681] Jakub Jelinek
2021-01-15 19:49 ` 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).