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

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

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

commit r17-90-g5de59c6d8267ddfefec34317a67fa96b4f0900e2
Author: Jeff Law <[email protected]>
Date:   Sat Apr 25 18:12:27 2026 -0600

    [RISC-V][PR rtl-optimization/56096] Improve equality comparisons of a
logical AND expressions

    This BZ shows that we can improve certain comparisons for RISC-V.  In
    particular if we are testing the result of a logical AND for equality and
one
    operand of the AND requires synthesis, we may be able to do better if we
right
    shift away any trailing zeros from the constant and shift the other input
as
    well.  This wins when the shifted constant does not require synthesis.

    That may in turn allow improvement of a select of 0 and 2^n based on the
    zero/nonzero status of a logical AND.  Essentially we can rewrite the
sequence
    to remove a data dependency.

    Concretely:

    >
    > unsigned f1 (unsigned x, unsigned m)
    > {
    >     x >>= ((m & 0x008080) ? 8 : 0);
    >     return x;
    > }

    Compiles into:

    >         li      a5,32768
    >         addi    a5,a5,128
    >         and     a1,a1,a5
    >         snez    a1,a1
    >         slliw   a1,a1,3
    >         srlw    a0,a0,a1
    >         ret

    But after this patch we generate this instead:

    >         srai    a5,a1,7
    >         andi    a5,a5,257
    >         li      a4,8
    >         czero.eqz       a1,a4,a5
    >         srlw    a0,a0,a1
    >         ret

    It's just one less instruction, but the li can issue whenever the uarch
wants
    before the srlw as it has no incoming dependency.  So we're slight more
dense
    on encoding and slightly more efficient as well.  Much like 57650, I'm
focused
    on the low level RISC-V codegen issues, not the broader issues that are
raised
    in the PR.

    This has been in my tree for a while, so it's been tested on riscv32-elf,
    riscv64-elf and bootstrapped on the BPI which has support for czero. 
Waiting
    on pre-commit CI before moving forward.

            PR rtl-optimization/56096
    gcc/
            * config/riscv/riscv.md: Add new patterns to optimize certain cases
with
            a logical AND feeding an equality test against zero.

    gcc/testsuite/

            * gcc.target/riscv/pr56096.c: New test.

Reply via email to