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>
Tim Northover
2013-Sep-16 16:17 UTC
[LLVMdev] Is GEP safe if not know the size of an array?
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.