http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51513
Bug #: 51513 Summary: [missed optimization] Only partially optimizes away unreachable switch default case Classification: Unclassified Product: gcc Version: 4.6.2 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: sgunder...@bigfoot.com Hi, I have code that looks like this: pannekake:~> cat test.c void foo(); void bar(); void baz(); void func(int i) { switch (i) { case 0: foo(); break; case 1: bar(); break; case 2: baz(); break; case 3: baz(); break; case 4: bar(); break; case 5: foo(); break; case 6: foo(); break; case 7: bar(); break; case 8: baz(); break; case 9: baz(); break; case 10: bar(); break; default: __builtin_unreachable(); break; } } Compiling this yields: pannekake:~> gcc-4.6 -O2 -c test.c && objdump --disassemble test.o test.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <func>: 0: 83 ff 0a cmp $0xa,%edi 3: 76 03 jbe 8 <func+0x8> 5: 0f 1f 00 nopl (%rax) 8: 89 ff mov %edi,%edi a: 31 c0 xor %eax,%eax c: ff 24 fd 00 00 00 00 jmpq *0x0(,%rdi,8) 13: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 18: e9 00 00 00 00 jmpq 1d <func+0x1d> 1d: 0f 1f 00 nopl (%rax) 20: e9 00 00 00 00 jmpq 25 <func+0x25> 25: 0f 1f 00 nopl (%rax) 28: e9 00 00 00 00 jmpq 2d <func+0x2d> The first compare is, as you can see, unneeded; the code for the default case itself (a repz ret) has been optimized away due to the __builtin_unreachable(), but the compare and branch remains. I've also seen it sometimes be able to remove the jump instruction itself, but not the compare.