optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code

Glen Nakamura glen@imodulo.com
Wed Feb 19 09:10:00 GMT 2003


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  <glen@imodulo.com>

	* 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);
 



More information about the Gcc-bugs mailing list