Thanks, you're right.  I checked: a user type such as

  struct my { int32x4_t v[2]; };

gets the same V2x4SI mode, and with the patch ESRA disqualifies it too
when it is passed whole to an ordinary function in a loop.  The code
didn't change in that example, but the predicate keys on the shape of the
type rather than on the intrinsic tuples.

The copies only matter when the tuple is handed whole to the ld<N>/st<N>
lane builtins.  I tried requiring the call to be a BUILT_IN_MD builtin
(gimple_call_builtin_p (call, BUILT_IN_MD)) instead.  User types can't
reach those calls without a conversion, so the user struct above stays an
SRA candidate. I haven't rerun the full bootstrap
with that variant yet.

1. Is restricting it to target builtins acceptable to you, or would you
   rather the middle-end didn't special-case vector tuples here at all?

2. If it should stay in tree-sra.cc, is scan_function the right place

Thanks.
Rajeev

On Mon, Oct 26, 2026 at 6:50 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.
>
>         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