<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 6/9/20 1:01 PM, Shishir V Jessu via
      llvm-dev wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAJ4f8rrwG62nEgX=poTw+YCZVgNSXMxS71Dxh09w3m7z2vCrjA@mail.gmail.com">
      <pre class="moz-quote-pre" wrap="">Hello,

I am adding function calls to an LLVM link-time optimization (LTO) pass,
using the IRBuilder::CreateCall method. I want these calls to remain in the
final x86 binary at any optimization level, but on levels -O2 and -O3, some
of these calls are being optimized out.

So far, I've tried adding each function in the program (excluding LLVM
intrinsics) to the llvm.used set, and I've also set noinline and optnone
attributes on each function in the program. This has allowed me to
retain *most,
*but not all, of the calls I've added with IRBuilder::CreateCall.

Furthermore, I have confirmed that all the calls I've created are present
in the LLVM IR that results immediately after my pass. Thus, I know some
future LTO pass is optimizing out some of these calls.

How can I ensure that none of the calls I add are optimized out? Thanks for
your help!</pre>
    </blockquote>
    <p>The answer, in short, is:</p>
    <p> Don't run any optimizations/normalizations or, better, only
      place calls where they are not statically dead.</p>
    <p><br>
    </p>
    <p>Basically, if you want optimization, O2/O3, you get them. We even
      have "reaosnable"</p>
    <p>way to tell optimizations to ignore some part of the code, e.g.,
      optnone, but even then,</p>
    <p>some trivial things will be removed (think `if (false) foo();`).
      That said, you can</p>
    <p>make the code "conditionally life" to avoid this. There are many
      ways, one would be</p>
    <p>something like:<br>
    </p>
    <p><br>
    </p>
    <p>Before:<br>
    </p>
    <p>```</p>
    <p>int foo(int a) {</p>
    <p>  if (a > 3)</p>
    <p>    return a-1;<br>
    </p>
    <p>  return 0;<br>
    </p>
    <p>}</p>
    <p>```<br>
    </p>
    <p><br>
    </p>
    <p>After:</p>
    <p>```</p>
    <p>int return_zero() __attribute__((pure)) // linked in as object
      file</p>
    <p><br>
    </p>
    <p>int foo(int a) {</p>
    <p>   if (return_zero() != 0)<br>
    </p>
    <p>     goto calls;<br>
    </p>
    <p><br>
    </p>
    <p>   if (a > 3) {</p>
    <p>call1:</p>
    <p>      my_new_call(1, a);</p>
    <p>      return a-1;<br>
    </p>
    <p>   }</p>
    <p><br>
    </p>
    <p>call2:</p>
    <p>   my_new_call(2, a);<br>
    </p>
    <p>   return 0;</p>
    <p><br>
    </p>
    <p>calls:</p>
    <p>   switch (return_zero()) {</p>
    <p>   case 1: goto call1;</p>
    <p>   case 2: goto call2;</p>
    <p>   default:</p>
    <p>           goto call2;<br>
    </p>
    <p>   };<br>
    </p>
    <p>}</p>
    <p><br>
    </p>
    <p>Now even if you inline the above for a call site like</p>
    <p>`foo(0)`, the `my_new_call(1, a)` call site will still</p>
    <p>be considered life, thus not be removed.<br>
    </p>
    <p><br>
    </p>
    <p>Hope this helps.<br>
    </p>
    <p><br>
    </p>
    <blockquote type="cite"
cite="mid:CAJ4f8rrwG62nEgX=poTw+YCZVgNSXMxS71Dxh09w3m7z2vCrjA@mail.gmail.com">
      <pre class="moz-quote-pre" wrap="">

Best,
Shishir Jessu

</pre>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
    </blockquote>
  </body>
</html>