blob: d1c5b6c5367c00eb9999c1e0c644d32c8abdf5ee [file] [log] [blame]
Reid Spencere00906f2006-08-10 20:15:58 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>The Often Misunderstood GEP Instruction</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
Reid Spencerf19ccf82006-08-10 21:01:14 +00008 <style type="text/css">
9 TABLE { text-align: left; border: 1px solid black; border-collapse: collapse; margin: 0 0 0 0; }
10 </style>
Reid Spencere00906f2006-08-10 20:15:58 +000011</head>
12<body>
13
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000014<h1>
Reid Spencere00906f2006-08-10 20:15:58 +000015 The Often Misunderstood GEP Instruction
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000016</h1>
Reid Spencere00906f2006-08-10 20:15:58 +000017
18<ol>
19 <li><a href="#intro">Introduction</a></li>
Dan Gohmanb02c08c2010-02-25 18:16:03 +000020 <li><a href="#addresses">Address Computation</a>
Reid Spencere00906f2006-08-10 20:15:58 +000021 <ol>
22 <li><a href="#extra_index">Why is the extra 0 index required?</a></li>
23 <li><a href="#deref">What is dereferenced by GEP?</a></li>
24 <li><a href="#firstptr">Why can you index through the first pointer but not
25 subsequent ones?</a></li>
26 <li><a href="#lead0">Why don't GEP x,0,0,1 and GEP x,1 alias? </a></li>
27 <li><a href="#trail0">Why do GEP x,1,0,0 and GEP x,1 alias? </a></li>
Dan Gohmanb02c08c2010-02-25 18:16:03 +000028 <li><a href="#vectors">Can GEP index into vector elements?</a>
Dan Gohmanb02c08c2010-02-25 18:16:03 +000029 <li><a href="#addrspace">What effect do address spaces have on GEPs?</a>
30 <li><a href="#int">How is GEP different from ptrtoint, arithmetic, and inttoptr?</a></li>
31 <li><a href="#be">I'm writing a backend for a target which needs custom lowering for GEP. How do I do this?</a>
32 <li><a href="#vla">How does VLA addressing work with GEPs?</a>
33 </ol></li>
34 <li><a href="#rules">Rules</a>
35 <ol>
36 <li><a href="#bounds">What happens if an array index is out of bounds?</a>
37 <li><a href="#negative">Can array indices be negative?</a>
38 <li><a href="#compare">Can I compare two values computed with GEPs?</a>
39 <li><a href="#types">Can I do GEP with a different pointer type than the type of the underlying object?</a>
40 <li><a href="#null">Can I cast an object's address to integer and add it to null?</a>
41 <li><a href="#ptrdiff">Can I compute the distance between two objects, and add that value to one address to compute the other address?</a>
42 <li><a href="#tbaa">Can I do type-based alias analysis on LLVM IR?</a>
43 <li><a href="#overflow">What happens if a GEP computation overflows?</a>
44 <li><a href="#check">How can I tell if my front-end is following the rules?</a>
45 </ol></li>
46 <li><a href="#rationale">Rationale</a>
47 <ol>
48 <li><a href="#goals">Why is GEP designed this way?</a></li>
49 <li><a href="#i32">Why do struct member indices always use i32?</a></li>
50 <li><a href="#uglygep">What's an uglygep?</a>
Reid Spencere00906f2006-08-10 20:15:58 +000051 </ol></li>
52 <li><a href="#summary">Summary</a></li>
53</ol>
54
55<div class="doc_author">
56 <p>Written by: <a href="mailto:rspencer@reidspencer.com">Reid Spencer</a>.</p>
57</div>
58
59
60<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000061<h2><a name="intro">Introduction</a></h2>
Reid Spencere00906f2006-08-10 20:15:58 +000062<!-- *********************************************************************** -->
Dan Gohmanb02c08c2010-02-25 18:16:03 +000063
Reid Spencere00906f2006-08-10 20:15:58 +000064<div class="doc_text">
65 <p>This document seeks to dispel the mystery and confusion surrounding LLVM's
Dan Gohmanff70fe42010-07-06 15:26:33 +000066 <a href="LangRef.html#i_getelementptr">GetElementPtr</a> (GEP) instruction.
67 Questions about the wily GEP instruction are
Benjamin Kramer8040cd32009-10-12 14:46:08 +000068 probably the most frequently occurring questions once a developer gets down to
Reid Spencere00906f2006-08-10 20:15:58 +000069 coding with LLVM. Here we lay out the sources of confusion and show that the
70 GEP instruction is really quite simple.
71 </p>
72</div>
73
74<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000075<h2><a name="addresses">Address Computation</a></h2>
Reid Spencere00906f2006-08-10 20:15:58 +000076<!-- *********************************************************************** -->
77<div class="doc_text">
78 <p>When people are first confronted with the GEP instruction, they tend to
79 relate it to known concepts from other programming paradigms, most notably C
Dan Gohmanb02c08c2010-02-25 18:16:03 +000080 array indexing and field selection. GEP closely resembles C array indexing
81 and field selection, however it's is a little different and this leads to
82 the following questions.</p>
Reid Spencere00906f2006-08-10 20:15:58 +000083</div>
84
85<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000086<h3>
87 <a name="firstptr">What is the first index of the GEP instruction?</a>
88</h3>
Reid Spencer919d3712006-08-15 03:32:10 +000089<div class="doc_text">
Reid Spencer80a4d052006-08-15 03:43:31 +000090 <p>Quick answer: The index stepping through the first operand.</p>
91 <p>The confusion with the first index usually arises from thinking about
92 the GetElementPtr instruction as if it was a C index operator. They aren't the
93 same. For example, when we write, in "C":</p>
Bill Wendling32759082008-01-04 12:04:32 +000094
95<div class="doc_code">
96<pre>
97AType *Foo;
98...
99X = &amp;Foo-&gt;F;
100</pre>
101</div>
102
Reid Spencereda573d2006-08-15 04:00:29 +0000103 <p>it is natural to think that there is only one index, the selection of the
104 field <tt>F</tt>. However, in this example, <tt>Foo</tt> is a pointer. That
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000105 pointer must be indexed explicitly in LLVM. C, on the other hand, indices
Jim Laskeye3c312f2006-08-15 08:14:19 +0000106 through it transparently. To arrive at the same address location as the C
Reid Spencereda573d2006-08-15 04:00:29 +0000107 code, you would provide the GEP instruction with two index operands. The
108 first operand indexes through the pointer; the second operand indexes the
109 field <tt>F</tt> of the structure, just as if you wrote:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000110
111<div class="doc_code">
112<pre>
113X = &amp;Foo[0].F;
114</pre>
115</div>
116
Reid Spencer80a4d052006-08-15 03:43:31 +0000117 <p>Sometimes this question gets rephrased as:</p>
Chris Lattner4a5dfee2006-08-17 03:26:50 +0000118 <blockquote><p><i>Why is it okay to index through the first pointer, but
119 subsequent pointers won't be dereferenced?</i></p></blockquote>
Reid Spencer919d3712006-08-15 03:32:10 +0000120 <p>The answer is simply because memory does not have to be accessed to
121 perform the computation. The first operand to the GEP instruction must be a
122 value of a pointer type. The value of the pointer is provided directly to
Reid Spencer1c6f87d2006-08-15 03:57:05 +0000123 the GEP instruction as an operand without any need for accessing memory. It
124 must, therefore be indexed and requires an index operand. Consider this
125 example:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000126
127<div class="doc_code">
128<pre>
129struct munger_struct {
130 int f1;
131 int f2;
132};
133void munge(struct munger_struct *P) {
134 P[0].f1 = P[1].f1 + P[2].f2;
135}
136...
137munger_struct Array[3];
138...
139munge(Array);
140</pre>
141</div>
142
Reid Spencer919d3712006-08-15 03:32:10 +0000143 <p>In this "C" example, the front end compiler (llvm-gcc) will generate three
144 GEP instructions for the three indices through "P" in the assignment
145 statement. The function argument <tt>P</tt> will be the first operand of each
Reid Spencer10146472006-08-16 05:53:32 +0000146 of these GEP instructions. The second operand indexes through that pointer.
147 The third operand will be the field offset into the
148 <tt>struct munger_struct</tt> type, for either the <tt>f1</tt> or
Reid Spencer919d3712006-08-15 03:32:10 +0000149 <tt>f2</tt> field. So, in LLVM assembly the <tt>munge</tt> function looks
150 like:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000151
152<div class="doc_code">
153<pre>
154void %munge(%struct.munger_struct* %P) {
155entry:
156 %tmp = getelementptr %struct.munger_struct* %P, i32 1, i32 0
157 %tmp = load i32* %tmp
158 %tmp6 = getelementptr %struct.munger_struct* %P, i32 2, i32 1
159 %tmp7 = load i32* %tmp6
160 %tmp8 = add i32 %tmp7, %tmp
161 %tmp9 = getelementptr %struct.munger_struct* %P, i32 0, i32 0
162 store i32 %tmp8, i32* %tmp9
163 ret void
164}
165</pre>
166</div>
167
Reid Spencer919d3712006-08-15 03:32:10 +0000168 <p>In each case the first operand is the pointer through which the GEP
169 instruction starts. The same is true whether the first operand is an
170 argument, allocated memory, or a global variable. </p>
171 <p>To make this clear, let's consider a more obtuse example:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000172
173<div class="doc_code">
174<pre>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000175%MyVar = uninitialized global i32
Bill Wendling32759082008-01-04 12:04:32 +0000176...
177%idx1 = getelementptr i32* %MyVar, i64 0
178%idx2 = getelementptr i32* %MyVar, i64 1
179%idx3 = getelementptr i32* %MyVar, i64 2
180</pre>
181</div>
182
Reid Spencer919d3712006-08-15 03:32:10 +0000183 <p>These GEP instructions are simply making address computations from the
184 base address of <tt>MyVar</tt>. They compute, as follows (using C syntax):
185 </p>
Bill Wendling32759082008-01-04 12:04:32 +0000186
187<div class="doc_code">
188<pre>
189idx1 = (char*) &amp;MyVar + 0
190idx2 = (char*) &amp;MyVar + 4
191idx3 = (char*) &amp;MyVar + 8
192</pre>
193</div>
194
Reid Spencerb913a512007-02-09 17:56:02 +0000195 <p>Since the type <tt>i32</tt> is known to be four bytes long, the indices
Reid Spencer919d3712006-08-15 03:32:10 +0000196 0, 1 and 2 translate into memory offsets of 0, 4, and 8, respectively. No
197 memory is accessed to make these computations because the address of
198 <tt>%MyVar</tt> is passed directly to the GEP instructions.</p>
199 <p>The obtuse part of this example is in the cases of <tt>%idx2</tt> and
200 <tt>%idx3</tt>. They result in the computation of addresses that point to
201 memory past the end of the <tt>%MyVar</tt> global, which is only one
Reid Spencerb913a512007-02-09 17:56:02 +0000202 <tt>i32</tt> long, not three <tt>i32</tt>s long. While this is legal in LLVM,
Reid Spencer919d3712006-08-15 03:32:10 +0000203 it is inadvisable because any load or store with the pointer that results
204 from these GEP instructions would produce undefined results.</p>
205</div>
206
207<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000208<h3>
209 <a name="extra_index">Why is the extra 0 index required?</a>
210</h3>
Reid Spencere00906f2006-08-10 20:15:58 +0000211<!-- *********************************************************************** -->
212<div class="doc_text">
213 <p>Quick answer: there are no superfluous indices.</p>
214 <p>This question arises most often when the GEP instruction is applied to a
215 global variable which is always a pointer type. For example, consider
Bill Wendling32759082008-01-04 12:04:32 +0000216 this:</p>
217
218<div class="doc_code">
219<pre>
220%MyStruct = uninitialized global { float*, i32 }
221...
222%idx = getelementptr { float*, i32 }* %MyStruct, i64 0, i32 1
223</pre>
224</div>
225
Reid Spencerb913a512007-02-09 17:56:02 +0000226 <p>The GEP above yields an <tt>i32*</tt> by indexing the <tt>i32</tt> typed
Reid Spencere00906f2006-08-10 20:15:58 +0000227 field of the structure <tt>%MyStruct</tt>. When people first look at it, they
Reid Spencerb913a512007-02-09 17:56:02 +0000228 wonder why the <tt>i64 0</tt> index is needed. However, a closer inspection
Reid Spencer919d3712006-08-15 03:32:10 +0000229 of how globals and GEPs work reveals the need. Becoming aware of the following
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000230 facts will dispel the confusion:</p>
Reid Spencere00906f2006-08-10 20:15:58 +0000231 <ol>
Reid Spencerb913a512007-02-09 17:56:02 +0000232 <li>The type of <tt>%MyStruct</tt> is <i>not</i> <tt>{ float*, i32 }</tt>
233 but rather <tt>{ float*, i32 }*</tt>. That is, <tt>%MyStruct</tt> is a
Reid Spencere00906f2006-08-10 20:15:58 +0000234 pointer to a structure containing a pointer to a <tt>float</tt> and an
Reid Spencerb913a512007-02-09 17:56:02 +0000235 <tt>i32</tt>.</li>
Reid Spencere00906f2006-08-10 20:15:58 +0000236 <li>Point #1 is evidenced by noticing the type of the first operand of
237 the GEP instruction (<tt>%MyStruct</tt>) which is
Reid Spencerb913a512007-02-09 17:56:02 +0000238 <tt>{ float*, i32 }*</tt>.</li>
239 <li>The first index, <tt>i64 0</tt> is required to step over the global
Reid Spencer919d3712006-08-15 03:32:10 +0000240 variable <tt>%MyStruct</tt>. Since the first argument to the GEP
241 instruction must always be a value of pointer type, the first index
242 steps through that pointer. A value of 0 means 0 elements offset from that
243 pointer.</li>
Reid Spencerb913a512007-02-09 17:56:02 +0000244 <li>The second index, <tt>i32 1</tt> selects the second field of the
245 structure (the <tt>i32</tt>). </li>
Reid Spencere00906f2006-08-10 20:15:58 +0000246 </ol>
247</div>
248
249<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000250<h3>
251 <a name="deref">What is dereferenced by GEP?</a>
252</h3>
Reid Spencere00906f2006-08-10 20:15:58 +0000253<div class="doc_text">
254 <p>Quick answer: nothing.</p>
255 <p>The GetElementPtr instruction dereferences nothing. That is, it doesn't
Reid Spencer919d3712006-08-15 03:32:10 +0000256 access memory in any way. That's what the Load and Store instructions are for.
257 GEP is only involved in the computation of addresses. For example, consider
258 this:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000259
260<div class="doc_code">
261<pre>
262%MyVar = uninitialized global { [40 x i32 ]* }
263...
264%idx = getelementptr { [40 x i32]* }* %MyVar, i64 0, i32 0, i64 0, i64 17
265</pre>
266</div>
267
Reid Spencere00906f2006-08-10 20:15:58 +0000268 <p>In this example, we have a global variable, <tt>%MyVar</tt> that is a
269 pointer to a structure containing a pointer to an array of 40 ints. The
Reid Spencer80a4d052006-08-15 03:43:31 +0000270 GEP instruction seems to be accessing the 18th integer of the structure's
Reid Spencere00906f2006-08-10 20:15:58 +0000271 array of ints. However, this is actually an illegal GEP instruction. It
272 won't compile. The reason is that the pointer in the structure <i>must</i>
273 be dereferenced in order to index into the array of 40 ints. Since the
274 GEP instruction never accesses memory, it is illegal.</p>
275 <p>In order to access the 18th integer in the array, you would need to do the
276 following:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000277
278<div class="doc_code">
279<pre>
280%idx = getelementptr { [40 x i32]* }* %, i64 0, i32 0
281%arr = load [40 x i32]** %idx
282%idx = getelementptr [40 x i32]* %arr, i64 0, i64 17
283</pre>
284</div>
285
Reid Spencere00906f2006-08-10 20:15:58 +0000286 <p>In this case, we have to load the pointer in the structure with a load
287 instruction before we can index into the array. If the example was changed
288 to:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000289
290<div class="doc_code">
291<pre>
292%MyVar = uninitialized global { [40 x i32 ] }
293...
294%idx = getelementptr { [40 x i32] }*, i64 0, i32 0, i64 17
295</pre>
296</div>
297
Reid Spencere00906f2006-08-10 20:15:58 +0000298 <p>then everything works fine. In this case, the structure does not contain a
Reid Spencer80a4d052006-08-15 03:43:31 +0000299 pointer and the GEP instruction can index through the global variable,
Reid Spencerb913a512007-02-09 17:56:02 +0000300 into the first field of the structure and access the 18th <tt>i32</tt> in the
Reid Spencere00906f2006-08-10 20:15:58 +0000301 array there.</p>
302</div>
303
304<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000305<h3>
306 <a name="lead0">Why don't GEP x,0,0,1 and GEP x,1 alias?</a>
307</h3>
Reid Spencere00906f2006-08-10 20:15:58 +0000308<div class="doc_text">
309 <p>Quick Answer: They compute different address locations.</p>
310 <p>If you look at the first indices in these GEP
311 instructions you find that they are different (0 and 1), therefore the address
312 computation diverges with that index. Consider this example:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000313
314<div class="doc_code">
315<pre>
316%MyVar = global { [10 x i32 ] }
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000317%idx1 = getelementptr { [10 x i32 ] }* %MyVar, i64 0, i32 0, i64 1
318%idx2 = getelementptr { [10 x i32 ] }* %MyVar, i64 1
Bill Wendling32759082008-01-04 12:04:32 +0000319</pre>
320</div>
321
Reid Spencere00906f2006-08-10 20:15:58 +0000322 <p>In this example, <tt>idx1</tt> computes the address of the second integer
Misha Brukmanfc13d1c2009-08-18 19:18:40 +0000323 in the array that is in the structure in <tt>%MyVar</tt>, that is
324 <tt>MyVar+4</tt>. The type of <tt>idx1</tt> is <tt>i32*</tt>. However,
325 <tt>idx2</tt> computes the address of <i>the next</i> structure after
326 <tt>%MyVar</tt>. The type of <tt>idx2</tt> is <tt>{ [10 x i32] }*</tt> and its
327 value is equivalent to <tt>MyVar + 40</tt> because it indexes past the ten
328 4-byte integers in <tt>MyVar</tt>. Obviously, in such a situation, the
329 pointers don't alias.</p>
330
Reid Spencere00906f2006-08-10 20:15:58 +0000331</div>
332
333<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000334<h3>
335 <a name="trail0">Why do GEP x,1,0,0 and GEP x,1 alias?</a>
336</h3>
Reid Spencere00906f2006-08-10 20:15:58 +0000337<div class="doc_text">
338 <p>Quick Answer: They compute the same address location.</p>
339 <p>These two GEP instructions will compute the same address because indexing
340 through the 0th element does not change the address. However, it does change
341 the type. Consider this example:</p>
Bill Wendling32759082008-01-04 12:04:32 +0000342
343<div class="doc_code">
344<pre>
345%MyVar = global { [10 x i32 ] }
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000346%idx1 = getelementptr { [10 x i32 ] }* %MyVar, i64 1, i32 0, i64 0
347%idx2 = getelementptr { [10 x i32 ] }* %MyVar, i64 1
Bill Wendling32759082008-01-04 12:04:32 +0000348</pre>
349</div>
350
Reid Spencere00906f2006-08-10 20:15:58 +0000351 <p>In this example, the value of <tt>%idx1</tt> is <tt>%MyVar+40</tt> and
Reid Spencerb913a512007-02-09 17:56:02 +0000352 its type is <tt>i32*</tt>. The value of <tt>%idx2</tt> is also
353 <tt>MyVar+40</tt> but its type is <tt>{ [10 x i32] }*</tt>.</p>
Reid Spencere00906f2006-08-10 20:15:58 +0000354</div>
355
356<!-- *********************************************************************** -->
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000357
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000358<h3>
359 <a name="vectors">Can GEP index into vector elements?</a>
360</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000361<div class="doc_text">
362 <p>This hasn't always been forcefully disallowed, though it's not recommended.
363 It leads to awkward special cases in the optimizers, and fundamental
364 inconsistency in the IR. In the future, it will probably be outright
365 disallowed.</p>
366
367</div>
368
369<!-- *********************************************************************** -->
370
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000371<h3>
372 <a name="addrspace">What effect do address spaces have on GEPs?</a>
373</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000374<div class="doc_text">
375 <p>None, except that the address space qualifier on the first operand pointer
376 type always matches the address space qualifier on the result type.</p>
377
378</div>
379
380<!-- *********************************************************************** -->
381
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000382<h3>
383 <a name="int">
384 How is GEP different from ptrtoint, arithmetic, and inttoptr?
385 </a>
386</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000387<div class="doc_text">
388 <p>It's very similar; there are only subtle differences.</p>
389
390 <p>With ptrtoint, you have to pick an integer type. One approach is to pick i64;
391 this is safe on everything LLVM supports (LLVM internally assumes pointers
392 are never wider than 64 bits in many places), and the optimizer will actually
393 narrow the i64 arithmetic down to the actual pointer size on targets which
394 don't support 64-bit arithmetic in most cases. However, there are some cases
395 where it doesn't do this. With GEP you can avoid this problem.
396
397 <p>Also, GEP carries additional pointer aliasing rules. It's invalid to take a
398 GEP from one object, address into a different separately allocated
399 object, and dereference it. IR producers (front-ends) must follow this rule,
400 and consumers (optimizers, specifically alias analysis) benefit from being
401 able to rely on it. See the <a href="#rules">Rules</a> section for more
402 information.</p>
403
404 <p>And, GEP is more concise in common cases.</p>
405
406 <p>However, for the underlying integer computation implied, there
407 is no difference.</p>
408
409</div>
410
411<!-- *********************************************************************** -->
412
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000413<h3>
414 <a name="be">
415 I'm writing a backend for a target which needs custom lowering for GEP.
416 How do I do this?
417 </a>
418</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000419<div class="doc_text">
420 <p>You don't. The integer computation implied by a GEP is target-independent.
421 Typically what you'll need to do is make your backend pattern-match
422 expressions trees involving ADD, MUL, etc., which are what GEP is lowered
423 into. This has the advantage of letting your code work correctly in more
424 cases.</p>
425
426 <p>GEP does use target-dependent parameters for the size and layout of data
427 types, which targets can customize.</p>
428
429 <p>If you require support for addressing units which are not 8 bits, you'll
430 need to fix a lot of code in the backend, with GEP lowering being only a
431 small piece of the overall picture.</p>
432
433</div>
434
435<!-- *********************************************************************** -->
436
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000437<h3>
438 <a name="vla">How does VLA addressing work with GEPs?</a>
439</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000440<div class="doc_text">
441 <p>GEPs don't natively support VLAs. LLVM's type system is entirely static,
442 and GEP address computations are guided by an LLVM type.</p>
443
444 <p>VLA indices can be implemented as linearized indices. For example, an
445 expression like X[a][b][c], must be effectively lowered into a form
446 like X[a*m+b*n+c], so that it appears to the GEP as a single-dimensional
447 array reference.</p>
448
449 <p>This means if you want to write an analysis which understands array
450 indices and you want to support VLAs, your code will have to be
451 prepared to reverse-engineer the linearization. One way to solve this
452 problem is to use the ScalarEvolution library, which always presents
453 VLA and non-VLA indexing in the same manner.</p>
454</div>
455
456<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000457<h2><a name="rules">Rules</a></h2>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000458<!-- *********************************************************************** -->
459
460<!-- *********************************************************************** -->
461
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000462<h3>
463 <a name="bounds">What happens if an array index is out of bounds?</a>
464</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000465<div class="doc_text">
466 <p>There are two senses in which an array index can be out of bounds.</p>
467
468 <p>First, there's the array type which comes from the (static) type of
469 the first operand to the GEP. Indices greater than the number of elements
470 in the corresponding static array type are valid. There is no problem with
471 out of bounds indices in this sense. Indexing into an array only depends
472 on the size of the array element, not the number of elements.</p>
473
474 <p>A common example of how this is used is arrays where the size is not known.
475 It's common to use array types with zero length to represent these. The
476 fact that the static type says there are zero elements is irrelevant; it's
477 perfectly valid to compute arbitrary element indices, as the computation
478 only depends on the size of the array element, not the number of
479 elements. Note that zero-sized arrays are not a special case here.</p>
480
481 <p>This sense is unconnected with <tt>inbounds</tt> keyword. The
482 <tt>inbounds</tt> keyword is designed to describe low-level pointer
483 arithmetic overflow conditions, rather than high-level array
484 indexing rules.
485
486 <p>Analysis passes which wish to understand array indexing should not
487 assume that the static array type bounds are respected.</p>
488
489 <p>The second sense of being out of bounds is computing an address that's
490 beyond the actual underlying allocated object.</p>
491
492 <p>With the <tt>inbounds</tt> keyword, the result value of the GEP is
493 undefined if the address is outside the actual underlying allocated
494 object and not the address one-past-the-end.</p>
495
496 <p>Without the <tt>inbounds</tt> keyword, there are no restrictions
497 on computing out-of-bounds addresses. Obviously, performing a load or
498 a store requires an address of allocated and sufficiently aligned
499 memory. But the GEP itself is only concerned with computing addresses.</p>
500
501</div>
502
503<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000504<h3>
505 <a name="negative">Can array indices be negative?</a>
506</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000507<div class="doc_text">
508 <p>Yes. This is basically a special case of array indices being out
509 of bounds.</p>
510
511</div>
512
513<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000514<h3>
515 <a name="compare">Can I compare two values computed with GEPs?</a>
516</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000517<div class="doc_text">
518 <p>Yes. If both addresses are within the same allocated object, or
519 one-past-the-end, you'll get the comparison result you expect. If either
520 is outside of it, integer arithmetic wrapping may occur, so the
521 comparison may not be meaningful.</p>
522
523</div>
524
525<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000526<h3>
527 <a name="types">
528 Can I do GEP with a different pointer type than the type of
529 the underlying object?
530 </a>
531</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000532<div class="doc_text">
533 <p>Yes. There are no restrictions on bitcasting a pointer value to an arbitrary
534 pointer type. The types in a GEP serve only to define the parameters for the
535 underlying integer computation. They need not correspond with the actual
536 type of the underlying object.</p>
537
538 <p>Furthermore, loads and stores don't have to use the same types as the type
539 of the underlying object. Types in this context serve only to specify
540 memory size and alignment. Beyond that there are merely a hint to the
541 optimizer indicating how the value will likely be used.</p>
542
543</div>
544
545<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000546<h3>
547 <a name="null">
548 Can I cast an object's address to integer and add it to null?
549 </a>
550</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000551<div class="doc_text">
552 <p>You can compute an address that way, but if you use GEP to do the add,
553 you can't use that pointer to actually access the object, unless the
554 object is managed outside of LLVM.</p>
555
556 <p>The underlying integer computation is sufficiently defined; null has a
557 defined value -- zero -- and you can add whatever value you want to it.</p>
558
559 <p>However, it's invalid to access (load from or store to) an LLVM-aware
560 object with such a pointer. This includes GlobalVariables, Allocas, and
561 objects pointed to by noalias pointers.</p>
562
563 <p>If you really need this functionality, you can do the arithmetic with
564 explicit integer instructions, and use inttoptr to convert the result to
565 an address. Most of GEP's special aliasing rules do not apply to pointers
566 computed from ptrtoint, arithmetic, and inttoptr sequences.</p>
567
568</div>
569
570<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000571<h3>
572 <a name="ptrdiff">
573 Can I compute the distance between two objects, and add
574 that value to one address to compute the other address?
575 </a>
576</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000577<div class="doc_text">
578 <p>As with arithmetic on null, You can use GEP to compute an address that
579 way, but you can't use that pointer to actually access the object if you
580 do, unless the object is managed outside of LLVM.</p>
581
582 <p>Also as above, ptrtoint and inttoptr provide an alternative way to do this
583 which do not have this restriction.</p>
584
585</div>
586
587<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000588<h3>
589 <a name="tbaa">Can I do type-based alias analysis on LLVM IR?</a>
590</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000591<div class="doc_text">
592 <p>You can't do type-based alias analysis using LLVM's built-in type system,
593 because LLVM has no restrictions on mixing types in addressing, loads or
594 stores.</p>
595
596 <p>It would be possible to add special annotations to the IR, probably using
597 metadata, to describe a different type system (such as the C type system),
598 and do type-based aliasing on top of that. This is a much bigger
599 undertaking though.</p>
600
601</div>
602
603<!-- *********************************************************************** -->
604
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000605<h3>
606 <a name="overflow">What happens if a GEP computation overflows?</a>
607</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000608<div class="doc_text">
Chris Lattner776b7df2011-02-11 21:50:52 +0000609 <p>If the GEP lacks the <tt>inbounds</tt> keyword, the value is the result
610 from evaluating the implied two's complement integer computation. However,
611 since there's no guarantee of where an object will be allocated in the
612 address space, such values have limited meaning.</p>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000613
Chris Lattner776b7df2011-02-11 21:50:52 +0000614 <p>If the GEP has the <tt>inbounds</tt> keyword, the result value is
615 undefined (a "<a href="LangRef.html#trapvalues">trap value</a>") if the GEP
616 overflows (i.e. wraps around the end of the address space).</p>
617
618 <p>As such, there are some ramifications of this for inbounds GEPs: scales
619 implied by array/vector/pointer indices are always known to be "nsw" since
620 they are signed values that are scaled by the element size. These values
621 are also allowed to be negative (e.g. "gep i32 *%P, i32 -1") but the
622 pointer itself is logically treated as an unsigned value. This means that
623 GEPs have an asymmetric relation between the pointer base (which is treated
624 as unsigned) and the offset applied to it (which is treated as signed). The
625 result of the additions within the offset calculation cannot have signed
626 overflow, but when applied to the base pointer, there can be signed
627 overflow.
628 </p>
629
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000630
631</div>
632
633<!-- *********************************************************************** -->
634
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000635<h3>
636 <a name="check">
637 How can I tell if my front-end is following the rules?
638 </a>
639</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000640<div class="doc_text">
641 <p>There is currently no checker for the getelementptr rules. Currently,
642 the only way to do this is to manually check each place in your front-end
643 where GetElementPtr operators are created.</p>
644
645 <p>It's not possible to write a checker which could find all rule
646 violations statically. It would be possible to write a checker which
647 works by instrumenting the code with dynamic checks though. Alternatively,
648 it would be possible to write a static checker which catches a subset of
649 possible problems. However, no such checker exists today.</p>
650
651</div>
652
653<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000654<h2><a name="rationale">Rationale</a></h2>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000655<!-- *********************************************************************** -->
656
657<!-- *********************************************************************** -->
658
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000659<h3>
660 <a name="goals">Why is GEP designed this way?</a>
661</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000662<div class="doc_text">
663 <p>The design of GEP has the following goals, in rough unofficial
664 order of priority:</p>
665 <ul>
666 <li>Support C, C-like languages, and languages which can be
667 conceptually lowered into C (this covers a lot).</li>
668 <li>Support optimizations such as those that are common in
Dan Gohmanff70fe42010-07-06 15:26:33 +0000669 C compilers. In particular, GEP is a cornerstone of LLVM's
670 <a href="LangRef.html#pointeraliasing">pointer aliasing model</a>.</li>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000671 <li>Provide a consistent method for computing addresses so that
672 address computations don't need to be a part of load and
673 store instructions in the IR.</li>
674 <li>Support non-C-like languages, to the extent that it doesn't
675 interfere with other goals.</li>
676 <li>Minimize target-specific information in the IR.</li>
677 </ul>
678</div>
679
680<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000681<h3>
682 <a name="i32">Why do struct member indices always use i32?</a>
683</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000684<div class="doc_text">
685 <p>The specific type i32 is probably just a historical artifact, however it's
686 wide enough for all practical purposes, so there's been no need to change it.
687 It doesn't necessarily imply i32 address arithmetic; it's just an identifier
688 which identifies a field in a struct. Requiring that all struct indices be
689 the same reduces the range of possibilities for cases where two GEPs are
690 effectively the same but have distinct operand types.</p>
691
692</div>
693
694<!-- *********************************************************************** -->
695
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000696<h3>
697 <a name="uglygep">What's an uglygep?</a>
698</h3>
Dan Gohmanb02c08c2010-02-25 18:16:03 +0000699<div class="doc_text">
700 <p>Some LLVM optimizers operate on GEPs by internally lowering them into
701 more primitive integer expressions, which allows them to be combined
702 with other integer expressions and/or split into multiple separate
703 integer expressions. If they've made non-trivial changes, translating
704 back into LLVM IR can involve reverse-engineering the structure of
705 the addressing in order to fit it into the static type of the original
706 first operand. It isn't always possibly to fully reconstruct this
707 structure; sometimes the underlying addressing doesn't correspond with
708 the static type at all. In such cases the optimizer instead will emit
709 a GEP with the base pointer casted to a simple address-unit pointer,
710 using the name "uglygep". This isn't pretty, but it's just as
711 valid, and it's sufficient to preserve the pointer aliasing guarantees
712 that GEP provides.</p>
713
714</div>
715
716<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000717<h2><a name="summary">Summary</a></h2>
Reid Spencere00906f2006-08-10 20:15:58 +0000718<!-- *********************************************************************** -->
719
720<div class="doc_text">
721 <p>In summary, here's some things to always remember about the GetElementPtr
722 instruction:</p>
723 <ol>
724 <li>The GEP instruction never accesses memory, it only provides pointer
725 computations.</li>
726 <li>The first operand to the GEP instruction is always a pointer and it must
727 be indexed.</li>
728 <li>There are no superfluous indices for the GEP instruction.</li>
729 <li>Trailing zero indices are superfluous for pointer aliasing, but not for
730 the types of the pointers.</li>
731 <li>Leading zero indices are not superfluous for pointer aliasing nor the
732 types of the pointers.</li>
733 </ol>
734</div>
735
736<!-- *********************************************************************** -->
Reid Spencere00906f2006-08-10 20:15:58 +0000737
738<hr>
739<address>
740 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000741 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Reid Spencere00906f2006-08-10 20:15:58 +0000742 <a href="http://validator.w3.org/check/referer"><img
Misha Brukmanf00ddb02008-12-11 18:23:24 +0000743 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000744 <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br/>
Reid Spencere00906f2006-08-10 20:15:58 +0000745 Last modified: $Date$
746</address>
747</body>
748</html>