tree-ssa-strlen.c (adjust_related_strinfos): Avoid trailing article.

gcc/ChangeLog:

	* tree-ssa-strlen.c (adjust_related_strinfos): Avoid trailing article.
	(handle_builtin_malloc): Remove trailing spaces.
	(handle_builtin_memset): Same.
	(handle_builtin_memcmp): Same.
	(compute_string_length): Same.
	(determine_min_objsize): Same.
	(handle_builtin_string_cmp): Same.
	(handle_char_store): Same.  Break up excessively long line.

From-SVN: r272016
This commit is contained in:
Martin Sebor 2019-06-06 18:46:20 +00:00 committed by Martin Sebor
parent c56c86024f
commit 2172277702
2 changed files with 70 additions and 57 deletions

View File

@ -1,3 +1,14 @@
2019-06-06 Martin Sebor <msebor@redhat.com>
* tree-ssa-strlen.c (adjust_related_strinfos): Avoid trailing article.
(handle_builtin_malloc): Remove trailing spaces.
(handle_builtin_memset): Same.
(handle_builtin_memcmp): Same.
(compute_string_length): Same.
(determine_min_objsize): Same.
(handle_builtin_string_cmp): Same.
(handle_char_store): Same. Break up excessively long line.
2019-06-06 Martin Jambor <mjambor@suse.cz> 2019-06-06 Martin Jambor <mjambor@suse.cz>
* tree-sra.c (build_reconstructed_reference): Drop the alignment * tree-sra.c (build_reconstructed_reference): Drop the alignment

View File

@ -891,9 +891,9 @@ adjust_related_strinfos (location_t loc, strinfo *origsi, tree adj)
tree tem; tree tem;
si = unshare_strinfo (si); si = unshare_strinfo (si);
/* We shouldn't see delayed lengths here; the caller must have /* We shouldn't see delayed lengths here; the caller must
calculated the old length in order to calculate the have calculated the old length in order to calculate
adjustment. */ the adjustment. */
gcc_assert (si->nonzero_chars); gcc_assert (si->nonzero_chars);
tem = fold_convert_loc (loc, TREE_TYPE (si->nonzero_chars), adj); tem = fold_convert_loc (loc, TREE_TYPE (si->nonzero_chars), adj);
si->nonzero_chars = fold_build2_loc (loc, PLUS_EXPR, si->nonzero_chars = fold_build2_loc (loc, PLUS_EXPR,
@ -2759,7 +2759,7 @@ handle_builtin_malloc (enum built_in_function bcode, gimple_stmt_iterator *gsi)
/* Handle a call to memset. /* Handle a call to memset.
After a call to calloc, memset(,0,) is unnecessary. After a call to calloc, memset(,0,) is unnecessary.
memset(malloc(n),0,n) is calloc(n,1). memset(malloc(n),0,n) is calloc(n,1).
return true when the call is transfomred, false otherwise. */ return true when the call is transfomred, false otherwise. */
static bool static bool
@ -2815,7 +2815,7 @@ handle_builtin_memset (gimple_stmt_iterator *gsi)
/* Handle a call to memcmp. We try to handle small comparisons by /* Handle a call to memcmp. We try to handle small comparisons by
converting them to load and compare, and replacing the call to memcmp converting them to load and compare, and replacing the call to memcmp
with a __builtin_memcmp_eq call where possible. with a __builtin_memcmp_eq call where possible.
return true when call is transformed, return false otherwise. */ return true when call is transformed, return false otherwise. */
static bool static bool
@ -2898,13 +2898,13 @@ handle_builtin_memcmp (gimple_stmt_iterator *gsi)
return true; return true;
} }
/* Given an index to the strinfo vector, compute the string length for the /* Given an index to the strinfo vector, compute the string length
corresponding string. Return -1 when unknown. */ for the corresponding string. Return -1 when unknown. */
static HOST_WIDE_INT static HOST_WIDE_INT
compute_string_length (int idx) compute_string_length (int idx)
{ {
HOST_WIDE_INT string_leni = -1; HOST_WIDE_INT string_leni = -1;
gcc_assert (idx != 0); gcc_assert (idx != 0);
if (idx < 0) if (idx < 0)
@ -2924,9 +2924,9 @@ compute_string_length (int idx)
return string_leni; return string_leni;
} }
/* Determine the minimum size of the object referenced by DEST expression which /* Determine the minimum size of the object referenced by DEST expression
must have a pointer type. which must have a pointer type.
Return the minimum size of the object if successful or NULL when the size Return the minimum size of the object if successful or NULL when the size
cannot be determined. */ cannot be determined. */
static tree static tree
determine_min_objsize (tree dest) determine_min_objsize (tree dest)
@ -2936,8 +2936,8 @@ determine_min_objsize (tree dest)
if (compute_builtin_object_size (dest, 2, &size)) if (compute_builtin_object_size (dest, 2, &size))
return build_int_cst (sizetype, size); return build_int_cst (sizetype, size);
/* Try to determine the size of the object through the RHS of the /* Try to determine the size of the object through the RHS
assign statement. */ of the assign statement. */
if (TREE_CODE (dest) == SSA_NAME) if (TREE_CODE (dest) == SSA_NAME)
{ {
gimple *stmt = SSA_NAME_DEF_STMT (dest); gimple *stmt = SSA_NAME_DEF_STMT (dest);
@ -2962,13 +2962,13 @@ determine_min_objsize (tree dest)
type = TYPE_MAIN_VARIANT (type); type = TYPE_MAIN_VARIANT (type);
/* We cannot determine the size of the array if it's a flexible array, /* We cannot determine the size of the array if it's a flexible array,
which is declared at the end of a structure. */ which is declared at the end of a structure. */
if (TREE_CODE (type) == ARRAY_TYPE if (TREE_CODE (type) == ARRAY_TYPE
&& !array_at_struct_end_p (dest)) && !array_at_struct_end_p (dest))
{ {
tree size_t = TYPE_SIZE_UNIT (type); tree size_t = TYPE_SIZE_UNIT (type);
if (size_t && TREE_CODE (size_t) == INTEGER_CST if (size_t && TREE_CODE (size_t) == INTEGER_CST
&& !integer_zerop (size_t)) && !integer_zerop (size_t))
return size_t; return size_t;
} }
@ -2976,19 +2976,19 @@ determine_min_objsize (tree dest)
return NULL_TREE; return NULL_TREE;
} }
/* Handle a call to strcmp or strncmp. When the result is ONLY used to do /* Handle a call to strcmp or strncmp. When the result is ONLY used to do
equality test against zero: equality test against zero:
A. When the lengths of both arguments are constant and it's a strcmp: A. When the lengths of both arguments are constant and it's a strcmp:
* if the lengths are NOT equal, we can safely fold the call * if the lengths are NOT equal, we can safely fold the call
to a non-zero value. to a non-zero value.
* otherwise, do nothing now. * otherwise, do nothing now.
B. When the length of one argument is constant, try to replace the call with
a __builtin_str(n)cmp_eq call where possible, i.e:
strncmp (s, STR, C) (!)= 0 in which, s is a pointer to a string, STR is a B. When the length of one argument is constant, try to replace the call
string with constant length , C is a constant. with a __builtin_str(n)cmp_eq call where possible, i.e:
strncmp (s, STR, C) (!)= 0 in which, s is a pointer to a string, STR
is a string with constant length , C is a constant.
if (C <= strlen(STR) && sizeof_array(s) > C) if (C <= strlen(STR) && sizeof_array(s) > C)
{ {
replace this call with replace this call with
@ -3000,16 +3000,16 @@ determine_min_objsize (tree dest)
can handled by the following strcmp. can handled by the following strcmp.
} }
strcmp (s, STR) (!)= 0 in which, s is a pointer to a string, STR is a strcmp (s, STR) (!)= 0 in which, s is a pointer to a string, STR
string with constant length. is a string with constant length.
if (sizeof_array(s) > strlen(STR)) if (sizeof_array(s) > strlen(STR))
{ {
replace this call with replace this call with
strcmp_eq (s, STR, strlen(STR)+1) (!)= 0 strcmp_eq (s, STR, strlen(STR)+1) (!)= 0
} }
Return true when the call is transformed, return false otherwise. Return true when the call is transformed, return false otherwise.
*/ */
static bool static bool
handle_builtin_string_cmp (gimple_stmt_iterator *gsi) handle_builtin_string_cmp (gimple_stmt_iterator *gsi)
@ -3047,19 +3047,19 @@ handle_builtin_string_cmp (gimple_stmt_iterator *gsi)
return false; return false;
/* When the result is ONLY used to do equality test against zero. */ /* When the result is ONLY used to do equality test against zero. */
FOR_EACH_IMM_USE_FAST (use_p, iter, res) FOR_EACH_IMM_USE_FAST (use_p, iter, res)
{ {
gimple *use_stmt = USE_STMT (use_p); gimple *use_stmt = USE_STMT (use_p);
if (is_gimple_debug (use_stmt)) if (is_gimple_debug (use_stmt))
continue; continue;
if (gimple_code (use_stmt) == GIMPLE_ASSIGN) if (gimple_code (use_stmt) == GIMPLE_ASSIGN)
{ {
tree_code code = gimple_assign_rhs_code (use_stmt); tree_code code = gimple_assign_rhs_code (use_stmt);
if (code == COND_EXPR) if (code == COND_EXPR)
{ {
tree cond_expr = gimple_assign_rhs1 (use_stmt); tree cond_expr = gimple_assign_rhs1 (use_stmt);
if ((TREE_CODE (cond_expr) != EQ_EXPR if ((TREE_CODE (cond_expr) != EQ_EXPR
&& (TREE_CODE (cond_expr) != NE_EXPR)) && (TREE_CODE (cond_expr) != NE_EXPR))
|| !integer_zerop (TREE_OPERAND (cond_expr, 1))) || !integer_zerop (TREE_OPERAND (cond_expr, 1)))
return false; return false;
@ -3069,7 +3069,7 @@ handle_builtin_string_cmp (gimple_stmt_iterator *gsi)
if (!integer_zerop (gimple_assign_rhs2 (use_stmt))) if (!integer_zerop (gimple_assign_rhs2 (use_stmt)))
return false; return false;
} }
else else
return false; return false;
} }
else if (gimple_code (use_stmt) == GIMPLE_COND) else if (gimple_code (use_stmt) == GIMPLE_COND)
@ -3082,21 +3082,21 @@ handle_builtin_string_cmp (gimple_stmt_iterator *gsi)
else else
return false; return false;
} }
/* When the lengths of both arguments are known, and they are unequal, we can /* When the lengths of both arguments are known, and they are unequal,
safely fold the call to a non-zero value for strcmp; we can safely fold the call to a non-zero value for strcmp;
othewise, do nothing now. */ othewise, do nothing now. */
if (idx1 != 0 && idx2 != 0) if (idx1 != 0 && idx2 != 0)
{ {
HOST_WIDE_INT const_string_leni1 = compute_string_length (idx1); HOST_WIDE_INT const_string_leni1 = compute_string_length (idx1);
HOST_WIDE_INT const_string_leni2 = compute_string_length (idx2); HOST_WIDE_INT const_string_leni2 = compute_string_length (idx2);
if (!is_ncmp if (!is_ncmp
&& const_string_leni1 != -1 && const_string_leni1 != -1
&& const_string_leni2 != -1 && const_string_leni2 != -1
&& const_string_leni1 != const_string_leni2) && const_string_leni1 != const_string_leni2)
{ {
replace_call_with_value (gsi, integer_one_node); replace_call_with_value (gsi, integer_one_node);
return true; return true;
} }
return false; return false;
@ -3105,56 +3105,56 @@ handle_builtin_string_cmp (gimple_stmt_iterator *gsi)
/* When the length of one argument is constant. */ /* When the length of one argument is constant. */
tree var_string = NULL_TREE; tree var_string = NULL_TREE;
HOST_WIDE_INT const_string_leni = -1; HOST_WIDE_INT const_string_leni = -1;
if (idx1) if (idx1)
{ {
const_string_leni = compute_string_length (idx1); const_string_leni = compute_string_length (idx1);
var_string = arg2; var_string = arg2;
} }
else else
{ {
gcc_checking_assert (idx2); gcc_checking_assert (idx2);
const_string_leni = compute_string_length (idx2); const_string_leni = compute_string_length (idx2);
var_string = arg1; var_string = arg1;
} }
if (const_string_leni < 0) if (const_string_leni < 0)
return false; return false;
unsigned HOST_WIDE_INT var_sizei = 0; unsigned HOST_WIDE_INT var_sizei = 0;
/* try to determine the minimum size of the object pointed by var_string. */ /* try to determine the minimum size of the object pointed by var_string. */
tree size = determine_min_objsize (var_string); tree size = determine_min_objsize (var_string);
if (!size) if (!size)
return false; return false;
if (tree_fits_uhwi_p (size)) if (tree_fits_uhwi_p (size))
var_sizei = tree_to_uhwi (size); var_sizei = tree_to_uhwi (size);
if (var_sizei == 0) if (var_sizei == 0)
return false; return false;
/* For strncmp, if length > const_string_leni , this call can be safely /* For strncmp, if length > const_string_leni , this call can be safely
transformed to a strcmp. */ transformed to a strcmp. */
if (is_ncmp && length > const_string_leni) if (is_ncmp && length > const_string_leni)
is_ncmp = false; is_ncmp = false;
unsigned HOST_WIDE_INT final_length unsigned HOST_WIDE_INT final_length
= is_ncmp ? length : const_string_leni + 1; = is_ncmp ? length : const_string_leni + 1;
/* Replace strcmp or strncmp with the corresponding str(n)cmp_eq. */ /* Replace strcmp or strncmp with the corresponding str(n)cmp_eq. */
if (var_sizei > final_length) if (var_sizei > final_length)
{ {
tree fn tree fn
= (is_ncmp = (is_ncmp
? builtin_decl_implicit (BUILT_IN_STRNCMP_EQ) ? builtin_decl_implicit (BUILT_IN_STRNCMP_EQ)
: builtin_decl_implicit (BUILT_IN_STRCMP_EQ)); : builtin_decl_implicit (BUILT_IN_STRCMP_EQ));
if (!fn) if (!fn)
return false; return false;
tree const_string_len = build_int_cst (size_type_node, final_length); tree const_string_len = build_int_cst (size_type_node, final_length);
update_gimple_call (gsi, fn, 3, arg1, arg2, const_string_len); update_gimple_call (gsi, fn, 3, arg1, arg2, const_string_len);
} }
else else
return false; return false;
return true; return true;
@ -3355,7 +3355,7 @@ handle_char_store (gimple_stmt_iterator *gsi)
and if we aren't storing '\0', we know that the length of the and if we aren't storing '\0', we know that the length of the
string and any other zero terminated string in memory remains string and any other zero terminated string in memory remains
the same. In that case we move to the next gimple statement and the same. In that case we move to the next gimple statement and
return to signal the caller that it shouldn't invalidate anything. return to signal the caller that it shouldn't invalidate anything.
This is benefical for cases like: This is benefical for cases like:
@ -3370,13 +3370,15 @@ handle_char_store (gimple_stmt_iterator *gsi)
size_t len4 = strlen (q); // This can be optimized into len2 size_t len4 = strlen (q); // This can be optimized into len2
bar (len, len2, len3, len4); bar (len, len2, len3, len4);
} }
*/ */
else if (storing_nonzero_p && cmp > 0) else if (storing_nonzero_p && cmp > 0)
{ {
gsi_next (gsi); gsi_next (gsi);
return false; return false;
} }
else if (storing_all_zeros_p || storing_nonzero_p || (offset != 0 && cmp > 0)) else if (storing_all_zeros_p
|| storing_nonzero_p
|| (offset != 0 && cmp > 0))
{ {
/* When STORING_NONZERO_P, we know that the string will start /* When STORING_NONZERO_P, we know that the string will start
with at least OFFSET + 1 nonzero characters. If storing with at least OFFSET + 1 nonzero characters. If storing