Fix PR110726: a | (a == b) can sometimes produce wrong code
Checks
Commit Message
So I had missed/forgot that EQ_EXPR could have an non boolean
type for generic when I implemented r14-2556-g0407ae8a7732d9.
This patch adds check for one bit precision intergal type
which fixes the problem.
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
PR tree-optimization/110726
gcc/ChangeLog:
* match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)):
Add checks to make sure the type was one bit precision
intergal type.
gcc/testsuite/ChangeLog:
* gcc.c-torture/execute/bitops-1.c: New test.
---
gcc/match.pd | 12 +++++--
.../gcc.c-torture/execute/bitops-1.c | 33 +++++++++++++++++++
2 files changed, 42 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.c-torture/execute/bitops-1.c
Comments
On Wed, Jul 19, 2023 at 3:53 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> So I had missed/forgot that EQ_EXPR could have an non boolean
> type for generic when I implemented r14-2556-g0407ae8a7732d9.
> This patch adds check for one bit precision intergal type
> which fixes the problem.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> PR tree-optimization/110726
>
> gcc/ChangeLog:
>
> * match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)):
> Add checks to make sure the type was one bit precision
> intergal type.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.c-torture/execute/bitops-1.c: New test.
> ---
> gcc/match.pd | 12 +++++--
> .../gcc.c-torture/execute/bitops-1.c | 33 +++++++++++++++++++
> 2 files changed, 42 insertions(+), 3 deletions(-)
> create mode 100644 gcc/testsuite/gcc.c-torture/execute/bitops-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 054e6585876..4dfe92623f7 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1229,7 +1229,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> /* (a | b) & (a == b) --> a & b (boolean version of the above). */
> (simplify
> (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
> - (bit_and @0 @1))
> + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
that's really a constraint on 'type', not sure if it would be clearer
to test that.
What's the nop_convert you've seen in practice here? With integer comparison
result shouldn't those be convert? instead?
> + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> + (bit_and @0 @1)))
>
> /* a | ~(a ^ b) --> a | ~b */
> (simplify
> @@ -1239,7 +1241,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> /* a | (a == b) --> a | (b^1) (boolean version of the above). */
> (simplify
> (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
> - (bit_ior @0 (bit_xor @1 { build_one_cst (type); })))
> + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> + (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
>
> /* (a | b) | (a &^ b) --> a | b */
> (for op (bit_and bit_xor)
> @@ -1255,7 +1259,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> /* (a & b) | (a == b) --> a == b */
> (simplify
> (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
> - @2)
> + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> + @2))
>
> /* ~(~a & b) --> a | ~b */
> (simplify
> diff --git a/gcc/testsuite/gcc.c-torture/execute/bitops-1.c b/gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> new file mode 100644
> index 00000000000..cfaa6b9fd26
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> @@ -0,0 +1,33 @@
> +/* PR tree-optimization/110726 */
> +
> +#define DECLS(n,VOL) \
> +__attribute__((noinline,noclone)) \
> +int h##n(VOL int A, VOL int B){ \
> + return (A | B) & (A == B); \
> +} \
> +__attribute__((noinline,noclone)) \
> +int i##n(VOL int A, VOL int B){ \
> + return A | (A == B); \
> +} \
> +__attribute__((noinline,noclone)) \
> +int k##n(VOL int A, VOL int B){ \
> + return (A & B) | (A == B); \
> +} \
> +
> +DECLS(0,)
> +DECLS(1,volatile)
> +
> +int values[] = { 0, 1, 2, 3, -1, -2, -3, 0x10080 };
> +int numvalues = sizeof(values)/sizeof(values[0]);
> +
> +int main(){
> + for(int A = 0; A < numvalues; A++)
> + for(int B = 0; B < numvalues; B++)
> + {
> + int a = values[A];
> + int b = values[B];
> + if (h0 (a, b) != h1 (a, b)) __builtin_abort();
> + if (i0 (a, b) != i1 (a, b)) __builtin_abort();
> + if (k0 (a, b) != k1 (a, b)) __builtin_abort();
> + }
> +}
> --
> 2.31.1
>
On Wed, Jul 19, 2023 at 12:16 AM Richard Biener via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Wed, Jul 19, 2023 at 3:53 AM Andrew Pinski via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > So I had missed/forgot that EQ_EXPR could have an non boolean
> > type for generic when I implemented r14-2556-g0407ae8a7732d9.
> > This patch adds check for one bit precision intergal type
> > which fixes the problem.
> >
> > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
> >
> > PR tree-optimization/110726
> >
> > gcc/ChangeLog:
> >
> > * match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)):
> > Add checks to make sure the type was one bit precision
> > intergal type.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.c-torture/execute/bitops-1.c: New test.
> > ---
> > gcc/match.pd | 12 +++++--
> > .../gcc.c-torture/execute/bitops-1.c | 33 +++++++++++++++++++
> > 2 files changed, 42 insertions(+), 3 deletions(-)
> > create mode 100644 gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 054e6585876..4dfe92623f7 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -1229,7 +1229,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > /* (a | b) & (a == b) --> a & b (boolean version of the above). */
> > (simplify
> > (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
> > - (bit_and @0 @1))
> > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>
> that's really a constraint on 'type', not sure if it would be clearer
> to test that.
> What's the nop_convert you've seen in practice here? With integer comparison
> result shouldn't those be convert? instead?
The case were nop_convert happen would happen is:
```
struct f
{
signed a:1;
signed b:1;
signed c:1;
};
void f(struct f t, struct f *t1)
{
t.c = (t.a == t.b);
t1->c = (t.a | t.b) & t.c;
}
```
I know the above does not show up much but it definitely can show up.
In the testcase in the bug report the EQ_EXPR didn't have convert
around it but the type was just changed to be the long type and we
cannot treat the `a == b` similar as `~(a^b)` which is only works for
1 bit integers (unsigned or signed).
Thanks,
Andrew Pinski
>
> > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> > + (bit_and @0 @1)))
> >
> > /* a | ~(a ^ b) --> a | ~b */
> > (simplify
> > @@ -1239,7 +1241,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > /* a | (a == b) --> a | (b^1) (boolean version of the above). */
> > (simplify
> > (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
> > - (bit_ior @0 (bit_xor @1 { build_one_cst (type); })))
> > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> > + (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
> >
> > /* (a | b) | (a &^ b) --> a | b */
> > (for op (bit_and bit_xor)
> > @@ -1255,7 +1259,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > /* (a & b) | (a == b) --> a == b */
> > (simplify
> > (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
> > - @2)
> > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> > + @2))
> >
> > /* ~(~a & b) --> a | ~b */
> > (simplify
> > diff --git a/gcc/testsuite/gcc.c-torture/execute/bitops-1.c b/gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> > new file mode 100644
> > index 00000000000..cfaa6b9fd26
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> > @@ -0,0 +1,33 @@
> > +/* PR tree-optimization/110726 */
> > +
> > +#define DECLS(n,VOL) \
> > +__attribute__((noinline,noclone)) \
> > +int h##n(VOL int A, VOL int B){ \
> > + return (A | B) & (A == B); \
> > +} \
> > +__attribute__((noinline,noclone)) \
> > +int i##n(VOL int A, VOL int B){ \
> > + return A | (A == B); \
> > +} \
> > +__attribute__((noinline,noclone)) \
> > +int k##n(VOL int A, VOL int B){ \
> > + return (A & B) | (A == B); \
> > +} \
> > +
> > +DECLS(0,)
> > +DECLS(1,volatile)
> > +
> > +int values[] = { 0, 1, 2, 3, -1, -2, -3, 0x10080 };
> > +int numvalues = sizeof(values)/sizeof(values[0]);
> > +
> > +int main(){
> > + for(int A = 0; A < numvalues; A++)
> > + for(int B = 0; B < numvalues; B++)
> > + {
> > + int a = values[A];
> > + int b = values[B];
> > + if (h0 (a, b) != h1 (a, b)) __builtin_abort();
> > + if (i0 (a, b) != i1 (a, b)) __builtin_abort();
> > + if (k0 (a, b) != k1 (a, b)) __builtin_abort();
> > + }
> > +}
> > --
> > 2.31.1
> >
On Wed, Jul 19, 2023 at 9:32 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> On Wed, Jul 19, 2023 at 12:16 AM Richard Biener via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > On Wed, Jul 19, 2023 at 3:53 AM Andrew Pinski via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> > >
> > > So I had missed/forgot that EQ_EXPR could have an non boolean
> > > type for generic when I implemented r14-2556-g0407ae8a7732d9.
> > > This patch adds check for one bit precision intergal type
> > > which fixes the problem.
> > >
> > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
> > >
> > > PR tree-optimization/110726
> > >
> > > gcc/ChangeLog:
> > >
> > > * match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)):
> > > Add checks to make sure the type was one bit precision
> > > intergal type.
> > >
> > > gcc/testsuite/ChangeLog:
> > >
> > > * gcc.c-torture/execute/bitops-1.c: New test.
> > > ---
> > > gcc/match.pd | 12 +++++--
> > > .../gcc.c-torture/execute/bitops-1.c | 33 +++++++++++++++++++
> > > 2 files changed, 42 insertions(+), 3 deletions(-)
> > > create mode 100644 gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> > >
> > > diff --git a/gcc/match.pd b/gcc/match.pd
> > > index 054e6585876..4dfe92623f7 100644
> > > --- a/gcc/match.pd
> > > +++ b/gcc/match.pd
> > > @@ -1229,7 +1229,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > > /* (a | b) & (a == b) --> a & b (boolean version of the above). */
> > > (simplify
> > > (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
> > > - (bit_and @0 @1))
> > > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> >
> > that's really a constraint on 'type', not sure if it would be clearer
> > to test that.
> > What's the nop_convert you've seen in practice here? With integer comparison
> > result shouldn't those be convert? instead?
>
> The case were nop_convert happen would happen is:
> ```
> struct f
> {
> signed a:1;
> signed b:1;
> signed c:1;
> };
>
> void f(struct f t, struct f *t1)
> {
> t.c = (t.a == t.b);
> t1->c = (t.a | t.b) & t.c;
> }
> ```
> I know the above does not show up much but it definitely can show up.
>
> In the testcase in the bug report the EQ_EXPR didn't have convert
> around it but the type was just changed to be the long type and we
> cannot treat the `a == b` similar as `~(a^b)` which is only works for
> 1 bit integers (unsigned or signed).
I see, the patch is OK then.
Thanks,
Richard.
> Thanks,
> Andrew Pinski
>
> >
> > > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> > > + (bit_and @0 @1)))
> > >
> > > /* a | ~(a ^ b) --> a | ~b */
> > > (simplify
> > > @@ -1239,7 +1241,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > > /* a | (a == b) --> a | (b^1) (boolean version of the above). */
> > > (simplify
> > > (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
> > > - (bit_ior @0 (bit_xor @1 { build_one_cst (type); })))
> > > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> > > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> > > + (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
> > >
> > > /* (a | b) | (a &^ b) --> a | b */
> > > (for op (bit_and bit_xor)
> > > @@ -1255,7 +1259,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > > /* (a & b) | (a == b) --> a == b */
> > > (simplify
> > > (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
> > > - @2)
> > > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> > > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
> > > + @2))
> > >
> > > /* ~(~a & b) --> a | ~b */
> > > (simplify
> > > diff --git a/gcc/testsuite/gcc.c-torture/execute/bitops-1.c b/gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> > > new file mode 100644
> > > index 00000000000..cfaa6b9fd26
> > > --- /dev/null
> > > +++ b/gcc/testsuite/gcc.c-torture/execute/bitops-1.c
> > > @@ -0,0 +1,33 @@
> > > +/* PR tree-optimization/110726 */
> > > +
> > > +#define DECLS(n,VOL) \
> > > +__attribute__((noinline,noclone)) \
> > > +int h##n(VOL int A, VOL int B){ \
> > > + return (A | B) & (A == B); \
> > > +} \
> > > +__attribute__((noinline,noclone)) \
> > > +int i##n(VOL int A, VOL int B){ \
> > > + return A | (A == B); \
> > > +} \
> > > +__attribute__((noinline,noclone)) \
> > > +int k##n(VOL int A, VOL int B){ \
> > > + return (A & B) | (A == B); \
> > > +} \
> > > +
> > > +DECLS(0,)
> > > +DECLS(1,volatile)
> > > +
> > > +int values[] = { 0, 1, 2, 3, -1, -2, -3, 0x10080 };
> > > +int numvalues = sizeof(values)/sizeof(values[0]);
> > > +
> > > +int main(){
> > > + for(int A = 0; A < numvalues; A++)
> > > + for(int B = 0; B < numvalues; B++)
> > > + {
> > > + int a = values[A];
> > > + int b = values[B];
> > > + if (h0 (a, b) != h1 (a, b)) __builtin_abort();
> > > + if (i0 (a, b) != i1 (a, b)) __builtin_abort();
> > > + if (k0 (a, b) != k1 (a, b)) __builtin_abort();
> > > + }
> > > +}
> > > --
> > > 2.31.1
> > >
@@ -1229,7 +1229,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* (a | b) & (a == b) --> a & b (boolean version of the above). */
(simplify
(bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
- (bit_and @0 @1))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
+ (bit_and @0 @1)))
/* a | ~(a ^ b) --> a | ~b */
(simplify
@@ -1239,7 +1241,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* a | (a == b) --> a | (b^1) (boolean version of the above). */
(simplify
(bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
- (bit_ior @0 (bit_xor @1 { build_one_cst (type); })))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
+ (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
/* (a | b) | (a &^ b) --> a | b */
(for op (bit_and bit_xor)
@@ -1255,7 +1259,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* (a & b) | (a == b) --> a == b */
(simplify
(bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
- @2)
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
+ @2))
/* ~(~a & b) --> a | ~b */
(simplify
new file mode 100644
@@ -0,0 +1,33 @@
+/* PR tree-optimization/110726 */
+
+#define DECLS(n,VOL) \
+__attribute__((noinline,noclone)) \
+int h##n(VOL int A, VOL int B){ \
+ return (A | B) & (A == B); \
+} \
+__attribute__((noinline,noclone)) \
+int i##n(VOL int A, VOL int B){ \
+ return A | (A == B); \
+} \
+__attribute__((noinline,noclone)) \
+int k##n(VOL int A, VOL int B){ \
+ return (A & B) | (A == B); \
+} \
+
+DECLS(0,)
+DECLS(1,volatile)
+
+int values[] = { 0, 1, 2, 3, -1, -2, -3, 0x10080 };
+int numvalues = sizeof(values)/sizeof(values[0]);
+
+int main(){
+ for(int A = 0; A < numvalues; A++)
+ for(int B = 0; B < numvalues; B++)
+ {
+ int a = values[A];
+ int b = values[B];
+ if (h0 (a, b) != h1 (a, b)) __builtin_abort();
+ if (i0 (a, b) != i1 (a, b)) __builtin_abort();
+ if (k0 (a, b) != k1 (a, b)) __builtin_abort();
+ }
+}