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

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <[email protected]>:

https://gcc.gnu.org/g:ed264541c353866cedf46ad873f2e2c46cf62839

commit r16-3647-ged264541c353866cedf46ad873f2e2c46cf62839
Author: Andrew Pinski <[email protected]>
Date:   Sat Apr 19 09:14:54 2025 -0700

    strlen: Handle empty constructor as memset for combining with malloc to
calloc [PR87900]

    This was noticed when turning memset (with constant size) into a store of
an empty constructor
    but can be reproduced without that.
    In this case we have the following IR:
    ```
      p_3 = __builtin_malloc (4096);
      *p_3 = {};
    ```

    Which we can treat the store as a memset.
    So this patch adds the similar optimization as memset/malloc now for
malloc/constructor.
    This patch is on top of
https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681439.html
    (it calls allow_memset_malloc_to_calloc but that can be removed if that
patch is rejected).

    Changes since v1:
    * v2: Correctly return false from handle_assign after removing stmt.

    Bootstrapped and tested on x86_64-linux-gnu.

            PR tree-optimization/87900

    gcc/ChangeLog:

            * tree-ssa-strlen.cc  (strlen_pass::handle_assign): Add RHS
argument.
            For empty constructor RHS, see if can combine with a previous
malloc into
            a calloc.
            (strlen_pass::check_and_optimize_call): Update call to
handle_assign;
            passing NULL_TREE for RHS.
            (strlen_pass::check_and_optimize_stmt): Update call to
handle_assign.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/calloc-10.c: New test.
            * gcc.dg/tree-ssa/calloc-11.c: New test.
            * gcc.dg/tree-ssa/calloc-12.c: New test.

    Signed-off-by: Andrew Pinski <[email protected]>

Reply via email to