print the memory address computed by getelementptr

Hi,

I want to print the memory locations computed by getelementptr. As I understood, getelementptr does not access the memory, but it contains the address it computes. I want to print these addresses at runtime (or process them). So, I try to build a function that takes as argument a pointer and prints its value. And to call this function, by sending the gep instruction as a parameter.

Surely, I obtain an error for a type mismatch. Can I declare the function as taking an int64 as parameter and then (somehow) sending the value computed by getelementptr?

declare void @myFunction ( i64 )

%tmp22 = getelementptr inbounds %struct.linked* %tmp21, i32 0, i32 1

%tmp = convert_tmp22_to_i64_to_get_the_value_of_the_pointer

call void @myFunction(i64 %tmp)

Or could you suggest a better method to get the value computed by getelementptr ?

Thanks,
Alexandra

Hi,

I want to print the memory locations computed by getelementptr. As I understood, getelementptr does not access the memory, but it contains the address it computes. I want to print these addresses at runtime (or process them). So, I try to build a function that takes as argument a pointer and prints its value. And to call this function, by sending the gep instruction as a parameter.

All you really need to do here is to cast the result of the getelementptr value to the type of the parameter of your function. In this case, you need to insert a ptrtoint instruction that takes the GEP and casts it to an i64.

That said, instead of casting the GEP to an i64, it would be better if your run-time function took a void *. In LLVM IR, a pointer to an i8 is a void *, and so you could insert a bitcast of the GEP to an i8 * instead of casting it to an i64. That way, you don’t have to worry about the pointer size if you start using your code on both 32-bit and 64-bit targets.

– John T.

This instruction is called ptrtoint. :slight_smile:

-Eli

Hi again,

Thank you, your suggestion worked well. I was looking for a pointer to void actually. But it seems that I do not get the memory address when passing the value of the GEP instruction.

For instance, when I print the address accessed by the second operand of the store instruction

store %struct.linked* %tmp23.reload8, %struct.linked** %curr.037.reg2mem
I obtain a memory address of the form

7fff29c28ac8

But when I try to get the address stored in the GEP

%tmp18 = getelementptr inbounds %struct.linked* %curr.025.reload, i64 0, i32 0

I obtain

2084a10

which is not the memory location I expect. Is this the computed memory location, or I did not access it correctly?

Thank you.

Alexandra

http://llvm.org/docs/GetElementPtr.html

-Eli