diff options
author | Martin Sebor <msebor@redhat.com> | 2018-06-25 20:46:45 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2018-06-25 14:46:45 -0600 |
commit | b36bc89e32a52ac41b6b68d834c7692b890741e6 (patch) | |
tree | 5b89ff8ce0de4a470d9653841c70fa1d0d32d042 /gcc/tree-ssa-strlen.c | |
parent | 03cd7d3fa2d756cb4fb7d62d4aba277893932ace (diff) | |
download | gcc-b36bc89e32a52ac41b6b68d834c7692b890741e6.zip gcc-b36bc89e32a52ac41b6b68d834c7692b890741e6.tar.gz gcc-b36bc89e32a52ac41b6b68d834c7692b890741e6.tar.bz2 |
PR tree-optimization/86204 - wrong strlen result after prior strnlen
gcc/ChangeLog:
PR tree-optimization/86204
* tree-ssa-strlen.c (handle_builtin_strlen): Avoid storing
a strnlen result if it's less than the length of the string.
gcc/testsuite/ChangeLog:
PR tree-optimization/86204
* gcc.dg/strlenopt-46.c: New test.
From-SVN: r262114
Diffstat (limited to 'gcc/tree-ssa-strlen.c')
-rw-r--r-- | gcc/tree-ssa-strlen.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 2da82a8..5807c79 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -1270,8 +1270,20 @@ handle_builtin_strlen (gimple_stmt_iterator *gsi) rhs = unshare_expr (rhs); if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs); + + /* Set for strnlen() calls with a non-constant bound. */ + bool noncst_bound = false; if (bound) - rhs = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (rhs), rhs, bound); + { + tree new_rhs + = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (rhs), rhs, bound); + + noncst_bound = (TREE_CODE (new_rhs) != INTEGER_CST + || tree_int_cst_lt (new_rhs, rhs)); + + rhs = new_rhs; + } + if (!update_call_from_tree (gsi, rhs)) gimplify_and_update_call_from_tree (gsi, rhs); stmt = gsi_stmt (*gsi); @@ -1281,6 +1293,12 @@ handle_builtin_strlen (gimple_stmt_iterator *gsi) fprintf (dump_file, "into: "); print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); } + + /* Avoid storing the length for calls to strnlen() with + a non-constant bound. */ + if (noncst_bound) + return; + if (si != NULL && TREE_CODE (si->nonzero_chars) != SSA_NAME && TREE_CODE (si->nonzero_chars) != INTEGER_CST @@ -1299,6 +1317,7 @@ handle_builtin_strlen (gimple_stmt_iterator *gsi) } if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)) return; + if (idx == 0) idx = new_stridx (src); else @@ -1333,9 +1352,14 @@ handle_builtin_strlen (gimple_stmt_iterator *gsi) } if (idx) { - strinfo *si = new_strinfo (src, idx, lhs, true); - set_strinfo (idx, si); - find_equal_ptrs (src, idx); + if (!bound) + { + /* Only store the new length information for calls to strlen(), + not for those to strnlen(). */ + strinfo *si = new_strinfo (src, idx, lhs, true); + set_strinfo (idx, si); + find_equal_ptrs (src, idx); + } /* For SRC that is an array of N elements, set LHS's range to [0, min (N, BOUND)]. A constant return value means @@ -1362,7 +1386,7 @@ handle_builtin_strlen (gimple_stmt_iterator *gsi) } } - if (strlen_to_stridx) + if (strlen_to_stridx && !bound) strlen_to_stridx->put (lhs, stridx_strlenloc (idx, loc)); } } |