Bug 52025

Summary: caller-saves triggers a frame without need
Product: gcc Reporter: Georg-Johann Lay <gjl>
Component: rtl-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: eric.weddington
Priority: P3 Keywords: missed-optimization, ra
Version: 4.7.0   
Target Milestone: ---   
Host: Target: avr
Build: Known to work:
Known to fail: 4.5.2, 4.6.2 Last reconfirmed: 2012-01-27 00:00:00
Attachments: main-loop.c

Description Georg-Johann Lay 2012-01-27 23:06:08 UTC
The following code triggers a frame even though there is just one local variable and there are plenty of call-saved registers.

Apart from the optimization regression, this PR might be related to PR50925.

Notice that the code is a minimized test case from the top-level code of a real-world embedded application. Therefore, the code runs in an infinite loop.

extern void putch (char);
extern char getch (void);
extern void verify (void);

void main_loop (void)
{
    unsigned char c;
    
    for (;;)
    {
        getch();
        
        c = getch();
        verify();
        putch (c);
    }
}

The generated code with -Os -dp -save-temps is

main_loop:
	push r29	 ;  36	*pushhi/1	[length = 2]
	push r28
	push __tmp_reg__	 ;  40	*addhi3_sp_R_pc2	[length = 1]
	in r28,__SP_L__	 ;  41	*movhi_sp/2	[length = 2]
	in r29,__SP_H__
.L2:
	rcall getch	 ;  7	call_value_insn/3	[length = 1]
	rcall getch	 ;  9	call_value_insn/3	[length = 1]
	std Y+1,r24	 ;  34	*movqi/3	[length = 1]
	rcall verify	 ;  12	call_insn/3	[length = 1]
	ldd r24,Y+1	 ;  35	*movqi/4	[length = 1]
	rcall putch	 ;  15	call_insn/3	[length = 1]
	rjmp .L2	 ;  44	jump	[length = 1]


The insns triggering the frame are 34 and 35.

-fno-caller-saves resolves the issue and the code then then uses r17 to backup the value from getch.

main_loop:
	push r17	 ;  30	*pushqi/1	[length = 1]
.L2:
	rcall getch	 ;  7	call_value_insn/3	[length = 1]
	rcall getch	 ;  9	call_value_insn/3	[length = 1]
	mov r17,r24	 ;  10	*movqi/1	[length = 1]
	rcall verify	 ;  12	call_insn/3	[length = 1]
	mov r24,r17	 ;  14	*movqi/1	[length = 1]
	rcall putch	 ;  15	call_insn/3	[length = 1]
	rjmp .L2	 ;  33	jump	[length = 1]

Target: avr
Configured with: ../../gcc.gnu.org/trunk/configure --target=avr --prefix=/home/georg/install/gcc-4.7 --enable-languages=c,c++ --disable-nls --disable-shared --disable-lto --disable-checking --disable-libquadmath --with-dwarf2
Thread model: single
gcc version 4.7.0 20120102 (experimental) (GCC)
Comment 1 Georg-Johann Lay 2012-01-27 23:08:34 UTC
Created attachment 26485 [details]
main-loop.c
Comment 2 Georg-Johann Lay 2017-01-07 12:18:48 UTC
Not actually fixed, but since v6.2+ -fno-caller-saves is the default, cf. r239080.

*** This bug has been marked as a duplicate of bug 70677 ***