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

喜你入骨 提交于 2019-12-24 02:44:07

问题


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?


回答1:


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 e.g. through data flow analysis. It's conceptually fairly simple though, since LLVM IR is in SSA form already.




回答2:


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



来源:https://stackoverflow.com/questions/58096202/how-to-get-all-the-line-number-of-a-variable-from-source-code-given-llvm-ir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!