Understanding AllocaInst

Hello,

Hopefully this is the correct place for this sort of question. I have been trying to do some simple instrumentation of LLVM IR, and I have been confused by the return type of alloca. When passing an AllocaInst as a parameter to a function call, the parameter type is i32**, which is not what I would expect. Really I would like the address of the allocated memory. What am I missing? And is it possible to represent the result of alloca as an integer? Thanks for any help.

Cheers,

Avery

The type of an alloca is a pointer to the type of the allocated object. For instance:

This C++ source code:

int i;
float f;
f1(i, f);

compiles to this LLVM IR:

%i = alloca i32, align 4
%f = alloca float, align 4
%0 = load i32, i32* %i, align 4
%1 = load float, float* %f, align 4
call void @_Z2f1if(i32 %0, float %1)

So you can see the type of an alloca of i32 is i32* and the type of an alloca of float is float*.

You can convert pointers to integers with a ptrtoint instruction. eg: %0 = ptrtoint i32* %i to i64

David Blaikie via llvm-dev <llvm-dev@lists.llvm.org> writes:

The type of an alloca is a pointer to the type of the allocated object. For
instance:

This C++ source code:

  int i;
  float f;
  f1(i, f);

compiles to this LLVM IR:

%i = alloca i32, align 4
  %f = alloca float, align 4
  %0 = load i32, i32* %i, align 4
  %1 = load float, float* %f, align 4
  call void @_Z2f1if(i32 %0, float %1)

So you can see the type of an alloca of i32 is i32* and the type of an
alloca of float is float*.

And the type of an alloca of i32* is i32** which is where that comes
from. So Avery is doing an alloca of an i32*, a pointer rather than an
integer.

HTH,

                 -David