* [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool
@ 2022-07-21 11:23 Richard Biener
2022-07-22 20:15 ` H.J. Lu
0 siblings, 1 reply; 5+ messages in thread
From: Richard Biener @ 2022-07-21 11:23 UTC (permalink / raw)
To: gcc-patches
The following makes sure to fold ~(a ^ b) to a == b for truth
values (but not vectors, we'd have to check for vector support of
equality). That turns the PR106379 testcase into a ranger one.
Note that while we arrive at ~(a ^ b) in a convoluted way from
original !a == !b one can eventually write the expression this
way directly as well.
Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
PR tree-optimization/106379
* match.pd (~(a ^ b) -> a == b): New pattern.
* gcc.dg/pr106379-1.c: New testcase.
---
gcc/match.pd | 6 ++++++
gcc/testsuite/gcc.dg/pr106379-1.c | 9 +++++++++
2 files changed, 15 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/pr106379-1.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 8bbc0dbd5cd..88a1a5aa9cc 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1938,6 +1938,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
(bit_not (bit_xor (view_convert @0) @1))))
+/* ~(a ^ b) is a == b for truth valued a and b. */
+(simplify
+ (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
+ (if (!VECTOR_TYPE_P (type))
+ (convert (eq @0 @1))))
+
/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
(simplify
(bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
diff --git a/gcc/testsuite/gcc.dg/pr106379-1.c b/gcc/testsuite/gcc.dg/pr106379-1.c
new file mode 100644
index 00000000000..7f2575e02dc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr106379-1.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-forwprop1" } */
+
+_Bool foo (_Bool a, _Bool b)
+{
+ return !a == !b;
+}
+
+/* { dg-final { scan-tree-dump "\[ab\]_\[0-9\]+\\(D\\) == \[ba\]_\[0-9\]+\\(D\\)" "forwprop1" } } */
--
2.35.3
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool
2022-07-21 11:23 [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool Richard Biener
@ 2022-07-22 20:15 ` H.J. Lu
2022-07-23 6:09 ` Richard Biener
0 siblings, 1 reply; 5+ messages in thread
From: H.J. Lu @ 2022-07-22 20:15 UTC (permalink / raw)
To: Richard Biener; +Cc: GCC Patches
On Thu, Jul 21, 2022 at 4:24 AM Richard Biener via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> The following makes sure to fold ~(a ^ b) to a == b for truth
> values (but not vectors, we'd have to check for vector support of
> equality). That turns the PR106379 testcase into a ranger one.
>
> Note that while we arrive at ~(a ^ b) in a convoluted way from
> original !a == !b one can eventually write the expression this
> way directly as well.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
>
> PR tree-optimization/106379
> * match.pd (~(a ^ b) -> a == b): New pattern.
>
> * gcc.dg/pr106379-1.c: New testcase.
> ---
> gcc/match.pd | 6 ++++++
> gcc/testsuite/gcc.dg/pr106379-1.c | 9 +++++++++
> 2 files changed, 15 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/pr106379-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 8bbc0dbd5cd..88a1a5aa9cc 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1938,6 +1938,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
> (bit_not (bit_xor (view_convert @0) @1))))
>
> +/* ~(a ^ b) is a == b for truth valued a and b. */
> +(simplify
> + (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
> + (if (!VECTOR_TYPE_P (type))
> + (convert (eq @0 @1))))
For integers, isn't it wrong to convert ~(boolean exp) to boolean exp?
> /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
> (simplify
> (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
> diff --git a/gcc/testsuite/gcc.dg/pr106379-1.c b/gcc/testsuite/gcc.dg/pr106379-1.c
> new file mode 100644
> index 00000000000..7f2575e02dc
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106379-1.c
> @@ -0,0 +1,9 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O -fdump-tree-forwprop1" } */
> +
> +_Bool foo (_Bool a, _Bool b)
> +{
> + return !a == !b;
> +}
> +
> +/* { dg-final { scan-tree-dump "\[ab\]_\[0-9\]+\\(D\\) == \[ba\]_\[0-9\]+\\(D\\)" "forwprop1" } } */
> --
> 2.35.3
--
H.J.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool
2022-07-22 20:15 ` H.J. Lu
@ 2022-07-23 6:09 ` Richard Biener
2022-07-25 14:05 ` H.J. Lu
0 siblings, 1 reply; 5+ messages in thread
From: Richard Biener @ 2022-07-23 6:09 UTC (permalink / raw)
To: H.J. Lu via Gcc-patches
> Am 22.07.2022 um 22:17 schrieb H.J. Lu via Gcc-patches <gcc-patches@gcc.gnu.org>:
>
> On Thu, Jul 21, 2022 at 4:24 AM Richard Biener via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>>
>> The following makes sure to fold ~(a ^ b) to a == b for truth
>> values (but not vectors, we'd have to check for vector support of
>> equality). That turns the PR106379 testcase into a ranger one.
>>
>> Note that while we arrive at ~(a ^ b) in a convoluted way from
>> original !a == !b one can eventually write the expression this
>> way directly as well.
>>
>> Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
>>
>> PR tree-optimization/106379
>> * match.pd (~(a ^ b) -> a == b): New pattern.
>>
>> * gcc.dg/pr106379-1.c: New testcase.
>> ---
>> gcc/match.pd | 6 ++++++
>> gcc/testsuite/gcc.dg/pr106379-1.c | 9 +++++++++
>> 2 files changed, 15 insertions(+)
>> create mode 100644 gcc/testsuite/gcc.dg/pr106379-1.c
>>
>> diff --git a/gcc/match.pd b/gcc/match.pd
>> index 8bbc0dbd5cd..88a1a5aa9cc 100644
>> --- a/gcc/match.pd
>> +++ b/gcc/match.pd
>> @@ -1938,6 +1938,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>> (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
>> (bit_not (bit_xor (view_convert @0) @1))))
>>
>> +/* ~(a ^ b) is a == b for truth valued a and b. */
>> +(simplify
>> + (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
>> + (if (!VECTOR_TYPE_P (type))
>> + (convert (eq @0 @1))))
>
> For integers, isn't it wrong to convert ~(boolean exp) to boolean exp?
That’s what the (convert. …) should compensate for?
Richard
>
>> /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
>> (simplify
>> (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
>> diff --git a/gcc/testsuite/gcc.dg/pr106379-1.c b/gcc/testsuite/gcc.dg/pr106379-1.c
>> new file mode 100644
>> index 00000000000..7f2575e02dc
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.dg/pr106379-1.c
>> @@ -0,0 +1,9 @@
>> +/* { dg-do compile } */
>> +/* { dg-options "-O -fdump-tree-forwprop1" } */
>> +
>> +_Bool foo (_Bool a, _Bool b)
>> +{
>> + return !a == !b;
>> +}
>> +
>> +/* { dg-final { scan-tree-dump "\[ab\]_\[0-9\]+\\(D\\) == \[ba\]_\[0-9\]+\\(D\\)" "forwprop1" } } */
>> --
>> 2.35.3
>
>
>
> --
> H.J.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool
2022-07-23 6:09 ` Richard Biener
@ 2022-07-25 14:05 ` H.J. Lu
2022-07-25 14:49 ` Richard Biener
0 siblings, 1 reply; 5+ messages in thread
From: H.J. Lu @ 2022-07-25 14:05 UTC (permalink / raw)
To: Richard Biener; +Cc: H.J. Lu via Gcc-patches
On Fri, Jul 22, 2022 at 11:10 PM Richard Biener via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
>
>
> > Am 22.07.2022 um 22:17 schrieb H.J. Lu via Gcc-patches <gcc-patches@gcc.gnu.org>:
> >
> > On Thu, Jul 21, 2022 at 4:24 AM Richard Biener via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> >>
> >> The following makes sure to fold ~(a ^ b) to a == b for truth
> >> values (but not vectors, we'd have to check for vector support of
> >> equality). That turns the PR106379 testcase into a ranger one.
> >>
> >> Note that while we arrive at ~(a ^ b) in a convoluted way from
> >> original !a == !b one can eventually write the expression this
> >> way directly as well.
> >>
> >> Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
> >>
> >> PR tree-optimization/106379
> >> * match.pd (~(a ^ b) -> a == b): New pattern.
> >>
> >> * gcc.dg/pr106379-1.c: New testcase.
> >> ---
> >> gcc/match.pd | 6 ++++++
> >> gcc/testsuite/gcc.dg/pr106379-1.c | 9 +++++++++
> >> 2 files changed, 15 insertions(+)
> >> create mode 100644 gcc/testsuite/gcc.dg/pr106379-1.c
> >>
> >> diff --git a/gcc/match.pd b/gcc/match.pd
> >> index 8bbc0dbd5cd..88a1a5aa9cc 100644
> >> --- a/gcc/match.pd
> >> +++ b/gcc/match.pd
> >> @@ -1938,6 +1938,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >> (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
> >> (bit_not (bit_xor (view_convert @0) @1))))
> >>
> >> +/* ~(a ^ b) is a == b for truth valued a and b. */
> >> +(simplify
> >> + (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
> >> + (if (!VECTOR_TYPE_P (type))
> >> + (convert (eq @0 @1))))
> >
> > For integers, isn't it wrong to convert ~(boolean exp) to boolean exp?
>
> That’s what the (convert. …) should compensate for?
Is ~(boolean exp) == ~((int) (boolean exp)) or (int) (~(boolean exp))?
> Richard
>
> >
> >> /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
> >> (simplify
> >> (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
> >> diff --git a/gcc/testsuite/gcc.dg/pr106379-1.c b/gcc/testsuite/gcc.dg/pr106379-1.c
> >> new file mode 100644
> >> index 00000000000..7f2575e02dc
> >> --- /dev/null
> >> +++ b/gcc/testsuite/gcc.dg/pr106379-1.c
> >> @@ -0,0 +1,9 @@
> >> +/* { dg-do compile } */
> >> +/* { dg-options "-O -fdump-tree-forwprop1" } */
> >> +
> >> +_Bool foo (_Bool a, _Bool b)
> >> +{
> >> + return !a == !b;
> >> +}
> >> +
> >> +/* { dg-final { scan-tree-dump "\[ab\]_\[0-9\]+\\(D\\) == \[ba\]_\[0-9\]+\\(D\\)" "forwprop1" } } */
> >> --
> >> 2.35.3
> >
> >
> >
> > --
> > H.J.
--
H.J.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool
2022-07-25 14:05 ` H.J. Lu
@ 2022-07-25 14:49 ` Richard Biener
0 siblings, 0 replies; 5+ messages in thread
From: Richard Biener @ 2022-07-25 14:49 UTC (permalink / raw)
To: H.J. Lu; +Cc: H.J. Lu via Gcc-patches
On Mon, 25 Jul 2022, H.J. Lu wrote:
> On Fri, Jul 22, 2022 at 11:10 PM Richard Biener via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> >
> >
> > > Am 22.07.2022 um 22:17 schrieb H.J. Lu via Gcc-patches <gcc-patches@gcc.gnu.org>:
> > >
> > > On Thu, Jul 21, 2022 at 4:24 AM Richard Biener via Gcc-patches
> > > <gcc-patches@gcc.gnu.org> wrote:
> > >>
> > >> The following makes sure to fold ~(a ^ b) to a == b for truth
> > >> values (but not vectors, we'd have to check for vector support of
> > >> equality). That turns the PR106379 testcase into a ranger one.
> > >>
> > >> Note that while we arrive at ~(a ^ b) in a convoluted way from
> > >> original !a == !b one can eventually write the expression this
> > >> way directly as well.
> > >>
> > >> Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
> > >>
> > >> PR tree-optimization/106379
> > >> * match.pd (~(a ^ b) -> a == b): New pattern.
> > >>
> > >> * gcc.dg/pr106379-1.c: New testcase.
> > >> ---
> > >> gcc/match.pd | 6 ++++++
> > >> gcc/testsuite/gcc.dg/pr106379-1.c | 9 +++++++++
> > >> 2 files changed, 15 insertions(+)
> > >> create mode 100644 gcc/testsuite/gcc.dg/pr106379-1.c
> > >>
> > >> diff --git a/gcc/match.pd b/gcc/match.pd
> > >> index 8bbc0dbd5cd..88a1a5aa9cc 100644
> > >> --- a/gcc/match.pd
> > >> +++ b/gcc/match.pd
> > >> @@ -1938,6 +1938,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > >> (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
> > >> (bit_not (bit_xor (view_convert @0) @1))))
> > >>
> > >> +/* ~(a ^ b) is a == b for truth valued a and b. */
> > >> +(simplify
> > >> + (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
> > >> + (if (!VECTOR_TYPE_P (type))
> > >> + (convert (eq @0 @1))))
> > >
> > > For integers, isn't it wrong to convert ~(boolean exp) to boolean exp?
> >
> > That’s what the (convert. …) should compensate for?
>
> Is ~(boolean exp) == ~((int) (boolean exp)) or (int) (~(boolean exp))?
Depends on what 'boolean exp' is, and as PR106414 shows I was wrong
because in C a == b has type 'int' and not type bool but we consider
it "boolean". There of course ~(a == b) is not equal to a != b.
Richard.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-07-25 14:49 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-21 11:23 [PATCH] tree-optimization/106379 - add missing ~(a ^ b) folding for _Bool Richard Biener
2022-07-22 20:15 ` H.J. Lu
2022-07-23 6:09 ` Richard Biener
2022-07-25 14:05 ` H.J. Lu
2022-07-25 14: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).