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

--- Comment #14 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:2cbd4409bcfaba2bd4200412090fd06db1948369

commit r15-6725-g2cbd4409bcfaba2bd4200412090fd06db1948369
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Jan 9 08:30:12 2025 +0100

    match.pd: Avoid introducing UB in the a r<< (32-b) -> a r>> b optimization
[PR117927]

    As mentioned in the PR, the a r<< (bitsize-b) to a r>> b and similar
    match.pd optimization which has been introduced in GCC 15 can introduce
    UB which wasn't there before, in particular if b is equal at runtime
    to bitsize, then a r<< 0 is turned into a r>> bitsize.

    The following patch fixes it by optimizing it early only if VRP
    tells us the count isn't equal to the bitsize, and late into
    a r>> (b & (bitsize - 1)) if bitsize is power of two and the subtraction
    has single use, on various targets the masking then goes away because
    its rotate instructions do masking already.  The latter can't be
    done too early though, because then the expr_not_equal_to case is
    basically useless and we introduce the masking always and can't find out
    anymore that there was originally no masking.  Even cfun->after_inlining
    check would be too early, there is forwprop before vrp, so the patch
    introduces a new PROP for the start of the last forwprop pass.

    2025-01-09  Jakub Jelinek  <ja...@redhat.com>
                Andrew Pinski  <quic_apin...@quicinc.com>

            PR tree-optimization/117927
            * tree-pass.h (PROP_last_full_fold): Define.
            * passes.def: Add last= parameters to pass_forwprop.
            * tree-ssa-forwprop.cc (pass_forwprop): Add last_p non-static
            data member and initialize it in the ctor.
            (pass_forwprop::set_pass_param): New method.
            (pass_forwprop::execute): Set PROP_last_full_fold in
curr_properties
            at the start if last_p.
            * match.pd (a rrotate (32-b) -> a lrotate b): Only optimize either
            if @2 is known not to be equal to prec or if during/after last
            forwprop the subtraction has single use and prec is power of two;
in
            that case transform it into orotate by masked count.

            * gcc.dg/tree-ssa/pr117927.c: New test.

Reply via email to