The LLVA and LLVM papers motivate the GetElementPtr instruction by arguing that it abstracts implementation details, in particular pointer size, from the compiler.  While it does this fine for pointer addresses, it does not manage it for address offsets.  Consider the following code:<br>
<br>$ cat test.c<br>int main() {<br>    int *x[2];<br>    int **y = &x[1];<br>    return (y - x);<br>}<br><br>$ llvm-gcc -O3 -c test.c -emit-llvm -o - | llvm-dis<br>; ModuleID = '<stdin>'<br>target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"<br>
target triple = "i686-pc-linux-gnu"<br><br>define i32 @main() nounwind  {<br>entry:<br>        %x = alloca [2 x i32*]          ; <[2 x i32*]*> [#uses=2]<br>        %tmp1 = getelementptr [2 x i32*]* %x, i32 0, i32 1              ; <i32**> [#uses=1]<br>
        %tmp23 = ptrtoint i32** %tmp1 to i32            ; <i32> [#uses=1]<br>        %x45 = ptrtoint [2 x i32*]* %x to i32           ; <i32> [#uses=1]<br>        %tmp6 = sub i32 %tmp23, %x45            ; <i32> [#uses=1]<br>
        %tmp7 = ashr i32 %tmp6, 2               ; <i32> [#uses=1]<br>        ret i32 %tmp7<br>}<br><br><br>The return value is 1.  The ashr exposes the pointer size by shifting the 4 byte distance over by 2.  <br>For the analysis that I am doing, it would be nice to have an instruction that explicitly performs this distance calculation in a type-safe manner, irrespective of pointer size.  Something like this:<br>
<br>define i32 @main() nounwind  {<br>
entry:<br>
        %x = alloca [2 x i32*]          ; <[2 x i32*]*> [#uses=2]<br>
        %tmp1 = getelementptr [2 x i32*]* %x, i32 0, i32 1              ; <i32**> [#uses=1]<br>
        %tmp2 = getdistance i32** %x, %tmp1            ; <i32> [#uses=1]<br>        ret i32 %tmp2<br>
}<br><br>I'm not really a compiler person, so I'm wondering if a need for such an instruction ever arises in more compiler-oriented situations such as optimization or wrt. portability?  Does the fact that pointer size is never completely hidden ever cause problems?  Is GetElementPtr generally "good enough"?  It would be nice to have a complete solution though, wouldn't it?  Thoughts?<br>
<br>Marc<br><br>