Bug 47270 - [4.7/4.8/4.9 Regression] GCC produces unnecessary code on -O2 and -O3 levels
Summary: [4.7/4.8/4.9 Regression] GCC produces unnecessary code on -O2 and -O3 levels
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.6.0
: P4 normal
Target Milestone: 4.7.4
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2011-01-12 15:45 UTC by Dmitry Gorbachev
Modified: 2013-12-24 23:59 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2013-03-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Gorbachev 2011-01-12 15:45:42 UTC
$ cat > testcase.c
register int r asm("esi");

void foo(void)
{
    if (r)
        __asm__("sar\t%0" : "+r" (r));

    __asm__("sar\t%0" : "+r" (r));
}
^D
$ gcc -O2 -S testcase.c
$ cat testcase.s
[...]
foo:
.LFB0:
        xorl    %eax, %eax
        testl   %esi, %esi
        je      .L2
#APP
# 6 "testcase.c" 1
        sar     %esi
# 0 "" 2
#NO_APP
        movl    %esi, %eax
.L2:
        movl    %eax, %esi
#APP
# 8 "testcase.c" 1
        sar     %esi
# 0 "" 2
#NO_APP
        ret
[...]
Comment 1 Jakub Jelinek 2011-01-17 12:06:11 UTC
Why do you think it is wrong?  If %esi is non-zero upon entry, then it is just moved to %eax and back, so it isn't changed, if it was zero upon entry, then 0 is loaded into it.
The reason for the extra code is that pre attempts to optimize it (register vars are not SSA vars), so we get at *.optimized:
  int prephitmp.4;
  int r.0;

<bb 2>:
  r.0_1 = r;
  if (r.0_1 != 0)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  __asm__("sar  %0" : "=r" r : "0" r.0_1);
  prephitmp.4_2 = r;

<bb 4>:
  # prephitmp.4_8 = PHI <0(2), prephitmp.4_2(3)>
  __asm__("sar  %0" : "=r" r : "0" prephitmp.4_8);
and RTL optimizations aren't able to undo that.  I doubt anything can be done about this easily, with -fno-tree-pre you get your expected output.
Comment 2 Dmitry Gorbachev 2011-01-26 18:03:33 UTC
Ah, yes; changed the summary.
Comment 3 Jakub Jelinek 2012-03-13 12:47:27 UTC
4.4 branch is being closed, moving to 4.5.4 target.
Comment 4 Steven Bosscher 2013-03-05 23:28:58 UTC
(In reply to comment #1)
> and RTL optimizations aren't able to undo that.  I doubt anything can
> be done about this easily.

Perhaps tree-ssa-uncprop should handle this.

FWIW, I've always thought that constant propagation into PHI arguments
was a mistake...
Comment 5 Jakub Jelinek 2013-04-12 15:16:51 UTC
GCC 4.6.4 has been released and the branch has been closed.
Comment 6 Dmitry Gorbachev 2013-04-12 20:34:31 UTC
I can't reproduce the bug with GCC 4.7.3 and 4.8.1.
Comment 7 Steven Bosscher 2013-12-24 23:59:56 UTC
Apparently fixed (probably by IRA).