Bug 41849 - optimization fails when register variables are used for an interrupt
Summary: optimization fails when register variables are used for an interrupt
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.4.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-27 19:27 UTC by Chris Efstathiou
Modified: 2011-07-25 15:26 UTC (History)
3 users (show)

See Also:
Host: Linux ubuntu 9.04
Target: avr
Build: Linux i386
Known to work: 4.4.2, 4.5.0, 4.5.2, 4.6.1
Known to fail:
Last reconfirmed:


Attachments
C test case (214 bytes, text/plain)
2011-07-25 15:19 UTC, Georg-Johann Lay
Details
Test case from attachment 24828 as compiled with avr-gcc 4.4.2 (1.20 KB, text/plain)
2011-07-25 15:21 UTC, Georg-Johann Lay
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Efstathiou 2009-10-27 19:27:49 UTC
If i use global register variables that are used and updated inside an interrupt the optimizer produces broken code.
Code example:
register volatile unsigned char  timer0_h8 asm("r3");
volatile unsigned char timer0_h8_buf = 0;

void foo(void)
{
timer0_h8_buf = timer0_h8; 

return;
}

ISR(TIMER0_OVF_vect)
{
   timer0_h8++;
}  

produces:

2432 0b5a 1092 0000  sts timer0_h8_buf,__zero_reg__	 ;  timer0_h8_buf,

instead of this when no optimization is used:

3248 104a 832D      		mov r24,r3	 ;  timer0_h8.47, timer0_h8
3249 104c 8093 0000 		sts timer0_h8_buf,r24	 ;  timer0_h8_buf, timer0_h8.47
Comment 1 Georg-Johann Lay 2011-04-20 19:45:54 UTC
Unfortunately, we don't see the exact command line.

I don't see this for 4.5.0, tried -O, -O2, -Os, -O3. All gives

foo:
/* prologue: function */
/* frame size = 0 */
/* stack size = 0 */
.L__stack_usage = 0
	sts timer0_h8_buf,r3
/* epilogue start */
	ret

Note that volatile on register variables does not work as expected. volatile is meaningful for memory locations only.

As the addresses in your snipp indicate there is much more code in the module, so presumably we don't see the proper source.
Comment 2 Georg-Johann Lay 2011-07-25 15:19:28 UTC
Created attachment 24828 [details]
C test case
Comment 3 Georg-Johann Lay 2011-07-25 15:21:54 UTC
Created attachment 24829 [details]
Test case from attachment 24828 [details] as compiled with avr-gcc 4.4.2

For compiler options, see the attachment: -Os -mmcu=atmega8.
Comment 4 Georg-Johann Lay 2011-07-25 15:26:02 UTC
Closed as WORKSFORME. avr-gcc 4.4.2 compiles the test case correctly.