Bug 8613 - [3.2/3.3/3.4 regression] -O2 optimization generates wrong code
Summary: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 3.1.x/3.2.x
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-11-17 05:46 UTC by mark.odonohue
Modified: 2004-08-25 17:25 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test1.cpp (337 bytes, application/octet-stream)
2003-05-21 15:17 UTC, mark.odonohue
Details
test1.c (302 bytes, application/octet-stream)
2003-05-21 15:17 UTC, mark.odonohue
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mark.odonohue 2002-11-17 05:46:06 UTC
In the provided code, 

*dpb++ = strlen(userName)

dpb is incorrectly incremented by two bytes, not one.

The output buffer should result in:
1, 35, 6, 'sysdba'.

Instead it results in:
1, 35, 6, x, 'sysdba'

where x is random.

Tracing in gdb shows that dpb is incremented by a value of two.

Release:
gcc 3.2

Environment:
mandrake linux 9.0

How-To-Repeat:
g++ -O2 -march=i686 -fPIC test1.cpp
./a.out 1
Comment 1 mark.odonohue 2002-11-17 05:46:06 UTC
Fix:
 -O1 seems to work.
Comment 2 mark.odonohue 2002-11-18 12:28:45 UTC
From: "Mark O'Donohue" <mark.odonohue@cytopia.com.au>
To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, mark.odonohue@cytopia.com.au,
        gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: optimization/8613: -O3 optimisation of ++ generates wrong code
Date: Mon, 18 Nov 2002 12:28:45 +1100

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8613
 
 
 confirmed on redhat 8.0
 
 
 Also it seem tied up with i686 architecture.
 
 Since on redhat 8.0 gcc 3.2:
 
 g++ -O2 test1.cpp
 
 works:
 
 and on mandrak9.0 gcc 3.2
 
 g++ -O2 -march=i386 test1.cpp
 
 also works.
 
 
 Mark
 
Comment 3 Eric Botcazou 2003-02-18 14:35:38 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed on all versions since gcc 3.0: simply compile with -O2 -mcpu=[456]86. Simplified C testcase attached.
Comment 4 glen 2003-02-18 23:10:09 UTC
From: Glen Nakamura <glen@imodulo.com>
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  <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);
  

Comment 5 glen 2003-02-20 09:39:43 UTC
From: Glen Nakamura <glen@imodulo.com>
To: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Cc:  
Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code
Date: Thu, 20 Feb 2003 09:39:43 -1000

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8613
 
 Someone with write access should checkin this patch:
 http://gcc.gnu.org/ml/gcc-patches/2003-02/msg01531.html
 
 Patch approval is here:
 http://gcc.gnu.org/ml/gcc-patches/2003-02/msg01578.html
 
 FYI, I've only regtested this patch on the 3.3 branch.
 

Comment 6 Eric Botcazou 2003-02-20 20:56:39 UTC
From: Eric Botcazou <ebotcazou@libertysurf.fr>
To: Glen Nakamura <glen@imodulo.com>
Cc: gcc-gnats@gcc.gnu.org,
 gcc-bugs@gcc.gnu.org
Subject: Re: optimization/8613: [3.2/3.3/3.4 regression] -O2 optimization generates wrong code
Date: Thu, 20 Feb 2003 20:56:39 +0100

 > Someone with write access should checkin this patch:
 > http://gcc.gnu.org/ml/gcc-patches/2003-02/msg01531.html
 
 I'll do.
 
 > FYI, I've only regtested this patch on the 3.3 branch.
 
 I'm going to bootstrap/regtest it on the 3.2 branch and commit it there too
 if everything goes well.
 
 -- 
 Eric Botcazou

Comment 7 Eric Botcazou 2003-02-21 08:07:25 UTC
From: ebotcazou@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: optimization/8613
Date: 21 Feb 2003 08:07:25 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	ebotcazou@gcc.gnu.org	2003-02-21 08:07:25
 
 Modified files:
 	gcc            : ChangeLog builtins.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.c-torture/execute: 20030221-1.c 
 
 Log message:
 	PR optimization/8613
 	* builtins.c (expand_builtin): Emit postincrements before expanding
 	builtin functions.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.16804&r2=1.16805
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/builtins.c.diff?cvsroot=gcc&r1=1.177&r2=1.178
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2427&r2=1.2428
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030221-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
 

Comment 8 Eric Botcazou 2003-02-21 08:13:01 UTC
From: ebotcazou@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: optimization/8613
Date: 21 Feb 2003 08:13:01 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	ebotcazou@gcc.gnu.org	2003-02-21 08:13:01
 
 Modified files:
 	gcc            : ChangeLog builtins.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.c-torture/execute: 20030221-1.c 
 
 Log message:
 	PR optimization/8613
 	* builtins.c (expand_builtin): Emit postincrements before expanding
 	builtin functions.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.211&r2=1.16114.2.212
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/builtins.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.165&r2=1.165.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.53&r2=1.2261.2.54
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030221-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 

Comment 10 Eric Botcazou 2003-02-21 08:34:56 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed.