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

--- Comment #1 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:022afdcb9b71f776b825e3d7d148cd73e33b720f

commit r17-250-g022afdcb9b71f776b825e3d7d148cd73e33b720f
Author: Jeff Law <[email protected]>
Date:   Fri May 1 06:47:07 2026 -0600

    [PR target/124559][RISC-V] Improve RISC-V constant synthesis for some
HImode constants

    So this is a trivial little bug we found doing some comparisons against
LLVM.

    For the function sub2 in load-immediate.c we get this code:

            li      a5,-32768
            sh      a5,0(a0)
            xori    a5,a5,-1
            sh      a5,0(a1)

    Note carefully that li+xori.  There's a slightly better sequence here from
an
    encoding standpoint.  Instead of using xori we can adjust the synthesis
    sequence to target an "addi" for that statement and in doing so we can save
two
    code bytes of space.

    The xori sequence was used because we can't do this in gcc:

    (set (dest:HI) (const_int 0x8000))

    We're in HI mode so the constant must be sign extended from bit 15 to a
    HOST_WIDE_INT.

    Fixing this isn't hard.  The key is realizing the vast majority of the time
we
    really don't want/need to load in HImode and in fact we're typically going
to
    be generating objects in word_mode.  So instead of passing in the
pre-promoted
    mode, pass in the post-promoted mode.

    That's fine and good with one caveat.   CSE fails to use NEG/NOT to derive
a
    new constant from an older constant, even if the cost is smaller, which
caused
    a code quality regression elsewhere on the RISC-V port.  So this patch
adjusts
    CSE ever-so-slightly to allow it to derive constants from a previous
constant
    using NOT/NEG in a fairly obvious way.

    This has been in my tester for a while, so it's been through the usual
    bootstrap & regression test on the Pioneer, BPI, x86 and aarch64 and others
as
    well as testing across the various embedded targets.

    Waiting on pre-commit testing to do its thing.

            PR target/124559
    gcc/
            * config/riscv/riscv-protos.h (riscv_move_integer): Drop mode
argument.
            * config/riscv/riscv.cc (riscv_move_integer): Pass mode after
promotions
            to riscv_build_integer.  All callers changed.
            * config/riscv/riscv.md: Corresponding changes.
            * cse.cc (cse_insn): Try to derive one constant from another using
NOT/NEG.

Reply via email to