http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47186
Summary: -O2 moves invariant address load INTO loop Product: gcc Version: 4.5.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassig...@gcc.gnu.org ReportedBy: gcc.h...@gmail.com Host: Fedora 14 Target: i?86-*-* Build: 4.5.1 20100924 (Red Hat 4.5.1-4) gcc -O1 try.c -S -masm=intel -o try.s In the example below, the address load to esi for the movsd is moved into the loop when -O2 is used. With -O1 its outside the loop. Perhaps this is something to do with scheduling? I tried with various x86 targets and it always happened, although the position of the address load within the loop changed between atom and core2 for example. -------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> int mike[100], joe[100]; int main( int argc, char *argv[] ) { int i; for( i = 0; i < 100; ++i ) mike[i] = rand(); memcpy( joe, mike, sizeof(joe) ); } -------------------------------------- Compile with -O1 produces ... .L2: call rand mov DWORD PTR mike[0+ebx*4], eax add ebx, 1 cmp ebx, 100 jne .L2 mov edi, OFFSET FLAT:joe mov esi, OFFSET FLAT:mike mov ecx, 100 rep movsd Now repeat with -O2, and esi is loaded within the loop .L2: call rand mov esi, OFFSET FLAT:mike mov DWORD PTR mike[0+ebx*4], eax add ebx, 1 cmp ebx, 100 jne .L2 mov ecx, ebx mov edi, OFFSET FLAT:joe rep movsd