On Tue, Aug 11, 2026 at 5:00 PM Andrea Pinski
<[email protected]> wrote:
>
> This adds some extra checks to see if we can remove the casts
> for signed integer overflow reasons while doing a negative.
> This is needed more due to the recent patch which adds them
> in some cases.

Can't you use expr_not_equal_to (..., INT_MIN)?

> Bootstrapped and tested on x86_64-linux-gnu.
>
>         PR tree-optimization/107765
>
> gcc/ChangeLog:
>
>         * match.pd (`(cast)-(cast)a`): Expand to use ranger
>         to see if we can remove the casts.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/neg-cast-1.c: New test.
>         * gcc.dg/tree-ssa/neg-cast-4.c: New test.
>
> Signed-off-by: Andrea Pinski <[email protected]>
> ---
>  gcc/match.pd                               | 37 ++++++++++++++++++----
>  gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c | 23 ++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/neg-cast-4.c | 16 ++++++++++
>  3 files changed, 69 insertions(+), 7 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-4.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 751f5571183..8de356b102c 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1233,15 +1233,38 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>     show up.  Else it is safe if the negation is done in an unsigned type.
>     Note the outer cast cannot be a boolean type as the only valid values
>     are 0,-1/1 (depending on the signedness of the boolean) and the negative
> -   is there to get the correct value.  */
> +   is there to get the correct value.
> +   Also handle the case where we   */
>  (simplify
> - (convert (negate:s@1 (convert:s @0)))
> + (convert (negate:s@1 (convert:s@ic @0)))
>   (if (INTEGRAL_TYPE_P (type)
> -      && tree_nop_conversion_p (type, TREE_TYPE (@1))
> -      && (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
> -         || TYPE_UNSIGNED (type))
> -      && TREE_CODE (type) != BOOLEAN_TYPE)
> -    (negate (convert @0))))
> +      && tree_nop_conversion_p (type, TREE_TYPE (@1)))
> +  (with {
> +     bool can_handle = false;
> +#if GIMPLE
> +    int_range_max vr;
> +#endif
> +    if ((TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
> +        || TYPE_UNSIGNED (type))
> +        && TREE_CODE (type) != BOOLEAN_TYPE)
> +      can_handle = true;
> +    else if (wi::ges_p (tree_nonzero_bits (@ic), 0))
> +      can_handle = true;
> +#if GIMPLE
> +    /* If we know that @0 does not contain signed max, then the
> +       conversion can be removed.  */
> +    else if (TREE_CODE (@0) == SSA_NAME
> +        && types_match (type, TREE_TYPE (@0))
> +       && get_range_query (cfun)->range_of_expr (vr, @0)
> +       && !vr.undefined_p ())
> +      {
> +        tree stype = signed_type_for (type);
> +       can_handle = !vr.contains_p (wi::min_value (stype));
> +      }
> +#endif
> +   }
> +   (if (can_handle)
> +    (negate (convert @0))))))
>
>  (for op (negate abs)
>   /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c
> new file mode 100644
> index 00000000000..ec5d0e15251
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-1.c
> @@ -0,0 +1,23 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized -fdump-tree-cddce1" } */
> +/* PR tree-optimization/107765 */
> +
> +int a(int input)
> +{
> +    if (input == -__INT_MAX__-1) return 1;
> +    unsigned t = input;
> +    int tt =  -t;
> +    return tt == -input;
> +}
> +
> +int b(int input)
> +{
> +    if (input == -__INT_MAX__-1) __builtin_trap();
> +    unsigned t = input;
> +    int tt =  -t;
> +    return tt;
> +}
> +
> +/* { dg-final { scan-tree-dump "return 1" "optimized" } } */
> +/* { dg-final { scan-tree-dump-not "\\(unsigned int\\)" "cddce1" } } */
> +/* { dg-final { scan-tree-dump-not "\\(int\\)" "cddce1" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-4.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-4.c
> new file mode 100644
> index 00000000000..fe3fe6a2ecc
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-4.c
> @@ -0,0 +1,16 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-phiopt2-details -fdump-tree-optimized" } */
> +/* PR tree-optimization/107765 */
> +
> +int b(int input)
> +{
> +    if (input == -__INT_MAX__-1) return input;
> +    unsigned t = input;
> +    int tt =  -t;
> +    return tt;
> +}
> +
> +/* { dg-final { scan-tree-dump-not "if " "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "\\(unsigned int\\)" 1 "optimized" } } 
> */
> +/* { dg-final { scan-tree-dump-times "\\(int\\)" 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "converted to straightline code" 1 
> "phiopt2" } } */
> --
> 2.43.0
>

Reply via email to