Dereference PointerType?

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(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

2009/10/20 Daniel Waterworth <da.waterworth@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(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

Daniel Waterworth <da.waterworth@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 allocated

This 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

Hi Daniel,

    Type *allocated_type = ptr_type->dereference(); // this is the
    operation that doesn't seem to exist.

Type *allocated_type = ptr_type->getElementType();

Ciao,

Duncan.

I can’t believe I missed it. I think I must have forgotten to look at SequentialType when I was searching through the API docs.

Thanks very much,

Daniel

2009/10/20 Duncan Sands <baldrick@free.fr>

Óscar Fuentes wrote:

Daniel Waterworth <da.waterworth@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 allocated

This looks suspicious to me.

Probably he wants getTypeAllocSize (available from target data). The
special casing of PointerType is both useless and bogus.

Ciao,

Duncan.

It may not be the best way to do what I’m trying to do, but it’s not useless and bogus. Consider the following:

%1 = alloca i32* ; %1 is of type i32**, dereferenced it becomes a type i32* and
; the size of that is sizeof(void *) bytes

Having said that I am looking at the target data getTypeAllocSize method.

Daniel

2009/10/20 Duncan Sands <baldrick@free.fr>

Daniel Waterworth <da.waterworth@googlemail.com> writes:

It may not be the best way to do what I'm trying to do, but it's not useless
and bogus. Consider the following:

%1 = alloca i32* ; %1 is of type i32**, dereferenced it becomes a type i32*
and
                         ; the size of that is sizeof(void *) bytes

What Duncan says is that any valid method for obtaining the size of a
Type should work for a PointerType, which is just another kind of Type.

I agree, however, I didn’t realise there was another method for obtaining the size until Duncan, correctly, pointed one out. I have since changed my code to use the getAllocSize method.

Thanks,

Daniel

2009/10/20 Óscar Fuentes <ofv@wanadoo.es>

Hi Daniel,

It may not be the best way to do what I'm trying to do, but it's not useless and bogus. Consider the following:

%1 = alloca i32* ; %1 is of type i32**, dereferenced it becomes a type i32* and
                         ; the size of that is sizeof(void *) bytes

it is useless because getTypeAllocSize will handle any type, including
pointer types. It is bogus because your code assumes that the LLVM
bitcode is targeting your own machine, i.e. that sizeof(void*) as given
by your machine is correct for the target. This may or may not be the
case, but it is something best avoided.

Ciao,

Duncan.

I’m writing a function pass that is run before an ExecutionEngine Jit’s the code. Therefore I can assume that my machine is the correct target. It is never the case that the pass would be run and then stored.

Daniel

2009/10/20 Duncan Sands <baldrick@free.fr>