public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/109992] New: Addition/subtraction to the last bitfield of a struct can be optimized
@ 2023-05-26 15:48 lh_mouse at 126 dot com
  2023-05-26 15:51 ` [Bug rtl-optimization/109992] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: lh_mouse at 126 dot com @ 2023-05-26 15:48 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2023-05-30  7:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-26 15:48 [Bug rtl-optimization/109992] New: Addition/subtraction to the last bitfield of a struct can be optimized lh_mouse at 126 dot com
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

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).