Merging identical functions in GCC
Joe Buck
Joe.Buck@synopsys.COM
Wed Sep 20 19:13:00 GMT 2006
On Wed, Sep 20, 2006 at 02:52:40AM -0300, Alexandre Oliva wrote:
> The linker already has code to merge sections. There's nothing to
> stop us from compiling every function into say:
>
> foo:
> jmp .foo.impl
> .section .gnu.impl.foo, "axM"
> .foo.impl:
> [actual code for foo emitted]
> .previous
>
> Then the linker doesn't need any smarts whatsoever to merge functions
> with identical code. Hmm, maybe it does, since it must compare
> relocations as well, but this doesn't sound too hard at first.
>
> For extra bonus points, it may short-circuit relocations that call foo
> (as opposed to taking its address) when it binds locally and skip the
> jmp altogether. Ditto for *all* relocations that reference foo, when
> it doesn't merge .foo.impl with anything else.
Alternatively, the compiler could arrange that pointers to functions always
point to a jump that then leads to the function, while direct calls
go directly to the function. That way pointers compare unequal
but the code is merged.
More information about the Gcc
mailing list