Trying to access the user defined variable name

Dear all,

I have the following source code:

long f(long x, long y)
{
  long u;
  
   u = x+y;
   return u;
};

After clang (with debug option set) and llvm opt using mem2reg, I get
the following .ll file

Dear all,

I have the following source code:

long f(long x, long y)
{
long u;

u = x+y;
return u;
};

After clang (with debug option set) and llvm opt using mem2reg, I get
the following .ll file
***************************************************************************
; ModuleID = '<stdin>'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
target triple = "i686-pc-linux-gnu"

define i32 @f(i32 %x, i32 %y) nounwind {
entry:
call void @llvm.dbg.value(metadata !{i32 %x}, i64 0, metadata !0)
call void @llvm.dbg.value(metadata !{i32 %y}, i64 0, metadata !7)
%add = add nsw i32 %x, %y, !dbg !8 ; <i32> [#uses=1]
call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10), !dbg !8
ret i32 %add, !dbg !11
}

declare void @llvm.dbg.declare(metadata, metadata) nounwind readnone

declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone

!0 = metadata !{i32 524545, metadata !1, metadata !"x", metadata !2,
i32 1, metadata !6} ; [ DW_TAG_arg_variable ]
!1 = metadata !{i32 524334, i32 0, metadata !2, metadata !"f",
metadata !"f", metadata !"f", metadata !2, i32 2, metadata !4, i1
false, i1 true, i32 0, i32 0, null, i1 false, i1 false} ; [
DW_TAG_subprogram ]
!2 = metadata !{i32 524329, metadata !"testadd.c", metadata
!"/home/asudarsanam/perforce/llvmtop/setup", metadata !3} ; [
DW_TAG_file_type ]
!3 = metadata !{i32 524305, i32 0, i32 12, metadata !"testadd.c",
metadata !".", metadata !"clang 2.0", i1 true, i1 false, metadata !"",
i32 0} ; [ DW_TAG_compile_unit ]
!4 = metadata !{i32 524309, metadata !2, metadata !"", metadata !2,
i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !5, i32 0, null} ; [
DW_TAG_subroutine_type ]
!5 = metadata !{metadata !6}
!6 = metadata !{i32 524324, metadata !2, metadata !"long", metadata
!2, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ]
!7 = metadata !{i32 524545, metadata !1, metadata !"y", metadata !2,
i32 1, metadata !6} ; [ DW_TAG_arg_variable ]
!8 = metadata !{i32 5, i32 4, metadata !9, null}
!9 = metadata !{i32 524299, metadata !1, i32 2, i32 1} ; [
DW_TAG_lexical_block ] !10 = metadata !{i32 524544, metadata !9,
metadata !"u", metadata !2, i32 3, metadata !6} ; [
DW_TAG_auto_variable ]
!11 = metadata !{i32 7, i32 1, metadata !9, null}
******************************************************************

Llvm has replaced the name of the local variable (u) by a temporary
name (%add). However, the debug information contains the information
about the original variable name. **** !10 = metadata !{i32 524544,
metadata !9, metadata !"u", metadata !2, i32 3, metadata !6} ; [
DW_TAG_auto_variable ] ******

Variable name 'u' is intentionally preserved in DW_TAG_auto_variable
because user debugging this code does not know anything about temp.
names used by compiler.

I have been trying for a while and have been unsuccessful in accessing
this information (mapping between the temporary name (%add) and the
actual name (u)).

That's what llvm.dbg.value is doing.

call void @llvm.dbg.value(metadata !{i32 %add}, i64 0, metadata !10), !dbg !8

First parameter is the compiler generated temp. and 3rd parameter
provides info about the variable, including its name 'u'.

Hi Devang,

Thanks for your reply. You mentioned
" First parameter is the compiler generated temp. and 3rd parameter
provides info about the variable, including its name 'u'."

I did manage to get this far. But, I am finding it difficult to access
this info using LLVM APIs. The third parameter is of type "metadata".
So I was able to access it by casting it to MDNode. After that, I am
assuming that it gives me a handle to the metadata represented as
"!10"

!10 = metadata !{i32 524544, metadata !9, metadata !"u", metadata !2,
i32 3, metadata !6} ; [DW_TAG_auto_variable ]

From here, I am trying invain to access the variable name.

Is there something wrong in my approach? I feel that I have not
understood the method to access metadata in llvm.

Thanks
Regards
Arvind

Use DIVariable from DebugInfo.h to access fields of MDNode that is
holding variable's info.

Hi Devang,

I did try using DIVariable for accessing the MDNode, to no effect.

One point to note is, I try to access the debug information after
running the optimization pass "mem2reg". It seems "mem2reg" removes
all debug information. When I run -print-dbginfo pass before running
the mem2reg pass, I get a lot of info. But when I run the
-print-dbginfo pass after the mem2reg pass, I get nothing.

I look at the .ll file after mem2reg pass, it still has all the debug
information. Somehow, it is not being identified by the llvm APIs. I
am planning to write a program to parse the .ll file and access this
info myself. I was hoping that this program is already available.

Please let me know if I am missing anything or am incorrect about something.

Thanks a lot for your replies
Regards
Arvind