<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Mar 8, 2011, at 1:17 AM, 邓尧 wrote:</div><blockquote type="cite"><div class="gmail_quote">On Tue, Mar 8, 2011 at 4:07 PM, John McCall <span dir="ltr"><<a href="mailto:rjmccall@apple.com">rjmccall@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On Mar 7, 2011, at 9:55 PM, 邓尧 wrote:<br>
> 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?<br>
<br>
</div>C and C++ require that almost all objects, including function objects, have different addresses. </blockquote><div>This isn't a big problem, just pad some nop/jmp instructions.<br> <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
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.<br>
</blockquote><div> </div><div>I know only a little about compilation theories, so the following possibly are just nonsense.<br>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. </div></div>
</blockquote><br></div><div>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.</div><div><br></div><div>Do you have a specific goal in mind for this optimization?</div><div><br></div><div>John.</div></body></html>