[cfe-users] Getting variable names in LLVM IR

Subhendu Malakar via cfe-users cfe-users at lists.llvm.org
Mon Feb 13 23:10:46 PST 2017


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"

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.



More information about the cfe-users mailing list