简体   繁体   中英

How to get all the line number of a variable from source code given LLVM IR?

I am totally a newbie in LLVM. I wanted to know how we can get all the line numbers for a particular variable from source code given LLVM IR?

For example (showing snippet of LLVM IR)

store i32 0, i32* %i, align 4, !dbg !12
!12 = !DILocation(line: 2, column: 6, scope: !7)
%4 = load i32*, i32** %ip, align 8, !dbg !30
!30 = !DILocation(line: 7, column: 4, scope: !25)

I believe, from inspecting LLVM IR, getting the line number details for any variable has something to do with accessing !dbg at the end of instruction. But I don't know how to access those information.

Another doubt is if a pointer is used to store address of a variable, how do we know for which variable it is storing address of?

I believe, from inspecting LLVM IR, getting the line number details for any variable has something to do with accessing.dbg at the end of instruction. But I don't know how to access this information.

I believe you are correct in that assumption. As far as I can tell, all the information is there for you:

The first instruction ends with !dbg !12 ( store i32 0, i32* %i, align 4, !dbg !12 ).

Then you should find a line starting with !12 . Hint, the debug info is usually at the bottom of the LLVM IR for the module.

In your case it is: :12 =,DILocation(line: 2, column: 6, scope: !7) <-- The interpretation here is: The instruction marked with !12 comes from line 2 column 6 in the source file that generated this LLVM IR. The name of the source file should also be available (usually at the top of the LLVM IR).

Another doubt is if a pointer is used to store address of a variable, how do we know for which variable it is storing address of?

You need to deduce that information yourself eg through data flow analysis. It's conceptually fairly simple though, since LLVM IR is in SSA form already.

Everything in LLVM is a Value, and some of the Values keep track of their users. 42 (a constant int) does not, but the Values you are interested in do keep track of their users. I have some code that processes the phi nodes that use a partciular Value; here are three lines that find those phi nodes:

for(auto u : someValue->users()) {
  PHINode * phi = dyn_cast<PHINode>(u);
  if(phi)
    …

Note that this only works in-module. If you have a global value (such as many functions) then uses from outside the module aren't tracked (such as most function calls).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM