Hello, I'm wondering if it's possible to dereference a PointerType. I have an AllocaInst and although I can find the number of elements allocated, (using Instruction::getOperand(0)), I can't find a way to get the size of each element. What I'd like to do is: AllocaInst *alloca; PointerType *ptr_type = dynamic_cast<PointerType*>(alloca); assert(ptr_type); Type *allocated_type = ptr_type->dereference(); // this is the operation that doesn't seem to exist. size_t size; if (isa<PointerType>(allocated_type)) { size = sizeof(void*) * 8; } else { size = allocated_type->getPrimitiveSizeInBits(); } // size now equals the size (in bits) of the type allocated If it doesn't exist, how do you manage without it? Is there another way to write the above code? Thanks, Daniel -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20091020/a35e4e62/attachment.html>
2009/10/20 Daniel Waterworth <da.waterworth at googlemail.com>> Hello, > > I'm wondering if it's possible to dereference a PointerType. I have an > AllocaInst and although I can find the number of elements allocated, (using > Instruction::getOperand(0)), I can't find a way to get the size of each > element. What I'd like to do is: > > AllocaInst *alloca; > > PointerType *ptr_type = dynamic_cast<PointerType*>(alloca); > assert(ptr_type); > Type *allocated_type = ptr_type->dereference(); // this is the operation > that doesn't seem to exist. > size_t size; > if (isa<PointerType>(allocated_type)) { > size = sizeof(void*) * 8; > } else { > size = allocated_type->getPrimitiveSizeInBits(); > } > // size now equals the size (in bits) of the type allocated > > If it doesn't exist, how do you manage without it? Is there another way to > write the above code? > > Thanks, > > Daniel >Spot the deliberate mistake [ PointerType *ptr_type = dynamic_cast<PointerType*>(alloca); should be, const PointerType *ptr_type = dynamic_cast<const PointerType*>(alloca->getType()); and, Type *allocated_type = ptr_type->dereference(); should probably be, const Type *allocated_type = ptr_type->dereference(); Daniel -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20091020/5ea65e1d/attachment.html>
Daniel Waterworth <da.waterworth at googlemail.com> writes: [snip] Use the getElementType method of PointerType.>> size_t size; >> if (isa<PointerType>(allocated_type)) { >> size = sizeof(void*) * 8; >> } else { >> size = allocated_type->getPrimitiveSizeInBits(); >> } >> // size now equals the size (in bits) of the type allocatedThis looks suspicious to me. Maybe you intented if (isa<PointerType>(allocated_type)) { size = sizeof(void*); } else { size = allocated_type->getPrimitiveSizeInBits() * 8; } but take a look at getPrimitiveSizeInBits' documentation to make sure that it is really what you want: http://llvm.org/doxygen/classllvm_1_1Type.html -- Óscar