From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3951 invoked by alias); 23 Aug 2007 21:23:58 -0000 Received: (qmail 3758 invoked by uid 22791); 23 Aug 2007 21:23:57 -0000 X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (66.187.233.31) by sourceware.org (qpsmtpd/0.31) with ESMTP; Thu, 23 Aug 2007 21:23:52 +0000 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.13.1/8.13.1) with ESMTP id l7NLNmKZ029815 for ; Thu, 23 Aug 2007 17:23:48 -0400 Received: from devserv.devel.redhat.com (devserv.devel.redhat.com [172.16.58.1]) by int-mx1.corp.redhat.com (8.13.1/8.13.1) with ESMTP id l7NLNmks011153 for ; Thu, 23 Aug 2007 17:23:48 -0400 Received: from devserv.devel.redhat.com (localhost.localdomain [127.0.0.1]) by devserv.devel.redhat.com (8.12.11.20060308/8.12.11) with ESMTP id l7NLNmJ7008441 for ; Thu, 23 Aug 2007 17:23:48 -0400 Received: (from jakub@localhost) by devserv.devel.redhat.com (8.12.11.20060308/8.12.11/Submit) id l7NLNlLg008439 for gcc-patches@gcc.gnu.org; Thu, 23 Aug 2007 17:23:48 -0400 Date: Thu, 23 Aug 2007 23:48:00 -0000 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [PATCH] Optimize var = STRING_CST Message-ID: <20070823212347.GC2063@devserv.devel.redhat.com> Reply-To: Jakub Jelinek Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.1i X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org X-SW-Source: 2007-08/txt/msg01591.txt.bz2 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 * 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); /* Functions from expr.c: */ Jakub