public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110613] New: optimization about combined store of adjacent bitfields
@ 2023-07-10 14:47 lh_mouse at 126 dot com
  2023-07-10 23:53 ` [Bug tree-optimization/110613] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: lh_mouse at 126 dot com @ 2023-07-10 14:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110613
           Summary: optimization about combined store of adjacent
                    bitfields
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

This is a piece of code taken from a WebSocket frame parser:
```
#include <stdint.h>

struct Header 
  {
    // 1st byte
    uint8_t opcode : 4;
    uint8_t rsv3 : 1;
    uint8_t rsv2 : 1;
    uint8_t rsv1 : 1;
    uint8_t fin : 1;

    // 2nd byte
    uint8_t reserved_1 : 7;
    uint8_t mask : 1;

    // 3rd and 4th bytes
    uint8_t reserved_2;
    uint8_t reserved_3;

    // 5th to 7th bytes
    union {
      char mask_key[4];
      uint32_t mask_key_u32;
    };

    // 8th to 15th bytes
    uint64_t payload_len;
  };

void
set_header(Header* ph, const uint8_t* bptr)
  {
    uint8_t f = bptr[0];
    uint8_t t = bptr[1];

    ph->fin = f >> 7;
    ph->rsv1 = f >> 6;
    ph->rsv2 = f >> 5;
    ph->rsv3 = f >> 4;
    ph->opcode = f;

    ph->mask = t >> 7;
    ph->reserved_1 = t;
  }
```

The structure is designed to match x86_64 ABI (little endian), so
```
    ph->fin = f >> 7;
    ph->rsv1 = f >> 6;
    ph->rsv2 = f >> 5;
    ph->rsv3 = f >> 4;
    ph->opcode = f;
```
should be a simple move (https://gcc.godbolt.org/z/9vTqs7axj), and
```
    ph->mask = t >> 7;
    ph->reserved_1 = t;
```
should also be a simple move (https://gcc.godbolt.org/z/KdchWvEn1), but!

When put these two pieces of code together, guess what?:
(godbolt: https://gcc.godbolt.org/z/hbEaeb3MT)
```
set_header(Header*, unsigned char const*):
        movzx   edx, BYTE PTR [rsi]
        movzx   ecx, BYTE PTR [rsi+1]
        mov     eax, edx
        mov     esi, edx
        shr     al, 4
        and     esi, 15
        and     eax, 1
        sal     eax, 4
        or      eax, esi
        mov     esi, edx
        shr     sil, 5
        and     esi, 1
        sal     esi, 5
        or      eax, esi
        mov     esi, edx
        shr     dl, 7
        shr     sil, 6
        movzx   edx, dl
        and     esi, 1
        sal     edx, 7
        sal     esi, 6
        or      eax, esi
        or      eax, edx
        mov     edx, ecx
        shr     cl, 7
        and     edx, 127
        sal     ecx, 15
        sal     edx, 8
        or      eax, edx
        or      eax, ecx
        mov     WORD PTR [rdi], ax
        ret
```

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

* [Bug tree-optimization/110613] optimization about combined store of adjacent bitfields
  2023-07-10 14:47 [Bug tree-optimization/110613] New: optimization about combined store of adjacent bitfields lh_mouse at 126 dot com
@ 2023-07-10 23:53 ` pinskia at gcc dot gnu.org
  2023-07-11  7:43 ` rguenth at gcc dot gnu.org
  2023-07-11  8:38 ` lh_mouse at 126 dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-10 23:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2023-07-10

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
Note neither independently is optimized at the tree level; only at the RTL
level.

Note moving the load from bptr[1] to right before the first usage fixes it at
the RTL level too (most likely due to aliasing of character types being special
in C/C++).

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

* [Bug tree-optimization/110613] optimization about combined store of adjacent bitfields
  2023-07-10 14:47 [Bug tree-optimization/110613] New: optimization about combined store of adjacent bitfields lh_mouse at 126 dot com
  2023-07-10 23:53 ` [Bug tree-optimization/110613] " pinskia at gcc dot gnu.org
@ 2023-07-11  7:43 ` rguenth at gcc dot gnu.org
  2023-07-11  8:38 ` lh_mouse at 126 dot com
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-11  7:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
bitfield lowering / store merging might be the enabling passes

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

* [Bug tree-optimization/110613] optimization about combined store of adjacent bitfields
  2023-07-10 14:47 [Bug tree-optimization/110613] New: optimization about combined store of adjacent bitfields lh_mouse at 126 dot com
  2023-07-10 23:53 ` [Bug tree-optimization/110613] " pinskia at gcc dot gnu.org
  2023-07-11  7:43 ` rguenth at gcc dot gnu.org
@ 2023-07-11  8:38 ` lh_mouse at 126 dot com
  2 siblings, 0 replies; 4+ messages in thread
From: lh_mouse at 126 dot com @ 2023-07-11  8:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from LIU Hao <lh_mouse at 126 dot com> ---
There are some more cases about loading adjacent bitfields; not sure whether I
should create new PRs or paste them here. They seem highly related to the
aliasing characteristics of `unsigned char`; if I inject
`__atomic_signal_fence(__ATOMIC_RELAXED);` between the loads and the stores,
the issue is gone.

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

end of thread, other threads:[~2023-07-11  8:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-10 14:47 [Bug tree-optimization/110613] New: optimization about combined store of adjacent bitfields lh_mouse at 126 dot com
2023-07-10 23:53 ` [Bug tree-optimization/110613] " pinskia at gcc dot gnu.org
2023-07-11  7:43 ` rguenth at gcc dot gnu.org
2023-07-11  8:38 ` lh_mouse at 126 dot com

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