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

1 view
Skip to first unread message

weixuegong

unread,
Sep 16, 2013, 4:57:47 AM9/16/13
to llv...@cs.uiuc.edu
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?

Tim Northover

unread,
Sep 16, 2013, 12:17:30 PM9/16/13
to weixuegong, LLVM Developers Mailing List
Hi,

> So, is there any problem? Not safe, or just not good but basically OK?

It's probably safe (at least on non-exotic targets, and probably
universally). But the canonical way to do it would probably be to use
the first index and offset the i32* directly:

%v1 = alloca i32
store i32 5, i32* %v1
%6 = load i32* %v1
%7 = getelementptr i32* %v0, i32 %6
%8 = load i32* %7
store i32 %8, i32* %v0

Cheers.

Tim.
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply all
Reply to author
Forward
0 new messages