[LLVMdev] Is GEP safe if not know the size of an array?

weixuegong weixuegong at gmail.com
Mon Sep 16 01:57:47 PDT 2013


Hi, all
I am trying to use GEP to get a pointer of i32 from an array.
But the problem is: I don't know the size of the array.
The IR document on llvm.org said GEP just adds the offsets to the base
address with silently-wrapping two’s complement arithmetic.
So, I want to ask for some advice.
Is it safe like this:

%v1 = alloca i32
store i32 5, i32* %v1
%6 = load i32* %v1
%7 = bitcast i32* %v0 to [1 x i32]*
%8 = getelementptr [1 x i32]* %7, i32 0, i32 %6
%9 = load i32* %8
store i32 %9, i32* %v0

Type of %v0 is i32*, and I know %v0 is pointing to an array in mem, but
the size is 9, not 1.
Then I "GEP" from %7 which I treat it as a [1 x i32]*, not [9 x i32]* ,
but the "offset" is 5(%6).

So, is there any problem? Not safe, or just not good but basically OK?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130916/9f45ae39/attachment.html>


More information about the llvm-dev mailing list