[llvm-dev] Preventing LLVM from optimizing out a variable

Kevin Modzelewski via llvm-dev llvm-dev at lists.llvm.org
Mon Jul 11 15:31:04 PDT 2016


We've used this in the past:

#define KEEP_ALIVE(t) asm volatile("" : : "X"(t))

On Mon, Jul 11, 2016 at 1:45 PM, David Jones via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> I have code along the lines of:
>
> void foo(struct A *a) {
>     struct B *b = a->m_B;
>     /* do a whole bunch of things with b */
> }
>
> When LLVM generates code for this, it sees that after initializing b, a is
> no longer used, so it is optimized out.
>
> 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.
>
> Is there any way of marking a Value* as "precious", not to be optimized
> out?
>
> 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:
>
> define void @foo(%A*) {
>     %2 = alloca %A*
>     store %0, %2
>     call void @llvm.dbg.declare(metadata %A* %2, metadata !101, metadata
> !100), !dbg ...
>
> !100 = !DIExpression()
> !101 = !DILocalVariable(...)
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160711/e92d1f14/attachment.html>


More information about the llvm-dev mailing list