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

邓尧 torshie at gmail.com
Tue Mar 8 01:17:26 PST 2011


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.


>  In principle, we can detect cases where a program doesn't rely on the
> address of a function;  in other cases we can just emit one function as a
> tail call to the other.  There's actually already an LLVM optimization that
> does something like this, although I believe it's not enabled by default
> because detecting the equivalence of functions is very difficult to do
> quickly.
>

I know only a little about compilation theories, so the following possibly
are just nonsense.
Detect equivalence of functions may be done at two different levels, one is
at the IR level, the other is at the C++ function template level. The second
one is useful because different instances of the same template should be the
largest source of equivalence. To detect at the IR level: 1. get hash of a
functions (can we??? I have no idea on this).  2. sort the hash 3. search &
compare. To detect at the C++ function template level is much more
complicated, I currently don't know what situations should be considered.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110308/4364ea79/attachment.html>


More information about the cfe-dev mailing list