[LLVMdev] Retrieving Underlying Type from AllocaInst

Nick Lewycky nicholas at mxc.ca
Thu May 27 22:06:29 PDT 2010


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



More information about the llvm-dev mailing list