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.
clang generates: movb %sil, %cl sarl %cl, %edi movl %edi, %eax ret
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.
Author: miyuki Date: Tue May 17 20:50:22 2016 New Revision: 236344 URL: https://gcc.gnu.org/viewcvs?rev=236344&root=gcc&view=rev Log: Fold bit_not through ASR and rotate gcc/ PR tree-optimization/54579 PR middle-end/55299 * match.pd (~(~X >> Y), ~(~X >>r Y), ~(~X <<r Y)): New patterns. gcc/testsuite PR tree-optimization/54579 PR middle-end/55299 * gcc.dg/fold-notrotate-1.c: New test. * gcc.dg/fold-notshift-1.c: New test. * gcc.dg/fold-notshift-2.c: New test. Added: trunk/gcc/testsuite/gcc.dg/fold-notrotate-1.c trunk/gcc/testsuite/gcc.dg/fold-notshift-1.c trunk/gcc/testsuite/gcc.dg/fold-notshift-2.c Modified: trunk/gcc/ChangeLog trunk/gcc/match.pd trunk/gcc/testsuite/ChangeLog
Fixed for GCC 7.