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] buitlins.c: Fix tree-optimization/14819.


Hi,

Attached is a patch to fix tree-optimization/14819.

This patch folds strchr like the followwing.

-   T.3160_20 = strchr ("<12ers", 60);
-   if (T.3160_20 == 0B) goto <L70>; else goto <L71>;

This is simplified in the usual manner.

Unfortunately,

!   T.2456_739 = strchr ("<12ers", 49);

is folded as

!   T.2456_739 = "<12ers" + 1B;

and the expression stays this way.

This problem has been filed as PR tree-optimization/15347.

As far as the performance of GCC is concerned, I compiled fold-const.i
and combine.i.

                 w/o   w/
--------------------------------------
fold-const.i 133.820s 132.780s -0.778%
combine.i     55.380s  55.100s -0.506%

With the patch, GCC's performance is improved.  The time shown is the
time it took to compile fold-const.i or combine.i three times with
-O2.

This patch is developed by Steven, and I made no change to it.

Tested on i686-pc-linux-gnu.  OK to apply?

Kazu Hirata

2004-05-21  Steven Bosscher  <stevenb@suse.de>

	PR tree-optimization/14819
	* builtins.c (fold_builtin_strchr): New.
	(fold_builtin_1): Handle BUILT_IN_STRCHR and BUILT_IN_STRRCHR
	with fold_builtin_strchr().

Index: builtins.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/builtins.c,v
retrieving revision 1.323
diff -u -r1.323 builtins.c
--- builtins.c	15 May 2004 18:17:20 -0000	1.323
+++ builtins.c	17 May 2004 14:58:42 -0000
@@ -7081,6 +7081,46 @@
   return 0;
 }
 
+/* Fold function call to buitlin strchr and strrchr.
+   Return NULL_TREE if no simplification can be made.  */
+static tree
+fold_builtin_strchr (tree exp, bool actually_strrchr)
+{
+  tree arglist = TREE_OPERAND (exp, 1);
+  if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
+    return 0;
+  else
+    {
+      tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist));
+      const char *p1;
+
+      if (TREE_CODE (s2) != INTEGER_CST)
+	return 0;
+
+      p1 = c_getstr (s1);
+      if (p1 != NULL)
+	{
+	  char c;
+	  const char *r;
+
+	  if (target_char_cast (s2, &c))
+	    return 0;
+
+	  r = actually_strrchr ? strrchr (p1, c) : strchr (p1, c);
+
+	  if (r == NULL)
+	    return integer_zero_node;
+
+	  /* Return an offset into the constant string argument.  */
+	  return fold (build (PLUS_EXPR, TREE_TYPE (s1),
+			      s1, convert (TREE_TYPE (s1),
+					   ssize_int (r - p1))));
+	}
+
+      return 0;
+    }
+}
+
 /* Fold function call to builtin memcmp.  Return
    NULL_TREE if no simplification can be made.  */
 
@@ -7792,6 +7832,12 @@
 
     case BUILT_IN_STRNCPY:
       return fold_builtin_strncpy (exp);
+
+    case BUILT_IN_STRCHR:
+      return fold_builtin_strchr (exp, false);
+
+    case BUILT_IN_STRRCHR:
+      return fold_builtin_strchr (exp, true);
 
     case BUILT_IN_MEMCMP:
       return fold_builtin_memcmp (exp);


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