[cfe-dev] Pointer Arithmetic

Chris Lattner clattner at apple.com
Thu Jul 12 17:07:43 PDT 2007


On Jul 12, 2007, at 4:53 PM, Keith Bauer wrote:
> define i32 @test4(i32* %a, i32* %b) {
> entry:
> 	%sub.ptr.lhs.cast = ptrtoint i32* %a to i32		; <i32> [#uses=1]
> 	%sub.ptr.rhs.cast = ptrtoint i32* %b to i32		; <i32> [#uses=1]
> 	%sub.ptr.sub = sub i32 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast		;  
> <i32> [#uses=1]
> 	%sub.ptr.div = sdiv i32 %sub.ptr.sub, 4		; <i32> [#uses=1]
> 	ret i32 %sub.ptr.div
> }
>
> (I guess the div can't be optimized to a shift because it's signed  
> division?)
>

Yep, this is right.  In the future, LLVM may get some way to  
represent "exact division" which is useful for pointer subtraction.   
When it has that, it will be able to simplify this.

-Chris



More information about the cfe-dev mailing list