How to save the variable name when use clang to generate llvm ir?

橙三吉。 提交于 2019-12-13 00:07:29

问题


I generate ir by use 'clang -S -emit-llvm test.c'.

int main(int argc, char **argv)
{
    int* a=0;
    a=(int *)malloc(sizeof(int));
    printf("hello world\n");
    return 0;
}

and this is the ir:

define i32 @main(i32, i8**) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8**, align 8
  %6 = alloca i32*, align 8
  store i32 0, i32* %3, align 4
  store i32 %0, i32* %4, align 4
  store i8** %1, i8*** %5, align 8
  store i32* null, i32** %6, align 8
  %7 = call noalias i8* @malloc(i64 4) #3
  %8 = bitcast i8* %7 to i32*
  store i32* %8, i32** %6, align 8
  %9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0))
  ret i32 0
}

how can I make the variable name remain unchanged,like a still %a ,not %3?


回答1:


Actually dropping of variable names is a feature and needs to be activated with -discard-value-names. Clang in a release build does this by its own (a self compiled clang in debug mode not).

You can circumvent it with

clang <your-command-line> -###

Then copy the output and drop -discard-value-names.

Newer clang version (since 7) expose the flag to the normal command line:

clang -fno-discard-value-names <your-command-line>

Source




回答2:


There is not such way. The variable names in LLVM IR are merely for debugging only and also there is certainly no way to preserve them when the code is converted to full SSA form.

If you need to preserve source code information consider using debug info.



来源:https://stackoverflow.com/questions/50432967/how-to-save-the-variable-name-when-use-clang-to-generate-llvm-ir

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