http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60641
Bug ID: 60641 Summary: Converting ushort to offset on x86_64 generates double movzwl Product: gcc Version: 4.7.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: akruppa at gmail dot com The following test cases produce sub-optimal assembly output. This was verified with these three versions of gcc: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 gcc (SUSE Linux) 4.7.3 gcc (Debian 4.8.2-16) 4.8.2 #include <stdlib.h> unsigned short foo (const unsigned short *start, const unsigned char *mask) { unsigned short r = 0; unsigned short ux = *start; if (mask[ux]) r = ux; return r; } void bar(unsigned int *s, unsigned short a) { s[a] = a; } Compile with, e.g., gcc -std=c99 -g -W -Wall -O3 -c movzwl.c The effect also occurs when using -O2 instead. The foo() function contains: 0x0000000000000000 <+0>: movzwl (%rdi),%edx 0x0000000000000003 <+3>: xor %eax,%eax 0x0000000000000005 <+5>: movzwl %dx,%ecx 0x0000000000000008 <+8>: cmpb $0x0,(%rsi,%rcx,1) The foo() function is: 0x0000000000000010 <+0>: movzwl %si,%eax 0x0000000000000013 <+3>: movzwl %si,%esi 0x0000000000000016 <+6>: mov %esi,(%rdi,%rax,4) 0x0000000000000019 <+9>: retq In both cases, an unnecessary movzwl instruction is generated. This may be the same issue as #36873, which was rejected because the test cases used volatile accesses. These test cases here show that the duplicate movzwl occurs without volatile as well.