newbi to llvm - how to get array size

Hi,

I am new to llvm to so please be kind to me if I am asking something that is too simple. I want to get the bounds of an array when I have a handle to getelementptr or insertelement or extractvalue instructions. I am writing a translation routine from llvm to a non-deterministic language and want to include array bounds check in it.

The array size gets printed as part of the type of the value

= getelementptr inbounds * (ty idx)

I am wrongly expecting I.getOperand(0)->getType()->getNumElements() to give me the array size (after casting ptrval to array type)

I am not getting the array size, I am getting a value of zero.

Can someone point out to me how to get array size (number of elements in the array) from the type object. What about multi-dimension arrays too.

Thanks.

Surinder Kumar Jain

Hi Surinder,

I am new to llvm to so please be kind to me if I am asking something that is too
simple. I want to get the bounds of an array when I have a handle to
getelementptr or insertelement or extractvalue instructions. I am writing a
translation routine from llvm to a non-deterministic language and want to
include array bounds check in it.
The array size gets printed as part of the type of the value <pty>
<result> = getelementptr inbounds <pty>* <ptrval> (ty idx)
I am wrongly expecting I.getOperand(0)->getType()->getNumElements() to give me
the array size (after casting ptrval to array type)
I am not getting the array size, I am getting a value of zero.
Can someone point out to me how to get array size (number of elements in the
array) from the type object. What about multi-dimension arrays too.

the first operand always has pointer type, you need the type of the pointee,
something like this: I.getPointerOperandType()->getElementType()
You can then cast the returned type T to an array type and grab the number
of elements:
   cast<ArrayType>(T)->getNumElements()

Ciao, Duncan.