public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
@ 2020-10-19 13:50 suochenyao at 163 dot com
  2020-10-19 14:06 ` [Bug c/97493] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: suochenyao at 163 dot com @ 2020-10-19 13:50 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97493
           Summary: generate wrong code with "-Os -fno-toplevel-reorder
                    -frename-registers"
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: suochenyao at 163 dot com
  Target Milestone: ---

Description:
I think gcc generates wrong code with -Os -fno-toplevel-reorder
-frename-registers.
But no difference is found with optimization levels.
No message and difference occurs with "gcc -Wall -Wextra" and
"-fno-strict-aliasing -fwrapv"
*******************************************************************************
OS and Platform:
CentOS Linux release 7.8.2003 (Core), x86_64 GNU/Linux
*******************************************************************************
Program:
int printf(const char *, ...);
union uuu{
  short a;
  long b;
} c={0};
long *d = &c.b;
short *e = &c.a;
short f=0;
int main() {
  *d = 3;
  f = ++*e;
  printf("%d\n", c.a);
}
*******************************************************************************
Gcc Version:
$ gcc -v
Using built-in specs.
COLLECT_GCC=/home/suocy/bin/gcc-dev/bin/gcc
COLLECT_LTO_WRAPPER=/home/suocy/bin/gcc-dev/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/home/suocy/bin/gcc-dev
--disable-multilib --enable-languages=c,c++
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.0 20201018 (experimental) (GCC)
*******************************************************************************
Command Line:
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -O0 a.c -o a.out0
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -O1 a.c -o a.out1
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -O2 a.c -o a.out2
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -O3 a.c -o a.out3
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -Os a.c -o a.outs
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -Os -fno-toplevel-reorder
-frename-registers a.c -o a.outf
$ ./a.out0 > out0
$ ./a.out1 > out1
$ ./a.out2 > out2
$ ./a.out3 > out3
$ ./a.outs > outs
$ ./a.outf > outf
$ diff out0 out1
$ diff out0 out2
$ diff out0 out3
$ diff out0 outs
$ diff out0 outf
1c1
< 4
---
> 1

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

* [Bug c/97493] generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
  2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
@ 2020-10-19 14:06 ` jakub at gcc dot gnu.org
  2020-10-19 15:23 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-10-19 14:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |jakub at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The testcase is invalid.  In standard C only one union member may be active at
a time, and while GCC as extension allows type puning through unions, the union
must be visible in the expressions accessing it, so taking addresses of the
union elts and dereferencing them through those pointers where the union is not
visible is UB.

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

* [Bug c/97493] generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
  2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
  2020-10-19 14:06 ` [Bug c/97493] " jakub at gcc dot gnu.org
@ 2020-10-19 15:23 ` rguenth at gcc dot gnu.org
  2020-10-19 15:39 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-10-19 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |WAITING
   Last reconfirmed|                            |2020-10-19
     Ever confirmed|0                           |1
         Resolution|INVALID                     |---

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
But he's using -fno-strict-aliasing so at least that part isn't relevant?
That said, the C rules still make it an INVALID testcase but I think
GCC-wise it could be well-defined, so understanding what goes wrong would be
nice.

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

* [Bug c/97493] generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
  2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
  2020-10-19 14:06 ` [Bug c/97493] " jakub at gcc dot gnu.org
  2020-10-19 15:23 ` rguenth at gcc dot gnu.org
@ 2020-10-19 15:39 ` jakub at gcc dot gnu.org
  2020-10-20 12:59 ` suochenyao at 163 dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-10-19 15:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I can't reproduce it with -fno-strict-aliasing, only when that option is
missing.

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

* [Bug c/97493] generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
  2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
                   ` (2 preceding siblings ...)
  2020-10-19 15:39 ` jakub at gcc dot gnu.org
@ 2020-10-20 12:59 ` suochenyao at 163 dot com
  2020-10-20 12:59 ` suochenyao at 163 dot com
  2020-10-30 15:28 ` suochenyao at 163 dot com
  5 siblings, 0 replies; 7+ messages in thread
From: suochenyao at 163 dot com @ 2020-10-20 12:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from suochenyao at 163 dot com <suochenyao at 163 dot com> ---
I made mistake, Jelinek is right.
This testcase is invalid and I can not reproduce it as well.
Sorry for wasting your time, my fault.
I apologize for wasting you time.

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

* [Bug c/97493] generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
  2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
                   ` (3 preceding siblings ...)
  2020-10-20 12:59 ` suochenyao at 163 dot com
@ 2020-10-20 12:59 ` suochenyao at 163 dot com
  2020-10-30 15:28 ` suochenyao at 163 dot com
  5 siblings, 0 replies; 7+ messages in thread
From: suochenyao at 163 dot com @ 2020-10-20 12:59 UTC (permalink / raw)
  To: gcc-bugs

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

suochenyao at 163 dot com <suochenyao at 163 dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|---                         |INVALID

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

* [Bug c/97493] generate wrong code with "-Os -fno-toplevel-reorder -frename-registers"
  2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
                   ` (4 preceding siblings ...)
  2020-10-20 12:59 ` suochenyao at 163 dot com
@ 2020-10-30 15:28 ` suochenyao at 163 dot com
  5 siblings, 0 replies; 7+ messages in thread
From: suochenyao at 163 dot com @ 2020-10-30 15:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from suochenyao at 163 dot com <suochenyao at 163 dot com> ---
*******************************************************************************
I think this can be reproduced with "-fno-strict-aliasing"...
I am not sure whether it can be helpful...
*******************************************************************************
OS and Platform:
CentOS Linux release 7.8.2003 (Core), x86_64 GNU/Linux
*******************************************************************************
Program:
int printf(const char *, ...);
union {
  long a;
  int b;
} c;
int d=0;
long e=0;
long *f = &c.a;
short g=0;
int *h = &c.b;
short i(int j) {
  g = *h;
  *f = 3;
  for (;; e--) {
    if (*h)
      return d;
    *h = j;
  }
  return 0;
}
int main() {
  i(1);
  printf("%d\n", (int)e);
  return 0;
}
*******************************************************************************
gcc version:
$ gcc -v
Using built-in specs.
COLLECT_GCC=/home/suocy/bin/gcc-dev/bin/gcc
COLLECT_LTO_WRAPPER=/home/suocy/bin/gcc-dev/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/home/suocy/bin/gcc-dev/
--disable-multilib --enable-languages=c,c++
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.0 20201029 (experimental) (GCC)
*******************************************************************************
Command Lines:
$ gcc a.c -o a.o1
$ gcc -Wall -Wextra -fno-strict-aliasing -fwrapv -Og -fgcse -fstrict-aliasing
a.c -o a.o2
$ gcc -Og -fgcse -fstrict-aliasing a.c -o a.o3
$ ./a.o1
0
$ ./a.o2
-1
$ ./a.o3
-1

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

end of thread, other threads:[~2020-10-30 15:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-19 13:50 [Bug c/97493] New: generate wrong code with "-Os -fno-toplevel-reorder -frename-registers" suochenyao at 163 dot com
2020-10-19 14:06 ` [Bug c/97493] " jakub at gcc dot gnu.org
2020-10-19 15:23 ` rguenth at gcc dot gnu.org
2020-10-19 15:39 ` jakub at gcc dot gnu.org
2020-10-20 12:59 ` suochenyao at 163 dot com
2020-10-20 12:59 ` suochenyao at 163 dot com
2020-10-30 15:28 ` suochenyao at 163 dot com

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