+<!-- *********************************************************************** -->
+<div class="doc_subsection">
+ <a name="firstptr"><b>What is the first index of the GEP instruction?</b></a>
+</div>
+<div class="doc_text">
+ <p>Quick answer: Because its already present.</p>
+ <p>Having understood the <a href="#deref">previous question</a>, a new
+ question then arises:</p>
+ <blockquote><i>Why is it okay to index through the first pointer, but
+ subsequent pointers won't be dereferenced?</i></blockquote>
+ <p>The answer is simply because memory does not have to be accessed to
+ perform the computation. The first operand to the GEP instruction must be a
+ value of a pointer type. The value of the pointer is provided directly to
+ the GEP instruction without any need for accessing memory. It must,
+ therefore be indexed like any other operand. Consider this example:</p>
+ <pre>
+ struct munger_struct {
+ int f1;
+ int f2;
+ };
+ void munge(struct munger_struct *P)
+ {
+ P[0].f1 = P[1].f1 + P[2].f2;
+ }
+ ...
+ complex Array[3];
+ ...
+ munge(Array);</pre>
+ <p>In this "C" example, the front end compiler (llvm-gcc) will generate three
+ GEP instructions for the three indices through "P" in the assignment
+ statement. The function argument <tt>P</tt> will be the first operand of each
+ of these GEP instructions. The second operand will be the field offset into
+ the <tt>struct munger_struct</tt> type, for either the <tt>f1</tt> or
+ <tt>f2</tt> field. So, in LLVM assembly the <tt>munge</tt> function looks
+ like:</p>
+ <pre>
+ void %munge(%struct.munger_struct* %P) {
+ entry:
+ %tmp = getelementptr %struct.munger_struct* %P, int 1, uint 0
+ %tmp = load int* %tmp
+ %tmp6 = getelementptr %struct.munger_struct* %P, int 2, uint 1
+ %tmp7 = load int* %tmp6
+ %tmp8 = add int %tmp7, %tmp
+ %tmp9 = getelementptr %struct.munger_struct* %P, int 0, uint 0
+ store int %tmp8, int* %tmp9
+ ret void
+ }</pre>
+ <p>In each case the first operand is the pointer through which the GEP
+ instruction starts. The same is true whether the first operand is an
+ argument, allocated memory, or a global variable. </p>
+ <p>To make this clear, let's consider a more obtuse example:</p>
+ <pre>
+ %MyVar = unintialized global int
+ ...
+ %idx1 = getelementptr int* %MyVar, long 0
+ %idx2 = getelementptr int* %MyVar, long 1
+ %idx3 = getelementptr int* %MyVar, long 2</pre>
+ <p>These GEP instructions are simply making address computations from the
+ base address of <tt>MyVar</tt>. They compute, as follows (using C syntax):
+ </p>
+ <ul>
+ <li> idx1 = (char*) &MyVar + 0</li>
+ <li> idx2 = (char*) &MyVar + 4</li>
+ <li> idx3 = (char*) &MyVar + 8</li>
+ </ul>
+ <p>Since the type <tt>int</tt> is known to be four bytes long, the indices
+ 0, 1 and 2 translate into memory offsets of 0, 4, and 8, respectively. No
+ memory is accessed to make these computations because the address of
+ <tt>%MyVar</tt> is passed directly to the GEP instructions.</p>
+ <p>The obtuse part of this example is in the cases of <tt>%idx2</tt> and
+ <tt>%idx3</tt>. They result in the computation of addresses that point to
+ memory past the end of the <tt>%MyVar</tt> global, which is only one
+ <tt>int</tt> long, not three <tt>int</tt>s long. While this is legal in LLVM,
+ it is inadvisable because any load or store with the pointer that results
+ from these GEP instructions would produce undefined results.</p>
+</div>
+