[LLVMdev] Size of structs & arrays

Duncan Sands baldrick at free.fr
Wed Feb 22 23:47:28 PST 2012


Hi Fraser,

> I'm trying to work out the size of a struct so that I can pass this to the
> 'llvm.memcpy' intrinsic. It's easy to find out how I'm supposed to do this,
> as I keep seeing the following:
>
> %Size = getelementptr %T* null, int 1
> %SizeI = cast %T* %Size to uint
>
> But I'm unsure how I actually do this in the C++ API. Is the 'null' here a
> llvm::ConstantPointerNull? Any help here would be much appreciated.

you can use ConstantExpr::getSizeOf.

> Additionally, I was wondering whether I can use this method to find out the
> size of variable-length arrays, such as [0 x i32], for example? I've tried
> doing this and am obviously doing something wrong.
>
> %Size = getelementptr [0 x i32]* %1, i32* null, i32 1
> %SizeI = ptrtoint i32* %Size to i64
>
> This gives entirely the wrong result,

This will return zero, since this array has size zero.  It is not a variable
length array, it is an array with length zero.

  and I'm not surprised, as it doesn't
> look correct. Should I instead be implementing these arrays with a size
> associated with them? Something like { i32, [0 x i32]}, for instance?

You need to know the length somehow.  This is one way.

Ciao, Duncan.



More information about the llvm-dev mailing list