[llvm-commits] CVS: llvm/docs/LangRef.html
Chris Lattner
lattner at cs.uiuc.edu
Sun Apr 4 20:32:38 PDT 2004
Changes in directory llvm/docs:
LangRef.html updated: 1.54 -> 1.55
---
Log message:
Update getelementptr instruction description
---
Diffs of the changes: (+84 -39)
Index: llvm/docs/LangRef.html
diff -u llvm/docs/LangRef.html:1.54 llvm/docs/LangRef.html:1.55
--- llvm/docs/LangRef.html:1.54 Fri Mar 12 15:19:06 2004
+++ llvm/docs/LangRef.html Sun Apr 4 20:30:49 2004
@@ -1423,58 +1423,103 @@
%val = load int* %ptr <i>; yields {int}:val = int 3</i>
</pre>
<!-- _______________________________________________________________________ -->
-<div class="doc_subsubsection"> <a name="i_getelementptr">'<tt>getelementptr</tt>'
-Instruction</a> </div>
+<div class="doc_subsubsection">
+ <a name="i_getelementptr">'<tt>getelementptr</tt>' Instruction</a>
+</div>
+
<div class="doc_text">
<h5>Syntax:</h5>
-<pre> <result> = getelementptr <ty>* <ptrval>{, long <aidx>|, ubyte <sidx>}*<br></pre>
+<pre>
+ <result> = getelementptr <ty>* <ptrval>{, <ty> <idx>}*
+</pre>
+
<h5>Overview:</h5>
-<p>The '<tt>getelementptr</tt>' instruction is used to get the address
-of a subelement of an aggregate data structure.</p>
+
+<p>
+The '<tt>getelementptr</tt>' instruction is used to get the address of a
+subelement of an aggregate data structure.</p>
+
<h5>Arguments:</h5>
-<p>This instruction takes a list of <tt>long</tt> values and <tt>ubyte</tt>
-constants that indicate what form of addressing to perform. The actual
-types of the arguments provided depend on the type of the first pointer
-argument. The '<tt>getelementptr</tt>' instruction is used to index
-down through the type levels of a structure.</p>
+
+<p>This instruction takes a list of integer constants that indicate what
+elements of the aggregate object to index to. The actual types of the arguments
+provided depend on the type of the first pointer argument. The
+'<tt>getelementptr</tt>' instruction is used to index down through the type
+levels of a structure. When indexing into a structure, only <tt>uint</tt>
+integer constants are allowed. When indexing into an array or pointer
+<tt>int</tt> and <tt>long</tt> indexes are allowed of any sign.</p>
+
<p>For example, let's consider a C code fragment and how it gets
compiled to LLVM:</p>
-<pre>struct RT {<br> char A;<br> int B[10][20];<br> char C;<br>};<br>struct ST {<br> int X;<br> double Y;<br> struct RT Z;<br>};<br><br>int *foo(struct ST *s) {<br> return &s[1].Z.B[5][13];<br>}<br></pre>
+
+<pre>
+ struct RT {
+ char A;
+ int B[10][20];
+ char C;
+ };
+ struct ST {
+ int X;
+ double Y;
+ struct RT Z;
+ };
+
+ int *foo(struct ST *s) {
+ return &s[1].Z.B[5][13];
+ }
+</pre>
+
<p>The LLVM code generated by the GCC frontend is:</p>
-<pre>%RT = type { sbyte, [10 x [20 x int]], sbyte }<br>%ST = type { int, double, %RT }<br><br>int* "foo"(%ST* %s) {<br> %reg = getelementptr %ST* %s, long 1, ubyte 2, ubyte 1, long 5, long 13<br> ret int* %reg<br>}<br></pre>
+
+<pre>
+ %RT = type { sbyte, [10 x [20 x int]], sbyte }
+ %ST = type { int, double, %RT }
+
+ int* "foo"(%ST* %s) {
+ %reg = getelementptr %ST* %s, int 1, uint 2, uint 1, int 5, int 13<br>
+ ret int* %reg
+ }
+</pre>
+
<h5>Semantics:</h5>
-<p>The index types specified for the '<tt>getelementptr</tt>'
-instruction depend on the pointer type that is being index into. <a
- href="t_pointer">Pointer</a> and <a href="t_array">array</a> types
-require '<tt>long</tt>' values, and <a href="t_struct">structure</a>
-types require '<tt>ubyte</tt>' <b>constants</b>.</p>
+
+<p>The index types specified for the '<tt>getelementptr</tt>' instruction depend
+on the pointer type that is being index into. <a href="t_pointer">Pointer</a>
+and <a href="t_array">array</a> types require <tt>uint</tt>, <tt>int</tt>,
+<tt>ulong</tt>, or <tt>long</tt> values, and <a href="t_struct">structure</a>
+types require <tt>uint</tt> <b>constants</b>.</p>
+
<p>In the example above, the first index is indexing into the '<tt>%ST*</tt>'
-type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int,
-double, %RT }</tt>' type, a structure. The second index indexes into
-the third element of the structure, yielding a '<tt>%RT</tt>' = '<tt>{
-sbyte, [10 x [20 x int]], sbyte }</tt>' type, another structure. The
-third index indexes into the second element of the structure, yielding
-a '<tt>[10 x [20 x int]]</tt>' type, an array. The two dimensions of
-the array are subscripted into, yielding an '<tt>int</tt>' type. The '<tt>getelementptr</tt>'
-instruction return a pointer to this element, thus yielding a '<tt>int*</tt>'
-type.</p>
+type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int, double, %RT
+}</tt>' type, a structure. The second index indexes into the third element of
+the structure, yielding a '<tt>%RT</tt>' = '<tt>{ sbyte, [10 x [20 x int]],
+sbyte }</tt>' type, another structure. The third index indexes into the second
+element of the structure, yielding a '<tt>[10 x [20 x int]]</tt>' type, an
+array. The two dimensions of the array are subscripted into, yielding an
+'<tt>int</tt>' type. The '<tt>getelementptr</tt>' instruction return a pointer
+to this element, thus computing a value of '<tt>int*</tt>' type.</p>
+
<p>Note that it is perfectly legal to index partially through a
structure, returning a pointer to an inner element. Because of this,
the LLVM code for the given testcase is equivalent to:</p>
-<pre>int* "foo"(%ST* %s) {<br> %t1 = getelementptr %ST* %s , long 1 <i>; yields %ST*:%t1</i>
- %t2 = getelementptr %ST* %t1, long 0, ubyte 2 <i>; yields %RT*:%t2</i>
- %t3 = getelementptr %RT* %t2, long 0, ubyte 1 <i>; yields [10 x [20 x int]]*:%t3</i>
- %t4 = getelementptr [10 x [20 x int]]* %t3, long 0, long 5 <i>; yields [20 x int]*:%t4</i>
- %t5 = getelementptr [20 x int]* %t4, long 0, long 13 <i>; yields int*:%t5</i>
- ret int* %t5
-}
+
+<pre>
+ int* "foo"(%ST* %s) {
+ %t1 = getelementptr %ST* %s, int 1 <i>; yields %ST*:%t1</i>
+ %t2 = getelementptr %ST* %t1, int 0, uint 2 <i>; yields %RT*:%t2</i>
+ %t3 = getelementptr %RT* %t2, int 0, uint 1 <i>; yields [10 x [20 x int]]*:%t3</i>
+ %t4 = getelementptr [10 x [20 x int]]* %t3, int 0, int 5 <i>; yields [20 x int]*:%t4</i>
+ %t5 = getelementptr [20 x int]* %t4, int 0, int 13 <i>; yields int*:%t5</i>
+ ret int* %t5
+ }
</pre>
<h5>Example:</h5>
-<pre> <i>; yields [12 x ubyte]*:aptr</i>
- %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, ubyte 1<br></pre>
-<h5> Note To The Novice:</h5>
-When using indexing into global arrays with the '<tt>getelementptr</tt>'
-instruction, you must remember that the </div>
+<pre>
+ <i>; yields [12 x ubyte]*:aptr</i>
+ %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, uint 1
+</pre>
+
+</div>
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="otherops">Other Operations</a> </div>
<div class="doc_text">
@@ -2148,7 +2193,7 @@
<a href="mailto:sabre at nondot.org">Chris Lattner</a><br>
<a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
- Last modified: $Date: 2004/03/12 21:19:06 $
+ Last modified: $Date: 2004/04/05 01:30:49 $
</address>
</body>
</html>
More information about the llvm-commits
mailing list