public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
@ 2020-03-17 21:15 ch3root at openwall dot com
  2020-03-17 21:22 ` [Bug middle-end/94206] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: ch3root at openwall dot com @ 2020-03-17 21:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94206
           Summary: Wrong optimization: memset of n-bit integer types
                    (from bit-fields) is truncated to n bits (instead of
                    sizeof)
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

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

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

* [Bug middle-end/94206] Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
  2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
@ 2020-03-17 21:22 ` pinskia at gcc dot gnu.org
  2020-03-18  8:12 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-03-17 21:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This again padding bits.

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

* [Bug middle-end/94206] Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
  2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
  2020-03-17 21:22 ` [Bug middle-end/94206] " pinskia at gcc dot gnu.org
@ 2020-03-18  8:12 ` rguenth at gcc dot gnu.org
  2020-03-18 12:12 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-03-18  8:12 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-03-18
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
      Known to fail|                            |4.8.5

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Broken since long via memset folding:

      __MEM <uint33> ((void *)&x) = _Literal (uint33) 8589934591;

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

* [Bug middle-end/94206] Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
  2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
  2020-03-17 21:22 ` [Bug middle-end/94206] " pinskia at gcc dot gnu.org
  2020-03-18  8:12 ` rguenth at gcc dot gnu.org
@ 2020-03-18 12:12 ` cvs-commit at gcc dot gnu.org
  2020-03-18 12:13 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-03-18 12:12 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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.

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

* [Bug middle-end/94206] Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
  2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
                   ` (2 preceding siblings ...)
  2020-03-18 12:12 ` cvs-commit at gcc dot gnu.org
@ 2020-03-18 12:13 ` rguenth at gcc dot gnu.org
  2020-04-02 14:49 ` cvs-commit at gcc dot gnu.org
  2020-04-15  9:59 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-03-18 12:13 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
      Known to work|                            |10.0

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed on trunk sofar.

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

* [Bug middle-end/94206] Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
  2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
                   ` (3 preceding siblings ...)
  2020-03-18 12:13 ` rguenth at gcc dot gnu.org
@ 2020-04-02 14:49 ` cvs-commit at gcc dot gnu.org
  2020-04-15  9:59 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-04-02 14:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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.

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

* [Bug middle-end/94206] Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof)
  2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
                   ` (4 preceding siblings ...)
  2020-04-02 14:49 ` cvs-commit at gcc dot gnu.org
@ 2020-04-15  9:59 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-15  9:59 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2020-04-15  9:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 21:15 [Bug middle-end/94206] New: Wrong optimization: memset of n-bit integer types (from bit-fields) is truncated to n bits (instead of sizeof) ch3root at openwall dot com
2020-03-17 21:22 ` [Bug middle-end/94206] " pinskia at gcc dot gnu.org
2020-03-18  8:12 ` rguenth at gcc dot gnu.org
2020-03-18 12:12 ` cvs-commit at gcc dot gnu.org
2020-03-18 12:13 ` rguenth at gcc dot gnu.org
2020-04-02 14:49 ` cvs-commit at gcc dot gnu.org
2020-04-15  9:59 ` rguenth at gcc dot gnu.org

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