array[(x>>2)&3] can be arranged to avoid the right shift. consider:
% cat shift-and-fold.c unsigned array[4]; unsigned foo(unsigned long x) { return array[(x>>2)&3ul]; } % /home/odo/gcc/bin/gcc -g -O3 -Wall -c -o shift-and-fold.o shift-and-fold.c % objdump -dr shift-and-fold.o shift-and-fold.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <foo>: 0: 48 c1 ef 02 shr $0x2,%rdi 4: 83 e7 03 and $0x3,%edi 7: 8b 04 bd 00 00 00 00 mov 0x0(,%rdi,4),%eax a: R_X86_64_32S array e: c3 retq could be: and $0xc,%edi mov 0(%rdi),%eax gcc does the right thing when the left shift isn't part of an address... thanks -dean % /home/odo/gcc/bin/gcc -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../gcc/configure --prefix=/home/odo/gcc --host=x86_64-linux-gnu --disable-multilib --enable-languages=c,c++ --disable-bootstrap Thread model: posix gcc version 4.2.0 20060603 (experimental) -- Summary: eliminate shift in array[(x>>2)&3] Product: gcc Version: 4.2.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: pending AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: dean at arctic dot org GCC build triplet: x86_64-linux-gnu GCC host triplet: x86_64-linux-gnu GCC target triplet: x86_64-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27971