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: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.8.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: documentation
Depends on:
Blocks:
 
Reported: 2013-09-18 01:28 UTC by mednafen
Modified: 2013-09-20 07:45 UTC (History)
3 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.