On Mon, Nov 9, 2026 at 6:38 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. 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
> * match.pd (x ==/!= (typeof x)(x CMP CST)): Handle all
> comparisons. Use the results of the comparison for 0 and 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.
>
> Signed-off-by: Kyrylo Tkachov <[email protected]>
> ---
> gcc/match.pd | 53 ++++++-----------
> gcc/testsuite/gcc.dg/torture/pr127292-2.c | 61 ++++++++++++++++++++
> gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c | 66 ++++++++++++++++++++++
> 3 files changed, 145 insertions(+), 35 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/torture/pr127292-2.c
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr127292-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index b8170597310..ea6afb3ef32 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -9529,14 +9529,14 @@ 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) */
> -/* These need (typeof x)(x CMP CST) to be 0 or 1. A signed one-bit type
> +/* (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. 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)
> + (for inner (simple_comparison)
> (simplify
> (outer:c @0 (convert (inner@2 @0 INTEGER_CST@1)))
> (if ((TYPE_UNSIGNED (TREE_TYPE (@0))
> @@ -9544,39 +9544,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> && (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;
> + tree itype = TREE_TYPE (@0);
> + bool c0 = integer_onep (const_binop (inner, boolean_type_node,
> + build_zero_cst (itype), @1));
> + bool c1 = integer_onep (const_binop (inner, boolean_type_node,
> + build_one_cst (itype), @1));
Don't use const_binop/integer_onep/build_one_cst here. Instead use
compare_tree_int with a switch.
So like:
int cc0 = compare_tree_int (@1, 0);
bool c0;
switch (inner)
{
}
Maybe outline this into a helper function since you are going to do it twice.
So thing like:
bool compare_tree_int (tree_code code, tree t, unsigned HOST_WIDE_INT u)
{
int cc0 = compare_tree_int (@1, 0);
switch (inner)
{
case LE:
return cc0 <= 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/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" } } */
> --
> 2.54.0 (Apple Git-157)
>