I try to learm llvm assembly language. Since I did not find any tutorial for it, my way to learn is writing simple C functions, and let clang reveal the corresponding llvm code with:
clang -S -emit-llvm simple.c
I am now trying to learn how to use pointers. So I tested the following C function:
int getVal(int* ptr) { return *ptr; }
which generated the following llvm:
define i32 @getVal(i32*) #0 {
%2 = alloca i32*, align 8
store i32* %0, i32** %2, align 8
%3 = load i32*, i32** %2, align 8
%4 = load i32, i32* %3, align 4
ret i32 %4
}
My questions on the llvm code:
- What is the %0, that the store operation is referring? Does this refer to the function argument? All other functions that I encountered start their variables with %1, not %0. What is the difference here?
- The next variable that I see being defined is %2, which means that %1 is skipped. And I have noticed that doing this (skipping) leads to compile errors. So how it comes that this code is valid?
- What is the actual logic of this code? Why is a store instruction and i32** types involved? Is there any simpler way to implement a "get value of" operation in llvm?
In LLVM function definition contains list of basicBlocks which optionally may start with a label. and if the explicit label is not provided then the implicit numbered labeled is provided from the same counter as used in unnamed temporaries.
This code is valid as %0 is implicitly used for argument and %1 is used to label the basicBlock, Kindly post the error messages if you are facing any issues with that.
I am not a clang-expert to say but optimization is llvm's responsibility. for simpler way you can use,
There is a very nice documentation available if you want to learn more about LLVM lang. LLVM Lang Ref
Also the points i have listed, you can find in Functions, Identifier sections.