[LLVMbugs] [Bug 494] NEW: [llvmgcc] Signed divide generated for pointer difference

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Thu Dec 30 23:39:17 PST 2004


           Summary: [llvmgcc] Signed divide generated for pointer difference
           Product: tools
           Version: 1.0
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: llvm-gcc
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: sabre at nondot.org

Consider this function:

int ptrdiff(int *X, int *Y) { return X-Y; }

llvmgcc currently compiles this to:

int %ptrdiff(int* %X, int* %Y) {
        %tmp.1 = cast int* %X to int
        %tmp.3 = cast int* %Y to int
        %tmp.4 = sub int %tmp.1, %tmp.3
        %tmp.5 = div int %tmp.4, 4
        ret int %tmp.5

If llvmgcc compiled the divide to an unsigned divide, this could be optimized to
a right shift.  Note that C and C++ guarantee that pointers used in pointer
arithmetic are into the same array.  This means that an unsigned divide is fine,
and, further, that the remainder is guaranteed to be zero.  Because of this
property, some cute code generation tricks can be used to generate slightly
better code.

In the future it might be worthwhile to consider adding a ptrdiff instruction to
LLVM.  GCC includes the notion of an "exact divide" (a divide that is known to
have a remainder of zero) for this purpose.  Maybe that would be a better way to go.

Note that the STL std::vector class (among others) use pointer differences
extensively, so it's worthwhile to generate decent code for this.


------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.

More information about the llvm-bugs mailing list