<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">you can find unused functions easily using static analysis tools; codeql comes to mind:<div><br></div><div><a href="https://help.semmle.com/QL/learn-ql/cpp/function-classes.html">https://help.semmle.com/QL/learn-ql/cpp/function-classes.html</a></div><div><br></div><div><br></div><div><pre style="overflow-x: auto; overflow-y: hidden; padding: 7px 30px; margin-top: 15px; margin-bottom: 15px; line-height: 1.3em; border: 1px solid rgb(187, 187, 187); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;"><font face="UICTFontTextStyleBody"><span style="white-space: normal; background-color: rgba(255, 255, 255, 0);"><span class="k" style="font-weight: bold;">import</span> <span class="n">cpp</span>

<span class="k" style="font-weight: bold;">from</span> <span class="n">Function</span> <span class="n">f</span>
<span class="k" style="font-weight: bold;">where</span> <span class="k" style="font-weight: bold;">not</span> <span class="k" style="font-weight: bold;">exists</span><span class="p">(</span><span class="n">FunctionCall</span> <span class="n">fc</span> <span class="o">|</span> <span class="n">fc</span><span class="p">.</span><span class="n">getTarget</span><span class="p">()</span> <span class="o">=</span> <span class="n">f</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">select</span> <span class="n">f</span><span class="p">,</span> <span class="s">"This function is never called."</span></span></font></pre></div><div><br><br><div id="AppleMailSignature" dir="ltr">Sent from my iPhone</div><div dir="ltr"><br>On Jan 27, 2020, at 19:43, Gábor Horváth via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br><br></div><blockquote type="cite"><div dir="ltr"><div dir="ltr">Hi!<div><br></div><div>LLVM newbie here, I was mainly working on the frontend so far. We had a small hackathon project idea to piggyback on LTO to detect dead code (unused functions). The basic idea is to compile the code for every target and dump the removed functions. Intersect the function symbol names for each target and those functions should be safe to remove from the source code (unless there were some configuration that was not compiled). Is this reasonable?</div><div><br></div><div>I started to play around with some toy examples and got stuck on the very beginning not being able to figure out where the unused functions are actually getting removed.<br><br>Here is what I did:<br>tu1.cpp:<br><font face="monospace">int unused(int a);<br>int probably_inlined(int a);<br>int main(int argc, const char *argv[]) {<br>  return probably_inlined(argc);<br>}</font><br><br>tu2.cpp:<br><font face="monospace">int unused(int a) {<br>  return a + 1;<br>}<br>int probably_inlined(int a) {<br>  return a + 2;<br>}</font><br></div><div><br>I produced two object files with bitcode:<br><font face="monospace">clang -c -flto tu1.cpp -o tu1.o</font><br></div><div><br></div><div>And I run LTO and attempted to dump the IR before each pass:<br><font face="monospace">clang -O2 -Wl,-mllvm -Wl,-print-before-all tu1.o tu2.o -o optimized</font><br></div><div><br>In my dumps I saw the function <font face="monospace">unused</font> removed even before the first pass. Where did that happen? I tried to invoke <font face="monospace">llvm-link</font> manually and that did not remove <font face="monospace">unused</font>.</div><div><br></div><div>I also tried to dump optimization remarks and was no trace of a function being removed (I only saw a function being inlined).<br><br>Thanks in advance,<br>Gabor</div></div>
</div></blockquote><blockquote type="cite"><div dir="ltr"><span>_______________________________________________</span><br><span>LLVM Developers mailing list</span><br><span><a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a></span><br><span><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></span><br></div></blockquote></div></body></html>