https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113023
Bug ID: 113023 Summary: RISCV redundant code for loading fixed address Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: iwfinlay at gmail dot com Target Milestone: --- Created attachment 56879 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56879&action=edit Save temps from: riscv64-unknown-elf-g++ --save-temps -march=rv32imv_zbb_zbs -mabi=ilp32 -O3 -Wall -Wextra -c test.cpp Load immediate of a constant leaves a redundant addi x, x, 0 (aka mv x, x) for the lower bits of the constant in some cases. The generated code is functionally correct but loses performance. void pcnt0() updates a global structure. int pcnt1() returns the value. Both access the global structure for the input but pcnt0() has the redundant mv a5, a5. Note that the use of cpop is not significant (other functions show the same behavior in the attached test case). Disassembly of section .text: 00000028 <_Z5pcnt0v>: 28: 000007b7 lui a5,0x0 2c: 00078793 mv a5,a5 30: 0047a703 lw a4,4(a5) # 4 <_Z6globalv+0x4> 34: 60271713 cpop a4,a4 38: 00e7a023 sw a4,0(a5) 3c: 00008067 ret 00000040 <_Z5pcnt1v>: 40: 000007b7 lui a5,0x0 44: 0047a503 lw a0,4(a5) # 4 <_Z6globalv+0x4> 48: 60251513 cpop a0,a0 4c: 00008067 ret riscv64-unknown-elf-g++ --save-temps -march=rv32imv_zbb_zbs -mabi=ilp32 -O3 -Wall -Wextra -c test.cpp riscv64-unknown-elf-objdump -d test.o > test.asm (subset shown above) A brew install of GCC for RISCV is used for this case. The problem originated in client code with a customized compiler build (also 12.2).