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

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jakub Jelinek
<ja...@gcc.gnu.org>:

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

commit r9-9420-gdfcddd8ed5c584f6eca6d918f8d88da2567d7350
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Mon Feb 15 09:16:06 2021 +0100

    match.pd: Fix up A % (cast) (pow2cst << B) simplification [PR99079]

    The (mod @0 (convert?@3 (power_of_two_cand@1 @2))) simplification
    uses tree_nop_conversion_p (type, TREE_TYPE (@3)) condition, but I believe
    it doesn't check what it was meant to check.  On convert?@3
    TREE_TYPE (@3) is not the type of what it has been converted from, but
    what it has been converted to, which needs to be (because it is operand
    of normal binary operation) equal or compatible to type of the modulo
    result and first operand - type.
    I could fix that by using && tree_nop_conversion_p (type, TREE_TYPE (@1))
    and be done with it, but actually most of the non-nop conversions are IMHO
    ok and so we would regress those optimizations.
    In particular, if we have say narrowing conversions (foo5 and foo6 in
    the new testcase), I think we are fine, either the shift of the power of
two
    constant after narrowing conversion is still that power of two (or negation
    of that) and then it will still work, or the result of narrowing conversion
    is 0 and then we would have UB which we can ignore.
    Similarly, widening conversions where the shift result is unsigned are
fine,
    or even widening conversions where the shift result is signed, but we sign
    extend to a signed wider divisor, the problematic case of INT_MIN will
    become x % (long long) INT_MIN and we can still optimize that to
    x & (long long) INT_MAX.
    What doesn't work is the case in the pr99079.c testcase, widening
conversion
    of a signed shift result to wider unsigned divisor, where if the shift
    is negative, we end up with x % (unsigned long long) INT_MIN which is
    x % 0xffffffff80000000ULL where the divisor is not a power of two and
    we can't optimize that to x & 0x7fffffffULL.

    So, the patch rejects only the single problematic case.

    Furthermore, when the shift result is signed, we were introducing UB into
    a program which previously didn't have one (well, left shift into the sign
    bit is UB in some language/version pairs, but it is definitely valid in
    C++20 - wonder if I shouldn't move the gcc.c-torture/execute/pr99079.c
    testcase to g++.dg/torture/pr99079.C and use -std=c++20), by adding that
    subtraction of 1, x % (1 << 31) in C++20 is well defined, but
    x & ((1 << 31) - 1) triggers UB on the subtraction.
    So, the patch performs the subtraction in the unsigned type if it isn't
    wrapping.

    2021-02-15  Jakub Jelinek  <ja...@redhat.com>

            PR tree-optimization/99079
            * match.pd (A % (pow2pcst << N) -> A & ((pow2pcst << N) - 1)):
Remove
            useless tree_nop_conversion_p (type, TREE_TYPE (@3)) check. 
Instead
            require both type and TREE_TYPE (@1) to be integral types and
either
            type having smaller or equal precision, or TREE_TYPE (@1) being
            unsigned type, or type being signed type.  If TREE_TYPE (@1)
            doesn't have wrapping overflow, perform the subtraction of one in
            unsigned type.

            * gcc.dg/fold-modpow2-2.c: New test.
            * gcc.c-torture/execute/pr99079.c: New test.

    (cherry picked from commit 45de8afb2d534e3b38b4d1898686b20c29cc6a94)

Reply via email to