In the following code:
void h(void);
static void g()
{
h();
}
static void (*f)(void) = g;
void k(void)
{
f();
}
It is trivial to see that 'f' cannot change and thus the statement 'f();' can
be compiled as a direct call or jump. gcc however emits an indirect jump on
x86_64:
0: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax # 7 <k+0x7>
3: R_X86_64_PC32 .rodata-0x4
7: ff e0 jmpq *%rax
Changing the initialization to 'f = h' produces the desired results:
0000000000000000 <k>:
0: e9 00 00 00 00 jmpq 5 <k+0x5>
1: R_X86_64_PC32 h-0x4
Both compiled with -O3 (though expected to work with -O2)
--
Summary: gcc fails to elide indirect function call through
immutable static variable
Product: gcc
Version: 4.4.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: avi at argo dot co dot il
GCC build triplet: x86_64-pc-linux
GCC host triplet: x86_64-pc-linux
GCC target triplet: x86_64-pc-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41483