https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113032
Bug ID: 113032 Summary: RISCV linker relaxation leaves redundant addi (from load immediate) 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 56887 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56887&action=edit Source files and save-temps for ELF build In the ELF image for RISCV 32-bit, the lower 12-bit parts of a load immediate are zero but not removed. Hence, it appears as a 'mv' in the objdump of the program ELF. 20010064 <_Z6globalv>: 20010064: 80000537 lui a0,0x80000 20010068: 00050513 mv a0,a0 <-- (1) 2001006c: 00008067 ret 20010070 <_Z4initv>: 20010070: 800007b7 lui a5,0x80000 20010074: 00078793 mv a5,a5 <-- (2) 20010078: 3e700713 li a4,999 2001007c: 00e7a223 sw a4,4(a5) # 80000004 <__global_pointer$+0xfffff804> 20010080: 0ea00713 li a4,234 20010084: 00e7a423 sw a4,8(a5) 20010088: 00008067 ret 2001008c <_Z5pcnt0v>: 2001008c: 800007b7 lui a5,0x80000 20010090: 00078793 mv a5,a5 <-- (3) 20010094: 0047a703 lw a4,4(a5) # 80000004 <__global_pointer$+0xfffff804> 20010098: 60271713 cpop a4,a4 The command line is: riscv64-unknown-elf-g++ --save-temps -march=rv32imv_zbb_zbs -mabi=ilp32 -O3 -Wall -Wextra -o relax.elf main.cpp test.cpp startup.cpp -fno-builtin -static -fno-common -mcmodel=medlow -nostdlib -T link.ld The behavior is the same if I drop -fno-builtin -fno-common -mcmodel (just need to provide memset). I'm attaching a tar-file for the above. I reviewed 91713 but concluded that it's different. I also filed yesterday. I apologize for wasting reviewer time. It's the same test case but I'm now including the main to show overall optimization. Thanks for the previous feedback.