[LLVMdev] array index access

Duncan Sands baldrick at free.fr
Mon Mar 28 12:25:40 PDT 2011


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.

>
> George
>
> On Mon, Mar 28, 2011 at 4:32 AM, Duncan Sands <baldrick at free.fr
> <mailto:baldrick at free.fr>> wrote:
>
>     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.
>     _______________________________________________
>     LLVM Developers mailing list
>     LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> http://llvm.cs.uiuc.edu
>     http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>




More information about the llvm-dev mailing list