From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22507 invoked by alias); 26 Jan 2015 15:08:08 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 22428 invoked by uid 48); 26 Jan 2015 15:07:56 -0000 From: "enkovich.gnu at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/64277] [4.9/5 Regression] Incorrect warning "array subscript is above array bounds" Date: Mon, 26 Jan 2015 15:08:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 4.9.3 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: enkovich.gnu at gmail dot com X-Bugzilla-Status: NEW X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 4.9.3 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-01/txt/msg02898.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64277 --- Comment #13 from Ilya Enkovich --- Ranges have to be used for maxiter computations to have consistent analysis in complete unroll and vrp. Following patch allows to refine maxiter estimation using ranges and avoid warnings. diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c index 919f5c0..14cce2a 100644 --- a/gcc/tree-ssa-loop-niter.c +++ b/gcc/tree-ssa-loop-niter.c @@ -2754,6 +2754,7 @@ record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt, { tree niter_bound, extreme, delta; tree type = TREE_TYPE (base), unsigned_type; + tree orig_base = base; if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step)) return; @@ -2777,7 +2778,14 @@ record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt, if (tree_int_cst_sign_bit (step)) { + wide_int min, max, highwi = high; extreme = fold_convert (unsigned_type, low); + if (TREE_CODE (orig_base) == SSA_NAME + && !POINTER_TYPE_P (TREE_TYPE (orig_base)) + && SSA_NAME_RANGE_INFO (orig_base) + && get_range_info (orig_base, &min, &max) == VR_RANGE + && wi::gts_p (highwi, max)) + base = wide_int_to_tree (unsigned_type, max); if (TREE_CODE (base) != INTEGER_CST) base = fold_convert (unsigned_type, high); delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme); @@ -2785,8 +2793,15 @@ record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt, } else { + wide_int min, max, lowwi = low; extreme = fold_convert (unsigned_type, high); - if (TREE_CODE (base) != INTEGER_CST) + if (TREE_CODE (orig_base) == SSA_NAME + && !POINTER_TYPE_P (TREE_TYPE (orig_base)) + && SSA_NAME_RANGE_INFO (orig_base) + && get_range_info (orig_base, &min, &max) == VR_RANGE + && wi::gts_p (min, lowwi)) + base = wide_int_to_tree (unsigned_type, min); + else if (TREE_CODE (base) != INTEGER_CST) base = fold_convert (unsigned_type, low); delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base); } diff --git a/gcc/testsuite/gcc.dg/pr64277.c b/gcc/testsuite/gcc.dg/pr64277.c new file mode 100644 index 0000000..0d5ef11 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr64277.c @@ -0,0 +1,21 @@ +/* PR tree-optimization/64277 */ +/* { dg-do compile } */ +/* { dg-options "-O3 -Wall -Werror" } */ + + +int f1[10]; +void test1 (short a[], short m, unsigned short l) +{ + int i = l; + for (i = i + 5; i < m; i++) + f1[i] = a[i]++; +} + +void test2 (short a[], short m, short l) +{ + int i; + if (m > 5) + m = 5; + for (i = m; i > l; i--) + f1[i] = a[i]++; +}