Message ID | CAAgBjMmBGLsviEya3V0xfzNfyPfeE8B8XGLdCz_HpR1PUMedJQ@mail.gmail.com |
---|---|
State | New |
Headers | show |
Series | PR83501 | expand |
On 21 December 2017 at 12:53, Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org> wrote: > Hi Jakub, > Based on your suggestions in PR83501, I have updated the patch to > check for integer_zerop for 2nd operand of mem_ref. > > With the patch, Warray-bounds started warning for the following test > in Warray-bounds-3.c in line 362 and thus I removed xfail on it: > TM (a5, "0123", ma.a5 + i, ma.a5); > > Does it look OK ? > Validation in progress. ping https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01415.html Thanks, Prathamesh > > Thanks, > Prathamesh
On Thu, Dec 28, 2017 at 8:42 AM, Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org> wrote: > On 21 December 2017 at 12:53, Prathamesh Kulkarni > <prathamesh.kulkarni@linaro.org> wrote: >> Hi Jakub, >> Based on your suggestions in PR83501, I have updated the patch to >> check for integer_zerop for 2nd operand of mem_ref. >> >> With the patch, Warray-bounds started warning for the following test >> in Warray-bounds-3.c in line 362 and thus I removed xfail on it: >> TM (a5, "0123", ma.a5 + i, ma.a5); >> >> Does it look OK ? >> Validation in progress. > ping https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01415.html Ok. RIchard. > Thanks, > Prathamesh >> >> Thanks, >> Prathamesh
On 2 January 2018 at 19:29, Richard Biener <richard.guenther@gmail.com> wrote: > On Thu, Dec 28, 2017 at 8:42 AM, Prathamesh Kulkarni > <prathamesh.kulkarni@linaro.org> wrote: >> On 21 December 2017 at 12:53, Prathamesh Kulkarni >> <prathamesh.kulkarni@linaro.org> wrote: >>> Hi Jakub, >>> Based on your suggestions in PR83501, I have updated the patch to >>> check for integer_zerop for 2nd operand of mem_ref. >>> >>> With the patch, Warray-bounds started warning for the following test >>> in Warray-bounds-3.c in line 362 and thus I removed xfail on it: >>> TM (a5, "0123", ma.a5 + i, ma.a5); >>> >>> Does it look OK ? >>> Validation in progress. >> ping https://gcc.gnu.org/ml/gcc-patches/2017-12/msg01415.html > > Ok. Thanks, I removed the Warray-bounds-3.c hunk since that was added in interim by Martin Sebor. Committed the attached version in r256180 after verifying bootstrap+test passes on x86_64-unknown-linux-gnu. Thanks, Prathamesh > > RIchard. > >> Thanks, >> Prathamesh >>> >>> Thanks, >>> Prathamesh diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c new file mode 100644 index 00000000000..d8d3bf6039a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-strlen" } */ + +char a[4]; + +void f (void) +{ + __builtin_strcpy (a, "abc"); + + if (__builtin_strlen (a) != 3) + __builtin_abort (); +} + +/* { dg-final { scan-tree-dump-not "__builtin_strlen" "strlen" } } */ diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 8971c7df4f3..f1d4f6ef059 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -2769,6 +2769,21 @@ handle_pointer_plus (gimple_stmt_iterator *gsi) } } +/* Check if RHS is string_cst possibly wrapped by mem_ref. */ +static tree +get_string_cst (tree rhs) +{ + if (TREE_CODE (rhs) == MEM_REF + && integer_zerop (TREE_OPERAND (rhs, 1))) + { + rhs = TREE_OPERAND (rhs, 0); + if (TREE_CODE (rhs) == ADDR_EXPR) + rhs = TREE_OPERAND (rhs, 0); + } + + return (TREE_CODE (rhs) == STRING_CST) ? rhs : NULL_TREE; +} + /* Handle a single character store. */ static bool @@ -2924,11 +2939,11 @@ handle_char_store (gimple_stmt_iterator *gsi) } } else if (idx == 0 - && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST + && (rhs = get_string_cst (gimple_assign_rhs1 (stmt))) && ssaname == NULL_TREE && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE) { - size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt))); + size_t l = strlen (TREE_STRING_POINTER (rhs)); HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs)); if (a > 0 && (unsigned HOST_WIDE_INT) a > l) {
diff --git a/gcc/testsuite/c-c++-common/Warray-bounds-3.c b/gcc/testsuite/c-c++-common/Warray-bounds-3.c index 73103f43d01..62667344c0a 100644 --- a/gcc/testsuite/c-c++-common/Warray-bounds-3.c +++ b/gcc/testsuite/c-c++-common/Warray-bounds-3.c @@ -359,7 +359,7 @@ void test_strcpy_bounds_memarray_range (void) TM (a5, "0", ma.a5 + i, ma.a5); TM (a5, "01", ma.a5 + i, ma.a5); TM (a5, "012", ma.a5 + i, ma.a5); - TM (a5, "0123", ma.a5 + i, ma.a5); /* { dg-warning "offset 10 from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char\\\[5]. at offset 4" "strcpy" { xfail *-*-* } } */ + TM (a5, "0123", ma.a5 + i, ma.a5); /* { dg-warning "offset 10 from the object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with type .char ?\\\[5]' at offset 4" } */ TM (a11, "0", ma.a5, ma.a11); TM (a11, "01", ma.a5, ma.a11); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c new file mode 100644 index 00000000000..d8d3bf6039a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-strlen" } */ + +char a[4]; + +void f (void) +{ + __builtin_strcpy (a, "abc"); + + if (__builtin_strlen (a) != 3) + __builtin_abort (); +} + +/* { dg-final { scan-tree-dump-not "__builtin_strlen" "strlen" } } */ diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 8971c7df4f3..f1d4f6ef059 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -2769,6 +2769,21 @@ handle_pointer_plus (gimple_stmt_iterator *gsi) } } +/* Check if RHS is string_cst possibly wrapped by mem_ref. */ +static tree +get_string_cst (tree rhs) +{ + if (TREE_CODE (rhs) == MEM_REF + && integer_zerop (TREE_OPERAND (rhs, 1))) + { + rhs = TREE_OPERAND (rhs, 0); + if (TREE_CODE (rhs) == ADDR_EXPR) + rhs = TREE_OPERAND (rhs, 0); + } + + return (TREE_CODE (rhs) == STRING_CST) ? rhs : NULL_TREE; +} + /* Handle a single character store. */ static bool @@ -2924,11 +2939,11 @@ handle_char_store (gimple_stmt_iterator *gsi) } } else if (idx == 0 - && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST + && (rhs = get_string_cst (gimple_assign_rhs1 (stmt))) && ssaname == NULL_TREE && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE) { - size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt))); + size_t l = strlen (TREE_STRING_POINTER (rhs)); HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs)); if (a > 0 && (unsigned HOST_WIDE_INT) a > l) {