This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR3511, missed PRE of strlen with constant argument


This patch makes PRE recognize that strlen ("...") (and other pure
builtin functions) is a compile-time constant and so optimize for example

  if (!s)
    s = "";
  return strlen(s);

to return !s ? 0 : strlen(s);

Bootstrapped and regtested on x86_64-unknown-linux-gnu.

Richard.

:ADDPATCH PRE:


2006-05-08  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/3511
	* tree-ssa-pre.c (phi_translate): Fold CALL_EXPRs that
	got new invariant arguments during PHI translation.

	* gcc.dg/tree-ssa/ssa-pre-15.c: New testcase.

Index: tree-ssa-pre.c
===================================================================
--- tree-ssa-pre.c	(revision 113628)
+++ tree-ssa-pre.c	(working copy)
@@ -1075,6 +1075,7 @@ phi_translate (tree expr, value_set_t se
 	    tree newexpr;
 	    tree vh = get_value_handle (expr);
 	    bool listchanged = false;
+	    bool invariantarg = false;
 	    VEC (tree, gc) *vuses = VALUE_HANDLE_VUSES (vh);
 	    VEC (tree, gc) *tvuses;
 
@@ -1133,10 +1134,26 @@ phi_translate (tree expr, value_set_t se
 		    if (newval != oldval)
 		      {
 			listchanged = true;
+			invariantarg |= is_gimple_min_invariant (newval);
 			TREE_VALUE (newwalker) = get_value_handle (newval);
 		      }
 		  }
 	      }
+
+	    /* In case of new invariant args we might try to fold the call
+	       again.  */
+	    if (invariantarg)
+	      {
+		tree tmp = fold_ternary (CALL_EXPR, TREE_TYPE (expr),
+					 newop0, newarglist, newop2);
+		if (tmp)
+		  {
+		    STRIP_TYPE_NOPS (tmp);
+		    if (is_gimple_min_invariant (tmp))
+		      return tmp;
+		  }
+	      }
+
 	    if (listchanged)
 	      vn_lookup_or_add (newarglist, NULL);
 	    
Index: testsuite/gcc.dg/tree-ssa/ssa-pre-15.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/ssa-pre-15.c	(revision 0)
+++ testsuite/gcc.dg/tree-ssa/ssa-pre-15.c	(revision 0)
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Verify we PRE the strlen call, as strlen("") folds to zero.  */
+
+extern __SIZE_TYPE__ strlen (const char *);
+
+__SIZE_TYPE__ mystrlen (const char *s)
+{
+  if (!s)
+    s = "";
+  return strlen(s);
+}
+
+/* { dg-final { scan-tree-dump "= 0;" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]