https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94356

            Bug ID: 94356
           Summary: Missed optimisation: useless multiplication generated
                    for pointer comparison
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pascal_cuoq at hotmail dot com
  Target Milestone: ---

The closest existing ticket I found for this one is
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48316 but this seems different
enough, although it might be linked.

Consider the function:

typedef int t[100000];

int f(t *p, long long o) {
    return p < p+o;
}

GCC 9.3 with -O2, targeting x86-64, correctly simplifies by p, but still
generates a 64x64->64 multiplication in the computation of the offset:

f:
        imulq   $400000, %rsi, %rsi
        xorl    %eax, %eax
        testq   %rsi, %rsi
        setg    %al
        ret

(Compiler Explorer link: https://gcc.godbolt.org/z/_pT8E- )

Clang 10 avoids generating the multiplication on this example:

f:                                      # @f
        xorl    %eax, %eax
        testq   %rsi, %rsi
        setg    %al
        retq

A variant of this example is this other function g with two offsets:

int g(t *p, long long o1, long long o2) {
    return p+o1 < p+o2;
}

Clang generates the same code as for “o1<o2”, 
whereas GCC generates two multiplications:

g:
        imulq   $400000, %rsi, %rsi
        xorl    %eax, %eax
        imulq   $400000, %rdx, %rdx
        cmpq    %rdx, %rsi
        setl    %al
        ret

Compiler Explorer link: https://gcc.godbolt.org/z/sDJyHP

Reply via email to