This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH, middle-end] Fix PR 37861 - Bogus array bounds warning
Hi,
On Wed, Nov 05, 2008 at 10:56:27AM +0100, Richard Guenther wrote:
> On Wed, 5 Nov 2008, Martin Jambor wrote:
> > the following patch gets rid of the bogus warning.
> >
> > The problem is, that fwprop currently generates array_refs of the form
> > &(*formatstr_3(D))[cumulative_offset] for multidimensional arrays and
> > the cumulative offset can then naturally be bigger than the respective
> > dimension of the array.
> >
> > This happens because, when multidimensional arrays are passed as a
> > formal parameter, they are gimplified into a pointer, thus
> > formatstr[10][100] becomes char[100] * formatstr. We do not want to
> > be doing the transformation on such variables and the patch inhibits
> > them.
> >
> > I have bootstrapped and regression tested it on x86_64-suse-linux
> > (revision 141561). I hope this won't cause any performance problems.
> >
> > OK for trunk?
>
> This is ok if you add both testcases from the PR and add a brief
> comment before the INDIRECT_REF check in the patch.
>
I originally intended to include the testcases but somehow forgot in
the end. This is the updated patch, I will commit it in a few hours,
unless someone objects.
Thanks,
Martin
2008-11-05 Martin Jambor <mjambor@suse.cz>
PR middle-end/37861
* tree-ssa-forwprop.c (forward_propagate_addr_expr_1): Don't turn
pointer arithmetics into array_ref if the array is accessed
through an indirect_ref.
* testsuite/gcc.dg/Warray-bounds-5.c: New file.
* testsuite/gcc.dg/Warray-bounds-6.c: New file.
Index: gcc/testsuite/gcc.dg/Warray-bounds-6.c
===================================================================
--- gcc/testsuite/gcc.dg/Warray-bounds-6.c (revision 0)
+++ gcc/testsuite/gcc.dg/Warray-bounds-6.c (revision 0)
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -Wall" } */
+/* based on PR 37861 */
+
+extern int printf (__const char *__restrict __format, ...);
+
+static int f3(int v)
+{
+ int i,j = 0;
+ for (i = 0; i <= v; i++)
+ j++;
+ return j;
+}
+
+static int f2(char formatstr[10][100]) {
+ printf( "%d %s\n", 0, formatstr[f3(0)] );
+ printf( "%d %s\n", 1, formatstr[f3(1)] );
+ printf( "%d %s\n", 2, formatstr[f3(2)] );
+ printf( "%d %s\n", 3, formatstr[f3(3)] );
+ return 3;
+}
+
+static char formatstr[10][100];
+int main( void ) {
+ int anz;
+ anz = f2(formatstr);
+ printf( " %d\n",anz);
+ return 0;
+}
Index: gcc/testsuite/gcc.dg/Warray-bounds-5.c
===================================================================
--- gcc/testsuite/gcc.dg/Warray-bounds-5.c (revision 0)
+++ gcc/testsuite/gcc.dg/Warray-bounds-5.c (revision 0)
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -Wall" } */
+/* based on PR 37861 */
+
+extern int printf (__const char *__restrict __format, ...);
+
+static int f2(char formatstr[10][100])
+{
+ int anz;
+ for( anz = 0; anz < 10; ++anz ) {
+ printf( "%d %s\n", anz, formatstr[anz] );
+ }
+ return anz;
+}
+
+
+static char formatstr[10][100];
+int main( void )
+{
+ int anz;
+ anz = f2(formatstr);
+ printf( " %d\n",anz);
+ return 0;
+}
Index: gcc/tree-ssa-forwprop.c
===================================================================
--- gcc/tree-ssa-forwprop.c (revision 141546)
+++ gcc/tree-ssa-forwprop.c (working copy)
@@ -812,6 +812,9 @@ forward_propagate_addr_expr_1 (tree name
array_ref = TREE_OPERAND (def_rhs, 0);
if (TREE_CODE (array_ref) != ARRAY_REF
|| TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
+ /* Avoid accessing hidden multidimensional arrays in this way or VRP
+ might give out bogus warnings (see PR 37861) */
+ || TREE_CODE (TREE_OPERAND (array_ref, 0)) == INDIRECT_REF
|| !integer_zerop (TREE_OPERAND (array_ref, 1)))
return false;