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

Chris Lattner clattner at apple.com
Tue Mar 8 09:32:13 PST 2011


On Mar 8, 2011, at 1:45 AM, John McCall wrote:

>> 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?

FWIW, LLVM does have a function body merging pass (lib/Transforms/IPO/MergeFunctions.cpp).  It isn't turned on by default yet, but hopefully will be in the LLVM 3.0 release.

-Chris



More information about the cfe-dev mailing list