Following testcase produces unnecessary moves in short loop: --cut here-- char table[256];
int test(void) { char val = 0; int i; for (i = 0; i < 10; i++) val += table[i]; return val; } --cut here-- gcc -O2: test: .LFB2: movzbl table(%rip), %ecx movl $1, %edx .p2align 4,,7 .L2: (*) movl %ecx, %eax addb table(%rdx), %al addq $1, %rdx cmpq $10, %rdx (*) movl %eax, %ecx jne .L2 movsbl %al,%eax ret Those unneccesary moves (*) are generated due to many (spurious?) casts in optimized tree dump: test () { long unsigned int ivtmp.31; char val; unsigned char D.1985; <bb 2>: val = (char) (unsigned char) MEM[symbol: table]; ivtmp.31 = 1; <L0>:; D.1985 = (unsigned char) val + (unsigned char) MEM[symbol: table, index: ivtmp.31]; val = (char) D.1985; ivtmp.31 = ivtmp.31 + 1; if (ivtmp.31 != 10) goto <L0>; else goto <L2>; <L2>:; return (int) val; -- Summary: Too much casting for char operands on tree level Product: gcc Version: 4.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: ubizjak at gmail dot com GCC build triplet: x86_64-pc-linux-gnu GCC host triplet: x86_64-pc-linux-gnu GCC target triplet: x86_64-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248