On Mon, Nov 9, 2026 at 10:41 AM Andrea Pinski
<[email protected]> wrote:
>
> On Mon, Nov 9, 2026 at 6:39 AM <[email protected]> wrote:
> >
> > From: Kyrylo Tkachov <[email protected]>
> >
> > The folds of x ==/!= (typeof x)(x ==/!= CST) assume that the converted
> > comparison is 0 or 1. If x or the comparison has a signed one-bit type,
> > true converts to -1. C2Y allows signed _BitInt(1), and for
> >
> > typedef signed _BitInt(1) S;
> > int f (S a) { return a != (S) (a == -1); }
> >
> > the fold gives a != 0, so f (-1) is 1 instead of 0 at -O1 and above.
> > A comparison with a signed one-bit result type in GIMPLE gives wrong
> > code in the same way.
> >
> > Fold only if x and the comparison have unsigned types or types wider
> > than one bit.
> >
> > Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu.
> > Ok for trunk?
> > Thanks,
> > Kyrill
> >
> > gcc/ChangeLog:
> >
> > * match.pd (x ==/!= (typeof x)(x ==/!= CST)): Require that x and
> > the comparison have the value 1.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.dg/torture/pr110293-bitint.c: New test.
> >
> > Signed-off-by: Kyrylo Tkachov <[email protected]>
> > ---
> > gcc/match.pd | 55 +++++++++++--------
> > .../gcc.dg/torture/pr110293-bitint.c | 48 ++++++++++++++++
> > 2 files changed, 79 insertions(+), 24 deletions(-)
> > create mode 100644 gcc/testsuite/gcc.dg/torture/pr110293-bitint.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 3912f9f7d54..b8170597310 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -9533,38 +9533,45 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) :
> > x != 1) */
> > /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) :
> > x == 0) */
> > /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) :
> > x == 1) */
> > +/* These need (typeof x)(x CMP CST) to be 0 or 1. A signed one-bit type
> > + has no value 1, so x and the comparison must not have such a type. */
> > (for outer (ne eq)
> > (for inner (ne eq)
> > (simplify
> > - (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
> > - (with {
Seems like there might be a simpler way of handling this than the
above. For an example for gimple, @2 will always be
boolean_trype_node. So maybe this can just be done for gimple only
and just need the check for @0. I am not seeing why you can't handle
signed 1bit int here.
So for signed 1bit, the only difference is using integer_minus_onep
instead of integer_onep .
> > - bool cst1 = integer_onep (@1);
So this above just becomes:
bool cst1;
if (TYPE_UNSIGNED (TREE_TYPE (@0)) || TYPE_PRECISION (TREE_TYPE (@0)) > 1)
cst1 = integer_onep (@1);
else
cst1 = integer_minus_onep (@1);
See below about the testcase.
> > - bool cst0 = integer_zerop (@1);
> > - bool innereq = inner == EQ_EXPR;
> > - bool outereq = outer == EQ_EXPR;
> > - }
> > - (switch
> > - (if (innereq ? cst0 : cst1)
> > - { constant_boolean_node (!outereq, type); })
> > - (if (innereq ? cst1 : cst0)
> > + (outer:c @0 (convert (inner@2 @0 INTEGER_CST@1)))
> > + (if ((TYPE_UNSIGNED (TREE_TYPE (@0))
> > + || TYPE_PRECISION (TREE_TYPE (@0)) > 1)
> > + && (TYPE_UNSIGNED (TREE_TYPE (@2))
> > + || TYPE_PRECISION (TREE_TYPE (@2)) > 1))
> > + (with {
> > + bool cst1 = integer_onep (@1);
> > + bool cst0 = integer_zerop (@1);
> > + bool innereq = inner == EQ_EXPR;
> > + bool outereq = outer == EQ_EXPR;
> > + }
> > + (switch
> > + (if (innereq ? cst0 : cst1)
> > + { constant_boolean_node (!outereq, type); })
> > + (if (innereq ? cst1 : cst0)
> > + (with {
> > + tree utype = unsigned_type_for (TREE_TYPE (@0));
> > + tree ucst1 = build_one_cst (utype);
> > + }
> > + (if (!outereq)
> > + (gt (convert:utype @0) { ucst1; })
> > + (le (convert:utype @0) { ucst1; })
> > + )
> > + )
> > + )
> > (with {
> > - tree utype = unsigned_type_for (TREE_TYPE (@0));
> > - tree ucst1 = build_one_cst (utype);
> > + tree value = build_int_cst (TREE_TYPE (@0), !innereq);
> > }
> > - (if (!outereq)
> > - (gt (convert:utype @0) { ucst1; })
> > - (le (convert:utype @0) { ucst1; })
> > + (if (outereq)
> > + (eq @0 { value; })
> > + (ne @0 { value; })
> > )
> > )
> > )
> > - (with {
> > - tree value = build_int_cst (TREE_TYPE (@0), !innereq);
> > - }
> > - (if (outereq)
> > - (eq @0 { value; })
> > - (ne @0 { value; })
> > - )
> > - )
> > )
> > )
> > )
> > diff --git a/gcc/testsuite/gcc.dg/torture/pr110293-bitint.c
> > b/gcc/testsuite/gcc.dg/torture/pr110293-bitint.c
> > new file mode 100644
> > index 00000000000..ba3688c8c6a
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/torture/pr110293-bitint.c
> > @@ -0,0 +1,48 @@
> > +/* { dg-do run { target bitint } } */
> > +/* { dg-additional-options "-std=c2y -fgimple" } */
> > +
> > +/* The PR110293 folds of x ==/!= (T)(x ==/!= C) need (T) true to be 1.
> > + The conversion of true to a signed one-bit type gives -1. */
> > +
> > +typedef signed _BitInt(1) S;
> > +
> > +__attribute__((noipa)) int
> > +f1 (S a)
> > +{
> > + return a != (S) (a == -1);
> > +}
> > +
> > +__attribute__((noipa)) int
> > +f2 (S a)
> > +{
> > + return a == (S) (a == -1);
> > +}
>
> You might be able to get a C99 testcase using bitfields. And if so
> please file a bug as it would be a regression from GCC 13.
So I came up with this testcase based on yours but moved over to using
bitfields:
```
struct s1
{
int s : 1;
};
__attribute__((noipa)) int
f1 (struct s1 a)
{
struct s1 b;
struct s1 c = a;
int t = a.s;
b.s = (t == -1);
int t1 = b.s;
int t2 = c.s;
return t2 != t1;
}
__attribute__((noipa)) int
f2 (struct s1 a)
{
struct s1 b;
struct s1 c = a;
int t = a.s;
b.s = (t == -1);
int t1 = b.s;
int t2 = c.s;
return t2 == t1;
}
int
main ()
{
if (f1 ((struct s1){-1}) != 0
|| f1 ((struct s1){0}) != 0)
__builtin_abort ();
if (f2 ((struct s1){-1}) != 1 || f2 ((struct s1){0}) != 1)
__builtin_abort ();
}
```
The extra variables are needed to stop fold converting early into using BFRs.
So please file a bug to track this regression.
Thanks,
Andrea
>
>
> > +
> > +/* Here the comparison has the signed one-bit type. */
> > +
> > +int __GIMPLE (ssa) __attribute__((noipa))
> > +f3 (int x)
> > +{
> > + S _1;
> > + int _2;
> > + _Bool _3;
> > + int _4;
> > +
> > + __BB(2):
> > + _1 = x_5(D) != 0;
> > + _2 = (int) _1;
> > + _3 = x_5(D) != _2;
> > + _4 = (int) _3;
> > + return _4;
> > +}
> > +
> > +int
> > +main ()
> > +{
> > + if (f1 (-1) != 0 || f1 (0) != 0)
> > + __builtin_abort ();
> > + if (f2 (-1) != 1 || f2 (0) != 1)
> > + __builtin_abort ();
> > + if (f3 (-1) != 0 || f3 (0) != 0 || f3 (1) != 1 || f3 (2) != 1)
> > + __builtin_abort ();
> > +}
> > --
> > 2.54.0 (Apple Git-157)
> >