http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54579
Bug #: 54579 Summary: missed optimization: ASR idiom Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: rearn...@gcc.gnu.org The idiom int asr(int a, int b) { return a < 0 ? -(-a - 1 >> b) - 1 : a >> b; } is equivalent to a portable form of arithmetic right shift. However, at -O2 GCC currently generates something like cmp r0, #0 mvnlt r0, r0 mvnlt r0, r0, asr r1 movge r0, r0, asr r1 on ARM, or testl %edi, %edi movl %esi, %ecx js .L7 movl %edi, %eax sarl %cl, %eax ret .p2align 4,,7 .L7: movl %edi, %eax notl %eax sarl %cl, %eax notl %eax ret on x86. On ARM this should just be: asr r0, r0, r1