The correct way to loopback using gcc

#include <stdio.h> int main() { int i; for(i=0;i<10000;i++){ printf("%d",i); } } 

I want to do a loop unwrap on this code using gcc but even using a flag.

 gcc -O2 -funroll-all-loops --save-temps unroll.c 

the compiled code that I get contains a loop of 10,000 iterations

 _main: Leh_func_begin1: pushq %rbp Ltmp0: movq %rsp, %rbp Ltmp1: pushq %r14 pushq %rbx Ltmp2: xorl %ebx, %ebx leaq L_.str(%rip), %r14 .align 4, 0x90 LBB1_1: xorb %al, %al movq %r14, %rdi movl %ebx, %esi callq _printf incl %ebx cmpl $10000, %ebx jne LBB1_1 popq %rbx popq %r14 popq %rbp ret Leh_func_end1: 

Can somone plz tell me how to properly execute a loop unfolding in gcc

+7
optimization with gcc loop-unrolling
source share
3 answers

The Loop expansion will not do you any good for this code, because the overhead of calling the printf() function itself dominates the work performed on each iteration. The compiler may be aware of this, and since he is asked to optimize the code, he may decide that the deployment increases the size of the code without a noticeable increase in performance at runtime and decides that the risk of an instruction cache error is too high to complete the turn.

The type of deployment required to speed up this loop will require a reduction in the number of calls to printf() . I do not know any optimizing compiler capable of doing this.

As an example of a loop unfolding to reduce the number of printf() calls, consider this code:

 void print_loop_unrolled (int n) { int i = -8; if (n % 8) { printf("%.*s", n % 8, "01234567"); i += n % 8; } while ((i += 8) < n) { printf("%d%d%d%d%d%d%d%d",i,i+1,i+2,i+3,i+4,i+5,i+6,i+7); } } 
+7
source share

gcc has maximum loop reversal options.

You should use -O3 -funroll-loops and play with the parameters max-unroll-times , max-unrolled-insns and max-average-unrolled-insns .

Example:

 -O3 -funroll-loops --param max-unroll-times=200 
+6
source share

Replace

  printf("%d",i); 

from

  volatile int j = i; 

and see if the loop will be deployed.

+1
source share

All Articles