In the test case below, both stores are done. The compiler chould eliminate the first loop completely.
$ cat a.c void foo(int *a, int n) { int i; for (i=0; i<n; i++) a[i] = 0; for (i=0; i<n; i++) a[i] = 1; } $ gcc -O3 -S a.c $ cat a.s .file "a.c" .text .p2align 4,,15 .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp movl 12(%ebp), %edx movl 8(%ebp), %ecx testl %edx, %edx jle .L5 xorl %eax, %eax .p2align 4,,7 .p2align 3 .L3: movl $0, (%ecx,%eax,4) addl $1, %eax cmpl %edx, %eax jne .L3 xorl %eax, %eax .p2align 4,,7 .p2align 3 .L4: movl $1, (%ecx,%eax,4) addl $1, %eax cmpl %edx, %eax jne .L4 .L5: popl %ebp ret .size foo, .-foo .ident "GCC: (GNU) 4.4.0 20090124 (experimental)" .section .note.GNU-stack,"",@progbits -- Summary: writing arrays twice not optimized Product: gcc Version: 4.4.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: tkoenig at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39052