public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lh_mouse at 126 dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/109992] New: Addition/subtraction to the last bitfield of a struct can be optimized
Date: Fri, 26 May 2023 15:48:36 +0000	[thread overview]
Message-ID: <bug-109992-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109992
           Summary: Addition/subtraction to the last bitfield of a struct
                    can be optimized
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

For an unsigned bit field:
```
struct foo
  {
    unsigned a :  3;
    unsigned b : 29;
  };

void
bad_add(struct foo* p, unsigned add)
  {
    p->b += add;
  }
```

GCC:
```
bad_add:
        mov     eax, DWORD PTR [rdi]
        mov     edx, eax
        and     eax, 7
        shr     edx, 3
        add     edx, esi
        sal     edx, 3
        or      eax, edx
        mov     DWORD PTR [rdi], eax
        ret
```

Clang:
```
bad_add:                                # @bad_add
        shl     esi, 3
        add     dword ptr [rdi], esi
        ret
```

It looks like GCC extracts the bitfield first, performs the addition, then
inserts it back.

The result is almost the same for a signed bitfield, but not exacting the
bitfield first is subject to overflows, so it may be a different story.

             reply	other threads:[~2023-05-26 15:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-26 15:48 lh_mouse at 126 dot com [this message]
2023-05-26 15:51 ` [Bug rtl-optimization/109992] " pinskia at gcc dot gnu.org
2023-05-26 15:52 ` [Bug tree-optimization/109992] " pinskia at gcc dot gnu.org
2023-05-26 16:07 ` pinskia at gcc dot gnu.org
2023-05-30  7:37 ` rguenth 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-109992-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).