public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64
@ 2020-03-07 19:33 larrowe.semaj11 at gmail dot com
  2020-03-08  4:30 ` [Bug tree-optimization/94086] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: larrowe.semaj11 at gmail dot com @ 2020-03-07 19:33 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94086
           Summary: Missed optimization when converting a bitfield to an
                    integer on x86-64
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: larrowe.semaj11 at gmail dot com
  Target Milestone: ---

When converting a structure containing bitfields to an unsigned 16-bit integer,
inefficient code is generated.

Minimal testcase:

    typedef struct half
    {
        unsigned short mantissa:10;
        unsigned short exponent:5;
        unsigned short sign:1;
    } half;

    unsigned short from_half(half h)
    {
        return h.mantissa | h.exponent << 10 | h.sign << 15;
    }

To compile and place output on `stdout`:

    gcc -O3 -S test.c -o -

Assembly output:

        movl    %edi, %edx
        movl    %edi, %eax
        andw    $1023, %di
        shrw    $15, %dx
        andl    $31744, %eax
        movzbl  %dl, %edx
        sall    $15, %edx
        orl     %edx, %eax
        orl     %edi, %eax
        ret

This could be optimized into:

        movl    %edi, %eax
        retq

This behavior does not occur when `unsigned short` is being converted into
`half` in a similar manner.

`gcc -v`:

    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/9.2.0/lto-wrapper
    Target: x86_64-pc-linux-gnu
    Configured with:
/var/tmp/portage/sys-devel/gcc-9.2.0-r3/work/gcc-9.2.0/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/9.2.0
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/g++-v9
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/9.2.0/python
--enable-objc-gc --enable-languages=c,c++,d,go,objc,obj-c++,fortran
--enable-obsolete --enable-secureplt --disable-werror --with-system-zlib
--enable-nls --without-included-gettext --enable-checking=release
--with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 9.2.0-r3 p4'
--disable-esp --enable-libstdcxx-time --with-build-config=bootstrap-lto
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64
--disable-altivec --disable-fixed-point --enable-targets=all --enable-libgomp
--disable-libmudflap --disable-libssp --disable-systemtap
--enable-vtable-verify --enable-lto --with-isl --disable-isl-version-check
--enable-default-pie --enable-default-ssp
    Thread model: posix
    gcc version 9.2.0 (Gentoo 9.2.0-r3 p4)

`-Wall -Wextra` reports nothing.

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

* [Bug tree-optimization/94086] Missed optimization when converting a bitfield to an integer on x86-64
  2020-03-07 19:33 [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64 larrowe.semaj11 at gmail dot com
@ 2020-03-08  4:30 ` pinskia at gcc dot gnu.org
  2020-03-08 19:05 ` alex_lop at walla dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-03-08  4:30 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu.org
           Severity|normal                      |enhancement
          Component|c                           |tree-optimization

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

* [Bug tree-optimization/94086] Missed optimization when converting a bitfield to an integer on x86-64
  2020-03-07 19:33 [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64 larrowe.semaj11 at gmail dot com
  2020-03-08  4:30 ` [Bug tree-optimization/94086] " pinskia at gcc dot gnu.org
@ 2020-03-08 19:05 ` alex_lop at walla dot com
  2020-03-09  9:40 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: alex_lop at walla dot com @ 2020-03-08 19:05 UTC (permalink / raw)
  To: gcc-bugs

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

alex_lop at walla dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |alex_lop at walla dot com

--- Comment #1 from alex_lop at walla dot com ---
Relevant discussion on stackoverflow:
https://stackoverflow.com/q/60580591/5218277

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

* [Bug tree-optimization/94086] Missed optimization when converting a bitfield to an integer on x86-64
  2020-03-07 19:33 [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64 larrowe.semaj11 at gmail dot com
  2020-03-08  4:30 ` [Bug tree-optimization/94086] " pinskia at gcc dot gnu.org
  2020-03-08 19:05 ` alex_lop at walla dot com
@ 2020-03-09  9:40 ` rguenth at gcc dot gnu.org
  2020-05-11  5:21 ` egallager at gcc dot gnu.org
  2021-08-22  0:29 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-03-09  9:40 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-03-09

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  This is another task for a combined bswap/store-merging where the
bswap tracking would need to be extended to cover bits.

Also part of the reason for the missed optimization is that on GIMPLE we
think 'half' is memory but in reality it is in a register.

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

* [Bug tree-optimization/94086] Missed optimization when converting a bitfield to an integer on x86-64
  2020-03-07 19:33 [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64 larrowe.semaj11 at gmail dot com
                   ` (2 preceding siblings ...)
  2020-03-09  9:40 ` rguenth at gcc dot gnu.org
@ 2020-05-11  5:21 ` egallager at gcc dot gnu.org
  2021-08-22  0:29 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: egallager at gcc dot gnu.org @ 2020-05-11  5:21 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
Possibly related to and/or a dup of bug 83784?

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

* [Bug tree-optimization/94086] Missed optimization when converting a bitfield to an integer on x86-64
  2020-03-07 19:33 [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64 larrowe.semaj11 at gmail dot com
                   ` (3 preceding siblings ...)
  2020-05-11  5:21 ` egallager at gcc dot gnu.org
@ 2021-08-22  0:29 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-22  0:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The testcase in comment #0 in GCC11+ GCC can optimize it to just a move
followed by a return.  Note the optimization only happens on the RTL level
even.
I have not looked into what allowed this to be optimized yet.

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

end of thread, other threads:[~2021-08-22  0:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-07 19:33 [Bug c/94086] New: Missed optimization when converting a bitfield to an integer on x86-64 larrowe.semaj11 at gmail dot com
2020-03-08  4:30 ` [Bug tree-optimization/94086] " pinskia at gcc dot gnu.org
2020-03-08 19:05 ` alex_lop at walla dot com
2020-03-09  9:40 ` rguenth at gcc dot gnu.org
2020-05-11  5:21 ` egallager at gcc dot gnu.org
2021-08-22  0:29 ` pinskia 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).