How to get number of elements of the array from AllocaInst?

If an alloca instruction is allocating memory for an array like this:
%arr = alloca [5 x i32], align 4
How can I get the number of elements in the array (which is 5 in this case)?

I haven’t tried, but I would guess it’s getArraySize():

I don’t think that’s what you want in this case. This is a single element allocation with array type, not an array allocation. You would want to check if getAllocatedType() is ArrayType, and check getNumElements.

If alloc is a variable of type AllocaInst* you can get the number of array elements with alloc->getAllocatedType()->getArrayNumElements(). (You might want to check if the allocated type is indeed ArrayType. Otherwise you should end in an error.)