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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Aldy Hernandez <al...@gcc.gnu.org>:

https://gcc.gnu.org/g:3bff15c1c9fb3eb0bb042717e072476ec2d6d88c

commit r13-3752-g3bff15c1c9fb3eb0bb042717e072476ec2d6d88c
Author: Aldy Hernandez <al...@redhat.com>
Date:   Mon Nov 7 08:40:12 2022 +0100

    [range-op] Restrict division by power of 2 optimization to positive
numbers.

    The problem here is that we are transforming a division by a power of
    2 into a right shift, and using this to shift the maybe nonzero bits.
    This gives the wrong result when the number being divided is negative.

    In the testcase we are dividing this by 8:

            [irange] int [-256, -255] NONZERO 0xffffff01

    and coming up with:

            [irange] int [-32, -31] NONZERO 0xffffffe0

    The maybe nonzero bits are wrong as -31 has the LSB set (0xffffffe1)
    whereas the bitmask says the lower 4 bits are off.

            PR tree-optimization/107541

    gcc/ChangeLog:

            * range-op.cc (operator_div::fold_range): Restrict power of 2
            optimization to positive numbers.

    gcc/testsuite/ChangeLog:

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

Reply via email to