https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125079

--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-15 branch has been updated by Jakub Jelinek
<[email protected]>:

https://gcc.gnu.org/g:7e7412042e90ec1b6f1268049061bcdc208ae267

commit r15-11226-g7e7412042e90ec1b6f1268049061bcdc208ae267
Author: Jakub Jelinek <[email protected]>
Date:   Fri May 1 14:54:35 2026 +0200

    strlen: Adjust objsz arg in __strcat_chk -> __stpcpy_chk transformation
[PR125079]

    As the following testcase shows, we have two different transformations
    of __strcat_chk.  One done in strlen_pass::handle_builtin_strcat,
    which transforms __strcat_chk (x, y, z) if we know beforehand strlen (x),
    so something like:
      l = strlen (x);
      __strcat_chk (x, y, z);
    and since PR87672 we change that to
      l = strlen (x);
      __strcpy_chk (x + l, y, z - l);
    i.e. decrease the objsz in
      if (objsz)
        {
          objsz = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (objsz), objsz,
                                   fold_convert_loc (loc, TREE_TYPE (objsz),
                                                     unshare_expr (dstlen)));
          objsz = force_gimple_operand_gsi (&m_gsi, objsz, true, NULL_TREE,
true,
                                            GSI_SAME_STMT);
        }
    And another transformation is when we have earlier __strcat_chk (x, y, z)
    call and want to compute strlen (x) after that.  In that case
    get_string_length transforms
      __strcat_chk (x, y, z);
    to
      t = strlen (x);
      l = __stpcpy_chk (x + t, y, z) - x;
    where l is the len we are looking for.  This patch changes it similarly to
    the PR87672 to
      t = strlen (x);
      l = __stpcpy_chk (x + t, y, z - t) - x;
    instead.

    2026-05-01  Jakub Jelinek  <[email protected]>

            PR tree-optimization/125079
            * tree-ssa-strlen.cc (get_string_length): Transform
            __strcat_chk (x, y, z) when we need strlen (x) afterwards into
            l1 = strlen (x); l = __stpcpy_chk (x + l1, y, z - l1) - x;
            where l is the strlen (x), instead of using z as last __stpcpy_chk
            argument.

            * gcc.dg/strlenopt-97.c: New test.

    Reviewed-by: Richard Biener <[email protected]>
    (cherry picked from commit c1aa090bb8e1c23e733bc7beafceb7c34dc713f8)

Reply via email to