I have a stack allocated value, created via an alloca
instruction, as a Value* v
.
I want to write the C++ llvm builder code that will assign the address of v
, which lives on the stack, to a new variable p
.
That is, for the equivalent in C, if I have a local variable v
, I want to get its address into p
, like:
int c = 10;
int* p = &c;
I put some code into GodBolt, and I found out this is apparently the equivalent LLVM IR - so I want to produce IR like this:
%2 = alloca i32, align 4
%3 = alloca i32*, align 8
store i32 %0, i32* %2, align 4
store i32* %2, i32** %3, align 8, !dbg !18
%4 = load i32*, i32** %3, align 8, !dbg !19
I should add that the above llvm
IR doesn't make sense to me. Why do we need the first store? It seems like a redundant copy of %0
to %2
to me. Why couldn't i take the address of $0
directly? And in the second store, it looks like we're casting the value into a pointer top... Is that just a trick of the light, and that instruction actually means "please copy the address of %2" even though it doesn't say that directly? Is this actually the right IR that I want to generate? Is there a better way to write this?
Thanks, apologies for my ignorance of LLVM IR.
Possibly related questions: