https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70676

            Bug ID: 70676
           Summary: suboptimal code generation on AVR
           Product: gcc
           Version: 5.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: night_ghost at ykoctpa dot ru
  Target Milestone: ---

gcc -Os -fno-optimize-sibling-calls

don't optimizes out tail recursion:

return SPI::transfer(0xff);
     f54:8f ef       ldi r24, 0xFF; 255
     f56:0e 94 d9 2e call 0x5db2 ; 0x5db2 <_ZN3SPI8transferEh>
}
     f5a:08 95       ret

where it should be 
     ldi r24, 0xFF; 255
     jmp  0x5db2 ; 0x5db2 <_ZN3SPI8transferEh>

Yes I can remove -fno-optimize-sibling-calls but then size of compiled project
will be much more:

with:    30566 bytes (93.3% Full)
without: 30772 bytes and binary don't fit to flash (30720 is maximum)

Reason is that even with -Os optimize-sibling-calls tries to make epilogue for
each tail recursion:


    2080:<----->92 e0       <-->ldi<--->r25, 0x02<----->; 2
    2082:<----->df 91       <-->pop<--->r29
    2084:<----->cf 91       <-->pop<--->r28
    2086:<----->1f 91       <-->pop<--->r17
    2088:<----->0f 91       <-->pop<--->r16
    208a:<----->ff 90       <-->pop<--->r15
    208c:<----->ef 90       <-->pop<--->r14
    208e:<----->bf 90       <-->pop<--->r11
    2090:<----->af 90       <-->pop<--->r10
    2092:<----->9f 90       <-->pop<--->r9
    2094:<----->8f 90       <-->pop<--->r8
    2096:<----->0c 94 2a 0f <-->jmp<--->0x1e54<>; 0x1e54
<_ZL12osd_printf_1PKcf>
(rjmp here)
    209a:<----->df 91       <-->pop<--->r29
    209c:<----->cf 91       <-->pop<--->r28
    209e:<----->1f 91       <-->pop<--->r17
    20a0:<----->0f 91       <-->pop<--->r16
    20a2:<----->ff 90       <-->pop<--->r15
    20a4:<----->ef 90       <-->pop<--->r14
    20a6:<----->bf 90       <-->pop<--->r11
    20a8:<----->af 90       <-->pop<--->r10
    20aa:<----->9f 90       <-->pop<--->r9
    20ac:<----->8f 90       <-->pop<--->r8
    20ae:<----->08 95       <-->ret

Can GCC in -Os really optimize only size and rollback optimizations if size of
"optimized" code is more than size of non-optimized ?

Reply via email to