From: Jakub Jelinek Date: Tue, 19 Dec 2017 07:44:07 +0000 (+0100) Subject: re PR tree-optimization/83444 (missing strlen optimization on a member array of a... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f744bfec7ffb92b476da82bc62a81eaf087b2391;p=gcc.git 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): Optimize character loads. * gcc.dg/strlenopt-38.c: New test. From-SVN: r255806 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 9d7875e3918..9c0419d8333 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,9 @@ 2017-12-19 Jakub Jelinek + PR tree-optimization/83444 + * tree-ssa-strlen.c (strlen_check_and_optimize_stmt): Optimize + character loads. + PR ipa/82801 PR ipa/83346 * ipa-inline.c (flatten_remove_node_hook): New function. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5c375c1b8b6..1eb45e541e9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2017-12-19 Jakub Jelinek + PR tree-optimization/83444 + * gcc.dg/strlenopt-38.c: New test. + PR ipa/82801 PR ipa/83346 * g++.dg/ipa/pr82801.C: New test. diff --git a/gcc/testsuite/gcc.dg/strlenopt-38.c b/gcc/testsuite/gcc.dg/strlenopt-38.c new file mode 100644 index 00000000000..3b698f9c1e8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strlenopt-38.c @@ -0,0 +1,38 @@ +/* PR tree-optimization/83444 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-not "abort \\(\\)" "optimized" } } */ + +#include "strlenopt.h" + +void +foo (void) +{ + char a[5] = "012"; + strcpy (a, ""); + if (strlen (a) != 0) + abort (); +} + +void +bar (void) +{ + char a[5] = "012"; + char b[7] = ""; + strcpy (a, b); + if (strlen (a) != 0) + abort (); +} + +struct S { char a[4]; char b[5]; char c[7]; }; + +void +baz (void) +{ + struct S s; + strcpy (s.b, "012"); + strcpy (s.c, ""); + strcpy (s.b, s.c); + if (s.b[0] != 0) + abort (); +} diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 0386883a749..a2d514cb35a 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -3146,6 +3146,64 @@ strlen_check_and_optimize_stmt (gimple_stmt_iterator *gsi) else if (code == EQ_EXPR || code == NE_EXPR) fold_strstr_to_strncmp (gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt), stmt); + else if (gimple_assign_load_p (stmt) + && TREE_CODE (TREE_TYPE (lhs)) == INTEGER_TYPE + && TYPE_MODE (TREE_TYPE (lhs)) == TYPE_MODE (char_type_node) + && (TYPE_PRECISION (TREE_TYPE (lhs)) + == TYPE_PRECISION (char_type_node)) + && !gimple_has_volatile_ops (stmt)) + { + tree off = integer_zero_node; + unsigned HOST_WIDE_INT coff = 0; + int idx = -1; + tree rhs1 = gimple_assign_rhs1 (stmt); + if (code == MEM_REF) + { + idx = get_stridx (TREE_OPERAND (rhs1, 0)); + off = TREE_OPERAND (rhs1, 1); + } + else + idx = get_addr_stridx (rhs1, NULL_TREE, &coff); + if (idx > 0) + { + strinfo *si = get_strinfo (idx); + if (si + && si->nonzero_chars + && TREE_CODE (si->nonzero_chars) == INTEGER_CST) + { + widest_int w1 = wi::to_widest (si->nonzero_chars); + widest_int w2 = wi::to_widest (off) + coff; + if (w1 == w2 + && si->full_string_p) + { + /* Reading the final '\0' character. */ + tree zero = build_int_cst (TREE_TYPE (lhs), 0); + gimple_set_vuse (stmt, NULL_TREE); + gimple_assign_set_rhs_from_tree (gsi, zero); + update_stmt (gsi_stmt (*gsi)); + } + else if (w1 > w2) + { + /* Reading a character before the final '\0' + character. Just set the value range to ~[0, 0] + if we don't have anything better. */ + wide_int min, max; + tree type = TREE_TYPE (lhs); + enum value_range_type vr + = get_range_info (lhs, &min, &max); + if (vr == VR_VARYING + || (vr == VR_RANGE + && min == wi::min_value (TYPE_PRECISION (type), + TYPE_SIGN (type)) + && max == wi::max_value (TYPE_PRECISION (type), + TYPE_SIGN (type)))) + set_range_info (lhs, VR_ANTI_RANGE, + wi::zero (TYPE_PRECISION (type)), + wi::zero (TYPE_PRECISION (type))); + } + } + } + } if (strlen_to_stridx) {