Because you have disabled all other optimizations in your "OPT" case, you tell to compiler to unroll all loops and then deny him the means to do so, like loop induction etc. Try
NOOPT = -O2
OPT = -O2 -funroll-all-loops
If I translate the snippet (changed slightly to an extern function to avoid any inlining and dead code elimination)
void use(int i, int *array);
int unroll(){
int i = 0;
int array[1000];
do {
use(i,array);
i++;
}while(i<1000);
return(0);
}
with gcc -O2 -funroll-all-loops test.c -o test2.o -c
, the resulting object code
is unrolled eight times:
0000000000000000 <unroll>:
0: 53 push %rbx
1: 31 db xor %ebx,%ebx
3: 48 81 ec a0 0f 00 00 sub $0xfa0,%rsp
a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
10: 89 df mov %ebx,%edi
12: 48 89 e6 mov %rsp,%rsi
15: e8 00 00 00 00 callq 1a <unroll+0x1a>
1a: 8d 7b 01 lea 0x1(%rbx),%edi
1d: 48 89 e6 mov %rsp,%rsi
20: e8 00 00 00 00 callq 25 <unroll+0x25>
25: 8d 7b 02 lea 0x2(%rbx),%edi
28: 48 89 e6 mov %rsp,%rsi
2b: e8 00 00 00 00 callq 30 <unroll+0x30>
30: 8d 7b 03 lea 0x3(%rbx),%edi
33: 48 89 e6 mov %rsp,%rsi
36: e8 00 00 00 00 callq 3b <unroll+0x3b>
3b: 8d 7b 04 lea 0x4(%rbx),%edi
3e: 48 89 e6 mov %rsp,%rsi
41: e8 00 00 00 00 callq 46 <unroll+0x46>
46: 8d 7b 05 lea 0x5(%rbx),%edi
49: 48 89 e6 mov %rsp,%rsi
4c: e8 00 00 00 00 callq 51 <unroll+0x51>
51: 8d 7b 06 lea 0x6(%rbx),%edi
54: 48 89 e6 mov %rsp,%rsi
57: e8 00 00 00 00 callq 5c <unroll+0x5c>
5c: 8d 7b 07 lea 0x7(%rbx),%edi
5f: 48 89 e6 mov %rsp,%rsi
62: 83 c3 08 add $0x8,%ebx
65: e8 00 00 00 00 callq 6a <unroll+0x6a>
6a: 81 fb e8 03 00 00 cmp $0x3e8,%ebx
70: 75 9e jne 10 <unroll+0x10>
72: 48 81 c4 a0 0f 00 00 add $0xfa0,%rsp
79: 31 c0 xor %eax,%eax
7b: 5b pop %rbx
7c: c3 retq