This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Patch to add __builtin_printf("string\n") -> puts("string")
- To: gcc-patches at gcc dot gnu dot org
- Subject: Patch to add __builtin_printf("string\n") -> puts("string")
- From: "Kaveh R. Ghazi" <ghazi at caip dot rutgers dot edu>
- Date: Thu, 21 Sep 2000 14:20:27 -0400 (EDT)
This patch adds another __builtin_printf transformation, namely:
printf("string\n") -> puts("string")
Note this transformation is a subset of the printf->fputs one, however
it is still is worthwhile to have it because it can be done even if
the user didn't include stdio.h. (Also there's no guarantee I'll be
able to get printf->fputs working for c++ so this is a good fallback.)
Testing on solaris2.7 indicates it's about 39% faster to use puts over
printf in the case of a constant string ending in a newline.
Bootstrap and a testsuite run are currently in progress, assuming no
regressions, okay to install?
Thanks,
--Kaveh
2000-09-21 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* builtins.c (expand_builtin_printf): Handle converting
printf("string\n") -> puts("string").
--- orig/egcs-CVS20000920/gcc/builtins.c Wed Sep 20 21:55:28 2000
+++ egcs-CVS20000920/gcc/builtins.c Thu Sep 21 12:38:39 2000
@@ -2498,6 +2498,28 @@ expand_builtin_printf (arglist, ignore)
fn = fn_putchar;
}
+ /* If the resulting constant was "string\n", call
+ __builtin_puts("string"). Ensure "string" has at least one
+ character besides the trailing \n. Note, TREE_STRING_LENGTH
+ includes the terminating NULL in its count. */
+ else if (TREE_STRING_LENGTH (stripped_string) > 2
+ && TREE_STRING_POINTER (stripped_string)
+ [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
+ {
+ /* Create a NULL-terminated string that's one char shorter
+ than the original, stripping off the trailing '\n'. */
+ const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
+ char *newstr = (char *) alloca (newlen);
+ memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
+ newstr[newlen - 1] = 0;
+
+ arglist = build_string (newlen, newstr);
+ TREE_TYPE (arglist) = copy_node (TREE_TYPE (stripped_string));
+ arglist = build1 (ADDR_EXPR,
+ build_pointer_type (TREE_TYPE (arglist)), arglist);
+ arglist = build_tree_list (NULL_TREE, arglist);
+ fn = fn_puts;
+ }
else
/* We'd like to arrange to call fputs(string) here, but we
need stdout and don't have a way to get it ... yet. */