| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 1 | <!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 Spencer | ba02d8f | 2006-08-10 21:01:14 +0000 | [diff] [blame] | 8 | <style type="text/css"> | 
|  | 9 | TABLE   { text-align: left; border: 1px solid black; border-collapse: collapse; margin: 0 0 0 0; } | 
|  | 10 | </style> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 11 | </head> | 
|  | 12 | <body> | 
|  | 13 |  | 
|  | 14 | <div class="doc_title"> | 
|  | 15 | The Often Misunderstood GEP Instruction | 
|  | 16 | </div> | 
|  | 17 |  | 
|  | 18 | <ol> | 
|  | 19 | <li><a href="#intro">Introduction</a></li> | 
|  | 20 | <li><a href="#questions">The Questions</a> | 
|  | 21 | <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> | 
|  | 28 | </ol></li> | 
|  | 29 | <li><a href="#summary">Summary</a></li> | 
|  | 30 | </ol> | 
|  | 31 |  | 
|  | 32 | <div class="doc_author"> | 
|  | 33 | <p>Written by: <a href="mailto:rspencer@reidspencer.com">Reid Spencer</a>.</p> | 
|  | 34 | </div> | 
|  | 35 |  | 
|  | 36 |  | 
|  | 37 | <!-- *********************************************************************** --> | 
|  | 38 | <div class="doc_section"><a name="intro"><b>Introduction</b></a></div> | 
|  | 39 | <!-- *********************************************************************** --> | 
|  | 40 | <div class="doc_text"> | 
|  | 41 | <p>This document seeks to dispel the mystery and confusion surrounding LLVM's | 
|  | 42 | GetElementPtr (GEP) instruction. Questions about the wiley GEP instruction are | 
|  | 43 | probably the most frequently occuring questions once a developer gets down to | 
|  | 44 | coding with LLVM. Here we lay out the sources of confusion and show that the | 
|  | 45 | GEP instruction is really quite simple. | 
|  | 46 | </p> | 
|  | 47 | </div> | 
|  | 48 |  | 
|  | 49 | <!-- *********************************************************************** --> | 
|  | 50 | <div class="doc_section"><a name="questions"><b>The Questions</b></a></div> | 
|  | 51 | <!-- *********************************************************************** --> | 
|  | 52 | <div class="doc_text"> | 
|  | 53 | <p>When people are first confronted with the GEP instruction, they tend to | 
|  | 54 | relate it to known concepts from other programming paradigms, most notably C | 
|  | 55 | array indexing and field selection. However, GEP is a little different and | 
|  | 56 | this leads to the following questions, all of which are answered in the | 
|  | 57 | following sections.</p> | 
|  | 58 | <ol> | 
| Jim Laskey | 6cef9de | 2006-08-15 12:11:42 +0000 | [diff] [blame] | 59 | <li><a href="#firstptr">What is the first index of the GEP instruction?</a> | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 60 | </li> | 
| Jim Laskey | 6cef9de | 2006-08-15 12:11:42 +0000 | [diff] [blame] | 61 | <li><a href="#extra_index">Why is the extra 0 index required?</a></li> | 
|  | 62 | <li><a href="#deref">What is dereferenced by GEP?</a></li> | 
|  | 63 | <li><a href="#lead0">Why don't GEP x,0,0,1 and GEP x,1 alias? </a></li> | 
|  | 64 | <li><a href="#trail0">Why do GEP x,1,0,0 and GEP x,1 alias? </a></li> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 65 | </ol> | 
|  | 66 | </div> | 
|  | 67 |  | 
|  | 68 | <!-- *********************************************************************** --> | 
|  | 69 | <div class="doc_subsection"> | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 70 | <a name="firstptr"><b>What is the first index of the GEP instruction?</b></a> | 
|  | 71 | </div> | 
|  | 72 | <div class="doc_text"> | 
| Reid Spencer | 6500f40 | 2006-08-15 03:43:31 +0000 | [diff] [blame] | 73 | <p>Quick answer: The index stepping through the first operand.</p> | 
|  | 74 | <p>The confusion with the first index usually arises from thinking about | 
|  | 75 | the GetElementPtr instruction as if it was a C index operator. They aren't the | 
|  | 76 | same. For example, when we write, in "C":</p> | 
|  | 77 | <pre> | 
|  | 78 | AType* Foo; | 
|  | 79 | ... | 
| Reid Spencer | 334e26b | 2006-08-15 03:57:05 +0000 | [diff] [blame] | 80 | X = &Foo->F;</pre> | 
| Reid Spencer | c438e08 | 2006-08-15 04:00:29 +0000 | [diff] [blame] | 81 | <p>it is natural to think that there is only one index, the selection of the | 
|  | 82 | field <tt>F</tt>.  However, in this example, <tt>Foo</tt> is a pointer. That | 
|  | 83 | pointer must be indexed explicitly in LLVM. C, on the other hand, indexs | 
| Jim Laskey | 00389a8 | 2006-08-15 08:14:19 +0000 | [diff] [blame] | 84 | through it transparently.  To arrive at the same address location as the C | 
| Reid Spencer | c438e08 | 2006-08-15 04:00:29 +0000 | [diff] [blame] | 85 | code, you would provide the GEP instruction with two index operands. The | 
|  | 86 | first operand indexes through the pointer; the second operand indexes the | 
|  | 87 | field <tt>F</tt> of the structure, just as if you wrote:</p> | 
| Reid Spencer | 334e26b | 2006-08-15 03:57:05 +0000 | [diff] [blame] | 88 | <pre> | 
|  | 89 | X = &Foo[0].F;</pre> | 
| Reid Spencer | 6500f40 | 2006-08-15 03:43:31 +0000 | [diff] [blame] | 90 | <p>Sometimes this question gets rephrased as:</p> | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 91 | <blockquote><i>Why is it okay to index through the first pointer, but | 
|  | 92 | subsequent pointers won't be dereferenced?</i></blockquote> | 
|  | 93 | <p>The answer is simply because memory does not have to be accessed to | 
|  | 94 | perform the computation. The first operand to the GEP instruction must be a | 
|  | 95 | value of a pointer type. The value of the pointer is provided directly to | 
| Reid Spencer | 334e26b | 2006-08-15 03:57:05 +0000 | [diff] [blame] | 96 | the GEP instruction as an operand without any need for accessing memory. It | 
|  | 97 | must, therefore be indexed and requires an index operand. Consider this | 
|  | 98 | example:</p> | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 99 | <pre> | 
|  | 100 | struct munger_struct { | 
|  | 101 | int f1; | 
|  | 102 | int f2; | 
|  | 103 | }; | 
|  | 104 | void munge(struct munger_struct *P) | 
|  | 105 | { | 
|  | 106 | P[0].f1 = P[1].f1 + P[2].f2; | 
|  | 107 | } | 
|  | 108 | ... | 
| Reid Spencer | 22bb5c3 | 2006-08-15 03:46:38 +0000 | [diff] [blame] | 109 | munger_struct Array[3]; | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 110 | ... | 
|  | 111 | munge(Array);</pre> | 
|  | 112 | <p>In this "C" example, the front end compiler (llvm-gcc) will generate three | 
|  | 113 | GEP instructions for the three indices through "P" in the assignment | 
|  | 114 | statement.  The function argument <tt>P</tt> will be the first operand of each | 
|  | 115 | of these GEP instructions.  The second operand will be the field offset into | 
|  | 116 | the <tt>struct munger_struct</tt> type,  for either the <tt>f1</tt> or | 
|  | 117 | <tt>f2</tt> field. So, in LLVM assembly the <tt>munge</tt> function looks | 
|  | 118 | like:</p> | 
|  | 119 | <pre> | 
|  | 120 | void %munge(%struct.munger_struct* %P) { | 
|  | 121 | entry: | 
|  | 122 | %tmp = getelementptr %struct.munger_struct* %P, int 1, uint 0 | 
|  | 123 | %tmp = load int* %tmp | 
|  | 124 | %tmp6 = getelementptr %struct.munger_struct* %P, int 2, uint 1 | 
|  | 125 | %tmp7 = load int* %tmp6 | 
|  | 126 | %tmp8 = add int %tmp7, %tmp | 
|  | 127 | %tmp9 = getelementptr %struct.munger_struct* %P, int 0, uint 0 | 
|  | 128 | store int %tmp8, int* %tmp9 | 
|  | 129 | ret void | 
|  | 130 | }</pre> | 
|  | 131 | <p>In each case the first operand is the pointer through which the GEP | 
|  | 132 | instruction starts. The same is true whether the first operand is an | 
|  | 133 | argument, allocated memory, or a global variable. </p> | 
|  | 134 | <p>To make this clear, let's consider a more obtuse example:</p> | 
|  | 135 | <pre> | 
|  | 136 | %MyVar = unintialized global int | 
|  | 137 | ... | 
|  | 138 | %idx1 = getelementptr int* %MyVar, long 0 | 
|  | 139 | %idx2 = getelementptr int* %MyVar, long 1 | 
|  | 140 | %idx3 = getelementptr int* %MyVar, long 2</pre> | 
|  | 141 | <p>These GEP instructions are simply making address computations from the | 
|  | 142 | base address of <tt>MyVar</tt>.  They compute, as follows (using C syntax): | 
|  | 143 | </p> | 
|  | 144 | <ul> | 
|  | 145 | <li> idx1 = (char*) &MyVar + 0</li> | 
|  | 146 | <li> idx2 = (char*) &MyVar + 4</li> | 
|  | 147 | <li> idx3 = (char*) &MyVar + 8</li> | 
|  | 148 | </ul> | 
|  | 149 | <p>Since the type <tt>int</tt> is known to be four bytes long, the indices | 
|  | 150 | 0, 1 and 2 translate into memory offsets of 0, 4, and 8, respectively. No | 
|  | 151 | memory is accessed to make these computations because the address of | 
|  | 152 | <tt>%MyVar</tt> is passed directly to the GEP instructions.</p> | 
|  | 153 | <p>The obtuse part of this example is in the cases of <tt>%idx2</tt> and | 
|  | 154 | <tt>%idx3</tt>. They result in the computation of addresses that point to | 
|  | 155 | memory past the end of the <tt>%MyVar</tt> global, which is only one | 
|  | 156 | <tt>int</tt> long, not three <tt>int</tt>s long.  While this is legal in LLVM, | 
|  | 157 | it is inadvisable because any load or store with the pointer that results | 
|  | 158 | from these GEP instructions would produce undefined results.</p> | 
|  | 159 | </div> | 
|  | 160 |  | 
|  | 161 | <!-- *********************************************************************** --> | 
|  | 162 | <div class="doc_subsection"> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 163 | <a name="extra_index"><b>Why is the extra 0 index required?</b></a> | 
|  | 164 | </div> | 
|  | 165 | <!-- *********************************************************************** --> | 
|  | 166 | <div class="doc_text"> | 
|  | 167 | <p>Quick answer: there are no superfluous indices.</p> | 
|  | 168 | <p>This question arises most often when the GEP instruction is applied to a | 
|  | 169 | global variable which is always a pointer type. For example, consider | 
|  | 170 | this:</p><pre> | 
|  | 171 | %MyStruct = uninitialized global { float*, int } | 
|  | 172 | ... | 
|  | 173 | %idx = getelementptr { float*, int }* %MyStruct, long 0, ubyte 1</pre> | 
|  | 174 | <p>The GEP above yields an <tt>int*</tt> by indexing the <tt>int</tt> typed | 
|  | 175 | field of the structure <tt>%MyStruct</tt>. When people first look at it, they | 
|  | 176 | wonder why the <tt>long 0</tt> index is needed. However, a closer inspection | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 177 | of how globals and GEPs work reveals the need. Becoming aware of the following | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 178 | facts will dispell the confusion:</p> | 
|  | 179 | <ol> | 
|  | 180 | <li>The type of <tt>%MyStruct</tt> is <i>not</i> <tt>{ float*, int }</tt> | 
|  | 181 | but rather <tt>{ float*, int }*</tt>. That is, <tt>%MyStruct</tt> is a | 
|  | 182 | pointer to a structure containing a pointer to a <tt>float</tt> and an | 
|  | 183 | <tt>int</tt>.</li> | 
|  | 184 | <li>Point #1 is evidenced by noticing the type of the first operand of | 
|  | 185 | the GEP instruction (<tt>%MyStruct</tt>) which is | 
|  | 186 | <tt>{ float*, int }*</tt>.</li> | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 187 | <li>The first index, <tt>long 0</tt> is required to step over the global | 
|  | 188 | variable <tt>%MyStruct</tt>.  Since the first argument to the GEP | 
|  | 189 | instruction must always be a value of pointer type, the first index | 
|  | 190 | steps through that pointer. A value of 0 means 0 elements offset from that | 
|  | 191 | pointer.</li> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 192 | <li>The second index, <tt>ubyte 1</tt> selects the second field of the | 
|  | 193 | structure (the <tt>int</tt>). </li> | 
|  | 194 | </ol> | 
|  | 195 | </div> | 
|  | 196 |  | 
|  | 197 | <!-- *********************************************************************** --> | 
|  | 198 | <div class="doc_subsection"> | 
|  | 199 | <a name="deref"><b>What is dereferenced by GEP?</b></a> | 
|  | 200 | </div> | 
|  | 201 | <div class="doc_text"> | 
|  | 202 | <p>Quick answer: nothing.</p> | 
|  | 203 | <p>The GetElementPtr instruction dereferences nothing. That is, it doesn't | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 204 | access memory in any way. That's what the Load and Store instructions are for. | 
|  | 205 | GEP is only involved in the computation of addresses. For example, consider | 
|  | 206 | this:</p> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 207 | <pre> | 
|  | 208 | %MyVar = uninitialized global { [40 x int ]* } | 
|  | 209 | ... | 
|  | 210 | %idx = getelementptr { [40 x int]* }* %MyVar, long 0, ubyte 0, long 0, long 17</pre> | 
|  | 211 | <p>In this example, we have a global variable, <tt>%MyVar</tt> that is a | 
|  | 212 | pointer to a structure containing a pointer to an array of 40 ints. The | 
| Reid Spencer | 6500f40 | 2006-08-15 03:43:31 +0000 | [diff] [blame] | 213 | GEP instruction seems to be accessing the 18th integer of the structure's | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 214 | array of ints. However, this is actually an illegal GEP instruction. It | 
|  | 215 | won't compile. The reason is that the pointer in the structure <i>must</i> | 
|  | 216 | be dereferenced in order to index into the array of 40 ints. Since the | 
|  | 217 | GEP instruction never accesses memory, it is illegal.</p> | 
|  | 218 | <p>In order to access the 18th integer in the array, you would need to do the | 
|  | 219 | following:</p> | 
|  | 220 | <pre> | 
|  | 221 | %idx = getelementptr { [40 x int]* }* %, long 0, ubyte 0 | 
|  | 222 | %arr = load [40 x int]** %idx | 
|  | 223 | %idx = getelementptr [40 x int]* %arr, long 0, long 17</pre> | 
|  | 224 | <p>In this case, we have to load the pointer in the structure with a load | 
|  | 225 | instruction before we can index into the array. If the example was changed | 
|  | 226 | to:</p> | 
|  | 227 | <pre> | 
|  | 228 | %MyVar = uninitialized global { [40 x int ] } | 
|  | 229 | ... | 
|  | 230 | %idx = getelementptr { [40 x int] }*, long 0, ubyte 0, long 17</pre> | 
|  | 231 | <p>then everything works fine. In this case, the structure does not contain a | 
| Reid Spencer | 6500f40 | 2006-08-15 03:43:31 +0000 | [diff] [blame] | 232 | pointer and the GEP instruction can index through the global variable, | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 233 | into the first field of the structure and access the 18th <tt>int</tt> in the | 
|  | 234 | array there.</p> | 
|  | 235 | </div> | 
|  | 236 |  | 
|  | 237 | <!-- *********************************************************************** --> | 
|  | 238 | <div class="doc_subsection"> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 239 | <a name="lead0"><b>Why don't GEP x,0,0,1 and GEP x,1 alias?</b></a> | 
|  | 240 | </div> | 
|  | 241 | <div class="doc_text"> | 
|  | 242 | <p>Quick Answer: They compute different address locations.</p> | 
|  | 243 | <p>If you look at the first indices in these GEP | 
|  | 244 | instructions you find that they are different (0 and 1), therefore the address | 
|  | 245 | computation diverges with that index. Consider this example:</p> | 
|  | 246 | <pre> | 
|  | 247 | %MyVar = global { [10 x int ] } | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 248 | %idx1 = getlementptr { [10 x int ] }* %MyVar, long 0, ubyte 0, long 1 | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 249 | %idx2 = getlementptr { [10 x int ] }* %MyVar, long 1</pre> | 
|  | 250 | <p>In this example, <tt>idx1</tt> computes the address of the second integer | 
|  | 251 | in the array that is in the structure in %MyVar, that is <tt>MyVar+4</tt>. The | 
|  | 252 | type of <tt>idx1</tt> is <tt>int*</tt>. However, <tt>idx2</tt> computes the | 
|  | 253 | address of <i>the next</i> structure after <tt>%MyVar</tt>. The type of | 
|  | 254 | <tt>idx2</tt> is <tt>{ [10 x int] }*</tt> and its value is equivalent | 
|  | 255 | to <tt>MyVar + 40</tt> because it indexes past the ten 4-byte integers | 
|  | 256 | in <tt>MyVar</tt>. Obviously, in such a situation, the pointers don't | 
|  | 257 | alias.</p> | 
|  | 258 | </div> | 
|  | 259 |  | 
|  | 260 | <!-- *********************************************************************** --> | 
|  | 261 | <div class="doc_subsection"> | 
| Jim Laskey | 6047ec97e | 2006-08-15 12:15:08 +0000 | [diff] [blame^] | 262 | <a name="trail0"><b>Why do GEP x,1,0,0 and GEP x,1 alias?</b></a> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 263 | </div> | 
|  | 264 | <div class="doc_text"> | 
|  | 265 | <p>Quick Answer: They compute the same address location.</p> | 
|  | 266 | <p>These two GEP instructions will compute the same address because indexing | 
|  | 267 | through the 0th element does not change the address. However, it does change | 
|  | 268 | the type. Consider this example:</p> | 
|  | 269 | <pre> | 
|  | 270 | %MyVar = global { [10 x int ] } | 
| Reid Spencer | afa58f1 | 2006-08-15 03:32:10 +0000 | [diff] [blame] | 271 | %idx1 = getlementptr { [10 x int ] }* %MyVar, long 1, ubyte 0, long 0 | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 272 | %idx2 = getlementptr { [10 x int ] }* %MyVar, long 1</pre> | 
|  | 273 | <p>In this example, the value of <tt>%idx1</tt> is <tt>%MyVar+40</tt> and | 
|  | 274 | its type is <tt>int*</tt>. The value of <tt>%idx2</tt> is also | 
|  | 275 | <tt>MyVar+40</tt> but its type is <tt>{ [10 x int] }*</tt>.</p> | 
|  | 276 | </div> | 
|  | 277 |  | 
|  | 278 | <!-- *********************************************************************** --> | 
|  | 279 | <div class="doc_section"><a name="summary"><b>Summary</b></a></div> | 
|  | 280 | <!-- *********************************************************************** --> | 
|  | 281 |  | 
|  | 282 | <div class="doc_text"> | 
|  | 283 | <p>In summary, here's some things to always remember about the GetElementPtr | 
|  | 284 | instruction:</p> | 
|  | 285 | <ol> | 
|  | 286 | <li>The GEP instruction never accesses memory, it only provides pointer | 
|  | 287 | computations.</li> | 
|  | 288 | <li>The first operand to the GEP instruction is always a pointer and it must | 
|  | 289 | be indexed.</li> | 
|  | 290 | <li>There are no superfluous indices for the GEP instruction.</li> | 
|  | 291 | <li>Trailing zero indices are superfluous for pointer aliasing, but not for | 
|  | 292 | the types of the pointers.</li> | 
|  | 293 | <li>Leading zero indices are not superfluous for pointer aliasing nor the | 
|  | 294 | types of the pointers.</li> | 
|  | 295 | </ol> | 
|  | 296 | </div> | 
|  | 297 |  | 
|  | 298 | <!-- *********************************************************************** --> | 
| Reid Spencer | ba02d8f | 2006-08-10 21:01:14 +0000 | [diff] [blame] | 299 | <div class="doc_section"><a name="discussion"><b>Appendix: Discussion</b></a></div> | 
|  | 300 | <!-- *********************************************************************** --> | 
|  | 301 | <div class="doc_text"> | 
|  | 302 | <p>The following is a real discussion from the | 
|  | 303 | <a href="irc://irc.oftc.net/#llvm">#llvm IRC channel</a> about the GEP | 
|  | 304 | instruction. You may find this instructive as it was the basis for this | 
|  | 305 | document.</p> | 
|  | 306 | <table> | 
|  | 307 | <tr><th>User</th><th>Comment</th></tr> | 
|  | 308 | <tr><td>Yorion</td><td>If x & y must alias, are  [ getelementptr x,0,0,1,2 ] and [ getelementptr x,1,2 ] aliased? (they obviously have different types, but they should alias...)</td></tr> | 
|  | 309 | <tr><td>Yorion</td><td>oops, for the second one I meant [ getelementptr y,1,2 ]</td></tr> | 
|  | 310 | <tr><td>Reid</td><td>I don't see how that could be, Yorion but I'm not the authority on this</td></tr> | 
|  | 311 | <tr><td>Yorion</td><td>hmm.. </td></tr> | 
|  | 312 | <tr><td>Reid</td><td>the two geps, by definition, are going to produce different pointers which are not aliased</td></tr> | 
|  | 313 | <tr><td>Yorion</td><td>would [ GEP x,1,0 ] and [ GEP y,1 ] be aliased?</td></tr> | 
|  | 314 | <tr><td>Reid</td><td>if the second gep was [gep y,0,0,1,2] then they should be aliased as well</td></tr> | 
|  | 315 | <tr><td>Reid</td><td>no, I wouldn't expect that to work either :)</td></tr> | 
|  | 316 | <tr><td>Reid</td><td>you can't just arbitrarily drop leading or trailing indices :)</td></tr> | 
|  | 317 | <tr><td>Reid</td><td>(.. leading or trailing 0 indices, I mean)</td></tr> | 
|  | 318 | <tr><td>Reid</td><td>this instruction walks through a data structure and generates a pointer to the resulting thing</td></tr> | 
|  | 319 | <tr><td>Reid</td><td>if the number of indices are different, you're ending up at a different place and by definition they'll have different addresses</td></tr> | 
|  | 320 | <tr><td>Yorion</td><td>oh, I see, because of different types, [ GEP x,0,1 ] | 
|  | 321 | & [ GEP x,1 ] actually might refer to different fields, but might also refer to the same ones... </td></tr> | 
|  | 322 | <tr><td>Reid</td><td>or, at least, that's my crude understanding of it :)</td></tr> | 
|  | 323 | <tr><td>Reid</td><td>no, they'll definitely refer to different fields</td></tr> | 
|  | 324 | <tr><td>nicholas</td><td>GEP x,0,1 ==> &((*(x+0))+1)? vs. GEP x,1 ==> &(*(x+1))?</td></tr> | 
|  | 325 | <tr><td>Reid</td><td>lemme grok that for a sec</td></tr> | 
|  | 326 | <tr><td>Reid</td><td>that might be true in some limited definition of x, but it wouldn't be generally</td></tr> | 
|  | 327 | <tr><td>nicholas</td><td>oh. fields of different sizes in a structure.</td></tr> | 
|  | 328 | <tr><td>Reid</td><td>yup</td></tr> | 
|  | 329 | <tr><td>Yorion</td><td>is perhaps the type unification the reason why [ GEP x,0,1 ] and [ GEP x,1 ] cannot alias?</td></tr> | 
|  | 330 | <tr><td>Reid</td><td>no</td></tr> | 
|  | 331 | <tr><td>Reid</td><td>they may or may not have the same type, but they are definitely different pointers</td></tr> | 
|  | 332 | <tr><td>Reid</td><td>lets use a concrete example for "x"</td></tr> | 
|  | 333 | <tr><td>Reid</td><td>suppose x is "struct {int a, float b} *"</td></tr> | 
|  | 334 | <tr><td>Reid</td><td>GEP X,0,1 is going to return the address of b</td></tr> | 
|  | 335 | <tr><td>Reid</td><td>GEP X,1 is going to return the address of the *second* "a" (after the first b)</td></tr> | 
|  | 336 | <tr><td>Yorion</td><td>ah, I see... </td></tr> | 
|  | 337 | <tr><td>Yorion</td><td>trailing zeros are still a bit confusing... </td></tr> | 
|  | 338 | <tr><td>Reid</td><td>same thing .. you're just selecting the 0th member of an array or structure</td></tr> | 
|  | 339 | <tr><td>Yorion</td><td>you don't move away from the pointer, only the type is changed</td></tr> | 
|  | 340 | <tr><td>Reid</td><td>no, you still move away from the pointer .. the type might change, or not</td></tr> | 
|  | 341 | <tr><td>Reid</td><td>the pointer definitely changes</td></tr> | 
|  | 342 | <tr><td>Reid</td><td>lets look at an example for trailing zero</td></tr> | 
|  | 343 | <tr><td>Reid</td><td>suppose x is "int x[10][10][10][10]" (in C)</td></tr> | 
|  | 344 | <tr><td>Reid</td><td>GEP X,0,0 will yield you a 3 dimensional array</td></tr> | 
|  | 345 | <tr><td>Reid</td><td>GEP X,0,0,0,0,0 will yield you an "int"</td></tr> | 
|  | 346 | <tr><td>Reid</td><td>make sense?</td></tr> | 
|  | 347 | <tr><td>Yorion</td><td>yes</td></tr> | 
|  | 348 | <tr><td>Reid</td><td>so, I think there's a law here: if the number of indices in two GEP instructions are not equivalent, there is no way the resulting pointers can alias</td></tr> | 
|  | 349 | <tr><td>Reid</td><td>(assuming the x and y alias)</td></tr> | 
|  | 350 | <tr><td>Yorion</td><td>I was confused with some code in BasicAliasAnalysis that says that two pointers are equal if they differ only in trailing zeros</td></tr> | 
|  | 351 | <tr><td>Yorion</td><td>BasicAliasAnalysis.cpp:504-518</td></tr> | 
|  | 352 | <tr><td>Reid</td><td>lemme look</td></tr> | 
|  | 353 | <tr><td>nicholas</td><td>if y1 = GEP X, 0, 0 and y2 = GEP X, 0, 0, 0, 0, 0 (from Reid's example)</td></tr> | 
|  | 354 | <tr><td>nicholas</td><td>then doesn't *y1 and *y2 both refer to the same "int"?</td></tr> | 
|  | 355 | <tr><td>Reid</td><td>they shouldn't</td></tr> | 
|  | 356 | <tr><td>Reid</td><td>hmm .. actually, maybe you're right :)</td></tr> | 
|  | 357 | <tr><td>Reid</td><td>they definitely have different *types*</td></tr> | 
|  | 358 | <tr><td>Yorion</td><td>true</td></tr> | 
|  | 359 | <tr><td>nicholas</td><td>different types just doesn't cut it. :)</td></tr> | 
|  | 360 | <tr><td>Reid</td><td>.. thinking on this :)</td></tr> | 
|  | 361 | <tr><td>nicholas</td><td>similarly, i could create a yucky with a struct that has a char *, then have you GEP right through the pointer into the pointed-to data. That could mean that the resulting point might alias anything.</td></tr> | 
|  | 362 | <tr><td>Yorion</td><td>my theory (after reading BAA) is that all zeros can be omitted, and that the pointers alias if they have the same sequence of indices</td></tr> | 
|  | 363 | <tr><td>Yorion</td><td>however, this screws the typing, so that's why zeros are for</td></tr> | 
|  | 364 | <tr><td>Yorion</td><td>nicholas, does that match your hunch?</td></tr> | 
|  | 365 | <tr><td>nicholas</td><td>I have to admit, I've had much grief with GEPIs already. I wish the semantics were plainly documented as part of their own language, instead of just relying on C aliasing rules and C semantics...</td></tr> | 
|  | 366 | <tr><td>nicholas</td><td>Yorion: leading zeroes can't be omitted.</td></tr> | 
|  | 367 | <tr><td>Reid</td><td>okay, if you have two GEPs and their leading indices are an exact match, if the one with more indices only has trailing 0s then they should alias</td></tr> | 
|  | 368 | <tr><td>nicholas</td><td>must alias, i think.</td></tr> | 
|  | 369 | <tr><td>Reid</td><td>yes, must alias, sorry</td></tr> | 
|  | 370 | <tr><td>Yorion</td><td>okay</td></tr> | 
|  | 371 | <tr><td>Yorion</td><td>I'm glad we cleared this up</td></tr> | 
|  | 372 | <tr><td>Reid</td><td>so, if y1 = GEP X, 1,2,0  and if y2 = GEP X, 1,2,0,0,0  then y1 "must alias" y2 :)</td></tr> | 
|  | 373 | <tr><td>Reid</td><td>but that doesn't work for leading 0s :)</td></tr> | 
|  | 374 | <tr><td>Yorion</td><td>yes, true... I was wrong </td></tr> | 
|  | 375 | <tr><td>Reid</td><td>I too have been having fun with GEP recently :)</td></tr> | 
|  | 376 | <tr><td>Yorion</td><td>but, there're cases like    [a = GEP x,1,0; b = GEP a,1,0; c = GEP b,1,0], and that should be equivalent to GEP x,1,0,1,0,1</td></tr> | 
|  | 377 | <tr><td>Reid</td><td>not quite</td></tr> | 
|  | 378 | <tr><td>nicholas</td><td>I'm sure another rule can be written for GEPIs, but they would only apply to type-safe code.</td></tr> | 
|  | 379 | <tr><td>nicholas</td><td>another *tautology</td></tr> | 
|  | 380 | <tr><td>Yorion</td><td>Reid: why not, only the type should be different...</td></tr> | 
|  | 381 | <tr><td>Reid</td><td>its should be equivalent to GEP x,1,0,1,0,1,0</td></tr> | 
|  | 382 | <tr><td>Yorion</td><td>and that must alias GEP x,1,0,1,0,1</td></tr> | 
|  | 383 | <tr><td>Reid</td><td>hmm, by the previous rule, I guess you're right :)</td></tr> | 
|  | 384 | <tr><td>Yorion</td><td>I'm a bit scared that even you're a bit confused about GEP</td></tr> | 
|  | 385 | <tr><td>Reid</td><td>I'm glad I'm not the only one that gets a little confused wrapping my head around this stuff :)</td></tr> | 
|  | 386 | <tr><td>Reid</td><td>GEP has always confused me .. partly because I think its wrong :)</td></tr> | 
|  | 387 | <tr><td>Reid</td><td>well, actually, not so much that GEP is wrong, but that gvars being pointers without storage</td></tr> | 
|  | 388 | <tr><td>Reid</td><td>i.e. when you say "%x = global int" in LLVM, the type of X is int*</td></tr> | 
|  | 389 | <tr><td>Reid</td><td>yet, there is no storage for that pointer</td></tr> | 
|  | 390 | <tr><td>Reid</td><td>its magically deduced</td></tr> | 
|  | 391 | <tr><td>nicholas</td><td>well, it makes no sense to have globals be SSA...</td></tr> | 
|  | 392 | <tr><td>Reid</td><td>heh</td></tr> | 
|  | 393 | <tr><td>Reid</td><td>yeah, well .. practicalities :)</td></tr> | 
|  | 394 | <tr><td>Yorion</td><td>true</td></tr> | 
|  | 395 | <tr><td>Yorion</td><td>sabre gave me a reference on how globals are handled in SSA</td></tr> | 
|  | 396 | <tr><td>Reid</td><td>anyway, gotta run</td></tr> | 
|  | 397 | <tr><td>Yorion</td><td>the paper was crappy, but I do understand now why is it implemented that way in LLVM</td></tr> | 
|  | 398 | <tr><td>Yorion</td><td>thx for the interesting discussion Reid</td></tr> | 
|  | 399 | <tr><td>Reid</td><td>heh .. its one that Chris and I keep having .. he just tells me that C has rotted my brain :)</td></tr> | 
|  | 400 | <tr><td>nicholas</td><td>lol</td></tr> | 
|  | 401 | <tr><td>Yorion</td><td>hehehe</td></tr> | 
|  | 402 | <tr><td>Reid</td><td>he might be right :)</td></tr> | 
|  | 403 | <tr><td>Yorion</td><td>sabre: have you seen the discussion on GEP?</td></tr> | 
|  | 404 | <tr><td>sabre</td><td>no</td></tr> | 
|  | 405 | <tr><td>sabre</td><td>I'll read the backlog, j/s</td></tr> | 
|  | 406 | <tr><td>sabre</td><td>ok, there's a lot</td></tr> | 
|  | 407 | <tr><td>sabre</td><td>what's the executive summary?</td></tr> | 
|  | 408 | <tr><td>sabre</td><td>do you have a q?</td></tr> | 
|  | 409 | <tr><td>Yorion</td><td>is it possible that GEP x,0,0,1 and GEP x,1 alias?</td></tr> | 
|  | 410 | <tr><td>sabre</td><td>no</td></tr> | 
|  | 411 | <tr><td>Yorion</td><td>and b) GEP x,1,0,0 and GEP x,1  should alias, right?</td></tr> | 
|  | 412 | <tr><td>sabre</td><td>I assume you mean for size = 1 ?</td></tr> | 
|  | 413 | <tr><td>sabre</td><td>b) yes</td></tr> | 
|  | 414 | <tr><td>Yorion</td><td>although they have different types</td></tr> | 
|  | 415 | <tr><td>sabre</td><td>right</td></tr> | 
|  | 416 | <tr><td>Yorion</td><td>okay</td></tr> | 
|  | 417 | <tr><td>Yorion</td><td>I'm still not 100% convinced that: a=GEP x,1,0; b=GEP a,1,0; c=GEP b,1,0 cannot alias  Z=GEP x,1,1,1</td></tr> | 
|  | 418 | <tr><td>Yorion</td><td>(that c and z cannot alias)</td></tr> | 
|  | 419 | <tr><td>sabre</td><td>that's becuse they do alias</td></tr> | 
|  | 420 | <tr><td>sabre</td><td>mustalias in fact</td></tr> | 
|  | 421 | <tr><td>Yorion</td><td>but then: GEP x,1,0,1,0,1,0 = GEP x,1,1,1</td></tr> | 
|  | 422 | <tr><td>sabre</td><td>Yorion: no</td></tr> | 
|  | 423 | <tr><td>sabre</td><td>c != GEP x,1,0,1,0,1,0</td></tr> | 
|  | 424 | <tr><td>sabre</td><td>the first index doesn't work like that</td></tr> | 
|  | 425 | <tr><td>Yorion</td><td>how does then the first index work? c and z must alias, but GEP x,1,0,1,0 != GEP x,1,1 ??</td></tr> | 
|  | 426 | <tr><td>sabre</td><td>*sigh*</td></tr> | 
|  | 427 | <tr><td>Reid</td><td>:)</td></tr> | 
|  | 428 | <tr><td>Reid</td><td>we need an FAQ on this</td></tr> | 
|  | 429 | <tr><td>sabre</td><td>Yorion: how did you get </td></tr> | 
|  | 430 | <tr><td>sabre</td><td>"GEP x,1,0,1,0"? </td></tr> | 
|  | 431 | <tr><td>Yorion</td><td>look</td></tr> | 
|  | 432 | <tr><td>sabre</td><td>you can't just concatenate subscripts</td></tr> | 
|  | 433 | <tr><td>Yorion</td><td>why?</td></tr> | 
|  | 434 | <tr><td>sabre</td><td>because... it doesn't work that way?</td></tr> | 
|  | 435 | <tr><td>sabre</td><td>consider C</td></tr> | 
|  | 436 | <tr><td>Yorion</td><td>how does it work?</td></tr> | 
|  | 437 | <tr><td>sabre</td><td>if I have blah* P</td></tr> | 
|  | 438 | <tr><td>sabre</td><td>P[0][1][2][3][4]</td></tr> | 
|  | 439 | <tr><td>sabre</td><td>this is *not* the same as:</td></tr> | 
|  | 440 | <tr><td>sabre</td><td>t = &P[0][1][2]   ... t[3][4]</td></tr> | 
|  | 441 | <tr><td>sabre</td><td>Yorion: Consider: struct *P </td></tr> | 
|  | 442 | <tr><td>sabre</td><td>P->X  == P[0].X</td></tr> | 
|  | 443 | <tr><td>sabre</td><td>You're losing the 0.</td></tr> | 
|  | 444 | <tr><td>sabre</td><td>P->X->Y == P[0].X[0].Y</td></tr> | 
|  | 445 | <tr><td>sabre</td><td>Not P.X.Y</td></tr> | 
|  | 446 | <tr><td>sabre</td><td>actually that's a bad analogy</td></tr> | 
|  | 447 | <tr><td>sabre</td><td>because C dereferences in this case</td></tr> | 
|  | 448 | <tr><td>sabre</td><td>Try: (&(P->X))->Y</td></tr> | 
|  | 449 | <tr><td>Yorion</td><td>so, a=GEP x,1,0; b=GEP a,1,0; c=GEP b,1,0, can you construct the definition of c in terms of x?</td></tr> | 
|  | 450 | <tr><td>sabre</td><td>yes, but you're going out of bounds :)</td></tr> | 
|  | 451 | <tr><td>sabre</td><td>consider this:</td></tr> | 
|  | 452 | <tr><td>sabre</td><td>{ float, { double , { int } } } *P</td></tr> | 
|  | 453 | <tr><td>sabre</td><td>int *X = gep P, 0, 1, 1, 0</td></tr> | 
|  | 454 | <tr><td>sabre</td><td>do you understand the leading zero?</td></tr> | 
|  | 455 | <tr><td>sabre</td><td>alternatively:</td></tr> | 
|  | 456 | <tr><td>sabre</td><td>t = gep P, 0, 1</td></tr> | 
|  | 457 | <tr><td>sabre</td><td>t2 = gep t, 0, 1</td></tr> | 
|  | 458 | <tr><td>sabre</td><td>X = gep t, 0, 0</td></tr> | 
|  | 459 | <tr><td>Yorion</td><td>what's t2 for?</td></tr> | 
|  | 460 | <tr><td>sabre</td><td>oh</td></tr> | 
|  | 461 | <tr><td>sabre</td><td>sorry :)</td></tr> | 
|  | 462 | <tr><td>sabre</td><td>X = gep t2, 0, 0</td></tr> | 
|  | 463 | <tr><td>Yorion</td><td>give me a minute please</td></tr> | 
|  | 464 | <tr><td>sabre</td><td>ok</td></tr> | 
|  | 465 | <tr><td>Yorion</td><td>sabre: shouldn't the type be: { float, { double, { int }* } }* P ?</td></tr> | 
|  | 466 | <tr><td>sabre</td><td>nope</td></tr> | 
|  | 467 | <tr><td>sabre</td><td>why the extra * ?</td></tr> | 
|  | 468 | <tr><td>sabre</td><td>if it helps, the type of t is { double, {int}}* and  t2 is {int}* and X is int*</td></tr> | 
|  | 469 | <tr><td>Yorion</td><td>wait... 0 represents dereference, natural number i | 
|  | 470 | represents &A[i] ?</td></tr> | 
|  | 471 | <tr><td>sabre</td><td>gep does no dereferences, ever</td></tr> | 
|  | 472 | <tr><td>sabre</td><td>gep P, 0, 1  is equivalent to &P[0].X</td></tr> | 
|  | 473 | <tr><td>sabre</td><td>aka &P->X</td></tr> | 
|  | 474 | <tr><td>sabre</td><td>gep P, 1  == &P[1]  aka P+1</td></tr> | 
|  | 475 | <tr><td>sabre</td><td>so gep P, 0, 1 can't alias gep P, 1  just like | 
|  | 476 | &P->Y can't alias P+1</td></tr> | 
|  | 477 | <tr><td>sabre</td><td>assuming P is a pointer to struct {X, Y }</td></tr> | 
|  | 478 | <tr><td>Yorion</td><td>sabre: is it possible to come up with a general rule for "arithmetic of GEP indices"? </td></tr> | 
|  | 479 | <tr><td>sabre</td><td>Yorion: of course, it's very simple</td></tr> | 
|  | 480 | <tr><td>sabre</td><td>just not what you're expecting :)</td></tr> | 
|  | 481 | <tr><td>sabre</td><td>See langref.html</td></tr> | 
|  | 482 | <tr><td>Yorion</td><td>for example,  a=GEP x,0,0,1 b=GEP a,0,0,1, c=GEP b,0,0,1, that should be equal to GEP x,0,1,1,0, right?</td></tr> | 
|  | 483 | <tr><td>Yorion</td><td>I did</td></tr> | 
|  | 484 | <tr><td>Yorion</td><td>oops, equal to GEP x,0,1,1,1,0</td></tr> | 
|  | 485 | <tr><td>sabre</td><td>that would be:</td></tr> | 
|  | 486 | <tr><td>sabre</td><td>GEP X, 0, 0, 1, 0, 1, 0, 1</td></tr> | 
|  | 487 | <tr><td>Yorion</td><td>you're killing me</td></tr> | 
|  | 488 | <tr><td>sabre</td><td>The basic rule when turning: A = GEP B, C    D = GEP A, 0, E</td></tr> | 
|  | 489 | <tr><td>sabre</td><td>is that you drop the 0, turning it into</td></tr> | 
|  | 490 | <tr><td>sabre</td><td>GEP B, C, E</td></tr> | 
|  | 491 | <tr><td>Yorion</td><td>okay, that's good. any other rules?</td></tr> | 
|  | 492 | <tr><td>nicholas</td><td>what if it isn't a 0?</td></tr> | 
|  | 493 | <tr><td>sabre</td><td>more generally: A = GEP Ptr, B, C, ...   D = GEP A, 0, E, F, ... </td></tr> | 
|  | 494 | <tr><td>sabre</td><td>D = GEP Ptr, B, C, ... E, F, ...</td></tr> | 
|  | 495 | <tr><td>sabre</td><td>if it's not zero, you generally cannot concatenate them</td></tr> | 
|  | 496 | <tr><td>sabre</td><td>unless the first gep has one subscript</td></tr> | 
|  | 497 | <tr><td>sabre</td><td>in which case you drop the zero</td></tr> | 
|  | 498 | <tr><td>sabre</td><td>if you look in InstCombiner::visitGetElementPtrInst, it should have this logic</td></tr> | 
|  | 499 | <tr><td>Yorion</td><td>what if there is no zero? how can I compute the offset from the base pointer in that case?</td></tr> | 
|  | 500 | <tr><td>Yorion</td><td>like A=GEP B,C   and D=GEP A,E,F</td></tr> | 
|  | 501 | <tr><td>sabre</td><td>you don't have to combine them to compute an offset</td></tr> | 
|  | 502 | <tr><td>sabre</td><td>are you *just* trying to get a byte offset from the pointer?</td></tr> | 
|  | 503 | <tr><td>Yorion</td><td>I'm trying to get offset of D from B</td></tr> | 
|  | 504 | <tr><td>sabre</td><td>why didn't you say so? :)</td></tr> | 
|  | 505 | <tr><td>sabre</td><td>with all constant subscripts, it's trivial</td></tr> | 
|  | 506 | <tr><td>sabre</td><td>look at SelectionDAGLowering::visitGetElementPtr</td></tr> | 
|  | 507 | <tr><td>sabre</td><td>in CodeGen/SelectionDAG/SelectionDAGISel.cpp</td></tr> | 
|  | 508 | <tr><td>sabre</td><td>basically the rule is that you multiply the index by the size of the thing indexed</td></tr> | 
|  | 509 | <tr><td>sabre</td><td>there is also a Support/GetElementPtrIterator or something</td></tr> | 
|  | 510 | <tr><td>sabre</td><td>that makes it trivial to see what type is indexed by which subscript</td></tr> | 
|  | 511 | <tr><td>sabre</td><td>for each subscript it gives you a type</td></tr> | 
|  | 512 | <tr><td>sabre</td><td>For an array subscript you multiply the index by the indexed type</td></tr> | 
|  | 513 | <tr><td>sabre</td><td>for a struct subscript, you add the field offset</td></tr> | 
|  | 514 | <tr><td>sabre</td><td>s/array/sequentialtype/ if you're in a pedantic mood</td></tr> | 
|  | 515 | <tr><td>Yorion</td><td>let's focus on structs: in that case, the above given example would be: D = GEP B,C,E,F?</td></tr> | 
|  | 516 | <tr><td>sabre</td><td>no</td></tr> | 
|  | 517 | <tr><td>sabre</td><td>you drop the E if it's zero</td></tr> | 
|  | 518 | <tr><td>sabre</td><td>if it's not you can't concat</td></tr> | 
|  | 519 | <tr><td>sabre</td><td>are you trying to trick me into saying "yes, just append the indices"? :)</td></tr> | 
|  | 520 | <tr><td>Yorion</td><td>okay, let's assume E is not zero, how do I compute offset from B for D for a struct?</td></tr> | 
|  | 521 | <tr><td>sabre</td><td>Why are you framing this in terms of concatenation?</td></tr> | 
|  | 522 | <tr><td>Yorion</td><td>no, I'm trying to understand it</td></tr> | 
|  | 523 | <tr><td>sabre</td><td>computing an offset and concatenating are entirely different</td></tr> | 
|  | 524 | <tr><td>sabre</td><td>Lets consider a specific example</td></tr> | 
|  | 525 | <tr><td>Yorion</td><td>because I want to express certain properties in the terms of base pointers either globals or parameters</td></tr> | 
|  | 526 | <tr><td>Yorion</td><td>I want to eliminate locals from my analysis</td></tr> | 
|  | 527 | <tr><td>sabre</td><td>you realize that parmeters can point into the middle of structs?</td></tr> | 
|  | 528 | <tr><td>Yorion</td><td>yes</td></tr> | 
|  | 529 | <tr><td>sabre</td><td>you realize invalid access paths can be constructed with geps/</td></tr> | 
|  | 530 | <tr><td>sabre</td><td>?</td></tr> | 
|  | 531 | <tr><td>Yorion</td><td>what do you mean by invalid access paths? </td></tr> | 
|  | 532 | <tr><td>Yorion</td><td>like offseting out of the struct which is passed to the function?</td></tr> | 
|  | 533 | <tr><td>sabre</td><td>The case where the subscript isn't zero is invalid code</td></tr> | 
|  | 534 | <tr><td>sabre</td><td>from a type-safety perspective</td></tr> | 
|  | 535 | <tr><td>DannyB</td><td>he means untypesafe things that seem valid :)</td></tr> | 
|  | 536 | <tr><td>DannyB</td><td>IE they point somewhere in the struct, but not to any particular field</td></tr> | 
|  | 537 | <tr><td>DannyB</td><td>(or whatever)</td></tr> | 
|  | 538 | <tr><td>sabre</td><td>right</td></tr> | 
|  | 539 | <tr><td>Yorion</td><td>okay</td></tr> | 
|  | 540 | <tr><td>sabre</td><td>or they might point in some other struct :)</td></tr> | 
|  | 541 | <tr><td>sabre</td><td>It's the equivalent to saying:</td></tr> | 
|  | 542 | <tr><td>sabre</td><td>struct Foo { int A, int B; }</td></tr> | 
|  | 543 | <tr><td>sabre</td><td>Foo* P = </td></tr> | 
|  | 544 | <tr><td>sabre</td><td>T = &P->B;</td></tr> | 
|  | 545 | <tr><td>sabre</td><td>S = T+1</td></tr> | 
|  | 546 | <tr><td>sabre</td><td>that is:</td></tr> | 
|  | 547 | <tr><td>sabre</td><td>T = gep 0, 1</td></tr> | 
|  | 548 | <tr><td>sabre</td><td>S = gep T, 1</td></tr> | 
|  | 549 | <tr><td>sabre</td><td>you can't concat those and get a type-safe access path</td></tr> | 
|  | 550 | <tr><td>sabre</td><td>remember T = &P->B  === T = &P[0].B</td></tr> | 
|  | 551 | <tr><td>sabre</td><td>understand?</td></tr> | 
|  | 552 | <tr><td>Yorion</td><td>let me think for a minute</td></tr> | 
|  | 553 | <tr><td>sabre</td><td>Consider what the C case does, it will be most clear if you're used to C</td></tr> | 
|  | 554 | <tr><td>sabre</td><td>:)</td></tr> | 
|  | 555 | <tr><td>sabre</td><td>Consider the byte offset and why it doesn't point into P-> anything</td></tr> | 
|  | 556 | <tr><td>sabre</td><td>it points into P[1] not P[0]</td></tr> | 
|  | 557 | <tr><td>Yorion</td><td>it's perfectly fine if GEP offsets out of the type. I'd still need to express GEP in the terms of base pointers. Take the example above: T=GEP P,0,1; S=GEP T,1</td></tr> | 
|  | 558 | <tr><td>Yorion</td><td>type safety is not crucial in my case</td></tr> | 
|  | 559 | <tr><td>sabre</td><td>That specific example is GEP P, 1, 0</td></tr> | 
|  | 560 | <tr><td>sabre</td><td>however, you can form geps that are NOT equivalent to anything else</td></tr> | 
|  | 561 | <tr><td>sabre</td><td>for example, consider:</td></tr> | 
|  | 562 | <tr><td>sabre</td><td>struct X { int, char}</td></tr> | 
|  | 563 | <tr><td>Yorion</td><td>that is fine. they're equivalent to something in the calling context</td></tr> | 
|  | 564 | <tr><td>sabre</td><td>the same sequence points into padding</td></tr> | 
|  | 565 | <tr><td>sabre</td><td>and there is no gep that can do that</td></tr> | 
|  | 566 | <tr><td>Yorion</td><td>the bottom line is: if the program is valid, interprocedural analysis will match that offset with something in one of the functions on the call stack</td></tr> | 
|  | 567 | <tr><td>Yorion</td><td>and that's all I care about</td></tr> | 
|  | 568 | <tr><td>Yorion</td><td>can you give me a formula for structs for computing | 
|  | 569 | offsets that takes into account the case GEP T,&lt:non_zeros> and the size of the structs/fields?</td></tr> | 
|  | 570 | <tr><td>sabre</td><td>yes, I did above</td></tr> | 
|  | 571 | <tr><td>sabre</td><td>Two things:</td></tr> | 
|  | 572 | <tr><td>sabre</td><td>GEP Ptr, A, X, Y, Z</td></tr> | 
|  | 573 | <tr><td>sabre</td><td>The result is Ptr + A * sizeof(struct) + fieldoffs(X) + fieldoffs(Y) + fieldoffs(Z)</td></tr> | 
|  | 574 | <tr><td>sabre</td><td>simple enough?</td></tr> | 
|  | 575 | <tr><td>sabre</td><td>you see why "A" is special?</td></tr> | 
|  | 576 | <tr><td>Yorion</td><td>give me a min, I'm constructing an example</td></tr> | 
|  | 577 | <tr><td>Reid</td><td>sabre: I think I finally understand</td></tr> | 
|  | 578 | <tr><td>Reid</td><td>your comment that GEP *never* dereferences makes a lot of sense</td></tr> | 
|  | 579 | <tr><td>Reid</td><td>it is only doing address calculation, so the first one is taking the address of the var</td></tr> | 
|  | 580 | <tr><td>sabre</td><td>If C didn't conflate lvalues and rvalues, GEP would be much easier to understand for people</td></tr> | 
|  | 581 | <tr><td>Reid</td><td>yeah</td></tr> | 
|  | 582 | <tr><td>Yorion</td><td>so, for example: M=GEP A,B,C; N=GEP M,D,E;   N = [ A + B*sizeof(struct) + fieldoffs(C) ]:(of type T) + D*sizeof(T) + fieldoffs(E)</td></tr> | 
|  | 583 | <tr><td>Reid</td><td>I just remember learning a hard lesson about the difference between char* A and char A[] .. long time ago when I was learning C</td></tr> | 
|  | 584 | <tr><td>sabre</td><td>of type T*</td></tr> | 
|  | 585 | <tr><td>sabre</td><td>otherwise fine</td></tr> | 
|  | 586 | <tr><td>Yorion</td><td>okay, I think I finally understand it</td></tr> | 
|  | 587 | <tr><td>sabre</td><td>without the T* your D sizeof will be wrong</td></tr> | 
|  | 588 | <tr><td>Yorion</td><td>a suggestion: the formula you gave above explains it all</td></tr> | 
|  | 589 | <tr><td>Yorion</td><td>I'd suggest explaining it that way in documentation</td></tr> | 
|  | 590 | <tr><td>sabre</td><td>That's not right though</td></tr> | 
|  | 591 | <tr><td>sabre</td><td>it doesn't include arrays or packed types</td></tr> | 
|  | 592 | <tr><td>sabre</td><td>so it is, at best, a half truth</td></tr> | 
|  | 593 | <tr><td>Yorion</td><td>tell me, how to compute the fieldoffs for an index?</td></tr> | 
|  | 594 | <tr><td>sabre</td><td>arrays can be in structs :)</td></tr> | 
|  | 595 | <tr><td>Yorion</td><td>in bytes</td></tr> | 
|  | 596 | <tr><td>sabre</td><td>idx * sizeof(arrayelt)</td></tr> | 
|  | 597 | <tr><td>sabre</td><td>just like for pointers (the first index)</td></tr> | 
|  | 598 | <tr><td>sabre</td><td>There are two cases: structs and sequentials</td></tr> | 
|  | 599 | <tr><td>sabre</td><td>for sequentials you use idx*sizeof(sequenced type)</td></tr> | 
|  | 600 | <tr><td>sabre</td><td>for structs you add their offset</td></tr> | 
|  | 601 | <tr><td>sabre</td><td>it's really very simple :)</td></tr> | 
|  | 602 | <tr><td>sabre</td><td>the first index of a gep is always over the pointer</td></tr> | 
|  | 603 | <tr><td>Yorion</td><td>no I meant in LLVM, how do I convert the field offset into bytes?</td></tr> | 
|  | 604 | <tr><td>sabre</td><td>which is why it's strange</td></tr> | 
|  | 605 | <tr><td>sabre</td><td>if you only think about structs</td></tr> | 
|  | 606 | <tr><td>sabre</td><td>TargetData::getFieldOffset </td></tr> | 
|  | 607 | <tr><td>sabre</td><td>or something</td></tr> | 
|  | 608 | <tr><td>sabre</td><td>look in SelectionDAGISel.cpp (visitGEP) as I suggested.</td></tr> | 
|  | 609 | <tr><td>Yorion</td><td>do you still have the energy to go over sequential types? :-)</td></tr> | 
|  | 610 | <tr><td>Yorion</td><td>what is the offset formula for sequential types?</td></tr> | 
|  | 611 | <tr><td>Reid</td><td>we just went over that: idx * sizeof(elementType)</td></tr> | 
|  | 612 | <tr><td>Yorion</td><td>so, if there's an array hidden somewhere in the struct, essentially I need to compute idx*sizeof() instead of fieldoffs() and that's it?</td></tr> | 
|  | 613 | <tr><td>sabre</td><td>yes</td></tr> | 
|  | 614 | <tr><td>Reid</td><td>yes</td></tr> | 
|  | 615 | <tr><td>Yorion</td><td>excellent.</td></tr> | 
|  | 616 | <tr><td>sabre</td><td>There are two cases: structs and sequentials</td></tr> | 
|  | 617 | <tr><td>sabre</td><td>[9:17pm] sabre: for sequentials you use idx*sizeof(sequenced type)</td></tr> | 
|  | 618 | <tr><td>sabre</td><td>[9:17pm] sabre: for structs you add their offset</td></tr> | 
|  | 619 | <tr><td>sabre</td><td>[9:17pm] sabre: it's really very simple :)</td></tr> | 
|  | 620 | <tr><td>Yorion</td><td>now when I understand it, it is simple... </td></tr> | 
|  | 621 | <tr><td>Yorion</td><td>thx</td></tr> | 
|  | 622 | </table> | 
|  | 623 |  | 
|  | 624 | <!-- *********************************************************************** --> | 
| Reid Spencer | 4bded9c | 2006-08-10 20:15:58 +0000 | [diff] [blame] | 625 |  | 
|  | 626 | <hr> | 
|  | 627 | <address> | 
|  | 628 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img | 
|  | 629 | src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a> | 
|  | 630 | <a href="http://validator.w3.org/check/referer"><img | 
|  | 631 | src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a> | 
|  | 632 | <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br/> | 
|  | 633 | Last modified: $Date$ | 
|  | 634 | </address> | 
|  | 635 | </body> | 
|  | 636 | </html> |