https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70341
Bug ID: 70341 Summary: Code size increase on ARM cortex-m3 for switch statements Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: fredrik.hederstie...@securitas-direct.com Target Milestone: --- Created attachment 38049 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38049&action=edit test_switch.c Starting with GCC-4.9.x suboptimal code is generated for this switch-statement. Toolchain arm-none-eabi. extern void handle_case_1(int name); extern void handle_case_2(int name); extern void handle_case_3(int name); extern void handle_case_4(int name); struct item_s { int index; int type; int name; int data; }; struct table_s { struct item_s items[1]; }; void test(struct table_s *table, int xi) { struct item_s *item = &(table->items[xi]); switch (item->type) { case 1: handle_case_1(item->name); break; case 2: handle_case_2(item->name); break; case 3: handle_case_3(item->name); break; case 4: handle_case_4(item->name); break; } } Compiled with gcc-4.6.x, gcc-4.7.x, gcc-4.8.x: 00000000 <test>: 0: eb00 1101 add.w r1, r0, r1, lsl #4 4: 684b ldr r3, [r1, #4] 6: 3b01 subs r3, #1 8: 2b03 cmp r3, #3 a: d80f bhi.n 2c <test+0x2c> c: e8df f003 tbb [pc, r3] 10: 0b080502 bleq 201420 <test+0x201420> 14: 6888 ldr r0, [r1, #8] 16: f7ff bffe b.w 0 <handle_case_1> 1a: 6888 ldr r0, [r1, #8] 1c: f7ff bffe b.w 0 <handle_case_2> 20: 6888 ldr r0, [r1, #8] 22: f7ff bffe b.w 0 <handle_case_3> 26: 6888 ldr r0, [r1, #8] 28: f7ff bffe b.w 0 <handle_case_4> 2c: 4770 bx lr Compiled with 4.9.x, 5.3.0, and with current master: 00000000 <test>: 0: 0109 lsls r1, r1, #4 2: 1843 adds r3, r0, r1 4: 685b ldr r3, [r3, #4] 6: 3b01 subs r3, #1 8: 2b03 cmp r3, #3 a: d813 bhi.n 34 <test+0x34> c: e8df f003 tbb [pc, r3] 10: 0e0a0602 cfmadd32eq mvax0, mvfx0, mvfx10, mvfx2 14: 4408 add r0, r1 16: 6880 ldr r0, [r0, #8] 18: f7ff bffe b.w 0 <handle_case_1> 1c: 4408 add r0, r1 1e: 6880 ldr r0, [r0, #8] 20: f7ff bffe b.w 0 <handle_case_2> 24: 4408 add r0, r1 26: 6880 ldr r0, [r0, #8] 28: f7ff bffe b.w 0 <handle_case_3> 2c: 4408 add r0, r1 2e: 6880 ldr r0, [r0, #8] 30: f7ff bffe b.w 0 <handle_case_4> 34: 4770 bx lr Flags: -mcpu=cortex-m3 Both -Os and -O2 gives increased code size.