Bug 38751 - [4.3 Regression] odd performance regression with -Os
Summary: [4.3 Regression] odd performance regression with -Os
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P2 normal
Target Milestone: 4.3.4
Assignee: Richard Biener
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2009-01-07 10:03 UTC by Andrew Haley
Modified: 2009-06-25 12:39 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.4.0
Known to fail: 4.3.3
Last reconfirmed: 2009-04-22 15:20:44


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Haley 2009-01-07 10:03:58 UTC
Here's a strange case of poor code generation with -Os.

unsigned short foo2 (unsigned char on_off, unsigned short *puls)
{
  return puls[on_off-1];
}

With -O2, it's fine:

	movzbl	%dil, %edi
	movzwl	-2(%rsi,%rdi,2), %eax
	ret

With -Os it's really weird:

	movzbl	%dil, %eax
	movl	$1, %edi
	subq	%rax, %rdi
	imulq	$-2, %rdi, %rdi
	movw	(%rdi,%rsi), %ax
	ret

The problem here seems to be that the FE (or the GIMPLE generation)
transforms

        addr + ((on_off-1)*2)

into

        addr + ((1-on_off)*-2)

and nothing in -Os has the wit to recover.  This badly affects targets
with no hardware multiplier, which end up calling libgcc to do the
multiply.

http://gcc.gnu.org/ml/gcc/2008-12/msg00329.html
Comment 1 Eric Botcazou 2009-01-07 10:11:37 UTC
Fallout of the change made for PR middle-end/23294 in GCC 4.2.
Comment 2 Richard Biener 2009-01-07 10:53:41 UTC
Subject: Bug 38751

Author: rguenth
Date: Wed Jan  7 10:53:30 2009
New Revision: 143152

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=143152
Log:
2009-01-07  Richard Guenther  <rguenther@suse.de>

	PR middle-end/38751
	* fold-const.c (extract_muldiv): Remove obsolete comment.
	(fold_plusminus_mult_expr): Undo MINUS_EXPR
	to PLUS_EXPR canonicalization for the canonicalization.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c

Comment 3 Jakub Jelinek 2009-01-15 13:57:03 UTC
Fixed on the trunk:
-O2:
        movzbl  %dil, %edi
        movzwl  -2(%rsi,%rdi,2), %eax
        ret
-Os:
        movzbl  %dil, %edi
        movw    -2(%rsi,%rdi,2), %ax
        ret
Comment 4 Joseph S. Myers 2009-03-31 21:06:27 UTC
Closing 4.2 branch.
Comment 5 Richard Biener 2009-06-25 12:39:11 UTC
Fixed.
Comment 6 Richard Biener 2009-06-25 12:39:18 UTC
Subject: Bug 38751

Author: rguenth
Date: Thu Jun 25 12:39:01 2009
New Revision: 148943

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148943
Log:
2009-06-25  Richard Guenther  <rguenther@suse.de>

	Backport from mainline
	2009-01-07  Richard Guenther  <rguenther@suse.de>

	PR middle-end/38751
	* fold-const.c (extract_muldiv): Remove obsolete comment.
	(fold_plusminus_mult_expr): Undo MINUS_EXPR
	to PLUS_EXPR canonicalization for the canonicalization.

Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/fold-const.c