How to get variable definition line number etc. us

2019-08-18 03:41发布

问题:

As far as I know, when I need to get the line number of a local variable I had to look for the invocation of the llvm.dbg.declare intrinsics and get the dbg metadata(since AllocaInst itself does not contain any dbg info). However there seems no guarantee that this CallInst is the next instruction of the the AllocaInst, and I have to traverse the instruction in a specified function, which is inefficient. So I'm wondering whether there is a method for AllocaInst to get the llvm.dbg.declare instruction directly.

For example,in a src called foo.c:

int foo(){
     int a;
}

and the corresponding llvm ir:

define i32 @foo() nounwind {
entry:
  %retval = alloca i32
  %a = alloca i32
  %"alloca point" = bitcast i32 0 to i32
  call void @llvm.dbg.declare(metadata !{i32* %a}, metadata !7), !dbg !9
  br label %return, !dbg !10

return:                                           ; preds = %entry
  %retval1 = load i32* %retval, !dbg !10                                                                                                     
  ret i32 %retval1, !dbg !10
}
......
!9 = metadata !{i32 3, i32 0, metadata !8, null}

If I need to know the line number of int a; defined in foo.c I have to traverse the ir and get !dbg !9 from call void @llvm.dbg.declare(metadata !{i32* %a}, metadata !7), !dbg !9.

BTW, there seems no difficulty when dealing with global variable since llvm.dbg.gv contains the very info.

回答1:

I finally figured out that we could use the static method DbgDeclareInst* findDbgDeclare(const Value *V) in DbgInfoPrinter.cpp,which can be seen here

It's also not very difficult to hack this piece of code^_^



回答2:

The version of my llvm is 3.3. There are not the function DbgDeclareInst* findDbgDeclare(const Value *V). What's your llvm's version?