public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106513] New: bswap is incorrectly generated
@ 2022-08-03 11:56 kristerw at gcc dot gnu.org
  2022-08-03 12:12 ` [Bug tree-optimization/106513] " schwab@linux-m68k.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: kristerw at gcc dot gnu.org @ 2022-08-03 11:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106513
           Summary: bswap is incorrectly generated
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

GCC may incorrectly generate bswap instructions for code not doing a correct
swap. This can be seen by running the function from testsuite/gcc.dg/pr40501.c
as

typedef long int int64_t;

__attribute__((noinline)) int64_t
swap64 (int64_t n)
{
  return (((n & (((int64_t) 0xff) )) << 56) |
          ((n & (((int64_t) 0xff) << 8)) << 40) |
          ((n & (((int64_t) 0xff) << 16)) << 24) |
          ((n & (((int64_t) 0xff) << 24)) << 8) |
          ((n & (((int64_t) 0xff) << 32)) >> 8) |
          ((n & (((int64_t) 0xff) << 40)) >> 24) |
          ((n & (((int64_t) 0xff) << 48)) >> 40) |
          ((n & (((int64_t) 0xff) << 56)) >> 56));
}

int main (void)
{
  volatile int64_t n = 0x8000000000000000l;

  if (swap64(n) != 0xffffffffffffff80l)
    __builtin_abort ();

  return 0;
}

This fails at -Os and higher optimization levels.

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

* [Bug tree-optimization/106513] bswap is incorrectly generated
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
@ 2022-08-03 12:12 ` schwab@linux-m68k.org
  2022-08-03 12:41 ` kristerw at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: schwab@linux-m68k.org @ 2022-08-03 12:12 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> ---
This subexpression has undefined behaviour: (((int64_t) 0xff) << 56).

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

* [Bug tree-optimization/106513] bswap is incorrectly generated
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
  2022-08-03 12:12 ` [Bug tree-optimization/106513] " schwab@linux-m68k.org
@ 2022-08-03 12:41 ` kristerw at gcc dot gnu.org
  2022-08-05 22:15 ` [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: kristerw at gcc dot gnu.org @ 2022-08-03 12:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Krister Walfridsson <kristerw at gcc dot gnu.org> ---
(In reply to Andreas Schwab from comment #1)
> This subexpression has undefined behaviour: (((int64_t) 0xff) << 56).

I thought that was allowed in GCC as the manual says
(https://gcc.gnu.org/onlinedocs/gcc-12.1.0/gcc/Integers-implementation.html#Integers-implementation)
"As an extension to the C language, GCC does not use the latitude given in C99
and C11 only to treat certain aspects of signed ‘<<’ as undefined."

If not, what behavior does the manual refer to?

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

* [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
  2022-08-03 12:12 ` [Bug tree-optimization/106513] " schwab@linux-m68k.org
  2022-08-03 12:41 ` kristerw at gcc dot gnu.org
@ 2022-08-05 22:15 ` pinskia at gcc dot gnu.org
  2022-08-10 13:43 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-05 22:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
      Known to fail|                            |4.5.3, 4.6.4
            Summary|bswap is incorrectly        |[10/11/12/13 Regression]
                   |generated                   |bswap pass misses that >>56
                   |                            |for signed types can be
                   |                            |replicate the sign bit
      Known to work|                            |4.4.7
   Target Milestone|---                         |10.5
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-08-05

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

Better testcase (without the questionable undefined behavior):

typedef long long int int64_t;

__attribute__((noinline)) int64_t
swap64 (int64_t n)
{
  return (((n & (((int64_t) 0xff) )) << 56) |
          ((n & (((int64_t) 0xff) << 8)) << 40) |
          ((n & (((int64_t) 0xff) << 16)) << 24) |
          ((n & (((int64_t) 0xff) << 24)) << 8) |
          ((n & (((int64_t) 0xff) << 32)) >> 8) |
          ((n & (((int64_t) 0xff) << 40)) >> 24) |
          ((n & (((int64_t) 0xff) << 48)) >> 40) |
          ((n & ((int64_t)(0xffull << 56))) >> 56));
}

int main (void)
{
  volatile int64_t n = 0x8000000000000000l;

  if (swap64(n) != 0xffffffffffffff80l)
    __builtin_abort ();

  return 0;
}

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

* [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-08-05 22:15 ` [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit pinskia at gcc dot gnu.org
@ 2022-08-10 13:43 ` rguenth at gcc dot gnu.org
  2022-08-10 13:51 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-08-10 13:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Mine.

diff --git a/gcc/gimple-ssa-store-merging.cc b/gcc/gimple-ssa-store-merging.cc
index 0640168bcc4..b80b8eac444 100644
--- a/gcc/gimple-ssa-store-merging.cc
+++ b/gcc/gimple-ssa-store-merging.cc
@@ -263,7 +263,7 @@ do_shift_rotate (enum tree_code code,
                 int count)
 {
   int i, size = TYPE_PRECISION (n->type) / BITS_PER_UNIT;
-  unsigned head_marker;
+  uint64_t head_marker;

   if (count < 0
       || count >= TYPE_PRECISION (n->type)

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

* [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-08-10 13:43 ` rguenth at gcc dot gnu.org
@ 2022-08-10 13:51 ` rguenth at gcc dot gnu.org
  2022-08-10 14:39 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-08-10 13:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org

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

* [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-08-10 13:51 ` rguenth at gcc dot gnu.org
@ 2022-08-10 14:39 ` cvs-commit at gcc dot gnu.org
  2022-08-10 14:40 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-08-10 14:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 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:f675afa4eeac9910a2c085a95aa04d6d9f2fd8d6

commit r13-2013-gf675afa4eeac9910a2c085a95aa04d6d9f2fd8d6
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Aug 10 15:45:22 2022 +0200

    tree-optimization/106513 - fix mistake in bswap symbolic number shifts

    This fixes a mistake in typing a local variable in the symbolic
    shift routine.

            PR tree-optimization/106513
            * gimple-ssa-store-merging.cc (do_shift_rotate): Use uint64_t
            for head_marker.

            * gcc.dg/torture/pr106513.c: New testcase.

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

* [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2022-08-10 14:39 ` cvs-commit at gcc dot gnu.org
@ 2022-08-10 14:40 ` cvs-commit at gcc dot gnu.org
  2022-10-11 13:04 ` [Bug tree-optimization/106513] [10/11 " cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-08-10 14:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Richard Biener
<rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:ab2ca2dbd528f0564b80fa0e6eda96e0237742bc

commit r12-8677-gab2ca2dbd528f0564b80fa0e6eda96e0237742bc
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Aug 10 15:45:22 2022 +0200

    tree-optimization/106513 - fix mistake in bswap symbolic number shifts

    This fixes a mistake in typing a local variable in the symbolic
    shift routine.

            PR tree-optimization/106513
            * gimple-ssa-store-merging.cc (do_shift_rotate): Use uint64_t
            for head_marker.

            * gcc.dg/torture/pr106513.c: New testcase.

    (cherry picked from commit f675afa4eeac9910a2c085a95aa04d6d9f2fd8d6)

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

* [Bug tree-optimization/106513] [10/11 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2022-08-10 14:40 ` cvs-commit at gcc dot gnu.org
@ 2022-10-11 13:04 ` cvs-commit at gcc dot gnu.org
  2022-10-14 10:47 ` [Bug tree-optimization/106513] [10 " cvs-commit at gcc dot gnu.org
  2022-10-14 10:48 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-11 13:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Richard Biener
<rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:ec718fb7a3a58955017cd80f6a8927afbe340fc4

commit r11-10306-gec718fb7a3a58955017cd80f6a8927afbe340fc4
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Aug 10 15:45:22 2022 +0200

    tree-optimization/106513 - fix mistake in bswap symbolic number shifts

    This fixes a mistake in typing a local variable in the symbolic
    shift routine.

            PR tree-optimization/106513
            * gimple-ssa-store-merging.c (do_shift_rotate): Use uint64_t
            for head_marker.

            * gcc.dg/torture/pr106513.c: New testcase.

    (cherry picked from commit f675afa4eeac9910a2c085a95aa04d6d9f2fd8d6)

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

* [Bug tree-optimization/106513] [10 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2022-10-11 13:04 ` [Bug tree-optimization/106513] [10/11 " cvs-commit at gcc dot gnu.org
@ 2022-10-14 10:47 ` cvs-commit at gcc dot gnu.org
  2022-10-14 10:48 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-14 10:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Richard Biener
<rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:29ad829c93d530e75dee88d2550350c14fcaf8f6

commit r10-11038-g29ad829c93d530e75dee88d2550350c14fcaf8f6
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Aug 10 15:45:22 2022 +0200

    tree-optimization/106513 - fix mistake in bswap symbolic number shifts

    This fixes a mistake in typing a local variable in the symbolic
    shift routine.

            PR tree-optimization/106513
            * gimple-ssa-store-merging.c (do_shift_rotate): Use uint64_t
            for head_marker.

            * gcc.dg/torture/pr106513.c: New testcase.

    (cherry picked from commit f675afa4eeac9910a2c085a95aa04d6d9f2fd8d6)

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

* [Bug tree-optimization/106513] [10 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit
  2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2022-10-14 10:47 ` [Bug tree-optimization/106513] [10 " cvs-commit at gcc dot gnu.org
@ 2022-10-14 10:48 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-10-14 10:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |10.4.1
         Resolution|---                         |FIXED
      Known to fail|                            |10.4.0
             Status|ASSIGNED                    |RESOLVED

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

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

end of thread, other threads:[~2022-10-14 10:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-03 11:56 [Bug tree-optimization/106513] New: bswap is incorrectly generated kristerw at gcc dot gnu.org
2022-08-03 12:12 ` [Bug tree-optimization/106513] " schwab@linux-m68k.org
2022-08-03 12:41 ` kristerw at gcc dot gnu.org
2022-08-05 22:15 ` [Bug tree-optimization/106513] [10/11/12/13 Regression] bswap pass misses that >>56 for signed types can be replicate the sign bit pinskia at gcc dot gnu.org
2022-08-10 13:43 ` rguenth at gcc dot gnu.org
2022-08-10 13:51 ` rguenth at gcc dot gnu.org
2022-08-10 14:39 ` cvs-commit at gcc dot gnu.org
2022-08-10 14:40 ` cvs-commit at gcc dot gnu.org
2022-10-11 13:04 ` [Bug tree-optimization/106513] [10/11 " cvs-commit at gcc dot gnu.org
2022-10-14 10:47 ` [Bug tree-optimization/106513] [10 " cvs-commit at gcc dot gnu.org
2022-10-14 10:48 ` 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).