From ad2a970f7973708ea3359c0b8cc2aec7c9ead1bb Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 19 Dec 2017 19:10:04 +0100 Subject: [PATCH] re PR tree-optimization/83444 (missing strlen optimization on a member array of a local struct) PR tree-optimization/83444 * tree-ssa-strlen.c (strlen_check_and_optimize_stmt): For the character load case, if get_stridx on MEM_REF's operand doesn't look usable, retry with get_addr_stridx. * gcc.dg/strlenopt-38.c: New test. From-SVN: r255835 --- gcc/ChangeLog | 7 +++++++ gcc/testsuite/ChangeLog | 3 +++ gcc/testsuite/gcc.dg/strlenopt-38.c | 11 +++++++++++ gcc/tree-ssa-strlen.c | 19 ++++++++++++++++--- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 328ce281e81..5e8e9bf6acc 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2017-12-19 Jakub Jelinek + + PR tree-optimization/83444 + * tree-ssa-strlen.c (strlen_check_and_optimize_stmt): For the + character load case, if get_stridx on MEM_REF's operand doesn't + look usable, retry with get_addr_stridx. + 2017-12-19 Alexandre Oliva PR debug/83422 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 81f96c6ce07..1146e94c0e9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -5,6 +5,9 @@ 2017-12-19 Jakub Jelinek + PR tree-optimization/83444 + * gcc.dg/strlenopt-38.c: New test. + PR testsuite/83454 * gcc.dg/tree-ssa/cswtch-4.c: Require nonpic effective target. * gcc.dg/tree-ssa/cswtch-5.c: Likewise. diff --git a/gcc/testsuite/gcc.dg/strlenopt-38.c b/gcc/testsuite/gcc.dg/strlenopt-38.c index 3b698f9c1e8..7c39f3a175b 100644 --- a/gcc/testsuite/gcc.dg/strlenopt-38.c +++ b/gcc/testsuite/gcc.dg/strlenopt-38.c @@ -36,3 +36,14 @@ baz (void) if (s.b[0] != 0) abort (); } + +void +boo (void) +{ + struct S s; + strcpy (s.b, "012"); + strcpy (s.c, ""); + strcpy (s.b, s.c); + if (strlen (s.b) != 0) + abort (); +} diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index a2d514cb35a..8971c7df4f3 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -3155,14 +3155,27 @@ strlen_check_and_optimize_stmt (gimple_stmt_iterator *gsi) { tree off = integer_zero_node; unsigned HOST_WIDE_INT coff = 0; - int idx = -1; + int idx = 0; tree rhs1 = gimple_assign_rhs1 (stmt); if (code == MEM_REF) { idx = get_stridx (TREE_OPERAND (rhs1, 0)); - off = TREE_OPERAND (rhs1, 1); + if (idx > 0) + { + strinfo *si = get_strinfo (idx); + if (si + && si->nonzero_chars + && TREE_CODE (si->nonzero_chars) == INTEGER_CST + && (wi::to_widest (si->nonzero_chars) + >= wi::to_widest (off))) + off = TREE_OPERAND (rhs1, 1); + else + /* This case is not useful. See if get_addr_stridx + returns something usable. */ + idx = 0; + } } - else + if (idx <= 0) idx = get_addr_stridx (rhs1, NULL_TREE, &coff); if (idx > 0) { -- 2.30.2