On Mon, Oct 26, 2026 at 2:20 AM Andrea Pinski
<[email protected]> wrote:
>
> The problem here is after r17-3822-g44c89620f73, the code to make
> sure the types match when factoring out operations, was moved after
> the check for some functions which could return true early.
> Moving the types matching code fixes the problem by not returning early.
> Also change the function args check not to return early so that
> the problem like this does not happen in the future if more code is added
> afterwards.
>
> Bootstrapped and tested on x86_64-linux-gnu.

OK.

Richard.

>         PR tree-optimization/128143
>
> gcc/ChangeLog:
>
>         * gimple-match-exports.cc (factor_operation_ok): Move the
>         type matching code before the function check. Also don't
>         return early for some builtin functions.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/torture/sub-overflow-1.c: New test.
>
> Signed-off-by: Andrea Pinski <[email protected]>
> ---
>  gcc/gimple-match-exports.cc                   |  9 +++++----
>  gcc/testsuite/gcc.dg/torture/sub-overflow-1.c | 13 +++++++++++++
>  2 files changed, 18 insertions(+), 4 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/torture/sub-overflow-1.c
>
> diff --git a/gcc/gimple-match-exports.cc b/gcc/gimple-match-exports.cc
> index 8077bbad165..8f2de738b2f 100644
> --- a/gcc/gimple-match-exports.cc
> +++ b/gcc/gimple-match-exports.cc
> @@ -1645,10 +1645,6 @@ factor_operation_ok (code_helper code, int opnum,
>        && code == VEC_PERM_EXPR && opnum == 2)
>      return false;
>
> -  /*  Some builtins should not be factored out with constants being 
> involved.  */
> -  if (!all_ssa_names && code.is_fn_code ())
> -    return !fn_arg_must_be_const_p (combined_fn (code), opnum);
> -
>    // The types are need to be compatible.
>    if (opnum >= 0)
>      {
> @@ -1657,5 +1653,10 @@ factor_operation_ok (code_helper code, int opnum,
>           return false;
>      }
>
> +  // Some builtins should not be factored out with constants being involved.
> +  if (!all_ssa_names && code.is_fn_code ()
> +      && fn_arg_must_be_const_p (combined_fn (code), opnum))
> +    return false;
> +
>    return true;
>  }
> diff --git a/gcc/testsuite/gcc.dg/torture/sub-overflow-1.c 
> b/gcc/testsuite/gcc.dg/torture/sub-overflow-1.c
> new file mode 100644
> index 00000000000..841cc7f8e92
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/torture/sub-overflow-1.c
> @@ -0,0 +1,13 @@
> +/* { dg-do compile } */
> +/* PR tree-optimization/128143 */
> +
> +int a(int c, signed char x, long long y)
> +{
> +  int r;
> +  if (c)
> +    r = __builtin_sub_overflow_p(y, x, (short) 0);
> +  else
> +    r = __builtin_sub_overflow_p(y, -40000, (short) 0);
> +  return r;
> +}
> +
> --
> 2.43.0
>

Reply via email to