public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "antoshkka at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/114661] New: Bit operations not optimized to multiplication
Date: Tue, 09 Apr 2024 16:11:10 +0000	[thread overview]
Message-ID: <bug-114661-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 114661
           Summary: Bit operations not optimized to multiplication
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the example:

unsigned mul(unsigned char c) {
    if (c > 3) __builtin_unreachable();
    return c << 18 | c << 15 |
        c << 12 | c << 9 |
        c << 6 | c << 3 | c;
}

GCC with -O2 generates the following assembly:

mul(unsigned char):
  movzx edi, dil
  lea edx, [rdi+rdi*8]
  lea eax, [0+rdx*8]
  mov ecx, edx
  sal edx, 15
  or eax, edi
  sal ecx, 9
  or eax, ecx
  or eax, edx
  ret

However it could be optimized to just:

mul(unsigned char):
  imul eax, edi, 299593
  ret

Compiling with -Os does not help.

Godbolt playground: https://godbolt.org/z/YszzMbovK

P.S.: without `c << 18 | c << 15 |` the bit operations are transformed to
multiplication.

             reply	other threads:[~2024-04-09 16:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-09 16:11 antoshkka at gmail dot com [this message]
2024-04-09 19:55 ` [Bug middle-end/114661] " pinskia at gcc dot gnu.org
2024-04-09 19:57 ` pinskia at gcc dot gnu.org
2024-04-09 22:04 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-114661-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).