Bug 94206 - Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
Summary: Wrong optimization: memset of n-bit integer types (from bit-fields) is trunca...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2020-03-17 21:15 UTC by Alexander Cherepanov
Modified: 2020-04-15 09:59 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 10.0, 9.3.1
Known to fail: 4.8.5, 9.3.0
Last reconfirmed: 2020-03-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Cherepanov 2020-03-17 21:15:32 UTC
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`.
Comment 1 Andrew Pinski 2020-03-17 21:22:55 UTC
This again padding bits.
Comment 2 Richard Biener 2020-03-18 08:12:02 UTC
Broken since long via memset folding:

      __MEM <uint33> ((void *)&x) = _Literal (uint33) 8589934591;
Comment 3 GCC Commits 2020-03-18 12:12:43 UTC
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.
Comment 4 Richard Biener 2020-03-18 12:13:11 UTC
Fixed on trunk sofar.
Comment 5 GCC Commits 2020-04-02 14:49:29 UTC
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.
Comment 6 Richard Biener 2020-04-15 09:59:21 UTC
Fixed.