Bug 39223

Summary: volatile bug on AVR
Product: gcc Reporter: John Regehr <regehr>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: anitha.boyapati, eric.weddington, gcc-bugs
Priority: P3 Keywords: wrong-code
Version: 4.3.3   
Target Milestone: 4.4.2   
Host: Target: avr-*-*
Build: Known to work:
Known to fail: 4.3.2 4.3.3 Last reconfirmed: 2009-02-18 15:19:37
Attachments: failure-inducing C program
asm displaying volatile bug

Description John Regehr 2009-02-18 04:58:14 UTC
This bug is seen using the avr-gcc that is built using the script that comes with FemtoOS 0.88.

Compile the attached program like this:

  avr-gcc -O -mmcu=atmega128 small.c -S

It can be seen that func_45() unconditionally loads from volatile g_54.  That is wrong, since in the C code the load is protected by a test.
Comment 1 John Regehr 2009-02-18 04:58:58 UTC
Created attachment 17317 [details]
failure-inducing C program
Comment 2 John Regehr 2009-02-18 04:59:28 UTC
Created attachment 17318 [details]
asm displaying volatile bug
Comment 3 Richard Biener 2009-02-18 10:03:58 UTC
Please fill out GCC version this bug is reported against.
Comment 4 Eric Weddington 2009-02-18 15:19:37 UTC
Fail on 4.3.2 with -O1, success with -O[023s].
Comment 5 Anitha Boyapati 2010-02-17 11:09:02 UTC
Fails with gcc 4.4.3 and gcc 4.5 with -O1.
Comment 6 Anitha Boyapati 2010-02-17 11:19:29 UTC
An observation - the same file when compiled with -O1 for i386 target also appears to load g_54 using movl instruction.

_func_45:
        ...
L11:
        testw   %bx, %bx
        je      L7
        movl    _g_54, %eax
L7:
        movl    $1, 4(%esp)
        
Comment 7 abnikant 2010-03-08 05:22:47 UTC
(In reply to comment #6)
> An observation - the same file when compiled with -O1 for i386 target also
> appears to load g_54 using movl instruction.
> 
> _func_45:
>         ...
> L11:
>         testw   %bx, %bx
>         je      L7
>         movl    _g_54, %eax
> L7:
>         movl    $1, 4(%esp)
> 
> 

It loads g_54 conditionally with gcc-4.4.0 and higher version in -O1
Comment 8 abnikant 2010-03-08 05:26:03 UTC
avr-gcc-4.4.0 -S -O1 small.c

code snippet version avr-gcc-4.4.0
.L__stack_usage = 0
    rcall func_15
    sbiw r24,0
    breq .L4
    lds r24,g_54
    lds r25,g_54+1
    lds r26,g_54+2
    lds r27,g_54+3
.L4:
    sts g_8,__zero_reg__
    sts g_8+1,__zero_reg__
    sts g_8+2,__zero_reg__
    sts g_8+3,__zero_reg__
Comment 9 Anitha Boyapati 2010-03-08 06:04:36 UTC
(In reply to comment #5)
> Fails with gcc 4.4.3 and gcc 4.5 with -O1.
> 

Correction : The bug passes with 4.4.2 and above versions. Code generated can be seen below.

func_45:
        ...
        rcall func_15
        mov r28,r24
        mov r29,r25
        ldi r22,lo8(1)
        ldi r23,hi8(1)
        ldi r24,hlo8(1)
        ldi r25,hhi8(1)
.L9:
        sbiw r28,0
        breq .L8
        lds r18,g_54
        lds r19,(g_54)+1
        lds r20,(g_54)+2
        lds r21,(g_54)+3
.L8:
        ldi r18,lo8(1)
Comment 10 Eric Weddington 2010-03-08 15:36:03 UTC
(In reply to comment #9)
> (In reply to comment #5)

> Correction : The bug passes with 4.4.2 and above versions.

Closed as FIXED.