Vladimir Tsyshnatiy
Vladimir Tsyshnatiy

Reputation: 1069

How to get pointer to pointer in LLVM?

C++ code:

int main() { 
  int* k = new int(0);
  int* j = k; 
  return 0;
}

clang++ -S -emit-llvm :

define dso_local i32 @main() #0 { 
 %1 = alloca i32, align 4
 %2 = alloca i32*, align 8
 %3 = alloca i32*, align 8
 store i32 0, i32 *%1, align 4
 %4 = call i8* @_Znwm(i64 4) #2
 %5 = bitcast i8* %4 to i32*
 store i32 0, i32* %5, align 4
 store i32* %5, i32** %2, align 8
 %6 = load i32*, i32** %2, align 8
 store i32 *%6, i32** %3, align 8
 ret i32 0
}

The question is about store i32* %5, i32** %2, align 8

How is it possible to get i32** from %2(i32*) without generating additional LLVMValue like (pseudocode):
%starstar = alloca(i32**)
store(%2, %starstar)

I do not see any bitcasts or something like this either.
%2 was i32* and then it is i32** in the store instruction.
I would like to know how.
Any help is appreciated.

Upvotes: 0

Views: 754

Answers (1)

sepp2k
sepp2k

Reputation: 370082

%2 was i32* and then it is i32** in the store instruction.

%2 was never i32*. alloca T allocates memory for a value of type T and then returns a pointer to that memory. So the type of alloca T is T*, meaning the type of %2 in your code is i32** and the type of %1 is i32*.

Upvotes: 2

Related Questions