llvm-gcc loop unrolling

What component is responsible for loop unrolling, llvm-gcc, or some LLVM pass after GCC? When compiling this function:

void printArray( int array[16] ) {
  for( int i = 0; i < 16; ++ i ) {
    printf( "%d\n", array[i] );
  }
}

llvm-gcc chooses to unroll this loop, and I don't think it should. It doesn't unroll the loop if there are 17 iterations, only 16. It also produces this output even when compiling with -Os, which is supposed to turn off optimizations like this.

Wouldn't it be better to leave the loop unrolling to some later optimization pass? This way, future passes can easily recognize that this is in fact a loop.

Evan Jones

What component is responsible for loop unrolling, llvm-gcc, or some LLVM pass after GCC? When compiling this function:

the llvm-gcc front-end does no non-trivial optimization. This is performed by the llvm -loop-unroll pass.

void printArray( int array[16] ) {
  for( int i = 0; i < 16; ++ i ) {
    printf( "%d\n", array[i] );
  }
}

llvm-gcc chooses to unroll this loop, and I don't think it should. It doesn't unroll the loop if there are 17 iterations, only 16. It also produces this output even when compiling with -Os, which is supposed to turn off optimizations like this.

Currently, -Os is ignored.

Wouldn't it be better to leave the loop unrolling to some later optimization pass? This way, future passes can easily recognize that this is in fact a loop.

I'm not sure what you mean here. Before the loop-unroll pass runs, the loop is not unrolled. If you don't want this behavior, you can hack tools/gccas/gccas.cpp to not run the loop unroll pass.

-Chris