Bug 55299 - missed optimization: ASR idiom
Summary: missed optimization: ASR idiom
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.7.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: 19987
  Show dependency treegraph
 
Reported: 2012-11-12 23:22 UTC by olly
Modified: 2016-05-17 20:52 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-11-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description olly 2012-11-12 23:22:07 UTC
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.
Comment 1 H.J. Lu 2012-11-13 14:58:03 UTC
clang generates:

	movb	%sil, %cl
	sarl	%cl, %edi
	movl	%edi, %eax
	ret
Comment 2 olly 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.
Comment 3 Mikhail Maltsev 2016-05-17 20:50:54 UTC
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
Comment 4 Mikhail Maltsev 2016-05-17 20:52:21 UTC
Fixed for GCC 7.