[LLVMdev] Pointer sizes, GetElementPtr, and offset sizes

Chris Lattner sabre at nondot.org
Fri May 2 15:24:12 PDT 2008


On Fri, 2 May 2008, Marc de Kruijf wrote:
> 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:
>
> $ cat test.c
> int main() {
>    int *x[2];
>    int **y = &x[1];
>    return (y - x);
> }

> The return value is 1.  The ashr exposes the pointer size by shifting the 4
> byte distance over by 2.

Right.  A related issue is:
http://llvm.org/bugs/show_bug.cgi?id=2247

> I didn't realize this before, but perhaps the fact that llvm-gcc was 
> unable to optimize out the offset calculation at -O3 is sufficient 
> evidence for supporting such an instruction. :)

Sure it does:

$ llvm-gcc t.c -S -o - -O3 -fomit-frame-pointer
_main:
 	subl	$8, %esp
 	movl	$1, %eax
 	addl	$8, %esp
 	ret

I agree that optimizing it before codegen time would be preferable, but 
adding a new instruction (by itself) doesn't handle this.  It would be 
easy to add this to the current optimizer if we cared *shrug*.

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list