Bug 27095 - [4.1 Regression] O2 produces duplicate code
Summary: [4.1 Regression] O2 produces duplicate code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: 4.1.1
Assignee: Alan Modra
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2006-04-09 21:43 UTC by Benoît Dejean
Modified: 2006-06-03 18:30 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-04-12 07:12:49


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benoît Dejean 2006-04-09 21:43:05 UTC
Hi,

#include <string.h>

void fill(char *s, int i)
{
	memset(s, i, strlen(s));
}


4.1 produces two strlen calls

fill:
        mflr 0
        stwu 1,-32(1)
        stw 0,36(1)
        stw 28,16(1)
        mr 28,4
        stw 29,20(1)
        mr 29,3
        bl strlen
        mr 3,29
        bl strlen
        mr 4,28
        mr 5,3
        mr 3,29
        bl memset
        lwz 0,36(1)
        lwz 28,16(1)
        lwz 29,20(1)
        addi 1,1,32
        mtlr 0
        blr


where 4.0 outputs

fill:
        mflr 0
        stwu 1,-32(1)
        stw 0,36(1)
        stw 28,16(1)
        mr 28,3
        stw 29,20(1)
        mr 29,4
        bl strlen
        mr 4,29
        mr 5,3
        mr 3,28
        bl memset
        lwz 0,36(1)
        lwz 28,16(1)
        lwz 29,20(1)
        addi 1,1,32
        mtlr 0
        blr
Comment 1 Andrew Pinski 2006-04-09 21:49:06 UTC
Confirmed, also happens on powerpc-darwin and on the mainline still.
Comment 2 Richard Biener 2006-04-10 09:00:29 UTC
We go via expand_builtin_memset which expands strlen at

      len_rtx = expand_normal (len);

but then, expand via setmem fails, so we bail out

          else if (!set_storage_via_setmem(dest_mem, len_rtx, val_rtx,
                                           dest_align))
            return 0;

and end up doing regular

      return expand_call (exp, target, ignore);

which then expands strlen again.
Comment 3 Alan Modra 2006-04-12 07:12:49 UTC
Testing a fix
Comment 4 patchapp@dberlin.org 2006-04-13 14:25:21 UTC
Subject: Bug number PR27095

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-04/msg00494.html
Comment 5 Alan Modra 2006-04-14 03:20:24 UTC
Subject: Bug 27095

Author: amodra
Date: Fri Apr 14 03:20:21 2006
New Revision: 112948

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=112948
Log:
	PR middle-end/27095
	* builtins.c: (expand_builtin_memset): Stabilize args before expansion
	and emit libcall here in case the builtin fails.
	(expand_builtin_strcmp): Always emit the libcall here on failure.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/builtins.c

Comment 6 Richard Biener 2006-04-18 09:02:29 UTC
Fixed on the mainline.  Btw, this is really wrong-code.  Can you apply to the 4.1 branch, too, please?
Comment 7 Alan Modra 2006-04-18 12:34:11 UTC
Subject: Bug 27095

Author: amodra
Date: Tue Apr 18 12:34:07 2006
New Revision: 113030

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113030
Log:
	PR middle-end/27095
	* builtins.c: (expand_builtin_memset): Stabilize args before expansion
	and emit libcall here in case the builtin fails.
	(expand_builtin_strcmp): Always emit the libcall here on failure.


Modified:
    branches/gcc-4_1-branch/gcc/ChangeLog
    branches/gcc-4_1-branch/gcc/builtins.c

Comment 8 Alan Modra 2006-04-18 12:35:17 UTC
As Roger requested, I was waiting a few days to apply the patch on the 4.1 branch.
Comment 9 Alan Modra 2006-04-28 03:36:18 UTC
Subject: Bug 27095

Author: amodra
Date: Fri Apr 28 03:36:15 2006
New Revision: 113340

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113340
Log:
	PR middle-end/27095
	* gcc.dg/pr27095.c: New.


Added:
    trunk/gcc/testsuite/gcc.dg/pr27095.c
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 10 Andrew Pinski 2006-05-05 07:20:32 UTC
I should note that this testcase fails on darwin because strlen is mentioned twice in the testcase as there is a stub for strlen.
Comment 11 Alan Modra 2006-05-08 00:49:36 UTC
Testcase fixed by simply xfailing *-*-darwin*.  It might be possible to tweak the pattern we match to accommodate darwin, but I'll leave that to a darwin maintainer.
Comment 12 John David Anglin 2006-06-03 18:30:28 UTC
The testcase also fails on hppa64-hp-hpux11.11:

        b,l strlen,%r2
	   ...
        .type   strlen, @function

At the moment, it's a puzzle to me why this also isn't
failing on the 32-bit ports.