[LLVMdev] Why variables get "optimized away" after the last use in unoptimized code?

David Chisnall David.Chisnall at cl.cam.ac.uk
Sun Feb 2 01:48:28 PST 2014


On 2 Feb 2014, at 09:11, Yuri <yuri at rawbw.com> wrote:

> I am seeing this with llvm generated code, and before also saw this with gcc generated code.
> In unoptimized code, debugger would say something like this when the variable isn't used any more, but is still in scope:
> (gdb) p this
> $11 = <optimized out>
> 
> Looking at the debug records, I don't see what is wrong, for example:
>  call void @llvm.dbg.declare(metadata !{%struct.Object* %1}, metadata !73), !dbg !74
> !73 = metadata !{i32 786689, metadata !63, metadata !"this", metadata !68, i32 33554949, metadata !12, i32 0, i32 0} ; [ DW_TAG_arg_variable ] [this] [line 517]
> !63 is DW_TAG_subprogram scope.
> 
> 'this' should exist through this subprogram, and never disappear. But it becomes "optimized away" after the last explicit use. Same is true with any other variable.
> Doesn't this mean that llvm generates wrong DWARF info?
> I don't think that debugger should ever say <optimized out> in unoptimized code.

In most calling conventions, this is a callee-save register.  After its last use, the register allocator may reuse that register. On x86 and ARM, the register that contains this is also (usually) the register used for return values, so it definitely will reuse it at some point before return in any method that returns a value.  The only way to avoid this would be to spill it to the stack and have the debug information point to the spill slot.  If there are any function / method calls within the method, this will be spilled and so there will be a copy of it on the stack.  It might be quite nice for the debug info to prefer to reference this one, but it's not possible for most other parameters.

David





More information about the llvm-dev mailing list