From d1af2f668fb0fca982183265e1a56e23d4f25fa8 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Tue, 28 Aug 2018 00:25:50 +0000 Subject: [PATCH] PR tree-optimization/86914 - wrong code with strlen() of poor-man's flexible array member plus offset gcc/ChangeLog: PR tree-optimization/86914 * tree-ssa-strlen.c (maybe_set_strlen_range): Avoid MEM_REF. gcc/testsuite/ChangeLog: PR tree-optimization/86914 * gcc.dg/strlenopt-57.c: New test. From-SVN: r263905 --- gcc/ChangeLog | 5 +++ gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/gcc.dg/strlenopt-57.c | 49 +++++++++++++++++++++++++++++ gcc/tree-ssa-strlen.c | 4 ++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/strlenopt-57.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 2449672c62f..4cbea7a195d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2018-08-27 Martin Sebor + + PR tree-optimization/86914 + * tree-ssa-strlen.c (maybe_set_strlen_range): Avoid MEM_REF. + 2018-08-27 Martin Sebor PR tree-optimization/87112 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 92f15401a22..4b5c1533c33 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-08-27 Martin Sebor + + PR tree-optimization/86914 + * gcc.dg/strlenopt-57.c: New test. + 2018-08-27 Martin Sebor PR tree-optimization/87112 diff --git a/gcc/testsuite/gcc.dg/strlenopt-57.c b/gcc/testsuite/gcc.dg/strlenopt-57.c new file mode 100644 index 00000000000..49dc8cd6fbb --- /dev/null +++ b/gcc/testsuite/gcc.dg/strlenopt-57.c @@ -0,0 +1,49 @@ +/* PR tree-optimization/86914 - wrong code with strlen() of poor-man's + flexible array member plus offset + { dg-do compile } + { dg-options "-O2 -Wall -fdump-tree-optimized" } */ + +#include "strlenopt.h" + +struct A0 { char i, a[0]; }; +struct A1 { char i, a[1]; }; +struct A9 { char i, a[9]; }; +struct Ax { char i, a[]; }; + +extern int a[]; + +extern struct A0 a0; +extern struct A1 a1; +extern struct A9 a9; +extern struct Ax ax; + +void test_var_flexarray_cst_off (void) +{ + /* Use arbitrary constants greater than 16 in case GCC ever starts + unrolling strlen() calls with small array arguments. */ + a[0] = 17 < strlen (a0.a + 1); + a[1] = 19 < strlen (a1.a + 1); + a[2] = 23 < strlen (a9.a + 9); + a[3] = 29 < strlen (ax.a + 3); +} + +void test_ptr_flexarray_cst_off (struct A0 *p0, struct A1 *p1, + struct A9 *p9, struct Ax *px) +{ + a[0] = 17 < strlen (p0->a + 1); + a[1] = 19 < strlen (p1->a + 1); + a[2] = 23 < strlen (p9->a + 9); + a[3] = 29 < strlen (px->a + 3); +} + +void test_ptr_flexarray_var_off (struct A0 *p0, struct A1 *p1, + struct A9 *p9, struct Ax *px, + int i) +{ + a[0] = 17 < strlen (p0->a + i); + a[1] = 19 < strlen (p1->a + i); + a[2] = 23 < strlen (p9->a + i); + a[3] = 29 < strlen (px->a + i); +} + +/* { dg-final { scan-tree-dump-times "strlen" 12 "optimized" } } */ diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index b90d4a17985..84e61526f94 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -1160,7 +1160,9 @@ maybe_set_strlen_range (tree lhs, tree src, tree bound) suggests if it's treated as a poor-man's flexible array member. */ src = TREE_OPERAND (src, 0); bool src_is_array = TREE_CODE (TREE_TYPE (src)) == ARRAY_TYPE; - if (src_is_array && !array_at_struct_end_p (src)) + if (src_is_array + && TREE_CODE (src) != MEM_REF + && !array_at_struct_end_p (src)) { tree type = TREE_TYPE (src); if (tree size = TYPE_SIZE_UNIT (type)) -- 2.30.2