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

John McCall rjmccall at apple.com
Tue Mar 8 01:45:09 PST 2011


On Mar 8, 2011, at 1:17 AM, 邓尧 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.
>  
>  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. 

Like I said, it's a cost/benefit analysis.  There's less potential for compression than you think:   a C++ program might have lots of templated functions, but most will be so tiny that there's no profit in merging them, even when they can't just be inlined everywhere.  There's profit in merging larger functions, but they're also more expensive to analyze, especially as they tend to have calls to other functions, which we then need to recursively ensure the equivalence of.

Do you have a specific goal in mind for this optimization?

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110308/0150a2bf/attachment.html>


More information about the cfe-dev mailing list