https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114289
Bug ID: 114289 Summary: Non-optimal assembly for accessing bit-fields in packed structs Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: dan at stahlke dot org Target Milestone: --- // gcc -O3 -g -Wall -march=haswell -mavx2 // https://godbolt.org/z/Yfb9dnYx4 struct foo { int x:31; } __attribute__((packed)); int fx(struct foo const *o) { return o->x; } Disassembly: movzx eax, BYTE PTR [rdi+1] movzx edx, BYTE PTR [rdi] sal rax, 8 or rax, rdx movzx edx, BYTE PTR [rdi+2] sal rdx, 16 or rdx, rax movzx eax, BYTE PTR [rdi+3] and eax, 127 sal rax, 24 or rax, rdx sal rax, 33 sar rax, 33 ret Compare to clang: mov eax, dword ptr [rdi] add eax, eax sar eax ret