Confuse on ptrtoint and load

Hi, all

When I read the document of IR on llvm.org,
I found two instructions, “ptrtoint” and load.

Load, I think, is : when after allocating some
bytes in memory and storing some data into it, we could
use load to get the data, like this:

%0 = alloca i32
store i32 5, i32* %0
%1 = load i32* %0
so, the type of %1 is i32, and the value is 5, right?

And “ptrtoint” is:we cast it from ptr to int? Just like
“&” in c++ ? When we are doing like this:

%0 = alloca i32
store i32 5, i32* %0
%1 = ptrtoint i32* %0 to i32
No doubt, the type of %1 is i32. But what is the value of %1?
Is it just like “load”, loading the value stored, or becoming a “var”
to store the address(not the real value stored in memory)?

If I want to get the value stored in memory, I should use load,
and if I want to get the addr of the ptr, use ptrtoint, am I right?

Could anyone tell me the difference between them clearly?

Best Regards

Weixue

Hi Weixue,

Hi, all

When I read the document of IR on llvm.org,
I found two instructions, “ptrtoint” and load.

Load, I think, is : when after allocating some
bytes in memory and storing some data into it, we could
use load to get the data, like this:

%0 = alloca i32
store i32 5, i32* %0
%1 = load i32* %0
so, the type of %1 is i32, and the value is 5, right?

And “ptrtoint” is:we cast it from ptr to int?

Just like
“&” in c++ ?

More or less yes. More specifically, we convert the pointer to the defined type.

When we are doing like this:

%0 = alloca i32
store i32 5, i32* %0
%1 = ptrtoint i32* %0 to i32
No doubt, the type of %1 is i32. But what is the value of %1?

%1 contains the address of the alloca converted to i32.
The exact value depends on the architecture. For instance on x86-64, addresses are 64-bits, thus here you will truncate the address to a 32-bits value.

Does it make sense?

Is it just like “load”, loading the value stored, or becoming a “var”
to store the address(not the real value stored in memory)?

If I want to get the value stored in memory, I should use load,
and if I want to get the addr of the ptr, use ptrtoint, am I right?

Basically, yes :).

Could anyone tell me the difference between them clearly?

Hope it helps.

-Quentin