http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49054
Summary: useless cmp+jmp generated for switch when "default:"
is unreachable
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
Host: x86-64
Build: x86-64
gcc (Debian 4.6.0-7) 4.6.1 20110507 (prerelease)
When I compile the following switch, GCC generates code to
check that id <= 4 and to conditionally jump to... the next instruction.
% cat foo.c
unsigned f(void);
unsigned g(void);
unsigned h(void);
unsigned i(void);
unsigned j(void);
unsigned int baz(unsigned int id)
{
switch (id)
{
case 0:
return f();
case 1:
return g();
case 2:
return h();
case 3:
return i();
case 4:
return j();
default:
__builtin_unreachable();
}
}
% gcc -march=core2 -m64 -O3 foo.c -c -o foo.o
% objdump -DC foo.o | head -23
foo.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <baz>:
0: 83 ff 05 cmp $0x4,%edi
3: 76 03 jbe 8 <baz+0x8>
5: 0f 1f 00 nopl (%rax)
8: 89 ff mov %edi,%edi
a: ff 24 fd 00 00 00 00 jmpq *0x0(,%rdi,8)
11: 0f 1f 80 00 00 00 00 nopl 0x0(%rax)
18: e9 00 00 00 00 jmpq 1d <baz+0x1d>
1d: 0f 1f 00 nopl (%rax)
20: e9 00 00 00 00 jmpq 25 <baz+0x25>
25: 0f 1f 00 nopl (%rax)
28: e9 00 00 00 00 jmpq 2d <baz+0x2d>
2d: 0f 1f 00 nopl (%rax)
30: e9 00 00 00 00 jmpq 35 <baz+0x35>
35: 0f 1f 00 nopl (%rax)
38: e9 00 00 00 00 jmpq 3d <baz+0x3d>
What is the point of the first three instructions? I would have expected baz
to start at adress 8.