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

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