http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50910

             Bug #: 50910
           Summary: [avr] inefficient division by 2
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: g...@gcc.gnu.org
        ReportedBy: g...@gcc.gnu.org
                CC: eric.wedding...@atmel.com
            Target: avr


The following source

char c;

void bar_c (int x)
{
    c = x ? x/2 : c+1;
}

reveals two issues

== 1 == 

Compiled with

$ avr-gcc -S -Os

there is a call to libgcc's division routine. RTX costs of division has to be
checked and costs of loading the constant added to the costs of division.

bar_c:
    sbiw r24,0
    breq .L2
    ldi r22,lo8(2)
    clr r23
    rcall __divmodhi4
    rjmp .L3
.L2:
    lds r22,c
    subi r22,lo8(-(1))
.L3:
    sts c,r22
    ret

== 1 == 

Compiled with

$ avr-gcc -S -O2

the code is happily jumping around to L7 and then back to L3.

bar_c:
    sbiw r24,0
    brne .L6
    lds r24,c
    subi r24,lo8(-(1))
    sts c,r24
    ret
.L6:
    sbrc r25,7
    rjmp .L7
.L3:
    asr r25
    ror r24
    sts c,r24
    ret
.L7:
    adiw r24,1
    rjmp .L3

Presumably, the cause is 
    #define BRANCH_COST 0
in avr.h, and the sequence could be instead:

    ...
.L6:
    sbrc r25,7
    adiw r24,1
.L3:
    asr r25
    ror r24
    sts c,r24
    ret

Reply via email to