From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6854 invoked by alias); 19 Feb 2003 09:16:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 6839 invoked by uid 71); 19 Feb 2003 09:16:01 -0000 Date: Wed, 19 Feb 2003 09:16:00 -0000 Message-ID: <20030219091601.6838.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: Glen Nakamura Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code Reply-To: Glen Nakamura X-SW-Source: 2003-02/txt/msg00884.txt.bz2 List-Id: The following reply was made to PR optimization/8613; it has been noted by GNATS. From: Glen Nakamura To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org, mark.odonohue@cytopia.com.au Cc: Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code Date: Tue, 18 Feb 2003 23:10:09 -1000 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8613 I've tracked this bug down to a problem with postincrements and the strlen builtin function. What happens is that the postincrement code isn't emitted before the builtin strlen function is expanded and gets emitted within the loop created by ix86_expand_strlensi_unroll_1. This causes the postincrement code to be executed multiple times when calculating the string length. The fix is to call emit_queue before expanding the strlen builtin so the postincrement code is emitted outside of the loop. I'm not exactly sure where the emit_queue call should be placed. Although adding emit_queue to expand_builtin_strlen would work, I added it to expand_builtin instead, because other builtins may have the same problem. It passed regression testing on i686-pc-linux-gnu for the 3.3 branch, but a more experienced developer should decide where the best place is. Here is a reduced testcase: extern void abort (void); int main () { char buf[16] = "1234567890"; char *p = buf; *p++ = (char) __builtin_strlen (buf); if ((buf[0] != 10) || (p - buf != 1)) abort (); } And here is the patch: 2003-02-19 Glen Nakamura * builtins.c (expand_builtin): Emit postincrements before expanding builtin functions. diff -Nru3p gcc-3.3.orig/gcc/builtins.c gcc-3.3/gcc/builtins.c --- gcc-3.3.orig/gcc/builtins.c 2002-12-01 17:51:43.000000000 +0000 +++ gcc-3.3/gcc/builtins.c 2002-12-01 17:51:43.000000000 +0000 @@ -3691,6 +3691,9 @@ expand_builtin (exp, target, subtarget, tree arglist = TREE_OPERAND (exp, 1); enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); + /* Perform postincrements before expanding builtin functions. */ + emit_queue (); + if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore);