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

David Jones via llvm-dev llvm-dev at lists.llvm.org
Mon Jul 11 13:45:07 PDT 2016

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(...)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160711/1e884d02/attachment.html>

More information about the llvm-dev mailing list