From: Kyrylo Tkachov <[email protected]>
The folds of x ==/!= (typeof x)(x ==/!= CST) assume that the converted
comparison is 0 or 1. For a signed one-bit x, (typeof x) true is -1.
For
struct s1 { int s : 1; };
int f (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;
}
forwprop1 sees
_2 = _1 == -1;
_3 = (<unnamed-signed:1>) _2;
b.s = _3;
_4 = b.s;
_5 = c.s;
_6 = _4 != _5;
and the fold gives a.s != 0, so f ((struct s1){-1}) is 1 instead of 0
at -O1 and above. This is a regression since GCC 14. C2Y signed
_BitInt(1) gives the same wrong code.
For a signed one-bit x, compare CST with -1 in place of 1. The other
parts of the folds are then correct, because x can only be 0 or -1.
The result above becomes 0.
A comparison can also have a signed one-bit result type in GIMPLE.
Then (typeof x) true is -1 or all ones for a wider x, so do not fold.
Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu.
Ok for trunk?
Thanks,
Kyrill
Cc: Andrea Pinski <[email protected]>
gcc/ChangeLog:
PR tree-optimization/128585
* match.pd (x ==/!= (typeof x)(x ==/!= CST)): Compare CST with -1
for a signed one-bit x. Do not fold if the comparison has a signed
one-bit type.
gcc/testsuite/ChangeLog:
PR tree-optimization/128585
* gcc.dg/torture/pr110293-bitint.c: New test.
* gcc.dg/torture/pr128585.c: New test.
* gcc.dg/tree-ssa/pr128585.c: New test.
Signed-off-by: Kyrylo Tkachov <[email protected]>
---
v1 is 5/8 of the series posted on 2026-11-09.
Changes from v1 (Andrea's review):
- Fold for a signed one-bit x and compare CST with -1 for it, with the
code that Andrea gave.
- Keep the check that the comparison does not have a signed one-bit type.
GIMPLE allows such a comparison (f3 in pr110293-bitint.c).
- New tests from Andrea's C99 bitfield testcase, PR128585.
gcc/match.pd | 59 +++++++++++--------
.../gcc.dg/torture/pr110293-bitint.c | 48 +++++++++++++++
gcc/testsuite/gcc.dg/torture/pr128585.c | 44 ++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr128585.c | 38 ++++++++++++
4 files changed, 165 insertions(+), 24 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr110293-bitint.c
create mode 100644 gcc/testsuite/gcc.dg/torture/pr128585.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr128585.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 3912f9f7d54..ee9956ceaba 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -9533,38 +9533,49 @@ 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) */
+/* For a signed one-bit x, (typeof x) true is -1, so -1 takes the place
+ of 1 above. If the comparison has a signed one-bit type, true is -1
+ and (typeof x) true is not 1 for a wider x, so do not fold. */
(for outer (ne eq)
(for inner (ne eq)
(simplify
- (outer:c @0 (convert (inner @0 INTEGER_CST@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)
+ (outer:c @0 (convert (inner@2 @0 INTEGER_CST@1)))
+ (if (TYPE_UNSIGNED (TREE_TYPE (@2))
+ || TYPE_PRECISION (TREE_TYPE (@2)) > 1)
+ (with {
+ bool cst1;
+ if (TYPE_UNSIGNED (TREE_TYPE (@0))
+ || TYPE_PRECISION (TREE_TYPE (@0)) > 1)
+ cst1 = integer_onep (@1);
+ else
+ cst1 = integer_minus_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..dcdc03e29d5
--- /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) for signed one-bit types.
+ The conversion of true to such a 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);
+}
+
+/* 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 ();
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128585.c
b/gcc/testsuite/gcc.dg/torture/pr128585.c
new file mode 100644
index 00000000000..3d77f59b010
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128585.c
@@ -0,0 +1,44 @@
+/* PR tree-optimization/128585 */
+/* { dg-do run } */
+
+/* The PR110293 folds of x ==/!= (T)(x ==/!= C) for a signed one-bit
+ bitfield x. The conversion of true to that type gives -1. The extra
+ variables keep the front end from folding to BIT_FIELD_REFs. */
+
+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 ();
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr128585.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr128585.c
new file mode 100644
index 00000000000..518b43b72f6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr128585.c
@@ -0,0 +1,38 @@
+/* PR tree-optimization/128585 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* For a signed one-bit x, x ==/!= (typeof x)(x == -1) folds to a
+ constant. */
+
+struct s1
+{
+ int s : 1;
+};
+
+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;
+}
+
+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;
+}
+
+/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "return 1;" 1 "optimized" } } */
--
2.54.0 (Apple Git-157)