<div dir="ltr">How about making your FAKE_USE instruction take a lot of arguments and return the first one? Then you could have the return instruction depend on it.</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 15, 2016 at 10:28 AM, Pieb, Wolfgang via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">FyI, we (at Sony) have been experimenting with a way to keep local variables and parameters alive past their point of last use by generating an artificial (fake)use
 later in the instruction chain. Concretely, the idea is that the FE – under control of a command line option – generates calls to a fake.use intrinsic for all locals and parameters at or near the end of their respective lexical scopes. LLVM turns the intrinsic
 calls eventually into FAKE_USE machine instructions, which generate no code, but otherwise force the compiler to treat their operands as if they were live, keeping them around in registers or spilling them to the stack if necessary. Appropriate debug information
 is generated automatically.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">So far we found that this can definitely improve debugging in the situations you describe at the expense of a slight performance penalty. We have floated this
 idea  - restricted to the ‘this’ pointer - previously (<a href="http://lists.llvm.org/pipermail/llvm-dev/2015-September/090663.html" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2015-September/090663.html</a>), but the general community response was not favorable.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">We still believe, however, that this feature could improve debugging of optimized code, which is quite important to our user community (game developers). We
 are currently in the process of gathering quantifiable data on the impact on location information and performance.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">-- Wolfgang<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> llvm-dev [mailto:<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>]
<b>On Behalf Of </b>David Jones via llvm-dev<br>
<b>Sent:</b> Monday, July 11, 2016 1:45 PM<br>
<b>To:</b> llvm-dev<br>
<b>Subject:</b> [llvm-dev] Preventing LLVM from optimizing out a variable<u></u><u></u></span></p>
</div>
</div><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">I have code along the lines of:<u></u><u></u></p>
</div>
<p class="MsoNormal">void foo(struct A *a) {<u></u><u></u></p>
</div>
<p class="MsoNormal">    struct B *b = a->m_B;<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">    /* do a whole bunch of things with b */<br>
}<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">When LLVM generates code for this, it sees that after initializing b, a is no longer used, so it is optimized out.<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Unfortunately, for debug, I'd like the value for a to be kept around, if only so that I can determine the value of b and go from there.  I don't care if a is spilled to the stack and never referenced thereafter,
 as long as it is available for debug.<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Is there any way of marking a Value* as "precious", not to be optimized out?<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Simply storing a to a local variable (result of alloca in the first basic block) and calling llvm.dbg.declare does not appear to be sufficient:<u></u><u></u></p>
</div>
<p class="MsoNormal">define void @foo(%A*) {<u></u><u></u></p>
</div>
<p class="MsoNormal">    %2 = alloca %A*<u></u><u></u></p>
</div>
<p class="MsoNormal">    store %0, %2<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">    call void @llvm.dbg.declare(metadata %A* %2, metadata !101, metadata !100), !dbg ...<u></u><u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal">!100 = !DIExpression()<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">!101 = !DILocalVariable(...)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
</div>
</div>
</div></div></div>
</div>
</div>

<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>