* [PATCH] match.pd: Fold x == ~x to false [PR96782]
@ 2021-01-03 9:27 Jakub Jelinek
2021-01-04 9:33 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2021-01-03 9:27 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
Hi!
x is never equal to ~x, so we can fold such comparisons to constants.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2021-01-03 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/96782
* match.pd (x == ~x -> false, x != ~x -> true): New simplifications.
* gcc.dg/tree-ssa/pr96782.c: New test.
--- gcc/match.pd.jj 2021-01-01 00:03:14.987440788 +0100
+++ gcc/match.pd 2021-01-02 10:40:34.289209121 +0100
@@ -4045,6 +4045,13 @@ (define_operator_list COND_TERNARY
(if (!flag_trapping_math)
{ constant_boolean_node (false, type); }))
+/* x == ~x -> false */
+/* x != ~x -> true */
+(for cmp (eq ne)
+ (simplify
+ (cmp:c @0 (bit_not @0))
+ { constant_boolean_node (cmp == NE_EXPR, type); }))
+
/* Fold ~X op ~Y as Y op X. */
(for cmp (simple_comparison)
(simplify
--- gcc/testsuite/gcc.dg/tree-ssa/pr96782.c.jj 2021-01-02 10:39:50.740704373 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr96782.c 2021-01-02 10:39:36.268868954 +0100
@@ -0,0 +1,17 @@
+/* PR tree-optimization/96782 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "return 1;" 1 "optimized" } } */
+
+int
+foo (int a)
+{
+ return a == ~a;
+}
+
+int
+bar (int b)
+{
+ return ~b != b;
+}
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] match.pd: Fold x == ~x to false [PR96782]
2021-01-03 9:27 [PATCH] match.pd: Fold x == ~x to false [PR96782] Jakub Jelinek
@ 2021-01-04 9:33 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2021-01-04 9:33 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On Sun, 3 Jan 2021, Jakub Jelinek wrote:
> Hi!
>
> x is never equal to ~x, so we can fold such comparisons to constants.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
OK.
Richard.
> 2021-01-03 Jakub Jelinek <jakub@redhat.com>
>
> PR tree-optimization/96782
> * match.pd (x == ~x -> false, x != ~x -> true): New simplifications.
>
> * gcc.dg/tree-ssa/pr96782.c: New test.
>
> --- gcc/match.pd.jj 2021-01-01 00:03:14.987440788 +0100
> +++ gcc/match.pd 2021-01-02 10:40:34.289209121 +0100
> @@ -4045,6 +4045,13 @@ (define_operator_list COND_TERNARY
> (if (!flag_trapping_math)
> { constant_boolean_node (false, type); }))
>
> +/* x == ~x -> false */
> +/* x != ~x -> true */
> +(for cmp (eq ne)
> + (simplify
> + (cmp:c @0 (bit_not @0))
> + { constant_boolean_node (cmp == NE_EXPR, type); }))
> +
> /* Fold ~X op ~Y as Y op X. */
> (for cmp (simple_comparison)
> (simplify
> --- gcc/testsuite/gcc.dg/tree-ssa/pr96782.c.jj 2021-01-02 10:39:50.740704373 +0100
> +++ gcc/testsuite/gcc.dg/tree-ssa/pr96782.c 2021-01-02 10:39:36.268868954 +0100
> @@ -0,0 +1,17 @@
> +/* PR tree-optimization/96782 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "return 1;" 1 "optimized" } } */
> +
> +int
> +foo (int a)
> +{
> + return a == ~a;
> +}
> +
> +int
> +bar (int b)
> +{
> + return ~b != b;
> +}
>
> Jakub
>
>
--
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Felix Imendörffer; HRB 36809 (AG Nuernberg)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-01-04 9:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-03 9:27 [PATCH] match.pd: Fold x == ~x to false [PR96782] Jakub Jelinek
2021-01-04 9:33 ` 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).