Bug 43949 - [4.4 Regression] bogus warning: array subscript is above array bounds
Summary: [4.4 Regression] bogus warning: array subscript is above array bounds
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.4.3
: P2 normal
Target Milestone: 4.5.4
Assignee: Richard Biener
URL:
Keywords: diagnostic, missed-optimization
Depends on:
Blocks:
 
Reported: 2010-04-30 16:25 UTC by Joachim Reichel
Modified: 2012-03-13 13:19 UTC (History)
2 users (show)

See Also:
Host: x86_64-linux-gnu
Target: x86_64-linux-gnu
Build: x86_64-linux-gnu
Known to work: 4.2.4, 4.5.1, 4.6.0
Known to fail: 4.5.0
Last reconfirmed: 2010-04-30 17:25:26


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joachim Reichel 2010-04-30 16:25:49 UTC
$ 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).
Comment 1 Richard Biener 2010-04-30 17:25:26 UTC
Confirmed.  VRP is stupid and does

Visiting statement:
index_4 = i_21 % 3;

Found new range for index_4: VARYING

Yet another missed optimization.
Comment 2 Richard Biener 2010-05-01 21:30:42 UTC
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

Comment 3 Richard Biener 2010-05-01 21:31:19 UTC
Fixed for trunk sofar.
Comment 4 Richard Biener 2010-05-04 09:53:52 UTC
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

Comment 5 Joachim Reichel 2010-05-21 09:12:39 UTC
Will this problem get fixed on the 4.5 branch too?
Comment 6 rguenther@suse.de 2010-05-21 09:15:23 UTC
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.
Comment 7 Richard Biener 2010-05-21 11:17:05 UTC
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

Comment 8 Richard Biener 2010-05-22 18:14:00 UTC
GCC 4.3.5 is being released, adjusting target milestone.
Comment 9 Richard Biener 2011-06-27 12:13:49 UTC
4.3 branch is being closed, moving to 4.4.7 target.
Comment 10 Jakub Jelinek 2012-03-13 13:19:38 UTC
Fixed in 4.5+, 4.4 is no longer supported.