[cfe-users] Getting variable names in LLVM IR

Duncan P. N. Exon Smith via cfe-users cfe-users at lists.llvm.org
Tue Feb 14 16:14:49 PST 2017


> On 2017-Feb-13, at 23:10, Subhendu Malakar via cfe-users <cfe-users at lists.llvm.org> wrote:
> 
> Hi,
> I'm a newbie in LLVM environment.
> 
> I'm trying to generate the LLVM IR of a c file using clang. The command line argument I'm passing is as :
> "clang -O0 -S -emit-llvm test.c -c -o test.ll"

If you add "-###" to the end of the command-line you can see the raw -cc1 command for the compilation.  Depending on your version of clang, you should see "-discard-value-names" in there.  If you repeat the -cc1 command yourself, skipping that option, you'll get variable names.

If you're compiling clang yourself (it doesn't sound like you are, but just in case), this -discard-value-names option is *not* passed by default in asserts builds.

> 
> It is generating the LLVM IR properly but I'm not getting the variable names. e.g,
> 
> for the c file :
> #include <stdio.h>
> 
> int main()
> {
>    int x;
>    int y;
>    x = 2;
>    y =4;
>    int z = x*y;
> 
>    if(x==y)
>    {
>        z = x*y;
>        return z;
>    } else
>    {
>        z = x+y;
>    }
>    printf("z = %d", z);
>    return 0;
> }
> 
> 
> the corresponding llvm IR is :
> 
> ; Function Attrs: nounwind uwtable
> define i32 @main() #0 {
>  %1 = alloca i32, align 4
>  %2 = alloca i32, align 4
>  %3 = alloca i32, align 4
>  %4 = alloca i32, align 4
>  store i32 0, i32* %1, align 4
>  store i32 2, i32* %2, align 4
>  store i32 4, i32* %3, align 4
>  %5 = load i32, i32* %2, align 4
>  %6 = load i32, i32* %3, align 4
>  %7 = mul nsw i32 %5, %6
>  store i32 %7, i32* %4, align 4
>  %8 = load i32, i32* %2, align 4
>  %9 = load i32, i32* %3, align 4
>  %10 = icmp eq i32 %8, %9
>  br i1 %10, label %11, label %16
> 
> ; <label>:11:                                     ; preds = %0
>  %12 = load i32, i32* %2, align 4
>  %13 = load i32, i32* %3, align 4
>  %14 = mul nsw i32 %12, %13
>  store i32 %14, i32* %4, align 4
>  %15 = load i32, i32* %4, align 4
>  store i32 %15, i32* %1, align 4
>  br label %21
> 
> ; <label>:16:                                     ; preds = %0
>  %17 = load i32, i32* %2, align 4
>  %18 = load i32, i32* %3, align 4
>  %19 = add nsw i32 %17, %18
>  store i32 %19, i32* %4, align 4
>  br label %20
> 
> ; <label>:20:                                     ; preds = %16
>  store i32 0, i32* %1, align 4
>  br label %21
> 
> ; <label>:21:                                     ; preds = %20, %11
>  %22 = load i32, i32* %1, align 4
>  ret i32 %22
> }
> 
> 
> I wanted to get the variables in their original name i.e, x,y,z instead of %1,%2,%3, etc.
> Is it possible to do so?
> 
> Thanks.
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users




More information about the cfe-users mailing list