gcc --version gcc (GCC) 4.4.1 20090507 (prerelease) The following test compiled with gcc -S -Os
struct struct_t { int* data; }; void testAddr (struct struct_t* sp, int len) { int i; for (i = 0; i < len; i++) { sp->data[i] = 0; } } generates the following code for x86 testAddr : pushl %ebp xorl %eax, %eax movl %esp, %ebp movl 8(%ebp), %ecx pushl %ebx movl 12(%ebp), %edx jmp .L2 .L3: movl (%ecx), %ebx <-- invariant address load movl $0, (%ebx,%eax,4) incl %eax .L2: cmpl %edx, %eax jl .L3 popl %ebx popl %ebp ret Whereas making the intent explicit like so void testAddr (struct struct_t* sp, int len) { int i; int *p = sp->data; for (i = 0; i < len; i++) { p[i] = 0; } } generates testAddr : pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %ecx movl (%eax), %edx <-- now outside the loop xorl %eax, %eax jmp .L2 .L3: movl $0, (%edx,%eax,4) incl %eax .L2: cmpl %ecx, %eax jl .L3 popl %ebp ret Why can't we move the address load outside the loop in the first case? -- Summary: invariant address load inside loop Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: rahul at icerasemi dot com GCC build triplet: i686-pc-linux GCC host triplet: i686-pc-linux GCC target triplet: i686-pc-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41026