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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rgue...@gcc.gnu.org>:

https://gcc.gnu.org/g:5f413dc41ee4f8bc3a0fc295f98b75dceae52fa8

commit r13-6773-g5f413dc41ee4f8bc3a0fc295f98b75dceae52fa8
Author: Richard Biener <rguent...@suse.de>
Date:   Fri Mar 17 13:14:49 2023 +0100

    tree-optimization/109170 - bogus use-after-free with __builtin_expect

    The following adds a missing range-op for __builtin_expect which
    helps -Wuse-after-free to detect the case a realloc original
    pointer is used when the result was NULL.  The implementation
    should handle all argument one pass-through builtins we handle
    in the fnspec machinery, but that's defered to GCC 14.

    The gcc.dg/tree-ssa/ssa-lim-21.c testcase needs adjustment because

       for (int j = 0; j < m; j++)
         if (__builtin_expect (m, 0))
           for (int i = 0; i < m; i++)

    is now correctly optimized to a unconditional jump by EVRP - m
    cannot be zero when the outer loop is entered.  I've adjusted
    the outer loop to iterate 'n' times which makes us apply store-motion
    to 'count' and 'q->data1' but only out of the inner loop and
    as expected not apply store motion to 'q->data' at all.

    The gcc.dg/predict-20.c testcase relies on broken behavior of
    profile estimation when trying to handle __builtin_expect values
    flowing into PHI nodes.  I have opened PR109210 and removed
    the expected matching from the testcase.

            PR tree-optimization/109170
            * gimple-range-op.cc (cfn_pass_through_arg1): New.
            (gimple_range_op_handler::maybe_builtin_call): Handle
            __builtin_expect via cfn_pass_through_arg1.

            * gcc.dg/Wuse-after-free-pr109170.c: New testcase.
            * gcc.dg/tree-ssa/ssa-lim-21.c: Adjust.
            * gcc.dg/predict-20.c: Likewise.

Reply via email to