public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] Optimize var = STRING_CST
Date: Thu, 23 Aug 2007 23:48:00 -0000	[thread overview]
Message-ID: <20070823212347.GC2063@devserv.devel.redhat.com> (raw)

Hi!

The following patch optimizes initialization of an array from a STRING_CST.
Without this patch store_expr will always force the STRING_CST into memory
and then do a block move from there, followed by optional clear_storage
if the STRING_CST is shorter than the array.  This patch uses
store_by_pieces if possible.
With the patch:
struct A { char c[10]; };

void
foo (void)
{
  struct A a = { "abcdefghi" };
  baz (&a);
}

void
bar (void)
{
  struct A a;
  __builtin_strcpy (&a.c[0], "abcdefghi");
  baz (&a);
}
both routines are the same on x86_64 except for
slightly different register allocation, without that patch
for foo a .LC0 constant with the string literal is emitted and foo
copies over from that string into a.c array.

Tested on x86_64-linux, ok for trunk?

BTW, I wonder if we shouldn't use alias set 0 for all the initialization
stores.  If yes, this is something that has been wrong already before,
both emit_block_move and clear_storage store_expr uses are done with
the alias set computed for target by the caller.

2007-08-23  Jakub Jelinek  <jakub@redhat.com>

	* expr.c (store_expr): Optimize initialization of an array
	with STRING_CST.
	* expr.h (builtin_strncpy_read_str): New prototype.
	* builtins.c (builtin_strncpy_read_str): Remove prototype.
	No longer static.

--- gcc/expr.c.jj	2007-08-15 15:36:32.000000000 +0200
+++ gcc/expr.c	2007-08-23 21:51:57.000000000 +0200
@@ -4472,10 +4472,52 @@ store_expr (tree exp, rtx target, int ca
 
       return NULL_RTX;
     }
+  else if (TREE_CODE (exp) == STRING_CST
+	   && !nontemporal && !call_param_p
+	   && TREE_STRING_LENGTH (exp) > 0
+	   && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
+    {
+      /* Optimize initialization of an array with a STRING_CST.  */
+      HOST_WIDE_INT exp_len, str_copy_len;
+      rtx dest_mem;
+
+      exp_len = int_expr_size (exp);
+      if (exp_len <= 0)
+	goto normal_expr;
+
+      str_copy_len = strlen (TREE_STRING_POINTER (exp));
+      if (str_copy_len < TREE_STRING_LENGTH (exp) - 1)
+	goto normal_expr;
+
+      str_copy_len = TREE_STRING_LENGTH (exp);
+      if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
+	{
+	  str_copy_len += STORE_MAX_PIECES - 1;
+	  str_copy_len &= ~(STORE_MAX_PIECES - 1);
+	}
+      str_copy_len = MIN (str_copy_len, exp_len);
+      if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
+				(void *) TREE_STRING_POINTER (exp),
+				MEM_ALIGN (target)))
+	goto normal_expr;
+
+      dest_mem = target;
+
+      dest_mem = store_by_pieces (dest_mem,
+				  str_copy_len, builtin_strncpy_read_str,
+				  (void *) TREE_STRING_POINTER (exp),
+				  MEM_ALIGN (target),
+				  exp_len > str_copy_len ? 1 : 0);
+      if (exp_len > str_copy_len)
+	clear_storage (dest_mem, GEN_INT (exp_len - str_copy_len),
+		       BLOCK_OP_NORMAL);
+      return NULL_RTX;
+    }
   else
     {
       rtx tmp_target;
 
+  normal_expr:
       /* If we want to use a nontemporal store, force the value to
 	 register first.  */
       tmp_target = nontemporal ? NULL_RTX : target;
--- gcc/builtins.c.jj	2007-08-13 15:11:18.000000000 +0200
+++ gcc/builtins.c	2007-08-23 21:33:18.000000000 +0200
@@ -125,7 +125,6 @@ static rtx expand_builtin_bcopy (tree, i
 static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode);
 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode);
 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
-static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
 static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode);
 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
@@ -3773,7 +3772,7 @@ expand_builtin_stpcpy (tree exp, rtx tar
    bytes from constant string DATA + OFFSET and return it as target
    constant.  */
 
-static rtx
+rtx
 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
 			  enum machine_mode mode)
 {
--- gcc/expr.h.jj	2007-08-15 15:36:32.000000000 +0200
+++ gcc/expr.h	2007-08-23 21:33:46.000000000 +0200
@@ -341,6 +341,7 @@ extern void expand_builtin_setjmp_setup 
 extern void expand_builtin_setjmp_receiver (rtx);
 extern rtx expand_builtin_saveregs (void);
 extern void expand_builtin_trap (void);
+extern rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
 \f
 /* Functions from expr.c:  */
 

	Jakub

             reply	other threads:[~2007-08-23 21:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-23 23:48 Jakub Jelinek [this message]
2007-08-24  9:59 ` Richard Guenther
2007-08-24 13:42   ` Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070823212347.GC2063@devserv.devel.redhat.com \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).