Source Code Location of an Instruction

Hi,

I would like to know how to locate an LLVM instruction in the source
code, i.e. to get the line number of its corresponding source code
statement. I remember in LLVM 2.4, DbgStopPointInst is designed to
help this locating, but it seems deprecated in the latest LLVM.

Thanks,
Jingyue

We were discussing that a few days ago:

http://lists.cs.uiuc.edu/pipermail/llvmdev/2010-February/029245.html

Trevor

Hi Trevor,

On a similar question, I'd want to see what line is being compiled to
which instructions. Is it possible to print the before the
instructions as comment?

For example:

array.c:

LLVM instruction or machine instruction ? If LLVM instruction then it
is possible. Add -g on the command line along with -O0. Using your
example, you'll get following with clang.

define i32 @main() nounwind ssp {
entry:
  %retval = alloca i32 ; <i32*> [#uses=3]
  store i32 0, i32* %retval
  store i32 5, i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i64 1), !dbg !6
  %tmp = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i64 1), !dbg !11 ; <i32> [#uses=1]
  %tmp1 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i64 3), !dbg !11 ; <i32> [#uses=1]
  %add = add nsw i32 %tmp, %tmp1, !dbg !11 ; <i32> [#uses=1]
  store i32 %add, i32* getelementptr inbounds ([5 x i32]* @array, i32
0, i64 4), !dbg !11
  %tmp2 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i64 4), !dbg !12 ; <i32> [#uses=1]
  %tmp3 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i64 1), !dbg !12 ; <i32> [#uses=1]
  %mul = mul i32 %tmp2, %tmp3, !dbg !12 ; <i32> [#uses=1]
  store i32 %mul, i32* getelementptr inbounds ([5 x i32]* @array, i32
0, i32 0), !dbg !12
  %tmp4 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i32 0), !dbg !13 ; <i32> [#uses=1]
  %add5 = add nsw i32 %tmp4, 1, !dbg !13 ; <i32> [#uses=1]
  store i32 %add5, i32* getelementptr inbounds ([5 x i32]* @array, i32
0, i64 2), !dbg !13
  %tmp6 = load i32* getelementptr inbounds ([5 x i32]* @array, i32 0,
i64 2), !dbg !14 ; <i32> [#uses=1]
  store i32 %tmp6, i32* %retval, !dbg !14
  %0 = load i32* %retval, !dbg !15 ; <i32> [#uses=1]
  ret i32 %0, !dbg !15
}

and list of metadata. Now to get the location of
    %add = add nsw i32 %tmp, %tmp1, !dbg !11 ; <i32> [#uses=1]
follow !11 from !dbg !11, which is
    !11 = metadata !{i32 4, i32 8, metadata !7, null}

This says the above LLVM instruction corresponds to source line no 4,
column no 8 inside a scope marked as !7. Which is ...

    !7 = metadata !{i32 458763, metadata !8, i32 2, i32 13} ; [
DW_TAG_lexical_block ]

... a lexical block starting at line no 2, column 13. This lexical
block is part of !8, which is

    !8 = metadata !{i32 458798, i32 0, metadata !1, metadata !"main",
metadata !"main", metadata !"main", metadata !1, i32 2, metadata !9,
i1 false, i1 true, i32 0, i32 0, null, i1 false} ; [ DW_TAG_subprogram
]

... a function (subprogram) named "main".

Yeah, I did that, but the output really doesn't help much. What I
wanted was exactly what I had in my email, source code lines
intermixed with their respective LLVM IR instructions. No machine
instructions, asm or binary.

In simple cases I can normally know where each line starts/ends, but
if I'm mixing too many similar commands, it can get fuzzy, especially
after a few hours looking at the same code with subtle changes... :wink:

cheers,
--renato

http://systemcall.org/

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm