[LLVMdev] newbi to llvm - how to get array size

Duncan Sands baldrick at free.fr
Wed Jan 12 02:41:16 PST 2011


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.



More information about the llvm-dev mailing list