1

I am trying to generate LLVM bitcode and disassembled (.ll) code from a c source code. I want the instructions in the bitcode to have similar variable names as the source code.

Suppose I have a source code (sample.c):

  int test(int a){
    return a++;
  }

The sample.ll contains :

; Function Attrs: noinline nounwind uwtable
define i32 @test(i32) #0 {
  %2 = alloca i32, align 4
  store i32 %0, i32* %2, align 4
  %3 = load i32, i32* %2, align 4
  %4 = add nsw i32 %3, 1
  store i32 %4, i32* %2, align 4
  ret i32 %3
}

Here, %0 resembles variable a in the source code.

How can I generate a sample.ll like this?

; Function Attrs: noinline nounwind
define i32 @test(i32 %a) #0 {
entry:
  %a.addr = alloca i32, align 4
  store i32 %a, i32* %a.addr, align 4
  %0 = load i32, i32* %a.addr, align 4
  %inc = add nsw i32 %0, 1
  store i32 %inc, i32* %a.addr, align 4
  ret i32 %0
}

Where %a resembles variable a in the source code. NB: The clang version I am using is 6.0.0-1ubuntu2~16.04.1

I am using the command : clang -Xclang -disable-O0-optnone -O0 -emit-llvm -c sample.c -o sample.bc and then llvm-dis sample.bc

Jaeger
  • 159
  • 4
  • 14

1 Answers1

0

The thing you want to name isn't an Instruction, it's an Argument. The Argument constructor takes a Name argument, which is probably the intended way to set that. I've no idea why clang doesn't do that in your case. You can also call setName() later.

Making instructions have names follows the same pattern, provided that they don't have a void type. In your example, alloca and inc both have names. Making the load have a name would usually be done by passing a NameStr argument. setName() works on Instructions too (both Instruction and Argument inherit Value).

arnt
  • 8,949
  • 5
  • 24
  • 32