[LLVMdev] Implementing sizeof

Sarah Thompson thompson at email.arc.nasa.gov
Fri Jul 27 12:00:27 PDT 2007


Hi folks,

Assuming that I'm writing a pass and that for bizarre reasons I need to 
programmatically do the equivalent of a C/C++ sizeof on a Value (or a 
Type, it doesn't matter which really), yielding a result in bytes, what 
is the known-safe way to do this? I notice that doing something like

struct thingy
{
    ... some stuff ...
};

...
    printf("Size = %d", sizeof(thingy));
...

turns into a constant at the bitcode disassembly level, which is fair 
enough but I can't rely on the source passing down information like that 
-- I have to be able to work purely at the opt pass level. One of the 
transformations I need to do is turn load and store instructions into 
model-checker-specific intrinsics so I can track memory modifications 
efficiently, but just having the address isn't enough, I really need the 
size too. Or are reads and writes always guaranteed to be simple types, 
so the bit width (as given by the Type::getPrimitiveSizeInBits() member 
function) divided by 8 and rounded up if necessary  is enough? (The 
latter works well for me too, I just don't want to mess myself up by 
making assumptions).

As usual, thank you in  advance.

Sarah



More information about the llvm-dev mailing list