[cfe-dev] When two functions are the same could clang optimize out one of them?

Frits van Bommel fvbommel at gmail.com
Tue Mar 8 01:30:13 PST 2011


On Tue, Mar 8, 2011 at 10:17 AM, 邓尧 <torshie at gmail.com> wrote:
> On Tue, Mar 8, 2011 at 4:07 PM, John McCall <rjmccall at apple.com> wrote:
>> On Mar 7, 2011, at 9:55 PM, 邓尧 wrote:
>> > When the generated instructions of two functions "f()" & "g()" are the
>> > same(when templates are used it isn't unusual), could clang remove the
>> > function "f()" in the object file and translate "f()" calls to "g()" calls?
>>
>> C and C++ require that almost all objects, including function objects,
>> have different addresses.
>
> This isn't a big problem, just pad some nop/jmp instructions.

This can't be represented as a single function at the IR level, and
the 'jmp to the other function' is already implemented for a few
backends when they see functions that consist of only a tail call. The
only reason you're not seeing this happening is probably because the
-mergefunc pass isn't run by default (as John mentioned).

The 'nop' variant could possibly be a nice optimization for tail calls
to local functions; I'm sure patches are welcome :).




More information about the cfe-dev mailing list