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 tree-optimization/110613] New: optimization about combined store of adjacent bitfields
Date: Mon, 10 Jul 2023 14:47:13 +0000	[thread overview]
Message-ID: <bug-110613-4@http.gcc.gnu.org/bugzilla/> (raw)

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
```

             reply	other threads:[~2023-07-10 14:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-10 14:47 lh_mouse at 126 dot com [this message]
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

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