From 7ad491c6368516f2fa649d215f1782011465bb60 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Wed, 16 May 2018 02:30:38 +0000 Subject: [PATCH] PR tree-optimization/85753 - missing -Wrestrict on memcpy into a member array gcc/ChangeLog: PR tree-optimization/85753 * gimple-ssa-warn-restrict.c (builtin_memref::builtin_memref): Handle RECORD_TYPE in addition to ARRAY_TYPE. gcc/testsuite/ChangeLog: PR tree-optimization/85753 * gcc.dg/Wrestrict-10.c: Adjust. * gcc.dg/Wrestrict-16.c: New test. From-SVN: r260280 --- gcc/ChangeLog | 6 ++ gcc/gimple-ssa-warn-restrict.c | 40 ++++++------- gcc/testsuite/ChangeLog | 6 ++ gcc/testsuite/gcc.dg/Wrestrict-10.c | 2 +- gcc/testsuite/gcc.dg/Wrestrict-16.c | 88 +++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wrestrict-16.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 53fcbb1ff18..80097cbd96f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-05-15 Martin Sebor + + PR tree-optimization/85753 + * gimple-ssa-warn-restrict.c (builtin_memref::builtin_memref): Handle + RECORD_TYPE in addition to ARRAY_TYPE. + 2018-05-15 Martin Sebor PR middle-end/85643 diff --git a/gcc/gimple-ssa-warn-restrict.c b/gcc/gimple-ssa-warn-restrict.c index 3d0664da028..9f23f57c426 100644 --- a/gcc/gimple-ssa-warn-restrict.c +++ b/gcc/gimple-ssa-warn-restrict.c @@ -263,27 +263,29 @@ builtin_memref::builtin_memref (tree expr, tree size) else sizrange[1] = maxobjsize; + if (!DECL_P (base)) + return; + + /* If the offset could be in the range of the referenced object + constrain its bounds so neither exceeds those of the object. */ + if (offrange[0] < 0 && offrange[1] > 0) + offrange[0] = 0; + + offset_int maxoff = maxobjsize; tree basetype = TREE_TYPE (base); - if (DECL_P (base) && TREE_CODE (basetype) == ARRAY_TYPE) + if (TREE_CODE (basetype) == ARRAY_TYPE + && ref + && array_at_struct_end_p (ref)) + ; /* Use the maximum possible offset for last member arrays. */ + else if (tree basesize = TYPE_SIZE_UNIT (basetype)) + maxoff = wi::to_offset (basesize); + + if (offrange[0] >= 0) { - /* If the offset could be in range of the referenced object - constrain its bounds so neither exceeds those of the object. */ - if (offrange[0] < 0 && offrange[1] > 0) - offrange[0] = 0; - - offset_int maxoff = maxobjsize; - if (ref && array_at_struct_end_p (ref)) - ; /* Use the maximum possible offset for last member arrays. */ - else if (tree basesize = TYPE_SIZE_UNIT (basetype)) - maxoff = wi::to_offset (basesize); - - if (offrange[0] >= 0) - { - if (offrange[1] < 0) - offrange[1] = offrange[0] <= maxoff ? maxoff : maxobjsize; - else if (offrange[0] <= maxoff && offrange[1] > maxoff) - offrange[1] = maxoff; - } + if (offrange[1] < 0) + offrange[1] = offrange[0] <= maxoff ? maxoff : maxobjsize; + else if (offrange[0] <= maxoff && offrange[1] > maxoff) + offrange[1] = maxoff; } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b6e0e78c4a0..00f3c62ab73 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-05-15 Martin Sebor + + PR tree-optimization/85753 + * gcc.dg/Wrestrict-10.c: Adjust. + * gcc.dg/Wrestrict-16.c: New test. + 2018-05-15 Martin Sebor PR middle-end/85643 diff --git a/gcc/testsuite/gcc.dg/Wrestrict-10.c b/gcc/testsuite/gcc.dg/Wrestrict-10.c index a5a5ff1b5b2..c412e42bacc 100644 --- a/gcc/testsuite/gcc.dg/Wrestrict-10.c +++ b/gcc/testsuite/gcc.dg/Wrestrict-10.c @@ -58,7 +58,7 @@ test_arr_strncat_2 (void) void __attribute__ ((noclone, noinline)) test_arr_strcpy_1 (void) { - strcpy (&b.a[i], b.a); + strcpy (&b.a[i], b.a); /* { dg-warning "\\\[-Wrestrict" } */ } void __attribute__ ((noclone, noinline)) diff --git a/gcc/testsuite/gcc.dg/Wrestrict-16.c b/gcc/testsuite/gcc.dg/Wrestrict-16.c new file mode 100644 index 00000000000..196d2c5cbc7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wrestrict-16.c @@ -0,0 +1,88 @@ +/* PR tree-optimization/85753 - missing -Wrestrict on memcpy into a member + array + { dg-do compile } + { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */ + +#define memcpy __builtin_memcpy + +char a[16]; + +struct { char a[16]; } x; + +/* Exercise aggregate types. */ + +void test_aggr_idx_nowarn (int i, int j) +{ + memcpy (&a[i], &a[j], 7); + memcpy (&x.a[i], &x.a[j], 7); +} + +void test_aggr_idx_warn (int i, int j) +{ + memcpy (&a[i], &a[j], 9); /* { dg-warning "\\\[-Wrestrict" } */ + memcpy (&x.a[i], &x.a[j], 9); /* { dg-warning "\\\[-Wrestrict" } */ +} + +void test_aggr_off_nowarn (int i, int j) +{ + memcpy (a + i, a + j, 5); + memcpy (x.a + i, x.a + j, 5); +} + +void test_aggr_off_warn (int i, int j) +{ + memcpy (a + i, a + j, 9); /* { dg-warning "\\\[-Wrestrict" } */ + memcpy (x.a + i, x.a + j, 9); /* { dg-warning "\\\[-Wrestrict" } */ +} + + +void sink (void*); + +#define T(call) sink (call) + + +/* Also exercise basic types. */ + +#ifdef __UINT32_TYPE__ + +__UINT32_TYPE__ i32; + +void test_basic_32 (int i, int j) +{ + char *p = (char*)&i32; + + T (memcpy (&p[i], &p[j], 1)); + T (memcpy (&p[i], &p[j], 2)); + T (memcpy (&p[i], &p[j], 3)); /* { dg-warning "\\\[-Wrestrict" } */ + + T (memcpy (p + i, p + j, 1)); + T (memcpy (p + i, p + j, 2)); + T (memcpy (p + i, p + j, 3)); /* { dg-warning "\\\[-Wrestrict" } */ +} + +#endif + +#ifdef __UINT64_TYPE__ + +__UINT64_TYPE__ i64; + +void test_basic_64 (int i, int j) +{ + char *p = (char*)&i64; + + T (memcpy (&p[i], &p[j], 1)); + T (memcpy (&p[i], &p[j], 2)); + T (memcpy (&p[i], &p[j], 3)); + T (memcpy (&p[i], &p[j], 5)); /* { dg-warning "\\\[-Wrestrict" } */ + T (memcpy (&p[i], &p[j], 6)); /* { dg-warning "\\\[-Wrestrict" } */ + T (memcpy (&p[i], &p[j], 7)); /* { dg-warning "\\\[-Wrestrict" } */ + + T (memcpy (p + i, p + j, 1)); + T (memcpy (p + i, p + j, 2)); + T (memcpy (p + i, p + j, 3)); + T (memcpy (p + i, p + j, 5)); /* { dg-warning "\\\[-Wrestrict" } */ + T (memcpy (p + i, p + j, 6)); /* { dg-warning "\\\[-Wrestrict" } */ + T (memcpy (p + i, p + j, 7)); /* { dg-warning "\\\[-Wrestrict" } */ +} + +#endif -- 2.30.2