https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89139
Bug ID: 89139
Summary: GCC emits code for static functions that aren't used
by the optimized code
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
Consider the following C module.
**
typedef struct cont
{
void (*f) (struct cont, int a);
} Cont;
int quux (int a);
static void g (Cont c, Cont d, int a)
{
if (quux (a))
return g (c, d, a + 1);
c.f (d, a * 2); // XXX
}
void bar (struct cont, int a);
static void h (Cont d, int a)
{
if (d.f != bar)
d.f (d, a);
}
void foo (int a)
{
g ((Cont) { h }, (Cont) { bar }, a);
}
**
I compiled the code with the x86-64 gcc (trunk) version on https://godbolt.org
and I get:
**
h:
cmpq $bar, %rdi
je .L1
jmp *%rdi
.L1:
ret
foo:
pushq %rbx
movl %edi, %ebx
jmp .L6
.L8:
addl $1, %ebx
.L6:
movl %ebx, %edi
call quux
testl %eax, %eax
jne .L8
popq %rbx
ret
**
As one can see, code for the function `h' is emitted but nowhere used.
Interestingly, when I replace `a * 2' in the line marked with XXX by `a', gcc
does not emit code for `h':
**
foo:
pushq %rbx
movl %edi, %ebx
jmp .L3
.L6:
addl $1, %ebx
.L3:
movl %ebx, %edi
call quux
testl %eax, %eax
jne .L6
popq %rbx
ret
**