I am forwarding a question and response about the use of getelementptr:
Hi. I found an LLVM code example that might be an error. The relevant
URL is: http://llvm.cs.uiuc.edu/docs/LangRef.html#modulestructure
There is a piece of code which looks like this:
; Declare the string constant as a global constant...
%.LC0 = internal constant [13 x sbyte] c"hello world\0A\00"
Which is then followed by this:
; Convert [13x sbyte]* to sbyte *...
%cast210 = getelementptr [13 x sbyte]* %.LC0, uint 0, uint 0 ;
Shouldn't the getelementptr have only one "uint 0"? Or am I just not
understanding the documentation? Thanks
The example is correct (but the getElementPtr is one of the more confusing
things in LLVM, so it's a reasonable question). The LLVM language
reference online is useful for such questions about instruction semantics.
Basically, any pointer in LLVM represents a potential array just
like in C. (E.g., an int* may point to a single int or to an array
of 100 ints.) The first index of a getElementPtr instruction indexes
this pointer. (So if it is a pointer to a single object, the
first index must be 0). The next index indexes into the individual
objects in that array.
%.LC0 is a pointer to an array of sbytes, so it must be accessed like
an array of arrays. So
getelementptr [13 x sbyte]* %.LC0, uint 0 ;; [13 x sbyte]*
simply returns a pointer to the array itself, and
getelementptr [13 x sbyte]* %.LC0, uint 0, uint 0 ;; sbyte*
returns a pointer to the first sbyte in the array.
The first uint 0 may seem redundant in this case, but remember that
a variable of the same type as %.LC0 (i.e., [13 x sbyte]*) can also point
to an array of strings, and in that case the first index may not be 0.