[LLVMdev] array index access
Jeffrey Yasskin
jyasskin at google.com
Mon Mar 28 13:16:06 PDT 2011
On Mon, Mar 28, 2011 at 12:48 PM, George Baah <georgebaah at gmail.com> wrote:
> 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().
> 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.
> }
>
> On Mon, Mar 28, 2011 at 3:25 PM, Duncan Sands <baldrick at free.fr> wrote:
>>
>> 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
>>>
>>>
>>
>
>
> _______________________________________________
> LLVM Developers mailing list
> 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