From 65f2d1ee1c20860e238750099b88e394ee30a770 Mon Sep 17 00:00:00 2001 From: Prathamesh Kulkarni Date: Wed, 3 Jan 2018 16:07:32 +0000 Subject: [PATCH] re PR tree-optimization/83501 (strlen(a) not folded after strcpy(a, "...")) 2018-01-03 Prathamesh Kulkarni PR tree-optimization/83501 * tree-ssa-strlen.c (get_string_cst): New. (handle_char_store): Call get_string_cst. testsuite/ * gcc.dg/tree-ssa/pr83501.c: New test. From-SVN: r256180 --- gcc/ChangeLog | 6 ++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/tree-ssa/pr83501.c | 14 ++++++++++++++ gcc/tree-ssa-strlen.c | 19 +++++++++++++++++-- 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr83501.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 95b5ed2468d..d95c297e96a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-01-03 Prathamesh Kulkarni + + PR tree-optimization/83501 + * tree-ssa-strlen.c (get_string_cst): New. + (handle_char_store): Call get_string_cst. + 2018-01-03 Martin Liska PR tree-optimization/83593 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e252ae0f080..c18e6c7fd69 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-01-03 Prathamesh Kulkarni + + PR tree-optimization/83501 + * gcc.dg/tree-ssa/pr83501-1.c: New test. + 2018-01-03 Nathan Sidwell PR c++/83667 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 8f7020c14b3..f4fd69f58de 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -2772,6 +2772,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 @@ -2927,11 +2942,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) { -- 2.30.2