Here's my test with both dynamic and non-dynamic arrays:
Code:
#include <stdlib.h>
#define SIZE 10
int main(void)
{
int i;
double *A, B[SIZE];
A = malloc(SIZE * sizeof(*A));
for (i = 0; i < SIZE; ++i) {
A[i] = A[i];
}
free(A);
for (i = 0; i < SIZE; ++i) {
B[i] = B[i];
}
return 0;
}
After running gcc -Wall -O3 -S optimize.c, I get
Code:
.file "optimize.c"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $80, (%esp)
call malloc
movl %eax, (%esp)
call free
xorl %eax, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.5 20101112 (Red Hat 4.4.5-2)"
.section .note.GNU-stack,"",@progbits
No loop in there. How about -O2:
Code:
.file "optimize.c"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $80, (%esp)
call malloc
movl %eax, %edx
leal 80(%eax), %ecx
.p2align 4,,7
.p2align 3
.L2:
addl $8, %edx
cmpl %ecx, %edx
jne .L2
movl %eax, (%esp)
call free
xorl %eax, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.5 20101112 (Red Hat 4.4.5-2)"
.section .note.GNU-stack,"",@progbits
There it is -- at least, in part. GCC optimizes out the A[i] = A[i] even at -O1, but leaves the loop itself in there until you get to -O3. Note that the B loop is completely removed even at -O1. Fair enough for gcc to optimize general pointers or dynamically allocated memory differently than statically defined arrays, though both loops clearly do nothing. What version of GCC are you running? Can you post the complete code and exact command you used to compile?
From man gcc on my system:
Not all optimizations are controlled directly by a flag. Only optimizations that have a flag are listed.
...
-O3 Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning,
-fgcse-after-reload, -ftree-vectorize and -fipa-cp-clone options.
I compiled the code with -O2 and individually tried each of those -O3 options. None of them eliminated the loop entirely. I can only assume there's some other optimization happening at -O3 that lets gcc eliminate loops involving pointers. If I have time, I might look into GCC's optimizations more.