Bug 58454 - Potentially wrong(or at least weird/inconsistent) code generation with -O2 -fno-strict-overflow
Summary: Potentially wrong(or at least weird/inconsistent) code generation with -O2 -f...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.8.1
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: documentation
Depends on:
Blocks:
 
Reported: 2013-09-18 01:28 UTC by mednafen
Modified: 2021-05-14 10:00 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-09-18 00:00:00


Attachments
Testcase program. (259 bytes, text/x-csrc)
2013-09-18 01:28 UTC, mednafen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mednafen 2013-09-18 01:28:19 UTC
Created attachment 30844 [details]
Testcase program.

Working:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O0 -o halt halt.c 
XXX@willow:~/halt$ ./halt 
IMm3: ****
IMm2: ***
IMm1: **
IM: *


????:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -o halt halt.c 
XXX@willow:~/halt$ ./halt 
IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****Aborted


Working:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-aggressive-loop-optimizations -o halt halt.c
XXX@willow:~/halt$ ./halt 
IMm3: ****************************************************************************************************Aborted


Broken:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-strict-overflow -o halt halt.c 
XXX@willow:~/halt$ ./halt 
IMm3: ****
IMm2: ***
IMm1: *********************************************************************************************Aborted


Broken:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-aggressive-loop-optimizations -fno-strict-overflow -o halt halt.c 
XXX@willow:~/halt$ ./halt 
IMm3: ****
IMm2: ***
IMm1: *********************************************************************************************Aborted


Working:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-strict-overflow -fno-tree-vrp -o halt halt.c
XXX@willow:~/halt$ ./halt
IMm3: ****
IMm2: ***
IMm1: **
IM: *


Working:
XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-strict-overflow -fwrapv -o halt halt.c
XXX@willow:~/halt$ ./halt 
IMm3: ****
IMm2: ***
IMm1: **
IM: *


XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/usr/local/gcc-4.8.1/bin/gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.8.1/libexec/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.8.1/configure --prefix=/usr/local/gcc-4.8.1
Thread model: posix
gcc version 4.8.1 (GCC)
Comment 1 pinskia@gmail.com 2013-09-18 05:00:27 UTC
All of these functions overflow the loop induction variable so only -fwrapv will provide the behavior you want for all of the functions.  The inconsistent behavior is due to the overflows happening for induction variables.  If it was not an induction variable then -fno-strict-overflow would have worked..-fno-strict-overflow is only defined to work for non loop variables.

Thanks,
Andrew

On Sep 17, 2013, at 6:28 PM, "mednafen at gmail dot com" <gcc-bugzilla@gcc.gnu.org> wrote:

> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58454
> 
>            Bug ID: 58454
>           Summary: Potentially wrong(or at least weird/inconsistent) code
>                    generation with -O2 -fno-strict-overflow
>           Product: gcc
>           Version: 4.8.1
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c
>          Assignee: unassigned at gcc dot gnu.org
>          Reporter: mednafen at gmail dot com
> 
> Created attachment 30844 [details]
>  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30844&action=edit
> Testcase program.
> 
> Working:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O0 -o halt halt.c 
> XXX@willow:~/halt$ ./halt 
> IMm3: ****
> IMm2: ***
> IMm1: **
> IM: *
> 
> 
> ????:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -o halt halt.c 
> XXX@willow:~/halt$ ./halt 
> IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3:
> ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3:
> ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3: ****IMm3:
> ****IMm3: ****Aborted
> 
> 
> Working:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2
> -fno-aggressive-loop-optimizations -o halt halt.c
> XXX@willow:~/halt$ ./halt 
> IMm3:
> ****************************************************************************************************Aborted
> 
> 
> Broken:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-strict-overflow
> -o halt halt.c 
> XXX@willow:~/halt$ ./halt 
> IMm3: ****
> IMm2: ***
> IMm1:
> *********************************************************************************************Aborted
> 
> 
> Broken:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2
> -fno-aggressive-loop-optimizations -fno-strict-overflow -o halt halt.c 
> XXX@willow:~/halt$ ./halt 
> IMm3: ****
> IMm2: ***
> IMm1:
> *********************************************************************************************Aborted
> 
> 
> Working:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-strict-overflow
> -fno-tree-vrp -o halt halt.c
> XXX@willow:~/halt$ ./halt
> IMm3: ****
> IMm2: ***
> IMm1: **
> IM: *
> 
> 
> Working:
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -Wall -O2 -fno-strict-overflow
> -fwrapv -o halt halt.c
> XXX@willow:~/halt$ ./halt 
> IMm3: ****
> IMm2: ***
> IMm1: **
> IM: *
> 
> 
> XXX@willow:~/halt$ /usr/local/gcc-4.8.1/bin/gcc -v
> Using built-in specs.
> COLLECT_GCC=/usr/local/gcc-4.8.1/bin/gcc
> COLLECT_LTO_WRAPPER=/usr/local/gcc-4.8.1/libexec/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper
> Target: x86_64-unknown-linux-gnu
> Configured with: ../gcc-4.8.1/configure --prefix=/usr/local/gcc-4.8.1
> Thread model: posix
> gcc version 4.8.1 (GCC)
Comment 2 mednafen 2013-09-18 05:56:50 UTC
Your assertion conflicts with the gcc 4.2 release change-list at http://gcc.gnu.org/gcc-4.2/changes.html when the strict-overflow options were added.

Additionally, -fwrapv produces unnecessarily bloated code compared to -fno-strict-overflow, in my experience.
Comment 3 Richard Biener 2013-09-18 08:45:29 UTC
-fno-strict-overflow should have never been added because it certainly does not disable all use of undefined signed overflow behavior.  Which makes it kind-of
useless.

Ian, the documentation of -fno-strict-overflow suggests that the compiler
will not rely on signed overflow not happening.  That's not true, so this
is definitely a documentation bug.
Comment 4 Ian Lance Taylor 2013-09-20 00:08:15 UTC
Richi, as you know, -fno-strict-overflow was added to prevent people from configuring GCC to make -fwrapv the default.  I know you hate the option but I continue to think that adding was the right tactical move at the time.  We can't just ignore the world outside the compiler development community.

It's true that -fno-strict-overflow does not apply to loop optimizations, which are covered by -fno-aggressive-loop-optimizations.  I agree that that is a doc omission.
Comment 5 Manuel López-Ibáñez 2013-09-20 07:45:27 UTC
(In reply to Ian Lance Taylor from comment #4)
> It's true that -fno-strict-overflow does not apply to loop optimizations,
> which are covered by -fno-aggressive-loop-optimizations.  I agree that that
> is a doc omission.

The examples above suggest that this is not true either. Only -fno-tree-vrp or -fwrapv do the trick.
Comment 6 Manuel López-Ibáñez 2017-09-04 15:51:13 UTC
Richard, is this bug fixed for GCC 8?

I'd like to update the FAQ: https://gcc.gnu.org/wiki/FAQ#signed_overflow
Comment 7 rguenther@suse.de 2017-09-04 16:45:36 UTC
On September 4, 2017 5:51:13 PM GMT+02:00, "manu at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> wrote:
>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58454
>
>Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:
>
>           What    |Removed                     |Added
>----------------------------------------------------------------------------
>             CC|                            |rguenth at gcc dot gnu.org
>
>--- Comment #6 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
>Richard, is this bug fixed for GCC 8?
>
>I'd like to update the FAQ:
>https://gcc.gnu.org/wiki/FAQ#signed_overflow

Given -fno-strict-overflow is now mapped to -fwrapv you could say so.
Comment 8 Manuel López-Ibáñez 2017-09-04 18:51:29 UTC
Fixed in GCC 8, it won't be backported.
Comment 9 Jakub Jelinek 2018-05-02 10:06:23 UTC
GCC 8.1 has been released.
Comment 10 Jakub Jelinek 2018-07-26 11:20:15 UTC
GCC 8.2 has been released.
Comment 11 Jakub Jelinek 2019-02-22 15:21:57 UTC
GCC 8.3 has been released.
Comment 12 Jakub Jelinek 2020-03-04 09:37:47 UTC
GCC 8.4.0 has been released, adjusting target milestone.
Comment 13 Jakub Jelinek 2021-05-14 10:00:35 UTC
Fixed then.