http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57301
Bug ID: 57301 Summary: bit rotation is not optimized in c but not c++ Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: luto at mit dot edu It's well known [citation needed] that modern compilers are excellent at optimizing simple bit twiddling. Unfortunately, while gcc is, g++ is not. (See PR29749 for where gcc gained this ability.) Here's a trivial example borrowed from gcc/testsuite/gcc.dg/fold-rotate-1.c: unsigned short f1 (unsigned short a) { return a >> 8 | a << 8; } gcc -O2 (4.7 and some random 4.8 build) generates: f1: .LFB0: .cfi_startproc movl %edi, %eax rolw $8, %ax ret .cfi_endproc clang -O2 generates: f1: # @f1 .cfi_startproc # BB#0: rolw $8, %di movzwl %di, %eax ret (I suspect that these are equally good.) clang++ -O2 generates: _Z2f1t: # @_Z2f1t .cfi_startproc # BB#0: rolw $8, %di movzwl %di, %eax ret All unsurprising. But g++ -O2 generates: _Z2f1t: .LFB0: .cfi_startproc movzwl %di, %edi movl %edi, %eax sarl $8, %edi sall $8, %eax orl %edi, %eax ret .cfi_endproc Perhaps some decent subset of the gcc testsuite should also be built with the c++ frontend to make sure it still passes...