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: unassig...@gcc.gnu.org ReportedBy: o...@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.