Get Size of LLVM Type / "Dereference" Type

497 Views Asked by At

This question is quite similar to this one, but I am not realy sure how to get the size in the following situation:

I have a Pointer type, e.g. i32*.

Now I would like to get the size of the 'pointed-to' type, so i32 (aka 4).

Right now I am using the following code, that seems to work, but I am not sure if it is actualy correct:

Value *get_size(Type *t, IRBuilder<>& irb)
{
    Value *sizePtr = irb.CreateGEP(t->getContainedType(0),
            irb.CreateIntToPtr(ConstantInt::get(irb.getInt8Ty(), 0), t),
            ConstantInt::get(irb.getInt8Ty(), 1));

    return irb.CreatePtrToInt(sizePtr, irb.getInt64Ty());
}

(note: The Type t that is passed to the function will allways be a pointer type)

0

There are 0 best solutions below