Assign result of get_string_lenth to a SSA_NAME (PR tree-optimization/83552).

Message ID b564a5eb-4631-3f0b-9cfb-0e5dec9156b8@suse.cz
State New
Headers show
Series
  • Assign result of get_string_lenth to a SSA_NAME (PR tree-optimization/83552).
Related show

Commit Message

Martin Liška Dec. 22, 2017, 3:18 p.m.
Hi.

In order to fix the PR, I save temporary expression to a SSA_NAME that
is then used in the gcall.

Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.

Ready to be installed?
Martin


gcc/ChangeLog:

2017-12-22  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/83552
	* tree-ssa-strlen.c (fold_strstr_to_strncmp): Assign result
	of get_string_lenth to a SSA_NAME.

gcc/testsuite/ChangeLog:

2017-12-22  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/83552
	* gcc.dg/pr83552.c: New test.
---
 gcc/testsuite/gcc.dg/pr83552.c | 13 +++++++++++++
 gcc/tree-ssa-strlen.c          |  7 ++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr83552.c

Comments

Jakub Jelinek Dec. 22, 2017, 4:44 p.m. | #1
On Fri, Dec 22, 2017 at 04:18:05PM +0100, Martin Liška wrote:
> In order to fix the PR, I save temporary expression to a SSA_NAME that

> is then used in the gcall.


You need to do that only if (!is_gimple_val (arg1_len)).
Can you please emit the additional stmt only if that isn't true?

> --- a/gcc/tree-ssa-strlen.c

> +++ b/gcc/tree-ssa-strlen.c

> @@ -3005,12 +3005,17 @@ fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt)

>  	    {

>  	      gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt);

>  	      tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP);

> +

> +	      tree arg1_len_tmp = make_ssa_name (TREE_TYPE (arg1_len));

> +	      gassign *arg1_stmt = gimple_build_assign (arg1_len_tmp, arg1_len);

>  	      gcall *strncmp_call = gimple_build_call (strncmp_decl, 3,

> -						      arg0, arg1, arg1_len);

> +						      arg0, arg1,

> +						      arg1_len_tmp);

>  	      tree strncmp_lhs = make_ssa_name (integer_type_node);

>  	      gimple_set_vuse (strncmp_call, gimple_vuse (call_stmt));

>  	      gimple_call_set_lhs (strncmp_call, strncmp_lhs);

>  	      gsi_remove (&gsi, true);

> +	      gsi_insert_before (&gsi, arg1_stmt, GSI_SAME_STMT);

>  	      gsi_insert_before (&gsi, strncmp_call, GSI_SAME_STMT);

>  	      tree zero = build_zero_cst (TREE_TYPE (strncmp_lhs));

>  

> 



	Jakub
Martin Liška Dec. 27, 2017, 9:29 a.m. | #2
On 12/22/2017 05:44 PM, Jakub Jelinek wrote:
> You need to do that only if (!is_gimple_val (arg1_len)).

> Can you please emit the additional stmt only if that isn't true?


Sure.

This is what I'm going to install.

Martin
From 509e884e909a0b2ed56bf025c852f30b9ed43ae1 Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>

Date: Fri, 22 Dec 2017 12:37:17 +0100
Subject: [PATCH] Assign result of get_string_lenth to a SSA_NAME (PR
 tree-optimization/83552).

gcc/ChangeLog:

2017-12-22  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/83552
	* tree-ssa-strlen.c (fold_strstr_to_strncmp): Assign result
	of get_string_lenth to a SSA_NAME if not a GIMPLE value.

gcc/testsuite/ChangeLog:

2017-12-22  Martin Liska  <mliska@suse.cz>

	PR tree-optimization/83552
	* gcc.dg/pr83552.c: New test.
---
 gcc/testsuite/gcc.dg/pr83552.c | 13 +++++++++++++
 gcc/tree-ssa-strlen.c          | 10 ++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr83552.c

diff --git a/gcc/testsuite/gcc.dg/pr83552.c b/gcc/testsuite/gcc.dg/pr83552.c
new file mode 100644
index 00000000000..993cdd26581
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr83552.c
@@ -0,0 +1,13 @@
+/* PR tree-optimization/83364 */
+/* { dg-options "-O2" } */
+
+char *b;
+char d[100];
+void a ();
+void
+c (void)
+{
+  __builtin_strcat (d, "12345");
+  if (__builtin_strstr (b, d) == b)
+    a ();
+}
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c
index e812bd1e735..be6ab9f1e1b 100644
--- a/gcc/tree-ssa-strlen.c
+++ b/gcc/tree-ssa-strlen.c
@@ -3005,6 +3005,16 @@ fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt)
 	    {
 	      gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt);
 	      tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP);
+
+	      if (!is_gimple_val (arg1_len))
+		{
+		  tree arg1_len_tmp = make_ssa_name (TREE_TYPE (arg1_len));
+		  gassign *arg1_stmt = gimple_build_assign (arg1_len_tmp,
+							    arg1_len);
+		  gsi_insert_before (&gsi, arg1_stmt, GSI_SAME_STMT);
+		  arg1_len = arg1_len_tmp;
+		}
+
 	      gcall *strncmp_call = gimple_build_call (strncmp_decl, 3,
 						      arg0, arg1, arg1_len);
 	      tree strncmp_lhs = make_ssa_name (integer_type_node);
-- 
2.14.3

Patch

diff --git a/gcc/testsuite/gcc.dg/pr83552.c b/gcc/testsuite/gcc.dg/pr83552.c
new file mode 100644
index 00000000000..993cdd26581
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr83552.c
@@ -0,0 +1,13 @@ 
+/* PR tree-optimization/83364 */
+/* { dg-options "-O2" } */
+
+char *b;
+char d[100];
+void a ();
+void
+c (void)
+{
+  __builtin_strcat (d, "12345");
+  if (__builtin_strstr (b, d) == b)
+    a ();
+}
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c
index e812bd1e735..43dc22d5ce0 100644
--- a/gcc/tree-ssa-strlen.c
+++ b/gcc/tree-ssa-strlen.c
@@ -3005,12 +3005,17 @@  fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt)
 	    {
 	      gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt);
 	      tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP);
+
+	      tree arg1_len_tmp = make_ssa_name (TREE_TYPE (arg1_len));
+	      gassign *arg1_stmt = gimple_build_assign (arg1_len_tmp, arg1_len);
 	      gcall *strncmp_call = gimple_build_call (strncmp_decl, 3,
-						      arg0, arg1, arg1_len);
+						      arg0, arg1,
+						      arg1_len_tmp);
 	      tree strncmp_lhs = make_ssa_name (integer_type_node);
 	      gimple_set_vuse (strncmp_call, gimple_vuse (call_stmt));
 	      gimple_call_set_lhs (strncmp_call, strncmp_lhs);
 	      gsi_remove (&gsi, true);
+	      gsi_insert_before (&gsi, arg1_stmt, GSI_SAME_STMT);
 	      gsi_insert_before (&gsi, strncmp_call, GSI_SAME_STMT);
 	      tree zero = build_zero_cst (TREE_TYPE (strncmp_lhs));