struct size

Hello,

I have a code (from example in docs):
%struct.ST = type { i32, double, %struct.RT }
%struct.RT = type { i8, [10 x [20 x i32]], i8 }

; Function Attrs: norecurse nounwind readnone uwtable
define i32* @foo(%struct.ST* readnone %s) #0 {
%1 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 1, i32 2, i32 1, i64 5, i64 13
ret i32* %1
}

I want to identify, that %s is pointer to struct, determine what this struct is and determine the size of every component? With every component is more or less ok, but the operand %s is not a struct but a pointer. Is there a convenient way to track this structure usage in getelementptr?

If you have a Type* object that you know it a pointer, you can cast it to PointerType*, and then obtain the pointee type via getElementType member function.

If you have a Type* object that is a structure, you can cast it to StructType*, which then has iterators that allow you to inspect every member type.

Look in include/llvm/IR/DerivedTypes.h for more information.

-Krzysztof

I want to identify, that %s is pointer to struct, determine what this
struct is and determine the size of every component? With every
component is more or less ok, but the operand %s is not a struct but a
pointer. Is there a convenient way to track this structure usage in
getelementptr?

If you have a Type* object that you know it a pointer, you can cast it
to PointerType*, and then obtain the pointee type via getElementType
member function.

Note: Please don’t rely on this, eventually pointer element types will go away. GEP does have a value type you can access directly. (GetElementPointer::getValueType, if I recall correctly)