https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118591
Bug ID: 118591 Summary: [lra][avr] Wrong code with -mlra in pr43879-3.c Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: gjl at gcc dot gnu.org Target Milestone: --- Created attachment 60227 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60227&action=edit pr43879-3.c: C test case from test suite gcc.dg/torture/pr43879-3.c is failing with -mlra: $ avr-gcc -mmcu=attiny40 -dumpbase "" -save-temps -dp -Os -o pr43879-3.elf -mlra pr43879-3.c Target: avr Configured with: --target=avr --disable-nls --with-dwarf2 --with-gnu-as --with-gnu-ld --with-long-double=64 --disable-libcc1 --disable-shared --enable-languages=c,c++ gcc version 15.0.1 20250121 (experimental) (GCC) The assembly reads: main: ... /* prologue: function */ /* frame size = 8 */ /* stack size = 8 */ .L__stack_usage = 8 # Y[5]:SI = 5 looks ok. ldi r20,lo8(5) ; 173 [c=4 l=1] movqi_insn/1 subi r28,-5 ; 120 [c=8 l=2] *addhi3/3 sbci r29,-1 st Y+,r20 ; 169 [c=4 l=1] movqi_insn/2 st Y+,__zero_reg__ ; 177 [c=4 l=1] movqi_insn/2 st Y+,__zero_reg__ ; 178 [c=4 l=1] movqi_insn/2 st Y+,__zero_reg__ ; 179 [c=4 l=1] movqi_insn/2 subi r28,9 ; 122 [c=8 l=2] *addhi3/3 sbc r29,__zero_reg__ .L9: # Y[1]:SI = 5 looks ok. ldi r20,lo8(5) ; 180 [c=4 l=1] movqi_insn/1 ld __tmp_reg__,Y+ ; 73 [c=8 l=1] *addhi3/3 st Y+,r20 ; 143 [c=4 l=1] movqi_insn/2 st Y+,__zero_reg__ ; 184 [c=4 l=1] movqi_insn/2 st Y+,__zero_reg__ ; 185 [c=4 l=1] movqi_insn/2 st Y+,__zero_reg__ ; 186 [c=4 l=1] movqi_insn/2 subi r28,5 ; 75 [c=8 l=2] *addhi3/3 sbc r29,__zero_reg__ .L10: # Reding 4 times R20:QI = Y[4] ??? subi r28,-4 ; 88 [c=8 l=2] *addhi3/3 sbci r29,-1 ld r20,Y ; 89 [c=4 l=1] movqi_insn/3 push r20 ; 11 [c=4 l=1] pushqi1/0 ld r20,Y ; 93 [c=4 l=1] movqi_insn/3 push r20 ; 13 [c=4 l=1] pushqi1/0 ld r20,Y ; 97 [c=4 l=1] movqi_insn/3 push r20 ; 15 [c=4 l=1] pushqi1/0 ld r20,Y ; 101 [c=4 l=1] movqi_insn/3 push r20 ; 17 [c=4 l=1] pushqi1/0 subi r28,-5 ; 132 [c=8 l=2] *addhi3/3 sbci r29,-1 # Reading from R22:SI = Y[9] but was never initialized. Note frame size is 8. ld r22,Y+ ; 155 [c=4 l=1] movqi_insn/3 ld r23,Y+ ; 156 [c=4 l=1] movqi_insn/3 ld r24,Y+ ; 157 [c=4 l=1] movqi_insn/3 ld r25,Y+ ; 158 [c=4 l=1] movqi_insn/3 rcall f1 ; 19 [c=0 l=1] call_insn/1 The code after label .L10 looks very wrong. R23 (and maybe other regs) are never properly initialized since they read garbage from the stack. FYI, the error persists with -mno-fuse-add and is a bit more complicated then, but the problem remains: .L10: subi r28,lo8(-(4)) ; 78 [c=4 l=5] movqi_insn/3 sbci r29,hi8(-(4)) ld r20,Y subi r28,lo8((4)) sbci r29,hi8((4)) push r20 ; 11 [c=4 l=1] pushqi1/0 subi r28,lo8(-(4)) ; 79 [c=4 l=5] movqi_insn/3 sbci r29,hi8(-(4)) ld r20,Y subi r28,lo8((4)) sbci r29,hi8((4)) push r20 ; 13 [c=4 l=1] pushqi1/0 subi r28,lo8(-(4)) ; 80 [c=4 l=5] movqi_insn/3 sbci r29,hi8(-(4)) ld r20,Y subi r28,lo8((4)) sbci r29,hi8((4)) push r20 ; 15 [c=4 l=1] pushqi1/0 subi r28,lo8(-(4)) ; 81 [c=4 l=5] movqi_insn/3 sbci r29,hi8(-(4)) ld r20,Y subi r28,lo8((4)) sbci r29,hi8((4)) push r20 ; 17 [c=4 l=1] pushqi1/0 subi r28,lo8(-(9)) ; 82 [c=16 l=8] *movsi/2 sbci r29,hi8(-(9)) ld r22,Y+ ld r23,Y+ ld r24,Y+ ld r25,Y subi r28,lo8((9+3)) sbci r29,hi8((9+3)) Running the testsuite from $builddir/gcc: $ make -k check-gcc RUNTESTFLAGS="--target_board=attiny40-sim dg-torture.exp=pr43879-3.c -tool_opts='-mlra'" ... FAIL: gcc.dg/torture/pr43879-3.c -O1 execution test FAIL: gcc.dg/torture/pr43879-3.c -O2 execution test FAIL: gcc.dg/torture/pr43879-3.c -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions execution test FAIL: gcc.dg/torture/pr43879-3.c -O3 -g execution test FAIL: gcc.dg/torture/pr43879-3.c -Os execution test FAIL: gcc.dg/torture/pr43879-3.c -O2 -flto -fno-use-linker-plugin -flto-partition=none execution test