public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/113271] New: [Regression] Wrong code at -O1/2/3 since GCC-9 (could be due to wrong optimization)
@ 2024-01-08 12:08 652023330028 at smail dot nju.edu.cn
  2024-01-08 12:16 ` [Bug tree-optimization/113271] " jakub at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-01-08 12:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113271
           Summary: [Regression] Wrong code at -O1/2/3 since GCC-9 (could
                    be due to wrong optimization)
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we note that gcc may introduce erroneous code due to optimization:

https://godbolt.org/z/8vbn3KdqP

#include <iostream>
int a, b, c;
void func() {
    a=-b;
    //std::cout<<-a<<std::endl;    //Line 5
    c=(a)/(-2147483647-1);
}

int main(){
    b= -2147483647 - 1;
    func();
    std::cout<<c;
}

The output of GCC-O0 is:
1

The output of GCC-O3 is:
0

If we uncomment the fifth line:
#include <iostream>
int a, b, c;
void func() {
    a=-b;
    std::cout<<-a<<std::endl;    //Line 5
    c=(a)/(-2147483647-1);
}

int main(){
    b= -2147483647 - 1;
    func();
    std::cout<<c;
}

we get the correct output (GCC-O3):
-2147483648
1

This issue is probably due to a bad optimization of "func" by gcc -O3:
func():
        mov     DWORD PTR c[rip], 0
        mov     eax, DWORD PTR b[rip]
        neg     eax
        mov     DWORD PTR a[rip], eax
        ret

Thank you very much for your time and effort! We look forward to hearing from
you.

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

end of thread, other threads:[~2024-01-09  6:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-08 12:08 [Bug tree-optimization/113271] New: [Regression] Wrong code at -O1/2/3 since GCC-9 (could be due to wrong optimization) 652023330028 at smail dot nju.edu.cn
2024-01-08 12:16 ` [Bug tree-optimization/113271] " jakub at gcc dot gnu.org
2024-01-09  6:14 ` xry111 at gcc dot gnu.org
2024-01-09  6:27 ` 652023330028 at smail dot nju.edu.cn

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