public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/55299] New: missed optimization: ASR idiom
@ 2012-11-12 23:22 olly at survex dot com
  2012-11-13 14:58 ` [Bug c/55299] " hjl.tools at gmail dot com
  2012-11-13 20:40 ` olly at survex dot com
  0 siblings, 2 replies; 3+ messages in thread
From: olly at survex dot com @ 2012-11-12 23:22 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55299

             Bug #: 55299
           Summary: missed optimization: ASR idiom
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: olly@survex.com


This is similar to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54579 but for a
different (and perhaps clearer) idiom for arithmetic shift right.  I've filed
this as a separate PR rather than adding it as a comment, but if it's actually
the same issue underneath, please merge or mark as a duplicate.

int asr(int a, int b)
{
  return a < 0 ? ~((~a) >> b) : a >> b;
}

olly@gcc12:~$ /opt/cfarm/gcc-latest/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/opt/cfarm/gcc-latest/bin/gcc
COLLECT_LTO_WRAPPER=/home/iulius/autobuild/bin/gcc-4.7.1/libexec/gcc/x86_64-unknown-linux-gnu/4.7.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.7.1-src/configure
--prefix=/home/iulius/autobuild/bin/gcc-4.7.1
--with-gmp=/home/iulius/autobuild/bin/gmp-5.0.5
--with-mpfr=/home/iulius/autobuild/bin/mpfr-3.1.0
--with-mpc=/home/iulius/autobuild/bin/mpc-0.9 --disable-nls
--enable-threads=posix --disable-multilib --enable-languages=c,c++
Thread model: posix
gcc version 4.7.1 (GCC) 

Compiling with:

LD_LIBRARY_PATH=/opt/cfarm/mpc-latest/lib:/opt/cfarm/mpfr-latest/lib:/opt/cfarm/gmp-latest/lib
/opt/cfarm/gcc-latest/bin/gcc -O2 -S asr.c -o asr.S

Gives this for the function asr:

asr:
.LFB0:
    movl    %edi, %eax
    movl    %esi, %ecx
    sarl    %cl, %eax
    testl    %edi, %edi
    js    .L5
    rep
    ret
    .p2align 4,,10
    .p2align 3
.L5:
    movl    %edi, %eax
    notl    %eax
    sarl    %cl, %eax
    notl    %eax
    ret

Ideally the conditional would be optimised down to just the sarl instruction.

I've checked the older compiler versions on gcc12 (back to 4.1.1) and they all
leave the branch in.  So if this is a regression, it isn't at all recent.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/55299] missed optimization: ASR idiom
  2012-11-12 23:22 [Bug c/55299] New: missed optimization: ASR idiom olly at survex dot com
@ 2012-11-13 14:58 ` hjl.tools at gmail dot com
  2012-11-13 20:40 ` olly at survex dot com
  1 sibling, 0 replies; 3+ messages in thread
From: hjl.tools at gmail dot com @ 2012-11-13 14:58 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55299

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-11-13
                 CC|                            |areg.melikadamyan at gmail
                   |                            |dot com, hjl.tools at gmail
                   |                            |dot com
     Ever Confirmed|0                           |1

--- Comment #1 from H.J. Lu <hjl.tools at gmail dot com> 2012-11-13 14:58:03 UTC ---
clang generates:

    movb    %sil, %cl
    sarl    %cl, %edi
    movl    %edi, %eax
    ret


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/55299] missed optimization: ASR idiom
  2012-11-12 23:22 [Bug c/55299] New: missed optimization: ASR idiom olly at survex dot com
  2012-11-13 14:58 ` [Bug c/55299] " hjl.tools at gmail dot com
@ 2012-11-13 20:40 ` olly at survex dot com
  1 sibling, 0 replies; 3+ messages in thread
From: olly at survex dot com @ 2012-11-13 20:40 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55299

--- Comment #2 from olly at survex dot com 2012-11-13 20:40:06 UTC ---
Interestingly clang (at least 3.0-6 from Debian testing) fails to optimise the
same testcase but shifting by a constant.  For that variant case clang
generates essentially the same code as GCC currently does.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-11-13 20:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-12 23:22 [Bug c/55299] New: missed optimization: ASR idiom olly at survex dot com
2012-11-13 14:58 ` [Bug c/55299] " hjl.tools at gmail dot com
2012-11-13 20:40 ` olly at survex dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).