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

* [Bug rtl-optimization/109992] Addition/subtraction to the last bitfield of a struct can be optimized
  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 ` pinskia at gcc dot gnu.org
  2023-05-26 15:52 ` [Bug tree-optimization/109992] " pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-26 15:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
As an aside: it is funny how x86 does not have a bits insert instruction yet
(while almost all RISC targets have that now).

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

* [Bug tree-optimization/109992] Addition/subtraction to the last bitfield of a struct can be optimized
  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 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-26 15:52 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|rtl-optimization            |tree-optimization

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

* [Bug tree-optimization/109992] Addition/subtraction to the last bitfield of a struct can be optimized
  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
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-26 16:07 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-05-26
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
After my lowering pass (little-endian) we have:
  _1 = BIT_FIELD_REF <_9, 29, 3>;
  _2 = (unsigned int) _1;
  _3 = _2 + add_7(D);
  _4 = (<unnamed-unsigned:29>) _3;
  _11 = BIT_INSERT_EXPR <_9, _4, 3 (29 bits)>;

Which I suspect we could pattern match to:
_t = add_7 << 3;
_11 = _9 + _t;

iff 3+29 == 32(int)

Big-endian (with fields a and b swapped order in the source):
  _9 = MEM[(struct foo *)p_6(D)];
  _1 = BIT_FIELD_REF <_9, 29, 0>;
  _2 = (unsigned int) _1;
  _3 = _2 + add_7(D);
  _4 = (<unnamed-unsigned:29>) _3;
  _11 = BIT_INSERT_EXPR <_9, _4, 0 (29 bits)>;


Similar pattern matching, just using 0 for the offset rather than 3 ...

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

* [Bug tree-optimization/109992] Addition/subtraction to the last bitfield of a struct can be optimized
  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
                   ` (2 preceding siblings ...)
  2023-05-26 16:07 ` pinskia at gcc dot gnu.org
@ 2023-05-30  7:37 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-30  7:37 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |x86_64-*-*

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
At present (until we lower bitfield ops) this is a missed optimization on RTL.

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