[LLVMdev] Sizeof a type?

Duncan Sands baldrick at free.fr
Sun Jan 20 04:43:51 PST 2013


Hi Rick,

On 20/01/13 13:04, Rick Mann wrote:
>
> On Jan 20, 2013, at 3:21 , Duncan Sands <baldrick at free.fr> wrote:
>
>> if you have DataLayout available, use the getTypeAllocSize method.  If you don't
>> have information about the target then you can use ConstantExpr::getSizeOf.  The
>> advantage of DataLayout is that it returns a number (eg: 8) while getSizeOf
>> returns a mysterious expression (the optimizers will simplify it to a number,
>> the same as getTypeAllocSize returns, if you provide them with DataLayout).
>
> Thanks, Duncan. What's the difference between DataLayout::getTypeAllocSize and DataLayout::getTypeStoreSize?

the store size doesn't include alignment padding.  Consider an x86 long double
type; it contains 10 bytes of info.  It is aligned to a multiple of 4 bytes
on some platforms, and to a multiple of 8 bytes on others.  On the first type
of platform getTypeAllocSize and C's sizeof will return 12, while on the second
type of platform they will both return 16.  On all platforms the store size is
10 because that's the number of bytes that writing one of these guys to memory
will actually overwrite, the rest is padding.

Ciao, Duncan.



More information about the llvm-dev mailing list