public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/104763] New: [12.0] Generate wrong assembly code
@ 2022-03-02 18:01 570070308 at qq dot com
  2022-03-02 18:44 ` [Bug c/104763] " 570070308 at qq dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: 570070308 at qq dot com @ 2022-03-02 18:01 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104763
           Summary: [12.0] Generate wrong assembly code
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 570070308 at qq dot com
  Target Milestone: ---

file test.c:
```
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
void move_up()
{
    for ( size_t* i=(size_t *)(0xb8000+160*24); ; )
    {
        *i=0x0700070007000700;
        //if ( i == (size_t *)(0xb8000+160*24+7*sizeof(size_t)) )
        if ( i == (volatile size_t *)(0xb8000+160*24)+2 )
        {
            break;
        }
        ++i;
    }
    while (1){}
}
```
compile with `gcc -O1 test.c -S` or -O2 or -O3
the assemble code:
```
move_up:
.LFB0:
    .cfi_startproc
    endbr64
    movabsq $504410854964332288, %rax
    movq    %rax, 757512
    movq    %rax, 757520
.L2:
    jmp .L2
    .cfi_endproc
```

But the correct assembly code should be:
```
move_up:
    movabsq $504410854964332288, %rax
    movq    %rax, 757504
    movq    %rax, 757512
    movq    %rax, 757520
.L2:
    jmp .L2
```

I try to compile with -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -Wall -Wextra and the result is same.

This is the full gcc compile info:
```
ig@ig-virtual-machine:~/os/myos/temp$ gcc -O1 test.c -S -v
Using built-in specs.
COLLECT_GCC=gcc
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
12-20220222-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-12
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-12-TT8eTw/gcc-12-12-20220222/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-TT8eTw/gcc-12-12-20220222/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.0.1 20220222 (experimental) [master r12-7325-g2f59f067610]
(Ubuntu 12-20220222-1ubuntu1) 
COLLECT_GCC_OPTIONS='-O1' '-S' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/12/cc1 -quiet -v -imultiarch x86_64-linux-gnu
test.c -quiet -dumpbase test.c -dumpbase-ext .c -mtune=generic -march=x86-64
-O1 -version -o test.s -fasynchronous-unwind-tables -fstack-protector-strong
-Wformat -Wformat-security -fstack-clash-protection -fcf-protection
GNU C17 (Ubuntu 12-20220222-1ubuntu1) version 12.0.1 20220222 (experimental)
[master r12-7325-g2f59f067610] (x86_64-linux-gnu)
        compiled by GNU C version 12.0.1 20220222 (experimental) [master
r12-7325-g2f59f067610], GMP version 6.2.1, MPFR version 4.1.0, MPC version
1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/12/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/12/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C17 (Ubuntu 12-20220222-1ubuntu1) version 12.0.1 20220222 (experimental)
[master r12-7325-g2f59f067610] (x86_64-linux-gnu)
        compiled by GNU C version 12.0.1 20220222 (experimental) [master
r12-7325-g2f59f067610], GMP version 6.2.1, MPFR version 4.1.0, MPC version
1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: b0de683ccdd6a31c725d9273cea9e1f8
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-O1' '-S' '-v' '-mtune=generic' '-march=x86-64'
```
The gcc is installed by apt on ubuntu 22.04.

If I change the `while (1){}` to `return ;` then the code will be right.

Or change the `+2` of `(volatile size_t *)(0xb8000+160*24)+2` to `+1` or `+0`.

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

* [Bug c/104763] [12.0] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
@ 2022-03-02 18:44 ` 570070308 at qq dot com
  2022-03-02 18:58 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: 570070308 at qq dot com @ 2022-03-02 18:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from 。 <570070308 at qq dot com> ---
change `*i=0x0700070007000700;` to `*(volatile size_t *)i=0x0700070007000700;`
will fix it.
This is my mistake

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

* [Bug c/104763] [12.0] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
  2022-03-02 18:44 ` [Bug c/104763] " 570070308 at qq dot com
@ 2022-03-02 18:58 ` jakub at gcc dot gnu.org
  2022-03-03  5:08 ` [Bug middle-end/104763] " 570070308 at qq dot com
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-03-02 18:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Can't reproduce with -O2, with -O1 there are 2 stores instead of 3
before the endless loop starting with
r9-384-gf1bcb061d172ca7e3bdcc46476b20776382a2974

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

* [Bug middle-end/104763] [12.0] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
  2022-03-02 18:44 ` [Bug c/104763] " 570070308 at qq dot com
  2022-03-02 18:58 ` jakub at gcc dot gnu.org
@ 2022-03-03  5:08 ` 570070308 at qq dot com
  2022-03-03 11:35 ` [Bug middle-end/104763] [12 Regression] " marxin at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: 570070308 at qq dot com @ 2022-03-03  5:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from 。 <570070308 at qq dot com> ---
(In reply to Jakub Jelinek from comment #2)
> Can't reproduce with -O2, with -O1 there are 2 stores instead of 3
> before the endless loop starting with
> r9-384-gf1bcb061d172ca7e3bdcc46476b20776382a2974

edit `+2` to `+7` then -O3 will be wrong too.

test.c:
```
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
void move_up()
{
    for ( size_t* i=(size_t *)(0xb8000+160*24); ; )
    {
        *i=0x0700070007000700;
        if ( i == (size_t *)(0xb8000+160*24)+7 )
        {
            break;
        }
        ++i;
    }
    while (1){}
}
```
assembly with -O3:
```
move_up:
.LFB0:
        .cfi_startproc
        endbr64
        movabsq $504410854964332288, %rax
        movq    %rax, 757512
        movq    %rax, 757520
        movq    %rax, 757528
        movq    %rax, 757536
        movq    %rax, 757544
        movq    %rax, 757552
        movq    %rax, 757560
.L2:
        jmp     .L2
        .cfi_endproc
```

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

* [Bug middle-end/104763] [12 Regression] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
                   ` (2 preceding siblings ...)
  2022-03-03  5:08 ` [Bug middle-end/104763] " 570070308 at qq dot com
@ 2022-03-03 11:35 ` marxin at gcc dot gnu.org
  2022-03-07  8:03 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-03-03 11:35 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

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

--- Comment #4 from Martin Liška <marxin at gcc dot gnu.org> ---
Likely changed with r9-384-gf1bcb061d172ca7e.

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

* [Bug middle-end/104763] [12 Regression] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
                   ` (3 preceding siblings ...)
  2022-03-03 11:35 ` [Bug middle-end/104763] [12 Regression] " marxin at gcc dot gnu.org
@ 2022-03-07  8:03 ` rguenth at gcc dot gnu.org
  2022-03-07  8:07 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-03-07  8:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-03-07
     Ever confirmed|0                           |1
   Target Milestone|---                         |12.0
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think we have a duplicate somewhere involving EH.  The key here is that we
are missing the VUSE on the "return" - and in this case it's (again) missing
the
return, just with using an endless loop (the other PR has a EH re-throw or so).

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

* [Bug middle-end/104763] [12 Regression] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
                   ` (4 preceding siblings ...)
  2022-03-07  8:03 ` rguenth at gcc dot gnu.org
@ 2022-03-07  8:07 ` rguenth at gcc dot gnu.org
  2022-03-07  8:12 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-03-07  8:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=85803

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
PR85803 - I'd say this bug is a duplicate (to some extent).  Simplified
testcase
that, with the help of alarm() and signal handlers could be turned into a
runtime FAIL.

typedef __SIZE_TYPE__ size_t;
void move_up(void *p)
{
  for ( size_t* i=(size_t *)(p); ; )
    {
      *i=0x0700070007000700;
      if (i == (size_t *)(p)+2 )
        break;
      ++i;
    }
  while (1){}
}

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

* [Bug middle-end/104763] [12 Regression] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
                   ` (5 preceding siblings ...)
  2022-03-07  8:07 ` rguenth at gcc dot gnu.org
@ 2022-03-07  8:12 ` rguenth at gcc dot gnu.org
  2022-03-07  8:37 ` 570070308 at qq dot com
  2022-03-07  9:05 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-03-07  8:12 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note that the case of an endless loop is somewhat special since the store
is dead there since there is no way to reach a load from that point with
C standard methods.  So one could also argue the optimization is valid
and this bug is invalid.

How did you end up with this case?

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

* [Bug middle-end/104763] [12 Regression] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
                   ` (6 preceding siblings ...)
  2022-03-07  8:12 ` rguenth at gcc dot gnu.org
@ 2022-03-07  8:37 ` 570070308 at qq dot com
  2022-03-07  9:05 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: 570070308 at qq dot com @ 2022-03-07  8:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from 。 <570070308 at qq dot com> ---
(In reply to Richard Biener from comment #7)
> Note that the case of an endless loop is somewhat special since the store
> is dead there since there is no way to reach a load from that point with
> C standard methods.  So one could also argue the optimization is valid
> and this bug is invalid.
> 
> How did you end up with this case?

I agree with this bug is invaild.
I do something on memory 0xb8000, this is because the 0xb8000 is the video
memory, and can display on screen.
However gcc don't know it, and I think it doesn't need to know it. Gcc can
simply think that all the writing/reading on memory is useless, unless the
writing/reading on memory will affect subsequent function calls or the function
return. This way gcc can better optimize the code.
According to this, the while(1){} will never return or call a function, so gcc
can think that all the writing/reading on 0xb8000 before while(1){} is useless.
A way to tell gcc that writing/reading on 0xb8000 will make an impact is to
change `*i` to `*(volatile size_t *)i`, and it really work.

So I think:
test.c:
```
void move_up()
{
    for ( size_t* i=(size_t *)(0xb8000+160*24); ; )
    {
        *i=0x0700070007000700;
        if ( i == (size_t *)(0xb8000+160*24)+2 )
        {
            break;
        }
        ++i;
    }
    while (1){}
}
```
compile to
```
move_up:
    jmp move_up
```
and test.c:
```
void move_up()
{
    for ( size_t* i=(size_t *)(0xb8000+160*24); ; )
    {
        *(volatile size_t *)i=0x0700070007000700;
        if ( i == (size_t *)(0xb8000+160*24)+2 )
        {
            break;
        }
        ++i;
    }
    while (1){}
}
```
compile to:
```
move_up:
    movabsq $504410854964332288, %rax
    movq    %rax, 757504
    movq    %rax, 757512
    movq    %rax, 757520
.L2:
    jmp .L2
```
is the best.

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

* [Bug middle-end/104763] [12 Regression] Generate wrong assembly code
  2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
                   ` (7 preceding siblings ...)
  2022-03-07  8:37 ` 570070308 at qq dot com
@ 2022-03-07  9:05 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-03-07  9:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
OK, thanks for confirming.  There is an issue in DSE but all aspects should be
tracked by PR85803 already.

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

end of thread, other threads:[~2022-03-07  9:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-02 18:01 [Bug c/104763] New: [12.0] Generate wrong assembly code 570070308 at qq dot com
2022-03-02 18:44 ` [Bug c/104763] " 570070308 at qq dot com
2022-03-02 18:58 ` jakub at gcc dot gnu.org
2022-03-03  5:08 ` [Bug middle-end/104763] " 570070308 at qq dot com
2022-03-03 11:35 ` [Bug middle-end/104763] [12 Regression] " marxin at gcc dot gnu.org
2022-03-07  8:03 ` rguenth at gcc dot gnu.org
2022-03-07  8:07 ` rguenth at gcc dot gnu.org
2022-03-07  8:12 ` rguenth at gcc dot gnu.org
2022-03-07  8:37 ` 570070308 at qq dot com
2022-03-07  9:05 ` 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).