<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'><font class="Apple-style-span" size="3">Dear All,</font><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">As of late I am having a hard time getting my head around how array accesses</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">are translated by Clang into LLVM IR: the often misunderstood GEP instruction.</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">I am trying to reverse-engineer array accesses to discover the number of dimensions</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">and actual indexes of the original, and I am beginning to wonder whether this is</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">possible at all. To illustrate (some of) my troubles, consider the following code and</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">the LLVM IR for both 32 and 64 bit memory addresses:</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">--</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">original C:</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div><div>#define N 1000</div><div><br></div><div>int main(int argc, char **argv)</div><div>{</div><div> int i, k;</div><div> float aux, A[N][N];</div><div><br></div><div> aux = A[k][i];</div><div>}</div></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">--</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">32-bit addresses LLVM IR (relevant part):</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div><div> %4 = load i32* %i, align 4</div><div> %5 = load i32* %k, align 4</div><div> %6 = getelementptr inbounds [1000 x [1000 x float]]* %A, i32 0, i32 %5</div><div> %7 = getelementptr inbounds [1000 x float]* %6, i32 0, i32 %4</div><div> %8 = load float* %7</div><div> store float %8, float* %aux, align 4</div></div><div><br></div><div>--</div><div>64-bit addresses LLVM IR (relevant part):</div><div><br></div><div><div> %4 = load i32* %i, align 4</div><div> %5 = load i32* %k, align 4</div><div> %6 = getelementptr inbounds [1000 x [1000 x float]]* %A, i32 0, i32 0</div><div> %7 = sext i32 %5 to i64</div><div> %8 = getelementptr inbounds [1000 x float]* %6, i64 %7</div><div> %9 = load float* %8</div><div> store float %9, float* %aux, align 4</div></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">--</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">Why does the 64-bit addresses version use two leading 0s instead of one? I have tried reading</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><a href="http://llvm.org/docs/GetElementPtr.html">http://llvm.org/docs/GetElementPtr.html</a> and I don't think the explanation provided is accurate, or</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">at least I can't see how to apply it to this particular case. </div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">Besides, there is an incredible diversity of variations in how arrays can be represented and accessed</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">in C codes, leading to my final question: is it really possible to reverse-engineer array accesses? If so,</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">any insights?</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; "><br></div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">Thanks in advance, and best regards,</div><div style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 12pt; ">Gabriel</div></div></body></html>