[LLVMdev] Sizeof a type?

Duncan Sands baldrick at free.fr
Sun Jan 20 03:21:51 PST 2013

Hi Rick,

On 20/01/13 06:23, Rick Mann wrote:
> Is there a way to get the sizeof a type? Say I've created a struct type without any explicit attribtues, and I want to call malloc() to allocate space for it. Can I get the size as an llvm::Constant* (or other llvm::Value*) given an llvm::StructType* or other llvm::Type*?

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).

Ciao, Duncan.

More information about the llvm-dev mailing list