[Bug tree-optimization/125557] Missed if-conversion of load addresses

"cvs-commit at gcc dot gnu.org via Gcc-bugs" <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125557

--- Comment #16 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Kyrylo Tkachov <[email protected]>:

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

commit r17-3423-gee69b433860e8017974df2bb499924a5fd1c7894
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Jul 16 12:54:59 2026 +0200

    ifcvt: Make average_cost independent of arm order [PR125557]

    average_cost computes

      ELSE_COST + P * (THEN_COST - ELSE_COST)

    profile_probability::apply now rounds signed initialized values to the
nearest
    integer, with halfway values away from zero.  That signed rounding can
still
    give different integer costs when equivalent CFG arms are reversed. 
Unknown
    probabilities have the same issue for odd cost differences because apply
    truncates them toward zero.

    Write the documented weighted average as

      ELSE_COST + P * (THEN_COST - ELSE_COST)

    when THEN_COST is at least ELSE_COST, and as

      THEN_COST + (1 - P) * (ELSE_COST - THEN_COST)

    otherwise.  Both forms start with the cheaper arm and scale a nonnegative
    cost difference by the probability of the costlier arm.  Reversing the CFG
    arms therefore keeps the same base, magnitude, probability, and rounded
    result.  Cast the costlier arm to gcov_type before subtraction.

    Add an x86 test with the same costs and probabilities represented using
    reversed CFG arms.  The scaled cost difference is an exact halfway value.
    The apply-only compiler makes opposite profitability decisions for the two
    orientations.  With this change both forms convert to conditional moves.

    gcc/ChangeLog:

            PR tree-optimization/125557
            * ifcvt.cc (average_cost): Scale a nonnegative cost difference
using
            the probability of the more expensive arm.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/125557
            * gcc.target/i386/ifcvt-average-cost-1.c: New test.

    Signed-off-by: Kyrylo Tkachov <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.