获取 LLVM 值的原始变量名 [英] Getting the original variable name for an LLVM Value

查看:32
本文介绍了获取 LLVM 值的原始变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.也提供了一篇具有一定背景的旧博文.

<小时>

$ 猫 >zclong fact(long arg, long farg, long bart){long foo = farg + bart;返回 foo * arg;}$ clang -emit-llvm -O3 -g -c z.c$ llvm-dis z.bc -o -

产生这个:

定义 i64 @fact(i64 %arg, i64 %farg, i64 %bart) #0 {入口:尾调用无效@llvm.dbg.value(元数据!{i64 %arg},i64 0,元数据!10),!dbg!17尾调用无效@llvm.dbg.value(元数据!{i64 %farg},i64 0,元数据!11),!dbg!17尾调用无效@llvm.dbg.value(元数据!{i64 %bart},i64 0,元数据!12),!dbg!17%add = 添加 nsw i64 %bart, %farg, !dbg !18尾调用无效@llvm.dbg.value(元数据!{i64 %add},i64 0,元数据!13),!dbg!18%mul = mul nsw i64 %add, %arg, !dbg !19ret i64 %mul, !dbg !19}

使用 -O0 而不是 -O3,您不会看到 llvm.dbg.value,但会看到 llvm.dbg.declare.

The operands for an llvm::User (e.g. instruction) are llvm::Values.

After the mem2reg pass, variables are in SSA form, and their names as corresponding to the original source code are lost. Value::getName() is only set for some things; for most variables, which are intermediaries, its not set.

The instnamer pass can be run to give all the variables names like tmp1 and tmp2, but this doesn't capture where they originally come from. Here's some LLVM IR beside the original C code:

I am building a simple html page to visualise and debug some optimisations I am working on, and I want to show the SSA variables as namever notation, rather than just temporary instnamer names. Its just to aid my readability.

I am getting my LLVM IR from clang with a commandline such as:

 clang -g3 -O1 -emit-llvm -o test.bc -c test.c

There are calls to llvm.dbg.declare and llvm.dbg.value in the IR; how do you turn into the original sourcecode names and SSA version numbers?

So how can I determine the original variable (or named constant name) from an llvm::Value? Debuggers must be able to do this, so how can I?

解决方案

This is part of the debug information that's attached to LLVM IR in the form of metadata. Documentation is here. An old blog post with some background is also available.


$ cat  > z.c
long fact(long arg, long farg, long bart)
{
    long foo = farg + bart;
    return foo * arg;
}

$ clang -emit-llvm -O3 -g -c z.c
$ llvm-dis z.bc -o -

Produces this:

define i64 @fact(i64 %arg, i64 %farg, i64 %bart) #0 {
entry:
  tail call void @llvm.dbg.value(metadata !{i64 %arg}, i64 0, metadata !10), !dbg !17
  tail call void @llvm.dbg.value(metadata !{i64 %farg}, i64 0, metadata !11), !dbg !17
  tail call void @llvm.dbg.value(metadata !{i64 %bart}, i64 0, metadata !12), !dbg !17
  %add = add nsw i64 %bart, %farg, !dbg !18
  tail call void @llvm.dbg.value(metadata !{i64 %add}, i64 0, metadata !13), !dbg !18
  %mul = mul nsw i64 %add, %arg, !dbg !19
  ret i64 %mul, !dbg !19
}

With -O0 instead of -O3, you won't see llvm.dbg.value, but you will see llvm.dbg.declare.

这篇关于获取 LLVM 值的原始变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆