public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94356] New: Missed optimisation: useless multiplication generated for pointer comparison
@ 2020-03-27 13:27 pascal_cuoq at hotmail dot com
  2020-03-27 13:46 ` [Bug tree-optimization/94356] " glisse at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pascal_cuoq at hotmail dot com @ 2020-03-27 13:27 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-08-04 22:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-27 13:27 [Bug tree-optimization/94356] New: Missed optimisation: useless multiplication generated for pointer comparison pascal_cuoq at hotmail dot com
2020-03-27 13:46 ` [Bug tree-optimization/94356] " glisse at gcc dot gnu.org
2020-03-27 15:24 ` jakub at gcc dot gnu.org
2020-03-27 17:10 ` glisse at gcc dot gnu.org
2020-03-30  7:23 ` rguenth at gcc dot gnu.org
2021-08-03 23:09 ` pinskia at gcc dot gnu.org
2021-08-04 22:31 ` glisse at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).