$ cat test.cpp void f(); int c[3]; int result; struct Vector { static int get(int i) { if (i >= 3) f(); return c[i]; } }; void g() { for (int i = 0; i < 3; ++i) { const int index = i % 3; result = Vector::get(index) + Vector::get(index); } } $ g++ -Wall -c test.cpp [no warnings] $ g++ -Wall -c -O2 test.cpp test.cpp: In function ‘void g()’: test.cpp:10: error: array subscript is above array bounds $ g++ -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.2-9' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.4.3 20100108 (prerelease) (Debian 4.4.2-9) The bogus warning disappears if - (index) is replaced by (i) - Vector::get(index) is assigned to result (no addition) - the if statement is removed (call of f() in all cases) - get() is a global function The bogus warning does not appear in gcc 4.2.4 (Debian 4.2.4-6). It appears also in 4.3.2 (Debian 4.3.2-1.1) and gcc 4.5.0 (GCC).
Confirmed. VRP is stupid and does Visiting statement: index_4 = i_21 % 3; Found new range for index_4: VARYING Yet another missed optimization.
Subject: Bug 43949 Author: rguenth Date: Sat May 1 21:30:26 2010 New Revision: 158965 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158965 Log: 2010-05-01 Richard Guenther <rguenther@suse.de> PR tree-optimization/43949 * tree-vrp.c (ssa_name_nonnegative_p): Return true for unsigned types. (extract_range_from_binary_expr): Handle *_MOD_EXPR. * g++.dg/warn/Warray-bounds-5.C: New testcase. Added: trunk/gcc/testsuite/g++.dg/warn/Warray-bounds-5.C Modified: trunk/gcc/ChangeLog trunk/gcc/testsuite/ChangeLog trunk/gcc/tree-vrp.c
Fixed for trunk sofar.
Subject: Bug 43949 Author: rguenth Date: Tue May 4 09:53:28 2010 New Revision: 159020 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159020 Log: 2010-05-04 Richard Guenther <rguenther@suse.de> PR tree-optimization/43949 * tree-vrp.c (extract_range_from_binary_expr): Only handle TRUNC_MOD_EXPR. Modified: trunk/gcc/ChangeLog trunk/gcc/tree-vrp.c
Will this problem get fixed on the 4.5 branch too?
Subject: Re: [4.3/4.4/4.5 Regression] bogus warning: array subscript is above array bounds On Fri, 21 May 2010, joachim dot reichel at gmx dot de wrote: > ------- Comment #5 from joachim dot reichel at gmx dot de 2010-05-21 09:12 ------- > Will this problem get fixed on the 4.5 branch too? Yes.
Subject: Bug 43949 Author: rguenth Date: Fri May 21 11:16:46 2010 New Revision: 159656 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159656 Log: 2010-05-21 Richard Guenther <rguenther@suse.de> PR tree-optimization/43949 * tree-vrp.c (ssa_name_nonnegative_p): Return true for unsigend types. (extract_range_from_binary_expr): Handle TRUNC_MOD_EXPR. * g++.dg/warn/Warray-bounds-5.C: New testcase. Added: branches/gcc-4_5-branch/gcc/testsuite/g++.dg/warn/Warray-bounds-5.C Modified: branches/gcc-4_5-branch/gcc/ChangeLog branches/gcc-4_5-branch/gcc/testsuite/ChangeLog branches/gcc-4_5-branch/gcc/tree-vrp.c
GCC 4.3.5 is being released, adjusting target milestone.
4.3 branch is being closed, moving to 4.4.7 target.
Fixed in 4.5+, 4.4 is no longer supported.