http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49857
Summary: Put constant switch-tables into flash Product: gcc Version: 4.6.1 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: target AssignedTo: unassig...@gcc.gnu.org ReportedBy: g...@gcc.gnu.org CC: eric.wedding...@atmel.com Target: avr Created attachment 24837 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24837 C source with constant switch statement For constant switches like int sw_2 (char x) { switch(x) { case '0': return -1; case '1': return 2; case '2': return 3; case '3': return 5; case '4': return 7; case '5': return 11; case '6': return 13; case '7': return 17; case '8': return 19; case '9': return 23; case 'a':return 29; case 'A':return 29; } return -1; } avr-gcc 4.6.1 generates a lookup table (-Os -mmcu=atmega8 -S -fverbose-asm) sw_2: subi r24,lo8(-(-49)) ; csui.0, cpi r24,lo8(49) ; csui.0, brsh .L3 ; , mov r30,r24 ; tmp50, csui.0 ldi r31,lo8(0) ; , subi r30,lo8(-(CSWTCH.1)) ; tmp50, sbci r31,hi8(-(CSWTCH.1)) ; tmp50, ld r24,Z ; tmp51, CSWTCH.1 clr r25 ; D.1929 sbrc r24,7 ; D.1929 com r25 ; D.1929 ret .L3: ldi r24,lo8(-1) ; D.1929, ldi r25,hi8(-1) ; D.1929, ret .size sw_2, .-sw_2 .data .type CSWTCH.1, @object .size CSWTCH.1, 49 CSWTCH.1: .byte 2 .byte 3 .byte 5 .byte 7 .byte 11 .byte 13 .byte 17 .byte 19 .byte 23 .byte -1 .byte -1 .byte -1 ... CSWTCH.1 is put in .data (.rodata would not be better) so that the lookup table ends up in RAM. The table is constant and could go into flash memory, i.e. .progmem.data. =================================== avr-gcc -v -Os -fverbose-asm -mmcu=atmega8 -W -Wall -S foo.c # dp -mmcu=atmega128 foo.c -save-temps -Os -c Using built-in specs. COLLECT_GCC=e:\WinAVR\4.6.1\bin\avr-gcc.exe COLLECT_LTO_WRAPPER=e:/winavr/4.6.1/bin/../libexec/gcc/avr/4.6.1/lto-wrapper.exe Target: avr Configured with: ../../gcc.gnu.org/gcc-4_6-branch/configure --target=avr --prefix=/local/gnu/install/gcc-4.6-mingw32 --host=i586-mingw32 --build=i686-linux-gnu --enable-languages=c,c++ --disable-nls --disable-shared --with-dwarf2 Thread model: single gcc version 4.6.1 20110620 (prerelease) (GCC) GNU C (GCC) version 4.6.1 20110620 (prerelease) (avr) compiled by GNU C version 3.3.1 (mingw special 20030804-1), GMP version 4.3.2, MPFR version 2.4.2, MPC version 0.8.2 GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32702