Hi, everyone. I just begin to learn LLVM IR, and have some problems about temporary variables in LLVM IR. When I compile the C code below with clang -emit-llvm -S main.c -o main.ll -O0
// main.c
int foo(int first, int second) {
return first + second;
}
int a = 5;
int main() {
int b = 4;
return foo(a, b);
}
the generated main.ll looks like below
; ModuleID = 'main.cc'
source_filename = "main.cc"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@a = dso_local global i32 5, align 4
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @_Z3fooii(i32 %0, i32 %1) #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 norecurse nounwind optnone uwtable
define dso_local i32 @main() #1 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
store i32 4, i32* %2, align 4
%3 = load i32, i32* @a, align 4
%4 = load i32, i32* %2, align 4
%5 = call i32 @_Z3fooii(i32 %3, i32 %4)
ret i32 %5
}
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "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"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "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"="+cx8,+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 10.0.0-4ubuntu1 "}
I’ve noticed that foo function skips the %2 register and main function skips %0 register. But as what LLVM Language Reference Manual says
Unnamed temporaries are numbered sequentially (using a per-function incrementing counter, starting with 0)
If I adjust the unnamed temporaries in foo and execute main.ll with lli, it returns an error
lli: main.ll:5:3: error: instruction expected to be numbered '%3'
%2 = alloca i32, align 4
^
Does %2 in foo register has an internal usage? Hope someone can give some idea