public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement
@ 2015-01-21 17:42 aaz@q-fu.com
  2015-01-21 19:18 ` [Bug tree-optimization/64718] " hjl.tools at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: aaz@q-fu.com @ 2015-01-21 17:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 64718
           Summary: Bad 16-bit bswap replacement
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aaz@q-fu.com
            Target: i686-pc-linux-gnu

This code is reduced from htons((int)port) on FreeBSD.

The expression gets replaced with a rotation in a 32-bit register
instead of a 16-bit register, so the upper byte is lost.

int swap(int x) {
    return (unsigned short)((unsigned short)x << 8 | (unsigned short)x >> 8);
}

gcc -O2 -S swap.c

swap:
        movl    4(%esp), %eax
        roll    $8, %eax       # should be %ax
        movzwl  %ax, %eax
        ret


Complete program:

#include <stdio.h>

int swap(int x) {
    return (unsigned short)((unsigned short)x << 8 | (unsigned short)x >> 8);
}

int a = 0x1234;
int main() {
    int b = 0x1234;
    printf("%x -> %x\n", a, swap(a));
    printf("%x -> %x\n", b, swap(b));
}

gcc -O2 swap.c && ./a.out

1234 -> 3400
1234 -> 3412


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

* [Bug tree-optimization/64718] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
@ 2015-01-21 19:18 ` hjl.tools at gmail dot com
  2015-01-21 19:47 ` hjl.tools at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hjl.tools at gmail dot com @ 2015-01-21 19:18 UTC (permalink / raw)
  To: gcc-bugs

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

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-01-21
   Target Milestone|---                         |5.0
     Ever confirmed|0                           |1


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

* [Bug tree-optimization/64718] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
  2015-01-21 19:18 ` [Bug tree-optimization/64718] " hjl.tools at gmail dot com
@ 2015-01-21 19:47 ` hjl.tools at gmail dot com
  2015-01-21 19:48 ` hjl.tools at gmail dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hjl.tools at gmail dot com @ 2015-01-21 19:47 UTC (permalink / raw)
  To: gcc-bugs

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

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thomas.preudhomme at arm dot com

--- Comment #1 from H.J. Lu <hjl.tools at gmail dot com> ---
It is caused by r219256.


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

* [Bug tree-optimization/64718] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
  2015-01-21 19:18 ` [Bug tree-optimization/64718] " hjl.tools at gmail dot com
  2015-01-21 19:47 ` hjl.tools at gmail dot com
@ 2015-01-21 19:48 ` hjl.tools at gmail dot com
  2015-01-21 19:51 ` hjl.tools at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hjl.tools at gmail dot com @ 2015-01-21 19:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from H.J. Lu <hjl.tools at gmail dot com> ---
A run-time testcase:

[hjl@gnu-mic-2 gcc-regression]$ cat pr64718.c
int
__attribute__ ((noinline, noclone))
swap(int x)
{
  return (unsigned short)((unsigned short)x << 8 | (unsigned short)x >> 8);
}

int a = 0x1234;

int
main()
{
  int b = 0x1234;
  if (swap (a) != 0x3412)
    __builtin_abort ();
  if (swap (b) != 0x3412)
    __builtin_abort ();
  return 0;
}
[hjl@gnu-mic-2 gcc-regression]$


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

* [Bug tree-optimization/64718] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
                   ` (2 preceding siblings ...)
  2015-01-21 19:48 ` hjl.tools at gmail dot com
@ 2015-01-21 19:51 ` hjl.tools at gmail dot com
  2015-01-22  5:56 ` thopre01 at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hjl.tools at gmail dot com @ 2015-01-21 19:51 UTC (permalink / raw)
  To: gcc-bugs

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

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|i686-pc-linux-gnu           |x86
           Priority|P3                          |P1

--- Comment #3 from H.J. Lu <hjl.tools at gmail dot com> ---
Also happens on x86-64.


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

* [Bug tree-optimization/64718] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
                   ` (3 preceding siblings ...)
  2015-01-21 19:51 ` hjl.tools at gmail dot com
@ 2015-01-22  5:56 ` thopre01 at gcc dot gnu.org
  2015-01-28 10:20 ` thopre01 at gcc dot gnu.org
  2015-02-05 10:09 ` [Bug tree-optimization/64718] [5 Regression] " jakub at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: thopre01 at gcc dot gnu.org @ 2015-01-22  5:56 UTC (permalink / raw)
  To: gcc-bugs

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

thopre01 at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED

--- Comment #4 from thopre01 at gcc dot gnu.org ---
The problem comes from:

bswap_type = TREE_TYPE (src); in bswap_replace when dealing with 16-bit bswap.
I got a fix for that and testing is underway. Thanks for the testcase.

Best regards.


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

* [Bug tree-optimization/64718] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
                   ` (4 preceding siblings ...)
  2015-01-22  5:56 ` thopre01 at gcc dot gnu.org
@ 2015-01-28 10:20 ` thopre01 at gcc dot gnu.org
  2015-02-05 10:09 ` [Bug tree-optimization/64718] [5 Regression] " jakub at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: thopre01 at gcc dot gnu.org @ 2015-01-28 10:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Thomas Preud'homme <thopre01 at gcc dot gnu.org> ---
Author: thopre01
Date: Wed Jan 28 10:20:19 2015
New Revision: 220203

URL: https://gcc.gnu.org/viewcvs?rev=220203&root=gcc&view=rev
Log:
2015-01-28  Thomas Preud'homme  <thomas.preudhomme@arm.com>

    gcc/
    PR tree-optimization/64718
    * tree-ssa-math-opts.c (pass_optimize_bswap::execute): Make bswap_type
    be a 16bit unsigned integer when n->range is 16.
    (bswap_replace): Convert src to that type if necessary for all bswap
    sizes.  Fix rotation right notation in nearby comment.  Use bswap_type
    set in pass_optimize_bswap::execute ().

    gcc/testsuite/
    PR tree-optimization/64718
    * gcc.c-torture/execute/pr64718.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr64718.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-math-opts.c


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

* [Bug tree-optimization/64718] [5 Regression] Bad 16-bit bswap replacement
  2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
                   ` (5 preceding siblings ...)
  2015-01-28 10:20 ` thopre01 at gcc dot gnu.org
@ 2015-02-05 10:09 ` jakub at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2015-02-05 10:09 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
                 CC|                            |jakub at gcc dot gnu.org
         Resolution|---                         |FIXED
            Summary|Bad 16-bit bswap            |[5 Regression] Bad 16-bit
                   |replacement                 |bswap replacement

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.


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

end of thread, other threads:[~2015-02-05 10:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-21 17:42 [Bug tree-optimization/64718] New: Bad 16-bit bswap replacement aaz@q-fu.com
2015-01-21 19:18 ` [Bug tree-optimization/64718] " hjl.tools at gmail dot com
2015-01-21 19:47 ` hjl.tools at gmail dot com
2015-01-21 19:48 ` hjl.tools at gmail dot com
2015-01-21 19:51 ` hjl.tools at gmail dot com
2015-01-22  5:56 ` thopre01 at gcc dot gnu.org
2015-01-28 10:20 ` thopre01 at gcc dot gnu.org
2015-02-05 10:09 ` [Bug tree-optimization/64718] [5 Regression] " jakub 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).