public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/103855] New: Missed optimization: 64bit division used instead of 32bit division
@ 2021-12-29  3:00 zhaoweiliew at gmail dot com
  2021-12-29  3:16 ` [Bug tree-optimization/103855] " zhaoweiliew at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: zhaoweiliew at gmail dot com @ 2021-12-29  3:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103855
           Summary: Missed optimization: 64bit division used instead of
                    32bit division
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhaoweiliew at gmail dot com
  Target Milestone: ---

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

I compiled the following code with `x86-64 gcc (trunk) -std=c++20 -O3 -Wall
-Wextra -Werror`:

```
unsigned int optimized(unsigned int a, unsigned int b) {
    return (unsigned long long)a / b;
}

unsigned int unoptimized(unsigned int a, unsigned int b) {
    unsigned long long all = a;
    return all / b;
}
```

This is the assembly output:

```
optimized(unsigned int, unsigned int):
        mov     eax, edi
        xor     edx, edx
        div     esi
        ret
unoptimized(unsigned int, unsigned int):
        mov     eax, edi
        mov     esi, esi
        xor     edx, edx
        div     rsi
        ret
```

GCC uses a 64-bit divide for `unoptimized()`, when a 32-bit divide would be
equivalent and faster. GCC uses a 32-bit divide for `optimized()`, which is
fine. Note that LLVM does a 32-bit division in both cases.

I would like to tackle this optimization, but am not sure how to go about doing
it. Could someone tell me/point me to resources that tell me what part of the
compiler and codebase I should be looking to optimize? Thanks!

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

end of thread, other threads:[~2021-12-29  9:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-29  3:00 [Bug tree-optimization/103855] New: Missed optimization: 64bit division used instead of 32bit division zhaoweiliew at gmail dot com
2021-12-29  3:16 ` [Bug tree-optimization/103855] " zhaoweiliew at gmail dot com
2021-12-29  3:34 ` pinskia at gcc dot gnu.org
2021-12-29  3:46 ` pinskia at gcc dot gnu.org
2021-12-29  3:53 ` pinskia at gcc dot gnu.org
2021-12-29  8:21 ` zhaoweiliew at gmail dot com
2021-12-29  8:32 ` zhaoweiliew at gmail dot com
2021-12-29  9:41 ` jakub 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).