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: unassig...@gcc.gnu.org
        ReportedBy: gwpub...@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)

Reply via email to