How to generate a .ll file with functions' parameter names

Hi all,
I’m trying to obtain a .ll with parameters name for every function. My simple c C is the following:

int sum(int a, int b) {
return a+b;
}

int main() {
sum(1,2);
}

I obtain the .ll with the following commands:

clang -emit-llvm sum.c -c
llvm-dis-7 sum.bc

The obtained .ll is:

cat sum.ll
; ModuleID = ‘sum.bc’
source_filename = “sum.c”
target datalayout = “e-m:e-i64:64-f80:128-n8:16:32:64-S128”
target triple = “x86_64-pc-linux-gnu”

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

; Function Attrs: noinline nounwind optnone uwtable
define i32 @main() #0 {
%1 = call i32 @sum(i32 1, i32 2)
ret i32 0
}

attributes #0 = { noinline nounwind optnone uwtable “correctly-rounded-divide-sqrt-fp-math”=“false” “disable-tail-calls”=“false” “less-precise-fpmad”=“false” “no-frame-pointer-elim”=“true” “no-frame-pointer-elim-non-leaf” “no-infs-fp-math”=“false” “no-jump-tables”=“false” “no-nans-fp-math”=“false” “no-signed-zeros-fp-math”=“false” “no-trapping-math”=“false” “stack-protector-buffer-size”=“8” “target-cpu”=“x86-64” “target-features”=“+fxsr,+mmx,+sse,+sse2,+x87” “unsafe-fp-math”=“false” “use-soft-float”=“false” }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !“wchar_size”, i32 4}
!1 = !{!“clang version 6.0.1-svn334776-1~exp1~20190309042703.125 (branches/release_60)”}

Could you tell me why I don’t see a and b parameters for the sum function?

Thanks
Alberto

Hi Alberto,

Variable names in LLVM IR are meaningless and only used for debugging.
However, if you want to preserve the variable name, you can use -fno-discard-value-names option.

Regards,
Hideto

2019年12月8日(日) 17:38 Alberto Barbaro via llvm-dev <llvm-dev@lists.llvm.org>:

Hi Hideto,
thanks for your answer. Unfortunately I’m still using llvm 6.0 and in llvm-dis I don’t see that option. I tried reading the full list of options but I didn’t see an alternative.

Do you have any suggestions?

Thanks
Alberto

The option is for Clang. Once you have the IR generated it's too late,
the names will already have been discarded.

Cheers.

Tim.

Hi Tim,
My bad, I have installed a newer version of clang and all worked.

Thanks

Hi Alberto,

Note that even with this, there isn't necessarily a 1:1 correspondence between LLVM IR parameters and clang parameters. For example, a struct passed by value in a C function may be one IR parameter per field, or may be packed into an integer large enough to store the whole thing. Similarly, return values may be indirect via sret pointers or they may be explicit return values but of a different type (for example, on i386 a return of struct of a pair of int- or pointer-sized values may be returned as a single i64 in LLVM IR).

It would help to understand what you are trying to do, because there may be a better way of achieving your goal.

David

Hi David,
Thanks for your email. I’m just trying to perform some basic analysis on the IR. For instance, is it possible to understand if the variable %10 depends on the value of the first parameter? I know that the first parameter can be referenced as %0 but a proper name would increase the readability. It is not so crucial for me atm.

Thanks
Alberto

Hi,

If you're not worried about the mapping to C parameters, then you should be able to start with the SSA values for the parameters and then walk the def-use chains to find everything that depends on it (modulo things that depend on it as a result of control flow or memory stores and loads).

David

Hi David,
I think it should be a feasible solution. I’ll make some experiment soon.

If tutorials for this topic are not available it would be great to have them.

Thanks a lot for your help
Alberto