[Bug target/126454] [16/17 Regression] RISCV: cm.popretz pass deletes "li a0,0" and gives wrong return value

"cvs-commit at gcc dot gnu.org via Gcc-bugs" <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126454

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

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

commit r16-9534-g3cdb74f43678a97b01f1de0cfe9eb218d73460f7
Author: Kito Cheng <[email protected]>
Date:   Tue Aug 4 10:43:37 2026 +0800

    RISC-V: Don't let popretz combine across a call [PR target/126454]

    The popretz pass scans backwards from cm.popret for the "li a0, 0" that
    feeds the (use a0), but nothing stopped the scan at a call: the argument
    registers of a call live in CALL_INSN_FUNCTION_USAGE, which
    reg_referenced_p does not look at, and the a0 set by a call_value hides
    in a PARALLEL, which the bare SET test did not match.  A "li a0, 0" that
    sets up the first argument of a call was therefore deleted and cm.popret
    became cm.popretz, so the callee got garbage in a0 and the caller
    returned 0 instead of the result of the callee.

    Use insn level helpers instead, find_reg_fusage for the uses and
    reg_set_p for the definitions, and ask them about the word_mode a0
    rather than about the a0 of the (use a0), whose mode covers a0 and a1
    for a DImode return value on rv32.

    gcc/ChangeLog:

            PR target/126454
            * config/riscv/riscv-opt-popretz.cc
(pass_combine_popretz::execute):
            Stop the backward scan at any use or definition of a0, including
            those hidden in CALL_INSN_FUNCTION_USAGE or in a PARALLEL.

    gcc/testsuite/ChangeLog:

            PR target/126454
            * gcc.target/riscv/pr126454.c: New test.

    (cherry picked from commit c80ac3cb559f6ea2be6be3d0458c1615ff27c9a0)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.