Patchwork Improve handle_char_store in strlen pass (PR tree-optimization/57230)

login
register
mail settings
Submitter Jakub Jelinek
Date May 10, 2013, 2:49 p.m.
Message ID <20130510144949.GQ1377@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/243004/
State New
Headers show

Comments

Jakub Jelinek - May 10, 2013, 2:49 p.m.
Hi!

Related to the previously posted patch, I've noticed that
on the attached testcase we don't optimize the strlen into
constant, even when we easily could.

Implemented thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
ok for trunk?

2013-05-10  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/57230
	* tree-ssa-strlen.c (handle_char_store): Record length for
	array store from STRING_CST.

	* gcc.dg/strlenopt-24.c: New test.


	Jakub
Jeff Law - May 10, 2013, 11 p.m.
On 05/10/2013 08:49 AM, Jakub Jelinek wrote:
> Hi!
>
> Related to the previously posted patch, I've noticed that
> on the attached testcase we don't optimize the strlen into
> constant, even when we easily could.
>
> Implemented thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
> ok for trunk?
>
> 2013-05-10  Jakub Jelinek  <jakub@redhat.com>
>
> 	PR tree-optimization/57230
> 	* tree-ssa-strlen.c (handle_char_store): Record length for
> 	array store from STRING_CST.
>
> 	* gcc.dg/strlenopt-24.c: New test.
OK.

A bit surprised we weren't already handling this...

jeff

Patch

--- gcc/tree-ssa-strlen.c.jj	2013-05-10 08:57:20.000000000 +0200
+++ gcc/tree-ssa-strlen.c	2013-05-10 10:22:03.166458997 +0200
@@ -1740,6 +1740,25 @@  handle_char_store (gimple_stmt_iterator
       if (si != NULL)
 	si->writable = true;
     }
+  else if (idx == 0
+	   && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST
+	   && ssaname == NULL_TREE
+	   && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
+    {
+      size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt)));
+      HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
+      if (a > 0 && (unsigned HOST_WIDE_INT) a > l)
+	{
+	  int idx = new_addr_stridx (lhs);
+	  if (idx != 0)
+	    {
+	      si = new_strinfo (build_fold_addr_expr (lhs), idx,
+				build_int_cst (size_type_node, l));
+	      set_strinfo (idx, si);
+	      si->dont_invalidate = true;
+	    }
+	}
+    }
 
   if (si != NULL && initializer_zerop (gimple_assign_rhs1 (stmt)))
     {
--- gcc/testsuite/gcc.dg/strlenopt-24.c.jj	2013-05-10 10:30:01.343821849 +0200
+++ gcc/testsuite/gcc.dg/strlenopt-24.c	2013-05-10 10:30:37.736621582 +0200
@@ -0,0 +1,17 @@ 
+/* PR tree-optimization/57230 */
+/* { dg-do run } */
+/* { dg-options "-O2 -fdump-tree-strlen" } */
+
+#include "strlenopt.h"
+
+int
+main ()
+{
+  char p[] = "hello world";
+  if (strlen (p) != 11)
+    abort ();
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "strlen \\(" 0 "strlen" } } *
+/* { dg-final { cleanup-tree-dump "strlen" } } */