array index access

Hi Everyone,
I am trying to get the first index into this two-dimensional array, that is 5.

%4 = getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 5

I can iterate over the GEP and get the types of the operands but not the values.

How do I get the value (5)?
Thanks.

George

Hi George,

   I am trying to get the first index into this two-dimensional array, that is *5.*

not sure what you mean, but if you think of your array as being a 10 x 20 matrix
then to get a pointer to element M,N you would do:
   getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %N

%4 = getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 *5*
*
I can iterate over the GEP and get the types of the operands but not the values.

How do I get the value (5)?

getelementptr only does pointer arithmetic, i.e. using it you can get a pointer
to an array element. But to get the element itself you need to use a load
instruction to load the value pointed to by the pointer.

Ciao, Duncan.

Hi Duncan,
For this example,
getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %NI am trying to retrieve %N and instrument the program to see if the value
pointed to by %N exceeds the array bound (10). I assume that
%N will be associated with a load instruction. I have searched through
all the tutorials etc. and still have no clue as to how to do this, that is,
get the value and then instrument the code.

George

Hi George,

  For this example,
   getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %N
I am trying to retrieve %N and instrument the program to see if the value
pointed to by %N exceeds the array bound (10). I assume that
%N will be associated with a load instruction. I have searched through
all the tutorials etc. and still have no clue as to how to do this, that is,
get the value and then instrument the code.

if
   %p = getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %N
then you need an instruction
   %v = load %p
to get the value %v pointed to by %p.

Ciao, Duncan.

Oh I see what you mean but I don’t think that is the value that I want.
In the code, I know that

%N = load i32* %k, align 4
%p = getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %N

So I analyze the GEP and know that %N is a pointer to an int32, which is the array index.
I want to get that index so I can insert a check to see if it violates the array bounds.
So the final code (in pseudocode) will look like,

%N = load i32* %k, align 4
if( 0 <= %N < array-size){
%p = getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %N

}else{
throw an exception.
}

Oh I see what you mean but I don't think that is the value that I want.
In the code, I know that
%N = load i32* %k, align 4
%p = getelementptr inbounds [10 x [20 x i32]]* %3, i32 0, i32 %M, i32 %N
So I analyze the GEP and know that %N is a pointer to an int32, which is the
array index.

p->getOperand(3) will return %N as an llvm::Value(). Then you can use
dyn_cast<llvm::ConstantInt> to see if it's a ConstantInt as opposed to
a run-time variable (or some other flavor of constant like a
ConstantExpr), and then you can pull the value out using getValue() or
get{Z,S}ExtValue().

Thanks Guys for the information. I now understand!