In the gcc 4.4.6 docs it is stated:
-funroll-all-loops: Unroll all loops, even if their number of iterations is uncertain when the loop isentered.
I am compiling this code:
int unroll(){
int i = 0;
int array[1000];
do {
use(i,array);
i++;
}while(i<1000);
return(0);
}
void use(int i, int *array){
int x = i*5;
array[i] = x;
}
…once with the funroll-all-loops optimizations, once without:
OPT = -funroll-all-loops
NOOPT = -O0
Then I use diff to compare the assembly code of each (produced using -S -fverbose-asm).
The produced code is identical.
Tried changing the loop to a do while;
adjusting the loop counter (up to 100);
changing the statements within the loop body.
What could I be missing? Why is this loop not being unrolled?
Update
Nikos C suggested to raise the loop enroll parameter using --param max-unroll-times=N where N is the upper limit.
While that was a sensible suggestion, it did not change behaviour.
I also lowered the loop iterations to only 10.
Also updated the code to actually ‘do’ something, no change.
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
If I translate the snippet (changed slightly to an extern function to avoid any inlining and dead code elimination)
with
gcc -O2 -funroll-all-loops test.c -o test2.o -c, the resulting object codeis unrolled eight times: