Question About the LLVM IR unnamed values!

Hi.

I found that, in all the LLVM IR I get from the source C file. the unnamed values all created by the “load” instruction. eg:

; Function Attrs: nounwind uwtable
define i32 @add(i32 %x, i32 %y) #0 {
entry:
%x.addr = alloca i32, align 4
%y.addr = alloca i32, align 4
%c = alloca i32, align 4
store i32 %x, i32* %x.addr, align 4
store i32 %y, i32* %y.addr, align 4
%0 = load i32* %x.addr, align 4
%1 = load i32* %y.addr, align 4
%add = add nsw i32 %0, %1
store i32 %add, i32* %c, align 4
%2 = load i32* %c, align 4
ret i32 %2
}

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%a = alloca i32, align 4
%b = alloca i32, align 4
%d = alloca i32, align 4
%f = alloca i32, align 4
%g = alloca i32, align 4
%e = alloca i32, align 4
store i32 0, i32* %retval
store i32 8, i32* %a, align 4
store i32 9, i32* %b, align 4
%0 = load i32* %a, align 4
%1 = load i32* %b, align 4
%add = add nsw i32 %0, %1
store i32 %add, i32* %d, align 4
%2 = load i32* %a, align 4
%3 = load i32* %d, align 4
%add1 = add nsw i32 %2, %3
store i32 %add1, i32* %f, align 4
%4 = load i32* %f, align 4
%5 = load i32* %b, align 4
%add2 = add nsw i32 %4, %5
store i32 %add2, i32* %g, align 4
%6 = load i32* %d, align 4
%7 = load i32* %b, align 4
%call = call i32 @add(i32 %6, i32 %7)
store i32 %call, i32* %e, align 4
%call3 = call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0), i32* %a)
%call4 = call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str1, i32 0, i32 0), i32* %b)
%call5 = call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str2, i32 0, i32 0), i32* %e)
%call6 = call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0), i32* %d)
%call7 = call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0), i32* %g)
%call8 = call i32 (i8*, …)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0), i32* %f)
ret i32 0
}


the “add” instructions all have names like “%add”, “%add1”,etc.But the “load” instruction all use “%n”. ​

​is that means the unnamed instructions like “%0 %1” all created by the “load” instructions? ​is there any other type instructions can create unnamed values?

Thanks!

Hi Jin,

is that means the unnamed instructions like "%0 %1" all created by the
"load" instructions? is there any other type instructions can create unnamed
values?

The names are chosen in clang rather than LLVM. When it's creating an
instruction there's an optional "Name" field, which seems to mostly be
filled in. So to answer that question, you'd have to look at all
places in Clang that create instructions.

I strongly suspect there are other cases that don't give a name, but
the only ones I can think of at the moment are expansions of certain
compiler builtins (e.g. SIMD intrinsics, probably va_arg and similar).
Also, in release builds, the name is completely ignored and all
instructions are anonymous.

So you certainly shouldn't be relying on the fact that everything
except loads are named. But I can't see any particular reason we'd
turn down patches that added useful names in more cases.

Cheers.

Tim.

Hi Jin,

is that means the unnamed instructions like "%0 %1" all created by the
"load" instructions? is there any other type instructions can create unnamed
values?

The names are chosen in clang rather than LLVM. When it's creating an
instruction there's an optional "Name" field, which seems to mostly be
filled in. So to answer that question, you'd have to look at all
places in Clang that create instructions.

I strongly suspect there are other cases that don't give a name, but
the only ones I can think of at the moment are expansions of certain
compiler builtins (e.g. SIMD intrinsics, probably va_arg and similar).
Also, in release builds, the name is completely ignored and all
instructions are anonymous.

So you certainly shouldn't be relying on the fact that everything
except loads are named. But I can't see any particular reason we'd
turn down patches that added useful names in more cases.

Also, you can get names on the rest by running the instuction-namer pass (“opt -instnamer”) on the IR.