Bit-fields of different widths have their own types in gcc. And `memset` seems to be optimized strangely for them. Even at `-O0`! This is not standard C due to the use of `typeof` but this topic is starting to be more interesting in light of N2472 "Adding Fundamental Type for N-bit Integers". ---------------------------------------------------------------------- #include <string.h> #include <stdio.h> struct { unsigned long x:33; } s; typedef __typeof__(s.x + 0) uint33; int main() { uint33 x; printf("sizeof = %zu\n", sizeof x); memset(&x, -1, sizeof x); unsigned long u; memcpy(&u, &x, sizeof u); printf("%lx\n", u); } ---------------------------------------------------------------------- $ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out sizeof = 8 1ffffffff $ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out sizeof = 8 1ffffffff ---------------------------------------------------------------------- gcc x86-64 version: gcc (GCC) 10.0.1 20200317 (experimental) ---------------------------------------------------------------------- The right result is `ffffffffffffffff`.
This again padding bits.
Broken since long via memset folding: __MEM <uint33> ((void *)&x) = _Literal (uint33) 8589934591;
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>: https://gcc.gnu.org/g:1ba9acb11e3589b96ed945ed2a3af6acd6377018 commit r10-7242-g1ba9acb11e3589b96ed945ed2a3af6acd6377018 Author: Richard Biener <rguenther@suse.de> Date: Wed Mar 18 13:11:30 2020 +0100 middle-end/94206 fix memset folding to avoid types with padding This makes sure that the store a memset is folded to uses a type covering all bits. 2020-03-18 Richard Biener <rguenther@suse.de> PR middle-end/94206 * gimple-fold.c (gimple_fold_builtin_memset): Avoid using partial int modes or not mode-precision integer types for the store. * gcc.dg/torture/pr94206.c: New testcase.
Fixed on trunk sofar.
The releases/gcc-9 branch has been updated by Richard Biener <rguenth@gcc.gnu.org>: https://gcc.gnu.org/g:4b1087f8dc7505997dc475b554b5b86a06c78d69 commit r9-8443-g4b1087f8dc7505997dc475b554b5b86a06c78d69 Author: Richard Biener <rguenther@suse.de> Date: Wed Mar 18 13:11:30 2020 +0100 middle-end/94206 fix memset folding to avoid types with padding This makes sure that the store a memset is folded to uses a type covering all bits. 2020-03-18 Richard Biener <rguenther@suse.de> PR middle-end/94206 * gimple-fold.c (gimple_fold_builtin_memset): Avoid using partial int modes or not mode-precision integer types for the store. * gcc.dg/torture/pr94206.c: New testcase.
Fixed.