Retrieving Underlying Type from AllocaInst

Is there a recommended way to retrieve the original type from an AllocaInst object?

For example, I am creating alloca instructions using the IRBuilder interface like:

alloca = builder.CreateAlloca( Type::getDoubleTy( context ), 0, variableName.c_str() );

and I place the alloca into a symbol table.

Later when I am generating instructions for an assignment operation, I want to check the type of the variable allocated with the CreateAlloca instruction so I can see if I need to do type conversion before the store instruction, However, I cannot simply use:

alloca->getType()->isDoubleTy() etc.

because this doesn’t return true in the above case. The ID of the Ty member of the VTy member of the alloca is PointerTyID instead of DoubleTyID as the construction for AllocaInst::AllocaInst seems to create a PointerType pointing to the original type.

I was hoping for a member function of the alloca that would return the original type that was passed to CreateAlloca. I can test for a PointerTyID and then perform a cast to PointerType and then call the getElementType() function on the PointerType but that seems a bit messy.

Thanks in advance,

Curtis

Curtis Faith wrote:

Is there a recommended way to retrieve the original type from an
AllocaInst object?

For example, I am creating alloca instructions using the IRBuilder
interface like:

alloca = builder.CreateAlloca( Type::getDoubleTy( context ), 0,
variableName.c_str() );

and I place the alloca into a symbol table.

Later when I am generating instructions for an assignment operation, I
want to check the type of the variable allocated with the CreateAlloca
instruction so I can see if I need to do type conversion before the
store instruction, However, I cannot simply use:

alloca->getType()->isDoubleTy() etc.

because this doesn't return true in the above case. The ID of the Ty
member of the VTy member of the alloca is PointerTyID instead of
DoubleTyID as the construction for AllocaInst::AllocaInst seems to
create a PointerType pointing to the original type.

I was hoping for a member function of the alloca that would return the
original type that was passed to CreateAlloca. I can test for a
PointerTyID and then perform a cast to PointerType and then call the
getElementType() function on the PointerType but that seems a bit messy.

There's alloca->getAllocatedType() which merely calls getType()->getElementType() for you.

http://llvm.org/doxygen/classllvm_1_1AllocaInst.html .

Nick

Thanks Nick,

Unfortunately, that is indeed what I asked for but not what I really am looking for.

My naive approach is to store symbol table entries as Value* objects so I can allocate global variables and alloca variables and place them into the symbol table and the rest of the code didn’t need to know which kind they were, in general. Loads and Stores of these types (as well as other instructions) seem to work fine as long as the types being pointed to match the type of the value being stored.

So what I was really looking for, I suppose, was a Value class virtual member function something like getLoadStoreType or perhaps getReferencedType which returned the type as it would be used in a load or store. It appears that both load and store do handle the pointer dereferencing automatically for both types, i.e. AllocaInst and GlobalVariable both dereference the pointers automatically.

I can cast the value I get from the symbol table to a AllocaInst, of course, in the case where it is one, but this requires a test for one and is just as messy as first getting the type and then testing it for being a PointerType and less general since a GlobalVariable also store its type as a PointerType which point to the actual type. They, unfortunately, do not share a common superclass other than Value itself.

  • Curtis

You should be able to use the second alternative that Nick proposed:
cast<PointerType*>(pointer_value->getType())->getElementType()

Reid

Reid,

Thanks, I ended up doing the cast this way as it works for both AllocaInst and GlobalVariable. Since a store expects a pointer it should work for anything that I put into my symbol table.

- Curtis