Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

%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*.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading