public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
@ 2012-04-02 10:08 gwpublic at wp dot pl
2012-04-02 10:09 ` [Bug c++/52833] " gwpublic at wp dot pl
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: gwpublic at wp dot pl @ 2012-04-02 10:08 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
Bug #: 52833
Summary: -O2 optimizes loop to infinite when loop invariant
based on arithmetic overflow
Classification: Unclassified
Product: gcc
Version: 4.6.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: gwpublic@wp.pl
Created attachment 27063
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27063
This loop compiles to endless when O2 in gcc 4.6.1
Hello,
following code is based on artihmetic overflow assumption (after overflow we
gen <0 number on x86 and x86_64):
$cat overflow_loop.cpp
int main(){
int s=1, i=0;
while (s>0) {
++i;
s=2*s;
}
return i;
}
While it compiles fine when -O1 and -O2 , on -O3 compiles to infinite loop.
Let's generate assembly codes:
$ g++ -O1 -S -o overflow_loop-O1.s overflow_loop.cpp
$ g++ -O2 -S -o overflow_loop-O2.s overflow_loop.cpp
There is following difference between loops:
overflow_loop-O1.s
(...)
.L2:
addl $1, %eax
cmpl $31, %eax
jne .L2
(...)
overflow_loop-O2.s
(...)
.L2:
jmp .L2
(...)
Let's check what flags are enabled for O1 and O2
$ echo '$ gcc -c -Q -O2 --help=optimizers | grep enabled | sort ' >
optim_O2.txt
$ gcc -c -Q -O2 --help=optimizers | grep enabled | sort >> optim_O2.txt
$ echo '$ gcc -c -Q -O1 --help=optimizers | grep enabled | sort ' >
optim_O1.txt
$ gcc -c -Q -O1 --help=optimizers | grep enabled | sort >> optim_O1.txt
Here is the difference:
$ diff optim_O{1,2}.txt
1c1,5
< $ gcc -c -Q -O1 --help=optimizers | grep enabled | sort
---
> $ gcc -c -Q -O2 --help=optimizers | grep enabled | sort
> -falign-functions [enabled]
> -falign-jumps [enabled]
> -falign-labels [enabled]
> -falign-loops [enabled]
3a8
> -fcaller-saves [enabled]
7a13,14
> -fcrossjumping [enabled]
> -fcse-follow-jumps [enabled]
10a18
> -fdevirtualize [enabled]
12a21
> -fexpensive-optimizations [enabled]
13a23
> -fgcse [enabled]
18a29,30
> -finline-small-functions [enabled]
> -fipa-cp [enabled]
21a34
> -fipa-sra [enabled]
27a41,43
> -foptimize-register-move [enabled]
> -foptimize-sibling-calls [enabled]
> -fpeephole2 [enabled]
29a46
> -fregmove [enabled]
30a48,50
> -freorder-blocks [enabled]
> -freorder-functions [enabled]
> -frerun-cse-after-loop [enabled]
40a61
> -fschedule-insns2 [enabled]
44a66,67
> -fstrict-aliasing [enabled]
> -fthread-jumps [enabled]
47a71
> -ftree-builtin-call-dce [enabled]
62a87
> -ftree-pre [enabled]
68a94
> -ftree-switch-conversion [enabled]
70a97
> -ftree-vrp [enabled]
Finally, detailed compiler version:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/src/gcc-4.6-20110819/configure --prefix=/usr
--libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared
--enable-threads=posix --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-clocale=gnu --enable-gnu-unique-object
--enable-linker-build-id --with-ppl --enable-cloog-backend=isl --enable-lto
--enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold
--disable-multilib --disable-libssp --disable-libstdcxx-pch
--enable-checking=release
Thread model: posix
gcc version 4.6.1 20110819 (prerelease) (GCC)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
@ 2012-04-02 10:09 ` gwpublic at wp dot pl
2012-04-02 10:10 ` gwpublic at wp dot pl
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: gwpublic at wp dot pl @ 2012-04-02 10:09 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
--- Comment #1 from Grzegorz Wierzowiecki <gwpublic at wp dot pl> 2012-04-02 10:09:15 UTC ---
Created attachment 27064
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27064
overflow_loop-O1.s - Assembly after -O1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
2012-04-02 10:09 ` [Bug c++/52833] " gwpublic at wp dot pl
@ 2012-04-02 10:10 ` gwpublic at wp dot pl
2012-04-02 10:11 ` gwpublic at wp dot pl
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: gwpublic at wp dot pl @ 2012-04-02 10:10 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
--- Comment #2 from Grzegorz Wierzowiecki <gwpublic at wp dot pl> 2012-04-02 10:09:48 UTC ---
Created attachment 27065
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27065
overflow_loop-O2.s - assembly after -O2
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
2012-04-02 10:09 ` [Bug c++/52833] " gwpublic at wp dot pl
2012-04-02 10:10 ` gwpublic at wp dot pl
@ 2012-04-02 10:11 ` gwpublic at wp dot pl
2012-04-02 10:11 ` gwpublic at wp dot pl
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: gwpublic at wp dot pl @ 2012-04-02 10:11 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
--- Comment #3 from Grzegorz Wierzowiecki <gwpublic at wp dot pl> 2012-04-02 10:11:02 UTC ---
Created attachment 27066
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27066
compiler flags enabled by -O1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
` (2 preceding siblings ...)
2012-04-02 10:11 ` gwpublic at wp dot pl
@ 2012-04-02 10:11 ` gwpublic at wp dot pl
2012-04-02 10:12 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: gwpublic at wp dot pl @ 2012-04-02 10:11 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
--- Comment #4 from Grzegorz Wierzowiecki <gwpublic at wp dot pl> 2012-04-02 10:11:32 UTC ---
Created attachment 27067
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27067
compiler flags enabled by -O2
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
` (3 preceding siblings ...)
2012-04-02 10:11 ` gwpublic at wp dot pl
@ 2012-04-02 10:12 ` redi at gcc dot gnu.org
2012-04-02 10:16 ` gwpublic at wp dot pl
2012-04-02 11:38 ` redi at gcc dot gnu.org
6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-04-02 10:12 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-02 10:12:34 UTC ---
(In reply to comment #0)
> following code is based on artihmetic overflow assumption (after overflow we
> gen <0 number on x86 and x86_64):
Bad assumption, overflow is undefined behaviour.
The bug submission form asys to read http://gcc.gnu.org/bugs/ before reporting
bugs, which says if your code works as you expect with -fwrapv then it's not a
bug in the compiler, it's a bug in your code.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
` (4 preceding siblings ...)
2012-04-02 10:12 ` redi at gcc dot gnu.org
@ 2012-04-02 10:16 ` gwpublic at wp dot pl
2012-04-02 11:38 ` redi at gcc dot gnu.org
6 siblings, 0 replies; 8+ messages in thread
From: gwpublic at wp dot pl @ 2012-04-02 10:16 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
Grzegorz Wierzowiecki <gwpublic at wp dot pl> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |FIXED
--- Comment #6 from Grzegorz Wierzowiecki <gwpublic at wp dot pl> 2012-04-02 10:16:31 UTC ---
Thanks for confirmation, I wanted to ensure it works as expected.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/52833] -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
` (5 preceding siblings ...)
2012-04-02 10:16 ` gwpublic at wp dot pl
@ 2012-04-02 11:38 ` redi at gcc dot gnu.org
6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-04-02 11:38 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52833
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|FIXED |INVALID
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2012-04-02 11:38 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-02 10:08 [Bug c++/52833] New: -O2 optimizes loop to infinite when loop invariant based on arithmetic overflow gwpublic at wp dot pl
2012-04-02 10:09 ` [Bug c++/52833] " gwpublic at wp dot pl
2012-04-02 10:10 ` gwpublic at wp dot pl
2012-04-02 10:11 ` gwpublic at wp dot pl
2012-04-02 10:11 ` gwpublic at wp dot pl
2012-04-02 10:12 ` redi at gcc dot gnu.org
2012-04-02 10:16 ` gwpublic at wp dot pl
2012-04-02 11:38 ` redi 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).