llvm: value of pointer operation

2019-08-20 05:10发布

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:

  1. 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?
  2. 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?
  3. 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?

标签: llvm
1条回答
啃猪蹄的小仙女
2楼-- · 2019-08-20 05:35
  1. 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?

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.

  1. 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?

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.

  1. 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?

I am not a clang-expert to say but optimization is llvm's responsibility. for simpler way you can use,

define i32 @getVal(i32*) #0 {
  %2 = load i32, i32* %0
  ret i32 %2
}

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.

查看更多
登录 后发表回答