[LLVMdev] Source Code Location of an Instruction

Devang Patel devang.patel at gmail.com
Wed Feb 17 09:32:12 PST 2010


On Wed, Feb 17, 2010 at 3:07 AM, Renato Golin <rengolin at systemcall.org> wrote:
> On 16 February 2010 20:49, Trevor Harmon <Trevor.W.Harmon at nasa.gov> wrote:
>> We were discussing that a few days ago:
>>
>> http://lists.cs.uiuc.edu/pipermail/llvmdev/2010-February/029245.html
>
> Hi Trevor,
>
> On a similar question, I'd want to see what line is being compiled to
> which instructions.

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".

-
Devang



More information about the llvm-dev mailing list