public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109507] New: Optimizer creates incorrect program
@ 2023-04-13 22:47 aran at 100acres dot us
  2023-04-13 22:50 ` [Bug c/109507] " sjames at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: aran at 100acres dot us @ 2023-04-13 22:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109507
           Summary: Optimizer creates incorrect program
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aran at 100acres dot us
  Target Milestone: ---

Created attachment 54854
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54854&action=edit
Example program.

The attached program should print 0 and does with optimization level zero (0). 
However, with optimization at any non-zero value (e.g., g, s, 1, 2) it prints
1.  This is on an Intel I7-2600.

The code that the optimizer writes reduces to a single test instruction that
checks if x is less or equal to zero.  However, this is incorrect for the most
negative number.  

To reproduce compile the attached file with -O0 and with -O1.

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

* [Bug c/109507] Optimizer creates incorrect program
  2023-04-13 22:47 [Bug c/109507] New: Optimizer creates incorrect program aran at 100acres dot us
@ 2023-04-13 22:50 ` sjames at gcc dot gnu.org
  2023-04-13 22:53 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: sjames at gcc dot gnu.org @ 2023-04-13 22:50 UTC (permalink / raw)
  To: gcc-bugs

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

Sam James <sjames at gcc dot gnu.org> changed:

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

--- Comment #1 from Sam James <sjames at gcc dot gnu.org> ---
UBSAN with GCC:
```
$ /tmp/foo
/tmp/foo.c:4:7: runtime error: negation of -2147483648 cannot be represented in
type 'int'; cast to an unsigned type to negate this value to itself
0
```

UBSAN with Clang:
```
/tmp/foo.c:4:15: runtime error: signed integer overflow: 2147483647 + 1 cannot
be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /tmp/foo.c:4:15 in
0
```

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

* [Bug c/109507] Optimizer creates incorrect program
  2023-04-13 22:47 [Bug c/109507] New: Optimizer creates incorrect program aran at 100acres dot us
  2023-04-13 22:50 ` [Bug c/109507] " sjames at gcc dot gnu.org
@ 2023-04-13 22:53 ` pinskia at gcc dot gnu.org
  2023-04-14 10:55 ` redi at gcc dot gnu.org
  2023-04-14 16:30 ` aran at 100acres dot us
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-13 22:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
-fsanitize=undefined, at runtime gives:

/app/example.cpp:4:7: runtime error: negation of -2147483648 cannot be
represented in type 'int'; cast to an unsigned type to negate this value to
itself

Note the message is slightly wrong but points you to the right reason.
There is an overflow. because ~0x80000000 is 2147483648 and then you add 1 to
it giving you an overflow which is undefined. Note ~a+1 is the same as -a as
both cases will overflow at the same time with 0x80000000 which is why GCC is
providing that error message rather then on dealing with +1 like clang does:

Note clang gives:
/app/example.cpp:4:15: runtime error: signed integer overflow: 2147483647 + 1
cannot be represented in type 'int'

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

* [Bug c/109507] Optimizer creates incorrect program
  2023-04-13 22:47 [Bug c/109507] New: Optimizer creates incorrect program aran at 100acres dot us
  2023-04-13 22:50 ` [Bug c/109507] " sjames at gcc dot gnu.org
  2023-04-13 22:53 ` pinskia at gcc dot gnu.org
@ 2023-04-14 10:55 ` redi at gcc dot gnu.org
  2023-04-14 16:30 ` aran at 100acres dot us
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2023-04-14 10:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
When you created this bug report there was a red banner at the top of the page
that begins by asking you to read https://gcc.gnu.org/bugs/ and that tells you
to try -fsanitize=undefined

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

* [Bug c/109507] Optimizer creates incorrect program
  2023-04-13 22:47 [Bug c/109507] New: Optimizer creates incorrect program aran at 100acres dot us
                   ` (2 preceding siblings ...)
  2023-04-14 10:55 ` redi at gcc dot gnu.org
@ 2023-04-14 16:30 ` aran at 100acres dot us
  3 siblings, 0 replies; 5+ messages in thread
From: aran at 100acres dot us @ 2023-04-14 16:30 UTC (permalink / raw)
  To: gcc-bugs

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

Aran Clauson <aran at 100acres dot us> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |CLOSED

--- Comment #4 from Aran Clauson <aran at 100acres dot us> ---
Disregard.

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

end of thread, other threads:[~2023-04-14 16:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-13 22:47 [Bug c/109507] New: Optimizer creates incorrect program aran at 100acres dot us
2023-04-13 22:50 ` [Bug c/109507] " sjames at gcc dot gnu.org
2023-04-13 22:53 ` pinskia at gcc dot gnu.org
2023-04-14 10:55 ` redi at gcc dot gnu.org
2023-04-14 16:30 ` aran at 100acres dot us

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