On 11/11/26 3:20 AM, [email protected] wrote:
From: Kyrylo Tkachov <[email protected]>

The folds of x ==/!= (typeof x)(x CMP CST) handle only == and != as
CMP.  For

   int f (int a)
   {
     int t = a > 1;
     return t != a;
   }

GCC keeps

   _1 = a_3(D) > 1;
   t_4 = (int) _1;
   _2 = a_3(D) != t_4;

although the result is a != 0.

(typeof x)(x CMP CST) is 0 or 1, so x can only be equal to it if x is 0
or 1.  With C0 = (0 CMP CST) and C1 = (1 CMP CST), x == (typeof x)(x
CMP CST) is x == 0 if C0 and C1 are false, x == 1 if both are true,
(unsigned) x <= 1 if only C1 is true, and false if only C0 is true.
x != (typeof x)(x CMP CST) is the inverse.  For == and != these are the
results that the folds give now, so compute C0 and C1 and use the folds
for all six comparisons.  For a signed one-bit x, (typeof x) true is -1
and C1 is (-1 CMP CST).  Compute C0 and C1 with compare_tree_int and a
new helper, comparison_result_p, which gives the value of a comparison
from the sign of A - B.  f becomes

   _1 = a_2(D) != 0;

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/127292
        * fold-const.cc (comparison_result_p): New function.
        * fold-const.h (comparison_result_p): Declare.
        * match.pd (x ==/!= (typeof x)(x CMP CST)): Handle all
        comparisons.  Use the results of the comparison for 0 and
        (typeof x) 1.

gcc/testsuite/ChangeLog:

        PR tree-optimization/127292
        * gcc.dg/tree-ssa/pr127292-1.c: New test.
        * gcc.dg/torture/pr127292-2.c: New test.
        * gcc.dg/torture/pr127292-3.c: New test.

Signed-off-by: Kyrylo Tkachov <[email protected]>
---
v1 is 6/8 of the series posted on 2026-11-09.
Changes from v1 (Andrea Pinski's review):
- Compute C0 and C1 with compare_tree_int and the new helper
   comparison_result_p, not with const_binop and build_one_cst.
- Handle a signed one-bit x, after the change to 5/8.
- New test gcc.dg/torture/pr127292-3.c for a signed one-bit bitfield.
LGTM.  Sashiko didn't pick it up, but Claude seems reasonably happy with it.  Claude asked for a run test as well as tests of more types.  Your call on whether or not to beef up the tests.

I didn't have any other patches from this series marked in my inbox.  So presumably they're acked or hit areas others typically would review.

jeff

  gcc/fold-const.cc                          | 26 +++++++++
  gcc/fold-const.h                           |  1 +
  gcc/match.pd                               | 65 ++++++++-------------
  gcc/testsuite/gcc.dg/torture/pr127292-2.c  | 61 ++++++++++++++++++++
  gcc/testsuite/gcc.dg/torture/pr127292-3.c  | 60 ++++++++++++++++++++
  gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c | 66 ++++++++++++++++++++++
  6 files changed, 238 insertions(+), 41 deletions(-)
  create mode 100644 gcc/testsuite/gcc.dg/torture/pr127292-2.c
  create mode 100644 gcc/testsuite/gcc.dg/torture/pr127292-3.c
  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 4bb80084b8b..8de144275c4 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -2989,6 +2989,32 @@ compcode_to_comparison (enum comparison_code code)
      }
  }
+/* Return the value of A CODE B, where CODE is LT_EXPR, LE_EXPR, EQ_EXPR,
+   NE_EXPR, GE_EXPR or GT_EXPR, and CMP is negative, zero or positive if A
+   is less than, equal to or greater than B.  */
+
+bool
+comparison_result_p (tree_code code, int cmp)
+{
+  switch (code)
+    {
+    case LT_EXPR:
+      return cmp < 0;
+    case LE_EXPR:
+      return cmp <= 0;
+    case EQ_EXPR:
+      return cmp == 0;
+    case NE_EXPR:
+      return cmp != 0;
+    case GE_EXPR:
+      return cmp >= 0;
+    case GT_EXPR:
+      return cmp > 0;
+    default:
+      gcc_unreachable ();
+    }
+}
+
  /* Return true if COND1 tests the opposite condition of COND2.  */
bool
diff --git a/gcc/fold-const.h b/gcc/fold-const.h
index 4e640de725f..0ddc9940965 100644
--- a/gcc/fold-const.h
+++ b/gcc/fold-const.h
@@ -152,6 +152,7 @@ extern tree constant_boolean_node (bool, tree);
extern bool tree_swap_operands_p (const_tree, const_tree);
  extern enum tree_code swap_tree_comparison (enum tree_code);
+extern bool comparison_result_p (tree_code, int);
extern bool ptr_difference_const (tree, tree, poly_int64 *);
  extern enum tree_code invert_tree_comparison (enum tree_code, bool);
diff --git a/gcc/match.pd b/gcc/match.pd
index ee9956ceaba..05be0b59a60 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -9529,58 +9529,41 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (lt:c @0 (convert (ne tree_expr_nonnegative_p@0 integer_zerop)))
   { constant_boolean_node (false, type); })
-/* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
-/* 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.  */
+/* (typeof x)(x CMP CST) is 0 or 1, so x can only be equal to it if x is
+   0 or 1.  With C0 = (0 CMP CST) and C1 = (1 CMP CST):
+   x == (typeof x)(x CMP CST) -> C0 ? (C1 ? x == 1 : 0)
+                                   : (C1 ? (unsigned) x <= 1 : x == 0)
+   and x != (typeof x)(x CMP CST) is the inverse.  For a signed one-bit x,
+   (typeof x) true is -1, so -1 takes the place of 1.  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)
+ (for inner (simple_comparison)
    (simplify
     (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);
+      tree itype = TREE_TYPE (@0);
+      /* -compare_tree_int (@1, V) compares V with CST.  */
+      bool c0 = comparison_result_p (inner, -compare_tree_int (@1, 0));
+      bool c1;
+      if (TYPE_UNSIGNED (itype) || TYPE_PRECISION (itype) > 1)
+       c1 = comparison_result_p (inner, -compare_tree_int (@1, 1));
        else
-       cst1 = integer_minus_onep (@1);
-      bool cst0 = integer_zerop (@1);
-      bool innereq = inner == EQ_EXPR;
+       /* CST is 0 or -1.  */
+       c1 = comparison_result_p (inner, integer_zerop (@1) ? -1 : 0);
        bool outereq = outer == EQ_EXPR;
       }
       (switch
-      (if (innereq ? cst0 : cst1)
+      (if (c0 && !c1)
         { 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 value = build_int_cst (TREE_TYPE (@0), !innereq);
-       }
-       (if (outereq)
-       (eq @0 { value; })
-       (ne @0 { value; })
-       )
-      )
-     )
-    )
-   )
-  )
- )
-)
+      (if (!c0 && c1)
+       (with { tree utype = unsigned_type_for (itype); }
+       (if (outereq)
+        (le (convert:utype @0) { build_one_cst (utype); })
+        (gt (convert:utype @0) { build_one_cst (utype); }))))
+      (outer @0 { build_int_cst (itype, c0); })))))))
(for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
   /* If the second operand is NaN, the result is constant.  */
diff --git a/gcc/testsuite/gcc.dg/torture/pr127292-2.c 
b/gcc/testsuite/gcc.dg/torture/pr127292-2.c
new file mode 100644
index 00000000000..d83a9bcac76
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr127292-2.c
@@ -0,0 +1,61 @@
+/* PR tree-optimization/127292 */
+/* { dg-do run } */
+
+/* Compare x OUT (T) (x IN C) with a version where C is not known.  */
+
+#define F(T, N, OUT, IN, C)                                            \
+  __attribute__((noipa)) int N (T x) { return x OUT (T) (x IN (T) (C)); } \
+  __attribute__((noipa)) int N##_r (T x)                               \
+  {                                                                    \
+    volatile T c = (T) (C);                                            \
+    T b = x IN c;                                                      \
+    return x OUT b;                                                    \
+  }
+
+#define G(T, N, C)                                                     \
+  F (T, N##_eq_lt, ==, <, C) F (T, N##_eq_le, ==, <=, C)         \
+  F (T, N##_eq_eq, ==, ==, C) F (T, N##_eq_ne, ==, !=, C)              \
+  F (T, N##_eq_ge, ==, >=, C) F (T, N##_eq_gt, ==, >, C)         \
+  F (T, N##_ne_lt, !=, <, C) F (T, N##_ne_le, !=, <=, C)         \
+  F (T, N##_ne_eq, !=, ==, C) F (T, N##_ne_ne, !=, !=, C)              \
+  F (T, N##_ne_ge, !=, >=, C) F (T, N##_ne_gt, !=, >, C)
+
+#define CF(N) if (N (x) != N##_r (x)) __builtin_abort ();
+
+#define CG(N)                                                          \
+  CF (N##_eq_lt) CF (N##_eq_le) CF (N##_eq_eq) CF (N##_eq_ne)          \
+  CF (N##_eq_ge) CF (N##_eq_gt) CF (N##_ne_lt) CF (N##_ne_le)          \
+  CF (N##_ne_eq) CF (N##_ne_ne) CF (N##_ne_ge) CF (N##_ne_gt)
+
+#define TYPE(T, N)                                                     \
+  G (T, N##_m1, -1) G (T, N##_0, 0) G (T, N##_1, 1) G (T, N##_2, 2)    \
+  __attribute__((noipa)) void                                          \
+  check_##N (T x)                                                      \
+  {                                                                    \
+    CG (N##_m1) CG (N##_0) CG (N##_1) CG (N##_2)                       \
+  }
+
+TYPE (int, i)
+TYPE (unsigned, u)
+TYPE (signed char, sc)
+TYPE (unsigned char, uc)
+TYPE (long long, ll)
+
+int
+main ()
+{
+  static const long long vals[]
+    = { -__LONG_LONG_MAX__ - 1, -__INT_MAX__ - 1, -3, -2, -1, 0, 1, 2, 3,
+       __INT_MAX__, (long long) __INT_MAX__ * 2 + 1, __LONG_LONG_MAX__ };
+  for (int i = 0; i < sizeof (vals) / sizeof (vals[0]); i++)
+    {
+      check_i (vals[i]);
+      check_u (vals[i]);
+      check_ll (vals[i]);
+    }
+  for (int i = -128; i < 256; i++)
+    {
+      check_sc (i);
+      check_uc (i);
+    }
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr127292-3.c 
b/gcc/testsuite/gcc.dg/torture/pr127292-3.c
new file mode 100644
index 00000000000..452d3e33aa8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr127292-3.c
@@ -0,0 +1,60 @@
+/* PR tree-optimization/127292 */
+/* { dg-do run } */
+
+/* Compare x OUT (T) (x IN C) for a signed one-bit bitfield x with a
+   version where C is not known.  The extra variables keep the front end
+   from folding to BIT_FIELD_REFs.  */
+
+struct s1
+{
+  int s : 1;
+};
+
+#define F(N, OUT, IN, C)                                               \
+  __attribute__((noipa)) int N (struct s1 a)                           \
+  {                                                                    \
+    struct s1 b;                                                       \
+    struct s1 c = a;                                                   \
+    int t = a.s;                                                       \
+    b.s = (t IN (C));                                                  \
+    int t1 = b.s;                                                      \
+    int t2 = c.s;                                                      \
+    return t2 OUT t1;                                                  \
+  }                                                                    \
+  __attribute__((noipa)) int N##_r (struct s1 a)                       \
+  {                                                                    \
+    volatile int vc = (C);                                             \
+    struct s1 b;                                                       \
+    b.s = (a.s IN vc);                                                 \
+    int t1 = b.s;                                                      \
+    int t2 = a.s;                                                      \
+    return t2 OUT t1;                                                  \
+  }
+
+#define G(N, C)                                                                
\
+  F (N##_eq_lt, ==, <, C) F (N##_eq_le, ==, <=, C)                       \
+  F (N##_eq_eq, ==, ==, C) F (N##_eq_ne, ==, !=, C)                    \
+  F (N##_eq_ge, ==, >=, C) F (N##_eq_gt, ==, >, C)                       \
+  F (N##_ne_lt, !=, <, C) F (N##_ne_le, !=, <=, C)                       \
+  F (N##_ne_eq, !=, ==, C) F (N##_ne_ne, !=, !=, C)                    \
+  F (N##_ne_ge, !=, >=, C) F (N##_ne_gt, !=, >, C)
+
+#define CF(N) if (N (x) != N##_r (x)) __builtin_abort ();
+
+#define CG(N)                                                          \
+  CF (N##_eq_lt) CF (N##_eq_le) CF (N##_eq_eq) CF (N##_eq_ne)          \
+  CF (N##_eq_ge) CF (N##_eq_gt) CF (N##_ne_lt) CF (N##_ne_le)          \
+  CF (N##_ne_eq) CF (N##_ne_ne) CF (N##_ne_ge) CF (N##_ne_gt)
+
+G (m1, -1)
+G (z, 0)
+
+int
+main ()
+{
+  struct s1 x;
+  x.s = -1;
+  CG (m1) CG (z)
+  x.s = 0;
+  CG (m1) CG (z)
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c
new file mode 100644
index 00000000000..986de82a91b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c
@@ -0,0 +1,66 @@
+/* PR tree-optimization/127292 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f23 (int a)
+{
+  int t = a > 1;
+  return t != a;
+}
+
+int
+f23_1 (int a)
+{
+  int t;
+  if (a > 1)
+    t = 1;
+  else
+    t = 0;
+  return t != a;
+}
+
+int
+f23_2 (int a)
+{
+  int t;
+  if (a > 1)
+    t = 1;
+  else
+    t = 0;
+  if (t != a)
+    t = 1;
+  else
+    t = 0;
+  return t;
+}
+
+/* (0 < 1) is true and (1 < 1) is false, so a is never equal.  */
+
+int
+f_never (int a)
+{
+  return (a < 1) == a;
+}
+
+/* (0 >= 1) is false and (1 >= 1) is true, so a must be 0 or 1.  */
+
+int
+f_zero_one (int a)
+{
+  return (a >= 1) == a;
+}
+
+/* (0 >= 0) and (1 >= 0) are true, so a must be 1.  */
+
+int
+f_one (int a)
+{
+  return a != (a >= 0);
+}
+
+/* { dg-final { scan-tree-dump-times "a_\[0-9\]+\\(D\\) != 0" 3 "optimized" } 
} */
+/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\\(unsigned int\\) a_\[0-9\]+\\(D\\);" 1 
"optimized" } } */
+/* { dg-final { scan-tree-dump-times "a_\[0-9\]+\\(D\\) != 1" 1 "optimized" } 
} */
+/* { dg-final { scan-tree-dump-not "a_\[0-9\]+\\(D\\) \[<>\]=? \[01\];" 
"optimized" } } */

Reply via email to