Commit f368600f by Martin Liska Committed by Martin Liska

Fix strstr folding (PR tree-optimization/79196).

2017-01-23  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/79196
	* tree-ssa-strlen.c (fold_strstr_to_memcmp): Rename to ...
	(fold_strstr_to_strncmp): ... this.  Fold the pattern to strncmp
	instead of memcmp.
	(strlen_optimize_stmt): Call the renamed function.
2017-01-23  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/79196
	* gcc.dg/asan/pr79196.c: New test.
	* gcc.dg/strlenopt-30.c: Update scanned pattern.

From-SVN: r244812
parent d886761f
2017-01-23 Martin Liska <mliska@suse.cz>
PR tree-optimization/79196
* tree-ssa-strlen.c (fold_strstr_to_memcmp): Rename to ...
(fold_strstr_to_strncmp): ... this. Fold the pattern to strncmp
instead of memcmp.
(strlen_optimize_stmt): Call the renamed function.
2017-01-23 Michael Matz <matz@suse.de> 2017-01-23 Michael Matz <matz@suse.de>
PR tree-optimization/78384 PR tree-optimization/78384
......
2017-01-23 Martin Liska <mliska@suse.cz>
PR tree-optimization/79196
* gcc.dg/asan/pr79196.c: New test.
* gcc.dg/strlenopt-30.c: Update scanned pattern.
2017-01-23 Michael Matz <matz@suse.de> 2017-01-23 Michael Matz <matz@suse.de>
PR tree-optimization/78384 PR tree-optimization/78384
......
// PR tree-optimization/79196
// { dg-do run }
int
__attribute__((noinline))
test(char *a)
{
if (__builtin_strstr (a, "DROP CONVERSION") == a)
return 1;
return 0;
}
int main(int argc, char **argv)
{
return test ("x");
}
...@@ -60,4 +60,4 @@ _Bool f7(char *s) ...@@ -60,4 +60,4 @@ _Bool f7(char *s)
return (t1 == s); return (t1 == s);
} }
/* { dg-final { scan-tree-dump-times "__builtin_memcmp" 5 "strlen" } } */ /* { dg-final { scan-tree-dump-times "__builtin_strncmp" 5 "strlen" } } */
...@@ -2225,10 +2225,10 @@ handle_char_store (gimple_stmt_iterator *gsi) ...@@ -2225,10 +2225,10 @@ handle_char_store (gimple_stmt_iterator *gsi)
return true; return true;
} }
/* Try to fold strstr (s, t) eq/ne s to memcmp (s, t, strlen (t)) eq/ne 0. */ /* Try to fold strstr (s, t) eq/ne s to strncmp (s, t, strlen (t)) eq/ne 0. */
static void static void
fold_strstr_to_memcmp (tree rhs1, tree rhs2, gimple *stmt) fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt)
{ {
if (TREE_CODE (rhs1) != SSA_NAME if (TREE_CODE (rhs1) != SSA_NAME
|| TREE_CODE (rhs2) != SSA_NAME) || TREE_CODE (rhs2) != SSA_NAME)
...@@ -2273,34 +2273,34 @@ fold_strstr_to_memcmp (tree rhs1, tree rhs2, gimple *stmt) ...@@ -2273,34 +2273,34 @@ fold_strstr_to_memcmp (tree rhs1, tree rhs2, gimple *stmt)
if (arg1_len != NULL_TREE) if (arg1_len != NULL_TREE)
{ {
gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt); gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt);
tree memcmp_decl = builtin_decl_explicit (BUILT_IN_MEMCMP); tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP);
gcall *memcmp_call = gimple_build_call (memcmp_decl, 3, gcall *strncmp_call = gimple_build_call (strncmp_decl, 3,
arg0, arg1, arg1_len); arg0, arg1, arg1_len);
tree memcmp_lhs = make_ssa_name (integer_type_node); tree strncmp_lhs = make_ssa_name (integer_type_node);
gimple_set_vuse (memcmp_call, gimple_vuse (call_stmt)); gimple_set_vuse (strncmp_call, gimple_vuse (call_stmt));
gimple_call_set_lhs (memcmp_call, memcmp_lhs); gimple_call_set_lhs (strncmp_call, strncmp_lhs);
gsi_remove (&gsi, true); gsi_remove (&gsi, true);
gsi_insert_before (&gsi, memcmp_call, GSI_SAME_STMT); gsi_insert_before (&gsi, strncmp_call, GSI_SAME_STMT);
tree zero = build_zero_cst (TREE_TYPE (memcmp_lhs)); tree zero = build_zero_cst (TREE_TYPE (strncmp_lhs));
if (is_gimple_assign (stmt)) if (is_gimple_assign (stmt))
{ {
if (gimple_assign_rhs_code (stmt) == COND_EXPR) if (gimple_assign_rhs_code (stmt) == COND_EXPR)
{ {
tree cond = gimple_assign_rhs1 (stmt); tree cond = gimple_assign_rhs1 (stmt);
TREE_OPERAND (cond, 0) = memcmp_lhs; TREE_OPERAND (cond, 0) = strncmp_lhs;
TREE_OPERAND (cond, 1) = zero; TREE_OPERAND (cond, 1) = zero;
} }
else else
{ {
gimple_assign_set_rhs1 (stmt, memcmp_lhs); gimple_assign_set_rhs1 (stmt, strncmp_lhs);
gimple_assign_set_rhs2 (stmt, zero); gimple_assign_set_rhs2 (stmt, zero);
} }
} }
else else
{ {
gcond *cond = as_a<gcond *> (stmt); gcond *cond = as_a<gcond *> (stmt);
gimple_cond_set_lhs (cond, memcmp_lhs); gimple_cond_set_lhs (cond, strncmp_lhs);
gimple_cond_set_rhs (cond, zero); gimple_cond_set_rhs (cond, zero);
} }
update_stmt (stmt); update_stmt (stmt);
...@@ -2398,12 +2398,12 @@ strlen_optimize_stmt (gimple_stmt_iterator *gsi) ...@@ -2398,12 +2398,12 @@ strlen_optimize_stmt (gimple_stmt_iterator *gsi)
enum tree_code cond_code = TREE_CODE (cond); enum tree_code cond_code = TREE_CODE (cond);
if (cond_code == EQ_EXPR || cond_code == NE_EXPR) if (cond_code == EQ_EXPR || cond_code == NE_EXPR)
fold_strstr_to_memcmp (TREE_OPERAND (cond, 0), fold_strstr_to_strncmp (TREE_OPERAND (cond, 0),
TREE_OPERAND (cond, 1), stmt); TREE_OPERAND (cond, 1), stmt);
} }
else if (code == EQ_EXPR || code == NE_EXPR) else if (code == EQ_EXPR || code == NE_EXPR)
fold_strstr_to_memcmp (gimple_assign_rhs1 (stmt), fold_strstr_to_strncmp (gimple_assign_rhs1 (stmt),
gimple_assign_rhs2 (stmt), stmt); gimple_assign_rhs2 (stmt), stmt);
} }
else if (TREE_CODE (lhs) != SSA_NAME && !TREE_SIDE_EFFECTS (lhs)) else if (TREE_CODE (lhs) != SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
{ {
...@@ -2423,8 +2423,8 @@ strlen_optimize_stmt (gimple_stmt_iterator *gsi) ...@@ -2423,8 +2423,8 @@ strlen_optimize_stmt (gimple_stmt_iterator *gsi)
{ {
enum tree_code code = gimple_cond_code (cond); enum tree_code code = gimple_cond_code (cond);
if (code == EQ_EXPR || code == NE_EXPR) if (code == EQ_EXPR || code == NE_EXPR)
fold_strstr_to_memcmp (gimple_cond_lhs (stmt), fold_strstr_to_strncmp (gimple_cond_lhs (stmt),
gimple_cond_rhs (stmt), stmt); gimple_cond_rhs (stmt), stmt);
} }
if (gimple_vdef (stmt)) if (gimple_vdef (stmt))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment