So I have this LLVM-IR code:
; ModuleID = 'example.-.input.c'
source_filename = "example.-.input.c"
define ptr @test() {
entry:
%0 = alloca ptr, align 8
%1 = alloca i32, align 4
store i32 2, ptr %1, align 4
store ptr %1, ptr %0, align 8
br label %ret
ret: ; preds = %entry
%2 = load ptr, ptr %0, align 8
ret ptr %2
}
define i32 @main() {
entry:
%0 = alloca i32, align 4
store i32 0, ptr %0, align 4
%1 = call ptr @test()
%2 = load i32, ptr %1, align 4
store i32 %2, ptr %0, align 4
br label %ret
ret: ; preds = %entry
%3 = load i32, ptr %0, align 4
ret i32 %3
}
test
returns a pointer to the local variable %1
, a integer with the value of 2.
main
calls test
and extracts the integer from the pointer returned by test
. But somehow main
returns the “expected” value of 2 (%2
of main
: pointed to %0
of test
). But why? Shouldn’t the local variable %0
be destroyed at the end of test
and the pointer returnd by test
then invalid?